2016-02-18 07:11:59 -08:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* OCaml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
|
|
|
(* en Automatique. *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. This file is distributed under the terms of *)
|
|
|
|
(* the GNU Lesser General Public License version 2.1, with the *)
|
|
|
|
(* special exception on linking described in the file LICENSE. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
1995-08-09 08:06:35 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Translation from closed lambda to C-- *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Arch
|
|
|
|
open Asttypes
|
1995-07-27 10:38:53 -07:00
|
|
|
open Primitive
|
2013-05-22 06:59:24 -07:00
|
|
|
open Types
|
1995-07-02 09:41:48 -07:00
|
|
|
open Lambda
|
|
|
|
open Clambda
|
|
|
|
open Cmm
|
2010-05-19 04:29:38 -07:00
|
|
|
open Cmx_format
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
(* Local binding of complex expressions *)
|
|
|
|
|
|
|
|
let bind name arg fn =
|
|
|
|
match arg with
|
2006-01-04 08:55:50 -08:00
|
|
|
Cvar _ | Cconst_int _ | Cconst_natint _ | Cconst_symbol _
|
2014-03-17 07:34:00 -07:00
|
|
|
| Cconst_pointer _ | Cconst_natpointer _
|
2016-06-29 23:46:45 -07:00
|
|
|
| Cblockheader _ -> fn arg
|
1996-04-22 04:15:41 -07:00
|
|
|
| _ -> let id = Ident.create name in Clet(id, arg, fn (Cvar id))
|
1995-07-10 02:48:27 -07:00
|
|
|
|
2015-07-25 13:44:08 -07:00
|
|
|
let bind_load name arg fn =
|
|
|
|
match arg with
|
|
|
|
| Cop(Cload _, [Cvar _]) -> fn arg
|
|
|
|
| _ -> bind name arg fn
|
|
|
|
|
1999-11-09 06:41:24 -08:00
|
|
|
let bind_nonvar name arg fn =
|
|
|
|
match arg with
|
2000-03-17 05:24:17 -08:00
|
|
|
Cconst_int _ | Cconst_natint _ | Cconst_symbol _
|
2014-03-17 07:34:00 -07:00
|
|
|
| Cconst_pointer _ | Cconst_natpointer _
|
2016-06-29 23:46:45 -07:00
|
|
|
| Cblockheader _ -> fn arg
|
1999-11-09 06:41:24 -08:00
|
|
|
| _ -> let id = Ident.create name in Clet(id, arg, fn (Cvar id))
|
|
|
|
|
2014-08-22 06:45:02 -07:00
|
|
|
let caml_black = Nativeint.shift_left (Nativeint.of_int 3) 8
|
|
|
|
(* cf. byterun/gc.h *)
|
2014-05-12 00:29:24 -07:00
|
|
|
|
2002-01-20 14:20:58 -08:00
|
|
|
(* Block headers. Meaning of the tag field: see stdlib/obj.ml *)
|
|
|
|
|
|
|
|
let floatarray_tag = Cconst_int Obj.double_array_tag
|
1995-07-27 10:38:53 -07:00
|
|
|
|
1997-03-04 02:19:51 -08:00
|
|
|
let block_header tag sz =
|
2000-02-11 07:09:27 -08:00
|
|
|
Nativeint.add (Nativeint.shift_left (Nativeint.of_int sz) 10)
|
|
|
|
(Nativeint.of_int tag)
|
2014-05-12 00:29:24 -07:00
|
|
|
(* Static data corresponding to "value"s must be marked black in case we are
|
|
|
|
in no-naked-pointers mode. See [caml_darken] and the code below that emits
|
|
|
|
structured constants and static module definitions. *)
|
|
|
|
let black_block_header tag sz = Nativeint.logor (block_header tag sz) caml_black
|
|
|
|
let white_closure_header sz = block_header Obj.closure_tag sz
|
|
|
|
let black_closure_header sz = black_block_header Obj.closure_tag sz
|
2002-01-20 14:20:58 -08:00
|
|
|
let infix_header ofs = block_header Obj.infix_tag ofs
|
|
|
|
let float_header = block_header Obj.double_tag (size_float / size_addr)
|
|
|
|
let floatarray_header len =
|
|
|
|
block_header Obj.double_array_tag (len * size_float / size_addr)
|
|
|
|
let string_header len =
|
|
|
|
block_header Obj.string_tag ((len + size_addr) / size_addr)
|
2010-01-29 06:42:09 -08:00
|
|
|
let boxedint32_header = block_header Obj.custom_tag 2
|
|
|
|
let boxedint64_header = block_header Obj.custom_tag (1 + 8 / size_addr)
|
|
|
|
let boxedintnat_header = block_header Obj.custom_tag 2
|
1995-07-10 02:48:27 -07:00
|
|
|
|
2016-06-29 23:46:45 -07:00
|
|
|
let alloc_float_header dbg = Cblockheader(float_header, dbg)
|
|
|
|
let alloc_floatarray_header len dbg = Cblockheader(floatarray_header len, dbg)
|
|
|
|
let alloc_closure_header sz dbg = Cblockheader(white_closure_header sz, dbg)
|
|
|
|
let alloc_infix_header ofs dbg = Cblockheader(infix_header ofs, dbg)
|
|
|
|
let alloc_boxedint32_header dbg = Cblockheader(boxedint32_header, dbg)
|
|
|
|
let alloc_boxedint64_header dbg = Cblockheader(boxedint64_header, dbg)
|
|
|
|
let alloc_boxedintnat_header dbg = Cblockheader(boxedintnat_header, dbg)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Integers *)
|
|
|
|
|
1997-03-04 02:19:51 -08:00
|
|
|
let max_repr_int = max_int asr 1
|
|
|
|
let min_repr_int = min_int asr 1
|
|
|
|
|
1995-10-26 09:23:25 -07:00
|
|
|
let int_const n =
|
2000-02-28 01:03:00 -08:00
|
|
|
if n <= max_repr_int && n >= min_repr_int
|
1995-10-26 09:23:25 -07:00
|
|
|
then Cconst_int((n lsl 1) + 1)
|
2003-04-25 05:27:31 -07:00
|
|
|
else Cconst_natint
|
|
|
|
(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2016-01-12 06:31:02 -08:00
|
|
|
let cint_const n =
|
|
|
|
Cint(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
|
|
|
|
|
2015-12-04 07:58:33 -08:00
|
|
|
let add_no_overflow n x c =
|
|
|
|
let d = n + x in
|
2016-02-10 09:28:38 -08:00
|
|
|
if d = 0 then c else Cop(Caddi, [c; Cconst_int d])
|
2015-12-04 07:58:33 -08:00
|
|
|
|
2013-11-04 13:39:55 -08:00
|
|
|
let rec add_const c n =
|
2012-04-18 00:12:54 -07:00
|
|
|
if n = 0 then c
|
|
|
|
else match c with
|
|
|
|
| Cconst_int x when no_overflow_add x n -> Cconst_int (x + n)
|
2015-12-04 07:58:33 -08:00
|
|
|
| Cop(Caddi, [Cconst_int x; c])
|
|
|
|
when no_overflow_add n x ->
|
|
|
|
add_no_overflow n x c
|
|
|
|
| Cop(Caddi, [c; Cconst_int x])
|
2015-09-11 04:58:31 -07:00
|
|
|
when no_overflow_add n x ->
|
2015-12-04 07:58:33 -08:00
|
|
|
add_no_overflow n x c
|
2013-11-04 13:39:55 -08:00
|
|
|
| Cop(Csubi, [Cconst_int x; c]) when no_overflow_add n x ->
|
|
|
|
Cop(Csubi, [Cconst_int (n + x); c])
|
|
|
|
| Cop(Csubi, [c; Cconst_int x]) when no_overflow_sub n x ->
|
|
|
|
add_const c (n - x)
|
2012-04-18 00:12:54 -07:00
|
|
|
| c -> Cop(Caddi, [c; Cconst_int n])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2015-02-07 03:32:12 -08:00
|
|
|
let incr_int c = add_const c 1
|
|
|
|
let decr_int c = add_const c (-1)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2015-02-07 03:32:12 -08:00
|
|
|
let rec add_int c1 c2 =
|
1995-07-02 09:41:48 -07:00
|
|
|
match (c1, c2) with
|
2015-02-07 03:32:12 -08:00
|
|
|
| (Cconst_int n, c) | (c, Cconst_int n) ->
|
|
|
|
add_const c n
|
1995-07-02 09:41:48 -07:00
|
|
|
| (Cop(Caddi, [c1; Cconst_int n1]), c2) ->
|
2015-02-07 03:32:12 -08:00
|
|
|
add_const (add_int c1 c2) n1
|
1995-07-02 09:41:48 -07:00
|
|
|
| (c1, Cop(Caddi, [c2; Cconst_int n2])) ->
|
2015-02-07 03:32:12 -08:00
|
|
|
add_const (add_int c1 c2) n2
|
1998-11-11 01:38:40 -08:00
|
|
|
| (_, _) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
Cop(Caddi, [c1; c2])
|
|
|
|
|
2015-02-07 03:32:12 -08:00
|
|
|
let rec sub_int c1 c2 =
|
1995-07-02 09:41:48 -07:00
|
|
|
match (c1, c2) with
|
2015-02-07 03:32:12 -08:00
|
|
|
| (c1, Cconst_int n2) when n2 <> min_int ->
|
|
|
|
add_const c1 (-n2)
|
1995-10-26 09:23:25 -07:00
|
|
|
| (c1, Cop(Caddi, [c2; Cconst_int n2])) when n2 <> min_int ->
|
2015-02-07 03:32:12 -08:00
|
|
|
add_const (sub_int c1 c2) (-n2)
|
|
|
|
| (Cop(Caddi, [c1; Cconst_int n1]), c2) ->
|
|
|
|
add_const (sub_int c1 c2) n1
|
1995-07-02 09:41:48 -07:00
|
|
|
| (c1, c2) ->
|
|
|
|
Cop(Csubi, [c1; c2])
|
|
|
|
|
2015-02-07 03:32:12 -08:00
|
|
|
let rec lsl_int c1 c2 =
|
2000-02-28 07:45:50 -08:00
|
|
|
match (c1, c2) with
|
2015-02-07 03:32:12 -08:00
|
|
|
| (Cop(Clsl, [c; Cconst_int n1]), Cconst_int n2)
|
|
|
|
when n1 > 0 && n2 > 0 && n1 + n2 < size_int * 8 ->
|
|
|
|
Cop(Clsl, [c; Cconst_int (n1 + n2)])
|
|
|
|
| (Cop(Caddi, [c1; Cconst_int n1]), Cconst_int n2)
|
|
|
|
when no_overflow_lsl n1 n2 ->
|
|
|
|
add_const (lsl_int c1 c2) (n1 lsl n2)
|
|
|
|
| (_, _) ->
|
|
|
|
Cop(Clsl, [c1; c2])
|
|
|
|
|
2015-12-04 07:58:33 -08:00
|
|
|
let is_power2 n = n = 1 lsl Misc.log2 n
|
|
|
|
|
|
|
|
and mult_power2 c n = lsl_int c (Cconst_int (Misc.log2 n))
|
|
|
|
|
2015-02-07 03:32:12 -08:00
|
|
|
let rec mul_int c1 c2 =
|
|
|
|
match (c1, c2) with
|
2016-03-09 02:40:16 -08:00
|
|
|
| (_, Cconst_int 0) | (Cconst_int 0, _) ->
|
2013-11-04 11:19:07 -08:00
|
|
|
Cconst_int 0
|
|
|
|
| (c, Cconst_int 1) | (Cconst_int 1, c) ->
|
|
|
|
c
|
|
|
|
| (c, Cconst_int(-1)) | (Cconst_int(-1), c) ->
|
|
|
|
sub_int (Cconst_int 0) c
|
2015-12-04 07:58:33 -08:00
|
|
|
| (c, Cconst_int n) when is_power2 n -> mult_power2 c n
|
|
|
|
| (Cconst_int n, c) when is_power2 n -> mult_power2 c n
|
2015-02-07 03:32:12 -08:00
|
|
|
| (Cop(Caddi, [c; Cconst_int n]), Cconst_int k) |
|
|
|
|
(Cconst_int k, Cop(Caddi, [c; Cconst_int n]))
|
|
|
|
when no_overflow_mul n k ->
|
|
|
|
add_const (mul_int c (Cconst_int k)) (n * k)
|
2013-11-04 11:19:07 -08:00
|
|
|
| (c1, c2) ->
|
|
|
|
Cop(Cmuli, [c1; c2])
|
2000-02-28 07:45:50 -08:00
|
|
|
|
1999-01-29 00:50:10 -08:00
|
|
|
|
2000-02-21 10:14:56 -08:00
|
|
|
let ignore_low_bit_int = function
|
2013-03-09 14:38:52 -08:00
|
|
|
Cop(Caddi, [(Cop(Clsl, [_; Cconst_int n]) as c); Cconst_int 1]) when n > 0
|
|
|
|
-> c
|
2000-02-21 10:14:56 -08:00
|
|
|
| Cop(Cor, [c; Cconst_int 1]) -> c
|
|
|
|
| c -> c
|
|
|
|
|
2012-11-17 09:23:15 -08:00
|
|
|
let lsr_int c1 c2 =
|
|
|
|
match c2 with
|
2013-11-18 23:01:54 -08:00
|
|
|
Cconst_int 0 ->
|
|
|
|
c1
|
|
|
|
| Cconst_int n when n > 0 ->
|
|
|
|
Cop(Clsr, [ignore_low_bit_int c1; c2])
|
2012-11-17 09:23:15 -08:00
|
|
|
| _ ->
|
2013-11-18 23:01:54 -08:00
|
|
|
Cop(Clsr, [c1; c2])
|
2012-11-17 09:23:15 -08:00
|
|
|
|
|
|
|
let asr_int c1 c2 =
|
|
|
|
match c2 with
|
2013-11-18 23:01:54 -08:00
|
|
|
Cconst_int 0 ->
|
|
|
|
c1
|
|
|
|
| Cconst_int n when n > 0 ->
|
|
|
|
Cop(Casr, [ignore_low_bit_int c1; c2])
|
2012-11-17 09:23:15 -08:00
|
|
|
| _ ->
|
2013-11-18 23:01:54 -08:00
|
|
|
Cop(Casr, [c1; c2])
|
2012-11-17 09:23:15 -08:00
|
|
|
|
2013-11-18 23:01:54 -08:00
|
|
|
let tag_int = function
|
|
|
|
Cconst_int n ->
|
|
|
|
int_const n
|
|
|
|
| Cop(Casr, [c; Cconst_int n]) when n > 0 ->
|
|
|
|
Cop(Cor, [asr_int c (Cconst_int (n - 1)); Cconst_int 1])
|
|
|
|
| c ->
|
|
|
|
incr_int (lsl_int c (Cconst_int 1))
|
2012-02-24 02:13:02 -08:00
|
|
|
|
2013-11-18 23:01:54 -08:00
|
|
|
let force_tag_int = function
|
|
|
|
Cconst_int n ->
|
|
|
|
int_const n
|
|
|
|
| Cop(Casr, [c; Cconst_int n]) when n > 0 ->
|
|
|
|
Cop(Cor, [asr_int c (Cconst_int (n - 1)); Cconst_int 1])
|
|
|
|
| c ->
|
|
|
|
Cop(Cor, [lsl_int c (Cconst_int 1); Cconst_int 1])
|
2003-11-21 07:53:10 -08:00
|
|
|
|
2013-11-18 23:01:54 -08:00
|
|
|
let untag_int = function
|
|
|
|
Cconst_int n -> Cconst_int(n asr 1)
|
|
|
|
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1]); Cconst_int 1]) -> c
|
|
|
|
| Cop(Cor, [Cop(Casr, [c; Cconst_int n]); Cconst_int 1])
|
|
|
|
when n > 0 && n < size_int * 8 ->
|
|
|
|
Cop(Casr, [c; Cconst_int (n+1)])
|
|
|
|
| Cop(Cor, [Cop(Clsr, [c; Cconst_int n]); Cconst_int 1])
|
|
|
|
when n > 0 && n < size_int * 8 ->
|
|
|
|
Cop(Clsr, [c; Cconst_int (n+1)])
|
|
|
|
| Cop(Cor, [c; Cconst_int 1]) -> Cop(Casr, [c; Cconst_int 1])
|
|
|
|
| c -> Cop(Casr, [c; Cconst_int 1])
|
|
|
|
|
2016-01-15 03:56:02 -08:00
|
|
|
let if_then_else (cond, ifso, ifnot) =
|
|
|
|
match cond with
|
|
|
|
| Cconst_int 0 -> ifnot
|
|
|
|
| Cconst_int 1 -> ifso
|
|
|
|
| _ ->
|
|
|
|
Cifthenelse(cond, ifso, ifnot)
|
|
|
|
|
2013-11-18 23:01:54 -08:00
|
|
|
(* Turning integer divisions into multiply-high then shift.
|
|
|
|
The [division_parameters] function is used in module Emit for
|
|
|
|
those target platforms that support this optimization. *)
|
|
|
|
|
|
|
|
(* Unsigned comparison between native integers. *)
|
|
|
|
|
|
|
|
let ucompare x y = Nativeint.(compare (add x min_int) (add y min_int))
|
|
|
|
|
|
|
|
(* Unsigned division and modulus at type nativeint.
|
|
|
|
Algorithm: Hacker's Delight section 9.3 *)
|
|
|
|
|
|
|
|
let udivmod n d = Nativeint.(
|
|
|
|
if d < 0n then
|
|
|
|
if ucompare n d < 0 then (0n, n) else (1n, sub n d)
|
|
|
|
else begin
|
|
|
|
let q = shift_left (div (shift_right_logical n 1) d) 1 in
|
|
|
|
let r = sub n (mul q d) in
|
|
|
|
if ucompare r d >= 0 then (succ q, sub r d) else (q, r)
|
|
|
|
end)
|
|
|
|
|
|
|
|
(* Compute division parameters.
|
|
|
|
Algorithm: Hacker's Delight chapter 10, fig 10-1. *)
|
|
|
|
|
|
|
|
let divimm_parameters d = Nativeint.(
|
|
|
|
assert (d > 0n);
|
|
|
|
let twopsm1 = min_int in (* 2^31 for 32-bit archs, 2^63 for 64-bit archs *)
|
|
|
|
let nc = sub (pred twopsm1) (snd (udivmod twopsm1 d)) in
|
|
|
|
let rec loop p (q1, r1) (q2, r2) =
|
|
|
|
let p = p + 1 in
|
|
|
|
let q1 = shift_left q1 1 and r1 = shift_left r1 1 in
|
|
|
|
let (q1, r1) =
|
|
|
|
if ucompare r1 nc >= 0 then (succ q1, sub r1 nc) else (q1, r1) in
|
|
|
|
let q2 = shift_left q2 1 and r2 = shift_left r2 1 in
|
|
|
|
let (q2, r2) =
|
|
|
|
if ucompare r2 d >= 0 then (succ q2, sub r2 d) else (q2, r2) in
|
|
|
|
let delta = sub d r2 in
|
|
|
|
if ucompare q1 delta < 0 || (q1 = delta && r1 = 0n)
|
|
|
|
then loop p (q1, r1) (q2, r2)
|
|
|
|
else (succ q2, p - size)
|
|
|
|
in loop (size - 1) (udivmod twopsm1 nc) (udivmod twopsm1 d))
|
|
|
|
|
|
|
|
(* The result [(m, p)] of [divimm_parameters d] satisfies the following
|
|
|
|
inequality:
|
|
|
|
|
|
|
|
2^(wordsize + p) < m * d <= 2^(wordsize + p) + 2^(p + 1) (i)
|
|
|
|
|
|
|
|
from which it follows that
|
|
|
|
|
|
|
|
floor(n / d) = floor(n * m / 2^(wordsize+p))
|
|
|
|
if 0 <= n < 2^(wordsize-1)
|
|
|
|
ceil(n / d) = floor(n * m / 2^(wordsize+p)) + 1
|
|
|
|
if -2^(wordsize-1) <= n < 0
|
|
|
|
|
|
|
|
The correctness condition (i) above can be checked by the code below.
|
|
|
|
It was exhaustively tested for values of d from 2 to 10^9 in the
|
|
|
|
wordsize = 64 case.
|
|
|
|
|
|
|
|
let add2 (xh, xl) (yh, yl) =
|
|
|
|
let zl = add xl yl and zh = add xh yh in
|
|
|
|
((if ucompare zl xl < 0 then succ zh else zh), zl)
|
|
|
|
|
|
|
|
let shl2 (xh, xl) n =
|
|
|
|
assert (0 < n && n < size + size);
|
|
|
|
if n < size
|
|
|
|
then (logor (shift_left xh n) (shift_right_logical xl (size - n)),
|
|
|
|
shift_left xl n)
|
|
|
|
else (shift_left xl (n - size), 0n)
|
|
|
|
|
|
|
|
let mul2 x y =
|
|
|
|
let halfsize = size / 2 in
|
|
|
|
let halfmask = pred (shift_left 1n halfsize) in
|
|
|
|
let xl = logand x halfmask and xh = shift_right_logical x halfsize in
|
|
|
|
let yl = logand y halfmask and yh = shift_right_logical y halfsize in
|
|
|
|
add2 (mul xh yh, 0n)
|
|
|
|
(add2 (shl2 (0n, mul xl yh) halfsize)
|
|
|
|
(add2 (shl2 (0n, mul xh yl) halfsize)
|
|
|
|
(0n, mul xl yl)))
|
|
|
|
|
|
|
|
let ucompare2 (xh, xl) (yh, yl) =
|
|
|
|
let c = ucompare xh yh in if c = 0 then ucompare xl yl else c
|
|
|
|
|
|
|
|
let validate d m p =
|
|
|
|
let md = mul2 m d in
|
|
|
|
let one2 = (0n, 1n) in
|
|
|
|
let twoszp = shl2 one2 (size + p) in
|
|
|
|
let twop1 = shl2 one2 (p + 1) in
|
|
|
|
ucompare2 twoszp md < 0 && ucompare2 md (add2 twoszp twop1) <= 0
|
|
|
|
*)
|
|
|
|
|
2016-07-28 04:46:23 -07:00
|
|
|
let raise_regular dbg exc =
|
|
|
|
Csequence(
|
|
|
|
Cop(Cstore (Thirtytwo_signed, Assignment),
|
|
|
|
[(Cconst_symbol "caml_backtrace_pos"); Cconst_int 0]),
|
2016-07-28 06:25:13 -07:00
|
|
|
Cop(Craise (Raise_withtrace, dbg),[exc]))
|
2016-07-28 04:46:23 -07:00
|
|
|
|
|
|
|
let raise_symbol dbg symb =
|
|
|
|
raise_regular dbg (Cconst_symbol symb)
|
|
|
|
|
2016-10-05 07:42:40 -07:00
|
|
|
let rec div_int c1 c2 is_safe dbg =
|
2013-11-18 23:01:54 -08:00
|
|
|
match (c1, c2) with
|
|
|
|
(c1, Cconst_int 0) ->
|
2016-07-28 04:46:23 -07:00
|
|
|
Csequence(c1, raise_symbol dbg "caml_exn_Division_by_zero")
|
2013-11-18 23:01:54 -08:00
|
|
|
| (c1, Cconst_int 1) ->
|
|
|
|
c1
|
|
|
|
| (Cconst_int 0 as c1, c2) ->
|
|
|
|
Csequence(c2, c1)
|
|
|
|
| (Cconst_int n1, Cconst_int n2) ->
|
|
|
|
Cconst_int (n1 / n2)
|
|
|
|
| (c1, Cconst_int n) when n <> min_int ->
|
|
|
|
let l = Misc.log2 n in
|
|
|
|
if n = 1 lsl l then
|
|
|
|
(* Algorithm:
|
|
|
|
t = shift-right-signed(c1, l - 1)
|
|
|
|
t = shift-right(t, W - l)
|
|
|
|
t = c1 + t
|
|
|
|
res = shift-right-signed(c1 + t, l)
|
|
|
|
*)
|
|
|
|
Cop(Casr, [bind "dividend" c1 (fun c1 ->
|
|
|
|
let t = asr_int c1 (Cconst_int (l - 1)) in
|
|
|
|
let t = lsr_int t (Cconst_int (Nativeint.size - l)) in
|
|
|
|
add_int c1 t);
|
|
|
|
Cconst_int l])
|
|
|
|
else if n < 0 then
|
2016-10-05 07:42:40 -07:00
|
|
|
sub_int (Cconst_int 0) (div_int c1 (Cconst_int (-n)) is_safe dbg)
|
2013-11-18 23:01:54 -08:00
|
|
|
else begin
|
|
|
|
let (m, p) = divimm_parameters (Nativeint.of_int n) in
|
|
|
|
(* Algorithm:
|
|
|
|
t = multiply-high-signed(c1, m)
|
|
|
|
if m < 0, t = t + c1
|
|
|
|
if p > 0, t = shift-right-signed(t, p)
|
|
|
|
res = t + sign-bit(c1)
|
|
|
|
*)
|
|
|
|
bind "dividend" c1 (fun c1 ->
|
|
|
|
let t = Cop(Cmulhi, [c1; Cconst_natint m]) in
|
|
|
|
let t = if m < 0n then Cop(Caddi, [t; c1]) else t in
|
|
|
|
let t = if p > 0 then Cop(Casr, [t; Cconst_int p]) else t in
|
|
|
|
add_int t (lsr_int c1 (Cconst_int (Nativeint.size - 1))))
|
|
|
|
end
|
2016-10-05 07:42:40 -07:00
|
|
|
| (c1, c2) when !Clflags.fast || is_safe = Lambda.Unsafe ->
|
2013-11-18 23:01:54 -08:00
|
|
|
Cop(Cdivi, [c1; c2])
|
|
|
|
| (c1, c2) ->
|
|
|
|
bind "divisor" c2 (fun c2 ->
|
|
|
|
Cifthenelse(c2,
|
|
|
|
Cop(Cdivi, [c1; c2]),
|
2016-07-28 04:46:23 -07:00
|
|
|
raise_symbol dbg "caml_exn_Division_by_zero"))
|
2013-11-18 23:01:54 -08:00
|
|
|
|
2016-10-05 07:42:40 -07:00
|
|
|
let mod_int c1 c2 is_safe dbg =
|
2013-11-18 23:01:54 -08:00
|
|
|
match (c1, c2) with
|
|
|
|
(c1, Cconst_int 0) ->
|
2016-07-28 04:46:23 -07:00
|
|
|
Csequence(c1, raise_symbol dbg "caml_exn_Division_by_zero")
|
2015-02-08 02:00:18 -08:00
|
|
|
| (c1, Cconst_int (1 | (-1))) ->
|
|
|
|
Csequence(c1, Cconst_int 0)
|
2015-07-17 07:31:05 -07:00
|
|
|
| (Cconst_int 0, c2) ->
|
|
|
|
Csequence(c2, Cconst_int 0)
|
2013-11-18 23:01:54 -08:00
|
|
|
| (Cconst_int n1, Cconst_int n2) ->
|
|
|
|
Cconst_int (n1 mod n2)
|
|
|
|
| (c1, (Cconst_int n as c2)) when n <> min_int ->
|
|
|
|
let l = Misc.log2 n in
|
|
|
|
if n = 1 lsl l then
|
|
|
|
(* Algorithm:
|
|
|
|
t = shift-right-signed(c1, l - 1)
|
|
|
|
t = shift-right(t, W - l)
|
|
|
|
t = c1 + t
|
|
|
|
t = bit-and(t, -n)
|
|
|
|
res = c1 - t
|
|
|
|
*)
|
|
|
|
bind "dividend" c1 (fun c1 ->
|
|
|
|
let t = asr_int c1 (Cconst_int (l - 1)) in
|
|
|
|
let t = lsr_int t (Cconst_int (Nativeint.size - l)) in
|
|
|
|
let t = add_int c1 t in
|
|
|
|
let t = Cop(Cand, [t; Cconst_int (-n)]) in
|
|
|
|
sub_int c1 t)
|
|
|
|
else
|
|
|
|
bind "dividend" c1 (fun c1 ->
|
2016-10-05 07:42:40 -07:00
|
|
|
sub_int c1 (mul_int (div_int c1 c2 is_safe dbg) c2))
|
|
|
|
| (c1, c2) when !Clflags.fast || is_safe = Lambda.Unsafe ->
|
|
|
|
(* Flambda already generates that test *)
|
2013-11-18 23:01:54 -08:00
|
|
|
Cop(Cmodi, [c1; c2])
|
|
|
|
| (c1, c2) ->
|
|
|
|
bind "divisor" c2 (fun c2 ->
|
|
|
|
Cifthenelse(c2,
|
|
|
|
Cop(Cmodi, [c1; c2]),
|
2016-07-28 04:46:23 -07:00
|
|
|
raise_symbol dbg "caml_exn_Division_by_zero"))
|
2003-11-21 07:53:10 -08:00
|
|
|
|
2012-02-24 02:13:02 -08:00
|
|
|
(* Division or modulo on boxed integers. The overflow case min_int / -1
|
|
|
|
can occur, in which case we force x / -1 = -x and x mod -1 = 0. (PR#5513). *)
|
|
|
|
|
2013-11-18 23:01:54 -08:00
|
|
|
let is_different_from x = function
|
|
|
|
Cconst_int n -> n <> x
|
|
|
|
| Cconst_natint n -> n <> Nativeint.of_int x
|
|
|
|
| _ -> false
|
|
|
|
|
2016-10-05 07:42:40 -07:00
|
|
|
let safe_divmod_bi mkop is_safe mkm1 c1 c2 bi dbg =
|
2012-02-24 02:13:02 -08:00
|
|
|
bind "dividend" c1 (fun c1 ->
|
|
|
|
bind "divisor" c2 (fun c2 ->
|
2016-10-05 07:42:40 -07:00
|
|
|
let c = mkop c1 c2 is_safe dbg in
|
2013-11-18 23:01:54 -08:00
|
|
|
if Arch.division_crashes_on_overflow
|
|
|
|
&& (size_int = 4 || bi <> Pint32)
|
|
|
|
&& not (is_different_from (-1) c2)
|
|
|
|
then Cifthenelse(Cop(Ccmpi Cne, [c2; Cconst_int(-1)]), c, mkm1 c1)
|
|
|
|
else c))
|
2012-02-24 02:13:02 -08:00
|
|
|
|
2016-10-05 07:42:40 -07:00
|
|
|
let safe_div_bi is_safe =
|
|
|
|
safe_divmod_bi div_int is_safe (fun c1 -> Cop(Csubi, [Cconst_int 0; c1]))
|
2012-02-24 02:13:02 -08:00
|
|
|
|
2016-10-05 07:42:40 -07:00
|
|
|
let safe_mod_bi is_safe =
|
|
|
|
safe_divmod_bi mod_int is_safe (fun _ -> Cconst_int 0)
|
2012-02-24 02:13:02 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Bool *)
|
|
|
|
|
2016-01-22 00:36:59 -08:00
|
|
|
let test_bool = function
|
1995-07-02 09:41:48 -07:00
|
|
|
Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1]); Cconst_int 1]) -> c
|
2016-03-30 03:05:30 -07:00
|
|
|
| Cconst_int n ->
|
|
|
|
if n = 1 then
|
|
|
|
Cconst_int 0
|
|
|
|
else
|
|
|
|
Cconst_int 1
|
1995-07-02 09:41:48 -07:00
|
|
|
| c -> Cop(Ccmpi Cne, [c; Cconst_int 1])
|
|
|
|
|
|
|
|
(* Float *)
|
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
let box_float dbg c = Cop(Calloc dbg, [alloc_float_header dbg; c])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2008-08-05 06:35:20 -07:00
|
|
|
let rec unbox_float = function
|
2016-06-27 00:49:26 -07:00
|
|
|
Cop(Calloc _, [_header; c]) -> c
|
2008-08-05 06:35:20 -07:00
|
|
|
| Clet(id, exp, body) -> Clet(id, exp, unbox_float body)
|
|
|
|
| Cifthenelse(cond, e1, e2) ->
|
|
|
|
Cifthenelse(cond, unbox_float e1, unbox_float e2)
|
|
|
|
| Csequence(e1, e2) -> Csequence(e1, unbox_float e2)
|
|
|
|
| Cswitch(e, tbl, el) -> Cswitch(e, tbl, Array.map unbox_float el)
|
|
|
|
| Ccatch(n, ids, e1, e2) -> Ccatch(n, ids, unbox_float e1, unbox_float e2)
|
|
|
|
| Ctrywith(e1, id, e2) -> Ctrywith(unbox_float e1, id, unbox_float e2)
|
2000-03-10 06:31:06 -08:00
|
|
|
| c -> Cop(Cload Double_u, [c])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2002-02-10 09:01:27 -08:00
|
|
|
(* Complex *)
|
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
let box_complex dbg c_re c_im =
|
|
|
|
Cop(Calloc dbg, [alloc_floatarray_header 2 dbg; c_re; c_im])
|
2002-02-10 09:01:27 -08:00
|
|
|
|
|
|
|
let complex_re c = Cop(Cload Double_u, [c])
|
|
|
|
let complex_im c = Cop(Cload Double_u,
|
|
|
|
[Cop(Cadda, [c; Cconst_int size_float])])
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Unit *)
|
|
|
|
|
2000-03-17 05:24:17 -08:00
|
|
|
let return_unit c = Csequence(c, Cconst_pointer 1)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let rec remove_unit = function
|
2000-03-17 05:24:17 -08:00
|
|
|
Cconst_pointer 1 -> Ctuple []
|
|
|
|
| Csequence(c, Cconst_pointer 1) -> c
|
1995-07-02 09:41:48 -07:00
|
|
|
| Csequence(c1, c2) ->
|
|
|
|
Csequence(c1, remove_unit c2)
|
|
|
|
| Cifthenelse(cond, ifso, ifnot) ->
|
|
|
|
Cifthenelse(cond, remove_unit ifso, remove_unit ifnot)
|
|
|
|
| Cswitch(sel, index, cases) ->
|
|
|
|
Cswitch(sel, index, Array.map remove_unit cases)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ccatch(io, ids, body, handler) ->
|
|
|
|
Ccatch(io, ids, remove_unit body, remove_unit handler)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Ctrywith(body, exn, handler) ->
|
|
|
|
Ctrywith(remove_unit body, exn, remove_unit handler)
|
1995-11-25 07:38:43 -08:00
|
|
|
| Clet(id, c1, c2) ->
|
|
|
|
Clet(id, c1, remove_unit c2)
|
2016-03-09 02:40:16 -08:00
|
|
|
| Cop(Capply (_mty, dbg), args) ->
|
2007-01-29 04:11:18 -08:00
|
|
|
Cop(Capply (typ_void, dbg), args)
|
2016-07-06 03:44:00 -07:00
|
|
|
| Cop(Cextcall(proc, _mty, alloc, dbg, label_after), args) ->
|
|
|
|
Cop(Cextcall(proc, typ_void, alloc, dbg, label_after), args)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Cexit (_,_) as c -> c
|
1996-07-25 06:15:16 -07:00
|
|
|
| Ctuple [] as c -> c
|
|
|
|
| c -> Csequence(c, Ctuple [])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Access to block fields *)
|
|
|
|
|
|
|
|
let field_address ptr n =
|
|
|
|
if n = 0
|
|
|
|
then ptr
|
|
|
|
else Cop(Cadda, [ptr; Cconst_int(n * size_addr)])
|
|
|
|
|
2014-11-06 00:54:14 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let get_field ptr n =
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Cload Word_val, [field_address ptr n])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2015-12-18 08:42:40 -08:00
|
|
|
let set_field ptr n newval init =
|
|
|
|
Cop(Cstore (Word_val, init), [field_address ptr n; newval])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2016-08-02 09:52:07 -07:00
|
|
|
let non_profinfo_mask = (1 lsl (64 - Config.profinfo_width)) - 1
|
|
|
|
|
|
|
|
let get_header ptr =
|
|
|
|
Cop(Cload Word_int, [Cop(Cadda, [ptr; Cconst_int(-size_int)])])
|
|
|
|
|
|
|
|
let get_header_without_profinfo ptr =
|
|
|
|
if Config.profinfo then
|
|
|
|
Cop(Cand, [get_header ptr; Cconst_int non_profinfo_mask])
|
2016-07-29 07:07:10 -07:00
|
|
|
else
|
2016-08-02 09:52:07 -07:00
|
|
|
get_header ptr
|
1995-07-27 10:38:53 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let tag_offset =
|
1995-07-25 04:37:38 -07:00
|
|
|
if big_endian then -1 else -size_int
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let get_tag ptr =
|
1995-07-25 04:37:38 -07:00
|
|
|
if Proc.word_addressed then (* If byte loads are slow *)
|
2016-08-02 09:52:07 -07:00
|
|
|
Cop(Cand, [get_header ptr; Cconst_int 255])
|
1995-07-25 04:37:38 -07:00
|
|
|
else (* If byte loads are efficient *)
|
2000-02-04 04:43:18 -08:00
|
|
|
Cop(Cload Byte_unsigned,
|
1995-07-25 04:37:38 -07:00
|
|
|
[Cop(Cadda, [ptr; Cconst_int(tag_offset)])])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
let get_size ptr =
|
2016-08-02 09:52:07 -07:00
|
|
|
Cop(Clsr, [get_header_without_profinfo ptr; Cconst_int 10])
|
2004-05-26 04:10:52 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Array indexing *)
|
|
|
|
|
|
|
|
let log2_size_addr = Misc.log2 size_addr
|
1995-07-27 10:38:53 -07:00
|
|
|
let log2_size_float = Misc.log2 size_float
|
|
|
|
|
|
|
|
let wordsize_shift = 9
|
|
|
|
let numfloat_shift = 9 + log2_size_float - log2_size_addr
|
|
|
|
|
1998-04-06 02:07:57 -07:00
|
|
|
let is_addr_array_hdr hdr =
|
1995-09-13 05:27:41 -07:00
|
|
|
Cop(Ccmpi Cne, [Cop(Cand, [hdr; Cconst_int 255]); floatarray_tag])
|
1995-07-27 10:38:53 -07:00
|
|
|
|
1998-04-06 02:07:57 -07:00
|
|
|
let is_addr_array_ptr ptr =
|
|
|
|
Cop(Ccmpi Cne, [get_tag ptr; floatarray_tag])
|
|
|
|
|
1995-07-27 10:38:53 -07:00
|
|
|
let addr_array_length hdr = Cop(Clsr, [hdr; Cconst_int wordsize_shift])
|
|
|
|
let float_array_length hdr = Cop(Clsr, [hdr; Cconst_int numfloat_shift])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let lsl_const c n =
|
2015-04-26 13:41:50 -07:00
|
|
|
if n = 0 then c
|
|
|
|
else Cop(Clsl, [c; Cconst_int n])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2015-04-26 13:41:50 -07:00
|
|
|
(* Produces a pointer to the element of the array [ptr] on the position [ofs]
|
|
|
|
with the given element [log2size] log2 element size. [ofs] is given as a
|
2015-07-27 01:18:53 -07:00
|
|
|
tagged int expression.
|
|
|
|
The optional ?typ argument is the C-- type of the result.
|
|
|
|
By default, it is Addr, meaning we are constructing a derived pointer
|
|
|
|
into the heap. If we know the pointer is outside the heap
|
|
|
|
(this is the case for bigarray indexing), we give type Int instead. *)
|
|
|
|
|
|
|
|
let array_indexing ?typ log2size ptr ofs =
|
|
|
|
let add =
|
|
|
|
match typ with
|
|
|
|
| None | Some Addr -> Cadda
|
|
|
|
| Some Int -> Caddi
|
|
|
|
| _ -> assert false in
|
1995-07-02 09:41:48 -07:00
|
|
|
match ofs with
|
|
|
|
Cconst_int n ->
|
1995-07-27 10:38:53 -07:00
|
|
|
let i = n asr 1 in
|
2015-07-27 01:18:53 -07:00
|
|
|
if i = 0 then ptr else Cop(add, [ptr; Cconst_int(i lsl log2size)])
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cop(Caddi, [Cop(Clsl, [c; Cconst_int 1]); Cconst_int 1]) ->
|
2015-07-27 01:18:53 -07:00
|
|
|
Cop(add, [ptr; lsl_const c log2size])
|
2015-04-26 13:41:50 -07:00
|
|
|
| Cop(Caddi, [c; Cconst_int n]) when log2size = 0 ->
|
2015-07-27 01:18:53 -07:00
|
|
|
Cop(add, [Cop(add, [ptr; untag_int c]); Cconst_int (n asr 1)])
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cop(Caddi, [c; Cconst_int n]) ->
|
2015-07-27 01:18:53 -07:00
|
|
|
Cop(add, [Cop(add, [ptr; lsl_const c (log2size - 1)]);
|
1998-04-06 02:07:57 -07:00
|
|
|
Cconst_int((n-1) lsl (log2size - 1))])
|
2015-04-26 13:41:50 -07:00
|
|
|
| _ when log2size = 0 ->
|
2015-07-27 01:18:53 -07:00
|
|
|
Cop(add, [ptr; untag_int ofs])
|
1995-07-02 09:41:48 -07:00
|
|
|
| _ ->
|
2015-07-27 01:18:53 -07:00
|
|
|
Cop(add, [Cop(add, [ptr; lsl_const ofs (log2size - 1)]);
|
1998-04-06 02:07:57 -07:00
|
|
|
Cconst_int((-1) lsl (log2size - 1))])
|
1995-07-27 10:38:53 -07:00
|
|
|
|
|
|
|
let addr_array_ref arr ofs =
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Cload Word_val, [array_indexing log2_size_addr arr ofs])
|
|
|
|
let int_array_ref arr ofs =
|
|
|
|
Cop(Cload Word_int, [array_indexing log2_size_addr arr ofs])
|
1995-07-27 10:38:53 -07:00
|
|
|
let unboxed_float_array_ref arr ofs =
|
2000-03-10 06:31:06 -08:00
|
|
|
Cop(Cload Double_u, [array_indexing log2_size_float arr ofs])
|
2016-06-27 00:49:26 -07:00
|
|
|
let float_array_ref dbg arr ofs =
|
|
|
|
box_float dbg (unboxed_float_array_ref arr ofs)
|
1995-07-27 10:38:53 -07:00
|
|
|
|
|
|
|
let addr_array_set arr ofs newval =
|
2016-07-06 03:44:00 -07:00
|
|
|
Cop(Cextcall("caml_modify", typ_void, false, Debuginfo.none, None),
|
1995-07-27 10:38:53 -07:00
|
|
|
[array_indexing log2_size_addr arr ofs; newval])
|
|
|
|
let int_array_set arr ofs newval =
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Word_int, Assignment),
|
|
|
|
[array_indexing log2_size_addr arr ofs; newval])
|
1995-07-27 10:38:53 -07:00
|
|
|
let float_array_set arr ofs newval =
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Double_u, Assignment),
|
|
|
|
[array_indexing log2_size_float arr ofs; newval])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
(* String length *)
|
|
|
|
|
2014-03-21 10:02:44 -07:00
|
|
|
(* Length of string block *)
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
let string_length exp =
|
|
|
|
bind "str" exp (fun str ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let tmp_var = Ident.create "tmp" in
|
1995-07-10 02:48:27 -07:00
|
|
|
Clet(tmp_var,
|
|
|
|
Cop(Csubi,
|
|
|
|
[Cop(Clsl,
|
2014-03-27 07:33:28 -07:00
|
|
|
[get_size str;
|
1998-04-06 02:07:57 -07:00
|
|
|
Cconst_int log2_size_addr]);
|
1995-07-10 02:48:27 -07:00
|
|
|
Cconst_int 1]),
|
|
|
|
Cop(Csubi,
|
|
|
|
[Cvar tmp_var;
|
2000-02-04 04:43:18 -08:00
|
|
|
Cop(Cload Byte_unsigned,
|
1998-04-06 02:07:57 -07:00
|
|
|
[Cop(Cadda, [str; Cvar tmp_var])])])))
|
1995-07-10 02:48:27 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Message sending *)
|
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
let lookup_tag obj tag =
|
|
|
|
bind "tag" tag (fun tag ->
|
2016-07-06 03:44:00 -07:00
|
|
|
Cop(Cextcall("caml_get_public_method", typ_val, false, Debuginfo.none,
|
|
|
|
None),
|
2007-01-29 04:11:18 -08:00
|
|
|
[obj; tag]))
|
2004-05-26 04:10:52 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
let lookup_label obj lab =
|
|
|
|
bind "lab" lab (fun lab ->
|
2014-11-06 00:54:14 -08:00
|
|
|
let table = Cop (Cload Word_val, [obj]) in
|
2004-05-26 04:10:52 -07:00
|
|
|
addr_array_ref table lab)
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
let call_cached_method obj tag cache pos args dbg =
|
2004-05-26 04:10:52 -07:00
|
|
|
let arity = List.length args in
|
|
|
|
let cache = array_indexing log2_size_addr cache pos in
|
|
|
|
Compilenv.need_send_fun arity;
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply (typ_val, dbg),
|
2004-05-26 04:10:52 -07:00
|
|
|
Cconst_symbol("caml_send" ^ string_of_int arity) ::
|
|
|
|
obj :: tag :: cache :: args)
|
1996-04-22 04:15:41 -07:00
|
|
|
|
2003-03-06 03:55:55 -08:00
|
|
|
(* Allocation *)
|
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
let make_alloc_generic set_fn dbg tag wordsize args =
|
2003-03-06 03:55:55 -08:00
|
|
|
if wordsize <= Config.max_young_wosize then
|
2016-06-29 23:46:45 -07:00
|
|
|
Cop(Calloc dbg, Cblockheader(block_header tag wordsize, dbg) :: args)
|
2003-03-06 03:55:55 -08:00
|
|
|
else begin
|
|
|
|
let id = Ident.create "alloc" in
|
|
|
|
let rec fill_fields idx = function
|
|
|
|
[] -> Cvar id
|
|
|
|
| e1::el -> Csequence(set_fn (Cvar id) (Cconst_int idx) e1,
|
|
|
|
fill_fields (idx + 2) el) in
|
2006-01-04 08:55:50 -08:00
|
|
|
Clet(id,
|
2016-07-06 03:44:00 -07:00
|
|
|
Cop(Cextcall("caml_alloc", typ_val, true, Debuginfo.none, None),
|
2003-03-06 03:55:55 -08:00
|
|
|
[Cconst_int wordsize; Cconst_int tag]),
|
|
|
|
fill_fields 1 args)
|
|
|
|
end
|
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
let make_alloc dbg tag args =
|
|
|
|
make_alloc_generic addr_array_set dbg tag (List.length args) args
|
|
|
|
let make_float_alloc dbg tag args =
|
|
|
|
make_alloc_generic float_array_set dbg tag
|
2003-03-06 03:55:55 -08:00
|
|
|
(List.length args * size_float / size_addr) args
|
|
|
|
|
2011-12-22 01:36:45 -08:00
|
|
|
(* Bounds checking *)
|
|
|
|
|
|
|
|
let make_checkbound dbg = function
|
|
|
|
| [Cop(Clsr, [a1; Cconst_int n]); Cconst_int m] when (m lsl n) > n ->
|
|
|
|
Cop(Ccheckbound dbg, [a1; Cconst_int(m lsl n + 1 lsl n - 1)])
|
|
|
|
| args ->
|
|
|
|
Cop(Ccheckbound dbg, args)
|
|
|
|
|
1995-07-07 05:07:07 -07:00
|
|
|
(* To compile "let rec" over values *)
|
|
|
|
|
|
|
|
let fundecls_size fundecls =
|
|
|
|
let sz = ref (-1) in
|
|
|
|
List.iter
|
2016-01-06 10:20:49 -08:00
|
|
|
(fun f ->
|
|
|
|
let indirect_call_code_pointer_size =
|
|
|
|
match f.arity with
|
|
|
|
| 0 | 1 -> 0
|
|
|
|
(* arity 1 does not need an indirect call handler.
|
|
|
|
arity 0 cannot be indirect called *)
|
|
|
|
| _ -> 1
|
|
|
|
(* For other arities there is an indirect call handler.
|
|
|
|
if arity >= 2 it is caml_curry...
|
|
|
|
if arity < 0 it is caml_tuplify... *)
|
|
|
|
in
|
|
|
|
sz := !sz + 1 + 2 + indirect_call_code_pointer_size)
|
1995-07-07 05:07:07 -07:00
|
|
|
fundecls;
|
|
|
|
!sz
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2002-11-07 09:57:23 -08:00
|
|
|
type rhs_kind =
|
|
|
|
| RHS_block of int
|
2012-01-13 09:46:21 -08:00
|
|
|
| RHS_floatblock of int
|
2002-11-07 09:57:23 -08:00
|
|
|
| RHS_nonrec
|
|
|
|
;;
|
2013-05-22 05:56:54 -07:00
|
|
|
let rec expr_size env = function
|
|
|
|
| Uvar id ->
|
|
|
|
begin try Ident.find_same id env with Not_found -> RHS_nonrec end
|
2002-11-07 09:57:23 -08:00
|
|
|
| Uclosure(fundecls, clos_vars) ->
|
|
|
|
RHS_block (fundecls_size fundecls + List.length clos_vars)
|
2015-12-07 03:32:03 -08:00
|
|
|
| Ulet(_str, _kind, id, exp, body) ->
|
2013-05-22 05:56:54 -07:00
|
|
|
expr_size (Ident.add id (expr_size env exp) env) body
|
2016-03-09 02:40:16 -08:00
|
|
|
| Uletrec(_bindings, body) ->
|
2013-05-22 05:56:54 -07:00
|
|
|
expr_size env body
|
2016-03-09 02:40:16 -08:00
|
|
|
| Uprim(Pmakeblock _, args, _) ->
|
2002-11-07 09:57:23 -08:00
|
|
|
RHS_block (List.length args)
|
2016-01-12 10:51:06 -08:00
|
|
|
| Uprim(Pmakearray((Paddrarray | Pintarray), _), args, _) ->
|
2002-11-07 09:57:23 -08:00
|
|
|
RHS_block (List.length args)
|
2016-01-12 10:51:06 -08:00
|
|
|
| Uprim(Pmakearray(Pfloatarray, _), args, _) ->
|
2012-01-13 09:46:21 -08:00
|
|
|
RHS_floatblock (List.length args)
|
2014-10-14 08:51:30 -07:00
|
|
|
| Uprim (Pduprecord ((Record_regular | Record_inlined _), sz), _, _) ->
|
2013-05-22 05:56:54 -07:00
|
|
|
RHS_block sz
|
2016-05-25 07:29:05 -07:00
|
|
|
| Uprim (Pduprecord (Record_unboxed _, _), _, _) ->
|
|
|
|
assert false
|
2014-10-14 08:51:30 -07:00
|
|
|
| Uprim (Pduprecord (Record_extension, sz), _, _) ->
|
2014-04-04 05:37:49 -07:00
|
|
|
RHS_block (sz + 1)
|
2013-05-22 05:56:54 -07:00
|
|
|
| Uprim (Pduprecord (Record_float, sz), _, _) ->
|
|
|
|
RHS_floatblock sz
|
2016-01-26 07:43:24 -08:00
|
|
|
| Uprim (Pccall { prim_name; _ }, closure::_, _)
|
|
|
|
when prim_name = "caml_check_value_is_closure" ->
|
|
|
|
(* Used for "-clambda-checks". *)
|
|
|
|
expr_size env closure
|
2016-03-09 02:40:16 -08:00
|
|
|
| Usequence(_exp, exp') ->
|
2013-05-22 05:56:54 -07:00
|
|
|
expr_size env exp'
|
2002-11-07 09:57:23 -08:00
|
|
|
| _ -> RHS_nonrec
|
1996-06-04 08:12:08 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Record application and currying functions *)
|
|
|
|
|
|
|
|
let apply_function n =
|
|
|
|
Compilenv.need_apply_fun n; "caml_apply" ^ string_of_int n
|
|
|
|
let curry_function n =
|
1996-10-22 06:36:12 -07:00
|
|
|
Compilenv.need_curry_fun n;
|
|
|
|
if n >= 0
|
|
|
|
then "caml_curry" ^ string_of_int n
|
|
|
|
else "caml_tuplify" ^ string_of_int (-n)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Comparisons *)
|
|
|
|
|
|
|
|
let transl_comparison = function
|
|
|
|
Lambda.Ceq -> Ceq
|
|
|
|
| Lambda.Cneq -> Cne
|
|
|
|
| Lambda.Cge -> Cge
|
|
|
|
| Lambda.Cgt -> Cgt
|
|
|
|
| Lambda.Cle -> Cle
|
|
|
|
| Lambda.Clt -> Clt
|
|
|
|
|
|
|
|
(* Translate structured constants *)
|
|
|
|
|
|
|
|
let transl_constant = function
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_int n ->
|
1995-10-26 09:40:23 -07:00
|
|
|
int_const n
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_ptr n ->
|
2000-03-17 05:24:17 -08:00
|
|
|
if n <= max_repr_int && n >= min_repr_int
|
|
|
|
then Cconst_pointer((n lsl 1) + 1)
|
2003-04-25 05:27:31 -07:00
|
|
|
else Cconst_natpointer
|
|
|
|
(Nativeint.add (Nativeint.shift_left (Nativeint.of_int n) 1) 1n)
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_ref (label, _) ->
|
|
|
|
Cconst_symbol label
|
|
|
|
|
|
|
|
let transl_structured_constant cst =
|
|
|
|
let label = Compilenv.new_structured_constant cst ~shared:true in
|
|
|
|
Cconst_symbol label
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1999-01-27 02:52:33 -08:00
|
|
|
(* Translate constant closures *)
|
|
|
|
|
2016-01-14 07:16:15 -08:00
|
|
|
type is_global = Global | Not_global
|
|
|
|
|
1999-01-27 02:52:33 -08:00
|
|
|
let constant_closures =
|
2016-01-14 07:16:15 -08:00
|
|
|
ref ([] : ((string * is_global) * ufunction list * uconstant list) list)
|
1999-01-27 02:52:33 -08:00
|
|
|
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Boxed integers *)
|
|
|
|
|
2003-04-25 05:27:31 -07:00
|
|
|
let box_int_constant bi n =
|
|
|
|
match bi with
|
2014-03-06 09:03:16 -08:00
|
|
|
Pnativeint -> Uconst_nativeint n
|
|
|
|
| Pint32 -> Uconst_int32 (Nativeint.to_int32 n)
|
|
|
|
| Pint64 -> Uconst_int64 (Int64.of_nativeint n)
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2003-04-25 05:27:31 -07:00
|
|
|
let operations_boxed_int bi =
|
|
|
|
match bi with
|
2004-01-01 08:42:43 -08:00
|
|
|
Pnativeint -> "caml_nativeint_ops"
|
|
|
|
| Pint32 -> "caml_int32_ops"
|
|
|
|
| Pint64 -> "caml_int64_ops"
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2010-01-29 06:42:09 -08:00
|
|
|
let alloc_header_boxed_int bi =
|
|
|
|
match bi with
|
|
|
|
Pnativeint -> alloc_boxedintnat_header
|
|
|
|
| Pint32 -> alloc_boxedint32_header
|
|
|
|
| Pint64 -> alloc_boxedint64_header
|
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
let box_int dbg bi arg =
|
2000-02-21 10:14:56 -08:00
|
|
|
match arg with
|
|
|
|
Cconst_int n ->
|
2014-03-06 09:03:16 -08:00
|
|
|
transl_structured_constant (box_int_constant bi (Nativeint.of_int n))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Cconst_natint n ->
|
2014-03-06 09:03:16 -08:00
|
|
|
transl_structured_constant (box_int_constant bi n)
|
2000-02-21 10:14:56 -08:00
|
|
|
| _ ->
|
2003-04-25 05:27:31 -07:00
|
|
|
let arg' =
|
|
|
|
if bi = Pint32 && size_int = 8 && big_endian
|
|
|
|
then Cop(Clsl, [arg; Cconst_int 32])
|
|
|
|
else arg in
|
2016-06-27 00:49:26 -07:00
|
|
|
Cop(Calloc dbg, [alloc_header_boxed_int bi dbg;
|
2003-04-25 05:27:31 -07:00
|
|
|
Cconst_symbol(operations_boxed_int bi);
|
2004-11-03 01:29:21 -08:00
|
|
|
arg'])
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2015-08-25 09:18:50 -07:00
|
|
|
let split_int64_for_32bit_target arg =
|
|
|
|
bind "split_int64" arg (fun arg ->
|
2016-04-15 05:41:54 -07:00
|
|
|
let first = Cop (Cadda, [Cconst_int size_int; arg]) in
|
|
|
|
let second = Cop (Cadda, [Cconst_int (2 * size_int); arg]) in
|
|
|
|
Ctuple [Cop (Cload Thirtytwo_unsigned, [first]);
|
|
|
|
Cop (Cload Thirtytwo_unsigned, [second])])
|
2015-08-25 09:18:50 -07:00
|
|
|
|
2008-08-05 06:35:20 -07:00
|
|
|
let rec unbox_int bi arg =
|
2000-02-21 10:14:56 -08:00
|
|
|
match arg with
|
2016-06-27 00:49:26 -07:00
|
|
|
Cop(Calloc _, [_hdr; _ops; Cop(Clsl, [contents; Cconst_int 32])])
|
2003-04-25 05:27:31 -07:00
|
|
|
when bi = Pint32 && size_int = 8 && big_endian ->
|
|
|
|
(* Force sign-extension of low 32 bits *)
|
|
|
|
Cop(Casr, [Cop(Clsl, [contents; Cconst_int 32]); Cconst_int 32])
|
2016-06-27 00:49:26 -07:00
|
|
|
| Cop(Calloc _, [_hdr; _ops; contents])
|
2003-04-25 05:27:31 -07:00
|
|
|
when bi = Pint32 && size_int = 8 && not big_endian ->
|
|
|
|
(* Force sign-extension of low 32 bits *)
|
|
|
|
Cop(Casr, [Cop(Clsl, [contents; Cconst_int 32]); Cconst_int 32])
|
2016-06-27 00:49:26 -07:00
|
|
|
| Cop(Calloc _, [_hdr; _ops; contents]) ->
|
2003-04-25 05:27:31 -07:00
|
|
|
contents
|
2008-08-05 06:35:20 -07:00
|
|
|
| Clet(id, exp, body) -> Clet(id, exp, unbox_int bi body)
|
|
|
|
| Cifthenelse(cond, e1, e2) ->
|
|
|
|
Cifthenelse(cond, unbox_int bi e1, unbox_int bi e2)
|
|
|
|
| Csequence(e1, e2) -> Csequence(e1, unbox_int bi e2)
|
|
|
|
| Cswitch(e, tbl, el) -> Cswitch(e, tbl, Array.map (unbox_int bi) el)
|
|
|
|
| Ccatch(n, ids, e1, e2) -> Ccatch(n, ids, unbox_int bi e1, unbox_int bi e2)
|
|
|
|
| Ctrywith(e1, id, e2) -> Ctrywith(unbox_int bi e1, id, unbox_int bi e2)
|
2000-02-21 10:14:56 -08:00
|
|
|
| _ ->
|
2015-08-25 09:18:50 -07:00
|
|
|
if size_int = 4 && bi = Pint64 then
|
|
|
|
split_int64_for_32bit_target arg
|
|
|
|
else
|
|
|
|
Cop(Cload(if bi = Pint32 then Thirtytwo_signed else Word_int),
|
|
|
|
[Cop(Cadda, [arg; Cconst_int size_addr])])
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2003-04-25 06:26:55 -07:00
|
|
|
let make_unsigned_int bi arg =
|
|
|
|
if bi = Pint32 && size_int = 8
|
|
|
|
then Cop(Cand, [arg; Cconst_natint 0xFFFFFFFFn])
|
|
|
|
else arg
|
2000-02-21 11:38:09 -08:00
|
|
|
|
2015-08-25 09:18:49 -07:00
|
|
|
(* Boxed numbers *)
|
|
|
|
|
|
|
|
type boxed_number =
|
2016-06-27 00:49:26 -07:00
|
|
|
| Boxed_float of Debuginfo.t
|
|
|
|
| Boxed_integer of boxed_integer * Debuginfo.t
|
|
|
|
|
|
|
|
let equal_unboxed_integer ui1 ui2 =
|
|
|
|
match ui1, ui2 with
|
|
|
|
| Pnativeint, Pnativeint -> true
|
|
|
|
| Pint32, Pint32 -> true
|
|
|
|
| Pint64, Pint64 -> true
|
|
|
|
| _, _ -> false
|
|
|
|
|
|
|
|
let equal_boxed_number bn1 bn2 =
|
|
|
|
match bn1, bn2 with
|
|
|
|
| Boxed_float _, Boxed_float _ -> true
|
|
|
|
| Boxed_integer(ui1, _), Boxed_integer(ui2, _) ->
|
|
|
|
equal_unboxed_integer ui1 ui2
|
|
|
|
| _, _ -> false
|
2015-08-25 09:18:49 -07:00
|
|
|
|
|
|
|
let box_number bn arg =
|
|
|
|
match bn with
|
2016-06-27 00:49:26 -07:00
|
|
|
| Boxed_float dbg -> box_float dbg arg
|
|
|
|
| Boxed_integer (bi, dbg) -> box_int dbg bi arg
|
2015-08-25 09:18:49 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
type env = {
|
|
|
|
unboxed_ids : (Ident.t * boxed_number) Ident.tbl;
|
|
|
|
}
|
|
|
|
|
|
|
|
let empty_env =
|
|
|
|
{
|
|
|
|
unboxed_ids =Ident.empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
let is_unboxed_id id env =
|
|
|
|
try Some (Ident.find_same id env.unboxed_ids)
|
|
|
|
with Not_found -> None
|
|
|
|
|
|
|
|
let add_unboxed_id id unboxed_id bn env =
|
|
|
|
{
|
|
|
|
unboxed_ids = Ident.add id (unboxed_id, bn) env.unboxed_ids;
|
|
|
|
}
|
|
|
|
|
2015-08-25 09:18:49 -07:00
|
|
|
|
2000-02-28 07:45:50 -08:00
|
|
|
(* Big arrays *)
|
|
|
|
|
2002-02-10 09:01:27 -08:00
|
|
|
let bigarray_elt_size = function
|
|
|
|
Pbigarray_unknown -> assert false
|
|
|
|
| Pbigarray_float32 -> 4
|
|
|
|
| Pbigarray_float64 -> 8
|
|
|
|
| Pbigarray_sint8 -> 1
|
|
|
|
| Pbigarray_uint8 -> 1
|
|
|
|
| Pbigarray_sint16 -> 2
|
|
|
|
| Pbigarray_uint16 -> 2
|
|
|
|
| Pbigarray_int32 -> 4
|
|
|
|
| Pbigarray_int64 -> 8
|
|
|
|
| Pbigarray_caml_int -> size_int
|
|
|
|
| Pbigarray_native_int -> size_int
|
|
|
|
| Pbigarray_complex32 -> 8
|
|
|
|
| Pbigarray_complex64 -> 16
|
|
|
|
|
2015-04-26 13:41:50 -07:00
|
|
|
(* Produces a pointer to the element of the bigarray [b] on the position
|
|
|
|
[args]. [args] is given as a list of tagged int expressions, one per array
|
|
|
|
dimension. *)
|
2008-07-14 02:09:53 -07:00
|
|
|
let bigarray_indexing unsafe elt_kind layout b args dbg =
|
2015-04-26 13:41:50 -07:00
|
|
|
let check_ba_bound bound idx v =
|
|
|
|
Csequence(make_checkbound dbg [bound;idx], v) in
|
|
|
|
(* Validates the given multidimensional offset against the array bounds and
|
|
|
|
transforms it into a one dimensional offset. The offsets are expressions
|
|
|
|
evaluating to tagged int. *)
|
2000-02-28 07:45:50 -08:00
|
|
|
let rec ba_indexing dim_ofs delta_ofs = function
|
|
|
|
[] -> assert false
|
|
|
|
| [arg] ->
|
2015-04-26 13:41:50 -07:00
|
|
|
if unsafe then arg
|
|
|
|
else
|
|
|
|
bind "idx" arg (fun idx ->
|
|
|
|
(* Load the untagged int bound for the given dimension *)
|
2015-07-27 01:18:53 -07:00
|
|
|
let bound = Cop(Cload Word_int,[field_address b dim_ofs]) in
|
2015-04-26 13:41:50 -07:00
|
|
|
let idxn = untag_int idx in
|
|
|
|
check_ba_bound bound idxn idx)
|
2000-02-28 07:45:50 -08:00
|
|
|
| arg1 :: argl ->
|
2015-04-26 13:41:50 -07:00
|
|
|
(* The remainder of the list is transformed into a one dimensional offset
|
|
|
|
*)
|
2000-02-28 07:45:50 -08:00
|
|
|
let rem = ba_indexing (dim_ofs + delta_ofs) delta_ofs argl in
|
2015-04-26 13:41:50 -07:00
|
|
|
(* Load the untagged int bound for the given dimension *)
|
2015-07-27 01:18:53 -07:00
|
|
|
let bound = Cop(Cload Word_int, [field_address b dim_ofs]) in
|
2015-04-26 13:41:50 -07:00
|
|
|
if unsafe then add_int (mul_int (decr_int rem) bound) arg1
|
|
|
|
else
|
|
|
|
bind "idx" arg1 (fun idx ->
|
|
|
|
bind "bound" bound (fun bound ->
|
|
|
|
let idxn = untag_int idx in
|
|
|
|
(* [offset = rem * (tag_int bound) + idx] *)
|
|
|
|
let offset = add_int (mul_int (decr_int rem) bound) idx in
|
|
|
|
check_ba_bound bound idxn offset)) in
|
|
|
|
(* The offset as an expression evaluating to int *)
|
2000-02-28 07:45:50 -08:00
|
|
|
let offset =
|
|
|
|
match layout with
|
|
|
|
Pbigarray_unknown_layout ->
|
|
|
|
assert false
|
|
|
|
| Pbigarray_c_layout ->
|
|
|
|
ba_indexing (4 + List.length args) (-1) (List.rev args)
|
|
|
|
| Pbigarray_fortran_layout ->
|
|
|
|
ba_indexing 5 1 (List.map (fun idx -> sub_int idx (Cconst_int 2)) args)
|
|
|
|
and elt_size =
|
2002-02-10 09:01:27 -08:00
|
|
|
bigarray_elt_size elt_kind in
|
2015-04-26 13:41:50 -07:00
|
|
|
(* [array_indexing] can simplify the given expressions *)
|
2015-07-27 01:18:53 -07:00
|
|
|
array_indexing ~typ:Int (log2 elt_size)
|
|
|
|
(Cop(Cload Word_int, [field_address b 1])) offset
|
2000-02-28 07:45:50 -08:00
|
|
|
|
|
|
|
let bigarray_word_kind = function
|
|
|
|
Pbigarray_unknown -> assert false
|
|
|
|
| Pbigarray_float32 -> Single
|
|
|
|
| Pbigarray_float64 -> Double
|
|
|
|
| Pbigarray_sint8 -> Byte_signed
|
|
|
|
| Pbigarray_uint8 -> Byte_unsigned
|
|
|
|
| Pbigarray_sint16 -> Sixteen_signed
|
|
|
|
| Pbigarray_uint16 -> Sixteen_unsigned
|
|
|
|
| Pbigarray_int32 -> Thirtytwo_signed
|
2014-11-06 00:54:14 -08:00
|
|
|
| Pbigarray_int64 -> Word_int
|
|
|
|
| Pbigarray_caml_int -> Word_int
|
|
|
|
| Pbigarray_native_int -> Word_int
|
2002-02-10 09:01:27 -08:00
|
|
|
| Pbigarray_complex32 -> Single
|
|
|
|
| Pbigarray_complex64 -> Double
|
2000-02-28 07:45:50 -08:00
|
|
|
|
2008-07-14 02:09:53 -07:00
|
|
|
let bigarray_get unsafe elt_kind layout b args dbg =
|
2011-07-20 02:17:07 -07:00
|
|
|
bind "ba" b (fun b ->
|
|
|
|
match elt_kind with
|
|
|
|
Pbigarray_complex32 | Pbigarray_complex64 ->
|
|
|
|
let kind = bigarray_word_kind elt_kind in
|
|
|
|
let sz = bigarray_elt_size elt_kind / 2 in
|
2013-03-09 14:38:52 -08:00
|
|
|
bind "addr" (bigarray_indexing unsafe elt_kind layout b args dbg)
|
|
|
|
(fun addr ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_complex dbg
|
2011-07-20 02:17:07 -07:00
|
|
|
(Cop(Cload kind, [addr]))
|
|
|
|
(Cop(Cload kind, [Cop(Cadda, [addr; Cconst_int sz])])))
|
|
|
|
| _ ->
|
|
|
|
Cop(Cload (bigarray_word_kind elt_kind),
|
|
|
|
[bigarray_indexing unsafe elt_kind layout b args dbg]))
|
2000-02-28 07:45:50 -08:00
|
|
|
|
2008-07-14 02:09:53 -07:00
|
|
|
let bigarray_set unsafe elt_kind layout b args newval dbg =
|
2011-07-20 02:17:07 -07:00
|
|
|
bind "ba" b (fun b ->
|
|
|
|
match elt_kind with
|
|
|
|
Pbigarray_complex32 | Pbigarray_complex64 ->
|
|
|
|
let kind = bigarray_word_kind elt_kind in
|
|
|
|
let sz = bigarray_elt_size elt_kind / 2 in
|
|
|
|
bind "newval" newval (fun newv ->
|
2013-03-09 14:38:52 -08:00
|
|
|
bind "addr" (bigarray_indexing unsafe elt_kind layout b args dbg)
|
|
|
|
(fun addr ->
|
2011-07-20 02:17:07 -07:00
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (kind, Assignment), [addr; complex_re newv]),
|
|
|
|
Cop(Cstore (kind, Assignment),
|
2011-07-20 02:17:07 -07:00
|
|
|
[Cop(Cadda, [addr; Cconst_int sz]); complex_im newv]))))
|
|
|
|
| _ ->
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (bigarray_word_kind elt_kind, Assignment),
|
2011-07-20 02:17:07 -07:00
|
|
|
[bigarray_indexing unsafe elt_kind layout b args dbg; newval]))
|
2000-02-28 07:45:50 -08:00
|
|
|
|
2012-11-09 08:15:29 -08:00
|
|
|
let unaligned_load_16 ptr idx =
|
|
|
|
if Arch.allow_unaligned_access
|
|
|
|
then Cop(Cload Sixteen_unsigned, [add_int ptr idx])
|
|
|
|
else
|
|
|
|
let v1 = Cop(Cload Byte_unsigned, [add_int ptr idx]) in
|
|
|
|
let v2 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 1)]) in
|
|
|
|
let b1, b2 = if Arch.big_endian then v1, v2 else v2, v1 in
|
|
|
|
Cop(Cor, [lsl_int b1 (Cconst_int 8); b2])
|
|
|
|
|
|
|
|
let unaligned_set_16 ptr idx newval =
|
|
|
|
if Arch.allow_unaligned_access
|
2015-12-18 08:42:40 -08:00
|
|
|
then Cop(Cstore (Sixteen_unsigned, Assignment), [add_int ptr idx; newval])
|
2012-11-09 08:15:29 -08:00
|
|
|
else
|
|
|
|
let v1 = Cop(Cand, [Cop(Clsr, [newval; Cconst_int 8]); Cconst_int 0xFF]) in
|
|
|
|
let v2 = Cop(Cand, [newval; Cconst_int 0xFF]) in
|
|
|
|
let b1, b2 = if Arch.big_endian then v1, v2 else v2, v1 in
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment), [add_int ptr idx; b1]),
|
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 1); b2]))
|
|
|
|
|
|
|
|
let unaligned_load_32 ptr idx =
|
|
|
|
if Arch.allow_unaligned_access
|
|
|
|
then Cop(Cload Thirtytwo_unsigned, [add_int ptr idx])
|
|
|
|
else
|
|
|
|
let v1 = Cop(Cload Byte_unsigned, [add_int ptr idx]) in
|
|
|
|
let v2 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 1)]) in
|
|
|
|
let v3 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 2)]) in
|
|
|
|
let v4 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 3)]) in
|
|
|
|
let b1, b2, b3, b4 =
|
|
|
|
if Arch.big_endian
|
|
|
|
then v1, v2, v3, v4
|
|
|
|
else v4, v3, v2, v1 in
|
|
|
|
Cop(Cor,
|
|
|
|
[Cop(Cor, [lsl_int b1 (Cconst_int 24); lsl_int b2 (Cconst_int 16)]);
|
|
|
|
Cop(Cor, [lsl_int b3 (Cconst_int 8); b4])])
|
|
|
|
|
|
|
|
let unaligned_set_32 ptr idx newval =
|
|
|
|
if Arch.allow_unaligned_access
|
2015-12-18 08:42:40 -08:00
|
|
|
then Cop(Cstore (Thirtytwo_unsigned, Assignment), [add_int ptr idx; newval])
|
2012-11-09 08:15:29 -08:00
|
|
|
else
|
|
|
|
let v1 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int 24]); Cconst_int 0xFF]) in
|
|
|
|
let v2 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int 16]); Cconst_int 0xFF]) in
|
|
|
|
let v3 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int 8]); Cconst_int 0xFF]) in
|
|
|
|
let v4 = Cop(Cand, [newval; Cconst_int 0xFF]) in
|
|
|
|
let b1, b2, b3, b4 =
|
|
|
|
if Arch.big_endian
|
|
|
|
then v1, v2, v3, v4
|
|
|
|
else v4, v3, v2, v1 in
|
|
|
|
Csequence(
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment), [add_int ptr idx; b1]),
|
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 1); b2])),
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 2); b3]),
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 3); b4])))
|
|
|
|
|
|
|
|
let unaligned_load_64 ptr idx =
|
|
|
|
assert(size_int = 8);
|
|
|
|
if Arch.allow_unaligned_access
|
2014-11-06 00:54:14 -08:00
|
|
|
then Cop(Cload Word_int, [add_int ptr idx])
|
2012-11-09 08:15:29 -08:00
|
|
|
else
|
|
|
|
let v1 = Cop(Cload Byte_unsigned, [add_int ptr idx]) in
|
|
|
|
let v2 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 1)]) in
|
|
|
|
let v3 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 2)]) in
|
|
|
|
let v4 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 3)]) in
|
|
|
|
let v5 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 4)]) in
|
|
|
|
let v6 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 5)]) in
|
|
|
|
let v7 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 6)]) in
|
|
|
|
let v8 = Cop(Cload Byte_unsigned,
|
|
|
|
[add_int (add_int ptr idx) (Cconst_int 7)]) in
|
|
|
|
let b1, b2, b3, b4, b5, b6, b7, b8 =
|
|
|
|
if Arch.big_endian
|
|
|
|
then v1, v2, v3, v4, v5, v6, v7, v8
|
|
|
|
else v8, v7, v6, v5, v4, v3, v2, v1 in
|
|
|
|
Cop(Cor,
|
|
|
|
[Cop(Cor,
|
|
|
|
[Cop(Cor, [lsl_int b1 (Cconst_int (8*7));
|
|
|
|
lsl_int b2 (Cconst_int (8*6))]);
|
|
|
|
Cop(Cor, [lsl_int b3 (Cconst_int (8*5));
|
|
|
|
lsl_int b4 (Cconst_int (8*4))])]);
|
|
|
|
Cop(Cor,
|
|
|
|
[Cop(Cor, [lsl_int b5 (Cconst_int (8*3));
|
|
|
|
lsl_int b6 (Cconst_int (8*2))]);
|
|
|
|
Cop(Cor, [lsl_int b7 (Cconst_int 8);
|
|
|
|
b8])])])
|
|
|
|
|
|
|
|
let unaligned_set_64 ptr idx newval =
|
|
|
|
assert(size_int = 8);
|
|
|
|
if Arch.allow_unaligned_access
|
2015-12-18 08:42:40 -08:00
|
|
|
then Cop(Cstore (Word_int, Assignment), [add_int ptr idx; newval])
|
2012-11-09 08:15:29 -08:00
|
|
|
else
|
|
|
|
let v1 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*7)]); Cconst_int 0xFF]) in
|
|
|
|
let v2 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*6)]); Cconst_int 0xFF]) in
|
|
|
|
let v3 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*5)]); Cconst_int 0xFF]) in
|
|
|
|
let v4 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*4)]); Cconst_int 0xFF]) in
|
|
|
|
let v5 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*3)]); Cconst_int 0xFF]) in
|
|
|
|
let v6 =
|
|
|
|
Cop(Cand, [Cop(Clsr, [newval; Cconst_int (8*2)]); Cconst_int 0xFF]) in
|
|
|
|
let v7 = Cop(Cand, [Cop(Clsr, [newval; Cconst_int 8]); Cconst_int 0xFF]) in
|
|
|
|
let v8 = Cop(Cand, [newval; Cconst_int 0xFF]) in
|
|
|
|
let b1, b2, b3, b4, b5, b6, b7, b8 =
|
|
|
|
if Arch.big_endian
|
|
|
|
then v1, v2, v3, v4, v5, v6, v7, v8
|
|
|
|
else v8, v7, v6, v5, v4, v3, v2, v1 in
|
|
|
|
Csequence(
|
|
|
|
Csequence(
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment), [add_int ptr idx; b1]),
|
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 1); b2])),
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 2); b3]),
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 3); b4]))),
|
|
|
|
Csequence(
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 4); b5]),
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 5); b6])),
|
|
|
|
Csequence(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 6); b7]),
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2012-11-09 08:15:29 -08:00
|
|
|
[add_int (add_int ptr idx) (Cconst_int 7); b8]))))
|
|
|
|
|
2014-04-07 08:07:35 -07:00
|
|
|
let max_or_zero a =
|
|
|
|
bind "size" a (fun a ->
|
|
|
|
(* equivalent to
|
|
|
|
Cifthenelse(Cop(Ccmpi Cle, [a; Cconst_int 0]), Cconst_int 0, a)
|
|
|
|
|
|
|
|
if a is positive, sign is 0 hence sign_negation is full of 1
|
|
|
|
so sign_negation&a = a
|
|
|
|
if a is negative, sign is full of 1 hence sign_negation is 0
|
|
|
|
so sign_negation&a = 0 *)
|
|
|
|
let sign = Cop(Casr, [a; Cconst_int (size_int * 8 - 1)]) in
|
|
|
|
let sign_negation = Cop(Cxor, [sign; Cconst_int (-1)]) in
|
|
|
|
Cop(Cand, [sign_negation; a]))
|
|
|
|
|
2012-11-09 08:15:29 -08:00
|
|
|
let check_bound unsafe dbg a1 a2 k =
|
2014-04-07 08:07:35 -07:00
|
|
|
if unsafe then k
|
|
|
|
else Csequence(make_checkbound dbg [max_or_zero a1;a2], k)
|
2012-11-09 08:15:29 -08:00
|
|
|
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Simplification of some primitives into C calls *)
|
|
|
|
|
|
|
|
let default_prim name =
|
2015-08-25 09:18:46 -07:00
|
|
|
Primitive.simple ~name ~arity:0(*ignored*) ~alloc:true
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2000-02-28 07:45:50 -08:00
|
|
|
let simplif_primitive_32bits = function
|
2004-01-01 08:42:43 -08:00
|
|
|
Pbintofint Pint64 -> Pccall (default_prim "caml_int64_of_int")
|
|
|
|
| Pintofbint Pint64 -> Pccall (default_prim "caml_int64_to_int")
|
|
|
|
| Pcvtbint(Pint32, Pint64) -> Pccall (default_prim "caml_int64_of_int32")
|
|
|
|
| Pcvtbint(Pint64, Pint32) -> Pccall (default_prim "caml_int64_to_int32")
|
|
|
|
| Pcvtbint(Pnativeint, Pint64) ->
|
|
|
|
Pccall (default_prim "caml_int64_of_nativeint")
|
|
|
|
| Pcvtbint(Pint64, Pnativeint) ->
|
|
|
|
Pccall (default_prim "caml_int64_to_nativeint")
|
|
|
|
| Pnegbint Pint64 -> Pccall (default_prim "caml_int64_neg")
|
|
|
|
| Paddbint Pint64 -> Pccall (default_prim "caml_int64_add")
|
|
|
|
| Psubbint Pint64 -> Pccall (default_prim "caml_int64_sub")
|
|
|
|
| Pmulbint Pint64 -> Pccall (default_prim "caml_int64_mul")
|
2016-10-05 07:42:40 -07:00
|
|
|
| Pdivbint {size=Pint64} -> Pccall (default_prim "caml_int64_div")
|
|
|
|
| Pmodbint {size=Pint64} -> Pccall (default_prim "caml_int64_mod")
|
2004-01-01 08:42:43 -08:00
|
|
|
| Pandbint Pint64 -> Pccall (default_prim "caml_int64_and")
|
|
|
|
| Porbint Pint64 -> Pccall (default_prim "caml_int64_or")
|
|
|
|
| Pxorbint Pint64 -> Pccall (default_prim "caml_int64_xor")
|
|
|
|
| Plslbint Pint64 -> Pccall (default_prim "caml_int64_shift_left")
|
|
|
|
| Plsrbint Pint64 -> Pccall (default_prim "caml_int64_shift_right_unsigned")
|
|
|
|
| Pasrbint Pint64 -> Pccall (default_prim "caml_int64_shift_right")
|
2003-12-31 06:20:40 -08:00
|
|
|
| Pbintcomp(Pint64, Lambda.Ceq) -> Pccall (default_prim "caml_equal")
|
|
|
|
| Pbintcomp(Pint64, Lambda.Cneq) -> Pccall (default_prim "caml_notequal")
|
|
|
|
| Pbintcomp(Pint64, Lambda.Clt) -> Pccall (default_prim "caml_lessthan")
|
|
|
|
| Pbintcomp(Pint64, Lambda.Cgt) -> Pccall (default_prim "caml_greaterthan")
|
|
|
|
| Pbintcomp(Pint64, Lambda.Cle) -> Pccall (default_prim "caml_lessequal")
|
|
|
|
| Pbintcomp(Pint64, Lambda.Cge) -> Pccall (default_prim "caml_greaterequal")
|
2016-03-09 02:40:16 -08:00
|
|
|
| Pbigarrayref(_unsafe, n, Pbigarray_int64, _layout) ->
|
2006-01-27 06:33:42 -08:00
|
|
|
Pccall (default_prim ("caml_ba_get_" ^ string_of_int n))
|
2016-03-09 02:40:16 -08:00
|
|
|
| Pbigarrayset(_unsafe, n, Pbigarray_int64, _layout) ->
|
2006-01-27 06:33:42 -08:00
|
|
|
Pccall (default_prim ("caml_ba_set_" ^ string_of_int n))
|
2012-11-09 08:15:29 -08:00
|
|
|
| Pstring_load_64(_) -> Pccall (default_prim "caml_string_get64")
|
|
|
|
| Pstring_set_64(_) -> Pccall (default_prim "caml_string_set64")
|
|
|
|
| Pbigstring_load_64(_) -> Pccall (default_prim "caml_ba_uint8_get64")
|
|
|
|
| Pbigstring_set_64(_) -> Pccall (default_prim "caml_ba_uint8_set64")
|
2012-11-29 01:55:00 -08:00
|
|
|
| Pbbswap Pint64 -> Pccall (default_prim "caml_int64_bswap")
|
2000-02-21 10:14:56 -08:00
|
|
|
| p -> p
|
|
|
|
|
2000-02-28 07:45:50 -08:00
|
|
|
let simplif_primitive p =
|
|
|
|
match p with
|
2007-02-09 05:31:15 -08:00
|
|
|
| Pduprecord _ ->
|
|
|
|
Pccall (default_prim "caml_obj_dup")
|
2016-03-09 02:40:16 -08:00
|
|
|
| Pbigarrayref(_unsafe, n, Pbigarray_unknown, _layout) ->
|
2006-01-27 06:33:42 -08:00
|
|
|
Pccall (default_prim ("caml_ba_get_" ^ string_of_int n))
|
2016-03-09 02:40:16 -08:00
|
|
|
| Pbigarrayset(_unsafe, n, Pbigarray_unknown, _layout) ->
|
2006-01-27 06:33:42 -08:00
|
|
|
Pccall (default_prim ("caml_ba_set_" ^ string_of_int n))
|
2016-03-09 02:40:16 -08:00
|
|
|
| Pbigarrayref(_unsafe, n, _kind, Pbigarray_unknown_layout) ->
|
2006-01-27 06:33:42 -08:00
|
|
|
Pccall (default_prim ("caml_ba_get_" ^ string_of_int n))
|
2016-03-09 02:40:16 -08:00
|
|
|
| Pbigarrayset(_unsafe, n, _kind, Pbigarray_unknown_layout) ->
|
2006-01-27 06:33:42 -08:00
|
|
|
Pccall (default_prim ("caml_ba_set_" ^ string_of_int n))
|
2000-02-28 07:45:50 -08:00
|
|
|
| p ->
|
|
|
|
if size_int = 8 then p else simplif_primitive_32bits p
|
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
(* Build switchers both for constants and blocks *)
|
|
|
|
|
|
|
|
let transl_isout h arg = tag_int (Cop(Ccmpa Clt, [h ; arg]))
|
|
|
|
|
2014-04-07 08:43:20 -07:00
|
|
|
(* Build an actual switch (ie jump table) *)
|
2000-10-02 07:08:30 -07:00
|
|
|
|
|
|
|
module SArgBlocks =
|
|
|
|
struct
|
|
|
|
type primitive = operation
|
|
|
|
|
|
|
|
let eqint = Ccmpi Ceq
|
|
|
|
let neint = Ccmpi Cne
|
|
|
|
let leint = Ccmpi Cle
|
|
|
|
let ltint = Ccmpi Clt
|
|
|
|
let geint = Ccmpi Cge
|
|
|
|
let gtint = Ccmpi Cgt
|
|
|
|
|
|
|
|
type act = expression
|
|
|
|
|
2014-04-08 02:38:22 -07:00
|
|
|
let make_const i = Cconst_int i
|
2000-10-02 07:08:30 -07:00
|
|
|
let make_prim p args = Cop (p,args)
|
|
|
|
let make_offset arg n = add_const arg n
|
|
|
|
let make_isout h arg = Cop (Ccmpa Clt, [h ; arg])
|
2001-04-30 09:31:09 -07:00
|
|
|
let make_isin h arg = Cop (Ccmpa Cge, [h ; arg])
|
2000-10-02 07:08:30 -07:00
|
|
|
let make_if cond ifso ifnot = Cifthenelse (cond, ifso, ifnot)
|
2014-04-07 08:43:20 -07:00
|
|
|
let make_switch arg cases actions = Cswitch (arg,cases,actions)
|
2000-10-02 07:08:30 -07:00
|
|
|
let bind arg body = bind "switcher" arg body
|
|
|
|
|
2014-04-07 08:43:20 -07:00
|
|
|
let make_catch handler = match handler with
|
|
|
|
| Cexit (i,[]) -> i,fun e -> e
|
|
|
|
| _ ->
|
|
|
|
let i = next_raise_count () in
|
|
|
|
(*
|
|
|
|
Printf.eprintf "SHARE CMM: %i\n" i ;
|
|
|
|
Printcmm.expression Format.str_formatter handler ;
|
|
|
|
Printf.eprintf "%s\n" (Format.flush_str_formatter ()) ;
|
|
|
|
*)
|
|
|
|
i,
|
|
|
|
(fun body -> match body with
|
|
|
|
| Cexit (j,_) ->
|
|
|
|
if i=j then handler
|
|
|
|
else body
|
|
|
|
| _ -> Ccatch (i,[],body,handler))
|
|
|
|
|
|
|
|
let make_exit i = Cexit (i,[])
|
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
end
|
|
|
|
|
2014-04-07 08:43:20 -07:00
|
|
|
(* cmm store, as sharing as normally been detected in previous
|
|
|
|
phases, we only share exits *)
|
|
|
|
module StoreExp =
|
|
|
|
Switch.Store
|
|
|
|
(struct
|
|
|
|
type t = expression
|
|
|
|
type key = int
|
|
|
|
let make_key = function
|
|
|
|
| Cexit (i,[]) -> Some i
|
|
|
|
| _ -> None
|
|
|
|
end)
|
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
module SwitcherBlocks = Switch.Make(SArgBlocks)
|
|
|
|
|
2014-03-21 10:02:44 -07:00
|
|
|
(* Int switcher, arg in [low..high],
|
|
|
|
cases is list of individual cases, and is sorted by first component *)
|
|
|
|
|
|
|
|
let transl_int_switch arg low high cases default = match cases with
|
|
|
|
| [] -> assert false
|
2014-04-07 08:43:20 -07:00
|
|
|
| _::_ ->
|
|
|
|
let store = StoreExp.mk_store () in
|
|
|
|
assert (store.Switch.act_store default = 0) ;
|
|
|
|
let cases =
|
|
|
|
List.map
|
|
|
|
(fun (i,act) -> i,store.Switch.act_store act)
|
|
|
|
cases in
|
|
|
|
let rec inters plow phigh pact = function
|
|
|
|
| [] ->
|
|
|
|
if phigh = high then [plow,phigh,pact]
|
|
|
|
else [(plow,phigh,pact); (phigh+1,high,0) ]
|
|
|
|
| (i,act)::rem ->
|
|
|
|
if i = phigh+1 then
|
|
|
|
if pact = act then
|
|
|
|
inters plow i pact rem
|
|
|
|
else
|
|
|
|
(plow,phigh,pact)::inters i i act rem
|
|
|
|
else (* insert default *)
|
|
|
|
if pact = 0 then
|
|
|
|
if act = 0 then
|
|
|
|
inters plow i 0 rem
|
2014-04-12 03:17:02 -07:00
|
|
|
else
|
2014-04-07 08:43:20 -07:00
|
|
|
(plow,i-1,pact)::
|
|
|
|
inters i i act rem
|
|
|
|
else (* pact <> 0 *)
|
|
|
|
(plow,phigh,pact)::
|
|
|
|
begin
|
|
|
|
if act = 0 then inters (phigh+1) i 0 rem
|
|
|
|
else (phigh+1,i-1,0)::inters i i act rem
|
|
|
|
end in
|
|
|
|
let inters = match cases with
|
|
|
|
| [] -> assert false
|
|
|
|
| (k0,act0)::rem ->
|
|
|
|
if k0 = low then inters k0 k0 act0 rem
|
|
|
|
else inters low (k0-1) 0 cases in
|
|
|
|
bind "switcher" arg
|
|
|
|
(fun a ->
|
|
|
|
SwitcherBlocks.zyva
|
|
|
|
(low,high)
|
|
|
|
a
|
|
|
|
(Array.of_list inters) store)
|
2014-03-21 10:02:44 -07:00
|
|
|
|
|
|
|
|
2001-01-13 03:55:43 -08:00
|
|
|
(* Auxiliary functions for optimizing "let" of boxed numbers (floats and
|
|
|
|
boxed integers *)
|
|
|
|
|
|
|
|
type unboxed_number_kind =
|
|
|
|
No_unboxing
|
2016-04-29 05:57:42 -07:00
|
|
|
| Boxed of boxed_number * bool (* true: boxed form available at no cost *)
|
2015-07-27 02:40:27 -07:00
|
|
|
| No_result (* expression never returns a result *)
|
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
let unboxed_number_kind_of_unbox dbg = function
|
2015-08-25 09:18:46 -07:00
|
|
|
| Same_as_ocaml_repr -> No_unboxing
|
2016-06-27 00:49:26 -07:00
|
|
|
| Unboxed_float -> Boxed (Boxed_float dbg, false)
|
|
|
|
| Unboxed_integer bi -> Boxed (Boxed_integer (bi, dbg), false)
|
2015-08-25 09:18:46 -07:00
|
|
|
| Untagged_int -> No_unboxing
|
|
|
|
|
2015-12-07 03:38:06 -08:00
|
|
|
let rec is_unboxed_number ~strict env e =
|
2015-07-27 02:40:27 -07:00
|
|
|
(* Given unboxed_number_kind from two branches of the code, returns the
|
2015-12-07 03:38:06 -08:00
|
|
|
resulting unboxed_number_kind.
|
|
|
|
|
|
|
|
If [strict=false], one knows that the type of the expression
|
|
|
|
is an unboxable number, and we decide to return an unboxed value
|
|
|
|
if this indeed eliminates at least one allocation.
|
|
|
|
|
|
|
|
If [strict=true], we need to ensure that all possible branches
|
|
|
|
return an unboxable number (of the same kind). This could not
|
|
|
|
be the case in presence of GADTs.
|
|
|
|
*)
|
2015-07-27 02:40:27 -07:00
|
|
|
let join k1 e =
|
2015-12-07 03:38:06 -08:00
|
|
|
match k1, is_unboxed_number ~strict env e with
|
2016-06-27 00:49:26 -07:00
|
|
|
| Boxed (b1, c1), Boxed (b2, c2) when equal_boxed_number b1 b2 ->
|
|
|
|
Boxed (b1, c1 && c2)
|
2015-08-25 09:18:46 -07:00
|
|
|
| No_result, k | k, No_result ->
|
|
|
|
k (* if a branch never returns, it is safe to unbox it *)
|
2015-12-07 03:38:06 -08:00
|
|
|
| No_unboxing, k | k, No_unboxing when not strict ->
|
|
|
|
k
|
2015-07-27 02:40:27 -07:00
|
|
|
| _, _ -> No_unboxing
|
|
|
|
in
|
|
|
|
match e with
|
2015-10-20 08:11:11 -07:00
|
|
|
| Uvar id ->
|
|
|
|
begin match is_unboxed_id id env with
|
|
|
|
| None -> No_unboxing
|
2016-04-29 05:57:42 -07:00
|
|
|
| Some (_, bn) -> Boxed (bn, false)
|
2015-10-20 08:11:11 -07:00
|
|
|
end
|
|
|
|
|
2016-01-04 07:02:35 -08:00
|
|
|
| Uconst(Uconst_ref(_, Some (Uconst_float _))) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_float Debuginfo.none, true)
|
2016-01-04 07:02:35 -08:00
|
|
|
| Uconst(Uconst_ref(_, Some (Uconst_int32 _))) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_integer (Pint32, Debuginfo.none), true)
|
2016-01-04 07:02:35 -08:00
|
|
|
| Uconst(Uconst_ref(_, Some (Uconst_int64 _))) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_integer (Pint64, Debuginfo.none), true)
|
2016-01-04 07:02:35 -08:00
|
|
|
| Uconst(Uconst_ref(_, Some (Uconst_nativeint _))) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_integer (Pnativeint, Debuginfo.none), true)
|
|
|
|
| Uprim(p, _, dbg) ->
|
2001-01-13 03:55:43 -08:00
|
|
|
begin match simplif_primitive p with
|
2016-06-27 00:49:26 -07:00
|
|
|
| Pccall p -> unboxed_number_kind_of_unbox dbg p.prim_native_repr_res
|
2016-04-29 05:57:42 -07:00
|
|
|
| Pfloatfield _
|
|
|
|
| Pfloatofint
|
|
|
|
| Pnegfloat
|
|
|
|
| Pabsfloat
|
|
|
|
| Paddfloat
|
|
|
|
| Psubfloat
|
|
|
|
| Pmulfloat
|
|
|
|
| Pdivfloat
|
|
|
|
| Parrayrefu Pfloatarray
|
2016-06-27 00:49:26 -07:00
|
|
|
| Parrayrefs Pfloatarray -> Boxed (Boxed_float dbg, false)
|
2016-04-29 05:57:42 -07:00
|
|
|
| Pbintofint bi
|
|
|
|
| Pcvtbint(_, bi)
|
|
|
|
| Pnegbint bi
|
|
|
|
| Paddbint bi
|
|
|
|
| Psubbint bi
|
|
|
|
| Pmulbint bi
|
2016-10-05 07:42:40 -07:00
|
|
|
| Pdivbint {size=bi}
|
|
|
|
| Pmodbint {size=bi}
|
2016-04-29 05:57:42 -07:00
|
|
|
| Pandbint bi
|
|
|
|
| Porbint bi
|
|
|
|
| Pxorbint bi
|
|
|
|
| Plslbint bi
|
|
|
|
| Plsrbint bi
|
|
|
|
| Pasrbint bi
|
2016-06-27 00:49:26 -07:00
|
|
|
| Pbbswap bi -> Boxed (Boxed_integer (bi, dbg), false)
|
2008-07-14 02:09:53 -07:00
|
|
|
| Pbigarrayref(_, _, (Pbigarray_float32 | Pbigarray_float64), _) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_float dbg, false)
|
|
|
|
| Pbigarrayref(_, _, Pbigarray_int32, _) ->
|
|
|
|
Boxed (Boxed_integer (Pint32, dbg), false)
|
|
|
|
| Pbigarrayref(_, _, Pbigarray_int64, _) ->
|
|
|
|
Boxed (Boxed_integer (Pint64, dbg), false)
|
2015-09-11 04:58:31 -07:00
|
|
|
| Pbigarrayref(_, _, Pbigarray_native_int,_) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_integer (Pnativeint, dbg), false)
|
|
|
|
| Pstring_load_32(_) -> Boxed (Boxed_integer (Pint32, dbg), false)
|
|
|
|
| Pstring_load_64(_) -> Boxed (Boxed_integer (Pint64, dbg), false)
|
|
|
|
| Pbigstring_load_32(_) -> Boxed (Boxed_integer (Pint32, dbg), false)
|
|
|
|
| Pbigstring_load_64(_) -> Boxed (Boxed_integer (Pint64, dbg), false)
|
2015-07-27 02:40:27 -07:00
|
|
|
| Praise _ -> No_result
|
2001-01-13 03:55:43 -08:00
|
|
|
| _ -> No_unboxing
|
|
|
|
end
|
2015-12-07 03:32:03 -08:00
|
|
|
| Ulet (_, _, _, _, e) | Uletrec (_, e) | Usequence (_, e) ->
|
2015-12-07 03:38:06 -08:00
|
|
|
is_unboxed_number ~strict env e
|
2015-07-27 02:40:27 -07:00
|
|
|
| Uswitch (_, switch) ->
|
|
|
|
let k = Array.fold_left join No_result switch.us_actions_consts in
|
|
|
|
Array.fold_left join k switch.us_actions_blocks
|
|
|
|
| Ustringswitch (_, actions, default_opt) ->
|
|
|
|
let k = List.fold_left (fun k (_, e) -> join k e) No_result actions in
|
|
|
|
begin match default_opt with
|
|
|
|
None -> k
|
|
|
|
| Some default -> join k default
|
|
|
|
end
|
|
|
|
| Ustaticfail _ -> No_result
|
|
|
|
| Uifthenelse (_, e1, e2) | Ucatch (_, _, e1, e2) | Utrywith (e1, _, e2) ->
|
2015-12-07 03:38:06 -08:00
|
|
|
join (is_unboxed_number ~strict env e1) e2
|
2001-01-13 03:55:43 -08:00
|
|
|
| _ -> No_unboxing
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Translate an expression *)
|
|
|
|
|
2012-02-21 09:41:02 -08:00
|
|
|
let functions = (Queue.create() : ufunction Queue.t)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2014-03-21 10:02:44 -07:00
|
|
|
let strmatch_compile =
|
|
|
|
let module S =
|
|
|
|
Strmatch.Make
|
|
|
|
(struct
|
2014-03-27 07:33:28 -07:00
|
|
|
let string_block_length = get_size
|
2014-03-21 10:02:44 -07:00
|
|
|
let transl_switch = transl_int_switch
|
|
|
|
end) in
|
|
|
|
S.compile
|
2014-04-12 03:17:02 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
let rec transl env e =
|
|
|
|
match e with
|
1995-07-02 09:41:48 -07:00
|
|
|
Uvar id ->
|
2015-10-20 08:11:11 -07:00
|
|
|
begin match is_unboxed_id id env with
|
|
|
|
| None -> Cvar id
|
|
|
|
| Some (unboxed_id, bn) -> box_number bn (Cvar unboxed_id)
|
|
|
|
end
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst sc ->
|
1995-07-02 09:41:48 -07:00
|
|
|
transl_constant sc
|
1999-01-27 02:52:33 -08:00
|
|
|
| Uclosure(fundecls, []) ->
|
2011-03-29 00:58:53 -07:00
|
|
|
let lbl = Compilenv.new_const_symbol() in
|
2016-02-10 09:28:38 -08:00
|
|
|
constant_closures :=
|
|
|
|
((lbl, Not_global), fundecls, []) :: !constant_closures;
|
2012-02-21 09:41:02 -08:00
|
|
|
List.iter (fun f -> Queue.add f functions) fundecls;
|
1999-01-27 02:52:33 -08:00
|
|
|
Cconst_symbol lbl
|
1995-07-07 05:07:07 -07:00
|
|
|
| Uclosure(fundecls, clos_vars) ->
|
|
|
|
let block_size =
|
|
|
|
fundecls_size fundecls + List.length clos_vars in
|
|
|
|
let rec transl_fundecls pos = function
|
2000-10-02 07:08:30 -07:00
|
|
|
[] ->
|
2015-10-20 08:11:11 -07:00
|
|
|
List.map (transl env) clos_vars
|
2012-02-21 09:41:02 -08:00
|
|
|
| f :: rem ->
|
|
|
|
Queue.add f functions;
|
2000-10-02 07:08:30 -07:00
|
|
|
let header =
|
|
|
|
if pos = 0
|
2016-06-27 00:49:26 -07:00
|
|
|
then alloc_closure_header block_size f.dbg
|
|
|
|
else alloc_infix_header pos f.dbg in
|
2016-01-06 10:20:49 -08:00
|
|
|
if f.arity = 1 || f.arity = 0 then
|
2000-10-02 07:08:30 -07:00
|
|
|
header ::
|
2012-02-21 09:41:02 -08:00
|
|
|
Cconst_symbol f.label ::
|
2016-01-06 10:20:49 -08:00
|
|
|
int_const f.arity ::
|
2000-10-02 07:08:30 -07:00
|
|
|
transl_fundecls (pos + 3) rem
|
|
|
|
else
|
|
|
|
header ::
|
2012-02-21 09:41:02 -08:00
|
|
|
Cconst_symbol(curry_function f.arity) ::
|
|
|
|
int_const f.arity ::
|
|
|
|
Cconst_symbol f.label ::
|
2000-10-02 07:08:30 -07:00
|
|
|
transl_fundecls (pos + 4) rem in
|
2016-06-27 00:49:26 -07:00
|
|
|
Cop(Calloc Debuginfo.none, transl_fundecls 0 fundecls)
|
1995-07-07 05:07:07 -07:00
|
|
|
| Uoffset(arg, offset) ->
|
2014-11-06 00:54:14 -08:00
|
|
|
(* produces a valid Caml value, pointing just after an infix header *)
|
2015-10-20 08:11:11 -07:00
|
|
|
let ptr = transl env arg in
|
2014-11-06 00:54:14 -08:00
|
|
|
if offset = 0
|
|
|
|
then ptr
|
|
|
|
else Cop(Caddv, [ptr; Cconst_int(offset * size_addr)])
|
2007-01-29 04:11:18 -08:00
|
|
|
| Udirect_apply(lbl, args, dbg) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Capply(typ_val, dbg), Cconst_symbol lbl :: List.map (transl env) args)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Ugeneric_apply(clos, [arg], dbg) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "fun" (transl env clos) (fun clos ->
|
|
|
|
Cop(Capply(typ_val, dbg), [get_field clos 0; transl env arg; clos]))
|
2007-01-29 04:11:18 -08:00
|
|
|
| Ugeneric_apply(clos, args, dbg) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let arity = List.length args in
|
1998-04-06 02:07:57 -07:00
|
|
|
let cargs = Cconst_symbol(apply_function arity) ::
|
2015-10-20 08:11:11 -07:00
|
|
|
List.map (transl env) (args @ [clos]) in
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, dbg), cargs)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Usend(kind, met, obj, args, dbg) ->
|
2004-05-26 04:10:52 -07:00
|
|
|
let call_met obj args clos =
|
2007-02-09 05:31:15 -08:00
|
|
|
if args = [] then
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, dbg), [get_field clos 0;obj;clos])
|
2007-01-29 04:11:18 -08:00
|
|
|
else
|
|
|
|
let arity = List.length args + 1 in
|
|
|
|
let cargs = Cconst_symbol(apply_function arity) :: obj ::
|
2015-10-20 08:11:11 -07:00
|
|
|
(List.map (transl env) args) @ [clos] in
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, dbg), cargs)
|
2004-05-26 04:10:52 -07:00
|
|
|
in
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "obj" (transl env obj) (fun obj ->
|
2006-01-04 08:55:50 -08:00
|
|
|
match kind, args with
|
|
|
|
Self, _ ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "met" (lookup_label obj (transl env met)) (call_met obj args)
|
2006-01-04 08:55:50 -08:00
|
|
|
| Cached, cache :: pos :: args ->
|
2015-10-20 08:11:11 -07:00
|
|
|
call_cached_method obj
|
|
|
|
(transl env met) (transl env cache) (transl env pos)
|
|
|
|
(List.map (transl env) args) dbg
|
2006-01-04 08:55:50 -08:00
|
|
|
| _ ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "met" (lookup_tag obj (transl env met)) (call_met obj args))
|
2015-12-07 03:32:03 -08:00
|
|
|
| Ulet(str, kind, id, exp, body) ->
|
|
|
|
transl_let env str kind id exp body
|
1995-07-02 09:41:48 -07:00
|
|
|
| Uletrec(bindings, body) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_letrec env bindings (transl env body)
|
1995-07-27 10:38:53 -07:00
|
|
|
|
|
|
|
(* Primitives *)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(prim, args, dbg) ->
|
2000-02-21 10:14:56 -08:00
|
|
|
begin match (simplif_primitive prim, args) with
|
|
|
|
(Pgetglobal id, []) ->
|
2006-01-04 08:55:50 -08:00
|
|
|
Cconst_symbol (Ident.name id)
|
2016-03-09 02:40:16 -08:00
|
|
|
| (Pmakeblock _, []) ->
|
2014-03-06 09:03:16 -08:00
|
|
|
assert false
|
2015-12-07 02:54:20 -08:00
|
|
|
| (Pmakeblock(tag, _mut, _kind), args) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
make_alloc dbg tag (List.map (transl env) args)
|
2000-02-21 10:14:56 -08:00
|
|
|
| (Pccall prim, args) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_ccall env prim args dbg
|
2016-01-15 06:49:01 -08:00
|
|
|
| (Pduparray (kind, _), [Uprim (Pmakearray (kind', _), args, _dbg)]) ->
|
|
|
|
(* We arrive here in two cases:
|
|
|
|
1. When using Closure, all the time.
|
|
|
|
2. When using Flambda, if a float array longer than
|
|
|
|
[Translcore.use_dup_for_constant_arrays_bigger_than] turns out
|
2016-01-15 06:54:33 -08:00
|
|
|
to be non-constant.
|
|
|
|
If for some reason Flambda fails to lift a constant array we
|
|
|
|
could in theory also end up here.
|
|
|
|
Note that [kind] above is unconstrained, but with the current
|
|
|
|
state of [Translcore], we will in fact only get here with
|
|
|
|
[Pfloatarray]s. *)
|
2016-01-15 06:49:01 -08:00
|
|
|
assert (kind = kind');
|
2016-06-27 00:49:26 -07:00
|
|
|
transl_make_array dbg env kind args
|
2016-01-12 10:51:06 -08:00
|
|
|
| (Pduparray _, [arg]) ->
|
|
|
|
let prim_obj_dup =
|
|
|
|
Primitive.simple ~name:"caml_obj_dup" ~arity:1 ~alloc:true
|
|
|
|
in
|
|
|
|
transl_ccall env prim_obj_dup [arg] dbg
|
2016-03-09 02:40:16 -08:00
|
|
|
| (Pmakearray _, []) ->
|
2014-03-06 09:03:16 -08:00
|
|
|
transl_structured_constant (Uconst_block(0, []))
|
2016-06-27 00:49:26 -07:00
|
|
|
| (Pmakearray (kind, _), args) -> transl_make_array dbg env kind args
|
2016-03-09 02:40:16 -08:00
|
|
|
| (Pbigarrayref(unsafe, _num_dims, elt_kind, layout), arg1 :: argl) ->
|
2000-02-28 07:45:50 -08:00
|
|
|
let elt =
|
2008-07-14 02:09:53 -07:00
|
|
|
bigarray_get unsafe elt_kind layout
|
2015-10-20 08:11:11 -07:00
|
|
|
(transl env arg1) (List.map (transl env) argl) dbg in
|
2000-02-28 07:45:50 -08:00
|
|
|
begin match elt_kind with
|
2016-06-27 00:49:26 -07:00
|
|
|
Pbigarray_float32 | Pbigarray_float64 -> box_float dbg elt
|
2002-02-10 09:01:27 -08:00
|
|
|
| Pbigarray_complex32 | Pbigarray_complex64 -> elt
|
2016-06-27 00:49:26 -07:00
|
|
|
| Pbigarray_int32 -> box_int dbg Pint32 elt
|
|
|
|
| Pbigarray_int64 -> box_int dbg Pint64 elt
|
|
|
|
| Pbigarray_native_int -> box_int dbg Pnativeint elt
|
2001-02-05 00:48:13 -08:00
|
|
|
| Pbigarray_caml_int -> force_tag_int elt
|
2000-02-28 07:45:50 -08:00
|
|
|
| _ -> tag_int elt
|
|
|
|
end
|
2016-03-09 02:40:16 -08:00
|
|
|
| (Pbigarrayset(unsafe, _num_dims, elt_kind, layout), arg1 :: argl) ->
|
2000-02-28 07:45:50 -08:00
|
|
|
let (argidx, argnewval) = split_last argl in
|
2008-07-14 02:09:53 -07:00
|
|
|
return_unit(bigarray_set unsafe elt_kind layout
|
2015-10-20 08:11:11 -07:00
|
|
|
(transl env arg1)
|
|
|
|
(List.map (transl env) argidx)
|
2000-02-28 07:45:50 -08:00
|
|
|
(match elt_kind with
|
2000-10-02 07:08:30 -07:00
|
|
|
Pbigarray_float32 | Pbigarray_float64 ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_unbox_float env argnewval
|
|
|
|
| Pbigarray_complex32 | Pbigarray_complex64 -> transl env argnewval
|
|
|
|
| Pbigarray_int32 -> transl_unbox_int env Pint32 argnewval
|
|
|
|
| Pbigarray_int64 -> transl_unbox_int env Pint64 argnewval
|
|
|
|
| Pbigarray_native_int -> transl_unbox_int env Pnativeint argnewval
|
|
|
|
| _ -> untag_int (transl env argnewval))
|
2007-01-29 04:11:18 -08:00
|
|
|
dbg)
|
2012-11-06 09:03:34 -08:00
|
|
|
| (Pbigarraydim(n), [b]) ->
|
|
|
|
let dim_ofs = 4 + n in
|
2015-10-20 08:11:11 -07:00
|
|
|
tag_int (Cop(Cload Word_int, [field_address (transl env b) dim_ofs]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| (p, [arg]) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_prim_1 env p arg dbg
|
2000-02-21 10:14:56 -08:00
|
|
|
| (p, [arg1; arg2]) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_prim_2 env p arg1 arg2 dbg
|
2000-02-21 10:14:56 -08:00
|
|
|
| (p, [arg1; arg2; arg3]) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_prim_3 env p arg1 arg2 arg3 dbg
|
2000-02-21 10:14:56 -08:00
|
|
|
| (_, _) ->
|
|
|
|
fatal_error "Cmmgen.transl:prim"
|
|
|
|
end
|
|
|
|
|
|
|
|
(* Control structures *)
|
|
|
|
| Uswitch(arg, s) ->
|
|
|
|
(* As in the bytecode interpreter, only matching against constants
|
|
|
|
can be checked *)
|
|
|
|
if Array.length s.us_index_blocks = 0 then
|
2001-02-19 12:15:42 -08:00
|
|
|
Cswitch
|
2015-10-20 08:11:11 -07:00
|
|
|
(untag_int (transl env arg),
|
2001-02-19 12:15:42 -08:00
|
|
|
s.us_index_consts,
|
2015-10-20 08:11:11 -07:00
|
|
|
Array.map (transl env) s.us_actions_consts)
|
2000-02-21 10:14:56 -08:00
|
|
|
else if Array.length s.us_index_consts = 0 then
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_switch env (get_tag (transl env arg))
|
2001-02-19 12:15:42 -08:00
|
|
|
s.us_index_blocks s.us_actions_blocks
|
2000-02-21 10:14:56 -08:00
|
|
|
else
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "switch" (transl env arg) (fun arg ->
|
2000-02-21 10:14:56 -08:00
|
|
|
Cifthenelse(
|
2000-10-02 07:08:30 -07:00
|
|
|
Cop(Cand, [arg; Cconst_int 1]),
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_switch env
|
2001-02-19 12:15:42 -08:00
|
|
|
(untag_int arg) s.us_index_consts s.us_actions_consts,
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_switch env
|
2001-02-19 12:15:42 -08:00
|
|
|
(get_tag arg) s.us_index_blocks s.us_actions_blocks))
|
2014-03-21 10:02:44 -07:00
|
|
|
| Ustringswitch(arg,sw,d) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "switch" (transl env arg)
|
2014-03-21 10:02:44 -07:00
|
|
|
(fun arg ->
|
2015-10-20 08:11:11 -07:00
|
|
|
strmatch_compile arg (Misc.may_map (transl env) d)
|
|
|
|
(List.map (fun (s,act) -> s,transl env act) sw))
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ustaticfail (nfail, args) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cexit (nfail, List.map (transl env) args)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ucatch(nfail, [], body, handler) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
make_catch nfail (transl env body) (transl env handler)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ucatch(nfail, ids, body, handler) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Ccatch(nfail, ids, transl env body, transl env handler)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Utrywith(body, exn, handler) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Ctrywith(transl env body, exn, transl env handler)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uifthenelse(Uprim(Pnot, [arg], _), ifso, ifnot) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl env (Uifthenelse(arg, ifnot, ifso))
|
2000-10-02 07:08:30 -07:00
|
|
|
| Uifthenelse(cond, ifso, Ustaticfail (nfail, [])) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_false env cond (transl env ifso) nfail
|
2000-10-02 07:08:30 -07:00
|
|
|
| Uifthenelse(cond, Ustaticfail (nfail, []), ifnot) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_true env cond nfail (transl env ifnot)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uifthenelse(Uprim(Psequand, _, _) as cond, ifso, ifnot) ->
|
2000-10-02 07:08:30 -07:00
|
|
|
let raise_num = next_raise_count () in
|
|
|
|
make_catch
|
|
|
|
raise_num
|
2015-10-20 08:11:11 -07:00
|
|
|
(exit_if_false env cond (transl env ifso) raise_num)
|
|
|
|
(transl env ifnot)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uifthenelse(Uprim(Psequor, _, _) as cond, ifso, ifnot) ->
|
2006-01-04 08:55:50 -08:00
|
|
|
let raise_num = next_raise_count () in
|
2000-10-02 07:08:30 -07:00
|
|
|
make_catch
|
|
|
|
raise_num
|
2015-10-20 08:11:11 -07:00
|
|
|
(exit_if_true env cond raise_num (transl env ifnot))
|
|
|
|
(transl env ifso)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Uifthenelse (Uifthenelse (cond, condso, condnot), ifso, ifnot) ->
|
|
|
|
let num_true = next_raise_count () in
|
|
|
|
make_catch
|
|
|
|
num_true
|
|
|
|
(make_catch2
|
|
|
|
(fun shared_false ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else
|
2015-10-20 08:11:11 -07:00
|
|
|
(test_bool (transl env cond),
|
|
|
|
exit_if_true env condso num_true shared_false,
|
|
|
|
exit_if_true env condnot num_true shared_false))
|
|
|
|
(transl env ifnot))
|
|
|
|
(transl env ifso)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Uifthenelse(cond, ifso, ifnot) ->
|
2016-02-10 09:28:38 -08:00
|
|
|
if_then_else(test_bool(transl env cond), transl env ifso,
|
|
|
|
transl env ifnot)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Usequence(exp1, exp2) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Csequence(remove_unit(transl env exp1), transl env exp2)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Uwhile(cond, body) ->
|
2000-10-02 07:08:30 -07:00
|
|
|
let raise_num = next_raise_count () in
|
2000-08-11 12:50:59 -07:00
|
|
|
return_unit
|
|
|
|
(Ccatch
|
2000-10-02 07:08:30 -07:00
|
|
|
(raise_num, [],
|
2015-10-20 08:11:11 -07:00
|
|
|
Cloop(exit_if_false env cond
|
|
|
|
(remove_unit(transl env body)) raise_num),
|
2000-08-11 12:50:59 -07:00
|
|
|
Ctuple []))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Ufor(id, low, high, dir, body) ->
|
|
|
|
let tst = match dir with Upto -> Cgt | Downto -> Clt in
|
|
|
|
let inc = match dir with Upto -> Caddi | Downto -> Csubi in
|
2000-10-02 07:08:30 -07:00
|
|
|
let raise_num = next_raise_count () in
|
2002-11-18 02:50:49 -08:00
|
|
|
let id_prev = Ident.rename id in
|
2000-02-21 10:14:56 -08:00
|
|
|
return_unit
|
2000-10-02 07:08:30 -07:00
|
|
|
(Clet
|
2015-10-20 08:11:11 -07:00
|
|
|
(id, transl env low,
|
|
|
|
bind_nonvar "bound" (transl env high) (fun high ->
|
2000-10-02 07:08:30 -07:00
|
|
|
Ccatch
|
|
|
|
(raise_num, [],
|
|
|
|
Cifthenelse
|
|
|
|
(Cop(Ccmpi tst, [Cvar id; high]), Cexit (raise_num, []),
|
|
|
|
Cloop
|
|
|
|
(Csequence
|
2015-10-20 08:11:11 -07:00
|
|
|
(remove_unit(transl env body),
|
2002-11-18 02:50:49 -08:00
|
|
|
Clet(id_prev, Cvar id,
|
2000-10-02 07:08:30 -07:00
|
|
|
Csequence
|
2006-01-04 08:55:50 -08:00
|
|
|
(Cassign(id,
|
2002-11-18 02:50:49 -08:00
|
|
|
Cop(inc, [Cvar id; Cconst_int 2])),
|
2000-10-02 07:08:30 -07:00
|
|
|
Cifthenelse
|
2002-11-18 02:50:49 -08:00
|
|
|
(Cop(Ccmpi Ceq, [Cvar id_prev; high]),
|
|
|
|
Cexit (raise_num,[]), Ctuple [])))))),
|
2000-10-02 07:08:30 -07:00
|
|
|
Ctuple []))))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Uassign(id, exp) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
begin match is_unboxed_id id env with
|
|
|
|
| None ->
|
|
|
|
return_unit (Cassign(id, transl env exp))
|
|
|
|
| Some (unboxed_id, bn) ->
|
|
|
|
return_unit(Cassign(unboxed_id, transl_unbox_number env bn exp))
|
|
|
|
end
|
2016-01-04 07:14:52 -08:00
|
|
|
| Uunreachable ->
|
|
|
|
Cop(Cload Word_int, [Cconst_int 0])
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2016-06-27 00:49:26 -07:00
|
|
|
and transl_make_array dbg env kind args =
|
2016-01-12 10:51:06 -08:00
|
|
|
match kind with
|
|
|
|
| Pgenarray ->
|
2016-07-06 03:44:00 -07:00
|
|
|
Cop(Cextcall("caml_make_array", typ_val, true, Debuginfo.none, None),
|
2016-06-27 00:49:26 -07:00
|
|
|
[make_alloc dbg 0 (List.map (transl env) args)])
|
2016-01-12 10:51:06 -08:00
|
|
|
| Paddrarray | Pintarray ->
|
2016-06-27 00:49:26 -07:00
|
|
|
make_alloc dbg 0 (List.map (transl env) args)
|
2016-01-12 10:51:06 -08:00
|
|
|
| Pfloatarray ->
|
2016-06-27 00:49:26 -07:00
|
|
|
make_float_alloc dbg Obj.double_array_tag
|
2016-01-12 10:51:06 -08:00
|
|
|
(List.map (transl_unbox_float env) args)
|
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_ccall env prim args dbg =
|
2015-08-25 09:18:46 -07:00
|
|
|
let transl_arg native_repr arg =
|
|
|
|
match native_repr with
|
2015-10-20 08:11:11 -07:00
|
|
|
| Same_as_ocaml_repr -> transl env arg
|
|
|
|
| Unboxed_float -> transl_unbox_float env arg
|
|
|
|
| Unboxed_integer bi -> transl_unbox_int env bi arg
|
|
|
|
| Untagged_int -> untag_int (transl env arg)
|
2015-08-25 09:18:46 -07:00
|
|
|
in
|
|
|
|
let rec transl_args native_repr_args args =
|
|
|
|
match native_repr_args, args with
|
|
|
|
| [], args ->
|
|
|
|
(* We don't require the two lists to be of the same length as
|
|
|
|
[default_prim] always sets the arity to [0]. *)
|
2015-10-20 08:11:11 -07:00
|
|
|
List.map (transl env) args
|
2015-08-25 09:18:46 -07:00
|
|
|
| _, [] -> assert false
|
|
|
|
| native_repr :: native_repr_args, arg :: args ->
|
|
|
|
transl_arg native_repr arg :: transl_args native_repr_args args
|
|
|
|
in
|
|
|
|
let typ_res, wrap_result =
|
|
|
|
match prim.prim_native_repr_res with
|
|
|
|
| Same_as_ocaml_repr -> (typ_val, fun x -> x)
|
2016-06-27 00:49:26 -07:00
|
|
|
| Unboxed_float -> (typ_float, box_float dbg)
|
|
|
|
| Unboxed_integer Pint64 when size_int = 4 ->
|
|
|
|
([|Int; Int|], box_int dbg Pint64)
|
|
|
|
| Unboxed_integer bi -> (typ_int, box_int dbg bi)
|
2015-08-25 09:18:46 -07:00
|
|
|
| Untagged_int -> (typ_int, tag_int)
|
|
|
|
in
|
|
|
|
let args = transl_args prim.prim_native_repr_args args in
|
|
|
|
wrap_result
|
|
|
|
(Cop(Cextcall(Primitive.native_name prim,
|
2016-07-06 03:44:00 -07:00
|
|
|
typ_res, prim.prim_alloc, dbg, None), args))
|
2015-08-25 09:18:46 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_prim_1 env p arg dbg =
|
2000-02-21 10:14:56 -08:00
|
|
|
match p with
|
|
|
|
(* Generic operations *)
|
2016-08-07 08:07:10 -07:00
|
|
|
Pidentity | Pbytes_to_string | Pbytes_of_string | Popaque ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl env arg
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pignore ->
|
2015-10-20 08:11:11 -07:00
|
|
|
return_unit(remove_unit (transl env arg))
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Heap operations *)
|
|
|
|
| Pfield n ->
|
2015-10-20 08:11:11 -07:00
|
|
|
get_field (transl env arg) n
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pfloatfield n ->
|
2015-10-20 08:11:11 -07:00
|
|
|
let ptr = transl env arg in
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (
|
2000-03-10 06:31:06 -08:00
|
|
|
Cop(Cload Double_u,
|
1998-04-06 02:07:57 -07:00
|
|
|
[if n = 0 then ptr
|
|
|
|
else Cop(Cadda, [ptr; Cconst_int(n * size_float)])]))
|
2014-05-02 05:47:02 -07:00
|
|
|
| Pint_as_pointer ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Caddi, [transl env arg; Cconst_int (-1)])
|
2014-11-06 00:54:14 -08:00
|
|
|
(* always a pointer outside the heap *)
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Exceptions *)
|
2016-07-28 04:46:23 -07:00
|
|
|
| Praise _ when not (!Clflags.debug) ->
|
2016-07-28 06:25:13 -07:00
|
|
|
Cop(Craise (Cmm.Raise_notrace, dbg), [transl env arg])
|
|
|
|
| Praise Lambda.Raise_notrace ->
|
|
|
|
Cop(Craise (Cmm.Raise_notrace, dbg), [transl env arg])
|
|
|
|
| Praise Lambda.Raise_reraise ->
|
|
|
|
Cop(Craise (Cmm.Raise_withtrace, dbg), [transl env arg])
|
|
|
|
| Praise Lambda.Raise_regular ->
|
2016-07-28 04:46:23 -07:00
|
|
|
raise_regular dbg (transl env arg)
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Integer operations *)
|
|
|
|
| Pnegint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Csubi, [Cconst_int 2; transl env arg])
|
2012-11-29 01:55:00 -08:00
|
|
|
| Pctconst c ->
|
|
|
|
let const_of_bool b = tag_int (Cconst_int (if b then 1 else 0)) in
|
|
|
|
begin
|
|
|
|
match c with
|
|
|
|
| Big_endian -> const_of_bool Arch.big_endian
|
|
|
|
| Word_size -> tag_int (Cconst_int (8*Arch.size_int))
|
2014-11-17 04:19:27 -08:00
|
|
|
| Int_size -> tag_int (Cconst_int ((8*Arch.size_int) - 1))
|
2015-09-11 04:58:31 -07:00
|
|
|
| Max_wosize ->
|
|
|
|
tag_int (Cconst_int ((1 lsl ((8*Arch.size_int) - 10)) - 1 ))
|
2012-11-29 01:55:00 -08:00
|
|
|
| Ostype_unix -> const_of_bool (Sys.os_type = "Unix")
|
|
|
|
| Ostype_win32 -> const_of_bool (Sys.os_type = "Win32")
|
|
|
|
| Ostype_cygwin -> const_of_bool (Sys.os_type = "Cygwin")
|
2016-08-01 07:06:59 -07:00
|
|
|
| Backend_type ->
|
|
|
|
tag_int (Cconst_int 0) (* tag 0 is the same as Native here *)
|
2012-11-29 01:55:00 -08:00
|
|
|
end
|
2000-02-21 10:14:56 -08:00
|
|
|
| Poffsetint n ->
|
2015-02-07 03:32:12 -08:00
|
|
|
if no_overflow_lsl n 1 then
|
2015-10-20 08:11:11 -07:00
|
|
|
add_const (transl env arg) (n lsl 1)
|
2002-01-10 07:52:40 -08:00
|
|
|
else
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_prim_2 env Paddint arg (Uconst (Uconst_int n))
|
2013-03-09 14:38:52 -08:00
|
|
|
Debuginfo.none
|
2000-02-21 10:14:56 -08:00
|
|
|
| Poffsetref n ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ref" (transl env arg) (fun arg ->
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Word_int, Assignment),
|
2014-11-06 00:54:14 -08:00
|
|
|
[arg; add_const (Cop(Cload Word_int, [arg])) (n lsl 1)])))
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Floating-point operations *)
|
|
|
|
| Pfloatofint ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Cfloatofint, [untag_int(transl env arg)]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pintoffloat ->
|
2015-10-20 08:11:11 -07:00
|
|
|
tag_int(Cop(Cintoffloat, [transl_unbox_float env arg]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pnegfloat ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Cnegf, [transl_unbox_float env arg]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pabsfloat ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Cabsf, [transl_unbox_float env arg]))
|
2000-02-21 10:14:56 -08:00
|
|
|
(* String operations *)
|
2016-08-07 08:07:10 -07:00
|
|
|
| Pstringlength | Pbyteslength ->
|
2015-10-20 08:11:11 -07:00
|
|
|
tag_int(string_length (transl env arg))
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Array operations *)
|
|
|
|
| Parraylength kind ->
|
2016-08-02 09:52:07 -07:00
|
|
|
let hdr = get_header_without_profinfo (transl env arg) in
|
2000-02-21 10:14:56 -08:00
|
|
|
begin match kind with
|
|
|
|
Pgenarray ->
|
|
|
|
let len =
|
|
|
|
if wordsize_shift = numfloat_shift then
|
2016-08-02 09:52:07 -07:00
|
|
|
Cop(Clsr, [hdr; Cconst_int wordsize_shift])
|
2000-02-21 10:14:56 -08:00
|
|
|
else
|
2016-08-02 09:52:07 -07:00
|
|
|
bind "header" hdr (fun hdr ->
|
2000-02-21 10:14:56 -08:00
|
|
|
Cifthenelse(is_addr_array_hdr hdr,
|
|
|
|
Cop(Clsr, [hdr; Cconst_int wordsize_shift]),
|
|
|
|
Cop(Clsr, [hdr; Cconst_int numfloat_shift]))) in
|
|
|
|
Cop(Cor, [len; Cconst_int 1])
|
|
|
|
| Paddrarray | Pintarray ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Cop(Cor, [addr_array_length hdr; Cconst_int 1])
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pfloatarray ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Cop(Cor, [float_array_length hdr; Cconst_int 1])
|
2000-02-21 10:14:56 -08:00
|
|
|
end
|
|
|
|
(* Boolean operations *)
|
|
|
|
| Pnot ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Csubi, [Cconst_int 4; transl env arg]) (* 1 -> 3, 3 -> 1 *)
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Test integer/block *)
|
|
|
|
| Pisint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
tag_int(Cop(Cand, [transl env arg; Cconst_int 1]))
|
2000-02-21 10:14:56 -08:00
|
|
|
(* Boxed integers *)
|
|
|
|
| Pbintofint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (untag_int (transl env arg))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pintofbint bi ->
|
2015-10-20 08:11:11 -07:00
|
|
|
force_tag_int (transl_unbox_int env bi arg)
|
2000-03-05 11:17:54 -08:00
|
|
|
| Pcvtbint(bi1, bi2) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi2 (transl_unbox_int env bi1 arg)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pnegbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Csubi, [Cconst_int 0; transl_unbox_int env bi arg]))
|
2012-11-29 01:55:00 -08:00
|
|
|
| Pbbswap bi ->
|
|
|
|
let prim = match bi with
|
|
|
|
| Pnativeint -> "nativeint"
|
|
|
|
| Pint32 -> "int32"
|
|
|
|
| Pint64 -> "int64" in
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Cextcall(Printf.sprintf "caml_%s_direct_bswap" prim,
|
2016-07-06 03:44:00 -07:00
|
|
|
typ_int, false, Debuginfo.none, None),
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg]))
|
2012-11-29 01:55:00 -08:00
|
|
|
| Pbswap16 ->
|
2013-03-09 14:38:52 -08:00
|
|
|
tag_int (Cop(Cextcall("caml_bswap16_direct", typ_int, false,
|
2016-07-06 03:44:00 -07:00
|
|
|
Debuginfo.none, None),
|
2015-10-20 08:11:11 -07:00
|
|
|
[untag_int (transl env arg)]))
|
2016-01-15 03:53:50 -08:00
|
|
|
| prim ->
|
|
|
|
fatal_errorf "Cmmgen.transl_prim_1: %a" Printlambda.primitive prim
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_prim_2 env p arg1 arg2 dbg =
|
2000-02-21 10:14:56 -08:00
|
|
|
match p with
|
|
|
|
(* Heap operations *)
|
2015-12-18 08:42:40 -08:00
|
|
|
Psetfield(n, ptr, init) ->
|
|
|
|
begin match init, ptr with
|
|
|
|
| Assignment, Pointer ->
|
2016-07-06 03:44:00 -07:00
|
|
|
return_unit(Cop(Cextcall("caml_modify", typ_void, false,Debuginfo.none,
|
|
|
|
None),
|
2015-10-20 08:11:11 -07:00
|
|
|
[field_address (transl env arg1) n; transl env arg2]))
|
2015-12-18 08:42:40 -08:00
|
|
|
| Assignment, Immediate
|
|
|
|
| Initialization, (Immediate | Pointer) ->
|
|
|
|
return_unit(set_field (transl env arg1) n (transl env arg2) init)
|
|
|
|
end
|
|
|
|
| Psetfloatfield (n, init) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
let ptr = transl env arg1 in
|
1998-04-06 02:07:57 -07:00
|
|
|
return_unit(
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Double_u, init),
|
1998-04-06 02:07:57 -07:00
|
|
|
[if n = 0 then ptr
|
|
|
|
else Cop(Cadda, [ptr; Cconst_int(n * size_float)]);
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_unbox_float env arg2]))
|
1995-07-27 10:38:53 -07:00
|
|
|
|
|
|
|
(* Boolean operations *)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Psequand ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else(test_bool(transl env arg1), transl env arg2, Cconst_int 1)
|
2004-11-29 23:28:00 -08:00
|
|
|
(* let id = Ident.create "res1" in
|
2015-10-20 08:11:11 -07:00
|
|
|
Clet(id, transl env arg1,
|
|
|
|
Cifthenelse(test_bool(Cvar id), transl env arg2, Cvar id)) *)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Psequor ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else(test_bool(transl env arg1), Cconst_int 3, transl env arg2)
|
1995-07-27 10:38:53 -07:00
|
|
|
|
|
|
|
(* Integer operations *)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Paddint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
decr_int(add_int (transl env arg1) (transl env arg2))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Psubint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
incr_int(sub_int (transl env arg1) (transl env arg2))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pmulint ->
|
2015-02-07 03:32:15 -08:00
|
|
|
begin
|
2015-09-11 04:58:31 -07:00
|
|
|
(* decrementing the non-constant part helps when the multiplication is
|
|
|
|
followed by an addition;
|
2015-02-07 03:32:15 -08:00
|
|
|
for example, using this trick compiles (100 * a + 7) into
|
|
|
|
(+ ( * a 100) -85)
|
|
|
|
rather than
|
|
|
|
(+ ( * 200 (>>s a 1)) 15)
|
|
|
|
*)
|
2015-10-20 08:11:11 -07:00
|
|
|
match transl env arg1, transl env arg2 with
|
2015-09-11 04:58:31 -07:00
|
|
|
| Cconst_int _ as c1, c2 ->
|
|
|
|
incr_int (mul_int (untag_int c1) (decr_int c2))
|
2015-02-07 03:32:15 -08:00
|
|
|
| c1, c2 -> incr_int (mul_int (decr_int c1) (untag_int c2))
|
|
|
|
end
|
2016-10-05 07:42:40 -07:00
|
|
|
| Pdivint is_safe ->
|
2016-02-10 09:28:38 -08:00
|
|
|
tag_int(div_int (untag_int(transl env arg1))
|
2016-10-05 07:42:40 -07:00
|
|
|
(untag_int(transl env arg2)) is_safe dbg)
|
|
|
|
| Pmodint is_safe ->
|
2016-02-10 09:28:38 -08:00
|
|
|
tag_int(mod_int (untag_int(transl env arg1))
|
2016-10-05 07:42:40 -07:00
|
|
|
(untag_int(transl env arg2)) is_safe dbg)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pandint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Cand, [transl env arg1; transl env arg2])
|
2000-02-21 10:14:56 -08:00
|
|
|
| Porint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Cor, [transl env arg1; transl env arg2])
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pxorint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Cor, [Cop(Cxor, [ignore_low_bit_int(transl env arg1);
|
|
|
|
ignore_low_bit_int(transl env arg2)]);
|
2000-02-21 10:14:56 -08:00
|
|
|
Cconst_int 1])
|
|
|
|
| Plslint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
incr_int(lsl_int (decr_int(transl env arg1)) (untag_int(transl env arg2)))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Plsrint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Cor, [lsr_int (transl env arg1) (untag_int(transl env arg2));
|
1999-01-29 00:50:10 -08:00
|
|
|
Cconst_int 1])
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pasrint ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Cop(Cor, [asr_int (transl env arg1) (untag_int(transl env arg2));
|
1999-01-29 00:50:10 -08:00
|
|
|
Cconst_int 1])
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pintcomp cmp ->
|
2015-10-20 08:11:11 -07:00
|
|
|
tag_int(Cop(Ccmpi(transl_comparison cmp),
|
|
|
|
[transl env arg1; transl env arg2]))
|
2000-10-02 07:08:30 -07:00
|
|
|
| Pisout ->
|
2015-10-20 08:11:11 -07:00
|
|
|
transl_isout (transl env arg1) (transl env arg2)
|
1995-07-27 10:38:53 -07:00
|
|
|
(* Float operations *)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Paddfloat ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Caddf,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_float env arg1; transl_unbox_float env arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Psubfloat ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Csubf,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_float env arg1; transl_unbox_float env arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pmulfloat ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Cmulf,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_float env arg1; transl_unbox_float env arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pdivfloat ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (Cop(Cdivf,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_float env arg1; transl_unbox_float env arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pfloatcomp cmp ->
|
1995-07-10 02:48:27 -07:00
|
|
|
tag_int(Cop(Ccmpf(transl_comparison cmp),
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_float env arg1; transl_unbox_float env arg2]))
|
1995-07-27 10:38:53 -07:00
|
|
|
|
|
|
|
(* String operations *)
|
2016-08-07 08:07:10 -07:00
|
|
|
| Pstringrefu | Pbytesrefu ->
|
2000-02-04 04:43:18 -08:00
|
|
|
tag_int(Cop(Cload Byte_unsigned,
|
2015-10-20 08:11:11 -07:00
|
|
|
[add_int (transl env arg1) (untag_int(transl env arg2))]))
|
2016-08-07 08:07:10 -07:00
|
|
|
| Pstringrefs | Pbytesrefs ->
|
1995-07-10 02:48:27 -07:00
|
|
|
tag_int
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
1995-07-10 02:48:27 -07:00
|
|
|
Csequence(
|
2011-12-22 01:36:45 -08:00
|
|
|
make_checkbound dbg [string_length str; idx],
|
2000-02-04 04:43:18 -08:00
|
|
|
Cop(Cload Byte_unsigned, [add_int str idx])))))
|
1995-07-27 10:38:53 -07:00
|
|
|
|
2012-11-09 08:15:29 -08:00
|
|
|
| Pstring_load_16(unsafe) ->
|
|
|
|
tag_int
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2013-03-09 14:38:52 -08:00
|
|
|
check_bound unsafe dbg (sub_int (string_length str) (Cconst_int 1))
|
|
|
|
idx (unaligned_load_16 str idx))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pbigstring_load_16(unsafe) ->
|
|
|
|
tag_int
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ba" (transl env arg1) (fun ba ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2015-09-11 04:58:31 -07:00
|
|
|
bind "ba_data" (Cop(Cload Word_int, [field_address ba 1]))
|
|
|
|
(fun ba_data ->
|
|
|
|
check_bound unsafe dbg (sub_int (Cop(Cload Word_int,
|
|
|
|
[field_address ba 5]))
|
2012-11-09 08:15:29 -08:00
|
|
|
(Cconst_int 1)) idx
|
|
|
|
(unaligned_load_16 ba_data idx)))))
|
|
|
|
|
|
|
|
| Pstring_load_32(unsafe) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg Pint32
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2013-03-09 14:38:52 -08:00
|
|
|
check_bound unsafe dbg (sub_int (string_length str) (Cconst_int 3))
|
|
|
|
idx (unaligned_load_32 str idx))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pbigstring_load_32(unsafe) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg Pint32
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ba" (transl env arg1) (fun ba ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2015-09-11 04:58:31 -07:00
|
|
|
bind "ba_data" (Cop(Cload Word_int, [field_address ba 1]))
|
|
|
|
(fun ba_data ->
|
|
|
|
check_bound unsafe dbg (sub_int (Cop(Cload Word_int,
|
|
|
|
[field_address ba 5]))
|
2012-11-09 08:15:29 -08:00
|
|
|
(Cconst_int 3)) idx
|
|
|
|
(unaligned_load_32 ba_data idx)))))
|
|
|
|
|
|
|
|
| Pstring_load_64(unsafe) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg Pint64
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2013-03-09 14:38:52 -08:00
|
|
|
check_bound unsafe dbg (sub_int (string_length str) (Cconst_int 7))
|
|
|
|
idx (unaligned_load_64 str idx))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pbigstring_load_64(unsafe) ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg Pint64
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ba" (transl env arg1) (fun ba ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2015-09-11 04:58:31 -07:00
|
|
|
bind "ba_data" (Cop(Cload Word_int, [field_address ba 1]))
|
|
|
|
(fun ba_data ->
|
|
|
|
check_bound unsafe dbg (sub_int (Cop(Cload Word_int,
|
|
|
|
[field_address ba 5]))
|
2012-11-09 08:15:29 -08:00
|
|
|
(Cconst_int 7)) idx
|
|
|
|
(unaligned_load_64 ba_data idx)))))
|
|
|
|
|
1995-07-27 10:38:53 -07:00
|
|
|
(* Array operations *)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Parrayrefu kind ->
|
1995-07-27 10:38:53 -07:00
|
|
|
begin match kind with
|
|
|
|
Pgenarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
1998-04-06 02:07:57 -07:00
|
|
|
Cifthenelse(is_addr_array_ptr arr,
|
1995-07-27 10:38:53 -07:00
|
|
|
addr_array_ref arr idx,
|
2016-06-27 00:49:26 -07:00
|
|
|
float_array_ref dbg arr idx)))
|
2014-11-06 00:54:14 -08:00
|
|
|
| Paddrarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
addr_array_ref (transl env arg1) (transl env arg2)
|
2014-11-06 00:54:14 -08:00
|
|
|
| Pintarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
int_array_ref (transl env arg1) (transl env arg2)
|
1995-07-27 10:38:53 -07:00
|
|
|
| Pfloatarray ->
|
2016-06-27 00:49:26 -07:00
|
|
|
float_array_ref dbg (transl env arg1) (transl env arg2)
|
1995-07-27 10:38:53 -07:00
|
|
|
end
|
2000-02-21 10:14:56 -08:00
|
|
|
| Parrayrefs kind ->
|
1995-07-27 10:38:53 -07:00
|
|
|
begin match kind with
|
2011-12-22 01:14:22 -08:00
|
|
|
| Pgenarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
bind "header" (get_header_without_profinfo arr) (fun hdr ->
|
2011-12-22 01:14:22 -08:00
|
|
|
if wordsize_shift = numfloat_shift then
|
2011-12-22 01:36:45 -08:00
|
|
|
Csequence(make_checkbound dbg [addr_array_length hdr; idx],
|
2012-07-30 11:04:46 -07:00
|
|
|
Cifthenelse(is_addr_array_hdr hdr,
|
2011-12-22 01:14:22 -08:00
|
|
|
addr_array_ref arr idx,
|
2016-06-27 00:49:26 -07:00
|
|
|
float_array_ref dbg arr idx))
|
2011-12-22 01:14:22 -08:00
|
|
|
else
|
|
|
|
Cifthenelse(is_addr_array_hdr hdr,
|
2011-12-22 01:36:45 -08:00
|
|
|
Csequence(make_checkbound dbg [addr_array_length hdr; idx],
|
2011-12-22 01:14:22 -08:00
|
|
|
addr_array_ref arr idx),
|
2011-12-22 01:36:45 -08:00
|
|
|
Csequence(make_checkbound dbg [float_array_length hdr; idx],
|
2016-06-27 00:49:26 -07:00
|
|
|
float_array_ref dbg arr idx)))))
|
2014-11-06 00:54:14 -08:00
|
|
|
| Paddrarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Csequence(make_checkbound dbg [
|
|
|
|
addr_array_length(get_header_without_profinfo arr); idx],
|
2013-03-09 14:38:52 -08:00
|
|
|
addr_array_ref arr idx)))
|
2014-11-06 00:54:14 -08:00
|
|
|
| Pintarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Csequence(make_checkbound dbg [
|
|
|
|
addr_array_length(get_header_without_profinfo arr); idx],
|
2014-11-06 00:54:14 -08:00
|
|
|
int_array_ref arr idx)))
|
1995-07-27 10:38:53 -07:00
|
|
|
| Pfloatarray ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_float dbg (
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2013-03-09 14:38:52 -08:00
|
|
|
Csequence(make_checkbound dbg
|
2016-08-02 09:52:07 -07:00
|
|
|
[float_array_length(get_header_without_profinfo arr);
|
|
|
|
idx],
|
2013-03-09 14:38:52 -08:00
|
|
|
unboxed_float_array_ref arr idx))))
|
1995-07-27 10:38:53 -07:00
|
|
|
end
|
2000-02-21 10:14:56 -08:00
|
|
|
|
|
|
|
(* Operations on bitvects *)
|
|
|
|
| Pbittest ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "index" (untag_int(transl env arg2)) (fun idx ->
|
2000-02-21 10:14:56 -08:00
|
|
|
tag_int(
|
|
|
|
Cop(Cand, [Cop(Clsr, [Cop(Cload Byte_unsigned,
|
2015-10-20 08:11:11 -07:00
|
|
|
[add_int (transl env arg1)
|
2000-02-21 10:14:56 -08:00
|
|
|
(Cop(Clsr, [idx; Cconst_int 3]))]);
|
|
|
|
Cop(Cand, [idx; Cconst_int 7])]);
|
|
|
|
Cconst_int 1])))
|
|
|
|
|
|
|
|
(* Boxed integers *)
|
|
|
|
| Paddbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Caddi,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Psubbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Csubi,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pmulbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Cmuli,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2016-10-05 07:42:40 -07:00
|
|
|
| Pdivbint { size = bi; is_safe } ->
|
|
|
|
box_int dbg bi (safe_div_bi is_safe
|
2015-10-20 08:11:11 -07:00
|
|
|
(transl_unbox_int env bi arg1)
|
|
|
|
(transl_unbox_int env bi arg2)
|
2012-02-24 02:13:02 -08:00
|
|
|
bi dbg)
|
2016-10-05 07:42:40 -07:00
|
|
|
| Pmodbint { size = bi; is_safe } ->
|
|
|
|
box_int dbg bi (safe_mod_bi is_safe
|
2015-10-20 08:11:11 -07:00
|
|
|
(transl_unbox_int env bi arg1)
|
|
|
|
(transl_unbox_int env bi arg2)
|
2012-02-24 02:13:02 -08:00
|
|
|
bi dbg)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pandbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Cand,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Porbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Cor,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pxorbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Cxor,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Plslbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Clsl,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
untag_int(transl env arg2)]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Plsrbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Clsr,
|
2015-10-20 08:11:11 -07:00
|
|
|
[make_unsigned_int bi (transl_unbox_int env bi arg1);
|
|
|
|
untag_int(transl env arg2)]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pasrbint bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
box_int dbg bi (Cop(Casr,
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
untag_int(transl env arg2)]))
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pbintcomp(bi, cmp) ->
|
|
|
|
tag_int (Cop(Ccmpi(transl_comparison cmp),
|
2015-10-20 08:11:11 -07:00
|
|
|
[transl_unbox_int env bi arg1;
|
|
|
|
transl_unbox_int env bi arg2]))
|
2016-01-15 03:53:50 -08:00
|
|
|
| prim ->
|
|
|
|
fatal_errorf "Cmmgen.transl_prim_2: %a" Printlambda.primitive prim
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_prim_3 env p arg1 arg2 arg3 dbg =
|
2000-02-21 10:14:56 -08:00
|
|
|
match p with
|
|
|
|
(* String operations *)
|
2016-08-07 08:07:10 -07:00
|
|
|
| Pbytessetu ->
|
2015-12-18 08:42:40 -08:00
|
|
|
return_unit(Cop(Cstore (Byte_unsigned, Assignment),
|
2015-10-20 08:11:11 -07:00
|
|
|
[add_int (transl env arg1) (untag_int(transl env arg2));
|
|
|
|
untag_int(transl env arg3)]))
|
2016-08-07 08:07:10 -07:00
|
|
|
| Pbytessets ->
|
2000-02-21 10:14:56 -08:00
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
2000-02-21 10:14:56 -08:00
|
|
|
Csequence(
|
2011-12-22 01:36:45 -08:00
|
|
|
make_checkbound dbg [string_length str; idx],
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Byte_unsigned, Assignment),
|
2015-10-20 08:11:11 -07:00
|
|
|
[add_int str idx; untag_int(transl env arg3)])))))
|
2000-02-21 10:14:56 -08:00
|
|
|
|
|
|
|
(* Array operations *)
|
|
|
|
| Parraysetu kind ->
|
|
|
|
return_unit(begin match kind with
|
|
|
|
Pgenarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "newval" (transl env arg3) (fun newval ->
|
|
|
|
bind "index" (transl env arg2) (fun index ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2000-02-21 10:14:56 -08:00
|
|
|
Cifthenelse(is_addr_array_ptr arr,
|
|
|
|
addr_array_set arr index newval,
|
|
|
|
float_array_set arr index (unbox_float newval)))))
|
|
|
|
| Paddrarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
addr_array_set (transl env arg1) (transl env arg2) (transl env arg3)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pintarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
int_array_set (transl env arg1) (transl env arg2) (transl env arg3)
|
2000-02-21 10:14:56 -08:00
|
|
|
| Pfloatarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
float_array_set (transl env arg1) (transl env arg2)
|
|
|
|
(transl_unbox_float env arg3)
|
2000-02-21 10:14:56 -08:00
|
|
|
end)
|
|
|
|
| Parraysets kind ->
|
1995-07-27 10:38:53 -07:00
|
|
|
return_unit(begin match kind with
|
2011-12-22 01:14:22 -08:00
|
|
|
| Pgenarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "newval" (transl env arg3) (fun newval ->
|
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
bind "header" (get_header_without_profinfo arr) (fun hdr ->
|
2011-12-22 01:14:22 -08:00
|
|
|
if wordsize_shift = numfloat_shift then
|
2011-12-22 01:36:45 -08:00
|
|
|
Csequence(make_checkbound dbg [addr_array_length hdr; idx],
|
2012-07-30 11:04:46 -07:00
|
|
|
Cifthenelse(is_addr_array_hdr hdr,
|
2011-12-22 01:14:22 -08:00
|
|
|
addr_array_set arr idx newval,
|
|
|
|
float_array_set arr idx
|
|
|
|
(unbox_float newval)))
|
|
|
|
else
|
|
|
|
Cifthenelse(is_addr_array_hdr hdr,
|
2011-12-22 01:36:45 -08:00
|
|
|
Csequence(make_checkbound dbg [addr_array_length hdr; idx],
|
2011-12-22 01:14:22 -08:00
|
|
|
addr_array_set arr idx newval),
|
2011-12-22 01:36:45 -08:00
|
|
|
Csequence(make_checkbound dbg [float_array_length hdr; idx],
|
2011-12-22 01:14:22 -08:00
|
|
|
float_array_set arr idx
|
|
|
|
(unbox_float newval)))))))
|
1995-07-27 10:38:53 -07:00
|
|
|
| Paddrarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "newval" (transl env arg3) (fun newval ->
|
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Csequence(make_checkbound dbg [
|
|
|
|
addr_array_length(get_header_without_profinfo arr); idx],
|
2012-03-14 02:27:13 -07:00
|
|
|
addr_array_set arr idx newval))))
|
1995-07-27 10:38:53 -07:00
|
|
|
| Pintarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind "newval" (transl env arg3) (fun newval ->
|
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Csequence(make_checkbound dbg [
|
|
|
|
addr_array_length(get_header_without_profinfo arr); idx],
|
2012-03-14 02:27:13 -07:00
|
|
|
int_array_set arr idx newval))))
|
1995-07-27 10:38:53 -07:00
|
|
|
| Pfloatarray ->
|
2015-10-20 08:11:11 -07:00
|
|
|
bind_load "newval" (transl_unbox_float env arg3) (fun newval ->
|
|
|
|
bind "index" (transl env arg2) (fun idx ->
|
|
|
|
bind "arr" (transl env arg1) (fun arr ->
|
2016-08-02 09:52:07 -07:00
|
|
|
Csequence(make_checkbound dbg [
|
|
|
|
float_array_length(get_header_without_profinfo arr);idx],
|
2012-03-14 02:27:13 -07:00
|
|
|
float_array_set arr idx newval))))
|
1995-07-27 10:38:53 -07:00
|
|
|
end)
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pstring_set_16(unsafe) ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
|
|
|
bind "newval" (untag_int (transl env arg3)) (fun newval ->
|
2013-03-09 14:38:52 -08:00
|
|
|
check_bound unsafe dbg (sub_int (string_length str) (Cconst_int 1))
|
|
|
|
idx (unaligned_set_16 str idx newval)))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pbigstring_set_16(unsafe) ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ba" (transl env arg1) (fun ba ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
|
|
|
bind "newval" (untag_int (transl env arg3)) (fun newval ->
|
2015-09-11 04:58:31 -07:00
|
|
|
bind "ba_data" (Cop(Cload Word_int, [field_address ba 1]))
|
|
|
|
(fun ba_data ->
|
|
|
|
check_bound unsafe dbg (sub_int (Cop(Cload Word_int,
|
|
|
|
[field_address ba 5]))
|
2013-03-09 14:38:52 -08:00
|
|
|
(Cconst_int 1))
|
|
|
|
idx (unaligned_set_16 ba_data idx newval))))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pstring_set_32(unsafe) ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
|
|
|
bind "newval" (transl_unbox_int env Pint32 arg3) (fun newval ->
|
2013-03-09 14:38:52 -08:00
|
|
|
check_bound unsafe dbg (sub_int (string_length str) (Cconst_int 3))
|
|
|
|
idx (unaligned_set_32 str idx newval)))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pbigstring_set_32(unsafe) ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ba" (transl env arg1) (fun ba ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
|
|
|
bind "newval" (transl_unbox_int env Pint32 arg3) (fun newval ->
|
2015-09-11 04:58:31 -07:00
|
|
|
bind "ba_data" (Cop(Cload Word_int, [field_address ba 1]))
|
|
|
|
(fun ba_data ->
|
|
|
|
check_bound unsafe dbg (sub_int (Cop(Cload Word_int,
|
|
|
|
[field_address ba 5]))
|
2013-03-09 14:38:52 -08:00
|
|
|
(Cconst_int 3))
|
|
|
|
idx (unaligned_set_32 ba_data idx newval))))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pstring_set_64(unsafe) ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "str" (transl env arg1) (fun str ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
|
|
|
bind "newval" (transl_unbox_int env Pint64 arg3) (fun newval ->
|
2013-03-09 14:38:52 -08:00
|
|
|
check_bound unsafe dbg (sub_int (string_length str) (Cconst_int 7))
|
|
|
|
idx (unaligned_set_64 str idx newval)))))
|
2012-11-09 08:15:29 -08:00
|
|
|
|
|
|
|
| Pbigstring_set_64(unsafe) ->
|
|
|
|
return_unit
|
2015-10-20 08:11:11 -07:00
|
|
|
(bind "ba" (transl env arg1) (fun ba ->
|
|
|
|
bind "index" (untag_int (transl env arg2)) (fun idx ->
|
|
|
|
bind "newval" (transl_unbox_int env Pint64 arg3) (fun newval ->
|
2015-09-11 04:58:31 -07:00
|
|
|
bind "ba_data" (Cop(Cload Word_int, [field_address ba 1]))
|
|
|
|
(fun ba_data ->
|
|
|
|
check_bound unsafe dbg (sub_int (Cop(Cload Word_int,
|
|
|
|
[field_address ba 5]))
|
2012-11-09 08:15:29 -08:00
|
|
|
(Cconst_int 7)) idx
|
|
|
|
(unaligned_set_64 ba_data idx newval))))))
|
|
|
|
|
2016-01-15 03:53:50 -08:00
|
|
|
| prim ->
|
|
|
|
fatal_errorf "Cmmgen.transl_prim_3: %a" Printlambda.primitive prim
|
1995-07-27 10:38:53 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_unbox_float env = function
|
2016-01-04 07:02:35 -08:00
|
|
|
Uconst(Uconst_ref(_, Some (Uconst_float f))) -> Cconst_float f
|
2015-10-20 08:11:11 -07:00
|
|
|
| exp -> unbox_float(transl env exp)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_unbox_int env bi = function
|
2016-01-04 07:02:35 -08:00
|
|
|
Uconst(Uconst_ref(_, Some (Uconst_int32 n))) ->
|
2003-04-25 05:27:31 -07:00
|
|
|
Cconst_natint (Nativeint.of_int32 n)
|
2016-01-04 07:02:35 -08:00
|
|
|
| Uconst(Uconst_ref(_, Some (Uconst_nativeint n))) ->
|
2003-04-25 05:27:31 -07:00
|
|
|
Cconst_natint n
|
2016-01-04 07:02:35 -08:00
|
|
|
| Uconst(Uconst_ref(_, Some (Uconst_int64 n))) ->
|
2015-08-25 09:18:50 -07:00
|
|
|
if size_int = 8 then
|
|
|
|
Cconst_natint (Int64.to_nativeint n)
|
2016-04-15 05:41:54 -07:00
|
|
|
else begin
|
2015-08-25 09:18:50 -07:00
|
|
|
let low = Int64.to_nativeint n in
|
|
|
|
let high = Int64.to_nativeint (Int64.shift_right_logical n 32) in
|
2016-04-15 05:41:54 -07:00
|
|
|
if big_endian then Ctuple [Cconst_natint high; Cconst_natint low]
|
|
|
|
else Ctuple [Cconst_natint low; Cconst_natint high]
|
|
|
|
end
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uprim(Pbintofint bi',[Uconst(Uconst_int i)],_) when bi = bi' ->
|
2000-02-21 10:14:56 -08:00
|
|
|
Cconst_int i
|
2015-10-20 08:11:11 -07:00
|
|
|
| exp -> unbox_int bi (transl env exp)
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_unbox_number env bn arg =
|
2015-08-25 09:18:49 -07:00
|
|
|
match bn with
|
2016-06-27 00:49:26 -07:00
|
|
|
| Boxed_float _ -> transl_unbox_float env arg
|
|
|
|
| Boxed_integer (bi, _) -> transl_unbox_int env bi arg
|
2015-08-25 09:18:49 -07:00
|
|
|
|
2015-12-07 03:32:03 -08:00
|
|
|
and transl_let env str kind id exp body =
|
2015-12-07 02:54:20 -08:00
|
|
|
let unboxing =
|
|
|
|
(* If [id] is a mutable variable (introduced to eliminate a local
|
|
|
|
reference) and it contains a type of unboxable numbers, then
|
2015-12-07 03:56:42 -08:00
|
|
|
force unboxing. Indeed, if not boxed, each assignment to the variable
|
2015-12-07 02:54:20 -08:00
|
|
|
might require some boxing, but such local references are often
|
2015-12-07 03:56:42 -08:00
|
|
|
used in loops and we really want to avoid repeated boxing. *)
|
2015-12-07 03:32:03 -08:00
|
|
|
match str, kind with
|
2016-03-16 08:11:43 -07:00
|
|
|
| Mutable, Pfloatval ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_float Debuginfo.none, false)
|
2016-03-16 08:11:43 -07:00
|
|
|
| Mutable, Pboxedintval bi ->
|
2016-06-27 00:49:26 -07:00
|
|
|
Boxed (Boxed_integer (bi, Debuginfo.none), false)
|
2016-03-16 09:23:04 -07:00
|
|
|
| _, (Pfloatval | Pboxedintval _) ->
|
2015-12-07 03:56:42 -08:00
|
|
|
(* It would be safe to always unbox in this case, but
|
|
|
|
we do it only if this indeed allows us to get rid of
|
|
|
|
some allocations in the bound expression. *)
|
2015-12-07 03:38:06 -08:00
|
|
|
is_unboxed_number ~strict:false env exp
|
2016-03-16 09:23:04 -07:00
|
|
|
| _, Pgenval ->
|
2015-12-07 03:56:42 -08:00
|
|
|
(* Here we don't know statically that the bound expression
|
|
|
|
evaluates to an unboxable number type. We need to be stricter
|
|
|
|
and ensure that all possible branches in the expression
|
|
|
|
return a boxed value (of the same kind). Indeed, with GADTs,
|
|
|
|
different branches could return different types. *)
|
2015-12-07 03:38:06 -08:00
|
|
|
is_unboxed_number ~strict:true env exp
|
2016-03-16 08:46:12 -07:00
|
|
|
| _, Pintval ->
|
|
|
|
No_unboxing
|
2015-12-07 02:54:20 -08:00
|
|
|
in
|
|
|
|
match unboxing with
|
2016-04-29 05:57:42 -07:00
|
|
|
| No_unboxing | Boxed (_, true) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Clet(id, transl env exp, transl env body)
|
2015-07-27 03:12:35 -07:00
|
|
|
| No_result ->
|
2015-09-11 04:58:31 -07:00
|
|
|
(* the let-bound expression never returns a value, we can ignore
|
|
|
|
the body *)
|
2015-10-20 08:11:11 -07:00
|
|
|
transl env exp
|
2016-04-29 05:57:42 -07:00
|
|
|
| Boxed (boxed_number, _false) ->
|
2015-07-27 03:16:57 -07:00
|
|
|
let unboxed_id = Ident.create (Ident.name id) in
|
2015-10-20 08:11:11 -07:00
|
|
|
Clet(unboxed_id, transl_unbox_number env boxed_number exp,
|
|
|
|
transl (add_unboxed_id id unboxed_id boxed_number env) body)
|
2001-01-13 03:55:43 -08:00
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
and make_catch ncatch body handler = match body with
|
|
|
|
| Cexit (nexit,[]) when nexit=ncatch -> handler
|
2006-01-04 08:55:50 -08:00
|
|
|
| _ -> Ccatch (ncatch, [], body, handler)
|
2000-10-02 07:08:30 -07:00
|
|
|
|
|
|
|
and make_catch2 mk_body handler = match handler with
|
|
|
|
| Cexit (_,[])|Ctuple []|Cconst_int _|Cconst_pointer _ ->
|
|
|
|
mk_body handler
|
|
|
|
| _ ->
|
|
|
|
let nfail = next_raise_count () in
|
|
|
|
make_catch
|
|
|
|
nfail
|
|
|
|
(mk_body (Cexit (nfail,[])))
|
|
|
|
handler
|
2006-01-04 08:55:50 -08:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and exit_if_true env cond nfail otherwise =
|
1995-07-02 09:41:48 -07:00
|
|
|
match cond with
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst (Uconst_ptr 0) -> otherwise
|
|
|
|
| Uconst (Uconst_ptr 1) -> Cexit (nfail,[])
|
2015-10-25 06:39:05 -07:00
|
|
|
| Uifthenelse (arg1, Uconst (Uconst_ptr 1), arg2)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(Psequor, [arg1; arg2], _) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_true env arg1 nfail (exit_if_true env arg2 nfail otherwise)
|
2015-10-25 06:39:05 -07:00
|
|
|
| Uifthenelse (_, _, Uconst (Uconst_ptr 0))
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(Psequand, _, _) ->
|
2000-10-02 07:08:30 -07:00
|
|
|
begin match otherwise with
|
|
|
|
| Cexit (raise_num,[]) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_false env cond (Cexit (nfail,[])) raise_num
|
2000-10-02 07:08:30 -07:00
|
|
|
| _ ->
|
|
|
|
let raise_num = next_raise_count () in
|
|
|
|
make_catch
|
|
|
|
raise_num
|
2015-10-20 08:11:11 -07:00
|
|
|
(exit_if_false env cond (Cexit (nfail,[])) raise_num)
|
2000-10-02 07:08:30 -07:00
|
|
|
otherwise
|
2006-01-04 08:55:50 -08:00
|
|
|
end
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(Pnot, [arg], _) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_false env arg otherwise nfail
|
2000-10-02 07:08:30 -07:00
|
|
|
| Uifthenelse (cond, ifso, ifnot) ->
|
|
|
|
make_catch2
|
|
|
|
(fun shared ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else
|
2015-10-20 08:11:11 -07:00
|
|
|
(test_bool (transl env cond),
|
|
|
|
exit_if_true env ifso nfail shared,
|
|
|
|
exit_if_true env ifnot nfail shared))
|
2000-10-02 07:08:30 -07:00
|
|
|
otherwise
|
1995-07-02 09:41:48 -07:00
|
|
|
| _ ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else(test_bool(transl env cond), Cexit (nfail, []), otherwise)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and exit_if_false env cond otherwise nfail =
|
1995-07-02 09:41:48 -07:00
|
|
|
match cond with
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst (Uconst_ptr 0) -> Cexit (nfail,[])
|
|
|
|
| Uconst (Uconst_ptr 1) -> otherwise
|
2015-10-25 06:39:05 -07:00
|
|
|
| Uifthenelse (arg1, arg2, Uconst (Uconst_ptr 0))
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(Psequand, [arg1; arg2], _) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_false env arg1 (exit_if_false env arg2 otherwise nfail) nfail
|
2015-10-25 06:39:05 -07:00
|
|
|
| Uifthenelse (_, Uconst (Uconst_ptr 1), _)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(Psequor, _, _) ->
|
2000-10-02 07:08:30 -07:00
|
|
|
begin match otherwise with
|
|
|
|
| Cexit (raise_num,[]) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_true env cond raise_num (Cexit (nfail,[]))
|
2000-10-02 07:08:30 -07:00
|
|
|
| _ ->
|
|
|
|
let raise_num = next_raise_count () in
|
|
|
|
make_catch
|
|
|
|
raise_num
|
2015-10-20 08:11:11 -07:00
|
|
|
(exit_if_true env cond raise_num (Cexit (nfail,[])))
|
2000-10-02 07:08:30 -07:00
|
|
|
otherwise
|
|
|
|
end
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(Pnot, [arg], _) ->
|
2015-10-20 08:11:11 -07:00
|
|
|
exit_if_true env arg nfail otherwise
|
2000-10-02 07:08:30 -07:00
|
|
|
| Uifthenelse (cond, ifso, ifnot) ->
|
|
|
|
make_catch2
|
|
|
|
(fun shared ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else
|
2015-10-20 08:11:11 -07:00
|
|
|
(test_bool (transl env cond),
|
|
|
|
exit_if_false env ifso shared nfail,
|
|
|
|
exit_if_false env ifnot shared nfail))
|
2000-10-02 07:08:30 -07:00
|
|
|
otherwise
|
1995-07-02 09:41:48 -07:00
|
|
|
| _ ->
|
2016-01-15 03:56:02 -08:00
|
|
|
if_then_else(test_bool(transl env cond), otherwise, Cexit (nfail, []))
|
2000-10-02 07:08:30 -07:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_switch env arg index cases = match Array.length cases with
|
2001-02-19 12:15:42 -08:00
|
|
|
| 0 -> fatal_error "Cmmgen.transl_switch"
|
2015-10-20 08:11:11 -07:00
|
|
|
| 1 -> transl env cases.(0)
|
2000-10-02 07:08:30 -07:00
|
|
|
| _ ->
|
2015-10-20 08:11:11 -07:00
|
|
|
let cases = Array.map (transl env) cases in
|
2014-04-07 08:43:20 -07:00
|
|
|
let store = StoreExp.mk_store () in
|
|
|
|
let index =
|
|
|
|
Array.map
|
|
|
|
(fun j -> store.Switch.act_store cases.(j))
|
|
|
|
index in
|
2000-10-02 07:08:30 -07:00
|
|
|
let n_index = Array.length index in
|
|
|
|
let inters = ref []
|
|
|
|
and this_high = ref (n_index-1)
|
|
|
|
and this_low = ref (n_index-1)
|
|
|
|
and this_act = ref index.(n_index-1) in
|
|
|
|
for i = n_index-2 downto 0 do
|
|
|
|
let act = index.(i) in
|
|
|
|
if act = !this_act then
|
|
|
|
decr this_low
|
|
|
|
else begin
|
|
|
|
inters := (!this_low, !this_high, !this_act) :: !inters ;
|
|
|
|
this_high := i ;
|
|
|
|
this_low := i ;
|
|
|
|
this_act := act
|
|
|
|
end
|
|
|
|
done ;
|
|
|
|
inters := (0, !this_high, !this_act) :: !inters ;
|
2014-04-07 08:43:20 -07:00
|
|
|
match !inters with
|
|
|
|
| [_] -> cases.(0)
|
2014-04-12 03:17:02 -07:00
|
|
|
| inters ->
|
2014-04-07 08:43:20 -07:00
|
|
|
bind "switcher" arg
|
|
|
|
(fun a ->
|
|
|
|
SwitcherBlocks.zyva
|
|
|
|
(0,n_index-1)
|
|
|
|
a
|
|
|
|
(Array.of_list inters) store)
|
2002-11-07 09:57:23 -08:00
|
|
|
|
2015-10-20 08:11:11 -07:00
|
|
|
and transl_letrec env bindings cont =
|
2013-05-22 05:56:54 -07:00
|
|
|
let bsz =
|
|
|
|
List.map (fun (id, exp) -> (id, exp, expr_size Ident.empty exp)) bindings in
|
2012-01-13 09:46:21 -08:00
|
|
|
let op_alloc prim sz =
|
2016-07-06 03:44:00 -07:00
|
|
|
Cop(Cextcall(prim, typ_val, true, Debuginfo.none, None), [int_const sz]) in
|
1995-07-17 09:10:15 -07:00
|
|
|
let rec init_blocks = function
|
2002-12-15 15:28:52 -08:00
|
|
|
| [] -> fill_nonrec bsz
|
2016-03-09 02:40:16 -08:00
|
|
|
| (id, _exp, RHS_block sz) :: rem ->
|
2012-01-13 09:46:21 -08:00
|
|
|
Clet(id, op_alloc "caml_alloc_dummy" sz, init_blocks rem)
|
2016-03-09 02:40:16 -08:00
|
|
|
| (id, _exp, RHS_floatblock sz) :: rem ->
|
2012-01-13 09:46:21 -08:00
|
|
|
Clet(id, op_alloc "caml_alloc_dummy_float" sz, init_blocks rem)
|
2016-03-09 02:40:16 -08:00
|
|
|
| (id, _exp, RHS_nonrec) :: rem ->
|
2015-07-27 04:33:21 -07:00
|
|
|
Clet (id, Cconst_int 0, init_blocks rem)
|
2002-12-15 15:28:52 -08:00
|
|
|
and fill_nonrec = function
|
|
|
|
| [] -> fill_blocks bsz
|
2016-03-09 02:40:16 -08:00
|
|
|
| (_id, _exp, (RHS_block _ | RHS_floatblock _)) :: rem ->
|
2012-01-13 09:46:21 -08:00
|
|
|
fill_nonrec rem
|
2002-12-15 15:28:52 -08:00
|
|
|
| (id, exp, RHS_nonrec) :: rem ->
|
2015-10-20 08:11:11 -07:00
|
|
|
Clet(id, transl env exp, fill_nonrec rem)
|
1995-07-17 09:10:15 -07:00
|
|
|
and fill_blocks = function
|
2002-11-07 09:57:23 -08:00
|
|
|
| [] -> cont
|
2012-01-13 09:46:21 -08:00
|
|
|
| (id, exp, (RHS_block _ | RHS_floatblock _)) :: rem ->
|
|
|
|
let op =
|
2016-07-06 03:44:00 -07:00
|
|
|
Cop(Cextcall("caml_update_dummy", typ_void, false, Debuginfo.none,
|
|
|
|
None),
|
2015-10-20 08:11:11 -07:00
|
|
|
[Cvar id; transl env exp]) in
|
2012-01-13 09:46:21 -08:00
|
|
|
Csequence(op, fill_blocks rem)
|
2016-03-09 02:40:16 -08:00
|
|
|
| (_id, _exp, RHS_nonrec) :: rem ->
|
2002-11-07 09:57:23 -08:00
|
|
|
fill_blocks rem
|
|
|
|
in init_blocks bsz
|
1995-07-17 09:10:15 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Translate a function definition *)
|
|
|
|
|
2012-02-21 09:41:02 -08:00
|
|
|
let transl_function f =
|
2016-01-26 07:43:24 -08:00
|
|
|
let body =
|
|
|
|
if Config.flambda then
|
|
|
|
Un_anf.apply f.body ~what:f.label
|
|
|
|
else
|
|
|
|
f.body
|
|
|
|
in
|
2012-02-21 09:41:02 -08:00
|
|
|
Cfunction {fun_name = f.label;
|
2014-11-06 00:54:14 -08:00
|
|
|
fun_args = List.map (fun id -> (id, typ_val)) f.params;
|
2016-01-26 07:43:24 -08:00
|
|
|
fun_body = transl empty_env body;
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = !Clflags.optimize_for_speed;
|
|
|
|
fun_dbg = f.dbg; }
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Translate all function definitions *)
|
|
|
|
|
1995-07-12 08:32:09 -07:00
|
|
|
module StringSet =
|
|
|
|
Set.Make(struct
|
|
|
|
type t = string
|
2013-03-19 00:22:12 -07:00
|
|
|
let compare (x:t) y = compare x y
|
1995-07-12 08:32:09 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
let rec transl_all_functions already_translated cont =
|
1995-07-02 09:41:48 -07:00
|
|
|
try
|
2012-02-21 09:41:02 -08:00
|
|
|
let f = Queue.take functions in
|
|
|
|
if StringSet.mem f.label already_translated then
|
1995-07-12 08:32:09 -07:00
|
|
|
transl_all_functions already_translated cont
|
2001-02-19 12:15:42 -08:00
|
|
|
else begin
|
2012-02-21 09:41:02 -08:00
|
|
|
transl_all_functions
|
|
|
|
(StringSet.add f.label already_translated)
|
2016-07-27 14:43:41 -07:00
|
|
|
((f.dbg, transl_function f) :: cont)
|
2001-02-19 12:15:42 -08:00
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
with Queue.Empty ->
|
2016-01-06 09:35:27 -08:00
|
|
|
cont, already_translated
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2016-01-13 09:44:20 -08:00
|
|
|
let cdefine_symbol (symb, global) =
|
2016-01-14 07:16:15 -08:00
|
|
|
match global with
|
|
|
|
| Global -> [Cglobal_symbol symb; Cdefine_symbol symb]
|
|
|
|
| Not_global -> [Cdefine_symbol symb]
|
2016-01-13 09:44:20 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Emit structured constants *)
|
|
|
|
|
2014-03-06 09:03:16 -08:00
|
|
|
let rec emit_structured_constant symb cst cont =
|
2014-05-12 00:29:24 -07:00
|
|
|
let emit_block white_header symb cont =
|
|
|
|
(* Headers for structured constants must be marked black in case we
|
|
|
|
are in no-naked-pointers mode. See [caml_darken]. *)
|
|
|
|
let black_header = Nativeint.logor white_header caml_black in
|
2016-01-13 09:44:20 -08:00
|
|
|
Cint black_header :: cdefine_symbol symb @ cont
|
2014-05-12 00:29:24 -07:00
|
|
|
in
|
1995-07-02 09:41:48 -07:00
|
|
|
match cst with
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_float s->
|
|
|
|
emit_block float_header symb (Cdouble s :: cont)
|
|
|
|
| Uconst_string s ->
|
|
|
|
emit_block (string_header (String.length s)) symb
|
|
|
|
(emit_string_constant s cont)
|
|
|
|
| Uconst_int32 n ->
|
|
|
|
emit_block boxedint32_header symb
|
|
|
|
(emit_boxed_int32_constant n cont)
|
|
|
|
| Uconst_int64 n ->
|
|
|
|
emit_block boxedint64_header symb
|
|
|
|
(emit_boxed_int64_constant n cont)
|
|
|
|
| Uconst_nativeint n ->
|
|
|
|
emit_block boxedintnat_header symb
|
|
|
|
(emit_boxed_nativeint_constant n cont)
|
|
|
|
| Uconst_block (tag, csts) ->
|
|
|
|
let cont = List.fold_right emit_constant csts cont in
|
|
|
|
emit_block (block_header tag (List.length csts)) symb cont
|
|
|
|
| Uconst_float_array fields ->
|
|
|
|
emit_block (floatarray_header (List.length fields)) symb
|
|
|
|
(Misc.map_end (fun f -> Cdouble f) fields cont)
|
2016-01-04 07:48:14 -08:00
|
|
|
| Uconst_closure(fundecls, lbl, fv) ->
|
2016-01-13 09:44:20 -08:00
|
|
|
assert(lbl = fst symb);
|
|
|
|
constant_closures := (symb, fundecls, fv) :: !constant_closures;
|
2016-01-04 07:48:14 -08:00
|
|
|
List.iter (fun f -> Queue.add f functions) fundecls;
|
|
|
|
cont
|
2014-03-06 09:03:16 -08:00
|
|
|
|
|
|
|
and emit_constant cst cont =
|
|
|
|
match cst with
|
|
|
|
| Uconst_int n | Uconst_ptr n ->
|
2016-01-12 06:31:02 -08:00
|
|
|
cint_const n
|
2014-04-12 03:17:02 -07:00
|
|
|
:: cont
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_ref (label, _) ->
|
|
|
|
Csymbol_address label :: cont
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
and emit_string_constant s cont =
|
|
|
|
let n = size_int - 1 - (String.length s) mod size_int in
|
|
|
|
Cstring s :: Cskip n :: Cint8 n :: cont
|
|
|
|
|
2003-04-25 05:27:31 -07:00
|
|
|
and emit_boxed_int32_constant n cont =
|
|
|
|
let n = Nativeint.of_int32 n in
|
|
|
|
if size_int = 8 then
|
2004-01-01 08:42:43 -08:00
|
|
|
Csymbol_address("caml_int32_ops") :: Cint32 n :: Cint32 0n :: cont
|
2003-04-25 05:27:31 -07:00
|
|
|
else
|
2004-01-01 08:42:43 -08:00
|
|
|
Csymbol_address("caml_int32_ops") :: Cint n :: cont
|
2003-04-25 05:27:31 -07:00
|
|
|
|
|
|
|
and emit_boxed_nativeint_constant n cont =
|
2004-01-01 08:42:43 -08:00
|
|
|
Csymbol_address("caml_nativeint_ops") :: Cint n :: cont
|
2000-02-21 10:14:56 -08:00
|
|
|
|
2003-04-25 05:27:31 -07:00
|
|
|
and emit_boxed_int64_constant n cont =
|
|
|
|
let lo = Int64.to_nativeint n in
|
|
|
|
if size_int = 8 then
|
2004-01-01 08:42:43 -08:00
|
|
|
Csymbol_address("caml_int64_ops") :: Cint lo :: cont
|
2003-04-25 05:27:31 -07:00
|
|
|
else begin
|
|
|
|
let hi = Int64.to_nativeint (Int64.shift_right n 32) in
|
|
|
|
if big_endian then
|
2004-01-01 08:42:43 -08:00
|
|
|
Csymbol_address("caml_int64_ops") :: Cint hi :: Cint lo :: cont
|
2003-04-25 05:27:31 -07:00
|
|
|
else
|
2004-01-01 08:42:43 -08:00
|
|
|
Csymbol_address("caml_int64_ops") :: Cint lo :: Cint hi :: cont
|
2003-04-25 05:27:31 -07:00
|
|
|
end
|
2000-02-21 10:14:56 -08:00
|
|
|
|
1999-01-27 02:52:33 -08:00
|
|
|
(* Emit constant closures *)
|
|
|
|
|
2016-01-26 07:43:24 -08:00
|
|
|
let emit_constant_closure ((_, global_symb) as symb) fundecls clos_vars cont =
|
|
|
|
let closure_symbol f =
|
|
|
|
if Config.flambda then
|
|
|
|
cdefine_symbol (f.label ^ "_closure", global_symb)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
in
|
1999-01-27 02:52:33 -08:00
|
|
|
match fundecls with
|
2016-01-26 07:43:24 -08:00
|
|
|
[] ->
|
|
|
|
(* This should probably not happen: dead code has normally been
|
|
|
|
eliminated and a closure cannot be accessed without going through
|
|
|
|
a [Project_closure], which depends on the function. *)
|
|
|
|
assert (clos_vars = []);
|
|
|
|
cdefine_symbol symb @
|
|
|
|
List.fold_right emit_constant clos_vars cont
|
2012-02-21 09:41:02 -08:00
|
|
|
| f1 :: remainder ->
|
1999-01-27 02:52:33 -08:00
|
|
|
let rec emit_others pos = function
|
2016-01-04 07:48:14 -08:00
|
|
|
[] ->
|
|
|
|
List.fold_right emit_constant clos_vars cont
|
2012-02-21 09:41:02 -08:00
|
|
|
| f2 :: rem ->
|
2016-01-06 10:20:49 -08:00
|
|
|
if f2.arity = 1 || f2.arity = 0 then
|
1999-01-27 02:52:33 -08:00
|
|
|
Cint(infix_header pos) ::
|
2016-01-26 07:43:24 -08:00
|
|
|
(closure_symbol f2) @
|
2012-02-21 09:41:02 -08:00
|
|
|
Csymbol_address f2.label ::
|
2016-01-12 06:31:02 -08:00
|
|
|
cint_const f2.arity ::
|
1999-01-27 02:52:33 -08:00
|
|
|
emit_others (pos + 3) rem
|
|
|
|
else
|
|
|
|
Cint(infix_header pos) ::
|
2016-01-26 07:43:24 -08:00
|
|
|
(closure_symbol f2) @
|
2012-02-21 09:41:02 -08:00
|
|
|
Csymbol_address(curry_function f2.arity) ::
|
2016-01-12 06:31:02 -08:00
|
|
|
cint_const f2.arity ::
|
2012-02-21 09:41:02 -08:00
|
|
|
Csymbol_address f2.label ::
|
1999-01-27 02:52:33 -08:00
|
|
|
emit_others (pos + 4) rem in
|
2016-01-04 07:48:14 -08:00
|
|
|
Cint(black_closure_header (fundecls_size fundecls
|
|
|
|
+ List.length clos_vars)) ::
|
2016-01-13 09:44:20 -08:00
|
|
|
cdefine_symbol symb @
|
2016-01-26 07:43:24 -08:00
|
|
|
(closure_symbol f1) @
|
2016-01-06 10:20:49 -08:00
|
|
|
if f1.arity = 1 || f1.arity = 0 then
|
2012-02-21 09:41:02 -08:00
|
|
|
Csymbol_address f1.label ::
|
2016-01-12 06:31:02 -08:00
|
|
|
cint_const f1.arity ::
|
1999-01-27 02:52:33 -08:00
|
|
|
emit_others 3 remainder
|
|
|
|
else
|
2012-02-21 09:41:02 -08:00
|
|
|
Csymbol_address(curry_function f1.arity) ::
|
2016-01-12 06:31:02 -08:00
|
|
|
cint_const f1.arity ::
|
2012-02-21 09:41:02 -08:00
|
|
|
Csymbol_address f1.label ::
|
1999-01-27 02:52:33 -08:00
|
|
|
emit_others 4 remainder
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Emit all structured constants *)
|
|
|
|
|
2016-01-13 09:05:35 -08:00
|
|
|
let emit_constants cont (constants:Clambda.preallocated_constant list) =
|
1997-01-01 07:35:28 -08:00
|
|
|
let c = ref cont in
|
2001-04-02 05:13:58 -07:00
|
|
|
List.iter
|
2016-01-14 07:16:15 -08:00
|
|
|
(fun { symbol = lbl; exported; definition = cst } ->
|
|
|
|
let global = if exported then Global else Not_global in
|
2016-01-13 09:44:20 -08:00
|
|
|
let cst = emit_structured_constant (lbl, global) cst [] in
|
2012-07-30 11:04:46 -07:00
|
|
|
c:= Cdata(cst):: !c)
|
2016-01-13 09:05:35 -08:00
|
|
|
constants;
|
1999-01-27 02:52:33 -08:00
|
|
|
List.iter
|
2016-01-04 07:48:14 -08:00
|
|
|
(fun (symb, fundecls, clos_vars) ->
|
|
|
|
c := Cdata(emit_constant_closure symb fundecls clos_vars []) :: !c)
|
1999-01-27 02:52:33 -08:00
|
|
|
!constant_closures;
|
|
|
|
constant_closures := [];
|
1997-01-01 07:35:28 -08:00
|
|
|
!c
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2016-01-13 09:05:35 -08:00
|
|
|
let emit_all_constants cont =
|
|
|
|
let constants = Compilenv.structured_constants () in
|
|
|
|
Compilenv.clear_structured_constants ();
|
|
|
|
emit_constants cont constants
|
|
|
|
|
2016-01-06 09:35:27 -08:00
|
|
|
let transl_all_functions_and_emit_all_constants cont =
|
2016-07-27 14:43:41 -07:00
|
|
|
let rec aux already_translated cont translated_functions =
|
2016-01-06 09:35:27 -08:00
|
|
|
if Compilenv.structured_constants () = [] &&
|
|
|
|
Queue.is_empty functions
|
2016-07-27 14:43:41 -07:00
|
|
|
then cont, translated_functions
|
2016-01-06 09:35:27 -08:00
|
|
|
else
|
2016-07-27 14:43:41 -07:00
|
|
|
let translated_functions, already_translated =
|
|
|
|
transl_all_functions already_translated translated_functions
|
|
|
|
in
|
2016-01-06 09:35:27 -08:00
|
|
|
let cont = emit_all_constants cont in
|
2016-07-27 14:43:41 -07:00
|
|
|
aux already_translated cont translated_functions
|
|
|
|
in
|
|
|
|
let cont, translated_functions =
|
|
|
|
aux StringSet.empty cont []
|
|
|
|
in
|
|
|
|
let translated_functions =
|
|
|
|
(* Sort functions according to source position *)
|
|
|
|
List.map snd
|
|
|
|
(List.sort (fun (dbg1, _) (dbg2, _) ->
|
|
|
|
Debuginfo.compare dbg1 dbg2) translated_functions)
|
2016-01-06 09:35:27 -08:00
|
|
|
in
|
2016-07-27 14:43:41 -07:00
|
|
|
translated_functions @ cont
|
2016-01-06 09:35:27 -08:00
|
|
|
|
2016-01-15 03:20:03 -08:00
|
|
|
(* Build the NULL terminated array of gc roots *)
|
2015-10-23 08:16:22 -07:00
|
|
|
|
2016-01-15 03:20:03 -08:00
|
|
|
let emit_gc_roots_table ~symbols cont =
|
2015-10-23 08:16:22 -07:00
|
|
|
let table_symbol = Compilenv.make_symbol (Some "gc_roots") in
|
|
|
|
Cdata(Cglobal_symbol table_symbol ::
|
|
|
|
Cdefine_symbol table_symbol ::
|
|
|
|
List.map (fun s -> Csymbol_address s) symbols @
|
|
|
|
[Cint 0n])
|
|
|
|
:: cont
|
|
|
|
|
2016-01-13 09:05:35 -08:00
|
|
|
(* Build preallocated blocks (used for Flambda [Initialize_symbol]
|
|
|
|
constructs, and Clambda global module) *)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2016-01-13 09:05:35 -08:00
|
|
|
let preallocate_block cont { Clambda.symbol; exported; tag; size } =
|
2014-05-12 00:29:24 -07:00
|
|
|
let space =
|
|
|
|
(* These words will be registered as roots and as such must contain
|
|
|
|
valid values, in case we are in no-naked-pointers mode. Likewise
|
|
|
|
the block header must be black, below (see [caml_darken]), since
|
|
|
|
the overall record may be referenced. *)
|
|
|
|
Array.to_list
|
|
|
|
(Array.init size (fun _index ->
|
|
|
|
Cint (Nativeint.of_int 1 (* Val_unit *))))
|
|
|
|
in
|
2016-01-13 09:05:35 -08:00
|
|
|
let data =
|
|
|
|
Cint(black_block_header tag size) ::
|
|
|
|
if exported then
|
|
|
|
Cglobal_symbol symbol ::
|
|
|
|
Cdefine_symbol symbol :: space
|
|
|
|
else
|
|
|
|
Cdefine_symbol symbol :: space
|
|
|
|
in
|
|
|
|
Cdata data :: cont
|
|
|
|
|
|
|
|
let emit_preallocated_blocks preallocated_blocks cont =
|
|
|
|
let symbols =
|
|
|
|
List.map (fun ({ Clambda.symbol }:Clambda.preallocated_block) -> symbol)
|
|
|
|
preallocated_blocks
|
|
|
|
in
|
2016-01-15 03:20:03 -08:00
|
|
|
let c1 = emit_gc_roots_table ~symbols cont in
|
2016-01-13 09:05:35 -08:00
|
|
|
List.fold_left preallocate_block c1 preallocated_blocks
|
|
|
|
|
|
|
|
(* Translate a compilation unit *)
|
|
|
|
|
|
|
|
let compunit (ulam, preallocated_blocks, constants) =
|
|
|
|
let init_code = transl empty_env ulam in
|
|
|
|
let c1 = [Cfunction {fun_name = Compilenv.make_symbol (Some "entry");
|
|
|
|
fun_args = [];
|
|
|
|
fun_body = init_code; fun_fast = false;
|
|
|
|
fun_dbg = Debuginfo.none }] in
|
|
|
|
let c2 = emit_constants c1 constants in
|
|
|
|
let c3 = transl_all_functions_and_emit_all_constants c2 in
|
|
|
|
emit_preallocated_blocks preallocated_blocks c3
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
(*
|
|
|
|
CAMLprim value caml_cache_public_method (value meths, value tag, value *cache)
|
|
|
|
{
|
|
|
|
int li = 3, hi = Field(meths,0), mi;
|
|
|
|
while (li < hi) { // no need to check the 1st time
|
|
|
|
mi = ((li+hi) >> 1) | 1;
|
|
|
|
if (tag < Field(meths,mi)) hi = mi-2;
|
|
|
|
else li = mi;
|
|
|
|
}
|
|
|
|
*cache = (li-3)*sizeof(value)+1;
|
|
|
|
return Field (meths, li-1);
|
|
|
|
}
|
|
|
|
*)
|
|
|
|
|
|
|
|
let cache_public_method meths tag cache =
|
|
|
|
let raise_num = next_raise_count () in
|
|
|
|
let li = Ident.create "li" and hi = Ident.create "hi"
|
|
|
|
and mi = Ident.create "mi" and tagged = Ident.create "tagged" in
|
|
|
|
Clet (
|
|
|
|
li, Cconst_int 3,
|
|
|
|
Clet (
|
2014-11-06 00:54:14 -08:00
|
|
|
hi, Cop(Cload Word_int, [meths]),
|
2004-05-26 04:10:52 -07:00
|
|
|
Csequence(
|
|
|
|
Ccatch
|
|
|
|
(raise_num, [],
|
|
|
|
Cloop
|
|
|
|
(Clet(
|
2006-01-04 08:55:50 -08:00
|
|
|
mi,
|
|
|
|
Cop(Cor,
|
|
|
|
[Cop(Clsr, [Cop(Caddi, [Cvar li; Cvar hi]); Cconst_int 1]);
|
|
|
|
Cconst_int 1]),
|
|
|
|
Csequence(
|
|
|
|
Cifthenelse
|
|
|
|
(Cop (Ccmpi Clt,
|
|
|
|
[tag;
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Cload Word_int,
|
2006-01-04 08:55:50 -08:00
|
|
|
[Cop(Cadda,
|
|
|
|
[meths; lsl_const (Cvar mi) log2_size_addr])])]),
|
|
|
|
Cassign(hi, Cop(Csubi, [Cvar mi; Cconst_int 2])),
|
|
|
|
Cassign(li, Cvar mi)),
|
|
|
|
Cifthenelse
|
|
|
|
(Cop(Ccmpi Cge, [Cvar li; Cvar hi]), Cexit (raise_num, []),
|
|
|
|
Ctuple [])))),
|
2004-05-26 04:10:52 -07:00
|
|
|
Ctuple []),
|
|
|
|
Clet (
|
|
|
|
tagged, Cop(Cadda, [lsl_const (Cvar li) log2_size_addr;
|
|
|
|
Cconst_int(1 - 3 * size_addr)]),
|
2015-12-18 08:42:40 -08:00
|
|
|
Csequence(Cop (Cstore (Word_int, Assignment), [cache; Cvar tagged]),
|
2004-05-26 04:10:52 -07:00
|
|
|
Cvar tagged)))))
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Generate an application function:
|
|
|
|
(defun caml_applyN (a1 ... aN clos)
|
|
|
|
(if (= clos.arity N)
|
|
|
|
(app clos.direct a1 ... aN clos)
|
|
|
|
(let (clos1 (app clos.code a1 clos)
|
|
|
|
clos2 (app clos1.code a2 clos)
|
|
|
|
...
|
|
|
|
closN-1 (app closN-2.code aN-1 closN-2))
|
|
|
|
(app closN-1.code aN closN-1))))
|
|
|
|
*)
|
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
let apply_function_body arity =
|
2014-08-22 06:45:02 -07:00
|
|
|
let arg = Array.make arity (Ident.create "arg") in
|
1996-04-22 04:15:41 -07:00
|
|
|
for i = 1 to arity - 1 do arg.(i) <- Ident.create "arg" done;
|
|
|
|
let clos = Ident.create "clos" in
|
1995-07-02 09:41:48 -07:00
|
|
|
let rec app_fun clos n =
|
|
|
|
if n = arity-1 then
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, Debuginfo.none),
|
1995-07-02 09:41:48 -07:00
|
|
|
[get_field (Cvar clos) 0; Cvar arg.(n); Cvar clos])
|
|
|
|
else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let newclos = Ident.create "clos" in
|
1995-07-02 09:41:48 -07:00
|
|
|
Clet(newclos,
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, Debuginfo.none),
|
1995-07-02 09:41:48 -07:00
|
|
|
[get_field (Cvar clos) 0; Cvar arg.(n); Cvar clos]),
|
|
|
|
app_fun newclos (n+1))
|
|
|
|
end in
|
2004-05-26 04:10:52 -07:00
|
|
|
let args = Array.to_list arg in
|
|
|
|
let all_args = args @ [clos] in
|
|
|
|
(args, clos,
|
|
|
|
if arity = 1 then app_fun clos 0 else
|
|
|
|
Cifthenelse(
|
|
|
|
Cop(Ccmpi Ceq, [get_field (Cvar clos) 1; int_const arity]),
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, Debuginfo.none),
|
2004-05-26 04:10:52 -07:00
|
|
|
get_field (Cvar clos) 2 :: List.map (fun s -> Cvar s) all_args),
|
|
|
|
app_fun clos 0))
|
|
|
|
|
|
|
|
let send_function arity =
|
|
|
|
let (args, clos', body) = apply_function_body (1+arity) in
|
|
|
|
let cache = Ident.create "cache"
|
|
|
|
and obj = List.hd args
|
|
|
|
and tag = Ident.create "tag" in
|
|
|
|
let clos =
|
|
|
|
let cache = Cvar cache and obj = Cvar obj and tag = Cvar tag in
|
|
|
|
let meths = Ident.create "meths" and cached = Ident.create "cached" in
|
|
|
|
let real = Ident.create "real" in
|
|
|
|
let mask = get_field (Cvar meths) 1 in
|
|
|
|
let cached_pos = Cvar cached in
|
|
|
|
let tag_pos = Cop(Cadda, [Cop (Cadda, [cached_pos; Cvar meths]);
|
|
|
|
Cconst_int(3*size_addr-1)]) in
|
2014-11-06 00:54:14 -08:00
|
|
|
let tag' = Cop(Cload Word_int, [tag_pos]) in
|
2004-05-26 04:10:52 -07:00
|
|
|
Clet (
|
2014-11-06 00:54:14 -08:00
|
|
|
meths, Cop(Cload Word_val, [obj]),
|
2004-05-26 04:10:52 -07:00
|
|
|
Clet (
|
2014-11-06 00:54:14 -08:00
|
|
|
cached, Cop(Cand, [Cop(Cload Word_int, [cache]); mask]),
|
2004-05-26 04:10:52 -07:00
|
|
|
Clet (
|
2006-01-04 08:55:50 -08:00
|
|
|
real,
|
2004-05-26 04:10:52 -07:00
|
|
|
Cifthenelse(Cop(Ccmpa Cne, [tag'; tag]),
|
2006-01-04 08:55:50 -08:00
|
|
|
cache_public_method (Cvar meths) tag cache,
|
2004-05-26 04:10:52 -07:00
|
|
|
cached_pos),
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Cload Word_val, [Cop(Cadda, [Cop (Cadda, [Cvar real; Cvar meths]);
|
|
|
|
Cconst_int(2*size_addr-1)])]))))
|
2006-01-04 08:55:50 -08:00
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
in
|
|
|
|
let body = Clet(clos', clos, body) in
|
|
|
|
let fun_args =
|
2014-11-06 00:54:14 -08:00
|
|
|
[obj, typ_val; tag, typ_int; cache, typ_val]
|
|
|
|
@ List.map (fun id -> (id, typ_val)) (List.tl args) in
|
2004-05-26 04:10:52 -07:00
|
|
|
Cfunction
|
|
|
|
{fun_name = "caml_send" ^ string_of_int arity;
|
|
|
|
fun_args = fun_args;
|
|
|
|
fun_body = body;
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = true;
|
|
|
|
fun_dbg = Debuginfo.none }
|
2004-05-26 04:10:52 -07:00
|
|
|
|
|
|
|
let apply_function arity =
|
|
|
|
let (args, clos, body) = apply_function_body arity in
|
|
|
|
let all_args = args @ [clos] in
|
1995-07-02 09:41:48 -07:00
|
|
|
Cfunction
|
|
|
|
{fun_name = "caml_apply" ^ string_of_int arity;
|
2014-11-06 00:54:14 -08:00
|
|
|
fun_args = List.map (fun id -> (id, typ_val)) all_args;
|
1995-07-02 09:41:48 -07:00
|
|
|
fun_body = body;
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = true;
|
|
|
|
fun_dbg = Debuginfo.none }
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1996-10-22 06:36:12 -07:00
|
|
|
(* Generate tuplifying functions:
|
|
|
|
(defun caml_tuplifyN (arg clos)
|
|
|
|
(app clos.direct #0(arg) ... #N-1(arg) clos)) *)
|
|
|
|
|
|
|
|
let tuplify_function arity =
|
|
|
|
let arg = Ident.create "arg" in
|
|
|
|
let clos = Ident.create "clos" in
|
|
|
|
let rec access_components i =
|
|
|
|
if i >= arity
|
|
|
|
then []
|
|
|
|
else get_field (Cvar arg) i :: access_components(i+1) in
|
|
|
|
Cfunction
|
|
|
|
{fun_name = "caml_tuplify" ^ string_of_int arity;
|
2014-11-06 00:54:14 -08:00
|
|
|
fun_args = [arg, typ_val; clos, typ_val];
|
1996-10-22 06:36:12 -07:00
|
|
|
fun_body =
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, Debuginfo.none),
|
1996-10-22 06:36:12 -07:00
|
|
|
get_field (Cvar clos) 2 :: access_components 0 @ [Cvar clos]);
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = true;
|
|
|
|
fun_dbg = Debuginfo.none }
|
1996-10-22 06:36:12 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Generate currying functions:
|
|
|
|
(defun caml_curryN (arg clos)
|
2011-06-12 03:04:46 -07:00
|
|
|
(alloc HDR caml_curryN_1 <arity (N-1)> caml_curry_N_1_app arg clos))
|
1995-07-02 09:41:48 -07:00
|
|
|
(defun caml_curryN_1 (arg clos)
|
2011-06-12 03:04:46 -07:00
|
|
|
(alloc HDR caml_curryN_2 <arity (N-2)> caml_curry_N_2_app arg clos))
|
1995-07-02 09:41:48 -07:00
|
|
|
...
|
|
|
|
(defun caml_curryN_N-1 (arg clos)
|
2011-06-12 03:04:46 -07:00
|
|
|
(let (closN-2 clos.vars[1]
|
|
|
|
closN-3 closN-2.vars[1]
|
1995-07-02 09:41:48 -07:00
|
|
|
...
|
2011-06-12 03:04:46 -07:00
|
|
|
clos1 clos2.vars[1]
|
|
|
|
clos clos1.vars[1])
|
1995-07-02 09:41:48 -07:00
|
|
|
(app clos.direct
|
2011-06-12 03:04:46 -07:00
|
|
|
clos1.vars[0] ... closN-2.vars[0] clos.vars[0] arg clos)))
|
2013-03-19 00:16:46 -07:00
|
|
|
|
2011-06-12 03:04:46 -07:00
|
|
|
Special "shortcut" functions are also generated to handle the
|
|
|
|
case where a partially applied function is applied to all remaining
|
|
|
|
arguments in one go. For instance:
|
|
|
|
(defun caml_curry_N_1_app (arg2 ... argN clos)
|
|
|
|
(let clos' clos.vars[1]
|
|
|
|
(app clos'.direct clos.vars[0] arg2 ... argN clos')))
|
2013-03-19 00:16:46 -07:00
|
|
|
|
|
|
|
Those shortcuts may lead to a quadratic number of application
|
|
|
|
primitives being generated in the worst case, which resulted in
|
|
|
|
linking time blowup in practice (PR#5933), so we only generate and
|
|
|
|
use them when below a fixed arity 'max_arity_optimized'.
|
2011-06-12 03:04:46 -07:00
|
|
|
*)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2013-03-19 00:16:46 -07:00
|
|
|
let max_arity_optimized = 15
|
1995-07-02 09:41:48 -07:00
|
|
|
let final_curry_function arity =
|
1996-04-22 04:15:41 -07:00
|
|
|
let last_arg = Ident.create "arg" in
|
|
|
|
let last_clos = Ident.create "clos" in
|
1995-07-02 09:41:48 -07:00
|
|
|
let rec curry_fun args clos n =
|
|
|
|
if n = 0 then
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, Debuginfo.none),
|
1995-07-02 09:41:48 -07:00
|
|
|
get_field (Cvar clos) 2 ::
|
|
|
|
args @ [Cvar last_arg; Cvar clos])
|
2011-06-12 03:04:46 -07:00
|
|
|
else
|
2013-03-19 00:16:46 -07:00
|
|
|
if n = arity - 1 || arity > max_arity_optimized then
|
2012-07-30 11:04:46 -07:00
|
|
|
begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let newclos = Ident.create "clos" in
|
1995-07-02 09:41:48 -07:00
|
|
|
Clet(newclos,
|
|
|
|
get_field (Cvar clos) 3,
|
|
|
|
curry_fun (get_field (Cvar clos) 2 :: args) newclos (n-1))
|
2012-07-30 11:04:46 -07:00
|
|
|
end else
|
|
|
|
begin
|
|
|
|
let newclos = Ident.create "clos" in
|
|
|
|
Clet(newclos,
|
2011-06-12 03:04:46 -07:00
|
|
|
get_field (Cvar clos) 4,
|
|
|
|
curry_fun (get_field (Cvar clos) 3 :: args) newclos (n-1))
|
1995-07-02 09:41:48 -07:00
|
|
|
end in
|
|
|
|
Cfunction
|
|
|
|
{fun_name = "caml_curry" ^ string_of_int arity ^
|
|
|
|
"_" ^ string_of_int (arity-1);
|
2014-11-06 00:54:14 -08:00
|
|
|
fun_args = [last_arg, typ_val; last_clos, typ_val];
|
1995-07-02 09:41:48 -07:00
|
|
|
fun_body = curry_fun [] last_clos (arity-1);
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = true;
|
|
|
|
fun_dbg = Debuginfo.none }
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let rec intermediate_curry_functions arity num =
|
|
|
|
if num = arity - 1 then
|
|
|
|
[final_curry_function arity]
|
|
|
|
else begin
|
|
|
|
let name1 = "caml_curry" ^ string_of_int arity in
|
|
|
|
let name2 = if num = 0 then name1 else name1 ^ "_" ^ string_of_int num in
|
1996-04-22 04:15:41 -07:00
|
|
|
let arg = Ident.create "arg" and clos = Ident.create "clos" in
|
1995-07-02 09:41:48 -07:00
|
|
|
Cfunction
|
|
|
|
{fun_name = name2;
|
2014-11-06 00:54:14 -08:00
|
|
|
fun_args = [arg, typ_val; clos, typ_val];
|
2011-06-12 03:04:46 -07:00
|
|
|
fun_body =
|
2013-03-19 00:16:46 -07:00
|
|
|
if arity - num > 2 && arity <= max_arity_optimized then
|
2016-06-27 00:49:26 -07:00
|
|
|
Cop(Calloc Debuginfo.none,
|
|
|
|
[alloc_closure_header 5 Debuginfo.none;
|
2011-06-12 03:04:46 -07:00
|
|
|
Cconst_symbol(name1 ^ "_" ^ string_of_int (num+1));
|
|
|
|
int_const (arity - num - 1);
|
|
|
|
Cconst_symbol(name1 ^ "_" ^ string_of_int (num+1) ^ "_app");
|
2012-07-30 11:04:46 -07:00
|
|
|
Cvar arg; Cvar clos])
|
|
|
|
else
|
2016-06-27 00:49:26 -07:00
|
|
|
Cop(Calloc Debuginfo.none,
|
|
|
|
[alloc_closure_header 4 Debuginfo.none;
|
1995-07-02 09:41:48 -07:00
|
|
|
Cconst_symbol(name1 ^ "_" ^ string_of_int (num+1));
|
|
|
|
int_const 1; Cvar arg; Cvar clos]);
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = true;
|
|
|
|
fun_dbg = Debuginfo.none }
|
2011-06-12 03:04:46 -07:00
|
|
|
::
|
2013-03-19 00:16:46 -07:00
|
|
|
(if arity <= max_arity_optimized && arity - num > 2 then
|
2012-07-30 11:04:46 -07:00
|
|
|
let rec iter i =
|
|
|
|
if i <= arity then
|
|
|
|
let arg = Ident.create (Printf.sprintf "arg%d" i) in
|
2014-11-06 00:54:14 -08:00
|
|
|
(arg, typ_val) :: iter (i+1)
|
2012-07-30 11:04:46 -07:00
|
|
|
else []
|
|
|
|
in
|
|
|
|
let direct_args = iter (num+2) in
|
|
|
|
let rec iter i args clos =
|
|
|
|
if i = 0 then
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Capply(typ_val, Debuginfo.none),
|
2012-07-30 11:04:46 -07:00
|
|
|
(get_field (Cvar clos) 2) :: args @ [Cvar clos])
|
|
|
|
else
|
|
|
|
let newclos = Ident.create "clos" in
|
|
|
|
Clet(newclos,
|
|
|
|
get_field (Cvar clos) 4,
|
|
|
|
iter (i-1) (get_field (Cvar clos) 3 :: args) newclos)
|
|
|
|
in
|
|
|
|
let cf =
|
|
|
|
Cfunction
|
|
|
|
{fun_name = name1 ^ "_" ^ string_of_int (num+1) ^ "_app";
|
2014-11-06 00:54:14 -08:00
|
|
|
fun_args = direct_args @ [clos, typ_val];
|
2012-07-30 11:04:46 -07:00
|
|
|
fun_body = iter (num+1)
|
|
|
|
(List.map (fun (arg,_) -> Cvar arg) direct_args) clos;
|
|
|
|
fun_fast = true;
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_dbg = Debuginfo.none }
|
2012-07-30 11:04:46 -07:00
|
|
|
in
|
|
|
|
cf :: intermediate_curry_functions arity (num+1)
|
2011-06-12 03:04:46 -07:00
|
|
|
else
|
2012-07-30 11:04:46 -07:00
|
|
|
intermediate_curry_functions arity (num+1))
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
2006-01-04 08:55:50 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let curry_function arity =
|
2016-01-06 10:20:49 -08:00
|
|
|
assert(arity <> 0);
|
|
|
|
(* Functions with arity = 0 does not have a curry_function *)
|
|
|
|
if arity > 0
|
1996-10-22 06:36:12 -07:00
|
|
|
then intermediate_curry_functions arity 0
|
|
|
|
else [tuplify_function (-arity)]
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
|
|
|
|
module IntSet = Set.Make(
|
|
|
|
struct
|
|
|
|
type t = int
|
2013-03-19 00:22:12 -07:00
|
|
|
let compare (x:t) y = compare x y
|
2007-11-06 07:16:56 -08:00
|
|
|
end)
|
|
|
|
|
|
|
|
let default_apply = IntSet.add 2 (IntSet.add 3 IntSet.empty)
|
2009-05-20 04:52:42 -07:00
|
|
|
(* These apply funs are always present in the main program because
|
|
|
|
the run-time system needs them (cf. asmrun/<arch>.S) . *)
|
2007-11-06 07:16:56 -08:00
|
|
|
|
|
|
|
let generic_functions shared units =
|
|
|
|
let (apply,send,curry) =
|
|
|
|
List.fold_left
|
|
|
|
(fun (apply,send,curry) ui ->
|
2010-05-19 04:29:38 -07:00
|
|
|
List.fold_right IntSet.add ui.ui_apply_fun apply,
|
|
|
|
List.fold_right IntSet.add ui.ui_send_fun send,
|
|
|
|
List.fold_right IntSet.add ui.ui_curry_fun curry)
|
2007-11-06 07:16:56 -08:00
|
|
|
(IntSet.empty,IntSet.empty,IntSet.empty)
|
2009-05-20 04:52:42 -07:00
|
|
|
units in
|
|
|
|
let apply = if shared then apply else IntSet.union apply default_apply in
|
2007-11-06 07:16:56 -08:00
|
|
|
let accu = IntSet.fold (fun n accu -> apply_function n :: accu) apply [] in
|
|
|
|
let accu = IntSet.fold (fun n accu -> send_function n :: accu) send accu in
|
|
|
|
IntSet.fold (fun n accu -> curry_function n @ accu) curry accu
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Generate the entry point *)
|
|
|
|
|
|
|
|
let entry_point namelist =
|
2000-01-05 05:16:31 -08:00
|
|
|
let incr_global_inited =
|
2015-12-18 08:42:40 -08:00
|
|
|
Cop(Cstore (Word_int, Assignment),
|
2000-02-04 04:43:18 -08:00
|
|
|
[Cconst_symbol "caml_globals_inited";
|
2014-11-06 00:54:14 -08:00
|
|
|
Cop(Caddi, [Cop(Cload Word_int, [Cconst_symbol "caml_globals_inited"]);
|
2000-02-04 04:43:18 -08:00
|
|
|
Cconst_int 1])]) in
|
1995-07-02 09:41:48 -07:00
|
|
|
let body =
|
|
|
|
List.fold_right
|
|
|
|
(fun name next ->
|
2004-01-05 14:20:34 -08:00
|
|
|
let entry_sym = Compilenv.make_symbol ~unitname:name (Some "entry") in
|
2007-01-29 04:11:18 -08:00
|
|
|
Csequence(Cop(Capply(typ_void, Debuginfo.none),
|
|
|
|
[Cconst_symbol entry_sym]),
|
2000-01-05 05:16:31 -08:00
|
|
|
Csequence(incr_global_inited, next)))
|
|
|
|
namelist (Cconst_int 1) in
|
1995-07-02 09:41:48 -07:00
|
|
|
Cfunction {fun_name = "caml_program";
|
|
|
|
fun_args = [];
|
|
|
|
fun_body = body;
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_fast = false;
|
|
|
|
fun_dbg = Debuginfo.none }
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1996-02-20 02:59:12 -08:00
|
|
|
(* Generate the table of globals *)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2003-04-25 05:27:31 -07:00
|
|
|
let cint_zero = Cint 0n
|
1997-03-04 02:19:51 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let global_table namelist =
|
2004-01-05 14:20:34 -08:00
|
|
|
let mksym name =
|
2015-10-23 08:16:22 -07:00
|
|
|
Csymbol_address (Compilenv.make_symbol ~unitname:name (Some "gc_roots"))
|
2004-01-05 14:20:34 -08:00
|
|
|
in
|
2002-11-24 07:55:26 -08:00
|
|
|
Cdata(Cglobal_symbol "caml_globals" ::
|
|
|
|
Cdefine_symbol "caml_globals" ::
|
2004-01-05 14:20:34 -08:00
|
|
|
List.map mksym namelist @
|
1997-03-04 02:19:51 -08:00
|
|
|
[cint_zero])
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2008-01-31 01:13:19 -08:00
|
|
|
let reference_symbols namelist =
|
|
|
|
let mksym name = Csymbol_address name in
|
|
|
|
Cdata(List.map mksym namelist)
|
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
let global_data name v =
|
2016-01-14 07:16:15 -08:00
|
|
|
Cdata(emit_structured_constant (name, Global)
|
2014-03-06 09:03:16 -08:00
|
|
|
(Uconst_string (Marshal.to_string v [])) [])
|
2007-11-06 07:16:56 -08:00
|
|
|
|
|
|
|
let globals_map v = global_data "caml_globals_map" v
|
1998-11-20 07:35:04 -08:00
|
|
|
|
1996-02-20 02:59:12 -08:00
|
|
|
(* Generate the master table of frame descriptors *)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let frame_table namelist =
|
2004-01-05 14:20:34 -08:00
|
|
|
let mksym name =
|
|
|
|
Csymbol_address (Compilenv.make_symbol ~unitname:name (Some "frametable"))
|
|
|
|
in
|
2002-11-24 07:55:26 -08:00
|
|
|
Cdata(Cglobal_symbol "caml_frametable" ::
|
|
|
|
Cdefine_symbol "caml_frametable" ::
|
2004-01-05 14:20:34 -08:00
|
|
|
List.map mksym namelist
|
2004-01-04 06:32:34 -08:00
|
|
|
@ [cint_zero])
|
1995-07-18 01:40:44 -07:00
|
|
|
|
2016-07-29 07:07:10 -07:00
|
|
|
(* Generate the master table of Spacetime shapes *)
|
|
|
|
|
|
|
|
let spacetime_shapes namelist =
|
|
|
|
let mksym name =
|
|
|
|
Csymbol_address (
|
|
|
|
Compilenv.make_symbol ~unitname:name (Some "spacetime_shapes"))
|
|
|
|
in
|
|
|
|
Cdata(Cglobal_symbol "caml_spacetime_shapes" ::
|
|
|
|
Cdefine_symbol "caml_spacetime_shapes" ::
|
|
|
|
List.map mksym namelist
|
|
|
|
@ [cint_zero])
|
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
(* Generate the table of module data and code segments *)
|
1996-02-20 02:59:12 -08:00
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
let segment_table namelist symbol begname endname =
|
2004-01-05 14:20:34 -08:00
|
|
|
let addsyms name lst =
|
|
|
|
Csymbol_address (Compilenv.make_symbol ~unitname:name (Some begname)) ::
|
|
|
|
Csymbol_address (Compilenv.make_symbol ~unitname:name (Some endname)) ::
|
|
|
|
lst
|
|
|
|
in
|
2002-11-24 07:55:26 -08:00
|
|
|
Cdata(Cglobal_symbol symbol ::
|
|
|
|
Cdefine_symbol symbol ::
|
2004-01-05 14:20:34 -08:00
|
|
|
List.fold_right addsyms namelist [cint_zero])
|
1996-02-20 02:59:12 -08:00
|
|
|
|
1997-07-02 11:16:15 -07:00
|
|
|
let data_segment_table namelist =
|
2004-01-05 14:20:34 -08:00
|
|
|
segment_table namelist "caml_data_segments" "data_begin" "data_end"
|
1997-07-02 11:16:15 -07:00
|
|
|
|
|
|
|
let code_segment_table namelist =
|
2004-01-05 14:20:34 -08:00
|
|
|
segment_table namelist "caml_code_segments" "code_begin" "code_end"
|
1997-07-02 11:16:15 -07:00
|
|
|
|
1995-07-18 01:40:44 -07:00
|
|
|
(* Initialize a predefined exception *)
|
|
|
|
|
2013-10-23 07:28:31 -07:00
|
|
|
let predef_exception i name =
|
2004-01-04 06:32:34 -08:00
|
|
|
let symname = "caml_exn_" ^ name in
|
2014-03-06 09:03:16 -08:00
|
|
|
let cst = Uconst_string name in
|
|
|
|
let label = Compilenv.new_const_symbol () in
|
2016-01-14 07:16:15 -08:00
|
|
|
let cont = emit_structured_constant (label, Not_global) cst [] in
|
|
|
|
Cdata(emit_structured_constant (symname, Global)
|
2014-03-06 09:03:16 -08:00
|
|
|
(Uconst_block(Obj.object_tag,
|
|
|
|
[
|
2016-01-04 07:02:35 -08:00
|
|
|
Uconst_ref(label, Some cst);
|
2014-03-06 09:03:16 -08:00
|
|
|
Uconst_int (-i-1);
|
|
|
|
])) cont)
|
2007-11-06 07:16:56 -08:00
|
|
|
|
|
|
|
(* Header for a plugin *)
|
|
|
|
|
|
|
|
let plugin_header units =
|
|
|
|
let mk (ui,crc) =
|
2010-05-19 04:29:38 -07:00
|
|
|
{ dynu_name = ui.ui_name;
|
|
|
|
dynu_crc = crc;
|
|
|
|
dynu_imports_cmi = ui.ui_imports_cmi;
|
|
|
|
dynu_imports_cmx = ui.ui_imports_cmx;
|
|
|
|
dynu_defines = ui.ui_defines
|
2007-11-06 07:16:56 -08:00
|
|
|
} in
|
|
|
|
global_data "caml_plugin_header"
|
2010-05-19 04:29:38 -07:00
|
|
|
{ dynu_magic = Config.cmxs_magic_number; dynu_units = List.map mk units }
|