Skip to content

Commit 3cd6c95

Browse files
committed
Reinstate the lambda lifting after the CPS transform
This is to avoid too many nested function definitions, which can cause stack overflows in JS parsers. There are now two lambda lifting passes in total: one before and one after CPS-transforming the code. The pre-CPS-transform lifting is done to avoid nesting functions that are doubly-translated, which would be both costly in code size and incorrect in terms of closure environment capture.
1 parent c1f7911 commit 3cd6c95

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

compiler/lib/driver.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,13 @@ let phi p =
8787

8888
let ( +> ) f g x = g (f x)
8989

90+
let map_fst f (x, y, z) = f x, y, z
91+
9092
let effects p =
9193
if Config.Flag.effects ()
9294
then (
9395
if debug () then Format.eprintf "Effects...@.";
94-
p |> Deadcode.f +> Effects.f)
96+
p |> Deadcode.f +> Effects.f +> map_fst Lambda_lifting.f)
9597
else
9698
( p
9799
, (Code.Var.Set.empty : Effects.cps_calls)

compiler/tests-compiler/lambda_lifting.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ Printf.printf "%d\n" (f 3)
3434
Stdlib_Printf = global_data.Stdlib__Printf,
3535
_e_ =
3636
[0, [4, 0, 0, 0, [12, 10, 0]], runtime.caml_string_of_jsbytes("%d\n")];
37-
function f(x){
38-
function g(y){function h(z){return (x + y | 0) + z | 0;} return h(7);}
39-
return g(5);
40-
}
37+
function f(x){var g$0 = g(x); return g$0(5);}
38+
function h(x, y){function h(z){return (x + y | 0) + z | 0;} return h;}
39+
function g(x){function g(y){var h$0 = h(x, y); return h$0(7);} return g;}
4140
var _d_ = f(3);
4241
caml_call2(Stdlib_Printf[2], _e_, _d_);
4342
var Test = [0];

0 commit comments

Comments
 (0)