• Top
    • Documentation
    • Books
    • Boolean-reasoning
    • Projects
      • Apt
      • Zfc
      • Acre
      • Milawa
      • Smtlink
      • Abnf
      • Vwsim
      • Isar
      • Wp-gen
      • Dimacs-reader
      • Pfcs
      • Legacy-defrstobj
      • Proof-checker-array
      • Soft
      • C
      • Farray
      • Rp-rewriter
      • Instant-runoff-voting
      • Imp-language
      • Sidekick
      • Leftist-trees
      • Java
      • Taspi
      • Bitcoin
      • Riscv
        • Instructions
        • States
        • Decoding
        • Encoding
          • Encode
            • Encode-load-funct
            • Encode-store-funct
            • Encode-op-imms-32-funct
            • Encode-op-funct
            • Encode-op-32-funct
            • Encode-op-imms64-funct
            • Encode-op-imms32-funct
            • Encode-op-imm-funct
            • Encode-op-imm-32-funct
            • Encode-branch-funct
          • Features
          • Semantics
          • Execution
        • Des
        • Ethereum
        • X86isa
        • Sha-2
        • Yul
        • Zcash
        • Proof-checker-itp13
        • Regex
        • ACL2-programming-language
        • Json
        • Jfkr
        • Equational
        • Cryptography
        • Poseidon
        • Where-do-i-place-my-book
        • Axe
        • Bigmems
        • Builtins
        • Execloader
        • Aleo
        • Solidity
        • Paco
        • Concurrent-programs
        • Bls12-377-curves
      • Debugging
      • Std
      • Proof-automation
      • Macro-libraries
      • ACL2
      • Interfacing-tools
      • Hardware-verification
      • Software-verification
      • Math
      • Testing-utilities
    • Encoding

    Encode

    Encode an instruction in the normal way (i.e. in 32 bits).

    Signature
    (encode instr feat) → encoding
    Arguments
    instr — Guard (instrp instr).
    feat — Guard (featp feat).
    Returns
    encoding — Type (ubyte32p encoding).

    We calculate the fields and we concatenate them using logappn.

    This is based on [ISA:2.4.1] [ISA:2.4.2] [ISA:2.5.1] [ISA:2.5.2] [ISA:2.6] [ISA:4.2.1] [ISA:4.2.2] [ISA:4.3] [ISA:34].

    Note that the 20 immediate bits in JAL are designated as bits 1 to 20 in [ISA:2.5.1], not 0 to 19. Thus, the indices passed to part-select and logbit are one less than the ones shown in the instruction format. A similar remark applies to the immediate bits in the BRANCH instructions [ISA:2.5.2].

    Definitions and Theorems

    Function: encode

    (defun encode (instr feat)
     (declare (xargs :guard (and (instrp instr) (featp feat))))
     (declare (xargs :guard (instr-validp instr feat)))
     (let ((__function__ 'encode))
       (declare (ignorable __function__))
       (instr-case
            instr :op-imm
            (b* ((opcode 19)
                 (funct3 (encode-op-imm-funct instr.funct)))
              (logappn 7 opcode 5 instr.rd
                       3 funct3 5 instr.rs1 12 instr.imm))
            :op-imms32
            (assert$ (feat-32p feat)
                     (b* ((opcode 19)
                          ((mv funct3 hi7imm)
                           (encode-op-imms32-funct instr.funct)))
                       (logappn 7 opcode 5 instr.rd 3 funct3
                                5 instr.rs1 5 instr.imm 7 hi7imm)))
            :op-imms64
            (assert$ (feat-64p feat)
                     (b* ((opcode 19)
                          ((mv funct3 hi6imm)
                           (encode-op-imms64-funct instr.funct)))
                       (logappn 7 opcode 5 instr.rd 3 funct3
                                5 instr.rs1 6 instr.imm 6 hi6imm)))
            :op-imm-32
            (assert$ (feat-64p feat)
                     (b* ((opcode 27)
                          (funct3 (encode-op-imm-32-funct instr.funct)))
                       (logappn 7 opcode 5 instr.rd
                                3 funct3 5 instr.rs1 12 instr.imm)))
            :op-imms-32
            (assert$ (feat-64p feat)
                     (b* ((opcode 27)
                          ((mv funct3 hi6imm)
                           (encode-op-imms-32-funct instr.funct)))
                       (logappn 7 opcode 5 instr.rd 3 funct3
                                5 instr.rs1 5 instr.imm 1 0 6 hi6imm)))
            :lui
            (b* ((opcode 55))
              (logappn 7 opcode 5 instr.rd 20 instr.imm))
            :auipc
            (b* ((opcode 23))
              (logappn 7 opcode 5 instr.rd 10 instr.imm))
            :op
            (b* ((opcode 51)
                 ((mv funct3 funct7)
                  (encode-op-funct instr.funct)))
              (logappn 7 opcode 5 instr.rd 3 funct3
                       5 instr.rs1 5 instr.rs2 7 funct7))
            :op-32
            (assert$ (feat-64p feat)
                     (b* ((opcode 59)
                          ((mv funct3 funct7)
                           (encode-op-32-funct instr.funct)))
                       (logappn 7 opcode 5 instr.rd 3 funct3
                                5 instr.rs1 5 instr.rs2 7 funct7)))
            :jal
            (b* ((opcode 111)
                 (imm-10-1 (part-select instr.imm :low 0 :high 9))
                 (imm-11 (logbit 10 instr.imm))
                 (imm-19-12 (part-select instr.imm
                                         :low 11
                                         :high 18))
                 (imm-20 (logbit 19 instr.imm)))
              (logappn 7 opcode 5 instr.rd 8 imm-19-12
                       1 imm-11 10 imm-10-1 1 imm-20))
            :jalr
            (b* ((opcode 103) (funct3 0))
              (logappn 7 opcode 5 instr.rd
                       3 funct3 5 instr.rs1 12 instr.imm))
            :branch
            (b* ((opcode 99)
                 (funct3 (encode-branch-funct instr.funct))
                 (imm-4-1 (part-select instr.imm :low 0 :high 3))
                 (imm-10-5 (part-select instr.imm :low 4 :high 9))
                 (imm-11 (logbit 10 instr.imm))
                 (imm-12 (logbit 11 instr.imm)))
              (logappn 7 opcode
                       1 imm-11 4 imm-4-1 3 funct3 5 instr.rs1
                       5 instr.rs2 6 imm-10-5 1 imm-12))
            :load
            (b* ((opcode 3)
                 (funct3 (encode-load-funct instr.funct feat)))
              (logappn 7 opcode 5 instr.rs1
                       3 funct3 5 instr.rs1 12 instr.imm))
            :store
            (b* ((opcode 35)
                 (funct3 (encode-store-funct instr.funct feat))
                 (imm-4-0 (part-select instr.imm :low 0 :high 4))
                 (imm-11-5 (part-select instr.imm
                                        :low 5
                                        :high 11)))
              (logappn 7 opcode 5 imm-4-0 3 funct3
                       5 instr.rs1 5 instr.rs2 7 imm-11-5)))))

    Theorem: ubyte32p-of-encode

    (defthm ubyte32p-of-encode
      (b* ((encoding (encode instr feat)))
        (ubyte32p encoding))
      :rule-classes :rewrite)

    Theorem: encode-of-instr-fix-instr

    (defthm encode-of-instr-fix-instr
      (equal (encode (instr-fix instr) feat)
             (encode instr feat)))

    Theorem: encode-instr-equiv-congruence-on-instr

    (defthm encode-instr-equiv-congruence-on-instr
      (implies (instr-equiv instr instr-equiv)
               (equal (encode instr feat)
                      (encode instr-equiv feat)))
      :rule-classes :congruence)

    Theorem: encode-of-feat-fix-feat

    (defthm encode-of-feat-fix-feat
      (equal (encode instr (feat-fix feat))
             (encode instr feat)))

    Theorem: encode-feat-equiv-congruence-on-feat

    (defthm encode-feat-equiv-congruence-on-feat
      (implies (feat-equiv feat feat-equiv)
               (equal (encode instr feat)
                      (encode instr feat-equiv)))
      :rule-classes :congruence)