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 module language *)
|
|
|
|
|
|
|
|
open Misc
|
1995-11-09 05:22:16 -08:00
|
|
|
open Asttypes
|
1996-09-23 04:30:27 -07:00
|
|
|
open Types
|
1995-05-04 03:15:53 -07:00
|
|
|
open Typedtree
|
|
|
|
open Lambda
|
1996-04-22 04:15:41 -07:00
|
|
|
open Translobj
|
1995-05-04 03:15:53 -07:00
|
|
|
open Translcore
|
1996-04-22 04:15:41 -07:00
|
|
|
open Translclass
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Compile a coercion *)
|
|
|
|
|
|
|
|
let rec apply_coercion restr arg =
|
|
|
|
match restr with
|
|
|
|
Tcoerce_none ->
|
|
|
|
arg
|
|
|
|
| Tcoerce_structure pos_cc_list ->
|
|
|
|
name_lambda arg (fun id ->
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(0, Immutable),
|
|
|
|
List.map (apply_coercion_field id) pos_cc_list))
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tcoerce_functor(cc_arg, cc_res) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let param = Ident.create "funarg" in
|
1995-05-04 03:15:53 -07:00
|
|
|
name_lambda arg (fun id ->
|
1996-10-22 06:36:59 -07:00
|
|
|
Lfunction(Curried, [param],
|
1995-05-04 03:15:53 -07:00
|
|
|
apply_coercion cc_res
|
|
|
|
(Lapply(Lvar id, [apply_coercion cc_arg (Lvar param)]))))
|
1995-10-23 09:56:52 -07:00
|
|
|
| Tcoerce_primitive p ->
|
|
|
|
fatal_error "Translmod.apply_coercion"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and apply_coercion_field id (pos, cc) =
|
1995-10-23 09:56:52 -07:00
|
|
|
match cc with
|
|
|
|
Tcoerce_primitive p -> transl_primitive p
|
|
|
|
| _ -> apply_coercion cc (Lprim(Pfield pos, [Lvar id]))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Compose two coercions
|
|
|
|
apply_coercion c1 (apply_coercion c2 e) behaves like
|
|
|
|
apply_coercion (compose_coercions c1 c2) e. *)
|
|
|
|
|
|
|
|
let rec compose_coercions c1 c2 =
|
|
|
|
match (c1, c2) with
|
|
|
|
(Tcoerce_none, c2) -> c2
|
|
|
|
| (c1, Tcoerce_none) -> c1
|
|
|
|
| (Tcoerce_structure pc1, Tcoerce_structure pc2) ->
|
|
|
|
let v2 = Array.of_list pc2 in
|
|
|
|
Tcoerce_structure
|
|
|
|
(List.map (fun (p1, c1) ->
|
|
|
|
let (p2, c2) = v2.(p1) in (p2, compose_coercions c1 c2))
|
|
|
|
pc1)
|
|
|
|
| (Tcoerce_functor(arg1, res1), Tcoerce_functor(arg2, res2)) ->
|
|
|
|
Tcoerce_functor(compose_coercions arg2 arg1,
|
|
|
|
compose_coercions res1 res2)
|
|
|
|
| (_, _) ->
|
|
|
|
fatal_error "Translmod.compose_coercions"
|
|
|
|
|
1995-11-05 09:32:12 -08:00
|
|
|
(* Record the primitive declarations occuring in the module compiled *)
|
1995-10-09 06:37:11 -07:00
|
|
|
|
1995-11-05 09:32:12 -08:00
|
|
|
let primitive_declarations = ref ([] : string list)
|
1995-10-09 06:37:11 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Compile a module expression *)
|
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
let rec transl_module cc mexp =
|
1995-05-04 03:15:53 -07:00
|
|
|
match mexp.mod_desc with
|
|
|
|
Tmod_ident path ->
|
|
|
|
apply_coercion cc (transl_path path)
|
|
|
|
| Tmod_structure str ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_structure [] cc str
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tmod_functor(param, mty, body) ->
|
|
|
|
begin match cc with
|
|
|
|
Tcoerce_none ->
|
1996-10-22 06:36:59 -07:00
|
|
|
Lfunction(Curried, [param], transl_module Tcoerce_none body)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tcoerce_functor(ccarg, ccres) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let param' = Ident.create "funarg" in
|
1996-10-22 06:36:59 -07:00
|
|
|
Lfunction(Curried, [param'],
|
1995-12-15 02:18:29 -08:00
|
|
|
Llet(Alias, param, apply_coercion ccarg (Lvar param'),
|
|
|
|
transl_module ccres body))
|
1995-11-06 03:07:13 -08:00
|
|
|
| _ ->
|
1995-05-04 03:15:53 -07:00
|
|
|
fatal_error "Translmod.transl_module"
|
|
|
|
end
|
|
|
|
| Tmod_apply(funct, arg, ccarg) ->
|
|
|
|
apply_coercion cc
|
1995-12-15 02:18:29 -08:00
|
|
|
(Lapply(transl_module Tcoerce_none funct, [transl_module ccarg arg]))
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tmod_constraint(arg, mty, ccarg) ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_module (compose_coercions cc ccarg) arg
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-12-15 02:18:29 -08:00
|
|
|
and transl_structure fields cc = function
|
1995-05-04 03:15:53 -07:00
|
|
|
[] ->
|
|
|
|
begin match cc with
|
|
|
|
Tcoerce_none ->
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(0, Immutable),
|
1995-12-15 02:18:29 -08:00
|
|
|
List.map (fun id -> Lvar id) (List.rev fields))
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tcoerce_structure pos_cc_list ->
|
|
|
|
let v = Array.of_list (List.rev fields) in
|
1995-11-09 05:22:16 -08:00
|
|
|
Lprim(Pmakeblock(0, Immutable),
|
1995-11-06 03:07:13 -08:00
|
|
|
List.map
|
|
|
|
(fun (pos, cc) ->
|
|
|
|
match cc with
|
|
|
|
Tcoerce_primitive p -> transl_primitive p
|
1995-12-15 02:18:29 -08:00
|
|
|
| _ -> apply_coercion cc (Lvar v.(pos)))
|
1995-11-06 03:07:13 -08:00
|
|
|
pos_cc_list)
|
1995-11-25 07:38:43 -08:00
|
|
|
| _ ->
|
1995-05-04 03:15:53 -07:00
|
|
|
fatal_error "Translmod.transl_structure"
|
|
|
|
end
|
|
|
|
| Tstr_eval expr :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Lsequence(transl_exp expr, transl_structure fields cc rem)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_value(rec_flag, pat_expr_list) :: rem ->
|
1996-02-18 06:43:18 -08:00
|
|
|
let ext_fields = rev_let_bound_idents pat_expr_list @ fields in
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_let rec_flag pat_expr_list (transl_structure ext_fields cc rem)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_primitive(id, descr) :: rem ->
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match descr.val_kind with
|
|
|
|
Val_prim p -> primitive_declarations :=
|
|
|
|
p.Primitive.prim_name :: !primitive_declarations
|
|
|
|
| _ -> ()
|
1995-11-05 09:32:12 -08:00
|
|
|
end;
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_structure fields cc rem
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_type(decls) :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_structure fields cc rem
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_exception(id, decl) :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Llet(Strict, id, transl_exception id decl,
|
|
|
|
transl_structure (id :: fields) cc rem)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_module(id, modl) :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
Llet(Strict, id, transl_module Tcoerce_none modl,
|
|
|
|
transl_structure (id :: fields) cc rem)
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_modtype(id, decl) :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_structure fields cc rem
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_open path :: rem ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_structure fields cc rem
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tstr_class cl_list :: rem ->
|
|
|
|
List.fold_right
|
|
|
|
(fun (id, cl) re ->
|
1996-08-13 08:10:35 -07:00
|
|
|
Llet(Strict, id, class_stub, re))
|
1996-04-22 04:15:41 -07:00
|
|
|
cl_list
|
1996-08-13 08:10:35 -07:00
|
|
|
(List.fold_right
|
|
|
|
(fun (id, cl) re ->
|
|
|
|
Lsequence(transl_class id cl, re))
|
|
|
|
cl_list
|
1996-09-23 13:34:45 -07:00
|
|
|
(transl_structure
|
|
|
|
((List.rev (List.map fst cl_list)) @ fields) cc rem))
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Compile an implementation *)
|
|
|
|
|
|
|
|
let transl_implementation module_name str cc =
|
1996-04-22 04:15:41 -07:00
|
|
|
reset_labels ();
|
1995-11-05 09:32:12 -08:00
|
|
|
primitive_declarations := [];
|
1996-04-22 04:15:41 -07:00
|
|
|
let module_id = Ident.create_persistent module_name in
|
|
|
|
Lprim(Psetglobal module_id, [transl_label_init (transl_structure [] cc str)])
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1996-02-18 06:43:18 -08:00
|
|
|
(* A variant of transl_structure used to compile toplevel structure definitions
|
|
|
|
for the native-code compiler. Store the defined values in the fields
|
|
|
|
of the global as soon as they are defined, in order to reduce register
|
|
|
|
pressure.
|
1996-04-18 09:28:28 -07:00
|
|
|
"map" is a table from idents to (position in global block, coercion).
|
|
|
|
"prim" is a list of (position in global block, primitive declaration). *)
|
1996-02-18 06:43:18 -08:00
|
|
|
|
1996-04-18 09:28:28 -07:00
|
|
|
let transl_store_structure glob map prims str =
|
|
|
|
let rec transl_store = function
|
1996-02-18 06:43:18 -08:00
|
|
|
[] ->
|
|
|
|
lambda_unit
|
|
|
|
| Tstr_eval expr :: rem ->
|
1996-04-18 09:28:28 -07:00
|
|
|
Lsequence(transl_exp expr, transl_store rem)
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_value(rec_flag, pat_expr_list) :: rem ->
|
|
|
|
transl_let rec_flag pat_expr_list
|
|
|
|
(store_idents glob map (let_bound_idents pat_expr_list)
|
1996-04-18 09:28:28 -07:00
|
|
|
(transl_store rem))
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_primitive(id, descr) :: rem ->
|
1996-04-22 04:15:41 -07:00
|
|
|
begin match descr.val_kind with
|
|
|
|
Val_prim p -> primitive_declarations :=
|
|
|
|
p.Primitive.prim_name :: !primitive_declarations
|
|
|
|
| _ -> ()
|
1996-02-18 06:43:18 -08:00
|
|
|
end;
|
1996-04-18 09:28:28 -07:00
|
|
|
transl_store rem
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_type(decls) :: rem ->
|
1996-04-18 09:28:28 -07:00
|
|
|
transl_store rem
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_exception(id, decl) :: rem ->
|
|
|
|
Llet(Strict, id, transl_exception id decl,
|
1996-04-18 09:28:28 -07:00
|
|
|
store_ident glob map id (transl_store rem))
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_module(id, modl) :: rem ->
|
|
|
|
Llet(Strict, id, transl_module Tcoerce_none modl,
|
1996-04-18 09:28:28 -07:00
|
|
|
store_ident glob map id (transl_store rem))
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_modtype(id, decl) :: rem ->
|
1996-04-18 09:28:28 -07:00
|
|
|
transl_store rem
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tstr_open path :: rem ->
|
1996-04-18 09:28:28 -07:00
|
|
|
transl_store rem
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tstr_class cl_list :: rem ->
|
|
|
|
List.fold_right
|
|
|
|
(fun (id, cl) re ->
|
1996-08-13 08:10:35 -07:00
|
|
|
Llet(Strict, id, class_stub, re))
|
1996-04-22 04:15:41 -07:00
|
|
|
cl_list
|
1996-08-13 08:10:35 -07:00
|
|
|
(List.fold_right
|
|
|
|
(fun (id, cl) re ->
|
|
|
|
Lsequence(transl_class id cl, re))
|
|
|
|
cl_list
|
|
|
|
(store_idents glob map (List.map fst cl_list) (transl_store rem)))
|
1996-04-18 09:28:28 -07:00
|
|
|
|
|
|
|
and store_ident glob map id cont =
|
|
|
|
try
|
|
|
|
let (pos, cc) = Ident.find_same id map in
|
|
|
|
let init_val = apply_coercion cc (Lvar id) in
|
|
|
|
Lsequence
|
|
|
|
(Lprim(Psetfield(pos, false), [Lprim(Pgetglobal glob, []); init_val]),
|
|
|
|
cont)
|
|
|
|
with Not_found ->
|
|
|
|
cont
|
|
|
|
|
|
|
|
and store_idents glob map idlist cont =
|
|
|
|
List.fold_right (store_ident glob map) idlist cont
|
|
|
|
|
|
|
|
and store_primitive (pos, prim) cont =
|
|
|
|
Lsequence(Lprim(Psetfield(pos, false),
|
|
|
|
[Lprim(Pgetglobal glob, []); transl_primitive prim]),
|
|
|
|
cont)
|
|
|
|
in
|
|
|
|
List.fold_right store_primitive prims (transl_store str)
|
1996-02-18 06:43:18 -08:00
|
|
|
|
|
|
|
(* Build the list of value identifiers defined by a toplevel structure *)
|
|
|
|
|
|
|
|
let rec defined_idents = function
|
|
|
|
[] -> []
|
|
|
|
| Tstr_eval expr :: rem -> defined_idents rem
|
|
|
|
| Tstr_value(rec_flag, pat_expr_list) :: rem ->
|
|
|
|
let_bound_idents pat_expr_list @ defined_idents rem
|
|
|
|
| Tstr_primitive(id, descr) :: rem -> defined_idents rem
|
|
|
|
| Tstr_type decls :: rem -> defined_idents rem
|
|
|
|
| Tstr_exception(id, decl) :: rem -> id :: defined_idents rem
|
|
|
|
| Tstr_module(id, modl) :: rem -> id :: defined_idents rem
|
|
|
|
| Tstr_modtype(id, decl) :: rem -> defined_idents rem
|
|
|
|
| Tstr_open path :: rem -> defined_idents rem
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tstr_class cl_list :: rem ->
|
|
|
|
List.map fst cl_list @ defined_idents rem
|
1996-02-18 06:43:18 -08:00
|
|
|
|
1996-04-18 09:28:28 -07:00
|
|
|
(* Transform a coercion and the list of value identifiers built above
|
|
|
|
into a table id -> (pos, coercion), with [pos] being the position
|
|
|
|
in the global block where the value of [id] must be stored,
|
|
|
|
and [coercion] the coercion to be applied to it.
|
|
|
|
A given identifier may appear several times
|
|
|
|
in the coercion (if it occurs several times in the signature); remember
|
|
|
|
to assign it the position of its last occurrence.
|
|
|
|
Also buid a list of primitives and their positions in the global block,
|
|
|
|
and the total size of the global block. *)
|
1996-02-18 06:43:18 -08:00
|
|
|
|
1996-04-18 09:28:28 -07:00
|
|
|
let build_ident_map restr idlist =
|
1996-02-18 06:43:18 -08:00
|
|
|
match restr with
|
|
|
|
Tcoerce_none ->
|
1996-04-18 09:28:28 -07:00
|
|
|
let rec build_map pos map = function
|
|
|
|
[] ->
|
|
|
|
(map, [], pos)
|
|
|
|
| id :: rem ->
|
|
|
|
build_map (pos+1) (Ident.add id (pos, Tcoerce_none) map) rem
|
|
|
|
in build_map 0 Ident.empty idlist
|
1996-02-18 06:43:18 -08:00
|
|
|
| Tcoerce_structure pos_cc_list ->
|
|
|
|
let idarray = Array.of_list idlist in
|
1996-04-18 09:28:28 -07:00
|
|
|
let rec build_map pos map prims = function
|
|
|
|
[] ->
|
|
|
|
(map, prims, pos)
|
|
|
|
| (source_pos, Tcoerce_primitive p) :: rem ->
|
|
|
|
build_map (pos+1) map ((pos, p) :: prims) rem
|
|
|
|
| (source_pos, cc) :: rem ->
|
|
|
|
build_map (pos+1) (Ident.add idarray.(source_pos) (pos, cc) map)
|
|
|
|
prims rem
|
|
|
|
in build_map 0 Ident.empty [] pos_cc_list
|
|
|
|
| _ ->
|
|
|
|
fatal_error "Translmod.build_ident_map"
|
|
|
|
|
1996-02-18 06:43:18 -08:00
|
|
|
(* Compile an implementation using transl_store_structure
|
|
|
|
(for the native-code compiler). *)
|
|
|
|
|
1996-04-18 09:28:28 -07:00
|
|
|
let transl_store_implementation module_name str restr =
|
1996-04-22 04:15:41 -07:00
|
|
|
reset_labels ();
|
1996-02-18 06:43:18 -08:00
|
|
|
primitive_declarations := [];
|
1996-04-22 04:15:41 -07:00
|
|
|
let module_id = Ident.create_persistent module_name in
|
1996-04-18 09:28:28 -07:00
|
|
|
let (map, prims, size) = build_ident_map restr (defined_idents str) in
|
1996-04-22 04:15:41 -07:00
|
|
|
(size, transl_label_init (transl_store_structure module_id map prims str))
|
1996-02-18 06:43:18 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Compile a sequence of expressions *)
|
|
|
|
|
|
|
|
let rec make_sequence fn = function
|
|
|
|
[] -> lambda_unit
|
|
|
|
| [x] -> fn x
|
1996-04-22 04:15:41 -07:00
|
|
|
| x::rem ->
|
|
|
|
Lsequence(fn x, make_sequence fn rem)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Compile a toplevel phrase *)
|
|
|
|
|
|
|
|
let transl_toplevel_item = function
|
|
|
|
Tstr_eval expr ->
|
1995-12-15 02:18:29 -08:00
|
|
|
transl_exp expr
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_value(rec_flag, pat_expr_list) ->
|
|
|
|
let idents = let_bound_idents pat_expr_list in
|
1996-02-18 06:43:18 -08:00
|
|
|
let lam =
|
|
|
|
transl_let rec_flag pat_expr_list
|
|
|
|
(make_sequence (fun id -> Lprim(Psetglobal id, [Lvar id])) idents) in
|
1995-05-04 03:15:53 -07:00
|
|
|
List.iter Ident.make_global idents;
|
1996-02-18 06:43:18 -08:00
|
|
|
lam
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_primitive(id, descr) ->
|
1995-10-23 09:56:52 -07:00
|
|
|
lambda_unit
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_type(decls) ->
|
|
|
|
lambda_unit
|
|
|
|
| Tstr_exception(id, decl) ->
|
|
|
|
Ident.make_global id;
|
|
|
|
Lprim(Psetglobal id, [transl_exception id decl])
|
|
|
|
| Tstr_module(id, modl) ->
|
|
|
|
Ident.make_global id;
|
1995-12-15 02:18:29 -08:00
|
|
|
Lprim(Psetglobal id, [transl_module Tcoerce_none modl])
|
1995-05-04 03:15:53 -07:00
|
|
|
| Tstr_modtype(id, decl) ->
|
|
|
|
lambda_unit
|
|
|
|
| Tstr_open path ->
|
|
|
|
lambda_unit
|
1996-04-22 04:15:41 -07:00
|
|
|
| Tstr_class cl_list ->
|
1996-08-13 08:10:35 -07:00
|
|
|
let lam =
|
|
|
|
List.fold_right
|
|
|
|
(fun (id, cl) re ->
|
|
|
|
Llet(Strict, id, class_stub, re))
|
|
|
|
cl_list
|
|
|
|
(make_sequence
|
|
|
|
(fun (id, cl) ->
|
|
|
|
Lsequence(Lprim(Psetglobal id, [Lvar id]), transl_class id cl))
|
|
|
|
cl_list)
|
|
|
|
in
|
1996-04-22 04:15:41 -07:00
|
|
|
List.iter (fun (id, cl) -> Ident.make_global id) cl_list;
|
1996-08-13 08:10:35 -07:00
|
|
|
lam
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let transl_toplevel_definition str =
|
1996-04-22 04:15:41 -07:00
|
|
|
reset_labels ();
|
|
|
|
transl_label_init (make_sequence transl_toplevel_item str)
|