- STCompiler --dump-map:编译后打印变量/槽映射(函数局部 r 寄存器、 全局 slot/off/type/func、实例 slot/off/fb/size/字段偏移) - CodegenMap 输出结构(codegen_project 可选参数) - 修复参数循环 bug(i+1<argc 漏掉末尾单参数选项) - 结构自检(临时 stbcheck):13 用例寄存器/func/跳转/槽/长度全部通过 - 13 正例逐条对照全部通过,产出 Doc/compiler/用例反汇编对照.md - 已知缺陷记录:LREAL 字面量装载按 REAL(TODO)
53 lines
2.0 KiB
C++
53 lines
2.0 KiB
C++
/**
|
||
* @file Codegen.h
|
||
* @brief 寄存器码生成(12.8,切片 1:MOVE / LOADK / RET)
|
||
* @author
|
||
* @date 2026-08-21
|
||
*
|
||
* @details 代码生成:输入 Project + Unit(AST)+ LinkResult,输出 .stb 映像字节。
|
||
* 寄存器分配:r0 结果、r1..r7 参数、r8+ 变量/临时;跳转偏移相对下一条。
|
||
*/
|
||
|
||
#pragma once
|
||
|
||
#include <cstdint>
|
||
#include <map>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include "compiler/Linker.h"
|
||
#include "compiler/MachineConfig.h"
|
||
#include "compiler/Parser.h"
|
||
#include "compiler/Project.h"
|
||
|
||
namespace compiler {
|
||
|
||
/**
|
||
* @brief 变量/槽映射(--dump-map 用;验证源码 ↔ 反汇编对照)。
|
||
*/
|
||
struct CodegenMap {
|
||
/// 每函数:{ 函数名, { 局部变量名 → "寄存器号" } }
|
||
std::vector<std::pair<std::string, std::map<std::string, std::string>>> funcs;
|
||
/// 全局:{ 变量名 → "slot=.. off=.. type=.." }
|
||
std::map<std::string, std::string> globals;
|
||
/// 实例:{ "POU名/实例名" → "slot=.. off=.. fb=.. size=.. 字段:名@off.." }
|
||
std::map<std::string, std::string> instances;
|
||
};
|
||
|
||
/**
|
||
* @brief 编译工程为 .stb 映像字节(在链接 + 类型检查成功后调用)。
|
||
* @param proj 工程定义(cycle_limit / dt_ms / 哈希)
|
||
* @param units 全部源文件的 AST
|
||
* @param link 链接结果(POU 顺序 / 全局符号 / FB 布局)
|
||
* @param cfg 机器定义(machine.toml;指令 opcode / 类型 tag / FB 布局)
|
||
* @param image 输出映像字节
|
||
* @param err 错误输出;可为 nullptr(静默)
|
||
* @return true 成功;false 失败(err 前缀 "codegen error")
|
||
* @details 指令 opcode / 类型 tag / FB 布局全部来自 machine.toml(cfg)。
|
||
*/
|
||
bool codegen_project(const Project& proj, const std::vector<SourceUnit>& units,
|
||
const LinkResult& link, const MachineConfig& cfg,
|
||
std::vector<uint8_t>* image, std::string* err,
|
||
CodegenMap* map = nullptr);
|
||
}
|