@@ -82,40 +82,63 @@ module Directive = struct
82
82
| Add of t * t
83
83
| Sub of t * t
84
84
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 =
86
90
match t with
87
91
| (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
91
105
92
- and print_subterm buf t =
106
+ and print_subterm ~ force_digits buf t =
93
107
match t with
94
108
| This -> (
95
109
match TS. assembler () with
96
110
| MacOS | GAS_like -> Buffer. add_string buf " ."
97
111
| MASM -> Buffer. add_string buf " THIS BYTE" )
98
112
| Named_thing name -> Buffer. add_string buf name
99
113
| 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
109
124
then Buffer. add_string buf (Int64. to_string n)
110
- else bprintf buf " 0%LxH " n)
125
+ else bprintf buf " 0x%Lx " n)
111
126
| Unsigned_int n ->
112
127
(* We can use the printer for [Signed_int] since we always print as an
113
128
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))
115
130
| 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
117
136
| 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
119
142
end
120
143
121
144
module Constant_with_width = struct
@@ -318,7 +341,8 @@ module Directive = struct
318
341
| Sixty_four -> " 8byte"
319
342
in
320
343
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 )
322
346
(Constant_with_width. constant constant)
323
347
comment
324
348
| Bytes { str; comment } ->
@@ -372,10 +396,13 @@ module Directive = struct
372
396
bprintf buf " \t .loc\t %d\t %d%a%a" file_num line print_col col
373
397
print_discriminator discriminator
374
398
| 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
376
401
| Sleb128 { constant; comment } ->
377
402
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
379
406
| Type (s , typ ) ->
380
407
let typ = symbol_type_to_string typ in
381
408
(* CR sspies: Technically, ",STT_OBJECT" violates the assembler syntax
@@ -385,10 +412,15 @@ module Directive = struct
385
412
bprintf buf " \t .type %s,%s" s typ
386
413
| Uleb128 { constant; comment } ->
387
414
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
389
418
| Direct_assignment (var , const ) -> (
390
419
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
392
424
| _ ->
393
425
Misc. fatal_error
394
426
" Cannot emit [Direct_assignment] except on macOS-like assemblers" )
@@ -398,9 +430,12 @@ module Directive = struct
398
430
(* masm only *)
399
431
| External _ -> assert false
400
432
| 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
402
436
(reloc_type_to_string name)
403
- Constant. print expr
437
+ (Constant. print ~force_digits: false )
438
+ expr
404
439
405
440
let print_masm buf t =
406
441
let unsupported name =
@@ -432,7 +467,8 @@ module Directive = struct
432
467
| Sixty_four -> " QWORD"
433
468
in
434
469
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 )
436
472
(Constant_with_width. constant constant)
437
473
comment
438
474
| Global s -> bprintf buf " \t PUBLIC\t %s" s
0 commit comments