Skip to content

Commit b959f6b

Browse files
committed
[refact & fix] smart ctor for prim (to finish), fix max_int, min_int , add regression tests (#347)
1 parent b630a7e commit b959f6b

17 files changed

+236
-113
lines changed

jscomp/lam_comb.ml

Lines changed: 109 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,42 @@ type triop = t -> t -> t -> t
3434

3535
type unop = t -> t
3636

37+
38+
39+
module Prim = struct
40+
type t = Lambda.primitive
41+
let js_is_nil : t =
42+
Lambda.Pccall{ prim_name = "js_is_nil";
43+
prim_arity = 1 ;
44+
prim_alloc = false;
45+
prim_native_name = "js_is_nil";
46+
prim_native_float = false;
47+
prim_attributes = [];
48+
prim_ty = None
49+
}
50+
51+
let js_is_undef : t =
52+
Lambda.Pccall{ prim_name = "js_is_undef";
53+
prim_arity = 1 ;
54+
prim_alloc = false;
55+
prim_native_name = "js_is_undef";
56+
prim_native_float = false;
57+
prim_attributes = [];
58+
prim_ty = None
59+
}
60+
61+
let js_is_nil_undef : t =
62+
Lambda.Pccall{ prim_name = "js_is_nil_undef";
63+
prim_arity = 1 ;
64+
prim_alloc = false;
65+
prim_native_name = "js_is_nil_undef";
66+
prim_native_float = false;
67+
prim_attributes = [];
68+
prim_ty = None
69+
}
70+
71+
end
72+
3773
let if_ (a : t) (b : t) c =
3874
match a with
3975
| Lconst v ->
@@ -115,41 +151,81 @@ let staticcatch a b c : t = Lstaticcatch(a,b,c)
115151

116152
let staticraise a b : t = Lstaticraise(a,b)
117153

118-
let prim prim ll : t = Lprim(prim,ll)
154+
let comparison (cmp : Lambda.comparison) a b : bool =
155+
match cmp with
156+
| Ceq -> a = b
157+
| Cneq -> a <> b
158+
| Cgt -> a > b
159+
| Cle -> a <= b
160+
| Clt -> a < b
161+
| Cge -> a >= b
119162

120-
let not x : t =
121-
prim Pnot [x]
163+
let int i : t =
164+
Lconst (Const_base (Const_int i))
122165

123-
module Prim = struct
124-
type t = Lambda.primitive
125-
let js_is_nil : t =
126-
Lambda.Pccall{ prim_name = "js_is_nil";
127-
prim_arity = 1 ;
128-
prim_alloc = false;
129-
prim_native_name = "js_is_nil";
130-
prim_native_float = false;
131-
prim_attributes = [];
132-
prim_ty = None
133-
}
134166

135-
let js_is_undef : t =
136-
Lambda.Pccall{ prim_name = "js_is_undef";
137-
prim_arity = 1 ;
138-
prim_alloc = false;
139-
prim_native_name = "js_is_undef";
140-
prim_native_float = false;
141-
prim_attributes = [];
142-
prim_ty = None
143-
}
167+
let int32 i : t =
168+
Lconst (Const_base (Const_int32 i))
144169

145-
let js_is_nil_undef : t =
146-
Lambda.Pccall{ prim_name = "js_is_nil_undef";
147-
prim_arity = 1 ;
148-
prim_alloc = false;
149-
prim_native_name = "js_is_nil_undef";
150-
prim_native_float = false;
151-
prim_attributes = [];
152-
prim_ty = None
153-
}
170+
let lift_bool b = if b then true_ else false_
154171

155-
end
172+
let prim (prim : Prim.t) (ll : t list) : t =
173+
let default () : t = Lprim(prim,ll) in
174+
match ll with
175+
| [Lconst a] ->
176+
begin match prim, a with
177+
| Pnegint, (Const_base (Const_int a))
178+
-> int (- a)
179+
| _ -> default ()
180+
end
181+
| [Lconst a ; Lconst b] ->
182+
begin match prim, a, b with
183+
| Pbintcomp(_, cmp), Const_base (Const_int32 a), Const_base (Const_int32 b)
184+
-> lift_bool (comparison cmp a b)
185+
| Pbintcomp(_, cmp), Const_base (Const_int64 a), Const_base (Const_int64 b)
186+
-> lift_bool (comparison cmp a b)
187+
| Pbintcomp(_, cmp), Const_base (Const_nativeint a), Const_base (Const_nativeint b)
188+
-> lift_bool (comparison cmp a b)
189+
| (Paddint
190+
| Psubint
191+
| Pmulint
192+
| Pdivint
193+
| Pmodint
194+
| Pandint
195+
| Porint
196+
| Pxorint
197+
| Plslint
198+
| Plsrint
199+
| Pasrint | Pintcomp _), _, _
200+
->
201+
begin match a, b with
202+
| Const_base (Const_int a), Const_base (Const_int b)
203+
->
204+
(* WE SHOULD keep it as [int], to preserve types *)
205+
let aa,bb = Int32.of_int a, Int32.of_int b in
206+
let int_ v = int (Int32.to_int v ) in
207+
begin match prim with
208+
| Paddint -> int_ (Int32.add aa bb)
209+
| Psubint -> int_ (Int32.sub aa bb)
210+
| Pmulint -> int_ (Int32.mul aa bb)
211+
| Pdivint -> int_ (Int32.div aa bb)
212+
| Pmodint -> int_ (Int32.rem aa bb)
213+
| Pandint -> int_ (Int32.logand aa bb)
214+
| Porint -> int_ (Int32.logor aa bb)
215+
| Pxorint -> int_ (Int32.logxor aa bb)
216+
| Plslint -> int_ (Int32.shift_left aa b )
217+
| Plsrint -> int_ (Int32.shift_right_logical aa b)
218+
| Pasrint -> int_ (Int32.shift_right aa b)
219+
| Pintcomp cmp
220+
-> lift_bool (comparison cmp a b)
221+
| _ -> default ()
222+
end
223+
| _ -> default ()
224+
end
225+
| _ -> default ()
226+
end
227+
| _ -> default ()
228+
229+
230+
let not x : t =
231+
prim Pnot [x]

jscomp/lam_compile_global.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ open Js_output.Ops
3939
*)
4040

4141

42-
(* TODO: add module into taginfo*)
43-
(* let len = List.length sigs in *)
44-
(* TODO: could be optimized *)
4542

4643
let query_lambda id env =
4744
Lam_compile_env.query_and_add_if_not_exist (Lam_module_ident.of_ml id)
4845
(Has_env env)
4946
~not_found:(fun id -> assert false)
5047
~found:(fun {signature = sigs; _} ->
51-
Lam_comb.prim (Pmakeblock(0, Blk_na, Immutable))
48+
Lam_comb.prim (Pmakeblock(0, Blk_module None, Immutable))
5249
(List.mapi (fun i _ ->
5350
Lam_comb.prim (Pfield (i, Lambda.Fld_na))
5451
[Lam_comb.prim (Pgetglobal id) [] ])

jscomp/stdlib/pervasives.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ function lnot(x) {
5757
return x ^ -1;
5858
}
5959

60-
var max_int = 2147483647;
61-
62-
var min_int = max_int + 1 | 0;
60+
var min_int = -2147483648;
6361

6462
function $caret(a, b) {
6563
return a + b;
@@ -547,6 +545,8 @@ function exit() {
547545
}();
548546
}
549547

548+
var max_int = 2147483647;
549+
550550
var infinity = Infinity;
551551

552552
var neg_infinity = -Infinity;

jscomp/test/.depend

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ libarg_test.cmx : ../stdlib/printf.cmx mt.cmx ../stdlib/list.cmx \
283283
../stdlib/arg.cmx
284284
libqueue_test.cmo : ../stdlib/queue.cmi ../stdlib/list.cmi
285285
libqueue_test.cmx : ../stdlib/queue.cmx ../stdlib/list.cmx
286+
limits_test.cmo : mt.cmi ../stdlib/int32.cmi
287+
limits_test.cmx : mt.cmx ../stdlib/int32.cmx
286288
list_test.cmo : ../stdlib/pervasives.cmi mt.cmi ../stdlib/list.cmi \
287289
../stdlib/array.cmi
288290
list_test.cmx : ../stdlib/pervasives.cmx mt.cmx ../stdlib/list.cmx \
@@ -427,6 +429,8 @@ test_common.cmo :
427429
test_common.cmx :
428430
test_const_elim.cmo :
429431
test_const_elim.cmx :
432+
test_const_propogate.cmo :
433+
test_const_propogate.cmx :
430434
test_cps.cmo : ../stdlib/array.cmi
431435
test_cps.cmx : ../stdlib/array.cmx
432436
test_demo.cmo : ../stdlib/list.cmi
@@ -891,6 +895,8 @@ libarg_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/list.cmj \
891895
../stdlib/arg.cmj
892896
libqueue_test.cmo : ../stdlib/queue.cmi ../stdlib/list.cmi
893897
libqueue_test.cmj : ../stdlib/queue.cmj ../stdlib/list.cmj
898+
limits_test.cmo : mt.cmi ../stdlib/int32.cmi
899+
limits_test.cmj : mt.cmj ../stdlib/int32.cmj
894900
list_test.cmo : ../stdlib/pervasives.cmi mt.cmi ../stdlib/list.cmi \
895901
../stdlib/array.cmi
896902
list_test.cmj : ../stdlib/pervasives.cmj mt.cmj ../stdlib/list.cmj \
@@ -1035,6 +1041,8 @@ test_common.cmo :
10351041
test_common.cmj :
10361042
test_const_elim.cmo :
10371043
test_const_elim.cmj :
1044+
test_const_propogate.cmo :
1045+
test_const_propogate.cmj :
10381046
test_cps.cmo : ../stdlib/array.cmi
10391047
test_cps.cmj : ../stdlib/array.cmj
10401048
test_demo.cmo : ../stdlib/list.cmi

jscomp/test/bdd.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,9 @@ function getId(bdd) {
4343
}
4444
}
4545

46-
var initSize_1 = 8191;
47-
4846
var nodeC = [1];
4947

50-
var sz_1 = [initSize_1];
48+
var sz_1 = [8191];
5149

5250
var htab = [Caml_array.caml_make_vect(sz_1[0] + 1 | 0, /* [] */0)];
5351

@@ -120,7 +118,7 @@ function insert(idl, idh, v, ind, bucket, newNode) {
120118
}
121119

122120
function resetUnique() {
123-
sz_1[0] = initSize_1;
121+
sz_1[0] = 8191;
124122
htab[0] = Caml_array.caml_make_vect(sz_1[0] + 1 | 0, /* [] */0);
125123
n_items[0] = 0;
126124
nodeC[0] = 1;
@@ -431,6 +429,8 @@ function main() {
431429

432430
main(/* () */0);
433431

432+
var initSize_1 = 8191;
433+
434434
var zero = /* Zero */1;
435435

436436
var one = /* One */0;

jscomp/test/int32_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ var shift_left_tests = /* tuple */[
151151
shift_left_tests_001
152152
];
153153

154-
var test_div = 30;
155-
156154
var $star$tilde = Caml_int32.imul
157155

158156
Mt.from_pair_suites("int32_test.ml", Pervasives.$at(/* :: */[
@@ -242,6 +240,8 @@ Mt.from_pair_suites("int32_test.ml", Pervasives.$at(/* :: */[
242240
];
243241
}, shift_left_tests_000, shift_left_tests_001))))));
244242

243+
var test_div = 30;
244+
245245
exports.f = f;
246246
exports.shift_right_logical_tests = shift_right_logical_tests;
247247
exports.shift_right_tests = shift_right_tests;

jscomp/test/int64_mul_div_test.js

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -875,62 +875,14 @@ var to_floats = Caml_obj.caml_obj_dup(/* array */[
875875
]);
876876

877877
var check_complete_compare = /* int array */[
878-
Caml_int64.ge(/* int64 */[
879-
/* hi */0,
880-
/* lo */3
881-
], /* int64 */[
882-
/* hi */0,
883-
/* lo */2
884-
]),
885-
Caml_int64.ge(/* int64 */[
886-
/* hi */0,
887-
/* lo */3
888-
], /* int64 */[
889-
/* hi */0,
890-
/* lo */3
891-
]),
892-
Caml_int64.eq(/* int64 */[
893-
/* hi */0,
894-
/* lo */3
895-
], /* int64 */[
896-
/* hi */0,
897-
/* lo */3
898-
]),
899-
Caml_int64.eq(/* int64 */[
900-
/* hi */0,
901-
/* lo */2
902-
], /* int64 */[
903-
/* hi */0,
904-
/* lo */2
905-
]),
906-
Caml_int64.lt(/* int64 */[
907-
/* hi */0,
908-
/* lo */2
909-
], /* int64 */[
910-
/* hi */0,
911-
/* lo */3
912-
]),
913-
Caml_int64.gt(/* int64 */[
914-
/* hi */0,
915-
/* lo */3
916-
], /* int64 */[
917-
/* hi */0,
918-
/* lo */2
919-
]),
920-
Caml_int64.le(/* int64 */[
921-
/* hi */0,
922-
/* lo */2
923-
], /* int64 */[
924-
/* hi */0,
925-
/* lo */3
926-
]),
927-
Caml_int64.le(/* int64 */[
928-
/* hi */0,
929-
/* lo */3
930-
], /* int64 */[
931-
/* hi */0,
932-
/* lo */3
933-
])
878+
/* true */1,
879+
/* true */1,
880+
/* true */1,
881+
/* true */1,
882+
/* true */1,
883+
/* true */1,
884+
/* true */1,
885+
/* true */1
934886
];
935887

936888
var of_float_pairs = Caml_obj.caml_obj_dup(/* array */[

jscomp/test/limits_test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.3 , PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
var Pervasives = require("../stdlib/pervasives");
5+
var Mt = require("./mt");
6+
var Block = require("../runtime/block");
7+
var Int32 = require("../stdlib/int32");
8+
9+
var suites = [/* [] */0];
10+
11+
var test_id = [0];
12+
13+
function eq(loc, x, y) {
14+
test_id[0] = test_id[0] + 1 | 0;
15+
suites[0] = /* :: */[
16+
/* tuple */[
17+
loc + (" id " + test_id[0]),
18+
function () {
19+
return /* Eq */Block.__(0, [
20+
x,
21+
y
22+
]);
23+
}
24+
],
25+
suites[0]
26+
];
27+
return /* () */0;
28+
}
29+
30+
eq('File "limits_test.ml", line 10, characters 5-12', Pervasives.max_int, (2147483647));
31+
32+
eq('File "limits_test.ml", line 11, characters 5-12', Pervasives.min_int, (-2147483648));
33+
34+
eq('File "limits_test.ml", line 12, characters 5-12', Int32.max_int, (2147483647));
35+
36+
eq('File "limits_test.ml", line 13, characters 5-12', Int32.min_int, (-2147483648));
37+
38+
Mt.from_pair_suites("limits_test.ml", suites[0]);
39+
40+
exports.suites = suites;
41+
exports.test_id = test_id;
42+
exports.eq = eq;
43+
/* Not a pure module */

0 commit comments

Comments
 (0)