aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-07-23 12:18:02 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-07-23 12:18:02 -0700
commit2980ce7c7096cbea33dd40acbeb4242fb4cd8037 (patch)
tree8e2cdae60d228ff7072fcb0bda348634cb849b40
parent3fcbc4487553a9d2bcc3234eac5ce5edfc70e27d (diff)
fix structure size when it has a [0 x ..] at the end - that should add nothing
-rw-r--r--src/runtime.js22
-rw-r--r--tests/cases/zeroembedded.ll16
-rw-r--r--tests/cases/zeroembedded.txt1
3 files changed, 35 insertions, 4 deletions
diff --git a/src/runtime.js b/src/runtime.js
index 684f11e7..062066af 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -201,14 +201,24 @@ var Runtime = {
type.alignSize = 0;
var diffs = [];
var prev = -1;
+ var index = 0;
type.flatIndexes = type.fields.map(function(field) {
+ index++;
var size, alignSize;
if (Runtime.isNumberType(field) || Runtime.isPointerType(field)) {
size = Runtime.getNativeTypeSize(field); // pack char; char; in structs, also char[X]s.
alignSize = Runtime.getAlignSize(field, size);
} else if (Runtime.isStructType(field)) {
- size = Types.types[field].flatSize;
- alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
+ if (field[1] === '0') {
+ // this is [0 x something]. When inside another structure like here, it must be at the end,
+ // and it adds no size
+ assert(index === type.fields.length);
+ size = 0;
+ alignSize = 0;
+ } else {
+ size = Types.types[field].flatSize;
+ alignSize = Runtime.getAlignSize(null, Types.types[field].alignSize);
+ }
} else if (field[0] == 'b') {
// bN, large number field, like a [N x i8]
size = field.substr(1)|0;
@@ -218,8 +228,12 @@ var Runtime = {
}
if (type.packed) alignSize = 1;
type.alignSize = Math.max(type.alignSize, alignSize);
- var curr = Runtime.alignMemory(type.flatSize, alignSize); // if necessary, place this on aligned memory
- type.flatSize = curr + size;
+ if (size > 0) {
+ var curr = Runtime.alignMemory(type.flatSize, alignSize); // if necessary, place this on aligned memory
+ type.flatSize = curr + size;
+ } else {
+ curr = prev;
+ }
if (prev >= 0) {
diffs.push(curr-prev);
}
diff --git a/tests/cases/zeroembedded.ll b/tests/cases/zeroembedded.ll
new file mode 100644
index 00000000..c1ffa212
--- /dev/null
+++ b/tests/cases/zeroembedded.ll
@@ -0,0 +1,16 @@
+; a.ll
+%struct.pypy_str = type { i32, [0 x i8] }
+%struct.pypy_strval = type { i32, [13 x i8] }
+
+@pypy_g_strval = global %struct.pypy_strval { i32 13, [13 x i8] c"hello world\0A\00" }
+
+declare i32 @printf(i8*, ...)
+
+define i32 @main(i32 %argc, i8** nocapture %argv) {
+ %1 = bitcast %struct.pypy_strval* @pypy_g_strval to %struct.pypy_str*
+ %2 = getelementptr inbounds %struct.pypy_str* %1, i32 1
+ %3 = bitcast %struct.pypy_str* %2 to i8*
+ call i32 (i8*, ...)* @printf(i8* %3)
+ ret i32 0
+}
+
diff --git a/tests/cases/zeroembedded.txt b/tests/cases/zeroembedded.txt
new file mode 100644
index 00000000..3b18e512
--- /dev/null
+++ b/tests/cases/zeroembedded.txt
@@ -0,0 +1 @@
+hello world