Computing effective address using ModR/M, SIB bytes, and displacement bytes present in the instruction
Function:
(defun x86-effective-addr-from-sib (proc-mode temp-rip rex-byte mod sib x86) (declare (xargs :stobjs (x86))) (declare (type (integer 0 4) proc-mode) (type (signed-byte 48) temp-rip) (type (unsigned-byte 8) rex-byte) (type (unsigned-byte 2) mod) (type (unsigned-byte 8) sib)) (declare (xargs :guard (sib-p sib))) (let ((__function__ 'x86-effective-addr-from-sib)) (declare (ignorable __function__)) (b* (((the (unsigned-byte 3) b) (sib->base sib)) (check-alignment? nil) ((mv flg base displacement nrip-bytes x86) (case mod (0 (if (equal b 5) (b* (((mv ?flg0 dword x86) (rime-size-opt proc-mode 4 temp-rip 1 :x check-alignment? x86 :mem-ptr? nil)) ((when flg0) (mv (cons flg0 'rime-size-opt-error) 0 0 0 x86))) (mv nil 0 dword 4 x86)) (mv nil (if (equal proc-mode 0) (rgfi (reg-index b rex-byte 0) x86) (rr32 b x86)) 0 0 x86))) (1 (b* (((mv ?flg1 byte x86) (rime-size-opt proc-mode 1 temp-rip 1 :x check-alignment? x86 :mem-ptr? nil)) ((when flg1) (mv (cons flg1 'rime-size-opt-error) 0 0 0 x86))) (mv nil (if (equal proc-mode 0) (rgfi (reg-index b rex-byte 0) x86) (rr32 b x86)) byte 1 x86))) (2 (b* (((mv ?flg2 dword x86) (rime-size-opt proc-mode 4 temp-rip 1 :x check-alignment? x86 :mem-ptr? nil)) ((when flg2) (mv (cons flg2 'rime-size-opt-error) 0 0 0 x86))) (mv nil (if (equal proc-mode 0) (rgfi (reg-index b rex-byte 0) x86) (rr32 b x86)) dword 4 x86))) (otherwise (mv 'mod-can-not-be-anything-other-than-0-1-or-2 0 0 0 x86)))) (ix (reg-index (sib->index sib) rex-byte 1)) (index (case ix (4 0) (otherwise (if (equal proc-mode 0) (rgfi ix x86) (i32 (rgfi ix x86)))))) (scale (the (unsigned-byte 2) (sib->scale sib))) (scaled-index (ash index scale)) (effective-addr (+ base scaled-index))) (mv flg effective-addr displacement nrip-bytes x86))))
Theorem:
(defthm integerp-of-x86-effective-addr-from-sib.non-truncated-memory-address (implies (and (force (x86p x86)) (integerp temp-rip)) (b* (((mv ?flg ?non-truncated-memory-address ?disp ?increment-rip-by ?x86) (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86))) (integerp non-truncated-memory-address))) :rule-classes :type-prescription)
Theorem:
(defthm natp-of-x86-effective-addr-from-sib.increment-rip-by (b* (((mv ?flg ?non-truncated-memory-address ?disp ?increment-rip-by ?x86) (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86))) (natp increment-rip-by)) :rule-classes :type-prescription)
Theorem:
(defthm x86p-of-x86-effective-addr-from-sib.x86 (implies (force (x86p x86)) (b* (((mv ?flg ?non-truncated-memory-address ?disp ?increment-rip-by ?x86) (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86))) (x86p x86))) :rule-classes :rewrite)
Theorem:
(defthm x86-effective-addr-from-sib-returns-integerp-displacement (implies (x86p x86) (integerp (mv-nth 2 (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86)))) :rule-classes (:rewrite :type-prescription))
Theorem:
(defthm x86-effective-addr-from-sib-returns-<=-increment-rip-bytes (<= (mv-nth 3 (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86)) 4) :rule-classes :linear)
Theorem:
(defthm logext-loghead-identity (implies (signed-byte-p n x) (equal (logext n (loghead n x)) x)))
Function:
(defun x86-effective-addr-16-disp (proc-mode temp-rip mod x86) (declare (xargs :stobjs (x86))) (declare (type (integer 0 4) proc-mode) (type (signed-byte 48) temp-rip) (type (unsigned-byte 2) mod)) (declare (xargs :guard (2bits-p mod))) (let ((__function__ 'x86-effective-addr-16-disp)) (declare (ignorable __function__)) (case mod (0 (mv nil 0 0 x86)) (1 (b* (((mv flg byte x86) (rime-size-opt proc-mode 1 temp-rip 1 :x nil x86 :mem-ptr? nil)) ((when flg) (mv flg 0 0 x86))) (mv nil byte 1 x86))) (2 (b* (((mv flg word x86) (rime-size-opt proc-mode 2 temp-rip 1 :x nil x86 :mem-ptr? nil)) ((when flg) (mv flg 0 0 x86))) (mv nil word 2 x86))) (otherwise (mv 'mod-value-wrong 0 0 x86)))))
Theorem:
(defthm i16p-of-x86-effective-addr-16-disp.disp (implies (x86p x86) (b* (((mv ?flg ?disp ?increment-rip-by ?x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86))) (i16p disp))) :rule-classes :rewrite)
Theorem:
(defthm natp-of-x86-effective-addr-16-disp.increment-rip-by (b* (((mv ?flg ?disp ?increment-rip-by ?x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86))) (natp increment-rip-by)) :rule-classes :rewrite)
Theorem:
(defthm x86p-of-x86-effective-addr-16-disp.x86 (implies (x86p x86) (b* (((mv ?flg ?disp ?increment-rip-by ?x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86))) (x86p x86))) :rule-classes :rewrite)
Theorem:
(defthm integerp-of-x86-effective-addr-16-disp.disp (implies (x86p x86) (b* (((mv ?flg ?disp ?increment-rip-by ?x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86))) (integerp disp))) :rule-classes :type-prescription)
Theorem:
(defthm mv-nth-2-x86-effective-addr-16-disp-<=-4 (<= (mv-nth 2 (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) 4) :rule-classes :linear)
Function:
(defun x86-effective-addr-16 (proc-mode temp-rip r/m mod x86) (declare (xargs :stobjs (x86))) (declare (type (integer 0 4) proc-mode) (type (signed-byte 48) temp-rip) (type (unsigned-byte 3) r/m) (type (unsigned-byte 2) mod)) (declare (xargs :guard (and (2bits-p mod) (3bits-p r/m)))) (let ((__function__ 'x86-effective-addr-16)) (declare (ignorable __function__)) (case r/m (0 (b* ((bx (rr16 3 x86)) (si (rr16 6 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ bx si disp)) increment-rip-by x86))) (1 (b* ((bx (rr16 3 x86)) (di (rr16 7 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ bx di disp)) increment-rip-by x86))) (2 (b* ((bp (rr16 5 x86)) (si (rr16 6 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ bp si disp)) increment-rip-by x86))) (3 (b* ((bp (rr16 5 x86)) (di (rr16 7 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ bp di disp)) increment-rip-by x86))) (4 (b* ((si (rr16 6 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ si disp)) increment-rip-by x86))) (5 (b* ((di (rr16 7 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ di disp)) increment-rip-by x86))) (6 (case mod (0 (b* (((mv flg disp x86) (rime-size-opt proc-mode 2 temp-rip 1 :x nil x86 :mem-ptr? nil)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 disp) 2 x86))) (otherwise (b* ((bp (rr16 5 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ bp disp)) increment-rip-by x86))))) (7 (b* ((bx (rr16 3 x86)) ((mv flg disp increment-rip-by x86) (x86-effective-addr-16-disp proc-mode temp-rip mod x86)) ((when flg) (mv flg 0 0 x86))) (mv nil (n16 (+ bx disp)) increment-rip-by x86))) (otherwise (mv :r/m-out-of-range 0 0 x86)))))
Theorem:
(defthm n16p-of-x86-effective-addr-16.address (b* (((mv ?flg ?address ?increment-rip-by ?x86) (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) (n16p address)) :rule-classes :rewrite)
Theorem:
(defthm natp-of-x86-effective-addr-16.increment-rip-by (b* (((mv ?flg ?address ?increment-rip-by ?x86) (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) (natp increment-rip-by)) :rule-classes :rewrite)
Theorem:
(defthm x86p-of-x86-effective-addr-16.x86 (implies (x86p x86) (b* (((mv ?flg ?address ?increment-rip-by ?x86) (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) (x86p x86))) :rule-classes :rewrite)
Theorem:
(defthm i64p-mv-nth-1-x86-effective-addr-16 (signed-byte-p 64 (mv-nth 1 (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) :rule-classes (:rewrite (:type-prescription :corollary (integerp (mv-nth 1 (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) :hints (("Goal" :in-theory '(signed-byte-p integer-range-p)))) (:linear :corollary (and (<= -9223372036854775808 (mv-nth 1 (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) (< (mv-nth 1 (x86-effective-addr-16 proc-mode temp-rip r/m mod x86)) 9223372036854775808)) :hints (("Goal" :in-theory '(signed-byte-p integer-range-p (:e expt)))))))
Theorem:
(defthm natp-mv-nth-2-x86-effective-addr-16 (natp (mv-nth 2 (x86-effective-addr-16 proc-mode temp-rip r/m mod x86))) :rule-classes :type-prescription)
Theorem:
(defthm mv-nth-2-x86-effective-addr-16-<=-4 (<= (mv-nth 2 (x86-effective-addr-16 proc-mode temp-rip r/m mod x86)) 4) :rule-classes :linear)
Function:
(defun x86-effective-addr-32/64 (proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86) (declare (xargs :stobjs (x86))) (declare (type (integer 0 4) proc-mode) (type (signed-byte 48) temp-rip) (type (unsigned-byte 8) rex-byte) (type (unsigned-byte 3) r/m) (type (unsigned-byte 2) mod) (type (unsigned-byte 8) sib) (type (unsigned-byte 3) num-imm-bytes)) (declare (xargs :guard (and (2bits-p mod) (3bits-p r/m) (sib-p sib)))) (let ((__function__ 'x86-effective-addr-32/64)) (declare (ignorable __function__)) (b* (((mv flg addr displacement increment-rip-by x86) (case mod (0 (case r/m (4 (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86)) (5 (if (equal proc-mode 0) (b* (((mv ?flg0 dword x86) (rime-size-opt 0 4 temp-rip 1 :x nil x86 :mem-ptr? nil)) ((mv flg next-rip) (add-to-*ip 0 temp-rip (+ 4 num-imm-bytes) x86)) ((when flg) (mv flg 0 0 0 x86))) (mv flg0 next-rip dword 4 x86)) (b* (((mv flg dword x86) (rime-size-opt proc-mode 4 temp-rip 1 :x nil x86 :mem-ptr? nil)) ((when flg) (mv flg 0 0 0 x86))) (mv nil 0 dword 4 x86)))) (otherwise (mv nil (if (equal proc-mode 0) (rgfi (reg-index r/m rex-byte 0) x86) (rr32 r/m x86)) 0 0 x86)))) (1 (case r/m (4 (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86)) (otherwise (b* (((mv ?flg2 byte2 x86) (rime-size-opt proc-mode 1 temp-rip 1 :x nil x86 :mem-ptr? nil)) (reg (if (equal proc-mode 0) (rgfi (reg-index r/m rex-byte 0) x86) (rr32 r/m x86)))) (mv flg2 reg byte2 1 x86))))) (2 (case r/m (4 (x86-effective-addr-from-sib proc-mode temp-rip rex-byte mod sib x86)) (otherwise (b* (((mv ?flg1 dword x86) (rime-size-opt proc-mode 4 temp-rip 1 :x nil x86 :mem-ptr? nil)) (reg (if (equal proc-mode 0) (rgfi (reg-index r/m rex-byte 0) x86) (rr32 r/m x86)))) (mv flg1 reg dword 4 x86))))) (otherwise (mv 'mod-value-wrong 0 0 0 x86)))) (dst-base (+ addr displacement)) (dst-base (if (equal proc-mode 0) (if p4 (n32 dst-base) (n64-to-i64 (n64 dst-base))) (n32 dst-base)))) (mv flg dst-base increment-rip-by x86))))
Theorem:
(defthm x86p-of-x86-effective-addr-32/64.x86 (implies (force (x86p x86)) (b* (((mv ?flg ?i64p-memory-address ?increment-rip-by ?x86) (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) (x86p x86))) :rule-classes :rewrite)
Theorem:
(defthm i64p-mv-nth-1-x86-effective-addr-32/64 (signed-byte-p 64 (mv-nth 1 (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) :rule-classes (:rewrite (:type-prescription :corollary (integerp (mv-nth 1 (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) :hints (("Goal" :in-theory '(signed-byte-p integer-range-p)))) (:linear :corollary (and (<= -9223372036854775808 (mv-nth 1 (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) (< (mv-nth 1 (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) 9223372036854775808)) :hints (("Goal" :in-theory '(signed-byte-p integer-range-p (:e expt)))))))
Theorem:
(defthm natp-mv-nth-2-x86-effective-addr-32/64 (natp (mv-nth 2 (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) :rule-classes :type-prescription)
Theorem:
(defthm mv-nth-2-x86-effective-addr-32/64-<=-4 (<= (mv-nth 2 (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) 4) :rule-classes :linear)
Function:
(defun x86-effective-addr (proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86) (declare (xargs :stobjs (x86))) (declare (type (integer 0 4) proc-mode) (type (signed-byte 48) temp-rip) (type (unsigned-byte 8) rex-byte) (type (unsigned-byte 3) r/m) (type (unsigned-byte 2) mod) (type (unsigned-byte 8) sib) (type (unsigned-byte 3) num-imm-bytes)) (declare (xargs :guard (and (2bits-p mod) (3bits-p r/m) (sib-p sib)))) (let ((__function__ 'x86-effective-addr)) (declare (ignorable __function__)) (if (eql 2 (select-address-size proc-mode (if p4 t nil) x86)) (x86-effective-addr-16 proc-mode temp-rip r/m mod x86) (x86-effective-addr-32/64 proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))))
Theorem:
(defthm x86p-of-x86-effective-addr.x86 (implies (force (x86p x86)) (b* (((mv ?flg ?i64p-memory-address ?increment-rip-by ?x86) (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) (x86p x86))) :rule-classes :rewrite)
Theorem:
(defthm i64p-mv-nth-1-x86-effective-addr (signed-byte-p 64 (mv-nth 1 (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) :rule-classes (:rewrite (:type-prescription :corollary (integerp (mv-nth 1 (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) :hints (("Goal" :in-theory '(signed-byte-p integer-range-p)))) (:linear :corollary (and (<= -9223372036854775808 (mv-nth 1 (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) (< (mv-nth 1 (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) 9223372036854775808)) :hints (("Goal" :in-theory '(signed-byte-p integer-range-p (:e expt)))))))
Theorem:
(defthm natp-mv-nth-2-x86-effective-addr (natp (mv-nth 2 (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86))) :rule-classes :type-prescription)
Theorem:
(defthm mv-nth-2-x86-effective-addr-<=-4 (<= (mv-nth 2 (x86-effective-addr proc-mode p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)) 4) :rule-classes :linear)
Theorem:
(defthm x86-effective-addr-when-64-bit-modep (equal (x86-effective-addr 0 p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86) (x86-effective-addr-32/64 0 p4 temp-rip rex-byte r/m mod sib num-imm-bytes x86)))