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
|
1996-05-16 07:17:59 -07:00
|
|
|
| Outgoing n -> n
|
1995-07-10 02:48:27 -07:00
|
|
|
|
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`)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Output a pseudo-register *)
|
|
|
|
|
1995-12-10 01:31:57 -08:00
|
|
|
let emit_reg = function
|
1996-05-16 12:39:26 -07:00
|
|
|
{ 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-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})`
|
|
|
|
|
|
|
|
(* 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"
|
1996-05-16 12:39:26 -07:00
|
|
|
| Iaddf -> "faddl"
|
|
|
|
| Isubf -> "fsubl"
|
|
|
|
| Imulf -> "fmull"
|
|
|
|
| Idivf -> "fdivl"
|
|
|
|
| Ispecific Isubfrev -> "fsubrl"
|
|
|
|
| Ispecific Idivfrev -> "fdivrl"
|
1995-12-10 01:31:57 -08:00
|
|
|
| _ -> fatal_error "Emit_i386: instr_for_floatop"
|
|
|
|
|
|
|
|
let instr_for_floatop_reversed = function
|
1996-05-16 12:39:26 -07:00
|
|
|
Iaddf -> "faddl"
|
|
|
|
| Isubf -> "fsubrl"
|
|
|
|
| Imulf -> "fmull"
|
|
|
|
| Idivf -> "fdivrl"
|
|
|
|
| Ispecific Isubfrev -> "fsubl"
|
|
|
|
| Ispecific Idivfrev -> "fdivl"
|
1996-01-09 10:18:11 -08:00
|
|
|
| _ -> 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)
|
|
|
|
|
1996-05-16 12:39:26 -07:00
|
|
|
let tos = phys_reg 100
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let emit_instr i =
|
|
|
|
match i.desc with
|
|
|
|
Lend -> ()
|
|
|
|
| Lop(Imove | Ispill | Ireload) ->
|
1996-05-16 12:39:26 -07:00
|
|
|
let src = i.arg.(0) and dst = i.res.(0) in
|
|
|
|
if src.loc <> dst.loc then begin
|
|
|
|
if src.typ = Float then
|
|
|
|
if src = tos then
|
|
|
|
` fstpl {emit_reg dst}\n`
|
|
|
|
else begin
|
|
|
|
` fldl {emit_reg src}\n`;
|
|
|
|
` fstpl {emit_reg dst}\n`
|
|
|
|
end
|
1996-09-11 08:59:11 -07:00
|
|
|
else begin
|
|
|
|
match (src.loc, dst.loc) with
|
|
|
|
(Stack _, Stack(Outgoing _)) ->
|
|
|
|
fatal_error "Emit_i386: stack to outgoing stack"
|
|
|
|
| (Stack _, Stack _) ->
|
|
|
|
(* Stack-to-stack moves are sometimes necessary because
|
|
|
|
of the lack of available registers. This can be done
|
|
|
|
with "pushl [src]; popl [dst]", but the form below
|
|
|
|
is more efficient on the Pentium and Pentium Pro. *)
|
|
|
|
print_string "Stack-to-stack move"; print_newline();
|
|
|
|
stack_offset := !stack_offset + 4;
|
|
|
|
` pushl %eax\n`;
|
|
|
|
` movl {emit_reg src}, %eax\n`;
|
|
|
|
` movl %eax, {emit_reg dst}\n`;
|
|
|
|
` popl %eax\n`;
|
|
|
|
stack_offset := !stack_offset - 4
|
|
|
|
| (_, _) ->
|
|
|
|
` movl {emit_reg src}, {emit_reg dst}\n`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
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`
|
1996-05-16 12:39:26 -07:00
|
|
|
end
|
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
|
|
|
|
` call {emit_symbol s}\n`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Istackoffset n) ->
|
1996-05-16 03:39:23 -07:00
|
|
|
if n < 0
|
|
|
|
then ` addl ${emit_int(-n)}, %esp\n`
|
|
|
|
else ` subl ${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 ->
|
1996-05-16 12:39:26 -07:00
|
|
|
` fldl {emit_addressing addr i.arg 0}\n`
|
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 ->
|
1996-05-16 12:39:26 -07:00
|
|
|
if i.arg.(0) = tos then
|
|
|
|
` fstpl {emit_addressing addr i.arg 1}\n`
|
|
|
|
else begin
|
|
|
|
` 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) ->
|
1996-05-16 12:39:26 -07:00
|
|
|
if i.arg.(0) <> tos then
|
1996-03-07 05:45:17 -08:00
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` {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) ->
|
1996-05-16 12:39:26 -07:00
|
|
|
if i.arg.(0) = tos && i.arg.(1) = tos then
|
|
|
|
(* both operands on top of FP stack *)
|
|
|
|
` {emit_string(instr_for_floatop_pop floatop)} %st(0), %st(1)\n`
|
|
|
|
else if i.arg.(0) = tos then
|
|
|
|
(* first operand on stack *)
|
|
|
|
` {emit_string(instr_for_floatop floatop)} {emit_reg i.arg.(1)}\n`
|
|
|
|
else if i.arg.(1) = tos then
|
|
|
|
(* second operand on stack *)
|
|
|
|
` {emit_string(instr_for_floatop_reversed floatop)} {emit_reg i.arg.(0)}\n`
|
|
|
|
else begin
|
|
|
|
(* both operands in memory *)
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` {emit_string(instr_for_floatop floatop)} {emit_reg i.arg.(1)}\n`
|
1995-12-10 01:31:57 -08:00
|
|
|
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`
|
1996-05-16 12:39:26 -07:00
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Iintoffloat) ->
|
1996-05-16 12:39:26 -07:00
|
|
|
if i.arg.(0) <> tos then
|
1995-12-10 08:41:38 -08:00
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
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;
|
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`
|
1996-05-16 03:39:23 -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
|
1996-05-16 12:39:26 -07:00
|
|
|
{loc = Reg _; typ = Float} ->
|
1996-05-16 03:39:23 -07:00
|
|
|
` subl $8, %esp\n`;
|
1996-05-16 12:39:26 -07:00
|
|
|
` fstpl 0(%esp)\n`;
|
|
|
|
stack_offset := !stack_offset + 8
|
1996-05-16 03:39:23 -07:00
|
|
|
| {loc = Stack sl; typ = Float} ->
|
1996-05-16 12:39:26 -07:00
|
|
|
let ofs = slot_offset sl 1 in
|
|
|
|
` pushl {emit_int(ofs + 4)}(%esp)\n`;
|
|
|
|
` pushl {emit_int(ofs + 4)}(%esp)\n`;
|
1996-05-16 03:39:23 -07:00
|
|
|
stack_offset := !stack_offset + 8
|
|
|
|
| _ ->
|
|
|
|
` pushl {emit_reg r}\n`;
|
|
|
|
stack_offset := !stack_offset + 4
|
|
|
|
done
|
|
|
|
| Lop(Ispecific(Ipush_int n)) ->
|
|
|
|
` pushl ${emit_int n}\n`;
|
|
|
|
stack_offset := !stack_offset + 4
|
|
|
|
| Lop(Ispecific(Ipush_symbol s)) ->
|
|
|
|
` pushl ${emit_symbol s}\n`;
|
|
|
|
stack_offset := !stack_offset + 4
|
1996-05-24 08:17:03 -07:00
|
|
|
| Lop(Ispecific(Ipush_load addr)) ->
|
1996-05-16 03:39:23 -07:00
|
|
|
` pushl {emit_addressing addr i.arg 0}\n`;
|
|
|
|
stack_offset := !stack_offset + 4
|
1996-05-24 08:17:03 -07:00
|
|
|
| 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`;
|
|
|
|
stack_offset := !stack_offset + 8
|
1995-12-10 01:31:57 -08:00
|
|
|
| Lop(Ispecific(Ifloatarithmem(op, addr))) ->
|
1996-05-16 12:39:26 -07:00
|
|
|
if i.arg.(0) <> tos then
|
1995-12-10 01:31:57 -08:00
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` {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`
|
1996-05-16 12:39:26 -07:00
|
|
|
| Ifloattest((Ceq | Cne as cmp), neg) ->
|
|
|
|
if i.arg.(1) <> tos then
|
|
|
|
` fldl {emit_reg i.arg.(1)}\n`;
|
|
|
|
if i.arg.(0) <> tos then
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fucompp\n`;
|
|
|
|
` fnstsw %ax\n`;
|
|
|
|
let neg1 = if cmp = Ceq then neg else not neg in
|
|
|
|
if neg1 then begin (* branch if different *)
|
|
|
|
` andb $68, %ah\n`;
|
|
|
|
` xorb $64, %ah\n`;
|
|
|
|
` jne {emit_label lbl}\n`
|
|
|
|
end else begin (* branch if equal *)
|
|
|
|
` andb $69, %ah\n`;
|
|
|
|
` cmpb $64, %ah\n`;
|
|
|
|
` je {emit_label lbl}\n`
|
|
|
|
end
|
1995-11-28 06:25:57 -08:00
|
|
|
| Ifloattest(cmp, neg) ->
|
1995-12-10 01:31:57 -08:00
|
|
|
let actual_cmp =
|
1996-05-16 12:39:26 -07:00
|
|
|
if i.arg.(0) = tos && i.arg.(1) = tos then begin
|
|
|
|
(* both args on top of FP stack *)
|
|
|
|
` fcompp\n`;
|
|
|
|
cmp
|
|
|
|
end else if i.arg.(0) = tos then begin
|
|
|
|
(* first arg on top of FP stack *)
|
|
|
|
` fcompl {emit_reg i.arg.(1)}\n`;
|
|
|
|
cmp
|
|
|
|
end else if i.arg.(1) = tos then begin
|
|
|
|
(* second arg on top of FP stack *)
|
|
|
|
` fcompl {emit_reg i.arg.(0)}\n`;
|
|
|
|
Cmm.swap_comparison cmp
|
|
|
|
end else begin
|
|
|
|
` fldl {emit_reg i.arg.(0)}\n`;
|
|
|
|
` fcompl {emit_reg i.arg.(1)}\n`;
|
|
|
|
cmp
|
|
|
|
end 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
|
1996-05-16 12:39:26 -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 `
|
1996-05-16 12:39:26 -07:00
|
|
|
| _ -> fatal_error "Emit_i386: floattest"
|
1995-11-28 06:25:57 -08:00
|
|
|
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`;
|
1996-06-22 00:52:38 -07:00
|
|
|
emit_align 4;
|
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
|
1996-06-04 09:01:29 -07:00
|
|
|
`{emit_label !range_check_trap}: call {emit_symbol "array_bound_error"}\n`;
|
|
|
|
(* Never returns, but useful to have retaddr on stack for debugging *)
|
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 := []
|