Files
Interpreter/isa/include/isa/Op.h
T
Admin 35dabf4c3b 步骤 1:OpFormat/OpClass 显式化 + disasm 表驱动。
- Op.h:新增 OpFormat(RR/RRR/IMM/SLOT/JMP/JC/CALL/CAL/NONE)与
  OpClass(Plain/Instance)枚举;34 条编译期表 kOpDefs(name/format/class,
  与 ops.txt 强校验的基准);mnemonic/format/op_class 查表
- Encode.cpp:disasm 按 format 表驱动输出(文本与之前逐条一致)
- 验证:11 个 ctest 全绿;表查询断言(8 个 INSTANCE=CAL、越界哨兵)
2026-08-21 21:13:36 +08:00

135 lines
5.8 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 操作码枚举 + 助记符
* @author
* @date 2026-08-19
*/
#pragma once
#include <cstdint>
namespace isa {
// 操作码一次列全。数值即编码:冻结后不得改顺序、不得插值,
// 增加指令只能追加到末尾,并同步 Doc/isa/指令与映像.md 与测试。
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
};
// 操作码总数
static const int kOpCount = 34;
// 操作数形态(三层分类第二层,见 Doc/isa/指令配置.md
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
};
// 操作对象大类(三层分类第一层)
enum class OpClass : uint8_t {
Plain, // 无实例:寄存器 / 立即数 / 槽 / 分支 / 调用
Instance, // 有实例:操作数据区实例块(内建 FB)
};
// 编译期指令表:下标与 Op 数值一一对应(ops.txt 强校验的基准)
struct OpDef {
const char* name;
OpFormat format;
OpClass cls;
};
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
};
// 助记符 / 格式 / 大类:查表(越界返回哨兵)
inline const char* mnemonic(Op op) {
const int idx = static_cast<int>(op);
return (idx >= 0 && idx < kOpCount) ? kOpDefs[idx].name : "???";
}
inline OpFormat format(Op op) {
const int idx = static_cast<int>(op);
return (idx >= 0 && idx < kOpCount) ? kOpDefs[idx].format : OpFormat::NONE;
}
inline OpClass op_class(Op op) {
const int idx = static_cast<int>(op);
return (idx >= 0 && idx < kOpCount) ? kOpDefs[idx].cls : OpClass::Plain;
}
}