[639] | 1 | (* Operations common to the Cminor and RTLabs front end stages. *) |
---|
| 2 | |
---|
| 3 | (* Adapted from CompCert's Cminor.ma: *) |
---|
| 4 | |
---|
| 5 | (* *********************************************************************) |
---|
| 6 | (* *) |
---|
| 7 | (* The Compcert verified compiler *) |
---|
| 8 | (* *) |
---|
| 9 | (* Xavier Leroy, INRIA Paris-Rocquencourt *) |
---|
| 10 | (* *) |
---|
| 11 | (* Copyright Institut National de Recherche en Informatique et en *) |
---|
| 12 | (* Automatique. All rights reserved. This file is distributed *) |
---|
| 13 | (* under the terms of the GNU General Public License as published by *) |
---|
| 14 | (* the Free Software Foundation, either version 2 of the License, or *) |
---|
| 15 | (* (at your option) any later version. This file is also distributed *) |
---|
| 16 | (* under the terms of the INRIA Non-Commercial License Agreement. *) |
---|
| 17 | (* *) |
---|
| 18 | (* *********************************************************************) |
---|
| 19 | |
---|
[702] | 20 | include "common/Values.ma". |
---|
[639] | 21 | |
---|
| 22 | inductive constant : Type[0] ≝ |
---|
[961] | 23 | | Ointconst: ∀sz. bvint sz → constant (**r integer constant *) |
---|
| 24 | | Ofloatconst: float → constant (**r floating-point constant *) |
---|
| 25 | | Oaddrsymbol: ident → nat → constant (**r address of the symbol plus the offset *) |
---|
| 26 | | Oaddrstack: nat → constant. (**r stack pointer plus the given offset *) |
---|
[639] | 27 | |
---|
[1369] | 28 | definition boolsrc : typ → Prop ≝ λt. match t with [ ASTint _ _ ⇒ True | ASTptr _ ⇒ True | _ ⇒ False ]. |
---|
[639] | 29 | |
---|
[1369] | 30 | inductive unary_operation : typ → typ → Type[0] ≝ |
---|
| 31 | | Ocastint: ∀sz,sg,sz',sg'. unary_operation (ASTint sz sg) (ASTint sz' sg') (**r integer casts *) |
---|
| 32 | | Onegint: ∀sz,sg. unary_operation (ASTint sz sg) (ASTint sz sg) (**r integer opposite *) |
---|
| 33 | | Onotbool: ∀t,sz,sg. boolsrc t → unary_operation t (ASTint sz sg) (**r boolean negation *) |
---|
| 34 | | Onotint: ∀sz,sg. unary_operation (ASTint sz sg) (ASTint sz sg) (**r bitwise complement *) |
---|
| 35 | | Onegf: ∀sz. unary_operation (ASTfloat sz) (ASTfloat sz) (**r float opposite *) |
---|
| 36 | | Oabsf: ∀sz. unary_operation (ASTfloat sz) (ASTfloat sz) (**r float absolute value *) |
---|
| 37 | | Osingleoffloat: unary_operation (ASTfloat F64) (ASTfloat F32) (**r float truncation *) |
---|
| 38 | | Ointoffloat: ∀fsz,sz. unary_operation (ASTfloat fsz) (ASTint sz Signed) (**r signed integer to float *) |
---|
| 39 | | Ointuoffloat: ∀fsz,sz. unary_operation (ASTfloat fsz) (ASTint sz Unsigned) (**r unsigned integer to float *) |
---|
| 40 | | Ofloatofint: ∀fsz,sz. unary_operation (ASTint sz Signed) (ASTfloat fsz) (**r float to signed integer *) |
---|
| 41 | | Ofloatofintu: ∀fsz,sz. unary_operation (ASTint sz Unsigned) (ASTfloat fsz) (**r float to unsigned integer *) |
---|
| 42 | | Oid: ∀t. unary_operation t t (**r identity (used to move between registers *) |
---|
| 43 | | Optrofint: ∀sz,sg,r. unary_operation (ASTint sz sg) (ASTptr r) (**r int to pointer with given representation *) |
---|
| 44 | | Ointofptr: ∀sz,sg,r. unary_operation (ASTptr r) (ASTint sz sg). (**r pointer to int *) |
---|
| 45 | |
---|
[639] | 46 | inductive binary_operation : Type[0] ≝ |
---|
| 47 | | Oadd: binary_operation (**r integer addition *) |
---|
| 48 | | Osub: binary_operation (**r integer subtraction *) |
---|
| 49 | | Omul: binary_operation (**r integer multiplication *) |
---|
| 50 | | Odiv: binary_operation (**r integer signed division *) |
---|
| 51 | | Odivu: binary_operation (**r integer unsigned division *) |
---|
| 52 | | Omod: binary_operation (**r integer signed modulus *) |
---|
| 53 | | Omodu: binary_operation (**r integer unsigned modulus *) |
---|
| 54 | | Oand: binary_operation (**r bitwise ``and'' *) |
---|
| 55 | | Oor: binary_operation (**r bitwise ``or'' *) |
---|
| 56 | | Oxor: binary_operation (**r bitwise ``xor'' *) |
---|
| 57 | | Oshl: binary_operation (**r left shift *) |
---|
| 58 | | Oshr: binary_operation (**r right signed shift *) |
---|
| 59 | | Oshru: binary_operation (**r right unsigned shift *) |
---|
| 60 | | Oaddf: binary_operation (**r float addition *) |
---|
| 61 | | Osubf: binary_operation (**r float subtraction *) |
---|
| 62 | | Omulf: binary_operation (**r float multiplication *) |
---|
| 63 | | Odivf: binary_operation (**r float division *) |
---|
| 64 | | Ocmp: comparison -> binary_operation (**r integer signed comparison *) |
---|
| 65 | | Ocmpu: comparison -> binary_operation (**r integer unsigned comparison *) |
---|
[727] | 66 | | Ocmpf: comparison -> binary_operation (**r float comparison *) |
---|
| 67 | | Oaddp: binary_operation (**r add an integer to a pointer *) |
---|
[774] | 68 | | Osubpi: binary_operation (**r subtract int from a pointers *) |
---|
[961] | 69 | | Osubpp: intsize → binary_operation (**r subtract two pointers *) |
---|
[727] | 70 | | Ocmpp: comparison → binary_operation. (**r compare pointers *) |
---|
[639] | 71 | |
---|
| 72 | |
---|
| 73 | (* * Evaluation of constants and operator applications. |
---|
| 74 | [None] is returned when the computation is undefined, e.g. |
---|
| 75 | if arguments are of the wrong types, or in case of an integer division |
---|
| 76 | by zero. *) |
---|
| 77 | |
---|
| 78 | definition eval_constant : (ident → option block) → block → constant → option val ≝ |
---|
| 79 | λfind_symbol,sp,cst. |
---|
| 80 | match cst with |
---|
[961] | 81 | [ Ointconst sz n ⇒ Some ? (Vint sz n) |
---|
[639] | 82 | | Ofloatconst n ⇒ Some ? (Vfloat n) |
---|
| 83 | | Oaddrsymbol s ofs ⇒ |
---|
| 84 | match find_symbol s with |
---|
| 85 | [ None ⇒ None ? |
---|
[961] | 86 | | Some b ⇒ Some ? (Vptr Any b (match b with [ mk_block r id ⇒ universal_compat r id ]) (shift_offset ? zero_offset (repr I16 ofs))) |
---|
[639] | 87 | ] |
---|
| 88 | | Oaddrstack ofs ⇒ |
---|
[961] | 89 | Some ? (Vptr Any sp ? (shift_offset ? zero_offset (repr I16 ofs))) |
---|
[639] | 90 | ]. cases sp // qed. |
---|
| 91 | |
---|
[1369] | 92 | definition eval_unop : ∀t,t'. unary_operation t t' → val → option val ≝ |
---|
| 93 | λt,t',op,arg. |
---|
[639] | 94 | match op with |
---|
[1369] | 95 | [ Ocastint sz sg sz' sg' ⇒ |
---|
[961] | 96 | match sg with |
---|
[1369] | 97 | [ Unsigned ⇒ Some ? (zero_ext sz' arg) |
---|
| 98 | | Signed ⇒ Some ? (sign_ext sz' arg) |
---|
[961] | 99 | ] |
---|
[1369] | 100 | | Onegint sz sg ⇒ match arg with [ Vint sz1 n1 ⇒ Some ? (Vint sz1 (two_complement_negation ? n1)) | _ ⇒ None ? ] |
---|
| 101 | | Onotbool t sz sg _ ⇒ |
---|
| 102 | match arg with |
---|
| 103 | [ Vint sz1 n1 ⇒ Some ? (Vint sz (if (eq_bv ? n1 (zero ?)) then (repr ? 1) else (zero ?))) |
---|
| 104 | | Vptr _ _ _ _ ⇒ Some ? (Vint sz (zero ?)) |
---|
| 105 | | Vnull _ ⇒ Some ? (Vint sz (repr ? 1)) |
---|
| 106 | | _ ⇒ None ? |
---|
| 107 | ] |
---|
| 108 | | Onotint sz sg ⇒ match arg with [ Vint sz1 n1 ⇒ Some ? (Vint sz1 (exclusive_disjunction_bv ? n1 (mone ?))) | _ ⇒ None ? ] |
---|
| 109 | | Onegf _ ⇒ match arg with [ Vfloat f1 ⇒ Some ? (Vfloat (Fneg f1)) | _ ⇒ None ? ] |
---|
| 110 | | Oabsf _ ⇒ match arg with [ Vfloat f1 ⇒ Some ? (Vfloat (Fabs f1)) | _ ⇒ None ? ] |
---|
[639] | 111 | | Osingleoffloat ⇒ Some ? (singleoffloat arg) |
---|
[1369] | 112 | | Ointoffloat _ sz ⇒ match arg with [ Vfloat f1 ⇒ Some ? (Vint sz (intoffloat ? f1)) | _ ⇒ None ? ] |
---|
| 113 | | Ointuoffloat _ sz ⇒ match arg with [ Vfloat f1 ⇒ Some ? (Vint sz (intuoffloat ? f1)) | _ ⇒ None ? ] |
---|
| 114 | | Ofloatofint _ _ ⇒ match arg with [ Vint sz1 n1 ⇒ Some ? (Vfloat (floatofint ? n1)) | _ ⇒ None ? ] |
---|
| 115 | | Ofloatofintu _ _ ⇒ match arg with [ Vint sz1 n1 ⇒ Some ? (Vfloat (floatofintu ? n1)) | _ ⇒ None ? ] |
---|
| 116 | | Oid t ⇒ Some ? arg (* XXX should we restricted the values allowed? *) |
---|
[727] | 117 | (* Only conversion of null pointers is specified. *) |
---|
[1369] | 118 | | Optrofint sz sg r ⇒ match arg with [ Vint sz1 n1 ⇒ if eq_bv ? n1 (zero ?) then Some ? (Vnull r) else None ? | _ ⇒ None ? ] |
---|
| 119 | | Ointofptr sz sg r ⇒ match arg with [ Vnull _ ⇒ Some ? (Vint sz (zero ?)) | _ ⇒ None ? ] |
---|
[639] | 120 | ]. |
---|
| 121 | |
---|
[1369] | 122 | lemma elim_val_typ : ∀v,t. ∀P:val → Prop. |
---|
| 123 | val_typ v t → |
---|
| 124 | match t with |
---|
| 125 | [ ASTint sz sg ⇒ ∀i.P (Vint sz i) |
---|
| 126 | | ASTfloat sz ⇒ ∀f.P (Vfloat f) |
---|
| 127 | | ASTptr r ⇒ P (Vnull r) ∧ ∀b,c,o. P (Vptr r b c o) |
---|
| 128 | ] → |
---|
| 129 | P v. |
---|
| 130 | #v #t #P * |
---|
| 131 | [ 1,2: // |
---|
| 132 | | #r * // |
---|
| 133 | | #r #b #c #o * // |
---|
| 134 | ] qed. |
---|
| 135 | |
---|
| 136 | lemma eval_unop_typ_correct : ∀t,t',op,v,v'. |
---|
| 137 | val_typ v t → |
---|
| 138 | eval_unop t t' op v = Some ? v' → |
---|
| 139 | val_typ v' t'. |
---|
| 140 | #t #t' #op elim op |
---|
[1516] | 141 | [ #sz #sg #sz' #sg' #v #v' #H @(elim_val_typ … H) #i whd in ⊢ (??%? → ?); |
---|
| 142 | cases sg whd in ⊢ (??%? → ?); #E' destruct % |
---|
| 143 | | #sz #sg #v #v' #H @(elim_val_typ … H) #i whd in ⊢ (??%? → ?); #E' destruct % |
---|
[1369] | 144 | | #t'' #sz #sg cases t'' |
---|
[1516] | 145 | [ #sz' #sg' #H #v #v' #H1 @(elim_val_typ … H1) #i whd in ⊢ (??%? → ?); #E' destruct cases (eq_bv ???) whd in ⊢ (?%?); % |
---|
[1369] | 146 | | #r #H #v #v' #H1 @(elim_val_typ … H1) whd % |
---|
[1516] | 147 | [ whd in ⊢ (??%? → ?); #E' destruct; % |
---|
| 148 | | #b #c #o whd in ⊢ (??%? → ?); #E' destruct % |
---|
[1369] | 149 | ] |
---|
| 150 | | #f * |
---|
| 151 | ] |
---|
[1516] | 152 | | #sz #sg #v #v' #H @(elim_val_typ … H) #i whd in ⊢ (??%? → ?); #E destruct % |
---|
| 153 | | #sz #v #v' #H @(elim_val_typ … H) #f whd in ⊢ (??%? → ?); #E destruct %2 |
---|
| 154 | | #sz #v #v' #H @(elim_val_typ … H) #f whd in ⊢ (??%? → ?); #E destruct %2 |
---|
| 155 | | #v #v' #H @(elim_val_typ … H) #f whd in ⊢ (??%? → ?); #E destruct %2 |
---|
| 156 | | #fsz #sz #v #v' #H @(elim_val_typ … H) #f whd in ⊢ (??%? → ?); #E destruct % |
---|
| 157 | | #fsz #sz #v #v' #H @(elim_val_typ … H) #f whd in ⊢ (??%? → ?); #E destruct % |
---|
| 158 | | #fsz #sz #v #v' #H @(elim_val_typ … H) #i whd in ⊢ (??%? → ?); #E destruct %2 |
---|
| 159 | | #fsz #sz #v #v' #H @(elim_val_typ … H) #i whd in ⊢ (??%? → ?); #E destruct %2 |
---|
| 160 | | #t'' #v #v' #H whd in ⊢ (??%? → ?); #E destruct @H |
---|
| 161 | | #sz #sg #r #v #v' #H @(elim_val_typ … H) #i whd in ⊢ (??%? → ?); cases (eq_bv ???) |
---|
| 162 | whd in ⊢ (??%? → ?); #E destruct // |
---|
[1369] | 163 | | #sz #sg #r #v #v' #H @(elim_val_typ … H) % |
---|
[1516] | 164 | [ whd in ⊢ (??%? → ?); #E destruct % |
---|
| 165 | | #b #c #o whd in ⊢ (??%? → ?); #E destruct |
---|
[1369] | 166 | ] |
---|
| 167 | ] qed. |
---|
| 168 | |
---|
[639] | 169 | (* I think this is duplicated somewhere else *) |
---|
| 170 | definition eval_compare_mismatch : comparison → option val ≝ |
---|
| 171 | λc. match c with [ Ceq ⇒ Some ? Vfalse | Cne ⇒ Some ? Vtrue | _ ⇒ None ? ]. |
---|
| 172 | |
---|
[961] | 173 | (* Individual operations, adapted from Values. These differ in that they |
---|
| 174 | implement the plain Cminor/RTLabs operations (e.g., with separate addition |
---|
| 175 | for ints,floats and pointers) and use option rather than Vundef. The ones |
---|
| 176 | in Value are probably not needed. *) |
---|
[639] | 177 | |
---|
| 178 | definition ev_add ≝ λv1,v2: val. |
---|
| 179 | match v1 with |
---|
[961] | 180 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 181 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 182 | (λn1. Some ? (Vint ? (addition_n ? n1 n2))) |
---|
| 183 | (None ?) |
---|
[639] | 184 | | _ ⇒ None ? ] |
---|
| 185 | | _ ⇒ None ? ]. |
---|
| 186 | |
---|
| 187 | definition ev_sub ≝ λv1,v2: val. |
---|
| 188 | match v1 with |
---|
[961] | 189 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 190 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 191 | (λn1. Some ? (Vint ? (subtraction ? n1 n2))) |
---|
| 192 | (None ?) |
---|
[639] | 193 | | _ ⇒ None ? ] |
---|
[774] | 194 | | _ ⇒ None ? ]. |
---|
| 195 | |
---|
| 196 | (* NB: requires arguments to be presented pointer first. *) |
---|
| 197 | definition ev_addp ≝ λv1,v2: val. |
---|
| 198 | match v1 with |
---|
| 199 | [ Vptr r b1 p ofs1 ⇒ match v2 with |
---|
[961] | 200 | [ Vint sz2 n2 ⇒ Some ? (Vptr r b1 p (shift_offset ? ofs1 n2)) |
---|
[774] | 201 | | _ ⇒ None ? ] |
---|
| 202 | | Vnull r ⇒ match v2 with |
---|
[961] | 203 | [ Vint sz2 n2 ⇒ if eq_bv ? n2 (zero ?) then Some ? (Vnull r) else None ? |
---|
[774] | 204 | | _ ⇒ None ? |
---|
| 205 | ] |
---|
| 206 | | _ ⇒ None ? ]. |
---|
| 207 | |
---|
| 208 | definition ev_subpi ≝ λv1,v2: val. |
---|
| 209 | match v1 with |
---|
| 210 | [ Vptr r1 b1 p1 ofs1 ⇒ match v2 with |
---|
[961] | 211 | [ Vint sz2 n2 ⇒ Some ? (Vptr r1 b1 p1 (neg_shift_offset ? ofs1 n2)) |
---|
[774] | 212 | | _ ⇒ None ? ] |
---|
[961] | 213 | | Vnull r ⇒ match v2 with [ Vint sz2 n2 ⇒ if eq_bv ? n2 (zero ?) then Some ? (Vnull r) else None ? | _ ⇒ None ? ] |
---|
[774] | 214 | | _ ⇒ None ? ]. |
---|
| 215 | |
---|
[961] | 216 | definition ev_subpp ≝ λsz. λv1,v2: val. |
---|
[774] | 217 | match v1 with |
---|
| 218 | [ Vptr r1 b1 p1 ofs1 ⇒ match v2 with |
---|
| 219 | [ Vptr r2 b2 p2 ofs2 ⇒ |
---|
[961] | 220 | if eq_block b1 b2 then Some ? (Vint sz (sub_offset ? ofs1 ofs2)) else None ? |
---|
[639] | 221 | | _ ⇒ None ? ] |
---|
[961] | 222 | | Vnull r ⇒ match v2 with [ Vnull r' ⇒ Some ? (Vint sz (zero ?)) | _ ⇒ None ? ] |
---|
[639] | 223 | | _ ⇒ None ? ]. |
---|
| 224 | |
---|
| 225 | definition ev_mul ≝ λv1, v2: val. |
---|
| 226 | match v1 with |
---|
[961] | 227 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 228 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 229 | (λn1. Some ? (Vint ? (\snd (split … (multiplication ? n1 n2))))) |
---|
| 230 | (None ?) |
---|
[639] | 231 | | _ ⇒ None ? ] |
---|
| 232 | | _ ⇒ None ? ]. |
---|
| 233 | |
---|
| 234 | definition ev_divs ≝ λv1, v2: val. |
---|
| 235 | match v1 with |
---|
[961] | 236 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 237 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 238 | (λn1. option_map ?? (Vint ?) (division_s ? n1 n2)) |
---|
| 239 | (None ?) |
---|
[639] | 240 | | _ ⇒ None ? ] |
---|
| 241 | | _ ⇒ None ? ]. |
---|
| 242 | |
---|
| 243 | definition ev_mods ≝ λv1, v2: val. |
---|
| 244 | match v1 with |
---|
[961] | 245 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 246 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 247 | (λn1. option_map ?? (Vint ?) (modulus_s ? n1 n2)) |
---|
| 248 | (None ?) |
---|
[639] | 249 | | _ ⇒ None ? |
---|
| 250 | ] |
---|
| 251 | | _ ⇒ None ? |
---|
| 252 | ]. |
---|
| 253 | |
---|
| 254 | definition ev_divu ≝ λv1, v2: val. |
---|
| 255 | match v1 with |
---|
[961] | 256 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 257 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 258 | (λn1. option_map ?? (Vint ?) (division_u ? n1 n2)) |
---|
| 259 | (None ?) |
---|
[639] | 260 | | _ ⇒ None ? |
---|
| 261 | ] |
---|
| 262 | | _ ⇒ None ? |
---|
| 263 | ]. |
---|
| 264 | |
---|
| 265 | definition ev_modu ≝ λv1, v2: val. |
---|
| 266 | match v1 with |
---|
[961] | 267 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 268 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 269 | (λn1. option_map ?? (Vint ?) (modulus_u ? n1 n2)) |
---|
| 270 | (None ?) |
---|
[639] | 271 | | _ ⇒ None ? |
---|
| 272 | ] |
---|
| 273 | | _ ⇒ None ? |
---|
| 274 | ]. |
---|
| 275 | |
---|
| 276 | definition ev_and ≝ λv1, v2: val. |
---|
| 277 | match v1 with |
---|
[961] | 278 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 279 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 280 | (λn1. Some ? (Vint ? (conjunction_bv ? n1 n2))) |
---|
| 281 | (None ?) |
---|
[639] | 282 | | _ ⇒ None ? ] |
---|
| 283 | | _ ⇒ None ? ]. |
---|
| 284 | |
---|
| 285 | definition ev_or ≝ λv1, v2: val. |
---|
| 286 | match v1 with |
---|
[961] | 287 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 288 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 289 | (λn1. Some ? (Vint ? (inclusive_disjunction_bv ? n1 n2))) |
---|
| 290 | (None ?) |
---|
[639] | 291 | | _ ⇒ None ? ] |
---|
| 292 | | _ ⇒ None ? ]. |
---|
| 293 | |
---|
| 294 | definition ev_xor ≝ λv1, v2: val. |
---|
| 295 | match v1 with |
---|
[961] | 296 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 297 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 298 | (λn1. Some ? (Vint ? (exclusive_disjunction_bv ? n1 n2))) |
---|
| 299 | (None ?) |
---|
[639] | 300 | | _ ⇒ None ? ] |
---|
| 301 | | _ ⇒ None ? ]. |
---|
| 302 | |
---|
| 303 | definition ev_shl ≝ λv1, v2: val. |
---|
| 304 | match v1 with |
---|
[961] | 305 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 306 | [ Vint sz2 n2 ⇒ |
---|
| 307 | if lt_u ? n2 (bitvector_of_nat … (bitsize_of_intsize sz1)) |
---|
| 308 | then Some ? (Vint sz1 (shift_left ?? (nat_of_bitvector … n2) n1 false)) |
---|
[639] | 309 | else None ? |
---|
| 310 | | _ ⇒ None ? ] |
---|
| 311 | | _ ⇒ None ? ]. |
---|
| 312 | |
---|
| 313 | definition ev_shr ≝ λv1, v2: val. |
---|
| 314 | match v1 with |
---|
[961] | 315 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 316 | [ Vint sz2 n2 ⇒ |
---|
| 317 | if lt_u ? n2 (bitvector_of_nat … (bitsize_of_intsize sz1)) |
---|
| 318 | then Some ? (Vint sz1 (shift_right ?? (nat_of_bitvector … n2) n1 (head' … n1))) |
---|
[639] | 319 | else None ? |
---|
| 320 | | _ ⇒ None ? ] |
---|
| 321 | | _ ⇒ None ? ]. |
---|
| 322 | |
---|
| 323 | definition ev_shru ≝ λv1, v2: val. |
---|
| 324 | match v1 with |
---|
[961] | 325 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 326 | [ Vint sz2 n2 ⇒ |
---|
| 327 | if lt_u ? n2 (bitvector_of_nat … (bitsize_of_intsize sz1)) |
---|
| 328 | then Some ? (Vint sz1 (shift_right ?? (nat_of_bitvector … n2) n1 false)) |
---|
[639] | 329 | else None ? |
---|
| 330 | | _ ⇒ None ? ] |
---|
| 331 | | _ ⇒ None ? ]. |
---|
| 332 | |
---|
| 333 | definition ev_addf ≝ λv1,v2: val. |
---|
| 334 | match v1 with |
---|
| 335 | [ Vfloat f1 ⇒ match v2 with |
---|
| 336 | [ Vfloat f2 ⇒ Some ? (Vfloat (Fadd f1 f2)) |
---|
| 337 | | _ ⇒ None ? ] |
---|
| 338 | | _ ⇒ None ? ]. |
---|
| 339 | |
---|
| 340 | definition ev_subf ≝ λv1,v2: val. |
---|
| 341 | match v1 with |
---|
| 342 | [ Vfloat f1 ⇒ match v2 with |
---|
| 343 | [ Vfloat f2 ⇒ Some ? (Vfloat (Fsub f1 f2)) |
---|
| 344 | | _ ⇒ None ? ] |
---|
| 345 | | _ ⇒ None ? ]. |
---|
| 346 | |
---|
| 347 | definition ev_mulf ≝ λv1,v2: val. |
---|
| 348 | match v1 with |
---|
| 349 | [ Vfloat f1 ⇒ match v2 with |
---|
| 350 | [ Vfloat f2 ⇒ Some ? (Vfloat (Fmul f1 f2)) |
---|
| 351 | | _ ⇒ None ? ] |
---|
| 352 | | _ ⇒ None ? ]. |
---|
| 353 | |
---|
| 354 | definition ev_divf ≝ λv1,v2: val. |
---|
| 355 | match v1 with |
---|
| 356 | [ Vfloat f1 ⇒ match v2 with |
---|
| 357 | [ Vfloat f2 ⇒ Some ? (Vfloat (Fdiv f1 f2)) |
---|
| 358 | | _ ⇒ None ? ] |
---|
| 359 | | _ ⇒ None ? ]. |
---|
| 360 | |
---|
| 361 | definition ev_cmp_match : comparison → option val ≝ λc. |
---|
| 362 | match c with |
---|
| 363 | [ Ceq ⇒ Some ? Vtrue |
---|
| 364 | | Cne ⇒ Some ? Vfalse |
---|
| 365 | | _ ⇒ None ? |
---|
| 366 | ]. |
---|
| 367 | |
---|
| 368 | definition ev_cmp_mismatch : comparison → option val ≝ λc. |
---|
| 369 | match c with |
---|
| 370 | [ Ceq ⇒ Some ? Vfalse |
---|
| 371 | | Cne ⇒ Some ? Vtrue |
---|
| 372 | | _ ⇒ None ? |
---|
| 373 | ]. |
---|
| 374 | |
---|
| 375 | definition ev_cmp ≝ λc: comparison. λv1,v2: val. |
---|
| 376 | match v1 with |
---|
[961] | 377 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 378 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 379 | (λn1. Some ? (of_bool (cmp_int ? c n1 n2))) |
---|
| 380 | (None ?) |
---|
[639] | 381 | | _ ⇒ None ? ] |
---|
[774] | 382 | | _ ⇒ None ? ]. |
---|
| 383 | |
---|
| 384 | definition ev_cmpp ≝ λc: comparison. λv1,v2: val. |
---|
| 385 | match v1 with |
---|
| 386 | [ Vptr r1 b1 p1 ofs1 ⇒ match v2 with |
---|
[639] | 387 | [ Vptr r2 b2 p2 ofs2 ⇒ |
---|
| 388 | if eq_block b1 b2 |
---|
| 389 | then Some ? (of_bool (cmp_offset c ofs1 ofs2)) |
---|
| 390 | else ev_cmp_mismatch c |
---|
| 391 | | Vnull r2 ⇒ ev_cmp_mismatch c |
---|
| 392 | | _ ⇒ None ? ] |
---|
| 393 | | Vnull r1 ⇒ match v2 with |
---|
| 394 | [ Vptr _ _ _ _ ⇒ ev_cmp_mismatch c |
---|
| 395 | | Vnull r2 ⇒ ev_cmp_match c |
---|
| 396 | | _ ⇒ None ? |
---|
| 397 | ] |
---|
| 398 | | _ ⇒ None ? ]. |
---|
| 399 | |
---|
| 400 | (* TODO: check this, it isn't the cmpu used elsewhere *) |
---|
| 401 | definition ev_cmpu ≝ λc: comparison. λv1,v2: val. |
---|
| 402 | match v1 with |
---|
[961] | 403 | [ Vint sz1 n1 ⇒ match v2 with |
---|
| 404 | [ Vint sz2 n2 ⇒ intsize_eq_elim ? sz1 sz2 ? n1 |
---|
| 405 | (λn1. Some ? (of_bool (cmpu_int ? c n1 n2))) |
---|
| 406 | (None ?) |
---|
[639] | 407 | | _ ⇒ None ? ] |
---|
| 408 | | _ ⇒ None ? ]. |
---|
| 409 | |
---|
| 410 | definition ev_cmpf ≝ λc: comparison. λv1,v2: val. |
---|
| 411 | match v1 with |
---|
| 412 | [ Vfloat f1 ⇒ match v2 with |
---|
| 413 | [ Vfloat f2 ⇒ Some ? (of_bool (Fcmp c f1 f2)) |
---|
| 414 | | _ ⇒ None ? ] |
---|
| 415 | | _ ⇒ None ? ]. |
---|
| 416 | |
---|
| 417 | definition eval_binop : binary_operation → val → val → option val ≝ |
---|
| 418 | λop. |
---|
| 419 | match op with |
---|
| 420 | [ Oadd ⇒ ev_add |
---|
| 421 | | Osub ⇒ ev_sub |
---|
| 422 | | Omul ⇒ ev_mul |
---|
| 423 | | Odiv ⇒ ev_divs |
---|
| 424 | | Odivu ⇒ ev_divu |
---|
| 425 | | Omod ⇒ ev_mods |
---|
| 426 | | Omodu ⇒ ev_modu |
---|
| 427 | | Oand ⇒ ev_and |
---|
| 428 | | Oor ⇒ ev_or |
---|
| 429 | | Oxor ⇒ ev_xor |
---|
| 430 | | Oshl ⇒ ev_shl |
---|
| 431 | | Oshr ⇒ ev_shr |
---|
| 432 | | Oshru ⇒ ev_shru |
---|
| 433 | | Oaddf ⇒ ev_addf |
---|
| 434 | | Osubf ⇒ ev_subf |
---|
| 435 | | Omulf ⇒ ev_mulf |
---|
| 436 | | Odivf ⇒ ev_divf |
---|
| 437 | | Ocmp c ⇒ ev_cmp c |
---|
| 438 | | Ocmpu c ⇒ ev_cmpu c |
---|
| 439 | | Ocmpf c ⇒ ev_cmpf c |
---|
[774] | 440 | | Oaddp ⇒ ev_addp |
---|
| 441 | | Osubpi ⇒ ev_subpi |
---|
[961] | 442 | | Osubpp sz ⇒ ev_subpp sz |
---|
[774] | 443 | | Ocmpp c ⇒ ev_cmpp c |
---|
[639] | 444 | ]. |
---|
| 445 | |
---|