From f62edee6988476ed8ba78424c870461ac8d44256 Mon Sep 17 00:00:00 2001 From: Alasdair Date: Thu, 5 Jul 2018 20:57:44 +0100 Subject: Fix equality comparisons for structs Add a test case in test/c/eq_struct.sail. Ensure that the macro EQUAL(type) will always give a valid equality function for any builtin type in sail.h. --- src/c_backend.ml | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/c_backend.ml b/src/c_backend.ml index 6f1379e3..6097e996 100644 --- a/src/c_backend.ml +++ b/src/c_backend.ml @@ -2120,7 +2120,19 @@ let codegen_type_def ctx = function rbrace in let codegen_eq = - string (Printf.sprintf "static bool eq_%s(struct %s op1, struct %s op2) { return true; }" (sgen_id id) (sgen_id id) (sgen_id id)) + let codegen_eq_test (id, ctyp) = + if is_stack_ctyp ctyp then + string (Printf.sprintf "op1.%s == op2.%s" (sgen_id id) (sgen_id id)) + else + string (Printf.sprintf "EQUAL(%s)(op1.%s, op2.%s)" (sgen_ctyp_name ctyp) (sgen_id id) (sgen_id id)) + in + string (Printf.sprintf "static bool EQUAL(%s)(struct %s op1, struct %s op2)" (sgen_id id) (sgen_id id) (sgen_id id)) + ^^ space + ^^ surround 2 0 lbrace + (string "return" ^^ space + ^^ separate_map (string " && ") codegen_eq_test ctors + ^^ string ";") + rbrace in (* Generate the struct and add the generated functions *) let codegen_ctor (id, ctyp) = -- cgit v1.2.3