Section LOCATIONS.
Variable ge: genv.
Variable sp: block.
Inductive nlive: nmem -> block -> Z -> Prop :=
| nlive_intro: forall stk gl b ofs
(STK: b = sp -> ~ISet.In ofs stk)
(GL: forall id iv,
Genv.find_symbol ge id = Some b ->
gl!id = Some iv ->
~ISet.In ofs iv),
nlive (NMem stk gl) b ofs.
(** All locations are live *)
Definition nmem_all := NMem ISet.empty (PTree.empty _).
(** Add a range of live locations to [nm].  The range starts at the abstract pointer [p] and has length [sz]. *)
Definition nmem_add (nm: nmem) (p: aptr) (sz: Z) : nmem :=
match nm with
| NMemDead => nmem_all       (**r very conservative, should never happen *)
| NMem stk gl =>
match p with
| Gl id ofs =>
match gl!id with
| Some iv => NMem stk (PTree.set id (ISet.remove (Ptrofs.unsigned ofs) (Ptrofs.unsigned ofs + sz) iv) gl)
| None => nm
end
| Glo id =>
NMem stk (PTree.remove id gl)
| Stk ofs =>
NMem (ISet.remove (Ptrofs.unsigned ofs) (Ptrofs.unsigned ofs + sz) stk) gl
| Stack =>
NMem ISet.empty gl
| _ => nmem_all
end
end.
(** Remove a range of locations from [nm], marking these locations as dead. The range starts at the abstract pointer [p] and has length [sz]. *)
Definition nmem_remove (nm: nmem) (p: aptr) (sz: Z) : nmem :=
match nm with
| NMemDead => NMemDead
| NMem stk gl =>
match p with
| Gl id ofs =>
let iv' :=
match gl!id with
| Some iv => ISet.add (Ptrofs.unsigned ofs) (Ptrofs.unsigned ofs + sz) iv
| None    => ISet.interval (Ptrofs.unsigned ofs) (Ptrofs.unsigned ofs + sz)
end in
NMem stk (PTree.set id iv' gl)
| Stk ofs =>
NMem (ISet.add (Ptrofs.unsigned ofs) (Ptrofs.unsigned ofs + sz) stk) gl
| _ => nm
end
end.

Lemma nlive_remove:
forall bc b ofs p nm sz b' i,
genv_match bc ge ->
bc sp = BCstack ->
pmatch bc b ofs p ->
nlive nm b' i ->
b' <> b \/ i < Ptrofs.unsigned ofs \/ Ptrofs.unsigned ofs + sz <= i ->
nlive (nmem_remove nm p sz) b' i.