diff options
author | Alon Zakai <alonzakai@gmail.com> | 2011-11-18 14:34:54 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2011-11-18 14:34:54 -0800 |
commit | 42b3bb7206356f6543737696d3bbd75ebe2aad4f (patch) | |
tree | 6c5b3d2855f13f0084a3f972f029686c726bd1af | |
parent | 7d95ee5784c4e1bd475aef46ebcdf4f4cac5cfbe (diff) |
phi progress, almost all unoptimized tests pass
-rw-r--r-- | src/analyzer.js | 7 | ||||
-rw-r--r-- | src/jsifier.js | 24 | ||||
-rw-r--r-- | src/parseTools.js | 5 | ||||
-rw-r--r-- | src/utility.js | 4 |
4 files changed, 33 insertions, 7 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index a3428250..c5d1d405 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -628,7 +628,7 @@ function analyzer(data) { var sourceLabelId = phi.params[i].label; var sourceLabel = func.labelsDict[sourceLabelId]; var lastLine = sourceLabel.lines.slice(-1)[0]; - assert(lastLine.intertype == 'branch', 'Only branches can lead to labels with phis'); + assert(lastLine.intertype == 'branch', 'Only branches can lead to labels with phis, line ' + [func.ident, label.ident]); lastLine.currLabelId = sourceLabelId; } phis.push(line); @@ -671,7 +671,7 @@ function analyzer(data) { var sourceLabelId = param.label; var sourceLabel = func.labelsDict[sourceLabelId]; var lastLine = sourceLabel.lines.slice(-1)[0]; - assert(lastLine.intertype == 'branch', 'Only branches can lead to labels with phis'); + assert(lastLine.intertype == 'branch', 'Only branches can lead to labels with phis, line ' + [func.ident, label.ident]); if (!lastLine.phi) { // We store the phi assignments in the branch's params (which are otherwise unused) lastLine.phi = true; @@ -681,7 +681,8 @@ function analyzer(data) { lastLine.params.push({ intertype: 'phiassign', ident: line.ident, - value: param.value + value: param.value, + targetLabel: label.ident }); } // The assign to phi is now just a var diff --git a/src/jsifier.js b/src/jsifier.js index 6d77fe4f..7a267e92 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -702,13 +702,29 @@ function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) { } makeFuncLineActor('branch', function(item) { - if (item.stolen) return ';'; // We will appear where we were stolen to + var phiSets = null; + if (item.phi) { + phiSets = {}; + // TODO: Do not always rename them all + // TODO: eliminate unneeded sets (to undefined etc.) + item.params.forEach(function(param) { + if (!phiSets[param.targetLabel]) phiSets[param.targetLabel] = []; + phiSets[param.targetLabel].unshift('var ' + param.ident + '$phi = ' + finalizeLLVMParameter(param.value) + ';'); + phiSets[param.targetLabel].push(param.ident + ' = ' + param.ident + '$phi;'); + }); + } + function getPhiSets(label) { + label = getOldLabel(label); + if (!phiSets || !phiSets[label]) return ''; + return sumStringy(phiSets[label]); + } + if (item.stolen) return getPhiSets(item.label) || ';'; // We will appear where we were stolen to if (!item.value) { - return makeBranch(item.label, item.currLabelId); + return getPhiSets(item.label) + makeBranch(item.label, item.currLabelId); } else { var condition = finalizeLLVMParameter(item.value); - var labelTrue = makeBranch(item.labelTrue, item.currLabelId); - var labelFalse = makeBranch(item.labelFalse, item.currLabelId); + var labelTrue = getPhiSets(item.labelTrue) + makeBranch(item.labelTrue, item.currLabelId); + var labelFalse = getPhiSets(item.labelFalse) + makeBranch(item.labelFalse, item.currLabelId); if (labelTrue == ';' && labelFalse == ';') return ';'; var head = 'if (' + condition + ') { '; var head2 = 'if (!(' + condition + ')) { '; diff --git a/src/parseTools.js b/src/parseTools.js index 591e6add..e3734e0e 100644 --- a/src/parseTools.js +++ b/src/parseTools.js @@ -731,6 +731,11 @@ function cleanLabel(label) { } } +function getOldLabel(label) { + var parts = label.split('|'); + return parts[parts.length-1]; +} + function calcAllocatedSize(type) { if (pointingLevels(type) == 0 && isStructType(type)) { return Types.types[type].flatSize; // makeEmptyStruct(item.allocatedType).length; diff --git a/src/utility.js b/src/utility.js index 9661f865..53555aa5 100644 --- a/src/utility.js +++ b/src/utility.js @@ -131,6 +131,10 @@ function sumTruthy(x) { return x.reduce(function(a,b) { return (!!a)+(!!b) }, 0); } +function sumStringy(x) { + return x.reduce(function(a,b) { return a+b }, ''); +} + function loopOn(array, func) { for (var i = 0; i < array.length; i++) { func(i, array[i]); |