From f063a1e71d90368c04cf1f8eb2ba36cd16592e01 Mon Sep 17 00:00:00 2001 From: Alasdair Armstrong Date: Tue, 13 Mar 2018 17:32:58 +0000 Subject: Polymorphic option types now compile to C Fixed an issue whereby an option constructor that was never constructed, but only matched on, would cause compilation to fail. Temporarily fixed an issue where union types that can be entirely stack-allocated were not being treated as such, by simply heap-allocating all unions. Need to adapt the code generator to handle this case properly. Fixed a further small issue whereby multiple union types would confuse the type specialisation pass. Added a test case for compiling option types. RISCV now generates C code, but there are still some bugs that need to be squashed before it compile and work. --- test/c/option.expect | 3 +++ test/c/option.sail | 40 ++++++++++++++++++++++++++++++++++++++++ test/c/sail.h | 4 ++++ 3 files changed, 47 insertions(+) create mode 100644 test/c/option.expect create mode 100644 test/c/option.sail (limited to 'test') diff --git a/test/c/option.expect b/test/c/option.expect new file mode 100644 index 00000000..f099c714 --- /dev/null +++ b/test/c/option.expect @@ -0,0 +1,3 @@ +a = 5 +b = 0xF +c = 0xA diff --git a/test/c/option.sail b/test/c/option.sail new file mode 100644 index 00000000..1ca59372 --- /dev/null +++ b/test/c/option.sail @@ -0,0 +1,40 @@ +default Order dec + +val print = "print_endline" : string -> unit + +val "print_int" : (string, int) -> unit +val "print_bits" : forall 'n. (string, vector('n, dec, bit)) -> unit + +union option ('a : Type) = { + None : unit, + Some : 'a +} + +union soption ('a : Type) = { + sNone : unit, + sSome : 'a +} + +val main : unit -> unit + +function main () = { + let x : option(int) = Some(5); + let y : option(int) = None(); + let z : option(vector(4, dec, bit)) = Some(0xF); + + match x { + Some(a) => print_int("a = ", 5), + None() => print("None") + }; + + match z { + Some(b) => print_bits("b = ", b), + None() => print("None") + }; + + let q : soption(vector(4, dec, bit)) = sSome(0xA); + + match q { + sSome(c) => print_bits("c = ", c) + } +} diff --git a/test/c/sail.h b/test/c/sail.h index b29e9535..c0f2674b 100644 --- a/test/c/sail.h +++ b/test/c/sail.h @@ -12,6 +12,10 @@ typedef int unit; #define UNIT 0 +unit undefined_unit(const unit u) { + return UNIT; +} + typedef struct { mp_bitcnt_t len; mpz_t *bits; -- cgit v1.2.3