阶段 B 步骤 5:类型元数据配置化(TypeInfo)。
- TypeInfo.h/cpp:TypeMeta(配置别名 + 内建基元解析),查询大小写不敏感; 提供 width/is_signed/is_float/tag/has_range/value_in_range - Codegen:layout_data 初值按 type_meta 的基元宽度/浮点写(删除手写宽度分支); const_id 的 tag 改查 type_meta - machine_test:+10 断言(BOOL 1B/range[0,1]/tag0、INT 2B 有符号、TIME 8B、大小写不敏感、未定义空) - 验收:line1 224 字节 hash 0xc3fe4ae74ad45900 不变、ctest 12/12
This commit is contained in:
+10
-10
@@ -42,6 +42,7 @@
|
||||
#include "compiler/Codec.h"
|
||||
#include "compiler/MachineConfig.h"
|
||||
#include "compiler/Stb.h"
|
||||
#include "compiler/TypeInfo.h"
|
||||
|
||||
namespace compiler {
|
||||
namespace {
|
||||
@@ -589,20 +590,19 @@ namespace {
|
||||
const int64_t v = has_init ? init : 0;
|
||||
const size_t off = static_cast<size_t>(s.address) * 8;
|
||||
data_.resize(off + 8, 0);
|
||||
// 初值按基元宽度写(类型元数据来自配置别名 → base)
|
||||
const ConfigType* ct = cfg_.find_type(uppercase_of(s.type_name));
|
||||
if (ct == nullptr) {
|
||||
// 初值按类型元数据写(配置别名 → 基元宽度/浮点)
|
||||
const TypeMeta tm = type_meta(cfg_, s.type_name);
|
||||
if (!tm) {
|
||||
return fail("no type in machine.toml for '" + s.type_name + "'");
|
||||
}
|
||||
const PrimType* prim = MachineConfig::find_prim(ct->base);
|
||||
if (prim == nullptr) {
|
||||
return fail("no prim '" + ct->base + "' for type '" + ct->name + "'");
|
||||
if (tm.is_float()) {
|
||||
return fail("float initializer not supported yet");
|
||||
}
|
||||
if (prim->width == 2 && !prim->is_float) {
|
||||
if (tm.width() == 2) {
|
||||
const uint16_t iv = static_cast<uint16_t>(v);
|
||||
data_[off] = static_cast<uint8_t>(iv & 0xFFu);
|
||||
data_[off + 1] = static_cast<uint8_t>((iv >> 8) & 0xFFu);
|
||||
} else if (prim->width >= 8 && !prim->is_float) {
|
||||
} else if (tm.width() == 8) {
|
||||
const uint64_t tv = static_cast<uint64_t>(v);
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
data_[off + i] = static_cast<uint8_t>((tv >> (8 * i)) & 0xFFu);
|
||||
@@ -672,8 +672,8 @@ namespace {
|
||||
* @return const_id(u16)
|
||||
*/
|
||||
uint16_t const_id(const char* type_name, int64_t value) {
|
||||
const ConfigType* ct = cfg_.find_type(type_name);
|
||||
const uint32_t tag = ct ? ct->tag : 0;
|
||||
const TypeMeta tm = type_meta(cfg_, type_name);
|
||||
const uint32_t tag = tm ? tm.tag() : 0;
|
||||
for (size_t i = 0; i < consts_.size(); ++i) {
|
||||
if (consts_[i].tag == tag && consts_[i].value == static_cast<uint64_t>(value)) {
|
||||
return static_cast<uint16_t>(i);
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file TypeInfo.cpp
|
||||
* @brief 语言类型元数据(machine.toml 别名 + 内建基元解析)
|
||||
* @author
|
||||
* @date 2026-08-21
|
||||
*/
|
||||
|
||||
#include "compiler/TypeInfo.h"
|
||||
|
||||
#include <cctype>
|
||||
|
||||
namespace compiler {
|
||||
|
||||
namespace {
|
||||
|
||||
std::string lower(const std::string& s) {
|
||||
std::string out = s;
|
||||
for (char& ch : out) {
|
||||
ch = static_cast<char>(std::tolower(static_cast<unsigned char>(ch)));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TypeMeta type_meta(const MachineConfig& cfg, const std::string& name) {
|
||||
TypeMeta m;
|
||||
const std::string key = lower(name);
|
||||
for (const ConfigType& t : cfg.types()) {
|
||||
if (lower(t.name) == key) {
|
||||
m.config = &t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (m.config != nullptr) {
|
||||
m.prim = MachineConfig::find_prim(m.config->base);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
} // namespace compiler
|
||||
Reference in New Issue
Block a user