• 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
      • Riscv
        • Semantics
        • Features
        • States
        • Instructions
        • Decoding
        • Encoding
          • Encode
            • Decoding-of-encoding
            • 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
          • Reads-over-writes
          • Rv32im
          • Rv64im
          • Execution
        • Instant-runoff-voting
        • Imp-language
        • Sidekick
        • Leftist-trees
        • Java
        • Taspi
        • Bitcoin
        • 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
        • Aleo
        • Bigmems
        • Builtins
        • Execloader
        • Solidity
        • Paco
        • Concurrent-programs
        • Bls12-377-curves
      • Debugging
      • Std
      • Community
      • 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 (assert$ (implies (feat-embedp feat)
                                 (and (ubyte4p instr.rd)
                                      (ubyte4p instr.rs1)))
                        (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$ (and (feat-32p feat)
                     (implies (feat-embedp feat)
                              (and (ubyte4p instr.rd)
                                   (ubyte4p instr.rs1))))
                (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$ (and (feat-64p feat)
                     (implies (feat-embedp feat)
                              (and (ubyte4p instr.rd)
                                   (ubyte4p instr.rs1))))
                (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$ (and (feat-64p feat)
                     (implies (feat-embedp feat)
                              (and (ubyte4p instr.rd)
                                   (ubyte4p instr.rs1))))
                (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$ (and (feat-64p feat)
                     (implies (feat-embedp feat)
                              (and (ubyte4p instr.rd)
                                   (ubyte4p instr.rs1))))
                (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 (assert$ (implies (feat-embedp feat)
                              (ubyte4p instr.rd))
                     (b* ((opcode 55))
                       (logappn 7 opcode 5 instr.rd 20 instr.imm)))
       :auipc (assert$ (implies (feat-embedp feat)
                                (ubyte4p instr.rd))
                       (b* ((opcode 23))
                         (logappn 7 opcode 5 instr.rd 20 instr.imm)))
       :op (assert$ (and (implies (feat-embedp feat)
                                  (and (ubyte4p instr.rd)
                                       (ubyte4p instr.rs1)
                                       (ubyte4p instr.rs2)))
                         (implies (member-eq (op-funct-kind instr.funct)
                                             '(:mul :mulh
                                                    :mulhu :mulhsu
                                                    :div :divu
                                                    :rem :remu))
                                  (feat-mp feat)))
                    (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$
            (and (feat-64p feat)
                 (implies (feat-embedp feat)
                          (and (ubyte4p instr.rd)
                               (ubyte4p instr.rs1)
                               (ubyte4p instr.rs2)))
                 (implies (member-eq (op-32-funct-kind instr.funct)
                                     '(:mulw :divw :divuw :remw :remuw))
                          (feat-mp 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
       (assert$ (implies (feat-embedp feat)
                         (ubyte4p instr.rd))
                (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 (assert$ (implies (feat-embedp feat)
                               (and (ubyte4p instr.rd)
                                    (ubyte4p instr.rs1)))
                      (b* ((opcode 103) (funct3 0))
                        (logappn 7 opcode 5 instr.rd
                                 3 funct3 5 instr.rs1 12 instr.imm)))
       :branch
       (assert$ (implies (feat-embedp feat)
                         (and (ubyte4p instr.rs1)
                              (ubyte4p instr.rs2)))
                (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
       (assert$ (and (load-funct-case instr.funct
                                      :lb t
                                      :lbu t
                                      :lh t
                                      :lhu t
                                      :lw t
                                      :lwu (feat-64p feat)
                                      :ld (feat-64p feat))
                     (implies (feat-embedp feat)
                              (and (ubyte4p instr.rd)
                                   (ubyte4p instr.rs1))))
                (b* ((opcode 3)
                     (funct3 (encode-load-funct instr.funct feat)))
                  (logappn 7 opcode 5 instr.rd
                           3 funct3 5 instr.rs1 12 instr.imm)))
       :store
       (assert$ (and (store-funct-case instr.funct
                                       :sb t
                                       :sh t
                                       :sw t
                                       :sd (feat-64p feat))
                     (implies (feat-embedp feat)
                              (and (ubyte4p instr.rs1)
                                   (ubyte4p instr.rs2))))
                (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)