1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Translation from typed abstract syntax to lambda terms,
|
|
|
|
for the core language *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Asttypes
|
1995-07-27 10:40:34 -07:00
|
|
|
open Primitive
|
1995-05-04 03:15:53 -07:00
|
|
|
open Path
|
|
|
|
open Typedtree
|
|
|
|
open Lambda
|
1996-04-22 04:15:41 -07:00
|
|
|
open Translobj
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type error =
|
|
|
|
Illegal_letrec_pat
|
|
|
|
| Illegal_letrec_expr
|
1996-04-22 04:15:41 -07:00
|
|
|
| Free_super_var
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
exception Error of Location.t * error
|
|
|
|
|
|
|
|
(* Translation of primitives *)
|
|
|
|
|
|
|
|
let comparisons_table = create_hashtable 11 [
|
|
|
|
"%equal",
|
1995-07-27 10:40:34 -07:00
|
|
|
(Pccall{prim_name = "equal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false},
|
1995-07-10 02:48:27 -07:00
|
|
|
Pintcomp Ceq,
|
|
|
|
Pfloatcomp Ceq,
|
1995-07-27 10:40:34 -07:00
|
|
|
Pccall{prim_name = "string_equal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false});
|
1995-05-04 03:15:53 -07:00
|
|
|
"%notequal",
|
1995-07-27 10:40:34 -07:00
|
|
|
(Pccall{prim_name = "notequal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false},
|
1995-07-10 02:48:27 -07:00
|
|
|
Pintcomp Cneq,
|
|
|
|
Pfloatcomp Cneq,
|
1995-07-25 04:38:42 -07:00
|
|
|
Pccall{prim_name = "string_notequal"; prim_arity = 2;
|
1995-07-27 10:40:34 -07:00
|
|
|
prim_alloc = false; prim_native_name = "";
|
|
|
|
prim_native_float = false});
|
1995-05-04 03:15:53 -07:00
|
|
|
"%lessthan",
|
1995-07-27 10:40:34 -07:00
|
|
|
(Pccall{prim_name = "lessthan"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false},
|
1995-07-10 02:48:27 -07:00
|
|
|
Pintcomp Clt,
|
|
|
|
Pfloatcomp Clt,
|
1995-07-27 10:40:34 -07:00
|
|
|
Pccall{prim_name = "lessthan"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false});
|
1995-05-04 03:15:53 -07:00
|
|
|
"%greaterthan",
|
1995-07-27 10:40:34 -07:00
|
|
|
(Pccall{prim_name = "greaterthan"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false},
|
1995-07-10 02:48:27 -07:00
|
|
|
Pintcomp Cgt,
|
|
|
|
Pfloatcomp Cgt,
|
1995-07-27 10:40:34 -07:00
|
|
|
Pccall{prim_name = "greaterthan"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false});
|
1995-05-04 03:15:53 -07:00
|
|
|
"%lessequal",
|
1995-07-27 10:40:34 -07:00
|
|
|
(Pccall{prim_name = "lessequal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false},
|
1995-07-10 02:48:27 -07:00
|
|
|
Pintcomp Cle,
|
|
|
|
Pfloatcomp Cle,
|
1995-07-27 10:40:34 -07:00
|
|
|
Pccall{prim_name = "lessequal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false});
|
1995-05-04 03:15:53 -07:00
|
|
|
"%greaterequal",
|
1995-07-27 10:40:34 -07:00
|
|
|
(Pccall{prim_name = "greaterequal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false},
|
1995-07-10 02:48:27 -07:00
|
|
|
Pintcomp Cge,
|
|
|
|
Pfloatcomp Cge,
|
1995-07-27 10:40:34 -07:00
|
|
|
Pccall{prim_name = "greaterequal"; prim_arity = 2; prim_alloc = false;
|
|
|
|
prim_native_name = ""; prim_native_float = false})
|
1995-05-04 03:15:53 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
let primitives_table = create_hashtable 31 [
|
|
|
|
"%identity", Pidentity;
|
|
|
|
"%field0", Pfield 0;
|
|
|
|
"%field1", Pfield 1;
|
1995-07-10 02:48:27 -07:00
|
|
|
"%setfield0", Psetfield(0, true);
|
1995-11-09 05:22:16 -08:00
|
|
|
"%makeblock", Pmakeblock(0, Immutable);
|
|
|
|
"%makemutable", Pmakeblock(0, Mutable);
|
1995-05-04 03:15:53 -07:00
|
|
|
"%raise", Praise;
|
|
|
|
"%sequand", Psequand;
|
|
|
|
"%sequor", Psequor;
|
|
|
|
"%boolnot", Pnot;
|
|
|
|
"%negint", Pnegint;
|
|
|
|
"%succint", Poffsetint 1;
|
|
|
|
"%predint", Poffsetint(-1);
|
|
|
|
"%addint", Paddint;
|
|
|
|
"%subint", Psubint;
|
|
|
|
"%mulint", Pmulint;
|
|
|
|
"%divint", Pdivint;
|
|
|
|
"%modint", Pmodint;
|
|
|
|
"%andint", Pandint;
|
|
|
|
"%orint", Porint;
|
|
|
|
"%xorint", Pxorint;
|
|
|
|
"%lslint", Plslint;
|
|
|
|
"%lsrint", Plsrint;
|
|
|
|
"%asrint", Pasrint;
|
1995-06-18 07:44:56 -07:00
|
|
|
"%eq", Pintcomp Ceq;
|
|
|
|
"%noteq", Pintcomp Cneq;
|
|
|
|
"%ltint", Pintcomp Clt;
|
|
|
|
"%leint", Pintcomp Cle;
|
|
|
|
"%gtint", Pintcomp Cgt;
|
|
|
|
"%geint", Pintcomp Cge;
|
1995-05-04 03:15:53 -07:00
|
|
|
"%incr", Poffsetref(1);
|
|
|
|
"%decr", Poffsetref(-1);
|
1995-07-11 01:53:14 -07:00
|
|
|
"%intoffloat", Pintoffloat;
|
|
|
|
"%floatofint", Pfloatofint;
|
1995-07-02 09:45:21 -07:00
|
|
|
"%negfloat", Pnegfloat;
|
1996-03-07 05:45:57 -08:00
|
|
|
"%absfloat", Pabsfloat;
|
1995-06-18 07:44:56 -07:00
|
|
|
"%addfloat", Paddfloat;
|
|
|
|
"%subfloat", Psubfloat;
|
|
|
|
"%mulfloat", Pmulfloat;
|
|
|
|
"%divfloat", Pdivfloat;
|
|
|
|
"%eqfloat", Pfloatcomp Ceq;
|
|
|
|
"%noteqfloat", Pfloatcomp Cneq;
|
|
|
|
"%ltfloat", Pfloatcomp Clt;
|
|
|
|
"%lefloat", Pfloatcomp Cle;
|
|
|
|
"%gtfloat", Pfloatcomp Cgt;
|
|
|
|
"%gefloat", Pfloatcomp Cge;
|
1995-07-10 02:48:27 -07:00
|
|
|
"%string_length", Pstringlength;
|
1995-07-27 10:40:34 -07:00
|
|
|
"%string_safe_get", Pstringrefs;
|
|
|
|
"%string_safe_set", Pstringsets;
|
|
|
|
"%string_unsafe_get", Pstringrefu;
|
|
|
|
"%string_unsafe_set", Pstringsetu;
|
|
|
|
"%array_length", Parraylength Pgenarray;
|
|
|
|
"%array_safe_get", Parrayrefs Pgenarray;
|
|
|
|
"%array_safe_set", Parraysets Pgenarray;
|
|
|
|
"%array_unsafe_get", Parrayrefu Pgenarray;
|
|
|
|
"%array_unsafe_set", Parraysetu Pgenarray;
|
|
|
|
"%obj_size", Parraylength Paddrarray;
|
|
|
|
"%obj_field", Parrayrefu Paddrarray;
|
|
|
|
"%obj_set_field", Parraysetu Paddrarray
|
1995-05-04 03:15:53 -07:00
|
|
|
]
|
|
|
|
|
|
|
|
let same_base_type ty1 ty2 =
|
1996-04-22 04:15:41 -07:00
|
|
|
match ((Ctype.repr ty1).desc, (Ctype.repr ty2).desc) with
|
|
|
|
(Tconstr(p1, [], _), Tconstr(p2, [], _)) -> Path.same p1 p2
|
1995-05-04 03:15:53 -07:00
|
|
|
| (_, _) -> false
|
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
let maybe_pointer arg =
|
1995-07-27 10:40:34 -07:00
|
|
|
not(same_base_type arg.exp_type Predef.type_int or
|
|
|
|
same_base_type arg.exp_type Predef.type_char)
|
|
|
|
|
|
|
|
let array_kind arg =
|
1996-04-22 04:15:41 -07:00
|
|
|
match (Ctype.repr arg.exp_type).desc with
|
|
|
|
Tconstr(p, [ty], _) when Path.same p Predef.path_array ->
|
|
|
|
begin match (Ctype.repr ty).desc with
|
|
|
|
Tvar -> Pgenarray
|
|
|
|
| Tconstr(p, _, _) ->
|
1995-07-27 10:40:34 -07:00
|
|
|
if Path.same p Predef.path_int or Path.same p Predef.path_char then
|
|
|
|
Pintarray
|
|
|
|
else if Path.same p Predef.path_float then
|
|
|
|
Pfloatarray
|
|
|
|
else
|
|
|
|
Paddrarray
|
|
|
|
| _ -> Paddrarray
|
|
|
|
end
|
1995-07-28 05:25:17 -07:00
|
|
|
| _ -> Pgenarray (* This can happen with abbreviations that we can't expand
|
1995-07-28 05:22:33 -07:00
|
|
|
here because the typing environment is lost *)
|
1995-07-10 02:48:27 -07:00
|
|
|
|
1995-10-03 07:03:30 -07:00
|
|
|
let prim_makearray =
|
|
|
|
{ prim_name = "make_vect"; prim_arity = 2; prim_alloc = true;
|
|
|
|
prim_native_name = ""; prim_native_float = false }
|
|
|
|
|
1995-07-25 04:38:42 -07:00
|
|
|
let transl_prim prim args =
|
1995-05-04 03:15:53 -07:00
|
|
|
try
|
1995-07-10 02:48:27 -07:00
|
|
|
let (gencomp, intcomp, floatcomp, stringcomp) =
|
1995-07-25 04:38:42 -07:00
|
|
|
Hashtbl.find comparisons_table prim.prim_name in
|
1995-05-04 03:15:53 -07:00
|
|
|
match args with
|
1995-07-27 10:40:34 -07:00
|
|
|
[arg1; {exp_desc = Texp_construct(cstr, [])}] ->
|
|
|
|
intcomp
|
|
|
|
| [{exp_desc = Texp_construct(cstr, [])}; arg2] ->
|
|
|
|
intcomp
|
|
|
|
| [arg1; arg2] when same_base_type arg1.exp_type Predef.type_int
|
1995-05-04 03:15:53 -07:00
|
|
|
or same_base_type arg1.exp_type Predef.type_char ->
|
|
|
|
intcomp
|
|
|
|
| [arg1; arg2] when same_base_type arg1.exp_type Predef.type_float ->
|
|
|
|
floatcomp
|
1995-07-10 02:48:27 -07:00
|
|
|
| [arg1; arg2] when same_base_type arg1.exp_type Predef.type_string ->
|
|
|
|
stringcomp
|
1995-05-04 03:15:53 -07:00
|
|
|
| _ ->
|
|
|
|
gencomp
|
|
|
|
with Not_found ->
|
|
|
|
try
|
1995-07-25 04:38:42 -07:00
|
|
|
let p = Hashtbl.find primitives_table prim.prim_name in
|
1995-07-27 10:40:34 -07:00
|
|
|
(* Try strength reduction based on the type of the argument *)
|
1995-07-10 02:48:27 -07:00
|
|
|
begin match (p, args) with
|
1995-07-27 10:40:34 -07:00
|
|
|
(Psetfield(n, _), [arg1; arg2]) -> Psetfield(n, maybe_pointer arg2)
|
|
|
|
| (Parraylength Pgenarray, [arg]) -> Parraylength(array_kind arg)
|
|
|
|
| (Parrayrefu Pgenarray, arg1 :: _) -> Parrayrefu(array_kind arg1)
|
|
|
|
| (Parraysetu Pgenarray, arg1 :: _) -> Parraysetu(array_kind arg1)
|
|
|
|
| (Parrayrefs Pgenarray, arg1 :: _) -> Parrayrefs(array_kind arg1)
|
|
|
|
| (Parraysets Pgenarray, arg1 :: _) -> Parraysets(array_kind arg1)
|
1995-07-10 02:48:27 -07:00
|
|
|
| _ -> p
|
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
with Not_found ->
|
1995-07-25 04:38:42 -07:00
|
|
|
Pccall prim
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-10-23 09:56:52 -07:00
|
|
|
(* Eta-expand a primitive without knowing the types of its arguments *)
|
|
|
|
|
|
|
|
let transl_primitive p =
|
|
|
|
let prim =
|
|
|
|
try
|
|
|
|
let (gencomp, intcomp, floatcomp, stringcomp) =
|
|
|
|
Hashtbl.find comparisons_table p.prim_name in
|
|
|
|
gencomp
|
|
|
|
with Not_found ->
|
|
|
|
try
|
|
|
|
Hashtbl.find primitives_table p.prim_name
|
|
|
|
with Not_found ->
|
|
|
|
Pccall p in
|
1995-12-19 02:19:12 -08:00
|
|
|
let rec make_params n =
|
1996-04-22 04:15:41 -07:00
|
|
|
if n <= 0 then [] else Ident.create "prim" :: make_params (n-1) in
|
1995-12-19 02:19:12 -08:00
|
|
|
let params = make_params p.prim_arity in
|
|
|
|
Lfunction(params, Lprim(prim, List.map (fun id -> Lvar id) params))
|
1995-10-23 09:56:52 -07:00
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* To check the well-formedness of r.h.s. of "let rec" definitions *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-05-07 01:14:10 -07:00
|
|
|
let check_recursive_lambda idlist lam =
|
1995-07-02 09:45:21 -07:00
|
|
|
let rec check_top = function
|
1995-12-19 02:19:12 -08:00
|
|
|
Lfunction(params, body) as funct -> true
|
1996-05-07 01:14:10 -07:00
|
|
|
| Lprim(Pmakeblock(tag, mut), args) -> List.for_all check args
|
|
|
|
| Lprim(Pmakearray kind, args) -> List.for_all check args
|
|
|
|
| Llet(str, id, arg, body) -> check arg & check_top body
|
1995-07-02 09:45:21 -07:00
|
|
|
| _ -> false
|
1996-05-07 01:14:10 -07:00
|
|
|
and check = function
|
|
|
|
Lvar _ -> true
|
1995-07-02 09:45:21 -07:00
|
|
|
| Lconst cst -> true
|
1996-05-07 01:14:10 -07:00
|
|
|
| Lfunction(params, body) -> true
|
|
|
|
| Llet(_, _, arg, body) -> check arg & check body
|
|
|
|
| Lprim(Pmakeblock(tag, mut), args) -> List.for_all check args
|
|
|
|
| Lprim(Pmakearray kind, args) -> List.for_all check args
|
|
|
|
| lam ->
|
|
|
|
let fv = free_variables lam in
|
|
|
|
List.for_all (fun id -> not(IdentSet.mem id fv)) idlist
|
1995-07-02 09:45:21 -07:00
|
|
|
in check_top lam
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* To propagate structured constants *)
|
|
|
|
|
|
|
|
exception Not_constant
|
|
|
|
|
1995-07-27 10:40:34 -07:00
|
|
|
let extract_constant = function
|
|
|
|
Lconst sc -> sc
|
|
|
|
| _ -> raise Not_constant
|
|
|
|
|
|
|
|
let extract_float = function
|
|
|
|
Const_base(Const_float f) -> f
|
|
|
|
| _ -> fatal_error "Translcore.extract_float"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
(* To find reasonable names for let-bound and lambda-bound idents *)
|
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
let rec name_pattern default = function
|
1996-04-22 04:15:41 -07:00
|
|
|
[] -> Ident.create default
|
1995-12-15 02:18:29 -08:00
|
|
|
| (p, e) :: rem ->
|
|
|
|
match p.pat_desc with
|
|
|
|
Tpat_var id -> id
|
|
|
|
| Tpat_alias(p, id) -> id
|
|
|
|
| _ -> name_pattern default rem
|
1995-11-25 07:38:43 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Translation of expressions *)
|
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
let rec transl_exp e =
|
1995-05-04 03:15:53 -07:00
|
|
|
match e.exp_desc with
|
1996-04-22 04:15:41 -07:00
|
|
|
Texp_ident(path, {val_kind = Val_prim p}) ->
|
1995-10-23 09:56:52 -07:00
|
|
|
transl_primitive p
|
1996-04-22 04:15:41 -07:00
|
|
|
| Texp_ident(path, {val_kind = Val_anc _}) ->
|
|
|
|
raise(Error(e.exp_loc, Free_super_var))
|
1995-10-23 09:56:52 -07:00
|
|
|
| Texp_ident(path, desc) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_path path
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_constant cst ->
|
|
|
|
Lconst(Const_base cst)
|
|
|
|
| Texp_let(rec_flag, pat_expr_list, body) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_let rec_flag pat_expr_list (transl_exp body)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_function pat_expr_list ->
|
1995-12-19 02:19:12 -08:00
|
|
|
let (params, body) = transl_function e.exp_loc pat_expr_list in
|
|
|
|
Lfunction(params, body)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Texp_apply({exp_desc = Texp_ident(path, {val_kind = Val_prim p})}, args)
|
1995-10-23 09:56:52 -07:00
|
|
|
when List.length args = p.prim_arity ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lprim(transl_prim p args, transl_list args)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_apply(funct, args) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match transl_exp funct with
|
|
|
|
Lapply(lfunct, largs) ->
|
|
|
|
Lapply(lfunct, largs @ transl_list args)
|
|
|
|
| Lsend(lmet, lobj, largs) ->
|
|
|
|
Lsend(lmet, lobj, largs @ transl_list args)
|
|
|
|
| lexp ->
|
|
|
|
Lapply(lexp, transl_list args)
|
|
|
|
end
|
1995-07-02 09:45:21 -07:00
|
|
|
| Texp_match({exp_desc = Texp_tuple argl} as arg, pat_expr_list) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Matching.for_multiple_match e.exp_loc
|
|
|
|
(transl_list argl) (transl_cases pat_expr_list)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_match(arg, pat_expr_list) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Matching.for_function e.exp_loc
|
|
|
|
(transl_exp arg) (transl_cases pat_expr_list)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_try(body, pat_expr_list) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let id = Ident.create "exn" in
|
1995-12-15 02:18:29 -08:00
|
|
|
Ltrywith(transl_exp body, id,
|
|
|
|
Matching.for_trywith (Lvar id) (transl_cases pat_expr_list))
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_tuple el ->
|
1995-12-15 02:18:29 -08:00
|
|
|
let ll = transl_list el in
|
1995-05-04 03:15:53 -07:00
|
|
|
begin try
|
|
|
|
Lconst(Const_block(0, List.map extract_constant ll))
|
|
|
|
with Not_constant ->
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(0, Immutable), ll)
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
| Texp_construct(cstr, args) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
let ll = transl_list args in
|
1995-05-04 03:15:53 -07:00
|
|
|
begin match cstr.cstr_tag with
|
1995-06-18 07:44:56 -07:00
|
|
|
Cstr_constant n ->
|
1995-07-02 09:45:21 -07:00
|
|
|
Lconst(Const_pointer n)
|
1995-06-18 07:44:56 -07:00
|
|
|
| Cstr_block n ->
|
1995-05-04 03:15:53 -07:00
|
|
|
begin try
|
|
|
|
Lconst(Const_block(n, List.map extract_constant ll))
|
|
|
|
with Not_constant ->
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(n, Immutable), ll)
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
| Cstr_exception path ->
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(0, Immutable), transl_path path :: ll)
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
1995-07-27 10:40:34 -07:00
|
|
|
| Texp_record ((lbl1, _) :: _ as lbl_expr_list) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let lv = Array.create (Array.length lbl1.lbl_all) Lstaticfail in
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter
|
1995-12-15 02:18:29 -08:00
|
|
|
(fun (lbl, expr) -> lv.(lbl.lbl_pos) <- transl_exp expr)
|
1995-05-04 03:15:53 -07:00
|
|
|
lbl_expr_list;
|
|
|
|
let ll = Array.to_list lv in
|
1995-11-09 05:22:16 -08:00
|
|
|
if List.exists (fun (lbl, expr) -> lbl.lbl_mut = Mutable) lbl_expr_list
|
|
|
|
then begin
|
1995-07-27 10:40:34 -07:00
|
|
|
match lbl1.lbl_repres with
|
1995-11-09 05:22:16 -08:00
|
|
|
Record_regular -> Lprim(Pmakeblock(0, Mutable), ll)
|
1995-07-27 10:40:34 -07:00
|
|
|
| Record_float -> Lprim(Pmakearray Pfloatarray, ll)
|
1995-11-09 05:22:16 -08:00
|
|
|
end else begin
|
|
|
|
try
|
|
|
|
let cl = List.map extract_constant ll in
|
|
|
|
match lbl1.lbl_repres with
|
|
|
|
Record_regular -> Lconst(Const_block(0, cl))
|
|
|
|
| Record_float ->
|
|
|
|
Lconst(Const_float_array(List.map extract_float cl))
|
|
|
|
with Not_constant ->
|
|
|
|
match lbl1.lbl_repres with
|
|
|
|
Record_regular -> Lprim(Pmakeblock(0, Immutable), ll)
|
|
|
|
| Record_float -> Lprim(Pmakearray Pfloatarray, ll)
|
1995-07-27 10:40:34 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_field(arg, lbl) ->
|
1995-07-27 10:40:34 -07:00
|
|
|
let access =
|
|
|
|
match lbl.lbl_repres with
|
|
|
|
Record_regular -> Pfield lbl.lbl_pos
|
|
|
|
| Record_float -> Pfloatfield lbl.lbl_pos in
|
1995-12-15 02:18:29 -08:00
|
|
|
Lprim(access, [transl_exp arg])
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_setfield(arg, lbl, newval) ->
|
1995-07-27 10:40:34 -07:00
|
|
|
let access =
|
|
|
|
match lbl.lbl_repres with
|
|
|
|
Record_regular -> Psetfield(lbl.lbl_pos, maybe_pointer newval)
|
|
|
|
| Record_float -> Psetfloatfield lbl.lbl_pos in
|
1995-12-15 02:18:29 -08:00
|
|
|
Lprim(access, [transl_exp arg; transl_exp newval])
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_array expr_list ->
|
1995-10-03 07:03:30 -07:00
|
|
|
let kind = array_kind e in
|
|
|
|
let len = List.length expr_list in
|
|
|
|
if len <= Config.max_young_wosize then
|
1995-12-15 02:18:29 -08:00
|
|
|
Lprim(Pmakearray kind, transl_list expr_list)
|
1995-10-03 07:03:30 -07:00
|
|
|
else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let v = Ident.create "makearray" in
|
1995-10-03 07:03:30 -07:00
|
|
|
let rec fill_fields pos = function
|
|
|
|
[] ->
|
|
|
|
Lvar v
|
|
|
|
| arg :: rem ->
|
|
|
|
Lsequence(Lprim(Parraysetu kind,
|
|
|
|
[Lvar v;
|
|
|
|
Lconst(Const_base(Const_int pos));
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_exp arg]),
|
1995-10-03 07:03:30 -07:00
|
|
|
fill_fields (pos+1) rem) in
|
1995-12-15 02:18:29 -08:00
|
|
|
Llet(Strict, v,
|
|
|
|
Lprim(Pccall prim_makearray,
|
|
|
|
[Lconst(Const_base(Const_int len));
|
|
|
|
transl_exp (List.hd expr_list)]),
|
|
|
|
fill_fields 1 (List.tl expr_list))
|
1995-10-03 07:03:30 -07:00
|
|
|
end
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_ifthenelse(cond, ifso, Some ifnot) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lifthenelse(transl_exp cond, transl_exp ifso, transl_exp ifnot)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_ifthenelse(cond, ifso, None) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lifthenelse(transl_exp cond, transl_exp ifso, lambda_unit)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_sequence(expr1, expr2) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lsequence(transl_exp expr1, transl_exp expr2)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_while(cond, body) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lwhile(transl_exp cond, transl_exp body)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_for(param, low, high, dir, body) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lfor(param, transl_exp low, transl_exp high, dir, transl_exp body)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Texp_when(cond, body) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lifthenelse(transl_exp cond, transl_exp body, Lstaticfail)
|
1996-04-22 04:15:41 -07:00
|
|
|
| Texp_send(expr, met) ->
|
|
|
|
Lsend(Lvar (meth met), transl_exp expr, [])
|
|
|
|
| Texp_new cl ->
|
|
|
|
Lprim(Pfield 0, [transl_path cl])
|
|
|
|
| Texp_instvar(path_self, path) ->
|
|
|
|
Lprim(Parrayrefu Paddrarray , [transl_path path_self; transl_path path])
|
|
|
|
| Texp_setinstvar(path_self, path, expr) ->
|
|
|
|
transl_setinstvar (transl_path path_self) path expr
|
|
|
|
| Texp_override(path_self, modifs) ->
|
|
|
|
let cpy = Ident.create "copy" in
|
|
|
|
Llet(Strict, cpy, Lapply(oo_prim "copy", [transl_path path_self]),
|
|
|
|
List.fold_right
|
|
|
|
(fun (path, expr) rem ->
|
|
|
|
Lsequence(transl_setinstvar (Lvar cpy) path expr,
|
|
|
|
rem))
|
|
|
|
modifs
|
|
|
|
(Lvar cpy))
|
1995-07-27 10:40:34 -07:00
|
|
|
| _ ->
|
|
|
|
fatal_error "Translcore.transl"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
and transl_list expr_list =
|
|
|
|
List.map transl_exp expr_list
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
and transl_cases pat_expr_list =
|
|
|
|
List.map (fun (pat, expr) -> (pat, transl_exp expr)) pat_expr_list
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-12-19 02:19:12 -08:00
|
|
|
and transl_function loc pat_expr_list =
|
|
|
|
let param = name_pattern "param" pat_expr_list in
|
|
|
|
match pat_expr_list with
|
|
|
|
[pat, ({exp_desc = Texp_function pl} as exp)] ->
|
|
|
|
let (params, body) = transl_function exp.exp_loc pl in
|
|
|
|
(param :: params, Matching.for_function loc (Lvar param) [pat, body])
|
|
|
|
| _ ->
|
|
|
|
([param], Matching.for_function loc (Lvar param)
|
|
|
|
(transl_cases pat_expr_list))
|
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
and transl_let rec_flag pat_expr_list body =
|
1995-05-04 03:15:53 -07:00
|
|
|
match rec_flag with
|
|
|
|
Nonrecursive ->
|
1995-12-15 02:18:29 -08:00
|
|
|
let rec transl = function
|
1995-05-04 03:15:53 -07:00
|
|
|
[] ->
|
1995-12-15 02:18:29 -08:00
|
|
|
body
|
1995-05-04 03:15:53 -07:00
|
|
|
| (pat, expr) :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Matching.for_let pat.pat_loc (transl_exp expr) pat (transl rem)
|
|
|
|
in transl pat_expr_list
|
1995-05-04 03:15:53 -07:00
|
|
|
| Recursive ->
|
1996-05-07 01:14:10 -07:00
|
|
|
let idlist =
|
|
|
|
List.map
|
|
|
|
(fun (pat, expr) ->
|
|
|
|
match pat.pat_desc with
|
|
|
|
Tpat_var id -> id
|
|
|
|
| _ -> raise(Error(pat.pat_loc, Illegal_letrec_pat)))
|
|
|
|
pat_expr_list in
|
|
|
|
let transl_case (pat, expr) id =
|
1995-12-15 02:18:29 -08:00
|
|
|
let lam = transl_exp expr in
|
1996-05-07 01:14:10 -07:00
|
|
|
if not (check_recursive_lambda idlist lam) then
|
1995-07-02 09:45:21 -07:00
|
|
|
raise(Error(expr.exp_loc, Illegal_letrec_expr));
|
|
|
|
(id, lam) in
|
1996-05-07 01:14:10 -07:00
|
|
|
Lletrec(List.map2 transl_case pat_expr_list idlist, body)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
and transl_setinstvar self var expr =
|
|
|
|
Lprim(Parraysetu (if maybe_pointer expr then Paddrarray else Pintarray),
|
|
|
|
[self; transl_path var; transl_exp expr])
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Compile an exception definition *)
|
|
|
|
|
|
|
|
let transl_exception id decl =
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(0, Immutable),
|
|
|
|
[Lconst(Const_base(Const_string(Ident.name id)))])
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
open Format
|
|
|
|
|
|
|
|
let report_error = function
|
|
|
|
Illegal_letrec_pat ->
|
|
|
|
print_string
|
|
|
|
"Only variables are allowed as left-hand side of `let rec'"
|
|
|
|
| Illegal_letrec_expr ->
|
|
|
|
print_string
|
|
|
|
"This kind of expression is not allowed as right-hand side of `let rec'"
|
1996-04-22 04:15:41 -07:00
|
|
|
| Free_super_var ->
|
|
|
|
print_string
|
|
|
|
"Ancestor names can only be used to select inherited methods"
|