Debian Codegraph @

src/Assembly/Equality.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import NArith.
3 From Coq Require Import String.
4 From Coq Require Import List.
5 From Coq Require Import Morphisms.
6 From Coq Require Import Bool.
7 Require Import Crypto.Util.Option.
8 Require Import Crypto.Util.Bool.
9 Require Import Crypto.Util.Bool.Reflect.
10 Require Import Crypto.Util.ListUtil.
11 Require Import Crypto.Util.Tactics.DestructHead.
12 Require Import Crypto.Util.Notations.
13 Require Import Crypto.Util.Prod.
14 Require Import Crypto.Assembly.Syntax.
15
16 Local Ltac t_dec :=
17 lazymatch goal with
18 | [ |- ?beq ?x ?y = true -> ?x = ?y ]
19 => is_var x; is_var y; destruct x, y; cbv [beq]
20 | [ |- ?x = ?y -> ?beq ?x ?y = true ]
21 => is_var x; is_var y; destruct x; cbv [beq]; intros; subst
22 end;
23 simpl;
24 rewrite ?Bool.andb_true_iff; intros; destruct_head'_and;
25 repeat apply conj; try reflexivity;
26 reflect_hyps; subst;
27 try solve [ reflexivity
28 | exfalso; assumption
29 | apply unreflect_bool; reflexivity ].
30
31 Declare Scope REG_scope.
32 Delimit Scope REG_scope with REG.
33 Bind Scope REG_scope with REG.
34
35 Infix "=?" := REG_beq : REG_scope.
36
37 Global Instance REG_beq_spec : reflect_rel (@eq REG) REG_beq | 10
38 := reflect_of_beq REG_dec_bl REG_dec_lb.
39 Definition REG_beq_eq x y : (x =? y)%REG = true <-> x = y := conj (@REG_dec_bl _ _) (@REG_dec_lb _ _).
40 Lemma REG_beq_neq x y : (x =? y)%REG = false <-> x <> y.
41 Proof. rewrite <- REG_beq_eq; destruct (x =? y)%REG; intuition congruence. Qed.
42 Global Instance REG_beq_compat : Proper (eq ==> eq ==> eq) REG_beq | 10.
43 Proof. repeat intro; subst; reflexivity. Qed.
44
45 Definition CONST_beq : CONST -> CONST -> bool := Z.eqb.
46 Definition CONST_dec_bl (x y : CONST) : CONST_beq x y = true -> x = y := proj1 (Z.eqb_eq x y).
47 Definition CONST_dec_lb (x y : CONST) : x = y -> CONST_beq x y = true := proj2 (Z.eqb_eq x y).
48 Definition CONST_eq_dec (x y : CONST) : {x = y} + {x <> y} := Z.eq_dec x y.
49
50 Declare Scope CONST_scope.
51 Delimit Scope CONST_scope with CONST.
52 Bind Scope CONST_scope with CONST.
53
54 Infix "=?" := CONST_beq : CONST_scope.
55
56 Global Instance CONST_beq_spec : reflect_rel (@eq CONST) CONST_beq | 10
57 := reflect_of_beq CONST_dec_bl CONST_dec_lb.
58 Definition CONST_beq_eq x y : (x =? y)%CONST = true <-> x = y := conj (@CONST_dec_bl _ _) (@CONST_dec_lb _ _).
59 Lemma CONST_beq_neq x y : (x =? y)%CONST = false <-> x <> y.
60 Proof. rewrite <- CONST_beq_eq; destruct (x =? y)%CONST; intuition congruence. Qed.
61 Global Instance CONST_beq_compat : Proper (eq ==> eq ==> eq) CONST_beq | 10.
62 Proof. repeat intro; subst; reflexivity. Qed.
63
64 Declare Scope JUMP_LABEL_scope.
65 Delimit Scope JUMP_LABEL_scope with JUMP_LABEL.
66 Bind Scope JUMP_LABEL_scope with JUMP_LABEL.
67
68 Definition JUMP_LABEL_beq (x y : JUMP_LABEL) : bool
69 := ((Bool.eqb x.(jump_near) y.(jump_near))
70 && (x.(label_name) =? y.(label_name))%string).
71 Global Arguments JUMP_LABEL_beq !_ !_ / .
72
73 Infix "=?" := JUMP_LABEL_beq : JUMP_LABEL_scope.
74 Lemma JUMP_LABEL_dec_bl (x y : JUMP_LABEL) : JUMP_LABEL_beq x y = true -> x = y.
75 Proof. t_dec. Qed.
76 Lemma JUMP_LABEL_dec_lb (x y : JUMP_LABEL) : x = y -> JUMP_LABEL_beq x y = true.
77 Proof. t_dec. Qed.
78 Definition JUMP_LABEL_eq_dec (x y : JUMP_LABEL) : {x = y} + {x <> y}
79 := (if (x =? y)%JUMP_LABEL as b return (x =? y)%JUMP_LABEL = b -> _
80 then fun pf => left (@JUMP_LABEL_dec_bl _ _ pf)
81 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@JUMP_LABEL_dec_lb _ _ pf'))))
82 eq_refl.
83
84 Global Instance JUMP_LABEL_beq_spec : reflect_rel (@eq JUMP_LABEL) JUMP_LABEL_beq | 10
85 := reflect_of_beq JUMP_LABEL_dec_bl JUMP_LABEL_dec_lb.
86 Definition JUMP_LABEL_beq_eq x y : (x =? y)%JUMP_LABEL = true <-> x = y := conj (@JUMP_LABEL_dec_bl _ _) (@JUMP_LABEL_dec_lb _ _).
87 Lemma JUMP_LABEL_beq_neq x y : (x =? y)%JUMP_LABEL = false <-> x <> y.
88 Proof. rewrite <- JUMP_LABEL_beq_eq; destruct (x =? y)%JUMP_LABEL; intuition congruence. Qed.
89 Global Instance JUMP_LABEL_beq_compat : Proper (eq ==> eq ==> eq) JUMP_LABEL_beq | 10.
90 Proof. repeat intro; subst; reflexivity. Qed.
91 Declare Scope AccessSize_scope.
92 Delimit Scope AccessSize_scope with AccessSize.
93 Bind Scope AccessSize_scope with AccessSize.
94
95 Infix "=?" := AccessSize_beq : AccessSize_scope.
96
97 Global Instance AccessSize_beq_spec : reflect_rel (@eq AccessSize) AccessSize_beq | 10
98 := reflect_of_beq AccessSize_dec_bl AccessSize_dec_lb.
99 Definition AccessSize_beq_eq x y : (x =? y)%AccessSize = true <-> x = y := conj (@AccessSize_dec_bl _ _) (@AccessSize_dec_lb _ _).
100 Lemma AccessSize_beq_neq x y : (x =? y)%AccessSize = false <-> x <> y.
101 Proof. rewrite <- AccessSize_beq_eq; destruct (x =? y)%AccessSize; intuition congruence. Qed.
102 Global Instance AccessSize_beq_compat : Proper (eq ==> eq ==> eq) AccessSize_beq | 10.
103 Proof. repeat intro; subst; reflexivity. Qed.
104
105 Declare Scope rip_relative_kind_scope.
106 Delimit Scope rip_relative_kind_scope with rip_relative_kind.
107 Bind Scope rip_relative_kind_scope with rip_relative_kind.
108
109 Infix "=?" := rip_relative_kind_beq : rip_relative_kind_scope.
110
111 Global Instance rip_relative_kind_beq_spec : reflect_rel (@eq rip_relative_kind) rip_relative_kind_beq | 10
112 := reflect_of_beq internal_rip_relative_kind_dec_bl internal_rip_relative_kind_dec_lb.
113 Definition rip_relative_kind_beq_eq x y : (x =? y)%rip_relative_kind = true <-> x = y := conj (@internal_rip_relative_kind_dec_bl _ _) (@internal_rip_relative_kind_dec_lb _ _).
114 Lemma rip_relative_kind_beq_neq x y : (x =? y)%rip_relative_kind = false <-> x <> y.
115 Proof. rewrite <- rip_relative_kind_beq_eq; destruct (x =? y)%rip_relative_kind; intuition congruence. Qed.
116 Global Instance rip_relative_kind_beq_compat : Proper (eq ==> eq ==> eq) rip_relative_kind_beq | 10.
117 Proof. repeat intro; subst; reflexivity. Qed.
118
119 Declare Scope MEM_scope.
120 Delimit Scope MEM_scope with MEM.
121 Bind Scope MEM_scope with MEM.
122
123 Definition MEM_beq (x y : MEM) : bool
124 := ((option_beq AccessSize_beq x.(mem_bits_access_size) y.(mem_bits_access_size))
125 && option_beq String.eqb x.(mem_base_label) y.(mem_base_label)
126 && (option_beq REG_beq x.(mem_base_reg) y.(mem_base_reg))
127 && (option_beq (prod_beq _ _ Z.eqb REG_beq) x.(mem_scale_reg) y.(mem_scale_reg))
128 && (option_beq Z.eqb x.(mem_offset) y.(mem_offset))
129 && (rip_relative_kind_beq x.(rip_relative) y.(rip_relative)))%bool.
130 Global Arguments MEM_beq !_ !_ / .
131
132 Infix "=?" := MEM_beq : MEM_scope.
133
134 Lemma MEM_dec_bl (x y : MEM) : MEM_beq x y = true -> x = y.
135 Proof. t_dec. Qed.
136 Lemma MEM_dec_lb (x y : MEM) : x = y -> MEM_beq x y = true.
137 Proof. t_dec. Qed.
138 Definition MEM_eq_dec (x y : MEM) : {x = y} + {x <> y}
139 := (if (x =? y)%MEM as b return (x =? y)%MEM = b -> _
140 then fun pf => left (@MEM_dec_bl _ _ pf)
141 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@MEM_dec_lb _ _ pf'))))
142 eq_refl.
143
144 Global Instance MEM_beq_spec : reflect_rel (@eq MEM) MEM_beq | 10
145 := reflect_of_beq MEM_dec_bl MEM_dec_lb.
146 Definition MEM_beq_eq x y : (x =? y)%MEM = true <-> x = y := conj (@MEM_dec_bl _ _) (@MEM_dec_lb _ _).
147 Lemma MEM_beq_neq x y : (x =? y)%MEM = false <-> x <> y.
148 Proof. rewrite <- MEM_beq_eq; destruct (x =? y)%MEM; intuition congruence. Qed.
149 Global Instance MEM_beq_compat : Proper (eq ==> eq ==> eq) MEM_beq | 10.
150 Proof. repeat intro; subst; reflexivity. Qed.
151
152 Declare Scope FLAG_scope.
153 Delimit Scope FLAG_scope with FLAG.
154 Bind Scope FLAG_scope with FLAG.
155
156 Infix "=?" := FLAG_beq : FLAG_scope.
157
158 Global Instance FLAG_beq_spec : reflect_rel (@eq FLAG) FLAG_beq | 10
159 := reflect_of_beq FLAG_dec_bl FLAG_dec_lb.
160 Definition FLAG_beq_eq x y : (x =? y)%FLAG = true <-> x = y := conj (@FLAG_dec_bl _ _) (@FLAG_dec_lb _ _).
161 Lemma FLAG_beq_neq x y : (x =? y)%FLAG = false <-> x <> y.
162 Proof. rewrite <- FLAG_beq_eq; destruct (x =? y)%FLAG; intuition congruence. Qed.
163 Global Instance FLAG_beq_compat : Proper (eq ==> eq ==> eq) FLAG_beq | 10.
164 Proof. repeat intro; subst; reflexivity. Qed.
165
166 Declare Scope OpCode_scope.
167 Delimit Scope OpCode_scope with OpCode.
168 Bind Scope OpCode_scope with OpCode.
169
170 Infix "=?" := OpCode_beq : OpCode_scope.
171
172 Global Instance OpCode_beq_spec : reflect_rel (@eq OpCode) OpCode_beq | 10
173 := reflect_of_beq OpCode_dec_bl OpCode_dec_lb.
174 Definition OpCode_beq_eq x y : (x =? y)%OpCode = true <-> x = y := conj (@OpCode_dec_bl _ _) (@OpCode_dec_lb _ _).
175 Lemma OpCode_beq_neq x y : (x =? y)%OpCode = false <-> x <> y.
176 Proof. rewrite <- OpCode_beq_eq; destruct (x =? y)%OpCode; intuition congruence. Qed.
177 Global Instance OpCode_beq_compat : Proper (eq ==> eq ==> eq) OpCode_beq | 10.
178 Proof. repeat intro; subst; reflexivity. Qed.
179
180 Declare Scope OpPrefix_scope.
181 Delimit Scope OpPrefix_scope with OpPrefix.
182 Bind Scope OpPrefix_scope with OpPrefix.
183
184 Infix "=?" := OpPrefix_beq : OpPrefix_scope.
185
186 Global Instance OpPrefix_beq_spec : reflect_rel (@eq OpPrefix) OpPrefix_beq | 10
187 := reflect_of_beq OpPrefix_dec_bl OpPrefix_dec_lb.
188 Definition OpPrefix_beq_eq x y : (x =? y)%OpPrefix = true <-> x = y := conj (@OpPrefix_dec_bl _ _) (@OpPrefix_dec_lb _ _).
189 Lemma OpPrefix_beq_neq x y : (x =? y)%OpPrefix = false <-> x <> y.
190 Proof. rewrite <- OpPrefix_beq_eq; destruct (x =? y)%OpPrefix; intuition congruence. Qed.
191 Global Instance OpPrefix_beq_compat : Proper (eq ==> eq ==> eq) OpPrefix_beq | 10.
192 Proof. repeat intro; subst; reflexivity. Qed.
193
194 Declare Scope ARG_scope.
195 Delimit Scope ARG_scope with ARG.
196 Bind Scope ARG_scope with ARG.
197
198 Definition ARG_beq (x y : ARG) : bool
199 := match x, y with
200 | reg x, reg y => REG_beq x y
201 | mem x, mem y => MEM_beq x y
202 | const x, const y => CONST_beq x y
203 | label x, label y => JUMP_LABEL_beq x y
204 | reg _, _
205 | mem _, _
206 | const _, _
207 | label _, _
208 => false
209 end.
210 Global Arguments ARG_beq !_ !_ / .
211
212 Infix "=?" := ARG_beq : ARG_scope.
213
214 Lemma ARG_dec_bl (x y : ARG) : ARG_beq x y = true -> x = y.
215 Proof. t_dec. Qed.
216 Lemma ARG_dec_lb (x y : ARG) : x = y -> ARG_beq x y = true.
217 Proof. t_dec. Qed.
218 Definition ARG_eq_dec (x y : ARG) : {x = y} + {x <> y}
219 := (if (x =? y)%ARG as b return (x =? y)%ARG = b -> _
220 then fun pf => left (@ARG_dec_bl _ _ pf)
221 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@ARG_dec_lb _ _ pf'))))
222 eq_refl.
223
224 Global Instance ARG_beq_spec : reflect_rel (@eq ARG) ARG_beq | 10
225 := reflect_of_beq ARG_dec_bl ARG_dec_lb.
226 Definition ARG_beq_eq x y : (x =? y)%ARG = true <-> x = y := conj (@ARG_dec_bl _ _) (@ARG_dec_lb _ _).
227 Lemma ARG_beq_neq x y : (x =? y)%ARG = false <-> x <> y.
228 Proof. rewrite <- ARG_beq_eq; destruct (x =? y)%ARG; intuition congruence. Qed.
229 Global Instance ARG_beq_compat : Proper (eq ==> eq ==> eq) ARG_beq | 10.
230 Proof. repeat intro; subst; reflexivity. Qed.
231
232 Declare Scope NormalInstruction_scope.
233 Delimit Scope NormalInstruction_scope with NormalInstruction.
234 Bind Scope NormalInstruction_scope with NormalInstruction.
235
236 Definition NormalInstruction_beq (x y : NormalInstruction) : bool
237 := (option_beq OpPrefix_beq x.(prefix) y.(prefix)
238 && (x.(op) =? y.(op))%OpCode
239 && list_beq _ ARG_beq x.(args) y.(args))%bool.
240 Global Arguments NormalInstruction_beq !_ !_ / .
241
242 Infix "=?" := NormalInstruction_beq : NormalInstruction_scope.
243
244 Lemma NormalInstruction_dec_bl (x y : NormalInstruction) : NormalInstruction_beq x y = true -> x = y.
245 Proof. t_dec. Qed.
246 Lemma NormalInstruction_dec_lb (x y : NormalInstruction) : x = y -> NormalInstruction_beq x y = true.
247 Proof. t_dec. Qed.
248 Definition NormalInstruction_eq_dec (x y : NormalInstruction) : {x = y} + {x <> y}
249 := (if (x =? y)%NormalInstruction as b return (x =? y)%NormalInstruction = b -> _
250 then fun pf => left (@NormalInstruction_dec_bl _ _ pf)
251 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@NormalInstruction_dec_lb _ _ pf'))))
252 eq_refl.
253
254 Global Instance NormalInstruction_beq_spec : reflect_rel (@eq NormalInstruction) NormalInstruction_beq | 10
255 := reflect_of_beq NormalInstruction_dec_bl NormalInstruction_dec_lb.
256 Definition NormalInstruction_beq_eq x y : (x =? y)%NormalInstruction = true <-> x = y := conj (@NormalInstruction_dec_bl _ _) (@NormalInstruction_dec_lb _ _).
257 Lemma NormalInstruction_beq_neq x y : (x =? y)%NormalInstruction = false <-> x <> y.
258 Proof. rewrite <- NormalInstruction_beq_eq; destruct (x =? y)%NormalInstruction; intuition congruence. Qed.
259 Global Instance NormalInstruction_beq_compat : Proper (eq ==> eq ==> eq) NormalInstruction_beq | 10.
260 Proof. repeat intro; subst; reflexivity. Qed.
261
262 Declare Scope RawLine_scope.
263 Delimit Scope RawLine_scope with RawLine.
264 Bind Scope RawLine_scope with RawLine.
265
266 Definition RawLine_beq (x y : RawLine) : bool
267 := match x, y with
268 | SECTION x, SECTION y => String.eqb x y
269 | GLOBAL x, GLOBAL y => String.eqb x y
270 | LABEL x, LABEL y => String.eqb x y
271 | EMPTY, EMPTY => true
272 | DEFAULT_REL, DEFAULT_REL => true
273 | INSTR x, INSTR y => NormalInstruction_beq x y
274 | ALIGN x, ALIGN y => String.eqb x y
275 | DIRECTIVE x, DIRECTIVE y => String.eqb x y
276 | ASCII_ z x, ASCII_ z' y => Bool.eqb z z' && String.eqb x y
277 | SECTION _, _
278 | GLOBAL _, _
279 | LABEL _, _
280 | EMPTY, _
281 | INSTR _, _
282 | ALIGN _, _
283 | DEFAULT_REL, _
284 | DIRECTIVE _, _
285 | ASCII_ _ _, _
286 => false
287 end%bool.
288 Global Arguments RawLine_beq !_ !_ / .
289
290 Infix "=?" := RawLine_beq : RawLine_scope.
291
292 Lemma RawLine_dec_bl (x y : RawLine) : RawLine_beq x y = true -> x = y.
293 Proof. t_dec. Qed.
294 Lemma RawLine_dec_lb (x y : RawLine) : x = y -> RawLine_beq x y = true.
295 Proof. t_dec. Qed.
296 Definition RawLine_eq_dec (x y : RawLine) : {x = y} + {x <> y}
297 := (if (x =? y)%RawLine as b return (x =? y)%RawLine = b -> _
298 then fun pf => left (@RawLine_dec_bl _ _ pf)
299 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@RawLine_dec_lb _ _ pf'))))
300 eq_refl.
301
302 Global Instance RawLine_beq_spec : reflect_rel (@eq RawLine) RawLine_beq | 10
303 := reflect_of_beq RawLine_dec_bl RawLine_dec_lb.
304 Definition RawLine_beq_eq x y : (x =? y)%RawLine = true <-> x = y := conj (@RawLine_dec_bl _ _) (@RawLine_dec_lb _ _).
305 Lemma RawLine_beq_neq x y : (x =? y)%RawLine = false <-> x <> y.
306 Proof. rewrite <- RawLine_beq_eq; destruct (x =? y)%RawLine; intuition congruence. Qed.
307 Global Instance RawLine_beq_compat : Proper (eq ==> eq ==> eq) RawLine_beq | 10.
308 Proof. repeat intro; subst; reflexivity. Qed.
309
310 Declare Scope Line_scope.
311 Delimit Scope Line_scope with Line.
312 Bind Scope Line_scope with Line.
313
314 Definition Line_beq (x y : Line) : bool
315 := ((x.(indent) =? y.(indent))%string
316 && (x.(rawline) =? y.(rawline))%RawLine
317 && (x.(pre_comment_whitespace) =? y.(pre_comment_whitespace))%string
318 && option_beq String.eqb x.(comment) y.(comment))%bool
319 && (x.(line_number) =? y.(line_number))%N.
320 Global Arguments Line_beq !_ !_ / .
321
322 Infix "=?" := Line_beq : Line_scope.
323
324 Lemma Line_dec_bl (x y : Line) : Line_beq x y = true -> x = y.
325 Proof. t_dec. Qed.
326 Lemma Line_dec_lb (x y : Line) : x = y -> Line_beq x y = true.
327 Proof. t_dec. Qed.
328 Definition Line_eq_dec (x y : Line) : {x = y} + {x <> y}
329 := (if (x =? y)%Line as b return (x =? y)%Line = b -> _
330 then fun pf => left (@Line_dec_bl _ _ pf)
331 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@Line_dec_lb _ _ pf'))))
332 eq_refl.
333
334 Global Instance Line_beq_spec : reflect_rel (@eq Line) Line_beq | 10
335 := reflect_of_beq Line_dec_bl Line_dec_lb.
336 Definition Line_beq_eq x y : (x =? y)%Line = true <-> x = y := conj (@Line_dec_bl _ _) (@Line_dec_lb _ _).
337 Lemma Line_beq_neq x y : (x =? y)%Line = false <-> x <> y.
338 Proof. rewrite <- Line_beq_eq; destruct (x =? y)%Line; intuition congruence. Qed.
339 Global Instance Line_beq_compat : Proper (eq ==> eq ==> eq) Line_beq | 10.
340 Proof. repeat intro; subst; reflexivity. Qed.
341
342 Declare Scope Lines_scope.
343 Delimit Scope Lines_scope with Lines.
344 Bind Scope Lines_scope with Lines.
345
346 Definition Lines_beq (x y : Lines) : bool
347 := list_beq _ Line_beq x y.
348 Global Arguments Lines_beq !_ !_ / .
349
350 Infix "=?" := Lines_beq : Lines_scope.
351
352 Lemma Lines_dec_bl (x y : Lines) : Lines_beq x y = true -> x = y.
353 Proof. t_dec. Qed.
354 Lemma Lines_dec_lb (x y : Lines) : x = y -> Lines_beq x y = true.
355 Proof. t_dec. Qed.
356 Definition Lines_eq_dec (x y : Lines) : {x = y} + {x <> y}
357 := (if (x =? y)%Lines as b return (x =? y)%Lines = b -> _
358 then fun pf => left (@Lines_dec_bl _ _ pf)
359 else fun pf => right (fun pf' => diff_false_true (eq_trans (eq_sym pf) (@Lines_dec_lb _ _ pf'))))
360 eq_refl.
361
362 Global Instance Lines_beq_spec : reflect_rel (@eq Lines) Lines_beq | 10
363 := reflect_of_beq Lines_dec_bl Lines_dec_lb.
364 Definition Lines_beq_eq x y : (x =? y)%Lines = true <-> x = y := conj (@Lines_dec_bl _ _) (@Lines_dec_lb _ _).
365 Lemma Lines_beq_neq x y : (x =? y)%Lines = false <-> x <> y.
366 Proof. rewrite <- Lines_beq_eq; destruct (x =? y)%Lines; intuition congruence. Qed.
367 Global Instance Lines_beq_compat : Proper (eq ==> eq ==> eq) Lines_beq | 10.
368 Proof. repeat intro; subst; reflexivity. Qed.