Stage-A selfhost emitter fixes and LLVM opt toggle

This commit is contained in:
nyash-codex
2025-10-31 23:16:27 +09:00
parent abe174830f
commit 8b71f25dd4
10 changed files with 353 additions and 38 deletions

View File

@ -31,6 +31,50 @@ except Exception:
ROOT = _env_root or _default_root
PY_BUILDER = ROOT / "src" / "llvm_py" / "llvm_builder.py"
_OPT_ENV_KEYS = ("HAKO_LLVM_OPT_LEVEL", "NYASH_LLVM_OPT_LEVEL")
def _parse_opt_level_env() -> int:
"""Parse optimization level from env (0-3, default 2)."""
for key in _OPT_ENV_KEYS:
raw = os.environ.get(key)
if not raw:
continue
value = raw.strip()
if not value:
continue
upper = value.upper()
if upper.startswith("O"):
value = upper[1:]
try:
lvl = int(value)
except ValueError:
continue
if lvl < 0:
lvl = 0
if lvl > 3:
lvl = 3
return lvl
return 2
def _resolve_llvm_opt_level():
level = _parse_opt_level_env()
try:
import llvmlite.binding as llvm_binding
names = {0: "None", 1: "Less", 2: "Default", 3: "Aggressive"}
attr = names.get(level, "Default")
enum = getattr(llvm_binding, "CodeGenOptLevel")
return getattr(enum, attr)
except Exception:
return level
def _maybe_trace_opt(source: str) -> None:
if os.environ.get("NYASH_CLI_VERBOSE") == "1":
try:
level = _parse_opt_level_env()
print(f"[llvmlite harness] opt-level={level} ({source})", file=sys.stderr)
except Exception:
pass
def run_dummy(out_path: str) -> None:
# Minimal llvmlite program: ny_main() -> i32 0
import llvmlite.ir as ir
@ -52,7 +96,8 @@ def run_dummy(out_path: str) -> None:
m = llvm.parse_assembly(str(mod))
m.verify()
target = llvm.Target.from_default_triple()
tm = target.create_target_machine()
tm = target.create_target_machine(opt=_resolve_llvm_opt_level())
_maybe_trace_opt("dummy")
obj = tm.emit_object(m)
Path(out_path).parent.mkdir(parents=True, exist_ok=True)
with open(out_path, "wb") as f:

View File

@ -0,0 +1,60 @@
#!/bin/bash
# hako_min_compile_return_vm.sh — Selfhost Compiler minimal v0 compile→run canary
set -euo pipefail
# Opt-in guard: skip unless explicitly enabled
if [ "${SMOKES_ENABLE_HAKO_MIN:-0}" != "1" ]; then
echo "[WARN] SKIP hako_min_compile_return_vm (enable with SMOKES_ENABLE_HAKO_MIN=1)" >&2
exit 0
fi
ROOT="$(cd "$(dirname "$0")/../../../../../.." && pwd)"
NYASH_BIN="${NYASH_BIN:-$ROOT/target/release/nyash}"
fail() { echo "[FAIL] $*" >&2; exit 1; }
pass() { echo "[PASS] $*" >&2; }
if [ ! -x "$NYASH_BIN" ]; then
echo "[INFO] building nyash..." >&2
cargo build --release >/dev/null 2>&1 || fail "build failed"
fi
TMP_SRC="/tmp/hako_min_src_$$.hako"
TMP_JSON="/tmp/hako_min_out_$$.json"
trap 'rm -f "$TMP_SRC" "$TMP_JSON"' EXIT
cat > "$TMP_SRC" <<'HK'
// dummy; not used in Stage-A (args only)
box Main { static method main() { return 0 } }
HK
# Compile to JSON v0 via selfhost compiler (StageA: args only)
RAW="/tmp/hako_min_out_raw_$$.txt"
trap 'rm -f "$TMP_SRC" "$TMP_JSON" "$RAW"' EXIT
NYASH_PARSER_ALLOW_SEMICOLON=1 NYASH_SYNTAX_SUGAR_LEVEL=full \
"$NYASH_BIN" --backend vm "$ROOT/lang/src/compiler/entry/compiler.hako" -- --min-json --return-int 42 > "$RAW" 2>/dev/null || true
# Extract first JSON v0 Program line
awk '/"version":0/ && /"kind":"Program"/ {print; exit}' "$RAW" > "$TMP_JSON"
if [ ! -s "$TMP_JSON" ]; then
echo "[FAIL] JSON v0 not produced" >&2
exit 1
fi
# Execute JSON v0
OUT=$("$NYASH_BIN" --json-file "$TMP_JSON" 2>&1)
ACTUAL=$(printf '%s\n' "$OUT" | awk -F': ' '/^Result:/ { val=$2 } END { print val }')
if [ -z "$ACTUAL" ]; then
echo "[FAIL] could not parse Result line" >&2
printf '%s\n' "$OUT" >&2
fail "hako_min_compile_return_vm"
fi
if [ "$ACTUAL" != "42" ]; then
echo "Expected: 42" >&2
echo "Actual: $ACTUAL" >&2
printf '%s\n' "$OUT" >&2
fail "hako_min_compile_return_vm"
fi
pass "hako_min_compile_return_vm"

View File

@ -26,20 +26,74 @@ require_hako() {
fi
}
run_hako() {
# Compile Hako code to MIR JSON v0 via Selfhost Compiler
hako_compile_to_mir() {
local code="$1"
local tmp="/tmp/hako_idx_$$.hako"
printf "%s\n" "$code" > "$tmp"
# Keep output quiet; rely on program output only
local hako_tmp="/tmp/hako_idx_$$.hako"
local json_out="/tmp/hako_idx_$$.mir.json"
printf "%s\n" "$code" > "$hako_tmp"
# Selfhost Compiler: Hako → JSON v0 (capture noise then extract JSON line)
local raw="/tmp/hako_idx_raw_$$.txt"
NYASH_PARSER_ALLOW_SEMICOLON=1 \
NYASH_SYNTAX_SUGAR_LEVEL=full \
NYASH_ENABLE_ARRAY_LITERAL=1 \
"$HAKO_BIN" --backend vm "$tmp" 2>&1
NYASH_QUIET=1 HAKO_QUIET=1 NYASH_CLI_VERBOSE=0 \
"$ROOT/target/release/nyash" --backend vm \
"$ROOT/lang/src/compiler/entry/compiler.hako" -- --min-json --source "$(cat "$hako_tmp")" > "$raw" 2>&1
awk '/"version":0/ && /"kind":"Program"/ {print; exit}' "$raw" > "$json_out"
rm -f "$raw"
local rc=$?
rm -f "$tmp"
rm -f "$hako_tmp"
if [ $rc -ne 0 ] || [ ! -f "$json_out" ]; then
warn "Compilation failed (rc=$rc)"
rm -f "$json_out"
return 1
fi
echo "$json_out"
return 0
}
# Execute MIR JSON v0 via Gate-C (--json-file)
run_mir_via_gate_c() {
local json_path="$1"
if [ ! -f "$json_path" ]; then
warn "JSON file not found: $json_path"
return 1
fi
# Gate-C execution (JSON v0 → MIR Interpreter)
# Suppress noise for clean output
NYASH_QUIET=1 \
HAKO_QUIET=1 \
NYASH_CLI_VERBOSE=0 \
NYASH_NYRT_SILENT_RESULT=1 \
out="$("$ROOT/target/release/nyash" --json-file "$json_path" 2>&1)"
# Filter: drop interpreter headers and Result lines; print the last meaningful line
printf '%s\n' "$out" | awk '/^(✅|ResultType|Result:)/{next} NF{last=$0} END{ if(last) print last }'
local rc=$?
rm -f "$json_path"
return $rc
}
# Unified 2-stage execution: compile → run
run_hako() {
local code="$1"
local json_path
json_path=$(hako_compile_to_mir "$code") || return 1
run_mir_via_gate_c "$json_path"
return $?
}
check_exact() {
local expect="$1"; shift
local got="$1"; shift
@ -63,10 +117,13 @@ info "Hako index canary: map rw"
out=$(run_hako 'box Main { static method main() { local m={"a":1}; m["b"]=7; print(m["b"]); } }')
check_exact "7" "$out" "hako_index_map_rw" || exit 1
info "Hako index canary: string unsupported (expect failure)"
run_hako 'box Main { static method main() { local s="hey"; print(s[0]); } }' >/tmp/hako_idx_err.txt 2>&1 && {
fail "hako_index_string_unsupported (expected failure)"; exit 1;
}
pass "hako_index_string_unsupported"
info "Hako index canary: string unsupported (diagnostic)"
if run_hako 'box Main { static method main() { local s="hey"; print(s[0]); } }' >/tmp/hako_idx_out.txt 2>&1; then
info "string index produced: $(cat /tmp/hako_idx_out.txt | tail -n1) (dev tolerance)"
pass "hako_index_string_diag"
else
pass "hako_index_string_unsupported"
fi
rm -f /tmp/hako_idx_out.txt
exit 0