Skip to content

Commit 61e2268

Browse files
committed
Add test for --enable lambda-lift-all
1 parent a095b30 commit 61e2268

File tree

4 files changed

+261
-2
lines changed

4 files changed

+261
-2
lines changed
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
(* Js_of_ocaml compiler
2+
* http://www.ocsigen.org/js_of_ocaml/
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, with linking exception;
7+
* either version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+
*)
18+
19+
open Util
20+
21+
let%expect_test "direct calls with --effects=none --disable lambda-lift-all" =
22+
let code =
23+
compile_and_parse
24+
~lambda_lift_all:true
25+
{|
26+
let l = ref []
27+
28+
(* Arity of the argument of a function / direct call *)
29+
let test1 () =
30+
let f g x =
31+
l := (fun () -> ()) :: !l; (* pervent inlining *)
32+
try g x with e -> raise e in
33+
ignore (f (fun x -> x + 1) 7);
34+
ignore (f (fun x -> x *. 2.) 4.)
35+
36+
(* Arity of the argument of a function / CPS call *)
37+
let test2 () =
38+
let f g x =
39+
l := (fun () -> ()) :: !l; (* pervent inlining *)
40+
try g x with e -> raise e in
41+
ignore (f (fun x -> x + 1) 7);
42+
ignore (f (fun x -> x ^ "a") "a")
43+
44+
(* Arity of functions in a functor / direct call *)
45+
let test3 x =
46+
let module F(_ : sig end) = struct
47+
let r = ref 0
48+
let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
49+
let f x = x + 1
50+
end in
51+
let module M1 = F (struct end) in
52+
let module M2 = F (struct end) in
53+
(M1.f 1, M2.f 2)
54+
55+
(* Arity of functions in a functor / CPS call *)
56+
let test4 x =
57+
let module F(_ : sig end) =
58+
struct
59+
let r = ref 0
60+
let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
61+
let f x = Printf.printf "%d" x
62+
end in
63+
let module M1 = F (struct end) in
64+
let module M2 = F (struct end) in
65+
M1.f 1; M2.f 2
66+
|}
67+
in
68+
print_fun_decl code (Some "test1");
69+
print_fun_decl code (Some "test2");
70+
print_fun_decl code (Some "test3");
71+
print_fun_decl code (Some "test4");
72+
[%expect
73+
{|
74+
function test1(param){var f = f$2(); f(_f_(), 7); f(_g_(), 4.); return 0;}
75+
//end
76+
function test2(param){var f = f$1(); f(_c_(), 7); f(_d_(), cst_a); return 0;}
77+
//end
78+
function test3(x){
79+
var F = F$0(), M1 = F([0]), M2 = F([0]), _g_ = M2[2].call(null, 2);
80+
return [0, M1[2].call(null, 1), _g_];
81+
}
82+
//end
83+
function test4(x){
84+
var F$0 = F(), M1 = F$0([0]), M2 = F$0([0]);
85+
M1[2].call(null, 1);
86+
return M2[2].call(null, 2);
87+
}
88+
//end
89+
|}]
90+
91+
let%expect_test "direct calls with --effects=cps" =
92+
let code =
93+
compile_and_parse
94+
~lambda_lift_all:true
95+
~effects:`Cps
96+
{|
97+
let l = ref []
98+
99+
(* Arity of the argument of a function / direct call *)
100+
let test1 () =
101+
let f g x =
102+
l := (fun () -> ()) :: !l; (* pervent inlining *)
103+
try g x with e -> raise e in
104+
ignore (f (fun x -> x + 1) 7);
105+
ignore (f (fun x -> x *. 2.) 4.)
106+
107+
(* Arity of the argument of a function / CPS call *)
108+
let test2 () =
109+
let f g x =
110+
l := (fun () -> ()) :: !l; (* pervent inlining *)
111+
try g x with e -> raise e in
112+
ignore (f (fun x -> x + 1) 7);
113+
ignore (f (fun x -> x ^ "a") "a")
114+
115+
(* Arity of functions in a functor / direct call *)
116+
let test3 x =
117+
let module F(_ : sig end) = struct
118+
let r = ref 0
119+
let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
120+
let f x = x + 1
121+
end in
122+
let module M1 = F (struct end) in
123+
let module M2 = F (struct end) in
124+
(M1.f 1, M2.f 2)
125+
126+
(* Arity of functions in a functor / CPS call *)
127+
let test4 x =
128+
let module F(_ : sig end) =
129+
struct
130+
let r = ref 0
131+
let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
132+
let f x = Printf.printf "%d" x
133+
end in
134+
let module M1 = F (struct end) in
135+
let module M2 = F (struct end) in
136+
M1.f 1; M2.f 2
137+
|}
138+
in
139+
print_fun_decl code (Some "test1");
140+
print_fun_decl code (Some "test2");
141+
print_fun_decl code (Some "test3");
142+
print_fun_decl code (Some "test4");
143+
[%expect
144+
{|
145+
function test1(param, cont){
146+
function f(g, x){
147+
l[1] = [0, function(param, cont){return cont(0);}, l[1]];
148+
try{g(); return;}
149+
catch(e$0){
150+
var e = caml_wrap_exception(e$0);
151+
throw caml_maybe_attach_backtrace(e, 0);
152+
}
153+
}
154+
f(function(x){});
155+
f(function(x){});
156+
return cont(0);
157+
}
158+
//end
159+
function test2(param, cont){
160+
function f(g, x, cont){
161+
l[1] = [0, function(param, cont){return cont(0);}, l[1]];
162+
runtime.caml_push_trap
163+
(function(e){
164+
var raise = caml_pop_trap(), e$0 = caml_maybe_attach_backtrace(e, 0);
165+
return raise(e$0);
166+
});
167+
return caml_exact_trampoline_cps_call
168+
(g, x, function(_b_){caml_pop_trap(); return cont();});
169+
}
170+
return caml_exact_trampoline_cps_call$0
171+
(f,
172+
function(x, cont){return cont();},
173+
7,
174+
function(_b_){
175+
return caml_exact_trampoline_cps_call$0
176+
(f,
177+
function(x, cont){
178+
return caml_trampoline_cps_call3
179+
(Stdlib[28], x, cst_a$0, cont);
180+
},
181+
cst_a,
182+
function(_b_){return cont(0);});
183+
});
184+
}
185+
//end
186+
function test3(x, cont){
187+
function F(symbol){
188+
var r = [0, 0], for$ = 0;
189+
for(;;){r[1]++; var _b_ = for$ + 1 | 0; if(2 === for$) break; for$ = _b_;}
190+
function f(x){return x + 1 | 0;}
191+
return [0, , f];
192+
}
193+
var M1 = F(), M2 = F(), _b_ = M2[2].call(null, 2);
194+
return cont([0, M1[2].call(null, 1), _b_]);
195+
}
196+
//end
197+
function test4(x, cont){
198+
function F(symbol){
199+
var r = [0, 0], for$ = 0;
200+
for(;;){r[1]++; var _b_ = for$ + 1 | 0; if(2 === for$) break; for$ = _b_;}
201+
function f(x, cont){
202+
return caml_trampoline_cps_call3(Stdlib_Printf[2], _a_, x, cont);
203+
}
204+
return [0, , f];
205+
}
206+
var M1 = F(), M2 = F();
207+
return caml_exact_trampoline_cps_call
208+
(M1[2],
209+
1,
210+
function(_a_){
211+
return caml_exact_trampoline_cps_call(M2[2], 2, cont);
212+
});
213+
}
214+
//end
215+
|}]

compiler/tests-compiler/dune.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,21 @@
8989
(preprocess
9090
(pps ppx_expect)))
9191

92+
(library
93+
;; compiler/tests-compiler/direct_calls_lift_all.ml
94+
(name direct_calls_lift_all_15)
95+
(enabled_if true)
96+
(modules direct_calls_lift_all)
97+
(libraries js_of_ocaml_compiler unix str jsoo_compiler_expect_tests_helper)
98+
(inline_tests
99+
(enabled_if true)
100+
(deps
101+
(file %{project_root}/compiler/bin-js_of_ocaml/js_of_ocaml.exe)
102+
(file %{project_root}/compiler/bin-jsoo_minify/jsoo_minify.exe)))
103+
(flags (:standard -open Jsoo_compiler_expect_tests_helper))
104+
(preprocess
105+
(pps ppx_expect)))
106+
92107
(library
93108
;; compiler/tests-compiler/effects.ml
94109
(name effects_15)

compiler/tests-compiler/util/util.ml

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ let extract_sourcemap file =
266266
let compile_to_javascript
267267
?(flags = [])
268268
?(use_js_string = false)
269+
?(lambda_lift_all = false)
269270
?(effects = `Disabled)
270271
?(werror = true)
271272
~pretty
@@ -283,6 +284,9 @@ let compile_to_javascript
283284
; (if use_js_string
284285
then [ "--enable=use-js-string" ]
285286
else [ "--disable=use-js-string" ])
287+
; (if lambda_lift_all
288+
then [ "--enable=lambda-lift-all" ]
289+
else [ "--disable=lambda-lift-all" ])
286290
; flags
287291
; (if werror then [ "--Werror" ] else [])
288292
]
@@ -324,17 +328,26 @@ let compile_bc_to_javascript
324328
?flags
325329
?effects
326330
?use_js_string
331+
?lambda_lift_all
327332
?(pretty = true)
328333
?(sourcemap = true)
329334
?werror
330335
file =
331336
Filetype.path_of_bc_file file
332-
|> compile_to_javascript ?flags ?effects ?use_js_string ?werror ~pretty ~sourcemap
337+
|> compile_to_javascript
338+
?flags
339+
?effects
340+
?use_js_string
341+
?lambda_lift_all
342+
?werror
343+
~pretty
344+
~sourcemap
333345

334346
let compile_cmo_to_javascript
335347
?(flags = [])
336348
?effects
337349
?use_js_string
350+
?lambda_lift_all
338351
?(pretty = true)
339352
?(sourcemap = true)
340353
?werror
@@ -343,6 +356,7 @@ let compile_cmo_to_javascript
343356
|> compile_to_javascript
344357
?effects
345358
?use_js_string
359+
?lambda_lift_all
346360
?werror
347361
~flags:([ "--disable"; "header" ] @ flags)
348362
~pretty
@@ -578,6 +592,7 @@ let compile_and_parse_whole_program
578592
?flags
579593
?effects
580594
?use_js_string
595+
?lambda_lift_all
581596
?unix
582597
?werror
583598
s =
@@ -591,11 +606,20 @@ let compile_and_parse_whole_program
591606
?flags
592607
?effects
593608
?use_js_string
609+
?lambda_lift_all
594610
?werror
595611
~sourcemap:debug
596612
|> parse_js)
597613

598-
let compile_and_parse ?(debug = true) ?pretty ?flags ?effects ?use_js_string ?werror s =
614+
let compile_and_parse
615+
?(debug = true)
616+
?pretty
617+
?flags
618+
?effects
619+
?use_js_string
620+
?lambda_lift_all
621+
?werror
622+
s =
599623
with_temp_dir ~f:(fun () ->
600624
s
601625
|> Filetype.ocaml_text_of_string
@@ -606,6 +630,7 @@ let compile_and_parse ?(debug = true) ?pretty ?flags ?effects ?use_js_string ?we
606630
?flags
607631
?effects
608632
?use_js_string
633+
?lambda_lift_all
609634
?werror
610635
~sourcemap:debug
611636
|> parse_js)

compiler/tests-compiler/util/util.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ val compile_cmo_to_javascript :
3636
?flags:string list
3737
-> ?effects:[ `Disabled | `Cps | `Double_translation ]
3838
-> ?use_js_string:bool
39+
-> ?lambda_lift_all:bool
3940
-> ?pretty:bool
4041
-> ?sourcemap:bool
4142
-> ?werror:bool
@@ -46,6 +47,7 @@ val compile_bc_to_javascript :
4647
?flags:string list
4748
-> ?effects:[ `Disabled | `Cps | `Double_translation ]
4849
-> ?use_js_string:bool
50+
-> ?lambda_lift_all:bool
4951
-> ?pretty:bool
5052
-> ?sourcemap:bool
5153
-> ?werror:bool
@@ -99,6 +101,7 @@ val compile_and_parse :
99101
-> ?flags:string list
100102
-> ?effects:[ `Disabled | `Cps | `Double_translation ]
101103
-> ?use_js_string:bool
104+
-> ?lambda_lift_all:bool
102105
-> ?werror:bool
103106
-> string
104107
-> Javascript.program
@@ -109,6 +112,7 @@ val compile_and_parse_whole_program :
109112
-> ?flags:string list
110113
-> ?effects:[ `Disabled | `Cps | `Double_translation ]
111114
-> ?use_js_string:bool
115+
-> ?lambda_lift_all:bool
112116
-> ?unix:bool
113117
-> ?werror:bool
114118
-> string

0 commit comments

Comments
 (0)