落地 isa 合同:饱和类型、操作码枚举、32-bit 指令编解码。
- Types.h:TIME 改 int64_t(毫秒),补 sat_add/sub/mul/div,除零与 INT_MIN/-1 规则 - Op.h:29 条操作码枚举 + mnemonic 助记符,数值即编码 - Instr.h:pack/拆字段 + 按形态 enc_rr/rrr/imm/jmp/jc/slot/call/ret
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* @file Instr.h
|
||||
* @brief 32-bit 指令打包 / 拆字段
|
||||
* @author
|
||||
* @date 2026-08-19
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "isa/Op.h"
|
||||
|
||||
namespace isa {
|
||||
// 指令字:[ op:8 | rd:8 | a:8 | b:8 ],整体是一个小端 uint32_t。
|
||||
// 字段含义按操作码形态解释,见 Doc/isa/指令与映像.md。
|
||||
typedef uint32_t Instr;
|
||||
|
||||
// 打包 / 拆字段
|
||||
inline Instr pack(Op op, uint8_t rd, uint8_t a, uint8_t b) {
|
||||
return static_cast<uint32_t>(static_cast<uint8_t>(op))
|
||||
| (static_cast<uint32_t>(rd) << 8)
|
||||
| (static_cast<uint32_t>(a) << 16)
|
||||
| (static_cast<uint32_t>(b) << 24);
|
||||
}
|
||||
|
||||
inline Op op(Instr w) {
|
||||
return static_cast<Op>(w & 0xFFu);
|
||||
}
|
||||
|
||||
inline uint8_t rd(Instr w) {
|
||||
return static_cast<uint8_t>((w >> 8) & 0xFFu);
|
||||
}
|
||||
|
||||
inline uint8_t a(Instr w) {
|
||||
return static_cast<uint8_t>((w >> 16) & 0xFFu);
|
||||
}
|
||||
|
||||
inline uint8_t b(Instr w) {
|
||||
return static_cast<uint8_t>((w >> 24) & 0xFFu);
|
||||
}
|
||||
|
||||
// 组合视图:a|b 拼成 16 位(const_id / fn_id / slot)
|
||||
inline uint16_t imm16(Instr w) {
|
||||
return static_cast<uint16_t>(a(w) | (static_cast<uint16_t>(b(w)) << 8));
|
||||
}
|
||||
|
||||
// 组合视图:a|b 为有符号相对偏移,单位是指令条数
|
||||
inline int16_t off16(Instr w) {
|
||||
return static_cast<int16_t>(imm16(w));
|
||||
}
|
||||
|
||||
// 按形态的便捷编码
|
||||
inline Instr enc_rr(Op op, uint8_t rd, uint8_t rs) {
|
||||
return pack(op, rd, rs, 0); // MOVE / NOT:a = rs
|
||||
}
|
||||
|
||||
inline Instr enc_rrr(Op op, uint8_t rd, uint8_t ra, uint8_t rb) {
|
||||
return pack(op, rd, ra, rb); // AND/OR/ADD/.../CMP_xx
|
||||
}
|
||||
|
||||
inline Instr enc_imm(Op op, uint8_t rd, uint16_t imm) {
|
||||
return pack(op, rd, static_cast<uint8_t>(imm & 0xFFu),
|
||||
static_cast<uint8_t>((imm >> 8) & 0xFFu)); // LOADK
|
||||
}
|
||||
|
||||
inline Instr enc_jmp(int16_t off) {
|
||||
return pack(Op::JMP, 0,
|
||||
static_cast<uint8_t>(static_cast<uint16_t>(off) & 0xFFu),
|
||||
static_cast<uint8_t>((static_cast<uint16_t>(off) >> 8) & 0xFFu));
|
||||
}
|
||||
|
||||
inline Instr enc_jc(Op op, uint8_t r, int16_t off) {
|
||||
// JT / JF:条件寄存器在 rd,偏移在 a|b
|
||||
return pack(op, r,
|
||||
static_cast<uint8_t>(static_cast<uint16_t>(off) & 0xFFu),
|
||||
static_cast<uint8_t>((static_cast<uint16_t>(off) >> 8) & 0xFFu));
|
||||
}
|
||||
|
||||
inline Instr enc_slot(Op op, uint8_t rd, uint16_t slot) {
|
||||
// LOAD_I / STORE_Q / LOAD_M / STORE_M / LOAD_GLOBAL / STORE_GLOBAL / CAL_*
|
||||
// 槽号 / 实例号在 a|b
|
||||
return enc_imm(op, rd, slot);
|
||||
}
|
||||
|
||||
inline Instr enc_call(uint16_t fn_id) {
|
||||
return enc_imm(Op::CALL, 0, fn_id);
|
||||
}
|
||||
|
||||
inline Instr enc_ret() {
|
||||
return pack(Op::RET, 0, 0, 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user