aboutsummaryrefslogtreecommitdiff
path: root/src/jsifier.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/jsifier.js')
-rw-r--r--src/jsifier.js66
1 files changed, 56 insertions, 10 deletions
diff --git a/src/jsifier.js b/src/jsifier.js
index c19eda3a..aa1e3c60 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,
@@ -257,17 +259,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
@@ -283,7 +286,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];
@@ -318,6 +321,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';
@@ -652,10 +658,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 ';';
@@ -685,9 +691,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);
}
@@ -705,6 +711,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
@@ -720,6 +729,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;
@@ -736,6 +749,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);
});
@@ -817,6 +839,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 ? ';' : '');
});
@@ -829,11 +852,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);