(** A memory area that contains a value sastifying a given predicate *)
Program Definition contains (chunk: memory_chunk) (b: block) (ofs: Z) (spec: val -> Prop) : massert := {|
m_pred := fun m =>
0 <= ofs <= Ptrofs.max_unsigned
/\ Mem.valid_access m chunk b ofs Freeable
/\ exists v, Mem.load chunk m b ofs = Some v /\ spec v;
m_footprint := fun b' ofs' => b' = b /\ ofs <= ofs' < ofs + size_chunk chunk
|}.
Next Obligation.
rename H2 into v. split;[|split].
- auto.
- destruct H1; split; auto. red; intros; eapply Mem.perm_unchanged_on; eauto. simpl; auto.
- exists v. split; auto. eapply Mem.load_unchanged_on; eauto. simpl; auto.
Qed.
Next Obligation.
eauto with mem.
Qed.

(* Specifications: *)
Lemma contains_no_overflow:
forall spec m chunk b ofs,
m |= contains chunk b ofs spec ->
0 <= ofs <= Ptrofs.max_unsigned.
Lemma load_rule:
forall spec m chunk b ofs,
m |= contains chunk b ofs spec ->
exists v, Mem.load chunk m b ofs = Some v /\ spec v.
Lemma loadv_rule:
forall spec m chunk b ofs,
m |= contains chunk b ofs spec ->
exists v, Mem.loadv chunk m (Vptr b (Ptrofs.repr ofs)) = Some v /\ spec v.
Lemma store_rule:
forall chunk m b ofs v (spec1 spec: val -> Prop) P,
m |= contains chunk b ofs spec1 ** P ->
spec (Val.load_result chunk v) ->
exists m',
Mem.store chunk m b ofs v = Some m' /\ m' |= contains chunk b ofs spec ** P.
Lemma storev_rule:
forall chunk m b ofs v (spec1 spec: val -> Prop) P,
m |= contains chunk b ofs spec1 ** P ->
spec (Val.load_result chunk v) ->
exists m',
Mem.storev chunk m (Vptr b (Ptrofs.repr ofs)) v = Some m' /\ m' |= contains chunk b ofs spec ** P.
Lemma range_contains:
forall chunk b ofs P m,
m |= range b ofs (ofs + size_chunk chunk) ** P ->
(align_chunk chunk | ofs) ->
m |= contains chunk b ofs (fun v => True) ** P.
Lemma contains_imp:
forall (spec1 spec2: val -> Prop) chunk b ofs,
(forall v, spec1 v -> spec2 v) ->
massert_imp (contains chunk b ofs spec1) (contains chunk b ofs spec2).