Files
hakorune/tools/hakorune_emit_mir.sh
nyash-codex 6055d53eff feat(phase21.5/22.1): MirBuilder JsonFrag refactor + FileBox ring-1 + registry tests
Phase 21.5 (AOT/LLVM Optimization Prep)
- FileBox ring-1 (core-ro) provider: priority=-100, always available, no panic path
  - src/runner/modes/common_util/provider_registry.rs: CoreRoFileProviderFactory
  - Auto-registers at startup, eliminates fallback panic structurally
- StringBox fast path prototypes (length/size optimization)
- Performance benchmarks (C/Python/Hako comparison baseline)

Phase 22.1 (JsonFrag Unification)
- JsonFrag.last_index_of_from() for backward search (VM fallback)
- Replace hand-written lastIndexOf in lower_loop_sum_bc_box.hako
- SentinelExtractorBox for Break/Continue pattern extraction

MirBuilder Refactor (Box → JsonFrag Migration)
- 20+ lower_*_box.hako: Box-heavy → JsonFrag text assembly
- MirBuilderMinBox: lightweight using set for dev env
- Registry-only fast path with [registry:*] tag observation
- pattern_util_box.hako: enhanced pattern matching

Dev Environment & Testing
- Dev toggles: SMOKES_DEV_PREINCLUDE=1 (point-enable), HAKO_MIR_BUILDER_SKIP_LOOPS=1
- phase2160: registry opt-in tests (array/map get/set/push/len) - content verification
- phase2034: rc-dependent → token grep (grep -F based validation)
- run_quick.sh: fast smoke testing harness
- ENV documentation: docs/ENV_VARS.md

Test Results
 quick phase2034: ALL GREEN (MirBuilder internal patterns)
 registry phase2160: ALL GREEN (array/map get/set/push/len)
 rc-dependent tests → content token verification complete
 PREINCLUDE policy: default OFF, point-enable only where needed

Technical Notes
- No INCLUDE by default (maintain minimalism)
- FAIL_FAST=0 in Bring-up contexts only (explicit dev toggles)
- Tag-based route observation ([mirbuilder/min:*], [registry:*])
- MIR structure validation (not just rc parity)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-10 19:42:42 +09:00

123 lines
4.2 KiB
Bash
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.

#!/usr/bin/env bash
# hakorune_emit_mir.sh — Emit MIR(JSON) using Hakorune StageB + MirBuilder (Hakofirst)
#
# Usage: tools/hakorune_emit_mir.sh <input.hako> <out.json>
# Notes:
# - Runs the StageB compiler (Hako) to emit Program(JSON v0), then feeds it to MirBuilderBox.emit_from_program_json_v0.
# - Defaults to the Hakorune VM path; no inline Ny compiler; Stage3 enabled.
# - Keeps defaults conservative: no global flips; this is a helper for dev/CI scripts.
set -euo pipefail
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <input.hako> <out.json>" >&2
exit 2
fi
IN="$1"
OUT="$2"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if ROOT_GIT=$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null); then
ROOT="$ROOT_GIT"
else
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
fi
# Resolve nyash/hakorune binary via test_runner helper (ensures consistent env)
if [ ! -f "$IN" ]; then
echo "[FAIL] input not found: $IN" >&2
exit 1
fi
# Resolve nyash/hakorune binary (simple detection; test_runner will override later if sourced)
if [ -z "${NYASH_BIN:-}" ]; then
if [ -x "$ROOT/target/release/hakorune" ]; then
export NYASH_BIN="$ROOT/target/release/hakorune"
else
export NYASH_BIN="$ROOT/target/release/nyash"
fi
fi
CODE="$(cat "$IN")"
# 1) StageB: Hako parser emits Program(JSON v0) to stdout
set +e
PROG_JSON_OUT=$(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=${NYASH_ENABLE_USING:-1} HAKO_ENABLE_USING=${HAKO_ENABLE_USING:-1} \
"$NYASH_BIN" --backend vm "$ROOT/lang/src/compiler/entry/compiler_stageb.hako" -- --source "$CODE" 2>/dev/null | awk '/^{/,/^}$/')
rc=$?
set -e
if [ $rc -ne 0 ] || [ -z "$PROG_JSON_OUT" ]; then
echo "[FAIL] Stage-B parse failed (rc=$rc)" >&2
exit 1
fi
# Quick validation for Program(JSON v0)
if ! printf '%s' "$PROG_JSON_OUT" | grep -q '"kind"\s*:\s*"Program"'; then
echo "[FAIL] StageB output is not Program(JSON)" >&2
printf '%s\n' "$PROG_JSON_OUT" | sed -n '1,80p' >&2 || true
exit 1
fi
# 2) Convert Program(JSON v0) → MIR(JSON)
# Prefer selfhost builder first when explicitly requested; otherwise use delegate (GateC) for stability.
try_selfhost_builder() {
local prog_json="$1" out_path="$2"
local tmp_hako; tmp_hako=$(mktemp --suffix .hako)
cat >"$tmp_hako" <<'HCODE'
using "hako.mir.builder" as MirBuilderBox
static box Main { method main(args) {
local prog_json = env.get("HAKO_BUILDER_PROGRAM_JSON")
if prog_json == null { print("[builder/selfhost-first:fail:nojson]"); return 1 }
local mir_out = MirBuilderBox.emit_from_program_json_v0(prog_json, null)
if mir_out == null { print("[builder/selfhost-first:fail:emit]"); return 1 }
print("[builder/selfhost-first:ok]")
print("[MIR_OUT_BEGIN]")
print("" + mir_out)
print("[MIR_OUT_END]")
return 0
} }
HCODE
local tmp_stdout; tmp_stdout=$(mktemp)
trap 'rm -f "$tmp_hako" "$tmp_stdout" || true' RETURN
set +e
HAKO_MIR_BUILDER_INTERNAL=1 HAKO_MIR_BUILDER_REGISTRY=1 \
NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 \
NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 NYASH_PARSER_ALLOW_SEMICOLON=1 \
HAKO_BUILDER_PROGRAM_JSON="$prog_json" \
"$NYASH_BIN" --backend vm "$tmp_hako" 2>/dev/null | tee "$tmp_stdout" >/dev/null
local rc=$?
set -e
if [ $rc -ne 0 ]; then return 1; fi
if ! grep -q "\[builder/selfhost-first:ok\]" "$tmp_stdout"; then return 1; fi
local mir
mir=$(awk '/\[MIR_OUT_BEGIN\]/{flag=1;next}/\[MIR_OUT_END\]/{flag=0}flag' "$tmp_stdout")
if [ -z "$mir" ]; then return 1; fi
printf '%s' "$mir" > "$out_path"
echo "[OK] MIR JSON written (selfhost-first): $out_path"
return 0
}
if [ "${HAKO_SELFHOST_BUILDER_FIRST:-0}" = "1" ]; then
if try_selfhost_builder "$PROG_JSON_OUT" "$OUT"; then
exit 0
fi
if [ "${HAKO_SELFHOST_NO_DELEGATE:-0}" = "1" ]; then
echo "[FAIL] selfhost-first failed and delegate disabled" >&2
exit 1
fi
fi
tmp_prog="/tmp/hako_emit_prog_$$.json"
trap 'rm -f "$tmp_prog" || true' EXIT
printf '%s' "$PROG_JSON_OUT" > "$tmp_prog"
if "$NYASH_BIN" --program-json-to-mir "$OUT" --json-file "$tmp_prog" >/dev/null 2>&1; then
echo "[OK] MIR JSON written (delegate): $OUT"
exit 0
fi
echo "[FAIL] Program→MIR delegate failed" >&2
exit 1