aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-10-27 22:12:02 -0700
committeralon@honor <none@none>2010-10-27 22:12:02 -0700
commit4b48a792ec61ce5b99c6eabf3b7b9f1e7f4afd8f (patch)
tree4b88210e14069791b83cd8c9a8a80cc86e936654
parent027d1d7ca694ef5128c03a1535235ec65cc87fac (diff)
modularize generated code
-rw-r--r--src/jsifier.js5
-rw-r--r--src/parseTools.js11
-rw-r--r--src/postamble.js2
-rw-r--r--src/preamble.js40
-rw-r--r--src/runtime.js5
5 files changed, 36 insertions, 27 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index 1a67e1cd..f140cecc 100644
--- a/src/jsifier.js
+++ b/src/jsifier.js
@@ -372,7 +372,7 @@ function JSify(data) {
// Finalize function
if (LABEL_DEBUG) func.JS += " INDENT = INDENT.substr(0, INDENT.length-2);\n";
func.JS += '}\n';
- func.JS += func.ident + '.__index__ = Runtime.getFunctionIndex(' + func.ident + ');\n';
+ func.JS += func.ident + '.__index__ = Runtime.getFunctionIndex(' + func.ident + ', "' + func.ident + '");\n';
func.__result__ = true;
return func;
},
@@ -880,6 +880,7 @@ function JSify(data) {
substrate.addItems(data.functionStubs, 'FunctionStub');
var params = { 'QUANTUM_SIZE': QUANTUM_SIZE };
- return preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + finalCombiner(substrate.solve()) + read('postamble.js'), params);
+ var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + finalCombiner(substrate.solve()) + read('postamble.js'), params);
+ return read('shell.js').replace('{{BODY}}', indentify(body, 2));
}
diff --git a/src/parseTools.js b/src/parseTools.js
index 3e65a039..4d6ae564 100644
--- a/src/parseTools.js
+++ b/src/parseTools.js
@@ -469,3 +469,14 @@ function getActualLabelId(labelId) {
return labelId.split('|').slice(-1)[0];
}
+// Misc
+
+function indentify(text, indent) {
+ if (typeof indent === 'number') {
+ var len = indent;
+ indent = '';
+ for (var i = 0; i < len; i++) indent += ' ';
+ }
+ return text.split('\n').map(function(line) { return indent + line }).join('\n');
+}
+
diff --git a/src/postamble.js b/src/postamble.js
index 07fe1d2e..142ee013 100644
--- a/src/postamble.js
+++ b/src/postamble.js
@@ -32,7 +32,7 @@ function run(args) {
}
try {
- run(this.arguments ? arguments : []);
+ run(this.args ? this.args : (this.arguments ? this.arguments : []));
} catch (e) {
print("Fatal exception: " + e);
throw e;
diff --git a/src/preamble.js b/src/preamble.js
index 6a1f897c..22473ce9 100644
--- a/src/preamble.js
+++ b/src/preamble.js
@@ -7,7 +7,7 @@ function __globalConstructor__() {
// Maps ints ==> functions. This lets us pass around ints, which are
// actually pointers to functions, and we convert at call()time
-FUNCTION_TABLE = [];
+var FUNCTION_TABLE = [];
var __THREW__ = false; // Used in checking for thrown exceptions.
@@ -15,7 +15,7 @@ var __ATEXIT__ = [];
#if SAFE_HEAP
// Semi-manual memory corruption debugging
-HEAP_WATCHED = {};
+var HEAP_WATCHED = {};
function SAFE_HEAP_STORE(dest, value) {
if (dest in HEAP_WATCHED) {
print((new Error()).stack);
@@ -33,14 +33,14 @@ function __Z18UNPROTECT_HEAPADDRPv(dest) {
#endif
#if LABEL_DEBUG
-INDENT = '';
+var INDENT = '';
#endif
#if EXECUTION_TIMEOUT
-START_TIME = Date.now();
+var START_TIME = Date.now();
#endif
-ABORT = false;
+var ABORT = false;
function assert(condition, text) {
if (!condition) {
@@ -60,9 +60,9 @@ function Pointer_niceify(ptr) {
// other words, do whatever is necessary in order to return a pointer, that
// points to the slab (and possibly position) we are given.
-ALLOC_NORMAL = 0; // Tries to use _malloc()
-ALLOC_STACK = 1; // Lives for the duration of the current function call
-ALLOC_STATIC = 2; // Cannot be freed
+var ALLOC_NORMAL = 0; // Tries to use _malloc()
+var ALLOC_STACK = 1; // Lives for the duration of the current function call
+var ALLOC_STATIC = 2; // Cannot be freed
function Pointer_make(slab, pos, allocator) {
pos = pos ? pos : 0;
@@ -118,27 +118,23 @@ function Pointer_stringify(ptr) {
// Memory management
-PAGE_SIZE = 4096;
+var PAGE_SIZE = 4096;
function alignMemoryPage(x) {
return Math.ceil(x/PAGE_SIZE)*PAGE_SIZE;
}
-// If we don't have malloc/free implemented, use a simple implementation.
-if (!this._malloc) {
- _malloc = Runtime.staticAlloc;
- _free = function() { }; // leak!
-}
+var HEAP, IHEAP, FHEAP;
+var STACK_ROOT, STACKTOP, STACK_MAX;
+var STATICTOP;
-// Mangled "new"
-__Znwj = _malloc; // new(uint)
-__Znaj = _malloc; // new[](uint)
-__Znam = _malloc; // new(ulong)
-__Znwm = _malloc; // new[](ulong)
-// Mangled "delete"
-__ZdlPv = _free; // delete(..)
-__ZdaPv = _free; // delete[](..)
+// Mangled |new| and |free| (various manglings, for int, long params; new and new[], etc.
+var _malloc, _free, __Znwj, __Znaj, __Znam, __Znwm, __ZdlPv, __ZdaPv;
function __initializeRuntime__() {
+ // If we don't have malloc/free implemented, use a simple implementation.
+ _malloc = __Znwj = __Znaj = __Znam = __Znwm = Module['_malloc'] ? Module['_malloc'] : Runtime.staticAlloc;
+ _free = __ZdlPv = __ZdaPv = Module['_free'] ? Module['_free'] : function() { };
+
HEAP = intArrayFromString('(null)'); // So printing %s of NULL gives '(null)'
// Also this ensures we leave 0 as an invalid address, 'NULL'
#if USE_TYPED_ARRAYS
diff --git a/src/runtime.js b/src/runtime.js
index dad0d0d5..cdd85ab3 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -70,10 +70,11 @@ Runtime = {
staticAlloc: unInline('staticAlloc', ['size']),
alignMemory: unInline('alignMemory', ['size', 'quantum']),
- getFunctionIndex: function getFunctionIndex(func) {
+ getFunctionIndex: function getFunctionIndex(func, ident) {
var key = FUNCTION_TABLE.length;
FUNCTION_TABLE[key] = func;
FUNCTION_TABLE[key+1] = null; // Need to have keys be even numbers, see |polymorph| test
+ Module[ident] = func; // Export using full name, for Closure Compiler
return key;
},
@@ -124,7 +125,7 @@ Runtime = {
} else if (Runtime.dedup(diffs).length == 1) {
type.flatFactor = diffs[0];
}
- type.needsFlattening = (this.flatFactor != 1);
+ type.needsFlattening = (type.flatFactor != 1);
return type.flatIndexes;
}