diff options
author | Michael Forney <mforney@mforney.org> | 2019-02-12 14:05:30 -0800 |
---|---|---|
committer | Michael Forney <mforney@mforney.org> | 2019-02-12 14:06:16 -0800 |
commit | 5784264d0eec915ccf5e7a930ee85e36e3a12923 (patch) | |
tree | 2246f9187a9198faa2000bce208a382a1642794d | |
parent | 5e8fa8708b65f633c011d3252d8372236acf9a7e (diff) | |
download | cproc-5784264d0eec915ccf5e7a930ee85e36e3a12923.tar.xz |
Fix union sizes and member offsets
-rw-r--r-- | decl.c | 10 | ||||
-rw-r--r-- | tests/union.c | 4 | ||||
-rw-r--r-- | tests/union.qbe | 2 |
3 files changed, 14 insertions, 2 deletions
@@ -660,8 +660,14 @@ structdecl(struct scope *s, struct type *t) if (align < m->type->align) align = m->type->align; t->size = ALIGNUP(t->size, align); - m->offset = t->size; - t->size += m->type->size; + if (t->kind == TYPESTRUCT) { + m->offset = t->size; + t->size += m->type->size; + } else { + m->offset = 0; + if (t->size < m->type->size) + t->size = m->type->size; + } if (t->align < align) t->align = align; } diff --git a/tests/union.c b/tests/union.c new file mode 100644 index 0000000..9b4e5f9 --- /dev/null +++ b/tests/union.c @@ -0,0 +1,4 @@ +union { + int x; + double y; +} a = {.x = 5}, b = {.y = 7.5}; diff --git a/tests/union.qbe b/tests/union.qbe new file mode 100644 index 0000000..adbda57 --- /dev/null +++ b/tests/union.qbe @@ -0,0 +1,2 @@ +export data $a = align 8 { w 5, z 4 } +export data $b = align 8 { d d_0x1.ep+2, } |