Files
hakorune/docs/development/roadmap/phases/phase-21.7-normalization
nyash-codex dda65b94b7 Phase 21.7 normalization: optimization pre-work + bench harness expansion
- Add opt-in optimizations (defaults OFF)
  - Ret purity verifier: NYASH_VERIFY_RET_PURITY=1
  - strlen FAST enhancement for const handles
  - FAST_INT gate for same-BB SSA optimization
  - length cache for string literals in llvmlite
- Expand bench harness (tools/perf/microbench.sh)
  - Add branch/call/stringchain/arraymap/chip8/kilo cases
  - Auto-calculate ratio vs C reference
  - Document in benchmarks/README.md
- Compiler health improvements
  - Unify PHI insertion to insert_phi_at_head()
  - Add NYASH_LLVM_SKIP_BUILD=1 for build reuse
- Runtime & safety enhancements
  - Clarify Rust/Hako ownership boundaries
  - Strengthen receiver localization (LocalSSA/pin/after-PHIs)
  - Stop excessive PluginInvoke→BoxCall rewrites
- Update CURRENT_TASK.md, docs, and canaries

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-13 16:40:58 +09:00
..

Phase 21.7 — Normalization & Unification (Methodize Static Boxes)

Goal

  • Unify user-defined function calls onto a single, consistent representation.
  • Move from ad-hoc Global("Box.method") calls toward Method calls with an explicit (singleton) receiver when appropriate.
  • Keep defaults stable; introduce dev toggles and canaries; then promote to default after green.

Scope

  • Parser/StageB: keep emitting Program(JSON v0). No schema changes required for MVP.
  • MirBuilder: add methodization (dev toggle) that:
    • Emits method functions as-is (defs) and/or provides a mapping to Method calls
    • Rewrites Global("Box.method") to Method {receiver=static singleton, box_name, method}
    • Preserves arity and naming as Box.method/N
  • VM: handle Method calls uniformly (receiver resolved via ensure_static_box_instance for static boxes).
  • LLVM: rely on mir_call(Method) lowering (already supported) and provide the static receiver where needed.

Toggles

  • HAKO_MIR_BUILDER_METHODIZE=1 — enable methodization (rewrite Global→Method with static singleton receiver)
  • HAKO_STAGEB_FUNC_SCAN=1 — StageB defs scan (already available)
  • HAKO_MIR_BUILDER_FUNCS=1 — defs→MIR 関数化(既存)
  • HAKO_MIR_BUILDER_CALL_RESOLVE=1 — Global 名解決(既存)

Design Rules

  • Naming: canonical "Box.method/N". Arity N must equal params.len (or callsite args len if defs unknown).
  • Receiver: for static boxes, provide implicit singleton receiver. For instance boxes, preserve existing Method path.
  • Effects: preserve EffectMask semantics; do not broaden side effects.
  • FailFast: when rewrite is ambiguous (multiple candidates), keep original form and WARN under dev toggle; never change defaults silently.

Acceptance

  • Canaries:
    • call (global style) passes when methodization ON (rc unchanged)
    • Existing return/binop/loop/call (global) canaries remain green when OFF
    • A dedicated methodization canary asserts presence of callee.type=="Method" in MIR(JSON v1) or correct v0 lowering

Rollout Plan

  1. Ship behind HAKO_MIR_BUILDER_METHODIZE=1 with canaries
  2. Validate on representative apps (selfhostfirst path)
  3. Promote to default ON only after green for a cycle; keep rollback instructions

Rollback

  • Disable HAKO_MIR_BUILDER_METHODIZE. Revert to Global("Box.method") resolution path (current 21.6 behavior).