-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenvironment.sml
471 lines (388 loc) · 19 KB
/
environment.sml
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
(* $Id$
*
* Copyright (c) 2008 Timothy Bourke (University of NSW and NICTA)
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the "BSD License" which is distributed with the
* software in the file LICENSE.
*
* This program 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 BSD
* License for more details.
*
* Adapted from `Modern compiler implementation in ML', Appel 1998.
*)
(* TODO:
* clean up the storage of scope, making it more consistent.
*)
structure Environment : ENVIRONMENT
(*structure Environment :> ENVIRONMENT (* TODO: sort out mlton & sml/nj *)
where (Expression = Expression)
where (Declaration = Declaration)*) =
struct
structure Expression = Expression
structure E = Expression
structure Declaration = Declaration
structure D = Declaration
structure ECVT = ExpressionCvt
exception UndeclaredTypeName of string
exception CannotStaticallyEvaluate of string
exception DuplicateDefinition of string
exception VariableWithoutType of string
(* shortcuts over Atom and AtomSet *)
infix <+ <- ++ <\ \ =:= ; open Symbol
type ty = Expression.ty
type expr = Expression.expr
type decl = Declaration.decl
type stmt = Declaration.stmt
type symbol = Atom.atom
structure Map = AtomMap
type 'a table = (int * 'a) Map.map
(* Track scope so as to identify shared variables. *)
datatype scopetag = GlobalScope (* defined globally *)
| ParameterScope (* template parameter *)
| TemplateScope (* defined locally to template *)
| LocalScope (* function parameter or local *)
| SelectScope (* select id on transition *)
| BoundScope (* bound variable (forall, exists) *)
datatype enventry = VarEntry of {ty: ty, init: D.initialiser option,
ref: bool, scope: scopetag}
| FunEntry of {formals: {ty:ty, id:symbol, ref: bool} list,
result: ty, body: stmt, scope : scopetag}
type tyentry = scopetag * ty
type env = tyentry table * enventry table * int
(* Each entry, in either table, is stamped with an integer, indicating
* when it was inserted. This allows a simple approximation of
* dependencies (provided insertions are performed in that order).
* The int tracks the next stamp. *)
val base_tenv = Map.empty
val base_venv = Map.empty
val base_env = (base_tenv, base_venv, 0)
(* NB: the base types are parsed in directly which simplifies handling
bounded integer expressions (e.g. int[-5,5]) *)
val base_tenv = Map.empty
(* NB: there are no variables to begin with *)
val base_venv = Map.empty
fun isEmpty (tenv, venv, _) = Map.isEmpty tenv andalso Map.isEmpty venv
(* SimpleInit, ArrayInit *)
fun compileTimeEval ((tenv, venv, _), e) = e
(* let
fun proc (VarExpr v) = ... getvalue, check const ...
| proc (v as (IntCExpr _)) = v
| proc (v as (BoolCExpr _)) = v
| proc (v as (CallExpr
(* Need an interpreter for funtions!... *)
in
proc e
end
*)
local fun resolve (E.Const, _) = E.Const (* Const is strongest *)
| resolve (_, E.Const) = E.Const
| resolve (E.NoQual, tq') = tq' (* NoQual is weakest *)
| resolve (tq, _) = tq
in
fun applyTyQual (E.NoQual, t) = t
| applyTyQual (tq, E.INT (b, tq')) = E.INT (b, resolve (tq, tq'))
| applyTyQual (tq, E.BOOL tq') = E.BOOL (resolve (tq, tq'))
| applyTyQual (_, E.CLOCK) = E.CLOCK
| applyTyQual (_, t as (E.CHANNEL _)) = t
| applyTyQual (tq, E.SCALAR (e, tq', u))= E.SCALAR (e,
resolve (tq, tq'), u)
| applyTyQual (tq, E.RECORD (fields, tq', u)) = E.RECORD (
map (fn (s, t)=> (s, applyTyQual (tq, t))) fields, resolve (tq, tq'), u)
| applyTyQual (tq, E.ARRAY (ty, e)) = E.ARRAY (applyTyQual (tq, ty), e)
| applyTyQual (tq, E.NAME (s, tq', NONE)) =
E.NAME (s, resolve (tq, tq'), NONE)
| applyTyQual (tq, E.NAME (s, tq', SOME exty)) =
E.NAME (s, resolve (tq, tq'), SOME (applyTyQual (tq, exty)))
| applyTyQual (tq, E.VOID) = E.VOID
end
(* Expand out all E.NAME entries from the given type.
Reduce all expressions as far as possible. *)
fun expandTyIds (env as (tenv, venv, _), t) = let
fun expandField (s, t) = (s, expand t)
and expand (E.INT (NONE, tq)) = E.INT (NONE, tq)
| expand (E.INT (SOME (e1, e2), tq)) = E.INT (SOME (
compileTimeEval (env, e1),
compileTimeEval (env, e2)), tq)
| expand (E.SCALAR (e, tq, u)) = E.SCALAR (
compileTimeEval (env, e), tq, u)
| expand (E.RECORD (fields, tyqual, u))
= E.RECORD (map expandField fields,
tyqual, u)
| expand (E.ARRAY (ty, E.Type sty)) = E.ARRAY (expand ty,
E.Type (expand sty))
| expand (E.ARRAY (ty, E.Unresolved s)) = let
val sty = case Map.find (tenv, s)
of SOME (_, (_, ety)) => E.NAME (s, E.tyQual ety,
SOME ety)
| NONE => let
val u = E.VarExpr (E.SimpleVar s)
(* int[l, u] bounds are inclusive *)
val umo = E.dec u
in E.INT (SOME (E.IntCExpr 0, umo), E.NoQual) end
in
E.ARRAY (expand ty, E.Type (expand sty))
end
| expand (E.NAME (s, tyqual, _)) = (case Map.find (tenv, s) of
NONE => raise UndeclaredTypeName (Atom.toString s)
| SOME (_, (_, ety)) => E.NAME (s, tyqual,
SOME (applyTyQual (tyqual, ety))))
(* assumption: ety is already expanded. *)
| expand (v as (E.BOOL _)) = v
| expand (v as (E.CHANNEL _)) = v
| expand E.CLOCK = E.CLOCK
| expand E.VOID = E.VOID
in expand t end
fun addDeclarations (oenv, scope, decls) = let
(* (* TODO: sometimes duplicates are ok (e.g. local variable),
only complain if the scopes are the same? *)
infix <?; fun m <? x = isSome (Map.find (m, x))
fun ensureNoDuplicates (d, env as (tenv, venv)) = case D.declToId d
of NONE => (d, env)
| SOME id => if tenv <? id orelse venv <? id
then raise DuplicateDefinition (Atom.toString id)
else (d, env)
*)
fun ensureNoDuplicates (d, env) = (d, env)
fun addDecl (D.TyDecl {id, ty, ...}, env as (tenv, venv, c)) =
let
val e = expandTyIds (env, ty)
in
(Map.insert (tenv, id, (c, (scope, e))), venv, c+1)
end
| addDecl (D.VarDecl {id, ty, initial, ...}, env as (tenv, venv, c)) =
let
fun evalInit (D.SimpleInit e) = D.SimpleInit
(compileTimeEval (env, e))
| evalInit (D.ArrayInit l) = D.ArrayInit (map evalInit l)
val init = case initial of
NONE => NONE
| SOME e => SOME (evalInit e)
val v = VarEntry {ty=expandTyIds (env, ty),
init=init, ref=false, scope=scope}
in (tenv, Map.insert (venv, id, (c, v)), c+1) end
| addDecl (D.FunDecl {id, rty, params,body, ...}, env as (tenv,venv,c)) =
let
fun doParam (D.ValParam {id, ty}) = {ty=expandTyIds (env, ty),
id=id, ref=false}
| doParam (D.RefParam {id, ty}) = {ty=expandTyIds (env, ty),
id=id, ref=true}
val e = FunEntry {formals=map doParam params,
result=expandTyIds (env, rty),
body=body, scope=scope}
in (tenv, Map.insert (venv, id, (c, e)), c+1) end
| addDecl (D.ChanPriDecl _, env) = env (* ignore *)
in
foldl (addDecl o ensureNoDuplicates) oenv decls
end
fun addParameters (oenv as (tenv, ovenv, c), scope, params) = let
fun getVar (r, ty) = VarEntry {ty=expandTyIds (oenv, ty),
init=NONE, ref=r, scope=scope}
fun doParam (D.ValParam {id, ty}, (c, venv)) =
(c+1, Map.insert (venv, id, (c, getVar (false, ty))))
| doParam (D.RefParam {id, ty}, (c, venv)) =
(c+1, Map.insert (venv, id, (c, getVar (true, ty))))
val (c', venv') = foldl doParam (c, ovenv) params
in (tenv, venv', c') end
fun typeEnvToString (tenv, _, _) = Map.foldli (fn (s, (_, (_, t)), pre) =>
pre ^ Atom.toString s ^ ": " ^
ECVT.Ty.toString t ^ "\n") "" tenv
local
fun paramToStr {id, ty, ref=false} = Atom.toString id ^
": " ^ ECVT.Ty.toString ty
| paramToStr {id, ty, ref=true } = Atom.toString id ^
": " ^ ECVT.Ty.toString ty ^ "(ref)"
fun scopeToStr GlobalScope = "global"
| scopeToStr ParameterScope = "param"
| scopeToStr TemplateScope = "template"
| scopeToStr LocalScope = "local"
| scopeToStr SelectScope = "select"
| scopeToStr BoundScope = "bound"
fun envEntryToStr (_, VarEntry {ty, scope, ...}) = ECVT.Ty.toString ty ^
"(" ^ scopeToStr scope ^ ")"
| envEntryToStr (_, FunEntry {formals, result, body, scope}) =
(ListFormat.fmt {init="(", sep=",", final=")", fmt=paramToStr} formals)
^ " -> " ^ ECVT.Ty.toString result ^ " (" ^ scopeToStr scope ^ ")"
in
fun varEnvToString (_, venv, _) = Map.foldli (fn (s, e, pre)=> pre ^
Atom.toString s ^ ": " ^
envEntryToStr e ^ "\n") ""
venv
end
fun baseType (E.NAME (_, _, SOME ety)) = ety
| baseType ty = ty
local
val <*< = Option.composePartial; infix <*<
fun funToType (_, FunEntry {result, ...}) = SOME (baseType result)
| funToType (_, VarEntry _) = NONE
fun varToType (_, VarEntry {ty, ...}) = SOME (baseType ty)
| varToType (_, FunEntry _) = NONE
fun varToScope (_, VarEntry {scope, ...}) = SOME scope
| varToScope (_, FunEntry _) = NONE
fun stripArray (E.ARRAY (ty, _)) = SOME ty
| stripArray _ = NONE
in
fun findVarExprType (tenv, venv, _) = let
fun findField field (E.RECORD (fields, tq, _)) = let
fun getType (_, t) = SOME (applyTyQual (tq, t))
fun isField (s, _) = Atom.compare (field, s) = EQUAL
in
(getType <*< (List.find isField)) fields
end
| findField _ _ = NONE
fun fve (E.SimpleVar s) = (varToType <*< Map.find) (venv, s)
| fve (E.ReturnVar {func, ...}) = (funToType <*< Map.find) (venv, func)
| fve (E.SubscriptVar (v, _)) = (stripArray <*< fve) v
| fve (E.RecordVar (v, field)) = ((findField field) <*< fve) v
in fve end
fun findVal (_, venv, _) s = (Option.compose (fn (_, v)=>v, Map.find)) (venv, s)
fun findValType (_, venv, _) s = (varToType <*< Map.find) (venv, s)
fun findValScope (_, venv, _) s = (varToScope <*< Map.find) (venv, s)
fun channels (tenv, venv, _) = let
fun elemTy (E.ARRAY (ty, _)) = elemTy ty
| elemTy ty = ty
fun getChan (id, (_, VarEntry {ty, ...})) = (case elemTy (baseType ty) of
E.CHANNEL _ => SOME id
| _ => NONE)
| getChan _ = NONE
in Map.listItems (Map.mapPartiali getChan venv) end
fun usedIds (tenv, venv, _) = let
fun addId (nm, _, s) = AtomSet.add (s, nm)
val tids = Map.foldli addId AtomSet.empty tenv
val vids = Map.foldli addId tids venv
in vids end
end (* local *)
local structure SS = Substring in
fun newId (baseSymbol, (tenv, venv, _)) =
(*{{{1*)
if not (Option.isSome (Map.find (venv, baseSymbol))
orelse Option.isSome (Map.find (tenv, baseSymbol)))
then baseSymbol
else let
fun getPrefix s = SS.string (SS.dropr Char.isDigit (SS.full s))
fun getSuffix s = let
val suf = SS.taker Char.isDigit (SS.full s)
in
if SS.isEmpty suf then 0
else valOf (Int.fromString (SS.string suf))
end
val prefix = getPrefix (Atom.toString baseSymbol)
fun maxSuffix (a, _, m) = let val s = Atom.toString a in
if String.isPrefix prefix s
then Int.max (m, getSuffix s) else m
end
val tsuffix = Map.foldli maxSuffix 0 tenv
val suffix = (Map.foldli maxSuffix tsuffix venv) + 1
in
Atom.atom (prefix ^ Int.toString suffix)
end (*}}}1*)
end (*local*)
fun addId scope ((id, ty), env as (tenv, venv, c)) = let
val v = VarEntry {ty=expandTyIds (env, ty), init=NONE,
ref=false, scope=scope}
in (tenv, Map.insert (venv, id, (c, v)), c+1) end
fun createdAfter ((_, (i, _)), (_, (j, _))) = i > j
fun writableVariables ((_, venv, _), seekScope) = let
fun goodTy (E.INT (_, E.NoQual)) = true
| goodTy (E.BOOL E.NoQual) = true
| goodTy (E.SCALAR (_, E.NoQual, _)) = true
| goodTy (E.RECORD (_, E.NoQual, _)) = true
| goodTy (E.ARRAY (ty, _)) = goodTy ty
| goodTy (E.NAME (_, E.NoQual, _)) = true
| goodTy _ = false
fun f (_, (_, FunEntry _), l) = l
| f (var, (_, entry as VarEntry {ty, scope, ...}), l) =
if (goodTy ty andalso scope = seekScope) then var::l else l
in Map.foldli f [] venv end
fun dupVariables vars (env as (_, ovenv, _), newscope') = let
fun newscope (v as VarEntry {ty, init, ref=r, scope}) = (case newscope' of
NONE => v
| SOME scope' => VarEntry {ty=ty, init=init, ref=r, scope=scope'})
| newscope _ = raise Fail "bad call to newscope in dupVariables"
fun dupVar (_, (_, FunEntry _), s) = s
| dupVar (var, (_, entry as VarEntry {ty, scope, ...}),
s as (oldnewmap, env as (tenv, venv, i))) =
let
val var' = newId (var, env)
in
(Map.insert (oldnewmap, var, var'),
(tenv, Map.insert (venv, var', (i, newscope entry)), i+1))
end
fun f (v, args) = case Map.find (ovenv, v) of
NONE => args
| SOME entry => dupVar (v, entry, args)
in foldl f (Map.empty, env) vars end
fun mapValues f (_, venv, _) = let
fun f' (i, (_, d)) = f (i, d)
in
List.mapPartial f' (ListMergeSort.sort createdAfter (Map.listItemsi venv))
end
fun mapTypes f (tenv, _, _) = let
fun f' (i, (_, (s, d))) = f (i, s, d)
in
List.mapPartial f' (ListMergeSort.sort createdAfter (Map.listItemsi tenv))
end
local (*{{{1*)
datatype mixed = EntryVal of symbol * enventry
| EntryTy of symbol * scopetag * ty
fun fromVal (i, (c, d)) = (c, EntryVal (i, d))
fun fromTy (i, (c, (s, d))) = (c, EntryTy (i, s, d))
fun createdAfter' ((i, _), (j, _)) = i > j
in (*}}}1*)
fun mapBoth (fVal, fTy) (tenv, venv, _) = let
fun f' (_, EntryVal d) = fVal d
| f' (_, EntryTy d) = fTy d
in
List.mapPartial f' (ListMergeSort.sort createdAfter'
(List.revAppend (map fromVal (Map.listItemsi venv),
map fromTy (Map.listItemsi tenv))))
end
end (* local *)
(* val filter : (env * expr -> bool) -> env -> expr -> expr list *)
fun filter p env e = let
fun f (env, e) = if p (env, e) then [e] (*{{{1*)
else case e
of E.VarExpr _ => []
| E.IntCExpr _ => []
| E.BoolCExpr _ => []
| E.Deadlock => []
| E.CallExpr {args, ...} => foldl (flist env) [] args
| E.NegExpr expr => f (env, expr)
| E.NotExpr expr => f (env, expr)
| E.UnaryModExpr {expr, ...}=> f (env, expr)
| E.BinIntExpr {left, right,...} => f (env, left)
@ f (env, right)
| E.BinBoolExpr {left, right,...} => f (env, left)
@ f (env, right)
| E.RelExpr {left, right, ...} => f (env, left)
@ f (env, right)
| E.AssignExpr {var, expr, ...} => f (env, var)
@ f (env, expr)
| E.CondExpr {test, trueexpr, falseexpr, ...}
=> f (env, test)
@ f (env, trueexpr)
@ f (env, falseexpr)
| E.ForAllExpr {id, ty, expr, ...} => let
val env' = addId BoundScope ((id, ty), env)
in f (env', expr) end
| E.ExistsExpr {id, ty, expr, ...} => let
val env' = addId BoundScope ((id, ty), env)
in f (env', expr) end
and flist env (e, el) = f (env, e) @ el
in f (env, e) end (*}}}1*)
local
fun isClk (env, v) = case findVarExprType env v
of SOME (E.CLOCK) => true
| NONE => raise VariableWithoutType
(E.varName v)
| _ => false
fun isClkVar (env, E.VarExpr v) = isClk (env, v)
| isClkVar _ = false
in
fun containsClocks env expr =
not (List.null (filter (fn (env',e)=>isClkVar (env', e)) env expr))
end
end