-
Notifications
You must be signed in to change notification settings - Fork 86
/
time_to_panScript.sml
341 lines (282 loc) · 8.36 KB
/
time_to_panScript.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
(*
Compilation from timeLang to panLang
*)
open preamble pan_commonTheory mlintTheory
timeLangTheory panLangTheory
val _ = new_theory "time_to_pan"
val _ = set_grammar_ancestry
["pan_common", "mlint", "timeLang", "panLang"];
Definition ohd_def:
(ohd [] = (0:num,[])) ∧
(ohd (x::xs) = x)
End
Definition ffiBufferAddr_def:
ffiBufferAddr = 4000w:'a word
End
Definition ffiBufferSize_def:
ffiBufferSize = (bytes_in_word + bytes_in_word): 'a word
End
Definition genShape_def:
genShape n = Comb (REPLICATE n One)
End
Definition mkClks_def:
mkClks vname n = REPLICATE n (Var vname)
End
Definition emptyConsts_def:
emptyConsts n = REPLICATE n (Const 0w)
End
Definition indicesOf_def:
indicesOf xs ys = MAP (λn. findi n xs) ys
End
Definition resetTermClocks_def:
resetTermClocks v clks tclks =
MAPi (λn e.
if (MEM e tclks)
then (Const 0w)
else Field n (Var v))
clks
End
Definition waitTimes_def:
waitTimes =
list$MAP2 (λt e. Op Sub [Const (n2w t); e])
End
Definition minOp_def:
(minOp v [] = Skip) ∧
(minOp v (e::es) =
Seq (If (Cmp Lower e (Var v)) (Assign v e) Skip)
(minOp v es))
End
Definition minExp_def:
(minExp v [] = Skip) ∧
(minExp v (e::es) = Seq (Assign v e) (minOp v es))
End
Definition compTerm_def:
(compTerm (clks:mlstring list) (Tm io cnds tclks loc wt)) : 'a prog =
let waitClks = indicesOf clks (MAP SND wt);
return = Return
(Struct
[Var «newClks»; Var «waitSet»;
Var «wakeUpAt»; Label (num_to_str loc)])
in
decs [
(«waitSet», case wt of [] => Const 1w | wt => Const 0w); (* not waitSet *)
(«wakeUpAt», Const 0w);
(«newClks», Struct (resetTermClocks «clks» clks tclks));
(«waitTimes», Struct (emptyConsts (LENGTH wt)))
]
(nested_seq
[Assign «waitTimes»
(Struct (
waitTimes
(MAP FST wt)
(MAP (λn. Field n (Var «newClks»)) waitClks)));
minExp «wakeUpAt» (MAPi (λn wt. Field n (Var «waitTimes»)) wt);
case io of
| (Input insig) => return
| (Output outsig) =>
decs
[(«ptr1»,Const 0w);
(«len1»,Const 0w);
(«ptr2»,BaseAddr (* Const ffiBufferAddr *));
(«len2»,Const ffiBufferSize)
] (Seq
(ExtCall (num_to_str outsig) (Var «ptr1») (Var «len1») (Var «ptr2») (Var «len2»))
return)
])
End
Definition compExp_def:
(compExp _ _ (ELit t) = Const (n2w t)) ∧
(compExp clks vname (EClock clk) =
Field (findi clk clks) (Var vname)) ∧
(compExp clks vname (ESub e1 e2) =
Op Sub [compExp clks vname e1;
compExp clks vname e2])
End
Definition compCondition_def:
(compCondition clks vname (CndLt e1 e2) =
Cmp Lower
(compExp clks vname e1)
(compExp clks vname e2)) ∧
(compCondition clks vname (CndLe e1 e2) =
Op Or [Cmp Lower
(compExp clks vname e1)
(compExp clks vname e2);
Cmp Equal
(compExp clks vname e1)
(compExp clks vname e2)])
End
Definition compConditions_def:
(compConditions clks vname [] = Const 1w) ∧
(compConditions clks vname cs =
Op And (MAP (compCondition clks vname) cs))
End
Definition compAction_def:
(compAction (Output _) = Const 0w) ∧
(compAction (Input n) = Const (n2w (n + 1)))
End
Definition event_match_def:
event_match vname act = Cmp Equal (Var vname) (compAction act)
End
Definition pick_term_def:
pick_term clks cname ename cds act =
Op And
[compConditions clks cname cds;
event_match ename act]
End
Definition compTerms_def:
(compTerms clks cname ename [] = Raise «panic» (Const 0w)) ∧
(compTerms clks cname ename (t::ts) =
let
cds = termConditions t;
act = termAction t
in
If (pick_term clks cname ename cds act)
(compTerm clks t)
(compTerms clks cname ename ts))
End
Definition compLocation_def:
compLocation clks (loc,ts) =
let n = LENGTH clks in
(num_to_str loc,
[(«clks», genShape n);
(«event», One)],
compTerms clks «clks» «event» ts)
End
Definition compProg_def:
(compProg clks [] = []) ∧
(compProg clks (p::ps) =
compLocation clks p :: compProg clks ps)
End
Definition comp_def:
comp prog =
compProg (clksOf prog) prog
End
Definition fieldsOf_def:
fieldsOf e n =
MAP (λn. Field n e) (GENLIST I n)
End
Definition normalisedClks_def:
normalisedClks v1 v2 n =
MAP2 (λx y. Op Sub [x;y])
(mkClks v1 n)
(fieldsOf (Var v2) n)
End
Definition check_input_time_def:
check_input_time =
let time = Load One (Var «ptr2»);
input = Load One
(Op Add [Var «ptr2»;
Const bytes_in_word])
in
nested_seq [
ExtCall «get_time_input» (Var «ptr1») (Var «len1») (Var «ptr2») (Var «len2») ;
Assign «sysTime» time ;
Assign «event» input;
Assign «isInput» (Cmp Equal input (Const 0w));
If (Cmp Equal (Var «sysTime») (Const (n2w (dimword (:α) - 1))))
(Return (Const 0w)) (Skip:'a prog)
]
End
Definition wait_def:
wait =
Op And [Var «isInput»; (* Not *)
Op Or
[Var «waitSet»; (* Not *)
Cmp NotEqual (Var «sysTime») (Var «wakeUpAt»)]]
End
Definition wait_input_time_limit_def:
wait_input_time_limit =
While wait check_input_time
End
Definition task_controller_def:
task_controller clksLength =
let
rt = Var «taskRet» ;
nClks = Field 0 rt;
nWaitSet = Field 1 rt;
nwakeUpAt = Field 2 rt;
nloc = Field 3 rt
in
(nested_seq [
wait_input_time_limit;
If (Cmp Equal (Var «sysTime») (Const (n2w (dimword (:α) - 2))))
check_input_time (Skip:'a prog);
Call (SOME (SOME «taskRet», NONE)) (Var «loc»)
[Struct (normalisedClks «sysTime» «clks» clksLength);
Var «event»];
Assign «clks» nClks;
Assign «clks» (Struct (normalisedClks «sysTime» «clks» clksLength));
Assign «waitSet» nWaitSet ;
Assign «wakeUpAt» (Op Add [Var «sysTime»; nwakeUpAt]);
Assign «loc» nloc;
Assign «isInput» (Const 1w);
Assign «event» (Const 0w)])
End
Definition always_def:
always clksLength =
While (Const 1w)
(task_controller clksLength)
End
Definition start_controller_def:
start_controller (ta_prog:program) =
let
prog = FST ta_prog;
initLoc = FST (ohd prog);
initWakeUp = SND ta_prog;
clksLength = nClks prog
in
decs
[(«loc», Label (num_to_str initLoc));
(«waitSet»,
case initWakeUp of NONE => Const 1w | _ => Const 0w); (* not waitSet *)
(«event», Const 0w);
(«isInput», Const 1w); (* not isInput, active low *)
(«wakeUpAt», Const 0w);
(«sysTime», Const 0w);
(«ptr1», Const 0w);
(«len1», Const 0w);
(«ptr2», BaseAddr (* Const ffiBufferAddr) *));
(«len2», Const ffiBufferSize);
(«taskRet»,
Struct [Struct (emptyConsts clksLength);
Const 0w; Const 0w; Label (num_to_str initLoc)]);
(«clks»,Struct (emptyConsts clksLength))
]
(nested_seq
[
check_input_time;
Assign «clks» (Struct (mkClks «sysTime» clksLength));
Assign «wakeUpAt»
(case initWakeUp of
| NONE => Var «sysTime»
| SOME n => Op Add [Var «sysTime»; Const (n2w n)]);
always clksLength
])
End
Definition ta_controller_def:
ta_controller (ta_prog:program) =
decs
[
(«retvar», Const 0w);
(«excpvar», Const 0w)
]
(nested_seq
[
Call (SOME (SOME «retvar»,
(SOME («panic», «excpvar», (Return (Const 1w))))))
(Label «start_controller»)
[];
Return (Const 0w)
])
End
Definition compile_prog_def:
compile_prog prog =
let i = («start»,[],start_controller prog) in
let clks = clksOf (FST prog) in
let n = LENGTH clks in
i :: MAP (λ(loc,tms).
(num_to_str loc,
[(«clks», genShape n); («event», One)],
compTerms clks «clks» «event» tms)) (FST prog)
End
val _ = export_theory();