Skip to content

Commit

Permalink
Produce correct x86 assembly code for identifiers starting with $
Browse files Browse the repository at this point in the history
They must be parenthesized in some contexts to avoid being confused for
immediate operands.

Fixes: #540
  • Loading branch information
xavierleroy committed Dec 1, 2024
1 parent 5e445cd commit 7f3102d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion test
28 changes: 22 additions & 6 deletions x86/TargetPrinter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module type SYSTEM =
val comment: string
val raw_symbol: out_channel -> string -> unit
val symbol: out_channel -> P.t -> unit
val symbol_paren: out_channel -> P.t -> unit
val label: out_channel -> int -> unit
val name_of_section: section_name -> string
val stack_alignment: int
Expand All @@ -130,6 +131,12 @@ module ELF_System : SYSTEM =

let symbol = elf_symbol

let symbol_paren oc symb =
let s = extern_atom symb in
if String.length s > 0 && s.[0] = '$'
then fprintf oc "(%s)" s
else fprintf oc "%s" s

let label = elf_label

let name_of_section = function
Expand Down Expand Up @@ -164,8 +171,8 @@ module ELF_System : SYSTEM =

let print_mov_rs oc rd id =
if Archi.ptr64
then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol id ireg64 rd
else fprintf oc " movl $%a, %a\n" symbol id ireg32 rd
then fprintf oc " movq %a@GOTPCREL(%%rip), %a\n" symbol_paren id ireg64 rd
else fprintf oc " movl $%a, %a\n" symbol_paren id ireg32 rd

let print_fun_info = elf_print_fun_info

Expand Down Expand Up @@ -196,6 +203,9 @@ module MacOS_System : SYSTEM =
let symbol oc symb =
raw_symbol oc (extern_atom symb)

let symbol_paren = symbol
(* the leading '_' protects the leading '$' *)

let label oc lbl =
fprintf oc "L%d" lbl

Expand Down Expand Up @@ -262,6 +272,12 @@ module Cygwin_System : SYSTEM =
let symbol oc symb =
raw_symbol oc (extern_atom symb)

let symbol_paren oc symb =
let s = extern_atom symb in
if String.length s > 0 && s.[0] = '$'
then fprintf oc "(%a)" raw_symbol s
else raw_symbol oc s

let label oc lbl =
fprintf oc "L%d" lbl

Expand Down Expand Up @@ -341,13 +357,13 @@ module Target(System: SYSTEM):TARGET =
(* RIP-relative addressing *)
let ofs' = Z.to_int64 ofs in
if ofs' = 0L
then fprintf oc "%a(%%rip)" symbol id
then fprintf oc "%a(%%rip)" symbol_paren id
else fprintf oc "(%a + %Ld)(%%rip)" symbol id ofs'
end else begin
(* Absolute addressing *)
let ofs' = Z.to_int32 ofs in
if ofs' = 0l
then fprintf oc "%a" symbol id
then fprintf oc "%a" symbol_paren id
else fprintf oc "(%a + %ld)" symbol id ofs'
end
end;
Expand Down Expand Up @@ -707,7 +723,7 @@ module Target(System: SYSTEM):TARGET =
| Pjmp_l(l) ->
fprintf oc " jmp %a\n" label (transl_label l)
| Pjmp_s(f, sg) ->
fprintf oc " jmp %a\n" symbol f
fprintf oc " jmp %a\n" symbol_paren f
| Pjmp_r(r, sg) ->
fprintf oc " jmp *%a\n" ireg r
| Pjcc(c, l) ->
Expand All @@ -733,7 +749,7 @@ module Target(System: SYSTEM):TARGET =
fprintf oc " jmp *%a(, %a, 4)\n" label l ireg r
end
| Pcall_s(f, sg) ->
fprintf oc " call %a\n" symbol f;
fprintf oc " call %a\n" symbol_paren f;
if (not Archi.ptr64) && sg.sig_cc.cc_structret then
fprintf oc " pushl %%eax\n"
| Pcall_r(r, sg) ->
Expand Down

0 comments on commit 7f3102d

Please sign in to comment.