fix(vtable): 虚方法表按类实际规模动态分配,去掉 32 上限
MemBuddy 继承 MemManager 后虚方法约 60+ 个,VTBL_METHOD_MAX=32 会触发 fail-fast(此前静默只收前 32 个,之后的调用点找不到槽位、退化成静态直调)。现在收集前先算容量上界:各父类已收集的 VTableMethodCount 之和 + 本类 FunctionDef 数量 + 1,三个 PermPool 数组按此一次性分配;两处越界检查保留为 InternalCompilerError(理论不可达)。删除 VTBL_METHOD_MAX。注:本次提交同时带上了该文件既有的 t.CPtr → rawptr 迁移与 _generate_default_init 删除等未提交改动。
This commit is contained in:
1 parent
d46fdf152a
commit
bf0bbcc89b
1 file changed
+37
-20
@@ -1,6 +1,5 @@
|
|||||||
import lib.core.Gen.GenTypes as GenTypes
|
import lib.core.Gen.GenTypes as GenTypes
|
||||||
import lib.core.Gen.Type.TypeSpec as TSpec
|
import lib.core.Gen.Type.TypeSpec as TSpec
|
||||||
import t, c
|
|
||||||
from stdint import *
|
from stdint import *
|
||||||
import ast
|
import ast
|
||||||
import mlirlite
|
import mlirlite
|
||||||
@@ -19,8 +18,8 @@ import lib.core.Gen.DebugInfoGen as DebugInfoGen
|
|||||||
|
|
||||||
# Method metadata flag: TLS (@t.TLS decorator; local copy avoids a cross-module CDefine lookup).
|
# Method metadata flag: TLS (@t.TLS decorator; local copy avoids a cross-module CDefine lookup).
|
||||||
define META_TLS = 32
|
define META_TLS = 32
|
||||||
# Max number of vtable methods.
|
# Vtable method tables are sized per class at collection time (parents' count + own methods);
|
||||||
define VTBL_METHOD_MAX = 32
|
# there is deliberately no fixed VTBL_METHOD_MAX.
|
||||||
define SCOPE_FUNCTION = 1
|
define SCOPE_FUNCTION = 1
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
@@ -67,15 +66,35 @@ novirtual class ClassGenVTable(ClassGenBase.ClassGenBase):
|
|||||||
if children is None: return 0
|
if children is None: return 0
|
||||||
cn: sizet = len(children)
|
cn: sizet = len(children)
|
||||||
|
|
||||||
|
# Capacity comes from the class itself: the parents' already-collected vtable methods
|
||||||
|
# (an upper bound — dedupe/override only shrink it) plus the number of own method
|
||||||
|
# definitions. A fixed 32 silently dropped every later method, after which a call missed
|
||||||
|
# the slot and fell back to a static call of the base implementation.
|
||||||
|
parent_count: int = self.Gen.StructGen.get_parent_count(class_name)
|
||||||
|
vt_cap: int = 0
|
||||||
|
if parent_count > 0:
|
||||||
|
for pi0 in range(int(parent_count)):
|
||||||
|
pname0: str = self.Gen.StructGen.get_parent_name_at(class_name, pi0)
|
||||||
|
if pname0 is None: continue
|
||||||
|
pe0: GenTypes.StructEntry* = self.Gen.StructGen.find_struct_by_module(pname0, trans.ModuleSha1)
|
||||||
|
if pe0 is None: pe0 = self.Gen.StructGen.find_struct(pname0)
|
||||||
|
if pe0 is not None and pe0.VTableMethodCount > 0: vt_cap = vt_cap + pe0.VTableMethodCount
|
||||||
|
for ci0 in range(int(cn)):
|
||||||
|
stmt0: ast.AST* = children.get(ci0)
|
||||||
|
if stmt0 is not None and stmt0.kind() == ast.ASTKind.FunctionDef: vt_cap = vt_cap + 1
|
||||||
|
vt_cap = vt_cap + 1 # one spare slot
|
||||||
|
if vt_cap < 1: vt_cap = 1
|
||||||
|
vt_bytes: sizet = sizet(8 * vt_cap)
|
||||||
|
|
||||||
# Allocate with PermPool: VTableMethods must survive per-file pool resets.
|
# Allocate with PermPool: VTableMethods must survive per-file pool resets.
|
||||||
full_names_buf: sizet* = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
full_names_buf: sizet* = CompilerFactory.factory.PermPool.alloc(vt_bytes)
|
||||||
method_names_buf: sizet* = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
method_names_buf: sizet* = CompilerFactory.factory.PermPool.alloc(vt_bytes)
|
||||||
# Record each method's parent SHA1 (non-zero = inherited, mangle with parent module SHA1).
|
# Record each method's parent SHA1 (non-zero = inherited, mangle with parent module SHA1).
|
||||||
method_parent_sha1s: sizet* = CompilerFactory.factory.PermPool.alloc(8 * VTBL_METHOD_MAX)
|
method_parent_sha1s: sizet* = CompilerFactory.factory.PermPool.alloc(vt_bytes)
|
||||||
if full_names_buf is None or method_names_buf is None or method_parent_sha1s is None: return 0
|
if full_names_buf is None or method_names_buf is None or method_parent_sha1s is None: return 0
|
||||||
string.memset(full_names_buf, 0, 8 * VTBL_METHOD_MAX)
|
string.memset(full_names_buf, 0, vt_bytes)
|
||||||
string.memset(method_names_buf, 0, 8 * VTBL_METHOD_MAX)
|
string.memset(method_names_buf, 0, vt_bytes)
|
||||||
string.memset(method_parent_sha1s, 0, 8 * VTBL_METHOD_MAX)
|
string.memset(method_parent_sha1s, 0, vt_bytes)
|
||||||
|
|
||||||
method_count: int = 0
|
method_count: int = 0
|
||||||
|
|
||||||
@@ -83,7 +102,6 @@ novirtual class ClassGenVTable(ClassGenBase.ClassGenBase):
|
|||||||
# subclass, others the parent; SHA1-aware lookup avoids cross-module same-name mismatches.
|
# subclass, others the parent; SHA1-aware lookup avoids cross-module same-name mismatches.
|
||||||
vt_self_entry: GenTypes.StructEntry* = self.Gen.StructGen.find_struct_by_module(class_name, trans.ModuleSha1)
|
vt_self_entry: GenTypes.StructEntry* = self.Gen.StructGen.find_struct_by_module(class_name, trans.ModuleSha1)
|
||||||
# Multiple inheritance: walk parents in declaration order, dedupe by short name (first wins).
|
# Multiple inheritance: walk parents in declaration order, dedupe by short name (first wins).
|
||||||
parent_count: int = self.Gen.StructGen.get_parent_count(class_name)
|
|
||||||
if parent_count > 0:
|
if parent_count > 0:
|
||||||
for pi in range(int(parent_count)):
|
for pi in range(int(parent_count)):
|
||||||
pname: str = self.Gen.StructGen.get_parent_name_at(class_name, pi)
|
pname: str = self.Gen.StructGen.get_parent_name_at(class_name, pi)
|
||||||
@@ -97,11 +115,10 @@ novirtual class ClassGenVTable(ClassGenBase.ClassGenBase):
|
|||||||
if vt_parent_entry is not None: parent_vt_count = vt_parent_entry.VTableMethodCount
|
if vt_parent_entry is not None: parent_vt_count = vt_parent_entry.VTableMethodCount
|
||||||
if parent_vt_count > 0:
|
if parent_vt_count > 0:
|
||||||
for pvi in range(int(parent_vt_count)):
|
for pvi in range(int(parent_vt_count)):
|
||||||
if method_count >= VTBL_METHOD_MAX:
|
if method_count >= vt_cap:
|
||||||
# fail-fast: the VTableMethods arrays are fixed at VTBL_METHOD_MAX;
|
# Unreachable with the upper-bound sizing above; stay loud rather than
|
||||||
# dropping the excess makes call sites miss the slot and silently fall
|
# writing past the PermPool array.
|
||||||
# back to a static call of the base implementation.
|
self.Gen.TypeGenErr.fatal_error(cd, f"internal error: vtable of '{class_name}' exceeded its computed capacity {vt_cap}", "InternalCompilerError")
|
||||||
self.Gen.TypeGenErr.fatal_error(cd, f"class '{class_name}' inherits more than {VTBL_METHOD_MAX} virtual methods (compiler limit); raise VTBL_METHOD_MAX", "OverflowError")
|
|
||||||
pmethod_name: str = None
|
pmethod_name: str = None
|
||||||
if vt_parent_entry is not None: pmethod_name = self.Gen.StructGen.get_vtable_method_name(pname, pvi)
|
if vt_parent_entry is not None: pmethod_name = self.Gen.StructGen.get_vtable_method_name(pname, pvi)
|
||||||
if pmethod_name is None: continue
|
if pmethod_name is None: continue
|
||||||
@@ -169,10 +186,10 @@ novirtual class ClassGenVTable(ClassGenBase.ClassGenBase):
|
|||||||
if stmt.kind() != ast.ASTKind.FunctionDef: continue
|
if stmt.kind() != ast.ASTKind.FunctionDef: continue
|
||||||
fd: ast.FunctionDef* = ast.FunctionDef*(stmt)
|
fd: ast.FunctionDef* = ast.FunctionDef*(stmt)
|
||||||
if fd is None or fd.name is None: continue
|
if fd is None or fd.name is None: continue
|
||||||
if method_count >= VTBL_METHOD_MAX:
|
if method_count >= vt_cap:
|
||||||
# fail-fast: see the inheritance branch above; a dropped method silently loses
|
# Unreachable with the upper-bound sizing above (own methods were counted);
|
||||||
# its vtable slot (subclass override stops taking effect).
|
# stay loud rather than writing past the PermPool array.
|
||||||
self.Gen.TypeGenErr.fatal_error(cd, f"class '{class_name}' declares more than {VTBL_METHOD_MAX} virtual methods (compiler limit); raise VTBL_METHOD_MAX", "OverflowError")
|
self.Gen.TypeGenErr.fatal_error(cd, f"internal error: vtable of '{class_name}' exceeded its computed capacity {vt_cap}", "InternalCompilerError")
|
||||||
|
|
||||||
mname: str = fd.name
|
mname: str = fd.name
|
||||||
# Skip constructors and special methods (they must not enter the vtable).
|
# Skip constructors and special methods (they must not enter the vtable).
|
||||||
@@ -598,7 +615,7 @@ novirtual class ClassGenVTable(ClassGenBase.ClassGenBase):
|
|||||||
if decl_reuse == 0:
|
if decl_reuse == 0:
|
||||||
# Default-parameter info (args[0] is self for normal methods; @staticmethod has no self).
|
# Default-parameter info (args[0] is self for normal methods; @staticmethod has no self).
|
||||||
# Assign the attribute to a typed list[...]* local before __len__() or GEP base becomes i32 0.
|
# Assign the attribute to a typed list[...]* local before __len__() or GEP base becomes i32 0.
|
||||||
md_defaults: t.CPtr = None
|
md_defaults: rawptr = None
|
||||||
md_default_count: int = 0
|
md_default_count: int = 0
|
||||||
md_param_count: int = 0
|
md_param_count: int = 0
|
||||||
md_args_node: ast.Arguments* = fd.args
|
md_args_node: ast.Arguments* = fd.args
|
||||||
|
|||||||
Reference in new issue
Block a user