refactor(llvm): Phase 286 - Modularize llvm.rs into 10 boxes

Following Phase 33 success pattern:
- Single responsibility per box
- Testability through isolated boxes
- Reusability across backends

Before: 449 lines monolithic function
After: 229 line orchestrator + 10 focused boxes

Boxes created:
- plugin_init.rs (plugin initialization)
- using_resolver.rs (using/prelude handling)
- mir_compiler.rs (AST → MIR compilation)
- method_id_injector.rs (method_id injection)
- joinir_experiment.rs (JoinIR experiment, feature-gated)
- pyvm_executor.rs (PyVM harness, dev/test)
- object_emitter.rs (LLVM object emit)
- harness_executor.rs (LLVM harness execution)
- exit_reporter.rs (leak report, Phase 285LLVM-0)
- fallback_executor.rs (mock/legacy execution)

Test Results:
-  cargo build --release
-  cargo build --release --features llvm
-  18 VM/LLVM parity tests PASS
-  No regressions

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-24 10:45:17 +09:00
parent 6b64e6415c
commit 13545fd57e
12 changed files with 766 additions and 449 deletions

View File

@ -1,449 +0,0 @@
use super::super::NyashRunner;
use nyash_rust::mir::passes::method_id_inject::inject_method_ids;
use nyash_rust::{
mir::{MirCompiler, MirInstruction},
parser::NyashParser,
};
use std::{fs, process};
impl NyashRunner {
/// Execute LLVM mode (split)
pub(crate) fn execute_llvm_mode(&self, filename: &str) {
// Initialize plugin host so method_id injection can resolve plugin calls
crate::runner_plugin_init::init_bid_plugins();
// Friendly plugin guard (nonstrict): unify diagnostics across modes
crate::runner::modes::common_util::plugin_guard::check_and_report(
false,
crate::config::env::env_bool("NYASH_JSON_ONLY"),
"llvm",
);
// Read the file
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => {
crate::console_println!("❌ Error reading file {}: {}", filename, e);
process::exit(1);
}
};
// Using handling (AST prelude merge like common/vm paths)
let use_ast = crate::config::env::using_ast_enabled();
let mut code_ref: &str = &code;
let cleaned_code_owned;
let mut prelude_asts: Vec<nyash_rust::ast::ASTNode> = Vec::new();
if crate::config::env::enable_using() {
match crate::runner::modes::common_util::resolve::resolve_prelude_paths_profiled(
self, &code, filename,
) {
Ok((clean, paths)) => {
cleaned_code_owned = clean;
code_ref = &cleaned_code_owned;
if !paths.is_empty() && !use_ast {
crate::console_println!("❌ using: AST prelude merge is disabled in this profile. Enable NYASH_USING_AST=1 or remove 'using' lines.");
std::process::exit(1);
}
if use_ast && !paths.is_empty() {
match crate::runner::modes::common_util::resolve::parse_preludes_to_asts(
self, &paths,
) {
Ok(v) => prelude_asts = v,
Err(e) => {
crate::console_println!("❌ {}", e);
std::process::exit(1);
}
}
}
}
Err(e) => {
crate::console_println!("❌ {}", e);
process::exit(1);
}
}
}
// Pre-expand '@name[:T] = expr' sugar at line-head (same as common path)
let preexpanded_owned =
crate::runner::modes::common_util::resolve::preexpand_at_local(code_ref);
code_ref = &preexpanded_owned;
// Parse to AST (main)
let main_ast = match NyashParser::parse_from_string(code_ref) {
Ok(ast) => ast,
Err(e) => {
crate::runner::modes::common_util::diag::print_parse_error_with_context(
filename, code_ref, &e,
);
// Enhanced context: list merged prelude files if any (from text-merge path)
let preludes =
crate::runner::modes::common_util::resolve::clone_last_merged_preludes();
if !preludes.is_empty() {
crate::runtime::get_global_ring0().log.debug(&format!(
"[parse/context] merged prelude files ({}):",
preludes.len()
));
let show = std::cmp::min(16, preludes.len());
for p in preludes.iter().take(show) {
crate::runtime::get_global_ring0()
.log
.debug(&format!(" - {}", p));
}
if preludes.len() > show {
crate::runtime::get_global_ring0()
.log
.debug(&format!(" ... ({} more)", preludes.len() - show));
}
}
process::exit(1);
}
};
// Merge preludes + main when enabled
let ast = if use_ast && !prelude_asts.is_empty() {
crate::runner::modes::common_util::resolve::merge_prelude_asts_with_main(
prelude_asts,
&main_ast,
)
} else {
main_ast
};
// Macro expansion (env-gated) after merge
let ast = crate::r#macro::maybe_expand_and_dump(&ast, false);
let ast = crate::runner::modes::macro_child::normalize_core_pass(&ast);
// Compile to MIR
let mut mir_compiler = MirCompiler::new();
let compile_result =
match crate::runner::modes::common_util::source_hint::compile_with_source_hint(
&mut mir_compiler,
ast,
Some(filename),
) {
Ok(result) => result,
Err(e) => {
crate::console_println!("❌ MIR compilation error: {}", e);
process::exit(1);
}
};
crate::console_println!("📊 MIR Module compiled successfully!");
crate::console_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 {
crate::cli_v!("[LLVM] method_id injected: {} places", injected);
}
// Phase 32 L-4.3a: JoinIR LLVM experiment hook
// When NYASH_JOINIR_EXPERIMENT=1 and NYASH_JOINIR_LLVM_EXPERIMENT=1,
// try to lower MIR → JoinIR → MIR' for Main.skip/1 to fix PHI issues.
// JoinIR-converted functions are merged back into the original module.
#[cfg(feature = "llvm-harness")]
let module = if crate::config::env::joinir_experiment_enabled()
&& crate::config::env::joinir_llvm_experiment_enabled()
&& crate::config::env::llvm_use_harness()
{
use nyash_rust::mir::join_ir::lower_skip_ws_to_joinir;
use nyash_rust::mir::join_ir_vm_bridge::bridge_joinir_to_mir;
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Attempting JoinIR path for LLVM execution");
// Try to lower Main.skip/1 to JoinIR
if module.functions.contains_key("Main.skip/1") {
match lower_skip_ws_to_joinir(&module) {
Some(join_module) => {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ✅ Lowered to JoinIR ({} functions)",
join_module.functions.len()
));
// Convert JoinIR back to MIR' (with normalized PHI)
match bridge_joinir_to_mir(&join_module) {
Ok(mir_from_joinir) => {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ✅ Converted to MIR' ({} functions)",
mir_from_joinir.functions.len()
));
// Merge JoinIR functions into original module
// Strategy: Remove Main.skip/1 (PHI-problematic) and rename join_func_0 to Main.skip/1
let mut merged = module.clone();
// Remove the original PHI-problematic Main.skip/1
if merged.functions.remove("Main.skip/1").is_some() {
crate::runtime::get_global_ring0().log.debug("[joinir/llvm] Removed original Main.skip/1 (PHI-problematic)");
}
for (name, func) in mir_from_joinir.functions {
// Rename join_func_0 → Main.skip/1 to maintain call compatibility
let target_name = if name == "join_func_0" {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] Renaming {} → Main.skip/1",
name
));
"Main.skip/1".to_string()
} else {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] Adding JoinIR function: {}",
name
));
name
};
merged.functions.insert(target_name, func);
}
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ✅ Merged module ({} functions)",
merged.functions.len()
));
merged
}
Err(e) => {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ❌ JoinIR→MIR conversion failed: {:?}",
e
));
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Falling back to original MIR");
module
}
}
}
None => {
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] ❌ JoinIR lowering returned None");
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Falling back to original MIR");
module
}
}
} else {
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Main.skip/1 not found, using original MIR");
module
}
} else {
module
};
// Dev/Test helper: allow executing via PyVM harness when requested
if std::env::var("SMOKES_USE_PYVM").ok().as_deref() == Some("1") {
match super::common_util::pyvm::run_pyvm_harness_lib(&module, "llvm-ast") {
Ok(code) => {
std::process::exit(code);
}
Err(e) => {
crate::console_println!("❌ PyVM harness error: {}", e);
std::process::exit(1);
}
}
}
// If explicit object path is requested, emit object only
if let Ok(_out_path) = std::env::var("NYASH_LLVM_OBJ_OUT") {
#[cfg(feature = "llvm-harness")]
{
// Harness path (optional): if NYASH_LLVM_USE_HARNESS=1, try Python/llvmlite first.
if crate::config::env::llvm_use_harness() {
if let Err(e) = crate::runner::modes::common_util::exec::llvmlite_emit_object(
&module, &_out_path, 20_000,
) {
crate::console_println!("❌ {}", e);
process::exit(1);
}
return;
}
// Verify object presence and size (>0)
match std::fs::metadata(&_out_path) {
Ok(meta) => {
if meta.len() == 0 {
crate::console_println!("❌ harness object is empty: {}", _out_path);
process::exit(1);
}
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
crate::console_println!(
"[LLVM] object emitted: {} ({} bytes)",
_out_path,
meta.len()
);
}
}
Err(e) => {
crate::console_println!(
"❌ harness output not found after emit: {} ({})",
_out_path,
e
);
process::exit(1);
}
}
return;
}
#[cfg(all(not(feature = "llvm-harness"), feature = "llvm-inkwell-legacy"))]
{
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);
}
crate::cli_v!(
"[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) {
crate::console_println!("❌ LLVM object emit error: {}", e);
process::exit(1);
}
match std::fs::metadata(&_out_path) {
Ok(meta) if meta.len() > 0 => {
crate::cli_v!(
"[LLVM] object emitted: {} ({} bytes)",
_out_path,
meta.len()
);
}
_ => {
crate::console_println!("❌ LLVM object not found or empty: {}", _out_path);
process::exit(1);
}
}
return;
}
#[cfg(all(not(feature = "llvm-harness"), not(feature = "llvm-inkwell-legacy")))]
{
crate::console_println!("❌ LLVM backend not available (object emit).");
process::exit(1);
}
}
// Execute via LLVM backend (harness preferred)
#[cfg(feature = "llvm-harness")]
{
if crate::config::env::llvm_use_harness() {
// Prefer producing a native executable via ny-llvmc, then execute it
let exe_out = "tmp/nyash_llvm_run";
let libs = std::env::var("NYASH_LLVM_EXE_LIBS").ok();
match crate::runner::modes::common_util::exec::ny_llvmc_emit_exe_lib(
&module,
exe_out,
None,
libs.as_deref(),
) {
Ok(()) => {
match crate::runner::modes::common_util::exec::run_executable(
exe_out,
&[],
20_000,
) {
Ok((code, _timed_out, stdout_text)) => {
// Forward program stdout so parity tests can compare outputs
if !stdout_text.is_empty() {
print!("{}", stdout_text);
}
crate::console_println!(
"✅ LLVM (harness) execution completed (exit={})",
code
);
// Phase 285LLVM-0: Emit Rust-side leak report before exit (if enabled)
// Note: Only reports Rust VM-side roots (modules, host_handles, plugin_boxes).
crate::runtime::leak_tracker::emit_leak_report();
std::process::exit(code);
}
Err(e) => {
crate::console_println!("❌ run executable error: {}", e);
// Phase 285LLVM-0: Emit leak report even on error (for consistency)
crate::runtime::leak_tracker::emit_leak_report();
std::process::exit(1);
}
}
}
Err(e) => {
crate::console_println!("❌ ny-llvmc emit-exe error: {}", e);
crate::console_println!(
" Hint: build ny-llvmc: cargo build -p nyash-llvm-compiler --release"
);
std::process::exit(1);
}
}
}
}
// Execute via LLVM backend (mock or real)
#[cfg(feature = "llvm-inkwell-legacy")]
{
use nyash_rust::backend::llvm_compile_and_execute;
let temp_path = "nyash_llvm_temp";
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;
crate::console_println!("✅ LLVM execution completed!");
crate::console_println!("📊 Exit code: {}", exit_code);
process::exit(exit_code as i32);
} else {
crate::console_println!(
"✅ LLVM execution completed (non-integer result)!"
);
crate::console_println!("📊 Result: {}", result.to_string_box().value);
}
}
Err(e) => {
crate::console_println!("❌ LLVM execution error: {}", e);
process::exit(1);
}
}
}
#[cfg(all(not(feature = "llvm-inkwell-legacy")))]
{
// Fail-fast: if the user explicitly requested the llvmlite harness but this binary
// was built without the `llvm-harness` feature, do not silently fall back to mock.
if crate::config::env::env_bool("NYASH_LLVM_USE_HARNESS") {
crate::console_println!(
"❌ LLVM harness requested (NYASH_LLVM_USE_HARNESS=1), but this binary was built without `--features llvm` (llvm-harness)."
);
crate::console_println!(
" Fix: cargo build --release --features llvm"
);
process::exit(1);
}
crate::console_println!("🔧 Mock LLVM Backend Execution:");
crate::console_println!(" Build with --features llvm-inkwell-legacy for Rust/inkwell backend, or set NYASH_LLVM_OBJ_OUT and NYASH_LLVM_USE_HARNESS=1 for harness.");
// NamingBox SSOT: Select entry (arity-aware, Main.main → main fallback)
let entry =
crate::runner::modes::common_util::entry_selection::select_entry_function(&module);
if let Some(main_func) = module.functions.get(&entry) {
for (_bid, block) in &main_func.blocks {
for inst in &block.instructions {
match inst {
MirInstruction::Return { value: Some(_) } => {
crate::console_println!("✅ Mock exit code: 42");
process::exit(42);
}
MirInstruction::Return { value: None } => {
crate::console_println!("✅ Mock exit code: 0");
process::exit(0);
}
_ => {}
}
}
}
}
crate::console_println!("✅ Mock exit code: 0");
process::exit(0);
}
}
}
// emit_mir_json_for_harness moved to crate::runner::mir_json_emit

View File

@ -0,0 +1,26 @@
//! Exit reporter for LLVM mode (Phase 285LLVM-0)
//!
//! Handles leak report emission and process exit.
/// Exit reporter Box
///
/// **Responsibility**: Emit leak report and exit process
/// **Input**: exit code (i32)
/// **Output**: ! (never returns)
pub struct ExitReporterBox;
impl ExitReporterBox {
/// Emit leak report and exit process
///
/// Phase 285LLVM-0: Emit Rust-side leak report before exit (if enabled).
/// Note: Only reports Rust VM-side roots (modules, host_handles, plugin_boxes).
///
/// This function never returns.
pub fn emit_and_exit(code: i32) -> ! {
// Phase 285LLVM-0: Emit Rust-side leak report before exit (if enabled)
// Note: Only reports Rust VM-side roots (modules, host_handles, plugin_boxes).
crate::runtime::leak_tracker::emit_leak_report();
std::process::exit(code);
}
}

View File

@ -0,0 +1,64 @@
//! Fallback executor for LLVM mode (mock/legacy)
//!
//! Handles fallback execution when LLVM backends are not available.
use nyash_rust::{mir::MirModule, mir::MirInstruction};
/// Fallback executor Box
///
/// **Responsibility**: Execute fallback path (feature check + mock)
/// **Input**: &MirModule
/// **Output**: i32 (exit code)
pub struct FallbackExecutorBox;
impl FallbackExecutorBox {
/// Execute fallback path (feature check + mock)
///
/// Fail-fast: if the user explicitly requested the llvmlite harness
/// but this binary was built without the `llvm-harness` feature,
/// do not silently fall back to mock.
///
/// Otherwise, executes mock execution that inspects the MIR
/// and returns a deterministic exit code based on Return instructions.
pub fn execute(module: &MirModule) -> i32 {
// Fail-fast: if the user explicitly requested the llvmlite harness
// but this binary was built without the `llvm-harness` feature,
// do not silently fall back to mock.
if crate::config::env::env_bool("NYASH_LLVM_USE_HARNESS") {
crate::console_println!(
"❌ LLVM harness requested (NYASH_LLVM_USE_HARNESS=1), but this binary was built without `--features llvm` (llvm-harness)."
);
crate::console_println!(
" Fix: cargo build --release --features llvm"
);
return 1;
}
crate::console_println!("🔧 Mock LLVM Backend Execution:");
crate::console_println!(" Build with --features llvm for real backend.");
// NamingBox SSOT: Select entry (arity-aware, Main.main → main fallback)
let entry = crate::runner::modes::common_util::entry_selection::select_entry_function(module);
if let Some(main_func) = module.functions.get(&entry) {
for (_bid, block) in &main_func.blocks {
for inst in &block.instructions {
match inst {
MirInstruction::Return { value: Some(_) } => {
crate::console_println!("✅ Mock exit code: 42");
return 42;
}
MirInstruction::Return { value: None } => {
crate::console_println!("✅ Mock exit code: 0");
return 0;
}
_ => {}
}
}
}
}
crate::console_println!("✅ Mock exit code: 0");
0
}
}

View File

@ -0,0 +1,77 @@
//! LLVM harness executor (native executable generation and execution)
//!
//! Handles execution via LLVM harness when available (feature-gated).
use nyash_rust::mir::MirModule;
/// Harness executor Box
///
/// **Responsibility**: Execute via LLVM harness (native executable generation and execution)
/// **Input**: &MirModule
/// **Output**: Option<i32> (Some(exit_code) if executed, None otherwise)
pub struct HarnessExecutorBox;
impl HarnessExecutorBox {
/// Execute via LLVM harness if available
///
/// This function:
/// 1. Generates a native executable via ny-llvmc
/// 2. Executes the generated executable
/// 3. Returns the exit code
///
/// Returns Some(exit_code) if executed, None if harness not available.
#[cfg(feature = "llvm-harness")]
pub fn try_execute(module: &MirModule) -> Option<i32> {
if !crate::config::env::llvm_use_harness() {
return None;
}
// Prefer producing a native executable via ny-llvmc, then execute it
let exe_out = "tmp/nyash_llvm_run";
let libs = std::env::var("NYASH_LLVM_EXE_LIBS").ok();
match crate::runner::modes::common_util::exec::ny_llvmc_emit_exe_lib(
module,
exe_out,
None,
libs.as_deref(),
) {
Ok(()) => {
match crate::runner::modes::common_util::exec::run_executable(
exe_out,
&[],
20_000,
) {
Ok((code, _timed_out, stdout_text)) => {
// Forward program stdout so parity tests can compare outputs
if !stdout_text.is_empty() {
print!("{}", stdout_text);
}
crate::console_println!(
"✅ LLVM (harness) execution completed (exit={})",
code
);
Some(code)
}
Err(e) => {
crate::console_println!("❌ run executable error: {}", e);
Some(1)
}
}
}
Err(e) => {
crate::console_println!("❌ ny-llvmc emit-exe error: {}", e);
crate::console_println!(
" Hint: build ny-llvmc: cargo build -p nyash-llvm-compiler --release"
);
Some(1)
}
}
}
#[cfg(not(feature = "llvm-harness"))]
pub fn try_execute(_module: &MirModule) -> Option<i32> {
None
}
}

View File

@ -0,0 +1,117 @@
//! JoinIR experiment for LLVM mode (Phase 32 L-4.3a)
//!
//! Handles JoinIR lowering experiment for fixing PHI issues (feature-gated).
use nyash_rust::mir::MirModule;
/// JoinIR experiment Box
///
/// **Responsibility**: Apply JoinIR lowering experiment when enabled (feature-gated)
/// **Input**: MirModule
/// **Output**: MirModule (converted if enabled, original otherwise)
pub struct JoinIrExperimentBox;
impl JoinIrExperimentBox {
/// Apply JoinIR experiment if enabled
///
/// Phase 32 L-4.3a: When NYASH_JOINIR_EXPERIMENT=1 and NYASH_JOINIR_LLVM_EXPERIMENT=1,
/// try to lower MIR → JoinIR → MIR' for Main.skip/1 to fix PHI issues.
/// JoinIR-converted functions are merged back into the original module.
#[cfg(feature = "llvm-harness")]
pub fn apply(module: MirModule) -> MirModule {
if !crate::config::env::joinir_experiment_enabled()
|| !crate::config::env::joinir_llvm_experiment_enabled()
|| !crate::config::env::llvm_use_harness()
{
return module;
}
use nyash_rust::mir::join_ir::lower_skip_ws_to_joinir;
use nyash_rust::mir::join_ir_vm_bridge::bridge_joinir_to_mir;
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Attempting JoinIR path for LLVM execution");
// Try to lower Main.skip/1 to JoinIR
if module.functions.contains_key("Main.skip/1") {
match lower_skip_ws_to_joinir(&module) {
Some(join_module) => {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ✅ Lowered to JoinIR ({} functions)",
join_module.functions.len()
));
// Convert JoinIR back to MIR' (with normalized PHI)
match bridge_joinir_to_mir(&join_module) {
Ok(mir_from_joinir) => {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ✅ Converted to MIR' ({} functions)",
mir_from_joinir.functions.len()
));
// Merge JoinIR functions into original module
// Strategy: Remove Main.skip/1 (PHI-problematic) and rename join_func_0 to Main.skip/1
let mut merged = module.clone();
// Remove the original PHI-problematic Main.skip/1
if merged.functions.remove("Main.skip/1").is_some() {
crate::runtime::get_global_ring0().log.debug("[joinir/llvm] Removed original Main.skip/1 (PHI-problematic)");
}
for (name, func) in mir_from_joinir.functions {
// Rename join_func_0 → Main.skip/1 to maintain call compatibility
let target_name = if name == "join_func_0" {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] Renaming {} → Main.skip/1",
name
));
"Main.skip/1".to_string()
} else {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] Adding JoinIR function: {}",
name
));
name
};
merged.functions.insert(target_name, func);
}
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ✅ Merged module ({} functions)",
merged.functions.len()
));
merged
}
Err(e) => {
crate::runtime::get_global_ring0().log.debug(&format!(
"[joinir/llvm] ❌ JoinIR→MIR conversion failed: {:?}",
e
));
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Falling back to original MIR");
module
}
}
}
None => {
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] ❌ JoinIR lowering returned None");
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Falling back to original MIR");
module
}
}
} else {
crate::runtime::get_global_ring0()
.log
.debug("[joinir/llvm] Main.skip/1 not found, using original MIR");
module
}
}
#[cfg(not(feature = "llvm-harness"))]
pub fn apply(module: MirModule) -> MirModule {
module
}
}

View File

@ -0,0 +1,26 @@
//! Method ID injection for LLVM mode
//!
//! Handles method_id injection for BoxCall/PluginInvoke instructions.
use nyash_rust::mir::MirModule;
/// Method ID injection Box
///
/// **Responsibility**: Inject method_id for BoxCall/PluginInvoke where resolvable
/// **Input**: &mut MirModule
/// **Output**: usize (number of injection sites)
pub struct MethodIdInjectorBox;
impl MethodIdInjectorBox {
/// Inject method_id for BoxCall/PluginInvoke instructions
///
/// This function resolves plugin calls and injects method_id where possible.
/// Returns the number of injection sites.
pub fn inject(module: &mut MirModule) -> usize {
let injected = crate::mir::passes::method_id_inject::inject_method_ids(module);
if injected > 0 {
crate::cli_v!("[LLVM] method_id injected: {} places", injected);
}
injected
}
}

View File

@ -0,0 +1,33 @@
//! MIR compilation for LLVM mode
//!
//! Handles AST → MIR compilation.
use nyash_rust::{ast::ASTNode, mir::MirCompiler, mir::MirModule};
/// MIR compiler Box
///
/// **Responsibility**: Compile AST to MIR
/// **Input**: ast, filename
/// **Output**: Result<MirModule, String>
pub struct MirCompilerBox;
impl MirCompilerBox {
/// Compile AST to MIR
///
/// This function compiles the AST to MIR using source hint for better error messages.
pub fn compile(ast: ASTNode, filename: Option<&str>) -> Result<MirModule, String> {
let mut mir_compiler = MirCompiler::new();
let compile_result =
crate::runner::modes::common_util::source_hint::compile_with_source_hint(
&mut mir_compiler,
ast,
filename,
).map_err(|e| format!("MIR compilation error: {}", e))?;
crate::console_println!("📊 MIR Module compiled successfully!");
crate::console_println!("📊 Functions: {}", compile_result.module.functions.len());
Ok(compile_result.module)
}
}

View File

@ -0,0 +1,225 @@
use super::super::NyashRunner;
use nyash_rust::parser::NyashParser;
use std::fs;
// Modularized boxes for LLVM mode
mod plugin_init;
mod exit_reporter;
mod method_id_injector;
mod using_resolver;
mod mir_compiler;
mod pyvm_executor;
mod joinir_experiment;
mod object_emitter;
mod harness_executor;
mod fallback_executor;
impl NyashRunner {
/// Execute LLVM mode (split)
pub(crate) fn execute_llvm_mode(&self, filename: &str) {
// Step 1: Plugin initialization
if let Err(e) = plugin_init::PluginInitBox::init() {
crate::console_println!("❌ Plugin init error: {}", e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
// Read the file
let code = match fs::read_to_string(filename) {
Ok(content) => content,
Err(e) => {
crate::console_println!("❌ Error reading file {}: {}", filename, e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
};
// Step 3: Using resolution and prelude merge
let (clean_code, prelude_asts) = match using_resolver::UsingResolverBox::resolve(self, &code, filename) {
Ok(result) => result,
Err(e) => {
crate::console_println!("❌ {}", e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
};
// Parse to AST (main)
let main_ast = match NyashParser::parse_from_string(&clean_code) {
Ok(ast) => ast,
Err(e) => {
crate::runner::modes::common_util::diag::print_parse_error_with_context(
filename, &clean_code, &e,
);
// Enhanced context: list merged prelude files if any (from text-merge path)
let preludes =
crate::runner::modes::common_util::resolve::clone_last_merged_preludes();
if !preludes.is_empty() {
crate::runtime::get_global_ring0().log.debug(&format!(
"[parse/context] merged prelude files ({}):",
preludes.len()
));
let show = std::cmp::min(16, preludes.len());
for p in preludes.iter().take(show) {
crate::runtime::get_global_ring0()
.log
.debug(&format!(" - {}", p));
}
if preludes.len() > show {
crate::runtime::get_global_ring0()
.log
.debug(&format!(" ... ({} more)", preludes.len() - show));
}
}
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
};
// Merge preludes + main when enabled
let use_ast = crate::config::env::using_ast_enabled();
let ast = if use_ast && !prelude_asts.is_empty() {
crate::runner::modes::common_util::resolve::merge_prelude_asts_with_main(
prelude_asts,
&main_ast,
)
} else {
main_ast
};
// Macro expansion (env-gated) after merge
let ast = crate::r#macro::maybe_expand_and_dump(&ast, false);
let ast = crate::runner::modes::macro_child::normalize_core_pass(&ast);
// Compile to MIR
let mut module = match mir_compiler::MirCompilerBox::compile(ast, Some(filename)) {
Ok(m) => m,
Err(e) => {
crate::console_println!("❌ {}", e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
};
// Inject method_id for BoxCall/PluginInvoke where resolvable (by-id path)
#[allow(unused_mut)]
let _injected = method_id_injector::MethodIdInjectorBox::inject(&mut module);
// Phase 32 L-4.3a: JoinIR LLVM experiment hook
let module = joinir_experiment::JoinIrExperimentBox::apply(module);
// Dev/Test helper: allow executing via PyVM harness when requested
if let Some(code) = pyvm_executor::PyVmExecutorBox::try_execute(&module) {
exit_reporter::ExitReporterBox::emit_and_exit(code);
}
// If explicit object path is requested, emit object only
if let Ok(_out_path) = std::env::var("NYASH_LLVM_OBJ_OUT") {
#[cfg(feature = "llvm-harness")]
{
// Harness path (optional): if NYASH_LLVM_USE_HARNESS=1, try Python/llvmlite first.
if crate::config::env::llvm_use_harness() {
if let Err(e) = object_emitter::ObjectEmitterBox::try_emit(&module) {
crate::console_println!("❌ {}", e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
return;
}
// Verify object presence and size (>0)
match std::fs::metadata(&_out_path) {
Ok(meta) => {
if meta.len() == 0 {
crate::console_println!("❌ harness object is empty: {}", _out_path);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
crate::console_println!(
"[LLVM] object emitted: {} ({} bytes)",
_out_path,
meta.len()
);
}
}
Err(e) => {
crate::console_println!(
"❌ harness output not found after emit: {} ({})",
_out_path,
e
);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
}
return;
}
#[cfg(all(not(feature = "llvm-harness"), feature = "llvm-inkwell-legacy"))]
{
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);
}
crate::cli_v!(
"[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) {
crate::console_println!("❌ LLVM object emit error: {}", e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
match std::fs::metadata(&_out_path) {
Ok(meta) if meta.len() > 0 => {
crate::cli_v!(
"[LLVM] object emitted: {} ({} bytes)",
_out_path,
meta.len()
);
}
_ => {
crate::console_println!("❌ LLVM object not found or empty: {}", _out_path);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
}
return;
}
#[cfg(all(not(feature = "llvm-harness"), not(feature = "llvm-inkwell-legacy")))]
{
crate::console_println!("❌ LLVM backend not available (object emit).");
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
}
// Execute via LLVM backend (harness preferred)
if let Some(code) = harness_executor::HarnessExecutorBox::try_execute(&module) {
exit_reporter::ExitReporterBox::emit_and_exit(code);
}
// Execute via LLVM backend (mock or real)
#[cfg(feature = "llvm-inkwell-legacy")]
{
use nyash_rust::backend::llvm_compile_and_execute;
let temp_path = "nyash_llvm_temp";
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;
crate::console_println!("✅ LLVM execution completed!");
crate::console_println!("📊 Exit code: {}", exit_code);
exit_reporter::ExitReporterBox::emit_and_exit(exit_code as i32);
} else {
crate::console_println!(
"✅ LLVM execution completed (non-integer result)!"
);
crate::console_println!("📊 Result: {}", result.to_string_box().value);
}
}
Err(e) => {
crate::console_println!("❌ LLVM execution error: {}", e);
exit_reporter::ExitReporterBox::emit_and_exit(1);
}
}
}
#[cfg(all(not(feature = "llvm-inkwell-legacy")))]
{
let code = fallback_executor::FallbackExecutorBox::execute(&module);
exit_reporter::ExitReporterBox::emit_and_exit(code);
}
}
}
// emit_mir_json_for_harness moved to crate::runner::mir_json_emit

View File

@ -0,0 +1,62 @@
//! Object file emitter for LLVM mode
//!
//! Handles LLVM object file generation when requested (feature-gated).
use nyash_rust::mir::MirModule;
/// Object emitter Box
///
/// **Responsibility**: Emit LLVM object file if requested
/// **Input**: &MirModule
/// **Output**: Result<bool, String> (Ok(true) if emitted, Ok(false) if not requested, Err on failure)
pub struct ObjectEmitterBox;
impl ObjectEmitterBox {
/// Emit LLVM object file if requested
///
/// Checks NYASH_LLVM_OBJ_OUT environment variable.
/// If set, emits object file and verifies it's not empty.
#[cfg(feature = "llvm-harness")]
pub fn try_emit(module: &MirModule) -> Result<bool, String> {
let out_path = match std::env::var("NYASH_LLVM_OBJ_OUT") {
Ok(p) => p,
Err(_) => return Ok(false), // Not requested
};
if crate::config::env::llvm_use_harness() {
crate::runner::modes::common_util::exec::llvmlite_emit_object(
module, &out_path, 20_000
)?;
// Verify object file
Self::verify_object(&out_path)?;
return Ok(true);
}
// Verify object presence and size (>0)
Self::verify_object(&out_path)?;
Ok(true)
}
#[cfg(feature = "llvm-harness")]
fn verify_object(path: &str) -> Result<(), String> {
match std::fs::metadata(path) {
Ok(meta) if meta.len() > 0 => {
if std::env::var("NYASH_CLI_VERBOSE").ok().as_deref() == Some("1") {
crate::console_println!(
"[LLVM] object emitted: {} ({} bytes)",
path, meta.len()
);
}
Ok(())
}
Ok(_) => Err(format!("harness object is empty: {}", path)),
Err(e) => Err(format!("harness output not found: {} ({})", path, e)),
}
}
#[cfg(not(feature = "llvm-harness"))]
pub fn try_emit(_module: &MirModule) -> Result<bool, String> {
Ok(false)
}
}

View File

@ -0,0 +1,31 @@
//! Plugin initialization for LLVM mode
//!
//! Handles plugin host initialization and diagnostic checks.
/// Plugin initialization Box
///
/// **Responsibility**: Initialize plugins and run diagnostics
/// **Input**: None
/// **Output**: Result<(), String>
pub struct PluginInitBox;
impl PluginInitBox {
/// Initialize plugin host and run diagnostics
///
/// This function:
/// 1. Initializes the plugin host for method_id injection
/// 2. Runs friendly plugin guard diagnostics (non-strict mode)
pub fn init() -> Result<(), String> {
// Initialize plugin host so method_id injection can resolve plugin calls
crate::runner_plugin_init::init_bid_plugins();
// Friendly plugin guard (nonstrict): unify diagnostics across modes
crate::runner::modes::common_util::plugin_guard::check_and_report(
false,
crate::config::env::env_bool("NYASH_JSON_ONLY"),
"llvm",
);
Ok(())
}
}

View File

@ -0,0 +1,36 @@
//! PyVM harness executor (dev/test helper)
//!
//! Handles execution via PyVM harness when requested for development/testing.
use nyash_rust::mir::MirModule;
/// PyVM executor Box
///
/// **Responsibility**: Execute via PyVM harness when requested (dev/test helper)
/// **Input**: &MirModule
/// **Output**: Option<i32> (Some(exit_code) if executed, None otherwise)
///
/// **IMPORTANT**: This Box is used by 8 JSON AST smoke tests. DO NOT REMOVE!
pub struct PyVmExecutorBox;
impl PyVmExecutorBox {
/// Execute via PyVM harness if requested
///
/// This function checks the SMOKES_USE_PYVM environment variable.
/// If set to "1", it executes the module via PyVM harness and returns the exit code.
///
/// Returns Some(exit_code) if executed, None otherwise.
pub fn try_execute(module: &MirModule) -> Option<i32> {
if std::env::var("SMOKES_USE_PYVM").ok().as_deref() != Some("1") {
return None;
}
match super::super::common_util::pyvm::run_pyvm_harness_lib(module, "llvm-ast") {
Ok(code) => Some(code),
Err(e) => {
crate::console_println!("❌ PyVM harness error: {}", e);
Some(1)
}
}
}
}

View File

@ -0,0 +1,69 @@
//! Using/prelude resolution for LLVM mode
//!
//! Handles `using` statement resolution and prelude merge.
use nyash_rust::ast::ASTNode;
/// Using resolver Box
///
/// **Responsibility**: Resolve `using` statements and merge preludes
/// **Input**: runner, code, filename
/// **Output**: Result<(String, Vec<ASTNode>), String>
pub struct UsingResolverBox;
impl UsingResolverBox {
/// Resolve `using` statements and merge preludes
///
/// This function:
/// 1. Resolves prelude paths from `using` statements
/// 2. Parses preludes to ASTs
/// 3. Returns cleaned code and prelude ASTs
///
/// Returns (cleaned_code, prelude_asts) on success.
pub fn resolve(
runner: &crate::runner::NyashRunner,
code: &str,
filename: &str,
) -> Result<(String, Vec<ASTNode>), String> {
let use_ast = crate::config::env::using_ast_enabled();
let mut code_ref: &str = code;
let cleaned_code_owned;
let mut prelude_asts: Vec<ASTNode> = Vec::new();
if crate::config::env::enable_using() {
match crate::runner::modes::common_util::resolve::resolve_prelude_paths_profiled(
runner, code, filename,
) {
Ok((clean, paths)) => {
cleaned_code_owned = clean;
code_ref = &cleaned_code_owned;
if !paths.is_empty() && !use_ast {
return Err(
"using: AST prelude merge is disabled in this profile. Enable NYASH_USING_AST=1 or remove 'using' lines.".to_string()
);
}
if use_ast && !paths.is_empty() {
match crate::runner::modes::common_util::resolve::parse_preludes_to_asts(
runner, &paths,
) {
Ok(v) => prelude_asts = v,
Err(e) => {
return Err(format!("{}", e));
}
}
}
}
Err(e) => {
return Err(format!("{}", e));
}
}
}
// Pre-expand '@name[:T] = expr' sugar at line-head (same as common path)
let preexpanded_owned =
crate::runner::modes::common_util::resolve::preexpand_at_local(code_ref);
code_ref = &preexpanded_owned;
Ok((code_ref.to_string(), prelude_asts))
}
}