• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
    • Debugging
    • Std
    • Community
    • Proof-automation
    • Macro-libraries
    • ACL2
    • Interfacing-tools
    • Hardware-verification
    • Software-verification
      • Kestrel-books
      • X86isa
        • Program-execution
        • Sdm-instruction-set-summary
        • Tlb
        • Running-linux
        • Introduction
        • Asmtest
        • X86isa-build-instructions
        • Publications
        • Contributors
        • Machine
          • X86isa-state
          • Syscalls
          • Cpuid
          • Linear-memory
          • Rflag-specifications
          • Characterizing-undefined-behavior
          • Top-level-memory
          • App-view
          • X86-decoder
          • Physical-memory
          • Decoding-and-spec-utils
            • Read-operands-and-write-results
            • Effective-address-computations
              • X86-effective-addr-32/64
              • X86-effective-addr-from-sib
                • X86-effective-addr-16
                • X86-effective-addr-16-disp
                • X86-effective-addr
              • Select-operand-size
              • Instruction-pointer-operations
              • Stack-pointer-operations
              • Select-segment-register
              • Prefix-modrm-sib-decoding
              • Select-address-size
              • Rex-byte-from-vex-prefixes
              • Check-instruction-length
              • Error-objects
              • Rip-guard-okp
              • Sib-decoding
            • Instructions
            • Register-readers-and-writers
            • X86-modes
            • Segmentation
            • Other-non-deterministic-computations
            • Environment
            • Paging
          • Implemented-opcodes
          • To-do
          • Proof-utilities
          • Peripherals
          • Model-validation
          • Modelcalls
          • Concrete-simulation-examples
          • Utils
          • Debugging-code-proofs
        • Axe
        • Execloader
      • Math
      • Testing-utilities
    • Effective-address-computations

    X86-effective-addr-from-sib

    Calculates effective address when SIB is present.

    Signature
    (x86-effective-addr-from-sib 
         proc-mode temp-rip rex-byte mod sib x86) 
     
      → 
    (mv flg non-truncated-memory-address disp increment-rip-by x86)
    Arguments
    rex-byte — REX byte.
    mod — mod field of a ModR/M byte.
    sib — SIB byte.
    Returns
    non-truncated-memory-address — Type (integerp non-truncated-memory-address), given (and (force (x86p x86)) (integerp temp-rip)).
    increment-rip-by — Type (natp increment-rip-by).
    x86 — Type (x86p x86), given (force (x86p x86)).

    Source: Intel Vol. 2A, Table 2-3.

    Also see Intel Vol. 2A, Table 2-2 and Figure 2-6.

    In 64-bit mode, we use rgfi to read bases as signed linear addresses, which encode canonical linear addresses, which are also effective addresses in 64-bit mode. In 32-bit mode, we use rr32 to read bases as unsigned effective addresses.

    In 64-bit mode, we use rgfi to read indices as signed 64-bit values. In 32-bit mode, we limit them to signed 32-bit values.

    Note that, in 32-bit mode, we call this function only when the address size is 32 bits. When the address size is 16 bits, there is no SIB byte: See Intel Vol. 2 Table 2-1.

    The displacement is read as a signed values: see AMD manual, Dec'17, Volume 3, Section 1.5.

    Definitions and Theorems

    Function: x86-effective-addr-from-sib

    (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: integerp-of-x86-effective-addr-from-sib.non-truncated-memory-address

    (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: natp-of-x86-effective-addr-from-sib.increment-rip-by

    (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: x86p-of-x86-effective-addr-from-sib.x86

    (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: x86-effective-addr-from-sib-returns-integerp-displacement

    (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: x86-effective-addr-from-sib-returns-<=-increment-rip-bytes

    (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)