harec

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

commit b209595d375107f1af47087645dad7e98673f2cb
parent 9480b1b86b396cce6a3fdaf5b5737b886b5d6758
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 24 Jul 2021 14:43:37 +0200

gen: add test for gen_copy

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Atests/905-copy.ha | 43+++++++++++++++++++++++++++++++++++++++++++
Mtests/configure | 5+++--
Atests/rt.c | 10++++++++++
Mtests/rt.ha | 5+++++
4 files changed, 61 insertions(+), 2 deletions(-)

diff --git a/tests/905-copy.ha b/tests/905-copy.ha @@ -0,0 +1,43 @@ +type coords = struct { x: i8, y: int, z: size }; +type anyint = struct { _8: i8, _16: i16, _32: i32, _64: i64 }; + +export fn main() int = { + // Simple case + let x = 10; + let y = x; + assert(y == 10); + + // With indirect target + let a = [1, 2, 3, 4]; + let x = 2z; + assert(a[x] == 3); + + // Aggregate types: + // arrays + let x = [1, 2, 3, 4]; + let y = x; + assert(&x != &y); + assert(x[0] == y[0]); + assert(x[1] == y[1]); + assert(x[2] == y[2]); + assert(x[3] == y[3]); + + // structs + let a = coords { x = 10, y = 20, z = 30 }; + let b = a; + assert(&a != &b); + assert(a.x == b.x); + assert(a.y == b.y); + assert(a.z == b.z); + + // unions + let a = anyint { _16 = 10 }; + let b = a; + assert(&a != &b); + assert(a._16 == b._16); + + // strings + // TODO: Need casts to examine string internals + + return 0; +}; diff --git a/tests/configure b/tests/configure @@ -8,14 +8,15 @@ tests() { 901-primitives \ 902-arithm \ 903-call \ - 904-deref + 904-deref \ + 905-copy do cat <<EOF tests/$t: harec tests/$t.ha tests/rt.o @printf 'HARECC\t%s\t$@\n' "tests/$t" @HARECACHE=\$(HARECACHE) ./harec -o tests/$t.ssa tests/$t.ha @\$(QBE) -o tests/$t.s tests/$t.ssa - @\$(CC) -o tests/$t tests/$t.s tests/rt.o + @\$(CC) -g -o tests/$t tests/$t.s tests/rt.c tests/rt.o check: tests/$t diff --git a/tests/rt.c b/tests/rt.c @@ -0,0 +1,10 @@ +#include <stdlib.h> + +void *c_memcpy(void *dest, const void *src, size_t n) { + unsigned char *a = dest; + const unsigned char *b = src; + for (size_t i = 0; i < n; ++i) { + a[i] = b[i]; + } + return dest; +} diff --git a/tests/rt.ha b/tests/rt.ha @@ -1,2 +1,7 @@ @symbol("abort") fn c_abort() void; export @symbol("rt.abort") fn abort_() void = c_abort(); + +fn c_memcpy(x: *void, y: *void, z: size) *void; +export @symbol("rt.memcpy") fn memcpy(x: *void, y: *void, z: size) *void = { + return c_memcpy(x, y, z); +};