Skip to content

Commit fe29750

Browse files
committed
Refactored the code of arm/PrintAsm.ml in order to allow the parametrization of the printing code over the configuration-dependent bits.
1 parent 64faa3f commit fe29750

File tree

1 file changed

+86
-44
lines changed

1 file changed

+86
-44
lines changed

arm/PrintAsm.ml

+86-44
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,30 @@ open AST
2020
open Memdata
2121
open Asm
2222

23+
(* Type for the ABI versions *)
24+
type float_abi_type =
25+
| Hard
26+
| Soft
27+
28+
(* Module type for the options *)
29+
module type PRINTER_OPTIONS =
30+
sig
31+
val float_abi: float_abi_type
32+
val vfpv3: bool
33+
val hardware_idiv: bool
34+
val cfi_startproc: out_channel -> unit
35+
val cfi_endproc: out_channel -> unit
36+
val cfi_adjust: out_channel -> int32 -> unit
37+
val cfi_rel_offset: out_channel -> string -> int32 -> unit
38+
val thumb: bool
39+
end
40+
41+
(* Module containing the printing functions *)
42+
43+
module AsmPrinter (Opt: PRINTER_OPTIONS) =
44+
(struct
2345
(* Code generation options. *)
2446

25-
let vfpv3 = Configuration.model >= "armv7"
26-
27-
let hardware_idiv () =
28-
match Configuration.model with
29-
| "armv7r" | "armv7m" -> !Clflags.option_mthumb
30-
| _ -> false
31-
3247
let literals_in_code = ref true (* to be turned into a proper option *)
3348

3449
(* On-the-fly label renaming *)
@@ -147,7 +162,7 @@ let neg_condition_name = function
147162
mode. *)
148163

149164
let thumbS oc =
150-
if !Clflags.option_mthumb then output_char oc 's'
165+
if Opt.thumb then output_char oc 's'
151166

152167
(* Names of sections *)
153168

@@ -324,24 +339,6 @@ let print_location oc loc =
324339
if loc <> Cutil.no_loc then
325340
print_file_line oc (fst loc) (string_of_int (snd loc))
326341

327-
(* Emit .cfi directives *)
328-
329-
let cfi_startproc oc =
330-
if Configuration.asm_supports_cfi then
331-
fprintf oc " .cfi_startproc\n"
332-
333-
let cfi_endproc oc =
334-
if Configuration.asm_supports_cfi then
335-
fprintf oc " .cfi_endproc\n"
336-
337-
let cfi_adjust oc delta =
338-
if Configuration.asm_supports_cfi then
339-
fprintf oc " .cfi_adjust_cfa_offset %ld\n" delta
340-
341-
let cfi_rel_offset oc reg ofs =
342-
if Configuration.asm_supports_cfi then
343-
fprintf oc " .cfi_rel_offset %s, %ld\n" reg ofs
344-
345342
(* Built-ins. They come in two flavors:
346343
- annotation statements: take their arguments in registers or stack
347344
locations; generate no code;
@@ -743,11 +740,10 @@ module FixupHF = struct
743740
end
744741

745742
let (fixup_arguments, fixup_result) =
746-
match Configuration.abi with
747-
| "eabi" -> (FixupEABI.fixup_arguments, FixupEABI.fixup_result)
748-
| "hardfloat" -> (FixupHF.fixup_arguments, FixupHF.fixup_result)
749-
| _ -> assert false
750-
743+
match Opt.float_abi with
744+
| Soft -> (FixupEABI.fixup_arguments, FixupEABI.fixup_result)
745+
| Hard -> (FixupHF.fixup_arguments, FixupHF.fixup_result)
746+
751747
(* Printing of instructions *)
752748

753749
let shift_op oc = function
@@ -840,7 +836,7 @@ let print_instruction oc = function
840836
| Pstr(r1, r2, sa) | Pstr_a(r1, r2, sa) ->
841837
fprintf oc " str %a, [%a, %a]\n" ireg r1 ireg r2 shift_op sa;
842838
begin match r1, r2, sa with
843-
| IR14, IR13, SOimm n -> cfi_rel_offset oc "lr" (camlint_of_coqint n)
839+
| IR14, IR13, SOimm n -> Opt.cfi_rel_offset oc "lr" (camlint_of_coqint n)
844840
| _ -> ()
845841
end;
846842
1
@@ -849,7 +845,7 @@ let print_instruction oc = function
849845
| Pstrh(r1, r2, sa) ->
850846
fprintf oc " strh %a, [%a, %a]\n" ireg r1 ireg r2 shift_op sa; 1
851847
| Psdiv ->
852-
if hardware_idiv() then begin
848+
if Opt.hardware_idiv then begin
853849
fprintf oc " sdiv r0, r0, r1\n"; 1
854850
end else begin
855851
fprintf oc " bl __aeabi_idiv\n"; 1
@@ -862,7 +858,7 @@ let print_instruction oc = function
862858
fprintf oc " sub%t %a, %a, %a\n"
863859
thumbS ireg r1 ireg r2 shift_op so; 1
864860
| Pudiv ->
865-
if hardware_idiv() then begin
861+
if Opt.hardware_idiv then begin
866862
fprintf oc " udiv r0, r0, r1\n"; 1
867863
end else begin
868864
fprintf oc " bl __aeabi_uidiv\n"; 1
@@ -886,7 +882,7 @@ let print_instruction oc = function
886882
fprintf oc " vsub.f64 %a, %a, %a\n" freg r1 freg r2 freg r3; 1
887883
| Pflid(r1, f) ->
888884
let f = camlint64_of_coqint(Floats.Float.to_bits f) in
889-
if vfpv3 && is_immediate_float64 f then begin
885+
if Opt.vfpv3 && is_immediate_float64 f then begin
890886
fprintf oc " vmov.f64 %a, #%F\n"
891887
freg r1 (Int64.float_of_bits f); 1
892888
(* immediate floats have at most 4 bits of fraction, so they
@@ -934,7 +930,7 @@ let print_instruction oc = function
934930
fprintf oc " vsub.f32 %a, %a, %a\n" freg_single r1 freg_single r2 freg_single r3; 1
935931
| Pflis(r1, f) ->
936932
let f = camlint_of_coqint(Floats.Float32.to_bits f) in
937-
if vfpv3 && is_immediate_float32 f then begin
933+
if Opt.vfpv3 && is_immediate_float32 f then begin
938934
fprintf oc " vmov.f32 %a, #%F\n"
939935
freg_single r1 (Int32.float_of_bits f); 1
940936
(* immediate floats have at most 4 bits of fraction, so they
@@ -984,11 +980,11 @@ let print_instruction oc = function
984980
fprintf oc " mov r12, sp\n";
985981
if (!current_function_sig).sig_cc.cc_vararg then begin
986982
fprintf oc " push {r0, r1, r2, r3}\n";
987-
cfi_adjust oc 16l
983+
Opt.cfi_adjust oc 16l
988984
end;
989985
let sz' = camlint_of_coqint sz in
990986
let ninstr = subimm oc "sp" "sp" sz in
991-
cfi_adjust oc sz';
987+
Opt.cfi_adjust oc sz';
992988
fprintf oc " str r12, [sp, #%a]\n" coqint ofs;
993989
current_function_stacksize := sz';
994990
ninstr + (if (!current_function_sig).sig_cc.cc_vararg then 3 else 2)
@@ -1104,11 +1100,11 @@ let print_function oc name fn =
11041100
fprintf oc " .thumb_func\n";
11051101
fprintf oc "%a:\n" print_symb name;
11061102
print_location oc (C2C.atom_location name);
1107-
cfi_startproc oc;
1103+
Opt.cfi_startproc oc;
11081104
ignore (fixup_arguments oc Incoming fn.fn_sig);
11091105
print_instructions oc fn.fn_code;
11101106
if !literals_in_code then emit_constants oc;
1111-
cfi_endproc oc;
1107+
Opt.cfi_endproc oc;
11121108
fprintf oc " .type %a, %%function\n" print_symb name;
11131109
fprintf oc " .size %a, . - %a\n" print_symb name print_symb name;
11141110
if not !literals_in_code && !size_constants > 0 then begin
@@ -1188,9 +1184,55 @@ let print_globdef oc (name, gdef) =
11881184
| Gfun(External ef) -> ()
11891185
| Gvar v -> print_var oc name v
11901186

1187+
end)
1188+
11911189
let print_program oc p =
1192-
PrintAnnot.print_version_and_options oc comment;
1193-
Hashtbl.clear filename_num;
1190+
let module Opt = (struct
1191+
1192+
let vfpv3 = Configuration.model >= "armv7"
1193+
1194+
let float_abi = match Configuration.abi with
1195+
| "eabi" -> Soft
1196+
| "hardfloat" -> Hard
1197+
| _ -> assert false
1198+
1199+
let hardware_idiv =
1200+
match Configuration.model with
1201+
| "armv7r" | "armv7m" -> !Clflags.option_mthumb
1202+
| _ -> false
1203+
1204+
1205+
(* Emit .cfi directives *)
1206+
let cfi_startproc =
1207+
if Configuration.asm_supports_cfi then
1208+
(fun oc -> fprintf oc " .cfi_startproc\n")
1209+
else
1210+
(fun _ -> ())
1211+
1212+
let cfi_endproc =
1213+
if Configuration.asm_supports_cfi then
1214+
(fun oc ->fprintf oc " .cfi_endproc\n")
1215+
else
1216+
(fun _ -> ())
1217+
1218+
let cfi_adjust =
1219+
if Configuration.asm_supports_cfi then
1220+
(fun oc delta -> fprintf oc " .cfi_adjust_cfa_offset %ld\n" delta)
1221+
else
1222+
(fun _ _ -> ())
1223+
1224+
let cfi_rel_offset =
1225+
if Configuration.asm_supports_cfi then
1226+
(fun oc reg ofs -> fprintf oc " .cfi_rel_offset %s, %ld\n" reg ofs)
1227+
else
1228+
(fun _ _ _ -> ())
1229+
1230+
let thumb = !Clflags.option_mthumb
1231+
1232+
end: PRINTER_OPTIONS) in
1233+
let module Printer = AsmPrinter(Opt) in
1234+
PrintAnnot.print_version_and_options oc Printer.comment;
1235+
Hashtbl.clear Printer.filename_num;
11941236
fprintf oc " .syntax unified\n";
11951237
fprintf oc " .arch %s\n"
11961238
(match Configuration.model with
@@ -1200,8 +1242,8 @@ let print_program oc p =
12001242
| "armv7m" -> "armv7-m"
12011243
| _ -> "armv7");
12021244
fprintf oc " .fpu %s\n"
1203-
(if vfpv3 then "vfpv3-d16" else "vfpv2");
1245+
(if Opt.vfpv3 then "vfpv3-d16" else "vfpv2");
12041246
fprintf oc " .%s\n" (if !Clflags.option_mthumb then "thumb" else "arm");
1205-
List.iter (print_globdef oc) p.prog_defs
1247+
List.iter (Printer.print_globdef oc) p.prog_defs
12061248

12071249

0 commit comments

Comments
 (0)