1997-07-24 06:36:24 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1997-07-24 06:36:24 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1997-07-24 06:36:24 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* Emission of PowerPC assembly code *)
|
|
|
|
|
2013-03-19 00:22:12 -07:00
|
|
|
module StringSet =
|
|
|
|
Set.Make(struct type t = string let compare (x:t) y = compare x y end)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Cmm
|
|
|
|
open Arch
|
|
|
|
open Proc
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
open Linearize
|
|
|
|
open Emitaux
|
|
|
|
|
2004-06-19 09:17:31 -07:00
|
|
|
(* Layout of the stack. The stack is kept 16-aligned. *)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let stack_offset = ref 0
|
|
|
|
|
|
|
|
let frame_size () =
|
|
|
|
let size =
|
|
|
|
!stack_offset + (* Trap frame, outgoing parameters *)
|
2006-05-31 01:16:34 -07:00
|
|
|
size_int * num_stack_slots.(0) + (* Local int variables *)
|
|
|
|
size_float * num_stack_slots.(1) + (* Local float variables *)
|
|
|
|
(if !contains_calls then size_int else 0) in (* The return address *)
|
2003-06-20 08:17:52 -07:00
|
|
|
Misc.align size 16
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let slot_offset loc cls =
|
|
|
|
match loc with
|
|
|
|
Local n ->
|
|
|
|
if cls = 0
|
2006-05-31 01:16:34 -07:00
|
|
|
then !stack_offset + num_stack_slots.(1) * size_float + n * size_int
|
|
|
|
else !stack_offset + n * size_float
|
1997-07-24 06:36:24 -07:00
|
|
|
| Incoming n -> frame_size() + n
|
|
|
|
| Outgoing n -> n
|
|
|
|
|
|
|
|
(* Output a symbol *)
|
|
|
|
|
1998-03-13 05:57:35 -08:00
|
|
|
let emit_symbol =
|
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" -> (fun s -> Emitaux.emit_symbol '.' s)
|
1998-03-13 05:57:35 -08:00
|
|
|
| "rhapsody" -> (fun s -> emit_char '_'; Emitaux.emit_symbol '$' s)
|
|
|
|
| _ -> assert false
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
(* Output a label *)
|
|
|
|
|
1998-03-13 05:57:35 -08:00
|
|
|
let label_prefix =
|
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" -> ".L"
|
1998-03-13 05:57:35 -08:00
|
|
|
| "rhapsody" -> "L"
|
|
|
|
| _ -> assert false
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let emit_label lbl =
|
|
|
|
emit_string label_prefix; emit_int lbl
|
|
|
|
|
2011-12-18 02:00:56 -08:00
|
|
|
let emit_data_label lbl =
|
|
|
|
emit_string label_prefix; emit_string "d"; emit_int lbl
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Section switching *)
|
|
|
|
|
|
|
|
let data_space =
|
1998-03-13 05:57:35 -08:00
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" -> " .section \".data\"\n"
|
2004-06-19 09:17:31 -07:00
|
|
|
| "rhapsody" -> " .data\n"
|
1998-03-13 05:57:35 -08:00
|
|
|
| _ -> assert false
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2007-11-09 07:06:57 -08:00
|
|
|
let code_space =
|
1998-03-13 05:57:35 -08:00
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" -> " .section \".text\"\n"
|
2007-11-09 07:06:57 -08:00
|
|
|
| "rhapsody" -> " .text\n"
|
1998-03-13 05:57:35 -08:00
|
|
|
| _ -> assert false
|
|
|
|
|
2007-11-09 07:06:57 -08:00
|
|
|
let rodata_space =
|
1998-03-13 05:57:35 -08:00
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" -> " .section \".rodata\"\n"
|
2004-06-19 09:17:31 -07:00
|
|
|
| "rhapsody" -> " .const\n"
|
1998-03-13 05:57:35 -08:00
|
|
|
| _ -> assert false
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2006-05-31 01:16:34 -07:00
|
|
|
(* Names of instructions that differ in 32 and 64-bit modes *)
|
|
|
|
|
|
|
|
let lg = if ppc64 then "ld" else "lwz"
|
|
|
|
let stg = if ppc64 then "std" else "stw"
|
|
|
|
let lwa = if ppc64 then "lwa" else "lwz"
|
|
|
|
let cmpg = if ppc64 then "cmpd" else "cmpw"
|
|
|
|
let cmplg = if ppc64 then "cmpld" else "cmplw"
|
|
|
|
let datag = if ppc64 then ".quad" else ".long"
|
|
|
|
let aligng = if ppc64 then 3 else 2
|
|
|
|
let mullg = if ppc64 then "mulld" else "mullw"
|
|
|
|
let divg = if ppc64 then "divd" else "divw"
|
|
|
|
let tglle = if ppc64 then "tdlle" else "twlle"
|
|
|
|
let sragi = if ppc64 then "sradi" else "srawi"
|
|
|
|
let slgi = if ppc64 then "sldi" else "slwi"
|
|
|
|
let fctigz = if ppc64 then "fctidz" else "fctiwz"
|
|
|
|
|
1997-07-24 06:36:24 -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"
|
|
|
|
|
2010-01-22 04:48:24 -08:00
|
|
|
let use_full_regnames =
|
1998-03-13 07:06:16 -08:00
|
|
|
Config.system = "rhapsody"
|
|
|
|
|
|
|
|
let emit_gpr r =
|
|
|
|
if use_full_regnames then emit_char 'r';
|
|
|
|
emit_int r
|
|
|
|
|
|
|
|
let emit_fpr r =
|
|
|
|
if use_full_regnames then emit_char 'f';
|
|
|
|
emit_int r
|
|
|
|
|
|
|
|
let emit_ccr r =
|
|
|
|
if use_full_regnames then emit_string "cr";
|
|
|
|
emit_int r
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output a stack reference *)
|
|
|
|
|
|
|
|
let emit_stack r =
|
|
|
|
match r.loc with
|
|
|
|
Stack s ->
|
1998-03-13 07:06:16 -08:00
|
|
|
let ofs = slot_offset s (register_class r) in `{emit_int ofs}({emit_gpr 1})`
|
1997-07-24 06:36:24 -07:00
|
|
|
| _ -> fatal_error "Emit.emit_stack"
|
|
|
|
|
|
|
|
(* Split a 32-bit integer constants in two 16-bit halves *)
|
|
|
|
|
|
|
|
let low n = n land 0xFFFF
|
|
|
|
let high n = n asr 16
|
|
|
|
|
|
|
|
let nativelow n = Nativeint.to_int n land 0xFFFF
|
2000-02-11 07:09:27 -08:00
|
|
|
let nativehigh n = Nativeint.to_int (Nativeint.shift_right n 16)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let is_immediate n =
|
|
|
|
n <= 32767 && n >= -32768
|
|
|
|
|
|
|
|
let is_native_immediate n =
|
2006-05-31 01:16:34 -07:00
|
|
|
n <= 32767n && n >= -32768n
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2004-06-19 09:17:31 -07:00
|
|
|
(* Output a "upper 16 bits" or "lower 16 bits" operator. *)
|
1998-03-13 05:57:35 -08:00
|
|
|
|
|
|
|
let emit_upper emit_fun arg =
|
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" ->
|
1998-03-13 05:57:35 -08:00
|
|
|
emit_fun arg; emit_string "@ha"
|
|
|
|
| "rhapsody" ->
|
|
|
|
emit_string "ha16("; emit_fun arg; emit_string ")"
|
|
|
|
| _ -> assert false
|
|
|
|
|
|
|
|
let emit_lower emit_fun arg =
|
|
|
|
match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" ->
|
1998-03-13 05:57:35 -08:00
|
|
|
emit_fun arg; emit_string "@l"
|
|
|
|
| "rhapsody" ->
|
|
|
|
emit_string "lo16("; emit_fun arg; emit_string ")"
|
|
|
|
| _ -> assert false
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output a load or store operation *)
|
|
|
|
|
1998-03-13 05:57:35 -08:00
|
|
|
let emit_symbol_offset (s, d) =
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_symbol s;
|
|
|
|
if d > 0 then `+`;
|
|
|
|
if d <> 0 then emit_int d
|
|
|
|
|
2006-05-31 01:16:34 -07:00
|
|
|
let valid_offset instr ofs =
|
|
|
|
ofs land 3 = 0 || (instr <> "ld" && instr <> "std")
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
let emit_load_store instr addressing_mode addr n arg =
|
|
|
|
match addressing_mode with
|
|
|
|
Ibased(s, d) ->
|
1998-03-13 09:47:24 -08:00
|
|
|
` addis {emit_gpr 11}, 0, {emit_upper emit_symbol_offset (s,d)}\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` {emit_string instr} {emit_reg arg}, {emit_lower emit_symbol_offset (s,d)}({emit_gpr 11})\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Iindexed ofs ->
|
2006-05-31 01:16:34 -07:00
|
|
|
if is_immediate ofs && valid_offset instr ofs then
|
1997-07-24 06:36:24 -07:00
|
|
|
` {emit_string instr} {emit_reg arg}, {emit_int ofs}({emit_reg addr.(n)})\n`
|
|
|
|
else begin
|
1998-03-13 07:06:16 -08:00
|
|
|
` lis {emit_gpr 0}, {emit_int(high ofs)}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
if low ofs <> 0 then
|
1998-03-13 07:06:16 -08:00
|
|
|
` ori {emit_gpr 0}, {emit_gpr 0}, {emit_int(low ofs)}\n`;
|
|
|
|
` {emit_string instr}x {emit_reg arg}, {emit_reg addr.(n)}, {emit_gpr 0}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
| Iindexed2 ->
|
|
|
|
` {emit_string instr}x {emit_reg arg}, {emit_reg addr.(n)}, {emit_reg addr.(n+1)}\n`
|
|
|
|
|
|
|
|
(* After a comparison, extract the result as 0 or 1 *)
|
|
|
|
|
|
|
|
let emit_set_comp cmp res =
|
1998-03-13 07:06:16 -08:00
|
|
|
` mfcr {emit_gpr 0}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
let bitnum =
|
|
|
|
match cmp with
|
|
|
|
Ceq | Cne -> 2
|
|
|
|
| Cgt | Cle -> 1
|
|
|
|
| Clt | Cge -> 0 in
|
1998-03-13 09:47:24 -08:00
|
|
|
` rlwinm {emit_reg res}, {emit_gpr 0}, {emit_int(bitnum+1)}, 31, 31\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
begin match cmp with
|
|
|
|
Cne | Cle | Cge -> ` xori {emit_reg res}, {emit_reg res}, 1\n`
|
|
|
|
| _ -> ()
|
|
|
|
end
|
|
|
|
|
|
|
|
(* Record live pointers at call points *)
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
let record_frame live dbg =
|
1997-07-24 06:36:24 -07:00
|
|
|
let lbl = new_label() in
|
|
|
|
let live_offset = ref [] in
|
|
|
|
Reg.Set.iter
|
|
|
|
(function
|
|
|
|
{typ = Addr; loc = Reg r} ->
|
|
|
|
live_offset := (r lsl 1) + 1 :: !live_offset
|
|
|
|
| {typ = Addr; loc = Stack s} as reg ->
|
|
|
|
live_offset := slot_offset s (register_class reg) :: !live_offset
|
|
|
|
| _ -> ())
|
|
|
|
live;
|
|
|
|
frame_descriptors :=
|
|
|
|
{ fd_lbl = lbl;
|
|
|
|
fd_frame_size = frame_size();
|
2007-01-29 04:11:18 -08:00
|
|
|
fd_live_offset = !live_offset;
|
|
|
|
fd_debuginfo = dbg } :: !frame_descriptors;
|
|
|
|
`{emit_label lbl}:\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2006-05-31 01:16:34 -07:00
|
|
|
(* Record floating-point and large integer literals *)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
2014-04-25 01:41:13 -07:00
|
|
|
let float_literals = ref ([] : (int64 * int) list)
|
2006-05-31 01:16:34 -07:00
|
|
|
let int_literals = ref ([] : (nativeint * int) list)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
1998-03-13 11:31:32 -08:00
|
|
|
(* Record external C functions to be called in a position-independent way
|
2004-06-19 09:17:31 -07:00
|
|
|
(for MacOSX) *)
|
1998-03-13 11:31:32 -08:00
|
|
|
|
|
|
|
let pic_externals = (Config.system = "rhapsody")
|
|
|
|
|
|
|
|
let external_functions = ref StringSet.empty
|
|
|
|
|
|
|
|
let emit_external s =
|
|
|
|
` .non_lazy_symbol_pointer\n`;
|
|
|
|
`L{emit_symbol s}$non_lazy_ptr:\n`;
|
|
|
|
` .indirect_symbol {emit_symbol s}\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string datag} 0\n`
|
1998-03-13 11:31:32 -08:00
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Names for conditional branches after comparisons *)
|
|
|
|
|
|
|
|
let branch_for_comparison = function
|
|
|
|
Ceq -> "beq" | Cne -> "bne"
|
|
|
|
| Cle -> "ble" | Cgt -> "bgt"
|
|
|
|
| Cge -> "bge" | Clt -> "blt"
|
|
|
|
|
|
|
|
let name_for_int_comparison = function
|
2006-05-31 01:16:34 -07:00
|
|
|
Isigned cmp -> (cmpg, branch_for_comparison cmp)
|
|
|
|
| Iunsigned cmp -> (cmplg, branch_for_comparison cmp)
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
(* Names for various instructions *)
|
|
|
|
|
|
|
|
let name_for_intop = function
|
2013-11-18 23:01:54 -08:00
|
|
|
Iadd -> "add"
|
2013-11-28 06:43:56 -08:00
|
|
|
| Imul -> if ppc64 then "mulld" else "mullw"
|
2013-11-18 23:01:54 -08:00
|
|
|
| Imulh -> if ppc64 then "mulhd" else "mulhw"
|
|
|
|
| Idiv -> if ppc64 then "divd" else "divw"
|
|
|
|
| Iand -> "and"
|
|
|
|
| Ior -> "or"
|
|
|
|
| Ixor -> "xor"
|
|
|
|
| Ilsl -> if ppc64 then "sld" else "slw"
|
|
|
|
| Ilsr -> if ppc64 then "srd" else "srw"
|
|
|
|
| Iasr -> if ppc64 then "srad" else "sraw"
|
1997-07-24 06:36:24 -07:00
|
|
|
| _ -> Misc.fatal_error "Emit.Intop"
|
|
|
|
|
|
|
|
let name_for_intop_imm = function
|
|
|
|
Iadd -> "addi"
|
|
|
|
| Imul -> "mulli"
|
|
|
|
| Iand -> "andi."
|
|
|
|
| Ior -> "ori"
|
|
|
|
| Ixor -> "xori"
|
2006-05-31 01:16:34 -07:00
|
|
|
| Ilsl -> if ppc64 then "sldi" else "slwi"
|
|
|
|
| Ilsr -> if ppc64 then "srdi" else "srwi"
|
|
|
|
| Iasr -> if ppc64 then "sradi" else "srawi"
|
1997-07-24 06:36:24 -07:00
|
|
|
| _ -> Misc.fatal_error "Emit.Intop_imm"
|
|
|
|
|
|
|
|
let name_for_floatop1 = function
|
|
|
|
Inegf -> "fneg"
|
|
|
|
| Iabsf -> "fabs"
|
|
|
|
| _ -> Misc.fatal_error "Emit.Iopf1"
|
|
|
|
|
|
|
|
let name_for_floatop2 = function
|
|
|
|
Iaddf -> "fadd"
|
|
|
|
| Isubf -> "fsub"
|
|
|
|
| Imulf -> "fmul"
|
|
|
|
| Idivf -> "fdiv"
|
|
|
|
| _ -> Misc.fatal_error "Emit.Iopf2"
|
|
|
|
|
|
|
|
let name_for_specific = function
|
|
|
|
Imultaddf -> "fmadd"
|
|
|
|
| Imultsubf -> "fmsub"
|
2002-01-09 11:40:48 -08:00
|
|
|
| _ -> Misc.fatal_error "Emit.Ispecific"
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
(* Name of current function *)
|
|
|
|
let function_name = ref ""
|
|
|
|
(* Entry point for tail recursive calls *)
|
|
|
|
let tailrec_entry_point = ref 0
|
|
|
|
(* Names of functions defined in the current file *)
|
|
|
|
let defined_functions = ref StringSet.empty
|
|
|
|
(* Label of glue code for calling the GC *)
|
|
|
|
let call_gc_label = ref 0
|
|
|
|
|
2002-01-09 11:40:48 -08:00
|
|
|
(* Fixup conditional branches that exceed hardware allowed range *)
|
|
|
|
|
|
|
|
let load_store_size = function
|
|
|
|
Ibased(s, d) -> 2
|
|
|
|
| Iindexed ofs -> if is_immediate ofs then 1 else 3
|
|
|
|
| Iindexed2 -> 1
|
|
|
|
|
|
|
|
let instr_size = function
|
|
|
|
Lend -> 0
|
|
|
|
| Lop(Imove | Ispill | Ireload) -> 1
|
2014-03-17 07:34:00 -07:00
|
|
|
| Lop(Iconst_int n | Iconst_blockheader n) ->
|
|
|
|
if is_native_immediate n then 1 else 2
|
2004-06-19 09:17:31 -07:00
|
|
|
| Lop(Iconst_float s) -> 2
|
|
|
|
| Lop(Iconst_symbol s) -> 2
|
|
|
|
| Lop(Icall_ind) -> 2
|
|
|
|
| Lop(Icall_imm s) -> 1
|
|
|
|
| Lop(Itailcall_ind) -> 5
|
|
|
|
| Lop(Itailcall_imm s) -> if s = !function_name then 1 else 4
|
|
|
|
| Lop(Iextcall(s, true)) -> 3
|
2002-01-09 11:40:48 -08:00
|
|
|
| Lop(Iextcall(s, false)) -> if pic_externals then 4 else 1
|
|
|
|
| Lop(Istackoffset n) -> 1
|
|
|
|
| Lop(Iload(chunk, addr)) ->
|
|
|
|
if chunk = Byte_signed
|
|
|
|
then load_store_size addr + 1
|
|
|
|
else load_store_size addr
|
2014-04-26 03:40:22 -07:00
|
|
|
| Lop(Istore(chunk, addr, _)) -> load_store_size addr
|
2002-01-09 11:40:48 -08:00
|
|
|
| Lop(Ialloc n) -> 4
|
|
|
|
| Lop(Ispecific(Ialloc_far n)) -> 5
|
2004-06-19 09:17:31 -07:00
|
|
|
| Lop(Iintop Imod) -> 3
|
2002-01-09 11:40:48 -08:00
|
|
|
| Lop(Iintop(Icomp cmp)) -> 4
|
|
|
|
| Lop(Iintop op) -> 1
|
|
|
|
| Lop(Iintop_imm(Icomp cmp, n)) -> 4
|
|
|
|
| Lop(Iintop_imm(op, n)) -> 1
|
|
|
|
| Lop(Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf) -> 1
|
|
|
|
| Lop(Ifloatofint) -> 9
|
|
|
|
| Lop(Iintoffloat) -> 4
|
|
|
|
| Lop(Ispecific sop) -> 1
|
|
|
|
| Lreloadretaddr -> 2
|
|
|
|
| Lreturn -> 2
|
|
|
|
| Llabel lbl -> 0
|
|
|
|
| Lbranch lbl -> 1
|
|
|
|
| Lcondbranch(tst, lbl) -> 2
|
|
|
|
| Lcondbranch3(lbl0, lbl1, lbl2) ->
|
|
|
|
1 + (if lbl0 = None then 0 else 1)
|
|
|
|
+ (if lbl1 = None then 0 else 1)
|
|
|
|
+ (if lbl2 = None then 0 else 1)
|
|
|
|
| Lswitch jumptbl -> 8
|
|
|
|
| Lsetuptrap lbl -> 1
|
2004-06-19 09:17:31 -07:00
|
|
|
| Lpushtrap -> 4
|
2002-01-09 11:40:48 -08:00
|
|
|
| Lpoptrap -> 2
|
2013-10-15 05:06:41 -07:00
|
|
|
| Lraise _ -> 6
|
2002-01-09 11:40:48 -08:00
|
|
|
|
|
|
|
let label_map code =
|
|
|
|
let map = Hashtbl.create 37 in
|
|
|
|
let rec fill_map pc instr =
|
|
|
|
match instr.desc with
|
|
|
|
Lend -> (pc, map)
|
|
|
|
| Llabel lbl -> Hashtbl.add map lbl pc; fill_map pc instr.next
|
|
|
|
| op -> fill_map (pc + instr_size op) instr.next
|
|
|
|
in fill_map 0 code
|
|
|
|
|
|
|
|
let max_branch_offset = 8180
|
|
|
|
(* 14-bit signed offset in words. Remember to cut some slack
|
|
|
|
for multi-word instructions where the branch can be anywhere in
|
|
|
|
the middle. 12 words of slack is plenty. *)
|
|
|
|
|
|
|
|
let branch_overflows map pc_branch lbl_dest =
|
|
|
|
let pc_dest = Hashtbl.find map lbl_dest in
|
|
|
|
let delta = pc_dest - (pc_branch + 1) in
|
|
|
|
delta <= -max_branch_offset || delta >= max_branch_offset
|
|
|
|
|
|
|
|
let opt_branch_overflows map pc_branch opt_lbl_dest =
|
|
|
|
match opt_lbl_dest with
|
|
|
|
None -> false
|
|
|
|
| Some lbl_dest -> branch_overflows map pc_branch lbl_dest
|
|
|
|
|
|
|
|
let fixup_branches codesize map code =
|
|
|
|
let expand_optbranch lbl n arg next =
|
|
|
|
match lbl with
|
|
|
|
None -> next
|
|
|
|
| Some l ->
|
|
|
|
instr_cons (Lcondbranch(Iinttest_imm(Isigned Ceq, n), l))
|
|
|
|
arg [||] next in
|
|
|
|
let rec fixup did_fix pc instr =
|
|
|
|
match instr.desc with
|
|
|
|
Lend -> did_fix
|
|
|
|
| Lcondbranch(test, lbl) when branch_overflows map pc lbl ->
|
|
|
|
let lbl2 = new_label() in
|
|
|
|
let cont =
|
|
|
|
instr_cons (Lbranch lbl) [||] [||]
|
|
|
|
(instr_cons (Llabel lbl2) [||] [||] instr.next) in
|
|
|
|
instr.desc <- Lcondbranch(invert_test test, lbl2);
|
|
|
|
instr.next <- cont;
|
|
|
|
fixup true (pc + 2) instr.next
|
|
|
|
| Lcondbranch3(lbl0, lbl1, lbl2)
|
|
|
|
when opt_branch_overflows map pc lbl0
|
|
|
|
|| opt_branch_overflows map pc lbl1
|
|
|
|
|| opt_branch_overflows map pc lbl2 ->
|
|
|
|
let cont =
|
|
|
|
expand_optbranch lbl0 0 instr.arg
|
|
|
|
(expand_optbranch lbl1 1 instr.arg
|
|
|
|
(expand_optbranch lbl2 2 instr.arg instr.next)) in
|
|
|
|
instr.desc <- cont.desc;
|
|
|
|
instr.next <- cont.next;
|
|
|
|
fixup true pc instr
|
|
|
|
| Lop(Ialloc n) when codesize - pc >= max_branch_offset ->
|
|
|
|
instr.desc <- Lop(Ispecific(Ialloc_far n));
|
|
|
|
fixup true (pc + 4) instr.next
|
|
|
|
| op ->
|
|
|
|
fixup did_fix (pc + instr_size op) instr.next
|
|
|
|
in fixup false 0 code
|
|
|
|
|
|
|
|
(* Iterate branch expansion till all conditional branches are OK *)
|
|
|
|
|
|
|
|
let rec branch_normalization code =
|
|
|
|
let (codesize, map) = label_map code in
|
|
|
|
if codesize >= max_branch_offset && fixup_branches codesize map code
|
|
|
|
then branch_normalization code
|
|
|
|
else ()
|
|
|
|
|
|
|
|
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Output the assembly code for an instruction *)
|
|
|
|
|
|
|
|
let rec emit_instr i dslot =
|
|
|
|
match i.desc with
|
|
|
|
Lend -> ()
|
|
|
|
| Lop(Imove | Ispill | Ireload) ->
|
|
|
|
let src = i.arg.(0) and dst = i.res.(0) in
|
|
|
|
if src.loc <> dst.loc then begin
|
|
|
|
match (src, dst) with
|
|
|
|
{loc = Reg rs; typ = (Int | Addr)}, {loc = Reg rd} ->
|
|
|
|
` mr {emit_reg dst}, {emit_reg src}\n`
|
|
|
|
| {loc = Reg rs; typ = Float}, {loc = Reg rd; typ = Float} ->
|
|
|
|
` fmr {emit_reg dst}, {emit_reg src}\n`
|
|
|
|
| {loc = Reg rs; typ = (Int | Addr)}, {loc = Stack sd} ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string stg} {emit_reg src}, {emit_stack dst}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| {loc = Reg rs; typ = Float}, {loc = Stack sd} ->
|
|
|
|
` stfd {emit_reg src}, {emit_stack dst}\n`
|
|
|
|
| {loc = Stack ss; typ = (Int | Addr)}, {loc = Reg rd} ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_reg dst}, {emit_stack src}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| {loc = Stack ss; typ = Float}, {loc = Reg rd} ->
|
|
|
|
` lfd {emit_reg dst}, {emit_stack src}\n`
|
|
|
|
| (_, _) ->
|
|
|
|
fatal_error "Emit: Imove"
|
|
|
|
end
|
2014-03-17 07:34:00 -07:00
|
|
|
| Lop(Iconst_int n | Iconst_blockheader n) ->
|
1997-07-24 06:36:24 -07:00
|
|
|
if is_native_immediate n then
|
|
|
|
` li {emit_reg i.res.(0)}, {emit_nativeint n}\n`
|
2006-05-31 01:16:34 -07:00
|
|
|
else if n >= -0x8000_0000n && n <= 0x7FFF_FFFFn then begin
|
1997-07-24 06:36:24 -07:00
|
|
|
` lis {emit_reg i.res.(0)}, {emit_int(nativehigh n)}\n`;
|
|
|
|
if nativelow n <> 0 then
|
|
|
|
` ori {emit_reg i.res.(0)}, {emit_reg i.res.(0)}, {emit_int(nativelow n)}\n`
|
2006-05-31 01:16:34 -07:00
|
|
|
end else begin
|
|
|
|
let lbl = new_label() in
|
|
|
|
int_literals := (n, lbl) :: !int_literals;
|
|
|
|
` addis {emit_gpr 11}, 0, {emit_upper emit_label lbl}\n`;
|
|
|
|
` {emit_string lg} {emit_reg i.res.(0)}, {emit_lower emit_label lbl}({emit_gpr 11})\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
2014-04-25 01:41:13 -07:00
|
|
|
| Lop(Iconst_float f) ->
|
2004-06-19 09:17:31 -07:00
|
|
|
let lbl = new_label() in
|
2014-04-25 01:41:13 -07:00
|
|
|
float_literals := (Int64.bits_of_float f, lbl) :: !float_literals;
|
2004-06-19 09:17:31 -07:00
|
|
|
` addis {emit_gpr 11}, 0, {emit_upper emit_label lbl}\n`;
|
|
|
|
` lfd {emit_reg i.res.(0)}, {emit_lower emit_label lbl}({emit_gpr 11})\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iconst_symbol s) ->
|
2007-11-09 07:06:57 -08:00
|
|
|
` addis {emit_reg i.res.(0)}, 0, {emit_upper emit_symbol s}\n`;
|
|
|
|
` addi {emit_reg i.res.(0)}, {emit_reg i.res.(0)}, {emit_lower emit_symbol s}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Icall_ind) ->
|
2004-06-19 09:17:31 -07:00
|
|
|
` mtctr {emit_reg i.arg.(0)}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
` bctrl\n`;
|
|
|
|
record_frame i.live i.dbg
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Icall_imm s) ->
|
2007-11-09 07:06:57 -08:00
|
|
|
` bl {emit_symbol s}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame i.live i.dbg
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Itailcall_ind) ->
|
|
|
|
let n = frame_size() in
|
2004-06-19 09:17:31 -07:00
|
|
|
` mtctr {emit_reg i.arg.(0)}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
if !contains_calls then begin
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_gpr 11}, {emit_int(n - size_addr)}({emit_gpr 1})\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int n}\n`;
|
|
|
|
` mtlr {emit_gpr 11}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end else begin
|
1998-06-22 05:43:04 -07:00
|
|
|
if n > 0 then
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int n}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end;
|
|
|
|
` bctr\n`
|
|
|
|
| Lop(Itailcall_imm s) ->
|
|
|
|
if s = !function_name then
|
|
|
|
` b {emit_label !tailrec_entry_point}\n`
|
2004-06-19 09:17:31 -07:00
|
|
|
else begin
|
1997-07-24 06:36:24 -07:00
|
|
|
let n = frame_size() in
|
|
|
|
if !contains_calls then begin
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_gpr 11}, {emit_int(n - size_addr)}({emit_gpr 1})\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int n}\n`;
|
|
|
|
` mtlr {emit_gpr 11}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end else begin
|
1998-06-22 05:43:04 -07:00
|
|
|
if n > 0 then
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int n}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end;
|
2004-06-19 09:17:31 -07:00
|
|
|
` b {emit_symbol s}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
| Lop(Iextcall(s, alloc)) ->
|
|
|
|
if alloc then begin
|
2004-06-19 09:17:31 -07:00
|
|
|
if pic_externals then begin
|
1998-03-13 11:31:32 -08:00
|
|
|
external_functions := StringSet.add s !external_functions;
|
|
|
|
` addis {emit_gpr 11}, 0, ha16(L{emit_symbol s}$non_lazy_ptr)\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_gpr 11}, lo16(L{emit_symbol s}$non_lazy_ptr)({emit_gpr 11})\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end else begin
|
1998-03-13 09:47:24 -08:00
|
|
|
` addis {emit_gpr 11}, 0, {emit_upper emit_symbol s}\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` addi {emit_gpr 11}, {emit_gpr 11}, {emit_lower emit_symbol s}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end;
|
2007-11-09 07:06:57 -08:00
|
|
|
` bl {emit_symbol "caml_c_call"}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame i.live i.dbg
|
1997-07-24 06:36:24 -07:00
|
|
|
end else begin
|
1998-03-13 11:31:32 -08:00
|
|
|
if pic_externals then begin
|
|
|
|
external_functions := StringSet.add s !external_functions;
|
|
|
|
` addis {emit_gpr 11}, 0, ha16(L{emit_symbol s}$non_lazy_ptr)\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_gpr 11}, lo16(L{emit_symbol s}$non_lazy_ptr)({emit_gpr 11})\n`;
|
2004-02-22 06:56:25 -08:00
|
|
|
` mtctr {emit_gpr 11}\n`;
|
|
|
|
` bctrl\n`
|
1998-03-13 11:31:32 -08:00
|
|
|
end else
|
2004-06-19 09:17:31 -07:00
|
|
|
` bl {emit_symbol s}\n`
|
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Istackoffset n) ->
|
1998-03-13 07:06:16 -08:00
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int (-n)}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
stack_offset := !stack_offset + n
|
|
|
|
| Lop(Iload(chunk, addr)) ->
|
|
|
|
let loadinstr =
|
|
|
|
match chunk with
|
2000-02-07 02:23:22 -08:00
|
|
|
Byte_unsigned -> "lbz"
|
1997-07-24 06:36:24 -07:00
|
|
|
| Byte_signed -> "lbz"
|
|
|
|
| Sixteen_unsigned -> "lhz"
|
2000-02-07 02:23:22 -08:00
|
|
|
| Sixteen_signed -> "lha"
|
2006-05-31 01:16:34 -07:00
|
|
|
| Thirtytwo_unsigned -> "lwz"
|
|
|
|
| Thirtytwo_signed -> if ppc64 then "lwa" else "lwz"
|
|
|
|
| Word -> lg
|
2000-02-07 02:23:22 -08:00
|
|
|
| Single -> "lfs"
|
2006-05-31 01:16:34 -07:00
|
|
|
| Double | Double_u -> "lfd" in
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_load_store loadinstr addr i.arg 0 i.res.(0);
|
|
|
|
if chunk = Byte_signed then
|
|
|
|
` extsb {emit_reg i.res.(0)}, {emit_reg i.res.(0)}\n`
|
2014-04-26 03:40:22 -07:00
|
|
|
| Lop(Istore(chunk, addr, _)) ->
|
1997-07-24 06:36:24 -07:00
|
|
|
let storeinstr =
|
|
|
|
match chunk with
|
2000-02-07 02:23:22 -08:00
|
|
|
Byte_unsigned | Byte_signed -> "stb"
|
|
|
|
| Sixteen_unsigned | Sixteen_signed -> "sth"
|
2006-05-31 01:16:34 -07:00
|
|
|
| Thirtytwo_unsigned | Thirtytwo_signed -> "stw"
|
|
|
|
| Word -> stg
|
2000-02-07 02:23:22 -08:00
|
|
|
| Single -> "stfs"
|
2006-05-31 01:16:34 -07:00
|
|
|
| Double | Double_u -> "stfd" in
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_load_store storeinstr addr i.arg 1 i.arg.(0)
|
|
|
|
| Lop(Ialloc n) ->
|
|
|
|
if !call_gc_label = 0 then call_gc_label := new_label();
|
1998-03-13 07:06:16 -08:00
|
|
|
` addi {emit_gpr 31}, {emit_gpr 31}, {emit_int(-n)}\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmplg} {emit_gpr 31}, {emit_gpr 30}\n`;
|
|
|
|
` addi {emit_reg i.res.(0)}, {emit_gpr 31}, {emit_int size_addr}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
` bltl {emit_label !call_gc_label}\n`;
|
|
|
|
record_frame i.live Debuginfo.none
|
2002-01-09 11:40:48 -08:00
|
|
|
| Lop(Ispecific(Ialloc_far n)) ->
|
|
|
|
if !call_gc_label = 0 then call_gc_label := new_label();
|
|
|
|
let lbl = new_label() in
|
|
|
|
` addi {emit_gpr 31}, {emit_gpr 31}, {emit_int(-n)}\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmplg} {emit_gpr 31}, {emit_gpr 30}\n`;
|
2002-01-09 11:40:48 -08:00
|
|
|
` bge {emit_label lbl}\n`;
|
|
|
|
` bl {emit_label !call_gc_label}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame i.live Debuginfo.none;
|
2006-05-31 01:16:34 -07:00
|
|
|
`{emit_label lbl}: addi {emit_reg i.res.(0)}, {emit_gpr 31}, {emit_int size_addr}\n`
|
2004-06-19 09:17:31 -07:00
|
|
|
| Lop(Iintop Isub) -> (* subfc has swapped arguments *)
|
1997-07-24 06:36:24 -07:00
|
|
|
` subfc {emit_reg i.res.(0)}, {emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`
|
|
|
|
| Lop(Iintop Imod) ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string divg} {emit_gpr 0}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
|
|
|
` {emit_string mullg} {emit_gpr 0}, {emit_gpr 0}, {emit_reg i.arg.(1)}\n`;
|
2004-06-19 09:17:31 -07:00
|
|
|
` subfc {emit_reg i.res.(0)}, {emit_gpr 0}, {emit_reg i.arg.(0)}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintop(Icomp cmp)) ->
|
|
|
|
begin match cmp with
|
|
|
|
Isigned c ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmpg} {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_set_comp c i.res.(0)
|
|
|
|
| Iunsigned c ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmplg} {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_set_comp c i.res.(0)
|
|
|
|
end
|
|
|
|
| Lop(Iintop Icheckbound) ->
|
2013-11-28 06:43:56 -08:00
|
|
|
if !Clflags.debug then
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame Reg.Set.empty i.dbg;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string tglle} {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintop op) ->
|
|
|
|
let instr = name_for_intop op in
|
|
|
|
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`
|
|
|
|
| Lop(Iintop_imm(Isub, n)) ->
|
|
|
|
` addi {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_int(-n)}\n`
|
|
|
|
| Lop(Iintop_imm(Icomp cmp, n)) ->
|
|
|
|
begin match cmp with
|
|
|
|
Isigned c ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmpg}i {emit_reg i.arg.(0)}, {emit_int n}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_set_comp c i.res.(0)
|
|
|
|
| Iunsigned c ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmplg}i {emit_reg i.arg.(0)}, {emit_int n}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_set_comp c i.res.(0)
|
|
|
|
end
|
|
|
|
| Lop(Iintop_imm(Icheckbound, n)) ->
|
2013-11-28 06:43:56 -08:00
|
|
|
if !Clflags.debug then
|
2007-01-29 04:11:18 -08:00
|
|
|
record_frame Reg.Set.empty i.dbg;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string tglle}i {emit_reg i.arg.(0)}, {emit_int n}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintop_imm(op, n)) ->
|
|
|
|
let instr = name_for_intop_imm op in
|
|
|
|
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_int n}\n`
|
|
|
|
| Lop(Inegf | Iabsf as op) ->
|
|
|
|
let instr = name_for_floatop1 op in
|
|
|
|
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}\n`
|
|
|
|
| Lop(Iaddf | Isubf | Imulf | Idivf as op) ->
|
|
|
|
let instr = name_for_floatop2 op in
|
|
|
|
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`
|
|
|
|
| Lop(Ifloatofint) ->
|
2006-05-31 01:16:34 -07:00
|
|
|
if ppc64 then begin
|
|
|
|
` stdu {emit_reg i.arg.(0)}, -16({emit_gpr 1})\n`;
|
|
|
|
` lfd {emit_reg i.res.(0)}, 0({emit_gpr 1})\n`;
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, 16\n`;
|
|
|
|
` fcfid {emit_reg i.res.(0)}, {emit_reg i.res.(0)}\n`
|
|
|
|
end else begin
|
|
|
|
let lbl = new_label() in
|
2014-04-25 01:41:13 -07:00
|
|
|
float_literals := (0x4330000080000000L, lbl) :: !float_literals;
|
2006-05-31 01:16:34 -07:00
|
|
|
` addis {emit_gpr 11}, 0, {emit_upper emit_label lbl}\n`;
|
|
|
|
` lfd {emit_fpr 0}, {emit_lower emit_label lbl}({emit_gpr 11})\n`;
|
|
|
|
` lis {emit_gpr 0}, 0x4330\n`;
|
|
|
|
` stwu {emit_gpr 0}, -16({emit_gpr 1})\n`;
|
|
|
|
` xoris {emit_gpr 0}, {emit_reg i.arg.(0)}, 0x8000\n`;
|
|
|
|
` stw {emit_gpr 0}, 4({emit_gpr 1})\n`;
|
|
|
|
` lfd {emit_reg i.res.(0)}, 0({emit_gpr 1})\n`;
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, 16\n`;
|
|
|
|
` fsub {emit_reg i.res.(0)}, {emit_reg i.res.(0)}, {emit_fpr 0}\n`
|
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Iintoffloat) ->
|
2006-05-31 01:16:34 -07:00
|
|
|
let ofs = if ppc64 then 0 else 4 in
|
|
|
|
` {emit_string fctigz} {emit_fpr 0}, {emit_reg i.arg.(0)}\n`;
|
|
|
|
` stfdu {emit_fpr 0}, -16({emit_gpr 1})\n`;
|
|
|
|
` {emit_string lg} {emit_reg i.res.(0)}, {emit_int ofs}({emit_gpr 1})\n`;
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, 16\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lop(Ispecific sop) ->
|
|
|
|
let instr = name_for_specific sop in
|
|
|
|
` {emit_string instr} {emit_reg i.res.(0)}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}, {emit_reg i.arg.(2)}\n`
|
|
|
|
| Lreloadretaddr ->
|
|
|
|
let n = frame_size() in
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_gpr 11}, {emit_int(n - size_addr)}({emit_gpr 1})\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` mtlr {emit_gpr 11}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lreturn ->
|
|
|
|
let n = frame_size() in
|
1998-06-22 05:43:04 -07:00
|
|
|
if n > 0 then
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int n}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
` blr\n`
|
|
|
|
| Llabel lbl ->
|
|
|
|
`{emit_label lbl}:\n`
|
|
|
|
| Lbranch lbl ->
|
|
|
|
` b {emit_label lbl}\n`
|
|
|
|
| Lcondbranch(tst, lbl) ->
|
|
|
|
begin match tst with
|
|
|
|
Itruetest ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmpg}i {emit_reg i.arg.(0)}, 0\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_delay dslot;
|
|
|
|
` bne {emit_label lbl}\n`
|
|
|
|
| Ifalsetest ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmpg}i {emit_reg i.arg.(0)}, 0\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_delay dslot;
|
|
|
|
` beq {emit_label lbl}\n`
|
|
|
|
| Iinttest cmp ->
|
|
|
|
let (comp, branch) = name_for_int_comparison cmp in
|
|
|
|
` {emit_string comp} {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
|
|
|
emit_delay dslot;
|
|
|
|
` {emit_string branch} {emit_label lbl}\n`
|
|
|
|
| Iinttest_imm(cmp, n) ->
|
|
|
|
let (comp, branch) = name_for_int_comparison cmp in
|
|
|
|
` {emit_string comp}i {emit_reg i.arg.(0)}, {emit_int n}\n`;
|
|
|
|
emit_delay dslot;
|
|
|
|
` {emit_string branch} {emit_label lbl}\n`
|
|
|
|
| Ifloattest(cmp, neg) ->
|
1998-03-13 07:06:16 -08:00
|
|
|
` fcmpu {emit_ccr 0}, {emit_reg i.arg.(0)}, {emit_reg i.arg.(1)}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
(* bit 0 = lt, bit 1 = gt, bit 2 = eq *)
|
|
|
|
let (bitnum, negtst) =
|
|
|
|
match cmp with
|
|
|
|
Ceq -> (2, neg)
|
|
|
|
| Cne -> (2, not neg)
|
1998-03-13 09:47:24 -08:00
|
|
|
| Cle -> ` cror 3, 0, 2\n`; (* lt or eq *)
|
1997-07-24 06:36:24 -07:00
|
|
|
(3, neg)
|
|
|
|
| Cgt -> (1, neg)
|
1998-03-13 09:47:24 -08:00
|
|
|
| Cge -> ` cror 3, 1, 2\n`; (* gt or eq *)
|
1997-07-24 06:36:24 -07:00
|
|
|
(3, neg)
|
|
|
|
| Clt -> (0, neg) in
|
|
|
|
emit_delay dslot;
|
|
|
|
if negtst
|
1998-03-13 09:47:24 -08:00
|
|
|
then ` bf {emit_int bitnum}, {emit_label lbl}\n`
|
|
|
|
else ` bt {emit_int bitnum}, {emit_label lbl}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Ioddtest ->
|
1998-03-13 07:06:16 -08:00
|
|
|
` andi. {emit_gpr 0}, {emit_reg i.arg.(0)}, 1\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_delay dslot;
|
|
|
|
` bne {emit_label lbl}\n`
|
|
|
|
| Ieventest ->
|
1998-03-13 07:06:16 -08:00
|
|
|
` andi. {emit_gpr 0}, {emit_reg i.arg.(0)}, 1\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_delay dslot;
|
|
|
|
` beq {emit_label lbl}\n`
|
|
|
|
end
|
|
|
|
| Lcondbranch3(lbl0, lbl1, lbl2) ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string cmpg}i {emit_reg i.arg.(0)}, 1\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_delay dslot;
|
|
|
|
begin match lbl0 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` blt {emit_label lbl}\n`
|
|
|
|
end;
|
|
|
|
begin match lbl1 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` beq {emit_label lbl}\n`
|
|
|
|
end;
|
|
|
|
begin match lbl2 with
|
|
|
|
None -> ()
|
|
|
|
| Some lbl -> ` bgt {emit_label lbl}\n`
|
|
|
|
end
|
|
|
|
| Lswitch jumptbl ->
|
2007-10-08 07:19:34 -07:00
|
|
|
let lbl = new_label() in
|
|
|
|
` addis {emit_gpr 11}, 0, {emit_upper emit_label lbl}\n`;
|
|
|
|
` addi {emit_gpr 11}, {emit_gpr 11}, {emit_lower emit_label lbl}\n`;
|
|
|
|
` {emit_string slgi} {emit_gpr 0}, {emit_reg i.arg.(0)}, 2\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lwa}x {emit_gpr 0}, {emit_gpr 11}, {emit_gpr 0}\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` add {emit_gpr 0}, {emit_gpr 11}, {emit_gpr 0}\n`;
|
|
|
|
` mtctr {emit_gpr 0}\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
` bctr\n`;
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string rodata_space;
|
2007-10-08 07:19:34 -07:00
|
|
|
`{emit_label lbl}:`;
|
1997-07-24 06:36:24 -07:00
|
|
|
for i = 0 to Array.length jumptbl - 1 do
|
2007-10-08 07:19:34 -07:00
|
|
|
` .long {emit_label jumptbl.(i)} - {emit_label lbl}\n`
|
|
|
|
done;
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string code_space
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lsetuptrap lbl ->
|
|
|
|
` bl {emit_label lbl}\n`
|
|
|
|
| Lpushtrap ->
|
2004-06-19 09:17:31 -07:00
|
|
|
stack_offset := !stack_offset + 16;
|
1998-03-13 07:06:16 -08:00
|
|
|
` mflr {emit_gpr 0}\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string stg}u {emit_gpr 0}, -16({emit_gpr 1})\n`;
|
|
|
|
` {emit_string stg} {emit_gpr 29}, {emit_int size_addr}({emit_gpr 1})\n`;
|
1998-03-13 07:06:16 -08:00
|
|
|
` mr {emit_gpr 29}, {emit_gpr 1}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Lpoptrap ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string lg} {emit_gpr 29}, {emit_int size_addr}({emit_gpr 1})\n`;
|
2004-06-19 09:17:31 -07:00
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, 16\n`;
|
|
|
|
stack_offset := !stack_offset - 16
|
2013-10-15 05:06:41 -07:00
|
|
|
| Lraise k ->
|
2013-11-28 06:43:56 -08:00
|
|
|
begin match !Clflags.debug, k with
|
|
|
|
| true, Lambda.Raise_regular ->
|
2007-01-29 04:11:18 -08:00
|
|
|
` bl {emit_symbol "caml_raise_exn"}\n`;
|
|
|
|
record_frame Reg.Set.empty i.dbg
|
2013-11-28 06:43:56 -08:00
|
|
|
| true, Lambda.Raise_reraise ->
|
|
|
|
` bl {emit_symbol "caml_reraise_exn"}\n`;
|
|
|
|
record_frame Reg.Set.empty i.dbg
|
2013-10-15 05:06:41 -07:00
|
|
|
| false, _
|
|
|
|
| true, Lambda.Raise_notrace ->
|
2007-01-29 04:11:18 -08:00
|
|
|
` {emit_string lg} {emit_gpr 0}, 0({emit_gpr 29})\n`;
|
|
|
|
` mr {emit_gpr 1}, {emit_gpr 29}\n`;
|
2013-11-28 06:43:56 -08:00
|
|
|
` mtctr {emit_gpr 0}\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
` {emit_string lg} {emit_gpr 29}, {emit_int size_addr}({emit_gpr 1})\n`;
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, 16\n`;
|
2013-11-28 06:43:56 -08:00
|
|
|
` bctr\n`
|
2007-01-29 04:11:18 -08:00
|
|
|
end
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
and emit_delay = function
|
|
|
|
None -> ()
|
|
|
|
| Some i -> emit_instr i None
|
|
|
|
|
|
|
|
(* Checks if a pseudo-instruction expands to instructions
|
|
|
|
that do not branch and do not affect CR0 nor R12. *)
|
|
|
|
|
|
|
|
let is_simple_instr i =
|
|
|
|
match i.desc with
|
|
|
|
Lop op ->
|
|
|
|
begin match op with
|
|
|
|
Icall_imm _ | Icall_ind | Itailcall_imm _ | Itailcall_ind |
|
|
|
|
Iextcall(_, _) -> false
|
|
|
|
| Ialloc(_) -> false
|
|
|
|
| Iintop(Icomp _) -> false
|
|
|
|
| Iintop_imm(Iand, _) -> false
|
|
|
|
| Iintop_imm(Icomp _, _) -> false
|
|
|
|
| _ -> true
|
|
|
|
end
|
|
|
|
| Lreloadretaddr -> true
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
(* Emit a sequence of instructions, trying to fill delay slots for branches *)
|
|
|
|
|
|
|
|
let rec emit_all i =
|
|
|
|
match i with
|
|
|
|
{desc = Lend} -> ()
|
|
|
|
| {next = {desc = (Lcondbranch(_, _) | Lcondbranch3(_, _, _))}}
|
2013-06-21 07:59:12 -07:00
|
|
|
when is_simple_instr i && no_interference i.res i.next.arg ->
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_instr i.next (Some i);
|
|
|
|
emit_all i.next.next
|
|
|
|
| _ ->
|
|
|
|
emit_instr i None;
|
|
|
|
emit_all i.next
|
|
|
|
|
|
|
|
(* Emission of a function declaration *)
|
|
|
|
|
|
|
|
let fundecl fundecl =
|
|
|
|
function_name := fundecl.fun_name;
|
|
|
|
defined_functions := StringSet.add fundecl.fun_name !defined_functions;
|
|
|
|
tailrec_entry_point := new_label();
|
|
|
|
stack_offset := 0;
|
|
|
|
call_gc_label := 0;
|
|
|
|
float_literals := [];
|
2006-05-31 01:16:34 -07:00
|
|
|
int_literals := [];
|
2010-01-20 08:26:46 -08:00
|
|
|
if Config.system = "rhapsody"
|
|
|
|
&& not !Clflags.output_c_object
|
|
|
|
&& is_generic_function fundecl.fun_name
|
2009-05-20 04:52:42 -07:00
|
|
|
then (* PR#4690 *)
|
|
|
|
` .private_extern {emit_symbol fundecl.fun_name}\n`
|
|
|
|
else
|
1997-07-24 06:36:24 -07:00
|
|
|
` .globl {emit_symbol fundecl.fun_name}\n`;
|
1998-03-13 05:57:35 -08:00
|
|
|
begin match Config.system with
|
2013-06-24 01:17:30 -07:00
|
|
|
| "elf" | "bsd" | "bsd_elf" ->
|
1998-03-13 05:57:35 -08:00
|
|
|
` .type {emit_symbol fundecl.fun_name}, @function\n`
|
|
|
|
| _ -> ()
|
1997-07-24 06:36:24 -07:00
|
|
|
end;
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string code_space;
|
1997-07-24 06:36:24 -07:00
|
|
|
` .align 2\n`;
|
2004-06-19 09:17:31 -07:00
|
|
|
`{emit_symbol fundecl.fun_name}:\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
let n = frame_size() in
|
|
|
|
if !contains_calls then begin
|
1998-03-13 07:06:16 -08:00
|
|
|
` mflr {emit_gpr 0}\n`;
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int(-n)}\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string stg} {emit_gpr 0}, {emit_int(n - size_addr)}({emit_gpr 1})\n`
|
1998-06-22 05:43:04 -07:00
|
|
|
end else begin
|
|
|
|
if n > 0 then
|
|
|
|
` addi {emit_gpr 1}, {emit_gpr 1}, {emit_int(-n)}\n`
|
|
|
|
end;
|
1997-07-24 06:36:24 -07:00
|
|
|
`{emit_label !tailrec_entry_point}:\n`;
|
2002-01-09 11:40:48 -08:00
|
|
|
branch_normalization fundecl.fun_body;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_all fundecl.fun_body;
|
|
|
|
(* Emit the glue code to call the GC *)
|
|
|
|
if !call_gc_label > 0 then begin
|
|
|
|
`{emit_label !call_gc_label}:\n`;
|
2007-11-09 07:06:57 -08:00
|
|
|
` b {emit_symbol "caml_call_gc"}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
end;
|
2006-05-31 01:16:34 -07:00
|
|
|
(* Emit the numeric literals *)
|
|
|
|
if !float_literals <> [] || !int_literals <> [] then begin
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string rodata_space;
|
1997-07-24 06:36:24 -07:00
|
|
|
` .align 3\n`;
|
|
|
|
List.iter
|
|
|
|
(fun (f, lbl) ->
|
2010-01-20 08:26:46 -08:00
|
|
|
`{emit_label lbl}:`;
|
|
|
|
if ppc64
|
|
|
|
then emit_float64_directive ".quad" f
|
|
|
|
else emit_float64_split_directive ".long" f)
|
2006-05-31 01:16:34 -07:00
|
|
|
!float_literals;
|
|
|
|
List.iter
|
|
|
|
(fun (n, lbl) ->
|
|
|
|
`{emit_label lbl}: {emit_string datag} {emit_nativeint n}\n`)
|
|
|
|
!int_literals
|
1997-07-24 06:36:24 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
(* Emission of data *)
|
|
|
|
|
|
|
|
let declare_global_data s =
|
|
|
|
` .globl {emit_symbol s}\n`;
|
2013-06-24 01:17:30 -07:00
|
|
|
match Config.system with
|
|
|
|
| "elf" | "bsd" | "bsd_elf" ->
|
1998-03-13 05:57:35 -08:00
|
|
|
` .type {emit_symbol s}, @object\n`
|
2013-06-24 01:17:30 -07:00
|
|
|
| "rhapsody" -> ()
|
|
|
|
| _ -> assert false
|
1997-07-24 06:36:24 -07:00
|
|
|
|
|
|
|
let emit_item = function
|
2002-11-24 07:55:26 -08:00
|
|
|
Cglobal_symbol s ->
|
1997-07-24 06:36:24 -07:00
|
|
|
declare_global_data s
|
2002-11-24 07:55:26 -08:00
|
|
|
| Cdefine_symbol s ->
|
|
|
|
`{emit_symbol s}:\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cdefine_label lbl ->
|
2011-12-18 02:00:56 -08:00
|
|
|
`{emit_data_label lbl}:\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cint8 n ->
|
|
|
|
` .byte {emit_int n}\n`
|
|
|
|
| Cint16 n ->
|
|
|
|
` .short {emit_int n}\n`
|
2000-02-07 02:23:22 -08:00
|
|
|
| Cint32 n ->
|
|
|
|
` .long {emit_nativeint n}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cint n ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string datag} {emit_nativeint n}\n`
|
2000-02-07 02:23:22 -08:00
|
|
|
| Csingle f ->
|
2014-04-25 01:41:13 -07:00
|
|
|
emit_float32_directive ".long" (Int32.bits_of_float f)
|
2000-02-07 02:23:22 -08:00
|
|
|
| Cdouble f ->
|
2010-01-20 08:26:46 -08:00
|
|
|
if ppc64
|
2014-04-25 01:41:13 -07:00
|
|
|
then emit_float64_directive ".quad" (Int64.bits_of_float f)
|
|
|
|
else emit_float64_split_directive ".long" (Int64.bits_of_float f)
|
1997-07-24 06:36:24 -07:00
|
|
|
| Csymbol_address s ->
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string datag} {emit_symbol s}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Clabel_address lbl ->
|
2011-12-18 02:00:56 -08:00
|
|
|
` {emit_string datag} {emit_data_label lbl}\n`
|
1997-07-24 06:36:24 -07:00
|
|
|
| Cstring s ->
|
|
|
|
emit_bytes_directive " .byte " s
|
|
|
|
| Cskip n ->
|
|
|
|
if n > 0 then ` .space {emit_int n}\n`
|
|
|
|
| Calign n ->
|
|
|
|
` .align {emit_int (Misc.log2 n)}\n`
|
|
|
|
|
|
|
|
let data l =
|
|
|
|
emit_string data_space;
|
|
|
|
List.iter emit_item l
|
|
|
|
|
|
|
|
(* Beginning / end of an assembly file *)
|
|
|
|
|
|
|
|
let begin_assembly() =
|
|
|
|
defined_functions := StringSet.empty;
|
1998-03-13 11:31:32 -08:00
|
|
|
external_functions := StringSet.empty;
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Emit the beginning of the segments *)
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_begin = Compilenv.make_symbol (Some "data_begin") in
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_string data_space;
|
|
|
|
declare_global_data lbl_begin;
|
|
|
|
`{emit_symbol lbl_begin}:\n`;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_begin = Compilenv.make_symbol (Some "code_begin") in
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string code_space;
|
1997-07-24 06:36:24 -07:00
|
|
|
declare_global_data lbl_begin;
|
|
|
|
`{emit_symbol lbl_begin}:\n`
|
|
|
|
|
|
|
|
let end_assembly() =
|
1998-03-13 11:31:32 -08:00
|
|
|
if pic_externals then
|
|
|
|
(* Emit the pointers to external functions *)
|
|
|
|
StringSet.iter emit_external !external_functions;
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Emit the end of the segments *)
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string code_space;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_end = Compilenv.make_symbol (Some "code_end") in
|
1997-07-24 06:36:24 -07:00
|
|
|
declare_global_data lbl_end;
|
|
|
|
`{emit_symbol lbl_end}:\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` .long 0\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
emit_string data_space;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl_end = Compilenv.make_symbol (Some "data_end") in
|
1997-07-24 06:36:24 -07:00
|
|
|
declare_global_data lbl_end;
|
|
|
|
`{emit_symbol lbl_end}:\n`;
|
2006-05-31 01:16:34 -07:00
|
|
|
` {emit_string datag} 0\n`;
|
1997-07-24 06:36:24 -07:00
|
|
|
(* Emit the frame descriptors *)
|
2007-11-09 07:06:57 -08:00
|
|
|
emit_string rodata_space;
|
2004-01-05 12:26:19 -08:00
|
|
|
let lbl = Compilenv.make_symbol (Some "frametable") in
|
1997-07-24 06:36:24 -07:00
|
|
|
declare_global_data lbl;
|
|
|
|
`{emit_symbol lbl}:\n`;
|
2007-01-29 04:11:18 -08:00
|
|
|
emit_frames
|
|
|
|
{ efa_label = (fun l -> ` {emit_string datag} {emit_label l}\n`);
|
|
|
|
efa_16 = (fun n -> ` .short {emit_int n}\n`);
|
|
|
|
efa_32 = (fun n -> ` .long {emit_int32 n}\n`);
|
|
|
|
efa_word = (fun n -> ` {emit_string datag} {emit_int n}\n`);
|
|
|
|
efa_align = (fun n -> ` .align {emit_int (Misc.log2 n)}\n`);
|
|
|
|
efa_label_rel = (fun lbl ofs ->
|
|
|
|
` .long ({emit_label lbl} - .) + {emit_int32 ofs}\n`);
|
|
|
|
efa_def_label = (fun l -> `{emit_label l}:\n`);
|
|
|
|
efa_string = (fun s -> emit_bytes_directive " .byte " (s ^ "\000"))
|
|
|
|
}
|