[3] | 1 | (**************************************************************************) |
---|
| 2 | (* ___ *) |
---|
| 3 | (* ||M|| *) |
---|
| 4 | (* ||A|| A project by Andrea Asperti *) |
---|
| 5 | (* ||T|| *) |
---|
| 6 | (* ||I|| Developers: *) |
---|
| 7 | (* ||T|| The HELM team. *) |
---|
| 8 | (* ||A|| http://helm.cs.unibo.it *) |
---|
| 9 | (* \ / *) |
---|
| 10 | (* \ / This file is distributed under the terms of the *) |
---|
| 11 | (* v GNU General Public License Version 2 *) |
---|
| 12 | (* *) |
---|
| 13 | (**************************************************************************) |
---|
| 14 | |
---|
[717] | 15 | include "common/Mem.ma". |
---|
[3] | 16 | |
---|
| 17 | |
---|
| 18 | (* The memory model stores the entire value regardless of whether it fits in |
---|
| 19 | the amount of memory used. Fortunately, it checks the size of the memory |
---|
| 20 | access, preventing you reading back more information than the memory can |
---|
| 21 | store. *) |
---|
[717] | 22 | definition store0 := empty. |
---|
| 23 | definition store1block : mem × block ≝ alloc store0 0 4 Any. |
---|
| 24 | definition store1 : mem ≝ fst ?? store1block. |
---|
| 25 | definition block := snd ?? store1block. |
---|
[12] | 26 | |
---|
[3] | 27 | (* write a value *) |
---|
[717] | 28 | definition store2 : mem. |
---|
[961] | 29 | letin r ≝ (store Mint16unsigned store1 Any block 0 (Vint I16 (repr ? 1))); |
---|
[12] | 30 | |
---|
[717] | 31 | lapply (refl ? r); elim r in ⊢ (???% → ?) [ whd in ⊢ (??%? → ?) #H destruct |
---|
| 32 | | #rr #_ @rr ] qed. |
---|
[12] | 33 | |
---|
[3] | 34 | (* overwrite the first byte of the value *) |
---|
[717] | 35 | definition store3 : mem. |
---|
[961] | 36 | letin r ≝ (store Mint8unsigned store1 Any block 0 (Vint I8 (repr ? 1))); |
---|
[12] | 37 | |
---|
[717] | 38 | lapply (refl ? r) elim r in ⊢ (???% → ?) [ whd in ⊢ (??%? → ?) #H destruct |
---|
| 39 | | #rr #_ @ rr ] qed. |
---|
[3] | 40 | |
---|
| 41 | (* The size checking rejects the read and gives us Some Vundef. *) |
---|
[717] | 42 | example vl_undef: load Mint16signed store3 Any block 0 = Some ? Vundef. @refl qed. |
---|
[3] | 43 | |
---|
[961] | 44 | (* The read is successful and returns the value. *) |
---|
| 45 | example vl_ok': load Mint8unsigned store3 Any block 0 = Some ? (Vint I8 (repr ? 1)). // qed. |
---|
[3] | 46 | |
---|
| 47 | (* NB: Double frees are allowed by the memory model (although not necessarily |
---|
| 48 | by the language). *) |
---|
[717] | 49 | definition store4 := free store3 block. |
---|
| 50 | definition store5 : mem. letin r ≝ (free store3 block); whd in r @r qed. |
---|
[3] | 51 | |
---|
| 52 | (* No ordering is imposed on deallocation (even though alloc and free are only used for |
---|
| 53 | stack memory in the semantics. *) |
---|
[717] | 54 | definition storeA := empty. |
---|
| 55 | definition storeBblkB := alloc storeA 0 4 Any. |
---|
| 56 | definition storeCblkC := alloc (fst ?? storeBblkB) 0 8 Any. |
---|
| 57 | definition storeD := free (fst ?? storeCblkC) (snd ?? storeBblkB). |
---|
| 58 | definition storeE : mem. letin r ≝ (free storeD (snd ?? storeCblkC)). |
---|
| 59 | whd in r @r qed. |
---|
[3] | 60 | |
---|
| 61 | (* Access to the "object representation" (as the C standard puts it) is not specified. *) |
---|
[717] | 62 | definition storeI := empty. |
---|
| 63 | definition storeIIblk := alloc storeA 0 4 Any. |
---|
| 64 | definition storeIII : mem. |
---|
[961] | 65 | letin r ≝ (store Mint32 (fst ?? storeIIblk) Any (snd ?? storeIIblk) 0 (Vint I32 (repr ? 1))); |
---|
[717] | 66 | lapply (refl ? r) elim r in ⊢ (???% → ?) [ whd in ⊢ (??%? → ?) #H destruct |
---|
| 67 | | #rr #_ @rr ] qed. |
---|
| 68 | definition byte := load Mint8unsigned storeIII Any (snd ?? storeIIblk) 0. |
---|
| 69 | example byteundef: byte = Some ? Vundef. // qed. (* :( *) |
---|