阶段 A 步骤 3:MachineConfig 加载层(toml++ + 强校验 5 项)。
- MachineConfig.h/cpp:解析 machine.toml → meta/types/ops/fbs 表 - 强校验:全属性必填、base 命中内建基元、range min≤max、tag 契约(BOOL=0/INT=1/TIME=2 唯一)、 opcode 唯一 0..255、类别-格式互锁(instance↔CAL)、params 数量与 format 一致、 fb.opcode 匹配 instance op、fb 字段类型命中 type 表 - 基元内置表(bit/int8..uint64/float32/64,宽度/符号/浮点) - machine_test:38 断言(正例仓库表 + 9 类负例 + 最小配置),ctest 12/12
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* @file MachineConfig.h
|
||||
* @brief 机器定义(machine.toml)加载与强校验
|
||||
* @author
|
||||
* @date 2026-08-21
|
||||
*
|
||||
* @details 见 Doc/isa/指令配置.md:
|
||||
* - 配置只给编译器(toml++ 运行时加载);vm 不读配置(内建全指令集)
|
||||
* - 强校验 5 项:字段必填 / type.base 命中内建基元·range 合法·tag 契约 /
|
||||
* op opcode 唯一·类别-格式互锁 / fb.opcode 与 op 一致·字段类型命中 type 表
|
||||
* - 基元内置(bit/int8..uint64/float32/float64,元数据在代码)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace compiler {
|
||||
|
||||
// 内建基元(编译器内建,C 语义)
|
||||
struct PrimType {
|
||||
const char* name;
|
||||
uint32_t width; // 字节
|
||||
bool is_signed;
|
||||
bool is_float;
|
||||
};
|
||||
|
||||
// 配置类型 = 基元别名(+ 值域约束 + .stb 契约 tag)
|
||||
struct ConfigType {
|
||||
std::string name;
|
||||
std::string base;
|
||||
bool has_range = false;
|
||||
int64_t range_min = 0;
|
||||
int64_t range_max = 0;
|
||||
uint32_t tag = 0;
|
||||
};
|
||||
|
||||
// 配置指令
|
||||
struct ConfigOp {
|
||||
std::string name;
|
||||
uint32_t opcode = 0;
|
||||
bool is_instance = false; // class: instance / plain
|
||||
std::string format; // RR/RRR/IMM/SLOT/JMP/JC/CALL/CAL/NONE
|
||||
std::vector<std::string> params;
|
||||
bool enabled = false;
|
||||
};
|
||||
|
||||
struct ConfigFbField {
|
||||
std::string name;
|
||||
std::string type; // 配置类型名(BOOL/INT/TIME)
|
||||
};
|
||||
|
||||
struct ConfigFb {
|
||||
std::string name;
|
||||
uint32_t opcode = 0;
|
||||
std::vector<ConfigFbField> fields;
|
||||
};
|
||||
|
||||
class MachineConfig {
|
||||
public:
|
||||
// 加载 + 强校验;失败返回 false 并写 err(前缀 "machine error")
|
||||
bool load(const std::string& path, std::string* err);
|
||||
|
||||
bool ok() const { return ok_; }
|
||||
const std::string& model_name() const { return model_name_; }
|
||||
uint32_t version() const { return version_; }
|
||||
|
||||
const std::vector<ConfigType>& types() const { return types_; }
|
||||
const std::vector<ConfigOp>& ops() const { return ops_; }
|
||||
const std::vector<ConfigFb>& fbs() const { return fbs_; }
|
||||
|
||||
const ConfigType* find_type(const std::string& name) const;
|
||||
const ConfigOp* find_op(const std::string& name) const;
|
||||
const ConfigOp* find_op_by_code(uint32_t opcode) const;
|
||||
bool op_enabled(uint32_t opcode) const;
|
||||
const ConfigFb* find_fb(const std::string& name) const;
|
||||
|
||||
// 内建基元表
|
||||
static const std::vector<PrimType>& prims();
|
||||
static const PrimType* find_prim(const std::string& name);
|
||||
|
||||
private:
|
||||
bool ok_ = false;
|
||||
std::string model_name_;
|
||||
uint32_t version_ = 0;
|
||||
std::vector<ConfigType> types_;
|
||||
std::vector<ConfigOp> ops_;
|
||||
std::vector<ConfigFb> fbs_;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user