Phase 21.6 solidification: chain green (return/binop/loop/call); add Phase 21.7 normalization plan (methodize static boxes). Update CURRENT_TASK.md and docs.

This commit is contained in:
nyash-codex
2025-11-11 22:35:45 +09:00
parent 52b62c5772
commit 9e2fa1e36e
19 changed files with 1309 additions and 35 deletions

View File

@ -196,8 +196,15 @@ def lower_blocks(builder, func: ir.Function, block_by_id: Dict[int, Dict[str, An
try:
dst = inst.get("dst")
if isinstance(dst, int):
if dst in builder.vmap:
# Prefer current vmap context (_current_vmap) updates; fallback to global vmap
_gval = None
try:
_gval = vmap_cur.get(dst)
except Exception:
_gval = None
if _gval is None and dst in builder.vmap:
_gval = builder.vmap[dst]
if _gval is not None:
try:
if hasattr(_gval, 'add_incoming'):
bb_of = getattr(getattr(_gval, 'basic_block', None), 'name', None)

View File

@ -30,6 +30,13 @@ def lower_function(builder, func_data: Dict[str, Any]):
# Default: i64(i64, ...) signature; derive arity from '/N' suffix when params missing
m = re.search(r"/(\d+)$", name)
arity = int(m.group(1)) if m else len(params)
# Dev fallback: when params are missing for global (Box.method) functions,
# use observed call-site arity if available (scanned in builder.build_from_mir)
if arity == 0 and '.' in name:
try:
arity = int(builder.call_arities.get(name, 0))
except Exception:
pass
param_types = [builder.i64] * arity
func_ty = ir.FunctionType(builder.i64, param_types)
@ -67,11 +74,38 @@ def lower_function(builder, func_data: Dict[str, Any]):
if func is None:
func = ir.Function(builder.module, func_ty, name=name)
# Map parameters to vmap (value_id: 0..arity-1)
# Map parameters to vmap. Prefer mapping by referenced value-ids that have no
# local definition (common in v0 JSON where params appear as lhs/rhs ids).
try:
arity = len(func.args)
# Collect defined and used ids
defs = set()
uses = set()
for bb in (blocks or []):
for ins in (bb.get('instructions') or []):
try:
dstv = ins.get('dst')
if isinstance(dstv, int):
defs.add(int(dstv))
except Exception:
pass
for k in ('lhs','rhs','value','cond','box_val'):
try:
v = ins.get(k)
if isinstance(v, int):
uses.add(int(v))
except Exception:
pass
cand = [vid for vid in uses if vid not in defs]
cand.sort()
mapped = 0
for i in range(min(arity, len(cand))):
builder.vmap[int(cand[i])] = func.args[i]
mapped += 1
# Fallback: also map positional 0..arity-1 to args if not already mapped
for i in range(arity):
builder.vmap[i] = func.args[i]
if i not in builder.vmap:
builder.vmap[i] = func.args[i]
except Exception:
pass

View File

@ -133,6 +133,46 @@ class NyashLLVMBuilder:
# Parse MIR
reader = MIRReader(mir_json)
functions = reader.get_functions()
# Pre-scan call sites to estimate arity for global functions when params are missing
def _scan_call_arities(funcs: List[Dict[str, Any]]):
ar: Dict[str, int] = {}
for f in funcs or []:
# Build map: const dst -> string name (per-function scope)
const_names: Dict[int, str] = {}
for bb in (f.get('blocks') or []):
for ins in (bb.get('instructions') or []):
try:
op = ins.get('op')
if op == 'const':
dst = ins.get('dst')
val = ins.get('value') or {}
name = None
if isinstance(val, dict):
v = val.get('value')
t = val.get('type')
if isinstance(v, str) and (
t == 'string' or (isinstance(t, dict) and t.get('box_type') == 'StringBox')
):
name = v
if isinstance(dst, int) and isinstance(name, str):
const_names[int(dst)] = name
elif op == 'call':
func_id = ins.get('func')
if isinstance(func_id, int) and func_id in const_names:
nm = const_names[func_id]
argc = len(ins.get('args') or [])
prev = ar.get(nm, 0)
if argc > prev:
ar[nm] = argc
except Exception:
continue
return ar
try:
self.call_arities = _scan_call_arities(functions)
except Exception:
self.call_arities = {}
if not functions:
# No functions - create dummy ny_main
@ -149,6 +189,12 @@ class NyashLLVMBuilder:
params_list = func_data.get("params", []) or []
if "." in name:
arity = len(params_list)
# Dev fallback: when params missing for Box.method, use call-site arity
if arity == 0:
try:
arity = int(self.call_arities.get(name, 0))
except Exception:
pass
else:
arity = int(m.group(1)) if m else len(params_list)
if name == "ny_main":