Files
ViperC/App/lib/core/Gen/Generator.vp
T
TermiNexus 8d5dbeab89 feat: 引入 CompilerFactory 永久内存池,1a 每文件 reset 解 NewAstTest OOM
main 创建 512MB perm_pool(MemBuddy 第二实例,永不 reset)并注册
CompilerFactory 工厂(对齐 TPC Translator 装配模式,memhub 保持通用)。
各 Gen Mixin 跨 mark 持久表(struct/枚举/Renum/spec/vtable/全局函数/
闭包/函数指针/Cexport/Reexport/TraceFrames/Doc)全部迁移到 perm_pool,
替代 stdlib.malloc 深拷贝提升速度。

Phase1 1a 主循环(98 includes 全量 parse AST 驻留主 pool → 1GB 耗尽)
改为:StdlibAlloc=1 注册数据进 perm_pool + 每文件翻译完 reset_to(文件级
mark) 回收 AST;1b-pre vpi 预生成循环同样 reset_to(mark1)。同步修 7 处
reset 后悬空隐患:StructTable 表体、add_field DefaultVal、Renum 变体/
别名表内容、vtable full_names 内容、FuncptrRets 深拷贝。

CompilerFactory 类名与模块名同名(合法场景,TPC 已支持模块别名优先)。
2026-08-14 04:02:36 +08:00

165 lines
6.2 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import t, c
from stdint import *
import memhub
import llvmlite
import lib.core.Gen.BaseGenMixin as BaseGenMixin
import lib.core.Gen.TypeGenMixin as TypeGenMixin
import lib.core.Gen.StructGenMixin as StructGenMixin
import lib.core.Gen.ExprGenMixin as ExprGenMixin
import lib.core.Gen.CallGenMixin as CallGenMixin
import lib.core.Gen.FuncGenMixin as FuncGenMixin
import lib.core.Gen.ClassGenMixin as ClassGenMixin
import lib.core.Gen.ImportsGenMixin as ImportsGenMixin
import lib.core.Gen.StmtGenMixin as StmtGenMixin
import lib.core.Gen.AssignGenMixin as AssignGenMixin
import lib.core.Gen.VarGenMixin as VarGenMixin
import lib.core.Gen.TranslatorGenMixin as TranslatorGenMixin
import lib.core.Handles.HandlesVar as HandlesVar
import lib.core.Handles.HandlesExprCall as HandlesExprCall
# ============================================================
# Generator - 生成器(P1 骨架 + P2 多继承 Mixin 组合)
#
# 对象化重构宿主(wiki/14-oo-refactor.md):
# @t.NoVTable + 多继承(BaseGen 状态 + TypeGen 类型映射),方法沿
# MRO 直接调用(Viper 多继承已支持,M1-M4)。后续 P3-P6 追加
# StructGen/ExprGen/CallGen/FuncGen/ClassGen/StmtGen/ImportsGen Mixin。
#
# 注意:Viper NoVTable 继承只合并字段、不转发父类方法符号(子类调
# 父类方法 mangle 成子类名 → undefined reference),因此字段初始化
# 全部内联,不调用父类 Init* 方法。
# ============================================================
@t.NoVTable
class Generator(BaseGenMixin.BaseGenMixin, TypeGenMixin.TypeGenMixin,
StructGenMixin.StructGenMixin, ExprGenMixin.ExprGenMixin,
CallGenMixin.CallGenMixin, FuncGenMixin.FuncGenMixin,
ClassGenMixin.ClassGenMixin, ImportsGenMixin.ImportsGenMixin,
StmtGenMixin.StmtGenMixin, AssignGenMixin.AssignGenMixin,
VarGenMixin.VarGenMixin, TranslatorGenMixin.TranslatorGenMixin):
"""生成器:多继承 Mixin 组合(对象化迁移宿主)"""
def __init__(self, pool: memhub.MemBuddy | t.CPtr):
if self is None:
return
# ---- BaseGenMixin 字段 ----
self.Pool = pool
self.Module = None
self.Builder = None
self.SymTab = None
self.Funcs = None
self.FuncCount = 0
self.ImportedModules = None
self.FromImports = None
self.ModuleSha1 = None
self.CurFunc = None
self.CurBuilder = None
self.CurClassName = None
# ---- TypeGenMixin 字段 ----
self.TraceFrames = None
self.TraceCount = 0
self.TempDir = None
self.GenericTpNames = None
self.GenericTypeArgs = None
self.GenericCtxStack = None
self.GenericCtxDepth = 0
self.CDefineNames = None
self.CDefineValues = None
self.CDefineDoubleFlags = None
self.CDefineStrFlags = None
self.CDefineIsDouble = 0
self.CDefineIsStr = 0
self.CDefineFound = 0
self.CurrentModuleSha1 = None
self.CTypedefNames = None
self.CTypedefTypeStrs = None
self.CTypedefFound = 0
self.CTypedefResolveDepth = 0
self.CTypedefVisiting = None
self.CurrentFile = None
self.ConstEvalOk = 0
# ---- StructGenMixin 字段 ----
self.StructTable = None
self.StructCount = 0
self.StdlibAlloc = 0
self.VisibleStructs = None
self.StrictVisibility = 0
# ---- CallGenMixin 字段 ----
self.CexportSha1Arr = None
self.CexportFuncArr = None
self.CexportCount = 0
self.LLVMIRRegSeq = 0
self.TempDir = None
self.TempDirLen = 0
self.ClosureNames = None
self.ClosureCount = 0
self.FuncptrNames = None
self.FuncptrRets = None
self.FuncptrCount = 0
self.GlobalFuncs = None
self.GlobalFuncCount = 0
self.GlobalDefaults = None
self.GlobalDefaultsCount = 0
# ---- ClassGenMixin 字段 ----
self.MethodMetaKeys = None
self.MethodMetaFlags = None
self.MethodMetaCount = 0
self.RenumClassNames = None
self.RenumClassVariantCounts = None
self.RenumVariantNames = None
self.RenumVariantTags = None
self.RenumClassCount = 0
self.RenumAliasClass = None
self.RenumAliasField = None
self.RenumAliasIndex = None
self.RenumAliasCount = 0
self.SpecKeys = None
self.SpecNames = None
self.SpecCount = 0
self.GenericClassNames = None
self.GenericClassNodes = None
self.GenericClassSha1s = None
self.GenericClassFiles = None
self.GenericClassTpParams = None
self.VtFullNames = None
self.VtMethodNames = None
self.VtParentSha1s = None
self.VtMethodCount = 0
# ---- ImportsGenMixin 字段 ----
self.ReexportSha1s = None
self.ReexportFuncs = None
self.ReexportSrcs = None
self.ReexportCount = 0
# ---- StmtGenMixin 字段 ----
self.CifEvalOk = 0
self.EnumTable = None
self.EnumCount = 0
# ---- AssignGenMixin 字段 ----
self.RecoverFound = 0
# ---- ExprGenMixin docstring 字段(__doc__ 魔法属性支持)----
self.DocKeys = None
self.DocValues = None
self.DocCount = 0
# ============================================================
# 全局生成器单例(对象化 P2 兼容层)
#
# 从 Gen/__init__.vp(包)迁入本模块(普通模块):TPC 的模块名注册
# 只注册"普通模块短名"(如 Generator)与"完整包名"(如 lib.core.Gen),
# 不注册包短名(如 Gen)。HandlesType 等旧模块的函数体 import 若引
# 用包短名会报 Unknown module,故单例存取放在非包模块,别名用短名。
# ============================================================
_g_gen: Generator | t.CPtr = None
def set_global_gen(gen: Generator | t.CPtr) -> None:
"""注册全局生成器单例(App/main.vp 创建 gen 后调用)"""
global _g_gen
_g_gen = gen
def get_global_gen() -> Generator | t.CPtr:
"""获取全局生成器单例(未注册返回 None)"""
return _g_gen