This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uvmTypesScript.sml
330 lines (298 loc) · 9.08 KB
/
uvmTypesScript.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
open HolKernel Parse boolLib bossLib;
val _ = new_theory "uvmTypes";
local open stringTheory finite_mapTheory in end
open lcsymtacs
val _ = ParseExtras.tight_equality()
val _ = type_abbrev ("typename", ``:string``)
val _ = Datatype`
machine = <|
ptrsize : num;
floatsize : num;
doublesize : num
|>
`
val _ = Datatype`
uvm_type =
| Int num
| Float
| Double
| Ref uvm_type
| IRef uvm_type
| WeakRef uvm_type
| UPtr uvm_type
| Struct ((uvm_type + typename) list)
| Array uvm_type num
| Hybrid (uvm_type list) uvm_type
| Void
| ThreadRef
| StackRef
| TagRef64
| Vector uvm_type num
| FuncRef funcsig
| UFuncPtr funcsig ;
funcsig = <|
arg_types : uvm_type list ;
return_types : uvm_type list
|>
`
val fp_type_def = Define`
fp_type Float = T ∧
fp_type Double = T ∧
fp_type _ = F
`
val int_type_def = Define`
int_type (Int _) = T ∧
int_type _ = F
`
val ptr_type_def = Define`
ptr_type (UPtr _) = T ∧
ptr_type (UFuncPtr _) = T ∧
ptr_type _ = F
`
val iref_type_def = Define`
iref_type (IRef _) = T ∧
iref_type _ = F
`
val eqcomparable_def = Define`
eqcomparable (Int _) = T ∧
eqcomparable (Ref _) = T ∧
eqcomparable (IRef _) = T ∧
eqcomparable (UPtr _) = T ∧
eqcomparable (FuncRef _) = T ∧
eqcomparable (UFuncPtr _) = T ∧
eqcomparable ThreadRef = T ∧
eqcomparable StackRef = T ∧
eqcomparable _ = F
`
val scalar_type_def = Define`
scalar_type ty =
case ty of
| Int _ => T
| Float => T
| Double => T
| Ref _ => T
| IRef _ => T
| WeakRef _ => T
| FuncRef _ => T
| UFuncPtr _ => T
| ThreadRef => T
| StackRef => T
| TagRef64 => T
| UPtr _ => T
| _ => F
`
(*
maybe_vector : (uvm_type -> bool) -> uvm_type -> bool
[maybe_vector P ty] checks to see if P is true of ty. Alternatively,
if ty is a vector type, it checks to see if P is true of the element
type of the vector
*)
val maybe_vector_def = Define`
maybe_vector P ty ⇔
P ty ∨ case ty of Vector ty0 _ => P ty0 | _ => F
`
(* Relation of type `(typename |-> uvm_type) -> uvm_type set` *)
val (tracedtype_rules, tracedtype_ind, tracedtype_cases) = Hol_reln`
(∀ty. tracedtype tmap (Ref ty))
∧ (∀ty. tracedtype tmap (IRef ty))
∧ (∀ty. tracedtype tmap (WeakRef ty))
∧ (∀sz ty.
tracedtype tmap ty ⇒
tracedtype tmap (Array ty sz))
∧ (∀sz ty.
tracedtype tmap ty ⇒
tracedtype tmap (Vector ty sz))
∧ tracedtype tmap ThreadRef
∧ tracedtype tmap StackRef
∧ tracedtype tmap TagRef64
∧ (∀ty fixtys varty.
tracedtype tmap ty ∧ MEM ty fixtys
⇒
tracedtype tmap (Hybrid fixtys varty))
∧ (∀fixtys varty.
tracedtype tmap varty ⇒ tracedtype tmap (Hybrid fixtys varty))
∧ (∀ty fields.
tracedtype tmap ty
∧ MEM (INL ty) fields
⇒
tracedtype tmap (Struct fields))
∧ (∀ty_name ty fields.
SOME ty = FLOOKUP tmap ty_name
∧ tracedtype tmap ty
∧ MEM (INR ty_name) fields
⇒
tracedtype tmap (Struct fields))
`
(* A native-safe type can be handed off to the "native" world.
Relation of type `(typename |-> uvm_type) -> uvm_type set`
*)
val (native_safe_rules, native_safe_ind, native_safe_cases) = Hol_reln`
(∀n. native_safe tmap (Int n))
∧ native_safe tmap Float
∧ native_safe tmap Double
∧ native_safe tmap Void
∧ (∀ty n. native_safe tmap ty ⇒ native_safe tmap (Vector ty n))
∧ (∀ty n. native_safe tmap ty ⇒ native_safe tmap (Array ty n))
∧ (∀ty. native_safe tmap (UPtr ty))
∧ (∀sig. native_safe tmap (UFuncPtr sig))
∧ (∀ftys vty.
(∀ty. MEM ty ftys ⇒ native_safe tmap ty)
∧ native_safe tmap vty
⇒
native_safe tmap (Hybrid ftys vty))
∧ (∀fields.
(∀ty. MEM (INL ty) fields ⇒ native_safe tmap ty)
∧ (∀ty_name ty.
SOME ty = FLOOKUP tmap ty_name
∧ MEM (INR ty_name) fields
⇒
native_safe tmap ty)
⇒
native_safe tmap (Struct fields))
`
(* The set of well-formed types. The type of this relation is
(typename |-> uvm_type) ->
typename set ->
uvm_type set
The first parameter is the global type name map (for recursively-defined
structs), and the second parameter is an accumulator of previously-followed
typenames (also for recursively-defined structs). The second parameter is
only used for recursion, and therefore should always be the empty set.
*)
val (wftype_rules, wftype_ind, wftype_cases) = Hol_reln`
(∀vset n.
0 < n
⇒
wftype tmap vset (Int n))
∧ (∀vset. wftype tmap vset Float)
∧ (∀vset. wftype tmap vset Double)
∧ (∀vset. wftype tmap vset Void)
∧ (∀vset. wftype tmap vset ThreadRef)
∧ (∀vset. wftype tmap vset StackRef)
∧ (∀vset. wftype tmap vset TagRef64)
∧ (∀vset ty.
wftype tmap vset ty
⇒
wftype tmap vset (Ref ty))
∧ (∀vset ty.
wftype tmap vset ty
⇒
wftype tmap vset (IRef ty))
∧ (∀vset ty.
wftype tmap vset ty
⇒
wftype tmap vset (WeakRef ty))
∧ (∀vset ty.
¬tracedtype tmap ty
∧ wftype tmap vset ty
⇒
wftype tmap vset (UPtr ty))
∧ (∀vset sig.
(∀ty. MEM ty sig.arg_types ⇒ wftype tmap vset ty)
∧ (∀ty. MEM ty sig.return_types ⇒ wftype tmap vset ty)
⇒
wftype tmap vset (FuncRef sig))
∧ (∀vset sig.
(∀ty. MEM ty sig.arg_types ⇒ wftype tmap vset ty)
∧ (∀ty. MEM ty sig.return_types ⇒ wftype tmap vset ty)
⇒
wftype tmap vset (UFuncPtr sig))
∧ (∀vset sz ty.
0 < sz ∧ wftype tmap vset ty ∧ ty ≠ Void
⇒
wftype tmap vset (Array ty sz))
∧ (∀vset sz ty.
0 < sz ∧ wftype tmap vset ty ∧ scalar_type ty
⇒
wftype tmap vset (Vector ty sz))
∧ (∀vset fixtys varty.
(∀ty. MEM ty fixtys ⇒ wftype tmap vset ty)
∧ wftype tmap vset varty
∧ varty ≠ Void
⇒
wftype tmap vset (Hybrid fixtys varty))
∧ (∀vset fields.
fields ≠ []
∧ (∀ty. MEM (INL ty) fields ⇒ wftype tmap vset ty ∧ ty ≠ Void)
∧ (∀ty_name ty.
MEM (INR ty_name) fields
⇒
SOME ty = FLOOKUP tmap ty_name
∧ ty ≠ Void
∧ (ty_name ∈ vset ∨ wftype tmap (ty_name INSERT vset) ty))
⇒
wftype tmap vset (Struct tag))
`
(* Note that FuncRefs are not native_safe, but are not traced either, so the
reverse implication doesn't hold *)
val native_safe_nottraced = store_thm(
"native_safe_traced",
``∀vset ty.
wftype sm vset ty ⇒ native_safe sm ty ⇒ ¬tracedtype sm ty``,
Induct_on `wftype` >> rpt conj_tac >> strip_tac >>
TRY (simp[Once native_safe_cases, Once tracedtype_cases] >> NO_TAC) >>
TRY (ntac 2 strip_tac >>
simp[Once native_safe_cases, Once tracedtype_cases] >> NO_TAC) >>
TRY (ntac 3 strip_tac >>
simp[Once native_safe_cases, Once tracedtype_cases] >> NO_TAC)
>- ((* hybrid *)
ntac 3 strip_tac >>
dsimp[Once native_safe_cases, Once tracedtype_cases] >> rpt strip_tac >>
metis_tac[])
>- ((* struct *)
ntac 2 strip_tac >>
dsimp[Once native_safe_cases, Once tracedtype_cases] >> rpt strip_tac >>
metis_tac[]))
(* ----------------------------------------------------------------------
canconvert pair
---------------------------------------------------------------------- *)
val _ = Datatype`
convtype =
| TRUNC
| ZEXT
| SEXT
| FPTRUNC
| FPEXT
| FPTOUI
| FPTOSI
| UITOFP
| SITOFP
| BITCAST
| REFCAST
| PTRCAST
`
(* canconvert M convtype (src : uvm_type) (tgt : uvm_type) *)
val (canconvert_rules, canconvert_ind, canconvert_cases) = Hol_reln`
(∀m n. m ≤ n ⇒ canconvert M Smap TRUNC (Int n) (Int m))
∧ (∀m n. m ≤ n ⇒ canconvert M Smap ZEXT (Int m) (Int n))
∧ (∀m n. m ≤ n ⇒ canconvert M Smap SEXT (Int m) (Int n))
∧ canconvert M Smap FPTRUNC Double Float
∧ canconvert M Smap FPEXT Float Double
∧ (∀ty n. fpType ty ⇒ canconvert M Smap FPTOUI ty (Int n))
∧ (∀ty n. fpType ty ⇒ canconvert M Smap FPTOSI ty (Int n))
∧ (∀ty n. fpType ty ⇒ canconvert M Smap UITOFP (Int n) ty)
∧ (∀ty n. fpType ty ⇒ canconvert M Smap SITOFP (Int n) ty)
∧ canconvert M Smap BITCAST (Int M.floatsize) Float
∧ canconvert M Smap BITCAST Float (Int M.floatsize)
∧ canconvert M Smap BITCAST (Int M.doublesize) Double
∧ canconvert M Smap BITCAST Double (Int M.doublesize)
(* ptrcast cases *)
∧ (∀n ty.
M.ptrsize = n
⇒
canconvert M Smap BITCAST (Int n) (UPtr ty))
∧ (∀n ty.
M.ptrsize = n (* tell KW *)
⇒
canconvert M Smap BITCAST (UPtr ty) (Int n))
(* refcast cases *)
∧ (∀ty. canconvert M Smap BITCAST (Ref Void) (Ref ty))
∧ (∀ty. canconvert M Smap BITCAST (Ref ty) (Ref Void))
∧ (∀ty. canconvert M Smap BITCAST (Ref Void) (Ref ty))
∧ (∀tag1 tag2.
tag1 ∈ FDOM Smap ∧ tag2 ∈ FDOM Smap
∧ (Smap ' tag1 <<= Smap ' tag2 ∨ Smap ' tag2 <<= Smap ' tag1)
⇒
canconvert M Smap BITCAST (Ref (Struct tag1)) (Ref (Struct tag2)))
`
val _ = export_theory()