fix(mir/builder): use function-local ValueId throughout MIR builder

Phase 25.1b: Complete SSA fix - eliminate all global ValueId usage in function contexts.

Root cause: ~75 locations throughout MIR builder were using global value
generator (self.value_gen.next()) instead of function-local allocator
(f.next_value_id()), causing SSA verification failures and runtime
"use of undefined value" errors.

Solution:
- Added next_value_id() helper that automatically chooses correct allocator
- Fixed 19 files with ~75 occurrences of ValueId allocation
- All function-context allocations now use function-local IDs

Files modified:
- src/mir/builder/utils.rs: Added next_value_id() helper, fixed 8 locations
- src/mir/builder/builder_calls.rs: 17 fixes
- src/mir/builder/ops.rs: 8 fixes
- src/mir/builder/stmts.rs: 7 fixes
- src/mir/builder/emission/constant.rs: 6 fixes
- src/mir/builder/rewrite/*.rs: 10 fixes
- + 13 other files

Verification:
- cargo build --release: SUCCESS
- Simple tests with NYASH_VM_VERIFY_MIR=1: Zero undefined errors
- Multi-parameter static methods: All working

Known remaining: ValueId(22) in Stage-B (separate issue to investigate)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-17 00:48:18 +09:00
parent 35842503e6
commit eadde8d1dd
59 changed files with 1603 additions and 370 deletions

View File

@ -0,0 +1,102 @@
#!/usr/bin/env bash
# selfhost_mir_loopform_multi_carrier_vm.sh
# - Canary for Phase 25.1b multi-carrier LoopForm (fibonacci-style).
# - Ensures LowerLoopMultiCarrierBox + LoopFormBox.build2(mode="multi_count")
# can emit MIR(JSON) for a simple fibonacci-like loop via selfhost builder.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || (cd "$SCRIPT_DIR/../../../../../../.." && pwd))"
TEST_HAKO="$(mktemp --suffix .hako)"
OUT_MIR="$(mktemp --suffix .json)"
LOG_OUT="$(mktemp --suffix .log)"
trap 'rm -f "$TEST_HAKO" "$OUT_MIR" "$LOG_OUT" || true' EXIT
# Fibonacci-style loop: i=0; a=0; b=1; loop(i<n){ t=a+b; a=b; b=t; i=i+1 }; return b
cat >"$TEST_HAKO" <<'HAKO'
static box TestBox {
method fib(n) {
local i = 0
local a = 0
local b = 1
loop(i < n) {
local t = a + b
a = b
b = t
i = i + 1
}
return b
}
}
static box Main {
method main(args) {
local t = new TestBox()
return t.fib(6)
}
}
HAKO
set +e
HAKO_SELFHOST_BUILDER_FIRST=1 \
HAKO_MIR_BUILDER_FUNCS=1 \
HAKO_SELFHOST_TRACE=1 \
HAKO_MIR_BUILDER_TRACE=1 \
HAKO_SILENT_TAGS=0 \
NYASH_JSON_ONLY=1 \
bash "$ROOT_DIR/tools/hakorune_emit_mir.sh" "$TEST_HAKO" "$OUT_MIR" >"$LOG_OUT" 2>&1
rc=$?
set -e
# If selfhost builder failed due to missing using modules and provider delegate
# wrote MIR instead, treat as SKIP環境が揃ってから本格確認する
if [ $rc -ne 0 ] || [ ! -s "$OUT_MIR" ]; then
if grep -q "[builder/selfhost-first:diagnose] Missing using modules detected" "$LOG_OUT" 2>/dev/null; then
echo "[SKIP] selfhost_mir_loopform_multi_carrier_vm (selfhost builder missing using modules; provider delegate used)" >&2
exit 0
fi
echo "[FAIL] selfhost_mir_loopform_multi_carrier_vm (MIR generation failed rc=$rc)" >&2
echo "=== LOG OUTPUT ===" >&2
sed -n '1,120p' "$LOG_OUT" >&2 || true
exit 1
fi
# If builder は失敗し、provider delegate が MIR を書いただけなら、まだ本命経路が
# 有効になっていないので SKIP 扱いにする。
if grep -q "[builder/selfhost-first:diagnose] Missing using modules detected" "$LOG_OUT" 2>/dev/null \
|| grep -q "delegate:provider" "$LOG_OUT" 2>/dev/null; then
echo "[SKIP] selfhost_mir_loopform_multi_carrier_vm (selfhost builder not active yet; provider delegate in use)" >&2
exit 0
fi
# Check that multi-carrier lower was actually used
# If StageB がまだ loop を含まない Program(JSON) しか出していない場合は、
# multi-carrier lower が動けないので SKIP 扱いにする(将来 StageB 側が
# defs/loop を含むようになったときに PASS へ昇格させる)。
if ! grep -q "\[mirbuilder/internal/loop:multi_carrier:detected" "$LOG_OUT"; then
if grep -q "tokens=Loop:0,Compare:0" "$LOG_OUT" 2>/dev/null; then
echo "[SKIP] selfhost_mir_loopform_multi_carrier_vm (Stage-B Program JSON has no Loop yet)" >&2
exit 0
fi
echo "[FAIL] selfhost_mir_loopform_multi_carrier_vm (no multi_carrier detection tag)" >&2
sed -n '1,120p' "$LOG_OUT" >&2 || true
exit 1
fi
if ! grep -q "\[funcs/basic:loop.multi_carrier\]" "$LOG_OUT"; then
echo "[FAIL] selfhost_mir_loopform_multi_carrier_vm (no [funcs/basic:loop.multi_carrier] tag)" >&2
sed -n '1,120p' "$LOG_OUT" >&2 || true
exit 1
fi
# Ensure TestBox.fib/1 function exists in MIR
if ! grep -q '"name":"TestBox.fib/1"' "$OUT_MIR"; then
echo "[FAIL] selfhost_mir_loopform_multi_carrier_vm (TestBox.fib/1 not present in MIR)" >&2
cat "$OUT_MIR" >&2
exit 1
fi
echo "[PASS] selfhost_mir_loopform_multi_carrier_vm (multi-carrier LoopForm used for TestBox.fib/1)"
exit 0

View File

@ -6,7 +6,7 @@
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || cd "$SCRIPT_DIR/../../../../../../.." && pwd)"
ROOT_DIR="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || (cd "$SCRIPT_DIR/../../../../../../.." && pwd))"
if [ ! -f "$ROOT_DIR/lang/src/runner/launcher.hako" ]; then
echo "[SKIP] stage1_launcher_program_to_mir_canary_vm (launcher.hako missing)"
@ -36,4 +36,3 @@ fi
echo "[PASS] stage1_launcher_program_to_mir_canary_vm"
exit 0

View File

@ -7,7 +7,7 @@
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || cd "$SCRIPT_DIR/../../../../../../.." && pwd)"
ROOT_DIR="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || (cd "$SCRIPT_DIR/../../../../../../.." && pwd))"
if [ ! -f "$ROOT_DIR/lang/src/runner/launcher.hako" ]; then
echo "[SKIP] stage1_launcher_program_to_mir_selfhost_vm (launcher.hako missing)"
@ -52,4 +52,3 @@ fi
echo "[PASS] stage1_launcher_program_to_mir_selfhost_vm"
exit 0

View File

@ -0,0 +1,189 @@
#!/usr/bin/env bash
# stageb_fib_program_defs_canary_vm.sh
# - Canary for StageB defs + Loop 出力確認fib 風 multi-carrier 用の下準備)。
# - 目的:
# - compiler_stageb.hako が FuncScannerBox を通じて TestBox.fib を defs に載せているか。
# - defs 内の body に Loop ノードが含まれているか(後続の LoopForm lower の前提確認)。
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || (cd "$SCRIPT_DIR/../../../../../../.." && pwd))"
source "$ROOT_DIR/tools/smokes/v2/lib/test_runner.sh" || true
require_env || { echo "[SKIP] env not ready"; exit 0; }
if ! command -v jq >/dev/null 2>&1; then
echo "[SKIP] stageb_fib_program_defs_canary_vm.sh (jq not available)" >&2
exit 0
fi
TMP_HAKO="$(mktemp --suffix .hako)"
OUT_JSON="$(mktemp --suffix .json)"
trap 'rm -f "$TMP_HAKO" "$OUT_JSON" || true' EXIT
cat >"$TMP_HAKO" <<'HAKO'
static box TestBox {
method fib(n) {
local i = 0
local a = 0
local b = 1
loop(i < n) {
local t = a + b
a = b
b = t
i = i + 1
}
return b
}
}
static box Main {
method main(args) {
local t = new TestBox()
return t.fib(6)
}
}
HAKO
# StageB: emit Program(JSON v0) with defs
set +e
OUT_RAW=$(
cd "$ROOT_DIR" && \
NYASH_JSON_ONLY=1 NYASH_DISABLE_NY_COMPILER=1 HAKO_DISABLE_NY_COMPILER=1 \
NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 NYASH_PARSER_ALLOW_SEMICOLON=1 \
NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 HAKO_STAGEB_FUNC_SCAN=1 \
HAKO_STAGEB_APPLY_USINGS=0 \
"$NYASH_BIN" --backend vm "$ROOT_DIR/lang/src/compiler/entry/compiler_stageb.hako" -- --source "$(cat "$TMP_HAKO")" 2>&1
)
rc=$?
set -e
if [ $rc -ne 0 ]; then
echo "[FAIL] stageb_fib_program_defs_canary_vm (StageB rc=$rc)" >&2
printf '%s\n' "$OUT_RAW" | head -n 40 >&2 || true
exit 1
fi
# 抽出: Program(JSON) 部分だけを抽出emit_mir.sh と同じ Python ロジックを簡略化)
extract_prog() {
python3 - <<'PYEOF'
import sys
text = sys.stdin.read()
start = text.find('"kind":"Program"')
if start < 0:
sys.exit(1)
pos = start
depth = 0
while pos >= 0:
if text[pos] == '{':
depth += 1
if depth == 1:
break
elif text[pos] == '}':
depth -= 1
pos -= 1
if pos < 0:
sys.exit(1)
obj_start = pos
depth = 0
in_str = False
esc = False
i = obj_start
while i < len(text):
ch = text[i]
if esc:
esc = False
elif in_str:
if ch == '\\\\':
esc = True
elif ch == '"':
in_str = False
else:
if ch == '"':
in_str = True
elif ch == '{':
depth += 1
elif ch == '}':
depth -= 1
if depth == 0:
print(text[obj_start:i+1])
sys.exit(0)
i += 1
sys.exit(1)
PYEOF
}
RAW_DUMP=$(mktemp --suffix .stageb.raw)
printf '%s\n' "$OUT_RAW" > "$RAW_DUMP"
if ! python3 - "$RAW_DUMP" <<'PYEOF' >"$OUT_JSON" 2>/dev/null; then
import sys
from pathlib import Path
dump_path = Path(sys.argv[1])
text = dump_path.read_text()
start = text.find('"kind":"Program"')
if start < 0:
sys.exit(1)
pos = start
while pos >= 0 and text[pos] != '{':
pos -= 1
if pos < 0:
sys.exit(1)
depth = 0
in_str = False
esc = False
i = pos
while i < len(text):
ch = text[i]
if esc:
esc = False
elif in_str:
if ch == '\\\\':
esc = True
elif ch == '"':
in_str = False
else:
if ch == '"':
in_str = True
elif ch == '{':
depth += 1
elif ch == '}':
depth -= 1
if depth == 0:
print(text[pos:i+1])
sys.exit(0)
i += 1
sys.exit(1)
PYEOF
echo "[FAIL] stageb_fib_program_defs_canary_vm (failed to extract Program JSON)" >&2
printf '%s\n' "$OUT_RAW" | head -n 40 >&2 || true
rm -f "$RAW_DUMP" || true
exit 1
fi
rm -f "$RAW_DUMP" || true
# 1) Program.kind == "Program"
if ! jq -e '.kind == "Program"' "$OUT_JSON" >/dev/null 2>&1; then
echo "[FAIL] stageb_fib_program_defs_canary_vm (kind != Program)" >&2
cat "$OUT_JSON" >&2
exit 1
fi
# 2) defs に TestBox.fib が含まれているか
has_fib=$(jq '.defs // [] | any(.box=="TestBox" and .name=="fib")' "$OUT_JSON")
if [ "$has_fib" != "true" ]; then
echo "[FAIL] stageb_fib_program_defs_canary_vm (defs に TestBox.fib が存在しない)" >&2
jq '.defs // []' "$OUT_JSON" >&2 || true
exit 1
fi
# 3) fib の body 内に Loop ノードがあるか
has_loop=$(jq '.defs // [] | map(select(.box=="TestBox" and .name=="fib")) | any(.body | .body? | any(.type=="Loop"))' "$OUT_JSON")
if [ "$has_loop" != "true" ]; then
echo "[FAIL] stageb_fib_program_defs_canary_vm (TestBox.fib body に Loop が無い)" >&2
jq '.defs // [] | map(select(.box=="TestBox" and .name=="fib"))' "$OUT_JSON" >&2 || true
exit 1
fi
echo "[PASS] stageb_fib_program_defs_canary_vm"
exit 0