1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Emission of Intel 386 assembly code *)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
let stack_offset = ref 0
|
|
|
|
|
|
|
|
(* Layout of the stack frame *)
|
|
|
|
|
|
|
|
let frame_size () = (* includes return address *)
|
|
|
|
!stack_offset + 4 * num_stack_slots.(0) + 8 * num_stack_slots.(1) + 4
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let slot_offset loc cl =
|
1995-07-10 02:48:27 -07:00
|
|
|
match loc with
|
|
|
|
Incoming n -> frame_size() + n
|
|
|
|
| Local n ->
|
1996-04-22 04:15:41 -07:00
|
|
|
if cl = 0
|
1995-07-10 02:48:27 -07:00
|
|
|
then !stack_offset + n * 4
|
|
|
|
else !stack_offset + num_stack_slots.(0) * 4 + n * 8
|
|
|
|
| Outgoing n -> n
|
|
|
|
|
1995-10-31 01:27:04 -08:00
|
|
|
(* Symbols are prefixed with _, except under Linux with ELF binaries *)
|
|
|
|
|
|
|
|
let symbol_prefix =
|
|
|
|
match Config.system with
|
|
|
|
"linux_elf" -> ""
|
|
|
|
| _ -> "_"
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let emit_symbol s =
|
1996-01-07 09:00:35 -08:00
|
|
|
emit_string symbol_prefix; Emitaux.emit_symbol '$' s
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Output a label *)
|
|
|
|
|
1995-10-31 01:27:04 -08:00
|
|
|
let label_prefix =
|
|
|
|
match Config.system with
|
|
|
|
"linux_elf" -> ".L"
|
|
|
|
| _ -> "L"
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let emit_label lbl =
|
1995-10-31 01:27:04 -08:00
|
|
|
emit_string label_prefix; emit_int lbl
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-10-31 02:47:35 -08: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
|
|
|
|
"linux_elf" -> (fun n -> ` .align {emit_int n}\n`)
|
|
|
|
| _ -> (fun n -> ` .align {emit_int(Misc.log2 n)}\n`)
|
|
|
|
|
1996-01-09 10:18:11 -08:00
|
|
|
(* Track the position of the floating-point stack *)
|
|
|
|
|
|
|
|
let fp_offset = ref 0
|
|
|
|
|
|
|
|
let push_fp () =
|
|
|
|
incr fp_offset;
|
|
|
|
if !fp_offset > 4 then fatal_error "Emit: float expression too complex"
|
|
|
|
|
|
|
|
let pop_fp () =
|
|
|
|
decr fp_offset
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Output a pseudo-register *)
|
|
|
|
|
1995-12-10 01:31:57 -08:00
|
|
|
let emit_reg = function
|
|
|
|
{ loc = Reg r; typ = Float } ->
|
|
|
|
emit_string (register_name(r + !fp_offset))
|
|
|
|
| { loc = Reg r } ->
|
1995-06-15 01:17:29 -07:00
|
|
|
emit_string (register_name r)
|
1995-12-10 01:31:57 -08:00
|
|
|
| { loc = Stack s } as r ->
|
1995-06-15 01:17:29 -07:00
|
|
|
let ofs = slot_offset s (register_class r) in
|
|
|
|
`{emit_int ofs}(%esp)`
|
1995-12-10 01:31:57 -08:00
|
|
|
| { loc = Unknown } ->
|
1995-06-15 01:17:29 -07:00
|
|
|
fatal_error "Emit_i386.emit_reg"
|
|
|
|
|
1995-10-25 06:21:30 -07:00
|
|
|
(* 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"
|
|
|
|
|
1995-10-25 07:54:47 -07:00
|
|
|
(* Check if the given register overlaps (same location) with the given
|
|
|
|
array of registers *)
|
|
|
|
|
|
|
|
let register_overlap reg arr =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length arr - 1 do
|
|
|
|
if reg.loc = arr.(i).loc then raise Exit
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit ->
|
|
|
|
true
|
|
|
|
|
1995-11-05 09:28:42 -08:00
|
|
|
(* Check if a set of registers contains a float *)
|
|
|
|
|
|
|
|
let contains_floats arr =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length arr - 1 do
|
|
|
|
if arr.(i).typ = Float then raise Exit
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit ->
|
|
|
|
true
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Output an addressing mode *)
|
|
|
|
|
|
|
|
let emit_addressing addr r n =
|
|
|
|
match addr with
|
|
|
|
Ibased(s, d) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
`{emit_symbol s}`;
|
1995-06-15 01:17:29 -07:00
|
|
|
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)})`
|
1995-07-02 09:41:48 -07:00
|
|
|
| Iscaled(scale, d) ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`(, {emit_reg r.(n)}, {emit_int scale})`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iindexed2scaled(scale, d) ->
|
|
|
|
if d <> 0 then emit_int d;
|
|
|
|
`({emit_reg r.(n)}, {emit_reg r.(n+1)}, {emit_int scale})`
|
|
|
|
|
1995-12-10 01:31:57 -08:00
|
|
|
(* Emit the operand of a floating-point operation *)
|
|
|
|
|
|
|
|
let emit_float_operand r =
|
|
|
|
match r.loc with
|
|
|
|
Stack s -> `l {emit_reg r}`
|
|
|
|
| _ -> ` {emit_reg r}`
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Record live pointers at call points *)
|
|
|
|
|
|
|
|
type frame_descr =
|
|
|
|
{ fd_lbl: int; (* Return address *)
|
|
|
|
fd_frame_size: int; (* Size of stack frame *)
|
|
|
|
fd_live_offset: int list } (* Offsets/regs of live addresses *)
|
|
|
|
|
|
|
|
let frame_descriptors = ref([] : frame_descr list)
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
let record_frame_label live =
|
1995-06-15 01:17:29 -07:00
|
|
|
let lbl = new_label() in
|
|
|
|
let live_offset = ref [] in
|
|
|
|
Reg.Set.iter
|
|
|
|
(function
|
|
|
|
{typ = Addr; loc = Reg r} ->
|
1995-11-26 12:17:06 -08:00
|
|
|
live_offset := ((r lsl 1) + 1) :: !live_offset
|
1995-06-15 01:17:29 -07:00
|
|
|
| {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();
|
|
|
|
fd_live_offset = !live_offset } :: !frame_descriptors;
|
1995-07-10 02:48:27 -07:00
|
|
|
lbl
|
|
|
|
|
|
|
|
let record_frame live =
|
|
|
|
let lbl = record_frame_label live in `{emit_label lbl}:`
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let emit_frame fd =
|
1995-07-10 02:48:27 -07:00
|
|
|
` .long {emit_label fd.fd_lbl}\n`;
|
1995-07-02 09:41:48 -07:00
|
|
|
` .word {emit_int fd.fd_frame_size}\n`;
|
|
|
|
` .word {emit_int (List.length fd.fd_live_offset)}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
List.iter
|
|
|
|
(fun n ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` .word {emit_int n}\n`)
|
1995-07-10 02:48:27 -07:00
|
|
|
fd.fd_live_offset;
|
1995-10-31 02:47:35 -08:00
|
|
|
emit_align 4
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1996-05-14 09:53:07 -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`
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Names for instructions *)
|
|
|
|
|
|
|
|
let instr_for_intop = function
|
|
|
|
Iadd -> "addl"
|
|
|
|
| Isub -> "subl"
|
|
|
|
| Imul -> "imull"
|
|
|
|
| Iand -> "andl"
|
|
|
|
| Ior -> "orl"
|
|
|
|
| Ixor -> "xorl"
|
1995-12-10 07:52:07 -08:00
|
|
|
| Ilsl -> "sall"
|
|
|
|
| Ilsr -> "shrl"
|
|
|
|
| Iasr -> "sarl"
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_intop"
|
|
|
|
|
1995-12-10 01:31:57 -08:00
|
|
|
let instr_for_floatop = function
|
1996-03-07 05:45:17 -08:00
|
|
|
Inegf -> "fchs"
|
|
|
|
| Iabsf -> "fabs"
|
|
|
|
| Iaddf -> "fadd"
|
1995-12-10 01:31:57 -08:00
|
|
|
| Isubf -> "fsub"
|
|
|
|
| Imulf -> "fmul"
|
|
|
|
| Idivf -> "fdiv"
|
1996-01-09 10:18:11 -08:00
|
|
|
| Ispecific Isubfrev -> "fsubr"
|
|
|
|
| Ispecific Idivfrev -> "fdivr"
|
1995-12-10 01:31:57 -08:00
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop"
|
|
|
|
|
|
|
|
let instr_for_floatop_reversed = function
|
|
|
|
Iaddf -> "fadd"
|
|
|
|
| Isubf -> "fsubr"
|
|
|
|
| Imulf -> "fmul"
|
|
|
|
| Idivf -> "fdivr"
|
1996-01-09 10:18:11 -08:00
|
|
|
| Ispecific Isubfrev -> "fsub"
|
|
|
|
| Ispecific Idivfrev -> "fdiv"
|
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop_reversed"
|
1995-12-10 01:31:57 -08:00
|
|
|
|
|
|
|
let instr_for_floatop_pop = function
|
|
|
|
Iaddf -> "faddp"
|
|
|
|
| Isubf -> "fsubp"
|
|
|
|
| Imulf -> "fmulp"
|
|
|
|
| Idivf -> "fdivp"
|
1996-01-09 10:18:11 -08:00
|
|
|
| Ispecific Isubfrev -> "fsubrp"
|
|
|
|
| Ispecific Idivfrev -> "fdivrp"
|
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop_pop"
|
1995-12-10 01:31:57 -08:00
|
|
|
|
|
|
|
let instr_for_floatarithmem = function
|
|
|
|
Ifloatadd -> "faddl"
|
|
|
|
| Ifloatsub -> "fsubl"
|
|
|
|
| Ifloatsubrev -> "fsubrl"
|
|
|
|
| Ifloatmul -> "fmull"
|
|
|
|
| Ifloatdiv -> "fdivl"
|
|
|
|
| Ifloatdivrev -> "fdivrl"
|
|
|
|
|
1995-06-15 01:17:29 -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"
|
|
|
|
|
1995-10-25 07:54:47 -07:00
|
|
|
(* Output an = 0 or <> 0 test. *)
|
1995-07-30 07:26:43 -07:00
|
|
|
|
1995-10-25 07:54:47 -07:00
|
|
|
let output_test_zero arg =
|
1995-07-30 07:26:43 -07:00
|
|
|
match arg.loc with
|
1995-10-25 07:54:47 -07:00
|
|
|
Reg r -> ` testl {emit_reg arg}, {emit_reg arg}\n`
|
|
|
|
| _ -> ` cmpl $0, {emit_reg arg}\n`
|
1995-07-30 07:26:43 -07:00
|
|
|
|
1995-10-25 06:21:30 -07:00
|
|
|
(* Deallocate the stack frame before a return or tail call *)
|
|
|
|
|
|
|
|
let output_epilogue () =
|
1996-02-22 02:24:14 -08:00
|
|
|
let n = frame_size() - 4 in
|
|
|
|
if n > 0 then ` addl ${emit_int n}, %esp\n`
|
1995-10-25 06:21:30 -07:00
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Output the assembly code for an instruction *)
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
(* Name of current function *)
|
1995-06-15 01:17:29 -07:00
|
|
|
let function_name = ref ""
|
1995-07-10 02:48:27 -07:00
|
|
|
(* Entry point for tail recursive calls *)
|
1995-06-15 01:17:29 -07:00
|
|
|
let tailrec_entry_point = ref 0
|
1995-07-10 02:48:27 -07:00
|
|
|
(* Label of trap for out-of-range accesses *)
|
|
|
|
let range_check_trap = ref 0
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let float_constants = ref ([] : (int * string) list)
|
|
|
|
|
|
|
|
let emit_instr i =
|
|
|
|
match i.desc with
|
|
|
|
Lend -> ()
|
|
|
|
| Lop(Imove | Ispill | Ireload) ->
|
|
|
|
if i.arg.(0).loc <> i.res.(0).loc then begin
|
|
|
|
match i.arg.(0).typ with
|
|
|
|
Int | Addr ->
|
|
|
|
` movl {emit_reg i.arg.(0)}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Float ->
|
1995-12-10 01:31:57 -08:00
|
|
|
begin match i.arg.(0).loc with
|
1995-12-10 07:52:07 -08:00
|
|
|
Reg 100 -> (* top of FP stack *)
|
1995-12-10 01:31:57 -08:00
|
|
|
` fstpl {emit_reg i.res.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp()
|
1995-12-10 07:52:07 -08:00
|
|
|
| Reg 101 when !fp_offset = 0 -> (* %st(0) *)
|
1995-12-10 01:31:57 -08:00
|
|
|
` fstl {emit_reg i.res.(0)}\n`
|
|
|
|
| _ ->
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp();
|
1995-12-10 01:31:57 -08:00
|
|
|
` fstpl {emit_reg i.res.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp()
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lop(Iconst_int 0) ->
|
|
|
|
begin 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`
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lop(Iconst_int n) ->
|
|
|
|
` movl ${emit_int n}, {emit_reg i.res.(0)}\n`
|
1995-12-11 02:01:20 -08:00
|
|
|
| Lop(Iconst_float s) ->
|
|
|
|
let f = float_of_string s in
|
|
|
|
if f = 0.0 then
|
1995-07-02 09:41:48 -07:00
|
|
|
` fldz\n`
|
1995-12-11 02:01:20 -08:00
|
|
|
else if f = 1.0 then
|
|
|
|
` fld1\n`
|
1995-07-02 09:41:48 -07:00
|
|
|
else begin
|
|
|
|
let lbl = new_label() in
|
1995-12-11 02:01:20 -08:00
|
|
|
float_constants := (lbl, s) :: !float_constants;
|
1995-07-02 09:41:48 -07:00
|
|
|
` fldl {emit_label lbl}\n`
|
|
|
|
end;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp()
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lop(Iconst_symbol s) ->
|
|
|
|
` movl ${emit_symbol s}, {emit_reg i.res.(0)}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Icall_ind) ->
|
1995-07-10 02:48:27 -07:00
|
|
|
` call *{emit_reg i.arg.(0)}\n`;
|
|
|
|
record_frame i.live
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Icall_imm s) ->
|
1995-07-10 02:48:27 -07:00
|
|
|
` call {emit_symbol s}\n`;
|
|
|
|
record_frame i.live
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Itailcall_ind) ->
|
1995-10-25 06:21:30 -07:00
|
|
|
output_epilogue();
|
1995-06-15 01:17:29 -07:00
|
|
|
` jmp *{emit_reg i.arg.(0)}\n`
|
|
|
|
| Lop(Itailcall_imm s) ->
|
|
|
|
if s = !function_name then
|
|
|
|
` jmp {emit_label !tailrec_entry_point}\n`
|
|
|
|
else begin
|
1995-10-25 06:21:30 -07:00
|
|
|
output_epilogue();
|
1995-07-02 09:41:48 -07:00
|
|
|
` jmp {emit_symbol s}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
1995-07-10 02:48:27 -07:00
|
|
|
| Lop(Iextcall(s, alloc)) ->
|
|
|
|
if alloc then begin
|
|
|
|
` movl ${emit_symbol s}, %eax\n`;
|
1995-10-31 02:47:35 -08:00
|
|
|
` call {emit_symbol "caml_c_call"}\n`;
|
1995-07-10 02:48:27 -07:00
|
|
|
record_frame i.live
|
1995-12-10 01:31:57 -08:00
|
|
|
end else begin
|
|
|
|
if contains_floats i.arg or contains_floats i.res then begin
|
|
|
|
` ffree %st(0)\n`;
|
|
|
|
` ffree %st(1)\n`;
|
|
|
|
` ffree %st(2)\n`;
|
|
|
|
` ffree %st(3)\n`
|
|
|
|
end;
|
|
|
|
` call {emit_symbol s}\n`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Istackoffset n) ->
|
|
|
|
if n >= 0
|
1995-07-02 09:41:48 -07:00
|
|
|
then ` subl ${emit_int n}, %esp\n`
|
|
|
|
else ` addl ${emit_int(-n)}, %esp\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
stack_offset := !stack_offset + n
|
|
|
|
| Lop(Iload(chunk, addr)) ->
|
1995-10-25 06:21:30 -07:00
|
|
|
let dest = i.res.(0) in
|
|
|
|
begin match dest.typ with
|
1995-06-15 01:17:29 -07:00
|
|
|
Int | Addr ->
|
1995-10-25 06:21:30 -07:00
|
|
|
begin match (chunk, dest.loc) with
|
|
|
|
(Word, _) ->
|
|
|
|
` movl {emit_addressing addr i.arg 0}, {emit_reg dest}\n`
|
1995-10-25 07:54:47 -07:00
|
|
|
| (Byte_unsigned, Reg r) when r < 4 & not (register_overlap dest i.arg) ->
|
1995-10-25 06:21:30 -07:00
|
|
|
` xorl {emit_reg dest}, {emit_reg dest}\n`;
|
|
|
|
` movb {emit_addressing addr i.arg 0}, {emit_reg8 dest}\n`
|
|
|
|
| (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`
|
1995-10-25 07:54:47 -07:00
|
|
|
| (Sixteen_unsigned, Reg r) when not (register_overlap dest i.arg) ->
|
1995-10-25 06:21:30 -07:00
|
|
|
` xorl {emit_reg dest}, {emit_reg dest}\n`;
|
|
|
|
` movw {emit_addressing addr i.arg 0}, {emit_reg16 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`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Float ->
|
|
|
|
` fldl {emit_addressing addr i.arg 0}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp()
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
1995-12-17 08:24:34 -08:00
|
|
|
| Lop(Istore(Word, addr)) ->
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match i.arg.(0).typ with
|
|
|
|
Int | Addr ->
|
|
|
|
` movl {emit_reg i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
|
|
|
|
| Float ->
|
1995-12-10 01:31:57 -08:00
|
|
|
begin match i.arg.(0).loc with
|
1995-12-10 07:52:07 -08:00
|
|
|
Reg 100 -> (* top of FP stack *)
|
1995-12-10 01:31:57 -08:00
|
|
|
` fstpl {emit_addressing addr i.arg 1}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp()
|
1995-12-10 07:52:07 -08:00
|
|
|
| Reg 101 when !fp_offset = 0 -> (* %st(0) *)
|
1995-12-10 01:31:57 -08:00
|
|
|
` fstl {emit_addressing addr i.arg 1}\n`
|
|
|
|
| _ ->
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fstpl {emit_addressing addr i.arg 1}\n`
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
1995-12-17 08:24:34 -08:00
|
|
|
| Lop(Istore(chunk, addr)) ->
|
1995-10-25 06:21:30 -07:00
|
|
|
(* i.arg.(0) is guaranteed to be in %edx, actually *)
|
1995-06-15 01:17:29 -07:00
|
|
|
begin match chunk with
|
|
|
|
Word -> fatal_error "Emit_i386: store word"
|
|
|
|
| Byte_unsigned | Byte_signed ->
|
1995-10-25 06:21:30 -07:00
|
|
|
` movb {emit_reg8 i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Sixteen_unsigned | Sixteen_signed ->
|
1995-10-25 06:21:30 -07:00
|
|
|
` movw {emit_reg16 i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
|
|
|
| Lop(Ialloc n) ->
|
|
|
|
if !fastcode_flag then begin
|
1996-05-14 09:53:07 -07:00
|
|
|
let lbl_redo = new_label() in
|
|
|
|
`{emit_label lbl_redo}: movl {emit_symbol "young_ptr"}, %eax\n`;
|
1995-07-02 09:41:48 -07:00
|
|
|
` subl ${emit_int n}, %eax\n`;
|
1995-10-31 02:47:35 -08:00
|
|
|
` movl %eax, {emit_symbol "young_ptr"}\n`;
|
1995-12-21 03:01:45 -08:00
|
|
|
` cmpl {emit_symbol "young_limit"}, %eax\n`;
|
1996-05-14 09:53:07 -07:00
|
|
|
let lbl_call_gc = new_label() in
|
|
|
|
let lbl_frame = record_frame_label i.live in
|
|
|
|
` 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
|
1995-06-15 01:17:29 -07:00
|
|
|
end else begin
|
|
|
|
begin match n with
|
1995-10-31 02:47:35 -08:00
|
|
|
8 -> ` call {emit_symbol "caml_alloc1"}\n`
|
|
|
|
| 12 -> ` call {emit_symbol "caml_alloc2"}\n`
|
|
|
|
| 16 -> ` call {emit_symbol "caml_alloc3"}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> ` movl ${emit_int n}, %eax\n`;
|
1995-10-31 02:47:35 -08:00
|
|
|
` call {emit_symbol "caml_alloc"}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
end;
|
1995-07-10 02:48:27 -07:00
|
|
|
`{record_frame i.live} leal 4(%eax), {emit_reg i.res.(0)}\n`
|
1995-06-15 01:17:29 -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)) ->
|
1995-10-25 07:54:47 -07:00
|
|
|
` cmpl ${emit_int n}, {emit_reg i.arg.(0)}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` set{emit_string b} %al\n`;
|
|
|
|
` movzbl %al, {emit_reg i.res.(0)}\n`
|
1995-07-10 02:48:27 -07:00
|
|
|
| Lop(Iintop Icheckbound) ->
|
|
|
|
if !range_check_trap = 0 then range_check_trap := new_label();
|
|
|
|
` cmpl {emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
` jbe {emit_label !range_check_trap}\n`
|
|
|
|
| Lop(Iintop_imm(Icheckbound, n)) ->
|
|
|
|
if !range_check_trap = 0 then range_check_trap := new_label();
|
|
|
|
` cmpl ${emit_int n}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
` jbe {emit_label !range_check_trap}\n`
|
1995-06-15 01:17:29 -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`
|
|
|
|
| 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`
|
1995-12-10 07:52:07 -08:00
|
|
|
| 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 l = Misc.log2 n in
|
|
|
|
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`
|
1995-06-15 01:17:29 -07:00
|
|
|
| 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`
|
1996-03-07 05:45:17 -08:00
|
|
|
| Lop(Inegf | Iabsf as floatop) ->
|
|
|
|
if i.arg.(0).loc <> Reg 100 then begin
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
push_fp()
|
|
|
|
end;
|
|
|
|
` {emit_string(instr_for_floatop floatop)}\n`
|
1996-01-09 10:18:11 -08:00
|
|
|
| Lop(Iaddf | Isubf | Imulf | Idivf | Ispecific(Isubfrev | Idivfrev)
|
|
|
|
as floatop) ->
|
1995-12-10 01:31:57 -08:00
|
|
|
begin match (i.arg.(0).loc, i.arg.(1).loc) with
|
|
|
|
(Reg 100, Reg 100) -> (* both operands on top of FP stack *)
|
|
|
|
` {emit_string(instr_for_floatop_pop floatop)} %st(0), %st(1)\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp()
|
1995-12-10 01:31:57 -08:00
|
|
|
| (Reg 100, _) -> (* first operand on stack *)
|
|
|
|
` {emit_string(instr_for_floatop floatop)}{emit_float_operand i.arg.(1)}\n`
|
|
|
|
| (_, Reg 100) -> (* second operand on stack *)
|
|
|
|
` {emit_string(instr_for_floatop_reversed floatop)}{emit_float_operand i.arg.(0)}\n`
|
|
|
|
| (_, _) -> (* both in regs or on stack *)
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp();
|
1995-12-10 01:31:57 -08:00
|
|
|
` {emit_string(instr_for_floatop floatop)}{emit_float_operand i.arg.(1)}\n`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Ifloatofint) ->
|
|
|
|
begin match i.arg.(0).loc with
|
|
|
|
Stack s ->
|
1995-12-10 01:31:57 -08:00
|
|
|
` fildl {emit_reg i.arg.(0)}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ ->
|
|
|
|
` pushl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fildl (%esp)\n`;
|
1995-12-10 07:52:07 -08:00
|
|
|
` addl $4, %esp\n`
|
1995-12-10 01:31:57 -08:00
|
|
|
end;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp()
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Iintoffloat) ->
|
1995-12-10 08:41:38 -08:00
|
|
|
if i.arg.(0).loc <> Reg 100 then begin
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp()
|
1995-12-10 08:41:38 -08:00
|
|
|
end;
|
1995-06-15 01:17:29 -07:00
|
|
|
stack_offset := !stack_offset - 8;
|
|
|
|
` subl $8, %esp\n`;
|
|
|
|
` fnstcw 4(%esp)\n`;
|
|
|
|
` movl 4(%esp), %eax\n`;
|
|
|
|
` movb $12, %ah\n`;
|
|
|
|
` movl %eax, (%esp)\n`;
|
|
|
|
` fldcw (%esp)\n`;
|
|
|
|
begin match i.res.(0).loc with
|
|
|
|
Stack s ->
|
1995-12-10 01:31:57 -08:00
|
|
|
` fistpl {emit_reg i.res.(0)}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ ->
|
|
|
|
` fistpl (%esp)\n`;
|
|
|
|
` movl (%esp), {emit_reg i.res.(0)}\n`
|
|
|
|
end;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp();
|
1995-11-03 09:22:25 -08:00
|
|
|
` fldcw 4(%esp)\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
` addl $8, %esp\n`;
|
|
|
|
stack_offset := !stack_offset + 8
|
|
|
|
| Lop(Ispecific(Ilea addr)) ->
|
|
|
|
` lea {emit_addressing addr i.arg 0}, {emit_reg i.res.(0)}\n`
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lop(Ispecific(Istore_int(n, addr))) ->
|
|
|
|
` movl ${emit_int n}, {emit_addressing addr i.arg 0}\n`
|
|
|
|
| Lop(Ispecific(Istore_symbol(s, addr))) ->
|
|
|
|
` movl ${emit_symbol s}, {emit_addressing addr i.arg 0}\n`
|
|
|
|
| Lop(Ispecific(Ioffset_loc(n, addr))) ->
|
|
|
|
` addl ${emit_int n}, {emit_addressing addr i.arg 0}\n`
|
1995-12-10 01:31:57 -08:00
|
|
|
| Lop(Ispecific(Ifloatarithmem(op, addr))) ->
|
|
|
|
if i.arg.(0).loc <> Reg 100 then begin
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp()
|
1995-11-05 09:28:42 -08:00
|
|
|
end;
|
1995-12-10 01:31:57 -08:00
|
|
|
` {emit_string(instr_for_floatarithmem op)} {emit_addressing addr i.arg 1}\n`
|
1995-08-25 01:46:03 -07:00
|
|
|
| Lreloadretaddr ->
|
|
|
|
()
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lreturn ->
|
1995-10-25 06:21:30 -07:00
|
|
|
output_epilogue();
|
1995-06-15 01:17:29 -07:00
|
|
|
` ret\n`
|
|
|
|
| Llabel lbl ->
|
|
|
|
`{emit_label lbl}:\n`
|
|
|
|
| Lbranch lbl ->
|
|
|
|
` jmp {emit_label lbl}\n`
|
|
|
|
| Lcondbranch(tst, lbl) ->
|
|
|
|
begin match tst with
|
1995-06-15 09:08:53 -07:00
|
|
|
Itruetest ->
|
1995-10-25 07:54:47 -07:00
|
|
|
output_test_zero i.arg.(0);
|
1995-06-15 01:17:29 -07:00
|
|
|
` jne {emit_label lbl}\n`
|
|
|
|
| Ifalsetest ->
|
1995-10-25 07:54:47 -07:00
|
|
|
output_test_zero i.arg.(0);
|
1995-06-15 01:17:29 -07:00
|
|
|
` 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`
|
1995-10-25 07:54:47 -07:00
|
|
|
| Iinttest_imm((Isigned Ceq | Isigned Cne |
|
|
|
|
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`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iinttest_imm(cmp, n) ->
|
1995-10-25 07:54:47 -07:00
|
|
|
` cmpl ${emit_int n}, {emit_reg i.arg.(0)}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
let b = name_for_cond_branch cmp in
|
|
|
|
` j{emit_string b} {emit_label lbl}\n`
|
1995-11-28 06:25:57 -08:00
|
|
|
| Ifloattest(cmp, neg) ->
|
1995-12-10 01:31:57 -08:00
|
|
|
let instr =
|
1995-07-24 05:44:17 -07:00
|
|
|
match cmp with
|
|
|
|
Ceq | Cne -> "fucom"
|
1995-12-10 01:31:57 -08:00
|
|
|
| _ -> "fcom" in
|
|
|
|
let actual_cmp =
|
|
|
|
match (i.arg.(0).loc, i.arg.(1).loc) with
|
|
|
|
(Reg 100, Reg 100) -> (* both args on top of FP stack *)
|
|
|
|
` {emit_string instr}pp\n`;
|
|
|
|
fp_offset := !fp_offset - 2;
|
|
|
|
cmp
|
|
|
|
| (Reg 100, _) -> (* first arg on top of FP stack *)
|
|
|
|
` {emit_string instr}p{emit_float_operand i.arg.(1)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp();
|
1995-12-10 01:31:57 -08:00
|
|
|
cmp
|
|
|
|
| (_, Reg 100) -> (* second arg on top of FP stack *)
|
|
|
|
` {emit_string instr}p{emit_float_operand i.arg.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp();
|
1995-12-10 01:31:57 -08:00
|
|
|
Cmm.swap_comparison cmp
|
|
|
|
| (_, _) ->
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
push_fp();
|
1995-12-10 01:31:57 -08:00
|
|
|
` {emit_string instr}p{emit_float_operand i.arg.(1)}\n`;
|
1996-01-09 10:18:11 -08:00
|
|
|
pop_fp();
|
1995-12-10 01:31:57 -08:00
|
|
|
cmp in
|
1995-06-15 01:17:29 -07:00
|
|
|
` fnstsw %ax\n`;
|
1995-12-10 01:31:57 -08:00
|
|
|
begin match actual_cmp with
|
1995-06-15 01:17:29 -07:00
|
|
|
Ceq ->
|
1995-11-28 06:25:57 -08:00
|
|
|
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
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cne ->
|
1995-11-28 06:25:57 -08:00
|
|
|
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
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cle ->
|
1995-07-24 05:44:17 -07:00
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` decb %ah\n`;
|
|
|
|
` cmpb $64, %ah\n`;
|
1995-11-28 06:25:57 -08:00
|
|
|
if neg
|
|
|
|
then ` jae `
|
|
|
|
else ` jb `
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cge ->
|
1995-07-24 05:44:17 -07:00
|
|
|
` andb $5, %ah\n`;
|
1995-11-28 06:25:57 -08:00
|
|
|
if neg
|
|
|
|
then ` jne `
|
|
|
|
else ` je `
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clt ->
|
1995-07-24 05:44:17 -07:00
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` cmpb $1, %ah\n`;
|
1995-11-28 06:25:57 -08:00
|
|
|
if neg
|
|
|
|
then ` jne `
|
|
|
|
else ` je `
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cgt ->
|
1995-07-24 05:44:17 -07:00
|
|
|
` andb $69, %ah\n`;
|
1995-11-28 06:25:57 -08:00
|
|
|
if neg
|
|
|
|
then ` jne `
|
|
|
|
else ` je `
|
|
|
|
end;
|
|
|
|
`{emit_label lbl}\n`
|
1995-07-17 09:10:15 -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`
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
1995-08-12 07:26:23 -07:00
|
|
|
| Lcondbranch3(lbl0, lbl1, lbl2) ->
|
1995-06-15 01:17:29 -07:00
|
|
|
` cmpl $1, {emit_reg i.arg.(0)}\n`;
|
1995-08-12 07:26:23 -07:00
|
|
|
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`;
|
1995-12-12 09:15:39 -08:00
|
|
|
` .data\n`;
|
1995-08-12 07:26:23 -07:00
|
|
|
`{emit_label lbl}:`;
|
|
|
|
for i = 0 to Array.length jumptbl - 1 do
|
|
|
|
` .long {emit_label jumptbl.(i)}\n`
|
1995-12-12 09:15:39 -08:00
|
|
|
done;
|
|
|
|
` .text\n`
|
1995-07-10 02:48:27 -07:00
|
|
|
| Lsetuptrap lbl ->
|
|
|
|
` call {emit_label lbl}\n`
|
|
|
|
| Lpushtrap ->
|
1995-10-31 02:47:35 -08:00
|
|
|
` pushl {emit_symbol "caml_exception_pointer"}\n`;
|
|
|
|
` movl %esp, {emit_symbol "caml_exception_pointer"}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
stack_offset := !stack_offset + 8
|
|
|
|
| Lpoptrap ->
|
1995-10-31 02:47:35 -08:00
|
|
|
` popl {emit_symbol "caml_exception_pointer"}\n`;
|
1995-07-10 02:48:27 -07:00
|
|
|
` addl $4, %esp\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
stack_offset := !stack_offset - 8
|
|
|
|
| Lraise ->
|
1995-10-31 02:47:35 -08:00
|
|
|
` movl {emit_symbol "caml_exception_pointer"}, %esp\n`;
|
|
|
|
` popl {emit_symbol "caml_exception_pointer"}\n`;
|
1995-07-10 02:48:27 -07:00
|
|
|
` ret\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let rec emit_all i =
|
|
|
|
match i.desc with Lend -> () | _ -> emit_instr i; emit_all i.next
|
|
|
|
|
|
|
|
(* Emission of the floating-point constants *)
|
|
|
|
|
|
|
|
let emit_float_constant (lbl, cst) =
|
|
|
|
` .data\n`;
|
|
|
|
`{emit_label lbl}: .double {emit_string cst}\n`
|
|
|
|
|
|
|
|
(* Emission of a function declaration *)
|
|
|
|
|
|
|
|
let fundecl fundecl =
|
|
|
|
function_name := fundecl.fun_name;
|
1995-07-02 09:41:48 -07:00
|
|
|
fastcode_flag := fundecl.fun_fast;
|
1995-06-15 01:17:29 -07:00
|
|
|
tailrec_entry_point := new_label();
|
|
|
|
stack_offset := 0;
|
|
|
|
float_constants := [];
|
1996-05-14 09:53:07 -07:00
|
|
|
call_gc_sites := [];
|
1995-07-10 02:48:27 -07:00
|
|
|
range_check_trap := 0;
|
1995-06-15 01:17:29 -07:00
|
|
|
` .text\n`;
|
1995-10-31 02:47:35 -08:00
|
|
|
emit_align 16; (* 16-byte alignment is recommended for the 486 *)
|
1995-07-02 09:41:48 -07:00
|
|
|
` .globl {emit_symbol fundecl.fun_name}\n`;
|
|
|
|
`{emit_symbol fundecl.fun_name}:\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
let n = frame_size() - 4 in
|
|
|
|
if n > 0 then
|
|
|
|
` subl ${emit_int n}, %esp\n`;
|
|
|
|
`{emit_label !tailrec_entry_point}:`;
|
|
|
|
emit_all fundecl.fun_body;
|
1996-05-14 09:53:07 -07:00
|
|
|
List.iter emit_call_gc !call_gc_sites;
|
1995-07-10 02:48:27 -07:00
|
|
|
if !range_check_trap > 0 then
|
1995-12-05 06:51:21 -08:00
|
|
|
`{emit_label !range_check_trap}: jmp {emit_symbol "array_bound_error"}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
List.iter emit_float_constant !float_constants
|
|
|
|
|
|
|
|
(* Emission of data *)
|
|
|
|
|
|
|
|
let emit_item = function
|
1995-06-22 03:11:18 -07:00
|
|
|
Cdefine_symbol s ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` .globl {emit_symbol s}\n`;
|
|
|
|
`{emit_symbol s}:\n`
|
1995-06-22 03:11:18 -07:00
|
|
|
| Cdefine_label lbl ->
|
|
|
|
`{emit_label (10000 + lbl)}:\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cint8 n ->
|
|
|
|
` .byte {emit_int n}\n`
|
|
|
|
| Cint16 n ->
|
|
|
|
` .word {emit_int n}\n`
|
|
|
|
| Cint n ->
|
|
|
|
` .long {emit_int n}\n`
|
1995-10-26 09:23:25 -07:00
|
|
|
| Cintlit n ->
|
|
|
|
` .long {emit_string n}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cfloat f ->
|
|
|
|
` .double {emit_string f}\n`
|
1995-06-22 03:11:18 -07:00
|
|
|
| Csymbol_address s ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` .long {emit_symbol s}\n`
|
1995-06-22 03:11:18 -07:00
|
|
|
| Clabel_address lbl ->
|
|
|
|
` .long {emit_label (10000 + lbl)}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cstring s ->
|
|
|
|
let l = String.length s in
|
|
|
|
if l = 0 then ()
|
|
|
|
else if l < 80 then
|
|
|
|
` .ascii {emit_string_literal s}\n`
|
|
|
|
else begin
|
|
|
|
let i = ref 0 in
|
|
|
|
while !i < l do
|
|
|
|
let n = min (l - !i) 80 in
|
|
|
|
` .ascii {emit_string_literal(String.sub s !i n)}\n`;
|
|
|
|
i := !i + n
|
|
|
|
done
|
|
|
|
end
|
|
|
|
| Cskip n ->
|
|
|
|
if n > 0 then ` .space {emit_int n}\n`
|
|
|
|
| Calign n ->
|
1995-10-31 02:47:35 -08:00
|
|
|
emit_align n
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let data l =
|
|
|
|
` .data\n`;
|
|
|
|
List.iter emit_item l
|
|
|
|
|
|
|
|
(* Beginning / end of an assembly file *)
|
|
|
|
|
1996-02-20 02:59:12 -08:00
|
|
|
let begin_assembly() =
|
|
|
|
let lbl_begin = Compilenv.current_unit_name() ^ "_begin" in
|
|
|
|
` .data\n`;
|
|
|
|
` .globl {emit_symbol lbl_begin}\n`;
|
|
|
|
`{emit_symbol lbl_begin}:\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let end_assembly() =
|
|
|
|
` .data\n`;
|
1996-02-20 02:59:12 -08:00
|
|
|
let lbl_end = Compilenv.current_unit_name() ^ "_end" in
|
|
|
|
` .globl {emit_symbol lbl_end}\n`;
|
|
|
|
`{emit_symbol lbl_end}:\n`;
|
|
|
|
` .long 0\n`;
|
|
|
|
let lbl = Compilenv.current_unit_name() ^ "_frametable" in
|
1995-07-02 09:41:48 -07:00
|
|
|
` .globl {emit_symbol lbl}\n`;
|
|
|
|
`{emit_symbol lbl}:\n`;
|
1995-07-07 05:07:07 -07:00
|
|
|
` .long {emit_int (List.length !frame_descriptors)}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
List.iter emit_frame !frame_descriptors;
|
1995-07-07 05:07:07 -07:00
|
|
|
frame_descriptors := []
|