1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Caml Special Light *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1995 Institut National de Recherche en Informatique et *)
|
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Emission of Sparc 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-18 06:09:02 -07:00
|
|
|
(* Layout of the stack *)
|
|
|
|
(* Always keep the stack 8-aligned.
|
|
|
|
Always leave 96 bytes at the bottom of the stack *)
|
|
|
|
|
|
|
|
let stack_offset = ref 0
|
|
|
|
|
|
|
|
let frame_size () =
|
|
|
|
let size =
|
|
|
|
!stack_offset +
|
|
|
|
4 * num_stack_slots.(0) + 8 * num_stack_slots.(1) +
|
1995-07-28 07:17:50 -07:00
|
|
|
(if !contains_calls then 4 else 0) in
|
1995-07-18 06:09:02 -07:00
|
|
|
Misc.align size 8
|
|
|
|
|
|
|
|
let slot_offset loc class =
|
|
|
|
match loc with
|
|
|
|
Incoming n -> frame_size() + n + 96
|
|
|
|
| Local n ->
|
|
|
|
if class = 0
|
1995-07-28 07:17:50 -07:00
|
|
|
then !stack_offset + num_stack_slots.(1) * 8 + n * 4 + 96
|
|
|
|
else !stack_offset + n * 8 + 96
|
1995-07-18 06:09:02 -07:00
|
|
|
| Outgoing n -> n + 96
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Return the other register in a register pair *)
|
|
|
|
|
|
|
|
let next_in_pair = function
|
|
|
|
{loc = Reg r; typ = (Int | Addr)} -> phys_reg (r + 1)
|
|
|
|
| {loc = Reg r; typ = Float} -> phys_reg (r + 15)
|
|
|
|
| _ -> fatal_error "Emit.next_in_pair"
|
|
|
|
|
1995-08-08 05:17:31 -07:00
|
|
|
(* Symbols are prefixed with _ under SunOS but not under Solaris *)
|
|
|
|
|
|
|
|
let symbol_prefix =
|
|
|
|
match Config.system with
|
|
|
|
"sunos" -> "_"
|
|
|
|
| "solaris" -> ""
|
1995-08-24 08:12:05 -07:00
|
|
|
| _ -> fatal_error "Emit_sparc.symbol_prefix"
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let emit_symbol s =
|
1995-08-08 05:17:31 -07: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-08-08 05:17:31 -07:00
|
|
|
let label_prefix =
|
|
|
|
match Config.system with
|
|
|
|
"sunos" -> "L"
|
|
|
|
| "solaris" -> ".L"
|
1995-08-24 08:12:05 -07:00
|
|
|
| _ -> fatal_error "Emit_sparc.label_prefix"
|
1995-08-08 05:17:31 -07:00
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let emit_label lbl =
|
1995-08-08 05:17:31 -07:00
|
|
|
emit_string label_prefix; emit_int lbl
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* Output a pseudo-register *)
|
|
|
|
|
|
|
|
let emit_reg r =
|
|
|
|
match r.loc with
|
|
|
|
Reg r -> emit_string (register_name r)
|
|
|
|
| _ -> fatal_error "Emit.emit_reg"
|
|
|
|
|
|
|
|
(* Output a stack reference *)
|
|
|
|
|
|
|
|
let emit_stack r =
|
|
|
|
match r.loc with
|
|
|
|
Stack s ->
|
|
|
|
let ofs = slot_offset s (register_class r) in `[%sp + {emit_int ofs}]`
|
|
|
|
| _ -> fatal_error "Emit.emit_stack"
|
|
|
|
|
|
|
|
(* Output a load *)
|
|
|
|
|
|
|
|
let emit_load instr addr arg dst =
|
|
|
|
match addr with
|
|
|
|
Ibased(s, 0) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` sethi %hi({emit_symbol s}), %g1\n`;
|
|
|
|
` {emit_string instr} [%g1 + %lo({emit_symbol s})], {emit_reg dst}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ibased(s, ofs) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` sethi %hi({emit_symbol s} + {emit_int ofs}), %g1\n`;
|
|
|
|
` {emit_string instr} [%g1 + %lo({emit_symbol s} + {emit_int ofs})], {emit_reg dst}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iindexed ofs ->
|
|
|
|
if is_immediate ofs then
|
|
|
|
` {emit_string instr} [{emit_reg arg.(0)} + {emit_int ofs}], {emit_reg dst}\n`
|
|
|
|
else begin
|
|
|
|
` sethi %hi({emit_int ofs}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_int ofs}), %g1\n`;
|
|
|
|
` {emit_string instr} [{emit_reg arg.(0)} + %g1], {emit_reg dst}\n`
|
|
|
|
end
|
|
|
|
| Iindexed2 ofs ->
|
|
|
|
if ofs = 0 then
|
|
|
|
` {emit_string instr} [{emit_reg arg.(0)} + {emit_reg arg.(1)}], {emit_reg dst}\n`
|
|
|
|
else if is_immediate ofs then begin
|
|
|
|
` add {emit_reg arg.(1)}, {emit_int ofs}, %g1\n`;
|
|
|
|
` {emit_string instr} [{emit_reg arg.(0)} + %g1], {emit_reg dst}\n`
|
|
|
|
end else begin
|
|
|
|
` sethi %hi({emit_int ofs}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_int ofs}), %g1\n`;
|
|
|
|
` add {emit_reg arg.(1)}, %g1, %g1\n`;
|
|
|
|
` {emit_string instr} [{emit_reg arg.(0)} + %g1], {emit_reg dst}\n`
|
|
|
|
end
|
|
|
|
|
|
|
|
(* Output a store *)
|
|
|
|
|
|
|
|
let emit_store instr addr arg src =
|
|
|
|
match addr with
|
|
|
|
Ibased(s, 0) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` sethi %hi({emit_symbol s}), %g1\n`;
|
|
|
|
` {emit_string instr} {emit_reg src}, [%g1 + %lo({emit_symbol s})]\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ibased(s, ofs) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` sethi %hi({emit_symbol s} + {emit_int ofs}), %g1\n`;
|
|
|
|
` {emit_string instr} {emit_reg src}, [%g1 + %lo({emit_symbol s} + {emit_int ofs})]\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iindexed ofs ->
|
|
|
|
if is_immediate ofs then
|
|
|
|
` {emit_string instr} {emit_reg src}, [{emit_reg arg.(1)} + {emit_int ofs}]\n`
|
|
|
|
else begin
|
|
|
|
` sethi %hi({emit_int ofs}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_int ofs}), %g1\n`;
|
|
|
|
` {emit_string instr} {emit_reg src}, [{emit_reg arg.(1)} + %g1]\n`
|
|
|
|
end
|
|
|
|
| Iindexed2 ofs ->
|
|
|
|
if ofs = 0 then
|
|
|
|
` {emit_string instr} {emit_reg src}, [{emit_reg arg.(1)} + {emit_reg arg.(2)}]\n`
|
|
|
|
else if is_immediate ofs then begin
|
|
|
|
` add {emit_reg arg.(2)}, {emit_int ofs}, %g1\n`;
|
|
|
|
` {emit_string instr} {emit_reg src}, [{emit_reg arg.(1)} + %g1]\n`
|
|
|
|
end else begin
|
|
|
|
` sethi %hi({emit_int ofs}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_int ofs}), %g1\n`;
|
|
|
|
` add {emit_reg arg.(2)}, %g1, %g1\n`;
|
|
|
|
` {emit_string instr} {emit_reg src}, [{emit_reg arg.(1)} + %g1]\n`
|
|
|
|
end
|
|
|
|
|
|
|
|
(* 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)
|
|
|
|
|
|
|
|
let record_frame live =
|
|
|
|
let lbl = new_label() in
|
|
|
|
let live_offset = ref [] in
|
|
|
|
Reg.Set.iter
|
|
|
|
(function
|
|
|
|
{typ = Addr; loc = Reg r} ->
|
|
|
|
live_offset := (-1 - r) :: !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();
|
|
|
|
fd_live_offset = !live_offset } :: !frame_descriptors;
|
|
|
|
`{emit_label lbl}:`
|
|
|
|
|
|
|
|
let emit_frame fd =
|
1995-07-18 10:40:23 -07:00
|
|
|
` .word {emit_label fd.fd_lbl}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
` .half {emit_int fd.fd_frame_size}\n`;
|
|
|
|
` .half {emit_int (List.length fd.fd_live_offset)}\n`;
|
|
|
|
List.iter
|
|
|
|
(fun n ->
|
|
|
|
` .half {emit_int n}\n`)
|
|
|
|
fd.fd_live_offset;
|
1995-07-18 10:40:23 -07:00
|
|
|
` .align 4\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* Record floating-point constants *)
|
|
|
|
|
|
|
|
let float_constants = ref ([] : (int * string) list)
|
|
|
|
|
|
|
|
let emit_float_constant (lbl, cst) =
|
|
|
|
` .data\n`;
|
1995-08-08 05:17:31 -07:00
|
|
|
` .align 8\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
`{emit_label lbl}: .double 0r{emit_string cst}\n`
|
|
|
|
|
|
|
|
(* Names of various instructions *)
|
|
|
|
|
|
|
|
let name_for_int_operation = function
|
|
|
|
Iadd -> "add"
|
|
|
|
| Isub -> "sub"
|
|
|
|
| Imul -> "smul"
|
|
|
|
| Iand -> "and"
|
|
|
|
| Ior -> "or"
|
|
|
|
| Ixor -> "xor"
|
|
|
|
| Ilsl -> "sll"
|
|
|
|
| Ilsr -> "srl"
|
|
|
|
| Iasr -> "sra"
|
|
|
|
| _ -> Misc.fatal_error "Emit.name_for_int_operation"
|
|
|
|
|
1995-08-13 02:31:50 -07:00
|
|
|
let name_for_float_operation = function
|
|
|
|
Iaddf -> "faddd"
|
1995-08-24 08:12:05 -07:00
|
|
|
| Isubf -> "fsubd"
|
1995-08-13 02:31:50 -07:00
|
|
|
| Imulf -> "fmuld"
|
|
|
|
| Idivf -> "fdivd"
|
|
|
|
| _ -> Misc.fatal_error "Emit.name_for_float_operation"
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let name_for_int_comparison = function
|
|
|
|
Isigned Ceq -> "be" | Isigned Cne -> "bne"
|
|
|
|
| Isigned Cle -> "ble" | Isigned Cgt -> "bg"
|
|
|
|
| Isigned Clt -> "bl" | Isigned Cge -> "bge"
|
|
|
|
| Iunsigned Ceq -> "be" | Iunsigned Cne -> "bne"
|
|
|
|
| Iunsigned Cle -> "bleu" | Iunsigned Cgt -> "bgu"
|
|
|
|
| Iunsigned Clt -> "blu" | Iunsigned Cge -> "bgeu"
|
|
|
|
|
|
|
|
let name_for_float_comparison = function
|
|
|
|
Ceq -> "fbe" | Cne -> "fbne"
|
1995-07-18 10:40:23 -07:00
|
|
|
| Cle -> "fble" | Cgt -> "fbg"
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clt -> "fbl" | Cge -> "fbge"
|
|
|
|
|
|
|
|
(* Output the assembly code for an instruction *)
|
|
|
|
|
|
|
|
let function_name = ref ""
|
|
|
|
let tailrec_entry_point = ref 0
|
|
|
|
|
1995-08-24 06:21:58 -07:00
|
|
|
let rec emit_instr i dslot =
|
1995-06-15 01:17:29 -07:00
|
|
|
match i.desc with
|
|
|
|
Lend -> ()
|
|
|
|
| Lop(Imove | Ispill | Ireload) ->
|
|
|
|
let src = i.arg.(0) and dst = i.res.(0) in
|
1995-08-13 02:31:50 -07:00
|
|
|
begin match (src, dst) with
|
1995-07-18 10:40:23 -07:00
|
|
|
{loc = Reg rs; typ = (Int | Addr)}, {loc = Reg rd} ->
|
1995-06-15 01:17:29 -07:00
|
|
|
` mov {emit_reg src}, {emit_reg dst}\n`
|
1995-07-18 10:40:23 -07:00
|
|
|
| {loc = Reg rs; typ = Float}, {loc = Reg rd; typ = Float} ->
|
1995-06-15 01:17:29 -07:00
|
|
|
` fmovd {emit_reg src}, {emit_reg dst}\n`
|
1995-07-18 10:40:23 -07:00
|
|
|
| {loc = Reg rs; typ = Float}, {loc = Reg rd; typ = (Int | Addr)} ->
|
|
|
|
(* This happens when calling C functions and passing a float arg
|
|
|
|
in %o0...%o5 *)
|
|
|
|
` sub %sp, 8, %sp\n`;
|
|
|
|
` std {emit_reg src}, [%sp + 96]\n`;
|
|
|
|
if rd land 1 = 0 then
|
|
|
|
` ldd [%sp + 96], {emit_reg dst}\n`
|
|
|
|
else begin
|
|
|
|
` ld [%sp + 96], {emit_reg dst}\n`;
|
1995-07-30 07:26:43 -07:00
|
|
|
` ld [%sp + 100], {emit_reg(next_in_pair dst)}\n`
|
1995-07-18 10:40:23 -07:00
|
|
|
end;
|
|
|
|
` add %sp, 8, %sp\n`
|
|
|
|
| {loc = Reg rs; typ = (Int | Addr)}, {loc = Stack sd} ->
|
|
|
|
` st {emit_reg src}, {emit_stack dst}\n`
|
|
|
|
| {loc = Reg rs; typ = Float}, {loc = Stack sd} ->
|
|
|
|
` std {emit_reg src}, {emit_stack dst}\n`
|
|
|
|
| {loc = Stack ss; typ = (Int | Addr)}, {loc = Reg rd} ->
|
|
|
|
` ld {emit_stack src}, {emit_reg dst}\n`
|
|
|
|
| {loc = Stack ss; typ = Float}, {loc = Reg rd} ->
|
|
|
|
` ldd {emit_stack src}, {emit_reg dst}\n`
|
|
|
|
| (_, _) ->
|
|
|
|
fatal_error "Emit: Imove"
|
1995-06-15 01:17:29 -07:00
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lop(Iconst_int n) ->
|
|
|
|
if is_immediate n then
|
|
|
|
` mov {emit_int n}, {emit_reg i.res.(0)}\n`
|
|
|
|
else begin
|
|
|
|
` sethi %hi({emit_int n}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_int n}), {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_float s) ->
|
|
|
|
let lbl = new_label() in
|
|
|
|
float_constants := (lbl, s) :: !float_constants;
|
|
|
|
` sethi %hi({emit_label lbl}), %g1\n`;
|
|
|
|
` ldd [%g1 + %lo({emit_label lbl})], {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iconst_symbol s) ->
|
|
|
|
` sethi %hi({emit_symbol s}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_symbol s}), {emit_reg i.res.(0)}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Icall_ind) ->
|
|
|
|
`{record_frame i.live} call {emit_reg i.arg.(0)}\n`;
|
1995-08-24 06:21:58 -07:00
|
|
|
fill_delay_slot dslot
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Icall_imm s) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
`{record_frame i.live} call {emit_symbol s}\n`;
|
1995-08-24 06:21:58 -07:00
|
|
|
fill_delay_slot dslot
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Itailcall_ind) ->
|
|
|
|
let n = frame_size() in
|
1995-08-24 06:21:58 -07:00
|
|
|
if !contains_calls then
|
1995-06-15 01:17:29 -07:00
|
|
|
` ld [%sp + {emit_int(n - 4 + 96)}], %o7\n`;
|
1995-08-24 06:21:58 -07:00
|
|
|
` jmp {emit_reg i.arg.(0)}\n`;
|
|
|
|
` add %sp, {emit_int n}, %sp\n` (* in delay slot *)
|
1995-08-13 02:31:50 -07:00
|
|
|
| Lop(Itailcall_imm s) ->
|
|
|
|
let n = frame_size() in
|
1995-08-24 06:21:58 -07:00
|
|
|
if s = !function_name then begin
|
1995-08-13 02:31:50 -07:00
|
|
|
` b {emit_label !tailrec_entry_point}\n`;
|
1995-08-24 06:21:58 -07:00
|
|
|
fill_delay_slot dslot
|
|
|
|
end else begin
|
|
|
|
if !contains_calls then
|
1995-08-13 02:31:50 -07:00
|
|
|
` ld [%sp + {emit_int(n - 4 + 96)}], %o7\n`;
|
1995-08-24 06:21:58 -07:00
|
|
|
` sethi %hi({emit_symbol s}), %g1\n`;
|
|
|
|
` jmp %g1 + %lo({emit_symbol s})\n`;
|
|
|
|
` add %sp, {emit_int n}, %sp\n` (* in delay slot *)
|
|
|
|
end
|
1995-07-18 06:09:02 -07:00
|
|
|
| Lop(Iextcall(s, alloc)) ->
|
|
|
|
if alloc then begin
|
1995-07-18 10:40:23 -07:00
|
|
|
` sethi %hi({emit_symbol s}), %g4\n`;
|
1995-08-08 05:17:31 -07:00
|
|
|
`{record_frame i.live} call {emit_symbol "caml_c_call"}\n`;
|
1995-08-13 02:31:50 -07:00
|
|
|
` or %g4, %lo({emit_symbol s}), %g4\n` (* in delay slot *)
|
1995-07-18 06:09:02 -07:00
|
|
|
end else begin
|
1995-07-18 10:40:23 -07:00
|
|
|
` call {emit_symbol s}\n`;
|
1995-08-24 06:21:58 -07:00
|
|
|
fill_delay_slot dslot
|
1995-07-18 06:09:02 -07:00
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Istackoffset n) ->
|
|
|
|
` add %sp, {emit_int (-n)}, %sp\n`;
|
|
|
|
stack_offset := !stack_offset + n
|
|
|
|
| Lop(Iload(chunk, addr)) ->
|
|
|
|
begin match i.res.(0).typ with
|
|
|
|
Int | Addr ->
|
|
|
|
let loadinstr =
|
|
|
|
match chunk with
|
|
|
|
Word -> "ld"
|
|
|
|
| Byte_unsigned -> "ldub"
|
|
|
|
| Byte_signed -> "ldsb"
|
|
|
|
| Sixteen_unsigned -> "lduh"
|
|
|
|
| Sixteen_signed -> "ldsh" in
|
|
|
|
emit_load loadinstr addr i.arg i.res.(0)
|
|
|
|
| Float ->
|
|
|
|
emit_load "ld" addr i.arg i.res.(0);
|
|
|
|
emit_load "ld" (offset_addressing addr 4) i.arg (next_in_pair i.res.(0))
|
|
|
|
end
|
|
|
|
| Lop(Istore(chunk, addr)) ->
|
|
|
|
begin match i.arg.(0).typ with
|
|
|
|
Int | Addr ->
|
|
|
|
let storeinstr =
|
|
|
|
match chunk with
|
|
|
|
Word -> "st"
|
|
|
|
| Byte_unsigned | Byte_signed -> "stb"
|
|
|
|
| Sixteen_unsigned | Sixteen_signed -> "sth" in
|
|
|
|
emit_store storeinstr addr i.arg i.arg.(0)
|
|
|
|
| Float ->
|
|
|
|
emit_store "st" addr i.arg i.arg.(0);
|
|
|
|
emit_store "st" (offset_addressing addr 4) i.arg (next_in_pair i.arg.(0))
|
|
|
|
end
|
|
|
|
| Lop(Ialloc n) ->
|
|
|
|
if !fastcode_flag then begin
|
|
|
|
let lbl_cont = new_label() in
|
|
|
|
` sub %g6, {emit_int n}, %g6\n`;
|
|
|
|
` cmp %g6, %g7\n`;
|
|
|
|
` bgeu {emit_label lbl_cont}\n`;
|
1995-08-13 02:31:50 -07:00
|
|
|
` add %g6, 4, {emit_reg i.res.(0)}\n`; (* in delay slot *)
|
1995-08-08 05:17:31 -07:00
|
|
|
`{record_frame i.live} call {emit_symbol "caml_call_gc"}\n`;
|
1995-08-13 02:31:50 -07:00
|
|
|
` mov {emit_int n}, %g4\n`; (* in delay slot *)
|
1995-06-15 01:17:29 -07:00
|
|
|
` add %g6, 4, {emit_reg i.res.(0)}\n`;
|
1995-07-20 01:30:16 -07:00
|
|
|
`{emit_label lbl_cont}:\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
end else begin
|
1995-08-08 05:17:31 -07:00
|
|
|
`{record_frame i.live} call {emit_symbol "caml_alloc"}\n`;
|
1995-08-13 02:31:50 -07:00
|
|
|
` mov {emit_int n}, %g4\n`; (* in delay slot *)
|
1995-06-15 01:17:29 -07:00
|
|
|
` add %g6, 4, {emit_reg i.res.(0)}\n`
|
|
|
|
end
|
|
|
|
| Lop(Iintop Idiv) ->
|
|
|
|
` sra {emit_reg i.arg.(0)}, 31, %g1\n`;
|
|
|
|
` wr %g0, %g1, %y\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` sdiv {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop Imod) ->
|
|
|
|
` sra {emit_reg i.arg.(0)}, 31, %g1\n`;
|
|
|
|
` wr %g0, %g1, %y\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` sdiv {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}, %g1\n`;
|
|
|
|
` smul %g1, {emit_reg i.arg.(1)}, %g1\n`;
|
|
|
|
` sub {emit_reg i.arg.(0)}, %g1, {emit_reg i.res.(0)}\n`
|
1995-07-18 06:09:02 -07:00
|
|
|
| Lop(Iintop(Icomp cmp)) ->
|
|
|
|
let comp = name_for_int_comparison cmp in
|
|
|
|
` cmp {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
|
|
|
let lbl = new_label() in
|
|
|
|
` {emit_string comp},a {emit_label lbl}\n`;
|
|
|
|
` mov 1, {emit_reg i.res.(0)}\n`;
|
|
|
|
` mov 0, {emit_reg i.res.(0)}\n`;
|
|
|
|
`{emit_label lbl}:\n`
|
|
|
|
| Lop(Iintop Icheckbound) ->
|
|
|
|
` cmp {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
|
|
|
` tleu 5\n` (* 5 = ST_RANGE_CHECK *)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Iintop op) ->
|
|
|
|
let instr = name_for_int_operation op in
|
|
|
|
` {emit_string instr} {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintop_imm(Idiv, n)) ->
|
1995-07-25 06:04:41 -07:00
|
|
|
let l = Misc.log2 n in
|
|
|
|
if n = 1 lsl l then begin
|
|
|
|
let lbl = new_label() in
|
1995-07-28 07:17:50 -07:00
|
|
|
` cmp {emit_reg i.arg.(0)}, 0\n`;
|
1995-07-25 06:04:41 -07:00
|
|
|
` bge {emit_label lbl}\n`;
|
1995-07-28 07:17:50 -07:00
|
|
|
` mov {emit_reg i.arg.(0)}, %g1\n`; (* in delay slot *)
|
|
|
|
` add %g1, {emit_int (n-1)}, %g1\n`;
|
|
|
|
`{emit_label lbl}:\n`;
|
|
|
|
` sra %g1, {emit_int l}, {emit_reg i.res.(0)}\n`
|
1995-07-25 06:04:41 -07:00
|
|
|
end else begin
|
|
|
|
` sra {emit_reg i.arg.(0)}, 31, %g1\n`;
|
|
|
|
` wr %g0, %g1, %y\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` sdiv {emit_reg i.arg.(0)}, {emit_int n}, {emit_reg i.res.(0)}\n`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Iintop_imm(Imod, n)) ->
|
1995-07-25 06:04:41 -07:00
|
|
|
let l = Misc.log2 n in
|
|
|
|
if n = 1 lsl l then begin
|
|
|
|
let lbl = new_label() in
|
|
|
|
` tst {emit_reg i.arg.(0)}\n`;
|
|
|
|
` bge {emit_label lbl}\n`;
|
|
|
|
` andcc {emit_reg i.arg.(0)}, {emit_int (n-1)}, {emit_reg i.res.(0)}\n`; (* in delay slot *)
|
|
|
|
` be {emit_label lbl}\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` sub {emit_reg i.res.(0)}, {emit_int n}, {emit_reg i.res.(0)}\n`;
|
|
|
|
`{emit_label lbl}:\n`
|
|
|
|
end else begin
|
|
|
|
` sra {emit_reg i.arg.(0)}, 31, %g1\n`;
|
|
|
|
` wr %g0, %g1, %y\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` nop\n`;
|
|
|
|
` sdiv {emit_reg i.arg.(0)}, {emit_int n}, %g1\n`;
|
|
|
|
` smul %g1, {emit_int n}, %g1\n`;
|
|
|
|
` sub {emit_reg i.arg.(0)}, %g1, {emit_reg i.res.(0)}\n`
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Iintop_imm(Icomp cmp, n)) ->
|
|
|
|
let comp = name_for_int_comparison cmp in
|
|
|
|
` cmp {emit_reg i.arg.(0)}, {emit_int n}\n`;
|
|
|
|
let lbl = new_label() in
|
|
|
|
` {emit_string comp},a {emit_label lbl}\n`;
|
|
|
|
` mov 1, {emit_reg i.res.(0)}\n`;
|
|
|
|
` mov 0, {emit_reg i.res.(0)}\n`;
|
|
|
|
`{emit_label lbl}:\n`
|
1995-07-18 06:09:02 -07:00
|
|
|
| Lop(Iintop_imm(Icheckbound, n)) ->
|
|
|
|
` cmp {emit_reg i.arg.(0)}, {emit_int n}\n`;
|
|
|
|
` tleu 5\n` (* 5 = ST_RANGE_CHECK *)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Iintop_imm(op, n)) ->
|
|
|
|
let instr = name_for_int_operation op in
|
|
|
|
` {emit_string instr} {emit_reg i.arg.(0)}, {emit_int n}, {emit_reg i.res.(0)}\n`
|
1995-08-24 08:12:05 -07:00
|
|
|
| Lop(Iaddf | Isubf | Imulf | Idivf as op) ->
|
1995-08-13 02:31:50 -07:00
|
|
|
let instr = name_for_float_operation op in
|
|
|
|
` {emit_string instr} {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}, {emit_reg i.res.(0)}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lop(Ifloatofint) ->
|
|
|
|
` sub %sp, 4, %sp\n`;
|
|
|
|
` st {emit_reg i.arg.(0)}, [%sp + 96]\n`;
|
|
|
|
` ld [%sp + 96], %f30\n`;
|
|
|
|
` add %sp, 4, %sp\n`;
|
|
|
|
` fitod %f30, {emit_reg i.res.(0)}\n`
|
|
|
|
| Lop(Iintoffloat) ->
|
|
|
|
` fdtoi {emit_reg i.arg.(0)}, %f30\n`;
|
|
|
|
` sub %sp, 4, %sp\n`;
|
|
|
|
` st %f30, [%sp + 96]\n`;
|
|
|
|
` ld [%sp + 96], {emit_reg i.res.(0)}\n`;
|
|
|
|
` add %sp, 4, %sp\n`
|
|
|
|
| Lop(Ispecific sop) ->
|
|
|
|
fatal_error "Emit: specific"
|
1995-08-25 01:46:03 -07:00
|
|
|
| Lreloadretaddr ->
|
|
|
|
let n = frame_size() in
|
|
|
|
` ld [%sp + {emit_int(n - 4 + 96)}], %o7\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lreturn ->
|
|
|
|
let n = frame_size() in
|
1995-08-24 08:12:05 -07:00
|
|
|
(* Must and the return address even if this is a leaf routine,
|
|
|
|
because it may have been tail-called with a marked retaddr. *)
|
|
|
|
` andn %o7, 1, %o7\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
` retl\n`;
|
1995-08-13 02:31:50 -07:00
|
|
|
` add %sp, {emit_int n}, %sp\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Llabel lbl ->
|
|
|
|
`{emit_label lbl}:\n`
|
|
|
|
| Lbranch lbl ->
|
|
|
|
` b {emit_label lbl}\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
fill_delay_slot dslot
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lcondbranch(tst, lbl) ->
|
|
|
|
begin match tst with
|
1995-06-15 09:08:53 -07:00
|
|
|
Itruetest ->
|
1995-06-15 01:17:29 -07:00
|
|
|
` tst {emit_reg i.arg.(0)}\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` bne {emit_label lbl}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ifalsetest ->
|
|
|
|
` tst {emit_reg i.arg.(0)}\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` be {emit_label lbl}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iinttest cmp ->
|
|
|
|
let comp = name_for_int_comparison cmp in
|
|
|
|
` cmp {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` {emit_string comp} {emit_label lbl}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iinttest_imm(cmp, n) ->
|
|
|
|
let comp = name_for_int_comparison cmp in
|
|
|
|
` cmp {emit_reg i.arg.(0)}, {emit_int n}\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` {emit_string comp} {emit_label lbl}\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ifloattest cmp ->
|
|
|
|
let comp = name_for_float_comparison cmp in
|
|
|
|
` fcmpd {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
1995-07-18 10:40:23 -07:00
|
|
|
` nop\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` {emit_string comp} {emit_label lbl}\n`
|
1995-07-17 09:10:15 -07:00
|
|
|
| Ioddtest ->
|
|
|
|
` andcc {emit_reg i.arg.(0)}, 1, %g0\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` bne {emit_label lbl}\n`
|
1995-07-18 06:09:02 -07:00
|
|
|
| Ieventest ->
|
1995-07-17 09:10:15 -07:00
|
|
|
` andcc {emit_reg i.arg.(0)}, 1, %g0\n`;
|
1995-08-24 08:12:05 -07:00
|
|
|
` be {emit_label lbl}\n`
|
|
|
|
end;
|
|
|
|
fill_delay_slot dslot
|
1995-08-12 07:26:23 -07:00
|
|
|
| Lcondbranch3(lbl0, lbl1, lbl2) ->
|
|
|
|
` cmp {emit_reg i.arg.(0)}, 1\n`;
|
|
|
|
begin match lbl0 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` bl {emit_label lbl}\n nop\n`
|
|
|
|
end;
|
|
|
|
begin match lbl1 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` be {emit_label lbl}\n nop\n`
|
|
|
|
end;
|
|
|
|
begin match lbl2 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` bg {emit_label lbl}\n nop\n`
|
1995-07-18 06:09:02 -07:00
|
|
|
end
|
1995-08-12 07:26:23 -07:00
|
|
|
| Lswitch jumptbl ->
|
1995-08-24 08:12:05 -07:00
|
|
|
let lbl_jumptbl = new_label() in
|
1995-08-12 07:26:23 -07:00
|
|
|
` sethi %hi({emit_label lbl_jumptbl}), %g1\n`;
|
|
|
|
` or %g1, %lo({emit_label lbl_jumptbl}), %g1\n`;
|
|
|
|
` sll {emit_reg i.arg.(0)}, 2, %g4\n`;
|
|
|
|
` ld [%g1 + %g4], %g1\n`;
|
1995-08-13 02:31:50 -07:00
|
|
|
` jmp %g1\n`; (* poor scheduling *)
|
1995-08-12 07:26:23 -07:00
|
|
|
` nop\n`;
|
|
|
|
`{emit_label lbl_jumptbl}:`;
|
|
|
|
for i = 0 to Array.length jumptbl - 1 do
|
|
|
|
` .word {emit_label jumptbl.(i)}\n`
|
|
|
|
done
|
1995-07-18 10:40:23 -07:00
|
|
|
| Lsetuptrap lbl ->
|
|
|
|
` call {emit_label lbl}\n`;
|
|
|
|
` mov %o7, %g4\n` (* in delay slot *)
|
|
|
|
| Lpushtrap ->
|
1995-06-15 01:17:29 -07:00
|
|
|
stack_offset := !stack_offset + 8;
|
|
|
|
` sub %sp, 8, %sp\n`;
|
|
|
|
` std %g4, [%sp + 96]\n`; (* Write %g4 and %g5 *)
|
|
|
|
` mov %sp, %g5\n`
|
|
|
|
| Lpoptrap ->
|
|
|
|
` ld [%sp + 100], %g5\n`;
|
|
|
|
` add %sp, 8, %sp\n`;
|
|
|
|
stack_offset := !stack_offset - 8
|
|
|
|
| Lraise ->
|
|
|
|
` mov %g5, %sp\n`;
|
|
|
|
` ldd [%sp + 96], %g4\n`; (* Load %g4 and %g5 *)
|
1995-08-13 02:31:50 -07:00
|
|
|
` jmp %g4 + 8\n`; (* poor scheduling *)
|
1995-06-15 01:17:29 -07:00
|
|
|
` add %sp, 8, %sp\n`
|
|
|
|
|
1995-08-24 06:21:58 -07:00
|
|
|
and emit_delay_slot = function
|
|
|
|
None -> ()
|
1995-08-24 08:12:05 -07:00
|
|
|
| Some i -> emit_instr i None
|
1995-08-24 06:21:58 -07:00
|
|
|
|
|
|
|
and fill_delay_slot = function
|
|
|
|
None -> ` nop\n`
|
1995-08-24 08:12:05 -07:00
|
|
|
| Some i -> emit_instr i None
|
1995-08-24 06:21:58 -07:00
|
|
|
|
1995-08-24 08:12:05 -07:00
|
|
|
(* Checks if a pseudo-instruction expands to exactly one machine instruction
|
|
|
|
that does not branch. *)
|
1995-08-24 06:21:58 -07:00
|
|
|
|
|
|
|
let is_one_instr_op = function
|
|
|
|
Idiv | Imod | Icomp _ | Icheckbound -> false
|
|
|
|
| _ -> true
|
|
|
|
|
1995-08-24 08:12:05 -07:00
|
|
|
let is_one_instr i =
|
|
|
|
match i.desc with
|
|
|
|
Lop op ->
|
|
|
|
begin match op with
|
|
|
|
Imove | Ispill | Ireload -> i.arg.(0).typ = i.res.(0).typ
|
|
|
|
| Iconst_int n -> is_immediate n
|
|
|
|
| Istackoffset _ -> true
|
|
|
|
| Iload(_, Iindexed n) -> is_immediate n
|
|
|
|
| Iload(_, Iindexed2 n) -> n = 0
|
|
|
|
| Istore(_, Iindexed n) -> is_immediate n
|
|
|
|
| Istore(_, Iindexed2 n) -> n = 0
|
|
|
|
| Iintop(op) -> is_one_instr_op op
|
|
|
|
| Iintop_imm(op, _) -> is_one_instr_op op
|
|
|
|
| Iaddf | Isubf | Imulf | Idivf -> true
|
|
|
|
| _ -> false
|
|
|
|
end
|
1995-08-24 06:21:58 -07:00
|
|
|
| _ -> false
|
|
|
|
|
1995-08-24 08:12:05 -07:00
|
|
|
let no_interference res arg =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length arg - 1 do
|
|
|
|
for j = 0 to Array.length res - 1 do
|
|
|
|
if arg.(i).loc = res.(j).loc then raise Exit
|
|
|
|
done
|
|
|
|
done;
|
|
|
|
true
|
|
|
|
with Exit ->
|
|
|
|
false
|
|
|
|
|
1995-08-24 06:21:58 -07:00
|
|
|
(* Emit a sequence of instructions, trying to fill delay slots for branches *)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
let rec emit_all i =
|
1995-08-24 06:21:58 -07:00
|
|
|
match i with
|
|
|
|
{desc = Lend} -> ()
|
1995-08-25 01:46:03 -07:00
|
|
|
| {next = {desc = Lop(Icall_imm _) | Lop(Iextcall(_, false)) | Lbranch _}}
|
1995-08-24 08:12:05 -07:00
|
|
|
when is_one_instr i ->
|
1995-08-24 06:21:58 -07:00
|
|
|
emit_instr i.next (Some i);
|
|
|
|
emit_all i.next.next
|
|
|
|
| {next = {desc = Lop(Itailcall_imm s)}}
|
1995-08-24 08:12:05 -07:00
|
|
|
when s = !function_name & is_one_instr i ->
|
1995-08-24 06:21:58 -07:00
|
|
|
emit_instr i.next (Some i);
|
|
|
|
emit_all i.next.next
|
|
|
|
| {next = {desc = Lop(Icall_ind)}}
|
1995-08-24 08:12:05 -07:00
|
|
|
when is_one_instr i & no_interference i.res i.next.arg ->
|
1995-08-24 06:21:58 -07:00
|
|
|
emit_instr i.next (Some i);
|
|
|
|
emit_all i.next.next
|
1995-08-24 08:12:05 -07:00
|
|
|
| {next = {desc = Lcondbranch(_, _)}}
|
|
|
|
when is_one_instr i & no_interference i.res i.next.arg ->
|
1995-08-24 06:21:58 -07:00
|
|
|
emit_instr i.next (Some i);
|
|
|
|
emit_all i.next.next
|
|
|
|
| _ ->
|
|
|
|
emit_instr i None;
|
|
|
|
emit_all i.next
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* 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 := [];
|
|
|
|
` .text\n`;
|
|
|
|
` .align 4\n`;
|
1995-07-02 09:41:48 -07:00
|
|
|
` .global {emit_symbol fundecl.fun_name}\n`;
|
|
|
|
`{emit_symbol fundecl.fun_name}:\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
let n = frame_size() in
|
|
|
|
if n > 0 then
|
|
|
|
` sub %sp, {emit_int n}, %sp\n`;
|
|
|
|
if !contains_calls then
|
|
|
|
` st %o7, [%sp + {emit_int(n - 4 + 96)}]\n`;
|
1995-07-18 10:40:23 -07:00
|
|
|
`{emit_label !tailrec_entry_point}:\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
emit_all fundecl.fun_body;
|
|
|
|
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
|
|
|
` .global {emit_symbol s}\n`;
|
|
|
|
`{emit_symbol s}:\n`
|
1995-06-22 03:11:18 -07:00
|
|
|
| Cdefine_label lbl ->
|
|
|
|
`{emit_label (lbl + 10000)}:\n`
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cint8 n ->
|
|
|
|
` .byte {emit_int n}\n`
|
|
|
|
| Cint16 n ->
|
|
|
|
` .half {emit_int n}\n`
|
|
|
|
| Cint n ->
|
|
|
|
` .word {emit_int n}\n`
|
|
|
|
| Cfloat f ->
|
|
|
|
` .double 0r{emit_string f}\n`
|
1995-06-22 03:11:18 -07:00
|
|
|
| Csymbol_address s ->
|
1995-07-02 09:41:48 -07:00
|
|
|
` .word {emit_symbol s}\n`
|
1995-07-18 10:40:23 -07:00
|
|
|
| Clabel_address lbl ->
|
1995-06-22 03:11:18 -07:00
|
|
|
` .word {emit_label (lbl + 10000)}\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 ` .skip {emit_int n}\n`
|
|
|
|
| Calign n ->
|
1995-07-18 10:40:23 -07:00
|
|
|
` .align {emit_int n}\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 *)
|
|
|
|
|
|
|
|
let begin_assembly() = ()
|
|
|
|
|
|
|
|
let end_assembly() =
|
1995-07-18 10:40:23 -07:00
|
|
|
let lbl = Compilenv.current_unit_name() ^ "_frametable" in
|
1995-06-15 01:17:29 -07:00
|
|
|
` .data\n`;
|
1995-07-18 10:40:23 -07:00
|
|
|
` .global {emit_symbol lbl}\n`;
|
|
|
|
`{emit_symbol lbl}:\n`;
|
|
|
|
` .word {emit_int (List.length !frame_descriptors)}\n`;
|
1995-06-15 01:17:29 -07:00
|
|
|
List.iter emit_frame !frame_descriptors;
|
1995-07-18 10:40:23 -07:00
|
|
|
frame_descriptors := []
|