Skip to content

Commit 6c013d9

Browse files
vouillonhhugo
authored andcommitted
Effects: introduce primitive caml_cps_trampoline
This is a better name for the primitive that sets up a trampoline to switch to CPS.
1 parent 7303d6c commit 6c013d9

File tree

5 files changed

+14
-17
lines changed

5 files changed

+14
-17
lines changed

compiler/lib-wasm/generate.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,11 +1196,7 @@ module Generate (Target : Target_sig.S) = struct
11961196
end
11971197

11981198
let init () =
1199-
let l =
1200-
[ "caml_callback", "caml_trampoline"
1201-
; "caml_make_array", "caml_array_of_uniform_array"
1202-
]
1203-
in
1199+
let l = [ "caml_make_array", "caml_array_of_uniform_array" ] in
12041200
Primitive.register "caml_array_of_uniform_array" `Mutable None None;
12051201
List.iter ~f:(fun (nm, nm') -> Primitive.alias nm nm') l
12061202

compiler/lib/effects.ml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
847847
(* Toplevel code: if we double-translate, no need to handle it
848848
specially: CPS calls in it are like all other CPS calls from
849849
direct code. Otherwise, it needs to wrapped within a
850-
[caml_callback], but only if it performs CPS calls. *)
850+
[caml_cps_trampoline], but only if it performs CPS calls. *)
851851
not (double_translate () || Addr.Set.is_empty blocks_to_transform)
852852
in
853853
if debug ()
@@ -957,7 +957,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
957957
match Hashtbl.find_opt closure_info p.start with
958958
| None -> p
959959
| Some (cps_params, cps_cont) ->
960-
(* Call [caml_callback] to set up the execution context. *)
960+
(* Call [caml_cps_trampoline] to set up the execution context. *)
961961
let new_start = p.free_pc in
962962
let blocks =
963963
let main = Var.fresh () in
@@ -969,7 +969,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
969969
; body =
970970
[ Let (main, Closure (cps_params, cps_cont))
971971
; Let (args, Prim (Extern "%js_array", []))
972-
; Let (res, Prim (Extern "caml_callback", [ Pv main; Pv args ]))
972+
; Let (res, Prim (Extern "caml_cps_trampoline", [ Pv main; Pv args ]))
973973
]
974974
; branch = Return res
975975
}
@@ -994,7 +994,7 @@ let wrap_call ~cps_needed p x f args accu =
994994
( p
995995
, Var.Set.remove x cps_needed
996996
, [ Let (arg_array, Prim (Extern "%js_array", List.map ~f:(fun y -> Pv y) args))
997-
; Let (x, Prim (Extern "caml_callback", [ Pv f; Pv arg_array ]))
997+
; Let (x, Prim (Extern "caml_cps_trampoline", [ Pv f; Pv arg_array ]))
998998
]
999999
:: accu )
10001000

@@ -1014,7 +1014,7 @@ let wrap_primitive ~cps_needed (p : program) x e accu =
10141014
, let args = Var.fresh () in
10151015
[ Let (f, Closure ([], (closure_pc, [])))
10161016
; Let (args, Prim (Extern "%js_array", []))
1017-
; Let (x, Prim (Extern "caml_callback", [ Pv f; Pv args ]))
1017+
; Let (x, Prim (Extern "caml_cps_trampoline", [ Pv f; Pv args ]))
10181018
]
10191019
:: accu )
10201020

@@ -1026,9 +1026,9 @@ let rewrite_toplevel_instr (p, cps_needed, accu) instr =
10261026
wrap_primitive ~cps_needed p x e accu
10271027
| _ -> p, cps_needed, [ instr ] :: accu
10281028

1029-
(* Wrap function calls inside [caml_callback] at toplevel to avoid
1029+
(* Wrap function calls inside [caml_cps_trampoline] at toplevel to avoid
10301030
unncessary function nestings. This is not done inside loops since
1031-
using repeatedly [caml_callback] can be costly. *)
1031+
using repeatedly [caml_cps_trampoline] can be costly. *)
10321032
let rewrite_toplevel ~cps_needed p =
10331033
let { start; blocks; _ } = p in
10341034
let cfg = build_graph blocks start in

runtime/js/jslib.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ var caml_callback = caml_call_gen;
8686
//If: !doubletranslate
8787
//Requires: caml_stack_depth, caml_call_gen, caml_wrap_exception
8888
//Requires: caml_current_stack
89+
//Alias: caml_cps_trampoline
8990
function caml_callback(f, args) {
9091
var saved_stack_depth = caml_stack_depth;
9192
var saved_current_stack = caml_current_stack;

runtime/wasm/effect.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@
565565
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
566566
(unreachable))
567567

568-
(func (export "caml_trampoline")
568+
(func $caml_trampoline (export "caml_cps_trampoline")
569569
(param $f (ref eq)) (param $vargs (ref eq)) (result (ref eq))
570570
(local $args (ref $block))
571571
(local $i i32) (local $res (ref eq))

runtime/wasm/obj.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
(func $caml_is_continuation (param (ref eq)) (result i32)))
2626
(@if (= effects "cps")
2727
(@then
28-
(import "effect" "caml_trampoline"
29-
(func $caml_trampoline (param (ref eq) (ref eq)) (result (ref eq))))
28+
(import "effect" "caml_cps_trampoline"
29+
(func $caml_cps_trampoline (param (ref eq) (ref eq)) (result (ref eq))))
3030
))
3131

3232

@@ -460,14 +460,14 @@
460460
(@then
461461
(func $caml_callback_1 (export "caml_callback_1")
462462
(param $f (ref eq)) (param $x (ref eq)) (result (ref eq))
463-
(return_call $caml_trampoline
463+
(return_call $caml_cps_trampoline
464464
(local.get $f)
465465
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $x))))
466466

467467
(func (export "caml_callback_2")
468468
(param $f (ref eq)) (param $x (ref eq)) (param $y (ref eq))
469469
(result (ref eq))
470-
(return_call $caml_trampoline
470+
(return_call $caml_cps_trampoline
471471
(local.get $f)
472472
(array.new_fixed $block 3 (ref.i31 (i32.const 0))
473473
(local.get $x) (local.get $y))))

0 commit comments

Comments
 (0)