1 | (* *********************************************************************) |
---|
2 | (* *) |
---|
3 | (* The Compcert verified compiler *) |
---|
4 | (* *) |
---|
5 | (* Xavier Leroy, INRIA Paris-Rocquencourt *) |
---|
6 | (* *) |
---|
7 | (* Copyright Institut National de Recherche en Informatique et en *) |
---|
8 | (* Automatique. All rights reserved. This file is distributed *) |
---|
9 | (* under the terms of the GNU General Public License as published by *) |
---|
10 | (* the Free Software Foundation, either version 2 of the License, or *) |
---|
11 | (* (at your option) any later version. This file is also distributed *) |
---|
12 | (* under the terms of the INRIA Non-Commercial License Agreement. *) |
---|
13 | (* *) |
---|
14 | (* *********************************************************************) |
---|
15 | |
---|
16 | (* * This module defines the type of values that is used in the dynamic |
---|
17 | semantics of all our intermediate languages. *) |
---|
18 | |
---|
19 | include "utilities/Coqlib.ma". |
---|
20 | include "common/Floats.ma". |
---|
21 | include "common/Errors.ma". |
---|
22 | include "common/Pointers.ma". |
---|
23 | include "basics/logic.ma". |
---|
24 | |
---|
25 | (* * A value is either: |
---|
26 | - a machine integer; |
---|
27 | - a floating-point number; |
---|
28 | - a pointer: a triple giving the representation of the pointer (in terms of the |
---|
29 | memory regions such a pointer could address), a memory address and |
---|
30 | an integer offset with respect to this address; |
---|
31 | - a null pointer: the region denotes the representation (i.e., pointer size) |
---|
32 | - the [Vundef] value denoting an arbitrary bit pattern, such as the |
---|
33 | value of an uninitialized variable. |
---|
34 | *) |
---|
35 | |
---|
36 | inductive val: Type[0] ≝ |
---|
37 | | Vundef: val |
---|
38 | | Vint: ∀sz:intsize. bvint sz → val |
---|
39 | | Vfloat: float → val |
---|
40 | | Vnull: region → val |
---|
41 | | Vptr: pointer → val. |
---|
42 | |
---|
43 | definition Vzero : intsize → val ≝ λsz. Vint sz (zero ?). |
---|
44 | definition Vone: intsize → val ≝ λsz. Vint sz (repr sz 1). |
---|
45 | definition mone ≝ λsz. bitvector_of_Z (bitsize_of_intsize sz) (neg one). |
---|
46 | definition Vmone: intsize → val ≝ λsz. Vint sz (mone ?). |
---|
47 | |
---|
48 | (* XXX 32bit booleans are Clight specific. *) |
---|
49 | definition Vtrue: val ≝ Vone I32. |
---|
50 | definition Vfalse: val ≝ Vzero I32. |
---|
51 | |
---|
52 | inductive val_typ : val → typ → Prop ≝ |
---|
53 | | VTint: ∀sz,sg,i. val_typ (Vint sz i) (ASTint sz sg) |
---|
54 | | VTfloat: ∀sz,f. val_typ (Vfloat f) (ASTfloat sz) |
---|
55 | | VTnull: ∀r. val_typ (Vnull r) (ASTptr r) |
---|
56 | | VTptr: ∀r,b,c,o. val_typ (Vptr (mk_pointer r b c o)) (ASTptr r). |
---|
57 | |
---|
58 | (* |
---|
59 | (** The module [Val] defines a number of arithmetic and logical operations |
---|
60 | over type [val]. Most of these operations are straightforward extensions |
---|
61 | of the corresponding integer or floating-point operations. *) |
---|
62 | |
---|
63 | Module Val. |
---|
64 | *) |
---|
65 | definition of_bool : bool → val ≝ λb. if b then Vtrue else Vfalse. |
---|
66 | |
---|
67 | (* * Truth values. Pointers and non-zero integers are treated as [True]. |
---|
68 | The integer 0 (also used to represent the null pointer) is [False]. |
---|
69 | [Vundef] and floats are neither true nor false. *) |
---|
70 | |
---|
71 | definition is_true : val → Prop ≝ λv. |
---|
72 | match v with |
---|
73 | [ Vint _ n ⇒ n ≠ (zero ?) |
---|
74 | | Vptr _ ⇒ True |
---|
75 | | _ ⇒ False |
---|
76 | ]. |
---|
77 | |
---|
78 | definition is_false : val → Prop ≝ λv. |
---|
79 | match v with |
---|
80 | [ Vint _ n ⇒ n = (zero ?) |
---|
81 | | Vnull _ ⇒ True |
---|
82 | | _ ⇒ False |
---|
83 | ]. |
---|
84 | |
---|
85 | inductive bool_of_val: val → bool → Prop ≝ |
---|
86 | | bool_of_val_int_true: |
---|
87 | ∀sz,n. n ≠ zero ? → bool_of_val (Vint sz n) true |
---|
88 | | bool_of_val_int_false: |
---|
89 | ∀sz. bool_of_val (Vzero sz) false |
---|
90 | | bool_of_val_ptr: |
---|
91 | ∀p. bool_of_val (Vptr p) true |
---|
92 | | bool_of_val_null: |
---|
93 | ∀r. bool_of_val (Vnull r) true. |
---|
94 | |
---|
95 | axiom ValueNotABoolean : String. |
---|
96 | |
---|
97 | definition eval_bool_of_val : val → res bool ≝ |
---|
98 | λv. match v with |
---|
99 | [ Vint _ i ⇒ OK ? (notb (eq_bv ? i (zero ?))) |
---|
100 | | Vnull _ ⇒ OK ? false |
---|
101 | | Vptr _ ⇒ OK ? true |
---|
102 | | _ ⇒ Error ? (msg ValueNotABoolean) |
---|
103 | ]. |
---|
104 | |
---|
105 | definition neg : val → val ≝ λv. |
---|
106 | match v with |
---|
107 | [ Vint sz n ⇒ Vint sz (two_complement_negation ? n) |
---|
108 | | _ ⇒ Vundef |
---|
109 | ]. |
---|
110 | |
---|
111 | definition negf : val → val ≝ λv. |
---|
112 | match v with |
---|
113 | [ Vfloat f ⇒ Vfloat (Fneg f) |
---|
114 | | _ => Vundef |
---|
115 | ]. |
---|
116 | |
---|
117 | definition absf : val → val ≝ λv. |
---|
118 | match v with |
---|
119 | [ Vfloat f ⇒ Vfloat (Fabs f) |
---|
120 | | _ ⇒ Vundef |
---|
121 | ]. |
---|
122 | |
---|
123 | definition intoffloat : intsize → val → val ≝ λsz,v. |
---|
124 | match v with |
---|
125 | [ Vfloat f ⇒ Vint sz (intoffloat ? f) |
---|
126 | | _ ⇒ Vundef |
---|
127 | ]. |
---|
128 | |
---|
129 | definition intuoffloat : intsize → val → val ≝ λsz,v. |
---|
130 | match v with |
---|
131 | [ Vfloat f ⇒ Vint sz (intuoffloat ? f) |
---|
132 | | _ ⇒ Vundef |
---|
133 | ]. |
---|
134 | |
---|
135 | definition floatofint : val → val ≝ λv. |
---|
136 | match v with |
---|
137 | [ Vint sz n ⇒ Vfloat (floatofint ? n) |
---|
138 | | _ ⇒ Vundef |
---|
139 | ]. |
---|
140 | |
---|
141 | definition floatofintu : val → val ≝ λv. |
---|
142 | match v with |
---|
143 | [ Vint sz n ⇒ Vfloat (floatofintu ? n) |
---|
144 | | _ ⇒ Vundef |
---|
145 | ]. |
---|
146 | |
---|
147 | definition notint : val → val ≝ λv. |
---|
148 | match v with |
---|
149 | [ Vint sz n ⇒ Vint sz (exclusive_disjunction_bv ? n (mone ?)) |
---|
150 | | _ ⇒ Vundef |
---|
151 | ]. |
---|
152 | |
---|
153 | definition notbool : val → val ≝ λv. |
---|
154 | match v with |
---|
155 | [ Vint sz n ⇒ of_bool (eq_bv ? n (zero ?)) |
---|
156 | | Vptr _ ⇒ Vfalse |
---|
157 | | Vnull _ ⇒ Vtrue |
---|
158 | | _ ⇒ Vundef |
---|
159 | ]. |
---|
160 | |
---|
161 | definition zero_ext ≝ λrsz: intsize. λv: val. |
---|
162 | match v with |
---|
163 | [ Vint sz n ⇒ Vint rsz (zero_ext … n) |
---|
164 | | _ ⇒ Vundef |
---|
165 | ]. |
---|
166 | |
---|
167 | definition sign_ext ≝ λrsz:intsize. λv:val. |
---|
168 | match v with |
---|
169 | [ Vint sz i ⇒ Vint rsz (sign_ext … i) |
---|
170 | | _ ⇒ Vundef |
---|
171 | ]. |
---|
172 | |
---|
173 | definition singleoffloat : val → val ≝ λv. |
---|
174 | match v with |
---|
175 | [ Vfloat f ⇒ Vfloat (singleoffloat f) |
---|
176 | | _ ⇒ Vundef |
---|
177 | ]. |
---|
178 | |
---|
179 | (* TODO: add zero to null? *) |
---|
180 | definition add ≝ λv1,v2: val. |
---|
181 | match v1 with |
---|
182 | [ Vint sz1 n1 ⇒ match v2 with |
---|
183 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
184 | (λn1. Vint sz2 (addition_n ? n1 n2)) |
---|
185 | Vundef |
---|
186 | | Vptr ptr ⇒ Vptr (shift_pointer ? ptr n1) |
---|
187 | | _ ⇒ Vundef ] |
---|
188 | | Vptr ptr ⇒ match v2 with |
---|
189 | [ Vint _ n2 ⇒ Vptr (shift_pointer ? ptr n2) |
---|
190 | | _ ⇒ Vundef ] |
---|
191 | | _ ⇒ Vundef ]. |
---|
192 | |
---|
193 | (* XXX Is I32 the best answer for ptr subtraction? *) |
---|
194 | |
---|
195 | definition sub ≝ λv1,v2: val. |
---|
196 | match v1 with |
---|
197 | [ Vint sz1 n1 ⇒ match v2 with |
---|
198 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
199 | (λn1. Vint sz2 (subtraction ? n1 n2)) |
---|
200 | Vundef |
---|
201 | | _ ⇒ Vundef ] |
---|
202 | | Vptr ptr1 ⇒ match v2 with |
---|
203 | [ Vint sz2 n2 ⇒ Vptr (neg_shift_pointer ? ptr1 n2) |
---|
204 | | Vptr ptr2 ⇒ |
---|
205 | if eq_block (pblock ptr1) (pblock ptr2) |
---|
206 | then Vint I32 (sub_offset ? (poff ptr1) (poff ptr2)) |
---|
207 | else Vundef |
---|
208 | | _ ⇒ Vundef ] |
---|
209 | | Vnull r ⇒ match v2 with [ Vnull r' ⇒ Vzero I32 | _ ⇒ Vundef ] |
---|
210 | | _ ⇒ Vundef ]. |
---|
211 | |
---|
212 | definition mul ≝ λv1, v2: val. |
---|
213 | match v1 with |
---|
214 | [ Vint sz1 n1 ⇒ match v2 with |
---|
215 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
216 | (λn1. Vint sz2 (\snd (split … (multiplication ? n1 n2)))) |
---|
217 | Vundef |
---|
218 | | _ ⇒ Vundef ] |
---|
219 | | _ ⇒ Vundef ]. |
---|
220 | (* |
---|
221 | definition divs ≝ λv1, v2: val. |
---|
222 | match v1 with |
---|
223 | [ Vint n1 ⇒ match v2 with |
---|
224 | [ Vint n2 ⇒ Vint (divs n1 n2) |
---|
225 | | _ ⇒ Vundef ] |
---|
226 | | _ ⇒ Vundef ]. |
---|
227 | |
---|
228 | Definition mods (v1 v2: val): val := |
---|
229 | match v1, v2 with |
---|
230 | | Vint n1, Vint n2 => |
---|
231 | if Int.eq n2 Int.zero then Vundef else Vint(Int.mods n1 n2) |
---|
232 | | _, _ => Vundef |
---|
233 | end. |
---|
234 | |
---|
235 | Definition divu (v1 v2: val): val := |
---|
236 | match v1, v2 with |
---|
237 | | Vint n1, Vint n2 => |
---|
238 | if Int.eq n2 Int.zero then Vundef else Vint(Int.divu n1 n2) |
---|
239 | | _, _ => Vundef |
---|
240 | end. |
---|
241 | |
---|
242 | Definition modu (v1 v2: val): val := |
---|
243 | match v1, v2 with |
---|
244 | | Vint n1, Vint n2 => |
---|
245 | if Int.eq n2 Int.zero then Vundef else Vint(Int.modu n1 n2) |
---|
246 | | _, _ => Vundef |
---|
247 | end. |
---|
248 | *) |
---|
249 | definition v_and ≝ λv1, v2: val. |
---|
250 | match v1 with |
---|
251 | [ Vint sz1 n1 ⇒ match v2 with |
---|
252 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
253 | (λn1. Vint ? (conjunction_bv ? n1 n2)) |
---|
254 | Vundef |
---|
255 | | _ ⇒ Vundef ] |
---|
256 | | _ ⇒ Vundef ]. |
---|
257 | |
---|
258 | definition or ≝ λv1, v2: val. |
---|
259 | match v1 with |
---|
260 | [ Vint sz1 n1 ⇒ match v2 with |
---|
261 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
262 | (λn1. Vint ? (inclusive_disjunction_bv ? n1 n2)) |
---|
263 | Vundef |
---|
264 | | _ ⇒ Vundef ] |
---|
265 | | _ ⇒ Vundef ]. |
---|
266 | |
---|
267 | definition xor ≝ λv1, v2: val. |
---|
268 | match v1 with |
---|
269 | [ Vint sz1 n1 ⇒ match v2 with |
---|
270 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
271 | (λn1. Vint ? (exclusive_disjunction_bv ? n1 n2)) |
---|
272 | Vundef |
---|
273 | | _ ⇒ Vundef ] |
---|
274 | | _ ⇒ Vundef ]. |
---|
275 | (* |
---|
276 | Definition shl (v1 v2: val): val := |
---|
277 | match v1, v2 with |
---|
278 | | Vint n1, Vint n2 => |
---|
279 | if Int.ltu n2 Int.iwordsize |
---|
280 | then Vint(Int.shl n1 n2) |
---|
281 | else Vundef |
---|
282 | | _, _ => Vundef |
---|
283 | end. |
---|
284 | |
---|
285 | Definition shr (v1 v2: val): val := |
---|
286 | match v1, v2 with |
---|
287 | | Vint n1, Vint n2 => |
---|
288 | if Int.ltu n2 Int.iwordsize |
---|
289 | then Vint(Int.shr n1 n2) |
---|
290 | else Vundef |
---|
291 | | _, _ => Vundef |
---|
292 | end. |
---|
293 | |
---|
294 | Definition shr_carry (v1 v2: val): val := |
---|
295 | match v1, v2 with |
---|
296 | | Vint n1, Vint n2 => |
---|
297 | if Int.ltu n2 Int.iwordsize |
---|
298 | then Vint(Int.shr_carry n1 n2) |
---|
299 | else Vundef |
---|
300 | | _, _ => Vundef |
---|
301 | end. |
---|
302 | |
---|
303 | Definition shrx (v1 v2: val): val := |
---|
304 | match v1, v2 with |
---|
305 | | Vint n1, Vint n2 => |
---|
306 | if Int.ltu n2 Int.iwordsize |
---|
307 | then Vint(Int.shrx n1 n2) |
---|
308 | else Vundef |
---|
309 | | _, _ => Vundef |
---|
310 | end. |
---|
311 | |
---|
312 | Definition shru (v1 v2: val): val := |
---|
313 | match v1, v2 with |
---|
314 | | Vint n1, Vint n2 => |
---|
315 | if Int.ltu n2 Int.iwordsize |
---|
316 | then Vint(Int.shru n1 n2) |
---|
317 | else Vundef |
---|
318 | | _, _ => Vundef |
---|
319 | end. |
---|
320 | |
---|
321 | Definition rolm (v: val) (amount mask: int): val := |
---|
322 | match v with |
---|
323 | | Vint n => Vint(Int.rolm n amount mask) |
---|
324 | | _ => Vundef |
---|
325 | end. |
---|
326 | |
---|
327 | Definition ror (v1 v2: val): val := |
---|
328 | match v1, v2 with |
---|
329 | | Vint n1, Vint n2 => |
---|
330 | if Int.ltu n2 Int.iwordsize |
---|
331 | then Vint(Int.ror n1 n2) |
---|
332 | else Vundef |
---|
333 | | _, _ => Vundef |
---|
334 | end. |
---|
335 | *) |
---|
336 | definition addf ≝ λv1,v2: val. |
---|
337 | match v1 with |
---|
338 | [ Vfloat f1 ⇒ match v2 with |
---|
339 | [ Vfloat f2 ⇒ Vfloat (Fadd f1 f2) |
---|
340 | | _ ⇒ Vundef ] |
---|
341 | | _ ⇒ Vundef ]. |
---|
342 | |
---|
343 | definition subf ≝ λv1,v2: val. |
---|
344 | match v1 with |
---|
345 | [ Vfloat f1 ⇒ match v2 with |
---|
346 | [ Vfloat f2 ⇒ Vfloat (Fsub f1 f2) |
---|
347 | | _ ⇒ Vundef ] |
---|
348 | | _ ⇒ Vundef ]. |
---|
349 | |
---|
350 | definition mulf ≝ λv1,v2: val. |
---|
351 | match v1 with |
---|
352 | [ Vfloat f1 ⇒ match v2 with |
---|
353 | [ Vfloat f2 ⇒ Vfloat (Fmul f1 f2) |
---|
354 | | _ ⇒ Vundef ] |
---|
355 | | _ ⇒ Vundef ]. |
---|
356 | |
---|
357 | definition divf ≝ λv1,v2: val. |
---|
358 | match v1 with |
---|
359 | [ Vfloat f1 ⇒ match v2 with |
---|
360 | [ Vfloat f2 ⇒ Vfloat (Fdiv f1 f2) |
---|
361 | | _ ⇒ Vundef ] |
---|
362 | | _ ⇒ Vundef ]. |
---|
363 | |
---|
364 | definition cmp_match : comparison → val ≝ λc. |
---|
365 | match c with |
---|
366 | [ Ceq ⇒ Vtrue |
---|
367 | | Cne ⇒ Vfalse |
---|
368 | | _ ⇒ Vundef |
---|
369 | ]. |
---|
370 | |
---|
371 | definition cmp_mismatch : comparison → val ≝ λc. |
---|
372 | match c with |
---|
373 | [ Ceq ⇒ Vfalse |
---|
374 | | Cne ⇒ Vtrue |
---|
375 | | _ ⇒ Vundef |
---|
376 | ]. |
---|
377 | |
---|
378 | definition cmp_offset ≝ |
---|
379 | λc: comparison. λx,y:offset. |
---|
380 | match c with |
---|
381 | [ Ceq ⇒ eq_offset x y |
---|
382 | | Cne ⇒ ¬eq_offset x y |
---|
383 | | Clt ⇒ lt_offset x y |
---|
384 | | Cle ⇒ ¬lt_offset y x |
---|
385 | | Cgt ⇒ lt_offset y x |
---|
386 | | Cge ⇒ ¬lt_offset x y |
---|
387 | ]. |
---|
388 | |
---|
389 | definition cmp_int : ∀n. comparison → BitVector n → BitVector n → bool ≝ |
---|
390 | λn,c,x,y. |
---|
391 | match c with |
---|
392 | [ Ceq ⇒ eq_bv ? x y |
---|
393 | | Cne ⇒ notb (eq_bv ? x y) |
---|
394 | | Clt ⇒ lt_s ? x y |
---|
395 | | Cle ⇒ notb (lt_s ? y x) |
---|
396 | | Cgt ⇒ lt_s ? y x |
---|
397 | | Cge ⇒ notb (lt_s ? x y) |
---|
398 | ]. |
---|
399 | |
---|
400 | definition cmpu_int : ∀n. comparison → BitVector n → BitVector n → bool ≝ |
---|
401 | λn,c,x,y. |
---|
402 | match c with |
---|
403 | [ Ceq ⇒ eq_bv ? x y |
---|
404 | | Cne ⇒ notb (eq_bv ? x y) |
---|
405 | | Clt ⇒ lt_u ? x y |
---|
406 | | Cle ⇒ notb (lt_u ? y x) |
---|
407 | | Cgt ⇒ lt_u ? y x |
---|
408 | | Cge ⇒ notb (lt_u ? x y) |
---|
409 | ]. |
---|
410 | |
---|
411 | definition cmp ≝ λc: comparison. λv1,v2: val. |
---|
412 | match v1 with |
---|
413 | [ Vint sz1 n1 ⇒ match v2 with |
---|
414 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
415 | (λn1. of_bool (cmp_int ? c n1 n2)) |
---|
416 | Vundef |
---|
417 | | _ ⇒ Vundef ] |
---|
418 | | Vptr ptr1 ⇒ match v2 with |
---|
419 | [ Vptr ptr2 ⇒ |
---|
420 | if eq_block (pblock ptr1) (pblock ptr2) |
---|
421 | then of_bool (cmp_offset c (poff ptr1) (poff ptr2)) |
---|
422 | else cmp_mismatch c |
---|
423 | | Vnull r2 ⇒ cmp_mismatch c |
---|
424 | | _ ⇒ Vundef ] |
---|
425 | | Vnull r1 ⇒ match v2 with |
---|
426 | [ Vptr _ ⇒ cmp_mismatch c |
---|
427 | | Vnull r2 ⇒ cmp_match c |
---|
428 | | _ ⇒ Vundef |
---|
429 | ] |
---|
430 | | _ ⇒ Vundef ]. |
---|
431 | |
---|
432 | definition cmpu ≝ λc: comparison. λv1,v2: val. |
---|
433 | match v1 with |
---|
434 | [ Vint sz1 n1 ⇒ match v2 with |
---|
435 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
436 | (λn1. of_bool (cmpu_int ? c n1 n2)) |
---|
437 | Vundef |
---|
438 | | _ ⇒ Vundef ] |
---|
439 | | Vptr ptr1 ⇒ match v2 with |
---|
440 | [ Vptr ptr2 ⇒ |
---|
441 | if eq_block (pblock ptr1) (pblock ptr2) |
---|
442 | then of_bool (cmp_offset c (poff ptr1) (poff ptr2)) |
---|
443 | else cmp_mismatch c |
---|
444 | | Vnull r2 ⇒ cmp_mismatch c |
---|
445 | | _ ⇒ Vundef ] |
---|
446 | | Vnull r1 ⇒ match v2 with |
---|
447 | [ Vptr _ ⇒ cmp_mismatch c |
---|
448 | | Vnull r2 ⇒ cmp_match c |
---|
449 | | _ ⇒ Vundef |
---|
450 | ] |
---|
451 | | _ ⇒ Vundef ]. |
---|
452 | |
---|
453 | definition cmpf ≝ λc: comparison. λsz:intsize. λv1,v2: val. |
---|
454 | match v1 with |
---|
455 | [ Vfloat f1 ⇒ match v2 with |
---|
456 | [ Vfloat f2 ⇒ of_bool (Fcmp c f1 f2) |
---|
457 | | _ ⇒ Vundef ] |
---|
458 | | _ ⇒ Vundef ]. |
---|
459 | |
---|
460 | (* * [load_result] is used in the memory model (library [Mem]) |
---|
461 | to post-process the results of a memory read. For instance, |
---|
462 | consider storing the integer value [0xFFF] on 1 byte at a |
---|
463 | given address, and reading it back. If it is read back with |
---|
464 | chunk [Mint8unsigned], zero-extension must be performed, resulting |
---|
465 | in [0xFF]. If it is read back as a [Mint8signed], sign-extension |
---|
466 | is performed and [0xFFFFFFFF] is returned. Type mismatches |
---|
467 | (e.g. reading back a float as a [Mint32]) read back as [Vundef]. *) |
---|
468 | (* XXX update comment *) |
---|
469 | (* XXX is this even necessary now? |
---|
470 | should we be able to extract bytes? *) |
---|
471 | |
---|
472 | let rec load_result (chunk: memory_chunk) (v: val) ≝ |
---|
473 | match v with |
---|
474 | [ Vint sz n ⇒ |
---|
475 | match chunk with |
---|
476 | [ Mint8signed ⇒ match sz with [ I8 ⇒ v | _ ⇒ Vundef ] |
---|
477 | | Mint8unsigned ⇒ match sz with [ I8 ⇒ v | _ ⇒ Vundef ] |
---|
478 | | Mint16signed ⇒ match sz with [ I16 ⇒ v | _ ⇒ Vundef ] |
---|
479 | | Mint16unsigned ⇒ match sz with [ I16 ⇒ v | _ ⇒ Vundef ] |
---|
480 | | Mint32 ⇒ match sz with [ I32 ⇒ v | _ ⇒ Vundef ] |
---|
481 | | _ ⇒ Vundef |
---|
482 | ] |
---|
483 | | Vptr ptr ⇒ |
---|
484 | match chunk with |
---|
485 | [ Mpointer r ⇒ if eq_region (ptype ptr) r then Vptr ptr else Vundef |
---|
486 | | _ ⇒ Vundef |
---|
487 | ] |
---|
488 | | Vnull r ⇒ |
---|
489 | match chunk with |
---|
490 | [ Mpointer r' ⇒ if eq_region r r' then Vnull r else Vundef |
---|
491 | | _ ⇒ Vundef |
---|
492 | ] |
---|
493 | | Vfloat f ⇒ |
---|
494 | match chunk with |
---|
495 | [ Mfloat32 ⇒ Vfloat(singleoffloat f) |
---|
496 | | Mfloat64 ⇒ Vfloat f |
---|
497 | | _ ⇒ Vundef |
---|
498 | ] |
---|
499 | | _ ⇒ Vundef |
---|
500 | ]. |
---|
501 | |
---|
502 | (* |
---|
503 | (** Theorems on arithmetic operations. *) |
---|
504 | |
---|
505 | Theorem cast8unsigned_and: |
---|
506 | forall x, zero_ext 8 x = and x (Vint(Int.repr 255)). |
---|
507 | Proof. |
---|
508 | destruct x; simpl; auto. decEq. |
---|
509 | change 255 with (two_p 8 - 1). apply Int.zero_ext_and. vm_compute; auto. |
---|
510 | Qed. |
---|
511 | |
---|
512 | Theorem cast16unsigned_and: |
---|
513 | forall x, zero_ext 16 x = and x (Vint(Int.repr 65535)). |
---|
514 | Proof. |
---|
515 | destruct x; simpl; auto. decEq. |
---|
516 | change 65535 with (two_p 16 - 1). apply Int.zero_ext_and. vm_compute; auto. |
---|
517 | Qed. |
---|
518 | |
---|
519 | Theorem istrue_not_isfalse: |
---|
520 | forall v, is_false v -> is_true (notbool v). |
---|
521 | Proof. |
---|
522 | destruct v; simpl; try contradiction. |
---|
523 | intros. subst i. simpl. discriminate. |
---|
524 | Qed. |
---|
525 | |
---|
526 | Theorem isfalse_not_istrue: |
---|
527 | forall v, is_true v -> is_false (notbool v). |
---|
528 | Proof. |
---|
529 | destruct v; simpl; try contradiction. |
---|
530 | intros. generalize (Int.eq_spec i Int.zero). |
---|
531 | case (Int.eq i Int.zero); intro. |
---|
532 | contradiction. simpl. auto. |
---|
533 | auto. |
---|
534 | Qed. |
---|
535 | |
---|
536 | Theorem bool_of_true_val: |
---|
537 | forall v, is_true v -> bool_of_val v true. |
---|
538 | Proof. |
---|
539 | intro. destruct v; simpl; intros; try contradiction. |
---|
540 | constructor; auto. constructor. |
---|
541 | Qed. |
---|
542 | |
---|
543 | Theorem bool_of_true_val2: |
---|
544 | forall v, bool_of_val v true -> is_true v. |
---|
545 | Proof. |
---|
546 | intros. inversion H; simpl; auto. |
---|
547 | Qed. |
---|
548 | |
---|
549 | Theorem bool_of_true_val_inv: |
---|
550 | forall v b, is_true v -> bool_of_val v b -> b = true. |
---|
551 | Proof. |
---|
552 | intros. inversion H0; subst v b; simpl in H; auto. |
---|
553 | Qed. |
---|
554 | |
---|
555 | Theorem bool_of_false_val: |
---|
556 | forall v, is_false v -> bool_of_val v false. |
---|
557 | Proof. |
---|
558 | intro. destruct v; simpl; intros; try contradiction. |
---|
559 | subst i; constructor. |
---|
560 | Qed. |
---|
561 | |
---|
562 | Theorem bool_of_false_val2: |
---|
563 | forall v, bool_of_val v false -> is_false v. |
---|
564 | Proof. |
---|
565 | intros. inversion H; simpl; auto. |
---|
566 | Qed. |
---|
567 | |
---|
568 | Theorem bool_of_false_val_inv: |
---|
569 | forall v b, is_false v -> bool_of_val v b -> b = false. |
---|
570 | Proof. |
---|
571 | intros. inversion H0; subst v b; simpl in H. |
---|
572 | congruence. auto. contradiction. |
---|
573 | Qed. |
---|
574 | |
---|
575 | Theorem notbool_negb_1: |
---|
576 | forall b, of_bool (negb b) = notbool (of_bool b). |
---|
577 | Proof. |
---|
578 | destruct b; reflexivity. |
---|
579 | Qed. |
---|
580 | |
---|
581 | Theorem notbool_negb_2: |
---|
582 | forall b, of_bool b = notbool (of_bool (negb b)). |
---|
583 | Proof. |
---|
584 | destruct b; reflexivity. |
---|
585 | Qed. |
---|
586 | |
---|
587 | Theorem notbool_idem2: |
---|
588 | forall b, notbool(notbool(of_bool b)) = of_bool b. |
---|
589 | Proof. |
---|
590 | destruct b; reflexivity. |
---|
591 | Qed. |
---|
592 | |
---|
593 | Theorem notbool_idem3: |
---|
594 | forall x, notbool(notbool(notbool x)) = notbool x. |
---|
595 | Proof. |
---|
596 | destruct x; simpl; auto. |
---|
597 | case (Int.eq i Int.zero); reflexivity. |
---|
598 | Qed. |
---|
599 | |
---|
600 | Theorem add_commut: forall x y, add x y = add y x. |
---|
601 | Proof. |
---|
602 | destruct x; destruct y; simpl; auto. |
---|
603 | decEq. apply Int.add_commut. |
---|
604 | Qed. |
---|
605 | |
---|
606 | Theorem add_assoc: forall x y z, add (add x y) z = add x (add y z). |
---|
607 | Proof. |
---|
608 | destruct x; destruct y; destruct z; simpl; auto. |
---|
609 | rewrite Int.add_assoc; auto. |
---|
610 | rewrite Int.add_assoc; auto. |
---|
611 | decEq. decEq. apply Int.add_commut. |
---|
612 | decEq. rewrite Int.add_commut. rewrite <- Int.add_assoc. |
---|
613 | decEq. apply Int.add_commut. |
---|
614 | decEq. rewrite Int.add_assoc. auto. |
---|
615 | Qed. |
---|
616 | |
---|
617 | Theorem add_permut: forall x y z, add x (add y z) = add y (add x z). |
---|
618 | Proof. |
---|
619 | intros. rewrite (add_commut y z). rewrite <- add_assoc. apply add_commut. |
---|
620 | Qed. |
---|
621 | |
---|
622 | Theorem add_permut_4: |
---|
623 | forall x y z t, add (add x y) (add z t) = add (add x z) (add y t). |
---|
624 | Proof. |
---|
625 | intros. rewrite add_permut. rewrite add_assoc. |
---|
626 | rewrite add_permut. symmetry. apply add_assoc. |
---|
627 | Qed. |
---|
628 | |
---|
629 | Theorem neg_zero: neg Vzero = Vzero. |
---|
630 | Proof. |
---|
631 | reflexivity. |
---|
632 | Qed. |
---|
633 | |
---|
634 | Theorem neg_add_distr: forall x y, neg(add x y) = add (neg x) (neg y). |
---|
635 | Proof. |
---|
636 | destruct x; destruct y; simpl; auto. decEq. apply Int.neg_add_distr. |
---|
637 | Qed. |
---|
638 | |
---|
639 | Theorem sub_zero_r: forall x, sub Vzero x = neg x. |
---|
640 | Proof. |
---|
641 | destruct x; simpl; auto. |
---|
642 | Qed. |
---|
643 | |
---|
644 | Theorem sub_add_opp: forall x y, sub x (Vint y) = add x (Vint (Int.neg y)). |
---|
645 | Proof. |
---|
646 | destruct x; intro y; simpl; auto; rewrite Int.sub_add_opp; auto. |
---|
647 | Qed. |
---|
648 | |
---|
649 | Theorem sub_opp_add: forall x y, sub x (Vint (Int.neg y)) = add x (Vint y). |
---|
650 | Proof. |
---|
651 | intros. unfold sub, add. |
---|
652 | destruct x; auto; rewrite Int.sub_add_opp; rewrite Int.neg_involutive; auto. |
---|
653 | Qed. |
---|
654 | |
---|
655 | Theorem sub_add_l: |
---|
656 | forall v1 v2 i, sub (add v1 (Vint i)) v2 = add (sub v1 v2) (Vint i). |
---|
657 | Proof. |
---|
658 | destruct v1; destruct v2; intros; simpl; auto. |
---|
659 | rewrite Int.sub_add_l. auto. |
---|
660 | rewrite Int.sub_add_l. auto. |
---|
661 | case (zeq b b0); intro. rewrite Int.sub_add_l. auto. reflexivity. |
---|
662 | Qed. |
---|
663 | |
---|
664 | Theorem sub_add_r: |
---|
665 | forall v1 v2 i, sub v1 (add v2 (Vint i)) = add (sub v1 v2) (Vint (Int.neg i)). |
---|
666 | Proof. |
---|
667 | destruct v1; destruct v2; intros; simpl; auto. |
---|
668 | rewrite Int.sub_add_r. auto. |
---|
669 | repeat rewrite Int.sub_add_opp. decEq. |
---|
670 | repeat rewrite Int.add_assoc. decEq. apply Int.add_commut. |
---|
671 | decEq. repeat rewrite Int.sub_add_opp. |
---|
672 | rewrite Int.add_assoc. decEq. apply Int.neg_add_distr. |
---|
673 | case (zeq b b0); intro. simpl. decEq. |
---|
674 | repeat rewrite Int.sub_add_opp. rewrite Int.add_assoc. decEq. |
---|
675 | apply Int.neg_add_distr. |
---|
676 | reflexivity. |
---|
677 | Qed. |
---|
678 | |
---|
679 | Theorem mul_commut: forall x y, mul x y = mul y x. |
---|
680 | Proof. |
---|
681 | destruct x; destruct y; simpl; auto. decEq. apply Int.mul_commut. |
---|
682 | Qed. |
---|
683 | |
---|
684 | Theorem mul_assoc: forall x y z, mul (mul x y) z = mul x (mul y z). |
---|
685 | Proof. |
---|
686 | destruct x; destruct y; destruct z; simpl; auto. |
---|
687 | decEq. apply Int.mul_assoc. |
---|
688 | Qed. |
---|
689 | |
---|
690 | Theorem mul_add_distr_l: |
---|
691 | forall x y z, mul (add x y) z = add (mul x z) (mul y z). |
---|
692 | Proof. |
---|
693 | destruct x; destruct y; destruct z; simpl; auto. |
---|
694 | decEq. apply Int.mul_add_distr_l. |
---|
695 | Qed. |
---|
696 | |
---|
697 | |
---|
698 | Theorem mul_add_distr_r: |
---|
699 | forall x y z, mul x (add y z) = add (mul x y) (mul x z). |
---|
700 | Proof. |
---|
701 | destruct x; destruct y; destruct z; simpl; auto. |
---|
702 | decEq. apply Int.mul_add_distr_r. |
---|
703 | Qed. |
---|
704 | |
---|
705 | Theorem mul_pow2: |
---|
706 | forall x n logn, |
---|
707 | Int.is_power2 n = Some logn -> |
---|
708 | mul x (Vint n) = shl x (Vint logn). |
---|
709 | Proof. |
---|
710 | intros; destruct x; simpl; auto. |
---|
711 | change 32 with (Z_of_nat Int.wordsize). |
---|
712 | rewrite (Int.is_power2_range _ _ H). decEq. apply Int.mul_pow2. auto. |
---|
713 | Qed. |
---|
714 | |
---|
715 | Theorem mods_divs: |
---|
716 | forall x y, mods x y = sub x (mul (divs x y) y). |
---|
717 | Proof. |
---|
718 | destruct x; destruct y; simpl; auto. |
---|
719 | case (Int.eq i0 Int.zero); simpl. auto. decEq. apply Int.mods_divs. |
---|
720 | Qed. |
---|
721 | |
---|
722 | Theorem modu_divu: |
---|
723 | forall x y, modu x y = sub x (mul (divu x y) y). |
---|
724 | Proof. |
---|
725 | destruct x; destruct y; simpl; auto. |
---|
726 | generalize (Int.eq_spec i0 Int.zero); |
---|
727 | case (Int.eq i0 Int.zero); simpl. auto. |
---|
728 | intro. decEq. apply Int.modu_divu. auto. |
---|
729 | Qed. |
---|
730 | |
---|
731 | Theorem divs_pow2: |
---|
732 | forall x n logn, |
---|
733 | Int.is_power2 n = Some logn -> |
---|
734 | divs x (Vint n) = shrx x (Vint logn). |
---|
735 | Proof. |
---|
736 | intros; destruct x; simpl; auto. |
---|
737 | change 32 with (Z_of_nat Int.wordsize). |
---|
738 | rewrite (Int.is_power2_range _ _ H). |
---|
739 | generalize (Int.eq_spec n Int.zero); |
---|
740 | case (Int.eq n Int.zero); intro. |
---|
741 | subst n. compute in H. discriminate. |
---|
742 | decEq. apply Int.divs_pow2. auto. |
---|
743 | Qed. |
---|
744 | |
---|
745 | Theorem divu_pow2: |
---|
746 | forall x n logn, |
---|
747 | Int.is_power2 n = Some logn -> |
---|
748 | divu x (Vint n) = shru x (Vint logn). |
---|
749 | Proof. |
---|
750 | intros; destruct x; simpl; auto. |
---|
751 | change 32 with (Z_of_nat Int.wordsize). |
---|
752 | rewrite (Int.is_power2_range _ _ H). |
---|
753 | generalize (Int.eq_spec n Int.zero); |
---|
754 | case (Int.eq n Int.zero); intro. |
---|
755 | subst n. compute in H. discriminate. |
---|
756 | decEq. apply Int.divu_pow2. auto. |
---|
757 | Qed. |
---|
758 | |
---|
759 | Theorem modu_pow2: |
---|
760 | forall x n logn, |
---|
761 | Int.is_power2 n = Some logn -> |
---|
762 | modu x (Vint n) = and x (Vint (Int.sub n Int.one)). |
---|
763 | Proof. |
---|
764 | intros; destruct x; simpl; auto. |
---|
765 | generalize (Int.eq_spec n Int.zero); |
---|
766 | case (Int.eq n Int.zero); intro. |
---|
767 | subst n. compute in H. discriminate. |
---|
768 | decEq. eapply Int.modu_and; eauto. |
---|
769 | Qed. |
---|
770 | |
---|
771 | Theorem and_commut: forall x y, and x y = and y x. |
---|
772 | Proof. |
---|
773 | destruct x; destruct y; simpl; auto. decEq. apply Int.and_commut. |
---|
774 | Qed. |
---|
775 | |
---|
776 | Theorem and_assoc: forall x y z, and (and x y) z = and x (and y z). |
---|
777 | Proof. |
---|
778 | destruct x; destruct y; destruct z; simpl; auto. |
---|
779 | decEq. apply Int.and_assoc. |
---|
780 | Qed. |
---|
781 | |
---|
782 | Theorem or_commut: forall x y, or x y = or y x. |
---|
783 | Proof. |
---|
784 | destruct x; destruct y; simpl; auto. decEq. apply Int.or_commut. |
---|
785 | Qed. |
---|
786 | |
---|
787 | Theorem or_assoc: forall x y z, or (or x y) z = or x (or y z). |
---|
788 | Proof. |
---|
789 | destruct x; destruct y; destruct z; simpl; auto. |
---|
790 | decEq. apply Int.or_assoc. |
---|
791 | Qed. |
---|
792 | |
---|
793 | Theorem xor_commut: forall x y, xor x y = xor y x. |
---|
794 | Proof. |
---|
795 | destruct x; destruct y; simpl; auto. decEq. apply Int.xor_commut. |
---|
796 | Qed. |
---|
797 | |
---|
798 | Theorem xor_assoc: forall x y z, xor (xor x y) z = xor x (xor y z). |
---|
799 | Proof. |
---|
800 | destruct x; destruct y; destruct z; simpl; auto. |
---|
801 | decEq. apply Int.xor_assoc. |
---|
802 | Qed. |
---|
803 | |
---|
804 | Theorem shl_mul: forall x y, Val.mul x (Val.shl Vone y) = Val.shl x y. |
---|
805 | Proof. |
---|
806 | destruct x; destruct y; simpl; auto. |
---|
807 | case (Int.ltu i0 Int.iwordsize); auto. |
---|
808 | decEq. symmetry. apply Int.shl_mul. |
---|
809 | Qed. |
---|
810 | |
---|
811 | Theorem shl_rolm: |
---|
812 | forall x n, |
---|
813 | Int.ltu n Int.iwordsize = true -> |
---|
814 | shl x (Vint n) = rolm x n (Int.shl Int.mone n). |
---|
815 | Proof. |
---|
816 | intros; destruct x; simpl; auto. |
---|
817 | rewrite H. decEq. apply Int.shl_rolm. exact H. |
---|
818 | Qed. |
---|
819 | |
---|
820 | Theorem shru_rolm: |
---|
821 | forall x n, |
---|
822 | Int.ltu n Int.iwordsize = true -> |
---|
823 | shru x (Vint n) = rolm x (Int.sub Int.iwordsize n) (Int.shru Int.mone n). |
---|
824 | Proof. |
---|
825 | intros; destruct x; simpl; auto. |
---|
826 | rewrite H. decEq. apply Int.shru_rolm. exact H. |
---|
827 | Qed. |
---|
828 | |
---|
829 | Theorem shrx_carry: |
---|
830 | forall x y, |
---|
831 | add (shr x y) (shr_carry x y) = shrx x y. |
---|
832 | Proof. |
---|
833 | destruct x; destruct y; simpl; auto. |
---|
834 | case (Int.ltu i0 Int.iwordsize); auto. |
---|
835 | simpl. decEq. apply Int.shrx_carry. |
---|
836 | Qed. |
---|
837 | |
---|
838 | Theorem or_rolm: |
---|
839 | forall x n m1 m2, |
---|
840 | or (rolm x n m1) (rolm x n m2) = rolm x n (Int.or m1 m2). |
---|
841 | Proof. |
---|
842 | intros; destruct x; simpl; auto. |
---|
843 | decEq. apply Int.or_rolm. |
---|
844 | Qed. |
---|
845 | |
---|
846 | Theorem rolm_rolm: |
---|
847 | forall x n1 m1 n2 m2, |
---|
848 | rolm (rolm x n1 m1) n2 m2 = |
---|
849 | rolm x (Int.modu (Int.add n1 n2) Int.iwordsize) |
---|
850 | (Int.and (Int.rol m1 n2) m2). |
---|
851 | Proof. |
---|
852 | intros; destruct x; simpl; auto. |
---|
853 | decEq. |
---|
854 | apply Int.rolm_rolm. apply int_wordsize_divides_modulus. |
---|
855 | Qed. |
---|
856 | |
---|
857 | Theorem rolm_zero: |
---|
858 | forall x m, |
---|
859 | rolm x Int.zero m = and x (Vint m). |
---|
860 | Proof. |
---|
861 | intros; destruct x; simpl; auto. decEq. apply Int.rolm_zero. |
---|
862 | Qed. |
---|
863 | |
---|
864 | Theorem addf_commut: forall x y, addf x y = addf y x. |
---|
865 | Proof. |
---|
866 | destruct x; destruct y; simpl; auto. decEq. apply Float.addf_commut. |
---|
867 | Qed. |
---|
868 | |
---|
869 | Lemma negate_cmp_mismatch: |
---|
870 | forall c, |
---|
871 | cmp_mismatch (negate_comparison c) = notbool(cmp_mismatch c). |
---|
872 | Proof. |
---|
873 | destruct c; reflexivity. |
---|
874 | Qed. |
---|
875 | |
---|
876 | Theorem negate_cmp: |
---|
877 | forall c x y, |
---|
878 | cmp (negate_comparison c) x y = notbool (cmp c x y). |
---|
879 | Proof. |
---|
880 | destruct x; destruct y; simpl; auto. |
---|
881 | rewrite Int.negate_cmp. apply notbool_negb_1. |
---|
882 | case (Int.eq i Int.zero). apply negate_cmp_mismatch. reflexivity. |
---|
883 | case (Int.eq i0 Int.zero). apply negate_cmp_mismatch. reflexivity. |
---|
884 | case (zeq b b0); intro. |
---|
885 | rewrite Int.negate_cmp. apply notbool_negb_1. |
---|
886 | apply negate_cmp_mismatch. |
---|
887 | Qed. |
---|
888 | |
---|
889 | Theorem negate_cmpu: |
---|
890 | forall c x y, |
---|
891 | cmpu (negate_comparison c) x y = notbool (cmpu c x y). |
---|
892 | Proof. |
---|
893 | destruct x; destruct y; simpl; auto. |
---|
894 | rewrite Int.negate_cmpu. apply notbool_negb_1. |
---|
895 | case (Int.eq i Int.zero). apply negate_cmp_mismatch. reflexivity. |
---|
896 | case (Int.eq i0 Int.zero). apply negate_cmp_mismatch. reflexivity. |
---|
897 | case (zeq b b0); intro. |
---|
898 | rewrite Int.negate_cmpu. apply notbool_negb_1. |
---|
899 | apply negate_cmp_mismatch. |
---|
900 | Qed. |
---|
901 | |
---|
902 | Lemma swap_cmp_mismatch: |
---|
903 | forall c, cmp_mismatch (swap_comparison c) = cmp_mismatch c. |
---|
904 | Proof. |
---|
905 | destruct c; reflexivity. |
---|
906 | Qed. |
---|
907 | |
---|
908 | Theorem swap_cmp: |
---|
909 | forall c x y, |
---|
910 | cmp (swap_comparison c) x y = cmp c y x. |
---|
911 | Proof. |
---|
912 | destruct x; destruct y; simpl; auto. |
---|
913 | rewrite Int.swap_cmp. auto. |
---|
914 | case (Int.eq i Int.zero). apply swap_cmp_mismatch. auto. |
---|
915 | case (Int.eq i0 Int.zero). apply swap_cmp_mismatch. auto. |
---|
916 | case (zeq b b0); intro. |
---|
917 | subst b0. rewrite zeq_true. rewrite Int.swap_cmp. auto. |
---|
918 | rewrite zeq_false. apply swap_cmp_mismatch. auto. |
---|
919 | Qed. |
---|
920 | |
---|
921 | Theorem swap_cmpu: |
---|
922 | forall c x y, |
---|
923 | cmpu (swap_comparison c) x y = cmpu c y x. |
---|
924 | Proof. |
---|
925 | destruct x; destruct y; simpl; auto. |
---|
926 | rewrite Int.swap_cmpu. auto. |
---|
927 | case (Int.eq i Int.zero). apply swap_cmp_mismatch. auto. |
---|
928 | case (Int.eq i0 Int.zero). apply swap_cmp_mismatch. auto. |
---|
929 | case (zeq b b0); intro. |
---|
930 | subst b0. rewrite zeq_true. rewrite Int.swap_cmpu. auto. |
---|
931 | rewrite zeq_false. apply swap_cmp_mismatch. auto. |
---|
932 | Qed. |
---|
933 | |
---|
934 | Theorem negate_cmpf_eq: |
---|
935 | forall v1 v2, notbool (cmpf Cne v1 v2) = cmpf Ceq v1 v2. |
---|
936 | Proof. |
---|
937 | destruct v1; destruct v2; simpl; auto. |
---|
938 | rewrite Float.cmp_ne_eq. rewrite notbool_negb_1. |
---|
939 | apply notbool_idem2. |
---|
940 | Qed. |
---|
941 | |
---|
942 | Theorem negate_cmpf_ne: |
---|
943 | forall v1 v2, notbool (cmpf Ceq v1 v2) = cmpf Cne v1 v2. |
---|
944 | Proof. |
---|
945 | destruct v1; destruct v2; simpl; auto. |
---|
946 | rewrite Float.cmp_ne_eq. rewrite notbool_negb_1. auto. |
---|
947 | Qed. |
---|
948 | |
---|
949 | Lemma or_of_bool: |
---|
950 | forall b1 b2, or (of_bool b1) (of_bool b2) = of_bool (b1 || b2). |
---|
951 | Proof. |
---|
952 | destruct b1; destruct b2; reflexivity. |
---|
953 | Qed. |
---|
954 | |
---|
955 | Theorem cmpf_le: |
---|
956 | forall v1 v2, cmpf Cle v1 v2 = or (cmpf Clt v1 v2) (cmpf Ceq v1 v2). |
---|
957 | Proof. |
---|
958 | destruct v1; destruct v2; simpl; auto. |
---|
959 | rewrite or_of_bool. decEq. apply Float.cmp_le_lt_eq. |
---|
960 | Qed. |
---|
961 | |
---|
962 | Theorem cmpf_ge: |
---|
963 | forall v1 v2, cmpf Cge v1 v2 = or (cmpf Cgt v1 v2) (cmpf Ceq v1 v2). |
---|
964 | Proof. |
---|
965 | destruct v1; destruct v2; simpl; auto. |
---|
966 | rewrite or_of_bool. decEq. apply Float.cmp_ge_gt_eq. |
---|
967 | Qed. |
---|
968 | |
---|
969 | Definition is_bool (v: val) := |
---|
970 | v = Vundef \/ v = Vtrue \/ v = Vfalse. |
---|
971 | |
---|
972 | Lemma of_bool_is_bool: |
---|
973 | forall b, is_bool (of_bool b). |
---|
974 | Proof. |
---|
975 | destruct b; unfold is_bool; simpl; tauto. |
---|
976 | Qed. |
---|
977 | |
---|
978 | Lemma undef_is_bool: is_bool Vundef. |
---|
979 | Proof. |
---|
980 | unfold is_bool; tauto. |
---|
981 | Qed. |
---|
982 | |
---|
983 | Lemma cmp_mismatch_is_bool: |
---|
984 | forall c, is_bool (cmp_mismatch c). |
---|
985 | Proof. |
---|
986 | destruct c; simpl; unfold is_bool; tauto. |
---|
987 | Qed. |
---|
988 | |
---|
989 | Lemma cmp_is_bool: |
---|
990 | forall c v1 v2, is_bool (cmp c v1 v2). |
---|
991 | Proof. |
---|
992 | destruct v1; destruct v2; simpl; try apply undef_is_bool. |
---|
993 | apply of_bool_is_bool. |
---|
994 | case (Int.eq i Int.zero). apply cmp_mismatch_is_bool. apply undef_is_bool. |
---|
995 | case (Int.eq i0 Int.zero). apply cmp_mismatch_is_bool. apply undef_is_bool. |
---|
996 | case (zeq b b0); intro. apply of_bool_is_bool. apply cmp_mismatch_is_bool. |
---|
997 | Qed. |
---|
998 | |
---|
999 | Lemma cmpu_is_bool: |
---|
1000 | forall c v1 v2, is_bool (cmpu c v1 v2). |
---|
1001 | Proof. |
---|
1002 | destruct v1; destruct v2; simpl; try apply undef_is_bool. |
---|
1003 | apply of_bool_is_bool. |
---|
1004 | case (Int.eq i Int.zero). apply cmp_mismatch_is_bool. apply undef_is_bool. |
---|
1005 | case (Int.eq i0 Int.zero). apply cmp_mismatch_is_bool. apply undef_is_bool. |
---|
1006 | case (zeq b b0); intro. apply of_bool_is_bool. apply cmp_mismatch_is_bool. |
---|
1007 | Qed. |
---|
1008 | |
---|
1009 | Lemma cmpf_is_bool: |
---|
1010 | forall c v1 v2, is_bool (cmpf c v1 v2). |
---|
1011 | Proof. |
---|
1012 | destruct v1; destruct v2; simpl; |
---|
1013 | apply undef_is_bool || apply of_bool_is_bool. |
---|
1014 | Qed. |
---|
1015 | |
---|
1016 | Lemma notbool_is_bool: |
---|
1017 | forall v, is_bool (notbool v). |
---|
1018 | Proof. |
---|
1019 | destruct v; simpl. |
---|
1020 | apply undef_is_bool. apply of_bool_is_bool. |
---|
1021 | apply undef_is_bool. unfold is_bool; tauto. |
---|
1022 | Qed. |
---|
1023 | |
---|
1024 | Lemma notbool_xor: |
---|
1025 | forall v, is_bool v -> v = xor (notbool v) Vone. |
---|
1026 | Proof. |
---|
1027 | intros. elim H; intro. |
---|
1028 | subst v. reflexivity. |
---|
1029 | elim H0; intro; subst v; reflexivity. |
---|
1030 | Qed. |
---|
1031 | |
---|
1032 | Lemma rolm_lt_zero: |
---|
1033 | forall v, rolm v Int.one Int.one = cmp Clt v (Vint Int.zero). |
---|
1034 | Proof. |
---|
1035 | intros. destruct v; simpl; auto. |
---|
1036 | transitivity (Vint (Int.shru i (Int.repr (Z_of_nat Int.wordsize - 1)))). |
---|
1037 | decEq. symmetry. rewrite Int.shru_rolm. auto. auto. |
---|
1038 | rewrite Int.shru_lt_zero. destruct (Int.lt i Int.zero); auto. |
---|
1039 | Qed. |
---|
1040 | |
---|
1041 | Lemma rolm_ge_zero: |
---|
1042 | forall v, |
---|
1043 | xor (rolm v Int.one Int.one) (Vint Int.one) = cmp Cge v (Vint Int.zero). |
---|
1044 | Proof. |
---|
1045 | intros. rewrite rolm_lt_zero. destruct v; simpl; auto. |
---|
1046 | destruct (Int.lt i Int.zero); auto. |
---|
1047 | Qed. |
---|
1048 | *) |
---|
1049 | (* * The ``is less defined'' relation between values. |
---|
1050 | A value is less defined than itself, and [Vundef] is |
---|
1051 | less defined than any value. *) |
---|
1052 | |
---|
1053 | inductive Val_lessdef: val → val → Prop ≝ |
---|
1054 | | lessdef_refl: ∀v. Val_lessdef v v |
---|
1055 | | lessdef_undef: ∀v. Val_lessdef Vundef v. |
---|
1056 | |
---|
1057 | inductive lessdef_list: list val → list val → Prop ≝ |
---|
1058 | | lessdef_list_nil: |
---|
1059 | lessdef_list (nil ?) (nil ?) |
---|
1060 | | lessdef_list_cons: |
---|
1061 | ∀v1,v2,vl1,vl2. |
---|
1062 | Val_lessdef v1 v2 → lessdef_list vl1 vl2 → |
---|
1063 | lessdef_list (v1 :: vl1) (v2 :: vl2). |
---|
1064 | |
---|
1065 | (*Hint Resolve lessdef_refl lessdef_undef lessdef_list_nil lessdef_list_cons.*) |
---|
1066 | (* |
---|
1067 | lemma lessdef_list_inv: |
---|
1068 | ∀vl1,vl2. lessdef_list vl1 vl2 → vl1 = vl2 ∨ in_list ? Vundef vl1. |
---|
1069 | #vl1 elim vl1; |
---|
1070 | [ #vl2 #H inversion H; /2/; #h1 #h2 #t1 #t2 #H1 #H2 #H3 #Hbad destruct |
---|
1071 | | #h #t #IH #vl2 #H |
---|
1072 | inversion H; |
---|
1073 | [ #H' destruct |
---|
1074 | | #h1 #h2 #t1 #t2 #H1 #H2 #H3 #e1 #e2 destruct; |
---|
1075 | elim H1; |
---|
1076 | [ elim (IH t2 H2); |
---|
1077 | [ #e destruct; /2/; |
---|
1078 | | /3/ ] |
---|
1079 | | /3/ ] |
---|
1080 | ] |
---|
1081 | ] qed. |
---|
1082 | *) |
---|
1083 | lemma load_result_lessdef: |
---|
1084 | ∀chunk,v1,v2. |
---|
1085 | Val_lessdef v1 v2 → Val_lessdef (load_result chunk v1) (load_result chunk v2). |
---|
1086 | #chunk #v1 #v2 #H inversion H; //; #v #e1 #e2 #e3 cases chunk |
---|
1087 | [ 8: #r ] whd in ⊢ (?%?); //; |
---|
1088 | qed. |
---|
1089 | |
---|
1090 | lemma zero_ext_lessdef: |
---|
1091 | ∀n,v1,v2. Val_lessdef v1 v2 → Val_lessdef (zero_ext n v1) (zero_ext n v2). |
---|
1092 | #n #v1 #v2 #H inversion H // |
---|
1093 | qed. |
---|
1094 | |
---|
1095 | lemma sign_ext_lessdef: |
---|
1096 | ∀n,v1,v2. Val_lessdef v1 v2 → Val_lessdef (sign_ext n v1) (sign_ext n v2). |
---|
1097 | #n #v1 #v2 #H inversion H // |
---|
1098 | qed. |
---|
1099 | (* |
---|
1100 | Lemma singleoffloat_lessdef: |
---|
1101 | forall v1 v2, lessdef v1 v2 -> lessdef (singleoffloat v1) (singleoffloat v2). |
---|
1102 | Proof. |
---|
1103 | intros; inv H; simpl; auto. |
---|
1104 | Qed. |
---|
1105 | |
---|
1106 | End Val. |
---|
1107 | *) |
---|