Skip to content

Commit 7f3102d

Browse files
committed
Produce correct x86 assembly code for identifiers starting with $
They must be parenthesized in some contexts to avoid being confused for immediate operands. Fixes: #540
1 parent 5e445cd commit 7f3102d

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

x86/TargetPrinter.ml

+22-6
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ module type SYSTEM =
106106
val comment: string
107107
val raw_symbol: out_channel -> string -> unit
108108
val symbol: out_channel -> P.t -> unit
109+
val symbol_paren: out_channel -> P.t -> unit
109110
val label: out_channel -> int -> unit
110111
val name_of_section: section_name -> string
111112
val stack_alignment: int
@@ -130,6 +131,12 @@ module ELF_System : SYSTEM =
130131

131132
let symbol = elf_symbol
132133

134+
let symbol_paren oc symb =
135+
let s = extern_atom symb in
136+
if String.length s > 0 && s.[0] = '$'
137+
then fprintf oc "(%s)" s
138+
else fprintf oc "%s" s
139+
133140
let label = elf_label
134141

135142
let name_of_section = function
@@ -164,8 +171,8 @@ module ELF_System : SYSTEM =
164171

165172
let print_mov_rs oc rd id =
166173
if Archi.ptr64
167-
then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol id ireg64 rd
168-
else fprintf oc " movl $%a, %a\n" symbol id ireg32 rd
174+
then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol_paren id ireg64 rd
175+
else fprintf oc " movl $%a, %a\n" symbol_paren id ireg32 rd
169176

170177
let print_fun_info = elf_print_fun_info
171178

@@ -196,6 +203,9 @@ module MacOS_System : SYSTEM =
196203
let symbol oc symb =
197204
raw_symbol oc (extern_atom symb)
198205

206+
let symbol_paren = symbol
207+
(* the leading '_' protects the leading '$' *)
208+
199209
let label oc lbl =
200210
fprintf oc "L%d" lbl
201211

@@ -262,6 +272,12 @@ module Cygwin_System : SYSTEM =
262272
let symbol oc symb =
263273
raw_symbol oc (extern_atom symb)
264274

275+
let symbol_paren oc symb =
276+
let s = extern_atom symb in
277+
if String.length s > 0 && s.[0] = '$'
278+
then fprintf oc "(%a)" raw_symbol s
279+
else raw_symbol oc s
280+
265281
let label oc lbl =
266282
fprintf oc "L%d" lbl
267283

@@ -341,13 +357,13 @@ module Target(System: SYSTEM):TARGET =
341357
(* RIP-relative addressing *)
342358
let ofs' = Z.to_int64 ofs in
343359
if ofs' = 0L
344-
then fprintf oc "%a(%%rip)" symbol id
360+
then fprintf oc "%a(%%rip)" symbol_paren id
345361
else fprintf oc "(%a + %Ld)(%%rip)" symbol id ofs'
346362
end else begin
347363
(* Absolute addressing *)
348364
let ofs' = Z.to_int32 ofs in
349365
if ofs' = 0l
350-
then fprintf oc "%a" symbol id
366+
then fprintf oc "%a" symbol_paren id
351367
else fprintf oc "(%a + %ld)" symbol id ofs'
352368
end
353369
end;
@@ -707,7 +723,7 @@ module Target(System: SYSTEM):TARGET =
707723
| Pjmp_l(l) ->
708724
fprintf oc " jmp %a\n" label (transl_label l)
709725
| Pjmp_s(f, sg) ->
710-
fprintf oc " jmp %a\n" symbol f
726+
fprintf oc " jmp %a\n" symbol_paren f
711727
| Pjmp_r(r, sg) ->
712728
fprintf oc " jmp *%a\n" ireg r
713729
| Pjcc(c, l) ->
@@ -733,7 +749,7 @@ module Target(System: SYSTEM):TARGET =
733749
fprintf oc " jmp *%a(, %a, 4)\n" label l ireg r
734750
end
735751
| Pcall_s(f, sg) ->
736-
fprintf oc " call %a\n" symbol f;
752+
fprintf oc " call %a\n" symbol_paren f;
737753
if (not Archi.ptr64) && sg.sig_cc.cc_structret then
738754
fprintf oc " pushl %%eax\n"
739755
| Pcall_r(r, sg) ->

0 commit comments

Comments
 (0)