Skip to content

Commit 71120ae

Browse files
author
Hongbo Zhang
committed
complete -- except we need a convert function from ocaml lambda to bs lambda
1 parent 60cb5b4 commit 71120ae

20 files changed

+847
-191
lines changed

jscomp/core.mllib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ config_util
99

1010
lam
1111
lam_mk
12+
lam_print
1213
lam_compile_env
1314
lam_dispatch_primitive
1415
lam_stats

jscomp/js_implementation.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let implementation ppf sourcefile outputprefix =
7676
match
7777
Lam_compile_group.lambda_as_module
7878
finalenv current_signature
79-
sourcefile outputprefix lambda with
79+
sourcefile outputprefix ((* Obj.magic *) lambda ) with
8080
| e -> e
8181
| exception e ->
8282
(* Save to a file instead so that it will not scare user *)

jscomp/lam.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
type primitive = Lambda.primitive
26-
27-
type t = Lambda.lambda =
26+
type switch = Lambda.lambda_switch =
27+
{ sw_numconsts: int;
28+
sw_consts: (int * t) list;
29+
sw_numblocks: int;
30+
sw_blocks: (int * t) list;
31+
sw_failaction : t option}
32+
and t = Lambda.lambda =
2833
| Lvar of Ident.t
2934
| Lconst of Lambda.structured_constant
3035
| Lapply of t * t list * Lambda.apply_info
3136
| Lfunction of Lambda.function_kind * Ident.t list * t
3237
| Llet of Lambda.let_kind * Ident.t * t * t
3338
| Lletrec of (Ident.t * t) list * t
3439
| Lprim of primitive * t list
35-
| Lswitch of t * Lambda.lambda_switch
40+
| Lswitch of t * switch
3641
| Lstringswitch of t * (string * t) list * t option
3742
| Lstaticraise of int * t list
3843
| Lstaticcatch of t * (int * Ident.t list) * t
@@ -93,7 +98,16 @@ type triop = t -> t -> t -> t
9398
type unop = t -> t
9499

95100

101+
let var id : t = Lvar id
102+
let const ct : t = Lconst ct
103+
let apply fn args info : t = Lapply(fn,args, info)
104+
let function_ kind ids body : t =
105+
Lfunction(kind, ids, body)
96106

107+
let let_ kind id e body : t
108+
= Llet (kind,id,e,body)
109+
let letrec bindings body : t =
110+
Lletrec(bindings,body)
97111

98112
let if_ (a : t) (b : t) c =
99113
match a with
@@ -353,3 +367,8 @@ let prim (prim : Prim.t) (ll : t list) : t =
353367

354368
let not x : t =
355369
prim Pnot [x]
370+
371+
372+
let free_variables = Lambda.free_variables
373+
374+
let subst_lambda = Lambda.subst_lambda

jscomp/lam.mli

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,23 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
type primitive = Lambda.primitive
26-
type t = Lambda.lambda =
26+
27+
28+
type switch = Lambda.lambda_switch =
29+
{ sw_numconsts: int;
30+
sw_consts: (int * t) list;
31+
sw_numblocks: int;
32+
sw_blocks: (int * t) list;
33+
sw_failaction : t option}
34+
and t = Lambda.lambda = private
2735
| Lvar of Ident.t
2836
| Lconst of Lambda.structured_constant
2937
| Lapply of t * t list * Lambda.apply_info
3038
| Lfunction of Lambda.function_kind * Ident.t list * t
3139
| Llet of Lambda.let_kind * Ident.t * t * t
3240
| Lletrec of (Ident.t * t) list * t
3341
| Lprim of primitive * t list
34-
| Lswitch of t * Lambda.lambda_switch
42+
| Lswitch of t * switch
3543
| Lstringswitch of t * (string * t) list * t option
3644
| Lstaticraise of int * t list
3745
| Lstaticcatch of t * (int * Ident.t list) * t
@@ -60,8 +68,14 @@ type triop = t -> t -> t -> t
6068

6169
type unop = t -> t
6270

71+
val var : Ident.t -> t
72+
val const : Lambda.structured_constant -> t
73+
val apply : t -> t list -> Lambda.apply_info -> t
74+
val function_ : Lambda.function_kind -> Ident.t list -> t -> t
75+
val let_ : Lambda.let_kind -> Ident.t -> t -> t -> t
76+
val letrec : (Ident.t * t) list -> t -> t
6377
val if_ : triop
64-
val switch : t -> Lambda.lambda_switch -> t
78+
val switch : t -> switch -> t
6579
val stringswitch : t -> (string * t) list -> t option -> t
6680

6781
val true_ : t
@@ -94,3 +108,6 @@ val for_ :
94108
t ->
95109
t -> Asttypes.direction_flag -> t -> t
96110

111+
val free_variables : t -> Lambda.IdentSet.t
112+
113+
val subst_lambda : t Ident.tbl -> t -> t

jscomp/lam_beta_reduce.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
7373

7474
let rebind i =
7575
let i' = Ident.rename i in
76-
Hashtbl.add map i (Lambda.Lvar i');
76+
Hashtbl.add map i (Lam.var i');
7777
i' in
7878
(* order matters, especially for let bindings *)
7979
let rec
@@ -92,17 +92,17 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
9292
let v = rebind v in
9393
let l1 = aux l1 in
9494
let l2 = aux l2 in
95-
Llet(str, v, l1, l2 )
95+
Lam.let_ str v l1 l2
9696
| Lletrec(bindings, body) ->
9797
(*order matters see GPR #405*)
9898
let vars = List.map (fun (k, _) -> rebind k) bindings in
9999
let bindings = List.map2 (fun var (_,l) -> var, aux l) vars bindings in
100100
let body = aux body in
101-
Lletrec(bindings, body)
101+
Lam.letrec bindings body
102102
| Lfunction(kind, params, body) ->
103103
let params = List.map rebind params in
104104
let body = aux body in
105-
Lfunction (kind, params, body)
105+
Lam.function_ kind params body
106106
| Lstaticcatch(l1, (i,xs), l2) ->
107107
let l1 = aux l1 in
108108
let xs = List.map rebind xs in
@@ -121,7 +121,7 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
121121
| Lapply(fn, args, info) ->
122122
let fn = aux fn in
123123
let args = List.map aux args in
124-
Lapply(fn, args, info)
124+
Lam.apply fn args info
125125
| Lswitch(l, {sw_failaction;
126126
sw_consts;
127127
sw_blocks;
@@ -219,7 +219,7 @@ let propogate_beta_reduce
219219
| Lvar _ -> rest_bindings , arg :: acc
220220
| _ ->
221221
let p = Ident.rename old_param in
222-
(p,arg) :: rest_bindings , (Lambda.Lvar p) :: acc
222+
(p,arg) :: rest_bindings , (Lam.var p) :: acc
223223
) ([],[]) params args in
224224
let new_body = rewrite (Ext_hashtbl.of_list2 (List.rev params) (rev_new_params)) body in
225225
List.fold_right
@@ -261,7 +261,7 @@ let propogate_beta_reduce_with_map
261261
(* TODO: we can pass Global, but you also need keep track of it*)
262262
->
263263
let p = Ident.rename old_param in
264-
(p,arg) :: rest_bindings , (Lambda.Lvar p) :: acc
264+
(p,arg) :: rest_bindings , (Lam.var p) :: acc
265265

266266
| _ ->
267267
if Lam_analysis.no_side_effects arg then
@@ -273,11 +273,11 @@ let propogate_beta_reduce_with_map
273273
rest_bindings, arg :: acc
274274
| _ ->
275275
let p = Ident.rename old_param in
276-
(p,arg) :: rest_bindings , (Lambda.Lvar p) :: acc
276+
(p,arg) :: rest_bindings , (Lam.var p) :: acc
277277
end
278278
else
279279
let p = Ident.rename old_param in
280-
(p,arg) :: rest_bindings , (Lambda.Lvar p) :: acc
280+
(p,arg) :: rest_bindings , (Lam.var p) :: acc
281281
) ([],[]) params args in
282282
let new_body = rewrite (Ext_hashtbl.of_list2 (List.rev params) (rev_new_params)) body in
283283
List.fold_right

jscomp/lam_beta_reduce_util.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let simple_beta_reduce params body args =
106106
if not used then
107107
Lam.seq lambda code
108108
else code )
109-
param_hash (Lambda.Lapply ( f, us , info)) in
109+
param_hash (Lam.apply f us info) in
110110
Hashtbl.clear param_hash;
111111
Some result
112112
| exception _ ->

jscomp/lam_compile.ml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ and compile_recursive_let
312312
E.fun_ params (Js_output.to_block output )
313313
), []
314314
| Lprim (Pmakeblock (0, _, _) , ls)
315-
when List.for_all (function | Lambda.Lvar _ -> true | _ -> false) ls
315+
when List.for_all (function | Lam.Lvar _ -> true | _ -> false) ls
316316
->
317317
(* capture cases like for {!Queue}
318318
{[let rec cell = { content = x; next = cell} ]}
@@ -321,7 +321,7 @@ and compile_recursive_let
321321
S.define ~kind:Variable id (E.arr Mutable []) ::
322322
(List.mapi (fun i x ->
323323
match x with
324-
| Lambda.Lvar lid
324+
| Lam.Lvar lid
325325
-> S.exp
326326
(Js_array.set_array (E.var id) (E.int (Int32.of_int i)) (E.var lid))
327327
| _ -> assert false
@@ -509,7 +509,7 @@ and
509509
see {!Ari_regress_test}
510510
*)
511511
compile_lambda cxt
512-
(Lapply (an, (args' @ args), (Lam_util.mk_apply_info App_na)))
512+
(Lam.apply an (args' @ args) (Lam_util.mk_apply_info App_na))
513513
(* External function calll *)
514514
| Lapply(Lprim(Pfield (n,_), [ Lprim(Pgetglobal id,[])]), args_lambda,
515515
{apply_status = App_na | App_ml_full}) ->
@@ -856,9 +856,10 @@ and
856856
{block = block1; value = Some value}, Public (Some setter)
857857
->
858858
if not @@ Ext_string.ends_with setter Literals.setter_suffix then
859-
compile_lambda cxt @@ Lapply (fn, [arg] ,
860-
{apply_loc = Location.none;
861-
apply_status = App_js_full})
859+
compile_lambda cxt @@
860+
Lam.apply fn [arg]
861+
{apply_loc = Location.none;
862+
apply_status = App_js_full}
862863
else
863864
let property =
864865
String.sub setter 0
@@ -877,9 +878,9 @@ and
877878

878879
| fn :: rest ->
879880
compile_lambda cxt
880-
(Lapply (fn, rest ,
881+
(Lam.apply fn rest
881882
{apply_loc = Location.none;
882-
apply_status = App_js_full}))
883+
apply_status = App_js_full})
883884
| _ -> assert false
884885
else
885886
begin match args_lambda with
@@ -904,27 +905,28 @@ and
904905
begin
905906
match fn with
906907
| Lfunction (_, [_], body)
907-
-> compile_lambda cxt (Lfunction (Curried, [], body))
908+
-> compile_lambda cxt (Lam.function_ Curried [] body)
908909
| _ ->
909910
compile_lambda cxt
910-
(Lfunction (Lambda.Curried, [],
911-
Lambda.Lapply(fn,
912-
[Lam.unit],
913-
Lam_util.default_apply_info
914-
)))
911+
(Lam.function_ Lambda.Curried []
912+
@@
913+
Lam.apply fn
914+
[Lam.unit]
915+
Lam_util.default_apply_info
916+
)
915917
end
916918
else
917919
begin match fn with
918-
| Lambda.Lfunction(kind,args, body)
920+
| Lam.Lfunction(kind,args, body)
919921
->
920922
let len = List.length args in
921923
if len = arity then
922924
compile_lambda cxt fn
923925
else if len > arity then
924926
let first, rest = Ext_list.take arity args in
925927
compile_lambda cxt
926-
(Lambda.Lfunction
927-
(kind, first, Lambda.Lfunction (kind, rest, body)))
928+
(Lam.function_
929+
kind first (Lam.function_ kind rest body))
928930
else
929931
compile_lambda cxt
930932
(Lam_util.eta_conversion arity Lam_util.default_apply_info
@@ -1488,7 +1490,7 @@ and
14881490
cont, _reraise )
14891491
)) when Ident.same id id2
14901492
->
1491-
compile_lambda cxt (Ltrywith(body, id, cont))
1493+
compile_lambda cxt (Lam.try_ body id cont)
14921494
| Ltrywith(lam,id, catch) -> (* generate documentation *)
14931495
(*
14941496
tail --> should be renamed to `shouldReturn`

jscomp/lam_dce.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ let remove export_idents (rest : Lam_group.t list) : Lam_group.t list =
3939
match x with
4040
| Single(kind, id,lam) -> (* assert false *)
4141
begin
42-
Hashtbl.add ident_free_vars id (Lambda.free_variables lam);
42+
Hashtbl.add ident_free_vars id
43+
(Lam.free_variables lam);
4344
match kind with
4445
| Alias | StrictOpt -> []
4546
| Strict | Variable -> [id]
@@ -48,7 +49,7 @@ let remove export_idents (rest : Lam_group.t list) : Lam_group.t list =
4849
begin
4950
bindings |> Ext_list.flat_map (fun (id,lam) ->
5051
begin
51-
Hashtbl.add ident_free_vars id (Lambda.free_variables lam);
52+
Hashtbl.add ident_free_vars id (Lam.free_variables lam);
5253
match (lam : Lam.t) with
5354
| Lfunction _ -> []
5455
| _ -> [id]
@@ -58,7 +59,7 @@ let remove export_idents (rest : Lam_group.t list) : Lam_group.t list =
5859
if Lam_analysis.no_side_effects lam then []
5960
else
6061
(** its free varaibles here will be defined above *)
61-
I.elements ( Lambda.free_variables lam)) rest @ export_idents
62+
I.elements ( Lam.free_variables lam)) rest @ export_idents
6263
in
6364
let current_ident_sets =
6465
Idents_analysis.calculate_used_idents ident_free_vars

0 commit comments

Comments
 (0)