remove clumsy iterative creation attempts of globals
This commit is contained in:
Родитель
e053177b97
Коммит
e3d1e034c6
|
@ -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
|
||||
|
|
|
@ -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++) {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче