commit edd4452053a3803ad9d4c304c1284f5ebba11487
parent 06e663dbf4809255f74e42d88abbc7a55c289787
Author: Drew DeVault <sir@cmpwn.com>
Date: Fri, 15 Jan 2021 17:30:18 -0500
tests: add tagged union test
Diffstat:
3 files changed, 40 insertions(+), 3 deletions(-)
diff --git a/src/gen.c b/src/gen.c
@@ -699,11 +699,11 @@ gen_cast_to_tagged(struct gen_context *ctx,
struct qbe_value tag = {0}, ptr = {0}, offs = {0};
gen_temp(ctx, &ptr, &qbe_long, "ptr.%d");
constl(&offs, 8);
- constl(&tag, expr->result->id);
+ constl(&tag, expr->cast.value->result->id);
pushi(ctx->current, &ptr, Q_COPY, out, NULL);
pushi(ctx->current, NULL, Q_STOREL, &tag, &ptr, NULL);
pushi(ctx->current, &ptr, Q_ADD, &ptr, &offs, NULL);
- ptr.type = qtype_for_type(ctx, expr->result, false);
+ ptr.type = qtype_for_type(ctx, expr->cast.value->result, false);
ptr.indirect = !type_is_aggregate(expr->cast.value->result);
gen_expression(ctx, expr->cast.value, &ptr);
}
diff --git a/tests/13-tagged.ha b/tests/13-tagged.ha
@@ -0,0 +1,36 @@
+fn measurements() void = {
+ let x: (u8 | u16 | u32 | u64) = 1337u16;
+ assert(size((u8 | u16 | u32 | u64)) == size(u64) + size(size));
+ const align: size =
+ if (size(u64) < size(size)) size(size)
+ else size(u64);
+ assert(&x: uintptr: size % align == 0z);
+};
+
+fn storage() void = {
+ let x: (u8 | u16 | u32 | u64) = 42u8;
+ let y = &x: *struct {
+ tag: size,
+ union { _u8: u8, _u16: u16, _u32: u32, _u64: u64 },
+ };
+ assert(y.tag == 605989269682102909z); // u8 type ID
+ assert(y._u8 == 42u8);
+
+ x = 1337u16;
+ assert(y.tag == 593553793169496424z); // u16 type ID
+ assert(y._u16 == 1337u16);
+
+ x = 0xCAFEBABEu32;
+ assert(y.tag == 596423518518559459z); // u32 type ID
+ assert(y._u32 == 0xCAFEBABEu32);
+
+ x = 0xCAFEBABEDEADBEEFu64;
+ assert(y.tag == 595466943402205114z); // u64 type ID
+ assert(y._u64 == 0xCAFEBABEDEADBEEFu64);
+};
+
+export fn main() void = {
+ measurements();
+ storage();
+ // TODO: Expand this as other tagged union features become available
+};
diff --git a/tests/configure b/tests/configure
@@ -15,7 +15,8 @@ tests() {
09-funcs \
10-binarithms \
11-globals \
- 12-loops
+ 12-loops \
+ 13-tagged
do
cat <<EOF
tests/$t: libhart.a tests/$t.ha