1 | include "ASM/BitVectorZ.ma". |
---|
2 | include "common/AST.ma". (* For the regions *) |
---|
3 | include "utilities/extralib.ma". |
---|
4 | |
---|
5 | |
---|
6 | (* To define pointers we need a few details about the memory model. |
---|
7 | |
---|
8 | There are several kinds of pointers, which differ in which regions of |
---|
9 | memory they address and the pointer's representation. |
---|
10 | |
---|
11 | Pointers are given as kind, block, offset triples, where a block identifies |
---|
12 | some memory in a given region with an arbitrary concrete address. A proof |
---|
13 | is also required that the representation is suitable for the region the |
---|
14 | memory resides in. Note that blocks cannot extend out of a region (in |
---|
15 | particular, a pdata pointer can address any byte in a pdata block - we never |
---|
16 | need to switch to a larger xdata pointer). |
---|
17 | *) |
---|
18 | |
---|
19 | (* blocks - represented by the region the memory resides in and a unique id *) |
---|
20 | |
---|
21 | record block : Type[0] ≝ |
---|
22 | { block_region : region |
---|
23 | ; block_id : Z |
---|
24 | }. |
---|
25 | |
---|
26 | definition eq_block ≝ |
---|
27 | λb1,b2. |
---|
28 | eq_region (block_region b1) (block_region b2) ∧ |
---|
29 | eqZb (block_id b1) (block_id b2) |
---|
30 | . |
---|
31 | |
---|
32 | lemma eq_block_elim : ∀P:bool → Prop. ∀b1,b2. |
---|
33 | (b1 = b2 → P true) → (b1 ≠ b2 → P false) → |
---|
34 | P (eq_block b1 b2). |
---|
35 | #P * #r1 #i1 * #r2 #i2 #H1 #H2 |
---|
36 | whd in ⊢ (?%); @eq_region_elim #H3 |
---|
37 | [ whd in ⊢ (?%); @eqZb_elim [ /2/ | * #NE @H2 % #E @NE destruct % ] |
---|
38 | | @H2 % #E destruct elim H3 /2/ |
---|
39 | ] qed. |
---|
40 | |
---|
41 | lemma eq_block_b_b : ∀b. eq_block b b = true. |
---|
42 | * * #z whd in ⊢ (??%?); >eqZb_z_z @refl |
---|
43 | qed. |
---|
44 | |
---|
45 | definition block_eq : DeqSet ≝ mk_DeqSet block eq_block ?. |
---|
46 | #x #y @eq_block_elim |
---|
47 | [ #E destruct /2/ |
---|
48 | | * #NE % #E destruct cases (NE (refl ??)) |
---|
49 | ] qed. |
---|
50 | |
---|
51 | (* This is only required for full 8051 memory spaces. |
---|
52 | |
---|
53 | (* Characterise the memory regions which the pointer representations can |
---|
54 | address. |
---|
55 | |
---|
56 | pointer_compat <block> <pointer representation> *) |
---|
57 | |
---|
58 | inductive pointer_compat : block → region → Prop ≝ |
---|
59 | | same_compat : ∀s,id. pointer_compat (mk_block s id) s |
---|
60 | | pxdata_compat : ∀id. pointer_compat (mk_block PData id) XData |
---|
61 | | universal_compat : ∀r,id. pointer_compat (mk_block r id) Any. |
---|
62 | |
---|
63 | lemma pointer_compat_dec : ∀b,p. pointer_compat b p + ¬pointer_compat b p. |
---|
64 | * * #id *; |
---|
65 | try ( %1 // ) |
---|
66 | %2 % #H inversion H #e1 #e2 try #e3 try #e4 destruct |
---|
67 | qed. |
---|
68 | |
---|
69 | definition is_pointer_compat : block → region → bool ≝ |
---|
70 | λb,p. match pointer_compat_dec b p with [ inl _ ⇒ true | inr _ ⇒ false ]. |
---|
71 | *) |
---|
72 | |
---|
73 | (* Offsets into the block. We allow integers like CompCert so that we have |
---|
74 | the option of extending blocks backwards. *) |
---|
75 | |
---|
76 | definition offset_size ≝ 8*size_pointer. |
---|
77 | |
---|
78 | (* CSC: why do we need this awful indirection? *) |
---|
79 | record offset : Type[0] ≝ { offv : BitVector offset_size }. |
---|
80 | |
---|
81 | definition eq_offset ≝ λx,y. eq_bv ? (offv x) (offv y). |
---|
82 | |
---|
83 | lemma reflexive_eq_offset: ∀x. eq_offset x x = true. |
---|
84 | whd in match eq_offset; /2/ |
---|
85 | qed. |
---|
86 | |
---|
87 | lemma eq_offset_elim : ∀P : bool → Prop.∀o1,o2. |
---|
88 | (o1 = o2 → P true) → (o1 ≠ o2 → P false) → P (eq_offset o1 o2). |
---|
89 | #P * #o1 * #o2 #T #F |
---|
90 | change with (eq_bv ???) in ⊢ (?%); |
---|
91 | @eq_bv_elim |
---|
92 | [ /2/ |
---|
93 | | * #FF @F % #E destruct /2/ |
---|
94 | ] qed. |
---|
95 | |
---|
96 | definition offset_of_Z : Z → offset ≝ |
---|
97 | λz. mk_offset (bitvector_of_Z … z). |
---|
98 | definition shift_offset : ∀n. offset → BitVector n → offset ≝ |
---|
99 | λn,o,i. mk_offset (addition_n ? (offv o) (sign_ext … i)). |
---|
100 | (* A version of shift_offset_n for multiplied addresses which avoids overflow. *) |
---|
101 | definition shift_offset_n : ∀n. offset → nat → BitVector n → offset ≝ |
---|
102 | λn,o,i,j. mk_offset (addition_n ? (offv o) (short_multiplication ? (bitvector_of_nat … i) (sign_ext … j))). |
---|
103 | definition neg_shift_offset : ∀n. offset → BitVector n → offset ≝ |
---|
104 | λn,o,i. mk_offset (subtraction ? (offv o) (sign_ext … i)). |
---|
105 | definition neg_shift_offset_n : ∀n. offset → nat → BitVector n → offset ≝ |
---|
106 | λn,o,i,j. mk_offset (subtraction ? (offv o) (short_multiplication ? (bitvector_of_nat … i) (sign_ext … j))). |
---|
107 | definition sub_offset : ∀n. offset → offset → BitVector n ≝ |
---|
108 | λn,x,y. sign_ext … (subtraction ? (offv x) (offv y)). |
---|
109 | definition zero_offset ≝ mk_offset (zero ?). |
---|
110 | definition lt_offset : offset → offset → bool ≝ |
---|
111 | λx,y. lt_u ? (offv x) (offv y). |
---|
112 | |
---|
113 | (*CSC: use this definition everywhere in Values.ma and Mem.ma. *) |
---|
114 | record pointer: Type[0] ≝ { |
---|
115 | (* ptype: region;*) |
---|
116 | pblock: block; |
---|
117 | (* pok: pointer_compat pblock ptype;*) |
---|
118 | poff: offset |
---|
119 | }. |
---|
120 | |
---|
121 | (* As we don't have full memory space support, grab the region from the block |
---|
122 | instead. *) |
---|
123 | definition ptype : pointer → region ≝ λp. block_region (pblock p). |
---|
124 | |
---|
125 | definition shift_pointer: ∀n. pointer → BitVector n → pointer ≝ |
---|
126 | λn,p,i. mk_pointer (*ptype p*) (pblock p) (*pok p*) (shift_offset n (poff p) i). |
---|
127 | |
---|
128 | (* multiply shift by a nat (i.e., sizeof) without danger of overflow *) |
---|
129 | definition shift_pointer_n: ∀n. pointer → nat → BitVector n → pointer ≝ |
---|
130 | λn,p,i,j. mk_pointer (*ptype p*) (pblock p) (*pok p*) (shift_offset_n n (poff p) i j). |
---|
131 | |
---|
132 | definition neg_shift_pointer: ∀n. pointer → BitVector n → pointer ≝ |
---|
133 | λn,p,i. mk_pointer (*ptype p*) (pblock p) (*pok p*) (neg_shift_offset n (poff p) i). |
---|
134 | |
---|
135 | definition neg_shift_pointer_n: ∀n. pointer → nat → BitVector n → pointer ≝ |
---|
136 | λn,p,i,j. mk_pointer (*ptype p*) (pblock p) (*pok p*) (neg_shift_offset_n n (poff p) i j). |
---|
137 | |
---|
138 | definition eq_pointer: pointer → pointer → bool ≝ |
---|
139 | λp1,p2. |
---|
140 | (*eq_region (ptype p1) (ptype p2) ∧*) (eq_block (pblock p1) (pblock p2)) ∧ |
---|
141 | eq_offset (poff p1) (poff p2). |
---|
142 | |
---|
143 | lemma reflexive_eq_pointer: ∀p. eq_pointer p p = true. |
---|
144 | #p whd in ⊢ (??%?); >eq_block_b_b >reflexive_eq_region >reflexive_eq_offset // |
---|
145 | qed. |
---|
146 | |
---|
147 | lemma eq_pointer_elim : ∀P : bool → Prop.∀p0,p1. |
---|
148 | (p0 = p1 → P true) → (p0 ≠ p1 → P false) → P (eq_pointer p0 p1). |
---|
149 | #P * #b0 * #o0 * #b1 * #o1 |
---|
150 | #Ptrue #Pfalse |
---|
151 | whd in match (eq_pointer ??); |
---|
152 | @eq_block_elim #block_eq |
---|
153 | [ @eq_offset_elim #offset_eq |
---|
154 | [ destruct @Ptrue % |
---|
155 | ] |
---|
156 | ] |
---|
157 | @Pfalse % #EQ destruct /2 by absurd/ |
---|
158 | qed. |
---|