@@ -86,40 +86,63 @@ module Directive = struct
86
86
| Add of t * t
87
87
| Sub of t * t
88
88
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 =
90
94
match t with
91
95
| (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
95
109
96
- and print_subterm buf t =
110
+ and print_subterm ~ force_digits buf t =
97
111
match t with
98
112
| This -> (
99
113
match TS. assembler () with
100
114
| MacOS | GAS_like -> Buffer. add_string buf " ."
101
115
| MASM -> Buffer. add_string buf " THIS BYTE" )
102
116
| Named_thing name -> Buffer. add_string buf name
103
117
| 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
113
128
then Buffer. add_string buf (Int64. to_string n)
114
- else bprintf buf " 0%LxH " n)
129
+ else bprintf buf " 0x%Lx " n)
115
130
| Unsigned_int n ->
116
131
(* We can use the printer for [Signed_int] since we always print as an
117
132
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))
119
134
| 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
121
140
| 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
123
146
end
124
147
125
148
module Constant_with_width = struct
@@ -323,7 +346,8 @@ module Directive = struct
323
346
| Sixty_four -> " 8byte"
324
347
in
325
348
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 )
327
351
(Constant_with_width. constant constant)
328
352
comment
329
353
| Bytes { str; comment } ->
@@ -377,10 +401,13 @@ module Directive = struct
377
401
bprintf buf " \t .loc\t %d\t %d%a%a" file_num line print_col col
378
402
print_discriminator discriminator
379
403
| 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
381
406
| Sleb128 { constant; comment } ->
382
407
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
384
411
| Type (s , typ ) ->
385
412
let typ = symbol_type_to_string typ in
386
413
(* CR sspies: Technically, ",STT_OBJECT" violates the assembler syntax
@@ -390,10 +417,15 @@ module Directive = struct
390
417
bprintf buf " \t .type %s,%s" s typ
391
418
| Uleb128 { constant; comment } ->
392
419
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
394
423
| Direct_assignment (var , const ) -> (
395
424
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
397
429
| _ ->
398
430
Misc. fatal_error
399
431
" Cannot emit [Direct_assignment] except on macOS-like assemblers" )
@@ -403,9 +435,12 @@ module Directive = struct
403
435
(* masm only *)
404
436
| External _ -> assert false
405
437
| 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
407
441
(reloc_type_to_string name)
408
- Constant. print expr
442
+ (Constant. print ~force_digits: false )
443
+ expr
409
444
410
445
let print_masm buf t =
411
446
let unsupported name =
@@ -437,7 +472,8 @@ module Directive = struct
437
472
| Sixty_four -> " QWORD"
438
473
in
439
474
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 )
441
477
(Constant_with_width. constant constant)
442
478
comment
443
479
| Global s -> bprintf buf " \t PUBLIC\t %s" s
0 commit comments