Files
hakorune/src/mir/join_ir_runner.rs
nyash-codex 78c9d4d7fc feat(joinir): Phase 27.8-1~3 — Ops Box導入 + Toggle対応完了
## Phase 27.8-1: JoinIR 命令意味箱(Ops Box)作成 

**新規ファイル**: `src/mir/join_ir_ops.rs`
- `eval_binop()`: Add, Sub, Mul, Div, Or, And の評価ロジック一元化
- `eval_compare()`: Lt, Le, Gt, Ge, Eq, Ne の比較ロジック一元化
- エラー処理: `JoinIrOpError` 型で型安全
- 完全テストカバレッジ: 13個のユニットテスト

**効果**:
- BinOp/Compare の評価ロジックを一箇所に集約
- 再利用可能な API で将来の拡張が容易
- テスタビリティ向上

## Phase 27.8-2: join_ir_runner.rs の Ops Box 統合 

**変更**: `src/mir/join_ir_runner.rs`
- BinOp/Compare の実装を ops box に完全移譲(約70行削減)
- `JoinValue` / `JoinIrOpError` を ops box から再エクスポート
- 後方互換性維持: `JoinRuntimeError = JoinIrOpError`

**効果**:
- コード重複削減(約70行)
- 実装の一貫性保証(ops box の単一実装を使用)

## Phase 27.8-3: MIR→JoinIR Toggle 対応 

**変更**: `src/mir/join_ir.rs`
- `lower_skip_ws_to_joinir()`: トグル対応ディスパッチャー
- `lower_skip_ws_handwritten()`: 既存実装をリネーム(Phase 27.1-27.7)
- `lower_skip_ws_from_mir()`: MIR自動解析版スタブ(Phase 27.8-4 で実装予定)

**環境変数制御**:
```bash
# 手書き版(デフォルト)
./target/release/hakorune program.hako

# MIR自動解析版(Phase 27.8-4 実装予定)
NYASH_JOINIR_LOWER_FROM_MIR=1 ./target/release/hakorune program.hako
```

**効果**:
- 段階的な移行が可能(既存動作を完全に維持)
- A/B テストによる検証が容易

## 変更ファイル

- `src/mir/join_ir_ops.rs` (新規): Ops Box 実装
- `src/mir/join_ir_runner.rs`: Ops Box 使用に変更
- `src/mir/join_ir.rs`: Toggle 対応ディスパッチャー追加
- `src/mir/mod.rs`: join_ir_ops モジュール追加

## コンパイル結果

 0 errors, 18 warnings(既存警告のみ)
 ビルド成功

## 次のステップ

**Phase 27.8-4**: `lower_skip_ws_from_mir()` 本実装
- MirQuery を使った MIR 解析
- パターンマッチング(init, header, break checks, body)
- JoinIR 自動生成(entry function + loop_step function)

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-23 14:47:00 +09:00

246 lines
8.7 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! JoinIR 実験用のミニ実行器Phase 27.2
//!
//! 目的: hand-written / minimal JoinIR を VM と A/B 比較するための軽量ランナー。
//! - 対応値: i64 / bool / String / Unit
//! - 対応命令: Const / BinOp / Compare / BoxCall(StringBox: length, substring) /
//! Call / Jump / Ret
//!
//! Phase 27.8: ops box 統合
//! - JoinValue / JoinIrOpError は join_ir_ops から再エクスポート
//! - eval_binop() / eval_compare() を使用(実装を一箇所に集約)
use std::collections::HashMap;
use crate::mir::join_ir::{
ConstValue, JoinFuncId, JoinInst, JoinModule, MirLikeInst, VarId,
};
// Phase 27.8: ops box からの再エクスポート
pub use crate::mir::join_ir_ops::{JoinIrOpError, JoinValue};
// Phase 27.8: 互換性のため JoinRuntimeError を JoinIrOpError の別名として保持
pub type JoinRuntimeError = JoinIrOpError;
pub fn run_joinir_function(
module: &JoinModule,
entry: JoinFuncId,
args: &[JoinValue],
) -> Result<JoinValue, JoinRuntimeError> {
execute_function(module, entry, args.to_vec())
}
fn execute_function(
module: &JoinModule,
mut current_func: JoinFuncId,
mut current_args: Vec<JoinValue>,
) -> Result<JoinValue, JoinRuntimeError> {
'exec: loop {
let func = module
.functions
.get(&current_func)
.ok_or_else(|| JoinRuntimeError::new(format!("Function {:?} not found", current_func)))?;
if func.params.len() != current_args.len() {
return Err(JoinRuntimeError::new(format!(
"Arity mismatch for {:?}: expected {}, got {}",
func.id,
func.params.len(),
current_args.len()
)));
}
let mut locals: HashMap<VarId, JoinValue> = HashMap::new();
for (param, arg) in func.params.iter().zip(current_args.iter()) {
locals.insert(*param, arg.clone());
}
let mut ip = 0usize;
while ip < func.body.len() {
match &func.body[ip] {
JoinInst::Compute(inst) => {
eval_compute(inst, &mut locals)?;
ip += 1;
}
JoinInst::Call {
func: target,
args,
k_next,
dst,
} => {
if k_next.is_some() {
return Err(JoinRuntimeError::new(
"Join continuation (k_next) is not supported in the experimental runner",
));
}
let resolved_args = materialize_args(args, &locals)?;
if let Some(dst_var) = dst {
let value = execute_function(module, *target, resolved_args)?;
locals.insert(*dst_var, value);
ip += 1;
} else {
current_func = *target;
current_args = resolved_args;
continue 'exec;
}
}
JoinInst::Jump { cont: _, args, cond } => {
let should_jump = match cond {
Some(var) => as_bool(&read_var(&locals, *var)?)?,
None => true,
};
if should_jump {
let ret = if let Some(first) = args.first() {
read_var(&locals, *first)?
} else {
JoinValue::Unit
};
return Ok(ret);
}
ip += 1;
}
JoinInst::Ret { value } => {
let ret = match value {
Some(var) => read_var(&locals, *var)?,
None => JoinValue::Unit,
};
return Ok(ret);
}
}
}
// fallthrough without explicit return
return Ok(JoinValue::Unit);
}
}
fn eval_compute(inst: &MirLikeInst, locals: &mut HashMap<VarId, JoinValue>) -> Result<(), JoinRuntimeError> {
match inst {
MirLikeInst::Const { dst, value } => {
let v = match value {
ConstValue::Integer(i) => JoinValue::Int(*i),
ConstValue::Bool(b) => JoinValue::Bool(*b),
ConstValue::String(s) => JoinValue::Str(s.clone()),
ConstValue::Null => JoinValue::Unit,
};
locals.insert(*dst, v);
}
MirLikeInst::BinOp { dst, op, lhs, rhs } => {
// Phase 27.8: ops box の eval_binop() を使用
let l = read_var(locals, *lhs)?;
let r = read_var(locals, *rhs)?;
let v = crate::mir::join_ir_ops::eval_binop(*op, &l, &r)?;
locals.insert(*dst, v);
}
MirLikeInst::Compare { dst, op, lhs, rhs } => {
// Phase 27.8: ops box の eval_compare() を使用
let l = read_var(locals, *lhs)?;
let r = read_var(locals, *rhs)?;
let v = crate::mir::join_ir_ops::eval_compare(*op, &l, &r)?;
locals.insert(*dst, v);
}
MirLikeInst::BoxCall {
dst,
box_name,
method,
args,
} => {
if box_name != "StringBox" {
return Err(JoinRuntimeError::new(format!(
"Unsupported box call target: {}",
box_name
)));
}
match method.as_str() {
"length" => {
let arg = expect_str(&read_var(locals, args[0])?)?;
locals.insert(*dst.as_ref().ok_or_else(|| {
JoinRuntimeError::new("length call requires destination")
})?, JoinValue::Int(arg.len() as i64));
}
"substring" => {
if args.len() != 3 {
return Err(JoinRuntimeError::new(
"substring expects 3 arguments (s, start, end)",
));
}
let s = expect_str(&read_var(locals, args[0])?)?;
let start = expect_int(&read_var(locals, args[1])?)?;
let end = expect_int(&read_var(locals, args[2])?)?;
let slice = safe_substring(&s, start, end)?;
let dst_var = dst.ok_or_else(|| {
JoinRuntimeError::new("substring call requires destination")
})?;
locals.insert(dst_var, JoinValue::Str(slice));
}
_ => {
return Err(JoinRuntimeError::new(format!(
"Unsupported StringBox method: {}",
method
)))
}
}
}
}
Ok(())
}
fn safe_substring(s: &str, start: i64, end: i64) -> Result<String, JoinRuntimeError> {
if start < 0 || end < 0 {
return Err(JoinRuntimeError::new("substring indices must be non-negative"));
}
let (start_usize, end_usize) = (start as usize, end as usize);
if start_usize > end_usize {
return Err(JoinRuntimeError::new("substring start > end"));
}
if start_usize > s.len() || end_usize > s.len() {
return Err(JoinRuntimeError::new("substring indices out of bounds"));
}
Ok(s[start_usize..end_usize].to_string())
}
fn read_var(locals: &HashMap<VarId, JoinValue>, var: VarId) -> Result<JoinValue, JoinRuntimeError> {
locals
.get(&var)
.cloned()
.ok_or_else(|| JoinRuntimeError::new(format!("Variable {:?} not bound", var)))
}
fn materialize_args(
args: &[VarId],
locals: &HashMap<VarId, JoinValue>,
) -> Result<Vec<JoinValue>, JoinRuntimeError> {
args.iter().map(|v| read_var(locals, *v)).collect()
}
fn as_bool(value: &JoinValue) -> Result<bool, JoinRuntimeError> {
match value {
JoinValue::Bool(b) => Ok(*b),
JoinValue::Int(i) => Ok(*i != 0),
JoinValue::Unit => Ok(false),
other => Err(JoinRuntimeError::new(format!(
"Expected bool-compatible value, got {:?}",
other
))),
}
}
fn expect_int(value: &JoinValue) -> Result<i64, JoinRuntimeError> {
match value {
JoinValue::Int(i) => Ok(*i),
other => Err(JoinRuntimeError::new(format!(
"Expected int, got {:?}",
other
))),
}
}
fn expect_str(value: &JoinValue) -> Result<String, JoinRuntimeError> {
match value {
JoinValue::Str(s) => Ok(s.clone()),
other => Err(JoinRuntimeError::new(format!(
"Expected string, got {:?}",
other
))),
}
}