Skip to content

Commit 486db79

Browse files
committed
fix bug with lack of recursion in simplifyBitops
1 parent 14bb76d commit 486db79

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

tools/js-optimizer.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,20 +418,25 @@ function simplifyExpressionsPre(ast) {
418418
var rerun = true;
419419
while (rerun) {
420420
rerun = false;
421-
traverseGenerated(ast, function(node, type, stack) {
421+
traverseGenerated(ast, function process(node, type, stack) {
422422
if (type == 'binary' && node[1] == '|' && (jsonCompare(node[2], ZERO) || jsonCompare(node[3], ZERO))) {
423-
stack.push(1); // From here on up, no need for this kind of correction, it's done at the top
424-
425423
// We might be able to remove this correction
426-
for (var i = stack.length-2; i >= 0; i--) {
424+
for (var i = stack.length-1; i >= 0; i--) {
427425
if (stack[i] == 1) {
428426
// Great, we can eliminate
429427
rerun = true;
430-
return jsonCompare(node[2], ZERO) ? node[3] : node[2];
428+
// we will replace ourselves with the non-zero side. Recursively process that node.
429+
var result = jsonCompare(node[2], ZERO) ? node[3] : node[2], other;
430+
while (other = process(result, result[0], stack)) {
431+
result = other;
432+
}
433+
return result;
431434
} else if (stack[i] == -1) {
432435
break; // Too bad, we can't
433436
}
434437
}
438+
stack.push(1); // From here on up, no need for this kind of correction, it's done at the top
439+
// (Add this at the end, so it is only added if we did not remove it)
435440
} else if (type == 'binary' && node[1] in USEFUL_BINARY_OPS) {
436441
stack.push(1);
437442
} else if ((type == 'binary' && node[1] in SAFE_BINARY_OPS) || type == 'num' || type == 'name') {

tools/test-js-optimizer-output.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,5 +290,8 @@ function asmy() {
290290
f((HEAPU8[_buf + i6 & 16777215] & 1) + i5 | 0);
291291
f((HEAP8[_buf + i6 & 16777215] & 1) + i5 | 0);
292292
f((HEAPU8[_buf + i6 & 16777215] & 1) + i5 | 0);
293+
if ((_sbrk($419 | 0) | 0) == -1) {
294+
print("fleefl");
295+
}
293296
}
294297

tools/test-js-optimizer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,8 @@ function asmy() {
400400
f((HEAPU8[_buf + i6 & 16777215] & 255 & 1) + i5 | 0);
401401
f((HEAP8[_buf + i6 & 16777215] & 1 & 255) + i5 | 0);
402402
f((HEAPU8[_buf + i6 & 16777215] & 1 & 255) + i5 | 0);
403+
if ((_sbrk($419 | 0) | 0 | 0) == -1) {
404+
print('fleefl');
405+
}
403406
}
404407
// EMSCRIPTEN_GENERATED_FUNCTIONS: ["abc", "xyz", "xyz2", "expr", "loopy", "bits", "maths", "hoisting", "demangle", "lua", "moreLabels", "notComps", "tricky", "asmy"]

0 commit comments

Comments
 (0)