aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-09-06 10:38:33 -0700
committeralon@honor <none@none>2010-09-06 10:38:33 -0700
commitfb18f94135ce2354ee3a225ffc2d73341139b6a8 (patch)
treef5a7fefe68e4f03356d115ef614ee3b45b47dd9c /src
parent3109ce8f1c903653a982d44c471cf7f5bab975b1 (diff)
preprocessor + example use in SAFE_HEAP
Diffstat (limited to 'src')
-rw-r--r--src/parser.js46
-rw-r--r--src/preamble.js27
-rw-r--r--src/settings.js2
3 files changed, 68 insertions, 7 deletions
diff --git a/src/parser.js b/src/parser.js
index b9873175..19e073bc 100644
--- a/src/parser.js
+++ b/src/parser.js
@@ -6,10 +6,6 @@
* * Re-use variables (of the same kind, native/nativized vs. emulated).
*/
-// Options
-
-LINEDEBUG = 0;
-
// Prep - allow this to run in both SpiderMonkey and V8
if (!this['load']) {
@@ -26,6 +22,34 @@ load('snippets.js');
// Tools
+// Simple #if/else/endif preprocessing for a file. Checks if the
+// ident checked is true in our global.
+function preprocess(text) {
+ var lines = text.split('\n');
+ var ret = '';
+ var show = true;
+ for (var i = 0; i < lines.length; i++) {
+ var line = lines[i];
+ if (line[0] != '#') {
+ if (show) {
+ ret += line + '\n';
+ }
+ } else {
+ if (line[1] == 'i') { // if
+ var ident = line.substr(4);
+ show = !!this[ident];
+ } else if (line[2] == 'l') { // else
+ show = !show;
+ } else if (line[2] == 'n') { // endif
+ show = true;
+ } else {
+ throw "Unclear preprocessor command: " + line;
+ }
+ }
+ }
+ return ret;
+}
+
function addPointing(type) { return type + '*' }
function removePointing(type, num) {
if (num === 0) return type;
@@ -1829,7 +1853,11 @@ function JSify(data) {
}
function makeSetValue(ptr, pos, value, noNeedFirst) {
- return makeGetSlab(ptr) + '[' + (noNeedFirst ? '0' : makeGetPos(ptr)) + (pos ? ' + ' + pos : '') + '] = ' + value;
+ if (SAFE_HEAP) {
+ return 'SAFE_HEAP_STORE(' + (noNeedFirst ? '0' : makeGetPos(ptr)) + (pos ? ' + ' + pos : '') + ', ' + value + ')';
+ } else {
+ return makeGetSlab(ptr) + '[' + (noNeedFirst ? '0' : makeGetPos(ptr)) + (pos ? ' + ' + pos : '') + '] = ' + value;
+ }
}
function makeEmptyStruct(type) {
@@ -2390,7 +2418,11 @@ function JSify(data) {
// Special cases
if (ident == '_llvm_va_start') {
- return 'HEAP[' + params[0].ident + '] = Pointer_make(Array.prototype.slice.call(arguments, 1).concat([0]), 0)'; // XXX 1
+ if (SAFE_HEAP) {
+ return 'SAFE_HEAP_STORE(' + params[0].ident + ', Pointer_make(Array.prototype.slice.call(arguments, 1).concat([0]), 0))';
+ } else {
+ return 'HEAP[' + params[0].ident + '] = Pointer_make(Array.prototype.slice.call(arguments, 1).concat([0]), 0)'; // XXX 1
+ }
} else if (ident == '_llvm_va_end') {
return ';'
}
@@ -2438,7 +2470,7 @@ function JSify(data) {
return ret.map(function(item) { return item.JS }).join('\n');
}
- return read('preamble.js') + finalCombiner(substrate.solve()) + read('postamble.js');
+ return preprocess(read('preamble.js')) + finalCombiner(substrate.solve()) + read('postamble.js');
// return finalCombiner(substrate.solve());
}
diff --git a/src/preamble.js b/src/preamble.js
index 0e21f70e..59c3e842 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -11,6 +11,25 @@ var __ATEXIT__ = [];
var HEAP = [];
var HEAPTOP = 0;
+#if SAFE_HEAP
+// Semi-manual memory corruption debugging
+HEAP_WATCHED = {};
+function SAFE_HEAP_STORE(dest, value) {
+ if (dest in HEAP_WATCHED) {
+ print((new Error()).stack);
+ throw "Bad store!" + dest;
+ }
+ HEAP[dest] = value;
+}
+function __Z16PROTECT_HEAPADDRPv(dest) {
+ HEAP_WATCHED[dest] = true;
+}
+function __Z18UNPROTECT_HEAPADDRPv(dest) {
+ delete HEAP_WATCHED[dest];
+}
+//==========================================
+#endif
+
function abort(text) {
text = "ABORT: " + text;
print(text + "\n");
@@ -41,7 +60,11 @@ function Pointer_make(slab, pos) {
// Finalize
var ret = _malloc(Math.max(slab.length - pos, 1));
for (var i = 0; i < slab.length - pos; i++) {
+#if SAFE_HEAP
+ SAFE_HEAP_STORE(ret + i, slab[pos + i]);
+#else
HEAP[ret + i] = slab[pos + i];
+#endif
}
return ret;
// return { slab: slab, pos: pos ? pos : 0 };
@@ -157,7 +180,11 @@ function _atoi(s) {
function _llvm_memcpy_i32(dest, src, num, idunno) {
// XXX hardcoded ptr impl
for (var i = 0; i < num; i++) {
+#if SAFE_HEAP
+ SAFE_HEAP_STORE(dest + i, HEAP[src + i]);
+#else
HEAP[dest + i] = HEAP[src + i];
+#endif
}
// dest = Pointer_niceify(dest);
// src = Pointer_niceify(src);
diff --git a/src/settings.js b/src/settings.js
index 287caf8f..65cb8f70 100644
--- a/src/settings.js
+++ b/src/settings.js
@@ -1,3 +1,5 @@
OPTIMIZE = 1;
RELOOP = 1;
+LINEDEBUG = 0;
+SAFE_HEAP = 0;