Phase 25.1 完了成果: - ✅ LoopForm v2 テスト・ドキュメント・コメント完備 - 4ケース(A/B/C/D)完全テストカバレッジ - 最小再現ケース作成(SSAバグ調査用) - SSOT文書作成(loopform_ssot.md) - 全ソースに [LoopForm] コメントタグ追加 - ✅ Stage-1 CLI デバッグ環境構築 - stage1_cli.hako 実装 - stage1_bridge.rs ブリッジ実装 - デバッグツール作成(stage1_debug.sh/stage1_minimal.sh) - アーキテクチャ改善提案文書 - ✅ 環境変数削減計画策定 - 25変数の完全調査・分類 - 6段階削減ロードマップ(25→5、80%削減) - 即時削除可能変数特定(NYASH_CONFIG/NYASH_DEBUG) Phase 26-D からの累積変更: - PHI実装改善(ExitPhiBuilder/HeaderPhiBuilder等) - MIRビルダーリファクタリング - 型伝播・最適化パス改善 - その他約300ファイルの累積変更 🎯 技術的成果: - SSAバグ根本原因特定(条件分岐内loop変数変更) - Region+next_iパターン適用完了(UsingCollectorBox等) - LoopFormパターン文書化・テスト化完了 - セルフホスティング基盤強化 Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <noreply@openai.com> Co-Authored-By: Task Assistant <task@anthropic.com>
126 lines
6.1 KiB
Rust
126 lines
6.1 KiB
Rust
use super::super::NyashRunner;
|
|
use nyash_rust::{parser::NyashParser, mir::MirCompiler, backend::MirInterpreter, runtime::{NyashRuntime, NyashRuntimeBuilder}, box_factory::{SharedState, user_defined::UserDefinedBoxFactory}};
|
|
use std::{fs, process};
|
|
use std::sync::Arc;
|
|
|
|
impl NyashRunner {
|
|
/// Execute MIR via lightweight interpreter backend
|
|
pub(crate) fn execute_mir_interpreter_mode(&self, filename: &str) {
|
|
// Read the file
|
|
let code = match fs::read_to_string(filename) {
|
|
Ok(content) => content,
|
|
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
|
|
};
|
|
|
|
// Parse to AST
|
|
let ast = match NyashParser::parse_from_string(&code) {
|
|
Ok(ast) => ast,
|
|
Err(e) => {
|
|
crate::runner::modes::common_util::diag::print_parse_error_with_context(
|
|
filename,
|
|
&code,
|
|
&e,
|
|
);
|
|
process::exit(1);
|
|
}
|
|
};
|
|
let ast = crate::r#macro::maybe_expand_and_dump(&ast, false);
|
|
|
|
// Prepare runtime and collect Box declarations for user-defined types
|
|
let runtime = {
|
|
let mut builder = NyashRuntimeBuilder::new();
|
|
if std::env::var("NYASH_GC_COUNTING").ok().as_deref() == Some("1") {
|
|
builder = builder.with_counting_gc();
|
|
}
|
|
let rt = builder.build();
|
|
self.collect_box_declarations(&ast, &rt);
|
|
// Register UserDefinedBoxFactory backed by the same declarations
|
|
let mut shared = SharedState::new();
|
|
shared.box_declarations = rt.box_declarations.clone();
|
|
let udf = Arc::new(UserDefinedBoxFactory::new(shared));
|
|
if let Ok(mut reg) = rt.box_registry.lock() { reg.register(udf); }
|
|
rt
|
|
};
|
|
|
|
// Compile to MIR (opt passes configurable)
|
|
let mut mir_compiler = MirCompiler::with_options(!self.config.no_optimize);
|
|
let compile_result = match mir_compiler.compile(ast) {
|
|
Ok(result) => result,
|
|
Err(e) => { eprintln!("❌ MIR compilation error: {}", e); process::exit(1); }
|
|
};
|
|
|
|
// Optional: VM-only escape analysis elides barriers; safe for interpreter too
|
|
let mut module_interp = compile_result.module.clone();
|
|
if std::env::var("NYASH_VM_ESCAPE_ANALYSIS").ok().as_deref() == Some("1") {
|
|
let removed = nyash_rust::mir::passes::escape::escape_elide_barriers_vm(&mut module_interp);
|
|
if removed > 0 { crate::cli_v!("[MIR-Interp] escape_elide_barriers: removed {} barriers", removed); }
|
|
}
|
|
|
|
// Execute with MIR interpreter
|
|
let mut interp = MirInterpreter::new();
|
|
match interp.execute_module(&module_interp) {
|
|
Ok(result) => {
|
|
use nyash_rust::box_trait::{NyashBox, IntegerBox, BoolBox, StringBox};
|
|
|
|
// Extract exit code from return value
|
|
let exit_code = if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
ib.value as i32
|
|
} else if let Some(bb) = result.as_any().downcast_ref::<BoolBox>() {
|
|
if bb.value { 1 } else { 0 }
|
|
} else {
|
|
// For non-integer/bool returns, default to 0 (success)
|
|
0
|
|
};
|
|
|
|
// Pretty-print using MIR return type when available (only in verbose mode)
|
|
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
|
|
println!("✅ MIR interpreter execution completed!");
|
|
if let Some(func) = module_interp.functions.get("main") {
|
|
use nyash_rust::mir::MirType;
|
|
use nyash_rust::boxes::FloatBox;
|
|
let (ety, sval) = match &func.signature.return_type {
|
|
MirType::Float => {
|
|
if let Some(fb) = result.as_any().downcast_ref::<FloatBox>() {
|
|
("Float", format!("{}", fb.value))
|
|
} else if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
("Float", format!("{}", ib.value as f64))
|
|
} else { ("Float", result.to_string_box().value) }
|
|
}
|
|
MirType::Integer => {
|
|
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
("Integer", ib.value.to_string())
|
|
} else { ("Integer", result.to_string_box().value) }
|
|
}
|
|
MirType::Bool => {
|
|
if let Some(bb) = result.as_any().downcast_ref::<BoolBox>() {
|
|
("Bool", bb.value.to_string())
|
|
} else if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
("Bool", (ib.value != 0).to_string())
|
|
} else { ("Bool", result.to_string_box().value) }
|
|
}
|
|
MirType::String => {
|
|
if let Some(sb) = result.as_any().downcast_ref::<StringBox>() {
|
|
("String", sb.value.clone())
|
|
} else { ("String", result.to_string_box().value) }
|
|
}
|
|
_ => { (result.type_name(), result.to_string_box().value) }
|
|
};
|
|
println!("ResultType(MIR): {}", ety);
|
|
println!("Result: {}", sval);
|
|
} else {
|
|
println!("Result: {:?}", result);
|
|
}
|
|
}
|
|
|
|
// Exit with the return value as exit code
|
|
process::exit(exit_code);
|
|
}
|
|
Err(e) => {
|
|
eprintln!("❌ MIR interpreter error: {}", e);
|
|
process::exit(1);
|
|
}
|
|
}
|
|
let _ = runtime; // reserved for future GC/safepoint integration
|
|
}
|
|
}
|