Skip to content

Commit 654f717

Browse files
committed
copy over force digits change
1 parent 1c0fafb commit 654f717

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

backend/asm_targets/asm_directives_new.ml

+62-26
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,63 @@ module Directive = struct
8282
| Add of t * t
8383
| Sub of t * t
8484

85-
let rec print buf t =
85+
(* CR sspies: The printing change here is needed for the Arm backend. It
86+
currently uses this printer and hence, without the change, prints always
87+
digits. It does not affect x86 in this version, since that one is printed
88+
via the print_gas/print_masm in this code base. *)
89+
let rec print ~force_digits buf t =
8690
match t with
8791
| (Named_thing _ | Signed_int _ | Unsigned_int _ | This) as c ->
88-
print_subterm buf c
89-
| Add (c1, c2) -> bprintf buf "%a + %a" print_subterm c1 print_subterm c2
90-
| Sub (c1, c2) -> bprintf buf "%a - %a" print_subterm c1 print_subterm c2
92+
print_subterm ~force_digits buf c
93+
| Add (c1, c2) ->
94+
bprintf buf "%a + %a"
95+
(print_subterm ~force_digits)
96+
c1
97+
(print_subterm ~force_digits)
98+
c2
99+
| Sub (c1, c2) ->
100+
bprintf buf "%a - %a"
101+
(print_subterm ~force_digits)
102+
c1
103+
(print_subterm ~force_digits)
104+
c2
91105

92-
and print_subterm buf t =
106+
and print_subterm ~force_digits buf t =
93107
match t with
94108
| This -> (
95109
match TS.assembler () with
96110
| MacOS | GAS_like -> Buffer.add_string buf "."
97111
| MASM -> Buffer.add_string buf "THIS BYTE")
98112
| Named_thing name -> Buffer.add_string buf name
99113
| Signed_int n -> (
100-
match TS.assembler () with
101-
(* We use %Ld and not %Lx on Unix-like platforms to ensure that
102-
".sleb128" directives do not end up with hex arguments (since this
103-
denotes a variable-length encoding it would not be clear where the
104-
sign bit is). *)
105-
| MacOS | GAS_like -> bprintf buf "%Ld" n
106-
| MASM ->
107-
if Int64.compare n 0x8000_0000L >= 0
108-
&& Int64.compare n 0x7fff_ffffL <= 0
114+
match TS.assembler (), force_digits with
115+
| _, true -> Buffer.add_string buf (Int64.to_string n)
116+
| MASM, _ ->
117+
if Int64.compare n 0x7FFF_FFFFL <= 0
118+
&& Int64.compare n (-0x8000_0000L) >= 0
119+
then Buffer.add_string buf (Int64.to_string n)
120+
else bprintf buf "0%LxH" n
121+
| _, false ->
122+
if Int64.compare n 0x7FFF_FFFFL <= 0
123+
&& Int64.compare n (-0x8000_0000L) >= 0
109124
then Buffer.add_string buf (Int64.to_string n)
110-
else bprintf buf "0%LxH" n)
125+
else bprintf buf "0x%Lx" n)
111126
| Unsigned_int n ->
112127
(* We can use the printer for [Signed_int] since we always print as an
113128
unsigned hex representation. *)
114-
print_subterm buf (Signed_int (Uint64.to_int64 n))
129+
print_subterm ~force_digits buf (Signed_int (Uint64.to_int64 n))
115130
| Add (c1, c2) ->
116-
bprintf buf "(%a + %a)" print_subterm c1 print_subterm c2
131+
bprintf buf "(%a + %a)"
132+
(print_subterm ~force_digits)
133+
c1
134+
(print_subterm ~force_digits)
135+
c2
117136
| Sub (c1, c2) ->
118-
bprintf buf "(%a - %a)" print_subterm c1 print_subterm c2
137+
bprintf buf "(%a - %a)"
138+
(print_subterm ~force_digits)
139+
c1
140+
(print_subterm ~force_digits)
141+
c2
119142
end
120143

121144
module Constant_with_width = struct
@@ -318,7 +341,8 @@ module Directive = struct
318341
| Sixty_four -> "8byte"
319342
in
320343
let comment = gas_comment_opt comment in
321-
bprintf buf "\t.%s\t%a%s" directive Constant.print
344+
bprintf buf "\t.%s\t%a%s" directive
345+
(Constant.print ~force_digits:false)
322346
(Constant_with_width.constant constant)
323347
comment
324348
| Bytes { str; comment } ->
@@ -372,10 +396,13 @@ module Directive = struct
372396
bprintf buf "\t.loc\t%d\t%d%a%a" file_num line print_col col
373397
print_discriminator discriminator
374398
| Private_extern s -> bprintf buf "\t.private_extern %s" s
375-
| Size (s, c) -> bprintf buf "\t.size %s,%a" s Constant.print c
399+
| Size (s, c) ->
400+
bprintf buf "\t.size %s,%a" s (Constant.print ~force_digits:false) c
376401
| Sleb128 { constant; comment } ->
377402
let comment = gas_comment_opt comment in
378-
bprintf buf "\t.sleb128\t%a%s" Constant.print constant comment
403+
bprintf buf "\t.sleb128\t%a%s"
404+
(Constant.print ~force_digits:false)
405+
constant comment
379406
| Type (s, typ) ->
380407
let typ = symbol_type_to_string typ in
381408
(* CR sspies: Technically, ",STT_OBJECT" violates the assembler syntax
@@ -385,10 +412,15 @@ module Directive = struct
385412
bprintf buf "\t.type %s,%s" s typ
386413
| Uleb128 { constant; comment } ->
387414
let comment = gas_comment_opt comment in
388-
bprintf buf "\t.uleb128\t%a%s" Constant.print constant comment
415+
bprintf buf "\t.uleb128\t%a%s"
416+
(Constant.print ~force_digits:true)
417+
constant comment
389418
| Direct_assignment (var, const) -> (
390419
match TS.assembler () with
391-
| MacOS -> bprintf buf "\t.set %s, %a" var Constant.print const
420+
| MacOS ->
421+
bprintf buf "\t.set %s, %a" var
422+
(Constant.print ~force_digits:true)
423+
const
392424
| _ ->
393425
Misc.fatal_error
394426
"Cannot emit [Direct_assignment] except on macOS-like assemblers")
@@ -398,9 +430,12 @@ module Directive = struct
398430
(* masm only *)
399431
| External _ -> assert false
400432
| Reloc { offset; name; expr } ->
401-
bprintf buf "\t.reloc\t%a, %s, %a" Constant.print offset
433+
bprintf buf "\t.reloc\t%a, %s, %a"
434+
(Constant.print ~force_digits:false)
435+
offset
402436
(reloc_type_to_string name)
403-
Constant.print expr
437+
(Constant.print ~force_digits:false)
438+
expr
404439

405440
let print_masm buf t =
406441
let unsupported name =
@@ -432,7 +467,8 @@ module Directive = struct
432467
| Sixty_four -> "QWORD"
433468
in
434469
let comment = masm_comment_opt comment in
435-
bprintf buf "\t%s\t%a%s" directive Constant.print
470+
bprintf buf "\t%s\t%a%s" directive
471+
(Constant.print ~force_digits:false)
436472
(Constant_with_width.constant constant)
437473
comment
438474
| Global s -> bprintf buf "\tPUBLIC\t%s" s

0 commit comments

Comments
 (0)