Module Val.
Definition eq (x y: val): {x=y} + {x<>y}.
Definition has_type_dec (v: val) (t: typ) : { has_type v t } + { ~ has_type v t }.
(** Select between two values based on the result of a comparison. *)
Definition select (cmp: option bool) (v1 v2: val) (ty: typ) :=
match cmp with
| Some b => normalize (if b then v1 else v2) ty
| None => Vundef
end.
(** [load_result] reflects the effect of storing a value with a given memory chunk, then reading it back with the same chunk.  Depending on the chunk and the type of the value, some normalization occurs. For instance, consider storing the integer value [0xFFF] on 1 byte at a given address, and reading it back.  If it is read back with chunk [Mint8unsigned], zero-extension must be performed, resulting in [0xFF].  If it is read back as a [Mint8signed], sign-extension is performed and [0xFFFFFFFF] is returned. *)
Definition load_result (chunk: memory_chunk) (v: val) :=
match chunk, v with
| Mint8signed, Vint n => Vint (Int.sign_ext 8 n)
| Mint8unsigned, Vint n => Vint (Int.zero_ext 8 n)
| Mint16signed, Vint n => Vint (Int.sign_ext 16 n)
| Mint16unsigned, Vint n => Vint (Int.zero_ext 16 n)
| Mint32, Vint n => Vint n
| Mint32, Vptr b ofs => if Archi.ptr64 then Vundef else Vptr b ofs
| Mint64, Vlong n => Vlong n
| Mint64, Vptr b ofs => if Archi.ptr64 then Vptr b ofs else Vundef
| Mfloat32, Vsingle f => Vsingle f
| Mfloat64, Vfloat f => Vfloat f
| Many32, (Vint _ | Vsingle _) => v
| Many32, Vptr _ _ => if Archi.ptr64 then Vundef else v
| Many64, _ => v
| _, _ => Vundef
end.
(** Theorems on arithmetic operations. *)
(** The ``is less defined'' relation between values. A value is less defined than itself, and [Vundef] is less defined than any value. *)
Inductive lessdef: val -> val -> Prop :=
| lessdef_refl: forall v, lessdef v v
| lessdef_undef: forall v, lessdef Vundef v.
Inductive lessdef_list: list val -> list val -> Prop :=
| lessdef_list_nil:
lessdef_list nil nil
| lessdef_list_cons:
forall v1 v2 vl1 vl2,
lessdef v1 v2 -> lessdef_list vl1 vl2 ->
lessdef_list (v1 :: vl1) (v2 :: vl2).
Hint Resolve lessdef_refl lessdef_undef lessdef_list_nil lessdef_list_cons.
(** Compatibility of operations with the [lessdef] relation. *)
(** * Values and memory injections *)
(** A memory injection [f] is a function from addresses to either [None] or [Some] of an address and an offset.  It defines a correspondence between the blocks of two memory states [m1] and [m2]: - if [f b = None], the block [b] of [m1] has no equivalent in [m2]; - if [f b = Some(b', ofs)], the block [b] of [m2] corresponds to a sub-block at offset [ofs] of the block [b'] in [m2]. *)
Definition meminj : Type := block -> option (block * Z).
(** A memory injection defines a relation between values that is the identity relation, except for pointer values which are shifted as prescribed by the memory injection.  Moreover, [Vundef] values inject into any other value. *)
Inductive inject (mi: meminj): val -> val -> Prop :=
| inject_int:
forall i, inject mi (Vint i) (Vint i)
| inject_long:
forall i, inject mi (Vlong i) (Vlong i)
| inject_float:
forall f, inject mi (Vfloat f) (Vfloat f)
| inject_single:
forall f, inject mi (Vsingle f) (Vsingle f)
| inject_ptr:
forall b1 ofs1 b2 ofs2 delta,
mi b1 = Some (b2, delta) ->
ofs2 = Ptrofs.add ofs1 (Ptrofs.repr delta) ->
inject mi (Vptr b1 ofs1) (Vptr b2 ofs2)
| val_inject_undef: forall v,
inject mi Vundef v.
Hint Constructors inject.
Inductive inject_list (mi: meminj): list val -> list val-> Prop:=
| inject_list_nil :
inject_list mi nil nil
| inject_list_cons : forall v v' vl vl' ,
inject mi v v' -> inject_list mi vl vl'->
inject_list mi (v :: vl) (v' :: vl').
Hint Resolve inject_list_nil inject_list_cons.

Lemma inject_ptrofs:
forall mi i, inject mi (Vptrofs i) (Vptrofs i).