From efc51afc7c8298ecd3b511b8d0faf9ff6d2df22e Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 27 Sep 2022 12:04:31 +0200 Subject: Revised passing of options to `Parse.preprocessed_file` Instead of passing a string that encodes the optional source-to-source transformations to perform, just pass one optional, named argument for each transformation. It is clearer and easier to extend this way. --- cparser/Parse.ml | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) (limited to 'cparser/Parse.ml') diff --git a/cparser/Parse.ml b/cparser/Parse.ml index a54af0cc..c0419301 100644 --- a/cparser/Parse.ml +++ b/cparser/Parse.ml @@ -16,33 +16,16 @@ (* Entry point for the library: parse, elaborate, and transform *) -module CharSet = Set.Make(struct type t = char let compare = compare end) - -let transform_program t p = - let run_pass pass flag p = - if CharSet.mem flag t then begin - let p = pass p in - Diagnostics.check_errors (); - p - end else - p - in - p - |> run_pass Unblock.program 'b' - |> run_pass PackedStructs.program 'p' - |> run_pass StructPassing.program 's' - |> Rename.program - -let parse_transformations s = - let t = ref CharSet.empty in - let set s = String.iter (fun c -> t := CharSet.add c !t) s in - String.iter - (function 'b' -> set "b" - | 's' -> set "s" - | 'p' -> set "bp" - | _ -> ()) - s; - !t +let transform_program ~unblock ~struct_passing ~packed_structs p = + let run_pass pass p = + let p' = pass p in Diagnostics.check_errors (); p' in + let run_opt_pass pass flag p = + if flag then run_pass pass p else p in + p + |> run_opt_pass Unblock.program (unblock || packed_structs) + |> run_opt_pass PackedStructs.program packed_structs + |> run_opt_pass StructPassing.program struct_passing + |> Rename.program let read_file sourcefile = let ic = open_in_bin sourcefile in @@ -65,7 +48,10 @@ let parse_string name text = Timeout_pr means that we ran for 2^50 steps. *) Diagnostics.fatal_error Diagnostics.no_loc "internal error while parsing" -let preprocessed_file transfs name sourcefile = +let preprocessed_file ?(unblock = false) + ?(struct_passing = false) + ?(packed_structs = false) + name sourcefile = Diagnostics.reset(); let check_errors x = Diagnostics.check_errors(); x in @@ -79,5 +65,6 @@ let preprocessed_file transfs name sourcefile = |> Timing.time2 "Parsing" parse_string name |> Timing.time "Elaboration" Elab.elab_file |> check_errors - |> Timing.time2 "Emulations" transform_program (parse_transformations transfs) + |> Timing.time "Emulations" + (transform_program ~unblock ~struct_passing ~packed_structs) |> check_errors -- cgit From a1dabb4792446538cce24eb87bcd3ccb3c09f18b Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Tue, 27 Sep 2022 12:31:07 +0200 Subject: Handle unstructured 'switch' statements such as Duff's device - New elaboration pass: SwitchNorm - recognizes structured 'switch' statements and puts them in a normalized form; - if selected, transforms unstructured 'switch' statements into a structured switch with goto actions + the original switch body with appropriate labels and gotos. - C2C treatment of 'switch' statements is simplified accordingly. - New language support option `-funstructured-switch`. - Some tests were added (test/regression/switch3.c). --- cparser/Parse.ml | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'cparser/Parse.ml') diff --git a/cparser/Parse.ml b/cparser/Parse.ml index c0419301..607d8927 100644 --- a/cparser/Parse.ml +++ b/cparser/Parse.ml @@ -16,13 +16,20 @@ (* Entry point for the library: parse, elaborate, and transform *) -let transform_program ~unblock ~struct_passing ~packed_structs p = +let transform_program ~unblock ~switch_norm ~struct_passing ~packed_structs p = let run_pass pass p = let p' = pass p in Diagnostics.check_errors (); p' in let run_opt_pass pass flag p = - if flag then run_pass pass p else p in + if flag then run_pass pass p else p + and run_opt_pass3 pass flag p = + match flag with + | `Off -> p + | `Partial -> run_pass (pass false) p + | `Full -> run_pass (pass true) p in + let unblock = unblock || switch_norm <> `Off || packed_structs in p - |> run_opt_pass Unblock.program (unblock || packed_structs) + |> run_opt_pass Unblock.program unblock + |> run_opt_pass3 SwitchNorm.program switch_norm |> run_opt_pass PackedStructs.program packed_structs |> run_opt_pass StructPassing.program struct_passing |> Rename.program @@ -49,6 +56,7 @@ let parse_string name text = Diagnostics.fatal_error Diagnostics.no_loc "internal error while parsing" let preprocessed_file ?(unblock = false) + ?(switch_norm = `Off) ?(struct_passing = false) ?(packed_structs = false) name sourcefile = @@ -66,5 +74,5 @@ let preprocessed_file ?(unblock = false) |> Timing.time "Elaboration" Elab.elab_file |> check_errors |> Timing.time "Emulations" - (transform_program ~unblock ~struct_passing ~packed_structs) + (transform_program ~unblock ~switch_norm ~struct_passing ~packed_structs) |> check_errors -- cgit