Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8c7611e

Browse files
committedApr 25, 2025·
new directives on x86
1 parent b3f70e5 commit 8c7611e

11 files changed

+606
-419
lines changed
 

‎backend/amd64/emit.ml

Lines changed: 421 additions & 364 deletions
Large diffs are not rendered by default.

‎backend/arm64/emit.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ let emit_literals p align emit_literal =
777777
(* CR sspies: The following section is incorrect. We are in a data section
778778
here. Fix this when cleaning up the section mechanism. *)
779779
D.unsafe_set_interal_section_ref Text);
780-
D.align ~bytes:align;
780+
D.align ~data_section:false ~bytes:align;
781781
List.iter emit_literal !p;
782782
p := [])
783783

@@ -2068,7 +2068,7 @@ let fundecl fundecl =
20682068
contains_calls := fundecl.fun_contains_calls;
20692069
emit_named_text_section !function_name;
20702070
let fun_sym = S.create fundecl.fun_name in
2071-
D.align ~bytes:8;
2071+
D.align ~data_section:false ~bytes:8;
20722072
D.global fun_sym;
20732073
D.type_symbol ~ty:Function fun_sym;
20742074
D.define_symbol_label ~section:Text fun_sym;
@@ -2132,11 +2132,11 @@ let emit_item (d : Cmm.data_item) =
21322132
D.symbol_plus_offset ~offset_in_bytes:(Targetint.of_int o) sym
21332133
| Cstring s -> D.string s
21342134
| Cskip n -> if n > 0 then D.space ~bytes:n
2135-
| Calign n -> D.align ~bytes:n
2135+
| Calign n -> D.align ~data_section:true ~bytes:n
21362136

21372137
let data l =
21382138
D.data ();
2139-
D.align ~bytes:8;
2139+
D.align ~data_section:true ~bytes:8;
21402140
List.iter emit_item l
21412141

21422142
let file_emitter ~file_num ~file_name =
@@ -2174,7 +2174,7 @@ let begin_assembly _unix =
21742174
if macosx
21752175
then (
21762176
DSL.ins I.NOP [||];
2177-
D.align ~bytes:8);
2177+
D.align ~data_section:false ~bytes:8);
21782178
let code_end = Cmm_helpers.make_symbol "code_end" in
21792179
Emitaux.Dwarf_helpers.begin_dwarf ~code_begin ~code_end ~file_emitter
21802180

@@ -2192,7 +2192,7 @@ let end_assembly () =
21922192
D.global data_end_sym;
21932193
D.define_symbol_label ~section:Data data_end_sym;
21942194
D.int64 0L;
2195-
D.align ~bytes:8;
2195+
D.align ~data_section:true ~bytes:8;
21962196
(* #7887 *)
21972197
let frametable = Cmm_helpers.make_symbol "frametable" in
21982198
let frametable_sym = S.create frametable in
@@ -2221,7 +2221,7 @@ let end_assembly () =
22212221
(* [efa_word] is [D.targetint (Targetint.of_int_exn n)] in
22222222
mshinwell/ocaml#gdb-names-gpr x86 emitter *)
22232223
efa_word = (fun n -> D.targetint (Targetint.of_int_exn n));
2224-
efa_align = (fun n -> D.align ~bytes:n);
2224+
efa_align = (fun n -> D.align ~data_section:true ~bytes:n);
22252225
efa_label_rel =
22262226
(fun lbl ofs ->
22272227
let lbl = label_to_asm_label ~section:Data lbl in

‎backend/asm_targets/asm_directives_new.ml

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,15 @@ module Directive = struct
150150
| Code
151151
| Machine_width_data
152152

153+
type reloc_type = R_X86_64_PLT32
154+
153155
type comment = string
154156

155157
type t =
156-
| Align of { bytes : int }
158+
| Align of
159+
{ bytes : int;
160+
data_section : bool
161+
}
157162
| Bytes of
158163
{ str : string;
159164
comment : string option
@@ -207,6 +212,14 @@ module Directive = struct
207212
comment : string option
208213
}
209214
| Protected of string
215+
| Hidden of string
216+
| Weak of string
217+
| External of string
218+
| Reloc of
219+
{ offset : Constant.t;
220+
name : reloc_type;
221+
expr : Constant.t
222+
}
210223

211224
let bprintf = Printf.bprintf
212225

@@ -271,6 +284,8 @@ module Directive = struct
271284
bprintf buf "\t.ascii\t\"%s\""
272285
(string_of_string_literal (String.sub s !i (l - !i)))
273286

287+
let reloc_type_to_string = function R_X86_64_PLT32 -> "R_X86_64_PLT32"
288+
274289
let print_gas buf t =
275290
let gas_comment_opt comment_opt =
276291
if not (emit_comments ())
@@ -281,7 +296,9 @@ module Directive = struct
281296
| Some comment -> Printf.sprintf "\t/* %s */" comment
282297
in
283298
match t with
284-
| Align { bytes = n } ->
299+
| Align { bytes = n; data_section = _ } ->
300+
(* The data_section is only relevant for the binary emitter. On GAS, we
301+
can ignore it and just use [.align] in both cases. *)
285302
(* Some assemblers interpret the integer n as a 2^n alignment and others
286303
as a number of bytes. *)
287304
let n =
@@ -381,6 +398,14 @@ module Directive = struct
381398
Misc.fatal_error
382399
"Cannot emit [Direct_assignment] except on macOS-like assemblers")
383400
| Protected s -> bprintf buf "\t.protected\t%s" s
401+
| Hidden s -> bprintf buf "\t.hidden\t%s" s
402+
| Weak s -> bprintf buf "\t.weak\t%s" s
403+
(* masm only *)
404+
| External _ -> assert false
405+
| Reloc { offset; name; expr } ->
406+
bprintf buf "\t.reloc\t%a, %s, %a" Constant.print offset
407+
(reloc_type_to_string name)
408+
Constant.print expr
384409

385410
let print_masm buf t =
386411
let unsupported name =
@@ -395,7 +420,10 @@ module Directive = struct
395420
| Some comment -> Printf.sprintf "\t; %s" comment
396421
in
397422
match t with
398-
| Align { bytes } -> bprintf buf "\tALIGN\t%d" bytes
423+
| Align { bytes; data_section = _ } ->
424+
(* The data_section is only relevant for the binary emitter. On MASM, we
425+
can ignore it. *)
426+
bprintf buf "\tALIGN\t%d" bytes
399427
| Bytes { str; comment } ->
400428
buf_bytes_directive buf ~directive:"BYTE" str;
401429
bprintf buf "%s" (masm_comment_opt comment)
@@ -441,6 +469,11 @@ module Directive = struct
441469
| Uleb128 _ -> unsupported "Uleb128"
442470
| Direct_assignment _ -> unsupported "Direct_assignment"
443471
| Protected _ -> unsupported "Protected"
472+
| Hidden _ -> unsupported "Hidden"
473+
| Weak _ -> unsupported "Weak"
474+
| External s -> bprintf buf "\tEXTRN\t%s: NEAR" s
475+
(* The only supported "type" on EXTRN declarations i NEAR. *)
476+
| Reloc _ -> unsupported "Reloc"
444477

445478
let print b t =
446479
match TS.assembler () with
@@ -485,6 +518,13 @@ let const_variable var = Variable var
485518

486519
let const_int64 i : expr = Signed_int i
487520

521+
let const_with_offset const (offset : int64) =
522+
if Int64.equal offset 0L
523+
then const
524+
else if Int64.compare offset 0L < 0
525+
then Add (const, Signed_int (Int64.neg offset))
526+
else Add (const, Signed_int offset)
527+
488528
let emit_ref = ref None
489529

490530
let emit (d : Directive.t) =
@@ -497,7 +537,7 @@ let emit_non_masm (d : Directive.t) =
497537

498538
let section ~names ~flags ~args = emit (Section { names; flags; args })
499539

500-
let align ~bytes = emit (Align { bytes })
540+
let align ~data_section ~bytes = emit (Align { bytes; data_section })
501541

502542
let should_generate_cfi () =
503543
(* We generate CFI info even if we're not generating any other debugging
@@ -548,8 +588,16 @@ let indirect_symbol symbol = emit (Indirect_symbol (Asm_symbol.encode symbol))
548588

549589
let private_extern symbol = emit (Private_extern (Asm_symbol.encode symbol))
550590

591+
let extrn symbol = emit (External (Asm_symbol.encode symbol))
592+
593+
let hidden symbol = emit (Hidden (Asm_symbol.encode symbol))
594+
595+
let weak symbol = emit (Weak (Asm_symbol.encode symbol))
596+
551597
let size symbol cst = emit (Size (Asm_symbol.encode symbol, lower_expr cst))
552598

599+
let size_const sym n = emit (Size (Asm_symbol.encode sym, Signed_int n))
600+
553601
let type_ symbol ~type_ = emit (Type (symbol, type_))
554602

555603
let sleb128 ?comment i =
@@ -626,7 +674,7 @@ let label ?comment label = const_machine_width ?comment (Label label)
626674
let label_plus_offset ?comment lab ~offset_in_bytes =
627675
let offset_in_bytes = Targetint.to_int64 offset_in_bytes in
628676
let lab = const_label lab in
629-
const_machine_width ?comment (const_add lab (const_int64 offset_in_bytes))
677+
const_machine_width ?comment (const_with_offset lab offset_in_bytes)
630678

631679
let define_label label =
632680
let lbl_section = Asm_label.section label in
@@ -797,7 +845,7 @@ let symbol ?comment sym = const_machine_width ?comment (Symbol sym)
797845

798846
let symbol_plus_offset symbol ~offset_in_bytes =
799847
let offset_in_bytes = Targetint.to_int64 offset_in_bytes in
800-
const_machine_width (Add (Symbol symbol, Signed_int offset_in_bytes))
848+
const_machine_width (const_with_offset (Symbol symbol) offset_in_bytes)
801849

802850
let int8 ?comment i =
803851
const ?comment (Signed_int (Int64.of_int (Int8.to_int i))) Eight
@@ -890,9 +938,11 @@ let between_labels_16_bit ?comment:_ ~upper:_ ~lower:_ () =
890938
(* CR poechsel: use the arguments *)
891939
Misc.fatal_error "between_labels_16_bit not implemented yet"
892940

893-
let between_labels_32_bit ?comment:_ ~upper:_ ~lower:_ () =
894-
(* CR poechsel: use the arguments *)
895-
Misc.fatal_error "between_labels_32_bit not implemented yet"
941+
let between_labels_32_bit ?comment:_comment ~upper ~lower () =
942+
let expr = const_sub (const_label upper) (const_label lower) in
943+
(* CR sspies: Following the x86 implementation, we *do not* force an assembly
944+
time constant here. *)
945+
const expr Thirty_two
896946

897947
let between_labels_64_bit ?comment:_ ~upper:_ ~lower:_ () =
898948
(* CR poechsel: use the arguments *)
@@ -1065,3 +1115,14 @@ let offset_into_dwarf_section_symbol ?comment:_comment
10651115
match width with
10661116
| Thirty_two -> const expr Thirty_two
10671117
| Sixty_four -> const expr Sixty_four
1118+
1119+
let reloc_x86_64_plt32 ~offset_from_this ~target_symbol ~rel_offset_from_next =
1120+
emit
1121+
(Reloc
1122+
{ offset = Sub (This, Signed_int offset_from_this);
1123+
name = R_X86_64_PLT32;
1124+
expr =
1125+
Sub
1126+
( Named_thing (Asm_symbol.encode target_symbol),
1127+
Signed_int rel_offset_from_next )
1128+
})

‎backend/asm_targets/asm_directives_new.mli

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ val cfi_def_cfa_register : reg:string -> unit
159159
val mark_stack_non_executable : unit -> unit
160160

161161
(** Leave as much space as is required to achieve the given alignment. *)
162-
val align : bytes:int -> unit
162+
val align : data_section:bool -> bytes:int -> unit
163163

164164
(** Emit a directive giving the displacement between the given symbol and
165165
the current position. This should only be used to state sizes of
@@ -168,6 +168,8 @@ val align : bytes:int -> unit
168168
from that whose size is being stated (e.g. on POWER with ELF ABI v1). *)
169169
val size : ?size_of:Asm_symbol.t -> Asm_symbol.t -> unit
170170

171+
val size_const : Asm_symbol.t -> int64 -> unit
172+
171173
(** Leave a gap in the object file. *)
172174
val space : bytes:int -> unit
173175

@@ -197,6 +199,15 @@ val protected : Asm_symbol.t -> unit
197199
details). *)
198200
val private_extern : Asm_symbol.t -> unit
199201

202+
(** Mark an already encoded symbol as external. *)
203+
val extrn : Asm_symbol.t -> unit
204+
205+
(** Mark an already encoded symbol or label as hidden. *)
206+
val hidden : Asm_symbol.t -> unit
207+
208+
(** Mark an already encoded symbol or label as weak. *)
209+
val weak : Asm_symbol.t -> unit
210+
200211
(** Marker inside the definition of a lazy symbol stub (see platform or
201212
assembler documentation for details). *)
202213
val indirect_symbol : Asm_symbol.t -> unit
@@ -306,6 +317,12 @@ val offset_into_dwarf_section_symbol :
306317
Asm_symbol.t ->
307318
unit
308319

320+
val reloc_x86_64_plt32 :
321+
offset_from_this:int64 ->
322+
target_symbol:Asm_symbol.t ->
323+
rel_offset_from_next:int64 ->
324+
unit
325+
309326
module Directive : sig
310327
module Constant : sig
311328
(* CR sspies: make this private again once the first-class module has been
@@ -354,6 +371,10 @@ module Directive : sig
354371
removed *)
355372
type comment = string
356373

374+
(* ELF specific *)
375+
type reloc_type = R_X86_64_PLT32
376+
(* X86 only *)
377+
357378
(* CR sspies: make this private again once the first-class module has been
358379
removed *)
359380

@@ -363,7 +384,14 @@ module Directive : sig
363384
have had all necessary prefixing, mangling, escaping and suffixing
364385
applied. *)
365386
type t =
366-
| Align of { bytes : int }
387+
| Align of
388+
{ bytes : int;
389+
(** The number of bytes to align to. This will be taken log2 by the emitter on
390+
Arm and macOS platforms.*)
391+
data_section : bool
392+
(** The data_section flag controls whether the binary emitter emits NOP instructions
393+
or null bytes. *)
394+
}
367395
| Bytes of
368396
{ str : string;
369397
comment : string option
@@ -417,6 +445,14 @@ module Directive : sig
417445
comment : string option
418446
}
419447
| Protected of string
448+
| Hidden of string
449+
| Weak of string
450+
| External of string
451+
| Reloc of
452+
{ offset : Constant.t;
453+
name : reloc_type;
454+
expr : Constant.t
455+
}
420456

421457
(** Translate the given directive to textual form. This produces output
422458
suitable for either gas or MASM as appropriate. *)

‎backend/asm_targets/asm_label.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ let create_string section label =
6969
assert (not (contains_escapable_char label));
7070
{ section; label = String label }
7171

72+
let create_string_unchecked section label = { section; label = String label }
73+
7274
let label_prefix =
7375
match Target_system.assembler () with MacOS -> "L" | MASM | GAS_like -> ".L"
7476

@@ -138,6 +140,7 @@ let for_dwarf_section (dwarf_section : Asm_section.dwarf_section) =
138140
| Debug_str -> Lazy.force debug_str_label
139141
| Debug_line -> Lazy.force debug_line_label
140142

143+
(* CR sspies: Remove the other cases where we never emit a label upfront. *)
141144
let for_section (section : Asm_section.t) =
142145
match section with
143146
| DWARF dwarf_section -> for_dwarf_section dwarf_section
@@ -147,3 +150,7 @@ let for_section (section : Asm_section.t) =
147150
| Eight_byte_literals -> Lazy.force eight_byte_literals_label
148151
| Sixteen_byte_literals -> Lazy.force sixteen_byte_literals_label
149152
| Jump_tables -> Lazy.force jump_tables_label
153+
| Stapsdt_base -> Misc.fatal_error "Stapsdt_base has no associated label"
154+
| Stapsdt_note -> Misc.fatal_error "Stapsdt_note has no associated label"
155+
| Probes -> Misc.fatal_error "Probes has no associated label"
156+
| Note_ocaml_eh -> Misc.fatal_error "Note_ocaml_eh has no associated label"

‎backend/asm_targets/asm_label.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ val create_int : Asm_section.t -> int -> t
5252
(** Create a textual label. The supplied name must not require escaping. *)
5353
val create_string : Asm_section.t -> string -> t
5454

55+
(** Create a textual label. Argument string is not checked, so use with caution. *)
56+
val create_string_unchecked : Asm_section.t -> string -> t
57+
5558
(** Convert a label to the corresponding textual form, suitable for direct
5659
emission into an assembly file. This may be useful e.g. when emitting an
5760
instruction referencing a label. *)

‎backend/asm_targets/asm_section.ml

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type t =
4848
| Sixteen_byte_literals
4949
| Jump_tables
5050
| Text
51+
| Stapsdt_base
52+
| Stapsdt_note
53+
| Probes
54+
| Note_ocaml_eh
5155

5256
let dwarf_sections_in_order () =
5357
let sections =
@@ -72,7 +76,7 @@ let is_delayed = function
7276
( Debug_info | Debug_abbrev | Debug_aranges | Debug_str | Debug_loclists
7377
| Debug_rnglists | Debug_addr | Debug_loc | Debug_ranges )
7478
| Data | Read_only_data | Eight_byte_literals | Sixteen_byte_literals
75-
| Jump_tables | Text ->
79+
| Jump_tables | Text | Stapsdt_base | Stapsdt_note | Probes | Note_ocaml_eh ->
7680
false
7781

7882
let print ppf t =
@@ -94,6 +98,10 @@ let print ppf t =
9498
| Sixteen_byte_literals -> "Sixteen_byte_literals"
9599
| Jump_tables -> "Jump_tables"
96100
| Text -> "Text"
101+
| Stapsdt_base -> "Stapsdt_base"
102+
| Stapsdt_note -> "Stapsdt_note"
103+
| Probes -> "Probes"
104+
| Note_ocaml_eh -> "Note_ocaml_eh"
97105
in
98106
Format.pp_print_string ppf str
99107

@@ -104,7 +112,8 @@ let equal t1 t2 = Stdlib.compare t1 t2 = 0
104112
let section_is_text = function
105113
| Text -> true
106114
| Data | Read_only_data | Eight_byte_literals | Sixteen_byte_literals
107-
| Jump_tables | DWARF _ ->
115+
| Jump_tables | DWARF _ | Stapsdt_base | Stapsdt_note | Probes | Note_ocaml_eh
116+
->
108117
false
109118

110119
type section_details =
@@ -164,19 +173,20 @@ let details t ~first_occurrence =
164173
| false, _ -> []
165174
in
166175
[name], flags, args
167-
| (Eight_byte_literals | Sixteen_byte_literals), (ARM | AArch64 | Z), _
168-
| (Eight_byte_literals | Sixteen_byte_literals), _, Solaris ->
169-
rodata ()
170-
| Sixteen_byte_literals, _, MacOS_like ->
171-
["__TEXT"; "__literal16"], None, ["16byte_literals"]
172-
| Sixteen_byte_literals, _, (MinGW_64 | Cygwin) -> [".rdata"], Some "dr", []
173-
| Sixteen_byte_literals, _, (MinGW_32 | Win32 | Win64) -> data ()
174-
| Sixteen_byte_literals, _, _ -> [".rodata.cst8"], Some "a", ["@progbits"]
176+
(* Eight Byte Literals; based on corresponding upstream secions *)
175177
| Eight_byte_literals, _, MacOS_like ->
176178
["__TEXT"; "__literal8"], None, ["8byte_literals"]
177179
| Eight_byte_literals, _, (MinGW_64 | Cygwin) -> [".rdata"], Some "dr", []
178-
| Eight_byte_literals, _, (MinGW_32 | Win32 | Win64) -> data ()
179-
| Eight_byte_literals, _, _ -> [".rodata.cst8"], Some "a", ["@progbits"]
180+
| Eight_byte_literals, _, Win64 -> data ()
181+
| Eight_byte_literals, _, _ ->
182+
[".rodata.cst8"], Some "aM", ["@progbits"; "8"]
183+
(* Sixteen Byte Literals; based on corresponding upstream secions *)
184+
| Sixteen_byte_literals, _, MacOS_like ->
185+
["__TEXT"; "__literal16"], None, ["16byte_literals"]
186+
| Sixteen_byte_literals, _, (MinGW_64 | Cygwin) -> [".rdata"], Some "dr", []
187+
| Sixteen_byte_literals, _, Win64 -> data ()
188+
| Sixteen_byte_literals, _, _ ->
189+
[".rodata.cst16"], Some "aM", ["@progbits"; "16"]
180190
| Jump_tables, _, (MinGW_64 | Cygwin) -> [".rdata"], Some "dr", []
181191
| Jump_tables, _, (MinGW_32 | Win32) -> data ()
182192
| Jump_tables, _, (MacOS_like | Win64) ->
@@ -185,6 +195,18 @@ let details t ~first_occurrence =
185195
| Read_only_data, _, (MinGW_32 | Win32) -> data ()
186196
| Read_only_data, _, (MinGW_64 | Cygwin) -> [".rdata"], Some "dr", []
187197
| Read_only_data, _, _ -> rodata ()
198+
(* CR sspies: Is this one really possible on all systems? *)
199+
| Stapsdt_base, _, _ ->
200+
[".stapsdt.base"], Some "aG", ["\"progbits\""; ".stapsdt.base"; "comdat"]
201+
| Stapsdt_note, _, MacOS_like ->
202+
["__DATA"; "__note_stapsdt"], None, ["regular"]
203+
| Stapsdt_note, _, (GNU | Solaris | Linux | Generic_BSD | BeOS) ->
204+
[".note.stapsdt"], Some "?", ["\"note\""]
205+
| Stapsdt_note, _, _ ->
206+
Misc.fatal_error "Target systems does not support stapsdt."
207+
| Probes, _, MacOS_like -> ["__TEXT"; "__probes"], None, ["regular"]
208+
| Probes, _, _ -> [".probes"], Some "wa", ["\"progbits\""]
209+
| Note_ocaml_eh, _, _ -> [".note.ocaml_eh"], Some "?", ["\"note\""]
188210
in
189211
{ names; flags; args }
190212

‎backend/asm_targets/asm_section.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ type t =
4848
| Sixteen_byte_literals
4949
| Jump_tables
5050
| Text
51+
| Stapsdt_base
52+
| Stapsdt_note
53+
| Probes
54+
| Note_ocaml_eh
5155

5256
val to_string : t -> string
5357

‎backend/asm_targets/asm_symbol.ml

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,29 @@ let should_be_escaped = function
4444
module Thing = struct
4545
type t =
4646
{ name : string;
47-
without_prefix : bool
47+
already_encoded : bool
4848
}
4949

50-
let compare { name = name1; without_prefix = without_prefix1 }
51-
{ name = name2; without_prefix = without_prefix2 } =
50+
let compare { name = name1; already_encoded = already_encoded1 }
51+
{ name = name2; already_encoded = already_encoded2 } =
5252
let cmp = String.compare name1 name2 in
53-
if cmp = 0 then Bool.compare without_prefix1 without_prefix2 else cmp
53+
if cmp = 0 then Bool.compare already_encoded1 already_encoded2 else cmp
5454

5555
let equal t1 t2 = compare t1 t2 = 0
5656

5757
let hash = Hashtbl.hash
5858

59-
let output chan { name; without_prefix } =
60-
let symbol_prefix = if without_prefix then symbol_prefix () else "" in
61-
Printf.fprintf chan "%s%s" symbol_prefix name
59+
let output chan { name; already_encoded : _ } = Printf.fprintf chan "%s" name
6260

63-
let print fmt { name; without_prefix } =
64-
let symbol_prefix = if without_prefix then symbol_prefix () else "" in
65-
Format.pp_print_string fmt (symbol_prefix ^ name)
61+
let print fmt { name; already_encoded : _ } = Format.pp_print_string fmt name
6662
end
6763

6864
include Thing
6965
include Identifiable.Make (Thing)
7066

71-
let create ?without_prefix name =
72-
let without_prefix = Option.is_some without_prefix in
73-
{ name; without_prefix }
67+
let create ?(already_encoded = false) name = { name; already_encoded }
7468

75-
let to_raw_string { name; without_prefix } = name
69+
let to_raw_string { name; already_encoded : _ } = name
7670

7771
let escape name =
7872
let escaped_nb = ref 0 in
@@ -97,6 +91,9 @@ let to_escaped_string ?suffix ~symbol_prefix t =
9791
let suffix = match suffix with None -> "" | Some suffix -> suffix in
9892
symbol_prefix ^ escape t ^ suffix
9993

100-
let encode t =
101-
let symbol_prefix = if t.without_prefix then "" else symbol_prefix () in
102-
to_escaped_string ~symbol_prefix t.name
94+
let encode { name; already_encoded } =
95+
if already_encoded
96+
then name
97+
else
98+
let symbol_prefix = symbol_prefix () in
99+
to_escaped_string ~symbol_prefix name

‎backend/asm_targets/asm_symbol.mli

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ val should_be_escaped : char -> bool
2929

3030
include Identifiable.S
3131

32-
(* If [without_prefix] is not provided [encode] will prefix the symbol using the
33-
(architecture-dependent) prefix for symbols, for example "_" on macOS. In
34-
contrast, [to_raw_string] will always return the non-prefixed version. *)
35-
val create : ?without_prefix:unit -> string -> t
32+
(** [create] creates a new symbol. By default, it is assumed that the symbol has not been
33+
encoded. In some rare cases, the symbol is encoded elsewhere. In these cases, set the
34+
flag [already_encoded] to [true]. *)
35+
val create : ?already_encoded:bool -> string -> t
3636

3737
val encode : t -> string
3838

‎utils/target_system.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ let is_macos () =
177177
| MASM | GAS_like -> false
178178
| MacOS -> true
179179

180-
let is_gas () =
181-
match assembler () with
182-
| MASM | MacOS -> false
183-
| GAS_like -> true
180+
let is_gas () =
181+
match assembler () with
182+
| MASM | MacOS -> false
183+
| GAS_like -> true

0 commit comments

Comments
 (0)
Please sign in to comment.