Skip to content

Commit 52eb3a5

Browse files
committed
Merge pull request #73 from bloomberg/if_then_else_fix
fix && || tail call, it generates some large expressions (Fix #50)
2 parents 0fd6f9b + 061695d commit 52eb3a5

26 files changed

+1303
-470
lines changed

jscomp/js_output.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ let of_block ?value ?(finished = False) block =
4848

4949
let dummy = {value = None; block = []; finished = Dummy }
5050

51-
let handle_name_tail (name : st) (should_return : Lam_compile_defs.return_type)
51+
let handle_name_tail
52+
(name : st)
53+
(should_return : Lam_compile_defs.return_type)
5254
lam (exp : J.expression) : t =
5355
begin match name, should_return with
5456
| EffectCall, False ->
@@ -66,10 +68,13 @@ let handle_name_tail (name : st) (should_return : Lam_compile_defs.return_type)
6668
| NeedValue, _ -> {block = []; value = Some exp; finished = False }
6769
end
6870

69-
let handle_block_return (st : st) (should_return : Lam_compile_defs.return_type) (lam : Lambda.lambda) (block : J.block) exp : t =
71+
let handle_block_return
72+
(st : st)
73+
(should_return : Lam_compile_defs.return_type)
74+
(lam : Lambda.lambda) (block : J.block) exp : t =
7075
match st, should_return with
7176
| Declare (kind,n), False ->
72-
make (block @ [ S.define ~kind n exp])
77+
make (block @ [ S.define ~kind n exp])
7378
| Assign n, False -> make (block @ [S.assign n exp])
7479
| (Declare _ | Assign _), True _ -> make [S.unknown_lambda lam] ~finished:True
7580
| EffectCall, False -> make block ~value:exp

jscomp/lam_analysis.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,4 @@ let is_closed_by set lam =
454454

455455
let is_closed lam =
456456
Ident_map.is_empty (free_variables Ident_set.empty Ident_map.empty lam)
457+

jscomp/lam_analysis.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ val free_variables : Ident_set.t -> stats Ident_map.t -> Lambda.lambda -> stats
6161

6262
val small_inline_size : int
6363
val exit_inline_size : int
64+
65+

jscomp/lam_compile.ml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,53 @@ and
453453
*)
454454
| {value = None; _} -> assert false
455455
end
456+
| Lprim(Psequand , [l;r] )
457+
->
458+
begin match cxt with
459+
| {should_return = True _ }
460+
(* Invariant: if [should_return], then [st] will not be [NeedValue] *)
461+
->
462+
compile_lambda cxt (Lifthenelse (l, r, Lam_util.lam_false))
463+
| _ ->
464+
let l_block,l_expr =
465+
match compile_lambda {cxt with st = NeedValue; should_return = False} l with
466+
| {block = a; value = Some b} -> a, b
467+
| _ -> assert false
468+
in
469+
let r_block, r_expr =
470+
match compile_lambda {cxt with st = NeedValue; should_return = False} r with
471+
| {block = a; value = Some b} -> a, b
472+
| _ -> assert false
473+
in
474+
let args_code = l_block @ r_block in
475+
let exp = E.and_ l_expr r_expr in
476+
Js_output.handle_block_return st should_return lam args_code exp
477+
end
478+
479+
| Lprim(Psequor, [l;r])
480+
->
481+
begin match cxt with
482+
| {should_return = True _ }
483+
(* Invariant: if [should_return], then [st] will not be [NeedValue] *)
484+
->
485+
compile_lambda cxt (Lifthenelse (l, Lam_util.lam_true, r))
486+
| _ ->
487+
let l_block,l_expr =
488+
match compile_lambda {cxt with st = NeedValue; should_return = False} l with
489+
| {block = a; value = Some b} -> a, b
490+
| _ -> assert false
491+
in
492+
let r_block, r_expr =
493+
match compile_lambda {cxt with st = NeedValue; should_return = False} r with
494+
| {block = a; value = Some b} -> a, b
495+
| _ -> assert false
496+
in
497+
let args_code = l_block @ r_block in
498+
let exp = E.or_ l_expr r_expr in
499+
Js_output.handle_block_return st should_return lam args_code exp
500+
end
501+
502+
456503
| Lprim (prim, args_lambda) ->
457504
begin
458505
let args_block, args_expr =

jscomp/lam_util.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,8 @@ let print_ident_set fmt s =
311311

312312
let mk_apply_info ?(loc = Location.none) apply_status : Lambda.apply_info =
313313
{ apply_loc = loc; apply_status }
314+
315+
316+
let lam_true : Lambda.lambda = Lconst (Const_pointer ( 1, NullConstructor "true"))
317+
318+
let lam_false : Lambda.lambda = Lconst (Const_pointer( 0, NullConstructor "false"))

jscomp/lam_util.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ val ident_set_of_list : Ident.t list -> Ident_set.t
5858
val print_ident_set : Format.formatter -> Ident_set.t -> unit
5959

6060
val mk_apply_info : ?loc:Location.t -> Lambda.apply_status -> Lambda.apply_info
61+
62+
val lam_true : Lambda.lambda
63+
val lam_false : Lambda.lambda

jscomp/runtime/caml_string.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ function caml_string_of_char_array(chars) {
149149

150150
function caml_is_printable(c) {
151151
var code = c;
152-
return +(code > 31 && code < 127);
152+
if (code > 31) {
153+
return +(code < 127);
154+
}
155+
else {
156+
return /* false */0;
157+
}
153158
}
154159

155160
exports.add = add;

jscomp/stdlib/camlinternalFormat.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,12 @@ function bprint_char_set(buf, char_set) {
339339
var is_alone = function (c) {
340340
var match_001 = Char.chr(c - 1);
341341
var match_002 = Char.chr(c + 1);
342-
return +(is_in_char_set(set, c) && !(is_in_char_set(set, match_001) && is_in_char_set(set, match_002)));
342+
if (is_in_char_set(set, c)) {
343+
return !(is_in_char_set(set, match_001) && is_in_char_set(set, match_002));
344+
}
345+
else {
346+
return /* false */0;
347+
}
343348
};
344349
if (is_alone(/* "]" */93)) {
345350
buffer_add_char(buf, /* "]" */93);

jscomp/stdlib/filename.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,35 @@ function is_dir_sep(s, i) {
9898
}
9999

100100
function is_relative(n) {
101-
return +(n.length < 1 || n.charCodeAt(0) !== /* "/" */47);
101+
if (n.length < 1) {
102+
return /* true */1;
103+
}
104+
else {
105+
return +(n.charCodeAt(0) !== /* "/" */47);
106+
}
102107
}
103108

104109
function is_implicit(n) {
105-
return +(is_relative(n) && (n.length < 2 || $$String.sub(n, 0, 2) !== "./") && (n.length < 3 || $$String.sub(n, 0, 3) !== "../"));
110+
if (is_relative(n) && (n.length < 2 || $$String.sub(n, 0, 2) !== "./")) {
111+
if (n.length < 3) {
112+
return /* true */1;
113+
}
114+
else {
115+
return +($$String.sub(n, 0, 3) !== "../");
116+
}
117+
}
118+
else {
119+
return /* false */0;
120+
}
106121
}
107122

108123
function check_suffix(name, suff) {
109-
return +(name.length >= suff.length && $$String.sub(name, name.length - suff.length, suff.length) === suff);
124+
if (name.length >= suff.length) {
125+
return +($$String.sub(name, name.length - suff.length, suff.length) === suff);
126+
}
127+
else {
128+
return /* false */0;
129+
}
110130
}
111131

112132
var temp_dir_name;

jscomp/stdlib/hashtbl.js

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -361,15 +361,21 @@ function replace(h, key, info) {
361361
}
362362

363363
function mem(h, key) {
364-
var mem_in_bucket = function (param) {
364+
var _param = h[2][key_index(h, key)];
365+
while(true) {
366+
var param = _param;
365367
if (param) {
366-
return +(Caml_primitive.caml_compare(param[1], key) === 0 || mem_in_bucket(param[3]));
368+
if (Caml_primitive.caml_compare(param[1], key)) {
369+
_param = param[3];
370+
}
371+
else {
372+
return /* true */1;
373+
}
367374
}
368375
else {
369376
return /* false */0;
370377
}
371378
};
372-
return mem_in_bucket(h[2][key_index(h, key)]);
373379
}
374380

375381
function iter(f, h) {
@@ -620,15 +626,21 @@ function MakeSeeded(H) {
620626
}
621627
};
622628
var mem = function (h, key) {
623-
var mem_in_bucket = function (param) {
629+
var _param = h[2][key_index(h, key)];
630+
while(true) {
631+
var param = _param;
624632
if (param) {
625-
return +(H[1](param[1], key) || mem_in_bucket(param[3]));
633+
if (H[1](param[1], key)) {
634+
return /* true */1;
635+
}
636+
else {
637+
_param = param[3];
638+
}
626639
}
627640
else {
628641
return /* false */0;
629642
}
630643
};
631-
return mem_in_bucket(h[2][key_index(h, key)]);
632644
};
633645
return [
634646
0,
@@ -823,15 +835,21 @@ function Make(H) {
823835
}
824836
};
825837
var mem = function (h, key) {
826-
var mem_in_bucket = function (param) {
838+
var _param = h[2][key_index(h, key)];
839+
while(true) {
840+
var param = _param;
827841
if (param) {
828-
return +(equal(param[1], key) || mem_in_bucket(param[3]));
842+
if (equal(param[1], key)) {
843+
return /* true */1;
844+
}
845+
else {
846+
_param = param[3];
847+
}
829848
}
830849
else {
831850
return /* false */0;
832851
}
833852
};
834-
return mem_in_bucket(h[2][key_index(h, key)]);
835853
};
836854
var create$1 = function (sz) {
837855
return create([

0 commit comments

Comments
 (0)