-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModuleState.ml
452 lines (390 loc) · 14.7 KB
/
ModuleState.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
(*
* Copyright (c) 2015 Stefan Krah. All rights reserved.
*
* This file is distributed under the terms of the Q Public License
* version 1.0.
*)
open Printf
open Shared
open ParseTree
open Types
module type S =
sig
val filename : string
module ScopeID : Symbol.S with type t = Symbol.ScopeID.t
module Tycon : Symbol.S with type t = Symbol.Tycon.t
module Tyvar : Symbol.S with type t = Symbol.Tyvar.t
module Tyfield : Symbol.S with type t = Symbol.Tyfield.t
module Tymeta : Symbol.S with type t = Symbol.Tymeta.t
module ValueID : Symbol.S with type t = Symbol.ValueID.t
type tycon_env = Symbol.Tycon.t Env.t
type tyvar_env = Symbol.Tyvar.t Env.t
type value_env = Symbol.ValueID.t Env.t
type scope = {
scope_id : Symbol.ScopeID.t;
scope_prev : Symbol.ScopeID.t;
mutable scope_locals : Symbol.ValueID.t list;
mutable scope_escapes : Symbol.ValueID.t list;
}
type value = {
value_id : Symbol.ValueID.t;
value_scope : Symbol.ScopeID.t;
value_level : int;
mutable value_type : Types.t option;
mutable value_kind : Shared.value_kind;
mutable value_escape : bool;
mutable value_assigned_to : bool;
mutable value_access : Llvm.llvalue option
}
type context = {
scope : Symbol.ScopeID.t;
level : int;
tycon_env : tycon_env;
tyvar_env : tyvar_env;
value_env : value_env;
}
module ScopeTable :
sig
val add : context -> string -> context
val add_main : string -> Symbol.ScopeID.t
val find : Symbol.ScopeID.t -> scope
end
module TyconTable :
sig
val add_env : context -> string -> context * Symbol.Tycon.t
val add_tbl : context -> string -> Types.tfun -> Symbol.Tycon.t
val unsafe_add : context -> Symbol.Tycon.t * Types.tfun -> context
val unsafe_add2 : Symbol.Tycon.t -> Types.tfun -> unit
val find_env : context -> string -> Location.t -> Symbol.Tycon.t
val find_tbl : Symbol.Tycon.t -> Location.t -> Types.tfun
val really_find : Symbol.Tycon.t -> Types.tfun
val get_tfun_params : Types.tfun -> Symbol.Tyvar.t list
val destruct : Symbol.Tycon.t -> Symbol.Tyvar.t list * Types.t
val get_record_fields : Symbol.Tycon.t -> Symbol.Tyfield.t list
val get_record_fields_with_types : Symbol.Tycon.t -> (Symbol.Tyfield.t * Types.t) list
end
module TyvarTable :
sig
val add : context -> ParseTree.core_type -> context * Symbol.Tyvar.t
val add_str : context -> string -> context * Symbol.Tyvar.t
val find_env : context -> string -> Location.t -> Symbol.Tyvar.t
end
module TymetaTable :
sig
val find : context -> string -> Types.t
val clear : unit -> unit
end
module FieldTable :
sig
val add_id : ParseTree.field -> Symbol.Tyfield.t
val add_id_str : string -> Symbol.Tyfield.t
val add_type : Symbol.Tyfield.t -> Types.t -> unit
val find_id : string -> Location.t -> Symbol.Tyfield.t
val typeof : Symbol.Tyfield.t -> Types.t
val flagof : Symbol.Tyfield.t -> Shared.mutable_flag
val record_abbr : (Symbol.Tyfield.t * Types.t) list -> Types.t
val validate : (Symbol.Tyfield.t * 'a) list -> Location.t -> Symbol.Tycon.t
end
module ValueTable :
sig
val add :
context -> ParseTree.value_description -> context * Symbol.ValueID.t
val add_name : Symbol.ScopeID.t -> Shared.value_kind -> string -> Symbol.ValueID.t
val find_and_update : bool -> context -> string -> Location.t -> Symbol.ValueID.t
val find : Symbol.ValueID.t -> Location.t -> value
val really_find : Symbol.ValueID.t -> value
val add_type : Symbol.ValueID.t -> Types.t -> unit
val typeof : Symbol.ValueID.t -> Types.t
val is_assigned_to : Symbol.ValueID.t -> bool
val fold : (Symbol.ValueID.t -> value -> unit -> unit) -> unit
end
val mk_main_context : string -> context
end
module type ARGS =
sig
val filename : string
end
(** Implementation of the module state *)
module Make (A: ARGS) =
struct
let filename = A.filename
include SymbolGenerator.Make (struct end)
type tycon_env = Symbol.Tycon.t Env.t
type tyvar_env = Symbol.Tyvar.t Env.t
type value_env = Symbol.ValueID.t Env.t
type scope =
{ scope_id : ScopeID.t;
scope_prev : ScopeID.t;
mutable scope_locals : ValueID.t list;
mutable scope_escapes : ValueID.t list }
type value =
{ value_id : ValueID.t;
value_scope : ScopeID.t;
value_level : int;
mutable value_type : Types.t option;
mutable value_kind : value_kind;
mutable value_escape : bool;
mutable value_assigned_to : bool;
mutable value_access : Llvm.llvalue option }
type context =
{ scope : ScopeID.t;
level : int;
tycon_env : tycon_env;
tyvar_env : tyvar_env;
value_env : value_env }
module ScopeTable =
struct
module Tbl = Hashtbl.Make(ScopeID)
let tbl : scope Tbl.t = Tbl.create 128
let add ctx name =
let id = ScopeID.make name in
let next = { scope_id = id;
scope_prev = ctx.scope;
scope_locals = [];
scope_escapes = [] }
in
let ctx' = { ctx with scope = id } in
Tbl.add tbl id next; ctx'
let add_main name =
let extern = ScopeID.make "external" in
let id = ScopeID.make name in
if ScopeID.id id <> 0 then internal_error "invalid main scope id";
let main = { scope_id = id;
scope_prev = extern;
scope_locals = [];
scope_escapes = [] } in
Tbl.add tbl id main; id
let find id =
if ScopeID.id id < 0 then internal_error "scope id < 0"
else Tbl.find tbl id
end
module TyconTable =
struct
module Tbl = Hashtbl.Make(Tycon)
let tbl : Types.tfun Tbl.t = Tbl.create 128
let add_env ctx name =
let id = Tycon.make name in
let env' = Env.add name id ctx.tycon_env in
let ctx' = { ctx with tycon_env = env' } in
(ctx', id)
let find_env ctx name pos =
try Env.find name ctx.tycon_env
with Not_found ->
Location.error pos ("env: undeclared type constructor: " ^ name)
let add_tbl ctx name tfun =
let id = Env.find name ctx.tycon_env in
Tbl.add tbl id tfun; id
let unsafe_add ctx (id, tfun) =
let env' = Env.add (Tycon.name id) id ctx.tycon_env in
let ctx' = { ctx with tycon_env = env' } in
Tbl.add tbl id tfun; ctx'
let unsafe_add2 id tfun = Tbl.add tbl id tfun
let find_tbl id loc =
try Tbl.find tbl id
with Not_found ->
let name = Tycon.name id in
Location.error loc ("tbl: undeclared type constructor: " ^ name)
let really_find id =
try Tbl.find tbl id
with Not_found ->
let name = Tycon.name id in
internal_error ("tbl: undeclared type constructor: " ^ name)
let get_tfun_params = function
Tfun (params, _) -> params
| Tlink id | Tconstr id ->
begin match Tbl.find tbl id with
Tfun (params, _) -> params
| Tlink _ -> internal_error "unresolved tlink"
| Tconstr _ -> internal_error "unresolved tconstr"
end
let destruct id =
match really_find id with
Tfun (params, body) -> (params, body)
| Tlink _ -> internal_error "unexpected tlink"
| Tconstr _ -> internal_error "unexpected tconstr"
let get_record_fields id =
match really_find id with
Tfun (_, Trecord fields) -> List.map fst fields
| _ -> internal_error "expected record type function"
let get_record_fields_with_types id =
match really_find id with
Tfun (_, Trecord fields) -> fields
| _ -> internal_error "expected record type function"
end
module TyvarTable =
struct
let add ctx t =
let name = match t.core_type_desc with
Ptyp_any -> "_"
| Ptyp_var s -> s
| _ -> internal_error "invalid type variable" in
let id = Tyvar.make name in
let env' = Env.add name id ctx.tyvar_env in
let ctx' = { ctx with tyvar_env = env' } in
(ctx', id)
let add_str ctx name =
let id = Tyvar.make name in
let env' = Env.add name id ctx.tyvar_env in
let ctx' = { ctx with tyvar_env = env' } in
(ctx', id)
let find_env ctx name pos =
try Env.find name ctx.tyvar_env
with Not_found ->
Location.error pos ("undeclared type variable: " ^ name)
end
module TymetaTable =
struct
module Tbl = Hashtbl.Make(Util.String)
let types : Types.t Tbl.t = Tbl.create 16
let add ctx name =
let t = Tmeta (ref (Unbound (Tymeta.make name, ctx.level))) in
Tbl.add types name t; t
let find ctx name =
try let t = Tbl.find types name in
Tmeta (ref (Link t))
with Not_found -> add ctx name
let clear () = Tbl.clear types
end
module FieldTable =
struct
module StringTbl = Hashtbl.Make(Util.String)
module FieldTbl = Hashtbl.Make(Tyfield)
let fields : Tyfield.t StringTbl.t = StringTbl.create 128
let types : Types.t FieldTbl.t = FieldTbl.create 128
let add_id f =
let id = Tyfield.make f.pfield_name in
try ignore (StringTbl.find fields f.pfield_name);
let msg = sprintf "duplicate field %s" f.pfield_name in
Location.error f.pfield_loc msg
with Not_found ->
StringTbl.add fields f.pfield_name id; id
let add_id_str s =
let id = Tyfield.make s in
try ignore (StringTbl.find fields s);
let msg = sprintf "duplicate field %s" s in
internal_error msg
with Not_found ->
StringTbl.add fields s id; id
let add_type id t = FieldTbl.add types id t
let find_id name pos =
try StringTbl.find fields name
with Not_found ->
let msg = sprintf "undeclared field %s" name in
Location.error pos msg
let typeof id =
try FieldTbl.find types id
with Not_found ->
let msg = sprintf "missing field table entry: %s" (Tyfield.name id) in
internal_error msg
let flagof id =
match typeof id with
Tpoly (_, Tfield (_, flag, _, _)) -> flag
| _ -> internal_error "not a field type"
let record_abbr = function
[] -> internal_error "empty record"
| (f, _) :: _ -> begin match typeof f with
Tpoly (_, Tfield (_, _, tr, _)) -> tr
| _ -> internal_error "not a field type"
end
(* validate record construction *)
let validate fields loc =
let extract id =
match typeof id with
Tpoly (_, Tfield (fld, _, Tapp (Tlink record, _), _)) ->
(fld, record)
| _ -> internal_error "not a field type"
in
let rec doit = function
[] -> internal_error "empty record"
| [(f, _)] ->
let (g, r) = extract f in
if f <> g then
error loc "record construction differs from declared type"
else r
| (f1, _) :: ((f2, _) :: _ as tl) ->
let (g1, r1) = extract f1 in
let (g2, r2) = extract f2 in
if f1 <> g1 || f2 <> g2 || r1 <> r2 then
error loc "record construction differs from declared type"
else doit tl
in doit fields
end
module ValueTable =
struct
module Tbl = Hashtbl.Make(ValueID)
let tbl : value Tbl.t = Tbl.create 128
let add ctx v =
let id = ValueID.make v.pvd_name in
let scope = ScopeTable.find ctx.scope in
let desc = { value_id = id;
value_scope = scope.scope_id;
value_level = 0;
value_type = None;
value_kind = v.pvd_kind;
value_escape = false;
value_assigned_to = false;
value_access = None } in
scope.scope_locals <- scope.scope_locals @ [id];
let env' = Env.add v.pvd_name id ctx.value_env in
let ctx' = { ctx with value_env = env' } in
Tbl.add tbl id desc; (ctx', id)
let add_name scope kind s =
let id = ValueID.make s in
let scope = ScopeTable.find scope in
let desc = { value_id = id;
value_scope = scope.scope_id;
value_level = 0;
value_type = None;
value_kind = kind;
value_escape = false;
value_assigned_to = false;
value_access = None } in
scope.scope_locals <- scope.scope_locals @ [id];
Tbl.add tbl id desc; id
let find id pos =
try Tbl.find tbl id
with Not_found ->
let name = ValueID.name id in
Location.error pos ("undeclared variable: " ^ name)
let really_find id =
try Tbl.find tbl id
with Not_found ->
let name = ValueID.name id in
internal_error ("missing value table entry: " ^ name)
let add_type id t =
let v = really_find id in
match v.value_type with
_ -> v.value_type <- Some t
(* XXX | Some t -> internal_error "setting type twice" *)
let typeof id =
match (really_find id).value_type with
None -> internal_error "type not set"
| Some t -> t
let is_assigned_to id = let v = really_find id in v.value_assigned_to
let find_and_update assign ctx name pos =
let id = try Env.find name ctx.value_env
with Not_found ->
Location.error pos ("undeclared variable: " ^ name)
in
let scope = ScopeTable.find ctx.scope in
let () = match really_find id with
{ value_kind = External; _ } -> ()
| v -> if assign then
v.value_assigned_to <- true;
if v.value_scope <> scope.scope_id then
let s = ScopeTable.find v.value_scope in
v.value_escape <- true;
if not (List.mem id s.scope_escapes) then
s.scope_escapes <- s.scope_escapes @ [id]
in id
let fold f = Tbl.fold f tbl ()
end
let mk_main_context name =
let main_scope = ScopeTable.add_main name in
let ctx = { scope = main_scope;
level = 0;
tycon_env = Env.empty;
tyvar_env = Env.empty;
value_env = Env.empty } in
List.fold_left TyconTable.unsafe_add ctx Types.prim_types
end