harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit d7f6058c1e653bbd88d01e400035980de46a8744
parent 9468b525112da6a978c9ffad01127c01a7cd6e55
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri,  1 Jan 2021 12:30:42 -0500

Add array test and fix some bugs

Diffstat:
Msrc/gen.c | 7++++---
Atests/01-arrays.ha | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtests/configure | 4+++-
3 files changed, 59 insertions(+), 4 deletions(-)

diff --git a/src/gen.c b/src/gen.c @@ -305,7 +305,9 @@ gen_expr_access_index(struct gen_context *ctx, constl(&temp, atype->array.members->size); pushi(ctx->current, &index, Q_MUL, &index, &temp, NULL); pushi(ctx->current, &obj, Q_ADD, &obj, &index, NULL); - qval_deref(&obj); + if (!type_is_aggregate(atype->array.members)) { + qval_deref(&obj); + } gen_loadtemp(ctx, &temp, &obj, qtype_for_type(ctx, atype->array.members, true), type_is_signed(atype->array.members)); @@ -660,8 +662,7 @@ gen_array(struct gen_context *ctx, const struct qbe_value *out) { const struct type *type = expr->result; - assert(!out->indirect); // Invariant - assert(!type->array.expandable); // Invariant + assert(!type->array.expandable); // Invariant // XXX: ARCH struct qbe_value ptr = {0}; diff --git a/tests/01-arrays.ha b/tests/01-arrays.ha @@ -0,0 +1,52 @@ +fn indexing() void = { + let x = [1, 2, 3]; + assert(x[0] == 1); + assert(x[1] == 2); + assert(x[2] == 3); +}; + +fn measurements() void = { + let x = [1, 2, 3]; + assert(len(x) == 3z); + assert(size([3]int) == size(int) * 3z); +}; + +fn storage() void = { + let x = [1, 2, 3]; + let y = &x: uintptr; + assert(*((y + (size(int) * 0z): uintptr): *int) == 1); + assert(*((y + (size(int) * 1z): uintptr): *int) == 2); + assert(*((y + (size(int) * 2z): uintptr): *int) == 3); +}; + +fn alignment() void = { + let x = [1i32, 2i32, 3i32]; + assert(&x: uintptr: size % 4z == 0z); + let y = [1i64, 2i64, 3i64]; + assert(&y: uintptr: size % 8z == 0z); +}; + +fn nested() void = { + let x = [[1, 2], [3, 4]]; + assert(x[0][0] == 1); + assert(x[0][1] == 2); + assert(x[1][0] == 3); + assert(x[1][1] == 4); + assert(len(x[0]) == 2z); +}; + +fn param(x: [3]int) void = { + assert(len(x) == 3z); + assert(x[0] == 1); + assert(x[1] == 2); + assert(x[2] == 3); +}; + +export fn main() void = { + indexing(); + measurements(); + storage(); + alignment(); + nested(); + param([1, 2, 3]); +}; diff --git a/tests/configure b/tests/configure @@ -2,7 +2,9 @@ all="$all tests" tests() { - for t in 00-constants + for t in \ + 00-constants \ + 01-arrays do cat <<EOF tests/$t: rt tests/$t.ha