summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/jib/c_backend.ml26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/jib/c_backend.ml b/src/jib/c_backend.ml
index fa4fa802..b1d6cc40 100644
--- a/src/jib/c_backend.ml
+++ b/src/jib/c_backend.ml
@@ -62,6 +62,7 @@ open Anf
module Big_int = Nat_big_num
let opt_static = ref false
+let static () = if !opt_static then "static " else ""
let opt_no_main = ref false
let opt_no_lib = ref false
let opt_no_rts = ref false
@@ -1592,7 +1593,7 @@ let codegen_type_def ctx = function
in
let codegen_undefined =
let name = sgen_id id in
- string (Printf.sprintf "enum %s UNDEFINED(%s)(unit u) { return %s; }" name name (sgen_id first_id))
+ string (Printf.sprintf "static enum %s UNDEFINED(%s)(unit u) { return %s; }" name name (sgen_id first_id))
in
string (Printf.sprintf "// enum %s" (string_of_id id)) ^^ hardline
^^ separate space [string "enum"; codegen_id id; lbrace; separate_map (comma ^^ space) codegen_id ids; rbrace ^^ semi]
@@ -2050,16 +2051,15 @@ let codegen_alloc = function
let codegen_def' ctx = function
| CDEF_reg_dec (id, ctyp, _) ->
string (Printf.sprintf "// register %s" (string_of_id id)) ^^ hardline
- ^^ string (Printf.sprintf "%s %s;" (sgen_ctyp ctyp) (sgen_id id))
+ ^^ string (Printf.sprintf "%s%s %s;" (static ()) (sgen_ctyp ctyp) (sgen_id id))
| CDEF_spec (id, _, arg_ctyps, ret_ctyp) ->
- let static = if !opt_static then "static " else "" in
if Env.is_extern id ctx.tc_env "c" then
empty
else if is_stack_ctyp ret_ctyp then
- string (Printf.sprintf "%s%s %s(%s%s);" static (sgen_ctyp ret_ctyp) (sgen_function_id id) (extra_params ()) (Util.string_of_list ", " sgen_ctyp arg_ctyps))
+ string (Printf.sprintf "%s%s %s(%s%s);" (static ()) (sgen_ctyp ret_ctyp) (sgen_function_id id) (extra_params ()) (Util.string_of_list ", " sgen_ctyp arg_ctyps))
else
- string (Printf.sprintf "%svoid %s(%s%s *rop, %s);" static (sgen_function_id id) (extra_params ()) (sgen_ctyp ret_ctyp) (Util.string_of_list ", " sgen_ctyp arg_ctyps))
+ string (Printf.sprintf "%svoid %s(%s%s *rop, %s);" (static ()) (sgen_function_id id) (extra_params ()) (sgen_ctyp ret_ctyp) (Util.string_of_list ", " sgen_ctyp arg_ctyps))
| CDEF_fundef (id, ret_arg, args, instrs) as def ->
let arg_ctyps, ret_ctyp = match Bindings.find_opt id ctx.valspecs with
@@ -2100,8 +2100,7 @@ let codegen_def' ctx = function
codegen_type_def ctx ctype_def
| CDEF_startup (id, instrs) ->
- let static = if !opt_static then "static " else "" in
- let startup_header = string (Printf.sprintf "%svoid startup_%s(void)" static (sgen_function_id id)) in
+ let startup_header = string (Printf.sprintf "%svoid startup_%s(void)" (static ()) (sgen_function_id id)) in
separate_map hardline codegen_decl instrs
^^ twice hardline
^^ startup_header ^^ hardline
@@ -2110,8 +2109,7 @@ let codegen_def' ctx = function
^^ string "}"
| CDEF_finish (id, instrs) ->
- let static = if !opt_static then "static " else "" in
- let finish_header = string (Printf.sprintf "%svoid finish_%s(void)" static (sgen_function_id id)) in
+ let finish_header = string (Printf.sprintf "%svoid finish_%s(void)" (static ()) (sgen_function_id id)) in
separate_map hardline codegen_decl (List.filter is_decl instrs)
^^ twice hardline
^^ finish_header ^^ hardline
@@ -2127,7 +2125,7 @@ let codegen_def' ctx = function
let cleanup =
List.concat (List.map (fun (id, ctyp) -> [iclear ctyp (name id)]) bindings)
in
- separate_map hardline (fun (id, ctyp) -> string (Printf.sprintf "%s %s;" (sgen_ctyp ctyp) (sgen_id id))) bindings
+ separate_map hardline (fun (id, ctyp) -> string (Printf.sprintf "%s%s %s;" (static ()) (sgen_ctyp ctyp) (sgen_id id))) bindings
^^ hardline ^^ string (Printf.sprintf "static void create_letbind_%d(void) " number)
^^ string "{"
^^ jump 0 2 (separate_map hardline codegen_alloc setup) ^^ hardline
@@ -2291,7 +2289,7 @@ let compile_ast env output_chan c_includes ast =
in
let model_init = separate hardline (List.map string
- ( [ "void model_init(void)";
+ ( [ Printf.sprintf "%svoid model_init(void)" (static ());
"{";
" setup_rts();" ]
@ fst exn_boilerplate
@@ -2303,7 +2301,7 @@ let compile_ast env output_chan c_includes ast =
in
let model_fini = separate hardline (List.map string
- ( [ "void model_fini(void)";
+ ( [ Printf.sprintf "%svoid model_fini(void)" (static ());
"{" ]
@ letbind_finalizers
@ List.concat (List.map (fun r -> snd (register_init_clear r)) regs)
@@ -2313,8 +2311,8 @@ let compile_ast env output_chan c_includes ast =
@ [ "}" ] ))
in
- let model_default_main =
- ([ "int model_main(int argc, char *argv[])";
+ let model_default_main =
+ ([ Printf.sprintf "%sint model_main(int argc, char *argv[])" (static ());
"{";
" model_init();";
" if (process_arguments(argc, argv)) exit(EXIT_FAILURE);";