diff options
author | Alon Zakai <azakai@mozilla.com> | 2011-02-01 22:56:02 -0800 |
---|---|---|
committer | Alon Zakai <azakai@mozilla.com> | 2011-02-01 22:56:02 -0800 |
commit | b6aeefa650fb91865c01d6e0f96b55f94c23cbff (patch) | |
tree | c4c3bd222d44ab9d3e34239d416242ebf456eb9e /src | |
parent | 9c584561a85eb6e011862612a0b21dae9d9ad9bf (diff) |
overflow checks in getelementptr
Diffstat (limited to 'src')
-rw-r--r-- | src/jsifier.js | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/src/jsifier.js b/src/jsifier.js index 34d19107..9a405f55 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -761,6 +761,13 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria } } + function handleOverflow(text, bits) { + if (!bits) return text; + if (CORRECT_OVERFLOWS && bits <= 32) text = '(' + text + ')&' + (Math.pow(2, bits) - 1); + if (!CHECK_OVERFLOWS) return text; + return 'CHECK_OVERFLOW(' + text + ', ' + bits + ')'; + } + var mathop = makeFuncLineActor('mathop', function(item) { with(item) { for (var i = 1; i <= 4; i++) { if (item['param'+i]) { @@ -780,23 +787,17 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria if (item.type[0] === 'i') { bits = parseInt(item.type.substr(1)); } - function handleOverflow(text) { - if (!bits) return text; - if (CORRECT_OVERFLOWS && bits <= 32) text = '(' + text + ')&' + (Math.pow(2, bits) - 1); - if (!CHECK_OVERFLOWS) return text; - return 'CHECK_OVERFLOW(' + text + ', ' + bits + ')'; - } switch (op) { // basic integer ops - case 'add': return handleOverflow(ident1 + ' + ' + ident2); - case 'sub': return handleOverflow(ident1 + ' - ' + ident2); + case 'add': return handleOverflow(ident1 + ' + ' + ident2, bits); + case 'sub': return handleOverflow(ident1 + ' - ' + ident2, bits); case 'sdiv': case 'udiv': return 'Math.floor(' + ident1 + ' / ' + ident2 + ')'; - case 'mul': return handleOverflow(ident1 + ' * ' + ident2); + case 'mul': return handleOverflow(ident1 + ' * ' + ident2, bits); case 'urem': case 'srem': return ident1 + ' % ' + ident2; case 'or': return ident1 + ' | ' + ident2; // TODO this forces into a 32-bit int - add overflow-style checks? also other bitops below us case 'and': return ident1 + ' & ' + ident2; case 'xor': return ident1 + ' ^ ' + ident2; - case 'shl': return handleOverflow(ident1 + ' << ' + ident2); + case 'shl': return handleOverflow(ident1 + ' << ' + ident2, bits); case 'ashr': return ident1 + ' >> ' + ident2; case 'lshr': return ident1 + ' >>> ' + ident2; // basic float ops @@ -932,6 +933,9 @@ function JSify(data, functionsOnly, givenTypes, givenFunctions, givenGlobalVaria for (var i = 1; i < indexes.length; i++) { ret = getFastValue(ret, '+', indexes[i]); } + + ret = handleOverflow(ret, 32); // XXX - we assume a 32-bit arch here. If you fail on this, change to 64 + return ret; } |