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
/
Copy pathuvmSystemScript.sml
185 lines (153 loc) · 4.51 KB
/
uvmSystemScript.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
open HolKernel Parse boolLib bossLib;
open uvmMemoryModelTheory;
open uvmThreadSemanticsTheory;
open lcsymtacs
open monadsyntax
open uvmIRTheory;
val _ = new_theory "uvmSystem";
val machine_state_def = Datatype`
machine_state = <|
g : graph;
threads : running_thread list
|>
`;
val _ = Datatype`
msstep_result = SuccessM α | AbortM | BlockedM
`
(* set up MSM, a monad *)
val _ = type_abbrev(
"MSM",
``:machine_state -> (α # machine_state) msstep_result``)
val MSUNIT_def = Define`
MSUNIT (x:α) :α MSM = λms. SuccessM (x, ms)
`;
val _ = overload_on ("return", ``MSUNIT``);
val MSBIND_def = Define`
MSBIND (x : α MSM) (f : α -> β MSM) : β MSM =
λms0.
case x ms0 of
| SuccessM (a, ms1) => (
case f a ms1 of
| SuccessM(b, ms2) => SuccessM(b, ms2)
| r => r)
| AbortM => AbortM
| BlockedM => BlockedM
`;
val _ = overload_on ("monad_bind", ``MSBIND``)
val _ = overload_on ("monad_unitbind", ``λm1 m2. MSBIND m1 (K m2)``)
val MSGET_def = Define`
MSGET : machine_state MSM = λms. SuccessM(ms, ms)
`
val read_var_def = Define`
read_var (x : num) : (graph) MSM =
do
ms <- MSGET ;
return ms.g
od
`;
val eval_exp_def = Define`
eval_exp (e:num) : (value # value) MSM =
return ((IntV 32 1w), (IntV 32 8w))
`
val valbind_def = Define`
valbind : unit MSM =
do
λms. SuccessM((), ms)
od
`;
val find_node_fun_def = Define`
find_node_fun list (n,tid) =
case list of
| [] => NONE
| x::xs => if (x.mid = n ∧ x.thread_id = tid) then SOME x else find_node_fun xs (n,tid)
`;
val find_node_def = Define`
find_node (n,tid) : (node option) MSM =
do
ms <- MSGET ;
return (find_node_fun (ms.g.nodes) (n,tid))
od
`;
val resolveM_def = Define`
resolveM R W : memory_response MSM =
λms. SuccessM (
(R.mid, [THE W.values]),
ms with g updated_by (λgr. gr with rf updated_by (λr. r|+(R,W))))
`;
val list_update_def = Define`
list_update list index v =
case list of
| [] => []
| x::xs => if (index=0) then (v::xs) else (x::(list_update xs (index-1) v))
`;
val thread_receiveM_def = Define`
thread_receiveM msg tid : unit MSM =
λms. SuccessM ((), <| g := ms.g ; threads := list_update ms.threads tid (thread_receive (EL tid ms.threads) msg) |>)
`;
val get_result_def = Define`
get_result (r:(unit # thread_state # memory_message list) tsstep_result) : (unit # thread_state # memory_message list) MSM =
case r of
| Success α => return α
`;
val run_inst_def = Define`
run_inst inst t1 : (memory_message list) MSM =
do
ms0 <- MSGET ;
(a,ts1,msg) <- get_result (exec_inst inst (EL t1 ms0.threads)) ;
λms. SuccessM ( msg, <| g:= ms.g ; threads := list_update ms.threads t1 ts1 |>)
od
`;
type_of(``get_result(exec_inst inst (EL t1 ms.threads))``);
type_of(``SuccessM ( msg, <| g:= ms.g ; threads := list_update ms.threads t1 ts1 |>)``);
val receiveH_def = Define`
receiveH inGraph (msg,ttid) =
case msg of
| MemLoad load =>
let new_node = <|
operation := Rd ;
address := load.addr ; values := NONE ;
mid := load.id ; thread_id := ttid ;
order := load.order ; ddeps := load.memdeps
|>
in inGraph with nodes updated_by (λlst. lst ++ [new_node])
| MemStore store =>
let new_node = <|
operation := Wr ;
address := load.addr ; values := SOME load.value ;
mid := load.id ; thread_id := ttid ;
order := load.order ; ddeps := load.memdeps
|>
in inGraph with nodes updated_by (λlst. lst ++ [new_node])
`;
val receive_list_def = Define`
receive_list g (messages,tid) =
case messages of
| [] => g
| m::rest => receive_list (receiveH g (m,tid)) (rest,tid)
`;
val receiveM_def = Define`
receiveM messages tid : unit MSM =
λms. SuccessM ((), <| g := receive_list ms.g (messages,tid) ; threads := ms.threads |>)
`;
val _ = Datatype`
command =
| read (memreqid # tid) (memreqid # tid)
| eval (instruction # tid)
`;
val machine_step_def = Define`
machine_step com : unit MSM =
case com of
| read (r,t1) (w,t2) =>
do
read <- find_node (r,t1) ;
write <- find_node (w,t2) ;
msg <- resolveM (THE read) (THE write) ;
thread_receiveM msg t1
od
| eval (inst, t1) =>
do
messages <- run_inst inst t1;
receiveM messages t1
od
`;
val _ = export_theory();