/
bslCps.ml
170 lines (129 loc) · 8.8 KB
/
bslCps.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(*
Copyright © 2011 MLstate
This file is part of OPA.
OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.
OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.
You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(**
CPS runtime server bsl-binding.
Bypass used by the generated code after the QmlCpsRewriter pass
some of them should not be projected (in particular functionnal bypass)
that is the meaning of the bsltag [no-projection]
This is a binding to QmlCpsServerLib.
this module is not inlined there in the hope to keep the opportunity to test it
ouside the bsl. This can anytime later be changed by removing the QmlCpsServerLib module
by bringing the code there.
*)
(**
Runtime debug output. Based on {b MLSTATE_CPS_DEBUG}.
@see 'debugVariables.ml' for details
*)
##register [opacapi] debug : int, string -> void
let debug __minlevel __s =
#<If:CPS_DEBUG$minlevel __minlevel>
Printf.fprintf stderr "[34m[Cps][0m %s\n%!" __s
#<End>
##extern-type Cps.future('a) = 'a QmlCpsServerLib.future
##extern-type [normalize] func('a, 'b) = ('a, 'b) QmlCpsServerLib.func
##extern-type [normalize] black_future = QmlCpsServerLib.black_future
##extern-type [normalize] _unit_continuation = unit QmlCpsServerLib.continuation
##extern-type [normalize] _unit = unit (* the type unit that doesn't get projected *)
let qml_unit = ServerLib.make_record ServerLib.empty_record_constructor
##register [opacapi, no-projection, restricted : cps] wait \ `QmlCpsServerLib.wait` : Cps.future('a), continuation('a) -> void
##register [opacapi, no-projection : cps, restricted : cps] spawn \ `QmlCpsServerLib.spawn` : (_unit, continuation('a) -> _unit) -> Cps.future('a)
(* Fixme: Check with Valentin. extern _unit ?? *)
##register [opacapi, no-projection : cps, restricted : cps] callcc_directive \ `QmlCpsServerLib.callcc_directive` : \
(continuation('a), _unit_continuation -> _unit), continuation('a) -> _unit
(* thread_context needs a projection because of the returned option *)
##register [opacapi, no-projection : cps] thread_context \ `QmlCpsServerLib.thread_context` : continuation('a) -> option(opa['thread_context])
##register [opacapi, no-projection, restricted : cps] with_thread_context \ `QmlCpsServerLib.with_thread_context` : opa['b]\
,continuation('a) -> continuation('a)
##register [opacapi, no-projection, restricted : cps] handler_cont \ `QmlCpsServerLib.handler_cont` : continuation('a) -> continuation('c)
##register [opacapi, no-projection : cps, restricted : cps] catch_native \ `QmlCpsServerLib.catch_ml` : \
(opa['c], continuation('a) -> _unit), continuation('a) -> continuation('a)
##register [opacapi, no-projection : cps, restricted : cps] catch \ `QmlCpsServerLib.catch` : Closure.t, continuation('a) -> continuation('a)
##register [opacapi, no-projection : cps, restricted : cps] cont_native \ `QmlCpsServerLib.cont_ml` : ('a -> _unit) -> continuation('a)
##register [opacapi, no-projection : cps, restricted : cps] cont \ `QmlCpsServerLib.cont` : Closure.t -> continuation('a)
##register [opacapi, no-projection : cps, restricted : cps] ccont_native \ `QmlCpsServerLib.ccont_ml` : continuation('d), ('a -> _unit) -> continuation('a)
##register [opacapi, no-projection : cps, restricted : cps] ccont \ `QmlCpsServerLib.ccont` : continuation('d), Closure.t -> continuation('a)
##register [opacapi, no-projection, restricted : cps] return \ `QmlCpsServerLib.return` : continuation('a), 'a -> void
##register [opacapi, no-projection, restricted : cps] magic_func \ `QmlCpsServerLib.magic_func` : func('a, 'd) -> func('e, 'f)
##register [opacapi, no-projection, restricted : cps] make_barrier \ `QmlCpsServerLib.make_barrier` : string -> Cps.future('a)
##register [opacapi, no-projection, restricted : cps] black_make_barrier \ `QmlCpsServerLib.black_make_barrier` : string -> black_future
##register [opacapi, no-projection, restricted : cps] release_barrier \ `QmlCpsServerLib.release_barrier` : Cps.future('a), 'a -> void
##register [opacapi, no-projection, restricted : cps] black_release_barrier \ `QmlCpsServerLib.black_release_barrier` : black_future, 'a -> void
##register [opacapi, no-projection, restricted : cps] uncps_native : string, continuation('d), (continuation('a) -> _unit) -> 'a
##register [no-projection, restricted : cps] uncps : string, continuation('d), Closure.t -> 'a
let uncps_ml = QmlCpsServerLib.uncps_ml
let uncps_native = uncps_ml
let uncps = QmlCpsServerLib.uncps
##register [opacapi, no-projection, restricted : cps] before_wait \ `QmlCpsServerLib.before_wait` : -> void
##register [opacapi, no-projection, restricted : cps] toplevel_wait : Cps.future('a) -> 'a
let toplevel_wait b =
let is_released () = QmlCpsServerLib.is_released b in
#<If:CPS_BLOCKING_WAIT>
Format.eprintf "LOOP-UNTIL: %a\n%!" QmlCpsServerLib.print_barrier b;
#<End>;
let () = Scheduler.loop_until BslScheduler.opa is_released in
QmlCpsServerLib.toplevel_wait b
##register [opacapi, no-projection, restricted : cps] black_toplevel_wait : black_future -> 'a
let black_toplevel_wait (black : QmlCpsServerLib.black_future) =
let b = (( Obj.magic black ) : _ QmlCpsServerLib.future ) in
toplevel_wait b
(* User ByPass (see, cps.opa) - used by the user, need a projection *)
##register user_return \ `QmlCpsServerLib.return` : continuation('a), 'a -> void
##register execute \ `QmlCpsServerLib.execute` : continuation('a), 'a -> void
##register [cps-bypass] user_cont_cps : ('a, continuation(opa[void]) -> void), continuation(continuation('a)) -> void
let user_cont_cps f k =
let fk = QmlCpsServerLib.ccont_ml k
(fun x -> f x (QmlCpsServerLib.ccont_ml k (fun _ -> ()))) in
QmlCpsServerLib.return k fk
##register user_cont : ('a -> void) -> continuation('a)
let user_cont f = QmlCpsServerLib.cont_ml (fun a -> ignore (f a))
##register [opacapi, no-projection] bt_add \ `QmlCpsServerLib.bt_add` : string -> void
##register [no-projection] bt_take \ `QmlCpsServerLib.bt_take` : -> string
##register [opacapi, no-projection] fun_args2string \ `QmlCpsServerLib.fun_args2string` : string, 'a -> string
(* The argument is the first world displayed, e.g. "Raised" or "Interrupted" *)
##register [no-projection] display_backtrace \ `QmlCpsServerLib.display_backtrace` : string -> void
##register update_cont \ `QmlCpsServerLib.update_cont` : continuation('a), option(continuation(_)), string, string, _ -> continuation('a)
##register print_trace \ `QmlCpsServerLib.print_trace` : continuation('a) -> void
##register [no-projection, restricted : cps] loop_schedule : opa['a] -> void
let loop_schedule _ = Scheduler.run BslScheduler.opa
(** Notcps_compatibility : add a dummy implementation for some primitive,
so that the same (qml/opa) code using cps features can be compiled
using them, without changing its semantic without --cps mode *)
##module Notcps_compatibility
let fatal_error = fun s -> Logger.critical "%s" s; BslSys.do_exit 1
##register [opacapi, no-projection, restricted : cps] dummy_cont : continuation(void)
let dummy_cont = QmlCpsServerLib.cont_ml (fun x -> x)
(** return always None *)
##register [opacapi] thread_context : opa['g] -> option(opa['c])
let thread_context _ = None
(** identity, ignore the context *)
##register with_thread_context : opa['g], 'a -> 'a
let with_thread_context _ a = a
##register callcc_directive : (continuation('a) -> void) -> 'a
let callcc_directive f =
QmlCpsServerLib.uncps_ml "CheatedCALLCC"
(QmlCpsServerLib.cont_ml (fun () -> ()))
f
##register [opacapi] cps0_native : (-> 'b) -> (continuation('b) -> void)
let cps0_native f = (fun k -> QmlCpsServerLib.return k (f ()))
##register [opacapi] cps1_native : ('a -> 'b) -> ('a, continuation('b) -> void)
let cps1_native f = (fun x k -> QmlCpsServerLib.return k (f x))
##register [opacapi] cps2_native : ('a, 'b -> 'c) -> ('a, 'b, continuation('c) -> void)
let cps2_native f = (fun x0 x1 k -> QmlCpsServerLib.return k (f x0 x1))
##register [opacapi]cps3_native : ('a, 'b, 'c -> 'd) -> ('a, 'b, 'c, continuation('d) -> void)
let cps3_native f = (fun x0 x1 x2 k -> QmlCpsServerLib.return k (f x0 x1 x2))
##register [opacapi] cps4_native : ('a, 'b, 'c, 'd -> 'e) -> ('a, 'b, 'c, 'd, continuation('e) -> void)
let cps4_native f = (fun x0 x1 x2 x3 k -> QmlCpsServerLib.return k (f x0 x1 x2 x3))
##register [opacapi] cps5_native : ('a, 'b, 'c, 'd, 'e -> 'f) -> ('a, 'b, 'c, 'd, 'e, continuation('f) -> void)
let cps5_native f = (fun x0 x1 x2 x3 x4 k -> QmlCpsServerLib.return k (f x0 x1 x2 x3 x4))
##endmodule