|
| 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 | + |}] |
0 commit comments