source: C-semantics/Values.ma @ 153

Last change on this file since 153 was 153, checked in by campbell, 10 years ago

Use appropriate memory chunks for 8051 pointers.

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