source: C-semantics/Values.ma @ 124

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

Initial work on Clight semantics with 8051 memory spaces.

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