Section INITMEM.
Variable ge: t.
Definition store_init_data (m: mem) (b: block) (p: Z) (id: init_data) : option mem :=
match id with
| Init_int8 n => Mem.store Mint8unsigned m b p (Vint n)
| Init_int16 n => Mem.store Mint16unsigned m b p (Vint n)
| Init_int32 n => Mem.store Mint32 m b p (Vint n)
| Init_int64 n => Mem.store Mint64 m b p (Vlong n)
| Init_float32 n => Mem.store Mfloat32 m b p (Vsingle n)
| Init_float64 n => Mem.store Mfloat64 m b p (Vfloat n)
| Init_addrof symb ofs =>
match find_symbol ge symb with
| None => None
| Some b' => Mem.store Mptr m b p (Vptr b' ofs)
end
| Init_space n => Some m
end.
Fixpoint store_init_data_list (m: mem) (b: block) (p: Z) (idl: list init_data)
{struct idl}: option mem :=
match idl with
| nil => Some m
| id :: idl' =>
match store_init_data m b p id with
| None => None
| Some m' => store_init_data_list m' b (p + init_data_size id) idl'
end
end.
Definition perm_globvar (gv: globvar V) : permission :=
if gv.(gvar_volatile) then Nonempty
else if gv.(gvar_readonly) then Readable
else Writable.
Definition alloc_global (m: mem) (idg: ident * globdef F V): option mem :=
match idg with
| (id, Gfun f) =>
let (m1, b) := Mem.alloc m 0 1 in
Mem.drop_perm m1 b 0 1 Nonempty
| (id, Gvar v) =>
let init := v.(gvar_init) in
let sz := init_data_list_size init in
let (m1, b) := Mem.alloc m 0 sz in
match store_zeros m1 b 0 sz with
| None => None
| Some m2 =>
match store_init_data_list m2 b 0 init with
| None => None
| Some m3 => Mem.drop_perm m3 b 0 sz (perm_globvar v)
end
end
end.
Fixpoint alloc_globals (m: mem) (gl: list (ident * globdef F V))
{struct gl} : option mem :=
match gl with
| nil => Some m
| g :: gl' =>
match alloc_global m g with
| None => None
| Some m' => alloc_globals m' gl'
end
end.
(** Next-block properties *)
Remark store_zeros_nextblock:
forall m b p n m', store_zeros m b p n = Some m' -> Mem.nextblock m' = Mem.nextblock m.
Remark store_init_data_list_nextblock:
forall idl b m p m',
store_init_data_list m b p idl = Some m' ->
Mem.nextblock m' = Mem.nextblock m.
Remark alloc_global_nextblock:
forall g m m',
alloc_global m g = Some m' ->
Mem.nextblock m' = Pos.succ(Mem.nextblock m).
Remark alloc_globals_nextblock:
forall gl m m',
alloc_globals m gl = Some m' ->
Mem.nextblock m' = advance_next gl (Mem.nextblock m).
(** Permissions *)
Remark store_zeros_perm:
forall k prm b' q m b p n m',
store_zeros m b p n = Some m' ->
(Mem.perm m b' q k prm <-> Mem.perm m' b' q k prm).
Remark store_init_data_perm:
forall k prm b' q i b m p m',
store_init_data m b p i = Some m' ->
(Mem.perm m b' q k prm <-> Mem.perm m' b' q k prm).
Remark store_init_data_list_perm:
forall k prm b' q idl b m p m',
store_init_data_list m b p idl = Some m' ->
(Mem.perm m b' q k prm <-> Mem.perm m' b' q k prm).
Remark alloc_global_perm:
forall k prm b' q idg m m',
alloc_global m idg = Some m' ->
Mem.valid_block m b' ->
(Mem.perm m b' q k prm <-> Mem.perm m' b' q k prm).
Remark alloc_globals_perm:
forall k prm b' q gl m m',
alloc_globals m gl = Some m' ->
Mem.valid_block m b' ->
(Mem.perm m b' q k prm <-> Mem.perm m' b' q k prm).
(** Data preservation properties *)
Remark store_zeros_unchanged:
forall (P: block -> Z -> Prop) m b p n m',
store_zeros m b p n = Some m' ->
(forall i, p <= i < p + n -> ~ P b i) ->
Mem.unchanged_on P m m'.
Remark store_init_data_unchanged:
forall (P: block -> Z -> Prop) b i m p m',
store_init_data m b p i = Some m' ->
(forall ofs, p <= ofs < p + init_data_size i -> ~ P b ofs) ->
Mem.unchanged_on P m m'.
Remark store_init_data_list_unchanged:
forall (P: block -> Z -> Prop) b il m p m',
store_init_data_list m b p il = Some m' ->
(forall ofs, p <= ofs -> ~ P b ofs) ->
Mem.unchanged_on P m m'.
(** Properties related to [loadbytes] *)
Definition readbytes_as_zero (m: mem) (b: block) (ofs len: Z) : Prop :=
forall p n,
ofs <= p -> p + Z.of_nat n <= ofs + len ->
Mem.loadbytes m b p (Z.of_nat n) = Some (list_repeat n (Byte Byte.zero)).

Lemma store_zeros_loadbytes:
forall m b p n m',
store_zeros m b p n = Some m' ->
readbytes_as_zero m' b p n.