2025-11-09 15:11:18 +09:00
|
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
# hakorune_emit_mir.sh — Emit MIR(JSON) using Hakorune Stage‑B + MirBuilder (Hako‑first)
|
|
|
|
|
|
#
|
|
|
|
|
|
# Usage: tools/hakorune_emit_mir.sh <input.hako> <out.json>
|
|
|
|
|
|
# Notes:
|
|
|
|
|
|
# - Runs the Stage‑B 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; Stage‑3 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) Stage‑B: Hako parser emits Program(JSON v0) to stdout
|
|
|
|
|
|
set +e
|
2025-11-09 15:50:12 +09:00
|
|
|
|
PROG_JSON_OUT=$(NYASH_JSON_ONLY=1 NYASH_DISABLE_NY_COMPILER=1 HAKO_DISABLE_NY_COMPILER=1 \
|
2025-11-13 16:40:58 +09:00
|
|
|
|
HAKO_STAGEB_FUNC_SCAN="${HAKO_STAGEB_FUNC_SCAN:-}" \
|
2025-11-09 15:11:18 +09:00
|
|
|
|
NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 NYASH_PARSER_ALLOW_SEMICOLON=1 \
|
2025-11-10 19:42:42 +09:00
|
|
|
|
NYASH_ENABLE_USING=${NYASH_ENABLE_USING:-1} HAKO_ENABLE_USING=${HAKO_ENABLE_USING:-1} \
|
2025-11-09 15:50:12 +09:00
|
|
|
|
"$NYASH_BIN" --backend vm "$ROOT/lang/src/compiler/entry/compiler_stageb.hako" -- --source "$CODE" 2>/dev/null | awk '/^{/,/^}$/')
|
2025-11-09 15:11:18 +09:00
|
|
|
|
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] Stage‑B output is not Program(JSON)" >&2
|
|
|
|
|
|
printf '%s\n' "$PROG_JSON_OUT" | sed -n '1,80p' >&2 || true
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-10 19:42:42 +09:00
|
|
|
|
# 2) Convert Program(JSON v0) → MIR(JSON)
|
|
|
|
|
|
# Prefer selfhost builder first when explicitly requested; otherwise use delegate (Gate‑C) for stability.
|
|
|
|
|
|
|
|
|
|
|
|
try_selfhost_builder() {
|
|
|
|
|
|
local prog_json="$1" out_path="$2"
|
2025-11-11 09:54:27 +09:00
|
|
|
|
|
2025-11-11 17:04:33 +09:00
|
|
|
|
# FORCE=1 direct assembly shortcut (dev toggle, bypasses using resolution)
|
|
|
|
|
|
if [ "${HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG:-0}" = "1" ]; then
|
|
|
|
|
|
# Extract limit from Program(JSON) using grep/awk
|
|
|
|
|
|
local limit=$(printf '%s' "$prog_json" | grep -o '"type":"Int","value":[0-9]*' | head -1 | grep -o '[0-9]*$' || echo "10")
|
|
|
|
|
|
|
|
|
|
|
|
# Generate minimal while-form MIR(JSON) directly (executable semantics)
|
|
|
|
|
|
# PHI incoming format: [[value_register, predecessor_block_id], ...]
|
|
|
|
|
|
cat > "$out_path" <<'MIRJSON'
|
|
|
|
|
|
{
|
|
|
|
|
|
"functions": [{
|
|
|
|
|
|
"name": "main",
|
|
|
|
|
|
"params": [],
|
|
|
|
|
|
"locals": [],
|
|
|
|
|
|
"blocks": [
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": 0,
|
|
|
|
|
|
"instructions": [
|
|
|
|
|
|
{"op": "const", "dst": 1, "value": {"type": "i64", "value": 0}},
|
|
|
|
|
|
{"op": "const", "dst": 2, "value": {"type": "i64", "value": LIMIT_PLACEHOLDER}},
|
|
|
|
|
|
{"op": "jump", "target": 1}
|
|
|
|
|
|
]
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": 1,
|
|
|
|
|
|
"instructions": [
|
|
|
|
|
|
{"op": "phi", "dst": 6, "incoming": [[2, 0], [6, 2]]},
|
|
|
|
|
|
{"op": "phi", "dst": 3, "incoming": [[1, 0], [5, 2]]},
|
|
|
|
|
|
{"op": "compare", "operation": "<", "lhs": 3, "rhs": 6, "dst": 4},
|
|
|
|
|
|
{"op": "branch", "cond": 4, "then": 2, "else": 3}
|
|
|
|
|
|
]
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": 2,
|
|
|
|
|
|
"instructions": [
|
|
|
|
|
|
{"op": "const", "dst": 10, "value": {"type": "i64", "value": 1}},
|
|
|
|
|
|
{"op": "binop", "operation": "+", "lhs": 3, "rhs": 10, "dst": 5},
|
|
|
|
|
|
{"op": "jump", "target": 1}
|
|
|
|
|
|
]
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"id": 3,
|
|
|
|
|
|
"instructions": [
|
|
|
|
|
|
{"op": "ret", "value": 3}
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
}]
|
|
|
|
|
|
}
|
|
|
|
|
|
MIRJSON
|
|
|
|
|
|
|
2025-11-13 16:40:58 +09:00
|
|
|
|
# Provider-first delegate: call env.mirbuilder.emit(prog_json) and capture v1 JSON
|
|
|
|
|
|
try_provider_emit() {
|
|
|
|
|
|
local prog_json="$1" out_path="$2"
|
|
|
|
|
|
local tmp_hako; tmp_hako=$(mktemp --suffix .hako)
|
|
|
|
|
|
cat >"$tmp_hako" <<'HCODE'
|
|
|
|
|
|
using "hako.mir.builder.internal.jsonfrag_normalizer" as NormBox
|
|
|
|
|
|
static box Main { method main(args) {
|
|
|
|
|
|
local p = env.get("HAKO_BUILDER_PROGRAM_JSON")
|
|
|
|
|
|
if p == null { print("[provider/emit:nojson]"); return 1 }
|
|
|
|
|
|
local a = new ArrayBox(); a.push(p)
|
|
|
|
|
|
local out = hostbridge.extern_invoke("env.mirbuilder", "emit", a)
|
|
|
|
|
|
// Optional normalization (dev): apply JsonFrag normalizer/purifier to provider output
|
|
|
|
|
|
{
|
|
|
|
|
|
local nv = env.get("HAKO_MIR_NORMALIZE_PROVIDER")
|
|
|
|
|
|
if nv != null && ("" + nv) == "1" {
|
|
|
|
|
|
local out_s = "" + out
|
|
|
|
|
|
out = NormBox.normalize_all(out_s)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
print("[provider/emit:ok]")
|
|
|
|
|
|
print("[MIR_OUT_BEGIN]")
|
|
|
|
|
|
print("" + 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
|
|
|
|
|
|
(cd "$ROOT" && \
|
|
|
|
|
|
NYASH_DISABLE_PLUGINS=1 NYASH_FILEBOX_MODE="core-ro" \
|
|
|
|
|
|
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>&1 | tee "$tmp_stdout" >/dev/null)
|
|
|
|
|
|
local rc=$?
|
|
|
|
|
|
set -e
|
|
|
|
|
|
if [ $rc -ne 0 ] || ! grep -q "\[provider/emit: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 (delegate:provider): $out_path"
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-11 17:04:33 +09:00
|
|
|
|
# Replace LIMIT_PLACEHOLDER with actual limit
|
|
|
|
|
|
sed -i "s/LIMIT_PLACEHOLDER/$limit/g" "$out_path"
|
|
|
|
|
|
|
|
|
|
|
|
if [ "${HAKO_SELFHOST_TRACE:-0}" = "1" ]; then
|
|
|
|
|
|
echo "[selfhost-direct:ok] Direct MIR assembly (FORCE=1), limit=$limit" >&2
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-11 09:54:27 +09:00
|
|
|
|
# Builder box selection (default: hako.mir.builder)
|
|
|
|
|
|
local builder_box="${HAKO_MIR_BUILDER_BOX:-hako.mir.builder}"
|
|
|
|
|
|
|
2025-11-10 19:42:42 +09:00
|
|
|
|
local tmp_hako; tmp_hako=$(mktemp --suffix .hako)
|
2025-11-13 16:40:58 +09:00
|
|
|
|
if [ "$builder_box" = "hako.mir.builder.min" ]; then
|
|
|
|
|
|
cat >"$tmp_hako" <<'HCODE'
|
|
|
|
|
|
using "hako.mir.builder.internal.runner_min" as BuilderRunnerMinBox
|
|
|
|
|
|
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 = BuilderRunnerMinBox.run(prog_json)
|
|
|
|
|
|
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
|
|
|
|
|
|
else
|
|
|
|
|
|
cat >"$tmp_hako" <<'HCODE'
|
2025-11-11 09:54:27 +09:00
|
|
|
|
using "__BUILDER_BOX__" as MirBuilderBox
|
2025-11-09 15:11:18 +09:00
|
|
|
|
static box Main { method main(args) {
|
|
|
|
|
|
local prog_json = env.get("HAKO_BUILDER_PROGRAM_JSON")
|
2025-11-10 19:42:42 +09:00
|
|
|
|
if prog_json == null { print("[builder/selfhost-first:fail:nojson]"); return 1 }
|
2025-11-09 15:11:18 +09:00
|
|
|
|
local mir_out = MirBuilderBox.emit_from_program_json_v0(prog_json, null)
|
2025-11-10 19:42:42 +09:00
|
|
|
|
if mir_out == null { print("[builder/selfhost-first:fail:emit]"); return 1 }
|
|
|
|
|
|
print("[builder/selfhost-first:ok]")
|
2025-11-09 15:11:18 +09:00
|
|
|
|
print("[MIR_OUT_BEGIN]")
|
|
|
|
|
|
print("" + mir_out)
|
|
|
|
|
|
print("[MIR_OUT_END]")
|
|
|
|
|
|
return 0
|
|
|
|
|
|
} }
|
|
|
|
|
|
HCODE
|
2025-11-13 16:40:58 +09:00
|
|
|
|
sed -i "s|__BUILDER_BOX__|$builder_box|g" "$tmp_hako"
|
|
|
|
|
|
fi
|
2025-11-10 19:42:42 +09:00
|
|
|
|
local tmp_stdout; tmp_stdout=$(mktemp)
|
|
|
|
|
|
trap 'rm -f "$tmp_hako" "$tmp_stdout" || true' RETURN
|
2025-11-11 05:50:23 +09:00
|
|
|
|
|
|
|
|
|
|
# Trace mode: analyze Program(JSON) before passing to builder
|
|
|
|
|
|
if [ "${HAKO_SELFHOST_TRACE:-0}" = "1" ]; then
|
|
|
|
|
|
local prog_len=${#prog_json}
|
2025-11-11 09:09:55 +09:00
|
|
|
|
local loop_count=$(printf '%s' "$prog_json" | grep -o '"type":"Loop"' 2>/dev/null | wc -l | tr -d ' \n')
|
|
|
|
|
|
local cmp_count=$(printf '%s' "$prog_json" | grep -o '"type":"Compare"' 2>/dev/null | wc -l | tr -d ' \n')
|
|
|
|
|
|
loop_count=${loop_count:-0}
|
|
|
|
|
|
cmp_count=${cmp_count:-0}
|
|
|
|
|
|
local cwd="$(pwd)"
|
|
|
|
|
|
local toml_status="absent"
|
|
|
|
|
|
if [ -f "$ROOT/nyash.toml" ]; then
|
|
|
|
|
|
toml_status="present"
|
|
|
|
|
|
fi
|
2025-11-11 09:54:27 +09:00
|
|
|
|
echo "[builder/selfhost-first:trace] builder_box=$builder_box prog_json_len=$prog_len tokens=Loop:$loop_count,Compare:$cmp_count cwd=$cwd nyash.toml=$toml_status" >&2
|
2025-11-11 05:50:23 +09:00
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-10 19:42:42 +09:00
|
|
|
|
set +e
|
2025-11-11 09:09:55 +09:00
|
|
|
|
# Run from repo root to ensure nyash.toml is available for using resolution
|
2025-11-11 09:54:27 +09:00
|
|
|
|
# Capture both stdout and stderr (2>&1) instead of discarding stderr
|
2025-11-11 09:09:55 +09:00
|
|
|
|
(cd "$ROOT" && \
|
|
|
|
|
|
HAKO_MIR_BUILDER_INTERNAL=1 HAKO_MIR_BUILDER_REGISTRY=1 \
|
2025-11-11 09:54:27 +09:00
|
|
|
|
HAKO_MIR_BUILDER_TRACE="${HAKO_SELFHOST_TRACE:-}" \
|
2025-11-11 09:09:55 +09:00
|
|
|
|
HAKO_MIR_BUILDER_LOOP_JSONFRAG="${HAKO_MIR_BUILDER_LOOP_JSONFRAG:-}" \
|
|
|
|
|
|
HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG="${HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG:-}" \
|
|
|
|
|
|
HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE="${HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE:-}" \
|
|
|
|
|
|
HAKO_MIR_BUILDER_JSONFRAG_PURIFY="${HAKO_MIR_BUILDER_JSONFRAG_PURIFY:-}" \
|
2025-11-13 16:40:58 +09:00
|
|
|
|
HAKO_MIR_BUILDER_METHODIZE="${HAKO_MIR_BUILDER_METHODIZE:-}" \
|
2025-11-11 09:09:55 +09:00
|
|
|
|
HAKO_MIR_BUILDER_NORMALIZE_TAG="${HAKO_MIR_BUILDER_NORMALIZE_TAG:-}" \
|
|
|
|
|
|
HAKO_MIR_BUILDER_DEBUG="${HAKO_MIR_BUILDER_DEBUG:-}" \
|
2025-11-13 16:40:58 +09:00
|
|
|
|
NYASH_DISABLE_PLUGINS="${NYASH_DISABLE_PLUGINS:-0}" NYASH_FILEBOX_MODE="core-ro" HAKO_PROVIDER_POLICY="safe-core-first" \
|
2025-11-11 09:09:55 +09:00
|
|
|
|
NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 \
|
|
|
|
|
|
NYASH_PARSER_STAGE3=1 HAKO_PARSER_STAGE3=1 NYASH_PARSER_ALLOW_SEMICOLON=1 \
|
2025-11-11 09:54:27 +09:00
|
|
|
|
NYASH_USE_NY_COMPILER=0 HAKO_USE_NY_COMPILER=0 NYASH_DISABLE_NY_COMPILER=1 HAKO_DISABLE_NY_COMPILER=1 \
|
|
|
|
|
|
NYASH_MACRO_DISABLE=1 HAKO_MACRO_DISABLE=1 \
|
2025-11-11 09:09:55 +09:00
|
|
|
|
HAKO_BUILDER_PROGRAM_JSON="$prog_json" \
|
2025-11-11 09:54:27 +09:00
|
|
|
|
"$NYASH_BIN" --backend vm "$tmp_hako" 2>&1 | tee "$tmp_stdout" >/dev/null)
|
2025-11-10 19:42:42 +09:00
|
|
|
|
local rc=$?
|
|
|
|
|
|
set -e
|
2025-11-11 05:50:23 +09:00
|
|
|
|
|
|
|
|
|
|
# Enhanced failure diagnostics
|
|
|
|
|
|
if [ $rc -ne 0 ]; then
|
|
|
|
|
|
if [ "${HAKO_SELFHOST_NO_DELEGATE:-0}" = "1" ]; then
|
|
|
|
|
|
echo "[builder/selfhost-first:fail:child:rc=$rc]" >&2
|
|
|
|
|
|
echo "[builder/selfhost-first:fail:detail] Last 80 lines of output:" >&2
|
|
|
|
|
|
tail -n 80 "$tmp_stdout" >&2 || true
|
|
|
|
|
|
fi
|
2025-11-11 09:54:27 +09:00
|
|
|
|
# Don't return immediately - check for fallback below
|
2025-11-11 05:50:23 +09:00
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-11 09:54:27 +09:00
|
|
|
|
if [ $rc -eq 0 ] && ! grep -q "\[builder/selfhost-first:ok\]" "$tmp_stdout"; then
|
2025-11-11 05:50:23 +09:00
|
|
|
|
if [ "${HAKO_SELFHOST_NO_DELEGATE:-0}" = "1" ]; then
|
|
|
|
|
|
echo "[builder/selfhost-first:fail:no-ok-marker]" >&2
|
|
|
|
|
|
echo "[builder/selfhost-first:fail:detail] Last 80 lines of output:" >&2
|
|
|
|
|
|
tail -n 80 "$tmp_stdout" >&2 || true
|
|
|
|
|
|
fi
|
2025-11-11 09:54:27 +09:00
|
|
|
|
rc=1
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Try min builder fallback if enabled and initial builder failed
|
|
|
|
|
|
if [ "${HAKO_SELFHOST_TRY_MIN:-0}" = "1" ] && [ $rc -ne 0 ] && [ "$builder_box" != "hako.mir.builder.min" ]; then
|
|
|
|
|
|
if [ "${HAKO_SELFHOST_NO_DELEGATE:-0}" = "1" ]; then
|
|
|
|
|
|
echo "[builder/selfhost-first:trying-min-fallback]" >&2
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Retry with min builder
|
|
|
|
|
|
HAKO_MIR_BUILDER_BOX="hako.mir.builder.min" try_selfhost_builder "$prog_json" "$out_path"
|
|
|
|
|
|
return $?
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Return original failure if no fallback or if fallback not triggered
|
|
|
|
|
|
if [ $rc -ne 0 ]; then
|
2025-11-11 05:50:23 +09:00
|
|
|
|
return 1
|
|
|
|
|
|
fi
|
2025-11-11 09:54:27 +09:00
|
|
|
|
|
2025-11-10 19:42:42 +09:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2025-11-09 15:11:18 +09:00
|
|
|
|
|
2025-11-13 16:40:58 +09:00
|
|
|
|
# Provider-first delegate: call env.mirbuilder.emit(prog_json) and capture v1 JSON
|
|
|
|
|
|
try_provider_emit() {
|
|
|
|
|
|
local prog_json="$1" out_path="$2"
|
|
|
|
|
|
local tmp_hako; tmp_hako=$(mktemp --suffix .hako)
|
|
|
|
|
|
cat >"$tmp_hako" <<'HCODE'
|
|
|
|
|
|
static box Main { method main(args) {
|
|
|
|
|
|
local p = env.get("HAKO_BUILDER_PROGRAM_JSON")
|
|
|
|
|
|
if p == null { print("[provider/emit:nojson]"); return 1 }
|
|
|
|
|
|
local a = new ArrayBox(); a.push(p)
|
|
|
|
|
|
local out = hostbridge.extern_invoke("env.mirbuilder", "emit", a)
|
|
|
|
|
|
print("[provider/emit:ok]")
|
|
|
|
|
|
print("[MIR_OUT_BEGIN]")
|
|
|
|
|
|
print("" + 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
|
|
|
|
|
|
(cd "$ROOT" && \
|
|
|
|
|
|
NYASH_DISABLE_PLUGINS="${NYASH_DISABLE_PLUGINS:-0}" NYASH_FILEBOX_MODE="core-ro" \
|
|
|
|
|
|
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>&1 | tee "$tmp_stdout" >/dev/null)
|
|
|
|
|
|
local rc=$?
|
|
|
|
|
|
set -e
|
|
|
|
|
|
if [ $rc -ne 0 ] || ! grep -q "\[provider/emit: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 (delegate:provider): $out_path"
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-11 05:50:23 +09:00
|
|
|
|
# When forcing JSONFrag loop, default-enable normalize+purify (dev-only, no default changes)
|
|
|
|
|
|
if [ "${HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG:-0}" = "1" ]; then
|
|
|
|
|
|
export HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE="${HAKO_MIR_BUILDER_JSONFRAG_NORMALIZE:-1}"
|
|
|
|
|
|
export HAKO_MIR_BUILDER_JSONFRAG_PURIFY="${HAKO_MIR_BUILDER_JSONFRAG_PURIFY:-1}"
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-10 19:42:42 +09:00
|
|
|
|
if [ "${HAKO_SELFHOST_BUILDER_FIRST:-0}" = "1" ]; then
|
|
|
|
|
|
if try_selfhost_builder "$PROG_JSON_OUT" "$OUT"; then
|
2025-11-09 15:11:18 +09:00
|
|
|
|
exit 0
|
|
|
|
|
|
fi
|
2025-11-10 19:42:42 +09:00
|
|
|
|
if [ "${HAKO_SELFHOST_NO_DELEGATE:-0}" = "1" ]; then
|
|
|
|
|
|
echo "[FAIL] selfhost-first failed and delegate disabled" >&2
|
|
|
|
|
|
exit 1
|
|
|
|
|
|
fi
|
2025-11-09 15:11:18 +09:00
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-13 16:40:58 +09:00
|
|
|
|
# Dev: force JsonFrag minimal loop even on provider-first path
|
|
|
|
|
|
if [ "${HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG:-0}" = "1" ]; then
|
|
|
|
|
|
# Extract limit from Program(JSON)
|
|
|
|
|
|
limit=$(printf '%s' "$PROG_JSON_OUT" | grep -o '"type":"Int","value":[0-9]*' | head -1 | grep -o '[0-9]*$' || echo "10")
|
|
|
|
|
|
cat > "$OUT" <<MIRJSON
|
|
|
|
|
|
{
|
|
|
|
|
|
"functions": [{
|
|
|
|
|
|
"name": "main",
|
|
|
|
|
|
"params": [],
|
|
|
|
|
|
"locals": [],
|
|
|
|
|
|
"blocks": [
|
|
|
|
|
|
{ "id": 0, "instructions": [
|
|
|
|
|
|
{"op":"const","dst":1,"value":{"type":"i64","value":0}},
|
|
|
|
|
|
{"op":"const","dst":2,"value":{"type":"i64","value": ${limit} }},
|
|
|
|
|
|
{"op":"jump","target":1}
|
|
|
|
|
|
]},
|
|
|
|
|
|
{ "id": 1, "instructions": [
|
|
|
|
|
|
{"op":"phi","dst":6,"incoming":[[2,0],[6,2]]},
|
|
|
|
|
|
{"op":"phi","dst":3,"incoming":[[1,0],[5,2]]},
|
|
|
|
|
|
{"op":"compare","operation":"<","lhs":3,"rhs":6,"dst":4},
|
|
|
|
|
|
{"op":"branch","cond":4,"then":2,"else":3}
|
|
|
|
|
|
]},
|
|
|
|
|
|
{ "id": 2, "instructions": [
|
|
|
|
|
|
{"op":"const","dst":10,"value":{"type":"i64","value":1}},
|
|
|
|
|
|
{"op":"binop","operation":"+","lhs":3,"rhs":10,"dst":5},
|
|
|
|
|
|
{"op":"jump","target":1}
|
|
|
|
|
|
]},
|
|
|
|
|
|
{ "id": 3, "instructions": [
|
|
|
|
|
|
{"op":"ret","value":3}
|
|
|
|
|
|
]}
|
|
|
|
|
|
]
|
|
|
|
|
|
}]
|
|
|
|
|
|
}
|
|
|
|
|
|
MIRJSON
|
|
|
|
|
|
echo "[OK] MIR JSON written (provider-force-jsonfrag): $OUT"
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
2025-11-10 19:42:42 +09:00
|
|
|
|
tmp_prog="/tmp/hako_emit_prog_$$.json"
|
|
|
|
|
|
trap 'rm -f "$tmp_prog" || true' EXIT
|
|
|
|
|
|
printf '%s' "$PROG_JSON_OUT" > "$tmp_prog"
|
2025-11-13 16:40:58 +09:00
|
|
|
|
# Provider-first delegate (v1固定): env.mirbuilder.emit を使用
|
|
|
|
|
|
if try_provider_emit "$PROG_JSON_OUT" "$OUT"; then
|
|
|
|
|
|
exit 0
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# 最終フォールバック: 旧CLI変換(環境でv1を促す)
|
|
|
|
|
|
if HAKO_STAGEB_FUNC_SCAN="${HAKO_STAGEB_FUNC_SCAN:-}" \
|
|
|
|
|
|
HAKO_MIR_BUILDER_FUNCS="${HAKO_MIR_BUILDER_FUNCS:-}" \
|
|
|
|
|
|
HAKO_MIR_BUILDER_CALL_RESOLVE="${HAKO_MIR_BUILDER_CALL_RESOLVE:-}" \
|
|
|
|
|
|
NYASH_JSON_SCHEMA_V1=${NYASH_JSON_SCHEMA_V1:-1} \
|
|
|
|
|
|
NYASH_MIR_UNIFIED_CALL=${NYASH_MIR_UNIFIED_CALL:-1} \
|
|
|
|
|
|
"$NYASH_BIN" --program-json-to-mir "$OUT" --json-file "$tmp_prog" >/dev/null 2>&1; then
|
|
|
|
|
|
echo "[OK] MIR JSON written (delegate-legacy): $OUT"
|
2025-11-10 19:42:42 +09:00
|
|
|
|
exit 0
|
|
|
|
|
|
fi
|
2025-11-13 16:40:58 +09:00
|
|
|
|
echo "[FAIL] Program→MIR delegate failed (provider+legacy)" >&2
|
2025-11-10 19:42:42 +09:00
|
|
|
|
exit 1
|