阶段2-2:MachineConfig 重写为 V2(强校验 5 项)+ TypeInfo/machine_test V2 适配

- ConfigType:base/kind/func(V1 的 tag/range 删除);ConfigOp:prefix/op/fmt/
  func_mode(V1 的 opcode/class 删除);ConfigFb:fb_id(V1 的 opcode 删除)
- 强校验:meta(version=2) / type 19 条全集·base 命中·kind 合法·func 与
  (width,signed,float) 互锁 / op prefix 合法·prefix↔fmt 互锁·(prefix,op) 分区
  唯一·func 类别·params 互锁 / fb 15 条全集·fb_id 唯一·字段命中 type 表 /
  op-fb 互锁(CAL↔fb 表)
- TypeMeta:tag/range → func/kind
- machine_test:V2 断言(正例 68 op/19 type/15 fb + 14 个负例)
- 状态:compiler 库暂挂于 V1 Codec/Codegen/Stb/main(下一步按
  Codec → Stb → Codegen → main 顺序 V2 化)
This commit is contained in:
2026-08-25 20:04:49 +08:00
parent fc76fea47b
commit a4335693d4
4 changed files with 545 additions and 344 deletions
+57 -41
View File
@@ -1,13 +1,16 @@
/**
* @file MachineConfig.h
* @brief 机器定义(machine.toml)加载与强校验
* @brief 机器定义(machine.toml V2)加载与强校验
* @author
* @date 2026-08-21
*
* @details 见 Doc/isa/指令配置.md
* @details 见 Doc/isa/指令配置.mdV2 schema
* - 配置只给编译器(toml++ 运行时加载);vm 不读配置(内建全指令集)
* - 强校验 5 项:字段必填 / type.base 命中内建基元·range 合法·tag 契约 /
* op opcode 唯一·类别-格式互锁 / fb.opcode 与 op 一致·字段类型命中 type 表
* - 强校验 5 项:metaversion=2/ type 19 条全集·base 命中基元·kind 合法·
* func 与 base (width,signed) 互锁 / op prefix 合法·prefix↔fmt 互锁·
* op 分区内唯一·func 类别合法·params 数量与 fmt 互锁 /
* fb 15 条全集(fb_id 0..14)·字段类型命中 type 表·字段名唯一 /
* op-fb 互锁(CAL 存在 ↔ fb 表非空)
* - 基元内置(bit/int8..uint64/float32/float64,元数据在代码)
*/
@@ -31,30 +34,31 @@ namespace compiler {
};
/**
* @brief 配置类型 = 基元别名+ 值域约束 + .stb 契约 tag)。
* @details 对应 machine.toml [[type]] 行:base 必为内建基元名;range 可选且
* min ≤ maxtag 与 .stb 常量表契约一致(0=BOOL、1=INT、2=TIME,唯一)。
* @brief 配置类型 = 基元别名 + kind 语义 + func 映射(V2)。
* @details 对应 machine.toml [[type]] 行:base 必为内建基元名;kind 为语言语义
* bool/bit/int/uint/float/time/date);func 为 type 位段值(0..9),
* 与 base 的 (width, signed, is_float) 互锁强校验。
*/
struct ConfigType {
std::string name; ///< 配置类型名(大写,如 BOOL/INT/TIME
std::string base; ///< 基元名(命中内建基元表)
bool has_range = false; ///< 是否有值域约束
int64_t range_min = 0; ///< 值域下界(含
int64_t range_max = 0; ///< 值域上界(含)
uint32_t tag = 0; ///< .stb 常量表契约 tag
std::string name; ///< 配置类型名(大写,如 BOOL/INT/TIME
std::string base; ///< 基元名(命中内建基元表)
std::string kind; ///< 语言语义:bool/bit/int/uint/float/time/date
uint32_t func = 0; ///< type 位段值(0..9
};
/**
* @brief 配置指令。
* @details 对应 machine.toml [[op]] 行:opcode 唯一且 0..255class 与 format
* 互锁(INSTANCE ↔ CAL);params 数量与 format 期望一致。
* @brief 配置指令V2prefix + op + fmt + func 类别)
* @details 对应 machine.toml [[op]] 行:prefix 为形态前缀(11/10/011/010000..
* 010101/0010/0011/0001),op 为分区内编号(0 起);prefix 与 fmt 互锁;
* func_mode 为数据语义类别(none/width/uint/full/float/dst)。
*/
struct ConfigOp {
std::string name; ///< 助记符(与 .stb / isa 一致)
uint32_t opcode = 0; ///< 操作码(0..255,表内唯一
bool is_instance = false; ///< classinstance / plain
std::string format; ///< 操作数形态:RR/RRR/IMM/SLOT/JMP/JC/CALL/CAL/NONE
std::vector<std::string> params; ///< 参数名(数量与 format 一致)
std::string prefix; ///< 形态前缀(长度+形态分区
uint32_t op = 0; ///< 分区内编号(0 起)
std::string fmt; ///< 操作数形态:RR/NONE/RRR/IMM/SLOT/JMP/JC/CALL/CAL/SLOT+off/IMM64/预留
std::string func_mode; ///< func 类别:none/width/uint/full/float/dst
std::vector<std::string> params; ///< 参数名(数量与 fmt 一致)
bool enabled = false; ///< 是否启用
};
@@ -63,34 +67,37 @@ namespace compiler {
*/
struct ConfigFbField {
std::string name; ///< 字段名(如 in/pt/q/et/cu/cv
std::string type; ///< 配置类型名(BOOL/INT/TIME
std::string type; ///< 配置类型名(BOOL/INT/TIME/...
};
/**
* @brief 内建功能块(FB)布局登记。
* @details 对应 machine.toml [[fb]] 行;opcode 必须命中 instance 类 op 行。
* @brief 内建功能块(FB)布局登记V2fb_id
* @details 对应 machine.toml [[fb]] 行;fb_id 唯一(0..1415 条全集);
* CAL 指令(010101)的 fb_id 由本表提供。
*/
struct ConfigFb {
std::string name; ///< FB 名(写,如 ton/ctu
uint32_t opcode = 0; ///< 对应 op 行 opcodeCAL_*
std::string name; ///< FB 名(写,如 TON/CTU
uint32_t fb_id = 0; ///< FB 类型 id0..14
std::vector<ConfigFbField> fields; ///< 字段列表(名称唯一,类型命中 type 表)
};
/**
* @brief 机器定义:加载 machine.toml 并做强校验。
* @brief 机器定义:加载 machine.toml 并做强校验V2
* @details 编译器指令集 / 类型的唯一事实来源;校验失败时 ok() 为 false
* err 前缀 "machine error"。vm 不读配置(内建全指令集)。
*/
class MachineConfig {
public:
/**
* @brief 加载 + 强校验。
* @brief 加载 + 强校验V2
* @param path machine.toml 路径
* @param err 错误输出;可为 nullptr(静默)
* @return true 成功;falseerr 已写,前缀 "machine error"
* @details 强校验项:字段必填 / type.base 命中内建基元·range 合法·tag 契约 /
* op opcode 唯一·类别-格式互锁 / fb.opcode 与 op 一致·字段类型命中
* type 表(清单见文件头)。
* @details 强校验项:metaname/version=2/ type 19 条全集·base 命中基元·
* kind 合法·func 与 (width,signed,float) 互锁 / op prefix 合法·
* prefix↔fmt 互锁·(prefix,op) 分区内唯一·func 类别合法·
* params 数量与 fmt 互锁 / fb 15 条全集·fb_id 唯一·字段类型命中
* type 表·字段名唯一 / op-fb 互锁(CAL ↔ fb 表非空)。
*/
bool load(const std::string& path, std::string* err);
@@ -98,7 +105,7 @@ namespace compiler {
bool ok() const { return ok_; }
/// @brief 型号名(meta.name,参与 .stb 型号标识)。
const std::string& model_name() const { return model_name_; }
/// @brief 指令集版本(meta.version,参与 .stb 型号标识)。
/// @brief 格式版本(meta.version= 2参与 .stb 型号标识)。
uint32_t version() const { return version_; }
/// @brief 配置类型表([[type]])。
@@ -110,7 +117,7 @@ namespace compiler {
/**
* @brief 按配置类型名查类型行。
* @param name 配置类型名
* @param name 配置类型名(如 "INT"
* @return 命中指针;未找到返回 nullptr
*/
const ConfigType* find_type(const std::string& name) const;
@@ -123,26 +130,35 @@ namespace compiler {
const ConfigOp* find_op(const std::string& name) const;
/**
* @brief 按 opcode 查指令行。
* @param opcode 操作码(0..255
* @brief 按 (prefix, op) 键查指令行。
* @param prefix 形态前缀(如 "11"/"010001"
* @param op 分区内编号
* @return 命中指针;未找到返回 nullptr
*/
const ConfigOp* find_op_by_code(uint32_t opcode) const;
const ConfigOp* find_op_by_key(const std::string& prefix, uint32_t op) const;
/**
* @brief 查询某 opcode 是否启用。
* @param opcode 操作码
* @return 指令存在且 enabled 为 true;未知 opcode 返回 false
* @brief 查询某 (prefix, op) 是否启用。
* @param prefix 形态前缀
* @param op 分区内编号
* @return 指令存在且 enabled 为 true;未知返回 false
*/
bool op_enabled(uint32_t opcode) const;
bool op_enabled(const std::string& prefix, uint32_t op) const;
/**
* @brief 按名称查内建 FB。
* @param name FB 名(小写,如 "ton"
* @param name FB 名(如 "TON"
* @return 命中指针;未找到返回 nullptr
*/
const ConfigFb* find_fb(const std::string& name) const;
/**
* @brief 按 fb_id 查内建 FB。
* @param fb_id FB 类型 id0..14
* @return 命中指针;未找到返回 nullptr
*/
const ConfigFb* find_fb_by_id(uint32_t fb_id) const;
/// @brief 内建基元表(bit / int8..uint64 / float32 / float64)。
static const std::vector<PrimType>& prims();
@@ -156,7 +172,7 @@ namespace compiler {
private:
bool ok_ = false; ///< 加载成功标志
std::string model_name_; ///< 型号名(meta.name
uint32_t version_ = 0; ///< 指令集版本(meta.version
uint32_t version_ = 0; ///< 格式版本(meta.version
std::vector<ConfigType> types_; ///< 配置类型表
std::vector<ConfigOp> ops_; ///< 配置指令表
std::vector<ConfigFb> fbs_; ///< 内建 FB 表
+9 -15
View File
@@ -5,7 +5,7 @@
* @date 2026-08-21
*
* @details 类型定义数据化(配置),运算/类型检查语义代码化:
* - 元数据(宽度/符号/浮点/base/range/tag)全部来自 machine.toml + 内建基元表
* - 元数据(宽度/符号/浮点/kind/func)全部来自 machine.toml + 内建基元表
* - 查询大小写不敏感(配置名大写,Linker type_name 小写)
*/
@@ -19,12 +19,12 @@
namespace compiler {
/**
* @brief 语言类型元数据。
* @brief 语言类型元数据V2
* @details 由配置类型行 + 内建基元合成;config 或 prim 未命中时
* 对应访问器取安全默认值,operator bool 为 false。
*/
struct TypeMeta {
const ConfigType* config = nullptr; ///< 配置类型行(别名 + range + tag
const ConfigType* config = nullptr; ///< 配置类型行(别名 + kind + func
const PrimType* prim = nullptr; ///< 内建基元(宽度/符号/浮点)
/// @brief 宽度(字节);无基元返回 0。
@@ -33,18 +33,12 @@ namespace compiler {
bool is_signed() const { return prim ? prim->is_signed : false; }
/// @brief 是否浮点;无基元返回 false。
bool is_float() const { return prim ? prim->is_float : false; }
/// @brief .stb 契约 tag;无配置返回 0。
uint32_t tag() const { return config ? config->tag : 0; }
/// @brief 是否有值域约束;无配置返回 false
bool has_range() const { return config ? config->has_range : false; }
/**
* @brief 值是否落在配置值域内。
* @param v 待查值
* @return 无配置或无 range 时为 true;否则 min ≤ v ≤ max
*/
bool value_in_range(int64_t v) const {
return !config || !config->has_range ||
(v >= config->range_min && v <= config->range_max);
/// @brief type 位段值 func0..9;无配置返回 0。
uint32_t func() const { return config ? config->func : 0; }
/// @brief 语言语义 kindbool/bit/int/uint/float/time/date);无配置返回空串
const std::string& kind() const {
static const std::string kEmpty;
return config ? config->kind : kEmpty;
}
/// @brief 是否有效(config 与 prim 都已命中)。