Section COMPARISONS.
Variable valid_ptr: block -> Z -> bool.
Let weak_valid_ptr (b: block) (ofs: Z) := valid_ptr b ofs || valid_ptr b (ofs - 1).
Definition cmp_bool (c: comparison) (v1 v2: val): option bool :=
match v1, v2 with
| Vint n1, Vint n2 => Some (Int.cmp c n1 n2)
| _, _ => None
end.
Definition cmp_different_blocks (c: comparison): option bool :=
match c with
| Ceq => Some false
| Cne => Some true
| _   => None
end.
Definition cmpu_bool (c: comparison) (v1 v2: val): option bool :=
match v1, v2 with
| Vint n1, Vint n2 =>
Some (Int.cmpu c n1 n2)
| Vint n1, Vptr b2 ofs2 =>
if Archi.ptr64 then None else
if Int.eq n1 Int.zero && weak_valid_ptr b2 (Ptrofs.unsigned ofs2)
then cmp_different_blocks c
else None
| Vptr b1 ofs1, Vptr b2 ofs2 =>
if Archi.ptr64 then None else
if eq_block b1 b2 then
if weak_valid_ptr b1 (Ptrofs.unsigned ofs1)
&& weak_valid_ptr b2 (Ptrofs.unsigned ofs2)
then Some (Ptrofs.cmpu c ofs1 ofs2)
else None
else
if valid_ptr b1 (Ptrofs.unsigned ofs1)
&& valid_ptr b2 (Ptrofs.unsigned ofs2)
then cmp_different_blocks c
else None
| Vptr b1 ofs1, Vint n2 =>
if Archi.ptr64 then None else
if Int.eq n2 Int.zero && weak_valid_ptr b1 (Ptrofs.unsigned ofs1)
then cmp_different_blocks c
else None
| _, _ => None
end.
Definition cmpf_bool (c: comparison) (v1 v2: val): option bool :=
match v1, v2 with
| Vfloat f1, Vfloat f2 => Some (Float.cmp c f1 f2)
| _, _ => None
end.
Definition cmpfs_bool (c: comparison) (v1 v2: val): option bool :=
match v1, v2 with
| Vsingle f1, Vsingle f2 => Some (Float32.cmp c f1 f2)
| _, _ => None
end.
Definition cmpl_bool (c: comparison) (v1 v2: val): option bool :=
match v1, v2 with
| Vlong n1, Vlong n2 => Some (Int64.cmp c n1 n2)
| _, _ => None
end.
Definition cmplu_bool (c: comparison) (v1 v2: val): option bool :=
match v1, v2 with
| Vlong n1, Vlong n2 => Some (Int64.cmpu c n1 n2)
| Vlong n1, Vptr b2 ofs2 =>
if negb Archi.ptr64 then None else
if Int64.eq n1 Int64.zero && weak_valid_ptr b2 (Ptrofs.unsigned ofs2)
then cmp_different_blocks c
else None
| Vptr b1 ofs1, Vptr b2 ofs2 =>
if negb Archi.ptr64 then None else
if eq_block b1 b2 then
if weak_valid_ptr b1 (Ptrofs.unsigned ofs1)
&& weak_valid_ptr b2 (Ptrofs.unsigned ofs2)
then Some (Ptrofs.cmpu c ofs1 ofs2)
else None
else
if valid_ptr b1 (Ptrofs.unsigned ofs1)
&& valid_ptr b2 (Ptrofs.unsigned ofs2)
then cmp_different_blocks c
else None
| Vptr b1 ofs1, Vlong n2 =>
if negb Archi.ptr64 then None else
if Int64.eq n2 Int64.zero && weak_valid_ptr b1 (Ptrofs.unsigned ofs1)
then cmp_different_blocks c
else None
| _, _ => None
end.
Definition of_optbool (ob: option bool): val :=
match ob with Some true => Vtrue | Some false => Vfalse | None => Vundef end.
Definition cmp (c: comparison) (v1 v2: val): val :=
of_optbool (cmp_bool c v1 v2).
Definition cmpu (c: comparison) (v1 v2: val): val :=
of_optbool (cmpu_bool c v1 v2).
Definition cmpf (c: comparison) (v1 v2: val): val :=
of_optbool (cmpf_bool c v1 v2).
Definition cmpfs (c: comparison) (v1 v2: val): val :=
of_optbool (cmpfs_bool c v1 v2).
Definition cmpl (c: comparison) (v1 v2: val): option val :=
option_map of_bool (cmpl_bool c v1 v2).
Definition cmplu (c: comparison) (v1 v2: val): option val :=
option_map of_bool (cmplu_bool c v1 v2).
Definition maskzero_bool (v: val) (mask: int): option bool :=
match v with
| Vint n => Some (Int.eq (Int.and n mask) Int.zero)
| _ => None
end.
End COMPARISONS.
(** Add the given offset to the given pointer. *)
Definition offset_ptr (v: val) (delta: ptrofs) : val :=
match v with
| Vptr b ofs => Vptr b (Ptrofs.add ofs delta)
| _ => 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. *)

Lemma hiword_lessdef:
forall v v', lessdef v v' -> lessdef (hiword v) (hiword v').