diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-01-28 09:41:49 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-01-28 09:41:49 -0800 |
commit | 25293737bd69e8e284e6896da0ddf58977002c2a (patch) | |
tree | 609391a1f2433dcfb9d32e9796a7150f62b89509 /src | |
parent | c17b30680c3a45dc3e190390c3f8676188d6ffee (diff) |
refactor lshr legalization
Diffstat (limited to 'src')
-rw-r--r-- | src/analyzer.js | 107 |
1 files changed, 55 insertions, 52 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index 2eb6f8d5..1c6e9a17 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -256,67 +256,70 @@ function analyzer(data, sidePass) { case 'mathop': { if (isIllegalType(value.type)) { dprint('legalizer', 'Legalizing mathop at line ' + item.lineNum); + label.lines.splice(i, 1); bits = getBits(value.type); var targetElements = getLegalVars(item.ident, bits); assert(value.param1.intertype == 'value', 'TODO: unfolding'); - var sourceElements = getLegalVars(value.param1.ident, bits); - label.lines.splice(i, 1); + var shifts = 0; + var sourceBits; switch (value.op) { case 'lshr': { assert(value.param2.intertype == 'value', 'TODO: unfolding'); - var shifts = parseInt(value.param2.ident); - assert(isNumber(shifts), 'TODO: handle nonconstant shifts'); - // Shift right makes values smaller. Towards the 0 index of the elements - var whole = Math.floor(shifts/32); - var fraction = shifts % 32; - var toAdd = []; - for (var j = 0; j < targetElements.length; j++) { - var result = { - intertype: 'value', - ident: (j + whole) < sourceElements.length ? sourceElements[j + whole].ident : '0', - type: 'i32', - }; - if (fraction > 0) { - var other = { - intertype: 'value', - ident: (j + 1 + whole) < sourceElements.length ? sourceElements[j + 1 + whole].ident : '0', - type: 'i32', - }; - other = { - intertype: 'mathop', - op: 'shl', - type: 'i32', - param1: other, - param2: { intertype: 'value', ident: (32 - fraction).toString(), type: 'i32' } - }; - result = { - intertype: 'mathop', - op: 'lshr', - type: 'i32', - param1: result, - param2: { intertype: 'value', ident: fraction.toString(), type: 'i32' } - }; - result = { - intertype: 'mathop', - op: 'or', - type: 'i32', - param1: result, - param2: other - } - } - toAdd.push({ - intertype: 'assign', - ident: targetElements[j].ident, - value: result, - lineNum: item.lineNum + (j/100) - }); - } - Array.prototype.splice.apply(label.lines, [i, 0].concat(toAdd)); - i += toAdd.length; - continue; + shifts = parseInt(value.param2.ident); + sourceBits = bits; + break; } default: throw 'Invalid mathop for legalization: ' + [value.op, item.lineNum]; } + assert(isNumber(shifts), 'TODO: handle nonconstant shifts'); + var sourceElements = getLegalVars(value.param1.ident, sourceBits); + var whole = Math.floor(shifts/32); + var fraction = shifts % 32; + var toAdd = []; + for (var j = 0; j < targetElements.length; j++) { + var result = { + intertype: 'value', + ident: (j + whole) < sourceElements.length ? sourceElements[j + whole].ident : '0', + type: 'i32', + }; + if (fraction > 0) { + var other = { + intertype: 'value', + ident: (j + 1 + whole) < sourceElements.length ? sourceElements[j + 1 + whole].ident : '0', + type: 'i32', + }; + other = { + intertype: 'mathop', + op: 'shl', + type: 'i32', + param1: other, + param2: { intertype: 'value', ident: (32 - fraction).toString(), type: 'i32' } + }; + result = { + intertype: 'mathop', + op: 'lshr', + type: 'i32', + param1: result, + param2: { intertype: 'value', ident: fraction.toString(), type: 'i32' } + }; + result = { + intertype: 'mathop', + op: 'or', + type: 'i32', + param1: result, + param2: other + } + } + toAdd.push({ + intertype: 'assign', + ident: targetElements[j].ident, + value: result, + lineNum: item.lineNum + (j/100) + }); + } + Array.prototype.splice.apply(label.lines, [i, 0].concat(toAdd)); + i += toAdd.length; + continue; } } } |