Repository navigation
Expand file tree
/
Copy pathInstructions.v
More file actions
215 lines (188 loc) · 6.18 KB
/
Copy pathInstructions.v
File metadata and controls
215 lines (188 loc) · 6.18 KB
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
(* Machine instructions *)
Require Import ZArith.
From mathcomp Require Import ssreflect ssrfun ssrbool ssrnat eqtype seq.
Require Import Utils.
Definition regId := Z.
Canonical regId_eqType :=
Eval hnf in EqType regId [eqMixin of Z].
Inductive BinOpT : Type :=
| BAdd
| BMult
| BJoin
| BFlowsTo
| BEq.
Scheme Equality for BinOpT.
Section Instr.
Context {Label : Type}.
Inductive Instr : Type :=
(* basic instructions *)
| Put (n : Z) : regId -> Instr
| Mov : regId -> regId -> Instr
| Load : regId -> regId -> Instr
| Store : regId -> regId -> Instr
| Write : regId -> regId -> Instr
| BinOp (o : BinOpT) : regId -> regId -> regId -> Instr
| Nop : Instr
| Halt : Instr
| Jump : regId -> Instr
| BNZ (n : Z) : regId -> Instr
| BCall : regId -> regId -> regId -> Instr
| BRet : Instr
(* public first-class labels *)
| Lab : regId -> regId -> Instr
| PcLab : regId -> Instr
| PutLab : Label -> regId -> Instr
(* dynamic memory allocation *)
| Alloc : regId -> regId -> regId -> Instr
| PGetOff : regId -> regId -> Instr
| PSetOff : regId -> regId -> regId -> Instr
| MSize : regId -> regId -> Instr
| MLab : regId -> regId -> Instr.
Inductive OpCode : Type :=
| OpPut
| OpMov
| OpLoad
| OpStore
| OpWrite
| OpBinOp
| OpNop
| OpJump
| OpBNZ
| OpBCall
| OpBRet
(* missing for Halt *)
| OpLab
| OpPcLab
| OpPutLab
| OpAlloc
| OpPGetOff
| OpPSetOff
| OpMSize
| OpMLab.
Definition opCodes :=
[::
OpPut
; OpMov
; OpLoad
; OpStore
; OpWrite
; OpBinOp
; OpNop
; OpJump
; OpBNZ
; OpBCall
; OpBRet
; OpLab
; OpPcLab
; OpPutLab
; OpAlloc
; OpPGetOff
; OpPSetOff
; OpMSize
; OpMLab ].
Definition opCode_eq_dec : forall o1 o2 : OpCode,
{o1 = o2} + {o1 <> o2}.
Proof. decide equality. Defined.
Lemma opCode_eq_decP : Equality.axiom opCode_eq_dec.
Proof. by move=> o1 o2; apply: sumboolP. Qed.
Definition opCode_eqMixin := EqMixin opCode_eq_decP.
Canonical opCode_eqType := Eval hnf in EqType OpCode opCode_eqMixin.
Lemma opCodes_correct : forall o : OpCode, o \in opCodes.
Proof. by case. Qed.
Definition opcode_of_instr (i : Instr) : option OpCode :=
match i with
| Put _ _ => Some OpPut
| Mov _ _ => Some OpMov
| Load _ _ => Some OpLoad
| Store _ _ => Some OpStore
| Write _ _ => Some OpWrite
| BinOp _ _ _ _ => Some OpBinOp
| Nop => Some OpNop
| Halt => None (* CH: halt has no opcode? why? *)
| Jump _ => Some OpJump
| BNZ _ _ => Some OpBNZ
| BCall _ _ _ => Some OpBCall
| BRet => Some OpBRet
| Lab _ _ => Some OpLab
| PcLab _ => Some OpPcLab
| PutLab _ _ => Some OpPutLab
| Alloc _ _ _ => Some OpAlloc
| PGetOff _ _ => Some OpPGetOff
| PSetOff _ _ _ => Some OpPSetOff
| MSize _ _ => Some OpMSize
| MLab _ _ => Some OpMLab
end.
End Instr.
Lemma BinOpT_beqP : Equality.axiom BinOpT_beq.
Proof.
move=> x y.
apply/(iffP idP); first exact: internal_BinOpT_dec_bl.
exact: internal_BinOpT_dec_lb.
Qed.
Definition BinOpT_eqMixin := EqMixin BinOpT_beqP.
Canonical BinOpT_eqType := Eval hnf in EqType _ BinOpT_eqMixin.
Definition instr_eqb (T : eqType) (i1 i2 : @Instr T) : bool :=
match i1, i2 with
| Put n1 r1, Put n2 r2 => [&& n1 == n2 & r1 == r2]
| Mov r11 r12, Mov r21 r22 => [&& r11 == r21 & r12 == r22]
| Load r11 r12, Load r21 r22 => [&& r11 == r21 & r12 == r22]
| Store r11 r12, Store r21 r22 => [&& r11 == r21 & r12 == r22]
| Write r11 r12, Write r21 r22 => [&& r11 == r21 & r12 == r22]
| BinOp o1 r11 r12 r13, BinOp o2 r21 r22 r23 => [&& o1 == o2, r11 == r21, r12 == r22 & r13 == r23]
| Nop, Nop => true
| Halt, Halt => true
| Jump r1, Jump r2 => r1 == r2
| BNZ o1 r1, BNZ o2 r2 => [&& o1 == o2 & r1 == r2]
| BCall r11 r12 r13, BCall r21 r22 r23 => [&& r11 == r21, r12 == r22 & r13 == r23]
| BRet, BRet => true
| Lab r11 r12, Lab r21 r22 => [&& r11 == r21 & r12 == r22]
| PcLab r1, PcLab r2 => r1 == r2
| PutLab l1 r1, PutLab l2 r2 => [&& l1 == l2 & r1 == r2]
| Alloc r11 r12 r13, Alloc r21 r22 r23 => [&& r11 == r21, r12 == r22 & r13 == r23]
| PGetOff r11 r12, PGetOff r21 r22 => [&& r11 == r21 & r12 == r22]
| PSetOff r11 r12 r13, PSetOff r21 r22 r23 => [&& r11 == r21, r12 == r22 & r13 == r23]
| MSize r11 r12, MSize r21 r22 => [&& r11 == r21 & r12 == r22]
| MLab r11 r12, MLab r21 r22 => [&& r11 == r21 & r12 == r22]
| _, _ => false
end.
Lemma instr_eqbP T : Equality.axiom (instr_eqb T).
Proof.
move=> i1 i2.
apply/(iffP idP); case: i1; case: i2 => //=.
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? ? ? ? ? /and4P [/eqP -> /eqP -> /eqP -> /eqP ->].
+ by move=> ? ? /eqP ->.
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? ? ? /and3P [/eqP -> /eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? /eqP ->.
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? ? ? /and3P [/eqP -> /eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? ? ? /and3P [/eqP -> /eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? /andP [/eqP -> /eqP ->].
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? ? ? ? ? [-> -> -> ->]; rewrite !eqxx.
+ by move=> ? ? [->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? ? ? [-> -> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? [->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? ? ? [-> -> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? ? ? [-> -> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
+ by move=> ? ? ? ? [-> ->]; rewrite !eqxx.
Qed.
Definition instr_eqMixin T := EqMixin (instr_eqbP T).
Canonical instr_eqType T := Eval hnf in EqType _ (instr_eqMixin T).