29
29
30
30
let no_side_effect = Js_analyzer. no_side_effect_expression
31
31
32
+ type t = J .expression
33
+
32
34
type binary_op =
33
35
?comment:string ->
34
- J .expression ->
35
- J .expression ->
36
- J .expression
36
+ t ->
37
+ t ->
38
+ t
37
39
type unary_op =
38
40
?comment:string ->
39
- J .expression ->
40
- J .expression
41
+ t ->
42
+ t
43
+
41
44
42
45
(*
43
- Remove pure part of the expression
46
+ [remove_pure_sub_exp x]
47
+ Remove pure part of the expression (minor optimization)
44
48
and keep the non-pure part while preserve the semantics
45
49
(modulo return value)
50
+ It will return None if [x] is pure
46
51
*)
47
- let rec extract_non_pure (x : J.expression ) =
52
+ let rec remove_pure_sub_exp (x : t ) : t option =
48
53
match x.expression_desc with
49
54
| Var _
50
55
| Str _
51
56
| Number _ -> None (* Can be refined later *)
52
57
| Access (a ,b ) ->
53
- begin match extract_non_pure a , extract_non_pure b with
58
+ begin match remove_pure_sub_exp a , remove_pure_sub_exp b with
54
59
| None , None -> None
55
60
| _ , _ -> Some x
56
61
end
57
62
| Array (xs ,_mutable_flag ) ->
58
- if List. for_all (fun x -> extract_non_pure x = None ) xs then
63
+ if List. for_all (fun x -> remove_pure_sub_exp x = None ) xs then
59
64
None
60
65
else Some x
61
66
| Seq (a ,b ) ->
62
- begin match extract_non_pure a , extract_non_pure b with
67
+ begin match remove_pure_sub_exp a , remove_pure_sub_exp b with
63
68
| None , None -> None
64
69
| Some u , Some v ->
65
70
Some { x with expression_desc = Seq (u,v)}
@@ -69,20 +74,28 @@ let rec extract_non_pure (x : J.expression) =
69
74
end
70
75
| _ -> Some x
71
76
72
- type t = J .expression
73
77
74
- let mk ?comment exp : t =
75
- {expression_desc = exp ; comment }
78
+ (* let mk ?comment exp : t =
79
+ {expression_desc = exp ; comment } *)
76
80
77
81
let var ?comment id : t =
78
82
{expression_desc = Var (Id id); comment }
79
83
84
+ let call ?comment ~info e0 args : t =
85
+ {expression_desc = Call (e0,args,info); comment }
86
+
87
+ let flat_call ?comment e0 es : t =
88
+ (* TODO: optimization when es is known at compile time
89
+ to be an array
90
+ *)
91
+ {expression_desc = FlatCall (e0,es); comment }
92
+
80
93
let runtime_var_dot ?comment (x : string ) (e1 : string ) : J.expression =
81
94
{expression_desc =
82
95
Var (Qualified (Ext_ident. create_js x,Runtime , Some e1)); comment }
83
96
84
- let runtime_var_vid x e1 : J.vident =
85
- Qualified (Ext_ident. create_js x,Runtime , Some e1)
97
+ (* let runtime_var_vid x e1 : J.vident =
98
+ Qualified(Ext_ident.create_js x,Runtime, Some e1) *)
86
99
87
100
let ml_var_dot ?comment ( id : Ident.t ) e : J.expression =
88
101
{expression_desc = Var (Qualified (id, Ml , Some e)); comment }
@@ -97,8 +110,17 @@ let external_var_dot ?comment ~external_name:name ?dot (id : Ident.t) : t =
97
110
{expression_desc = Var (Qualified (id, External name, dot)); comment }
98
111
99
112
100
- let ml_var ?comment (id : Ident.t ) : t =
101
- {expression_desc = Var (Qualified (id, Ml , None )); comment}
113
+ (* let ml_var ?comment (id : Ident.t) : t =
114
+ {expression_desc = Var (Qualified (id, Ml, None)); comment} *)
115
+
116
+ (* Dot .....................**)
117
+ let runtime_call ?comment module_name fn_name args =
118
+ call ?comment
119
+ ~info: Js_call_info. builtin_runtime_call
120
+ (runtime_var_dot module_name fn_name) args
121
+
122
+ let runtime_ref module_name fn_name =
123
+ runtime_var_dot module_name fn_name
102
124
103
125
let str ?(pure =true ) ?comment s : t =
104
126
{expression_desc = Str (pure,s); comment}
@@ -109,12 +131,13 @@ let unicode ?comment s : t =
109
131
let raw_js_code ?comment info s : t =
110
132
{expression_desc = Raw_js_code (s,info) ; comment }
111
133
134
+ (* TODO: could optimize literal *)
112
135
let anything_to_string ?comment (e : t ) : t =
113
136
match e.expression_desc with
114
137
| Str _ -> e
115
138
| _ -> {expression_desc = Anything_to_string e ; comment}
116
139
117
- let arr ?comment mt es : t =
140
+ let array ?comment mt es : t =
118
141
{expression_desc = Array (es,mt) ; comment}
119
142
120
143
let sep = " : "
@@ -150,10 +173,10 @@ let make_block ?comment tag tag_info es mutable_flag : t =
150
173
(* let uninitialized_object ?comment tag size : t =
151
174
{ expression_desc = Caml_uninitialized_obj(tag,size); comment } *)
152
175
153
- let uninitialized_array ?comment (e : t ) : t =
176
+ (* let uninitialized_array ?comment (e : t) : t =
154
177
match e.expression_desc with
155
- | Number (Int {i = 0l ; _} ) -> arr ?comment NA []
156
- | _ -> {comment; expression_desc = Array_of_size e}
178
+ | Number (Int {i = 0l; _}) -> array ?comment NA []
179
+ | _ -> {comment; expression_desc = Array_of_size e} *)
157
180
158
181
159
182
module L = Literals
@@ -340,23 +363,8 @@ let index_addr ?comment ~yes ~no (e0 : t) e1 : t =
340
363
| _ ->
341
364
yes ({ expression_desc = Access (e0, int ?comment e1); comment = None } : t)
342
365
343
- let call ?comment ~info e0 args : t =
344
- {expression_desc = Call (e0,args,info); comment }
345
-
346
- let flat_call ?comment e0 es : t =
347
- (* TODO: optimization when es is known at compile time
348
- to be an array
349
- *)
350
- {expression_desc = FlatCall (e0,es); comment }
351
366
352
- (* Dot .....................**)
353
- let runtime_call ?comment module_name fn_name args =
354
- call ?comment
355
- ~info: Js_call_info. builtin_runtime_call
356
- (runtime_var_dot module_name fn_name) args
357
367
358
- let runtime_ref module_name fn_name =
359
- runtime_var_dot module_name fn_name
360
368
361
369
362
370
(* only used in property access,
@@ -907,7 +915,7 @@ let public_method_call meth_name obj label cache args =
907
915
[label;
908
916
int cache;
909
917
obj ;
910
- arr NA (obj::args)
918
+ array NA (obj::args)
911
919
]
912
920
913
921
(* TODO: handle arbitrary length of args ..
0 commit comments