Files
Interpreter/isa/include/isa/Op.h
T
Admin 96607cfe75 isa 模块注释完善:Doxygen 风格。
- Types.h:定宽类型、TypeTag、饱和四则的 @brief/@param/@return
- Op.h:Op 枚举冻结约束、OpFormat/OpClass、OpDef/kOpDefs 表、三个查询函数
- Instr.h:指令字布局、pack/拆字段、按形态 enc_* 系列
- Encode.h/.cpp:Decoded 字段语义、encode/decode/disasm 契约
2026-08-21 23:19:08 +08:00

166 lines
6.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file Op.h
* @brief 操作码枚举、操作数形态与编译期指令表
* @details 操作码数值即 .stb 指令编码。执行器(vm)只依赖本表;
* 编译器侧的登记见 Doc/isa/指令配置.mdcompiler/machine.toml,与之强校验)。
* @author
* @date 2026-08-19
*/
#pragma once
#include <cstdint>
namespace isa {
/**
* @brief 操作码枚举。
* @details 数值即编码:冻结后不得改顺序、不得插值,增加指令只能追加到末尾,
* 并同步 Doc/isa/指令与映像.md、compiler/machine.toml 与测试。
*/
enum class Op : uint8_t {
MOVE = 0, // MOVE rd, rs
LOADK = 1, // LOADK rd, const_id
NOT = 2, // NOT rd, rs
AND = 3, // AND rd, ra, rb
OR = 4, // OR rd, ra, rb
ADD = 5, // ADD rd, ra, rb
SUB = 6, // SUB rd, ra, rb
MUL = 7, // MUL rd, ra, rb
DIV = 8, // DIV rd, ra, rb
CMP_EQ = 9, // CMP_EQ rd, ra, rb
CMP_NE = 10, // CMP_NE rd, ra, rb
CMP_LT = 11, // CMP_LT rd, ra, rb
CMP_LE = 12, // CMP_LE rd, ra, rb
CMP_GT = 13, // CMP_GT rd, ra, rb
CMP_GE = 14, // CMP_GE rd, ra, rb
JMP = 15, // JMP offset
JT = 16, // JT r, offset
JF = 17, // JF r, offset
LOAD_I = 18, // LOAD_I rd, slot
STORE_Q = 19, // STORE_Q rs, slot
LOAD_M = 20, // LOAD_M rd, slot
STORE_M = 21, // STORE_M rs, slot
LOAD_GLOBAL = 22, // LOAD_GLOBAL rd, slot
STORE_GLOBAL = 23, // STORE_GLOBAL rs, slot
// 内置功能块(8 个连续,12.11 扩充:TON/TOF/TP/CTU/CTD/CTUD/R_TRIG/F_TRIG
CAL_TON = 24, // CAL_TON instance_slot
CAL_TOF = 25, // CAL_TOF instance_slot
CAL_TP = 26, // CAL_TP instance_slot
CAL_CTU = 27, // CAL_CTU instance_slot
CAL_CTD = 28, // CAL_CTD instance_slot
CAL_CTUD = 29, // CAL_CTUD instance_slot
CAL_R_TRIG = 30, // CAL_R_TRIG instance_slot
CAL_F_TRIG = 31, // CAL_F_TRIG instance_slot
CALL = 32, // CALL fn_id
RET = 33, // RET
};
/// 操作码总数(= 最大 opcode + 1kOpDefs 的下标上界)。
static const int kOpCount = 34;
/**
* @brief 操作数形态(三层分类第二层,见 Doc/isa/指令配置.md)。
* @details 决定操作数 a|b 的解释方式与反汇编文本格式。
*/
enum class OpFormat : uint8_t {
RR, // 两寄存器:rd rsMOVE / NOT
RRR, // 三寄存器:rd ra rb(逻辑 / 算术 / 比较)
IMM, // 常量:rd const_idLOADK
SLOT, // 槽:rd slotLOAD_* / STORE_*
JMP, // 偏移:off
JC, // 条件跳转:r offJT / JF
CALL, // 调用:fn_id
CAL, // 实例:instanceCAL_*
NONE, // 无操作数(RET
};
/**
* @brief 操作对象大类(三层分类第一层)。
*/
enum class OpClass : uint8_t {
Plain, // 无实例:寄存器 / 立即数 / 槽 / 分支 / 调用
Instance, // 有实例:操作数据区实例块(内建 FB)
};
/**
* @brief 编译期指令表条目。
* @details 一条指令的名称、操作数形态与大类;下标与 Op 数值一一对应。
*/
struct OpDef {
const char* name; ///< 助记符(与 .stb / machine.toml 一致)
OpFormat format; ///< 操作数形态
OpClass cls; ///< 操作对象大类
};
/**
* @brief 编译期指令表:下标与 Op 数值一一对应(machine.toml 强校验的基准)。
* @details 表与 Op 枚举同步维护;查询入口见 mnemonic / format / op_class。
*/
static const OpDef kOpDefs[kOpCount] = {
{"MOVE", OpFormat::RR, OpClass::Plain}, // 0
{"LOADK", OpFormat::IMM, OpClass::Plain}, // 1
{"NOT", OpFormat::RR, OpClass::Plain}, // 2
{"AND", OpFormat::RRR, OpClass::Plain}, // 3
{"OR", OpFormat::RRR, OpClass::Plain}, // 4
{"ADD", OpFormat::RRR, OpClass::Plain}, // 5
{"SUB", OpFormat::RRR, OpClass::Plain}, // 6
{"MUL", OpFormat::RRR, OpClass::Plain}, // 7
{"DIV", OpFormat::RRR, OpClass::Plain}, // 8
{"CMP_EQ", OpFormat::RRR, OpClass::Plain}, // 9
{"CMP_NE", OpFormat::RRR, OpClass::Plain}, // 10
{"CMP_LT", OpFormat::RRR, OpClass::Plain}, // 11
{"CMP_LE", OpFormat::RRR, OpClass::Plain}, // 12
{"CMP_GT", OpFormat::RRR, OpClass::Plain}, // 13
{"CMP_GE", OpFormat::RRR, OpClass::Plain}, // 14
{"JMP", OpFormat::JMP, OpClass::Plain}, // 15
{"JT", OpFormat::JC, OpClass::Plain}, // 16
{"JF", OpFormat::JC, OpClass::Plain}, // 17
{"LOAD_I", OpFormat::SLOT, OpClass::Plain}, // 18
{"STORE_Q", OpFormat::SLOT, OpClass::Plain}, // 19
{"LOAD_M", OpFormat::SLOT, OpClass::Plain}, // 20
{"STORE_M", OpFormat::SLOT, OpClass::Plain}, // 21
{"LOAD_GLOBAL", OpFormat::SLOT, OpClass::Plain}, // 22
{"STORE_GLOBAL", OpFormat::SLOT, OpClass::Plain},// 23
{"CAL_TON", OpFormat::CAL, OpClass::Instance}, // 24
{"CAL_TOF", OpFormat::CAL, OpClass::Instance}, // 25
{"CAL_TP", OpFormat::CAL, OpClass::Instance}, // 26
{"CAL_CTU", OpFormat::CAL, OpClass::Instance}, // 27
{"CAL_CTD", OpFormat::CAL, OpClass::Instance}, // 28
{"CAL_CTUD", OpFormat::CAL, OpClass::Instance}, // 29
{"CAL_R_TRIG", OpFormat::CAL, OpClass::Instance},// 30
{"CAL_F_TRIG", OpFormat::CAL, OpClass::Instance},// 31
{"CALL", OpFormat::CALL, OpClass::Plain}, // 32
{"RET", OpFormat::NONE, OpClass::Plain}, // 33
};
/**
* @brief 查助记符。
* @param op 操作码
* @return 对应助记符;越界返回哨兵 "???"
*/
inline const char* mnemonic(Op op) {
const int idx = static_cast<int>(op);
return (idx >= 0 && idx < kOpCount) ? kOpDefs[idx].name : "???";
}
/**
* @brief 查操作数形态。
* @param op 操作码
* @return 对应 OpFormat;越界返回 OpFormat::NONE
*/
inline OpFormat format(Op op) {
const int idx = static_cast<int>(op);
return (idx >= 0 && idx < kOpCount) ? kOpDefs[idx].format : OpFormat::NONE;
}
/**
* @brief 查操作对象大类。
* @param op 操作码
* @return 对应 OpClass;越界返回 OpClass::Plain
*/
inline OpClass op_class(Op op) {
const int idx = static_cast<int>(op);
return (idx >= 0 && idx < kOpCount) ? kOpDefs[idx].cls : OpClass::Plain;
}
}