1995-05-04 03:15:53 -07:00
|
|
|
open Path
|
|
|
|
|
|
|
|
open Asttypes
|
|
|
|
|
|
|
|
type primitive =
|
|
|
|
Pidentity
|
|
|
|
| Pgetglobal of Ident.t
|
|
|
|
| Psetglobal of Ident.t
|
|
|
|
| Pmakeblock of int
|
|
|
|
| Pfield of int
|
1995-07-10 02:48:27 -07:00
|
|
|
| Psetfield of int * bool
|
|
|
|
| Pccall of string * int * bool
|
1995-05-04 03:15:53 -07:00
|
|
|
| Praise
|
|
|
|
| Psequand | Psequor | Pnot
|
|
|
|
| Pnegint | Paddint | Psubint | Pmulint | Pdivint | Pmodint
|
|
|
|
| Pandint | Porint | Pxorint
|
|
|
|
| Plslint | Plsrint | Pasrint
|
1995-06-18 07:44:56 -07:00
|
|
|
| Pintcomp of comparison
|
1995-05-04 03:15:53 -07:00
|
|
|
| Poffsetint of int
|
|
|
|
| Poffsetref of int
|
1995-07-11 01:53:14 -07:00
|
|
|
| Pintoffloat | Pfloatofint
|
1995-06-18 07:44:56 -07:00
|
|
|
| Pnegfloat | Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
|
|
|
|
| Pfloatcomp of comparison
|
1995-07-10 02:48:27 -07:00
|
|
|
| Pstringlength | Pgetstringchar | Psetstringchar
|
|
|
|
| Psafegetstringchar | Psafesetstringchar
|
|
|
|
| Pvectlength | Pgetvectitem | Psetvectitem of bool
|
|
|
|
| Psafegetvectitem | Psafesetvectitem of bool
|
1995-06-18 07:44:56 -07:00
|
|
|
| Ptranslate of (int * int * int) array
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
and comparison =
|
|
|
|
Ceq | Cneq | Clt | Cgt | Cle | Cge
|
|
|
|
|
|
|
|
type structured_constant =
|
|
|
|
Const_base of constant
|
|
|
|
| Const_block of int * structured_constant list
|
1995-07-02 09:45:21 -07:00
|
|
|
| Const_pointer of int
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
type lambda =
|
|
|
|
Lvar of Ident.t
|
|
|
|
| Lconst of structured_constant
|
|
|
|
| Lapply of lambda * lambda list
|
|
|
|
| Lfunction of Ident.t * lambda
|
|
|
|
| Llet of Ident.t * lambda * lambda
|
1995-07-02 09:45:21 -07:00
|
|
|
| Lletrec of (Ident.t * lambda) list * lambda
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lprim of primitive * lambda list
|
1995-06-18 07:44:56 -07:00
|
|
|
| Lswitch of lambda * int * (int * lambda) list * int * (int * lambda) list
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lstaticfail
|
|
|
|
| Lcatch of lambda * lambda
|
|
|
|
| Ltrywith of lambda * Ident.t * lambda
|
|
|
|
| Lifthenelse of lambda * lambda * lambda
|
|
|
|
| Lsequence of lambda * lambda
|
|
|
|
| Lwhile of lambda * lambda
|
|
|
|
| Lfor of Ident.t * lambda * lambda * direction_flag * lambda
|
|
|
|
| Lshared of lambda * int option ref
|
|
|
|
|
1995-06-18 07:44:56 -07:00
|
|
|
let const_unit = Const_base(Const_int 0)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lambda_unit = Lconst const_unit
|
|
|
|
|
|
|
|
let share_lambda = function
|
|
|
|
Lshared(_, _) as l -> l
|
|
|
|
| l -> Lshared(l, ref None)
|
|
|
|
|
|
|
|
let name_lambda arg fn =
|
|
|
|
match arg with
|
|
|
|
Lvar id -> fn id
|
|
|
|
| _ -> let id = Ident.new "let" in Llet(id, arg, fn id)
|
|
|
|
|
1995-07-02 09:45:21 -07:00
|
|
|
let name_lambda_list args fn =
|
|
|
|
let rec name_list names = function
|
|
|
|
[] -> fn (List.rev names)
|
|
|
|
| (Lvar id as arg) :: rem ->
|
|
|
|
name_list (arg :: names) rem
|
|
|
|
| arg :: rem ->
|
|
|
|
let id = Ident.new "let" in
|
|
|
|
Llet(id, arg, name_list (Lvar id :: names) rem) in
|
|
|
|
name_list [] args
|
|
|
|
|
1995-05-30 06:36:40 -07:00
|
|
|
module IdentSet =
|
|
|
|
Set.Make(struct
|
|
|
|
type t = Ident.t
|
|
|
|
let compare = compare
|
|
|
|
end)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let free_variables l =
|
1995-05-30 06:36:40 -07:00
|
|
|
let fv = ref IdentSet.empty in
|
1995-05-04 03:15:53 -07:00
|
|
|
let rec freevars = function
|
|
|
|
Lvar id ->
|
1995-05-30 06:36:40 -07:00
|
|
|
fv := IdentSet.add id !fv
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lconst sc -> ()
|
|
|
|
| Lapply(fn, args) ->
|
|
|
|
freevars fn; List.iter freevars args
|
|
|
|
| Lfunction(param, body) ->
|
1995-05-30 06:36:40 -07:00
|
|
|
freevars body; fv := IdentSet.remove param !fv
|
1995-05-04 03:15:53 -07:00
|
|
|
| Llet(id, arg, body) ->
|
1995-05-30 06:36:40 -07:00
|
|
|
freevars arg; freevars body; fv := IdentSet.remove id !fv
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lletrec(decl, body) ->
|
|
|
|
freevars body;
|
1995-07-02 09:45:21 -07:00
|
|
|
List.iter (fun (id, exp) -> freevars exp) decl;
|
|
|
|
List.iter (fun (id, exp) -> fv := IdentSet.remove id !fv) decl
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lprim(p, args) ->
|
|
|
|
List.iter freevars args
|
1995-06-18 07:44:56 -07:00
|
|
|
| Lswitch(arg, num_cases1, cases1, num_cases2, cases2) ->
|
|
|
|
freevars arg;
|
|
|
|
List.iter (fun (key, case) -> freevars case) cases1;
|
|
|
|
List.iter (fun (key, case) -> freevars case) cases2
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lstaticfail -> ()
|
|
|
|
| Lcatch(e1, e2) ->
|
|
|
|
freevars e1; freevars e2
|
|
|
|
| Ltrywith(e1, exn, e2) ->
|
1995-05-30 06:36:40 -07:00
|
|
|
freevars e1; freevars e2; fv := IdentSet.remove exn !fv
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lifthenelse(e1, e2, e3) ->
|
|
|
|
freevars e1; freevars e2; freevars e3
|
|
|
|
| Lsequence(e1, e2) ->
|
|
|
|
freevars e1; freevars e2
|
|
|
|
| Lwhile(e1, e2) ->
|
|
|
|
freevars e1; freevars e2
|
|
|
|
| Lfor(v, e1, e2, dir, e3) ->
|
1995-05-30 06:36:40 -07:00
|
|
|
freevars e1; freevars e2; freevars e3; fv := IdentSet.remove v !fv
|
1995-05-04 03:15:53 -07:00
|
|
|
| Lshared(e, lblref) ->
|
|
|
|
freevars e
|
1995-07-02 09:45:21 -07:00
|
|
|
in freevars l; !fv
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Check if an action has a "when" guard *)
|
|
|
|
|
|
|
|
let rec is_guarded = function
|
|
|
|
Lifthenelse(cond, body, Lstaticfail) -> true
|
|
|
|
| Lshared(lam, lbl) -> is_guarded lam
|
|
|
|
| Llet(id, lam, body) -> is_guarded body
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
type compilenv = lambda Ident.tbl
|
|
|
|
|
|
|
|
let empty_env = Ident.empty
|
|
|
|
|
|
|
|
let add_env = Ident.add
|
|
|
|
|
|
|
|
let find_env = Ident.find_same
|
|
|
|
|
|
|
|
let transl_access env id =
|
|
|
|
try
|
|
|
|
find_env id env
|
|
|
|
with Not_found ->
|
|
|
|
if Ident.global id then Lprim(Pgetglobal id, []) else Lvar id
|
|
|
|
|
|
|
|
let rec transl_path = function
|
|
|
|
Pident id ->
|
|
|
|
if Ident.global id then Lprim(Pgetglobal id, []) else Lvar id
|
|
|
|
| Pdot(p, s, pos) ->
|
|
|
|
Lprim(Pfield pos, [transl_path p])
|
|
|
|
|