Skip to content

Commit 3f31706

Browse files
committed
Allow for multiple fun2wires slots
This means a function can be called multiple times though this number must still of course be bounded.
1 parent 1be9491 commit 3f31706

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

src/sail_sv_backend/sail_plugin_sv.ml

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,11 @@ let verilog_options =
154154
);
155155
("-sv_nomem", Arg.Set opt_nomem, " don't emit a dynamic memory implementation");
156156
( "-sv_fun2wires",
157-
Arg.String (fun fn -> opt_fun2wires := fn :: !opt_fun2wires),
158-
"<functionname> Use input/output ports instead of emitting a function call"
157+
Arg.String (fun str ->
158+
match String.index_opt str ':' with
159+
| Some pos -> opt_fun2wires := (String.sub str (pos + 1) (String.length str - pos - 1), int_of_string (String.sub str 0 pos)) :: !opt_fun2wires
160+
| None -> opt_fun2wires := (str, 1) :: !opt_fun2wires),
161+
"<slots>:<functionname> Use input/output ports instead of emitting a function call"
159162
);
160163
( "-sv_specialize",
161164
Arg.Int (fun i -> opt_int_specialize := Some i),
@@ -390,7 +393,7 @@ let verilog_target _ default_sail_dir out_opt ast effect_info env =
390393
let union_padding = !opt_padding
391394
let unreachable = !opt_unreachable
392395
let comb = !opt_comb
393-
let ignore = !opt_fun2wires
396+
let ignore = List.map (fun (x, y) -> x) !opt_fun2wires
394397
end) in
395398
let open SV in
396399
let sail_dir = Reporting.get_sail_dir default_sail_dir in
@@ -443,7 +446,7 @@ let verilog_target _ default_sail_dir out_opt ast effect_info env =
443446
let out_doc = out_doc ^^ reg_ref_enums in
444447
let in_doc = reg_doc ^^ reg_ref_functions ^^ in_doc in
445448

446-
let mk_wire_fun nm =
449+
let mk_wire_fun nm slots =
447450
let id = mk_id nm in
448451
match Bindings.find_opt id fn_ctyps with
449452
| None -> (empty, [], [])
@@ -453,28 +456,32 @@ let verilog_target _ default_sail_dir out_opt ast effect_info env =
453456
let invoke_flag = string (nm ^ "_sail_invoke") in
454457
let result = string (nm ^ "_sail_invoke_ret") in
455458
let arg_out i = string (nm ^ "_sail_invoke_arg_" ^ string_of_int i) in
456-
let fun_body =
457-
string "if (" ^^ invoke_flag
458-
^^ string ") sail_reached_unreachable = 1;"
459-
^^ hardline ^^ invoke_flag ^^ string " = 1;" ^^ hardline
460-
^^ (arg_nms
461-
|> List.mapi (fun i arg -> arg_out i ^^ string " = " ^^ string (string_of_id arg) ^^ semi ^^ hardline)
462-
|> separate empty
463-
)
464-
^^ string "return " ^^ result ^^ string ";"
459+
let slot_index s = string ("[" ^ string_of_int s ^ "]") in
460+
let fun_body_slot s =
461+
let slot_index = slot_index s in
462+
string "if (!" ^^ invoke_flag ^^ slot_index ^^ string ") begin" ^^ nest 4 (
463+
hardline ^^ invoke_flag ^^ slot_index ^^ string " = 1;" ^^ hardline
464+
^^ (arg_nms
465+
|> List.mapi (fun i arg -> arg_out i ^^ slot_index ^^ string " = " ^^ string (string_of_id arg) ^^ semi ^^ hardline)
466+
|> separate empty
467+
)
468+
^^ string "return " ^^ result ^^ slot_index ^^ string ";"
469+
) ^^ hardline ^^ string "end" ^^ hardline
465470
in
471+
let fun_body = concat (List.init slots fun_body_slot) ^^ string "sail_reached_unreachable = 1;" in
472+
let slot_ranges = string ("[" ^ string_of_int (slots - 1) ^ ":0]") in
466473
( sv_fundef_with ctx real_name arg_nms arg_typs ret_ty fun_body ^^ twice hardline,
467-
separate space [string "output"; string "bit"; invoke_flag]
468-
:: separate space [string "input"; string (fst (sv_ctyp ret_ty)); result]
469-
:: List.mapi (fun i typ -> separate space [string "output"; string (fst (sv_ctyp typ)); arg_out i]) arg_typs,
470-
[invoke_flag ^^ string " = 0;"]
474+
separate space [string "output"; string "bit"; invoke_flag ^^ slot_ranges]
475+
:: separate space [string "input"; string (fst (sv_ctyp ret_ty)); result ^^ slot_ranges]
476+
:: List.mapi (fun i typ -> separate space [string "output"; string (fst (sv_ctyp typ)); arg_out i ^^ slot_ranges]) arg_typs,
477+
List.init slots (fun s -> invoke_flag ^^ slot_index s ^^ string " = 0;")
471478
)
472479
in
473480

474481
let wire_funs, wire_fun_ports, wire_invoke_inits =
475482
List.fold_right
476-
(fun nm (code, ports, inits) ->
477-
let new_code, new_ports, new_inits = mk_wire_fun nm in
483+
(fun (nm, slots) (code, ports, inits) ->
484+
let new_code, new_ports, new_inits = mk_wire_fun nm slots in
478485
(new_code ^^ code, new_ports @ ports, new_inits @ inits)
479486
)
480487
!opt_fun2wires (empty, [], [])

0 commit comments

Comments
 (0)