Skip to content

[refact & fix] smart ctor for prim (to finish), fix max_int, min_int … #347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 109 additions & 33 deletions jscomp/lam_comb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@ type triop = t -> t -> t -> t

type unop = t -> t



module Prim = struct
type t = Lambda.primitive
let js_is_nil : t =
Lambda.Pccall{ prim_name = "js_is_nil";
prim_arity = 1 ;
prim_alloc = false;
prim_native_name = "js_is_nil";
prim_native_float = false;
prim_attributes = [];
prim_ty = None
}

let js_is_undef : t =
Lambda.Pccall{ prim_name = "js_is_undef";
prim_arity = 1 ;
prim_alloc = false;
prim_native_name = "js_is_undef";
prim_native_float = false;
prim_attributes = [];
prim_ty = None
}

let js_is_nil_undef : t =
Lambda.Pccall{ prim_name = "js_is_nil_undef";
prim_arity = 1 ;
prim_alloc = false;
prim_native_name = "js_is_nil_undef";
prim_native_float = false;
prim_attributes = [];
prim_ty = None
}

end

let if_ (a : t) (b : t) c =
match a with
| Lconst v ->
Expand Down Expand Up @@ -115,41 +151,81 @@ let staticcatch a b c : t = Lstaticcatch(a,b,c)

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

let prim prim ll : t = Lprim(prim,ll)
let comparison (cmp : Lambda.comparison) a b : bool =
match cmp with
| Ceq -> a = b
| Cneq -> a <> b
| Cgt -> a > b
| Cle -> a <= b
| Clt -> a < b
| Cge -> a >= b

let not x : t =
prim Pnot [x]
let int i : t =
Lconst (Const_base (Const_int i))

module Prim = struct
type t = Lambda.primitive
let js_is_nil : t =
Lambda.Pccall{ prim_name = "js_is_nil";
prim_arity = 1 ;
prim_alloc = false;
prim_native_name = "js_is_nil";
prim_native_float = false;
prim_attributes = [];
prim_ty = None
}

let js_is_undef : t =
Lambda.Pccall{ prim_name = "js_is_undef";
prim_arity = 1 ;
prim_alloc = false;
prim_native_name = "js_is_undef";
prim_native_float = false;
prim_attributes = [];
prim_ty = None
}
let int32 i : t =
Lconst (Const_base (Const_int32 i))

let js_is_nil_undef : t =
Lambda.Pccall{ prim_name = "js_is_nil_undef";
prim_arity = 1 ;
prim_alloc = false;
prim_native_name = "js_is_nil_undef";
prim_native_float = false;
prim_attributes = [];
prim_ty = None
}
let lift_bool b = if b then true_ else false_

end
let prim (prim : Prim.t) (ll : t list) : t =
let default () : t = Lprim(prim,ll) in
match ll with
| [Lconst a] ->
begin match prim, a with
| Pnegint, (Const_base (Const_int a))
-> int (- a)
| _ -> default ()
end
| [Lconst a ; Lconst b] ->
begin match prim, a, b with
| Pbintcomp(_, cmp), Const_base (Const_int32 a), Const_base (Const_int32 b)
-> lift_bool (comparison cmp a b)
| Pbintcomp(_, cmp), Const_base (Const_int64 a), Const_base (Const_int64 b)
-> lift_bool (comparison cmp a b)
| Pbintcomp(_, cmp), Const_base (Const_nativeint a), Const_base (Const_nativeint b)
-> lift_bool (comparison cmp a b)
| (Paddint
| Psubint
| Pmulint
| Pdivint
| Pmodint
| Pandint
| Porint
| Pxorint
| Plslint
| Plsrint
| Pasrint | Pintcomp _), _, _
->
begin match a, b with
| Const_base (Const_int a), Const_base (Const_int b)
->
(* WE SHOULD keep it as [int], to preserve types *)
let aa,bb = Int32.of_int a, Int32.of_int b in
let int_ v = int (Int32.to_int v ) in
begin match prim with
| Paddint -> int_ (Int32.add aa bb)
| Psubint -> int_ (Int32.sub aa bb)
| Pmulint -> int_ (Int32.mul aa bb)
| Pdivint -> int_ (Int32.div aa bb)
| Pmodint -> int_ (Int32.rem aa bb)
| Pandint -> int_ (Int32.logand aa bb)
| Porint -> int_ (Int32.logor aa bb)
| Pxorint -> int_ (Int32.logxor aa bb)
| Plslint -> int_ (Int32.shift_left aa b )
| Plsrint -> int_ (Int32.shift_right_logical aa b)
| Pasrint -> int_ (Int32.shift_right aa b)
| Pintcomp cmp
-> lift_bool (comparison cmp a b)
| _ -> default ()
end
| _ -> default ()
end
| _ -> default ()
end
| _ -> default ()


let not x : t =
prim Pnot [x]
5 changes: 1 addition & 4 deletions jscomp/lam_compile_global.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ open Js_output.Ops
*)


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

let query_lambda id env =
Lam_compile_env.query_and_add_if_not_exist (Lam_module_ident.of_ml id)
(Has_env env)
~not_found:(fun id -> assert false)
~found:(fun {signature = sigs; _} ->
Lam_comb.prim (Pmakeblock(0, Blk_na, Immutable))
Lam_comb.prim (Pmakeblock(0, Blk_module None, Immutable))
(List.mapi (fun i _ ->
Lam_comb.prim (Pfield (i, Lambda.Fld_na))
[Lam_comb.prim (Pgetglobal id) [] ])
Expand Down
6 changes: 3 additions & 3 deletions jscomp/stdlib/pervasives.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ function lnot(x) {
return x ^ -1;
}

var max_int = 2147483647;

var min_int = max_int + 1 | 0;
var min_int = -2147483648;

function $caret(a, b) {
return a + b;
Expand Down Expand Up @@ -547,6 +545,8 @@ function exit() {
}();
}

var max_int = 2147483647;

var infinity = Infinity;

var neg_infinity = -Infinity;
Expand Down
8 changes: 8 additions & 0 deletions jscomp/test/.depend
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ libarg_test.cmx : ../stdlib/printf.cmx mt.cmx ../stdlib/list.cmx \
../stdlib/arg.cmx
libqueue_test.cmo : ../stdlib/queue.cmi ../stdlib/list.cmi
libqueue_test.cmx : ../stdlib/queue.cmx ../stdlib/list.cmx
limits_test.cmo : mt.cmi ../stdlib/int32.cmi
limits_test.cmx : mt.cmx ../stdlib/int32.cmx
list_test.cmo : ../stdlib/pervasives.cmi mt.cmi ../stdlib/list.cmi \
../stdlib/array.cmi
list_test.cmx : ../stdlib/pervasives.cmx mt.cmx ../stdlib/list.cmx \
Expand Down Expand Up @@ -427,6 +429,8 @@ test_common.cmo :
test_common.cmx :
test_const_elim.cmo :
test_const_elim.cmx :
test_const_propogate.cmo :
test_const_propogate.cmx :
test_cps.cmo : ../stdlib/array.cmi
test_cps.cmx : ../stdlib/array.cmx
test_demo.cmo : ../stdlib/list.cmi
Expand Down Expand Up @@ -891,6 +895,8 @@ libarg_test.cmj : ../stdlib/printf.cmj mt.cmj ../stdlib/list.cmj \
../stdlib/arg.cmj
libqueue_test.cmo : ../stdlib/queue.cmi ../stdlib/list.cmi
libqueue_test.cmj : ../stdlib/queue.cmj ../stdlib/list.cmj
limits_test.cmo : mt.cmi ../stdlib/int32.cmi
limits_test.cmj : mt.cmj ../stdlib/int32.cmj
list_test.cmo : ../stdlib/pervasives.cmi mt.cmi ../stdlib/list.cmi \
../stdlib/array.cmi
list_test.cmj : ../stdlib/pervasives.cmj mt.cmj ../stdlib/list.cmj \
Expand Down Expand Up @@ -1035,6 +1041,8 @@ test_common.cmo :
test_common.cmj :
test_const_elim.cmo :
test_const_elim.cmj :
test_const_propogate.cmo :
test_const_propogate.cmj :
test_cps.cmo : ../stdlib/array.cmi
test_cps.cmj : ../stdlib/array.cmj
test_demo.cmo : ../stdlib/list.cmi
Expand Down
8 changes: 4 additions & 4 deletions jscomp/test/bdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ function getId(bdd) {
}
}

var initSize_1 = 8191;

var nodeC = [1];

var sz_1 = [initSize_1];
var sz_1 = [8191];

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

Expand Down Expand Up @@ -120,7 +118,7 @@ function insert(idl, idh, v, ind, bucket, newNode) {
}

function resetUnique() {
sz_1[0] = initSize_1;
sz_1[0] = 8191;
htab[0] = Caml_array.caml_make_vect(sz_1[0] + 1 | 0, /* [] */0);
n_items[0] = 0;
nodeC[0] = 1;
Expand Down Expand Up @@ -431,6 +429,8 @@ function main() {

main(/* () */0);

var initSize_1 = 8191;

var zero = /* Zero */1;

var one = /* One */0;
Expand Down
4 changes: 2 additions & 2 deletions jscomp/test/int32_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ var shift_left_tests = /* tuple */[
shift_left_tests_001
];

var test_div = 30;

var $star$tilde = Caml_int32.imul

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

var test_div = 30;

exports.f = f;
exports.shift_right_logical_tests = shift_right_logical_tests;
exports.shift_right_tests = shift_right_tests;
Expand Down
64 changes: 8 additions & 56 deletions jscomp/test/int64_mul_div_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,62 +875,14 @@ var to_floats = Caml_obj.caml_obj_dup(/* array */[
]);

var check_complete_compare = /* int array */[
Caml_int64.ge(/* int64 */[
/* hi */0,
/* lo */3
], /* int64 */[
/* hi */0,
/* lo */2
]),
Caml_int64.ge(/* int64 */[
/* hi */0,
/* lo */3
], /* int64 */[
/* hi */0,
/* lo */3
]),
Caml_int64.eq(/* int64 */[
/* hi */0,
/* lo */3
], /* int64 */[
/* hi */0,
/* lo */3
]),
Caml_int64.eq(/* int64 */[
/* hi */0,
/* lo */2
], /* int64 */[
/* hi */0,
/* lo */2
]),
Caml_int64.lt(/* int64 */[
/* hi */0,
/* lo */2
], /* int64 */[
/* hi */0,
/* lo */3
]),
Caml_int64.gt(/* int64 */[
/* hi */0,
/* lo */3
], /* int64 */[
/* hi */0,
/* lo */2
]),
Caml_int64.le(/* int64 */[
/* hi */0,
/* lo */2
], /* int64 */[
/* hi */0,
/* lo */3
]),
Caml_int64.le(/* int64 */[
/* hi */0,
/* lo */3
], /* int64 */[
/* hi */0,
/* lo */3
])
/* true */1,
/* true */1,
/* true */1,
/* true */1,
/* true */1,
/* true */1,
/* true */1,
/* true */1
];

var of_float_pairs = Caml_obj.caml_obj_dup(/* array */[
Expand Down
43 changes: 43 additions & 0 deletions jscomp/test/limits_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.3 , PLEASE EDIT WITH CARE
'use strict';

var Pervasives = require("../stdlib/pervasives");
var Mt = require("./mt");
var Block = require("../runtime/block");
var Int32 = require("../stdlib/int32");

var suites = [/* [] */0];

var test_id = [0];

function eq(loc, x, y) {
test_id[0] = test_id[0] + 1 | 0;
suites[0] = /* :: */[
/* tuple */[
loc + (" id " + test_id[0]),
function () {
return /* Eq */Block.__(0, [
x,
y
]);
}
],
suites[0]
];
return /* () */0;
}

eq('File "limits_test.ml", line 10, characters 5-12', Pervasives.max_int, (2147483647));

eq('File "limits_test.ml", line 11, characters 5-12', Pervasives.min_int, (-2147483648));

eq('File "limits_test.ml", line 12, characters 5-12', Int32.max_int, (2147483647));

eq('File "limits_test.ml", line 13, characters 5-12', Int32.min_int, (-2147483648));

Mt.from_pair_suites("limits_test.ml", suites[0]);

exports.suites = suites;
exports.test_id = test_id;
exports.eq = eq;
/* Not a pure module */
Loading