Skip to content

Commit

Permalink
Reinstate the lambda lifting after the CPS transform
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
OlivierNicole committed May 22, 2023
1 parent c1f7911 commit 3cd6c95
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 3 additions & 1 deletion compiler/lib/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ let phi p =

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

let map_fst f (x, y, z) = f x, y, z

let effects p =
if Config.Flag.effects ()
then (
if debug () then Format.eprintf "Effects...@.";
p |> Deadcode.f +> Effects.f)
p |> Deadcode.f +> Effects.f +> map_fst Lambda_lifting.f)
else
( p
, (Code.Var.Set.empty : Effects.cps_calls)
Expand Down
7 changes: 3 additions & 4 deletions compiler/tests-compiler/lambda_lifting.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ Printf.printf "%d\n" (f 3)
Stdlib_Printf = global_data.Stdlib__Printf,
_e_ =
[0, [4, 0, 0, 0, [12, 10, 0]], runtime.caml_string_of_jsbytes("%d\n")];
function f(x){
function g(y){function h(z){return (x + y | 0) + z | 0;} return h(7);}
return g(5);
}
function f(x){var g$0 = g(x); return g$0(5);}
function h(x, y){function h(z){return (x + y | 0) + z | 0;} return h;}
function g(x){function g(y){var h$0 = h(x, y); return h$0(7);} return g;}
var _d_ = f(3);
caml_call2(Stdlib_Printf[2], _e_, _d_);
var Test = [0];
Expand Down

0 comments on commit 3cd6c95

Please sign in to comment.