diff options
author | Alon Zakai <azakai@mozilla.com> | 2010-11-26 21:37:09 -0800 |
---|---|---|
committer | Alon Zakai <azakai@mozilla.com> | 2010-11-26 21:37:09 -0800 |
commit | e3d1e034c6b8774a0da993b8ba82cd30135fb946 (patch) | |
tree | 7ec364d669da9af39b700181d2468df679497535 | |
parent | e053177b97a5bd2046019d573a0ce2619d922ae1 (diff) |
remove clumsy iterative creation attempts of globals
-rw-r--r-- | src/jsifier.js | 51 | ||||
-rw-r--r-- | src/postamble.js | 18 | ||||
-rw-r--r-- | src/preamble.js | 2 |
3 files changed, 31 insertions, 40 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index f525c067..5ad3f939 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -151,7 +151,7 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { function makeConst(value, type, ident) { //dprint('gconst', '//yyyyy ' + JSON.stringify(value) + ',' + type + '\n'); if (value.intertype) { - return finalizeLLVMFunctionCall(value); + return [finalizeLLVMFunctionCall(value)]; } else if (Runtime.isNumberType(type) || pointingLevels(type) >= 1) { return indexizeFunctions(parseNumerical(toNiceIdent(value.text))); } else if (value.text == 'zeroinitializer') { @@ -225,9 +225,9 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { } return ret; } - constant = '[' + flatten(constant).map(function(x) { return parseNumerical(x) }).join(', ') + ']'; + constant = flatten(constant).map(function(x) { return parseNumerical(x) }) } - return makePointer(constant, null, 'ALLOC_STATIC', type); + return constant; } // globalVariable @@ -235,24 +235,39 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { processItem: function(item) { item.intertype = 'GlobalVariableStub'; item.__result__ = true; + var ret = [item]; if (item.ident == '_llvm_global_ctors') { item.JS = '\n__globalConstructor__ = function() {\n' + item.ctors.map(function(ctor) { return ' ' + toNiceIdent(ctor) + '();' }).join('\n') + '\n}\n'; + return ret; } else { item.JS = 'var ' + item.ident + ';'; - return [item, { + var constant = item.external ? + JSON.stringify(makeEmptyStruct(item.type)) + ' /* external value? */' + : + parseConst(item.value, item.type, item.ident); + if (typeof constant === 'object') { + // This is a flattened object. We need to find its idents, so they can be assigned to later + constant.forEach(function(value, i) { + if (value[0] in set('_', '(')) { // ident, or expression containing an ident + ret.push({ + intertype: 'GlobalVariablePostSet', + JS: 'IHEAP[' + item.ident + '+' + i + '] = ' + value + ';', + __result__: true, + }); + constant[i] = '0'; + } + }); + constant = '[' + constant.join(', ') + ']'; + } + constant = makePointer(constant, null, 'ALLOC_STATIC', item.type); + return ret.concat({ intertype: 'GlobalVariable', - JS: 'globalFuncs.push(function() { return ' + item.ident + ' = ' + ( - item.external ? - makePointer(JSON.stringify(makeEmptyStruct(item.type)), null, 'ALLOC_STATIC', item.type) + ' /* external value? */' - : - parseConst(item.value, item.type, item.ident) - ) + ' });', + JS: item.ident + ' = ' + constant + ';', __result__: true, - }]; + }); } - return [item]; }, }); @@ -960,16 +975,10 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions) { if (functionsOnly) return ret; var body = preprocess(read('preamble.js').replace('{{RUNTIME}}', getRuntime()) + ret + read('postamble.js'), CONSTANTS); - function reverse_(x) { - if (LLVM_STYLE === 'old') { - return x.reverse(); - } else { - return x; - } - } - var globalVars = reverse_(items.filter(function(item) { return item.intertype == 'GlobalVariable' }).map(function(item) { return item.JS })).join('\n'); + var globalVars = items.filter(function(item) { return item.intertype == 'GlobalVariable' }).map(function(item) { return item.JS }).join('\n'); + var globalVarsPostSets = items.filter(function(item) { return item.intertype == 'GlobalVariablePostSet' }).map(function(item) { return item.JS }).join('\n'); return read('shell.js').replace('{{BODY}}', indentify(body, 2)) - .replace('{{GLOBAL_VARS}}', indentify(globalVars, 4)); + .replace('{{GLOBAL_VARS}}', indentify(globalVars+'\n\n\n'+globalVarsPostSets, 4)); } // Data diff --git a/src/postamble.js b/src/postamble.js index 72233980..6907814b 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -8,24 +8,6 @@ function run(args) { {{GLOBAL_VARS}} - var failures = 0; - while (globalFuncs.length > 0) { - var func = globalFuncs.pop(); - try { - var x = func(); - if (x == undefined) throw 'undefined'; - failures = 0; - } catch (e) { - failures++; - if (failures > 2*globalFuncs.length) { - throw 'Failed to generate global values'; - } - globalFuncs.unshift(func); - // We will try again later. The global vars we depend on should be resolved by then - } - } - assert(globalFuncs.length === 0); - var argc = args.length+1; function pad() { for (var i = 0; i < QUANTUM_SIZE-1; i++) { diff --git a/src/preamble.js b/src/preamble.js index 6958068f..833e64e4 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -114,7 +114,7 @@ function Pointer_make(slab, pos, allocator) { var i; for (i = 0; i < size; i++) { if (slab[i] === undefined) { - throw 'Invalid element in slab'; // This can be caught, and you can try again to allocate later, see globalFuncs in run() + throw 'Invalid element in slab at ' + new Error().stack; // This can be caught, and you can try again to allocate later, see globalFuncs in run() } } |