@@ -22,29 +22,43 @@ let%expect_test "direct calls without --effects=cps" =
2222 let code =
2323 compile_and_parse
2424 {|
25+ let l = ref []
26+
2527 (* Arity of the argument of a function / direct call *)
2628 let test1 () =
27- let f g x = try g x with e -> raise e in
29+ let f g x =
30+ l := (fun () -> ()) :: ! l; (* pervent inlining *)
31+ try g x with e -> raise e in
2832 ignore (f (fun x -> x + 1 ) 7 );
2933 ignore (f (fun x -> x *. 2. ) 4. )
3034
3135 (* Arity of the argument of a function / CPS call *)
3236 let test2 () =
33- let f g x = try g x with e -> raise e in
37+ let f g x =
38+ l := (fun () -> ()) :: ! l; (* pervent inlining *)
39+ try g x with e -> raise e in
3440 ignore (f (fun x -> x + 1 ) 7 );
3541 ignore (f (fun x -> x ^ " a" ) " a" )
3642
3743 (* Arity of functions in a functor / direct call *)
3844 let test3 x =
39- let module F(_ : sig end ) = struct let f x = x + 1 end in
45+ let module F(_ : sig end ) = struct
46+ let r = ref 0
47+ let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
48+ let f x = x + 1
49+ end in
4050 let module M1 = F (struct end ) in
4151 let module M2 = F (struct end ) in
4252 (M1. f 1 , M2. f 2 )
4353
4454 (* Arity of functions in a functor / CPS call *)
4555 let test4 x =
4656 let module F(_ : sig end ) =
47- struct let f x = Printf. printf " %d" x end in
57+ struct
58+ let r = ref 0
59+ let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
60+ let f x = Printf. printf " %d" x
61+ end in
4862 let module M1 = F (struct end ) in
4963 let module M2 = F (struct end ) in
5064 M1. f 1 ; M2. f 2
@@ -56,53 +70,111 @@ let%expect_test "direct calls without --effects=cps" =
5670 print_fun_decl code (Some " test4" );
5771 [% expect
5872 {|
59- function test1(param){return 0 ;}
73+ function test1(param){
74+ function f(g, x){
75+ l[1 ] = [0 , function(param){return 0 ;}, l[1 ]];
76+ try {caml_call1(g, x); return;}
77+ catch(e$ 0 ){
78+ var e = caml_wrap_exception(e$ 0 );
79+ throw caml_maybe_attach_backtrace(e, 0 );
80+ }
81+ }
82+ f(function(x){return x + 1 | 0 ;}, 7 );
83+ f(function(x){return x * 2. ;}, 4. );
84+ return 0 ;
85+ }
6086 // end
6187 function test2(param){
62- try {caml_call2(Stdlib [28 ], x, cst_a);}
63- catch(e$ 0 ){
64- var e = caml_wrap_exception(e$ 0 );
65- throw caml_maybe_attach_backtrace(e, 0 );
88+ function f(g, x){
89+ l[1 ] = [0 , function(param){return 0 ;}, l[1 ]];
90+ try {caml_call1(g, x); return;}
91+ catch(e$ 0 ){
92+ var e = caml_wrap_exception(e$ 0 );
93+ throw caml_maybe_attach_backtrace(e, 0 );
94+ }
6695 }
96+ f(function(x){return x + 1 | 0 ;}, 7 );
97+ f(function(x){return caml_call2(Stdlib [28 ], x, cst_a$ 0 );}, cst_a);
6798 return 0 ;
6899 }
69100 // end
70- function test3(x){return [0 , 2 , 3 ];}
101+ function test3(x){
102+ function F (symbol){
103+ var r = [0 , 0 ], for $ 0 = 0 ;
104+ for (;;){
105+ r[1 ]++ ;
106+ var _d_ = for $ 0 + 1 | 0 ;
107+ if (2 === for $ 0 ) break;
108+ for $ 0 = _d_;
109+ }
110+ function f(x){return x + 1 | 0 ;}
111+ return [0 , , f];
112+ }
113+ var M1 = F ([0 ]), M2 = F ([0 ]), _c_ = M2 [2 ].call(null, 2 );
114+ return [0 , M1 [2 ].call(null, 1 ), _c_];
115+ }
71116 // end
72117 function test4(x){
73- caml_call2(Stdlib_Printf [2 ], _a_, 1 );
74- return caml_call2(Stdlib_Printf [2 ], _a_, 2 );
118+ function F (symbol){
119+ var r = [0 , 0 ], for $ 0 = 0 ;
120+ for (;;){
121+ r[1 ]++ ;
122+ var _b_ = for $ 0 + 1 | 0 ;
123+ if (2 === for $ 0 ) break;
124+ for $ 0 = _b_;
125+ }
126+ function f(x){return caml_call2(Stdlib_Printf [2 ], _a_, x);}
127+ return [0 , , f];
128+ }
129+ var M1 = F ([0 ]), M2 = F ([0 ]);
130+ M1 [2 ].call(null, 1 );
131+ return M2 [2 ].call(null, 2 );
75132 }
76- // end | }]
133+ // end
134+ | }]
77135
78136let % expect_test " direct calls with --effects=cps" =
79137 let code =
80138 compile_and_parse
81139 ~effects: `Cps
82140 {|
141+ let l = ref []
142+
83143 (* Arity of the argument of a function / direct call *)
84144 let test1 () =
85- let f g x = try g x with e -> raise e in
145+ let f g x =
146+ l := (fun () -> ()) :: ! l; (* pervent inlining *)
147+ try g x with e -> raise e in
86148 ignore (f (fun x -> x + 1 ) 7 );
87149 ignore (f (fun x -> x *. 2. ) 4. )
88150
89151 (* Arity of the argument of a function / CPS call *)
90152 let test2 () =
91- let f g x = try g x with e -> raise e in
153+ let f g x =
154+ l := (fun () -> ()) :: ! l; (* pervent inlining *)
155+ try g x with e -> raise e in
92156 ignore (f (fun x -> x + 1 ) 7 );
93157 ignore (f (fun x -> x ^ " a" ) " a" )
94158
95159 (* Arity of functions in a functor / direct call *)
96160 let test3 x =
97- let module F(_ : sig end ) = struct let f x = x + 1 end in
161+ let module F(_ : sig end ) = struct
162+ let r = ref 0
163+ let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
164+ let f x = x + 1
165+ end in
98166 let module M1 = F (struct end ) in
99167 let module M2 = F (struct end ) in
100168 (M1. f 1 , M2. f 2 )
101169
102170 (* Arity of functions in a functor / CPS call *)
103171 let test4 x =
104172 let module F(_ : sig end ) =
105- struct let f x = Printf. printf " %d" x end in
173+ struct
174+ let r = ref 0
175+ let () = for _ = 0 to 2 do incr r done (* pervent inlining *)
176+ let f x = Printf. printf " %d" x
177+ end in
106178 let module M1 = F (struct end ) in
107179 let module M2 = F (struct end ) in
108180 M1. f 1 ; M2. f 2
@@ -114,30 +186,83 @@ let%expect_test "direct calls with --effects=cps" =
114186 print_fun_decl code (Some " test4" );
115187 [% expect
116188 {|
117- function test1(param, cont){return cont(0 );}
189+ function test1(param, cont){
190+ function f(g, x){
191+ l[1 ] = [0 , function(param, cont){return cont(0 );}, l[1 ]];
192+ try {g() ; return;}
193+ catch(e$ 0 ){
194+ var e = caml_wrap_exception(e$ 0 );
195+ throw caml_maybe_attach_backtrace(e, 0 );
196+ }
197+ }
198+ f(function(x){});
199+ f(function(x){});
200+ return cont(0 );
201+ }
118202 // end
119203 function test2(param, cont){
120- runtime.caml_push_trap
121- (function(e){
122- var raise = caml_pop_trap() , e$ 0 = caml_maybe_attach_backtrace(e, 0 );
123- return raise(e$ 0 );
124- });
125- return caml_trampoline_cps_call3
126- (Stdlib [28 ],
127- x,
128- cst_a,
129- function(_g_){caml_pop_trap() ; return cont(0 );});
204+ function f(g, x, cont){
205+ l[1 ] = [0 , function(param, cont){return cont(0 );}, l[1 ]];
206+ runtime.caml_push_trap
207+ (function(e){
208+ var raise = caml_pop_trap() , e$ 0 = caml_maybe_attach_backtrace(e, 0 );
209+ return raise(e$ 0 );
210+ });
211+ return caml_exact_trampoline_cps_call
212+ (g, x, function(_x_){caml_pop_trap() ; return cont() ;});
213+ }
214+ return caml_exact_trampoline_cps_call$ 0
215+ (f,
216+ function(x, cont){return cont() ;},
217+ 7 ,
218+ function(_v_){
219+ return caml_exact_trampoline_cps_call$ 0
220+ (f,
221+ function(x, cont){
222+ return caml_trampoline_cps_call3
223+ (Stdlib [28 ], x, cst_a$ 0 , cont);
224+ },
225+ cst_a,
226+ function(_w_){return cont(0 );});
227+ });
130228 }
131229 // end
132- function test3(x, cont){return cont([0 , 2 , 3 ]);}
230+ function test3(x, cont){
231+ function F (symbol){
232+ var r = [0 , 0 ], for $ 0 = 0 ;
233+ for (;;){
234+ r[1 ]++ ;
235+ var _u_ = for $ 0 + 1 | 0 ;
236+ if (2 === for $ 0 ) break;
237+ for $ 0 = _u_;
238+ }
239+ function f(x){return x + 1 | 0 ;}
240+ return [0 , , f];
241+ }
242+ var M1 = F () , M2 = F () , _t_ = M2 [2 ].call(null, 2 );
243+ return cont([0 , M1 [2 ].call(null, 1 ), _t_]);
244+ }
133245 // end
134246 function test4(x, cont){
135- return caml_trampoline_cps_call3
136- (Stdlib_Printf [2 ],
137- _e_,
247+ function F (symbol){
248+ var r = [0 , 0 ], for $ 0 = 0 ;
249+ for (;;){
250+ r[1 ]++ ;
251+ var _s_ = for $ 0 + 1 | 0 ;
252+ if (2 === for $ 0 ) break;
253+ for $ 0 = _s_;
254+ }
255+ function f(x, cont){
256+ return caml_trampoline_cps_call3(Stdlib_Printf [2 ], _q_, x, cont);
257+ }
258+ return [0 , , f];
259+ }
260+ var M1 = F () , M2 = F () ;
261+ return caml_exact_trampoline_cps_call
262+ (M1 [2 ],
138263 1 ,
139- function(_f_ ){
140- return caml_trampoline_cps_call3( Stdlib_Printf [2 ], _e_ , 2 , cont);
264+ function(_r_ ){
265+ return caml_exact_trampoline_cps_call( M2 [2 ], 2 , cont);
141266 });
142267 }
143268 // end
0 commit comments