runner: split modes (mir/vm/llvm/bench) and extract plugin init; interpreter: split objects into ops/methods/fields; VM logs gated; Phi selection minimal fix; CURRENT_TASK updated; remove legacy backups
This commit is contained in:
50
src/runner/modes/bench.rs
Normal file
50
src/runner/modes/bench.rs
Normal file
@ -0,0 +1,50 @@
|
||||
use super::super::NyashRunner;
|
||||
use nyash_rust::{parser::NyashParser, interpreter::NyashInterpreter, box_factory::builtin::BuiltinGroups, mir::MirCompiler, backend::VM};
|
||||
|
||||
impl NyashRunner {
|
||||
/// Execute benchmark mode (split)
|
||||
pub(crate) fn execute_benchmark_mode(&self) {
|
||||
println!("🏁 Running benchmark mode with {} iterations", self.config.iterations);
|
||||
let test_code = r#"
|
||||
local x
|
||||
x = 42
|
||||
local y
|
||||
y = x + 58
|
||||
return y
|
||||
"#;
|
||||
|
||||
println!("\n🧪 Test code:\n{}", test_code);
|
||||
|
||||
// Interpreter
|
||||
println!("\n⚡ Interpreter Backend:");
|
||||
let start = std::time::Instant::now();
|
||||
for _ in 0..self.config.iterations {
|
||||
if let Ok(ast) = NyashParser::parse_from_string(test_code) {
|
||||
let mut interp = NyashInterpreter::new_with_groups(BuiltinGroups::native_full());
|
||||
let _ = interp.execute(ast);
|
||||
}
|
||||
}
|
||||
let interpreter_time = start.elapsed();
|
||||
println!(" {} iterations in {:?} ({:.2} ops/sec)", self.config.iterations, interpreter_time, self.config.iterations as f64 / interpreter_time.as_secs_f64());
|
||||
|
||||
// VM
|
||||
println!("\n🚀 VM Backend:");
|
||||
let start = std::time::Instant::now();
|
||||
for _ in 0..self.config.iterations {
|
||||
if let Ok(ast) = NyashParser::parse_from_string(test_code) {
|
||||
let mut mc = MirCompiler::new();
|
||||
if let Ok(cr) = mc.compile(ast) {
|
||||
let mut vm = VM::new();
|
||||
let _ = vm.execute_module(&cr.module);
|
||||
}
|
||||
}
|
||||
}
|
||||
let vm_time = start.elapsed();
|
||||
println!(" {} iterations in {:?} ({:.2} ops/sec)", self.config.iterations, vm_time, self.config.iterations as f64 / vm_time.as_secs_f64());
|
||||
|
||||
// Summary
|
||||
let speedup = interpreter_time.as_secs_f64() / vm_time.as_secs_f64();
|
||||
println!("\n📊 Performance Summary:\n VM is {:.2}x {} than Interpreter", if speedup > 1.0 { speedup } else { 1.0 / speedup }, if speedup > 1.0 { "faster" } else { "slower" });
|
||||
}
|
||||
}
|
||||
|
||||
70
src/runner/modes/llvm.rs
Normal file
70
src/runner/modes/llvm.rs
Normal file
@ -0,0 +1,70 @@
|
||||
use super::super::NyashRunner;
|
||||
use nyash_rust::{parser::NyashParser, mir::{MirCompiler, MirInstruction}, box_trait::IntegerBox};
|
||||
use std::{fs, process};
|
||||
|
||||
impl NyashRunner {
|
||||
/// Execute LLVM mode (split)
|
||||
pub(crate) fn execute_llvm_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) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
|
||||
};
|
||||
|
||||
// Compile to MIR
|
||||
let mut mir_compiler = MirCompiler::new();
|
||||
let compile_result = match mir_compiler.compile(ast) {
|
||||
Ok(result) => result,
|
||||
Err(e) => { eprintln!("❌ MIR compilation error: {}", e); process::exit(1); }
|
||||
};
|
||||
|
||||
println!("📊 MIR Module compiled successfully!");
|
||||
println!("📊 Functions: {}", compile_result.module.functions.len());
|
||||
|
||||
// Execute via LLVM backend (mock or real)
|
||||
#[cfg(feature = "llvm")]
|
||||
{
|
||||
use nyash_rust::backend::llvm_compile_and_execute;
|
||||
let temp_path = "nyash_llvm_temp";
|
||||
match llvm_compile_and_execute(&compile_result.module, temp_path) {
|
||||
Ok(result) => {
|
||||
if let Some(int_result) = result.as_any().downcast_ref::<IntegerBox>() {
|
||||
let exit_code = int_result.value;
|
||||
println!("✅ LLVM execution completed!");
|
||||
println!("📊 Exit code: {}", exit_code);
|
||||
process::exit(exit_code as i32);
|
||||
} else {
|
||||
println!("✅ LLVM execution completed (non-integer result)!");
|
||||
println!("📊 Result: {}", result.to_string_box().value);
|
||||
}
|
||||
},
|
||||
Err(e) => { eprintln!("❌ LLVM execution error: {}", e); process::exit(1); }
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "llvm"))]
|
||||
{
|
||||
println!("🔧 Mock LLVM Backend Execution:");
|
||||
println!(" Build with --features llvm for real compilation.");
|
||||
if let Some(main_func) = compile_result.module.functions.get("Main.main") {
|
||||
for (_bid, block) in &main_func.blocks {
|
||||
for inst in &block.instructions {
|
||||
match inst {
|
||||
MirInstruction::Return { value: Some(_) } => { println!("✅ Mock exit code: 42"); process::exit(42); }
|
||||
MirInstruction::Return { value: None } => { println!("✅ Mock exit code: 0"); process::exit(0); }
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("✅ Mock exit code: 0");
|
||||
process::exit(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
49
src/runner/modes/mir.rs
Normal file
49
src/runner/modes/mir.rs
Normal file
@ -0,0 +1,49 @@
|
||||
use super::super::NyashRunner;
|
||||
use nyash_rust::{parser::NyashParser, mir::{MirCompiler, MirPrinter}};
|
||||
use std::{fs, process};
|
||||
|
||||
impl NyashRunner {
|
||||
/// Execute MIR compilation and processing mode (split)
|
||||
pub(crate) fn execute_mir_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) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
|
||||
};
|
||||
|
||||
// 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); }
|
||||
};
|
||||
|
||||
// Verify MIR if requested
|
||||
if self.config.verify_mir {
|
||||
println!("🔍 Verifying MIR...");
|
||||
match &compile_result.verification_result {
|
||||
Ok(()) => println!("✅ MIR verification passed!"),
|
||||
Err(errors) => {
|
||||
eprintln!("❌ MIR verification failed:");
|
||||
for error in errors { eprintln!(" • {}", error); }
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dump MIR if requested
|
||||
if self.config.dump_mir {
|
||||
let mut printer = if self.config.mir_verbose { MirPrinter::verbose() } else { MirPrinter::new() };
|
||||
if self.config.mir_verbose_effects { printer.set_show_effects_inline(true); }
|
||||
println!("🚀 MIR Output for {}:", filename);
|
||||
println!("{}", printer.print_module(&compile_result.module));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
6
src/runner/modes/mod.rs
Normal file
6
src/runner/modes/mod.rs
Normal file
@ -0,0 +1,6 @@
|
||||
pub mod mir;
|
||||
pub mod vm;
|
||||
pub mod llvm;
|
||||
pub mod bench;
|
||||
// WASM/AOT modes remain in runner.rs for now (feature-gated)
|
||||
|
||||
84
src/runner/modes/vm.rs
Normal file
84
src/runner/modes/vm.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use super::super::NyashRunner;
|
||||
use nyash_rust::{parser::NyashParser, mir::MirCompiler, backend::VM, runtime::{NyashRuntime, NyashRuntimeBuilder}, ast::ASTNode, core::model::BoxDeclaration as CoreBoxDecl, interpreter::SharedState, box_factory::{builtin::BuiltinGroups, user_defined::UserDefinedBoxFactory}};
|
||||
use std::{fs, process};
|
||||
use std::sync::Arc;
|
||||
|
||||
impl NyashRunner {
|
||||
/// Execute VM mode (split)
|
||||
pub(crate) fn execute_vm_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) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
|
||||
};
|
||||
|
||||
// Prepare runtime and collect Box declarations for VM user-defined types
|
||||
let runtime = {
|
||||
let rt = NyashRuntimeBuilder::new()
|
||||
.with_builtin_groups(BuiltinGroups::native_full())
|
||||
.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); }
|
||||
};
|
||||
|
||||
// Execute with VM using prepared runtime
|
||||
let mut vm = VM::with_runtime(runtime);
|
||||
match vm.execute_module(&compile_result.module) {
|
||||
Ok(result) => {
|
||||
println!("✅ VM execution completed successfully!");
|
||||
println!("Result: {:?}", result);
|
||||
},
|
||||
Err(e) => { eprintln!("❌ VM execution error: {}", e); process::exit(1); }
|
||||
}
|
||||
}
|
||||
|
||||
/// Collect Box declarations from AST and register into runtime
|
||||
pub(crate) fn collect_box_declarations(&self, ast: &ASTNode, runtime: &NyashRuntime) {
|
||||
fn walk(node: &ASTNode, runtime: &NyashRuntime) {
|
||||
match node {
|
||||
ASTNode::Program { statements, .. } => { for st in statements { walk(st, runtime); } }
|
||||
ASTNode::FunctionDeclaration { body, .. } => { for st in body { walk(st, runtime); } }
|
||||
ASTNode::BoxDeclaration { name, fields, public_fields, private_fields, methods, constructors, init_fields, weak_fields, is_interface, extends, implements, type_parameters, .. } => {
|
||||
for (_mname, mnode) in methods { walk(mnode, runtime); }
|
||||
for (_ckey, cnode) in constructors { walk(cnode, runtime); }
|
||||
let decl = CoreBoxDecl {
|
||||
name: name.clone(),
|
||||
fields: fields.clone(),
|
||||
public_fields: public_fields.clone(),
|
||||
private_fields: private_fields.clone(),
|
||||
methods: methods.clone(),
|
||||
constructors: constructors.clone(),
|
||||
init_fields: init_fields.clone(),
|
||||
weak_fields: weak_fields.clone(),
|
||||
is_interface: *is_interface,
|
||||
extends: extends.clone(),
|
||||
implements: implements.clone(),
|
||||
type_parameters: type_parameters.clone(),
|
||||
};
|
||||
if let Ok(mut map) = runtime.box_declarations.write() { map.insert(name.clone(), decl); }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
walk(ast, runtime);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user