Skip to content

Commit 45cda5d

Browse files
committed
copy over force digits change
1 parent 246f6df commit 45cda5d

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
@@ -86,40 +86,63 @@ module Directive = struct
8686
| Add of t * t
8787
| Sub of t * t
8888

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

96-
and print_subterm buf t =
110+
and print_subterm ~force_digits buf t =
97111
match t with
98112
| This -> (
99113
match TS.assembler () with
100114
| MacOS | GAS_like -> Buffer.add_string buf "."
101115
| MASM -> Buffer.add_string buf "THIS BYTE")
102116
| Named_thing name -> Buffer.add_string buf name
103117
| Signed_int n -> (
104-
match TS.assembler () with
105-
(* We use %Ld and not %Lx on Unix-like platforms to ensure that
106-
".sleb128" directives do not end up with hex arguments (since this
107-
denotes a variable-length encoding it would not be clear where the
108-
sign bit is). *)
109-
| MacOS | GAS_like -> bprintf buf "%Ld" n
110-
| MASM ->
111-
if Int64.compare n 0x8000_0000L >= 0
112-
&& Int64.compare n 0x7fff_ffffL <= 0
118+
match TS.assembler (), force_digits with
119+
| _, true -> Buffer.add_string buf (Int64.to_string n)
120+
| MASM, _ ->
121+
if Int64.compare n 0x7FFF_FFFFL <= 0
122+
&& Int64.compare n (-0x8000_0000L) >= 0
123+
then Buffer.add_string buf (Int64.to_string n)
124+
else bprintf buf "0%LxH" n
125+
| _, false ->
126+
if Int64.compare n 0x7FFF_FFFFL <= 0
127+
&& Int64.compare n (-0x8000_0000L) >= 0
113128
then Buffer.add_string buf (Int64.to_string n)
114-
else bprintf buf "0%LxH" n)
129+
else bprintf buf "0x%Lx" n)
115130
| Unsigned_int n ->
116131
(* We can use the printer for [Signed_int] since we always print as an
117132
unsigned hex representation. *)
118-
print_subterm buf (Signed_int (Uint64.to_int64 n))
133+
print_subterm ~force_digits buf (Signed_int (Uint64.to_int64 n))
119134
| Add (c1, c2) ->
120-
bprintf buf "(%a + %a)" print_subterm c1 print_subterm c2
135+
bprintf buf "(%a + %a)"
136+
(print_subterm ~force_digits)
137+
c1
138+
(print_subterm ~force_digits)
139+
c2
121140
| Sub (c1, c2) ->
122-
bprintf buf "(%a - %a)" print_subterm c1 print_subterm c2
141+
bprintf buf "(%a - %a)"
142+
(print_subterm ~force_digits)
143+
c1
144+
(print_subterm ~force_digits)
145+
c2
123146
end
124147

125148
module Constant_with_width = struct
@@ -323,7 +346,8 @@ module Directive = struct
323346
| Sixty_four -> "8byte"
324347
in
325348
let comment = gas_comment_opt comment in
326-
bprintf buf "\t.%s\t%a%s" directive Constant.print
349+
bprintf buf "\t.%s\t%a%s" directive
350+
(Constant.print ~force_digits:false)
327351
(Constant_with_width.constant constant)
328352
comment
329353
| Bytes { str; comment } ->
@@ -377,10 +401,13 @@ module Directive = struct
377401
bprintf buf "\t.loc\t%d\t%d%a%a" file_num line print_col col
378402
print_discriminator discriminator
379403
| Private_extern s -> bprintf buf "\t.private_extern %s" s
380-
| Size (s, c) -> bprintf buf "\t.size %s,%a" s Constant.print c
404+
| Size (s, c) ->
405+
bprintf buf "\t.size %s,%a" s (Constant.print ~force_digits:false) c
381406
| Sleb128 { constant; comment } ->
382407
let comment = gas_comment_opt comment in
383-
bprintf buf "\t.sleb128\t%a%s" Constant.print constant comment
408+
bprintf buf "\t.sleb128\t%a%s"
409+
(Constant.print ~force_digits:false)
410+
constant comment
384411
| Type (s, typ) ->
385412
let typ = symbol_type_to_string typ in
386413
(* CR sspies: Technically, ",STT_OBJECT" violates the assembler syntax
@@ -390,10 +417,15 @@ module Directive = struct
390417
bprintf buf "\t.type %s,%s" s typ
391418
| Uleb128 { constant; comment } ->
392419
let comment = gas_comment_opt comment in
393-
bprintf buf "\t.uleb128\t%a%s" Constant.print constant comment
420+
bprintf buf "\t.uleb128\t%a%s"
421+
(Constant.print ~force_digits:true)
422+
constant comment
394423
| Direct_assignment (var, const) -> (
395424
match TS.assembler () with
396-
| MacOS -> bprintf buf "\t.set %s, %a" var Constant.print const
425+
| MacOS ->
426+
bprintf buf "\t.set %s, %a" var
427+
(Constant.print ~force_digits:true)
428+
const
397429
| _ ->
398430
Misc.fatal_error
399431
"Cannot emit [Direct_assignment] except on macOS-like assemblers")
@@ -403,9 +435,12 @@ module Directive = struct
403435
(* masm only *)
404436
| External _ -> assert false
405437
| Reloc { offset; name; expr } ->
406-
bprintf buf "\t.reloc\t%a, %s, %a" Constant.print offset
438+
bprintf buf "\t.reloc\t%a, %s, %a"
439+
(Constant.print ~force_digits:false)
440+
offset
407441
(reloc_type_to_string name)
408-
Constant.print expr
442+
(Constant.print ~force_digits:false)
443+
expr
409444

410445
let print_masm buf t =
411446
let unsupported name =
@@ -437,7 +472,8 @@ module Directive = struct
437472
| Sixty_four -> "QWORD"
438473
in
439474
let comment = masm_comment_opt comment in
440-
bprintf buf "\t%s\t%a%s" directive Constant.print
475+
bprintf buf "\t%s\t%a%s" directive
476+
(Constant.print ~force_digits:false)
441477
(Constant_with_width.constant constant)
442478
comment
443479
| Global s -> bprintf buf "\tPUBLIC\t%s" s

0 commit comments

Comments
 (0)