Files
hakorune/crates/nyash_kernel
tomoaki c117a04035 fix(rewrite): toString normalization to BoxCall(slot #0) - Phase 287 P4
Root cause: toString/stringify/str were being rewritten to Global/Method calls
with class inference, causing Main.toString/0 to be called for primitives.

Fix (Box-First + Legacy Deletion):
1.  MIR Builder - toString normalization (special.rs)
   - ALWAYS emit BoxCall with method_id=0 for toString/stringify/str
   - Do NOT rewrite to Global(Class.str/0) or Method calls
   - DELETED 70+ lines of complex class inference logic
   - Primitive guard with method name filter (known.rs)

2.  JSON Serializer - method_id output (mir_json_emit.rs)
   - Include method_id field in BoxCall JSON for LLVM

3.  LLVM Backend - universal slot #0 support
   - Extract method_id from JSON (instruction_lower.py)
   - Box primitives via nyash.box.from_i64 (boxcall.py)
   - Invoke toString via plugin system with method_id=0
   - ⚠️ TODO: Add nyash.integer.tostring_h to kernel

Test Results:
 VM: local x = 1; print(x.toString()) → "1" (PASS)
 VM: array_length test (boxed Integer) → PASS
⚠️ LLVM: Compiles successfully, needs kernel function

SSOT: slot_registry - toString is ALWAYS universal slot #0

Legacy Deleted:
- special.rs: Complex class inference rewrite (~70 lines)
- special.rs: Unique suffix fallback for toString
- special.rs: Main box special handling

Files changed:
- src/mir/builder/rewrite/special.rs (try_early_str_like_to_dst)
- src/mir/builder/rewrite/known.rs (primitive guards x4)
- src/runner/mir_json_emit.rs (method_id serialization x2)
- src/llvm_py/builders/instruction_lower.py (method_id extraction)
- src/llvm_py/instructions/boxcall.py (slot #0 handler)
- docs/reference/language/quick-reference.md (toString SSOT)

🎊 Generated with Claude Code
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-25 11:38:05 +09:00
..

Nyash Kernel

Minimal runtime kernel for Nyash language - Plugin-First Architecture

Generated: 2025-09-24 Architecture: Phase 2.4 NyRT→NyKernel Revolution Complete

Overview

The Nyash Kernel (nyash_kernel) is the minimal runtime core that replaced the legacy NyRT system. This represents a 42% reduction in runtime complexity by moving from VM-dependent architecture to a unified Plugin-First system.

Architecture Revolution

From NyRT to NyKernel (Phase 2.4 Complete)

Before (Legacy NyRT):

  • Mixed VM/Plugin dependencies
  • with_legacy_vm_args scattered throughout codebase
  • 58% essential + 42% deletable functions
  • Complex shim layer for LLVM integration

After (NyKernel):

  • Pure Plugin-First architecture
  • Zero legacy VM dependencies
  • Only essential kernel functions remain
  • Clean C ABI for LLVM integration

🏗️ Core Components

Essential Kernel Functions (58% - Kept)

  • GC Management: Safepoints, write barriers, memory management
  • Handle Registry: Object handle management for AOT/JIT
  • Plugin Host: Unified plugin loading and method resolution
  • Process Entry: Main entry point and runtime initialization

Removed Shim Functions (42% - Deleted)

  • with_legacy_vm_args - 11 locations completely removed
  • Legacy VM argument processing
  • String/Box operation shims
  • VM-specific encoding functions

Build Output

Target: libnyash_kernel.a (static library)
Status: Clean build (0 errors, 0 warnings)
Integration: LLVM + VM unified

Implementation Details

Deleted Legacy Functions

File Locations Status
encode.rs 1 Removed
birth.rs 1 Removed
future.rs 2 Removed
invoke.rs 6 Removed
invoke_core.rs 1 Removed
Total 11 Complete

Plugin-First Integration

All Box operations now route through the unified plugin system:

// Before: VM-dependent
with_legacy_vm_args(|args| { ... })

// After: Plugin-First
let host = get_global_plugin_host().read()?;
host.create_box(type_name, &args)?

🔥 ExternCall Print修正 (codex技術力)

Phase 2.4で解決した重大問題: LLVM EXEでprint()出力されない

問題の詳細

  • 症状: VM実行は正常、LLVM EXEは無音
  • 根本原因: src/llvm_py/instructions/externcall.pyの引数変換バグ
  • 技術詳細: 文字列ハンドル→ポインタ変換後にnull上書き

修正内容

# src/llvm_py/instructions/externcall.py:152-154
else:
    # used_string_h2p was true: keep the resolved pointer (do not null it)
    pass

検証結果

/tmp/direct_python_test_fixed
# 出力:
# 🎉 ExternCall print修正テスト
# codex先生の名前解決修正確認
# Result: 0

Usage

For LLVM Backend

# Build with LLVM integration
cargo build --release -p nyash_kernel
# Output (workspace default):
#   target/release/libnyash_kernel.a
# Note: if you set `CARGO_TARGET_DIR`, the output is under `$CARGO_TARGET_DIR/release/`.

Notes:

  • libnyash_kernel.a is required for native executable linking (AOT/--emit-exe/ny-llvmc --emit exe).
  • The Python llvmlite harness path (NYASH_LLVM_USE_HARNESS=1) does not require the static library.

For VM Backend

# Runtime integration (automatic)
./target/release/hakorune program.hako

Design Philosophy

"Everything is Plugin" - The kernel provides only the essential infrastructure for plugin management, leaving all Box implementations to the plugin system.

Core Principles

  1. Minimal Surface: Only GC, handles, plugins, and process entry
  2. Plugin-First: All Box operations through unified plugin host
  3. C ABI Clean: Stable interface for LLVM/VM integration
  4. Zero Legacy: Complete removal of VM-dependent code paths

ChatGPT5 × codex × Claude Collaboration

This kernel represents a historic achievement in AI-assisted architecture design:

  • Design: ChatGPT5 Pro architectural analysis (42% reduction strategy)
  • Implementation: Claude systematic implementation (11 locations)
  • Debugging: codex root cause analysis (ExternCall print fix)
  • Result: 100% successful architecture revolution + critical bug resolution

Integration

The Nyash Kernel integrates seamlessly with:

  • LLVM Backend: Static linking via libnyash_kernel.a
  • VM Backend: Dynamic plugin loading
  • Build System: tools/build_llvm.sh integration complete

Part of Phase 15 Nyash Self-hosting Revolution Documentation: ChatGPT5 NyRT→NyKernel Design