Skip to content

Commit baaff35

Browse files
authored
Output calls to set_abstract_... in model_init() (#1436)
* Output calls to set_abstract_... in model_init() So that you don't need to call these functions automatically. As far as I can tell they are idempotent so it should be backwards compatible. * Don't call `sail_set_abstract_...` when it takes a parameter If an abstract type hasn't been initialised we can't automatically initialise it.
1 parent 3ae641e commit baaff35

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/lib/jib_compile.ml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ let is_ct_enum = function CT_enum _ -> true | _ -> false
102102

103103
let iblock1 = function [instr] -> instr | instrs -> iblock instrs
104104

105+
type abstract_type_initialised = Initialised | Uninitialised
106+
105107
(** The context type contains two type-checking environments. ctx.local_env contains the closest typechecking
106108
environment, usually from the expression we are compiling, whereas ctx.tc_env is the global type checking
107109
environment from type-checking the entire AST. We also keep track of local variables in ctx.locals, so we know when
@@ -111,7 +113,7 @@ type ctx = {
111113
records : (kid list * ctyp Bindings.t) Bindings.t;
112114
enums : IdSet.t Bindings.t;
113115
variants : (kid list * ctyp Bindings.t) Bindings.t;
114-
abstracts : ctyp Bindings.t;
116+
abstracts : (ctyp * abstract_type_initialised) Bindings.t;
115117
valspecs : (string option * ctyp list * ctyp * uannot) Bindings.t;
116118
quants : ctyp KBindings.t;
117119
local_env : Env.t;
@@ -132,7 +134,7 @@ let ctx_map_ctyps f ctx =
132134
ctx with
133135
records = Bindings.map (fun (params, fields) -> (params, Bindings.map f fields)) ctx.records;
134136
variants = Bindings.map (fun (params, fields) -> (params, Bindings.map f fields)) ctx.variants;
135-
abstracts = Bindings.map f ctx.abstracts;
137+
abstracts = Bindings.map (fun (ctyp, initialised) -> (f ctyp, initialised)) ctx.abstracts;
136138
valspecs =
137139
Bindings.map
138140
(fun (extern, param_ctyps, ret_ctyp, uannot) -> (extern, List.map f param_ctyps, f ret_ctyp, uannot))
@@ -771,7 +773,7 @@ module Make (C : CONFIG) = struct
771773
(mk_id "sail_config_bits_abstract_len", [])
772774
[V_id (json, CT_json)];
773775
]
774-
@ select_abstract l ctx abstract_name (fun id abstract_ctyp ->
776+
@ select_abstract l ctx abstract_name (fun id (abstract_ctyp, _) ->
775777
match abstract_ctyp with
776778
| CT_fint 64 ->
777779
[
@@ -1808,14 +1810,19 @@ module Make (C : CONFIG) = struct
18081810
CTDI_instrs (setup @ [call (CL_id (Abstract id, ctyp))] @ cleanup)
18091811
| TDC_none -> CTDI_none
18101812
in
1813+
let is_initialised = function CTDI_instrs _ -> Initialised | CTDI_none -> Uninitialised in
18111814
match kind with
18121815
| K_int ->
18131816
let ctyp = ctyp_of_typ ctx (atom_typ (nid id)) in
18141817
let inst = compile_inst ctyp inst in
1815-
(Some (CTD_abstract (id, ctyp, inst)), { ctx with abstracts = Bindings.add id ctyp ctx.abstracts })
1818+
( Some (CTD_abstract (id, ctyp, inst)),
1819+
{ ctx with abstracts = Bindings.add id (ctyp, is_initialised inst) ctx.abstracts }
1820+
)
18161821
| K_bool ->
18171822
let inst = compile_inst CT_bool inst in
1818-
(Some (CTD_abstract (id, CT_bool, inst)), { ctx with abstracts = Bindings.add id CT_bool ctx.abstracts })
1823+
( Some (CTD_abstract (id, CT_bool, inst)),
1824+
{ ctx with abstracts = Bindings.add id (CT_bool, is_initialised inst) ctx.abstracts }
1825+
)
18191826
| _ -> Reporting.unreachable l __POS__ "Found abstract type that was neither an integer nor a boolean"
18201827
)
18211828
(* Will be re-written before here, see bitfield.ml *)

src/lib/jib_compile.mli

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,20 @@ val opt_memo_cache : bool ref
6666

6767
(** {2 Jib context} *)
6868

69+
(* For an abstract type like `type xlen : Int`, is it initialised?
70+
Yes: `type xlen : Int = config xlen`
71+
No: `type xlen : Int`
72+
*)
73+
type abstract_type_initialised = Initialised | Uninitialised
74+
6975
(** Dynamic context for compiling Sail to Jib. We need to pass a (global) typechecking environment given by checking the
7076
full AST. *)
7177
type ctx = {
7278
target_name : string;
7379
records : (kid list * ctyp Bindings.t) Bindings.t;
7480
enums : IdSet.t Bindings.t;
7581
variants : (kid list * ctyp Bindings.t) Bindings.t;
76-
abstracts : ctyp Bindings.t;
82+
abstracts : (ctyp * abstract_type_initialised) Bindings.t;
7783
valspecs : (string option * ctyp list * ctyp * uannot) Bindings.t;
7884
quants : ctyp KBindings.t;
7985
local_env : Env.t;

src/sail_c_backend/c_backend.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,17 @@ module Codegen (Config : CODEGEN_CONFIG) = struct
24552455
List.map (fun n -> Printf.sprintf " create_letbind_%d();" n) (List.rev ctx.letbinds)
24562456
in
24572457
let letbind_finalizers = List.map (fun n -> Printf.sprintf " kill_letbind_%d();" n) ctx.letbinds in
2458+
2459+
let set_abstract_types =
2460+
Bindings.bindings ctx.abstracts
2461+
|> List.filter_map (fun (id, (_, initialised)) ->
2462+
match initialised with
2463+
| Initialised -> Some (Printf.sprintf " sail_set_abstract_%s();" (Ast_util.string_of_id id))
2464+
(* Skip abstract types that haven't been initialised; we can't initialise them automatically. *)
2465+
| Uninitialised -> None
2466+
)
2467+
in
2468+
24582469
let startup cdefs = List.map sgen_startup (List.filter is_cdef_startup cdefs) in
24592470
let finish cdefs = List.map sgen_finish (List.filter is_cdef_finish cdefs) in
24602471

@@ -2478,7 +2489,7 @@ module Codegen (Config : CODEGEN_CONFIG) = struct
24782489
([Printf.sprintf "%svoid model_init(void)" (static ()); "{"; " setup_rts();"]
24792490
@ fst exn_boilerplate
24802491
@ List.concat (List.map (fun r -> fst (register_init_clear r)) early_regs)
2481-
@ startup cdefs @ letbind_initializers
2492+
@ set_abstract_types @ startup cdefs @ letbind_initializers
24822493
@ List.concat (List.map (fun r -> fst (register_init_clear r)) regs)
24832494
@ ( if regs = [] then []
24842495
else [Printf.sprintf " %s(UNIT);" (sgen_function_id (mk_id "initialize_registers"))]

0 commit comments

Comments
 (0)