Skip to content

[feature] finish lamb constant -primitive propogation #349

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 6, 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
121 changes: 110 additions & 11 deletions jscomp/lam_comb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ let comparison (cmp : Lambda.comparison) a b : bool =
| Clt -> a < b
| Cge -> a >= b

let int i : t =
let lift_int i : t =
Lconst (Const_base (Const_int i))


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

let lift_bool b = if b then true_ else false_

(* ATTENTION: [float, nativeint] constant propogaton is not done
yet , due to cross platform problem
*)
let lift_float b : t =
Lconst (Const_base (Const_float b))

let lift_nativeint b : t =
Lconst (Const_base (Const_nativeint b))

let lift_int32 b : t =
Lconst (Const_base (Const_int32 b))

let lift_int64 b : t =
Lconst (Const_base (Const_int64 b))

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)
-> lift_int (- a)
(* | Pfloatofint, (Const_base (Const_int a)) *)
(* -> lift_float (float_of_int a) *)
| Pintoffloat, (Const_base (Const_float a))
->
lift_int (int_of_float (float_of_string a))
(* | Pnegfloat -> lift_float (-. a) *)
(* | Pabsfloat -> lift_float (abs_float a) *)
| Pstringlength, (Const_base (Const_string (a,_)) )
->
lift_int (String.length a)
(* | Pnegbint Pnativeint, (Const_base (Const_nativeint i)) *)
(* -> *)
(* lift_nativeint (Nativeint.neg i) *)
| Pnegbint Pint32, (Const_base (Const_int32 a))
->
lift_int32 (Int32.neg a)
| Pnegbint Pint64, (Const_base (Const_int64 a))
->
lift_int64 (Int64.neg a)
| Pnot , Const_pointer (a,_)
-> lift_bool (a = 0 )

| _ -> default ()
end


| [Lconst a ; Lconst b] ->
begin match prim, a, b with
| Pbintcomp(_, cmp), Const_base (Const_int32 a), Const_base (Const_int32 b)
Expand All @@ -186,6 +225,11 @@ let prim (prim : Prim.t) (ll : t list) : t =
-> lift_bool (comparison cmp a b)
| Pbintcomp(_, cmp), Const_base (Const_nativeint a), Const_base (Const_nativeint b)
-> lift_bool (comparison cmp a b)
| Pfloatcomp cmp, Const_base (Const_nativeint a), Const_base (Const_nativeint b)
-> lift_bool (comparison cmp a b)
| Pintcomp cmp , Const_base (Const_int a), Const_base (Const_int b)
-> lift_bool (comparison cmp a b)

| (Paddint
| Psubint
| Pmulint
Expand All @@ -196,34 +240,89 @@ let prim (prim : Prim.t) (ll : t list) : t =
| Pxorint
| Plslint
| Plsrint
| Pasrint | Pintcomp _), _, _
| Pasrint),Const_base (Const_int a), Const_base (Const_int b)
->
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
let int_ v = lift_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)
| Pdivint -> (try int_ (Int32.div aa bb) with _ -> default ())
| 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
| (Paddbint Pint32
| Psubbint Pint32
| Pmulbint Pint32
| Pdivbint Pint32
| Pmodbint Pint32
| Pandbint Pint32
| Porbint Pint32
| Pxorbint Pint32
), Const_base (Const_int32 aa), Const_base (Const_int32 bb)
->
begin match prim with
| Paddbint _ -> lift_int32 (Int32.add aa bb)
| Psubbint _ -> lift_int32 (Int32.sub aa bb)
| Pmulbint _ -> lift_int32 (Int32.mul aa bb)
| Pdivbint _ -> (try lift_int32 (Int32.div aa bb) with _ -> default ())
| Pmodbint _ -> lift_int32 (Int32.rem aa bb)
| Pandbint _ -> lift_int32 (Int32.logand aa bb)
| Porbint _ -> lift_int32 (Int32.logor aa bb)
| Pxorbint _ -> lift_int32 (Int32.logxor aa bb)
| _ -> default ()
end
end
| Plslbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
-> lift_int32 (Int32.shift_left aa b )
| Plsrbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
-> lift_int32 (Int32.shift_right_logical aa b )
| Pasrbint Pint32, Const_base (Const_int32 aa), Const_base (Const_int b)
-> lift_int32 (Int32.shift_right aa b )

| (Paddbint Pint64
| Psubbint Pint64
| Pmulbint Pint64
| Pdivbint Pint64
| Pmodbint Pint64
| Pandbint Pint64
| Porbint Pint64
| Pxorbint Pint64
), Const_base (Const_int64 aa), Const_base (Const_int64 bb)
->
begin match prim with
| Paddbint _ -> lift_int64 (Int64.add aa bb)
| Psubbint _ -> lift_int64 (Int64.sub aa bb)
| Pmulbint _ -> lift_int64 (Int64.mul aa bb)
| Pdivbint _ -> (try lift_int64 (Int64.div aa bb) with _ -> default ())
| Pmodbint _ -> lift_int64 (Int64.rem aa bb)
| Pandbint _ -> lift_int64 (Int64.logand aa bb)
| Porbint _ -> lift_int64 (Int64.logor aa bb)
| Pxorbint _ -> lift_int64 (Int64.logxor aa bb)
| _ -> default ()
end
| Plslbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
-> lift_int64 (Int64.shift_left aa b )
| Plsrbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
-> lift_int64 (Int64.shift_right_logical aa b )
| Pasrbint Pint64, Const_base (Const_int64 aa), Const_base (Const_int b)
-> lift_int64 (Int64.shift_right aa b )
| Psequand, Const_pointer (a, _), Const_pointer( b, _)
->
lift_bool (a = 1 && b = 1)
| Psequor, Const_pointer (a, _), Const_pointer( b, _)
->
lift_bool (a = 1 || b = 1)
| _ -> default ()
end

| _ -> default ()


Expand Down
6 changes: 3 additions & 3 deletions jscomp/test/ext_filename.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ function relative_path(file1, file2) {

var node_modules = "node_modules";

var node_modules_length = 12;

function node_relative_path(path1, path2) {
var v = Ext_string.find(/* None */0, node_modules, path2);
var len = path2.length;
Expand Down Expand Up @@ -159,7 +157,7 @@ function node_relative_path(path1, path2) {
}
};
};
return Ext_string.tail_from(path2, skip(v + node_modules_length | 0));
return Ext_string.tail_from(path2, skip(v + 12 | 0));
}
else {
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)));
Expand Down Expand Up @@ -202,6 +200,8 @@ function resolve(cwd, module_name) {
};
}

var node_modules_length = 12;

exports.node_sep = node_sep;
exports.node_parent = node_parent;
exports.node_current = node_current;
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/float_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ Mt.from_pair_suites("float_test.ml", Pervasives.$at(/* :: */[
"int_of_float",
function () {
return /* Eq */Block.__(0, [
3.2 | 0,
3,
3
]);
}
Expand Down
22 changes: 8 additions & 14 deletions jscomp/test/int64_mul_div_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1777,13 +1777,10 @@ Mt.from_pair_suites("int64_mul_div_test.ml", Pervasives.$at(from_pairs("random",
"div_rem_0",
function () {
return /* Eq */Block.__(0, [
Caml_int64.div(/* int64 */[
/* hi */-1,
/* lo */4294967295
], /* int64 */[
/* hi */0,
/* lo */16
]),
/* int64 */[
/* hi */0,
/* lo */0
],
/* int64 */[
/* hi */0,
/* lo */0
Expand All @@ -1796,13 +1793,10 @@ Mt.from_pair_suites("int64_mul_div_test.ml", Pervasives.$at(from_pairs("random",
"div_rem_1",
function () {
return /* Eq */Block.__(0, [
Caml_int64.mod_(/* int64 */[
/* hi */-1,
/* lo */4294967295
], /* int64 */[
/* hi */0,
/* lo */16
]),
/* int64 */[
/* hi */-1,
/* lo */4294967295
],
/* int64 */[
/* hi */-1,
/* lo */4294967295
Expand Down
Loading