Encode an instruction in the normal way (i.e. in 32 bits).
We calculate the fields and we concatenate them using
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
Function:
(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:
(defthm ubyte32p-of-encode (b* ((encoding (encode instr feat))) (ubyte32p encoding)) :rule-classes :rewrite)
Theorem:
(defthm encode-of-instr-fix-instr (equal (encode (instr-fix instr) feat) (encode instr feat)))
Theorem:
(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:
(defthm encode-of-feat-fix-feat (equal (encode instr (feat-fix feat)) (encode instr feat)))
Theorem:
(defthm encode-feat-equiv-congruence-on-feat (implies (feat-equiv feat feat-equiv) (equal (encode instr feat) (encode instr feat-equiv))) :rule-classes :congruence)