diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-01-10 13:27:50 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-01-10 13:27:50 -0800 |
commit | b2827e02a2b996e501bf00fe884cd34370dbcca1 (patch) | |
tree | db1359b1ce013f3344bd9cff5d39f3ed6459d4bd | |
parent | 5beb9b6b1909da1c527ac5cd9601fdba82f39ad5 (diff) |
align stack to QUANTUM_SIZE in ta2 properly, avoiding unnecessary checks and corrections
-rw-r--r-- | src/analyzer.js | 5 | ||||
-rw-r--r-- | src/preamble.js | 2 | ||||
-rw-r--r-- | src/runtime.js | 14 |
3 files changed, 15 insertions, 6 deletions
diff --git a/src/analyzer.js b/src/analyzer.js index 7d99ab69..4aac53a8 100644 --- a/src/analyzer.js +++ b/src/analyzer.js @@ -774,12 +774,15 @@ function analyzer(data, sidePass) { assert(isNumber(item.allocatedNum)); item.allocatedSize = func.variables[line.ident].impl === VAR_EMULATED ? calcAllocatedSize(item.allocatedType)*item.allocatedNum: 0; + if (USE_TYPED_ARRAYS === 2) { + // We need to keep the stack aligned + item.allocatedSize = Runtime.forceAlign(item.allocatedSize, QUANTUM_SIZE); + } } var index = 0; for (var i = 0; i < lines.length; i++) { var item = lines[i].value; if (!item || item.intertype != 'alloca') break; - if (USE_TYPED_ARRAYS === 2) index = Runtime.forceAlign(index, Math.min(item.allocatedSize, QUANTUM_SIZE)); item.allocatedIndex = index; index += item.allocatedSize; delete item.allocatedSize; diff --git a/src/preamble.js b/src/preamble.js index eb55defb..5bb904d5 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -643,7 +643,7 @@ Module['HEAPU32'] = HEAPU32; Module['HEAPF32'] = HEAPF32; #endif -STACK_ROOT = STACKTOP = alignMemoryPage(10); +STACK_ROOT = STACKTOP = Runtime.alignMemory(STATICTOP); STACK_MAX = STACK_ROOT + TOTAL_STACK; STATICTOP = alignMemoryPage(STACK_MAX); diff --git a/src/runtime.js b/src/runtime.js index 2c3cb117..2b81258d 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -7,14 +7,14 @@ // itself is as optimized as possible - no unneeded runtime checks). var RuntimeGenerator = { - alloc: function(size, type, init, sep) { + alloc: function(size, type, init, sep, ignoreAlign) { sep = sep || ';'; var ret = type + 'TOP'; if (init) { ret += sep + '_memset(' + type + 'TOP, 0, ' + size + ')'; } ret += sep + type + 'TOP += ' + size; - if ({{{ QUANTUM_SIZE }}} > 1) { + if ({{{ QUANTUM_SIZE }}} > 1 && !ignoreAlign) { ret += sep + RuntimeGenerator.alignMemory(type + 'TOP', {{{ QUANTUM_SIZE }}}); } return ret; @@ -24,7 +24,8 @@ var RuntimeGenerator = { stackAlloc: function(size, sep) { sep = sep || ';'; if (USE_TYPED_ARRAYS === 2) 'STACKTOP += STACKTOP % ' + ({{{ QUANTUM_SIZE }}} - (isNumber(size) ? Math.min(size, {{{ QUANTUM_SIZE }}}) : {{{ QUANTUM_SIZE }}})) + sep; - var ret = RuntimeGenerator.alloc(size, 'STACK', INIT_STACK, sep); + // The stack is always QUANTUM SIZE aligned, so we may not need to force alignment here + var ret = RuntimeGenerator.alloc(size, 'STACK', INIT_STACK, sep, USE_TYPED_ARRAYS != 2 || (isNumber(size) && parseInt(size) % {{{ QUANTUM_SIZE }}} == 0)); if (ASSERTIONS) { ret += sep + 'assert(STACKTOP < STACK_ROOT + STACK_MAX, "Ran out of stack")'; } @@ -33,8 +34,13 @@ var RuntimeGenerator = { stackEnter: function(initial, force) { if (initial === 0 && SKIP_STACK_IN_SMALL && !force) return ''; - if (USE_TYPED_ARRAYS === 2) initial = Runtime.forceAlign(initial); var ret = 'var __stackBase__ = STACKTOP; STACKTOP += ' + initial; + if (USE_TYPED_ARRAYS == 2) { + assert(initial % QUANTUM_SIZE == 0); + if (ASSERTIONS) { + ret += '; assert(STACKTOP % {{{ QUANTUM_SIZE }}} == 0, "Stack is unaligned")'; + } + } if (ASSERTIONS) { ret += '; assert(STACKTOP < STACK_MAX, "Ran out of stack")'; } |