Skip to content

Commit 47fd2e8

Browse files
author
Hongbo Zhang
committed
use record for Lprim
1 parent 900c3d2 commit 47fd2e8

18 files changed

+175
-122
lines changed

jscomp/lam.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ type switch =
2929
sw_numblocks: int;
3030
sw_blocks: (int * t) list;
3131
sw_failaction : t option}
32+
and prim_info =
33+
{ primitive : primitive ;
34+
args : t list ;
35+
}
3236
and t =
3337
| Lvar of Ident.t
3438
| Lconst of Lambda.structured_constant
3539
| Lapply of t * t list * Lambda.apply_info
3640
| Lfunction of int * Lambda.function_kind * Ident.t list * t
3741
| Llet of Lambda.let_kind * Ident.t * t * t
3842
| Lletrec of (Ident.t * t) list * t
39-
| Lprim of primitive * t list * int
43+
| Lprim of prim_info
4044
| Lswitch of t * switch
4145
| Lstringswitch of t * (string * t) list * t option
4246
| Lstaticraise of int * t list
@@ -223,8 +227,8 @@ let lift_int32 b : t =
223227
let lift_int64 b : t =
224228
Lconst (Const_base (Const_int64 b))
225229

226-
let prim (prim : Prim.t) (ll : t list) len : t =
227-
let default () : t = Lprim(prim,ll, len) in
230+
let prim (prim : Prim.t) (ll : t list) : t =
231+
let default () : t = Lprim { primitive = prim ;args = ll } in
228232
match ll with
229233
| [Lconst a] ->
230234
begin match prim, a with
@@ -366,7 +370,7 @@ let prim (prim : Prim.t) (ll : t list) len : t =
366370

367371

368372
let not x : t =
369-
prim Pnot [x] 1
373+
prim Pnot [x]
370374

371375

372376
let rec convert (lam : Lambda.lambda) : t =
@@ -383,9 +387,9 @@ let rec convert (lam : Lambda.lambda) : t =
383387
| Lletrec (bindings,body)
384388
->
385389
Lletrec (List.map (fun (id, e) -> id, convert e) bindings, convert body)
386-
| Lprim (prim,args)
390+
| Lprim (primitive,args)
387391
->
388-
Lprim (prim,List.map convert args, List.length args)
392+
Lprim {primitive ; args = List.map convert args }
389393
| Lswitch (e,s) ->
390394
Lswitch (convert e, convert_switch s)
391395
| Lstringswitch (e, cases, default) ->

jscomp/lam.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ type switch =
3131
sw_numblocks: int;
3232
sw_blocks: (int * t) list;
3333
sw_failaction : t option}
34+
and prim_info = private
35+
{ primitive : primitive ;
36+
args : t list ;
37+
}
3438
and t = private
3539
| Lvar of Ident.t
3640
| Lconst of Lambda.structured_constant
3741
| Lapply of t * t list * Lambda.apply_info
3842
| Lfunction of int (* length *) * Lambda.function_kind * Ident.t list * t
3943
| Llet of Lambda.let_kind * Ident.t * t * t
4044
| Lletrec of (Ident.t * t) list * t
41-
| Lprim of primitive * t list * int (* length *)
45+
| Lprim of prim_info
4246
| Lswitch of t * switch
4347
| Lstringswitch of t * (string * t) list * t option
4448
| Lstaticraise of int * t list
@@ -97,7 +101,7 @@ val send :
97101
t -> t -> t list ->
98102
Location.t -> t
99103

100-
val prim : Lambda.primitive -> t list -> int -> t
104+
val prim : Lambda.primitive -> t list -> t
101105

102106
val staticcatch :
103107
t -> int * Ident.t list -> t -> t

jscomp/lam_analysis.ml

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
3232
| Lvar _
3333
| Lconst _
3434
| Lfunction _ -> true
35-
| Lprim (primitive, args, _) ->
35+
| Lprim {primitive; args; _} ->
3636
List.for_all no_side_effects args &&
3737
(
3838
match primitive with
@@ -186,11 +186,12 @@ let rec no_side_effects (lam : Lam.t) : bool =
186186
if it [Not_found], there are no other exceptions
187187
can be thrown
188188
*)
189-
| Ltrywith (Lprim(Pccall{prim_name = "caml_sys_getenv"},
190-
[Lconst _], _),exn,
191-
Lifthenelse(Lprim(_, [Lvar exn1;
192-
Lprim(Pgetglobal ({name="Not_found"}),[],_)]
193-
, _),
189+
| Ltrywith (Lprim { primitive = Pccall{prim_name = "caml_sys_getenv"};
190+
args = [Lconst _]; _},exn,
191+
Lifthenelse(Lprim{args =
192+
[Lvar exn1;
193+
Lprim {primitive = Pgetglobal ({name="Not_found"}); args = []; _}]
194+
; _},
194195
then_, _)) when Ident.same exn1 exn
195196
(** we might put this in an optimization pass
196197
also make sure when we wrap this in [js] we
@@ -235,11 +236,13 @@ let rec size (lam : Lam.t) =
235236
| Lconst c -> size_constant c
236237
| Llet(_, _, l1, l2) -> 1 + size l1 + size l2
237238
| Lletrec _ -> really_big ()
238-
| Lprim(Pfield _, [Lprim(Pgetglobal _, [ ], _)], _)
239+
| Lprim{primitive = Pfield _;
240+
args = [Lprim { primitive = Pgetglobal _; args = [ ]; _}]
241+
; _}
239242
-> 1
240-
| Lprim (Praise _, [l ], _)
243+
| Lprim {primitive = Praise _; args = [l ]; _}
241244
-> size l
242-
| Lprim(_, ll, _) -> size_lams 1 ll
245+
| Lprim {args = ll; _} -> size_lams 1 ll
243246

244247
(** complicated
245248
1. inline this function
@@ -299,8 +302,9 @@ let rec eq_lambda (l1 : Lam.t) (l2 : Lam.t) =
299302
id = id1 && List.for_all2 eq_lambda ls ls1
300303
| Llet (_,_,_,_), Llet (_,_,_,_) -> false
301304
| Lletrec _, Lletrec _ -> false
302-
| Lprim (p,ls,len1), Lprim (p1,ls1,len2) ->
303-
len1 = len2 && eq_primitive p p1 && List.for_all2 eq_lambda ls ls1
305+
| Lprim {primitive = p; args = ls; } ,
306+
Lprim {primitive = p1; args = ls1} ->
307+
eq_primitive p p1 && List.for_all2 eq_lambda ls ls1
304308
| Lswitch _, Lswitch _ -> false
305309
| Lstringswitch _ , Lstringswitch _ -> false
306310
| Lstaticcatch _, Lstaticcatch _ -> false
@@ -402,7 +406,7 @@ let free_variables (export_idents : Ident_set.t ) (params : stats Ident_map.t )
402406
iter top fn;
403407
let top = new_env fn top in
404408
List.iter (iter top ) args
405-
| Lprim(_p, args,_) ->
409+
| Lprim {args ; _} ->
406410
(* Check: can top be propoaged for all primitives *)
407411
List.iter (iter top) args
408412
| Lfunction(_, _kind, params, body) ->

jscomp/lam_beta_reduce.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
115115
let l3 = aux l3 in
116116
Lam.for_ ident (aux l1) l2 dir l3
117117
| Lconst _ -> lam
118-
| Lprim(prim, ll) ->
118+
| Lprim {primitive; args } ->
119119
(* here it makes sure that global vars are not rebound *)
120-
Lam.prim prim (List.map aux ll)
120+
Lam.prim primitive (List.map aux args)
121121
| Lapply(fn, args, info) ->
122122
let fn = aux fn in
123123
let args = List.map aux args in
@@ -234,13 +234,13 @@ let propogate_beta_reduce
234234
Hashtbl.add meta.ident_tbl param ident_info
235235
end;
236236
arg
237-
| Lprim (Pgetglobal ident, []) ->
237+
| Lprim {primitive = Pgetglobal ident; args = []; _} ->
238238
(* It's not completeness, its to make it sound.. *)
239239
Lam_compile_global.query_lambda ident meta.env
240240
(* alias meta param ident (Module (Global ident)) Strict *)
241-
| Lprim (Pmakeblock (_, _, Immutable ) , ls) ->
241+
| Lprim {primitive = Pmakeblock (_, _, Immutable) ;args ; _} ->
242242
Hashtbl.replace meta.ident_tbl param
243-
(Lam_util.kind_of_lambda_block Normal ls ); (** *)
243+
(Lam_util.kind_of_lambda_block Normal args ); (** *)
244244
arg
245245
| _ -> arg in
246246
Lam_util.refine_let param arg l)
@@ -257,7 +257,7 @@ let propogate_beta_reduce_with_map
257257
match arg with
258258
| Lconst _
259259
| Lvar _ -> rest_bindings , arg :: acc
260-
| Lprim (Pgetglobal ident, [])
260+
| Lprim {primitive = Pgetglobal ident; args = []}
261261
(* TODO: we can pass Global, but you also need keep track of it*)
262262
->
263263
let p = Ident.rename old_param in
@@ -292,13 +292,13 @@ let propogate_beta_reduce_with_map
292292
Hashtbl.add meta.ident_tbl param ident_info
293293
end;
294294
arg
295-
| Lprim (Pgetglobal ident, []) ->
295+
| Lprim {primitive = Pgetglobal ident; args = []} ->
296296
(* It's not completeness, its to make it sound.. *)
297297
Lam_compile_global.query_lambda ident meta.env
298298
(* alias meta param ident (Module (Global ident)) Strict *)
299-
| Lprim (Pmakeblock (_, _, Immutable ) , ls) ->
299+
| Lprim {primitive = Pmakeblock (_, _, Immutable ) ; args} ->
300300
Hashtbl.replace meta.ident_tbl param
301-
(Lam_util.kind_of_lambda_block Normal ls ); (** *)
301+
(Lam_util.kind_of_lambda_block Normal args ); (** *)
302302
arg
303303
| _ -> arg in
304304
Lam_util.refine_let param arg l)

jscomp/lam_beta_reduce_util.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let simple_beta_reduce params body args =
6161
| _ :: _ -> raise E.Not_simple_apply
6262
in
6363
match (body : Lam.t) with
64-
| Lprim ( primitive , args' , _) (* There is no lambda in primitive *)
64+
| Lprim { primitive ; args = args' ; _} (* There is no lambda in primitive *)
6565
-> (* catch a special case of primitives *)
6666
(* Note in a very special case we can avoid any allocation
6767
{[
@@ -80,7 +80,7 @@ let simple_beta_reduce params body args =
8080
Hashtbl.fold (fun _param {lambda; used} code ->
8181
if not used then
8282
Lam.seq lambda code
83-
else code) param_hash (Lam.prim primitive us (List.length us)) in
83+
else code) param_hash (Lam.prim primitive us ) in
8484
Hashtbl.clear param_hash;
8585
Some result
8686
| exception _ ->

0 commit comments

Comments
 (0)