Skip to content
Snippets Groups Projects
Commit 7fe8fce0 authored by Basile Desloges's avatar Basile Desloges
Browse files

[eacsl:codegen] Change the type of main to kernel_function in injector.ml

parent 607e3dbf
No related branches found
No related tags found
No related merge requests found
...@@ -593,7 +593,7 @@ let inject_in_fundec main fundec = ...@@ -593,7 +593,7 @@ let inject_in_fundec main fundec =
add_generated_variables_in_function env fundec; add_generated_variables_in_function env fundec;
add_malloc_and_free_stmts kf fundec; add_malloc_and_free_stmts kf fundec;
(* setting main if necessary *) (* setting main if necessary *)
let main = if Kernel_function.is_main kf then Some fundec else main in let main = if Kernel_function.is_main kf then Some kf else main in
Options.feedback ~dkey ~level:2 "function %a done." Options.feedback ~dkey ~level:2 "function %a done."
Kernel_function.pretty kf; Kernel_function.pretty kf;
env, main env, main
...@@ -686,11 +686,17 @@ let inject_global_initializer file main = ...@@ -686,11 +686,17 @@ let inject_global_initializer file main =
in in
vi.vreferenced <- true; vi.vreferenced <- true;
(* insert [__e_acsl_globals_init ();] as first statement of [main] *) (* insert [__e_acsl_globals_init ();] as first statement of [main] *)
main.sbody.bstmts <- stmt :: main.sbody.bstmts; let main_fundec = try
Kernel_function.get_definition main
with _ -> assert false (* by construction, the main kf has a fundec *)
in
main_fundec.sbody.bstmts <- stmt :: main_fundec.sbody.bstmts;
(* Retrieve all globals except main *)
let main_vi = Globals.Functions.get_vi main in
let new_globals = let new_globals =
List.fold_left List.fold_left
(fun acc g -> match g with (fun acc g -> match g with
| GFun({ svar = vi }, _) when Varinfo.equal vi main.svar -> acc | GFun({ svar = vi }, _) when Varinfo.equal vi main_vi -> acc
| _ -> g :: acc) | _ -> g :: acc)
[] []
file.globals file.globals
...@@ -701,7 +707,7 @@ let inject_global_initializer file main = ...@@ -701,7 +707,7 @@ let inject_global_initializer file main =
| f :: l -> rev_and_extend (f :: acc) l | f :: l -> rev_and_extend (f :: acc) l
in in
(* [__e_acsl_globals_init] and [main] at the end *) (* [__e_acsl_globals_init] and [main] at the end *)
rev_and_extend [ cil_fct; GFun(main, Location.unknown) ] new_globals rev_and_extend [ cil_fct; GFun(main_fundec, Location.unknown) ] new_globals
in in
(* add the literal string varinfos as the very first globals *) (* add the literal string varinfos as the very first globals *)
let new_globals = let new_globals =
...@@ -725,18 +731,22 @@ let inject_mmodel_initializer main = ...@@ -725,18 +731,22 @@ let inject_mmodel_initializer main =
let loc = Location.unknown in let loc = Location.unknown in
let nulls = [ Cil.zero loc ; Cil.zero loc ] in let nulls = [ Cil.zero loc ; Cil.zero loc ] in
let handle_main main = let handle_main main =
let fundec =
try Kernel_function.get_definition main
with _ -> assert false (* by construction, the main kf has a fundec *)
in
let args = let args =
(* record arguments only if the second has a pointer type, so argument (* record arguments only if the second has a pointer type, so argument
strings can be recorded. This is sufficient to capture C99 compliant strings can be recorded. This is sufficient to capture C99 compliant
arguments and GCC extensions with environ. *) arguments and GCC extensions with environ. *)
match main.sformals with match fundec.sformals with
| [] -> | [] ->
(* no arguments to main given *) (* no arguments to main given *)
nulls nulls
| _argc :: argv :: _ when Cil.isPointerType argv.vtype -> | _argc :: argv :: _ when Cil.isPointerType argv.vtype ->
(* grab addresses of arguments for a call to the main initialization (* grab addresses of arguments for a call to the main initialization
function, i.e., [__e_acsl_memory_init] *) function, i.e., [__e_acsl_memory_init] *)
List.map Cil.mkAddrOfVi main.sformals; List.map Cil.mkAddrOfVi fundec.sformals;
| _ :: _ -> | _ :: _ ->
(* some non-standard arguments. *) (* some non-standard arguments. *)
nulls nulls
...@@ -744,7 +754,7 @@ let inject_mmodel_initializer main = ...@@ -744,7 +754,7 @@ let inject_mmodel_initializer main =
let ptr_size = Cil.sizeOf loc Cil.voidPtrType in let ptr_size = Cil.sizeOf loc Cil.voidPtrType in
let args = args @ [ ptr_size ] in let args = args @ [ ptr_size ] in
let init = Constructor.mk_rtl_call loc "memory_init" args in let init = Constructor.mk_rtl_call loc "memory_init" args in
main.sbody.bstmts <- init :: main.sbody.bstmts fundec.sbody.bstmts <- init :: fundec.sbody.bstmts
in in
Extlib.may handle_main main Extlib.may handle_main main
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment