aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoralon@honor <none@none>2010-10-09 13:55:35 -0700
committeralon@honor <none@none>2010-10-09 13:55:35 -0700
commiteec779fced0cb0e9df260768d90aa0832f7bef1b (patch)
tree6083c00699840a32f857335b852562d5e5c9a78e /src
parentb7a45a4236221343f96642499d1dfc84f327fb9b (diff)
fix memory alignment/padding of structures | TESTS FIXED
Diffstat (limited to 'src')
-rw-r--r--src/analyzer.js5
-rw-r--r--src/runtime.js10
2 files changed, 10 insertions, 5 deletions
diff --git a/src/analyzer.js b/src/analyzer.js
index 47c7281b..92aa0a8a 100644
--- a/src/analyzer.js
+++ b/src/analyzer.js
@@ -177,7 +177,7 @@ function analyzer(data) {
var soFar = type.flatSize;
var size;
if (isNumberType(field) || isPointerType(field)) {
- size = getNativeFieldSize(field);
+ size = getNativeFieldSize(field, true); // pack char; char; in structs, also char[X]s.
} else if (isStructType(field)) {
size = item.types[field].flatSize;
} else {
@@ -185,8 +185,9 @@ function analyzer(data) {
}
type.flatSize += size;
sizes.push(size);
- return soFar;
+ return Runtime.alignMemory(soFar, Math.min(QUANTUM_SIZE, size)); // if necessary, place this on aligned memory
});
+ type.flatSize = Runtime.alignMemory(type.flatSize); // padding at end
if (dedup(sizes).length == 1) {
type.flatFactor = sizes[0];
}
diff --git a/src/runtime.js b/src/runtime.js
index 250eb50a..346020b0 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -11,7 +11,7 @@ RuntimeGenerator = {
}
ret += '; ' + type + 'TOP += ' + size;
if (QUANTUM_SIZE > 1) {
- ret += ';' + RuntimeGenerator.alignMemory(type + 'TOP');
+ ret += ';' + RuntimeGenerator.alignMemory(type + 'TOP', QUANTUM_SIZE);
}
return ret;
},
@@ -38,8 +38,11 @@ RuntimeGenerator = {
return RuntimeGenerator.alloc(size, 'STATIC');
},
- alignMemory: function(target) {
- return target + ' = Math.ceil(' + target + '/QUANTUM_SIZE)*QUANTUM_SIZE;';
+ alignMemory: function(target, quantum) {
+ if (typeof quantum !== 'number') {
+ quantum = '(quantum ? quantum : QUANTUM_SIZE)';
+ }
+ return target + ' = Math.ceil(' + target + '/' + quantum + ')*' + quantum + ';';
},
};
@@ -56,6 +59,7 @@ function unInline(name_, params) {
Runtime = {
stackAlloc: unInline('stackAlloc', ['size']),
staticAlloc: unInline('staticAlloc', ['size']),
+ alignMemory: unInline('alignMemory', ['size', 'quantum']),
};
function getRuntime() {