Phase 11-12: LLVM backend initial, semantics layer, plugin unification

Major changes:
- LLVM backend initial implementation (compiler.rs, llvm mode)
- Semantics layer integration in interpreter (operators.rs)
- Phase 12 plugin architecture revision (3-layer system)
- Builtin box removal preparation
- MIR instruction set documentation (26→Core-15 migration)
- Cross-backend testing infrastructure
- Await/nowait syntax support

New features:
- LLVM AOT compilation support (--backend llvm)
- Semantics layer for interpreter→VM flow
- Tri-backend smoke tests
- Plugin-only registry mode

Bug fixes:
- Interpreter plugin box arithmetic operations
- Branch test returns incorrect values

Documentation:
- Phase 12 README.md updated with new plugin architecture
- Removed obsolete NYIR proposals
- Added LLVM test programs documentation

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Moe Charm
2025-09-01 23:44:34 +09:00
parent fff9749f47
commit 11506cee3b
196 changed files with 10955 additions and 380 deletions

View File

@ -45,12 +45,32 @@ impl NyashRunner {
// Backend selection
match self.config.backend.as_str() {
"mir" => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("🚀 Nyash MIR Interpreter - Executing file: {} 🚀", filename);
}
self.execute_mir_interpreter_mode(filename);
}
"vm" => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("🚀 Nyash VM Backend - Executing file: {} 🚀", filename);
}
self.execute_vm_mode(filename);
}
"cranelift" => {
#[cfg(feature = "cranelift-jit")]
{
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("⚙️ Nyash Cranelift JIT - Executing file: {}", filename);
}
self.execute_cranelift_mode(filename);
}
#[cfg(not(feature = "cranelift-jit"))]
{
eprintln!("❌ Cranelift backend not available. Please rebuild with: cargo build --features cranelift-jit");
process::exit(1);
}
}
"llvm" => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
println!("⚡ Nyash LLVM Backend - Executing file: {}", filename);

View File

@ -0,0 +1,45 @@
use super::super::NyashRunner;
use nyash_rust::{parser::NyashParser, mir::MirCompiler};
use std::{fs, process};
impl NyashRunner {
/// Execute Cranelift JIT mode (skeleton)
pub(crate) fn execute_cranelift_mode(&self, filename: &str) {
// Read source
let code = match fs::read_to_string(filename) {
Ok(c) => c,
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
};
// Parse → AST
let ast = match NyashParser::parse_from_string(&code) {
Ok(ast) => ast,
Err(e) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
};
// AST → MIR
let mut mir_compiler = MirCompiler::new();
let compile_result = match mir_compiler.compile(ast) {
Ok(r) => r,
Err(e) => { eprintln!("❌ MIR compilation error: {}", e); process::exit(1); }
};
println!("📊 MIR Module compiled (Cranelift JIT skeleton)");
// Execute via Cranelift JIT (featuregated)
#[cfg(feature = "cranelift-jit")]
{
use nyash_rust::backend::cranelift_compile_and_execute;
match cranelift_compile_and_execute(&compile_result.module, "nyash_cljit_temp") {
Ok(result) => {
println!("✅ Cranelift JIT execution completed (skeleton)!");
println!("📊 Result: {}", result.to_string_box().value);
}
Err(e) => { eprintln!("❌ Cranelift JIT error: {}", e); process::exit(1); }
}
}
#[cfg(not(feature = "cranelift-jit"))]
{
eprintln!("❌ Cranelift JIT not available. Rebuild with --features cranelift-jit");
process::exit(1);
}
}
}

View File

@ -1,5 +1,6 @@
use super::super::NyashRunner;
use nyash_rust::{parser::NyashParser, mir::{MirCompiler, MirInstruction}, box_trait::IntegerBox};
use nyash_rust::mir::passes::method_id_inject::inject_method_ids;
use std::{fs, process};
impl NyashRunner {
@ -27,12 +28,82 @@ impl NyashRunner {
println!("📊 MIR Module compiled successfully!");
println!("📊 Functions: {}", compile_result.module.functions.len());
// Inject method_id for BoxCall/PluginInvoke where resolvable (by-id path)
#[allow(unused_mut)]
let mut module = compile_result.module.clone();
let injected = inject_method_ids(&mut module);
if injected > 0 && std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[LLVM] method_id injected: {} places", injected);
}
// If explicit object path is requested, emit object only
if let Ok(out_path) = std::env::var("NYASH_LLVM_OBJ_OUT") {
#[cfg(feature = "llvm")]
{
use nyash_rust::backend::llvm_compile_to_object;
// Ensure parent directory exists for the object file
if let Some(parent) = std::path::Path::new(&out_path).parent() {
let _ = std::fs::create_dir_all(parent);
}
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[Runner/LLVM] emitting object to {} (cwd={})", out_path, std::env::current_dir().map(|p| p.display().to_string()).unwrap_or_default());
}
if let Err(e) = llvm_compile_to_object(&module, &out_path) {
eprintln!("❌ LLVM object emit error: {}", e);
process::exit(1);
}
// Verify object presence and size (>0)
match std::fs::metadata(&out_path) {
Ok(meta) => {
if meta.len() == 0 {
eprintln!("❌ LLVM object is empty: {}", out_path);
process::exit(1);
}
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[LLVM] object emitted: {} ({} bytes)", out_path, meta.len());
}
}
Err(e) => {
// Try one immediate retry by writing through the compiler again (rare FS lag safeguards)
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[Runner/LLVM] object not found after emit, retrying once: {} ({})", out_path, e);
}
if let Err(e2) = llvm_compile_to_object(&module, &out_path) {
eprintln!("❌ LLVM object emit error (retry): {}", e2);
process::exit(1);
}
match std::fs::metadata(&out_path) {
Ok(meta2) => {
if meta2.len() == 0 {
eprintln!("❌ LLVM object is empty (after retry): {}", out_path);
process::exit(1);
}
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[LLVM] object emitted after retry: {} ({} bytes)", out_path, meta2.len());
}
}
Err(e3) => {
eprintln!("❌ LLVM object not found after emit (retry): {} ({})", out_path, e3);
process::exit(1);
}
}
}
}
return;
}
#[cfg(not(feature = "llvm"))]
{
eprintln!("❌ LLVM backend not available (object emit).");
process::exit(1);
}
}
// 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) {
match llvm_compile_and_execute(&module, temp_path) {
Ok(result) => {
if let Some(int_result) = result.as_any().downcast_ref::<IntegerBox>() {
let exit_code = int_result.value;
@ -51,7 +122,7 @@ impl NyashRunner {
{
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") {
if let Some(main_func) = module.functions.get("Main.main") {
for (_bid, block) in &main_func.blocks {
for inst in &block.instructions {
match inst {
@ -67,4 +138,3 @@ impl NyashRunner {
}
}
}

View File

@ -0,0 +1,104 @@
use super::super::NyashRunner;
use nyash_rust::{parser::NyashParser, mir::MirCompiler, backend::MirInterpreter, runtime::{NyashRuntime, NyashRuntimeBuilder}, interpreter::SharedState, box_factory::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) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
};
// 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 && std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
eprintln!("[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) => {
println!("✅ MIR interpreter execution completed!");
// Pretty-print using MIR return type when available
if let Some(func) = module_interp.functions.get("main") {
use nyash_rust::mir::MirType;
use nyash_rust::box_trait::{NyashBox, IntegerBox, BoolBox, StringBox};
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);
}
}
Err(e) => {
eprintln!("❌ MIR interpreter error: {}", e);
process::exit(1);
}
}
let _ = runtime; // reserved for future GC/safepoint integration
}
}

View File

@ -1,4 +1,5 @@
pub mod mir;
pub mod mir_interpreter;
pub mod vm;
pub mod llvm;
pub mod bench;
@ -7,3 +8,5 @@ pub mod common;
pub mod wasm;
#[cfg(feature = "cranelift-jit")]
pub mod aot;
#[cfg(feature = "cranelift-jit")]
pub mod cranelift;

View File

@ -64,6 +64,9 @@ impl NyashRunner {
}
}
// Expose GC/scheduler hooks globally for JIT externs (checkpoint/await, etc.)
nyash_rust::runtime::global_hooks::set_from_runtime(&runtime);
// Execute with VM using prepared runtime
let mut vm = VM::with_runtime(runtime);
match vm.execute_module(&module_vm) {