Skip to content

Commit

Permalink
all enum constants have "RISCV_" as a prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
moste00 committed Sep 3, 2024
1 parent 10c2164 commit 81a5c5e
Show file tree
Hide file tree
Showing 8 changed files with 1,876 additions and 1,844 deletions.
10 changes: 5 additions & 5 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ open Riscv_disasm_from_sail
open Constants
open Printexc

let write_c_file ?(optional_includes = []) name code =
let write_c_file ?(additional_includes = []) name code =
let oc = open_out name in
let mk_include_lines incs =
String.concat "\n" (List.map (fun i -> "#include " ^ i ^ "\n") incs)
in
let include_string = mk_include_lines Constants.includes in
let optional_includes_string = mk_include_lines optional_includes in
let additional_includes_string = mk_include_lines additional_includes in
Printf.fprintf oc "%s" include_string;
Printf.fprintf oc "%s" optional_includes_string;
Printf.fprintf oc "%s" "\n\n\n";
Printf.fprintf oc "%s" additional_includes_string;
Printf.fprintf oc "%s" "\n\n";
Printf.fprintf oc "%s" code;
close_out oc

Expand Down Expand Up @@ -84,4 +84,4 @@ let proc_dec_str = Stringify.stringify_decode_procedure proc_dec typdefwalker
let () = write_c_file Constants.ast_type_filename ctypedefs_str
let () =
write_c_file Constants.decode_logic_filename proc_dec_str
~optional_includes:["\"" ^ Constants.ast_type_filename ^ "\""]
~additional_includes:["\"" ^ Constants.ast_type_filename ^ "\""]
2 changes: 2 additions & 0 deletions lib/constants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ let includes = ["<stdint.h>"]
let ast_type_filename = "riscv_ast.gen.inc"

let decode_logic_filename = "riscv_decode.gen.inc"

let identifier_prefix = "RISCV_"
12 changes: 10 additions & 2 deletions lib/gen_clike_typedef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ let int_to_clike_bitv_size size =
^ string_of_int size
)

let enum_case_to_str enum_case_id =
let str = id_to_str enum_case_id in
add_prefix_unless_exists identifier_prefix str

let gen_clike_builtin_from_bitvec_size_expr name arg =
Clike_builtin (name, int_to_clike_bitv_size (sail_bitv_size_to_int arg))

let gen_clike_type_from_enum enum_name name case_names =
Clike_enum (enum_name, name, List.map id_to_str case_names)
Clike_enum (enum_name, name, List.map enum_case_to_str case_names)

let gen_clike_type_from_app name constructor args =
match constructor with
Expand Down Expand Up @@ -166,11 +170,15 @@ let gen_clike_typedef ast_typename typgen_info =
)
case_bodies
in
let prefixed_case_names =
List.map (add_prefix_unless_exists identifier_prefix) case_names
in
Clike_struct
( ast_typename,
"",
[
Clike_enum ("", ast_typename ^ generated_ast_enum_suffix, case_names);
Clike_enum
("", ast_typename ^ generated_ast_enum_suffix, prefixed_case_names);
Clike_union
("", ast_typename ^ generated_ast_payload_suffix, clike_bodies);
]
Expand Down
4 changes: 3 additions & 1 deletion lib/gen_decoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ let lit_to_consequence_body lit =
| L_false -> Push (Bool_const false)
| _ -> failwith ("Unsupported literal @ " ^ stringify_sail_source_loc loc)

let idstr_to_consequence_body idstr = Push (Binding idstr)
let idstr_to_consequence_body idstr =
if idstr = String.capitalize_ascii idstr then Push (Enum_lit idstr)
else Push (Binding idstr)

let vec_concat_to_consequence_body slices =
Concat_push
Expand Down
12 changes: 12 additions & 0 deletions lib/sail_ast_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ let bitv_literal_size bitv_lit =
| L_hex lit_str -> String.length lit_str * 4
| L_bin lit_str -> String.length lit_str
| _ -> failwith "Expected a bitvec literal, found neither L_hex nor L_bin"

let str_starts_with prefix str =
let plen = String.length prefix in
let slen = String.length str in
if plen > slen then false
else (
let str_prefix = String.sub str 0 plen in
str_prefix = prefix
)

let add_prefix_unless_exists prefix str =
if str_starts_with prefix str then str else prefix ^ str
19 changes: 14 additions & 5 deletions lib/stringify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Decoder
open Decode_procedure
open Constants
open Gen_clike_typedef
open Sail_ast_utils

let mk_indentation lvl =
if lvl = 0 then ""
Expand Down Expand Up @@ -57,7 +58,9 @@ let rec stringify_clike_typedef ?(indentation_lvl = 0) clike_typdef =
| Clike_typename (typname, name) -> typname ^ " " ^ name ^ ";"

let stringify_typdef typdef =
"enum {false = 0, true = 1}; \n" ^ stringify_clike_typedef typdef
"enum {" ^ identifier_prefix ^ "false = 0, " ^ identifier_prefix
^ "true = 1}; \n\n"
^ stringify_clike_typedef typdef

type decproc_stringification_state = {
typedef_walker : typedef_walker;
Expand Down Expand Up @@ -143,7 +146,8 @@ let rec stringify_stmt ?(indentation_lvl = 0) str_state stmt =
List.map
(fun (bval, enmval) ->
indent ^ "case " ^ bval ^ ": \n" ^ indent ^ indent ^ var ^ " = "
^ enmval ^ " ;\n" ^ indent ^ "break; \n"
^ add_prefix_unless_exists identifier_prefix enmval
^ " ;\n" ^ indent ^ "break; \n"
)
cases
in
Expand All @@ -165,7 +169,9 @@ let rec stringify_stmt ?(indentation_lvl = 0) str_state stmt =
indent ^ var ^ "." ^ k ^ " = "
^ ( match v with
| Bool_const c -> if c then "1" else "0"
| Bv_const s | Binding s | Enum_lit s -> s
| Bv_const s | Binding s -> s
| Enum_lit s ->
add_prefix_unless_exists identifier_prefix s
)
^ ";"
)
Expand All @@ -179,14 +185,17 @@ let rec stringify_stmt ?(indentation_lvl = 0) str_state stmt =
var_decl ^ switch_start ^ String.concat "\n" c_cases ^ ind ^ "}\n"
| Set_ast_case case ->
let case_setter_path = set_walker_case str_state.typedef_walker case in
ind ^ ast_c_parameter ^ "->" ^ case_setter_path ^ " = " ^ case ^ " ;\n"
ind ^ ast_c_parameter ^ "->" ^ case_setter_path ^ " = "
^ add_prefix_unless_exists identifier_prefix case
^ " ;\n"
| Set_ast_next_case_member member_rhs -> (
let rhs_string =
match member_rhs with
| Val v -> (
match v with
| Bool_const c -> if c then "1" else "0"
| Bv_const s | Binding s | Enum_lit s -> s
| Bv_const s | Binding s -> s
| Enum_lit s -> add_prefix_unless_exists identifier_prefix s
)
| Exp bv_expr -> stringify_bv str_state bv_expr
in
Expand Down
Loading

0 comments on commit 81a5c5e

Please sign in to comment.