- Types.h:定宽类型、TypeTag、饱和四则的 @brief/@param/@return - Op.h:Op 枚举冻结约束、OpFormat/OpClass、OpDef/kOpDefs 表、三个查询函数 - Instr.h:指令字布局、pack/拆字段、按形态 enc_* 系列 - Encode.h/.cpp:Decoded 字段语义、encode/decode/disasm 契约
55 lines
1.7 KiB
C++
55 lines
1.7 KiB
C++
/**
|
|
* @file Encode.h
|
|
* @brief 指令编解码与反汇编
|
|
* @details 给 STCompiler --disasm 与测试往返使用;文本格式见 Doc/isa/指令与映像.md。
|
|
* @author
|
|
* @date 2026-08-19
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
#include "isa/Instr.h"
|
|
#include "isa/Op.h"
|
|
|
|
namespace isa {
|
|
/**
|
|
* @brief 解码后的字段视图。
|
|
* @details 各字段是 8 位原始值,含义按操作数形态(OpFormat)解释,
|
|
* a|b 可用 imm16 / off16 组合成 16 位视图。
|
|
*/
|
|
struct Decoded {
|
|
Op op; ///< 操作码
|
|
uint8_t rd; ///< 目标寄存器 / 条件寄存器
|
|
uint8_t a; ///< 源寄存器 / 立即数低 8 位 / 偏移低 8 位
|
|
uint8_t b; ///< 源寄存器 / 立即数高 8 位 / 偏移高 8 位
|
|
};
|
|
|
|
/**
|
|
* @brief 编码:字段视图打包成一条 32 位指令字。
|
|
* @param d 解码后的字段视图
|
|
* @return 打包后的指令字;满足 encode(decode(w)) == w
|
|
*/
|
|
Instr encode(Decoded d);
|
|
|
|
/**
|
|
* @brief 解码:指令字拆成字段视图。
|
|
* @param w 32 位指令字
|
|
* @return 各字段视图
|
|
*/
|
|
Decoded decode(Instr w);
|
|
|
|
/**
|
|
* @brief 反汇编一条指令为一行文本。
|
|
* @param w 指令字
|
|
* @param out 输出缓冲
|
|
* @param cap 缓冲容量(含 '\0');cap==0 时直接返回、不写 out
|
|
* @details 按操作数形态表驱动输出(如 `RET`、`MOVE r1, r2`、`LOADK r0, 3`、
|
|
* `ADD r1, r2, r3`、`JMP +4`、`JT r0, -1`、`CALL 1`、`CAL_TON 0`);
|
|
* 未知操作码输出 `??? 0x<w>`。
|
|
*/
|
|
void disasm(Instr w, char* out, size_t cap);
|
|
}
|