Skip to content

Commit b4449d1

Browse files
committed
[feature] finish lamb constant -primitive propogation (#349)
1 parent b959f6b commit b4449d1

7 files changed

+156
-78
lines changed

jscomp/lam_comb.ml

+110-11
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ let comparison (cmp : Lambda.comparison) a b : bool =
160160
| Clt -> a < b
161161
| Cge -> a >= b
162162

163-
let int i : t =
163+
let lift_int i : t =
164164
Lconst (Const_base (Const_int i))
165165

166166

@@ -169,15 +169,54 @@ let int32 i : t =
169169

170170
let lift_bool b = if b then true_ else false_
171171

172+
(* ATTENTION: [float, nativeint] constant propogaton is not done
173+
yet , due to cross platform problem
174+
*)
175+
let lift_float b : t =
176+
Lconst (Const_base (Const_float b))
177+
178+
let lift_nativeint b : t =
179+
Lconst (Const_base (Const_nativeint b))
180+
181+
let lift_int32 b : t =
182+
Lconst (Const_base (Const_int32 b))
183+
184+
let lift_int64 b : t =
185+
Lconst (Const_base (Const_int64 b))
186+
172187
let prim (prim : Prim.t) (ll : t list) : t =
173188
let default () : t = Lprim(prim,ll) in
174189
match ll with
175190
| [Lconst a] ->
176191
begin match prim, a with
177192
| Pnegint, (Const_base (Const_int a))
178-
-> int (- a)
193+
-> lift_int (- a)
194+
(* | Pfloatofint, (Const_base (Const_int a)) *)
195+
(* -> lift_float (float_of_int a) *)
196+
| Pintoffloat, (Const_base (Const_float a))
197+
->
198+
lift_int (int_of_float (float_of_string a))
199+
(* | Pnegfloat -> lift_float (-. a) *)
200+
(* | Pabsfloat -> lift_float (abs_float a) *)
201+
| Pstringlength, (Const_base (Const_string (a,_)) )
202+
->
203+
lift_int (String.length a)
204+
(* | Pnegbint Pnativeint, (Const_base (Const_nativeint i)) *)
205+
(* -> *)
206+
(* lift_nativeint (Nativeint.neg i) *)
207+
| Pnegbint Pint32, (Const_base (Const_int32 a))
208+
->
209+
lift_int32 (Int32.neg a)
210+
| Pnegbint Pint64, (Const_base (Const_int64 a))
211+
->
212+
lift_int64 (Int64.neg a)
213+
| Pnot , Const_pointer (a,_)
214+
-> lift_bool (a = 0 )
215+
179216
| _ -> default ()
180217
end
218+
219+
181220
| [Lconst a ; Lconst b] ->
182221
begin match prim, a, b with
183222
| Pbintcomp(_, cmp), Const_base (Const_int32 a), Const_base (Const_int32 b)
@@ -186,6 +225,11 @@ let prim (prim : Prim.t) (ll : t list) : t =
186225
-> lift_bool (comparison cmp a b)
187226
| Pbintcomp(_, cmp), Const_base (Const_nativeint a), Const_base (Const_nativeint b)
188227
-> lift_bool (comparison cmp a b)
228+
| Pfloatcomp cmp, Const_base (Const_nativeint a), Const_base (Const_nativeint b)
229+
-> lift_bool (comparison cmp a b)
230+
| Pintcomp cmp , Const_base (Const_int a), Const_base (Const_int b)
231+
-> lift_bool (comparison cmp a b)
232+
189233
| (Paddint
190234
| Psubint
191235
| Pmulint
@@ -196,34 +240,89 @@ let prim (prim : Prim.t) (ll : t list) : t =
196240
| Pxorint
197241
| Plslint
198242
| Plsrint
199-
| Pasrint | Pintcomp _), _, _
243+
| Pasrint),Const_base (Const_int a), Const_base (Const_int b)
200244
->
201-
begin match a, b with
202-
| Const_base (Const_int a), Const_base (Const_int b)
203-
->
204245
(* WE SHOULD keep it as [int], to preserve types *)
205246
let aa,bb = Int32.of_int a, Int32.of_int b in
206-
let int_ v = int (Int32.to_int v ) in
247+
let int_ v = lift_int (Int32.to_int v ) in
207248
begin match prim with
208249
| Paddint -> int_ (Int32.add aa bb)
209250
| Psubint -> int_ (Int32.sub aa bb)
210251
| Pmulint -> int_ (Int32.mul aa bb)
211-
| Pdivint -> int_ (Int32.div aa bb)
252+
| Pdivint -> (try int_ (Int32.div aa bb) with _ -> default ())
212253
| Pmodint -> int_ (Int32.rem aa bb)
213254
| Pandint -> int_ (Int32.logand aa bb)
214255
| Porint -> int_ (Int32.logor aa bb)
215256
| Pxorint -> int_ (Int32.logxor aa bb)
216257
| Plslint -> int_ (Int32.shift_left aa b )
217258
| Plsrint -> int_ (Int32.shift_right_logical aa b)
218259
| Pasrint -> int_ (Int32.shift_right aa b)
219-
| Pintcomp cmp
220-
-> lift_bool (comparison cmp a b)
221260
| _ -> default ()
222261
end
262+
| (Paddbint Pint32
263+
| Psubbint Pint32
264+
| Pmulbint Pint32
265+
| Pdivbint Pint32
266+
| Pmodbint Pint32
267+
| Pandbint Pint32
268+
| Porbint Pint32
269+
| Pxorbint Pint32
270+
), Const_base (Const_int32 aa), Const_base (Const_int32 bb)
271+
->
272+
begin match prim with
273+
| Paddbint _ -> lift_int32 (Int32.add aa bb)
274+
| Psubbint _ -> lift_int32 (Int32.sub aa bb)
275+
| Pmulbint _ -> lift_int32 (Int32.mul aa bb)
276+
| Pdivbint _ -> (try lift_int32 (Int32.div aa bb) with _ -> default ())
277+
| Pmodbint _ -> lift_int32 (Int32.rem aa bb)
278+
| Pandbint _ -> lift_int32 (Int32.logand aa bb)
279+
| Porbint _ -> lift_int32 (Int32.logor aa bb)
280+
| Pxorbint _ -> lift_int32 (Int32.logxor aa bb)
223281
| _ -> default ()
224-
end
282+
end
283+
| Plslbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
284+
-> lift_int32 (Int32.shift_left aa b )
285+
| Plsrbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
286+
-> lift_int32 (Int32.shift_right_logical aa b )
287+
| Pasrbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
288+
-> lift_int32 (Int32.shift_right aa b )
289+
290+
| (Paddbint Pint64
291+
| Psubbint Pint64
292+
| Pmulbint Pint64
293+
| Pdivbint Pint64
294+
| Pmodbint Pint64
295+
| Pandbint Pint64
296+
| Porbint Pint64
297+
| Pxorbint Pint64
298+
), Const_base (Const_int64 aa), Const_base (Const_int64 bb)
299+
->
300+
begin match prim with
301+
| Paddbint _ -> lift_int64 (Int64.add aa bb)
302+
| Psubbint _ -> lift_int64 (Int64.sub aa bb)
303+
| Pmulbint _ -> lift_int64 (Int64.mul aa bb)
304+
| Pdivbint _ -> (try lift_int64 (Int64.div aa bb) with _ -> default ())
305+
| Pmodbint _ -> lift_int64 (Int64.rem aa bb)
306+
| Pandbint _ -> lift_int64 (Int64.logand aa bb)
307+
| Porbint _ -> lift_int64 (Int64.logor aa bb)
308+
| Pxorbint _ -> lift_int64 (Int64.logxor aa bb)
309+
| _ -> default ()
310+
end
311+
| Plslbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
312+
-> lift_int64 (Int64.shift_left aa b )
313+
| Plsrbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
314+
-> lift_int64 (Int64.shift_right_logical aa b )
315+
| Pasrbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
316+
-> lift_int64 (Int64.shift_right aa b )
317+
| Psequand, Const_pointer (a, _), Const_pointer( b, _)
318+
->
319+
lift_bool (a = 1 && b = 1)
320+
| Psequor, Const_pointer (a, _), Const_pointer( b, _)
321+
->
322+
lift_bool (a = 1 || b = 1)
225323
| _ -> default ()
226324
end
325+
227326
| _ -> default ()
228327

229328

jscomp/test/ext_filename.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ function relative_path(file1, file2) {
130130

131131
var node_modules = "node_modules";
132132

133-
var node_modules_length = 12;
134-
135133
function node_relative_path(path1, path2) {
136134
var v = Ext_string.find(/* None */0, node_modules, path2);
137135
var len = path2.length;
@@ -159,7 +157,7 @@ function node_relative_path(path1, path2) {
159157
}
160158
};
161159
};
162-
return Ext_string.tail_from(path2, skip(v + node_modules_length | 0));
160+
return Ext_string.tail_from(path2, skip(v + 12 | 0));
163161
}
164162
else {
165163
return relative_path(try_chop_extension(absolute_path(path2)), try_chop_extension(absolute_path(path1))) + (node_sep + try_chop_extension(Curry._1(Filename.basename, path2)));
@@ -202,6 +200,8 @@ function resolve(cwd, module_name) {
202200
};
203201
}
204202

203+
var node_modules_length = 12;
204+
205205
exports.node_sep = node_sep;
206206
exports.node_parent = node_parent;
207207
exports.node_current = node_current;

jscomp/test/float_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ Mt.from_pair_suites("float_test.ml", Pervasives.$at(/* :: */[
268268
"int_of_float",
269269
function () {
270270
return /* Eq */Block.__(0, [
271-
3.2 | 0,
271+
3,
272272
3
273273
]);
274274
}

jscomp/test/int64_mul_div_test.js

+8-14
Original file line numberDiff line numberDiff line change
@@ -1777,13 +1777,10 @@ Mt.from_pair_suites("int64_mul_div_test.ml", Pervasives.$at(from_pairs("random",
17771777
"div_rem_0",
17781778
function () {
17791779
return /* Eq */Block.__(0, [
1780-
Caml_int64.div(/* int64 */[
1781-
/* hi */-1,
1782-
/* lo */4294967295
1783-
], /* int64 */[
1784-
/* hi */0,
1785-
/* lo */16
1786-
]),
1780+
/* int64 */[
1781+
/* hi */0,
1782+
/* lo */0
1783+
],
17871784
/* int64 */[
17881785
/* hi */0,
17891786
/* lo */0
@@ -1796,13 +1793,10 @@ Mt.from_pair_suites("int64_mul_div_test.ml", Pervasives.$at(from_pairs("random",
17961793
"div_rem_1",
17971794
function () {
17981795
return /* Eq */Block.__(0, [
1799-
Caml_int64.mod_(/* int64 */[
1800-
/* hi */-1,
1801-
/* lo */4294967295
1802-
], /* int64 */[
1803-
/* hi */0,
1804-
/* lo */16
1805-
]),
1796+
/* int64 */[
1797+
/* hi */-1,
1798+
/* lo */4294967295
1799+
],
18061800
/* int64 */[
18071801
/* hi */-1,
18081802
/* lo */4294967295

0 commit comments

Comments
 (0)