@@ -20,15 +20,30 @@ open AST
20
20
open Memdata
21
21
open Asm
22
22
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
23
45
(* Code generation options. *)
24
46
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
-
32
47
let literals_in_code = ref true (* to be turned into a proper option *)
33
48
34
49
(* On-the-fly label renaming *)
@@ -147,7 +162,7 @@ let neg_condition_name = function
147
162
mode. *)
148
163
149
164
let thumbS oc =
150
- if ! Clflags. option_mthumb then output_char oc 's'
165
+ if Opt. thumb then output_char oc 's'
151
166
152
167
(* Names of sections *)
153
168
@@ -324,24 +339,6 @@ let print_location oc loc =
324
339
if loc <> Cutil. no_loc then
325
340
print_file_line oc (fst loc) (string_of_int (snd loc))
326
341
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
-
345
342
(* Built-ins. They come in two flavors:
346
343
- annotation statements: take their arguments in registers or stack
347
344
locations; generate no code;
@@ -743,11 +740,10 @@ module FixupHF = struct
743
740
end
744
741
745
742
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
+
751
747
(* Printing of instructions *)
752
748
753
749
let shift_op oc = function
@@ -840,7 +836,7 @@ let print_instruction oc = function
840
836
| Pstr (r1 , r2 , sa ) | Pstr_a (r1 , r2 , sa ) ->
841
837
fprintf oc " str %a, [%a, %a]\n " ireg r1 ireg r2 shift_op sa;
842
838
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)
844
840
| _ -> ()
845
841
end ;
846
842
1
@@ -849,7 +845,7 @@ let print_instruction oc = function
849
845
| Pstrh (r1 , r2 , sa ) ->
850
846
fprintf oc " strh %a, [%a, %a]\n " ireg r1 ireg r2 shift_op sa; 1
851
847
| Psdiv ->
852
- if hardware_idiv() then begin
848
+ if Opt. hardware_idiv then begin
853
849
fprintf oc " sdiv r0, r0, r1\n " ; 1
854
850
end else begin
855
851
fprintf oc " bl __aeabi_idiv\n " ; 1
@@ -862,7 +858,7 @@ let print_instruction oc = function
862
858
fprintf oc " sub%t %a, %a, %a\n "
863
859
thumbS ireg r1 ireg r2 shift_op so; 1
864
860
| Pudiv ->
865
- if hardware_idiv() then begin
861
+ if Opt. hardware_idiv then begin
866
862
fprintf oc " udiv r0, r0, r1\n " ; 1
867
863
end else begin
868
864
fprintf oc " bl __aeabi_uidiv\n " ; 1
@@ -886,7 +882,7 @@ let print_instruction oc = function
886
882
fprintf oc " vsub.f64 %a, %a, %a\n " freg r1 freg r2 freg r3; 1
887
883
| Pflid (r1 , f ) ->
888
884
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
890
886
fprintf oc " vmov.f64 %a, #%F\n "
891
887
freg r1 (Int64. float_of_bits f); 1
892
888
(* immediate floats have at most 4 bits of fraction, so they
@@ -934,7 +930,7 @@ let print_instruction oc = function
934
930
fprintf oc " vsub.f32 %a, %a, %a\n " freg_single r1 freg_single r2 freg_single r3; 1
935
931
| Pflis (r1 , f ) ->
936
932
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
938
934
fprintf oc " vmov.f32 %a, #%F\n "
939
935
freg_single r1 (Int32. float_of_bits f); 1
940
936
(* immediate floats have at most 4 bits of fraction, so they
@@ -984,11 +980,11 @@ let print_instruction oc = function
984
980
fprintf oc " mov r12, sp\n " ;
985
981
if (! current_function_sig).sig_cc.cc_vararg then begin
986
982
fprintf oc " push {r0, r1, r2, r3}\n " ;
987
- cfi_adjust oc 16l
983
+ Opt. cfi_adjust oc 16l
988
984
end ;
989
985
let sz' = camlint_of_coqint sz in
990
986
let ninstr = subimm oc " sp" " sp" sz in
991
- cfi_adjust oc sz';
987
+ Opt. cfi_adjust oc sz';
992
988
fprintf oc " str r12, [sp, #%a]\n " coqint ofs;
993
989
current_function_stacksize := sz';
994
990
ninstr + (if (! current_function_sig).sig_cc.cc_vararg then 3 else 2 )
@@ -1104,11 +1100,11 @@ let print_function oc name fn =
1104
1100
fprintf oc " .thumb_func\n " ;
1105
1101
fprintf oc " %a:\n " print_symb name;
1106
1102
print_location oc (C2C. atom_location name);
1107
- cfi_startproc oc;
1103
+ Opt. cfi_startproc oc;
1108
1104
ignore (fixup_arguments oc Incoming fn.fn_sig);
1109
1105
print_instructions oc fn.fn_code;
1110
1106
if ! literals_in_code then emit_constants oc;
1111
- cfi_endproc oc;
1107
+ Opt. cfi_endproc oc;
1112
1108
fprintf oc " .type %a, %%function\n " print_symb name;
1113
1109
fprintf oc " .size %a, . - %a\n " print_symb name print_symb name;
1114
1110
if not ! literals_in_code && ! size_constants > 0 then begin
@@ -1188,9 +1184,55 @@ let print_globdef oc (name, gdef) =
1188
1184
| Gfun (External ef ) -> ()
1189
1185
| Gvar v -> print_var oc name v
1190
1186
1187
+ end )
1188
+
1191
1189
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;
1194
1236
fprintf oc " .syntax unified\n " ;
1195
1237
fprintf oc " .arch %s\n "
1196
1238
(match Configuration. model with
@@ -1200,8 +1242,8 @@ let print_program oc p =
1200
1242
| "armv7m" -> " armv7-m"
1201
1243
| _ -> " armv7" );
1202
1244
fprintf oc " .fpu %s\n "
1203
- (if vfpv3 then " vfpv3-d16" else " vfpv2" );
1245
+ (if Opt. vfpv3 then " vfpv3-d16" else " vfpv2" );
1204
1246
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
1206
1248
1207
1249
0 commit comments