diff options
Diffstat (limited to 'src/jsifier.js')
-rw-r--r-- | src/jsifier.js | 66 |
1 files changed, 56 insertions, 10 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index 9a2a82c4..ab5c8720 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -26,6 +26,8 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { ident: '_' + ident }); }); + + Functions.implementedFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident })); } // Does simple 'macro' substitution, using Django-like syntax, @@ -279,17 +281,18 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { item.intertype = 'GlobalVariableStub'; var ret = [item]; item.JS = 'var ' + item.ident + ';'; - // Set the actual value in a postset, since it may be a global variable. TODO: handle alias of alias (needs ordering) + // Set the actual value in a postset, since it may be a global variable. We also order by dependencies there + var value = finalizeLLVMParameter(item.value, true); // do *not* indexize functions here ret.push({ intertype: 'GlobalVariablePostSet', - JS: item.ident + ' = ' + item.aliasee + ';' + ident: item.ident, + dependencies: set([value]), + JS: item.ident + ' = ' + value + ';' }); return ret; } }); - var moduleFunctions = set(data.unparsedFunctions.map(function(func) { return func.ident })); - var addedLibraryItems = {}; // functionStub @@ -305,7 +308,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { if (ident in addedLibraryItems) return ''; // Don't replace implemented functions with library ones (which can happen when we add dependencies). // Note: We don't return the dependencies here. Be careful not to end up where this matters - if (('_' + ident) in moduleFunctions) return ''; + if (('_' + ident) in Functions.implementedFunctions) return ''; addedLibraryItems[ident] = true; var snippet = LibraryManager.library[ident]; @@ -340,6 +343,9 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { snippet = snippet.toString(); // name the function; overwrite if it's already named snippet = snippet.replace(/function(?:\s+([^(]+))?\s*\(/, 'function _' + ident + '('); + if (LIBRARY_DEBUG) { + snippet = snippet.replace('{', '{ print("[library call:' + ident + ']"); '); + } } var postsetId = ident + '__postset'; @@ -674,10 +680,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { makeFuncLineActor('branch', function(item) { if (item.stolen) return ';'; // We will appear where we were stolen to - if (!item.condition) { + if (!item.value) { return makeBranch(item.label, item.currLabelId); } else { - var condition = finalizeLLVMParameter(item.condition); + var condition = finalizeLLVMParameter(item.value); var labelTrue = makeBranch(item.labelTrue, item.currLabelId); var labelFalse = makeBranch(item.labelFalse, item.currLabelId); if (labelTrue == ';' && labelFalse == ';') return ';'; @@ -707,9 +713,9 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { ret += ' ' + makeBranch(switchLabel.label, item.currLabelId || null) + '\n'; ret += '}\n'; }); - ret += 'else {\n'; + if (item.switchLabels.length > 0) ret += 'else {\n'; ret += makeBranch(item.defaultLabel, item.currLabelId) + '\n'; - ret += '}\n'; + if (item.switchLabels.length > 0) ret += '}\n'; if (item.value) { ret += ' ' + toNiceIdent(item.value); } @@ -727,6 +733,9 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { } return ret + ';'; }); + makeFuncLineActor('resume', function(item) { + return (EXCEPTION_DEBUG ? 'print("Resuming exception");' : '') + 'throw [0,0];'; + }); makeFuncLineActor('invoke', function(item) { // Wrapping in a function lets us easily return values if we are // in an assignment @@ -743,6 +752,10 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { + ' } else { ' + makeBranch(item.unwindLabel, item.currLabelId) + ' }'; return ret; }); + makeFuncLineActor('landingpad', function(item) { + // Just a stub + return '{ f0: 0, f1: 0 }'; + }); makeFuncLineActor('load', function(item) { var value = finalizeLLVMParameter(item.pointer); var impl = item.ident ? getVarImpl(item.funcData, item.ident) : VAR_EMULATED; @@ -759,6 +772,15 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { // and we emulate them using simple JS objects { f1: , f2: , } etc., for speed return item.ident + '.f' + item.indexes[0][0].text; }); + makeFuncLineActor('insertvalue', function(item) { + assert(item.indexes.length == 1); // TODO: see extractvalue + var ret = '(', ident; + if (item.ident === 'undef') { + item.ident = 'tempValue'; + ret += item.ident + ' = [' + makeEmptyStruct(item.type) + '], '; + } + return ret + item.ident + '.f' + item.indexes[0][0].text + ' = ' + finalizeLLVMParameter(item.value) + ', ' + item.ident + ')'; + }); makeFuncLineActor('indirectbr', function(item) { return makeBranch(finalizeLLVMParameter(item.pointer), item.currLabelId, true); }); @@ -841,6 +863,7 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { } makeFuncLineActor('getelementptr', function(item) { return finalizeLLVMFunctionCall(item) }); makeFuncLineActor('call', function(item) { + if (item.standalone && LibraryManager.isStubFunction(item.ident)) return ';'; return makeFunctionCall(item.ident, item.params, item.funcData) + (item.standalone ? ';' : ''); }); @@ -853,11 +876,34 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { var itemsDict = { type: [], GlobalVariableStub: [], functionStub: [], function: [], GlobalVariable: [], GlobalVariablePostSet: [] }; items.forEach(function(item) { item.lines = null; - var small = { intertype: item.intertype, JS: item.JS }; // Release memory + var small = { intertype: item.intertype, JS: item.JS, ident: item.ident, dependencies: item.dependencies }; // Release memory itemsDict[small.intertype].push(small); }); items = null; + var splitPostSets = splitter(itemsDict.GlobalVariablePostSet, function(x) { return x.ident && x.dependencies }); + itemsDict.GlobalVariablePostSet = splitPostSets.leftIn; + var orderedPostSets = splitPostSets.splitOut; + + var limit = orderedPostSets.length * orderedPostSets.length; + for (var i = 0; i < orderedPostSets.length; i++) { + for (var j = i+1; j < orderedPostSets.length; j++) { + if (orderedPostSets[j].ident in orderedPostSets[i].dependencies) { + var temp = orderedPostSets[i]; + orderedPostSets[i] = orderedPostSets[j]; + orderedPostSets[j] = temp; + i--; + limit--; + assert(limit > 0, 'Could not sort postsets!'); + break; + } + } + } + + itemsDict.GlobalVariablePostSet = itemsDict.GlobalVariablePostSet.concat(orderedPostSets); + + // + var generated = []; if (mainPass) { generated = generated.concat(itemsDict.type).concat(itemsDict.GlobalVariableStub).concat(itemsDict.functionStub); |