Files
hakorune/docs/development/current/main/phases/phase-275/P0-INSTRUCTIONS.md
tomoaki 757193891f feat(llvm/phi): Phase 277 P1 - fail-fast validation for PHI strict mode
## Summary
Implemented fail-fast validation for PHI ordering and value resolution in strict mode.

## Changes

### P1-1: Strict mode for "PHI after terminator"
- File: `src/llvm_py/phi_wiring/wiring.py::ensure_phi`
- Behavior: `NYASH_LLVM_PHI_STRICT=1` → RuntimeError if PHI created after terminator
- Default: Warning only (no regression)

### P1-2: Strict mode for "fallback 0"
- File: `src/llvm_py/phi_wiring/wiring.py::wire_incomings`
- Behavior: Strict mode forbids silent fallback to 0 (2 locations)
  - Location 1: Unresolvable incoming value
  - Location 2: Type coercion failure
- Error messages point to next debug file: `llvm_builder.py::_value_at_end_i64`

### P1-3: Connect verify_phi_ordering() to execution path
- File: `src/llvm_py/builders/function_lower.py`
- Behavior: Verify PHI ordering after all instructions emitted
- Debug mode: Shows " All N blocks have correct PHI ordering"
- Strict mode: Raises RuntimeError with block list if violations found

## Testing
 Test 1: strict=OFF - passes without errors
 Test 2: strict=ON - passes without errors (no violations in test fixtures)
 Test 3: debug mode - verify_phi_ordering() connected and running

## Scope
- LLVM harness (Python) changes only
- No new environment variables (uses existing 3 from Phase 277 P2)
- No JoinIR/Rust changes (root fix is Phase 279)
- Default behavior unchanged (strict mode opt-in)

## Next Steps
- Phase 278: Remove deprecated env var support
- Phase 279: Root fix - unify "2本のコンパイラ" pipelines

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-22 14:48:37 +09:00

4.1 KiB
Raw Blame History

Phase 275 P0 (impl): Coercion SSOT rollout

Status: planned / implementation guide

This is the “next instruction sheet” for implementing the accepted coercion SSOT (A1/B2/C2) across backends.

SSOT decisions:

  • docs/development/current/main/phases/phase-274/P3-DECISIONS.md

Scope (P0)

Implement and lock these three rule sets:

  • truthiness: Void/BoxRef fail-fast (with bridge-box exceptions)
  • equality: B2 (Number-only, precise Int↔Float)
  • +: C2 (Number-only promotion; String+String only; String mixed → TypeError)

Backends in scope:

  • Rust VM (primary SSOT)
  • LLVM harness (llvmlite path) parity with Rust VM

Out of scope:

  • adding new language features (keywords)
  • expanding env var toggles
  • rewriting the optimizer broadly (only touch what is required to enforce semantics)

Step 0: Lock reference + plan

  • Keep docs/reference/language/types.md as “current executable SSOT” until the implementation is complete.
  • After implementation + tests land, update types.md to the new semantics (it becomes SSOT again).

Step 1: Rust VM — truthiness (A1)

Target:

  • src/backend/abi_util.rs::to_bool_vm (or equivalent truthiness entry)

Changes:

  • Void in boolean context → return TypeError
  • BoxRef:
    • allow only explicit bridge boxes: BoolBox/IntegerBox/StringBox
    • treat VoidBox as Void (→ TypeError)
    • other BoxRef types → TypeError

Acceptance:

  • A dedicated fixture demonstrates if Void { ... } is a runtime error (fail-fast).

Step 2: Rust VM — equality (B2)

Target:

  • src/backend/abi_util.rs::eq_vm (and any helpers it relies on)

Changes:

  • Remove Bool↔Int coercion.
  • Keep Int↔Float comparison, but make it precise:
    • if Float is NaN → false
    • if Float is integral and within i64 exact range → compare as Int exactly
    • otherwise → false
  • Mixed kinds (except Int↔Float) → false (not error).
  • BoxRef equality stays identity.

Acceptance:

  • Tests cover:
    • 1 == 1.0 true
    • 1 == 1.1 false
    • true == 1 false (or TypeError if you choose a transition rule; document explicitly)

Step 3: Rust VM — + (C2)

Target:

  • src/backend/mir_interpreter/helpers.rs::eval_binop for BinaryOp::Add

Changes:

  • Numeric:
    • Int+Int → Int
    • Float+Float → Float
    • Int+Float / Float+Int → Float (Int promoted to Float)
  • String:
    • String+String → concat
    • String mixed → TypeError (no implicit stringify)
  • Everything else → TypeError

Acceptance:

  • Tests cover:
    • 1 + 2.03.0
    • "a" + "b""ab"
    • "a" + 1 → TypeError

Step 4: LLVM harness parity

Targets (likely):

  • src/llvm_py/instructions/binop.py for +
  • src/llvm_py/instructions/compare.py for ==
  • truthiness for branch conditions (inspect branch lowering):
    • src/llvm_py/instructions/controlflow/branch.py
    • and any “truthy” conversions used in control-flow lowering

Notes:

  • LLVM harness uses MIR JSON metadata (value_types) for discriminating raw vs handle.
  • Keep behavior identical to Rust VM; do not re-introduce “string mixed concat”.

Acceptance:

  • VM and LLVM smokes use the same fixtures and produce identical exit codes.

Step 5: Fixtures + smoke tests (SSOT lock)

Create minimal, self-contained fixtures under apps/tests/ and add smokes under tools/smokes/v2/profiles/integration/apps/.

Suggested fixtures (names; adjust as needed):

  • apps/tests/phase275_p0_truthiness_void_error_min.hako
  • apps/tests/phase275_p0_eq_number_only_min.hako
  • apps/tests/phase275_p0_plus_number_only_min.hako

Smoke targets:

  • VM: ..._vm.sh
  • LLVM: ..._llvm.sh (harness/EXE path consistent with current infra)

Rules:

  • No reliance on hidden env toggles.
  • If a test needs runtime-unknown values, avoid externs that arent supported on VM/LLVM.

Step 6: Update SSOT docs

After the implementation is complete and tests pass:

  • Update docs/reference/language/types.md to the new semantics:
    • truthiness: Void/BoxRef fail-fast
    • equality: B2
    • +: C2
  • Update Phase 274/275 status:
    • docs/development/current/main/phases/phase-274/README.md
    • docs/development/current/main/10-Now.md
    • docs/development/current/main/30-Backlog.md