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
171
172
173
174
175
176
177
178
179
180
|
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Vincent Laviron, OCamlPro *)
(* *)
(* Copyright 2017 OCamlPro SAS *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
[@@@ocaml.warning "-40"]
module Int = Numbers.Int
(* Check a number of continuation-related invariants *)
module Env : sig
type t
val init : unit -> t
val handler : t -> cont:int -> arg_num:int -> t
val jump : t -> cont:int -> arg_num:int -> unit
val report : Format.formatter -> bool
end = struct
type t = {
bound_handlers : int Int.Map.t;
}
type error =
| Unbound_handler of { cont: int }
| Multiple_handlers of { cont: int; }
| Wrong_arguments_number of
{ cont: int; handler_args: int; jump_args: int; }
module Error = struct
type t = error
let compare = Stdlib.compare
end
module ErrorSet = Set.Make(Error)
type persistent_state = {
mutable all_handlers : Int.Set.t;
mutable errors : ErrorSet.t;
}
let state = {
all_handlers = Int.Set.empty;
errors = ErrorSet.empty;
}
let record_error error =
state.errors <- ErrorSet.add error state.errors
let unbound_handler cont =
record_error (Unbound_handler { cont; })
let multiple_handler cont =
record_error (Multiple_handlers { cont; })
let wrong_arguments cont handler_args jump_args =
record_error (Wrong_arguments_number { cont; handler_args; jump_args; })
let init () =
state.all_handlers <- Int.Set.empty;
state.errors <- ErrorSet.empty;
{
bound_handlers = Int.Map.empty;
}
let handler t ~cont ~arg_num =
if Int.Set.mem cont state.all_handlers then multiple_handler cont;
state.all_handlers <- Int.Set.add cont state.all_handlers;
let bound_handlers = Int.Map.add cont arg_num t.bound_handlers in
{ bound_handlers; }
let jump t ~cont ~arg_num =
match Int.Map.find cont t.bound_handlers with
| handler_args ->
if arg_num <> handler_args then
wrong_arguments cont handler_args arg_num
| exception Not_found -> unbound_handler cont
let print_error ppf error =
match error with
| Unbound_handler { cont } ->
if Int.Set.mem cont state.all_handlers then
Format.fprintf ppf
"Continuation %d was used outside the scope of its handler"
cont
else
Format.fprintf ppf
"Continuation %d was used but never bound"
cont
| Multiple_handlers { cont; } ->
Format.fprintf ppf
"Continuation %d was declared in more than one handler"
cont
| Wrong_arguments_number { cont; handler_args; jump_args } ->
Format.fprintf ppf
"Continuation %d was declared with %d arguments but called with %d"
cont
handler_args
jump_args
let print_error_newline ppf error =
Format.fprintf ppf "%a@." print_error error
let report ppf =
if ErrorSet.is_empty state.errors then false
else begin
ErrorSet.iter (fun err -> print_error_newline ppf err) state.errors;
true
end
end
let rec check env (expr : Cmm.expression) =
match expr with
| Cconst_int _ | Cconst_natint _ | Cconst_float _ | Cconst_symbol _
| Cvar _ ->
()
| Clet (_, expr, body)
| Clet_mut (_, _, expr, body) ->
check env expr;
check env body
| Cphantom_let (_, _, expr) ->
check env expr
| Cassign (_, expr) ->
check env expr
| Ctuple exprs ->
List.iter (check env) exprs
| Cop (_, args, _) ->
List.iter (check env) args;
| Csequence (expr1, expr2) ->
check env expr1;
check env expr2
| Cifthenelse (test, _, ifso, _, ifnot, _) ->
check env test;
check env ifso;
check env ifnot
| Cswitch (body, _, branches, _) ->
check env body;
Array.iter (fun (expr, _) -> check env expr) branches
| Ccatch (rec_flag, handlers, body) ->
let env_extended =
List.fold_left
(fun env (cont, args, _, _) ->
Env.handler env ~cont ~arg_num:(List.length args))
env
handlers
in
check env_extended body;
let env_handler =
match rec_flag with
| Recursive -> env_extended
| Nonrecursive -> env
in
List.iter (fun (_, _, handler, _) -> check env_handler handler) handlers
| Cexit (cont, args) ->
Env.jump env ~cont ~arg_num:(List.length args)
| Ctrywith (body, _, handler, _) ->
(* Jumping from inside a trywith body to outside isn't very nice,
but it's handled correctly by Linearize, as it happens
when compiling match ... with exception ..., for instance, so it is
not reported as an error. *)
check env body;
check env handler
let run ppf (fundecl : Cmm.fundecl) =
let env = Env.init () in
check env fundecl.fun_body;
Env.report ppf
|