Phase 12.7完了 + ChatGPT5によるVMリファクタリング

## 📚 Phase 12.7 ドキュメント整理
- ChatGPT5作成のANCP Token仕様書v1を整備
- フォルダ構造を機能別に再編成:
  - ancp-specs/ : ANCP圧縮技法仕様
  - grammar-specs/ : 文法改革仕様
  - implementation/ : 実装計画
  - ai-feedback/ : AIアドバイザーフィードバック
- 各フォルダにREADME.md作成で導線改善

## 🔧 ChatGPT5によるVMリファクタリング
- vm_instructions.rs (1927行) をモジュール分割:
  - boxcall.rs : Box呼び出し処理
  - call.rs : 関数呼び出し処理
  - extern_call.rs : 外部関数処理
  - function_new.rs : FunctionBox生成
  - newbox.rs : Box生成処理
  - plugin_invoke.rs : プラグイン呼び出し
- VM実行をファイル分割で整理:
  - vm_state.rs : 状態管理
  - vm_exec.rs : 実行エンジン
  - vm_control_flow.rs : 制御フロー
  - vm_gc.rs : GC処理
- plugin_loader_v2もモジュール化

##  新機能実装
- FunctionBox呼び出しのVM/MIR統一進捗
- ラムダ式のFunctionBox変換テスト追加
- 関数値の直接呼び出し基盤整備

次ステップ: ANCPプロトタイプ実装開始(Week 1)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-09-04 03:41:02 +09:00
parent 7455c9ec97
commit 6488b0542e
57 changed files with 3803 additions and 3871 deletions

View File

@ -186,6 +186,34 @@ impl LowerCore {
fn try_emit(&mut self, b: &mut dyn IRBuilder, instr: &MirInstruction, cur_bb: crate::mir::BasicBlockId, func: &crate::mir::MirFunction) -> Result<(), String> {
use crate::mir::MirInstruction as I;
match instr {
I::Call { dst, func, args, .. } => {
// FunctionBox call shim: emit hostcall nyash_fn_callN(func_h, args...)
// Push function operand (param or known)
self.push_value_if_known_or_param(b, func);
// Push up to 4 args (unknown become iconst 0 via helper)
for a in args.iter() { self.push_value_if_known_or_param(b, a); }
// Choose symbol by arity
let argc = args.len();
let sym = match argc {
0 => "nyash_fn_call0",
1 => "nyash_fn_call1",
2 => "nyash_fn_call2",
3 => "nyash_fn_call3",
4 => "nyash_fn_call4",
5 => "nyash_fn_call5",
6 => "nyash_fn_call6",
7 => "nyash_fn_call7",
_ => "nyash_fn_call8",
};
// Emit typed call: all params as I64, returning I64 handle
// Build param kinds vector: 1 (func) + argc (args)
let mut params: Vec<crate::jit::lower::builder::ParamKind> = Vec::new();
params.push(crate::jit::lower::builder::ParamKind::I64);
for _ in 0..core::cmp::min(argc, 8) { params.push(crate::jit::lower::builder::ParamKind::I64); }
b.emit_host_call_typed(sym, &params, true, false);
// Mark destination as handle-like
if let Some(d) = dst { self.handle_values.insert(*d); }
}
I::Await { dst, future } => {
// Push future param index when known; otherwise -1 to trigger legacy search in shim
if let Some(pidx) = self.param_index.get(future).copied() { b.emit_param_i64(pidx); } else { b.emit_const_i64(-1); }

View File

@ -761,6 +761,53 @@ pub(super) extern "C" fn nyash_string_from_ptr(ptr: u64, len: u64) -> i64 {
}
}
// ===== FunctionBox call shims (by arity, up to 4) =====
#[cfg(feature = "cranelift-jit")]
fn vmvalue_from_jit_arg_i64(v: i64) -> crate::backend::vm::VMValue { super::vmvalue_from_jit_arg_i64(v) }
#[cfg(feature = "cranelift-jit")]
fn i64_from_vmvalue(v: crate::backend::vm::VMValue) -> i64 { super::i64_from_vmvalue(v) }
#[cfg(feature = "cranelift-jit")]
fn fn_call_impl(func_h: u64, args: &[i64]) -> i64 {
use crate::box_trait::NyashBox;
let f_arc = match crate::jit::rt::handles::get(func_h) { Some(a) => a, None => return 0 };
if let Some(fun) = f_arc.as_any().downcast_ref::<crate::boxes::function_box::FunctionBox>() {
let mut ny_args: Vec<Box<dyn NyashBox>> = Vec::new();
for &ai in args {
let v = vmvalue_from_jit_arg_i64(ai);
ny_args.push(v.to_nyash_box());
}
match crate::interpreter::run_function_box(fun, ny_args) {
Ok(out) => {
let vmv = crate::backend::vm::VMValue::from_nyash_box(out);
i64_from_vmvalue(vmv)
}
Err(_) => 0,
}
} else { 0 }
}
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call0(func_h: u64) -> i64 { fn_call_impl(func_h, &[]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call1(func_h: u64, a0: i64) -> i64 { fn_call_impl(func_h, &[a0]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call2(func_h: u64, a0: i64, a1: i64) -> i64 { fn_call_impl(func_h, &[a0,a1]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call3(func_h: u64, a0: i64, a1: i64, a2: i64) -> i64 { fn_call_impl(func_h, &[a0,a1,a2]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call4(func_h: u64, a0: i64, a1: i64, a2: i64, a3: i64) -> i64 { fn_call_impl(func_h, &[a0,a1,a2,a3]) }
// extended arities (5..8)
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call5(func_h: u64, a0: i64, a1: i64, a2: i64, a3: i64, a4: i64) -> i64 { fn_call_impl(func_h, &[a0,a1,a2,a3,a4]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call6(func_h: u64, a0: i64, a1: i64, a2: i64, a3: i64, a4: i64, a5: i64) -> i64 { fn_call_impl(func_h, &[a0,a1,a2,a3,a4,a5]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call7(func_h: u64, a0: i64, a1: i64, a2: i64, a3: i64, a4: i64, a5: i64, a6: i64) -> i64 { fn_call_impl(func_h, &[a0,a1,a2,a3,a4,a5,a6]) }
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_fn_call8(func_h: u64, a0: i64, a1: i64, a2: i64, a3: i64, a4: i64, a5: i64, a6: i64, a7: i64) -> i64 { fn_call_impl(func_h, &[a0,a1,a2,a3,a4,a5,a6,a7]) }
// Build a StringBox handle from two u64 chunks (little-endian) and length (<=16)
#[cfg(feature = "cranelift-jit")]
pub(super) extern "C" fn nyash_string_from_u64x2(lo: u64, hi: u64, len: i64) -> i64 {