aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-04-28 13:45:14 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-04-29 13:02:33 -0700
commit200c188d210cb8a3a2c7e87b3dbab7744a14a49a (patch)
tree0ea52ec4b3dfe12b1904e50278206810be7a147c
parent5ec44114166624baec6849447576f030fa72f391 (diff)
align struct fields to 64-bit in le32
-rw-r--r--src/runtime.js1
-rwxr-xr-xtests/runner.py58
2 files changed, 59 insertions, 0 deletions
diff --git a/src/runtime.js b/src/runtime.js
index db334669..9daab820 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -180,6 +180,7 @@ var Runtime = {
// we align i64s and doubles on 64-bit boundaries, unlike x86
#if TARGET_LE32
if (type == 'i64' || type == 'double' || vararg) return 8;
+ if (!type) return Math.min(size, 8); // align structures internally to 64 bits
#endif
return Math.min(size || (type ? Runtime.getNativeFieldSize(type) : 0), Runtime.QUANTUM_SIZE);
},
diff --git a/tests/runner.py b/tests/runner.py
index 6c9e109d..cf6ac062 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -1540,6 +1540,64 @@ Succeeded!
except Exception, e:
assert 'must be aligned' in str(e), e # expected to fail without emulation
+ def test_align64(self):
+ src = r'''
+ #include <stdio.h>
+
+ // inspired by poppler
+
+ enum Type {
+ A = 10,
+ B = 20
+ };
+
+ struct Object {
+ Type type;
+ union {
+ int intg;
+ double real;
+ char *name;
+ };
+ };
+
+ struct Principal {
+ double x;
+ Object a;
+ double y;
+ };
+
+ int main(int argc, char **argv)
+ {
+ int base = argc-1;
+ Object *o = NULL;
+ printf("%d,%d\n", sizeof(Object), sizeof(Principal));
+ printf("%d,%d,%d,%d\n", (int)&o[base].type, (int)&o[base].intg, (int)&o[base].real, (int)&o[base].name);
+ printf("%d,%d,%d,%d\n", (int)&o[base+1].type, (int)&o[base+1].intg, (int)&o[base+1].real, (int)&o[base+1].name);
+ Principal p, q;
+ p.x = p.y = q.x = q.y = 0;
+ p.a.type = A;
+ p.a.real = 123.456;
+ *(&q.a) = p.a;
+ printf("%.2f,%d,%.2f,%.2f : %.2f,%d,%.2f,%.2f\n", p.x, p.a.type, p.a.real, p.y, q.x, q.a.type, q.a.real, q.y);
+ return 0;
+ }
+ '''
+
+ if 'le32-unknown-nacl' in COMPILER_OPTS:
+ self.do_run(src, '''16,32
+0,8,8,8
+16,24,24,24
+0.00,10,123.46,0.00 : 0.00,10,123.46,0.00
+''')
+ elif 'i386-pc-linux-gnu' in COMPILER_OPTS:
+ self.do_run(src, '''12,28
+0,4,4,4
+12,16,16,16
+0.00,10,123.46,0.00 : 0.00,10,123.46,0.00
+''')
+ else:
+ raise Exception('unknown arch')
+
def test_unsigned(self):
Settings.CORRECT_SIGNS = 1 # We test for exactly this sort of thing here
Settings.CHECK_SIGNS = 0