-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemOpsFuncs.v
186 lines (156 loc) · 5.41 KB
/
MemOpsFuncs.v
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
Require Import Kami.AllNotations.
Require Import ProcKami.FU.
Import ListNotations.
Section memOpsFuncs.
Context {procParams : ProcParams}.
Local Open Scope kami_expr.
(* specifies the value stored in the destination register by a memory operation. *)
Inductive MemRegValue
:= memRegValueFn : (forall ty, Data @# ty -> Data ## ty) -> MemRegValue
| memRegValueNone.
Definition isMemRegValueFn (x : MemRegValue) : bool
:= match x with
| memRegValueFn _ => true
| _ => false
end.
(* specifies the value written to memory by a memory operation. *)
Inductive MemWriteValue : Type
:= memWriteValueFn : (forall ty, Data @# ty -> Data @# ty -> Data ## ty) -> MemWriteValue
| memWriteValueStore (* write register value *)
| memWriteValueNone.
Definition isMemWriteValueFn (x : MemWriteValue) : bool
:= match x with
| memWriteValueFn _ => true
| _ => false
end.
Inductive PmaAmoClass := AmoNone | AmoSwap | AmoLogical | AmoArith.
Record Pma
:= {
pmaWidth : nat; (* in bytes *)
pmaReadable : bool;
pmaWriteable : bool;
pmaExecutable : bool;
pmaMisaligned : bool
}.
Definition pmas_default
:= map
(fun x
=> {|
pmaWidth := x;
pmaReadable := true;
pmaWriteable := true;
pmaExecutable := true;
pmaMisaligned := true
|})
[0; 1; 2; 3].
Record MemOp := {
memOpName : MemOpName;
memOpCode : N;
memOpSize : nat;
memOpAmoClass : PmaAmoClass;
memOpRegValue : MemRegValue;
memOpWriteValue : MemWriteValue
}.
Definition getMask (size: nat) ty
: DataMask @# ty
:= unpack DataMask
($(Nat.pow 2 (Nat.pow 2 size) - 1)).
Definition getMaskExpr ty n
(sz : Bit n @# ty)
: Bit (size DataMask) @# ty
:= ($1 << (($1 : Bit (S (Nat.log2_up Rlen_over_8)) @# ty) << sz)) - $1.
Definition getShiftAmt ty n m
(sz : Bit n @# ty)
(addr : Bit m @# ty)
: Bit 3 @# ty
:= ((ZeroExtendTruncLsb 3 addr) >> sz) << sz.
Definition getSize ty (req : MemOpCode @# ty) :=
UniBit (TruncLsb TlSizeSz TlParamSz)
(UniBit (TruncLsb (TlSizeSz + TlParamSz) TlOpcodeSz)
req).
Definition getByteShiftAmt ty n m
(sz : Bit n @# ty)
(addr : Bit m @# ty)
: Bit 6 @# ty
:= ({< getShiftAmt sz addr,
Const _ (natToWord 3 0) >}).
Section memOps.
Variable memOps : list MemOp.
Definition applyMemOpByName
(t : Type)
(f : MemOp -> t)
(default : t)
(name : MemOpName)
: t
:= match find (fun memOp => memOpNameEqb name (memOpName memOp)) memOps with
| None => default
| Some memOp => f memOp
end.
Section ty.
Variable ty : Kind -> Type.
Definition getMemOpCode
: MemOpName -> MemOpCode @# ty
:= applyMemOpByName
(fun memOp => $(N.to_nat (memOpCode memOp)) : MemOpCode @# ty)
$0.
Local Open Scope kami_action.
Definition applyMemOp
(k : Kind)
(f : MemOp -> ActionT ty k)
(code : MemOpCode @# ty)
: ActionT ty k
:= LETA result
: Maybe k
<- utila_acts_find_pkt
(map
(fun memOp
=> If code == $(N.to_nat (memOpCode memOp))
then
LETA result : k <- f memOp;
Ret (Valid #result : Maybe k @# ty)
else
Ret (Invalid : Maybe k @# ty)
as result;
Ret #result)
memOps);
Ret (#result @% "data").
Definition memOpHasLoad
: MemOpCode @# ty -> ActionT ty Bool
:= applyMemOp
(fun memOp => Ret $$(isMemRegValueFn (memOpRegValue memOp))).
Local Close Scope kami_action.
Section func_units.
Variable func_units : list FUEntry.
Local Open Scope kami_action.
Definition applyMemInst
(k : Kind)
(f : forall t u, InstEntry t u -> MemOp -> k ## ty)
: FuncUnitId func_units @# ty -> InstId func_units @# ty -> k ## ty
:= applyInst
(fun t u inst
=> match optMemParams inst with
| Some name
=> match find (fun memOp => memOpNameEqb name (memOpName memOp)) memOps with
| Some memOp => f t u inst memOp
| None => RetE $$(getDefaultConst k) (* impossible case. *)
end
| None => RetE $$(getDefaultConst k) (* impossible case. *)
end).
Definition getRegValue
(code : MemOpCode @# ty)
(memData : Data @# ty)
: ActionT ty Data
:= applyMemOp
(fun memOp
=> match memOpRegValue memOp return ActionT ty Data with
| memRegValueFn f => (LETA result : Data <- convertLetExprSyntax_ActionT (f ty memData);
Ret #result)
| memRegValueNone => Ret memData
end)
code.
Local Close Scope kami_action.
End func_units.
End ty.
End memOps.
Local Close Scope kami_expr.
End memOpsFuncs.