1997-07-24 06:36:24 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1997-07-24 06:36:24 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1997-07-24 06:36:24 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Emission of Intel 386 assembly code *)
|
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
module StringSet = Set.Make(struct type t = string let compare = compare end)
|
|
|
|
|
2002-07-22 09:38:07 -07:00
|
|
|
open Location
|
1997-07-24 06:36:24 -07:00
|
|
|
open Misc
|
|
|
|
open Cmm
|
|
|
|
open Arch
|
|
|
|
open Proc
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
open Linearize
|
|
|
|
open Emitaux
|
|
|
|
|
|
|
|
(* Tradeoff between code size and code speed *)
|
|
|
|
|
|
|
|
let fastcode_flag = ref true
|
|
|
|
|
|
|
|
let stack_offset = ref 0
|
|
|
|
|
|
|
|
(* Layout of the stack frame *)
|
|
|
|
|
|
|
|
let frame_size () = (* includes return address *)
|
2008-01-11 08:13:18 -08:00
|
|
|
let sz =
|
2006-04-16 16:28:22 -07:00
|
|
|
!stack_offset + 4 * num_stack_slots.(0) + 8 * num_stack_slots.(1) + 4
|
|
|
|
in Misc.align sz stack_alignment
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let slot_offset loc cl =
|
|
|
|
match loc with
|
2004-08-12 06:37:12 -07:00
|
|
|
Incoming n ->
|
|
|
|
assert (n >= 0);
|
|
|
|
frame_size() + n
|
1997-07-24 06:36:24 -07:00
|
|
|
| Local n ->
|
|
|
|
if cl = 0
|
|
|
|
then !stack_offset + n * 4
|
|
|
|
else !stack_offset + num_stack_slots.(0) * 4 + n * 8
|
2004-08-12 06:37:12 -07:00
|
|
|
| Outgoing n ->
|
|
|
|
assert (n >= 0);
|
|
|
|
n
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
let trap_frame_size = Misc.align 8 stack_alignment
|
|
|
|
|
1999-10-14 06:35:23 -07:00
|
|
|
(* Prefixing of symbols with "_" *)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let symbol_prefix =
|
|
|
|
match Config.system with
|
|
|
|
"linux_elf" -> ""
|
1999-10-14 06:35:23 -07:00
|
|
|
| "bsd_elf" -> ""
|
1997-07-24 06:36:24 -07:00
|
|
|
| "solaris" -> ""
|
1999-05-18 11:46:17 -07:00
|
|
|
| "beos" -> ""
|
2006-04-16 16:28:22 -07:00
|
|
|
| "gnu" -> ""
|
1997-07-24 06:36:24 -07:00
|
|
|
| _ -> "_"
|
|
|
|
|
|
|
|
let emit_symbol s =
|
|
|
|
emit_string symbol_prefix; Emitaux.emit_symbol '$' s
|
|
|
|
|
|
|
|
(* Output a label *)
|
|
|
|
|
|
|
|
let label_prefix =
|
|
|
|
match Config.system with
|
|
|
|
"linux_elf" -> ".L"
|
1999-10-14 06:35:23 -07:00
|
|
|
| "bsd_elf" -> ".L"
|
1997-07-24 06:36:24 -07:00
|
|
|
| "solaris" -> ".L"
|
1999-05-18 11:46:17 -07:00
|
|
|
| "beos" -> ".L"
|
2006-04-16 16:28:22 -07:00
|
|
|
| "gnu" -> ".L"
|
1997-07-24 06:36:24 -07:00
|
|
|
| _ -> "L"
|
|
|
|
|
|
|
|
let emit_label lbl =
|
|
|
|
emit_string label_prefix; emit_int lbl
|
|
|
|
|
2011-12-18 02:00:56 -08:00
|
|
|
let emit_data_label lbl =
|
|
|
|
emit_string label_prefix; emit_string "d"; emit_int lbl
|
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Some data directives have different names under Solaris *)
|
|
|
|
|
|
|
|
let word_dir =
|
|
|
|
match Config.system with
|
|
|
|
"solaris" -> ".value"
|
|
|
|
| _ -> ".word"
|
|
|
|
let skip_dir =
|
|
|
|
match Config.system with
|
|
|
|
"solaris" -> ".zero"
|
|
|
|
| _ -> ".space"
|
|
|
|
let use_ascii_dir =
|
|
|
|
match Config.system with
|
|
|
|
"solaris" -> false
|
|
|
|
| _ -> true
|
|
|
|
|
2008-12-03 10:09:09 -08:00
|
|
|
(* MacOSX has its own way to reference symbols potentially defined in
|
2006-04-16 16:28:22 -07:00
|
|
|
shared objects *)
|
|
|
|
|
|
|
|
let macosx =
|
|
|
|
match Config.system with
|
|
|
|
| "macosx" -> true
|
|
|
|
| _ -> false
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output a .align directive.
|
|
|
|
The numerical argument to .align is log2 of alignment size, except
|
|
|
|
under ELF, where it is the alignment size... *)
|
|
|
|
|
|
|
|
let emit_align =
|
|
|
|
match Config.system with
|
2006-04-16 16:28:22 -07:00
|
|
|
"linux_elf" | "bsd_elf" | "solaris" | "beos" | "cygwin" | "mingw" | "gnu" ->
|
1997-07-24 06:36:24 -07:00
|
|
|
(fun n -> ` .align {emit_int n}\n`)
|
|
|
|
| _ ->
|
|
|
|
(fun n -> ` .align {emit_int(Misc.log2 n)}\n`)
|
2008-01-11 08:13:18 -08:00
|
|
|
|
2001-02-05 00:49:10 -08:00
|
|
|
let emit_Llabel fallthrough lbl =
|
|
|
|
if not fallthrough && !fastcode_flag then
|
2000-10-02 07:08:30 -07:00
|
|
|
emit_align 16 ;
|
|
|
|
emit_label lbl
|
2008-01-11 08:13:18 -08:00
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output a pseudo-register *)
|
|
|
|
|
|
|
|
let emit_reg = function
|
|
|
|
{ loc = Reg r } ->
|
|
|
|
emit_string (register_name r)
|
2004-08-12 06:37:12 -07:00
|
|
|
| { loc = Stack(Incoming n | Outgoing n) } when n < 0 ->
|
2004-08-12 07:29:00 -07:00
|
|
|
`{emit_symbol "caml_extra_params"} + {emit_int (n + 64)}`
|
1997-07-24 06:36:24 -07:00
|
|
|
| { loc = Stack s } as r ->
|
|
|
|
let ofs = slot_offset s (register_class r) in
|
|
|
|
`{emit_int ofs}(%esp)`
|
|
|
|
| { loc = Unknown } ->
|
|
|
|
fatal_error "Emit_i386.emit_reg"
|
|
|
|
|
|
|
|
(* Output a reference to the lower 8 bits or lower 16 bits of a register *)
|
|
|
|
|
|
|
|
let reg_low_byte_name = [| "%al"; "%bl"; "%cl"; "%dl" |]
|
|
|
|
let reg_low_half_name = [| "%ax"; "%bx"; "%cx"; "%dx"; "%si"; "%di"; "%bp" |]
|
|
|
|
|
|
|
|
let emit_reg8 r =
|
|
|
|
match r.loc with
|
|
|
|
Reg r when r < 4 -> emit_string (reg_low_byte_name.(r))
|
|
|
|
| _ -> fatal_error "Emit_i386.emit_reg8"
|
|
|
|
|
|
|
|
let emit_reg16 r =
|
|
|
|
match r.loc with
|
|
|
|
Reg r when r < 7 -> emit_string (reg_low_half_name.(r))
|
|
|
|
| _ -> fatal_error "Emit_i386.emit_reg16"
|
|
|
|
|
|
|
|
(* Output an addressing mode *)
|
|
|
|
|
|
|
|
let emit_addressing addr r n =
|
|
|
|
match addr with
|
|
|
|
Ibased(s, d) ->
|
|
|
|
`{emit_symbol s}`;
|
|
|
|
if d <> 0 then ` + {emit_int d}`
|
|
|
|
| Iindexed d ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`({emit_reg r.(n)})`
|
|
|
|
| Iindexed2 d ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`({emit_reg r.(n)}, {emit_reg r.(n+1)})`
|
2003-06-30 08:32:45 -07:00
|
|
|
| Iscaled(2, d) ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`({emit_reg r.(n)}, {emit_reg r.(n)})`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Iscaled(scale, d) ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`(, {emit_reg r.(n)}, {emit_int scale})`
|
|
|
|
| Iindexed2scaled(scale, d) ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`({emit_reg r.(n)}, {emit_reg r.(n+1)}, {emit_int scale})`
|
|
|
|
|
|
|
|
(* Record live pointers at call points *)
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
let record_frame_label live dbg =
|
1997-07-24 06:36:24 -07:00
|
|
|
let lbl = new_label() in
|
|
|
|
let live_offset = ref [] in
|
|
|
|
Reg.Set.iter
|
|
|
|
(function
|
|
|
|
{typ = Addr; loc = Reg r} ->
|
|
|
|
live_offset := ((r lsl 1) + 1) :: !live_offset
|
|
|
|
| {typ = Addr; loc = Stack s} as reg ->
|
|
|
|
live_offset := slot_offset s (register_class reg) :: !live_offset
|
|
|
|
| _ -> ())
|
|
|
|
live;
|
|
|
|
frame_descriptors :=
|
|
|
|
{ fd_lbl = lbl;
|
|
|
|
fd_frame_size = frame_size();
|
2007-01-29 04:11:18 -08:00
|
|
|
fd_live_offset = !live_offset;
|
|
|
|
fd_debuginfo = dbg } :: !frame_descriptors;
|
1997-07-24 06:36:24 -07:00
|
|
|
lbl
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
let record_frame live dbg =
|
|
|
|
let lbl = record_frame_label live dbg in `{emit_label lbl}:\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
(* Record calls to the GC -- we've moved them out of the way *)
|
|
|
|
|
|
|
|
type gc_call =
|
|
|
|
{ gc_lbl: label; (* Entry label *)
|
|
|
|
gc_return_lbl: label; (* Where to branch after GC *)
|
|
|
|
gc_frame: label } (* Label of frame descriptor *)
|
|
|
|
|
|
|
|
let call_gc_sites = ref ([] : gc_call list)
|
|
|
|
|
|
|
|
let emit_call_gc gc =
|
|
|
|
`{emit_label gc.gc_lbl}: call {emit_symbol "caml_call_gc"}\n`;
|
|
|
|
`{emit_label gc.gc_frame}: jmp {emit_label gc.gc_return_lbl}\n`
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
(* Record calls to caml_ml_array_bound_error.
|
|
|
|
In -g mode, we maintain one call to caml_ml_array_bound_error
|
|
|
|
per bound check site. Without -g, we can share a single call. *)
|
|
|
|
|
|
|
|
type bound_error_call =
|
|
|
|
{ bd_lbl: label; (* Entry label *)
|
|
|
|
bd_frame: label } (* Label of frame descriptor *)
|
|
|
|
|
|
|
|
let bound_error_sites = ref ([] : bound_error_call list)
|
|
|
|
let bound_error_call = ref 0
|
|
|
|
|
|
|
|
let bound_error_label dbg =
|
|
|
|
if !Clflags.debug then begin
|
|
|
|
let lbl_bound_error = new_label() in
|
|
|
|
let lbl_frame = record_frame_label Reg.Set.empty dbg in
|
|
|
|
bound_error_sites :=
|
|
|
|
{ bd_lbl = lbl_bound_error; bd_frame = lbl_frame } :: !bound_error_sites;
|
|
|
|
lbl_bound_error
|
|
|
|
end else begin
|
|
|
|
if !bound_error_call = 0 then bound_error_call := new_label();
|
|
|
|
!bound_error_call
|
|
|
|
end
|
|
|
|
|
|
|
|
let emit_call_bound_error bd =
|
|
|
|
`{emit_label bd.bd_lbl}: call {emit_symbol "caml_ml_array_bound_error"}\n`;
|
|
|
|
`{emit_label bd.bd_frame}:\n`
|
|
|
|
|
|
|
|
let emit_call_bound_errors () =
|
|
|
|
List.iter emit_call_bound_error !bound_error_sites;
|
|
|
|
if !bound_error_call > 0 then
|
2008-01-11 08:13:18 -08:00
|
|
|
`{emit_label !bound_error_call}: call {emit_symbol "caml_ml_array_bound_error"}\n`
|
2007-01-29 04:11:18 -08:00
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Names for instructions *)
|
|
|
|
|
|
|
|
let instr_for_intop = function
|
|
|
|
Iadd -> "addl"
|
|
|
|
| Isub -> "subl"
|
|
|
|
| Imul -> "imull"
|
|
|
|
| Iand -> "andl"
|
|
|
|
| Ior -> "orl"
|
|
|
|
| Ixor -> "xorl"
|
|
|
|
| Ilsl -> "sall"
|
|
|
|
| Ilsr -> "shrl"
|
|
|
|
| Iasr -> "sarl"
|
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_intop"
|
|
|
|
|
|
|
|
let instr_for_floatop = function
|
|
|
|
Inegf -> "fchs"
|
|
|
|
| Iabsf -> "fabs"
|
|
|
|
| Iaddf -> "faddl"
|
|
|
|
| Isubf -> "fsubl"
|
|
|
|
| Imulf -> "fmull"
|
|
|
|
| Idivf -> "fdivl"
|
|
|
|
| Ispecific Isubfrev -> "fsubrl"
|
|
|
|
| Ispecific Idivfrev -> "fdivrl"
|
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop"
|
|
|
|
|
|
|
|
let instr_for_floatop_reversed = function
|
|
|
|
Iaddf -> "faddl"
|
|
|
|
| Isubf -> "fsubrl"
|
|
|
|
| Imulf -> "fmull"
|
|
|
|
| Idivf -> "fdivrl"
|
|
|
|
| Ispecific Isubfrev -> "fsubl"
|
|
|
|
| Ispecific Idivfrev -> "fdivl"
|
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop_reversed"
|
|
|
|
|
|
|
|
let instr_for_floatop_pop = function
|
|
|
|
Iaddf -> "faddp"
|
|
|
|
| Isubf -> "fsubp"
|
|
|
|
| Imulf -> "fmulp"
|
|
|
|
| Idivf -> "fdivp"
|
|
|
|
| Ispecific Isubfrev -> "fsubrp"
|
|
|
|
| Ispecific Idivfrev -> "fdivrp"
|
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop_pop"
|
|
|
|
|
2000-03-10 06:31:06 -08:00
|
|
|
let instr_for_floatarithmem double = function
|
|
|
|
Ifloatadd -> if double then "faddl" else "fadds"
|
|
|
|
| Ifloatsub -> if double then "fsubl" else "fsubs"
|
|
|
|
| Ifloatsubrev -> if double then "fsubrl" else "fsubrs"
|
|
|
|
| Ifloatmul -> if double then "fmull" else "fmuls"
|
|
|
|
| Ifloatdiv -> if double then "fdivl" else "fdivs"
|
|
|
|
| Ifloatdivrev -> if double then "fdivrl" else "fdivrs"
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let name_for_cond_branch = function
|
|
|
|
Isigned Ceq -> "e" | Isigned Cne -> "ne"
|
|
|
|
| Isigned Cle -> "le" | Isigned Cgt -> "g"
|
|
|
|
| Isigned Clt -> "l" | Isigned Cge -> "ge"
|
|
|
|
| Iunsigned Ceq -> "e" | Iunsigned Cne -> "ne"
|
|
|
|
| Iunsigned Cle -> "be" | Iunsigned Cgt -> "a"
|
|
|
|
| Iunsigned Clt -> "b" | Iunsigned Cge -> "ae"
|
2008-01-11 08:13:18 -08:00
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output an = 0 or <> 0 test. *)
|
|
|
|
|
|
|
|
let output_test_zero arg =
|
|
|
|
match arg.loc with
|
|
|
|
Reg r -> ` testl {emit_reg arg}, {emit_reg arg}\n`
|
|
|
|
| _ -> ` cmpl $0, {emit_reg arg}\n`
|
|
|
|
|
|
|
|
(* Deallocate the stack frame before a return or tail call *)
|
|
|
|
|
2012-02-21 09:41:02 -08:00
|
|
|
let output_epilogue f =
|
1997-07-24 06:36:24 -07:00
|
|
|
let n = frame_size() - 4 in
|
2012-02-21 09:41:02 -08:00
|
|
|
if n > 0 then
|
|
|
|
begin
|
|
|
|
` addl ${emit_int n}, %esp\n`;
|
|
|
|
cfi_adjust_cfa_offset (-n);
|
|
|
|
f ();
|
|
|
|
(* reset CFA back cause function body may continue *)
|
|
|
|
cfi_adjust_cfa_offset n
|
|
|
|
end
|
|
|
|
else
|
|
|
|
f ()
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2003-02-25 07:50:13 -08:00
|
|
|
(* Determine if the given register is the top of the floating-point stack *)
|
|
|
|
|
|
|
|
let is_tos = function { loc = Reg _; typ = Float } -> true | _ -> false
|
|
|
|
|
|
|
|
(* Emit the code for a floating-point comparison *)
|
|
|
|
|
|
|
|
let emit_float_test cmp neg arg lbl =
|
|
|
|
let actual_cmp =
|
|
|
|
match (is_tos arg.(0), is_tos arg.(1)) with
|
|
|
|
(true, true) ->
|
|
|
|
(* both args on top of FP stack *)
|
|
|
|
` fcompp\n`;
|
|
|
|
cmp
|
|
|
|
| (true, false) ->
|
|
|
|
(* first arg on top of FP stack *)
|
|
|
|
` fcompl {emit_reg arg.(1)}\n`;
|
|
|
|
cmp
|
|
|
|
| (false, true) ->
|
|
|
|
(* second arg on top of FP stack *)
|
|
|
|
` fcompl {emit_reg arg.(0)}\n`;
|
|
|
|
Cmm.swap_comparison cmp
|
|
|
|
| (false, false) ->
|
|
|
|
` fldl {emit_reg arg.(0)}\n`;
|
|
|
|
` fcompl {emit_reg arg.(1)}\n`;
|
|
|
|
cmp
|
|
|
|
in
|
|
|
|
` fnstsw %ax\n`;
|
|
|
|
begin match actual_cmp with
|
|
|
|
Ceq ->
|
|
|
|
if neg then begin
|
|
|
|
` andb $68, %ah\n`;
|
|
|
|
` xorb $64, %ah\n`;
|
|
|
|
` jne `
|
|
|
|
end else begin
|
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` cmpb $64, %ah\n`;
|
|
|
|
` je `
|
|
|
|
end
|
|
|
|
| Cne ->
|
|
|
|
if neg then begin
|
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` cmpb $64, %ah\n`;
|
|
|
|
` je `
|
|
|
|
end else begin
|
|
|
|
` andb $68, %ah\n`;
|
|
|
|
` xorb $64, %ah\n`;
|
|
|
|
` jne `
|
|
|
|
end
|
|
|
|
| Cle ->
|
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` decb %ah\n`;
|
|
|
|
` cmpb $64, %ah\n`;
|
|
|
|
if neg
|
|
|
|
then ` jae `
|
|
|
|
else ` jb `
|
|
|
|
| Cge ->
|
|
|
|
` andb $5, %ah\n`;
|
|
|
|
if neg
|
|
|
|
then ` jne `
|
|
|
|
else ` je `
|
|
|
|
| Clt ->
|
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` cmpb $1, %ah\n`;
|
|
|
|
if neg
|
|
|
|
then ` jne `
|
|
|
|
else ` je `
|
|
|
|
| Cgt ->
|
|
|
|
` andb $69, %ah\n`;
|
|
|
|
if neg
|
|
|
|
then ` jne `
|
|
|
|
else ` je `
|
|
|
|
end;
|
|
|
|
`{emit_label lbl}\n`
|
|
|
|
|
|
|
|
(* Emit a Ifloatspecial instruction *)
|
|
|
|
|
|
|
|
let emit_floatspecial = function
|
2003-03-03 07:58:06 -08:00
|
|
|
"atan" -> ` fld1; fpatan\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
| "atan2" -> ` fpatan\n`
|
|
|
|
| "cos" -> ` fcos\n`
|
|
|
|
| "log" -> ` fldln2; fxch; fyl2x\n`
|
|
|
|
| "log10" -> ` fldlg2; fxch; fyl2x\n`
|
|
|
|
| "sin" -> ` fsin\n`
|
|
|
|
| "sqrt" -> ` fsqrt\n`
|
|
|
|
| "tan" -> ` fptan; fstp %st(0)\n`
|
|
|
|
| _ -> assert false
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output the assembly code for an instruction *)
|
|
|
|
|
|
|
|
(* Name of current function *)
|
|
|
|
let function_name = ref ""
|
|
|
|
(* Entry point for tail recursive calls *)
|
|
|
|
let tailrec_entry_point = ref 0
|
|
|
|
(* Label of trap for out-of-range accesses *)
|
|
|
|
let range_check_trap = ref 0
|
2003-02-25 07:50:13 -08:00
|
|
|
(* Record float literals to be emitted later *)
|
1997-07-24 06:36:24 -07:00
|
|
|
let float_constants = ref ([] : (int * string) list)
|
2006-04-16 16:28:22 -07:00
|
|
|
(* Record references to external C functions (for MacOSX) *)
|
|
|
|
let external_symbols_direct = ref StringSet.empty
|
|
|
|
let external_symbols_indirect = ref StringSet.empty
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2001-02-05 00:49:10 -08:00
|
|
|
let emit_instr fallthrough i =
|
2012-02-21 09:41:02 -08:00
|
|
|
emit_debug_info i.dbg;
|
1997-07-24 06:36:24 -07:00
|
|
|
match i.desc with
|
|
|
|
Lend -> ()
|
|
|
|
| Lop(Imove | Ispill | Ireload) ->
|
|
|
|
let src = i.arg.(0) and dst = i.res.(0) in
|
|
|
|
if src.loc <> dst.loc then begin
|
|
|
|
if src.typ = Float then
|
2003-02-25 07:50:13 -08:00
|
|
|
if is_tos src then
|
2003-06-15 02:58:31 -07:00
|
|
|
` fstpl {emit_reg dst}\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
else if is_tos dst then
|
|
|
|
` fldl {emit_reg src}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
else begin
|
|
|
|
` fldl {emit_reg src}\n`;
|
|
|
|
` fstpl {emit_reg dst}\n`
|
|
|
|
end
|
|
|
|
else
|
|
|
|
` movl {emit_reg src}, {emit_reg dst}\n`
|
|
|
|
end
|
|
|
|
| Lop(Iconst_int n) ->
|
2003-04-25 05:27:31 -07:00
|
|
|
if n = 0n then begin
|
1997-07-24 06:36:24 -07:00
|
|
|
match i.res.(0).loc with
|
|
|
|
Reg n -> ` xorl {emit_reg i.res.(0)}, {emit_reg i.res.(0)}\n`
|
|
|
|
| _ -> ` movl $0, {emit_reg i.res.(0)}\n`
|
|
|
|
end else
|
|
|
|
` movl ${emit_nativeint n}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iconst_float s) ->
|
2004-05-03 05:46:51 -07:00
|
|
|
begin match Int64.bits_of_float (float_of_string s) with
|
|
|
|
| 0x0000_0000_0000_0000L -> (* +0.0 *)
|
1997-07-24 06:36:24 -07:00
|
|
|
` fldz\n`
|
2004-05-03 05:46:51 -07:00
|
|
|
| 0x8000_0000_0000_0000L -> (* -0.0 *)
|
|
|
|
` fldz\n fchs\n`
|
|
|
|
| 0x3FF0_0000_0000_0000L -> (* 1.0 *)
|
1997-07-24 06:36:24 -07:00
|
|
|
` fld1\n`
|
2004-05-03 05:46:51 -07:00
|
|
|
| 0xBFF0_0000_0000_0000L -> (* -1.0 *)
|
|
|
|
` fld1\n fchs\n`
|
|
|
|
| _ ->
|
1997-07-24 06:36:24 -07:00
|
|
|
let lbl = new_label() in
|
|
|
|
float_constants := (lbl, s) :: !float_constants;
|
|
|
|
` fldl {emit_label lbl}\n`
|
|
|
|
end
|
|
|
|
| Lop(Iconst_symbol s) ->
|
|
|
|
` movl ${emit_symbol s}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Icall_ind) ->
|
|
|
|
` call *{emit_reg i.arg.(0)}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame i.live i.dbg
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Icall_imm s) ->
|
|
|
|
` call {emit_symbol s}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame i.live i.dbg
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Itailcall_ind) ->
|
2012-02-21 09:41:02 -08:00
|
|
|
output_epilogue begin fun () ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` jmp *{emit_reg i.arg.(0)}\n`
|
2012-02-21 09:41:02 -08:00
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Itailcall_imm s) ->
|
|
|
|
if s = !function_name then
|
|
|
|
` jmp {emit_label !tailrec_entry_point}\n`
|
|
|
|
else begin
|
2012-02-21 09:41:02 -08:00
|
|
|
output_epilogue begin fun () ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` jmp {emit_symbol s}\n`
|
2012-02-21 09:41:02 -08:00
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
| Lop(Iextcall(s, alloc)) ->
|
|
|
|
if alloc then begin
|
2006-04-16 16:28:22 -07:00
|
|
|
if not macosx then
|
|
|
|
` movl ${emit_symbol s}, %eax\n`
|
|
|
|
else begin
|
|
|
|
external_symbols_indirect :=
|
|
|
|
StringSet.add s !external_symbols_indirect;
|
|
|
|
` movl L{emit_symbol s}$non_lazy_ptr, %eax\n`
|
|
|
|
end;
|
1997-07-24 06:36:24 -07:00
|
|
|
` call {emit_symbol "caml_c_call"}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame i.live i.dbg
|
1997-07-24 06:36:24 -07:00
|
|
|
end else begin
|
2006-04-16 16:28:22 -07:00
|
|
|
if not macosx then
|
|
|
|
` call {emit_symbol s}\n`
|
|
|
|
else begin
|
|
|
|
external_symbols_direct :=
|
|
|
|
StringSet.add s !external_symbols_direct;
|
|
|
|
` call L{emit_symbol s}$stub\n`
|
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
| Lop(Istackoffset n) ->
|
|
|
|
if n < 0
|
|
|
|
then ` addl ${emit_int(-n)}, %esp\n`
|
|
|
|
else ` subl ${emit_int(n)}, %esp\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset n;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + n
|
|
|
|
| Lop(Iload(chunk, addr)) ->
|
|
|
|
let dest = i.res.(0) in
|
2000-02-04 04:43:18 -08:00
|
|
|
begin match chunk with
|
2003-02-25 07:50:13 -08:00
|
|
|
| Word | Thirtytwo_signed | Thirtytwo_unsigned ->
|
|
|
|
` movl {emit_addressing addr i.arg 0}, {emit_reg dest}\n`
|
2000-02-04 04:43:18 -08:00
|
|
|
| Byte_unsigned ->
|
|
|
|
` movzbl {emit_addressing addr i.arg 0}, {emit_reg dest}\n`
|
|
|
|
| Byte_signed ->
|
|
|
|
` movsbl {emit_addressing addr i.arg 0}, {emit_reg dest}\n`
|
|
|
|
| Sixteen_unsigned ->
|
|
|
|
` movzwl {emit_addressing addr i.arg 0}, {emit_reg dest}\n`
|
|
|
|
| Sixteen_signed ->
|
|
|
|
` movswl {emit_addressing addr i.arg 0}, {emit_reg dest}\n`
|
|
|
|
| Single ->
|
|
|
|
` flds {emit_addressing addr i.arg 0}\n`
|
2000-03-10 06:31:06 -08:00
|
|
|
| Double | Double_u ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` fldl {emit_addressing addr i.arg 0}\n`
|
|
|
|
end
|
|
|
|
| Lop(Istore(chunk, addr)) ->
|
|
|
|
begin match chunk with
|
2000-02-04 04:43:18 -08:00
|
|
|
| Word | Thirtytwo_signed | Thirtytwo_unsigned ->
|
|
|
|
` movl {emit_reg i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
|
|
|
|
| Byte_unsigned | Byte_signed ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` movb {emit_reg8 i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
|
2000-02-04 04:43:18 -08:00
|
|
|
| Sixteen_unsigned | Sixteen_signed ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` movw {emit_reg16 i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
|
2000-02-04 04:43:18 -08:00
|
|
|
| Single ->
|
2003-02-25 07:50:13 -08:00
|
|
|
if is_tos i.arg.(0) then
|
2003-06-15 02:58:31 -07:00
|
|
|
` fstps {emit_addressing addr i.arg 1}\n`
|
2000-02-04 04:43:18 -08:00
|
|
|
else begin
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fstps {emit_addressing addr i.arg 1}\n`
|
|
|
|
end
|
2000-03-10 06:31:06 -08:00
|
|
|
| Double | Double_u ->
|
2003-02-25 07:50:13 -08:00
|
|
|
if is_tos i.arg.(0) then
|
2003-06-15 02:58:31 -07:00
|
|
|
` fstpl {emit_addressing addr i.arg 1}\n`
|
2000-02-04 04:43:18 -08:00
|
|
|
else begin
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fstpl {emit_addressing addr i.arg 1}\n`
|
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
| Lop(Ialloc n) ->
|
|
|
|
if !fastcode_flag then begin
|
|
|
|
let lbl_redo = new_label() in
|
2004-01-01 08:42:43 -08:00
|
|
|
`{emit_label lbl_redo}: movl {emit_symbol "caml_young_ptr"}, %eax\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
` subl ${emit_int n}, %eax\n`;
|
2004-01-01 08:42:43 -08:00
|
|
|
` movl %eax, {emit_symbol "caml_young_ptr"}\n`;
|
|
|
|
` cmpl {emit_symbol "caml_young_limit"}, %eax\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
let lbl_call_gc = new_label() in
|
2007-01-29 04:11:18 -08:00
|
|
|
let lbl_frame = record_frame_label i.live Debuginfo.none in
|
1997-07-24 06:36:24 -07:00
|
|
|
` jb {emit_label lbl_call_gc}\n`;
|
|
|
|
` leal 4(%eax), {emit_reg i.res.(0)}\n`;
|
|
|
|
call_gc_sites :=
|
|
|
|
{ gc_lbl = lbl_call_gc;
|
|
|
|
gc_return_lbl = lbl_redo;
|
|
|
|
gc_frame = lbl_frame } :: !call_gc_sites
|
|
|
|
end else begin
|
|
|
|
begin match n with
|
|
|
|
8 -> ` call {emit_symbol "caml_alloc1"}\n`
|
|
|
|
| 12 -> ` call {emit_symbol "caml_alloc2"}\n`
|
|
|
|
| 16 -> ` call {emit_symbol "caml_alloc3"}\n`
|
|
|
|
| _ -> ` movl ${emit_int n}, %eax\n`;
|
2004-01-01 08:42:43 -08:00
|
|
|
` call {emit_symbol "caml_allocN"}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end;
|
2007-01-29 04:11:18 -08:00
|
|
|
`{record_frame i.live Debuginfo.none} leal 4(%eax), {emit_reg i.res.(0)}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
| Lop(Iintop(Icomp cmp)) ->
|
|
|
|
` cmpl {emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` set{emit_string b} %al\n`;
|
|
|
|
` movzbl %al, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop_imm(Icomp cmp, n)) ->
|
|
|
|
` cmpl ${emit_int n}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` set{emit_string b} %al\n`;
|
|
|
|
` movzbl %al, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop Icheckbound) ->
|
2007-01-29 04:11:18 -08:00
|
|
|
let lbl = bound_error_label i.dbg in
|
1997-07-24 06:36:24 -07:00
|
|
|
` cmpl {emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
` jbe {emit_label lbl}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintop_imm(Icheckbound, n)) ->
|
2007-01-29 04:11:18 -08:00
|
|
|
let lbl = bound_error_label i.dbg in
|
1997-07-24 06:36:24 -07:00
|
|
|
` cmpl ${emit_int n}, {emit_reg i.arg.(0)}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
` jbe {emit_label lbl}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintop(Idiv | Imod)) ->
|
|
|
|
` cltd\n`;
|
|
|
|
` idivl {emit_reg i.arg.(1)}\n`
|
|
|
|
| Lop(Iintop(Ilsl | Ilsr | Iasr as op)) ->
|
|
|
|
(* We have i.arg.(0) = i.res.(0) and i.arg.(1) = %ecx *)
|
|
|
|
` {emit_string(instr_for_intop op)} %cl, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop op) ->
|
|
|
|
(* We have i.arg.(0) = i.res.(0) *)
|
|
|
|
` {emit_string(instr_for_intop op)} {emit_reg i.arg.(1)}, {emit_reg i.res.(0)}\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
| Lop(Iintop_imm(Iadd, n)) when i.arg.(0).loc <> i.res.(0).loc ->
|
|
|
|
` leal {emit_int n}({emit_reg i.arg.(0)}), {emit_reg i.res.(0)}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintop_imm(Iadd, 1) | Iintop_imm(Isub, -1)) ->
|
|
|
|
` incl {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop_imm(Iadd, -1) | Iintop_imm(Isub, 1)) ->
|
|
|
|
` decl {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop_imm(Idiv, n)) ->
|
|
|
|
let l = Misc.log2 n in
|
|
|
|
let lbl = new_label() in
|
|
|
|
output_test_zero i.arg.(0);
|
|
|
|
` jge {emit_label lbl}\n`;
|
|
|
|
` addl ${emit_int(n-1)}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
`{emit_label lbl}: sarl ${emit_int l}, {emit_reg i.arg.(0)}\n`
|
|
|
|
| Lop(Iintop_imm(Imod, n)) ->
|
|
|
|
let lbl = new_label() in
|
|
|
|
` movl {emit_reg i.arg.(0)}, %eax\n`;
|
|
|
|
` testl %eax, %eax\n`;
|
|
|
|
` jge {emit_label lbl}\n`;
|
|
|
|
` addl ${emit_int(n-1)}, %eax\n`;
|
|
|
|
`{emit_label lbl}: andl ${emit_int(-n)}, %eax\n`;
|
|
|
|
` subl %eax, {emit_reg i.arg.(0)}\n`
|
|
|
|
| Lop(Iintop_imm(op, n)) ->
|
|
|
|
(* We have i.arg.(0) = i.res.(0) *)
|
|
|
|
` {emit_string(instr_for_intop op)} ${emit_int n}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Inegf | Iabsf as floatop) ->
|
2003-02-25 07:50:13 -08:00
|
|
|
if not (is_tos i.arg.(0)) then
|
1997-07-24 06:36:24 -07:00
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` {emit_string(instr_for_floatop floatop)}\n`
|
|
|
|
| Lop(Iaddf | Isubf | Imulf | Idivf | Ispecific(Isubfrev | Idivfrev)
|
|
|
|
as floatop) ->
|
2003-02-25 07:50:13 -08:00
|
|
|
begin match (is_tos i.arg.(0), is_tos i.arg.(1)) with
|
|
|
|
(true, true) ->
|
1997-07-24 06:36:24 -07:00
|
|
|
(* both operands on top of FP stack *)
|
|
|
|
` {emit_string(instr_for_floatop_pop floatop)} %st, %st(1)\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
| (true, false) ->
|
1997-07-24 06:36:24 -07:00
|
|
|
(* first operand on stack *)
|
|
|
|
` {emit_string(instr_for_floatop floatop)} {emit_reg i.arg.(1)}\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
| (false, true) ->
|
1997-07-24 06:36:24 -07:00
|
|
|
(* second operand on stack *)
|
|
|
|
` {emit_string(instr_for_floatop_reversed floatop)} {emit_reg i.arg.(0)}\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
| (false, false) ->
|
1997-07-24 06:36:24 -07:00
|
|
|
(* both operands in memory *)
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` {emit_string(instr_for_floatop floatop)} {emit_reg i.arg.(1)}\n`
|
|
|
|
end
|
|
|
|
| Lop(Ifloatofint) ->
|
|
|
|
begin match i.arg.(0).loc with
|
|
|
|
Stack s ->
|
|
|
|
` fildl {emit_reg i.arg.(0)}\n`
|
|
|
|
| _ ->
|
|
|
|
` pushl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fildl (%esp)\n`;
|
|
|
|
` addl $4, %esp\n`
|
|
|
|
end
|
|
|
|
| Lop(Iintoffloat) ->
|
2003-02-25 07:50:13 -08:00
|
|
|
if not (is_tos i.arg.(0)) then
|
1997-07-24 06:36:24 -07:00
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
stack_offset := !stack_offset - 8;
|
|
|
|
` subl $8, %esp\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 8;
|
1997-07-24 06:36:24 -07:00
|
|
|
` fnstcw 4(%esp)\n`;
|
2003-04-25 05:27:31 -07:00
|
|
|
` movw 4(%esp), %ax\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
` movb $12, %ah\n`;
|
2003-04-25 05:27:31 -07:00
|
|
|
` movw %ax, 0(%esp)\n`;
|
|
|
|
` fldcw 0(%esp)\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
begin match i.res.(0).loc with
|
|
|
|
Stack s ->
|
2003-06-15 02:58:31 -07:00
|
|
|
` fistpl {emit_reg i.res.(0)}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| _ ->
|
2003-06-15 02:58:31 -07:00
|
|
|
` fistpl (%esp)\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
` movl (%esp), {emit_reg i.res.(0)}\n`
|
|
|
|
end;
|
|
|
|
` fldcw 4(%esp)\n`;
|
|
|
|
` addl $8, %esp\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset (-8);
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 8
|
|
|
|
| Lop(Ispecific(Ilea addr)) ->
|
|
|
|
` lea {emit_addressing addr i.arg 0}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Ispecific(Istore_int(n, addr))) ->
|
1999-02-04 02:30:31 -08:00
|
|
|
` movl ${emit_nativeint n}, {emit_addressing addr i.arg 0}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Ispecific(Istore_symbol(s, addr))) ->
|
|
|
|
` movl ${emit_symbol s}, {emit_addressing addr i.arg 0}\n`
|
1997-11-14 03:02:05 -08:00
|
|
|
| Lop(Ispecific(Ioffset_loc(n, addr))) ->
|
|
|
|
` addl ${emit_int n}, {emit_addressing addr i.arg 0}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Ispecific(Ipush)) ->
|
|
|
|
(* Push arguments in reverse order *)
|
|
|
|
for n = Array.length i.arg - 1 downto 0 do
|
|
|
|
let r = i.arg.(n) in
|
|
|
|
match r with
|
|
|
|
{loc = Reg _; typ = Float} ->
|
|
|
|
` subl $8, %esp\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 8;
|
1997-07-24 06:36:24 -07:00
|
|
|
` fstpl 0(%esp)\n`;
|
|
|
|
stack_offset := !stack_offset + 8
|
|
|
|
| {loc = Stack sl; typ = Float} ->
|
|
|
|
let ofs = slot_offset sl 1 in
|
|
|
|
` pushl {emit_int(ofs + 4)}(%esp)\n`;
|
|
|
|
` pushl {emit_int(ofs + 4)}(%esp)\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 8;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 8
|
|
|
|
| _ ->
|
|
|
|
` pushl {emit_reg r}\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 4;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 4
|
|
|
|
done
|
|
|
|
| Lop(Ispecific(Ipush_int n)) ->
|
1999-02-04 02:30:31 -08:00
|
|
|
` pushl ${emit_nativeint n}\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 4;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 4
|
|
|
|
| Lop(Ispecific(Ipush_symbol s)) ->
|
|
|
|
` pushl ${emit_symbol s}\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 4;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 4
|
|
|
|
| Lop(Ispecific(Ipush_load addr)) ->
|
|
|
|
` pushl {emit_addressing addr i.arg 0}\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 4;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 4
|
|
|
|
| Lop(Ispecific(Ipush_load_float addr)) ->
|
|
|
|
` pushl {emit_addressing (offset_addressing addr 4) i.arg 0}\n`;
|
|
|
|
` pushl {emit_addressing addr i.arg 0}\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset 8;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + 8
|
2000-03-10 06:31:06 -08:00
|
|
|
| Lop(Ispecific(Ifloatarithmem(double, op, addr))) ->
|
2003-02-25 07:50:13 -08:00
|
|
|
if not (is_tos i.arg.(0)) then
|
1997-07-24 06:36:24 -07:00
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
2000-03-10 06:31:06 -08:00
|
|
|
` {emit_string(instr_for_floatarithmem double op)} {emit_addressing addr i.arg 1}\n`
|
2003-02-25 07:50:13 -08:00
|
|
|
| Lop(Ispecific(Ifloatspecial s)) ->
|
|
|
|
(* Push args on float stack if necessary *)
|
|
|
|
for k = 0 to Array.length i.arg - 1 do
|
|
|
|
if not (is_tos i.arg.(k)) then ` fldl {emit_reg i.arg.(k)}\n`
|
|
|
|
done;
|
|
|
|
(* Fix-up for binary instrs whose args were swapped *)
|
|
|
|
if Array.length i.arg = 2 && is_tos i.arg.(1) then
|
|
|
|
` fxch %st(1)\n`;
|
|
|
|
emit_floatspecial s
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lreloadretaddr ->
|
|
|
|
()
|
|
|
|
| Lreturn ->
|
2012-02-21 09:41:02 -08:00
|
|
|
output_epilogue begin fun () ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` ret\n`
|
2012-02-21 09:41:02 -08:00
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
| Llabel lbl ->
|
2001-02-05 00:49:10 -08:00
|
|
|
`{emit_Llabel fallthrough lbl}:\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lbranch lbl ->
|
|
|
|
` jmp {emit_label lbl}\n`
|
|
|
|
| Lcondbranch(tst, lbl) ->
|
|
|
|
begin match tst with
|
|
|
|
Itruetest ->
|
|
|
|
output_test_zero i.arg.(0);
|
|
|
|
` jne {emit_label lbl}\n`
|
|
|
|
| Ifalsetest ->
|
|
|
|
output_test_zero i.arg.(0);
|
|
|
|
` je {emit_label lbl}\n`
|
|
|
|
| Iinttest cmp ->
|
|
|
|
` cmpl {emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` j{emit_string b} {emit_label lbl}\n`
|
2008-01-11 08:13:18 -08:00
|
|
|
| Iinttest_imm((Isigned Ceq | Isigned Cne |
|
1997-07-24 06:36:24 -07:00
|
|
|
Iunsigned Ceq | Iunsigned Cne) as cmp, 0) ->
|
|
|
|
output_test_zero i.arg.(0);
|
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` j{emit_string b} {emit_label lbl}\n`
|
|
|
|
| Iinttest_imm(cmp, n) ->
|
|
|
|
` cmpl ${emit_int n}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` j{emit_string b} {emit_label lbl}\n`
|
|
|
|
| Ifloattest(cmp, neg) ->
|
2003-02-25 07:50:13 -08:00
|
|
|
emit_float_test cmp neg i.arg lbl
|
1997-07-24 06:36:24 -07:00
|
|
|
| Ioddtest ->
|
|
|
|
` testl $1, {emit_reg i.arg.(0)}\n`;
|
|
|
|
` jne {emit_label lbl}\n`
|
|
|
|
| Ieventest ->
|
|
|
|
` testl $1, {emit_reg i.arg.(0)}\n`;
|
|
|
|
` je {emit_label lbl}\n`
|
|
|
|
end
|
|
|
|
| Lcondbranch3(lbl0, lbl1, lbl2) ->
|
|
|
|
` cmpl $1, {emit_reg i.arg.(0)}\n`;
|
|
|
|
begin match lbl0 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` jb {emit_label lbl}\n`
|
|
|
|
end;
|
|
|
|
begin match lbl1 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` je {emit_label lbl}\n`
|
|
|
|
end;
|
|
|
|
begin match lbl2 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` jg {emit_label lbl}\n`
|
|
|
|
end
|
|
|
|
| Lswitch jumptbl ->
|
|
|
|
let lbl = new_label() in
|
|
|
|
` jmp *{emit_label lbl}(, {emit_reg i.arg.(0)}, 4)\n`;
|
|
|
|
` .data\n`;
|
|
|
|
`{emit_label lbl}:`;
|
|
|
|
for i = 0 to Array.length jumptbl - 1 do
|
|
|
|
` .long {emit_label jumptbl.(i)}\n`
|
|
|
|
done;
|
|
|
|
` .text\n`
|
|
|
|
| Lsetuptrap lbl ->
|
|
|
|
` call {emit_label lbl}\n`
|
|
|
|
| Lpushtrap ->
|
2006-04-16 16:28:22 -07:00
|
|
|
if trap_frame_size > 8 then
|
|
|
|
` subl ${emit_int (trap_frame_size - 8)}, %esp\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
` pushl {emit_symbol "caml_exception_pointer"}\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset trap_frame_size;
|
1997-07-24 06:36:24 -07:00
|
|
|
` movl %esp, {emit_symbol "caml_exception_pointer"}\n`;
|
2006-04-16 16:28:22 -07:00
|
|
|
stack_offset := !stack_offset + trap_frame_size
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lpoptrap ->
|
|
|
|
` popl {emit_symbol "caml_exception_pointer"}\n`;
|
2006-04-16 16:28:22 -07:00
|
|
|
` addl ${emit_int (trap_frame_size - 4)}, %esp\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset (-trap_frame_size);
|
2006-04-16 16:28:22 -07:00
|
|
|
stack_offset := !stack_offset - trap_frame_size
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lraise ->
|
2007-01-29 04:11:18 -08:00
|
|
|
if !Clflags.debug then begin
|
|
|
|
` call {emit_symbol "caml_raise_exn"}\n`;
|
|
|
|
record_frame Reg.Set.empty i.dbg
|
|
|
|
end else begin
|
|
|
|
` movl {emit_symbol "caml_exception_pointer"}, %esp\n`;
|
|
|
|
` popl {emit_symbol "caml_exception_pointer"}\n`;
|
|
|
|
if trap_frame_size > 8 then
|
|
|
|
` addl ${emit_int (trap_frame_size - 8)}, %esp\n`;
|
|
|
|
` ret\n`
|
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2001-02-05 00:49:10 -08:00
|
|
|
let rec emit_all fallthrough i =
|
2000-10-02 07:08:30 -07:00
|
|
|
match i.desc with
|
|
|
|
| Lend -> ()
|
|
|
|
| _ ->
|
2001-02-05 00:49:10 -08:00
|
|
|
emit_instr fallthrough i;
|
2000-10-02 07:08:30 -07:00
|
|
|
emit_all
|
2001-02-05 00:49:10 -08:00
|
|
|
(Linearize.has_fallthrough i.desc)
|
2000-10-02 07:08:30 -07:00
|
|
|
i.next
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
(* Emission of the floating-point constants *)
|
|
|
|
|
|
|
|
let emit_float_constant (lbl, cst) =
|
|
|
|
` .data\n`;
|
2010-01-20 08:26:46 -08:00
|
|
|
`{emit_label lbl}:`;
|
|
|
|
emit_float64_split_directive ".long" cst
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
(* Emission of external symbol references (for MacOSX) *)
|
|
|
|
|
|
|
|
let emit_external_symbol_direct s =
|
|
|
|
`L{emit_symbol s}$stub:\n`;
|
|
|
|
` .indirect_symbol {emit_symbol s}\n`;
|
|
|
|
` hlt ; hlt ; hlt ; hlt ; hlt\n`
|
|
|
|
|
|
|
|
let emit_external_symbol_indirect s =
|
|
|
|
`L{emit_symbol s}$non_lazy_ptr:\n`;
|
|
|
|
` .indirect_symbol {emit_symbol s}\n`;
|
|
|
|
` .long 0\n`
|
|
|
|
|
|
|
|
let emit_external_symbols () =
|
|
|
|
` .section __IMPORT,__pointers,non_lazy_symbol_pointers\n`;
|
|
|
|
StringSet.iter emit_external_symbol_indirect !external_symbols_indirect;
|
|
|
|
external_symbols_indirect := StringSet.empty;
|
|
|
|
` .section __IMPORT,__jump_table,symbol_stubs,self_modifying_code+pure_instructions,5\n`;
|
|
|
|
StringSet.iter emit_external_symbol_direct !external_symbols_direct;
|
2006-10-16 01:40:42 -07:00
|
|
|
external_symbols_direct := StringSet.empty;
|
|
|
|
if !Clflags.gprofile then begin
|
|
|
|
`Lmcount$stub:\n`;
|
|
|
|
` .indirect_symbol mcount\n`;
|
|
|
|
` hlt ; hlt ; hlt ; hlt ; hlt\n`
|
|
|
|
end
|
2006-04-16 16:28:22 -07:00
|
|
|
|
1998-08-06 06:27:38 -07:00
|
|
|
(* Emission of the profiling prelude *)
|
|
|
|
|
|
|
|
let emit_profile () =
|
|
|
|
match Config.system with
|
2006-04-16 16:28:22 -07:00
|
|
|
"linux_elf" | "gnu" ->
|
1998-08-06 06:27:38 -07:00
|
|
|
` pushl %eax\n`;
|
|
|
|
` movl %esp, %ebp\n`;
|
|
|
|
` pushl %ecx\n`;
|
|
|
|
` pushl %edx\n`;
|
|
|
|
` call {emit_symbol "mcount"}\n`;
|
|
|
|
` popl %edx\n`;
|
|
|
|
` popl %ecx\n`;
|
|
|
|
` popl %eax\n`
|
2000-02-29 08:15:59 -08:00
|
|
|
| "bsd_elf" ->
|
|
|
|
` pushl %eax\n`;
|
|
|
|
` movl %esp, %ebp\n`;
|
|
|
|
` pushl %ecx\n`;
|
|
|
|
` pushl %edx\n`;
|
|
|
|
` call .mcount\n`;
|
|
|
|
` popl %edx\n`;
|
|
|
|
` popl %ecx\n`;
|
|
|
|
` popl %eax\n`
|
2006-10-16 01:40:42 -07:00
|
|
|
| "macosx" ->
|
|
|
|
` pushl %eax\n`;
|
|
|
|
` movl %esp, %ebp\n`;
|
|
|
|
` pushl %ecx\n`;
|
|
|
|
` pushl %edx\n`;
|
|
|
|
` call Lmcount$stub\n`;
|
|
|
|
` popl %edx\n`;
|
|
|
|
` popl %ecx\n`;
|
|
|
|
` popl %eax\n`
|
1998-08-06 06:27:38 -07:00
|
|
|
| _ -> () (*unsupported yet*)
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Emission of a function declaration *)
|
|
|
|
|
|
|
|
let fundecl fundecl =
|
|
|
|
function_name := fundecl.fun_name;
|
|
|
|
fastcode_flag := fundecl.fun_fast;
|
|
|
|
tailrec_entry_point := new_label();
|
|
|
|
stack_offset := 0;
|
|
|
|
float_constants := [];
|
|
|
|
call_gc_sites := [];
|
2007-01-29 04:11:18 -08:00
|
|
|
bound_error_sites := [];
|
|
|
|
bound_error_call := 0;
|
1997-07-24 06:36:24 -07:00
|
|
|
` .text\n`;
|
2000-10-02 07:08:30 -07:00
|
|
|
emit_align 16;
|
2010-01-20 08:26:46 -08:00
|
|
|
if macosx
|
|
|
|
&& not !Clflags.output_c_object
|
|
|
|
&& is_generic_function fundecl.fun_name
|
2009-05-20 04:52:42 -07:00
|
|
|
then (* PR#4690 *)
|
|
|
|
` .private_extern {emit_symbol fundecl.fun_name}\n`
|
|
|
|
else
|
|
|
|
` .globl {emit_symbol fundecl.fun_name}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
`{emit_symbol fundecl.fun_name}:\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
emit_debug_info fundecl.fun_dbg;
|
|
|
|
cfi_startproc ();
|
1998-08-06 06:27:38 -07:00
|
|
|
if !Clflags.gprofile then emit_profile();
|
1997-07-24 06:36:24 -07:00
|
|
|
let n = frame_size() - 4 in
|
|
|
|
if n > 0 then
|
2012-02-21 09:41:02 -08:00
|
|
|
begin
|
1997-07-24 06:36:24 -07:00
|
|
|
` subl ${emit_int n}, %esp\n`;
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_adjust_cfa_offset n;
|
|
|
|
end;
|
1997-07-24 06:36:24 -07:00
|
|
|
`{emit_label !tailrec_entry_point}:\n`;
|
2000-10-02 07:08:30 -07:00
|
|
|
emit_all true fundecl.fun_body;
|
1997-07-24 06:36:24 -07:00
|
|
|
List.iter emit_call_gc !call_gc_sites;
|
2007-01-29 04:11:18 -08:00
|
|
|
emit_call_bound_errors ();
|
2012-02-21 09:41:02 -08:00
|
|
|
cfi_endproc ();
|
2011-03-13 06:36:00 -07:00
|
|
|
begin match Config.system with
|
2008-12-03 10:09:09 -08:00
|
|
|
"linux_elf" | "bsd_elf" | "gnu" ->
|
|
|
|
` .type {emit_symbol fundecl.fun_name},@function\n`;
|
|
|
|
` .size {emit_symbol fundecl.fun_name},.-{emit_symbol fundecl.fun_name}\n`
|
2011-03-13 06:36:00 -07:00
|
|
|
| _ -> () end;
|
|
|
|
List.iter emit_float_constant !float_constants
|
2008-12-03 10:09:09 -08:00
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
(* Emission of data *)
|
|
|
|
|
|
|
|
let emit_item = function
|
2002-11-24 07:55:26 -08:00
|
|
|
Cglobal_symbol s ->
|
1997-07-24 06:36:24 -07:00
|
|
|
` .globl {emit_symbol s}\n`;
|
2002-11-24 07:55:26 -08:00
|
|
|
| Cdefine_symbol s ->
|
1997-07-24 06:36:24 -07:00
|
|
|
`{emit_symbol s}:\n`
|
|
|
|
| Cdefine_label lbl ->
|
2011-12-18 02:00:56 -08:00
|
|
|
`{emit_data_label lbl}:\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cint8 n ->
|
|
|
|
` .byte {emit_int n}\n`
|
|
|
|
| Cint16 n ->
|
|
|
|
` {emit_string word_dir} {emit_int n}\n`
|
2000-02-04 07:08:23 -08:00
|
|
|
| Cint32 n ->
|
|
|
|
` .long {emit_nativeint n}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cint n ->
|
|
|
|
` .long {emit_nativeint n}\n`
|
2000-02-04 04:43:18 -08:00
|
|
|
| Csingle f ->
|
2010-01-20 08:26:46 -08:00
|
|
|
emit_float32_directive ".long" f
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cdouble f ->
|
2010-01-20 08:26:46 -08:00
|
|
|
emit_float64_split_directive ".long" f
|
1997-07-24 06:36:24 -07:00
|
|
|
| Csymbol_address s ->
|
|
|
|
` .long {emit_symbol s}\n`
|
|
|
|
| Clabel_address lbl ->
|
2011-12-18 02:00:56 -08:00
|
|
|
` .long {emit_data_label lbl}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cstring s ->
|
|
|
|
if use_ascii_dir
|
|
|
|
then emit_string_directive " .ascii " s
|
|
|
|
else emit_bytes_directive " .byte " s
|
|
|
|
| Cskip n ->
|
|
|
|
if n > 0 then ` {emit_string skip_dir} {emit_int n}\n`
|
|
|
|
| Calign n ->
|
|
|
|
emit_align n
|
|
|
|
|
|
|
|
let data l =
|
|
|
|
` .data\n`;
|
|
|
|
List.iter emit_item l
|
|
|
|
|
|
|
|
(* Beginning / end of an assembly file *)
|
|
|
|
|
|
|
|
let begin_assembly() =
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_begin = Compilenv.make_symbol (Some "data_begin") in
|
1997-07-24 06:36:24 -07:00
|
|
|
` .data\n`;
|
|
|
|
` .globl {emit_symbol lbl_begin}\n`;
|
|
|
|
`{emit_symbol lbl_begin}:\n`;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_begin = Compilenv.make_symbol (Some "code_begin") in
|
1997-07-24 06:36:24 -07:00
|
|
|
` .text\n`;
|
|
|
|
` .globl {emit_symbol lbl_begin}\n`;
|
2009-05-20 04:52:42 -07:00
|
|
|
`{emit_symbol lbl_begin}:\n`;
|
|
|
|
if macosx then ` nop\n` (* PR#4690 *)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let end_assembly() =
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_end = Compilenv.make_symbol (Some "code_end") in
|
1997-07-24 06:36:24 -07:00
|
|
|
` .text\n`;
|
2009-05-20 04:52:42 -07:00
|
|
|
if macosx then ` nop\n`; (* suppress "ld warning: atom sorting error" *)
|
1997-07-24 06:36:24 -07:00
|
|
|
` .globl {emit_symbol lbl_end}\n`;
|
|
|
|
`{emit_symbol lbl_end}:\n`;
|
|
|
|
` .data\n`;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_end = Compilenv.make_symbol (Some "data_end") in
|
1997-07-24 06:36:24 -07:00
|
|
|
` .globl {emit_symbol lbl_end}\n`;
|
|
|
|
`{emit_symbol lbl_end}:\n`;
|
|
|
|
` .long 0\n`;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl = Compilenv.make_symbol (Some "frametable") in
|
1997-07-24 06:36:24 -07:00
|
|
|
` .globl {emit_symbol lbl}\n`;
|
|
|
|
`{emit_symbol lbl}:\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
emit_frames
|
|
|
|
{ efa_label = (fun l -> ` .long {emit_label l}\n`);
|
|
|
|
efa_16 = (fun n -> ` {emit_string word_dir} {emit_int n}\n`);
|
|
|
|
efa_32 = (fun n -> ` .long {emit_int32 n}\n`);
|
|
|
|
efa_word = (fun n -> ` .long {emit_int n}\n`);
|
|
|
|
efa_align = emit_align;
|
|
|
|
efa_label_rel = (fun lbl ofs ->
|
2007-10-08 07:19:34 -07:00
|
|
|
` .long {emit_label lbl} - . + {emit_int32 ofs}\n`);
|
2007-01-29 04:11:18 -08:00
|
|
|
efa_def_label = (fun l -> `{emit_label l}:\n`);
|
2008-12-03 10:09:09 -08:00
|
|
|
efa_string = (fun s ->
|
2007-01-29 04:11:18 -08:00
|
|
|
let s = s ^ "\000" in
|
|
|
|
if use_ascii_dir
|
|
|
|
then emit_string_directive " .ascii " s
|
|
|
|
else emit_bytes_directive " .byte " s) };
|
2008-08-01 01:04:57 -07:00
|
|
|
if macosx then emit_external_symbols ();
|
|
|
|
if Config.system = "linux_elf" then
|
|
|
|
(* Mark stack as non-executable, PR#4564 *)
|
|
|
|
`\n .section .note.GNU-stack,\"\",%progbits\n`
|