runner(cli): adopt CliConfig::as_groups across runner modules (dispatch/common/pipe_io/mir/bench). llvm-builder: extract ny_main wrapper to builders.entry; add optional env-gated function_lower delegation; keep default behavior unchanged

This commit is contained in:
Selfhosting Dev
2025-09-19 14:29:02 +09:00
parent c8c77d89a6
commit 3c7a5de900
11 changed files with 411 additions and 104 deletions

View File

@ -19,7 +19,8 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
}
// Direct v0 bridge when requested via CLI/env
let use_ny_parser = runner.config.parser_ny
let groups = runner.config.as_groups();
let use_ny_parser = groups.parser.parser_ny
|| std::env::var("NYASH_USE_NY_PARSER").ok().as_deref() == Some("1");
if use_ny_parser {
let code = match fs::read_to_string(filename) {
@ -43,7 +44,7 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
}
// AST dump mode
if runner.config.dump_ast {
if groups.debug.dump_ast {
println!("🧠 Nyash AST Dump - Processing file: {}", filename);
let code = match fs::read_to_string(filename) {
Ok(content) => content,
@ -64,14 +65,14 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
}
// MIR dump/verify
if runner.config.dump_mir || runner.config.verify_mir {
if groups.debug.dump_mir || groups.debug.verify_mir {
crate::cli_v!("🚀 Nyash MIR Compiler - Processing file: {} 🚀", filename);
runner.execute_mir_mode(filename);
return;
}
// WASM / AOT (feature-gated)
if runner.config.compile_wasm {
if groups.compile_wasm {
#[cfg(feature = "wasm-backend")]
{
super::modes::wasm::execute_wasm_mode(runner, filename);
@ -83,7 +84,7 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
process::exit(1);
}
}
if runner.config.compile_native {
if groups.compile_native {
#[cfg(feature = "cranelift-jit")]
{
runner.execute_aot_mode(filename);
@ -97,7 +98,7 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
}
// Backend selection
match runner.config.backend.as_str() {
match groups.backend.backend.as_str() {
"mir" => {
crate::cli_v!("🚀 Nyash MIR Interpreter - Executing file: {} 🚀", filename);
runner.execute_mir_mode(filename);
@ -154,7 +155,8 @@ pub(crate) fn execute_file_with_backend(runner: &NyashRunner, filename: &str) {
impl NyashRunner {
pub(crate) fn execute_mir_module(&self, module: &crate::mir::MirModule) {
// If CLI requested MIR JSON emit, write to file and exit immediately.
if let Some(path) = self.config.emit_mir_json.as_ref() {
let groups = self.config.as_groups();
if let Some(path) = groups.emit.emit_mir_json.as_ref() {
let p = std::path::Path::new(path);
if let Err(e) = crate::runner::mir_json_emit::emit_mir_json_for_harness_bin(module, p) {
eprintln!("❌ MIR JSON emit error: {}", e);
@ -164,12 +166,12 @@ impl NyashRunner {
std::process::exit(0);
}
// If CLI requested EXE emit, generate JSON then invoke ny-llvmc to link NyRT and exit.
if let Some(exe_out) = self.config.emit_exe.as_ref() {
if let Some(exe_out) = groups.emit.emit_exe.as_ref() {
if let Err(e) = crate::runner::modes::common_util::exec::ny_llvmc_emit_exe_bin(
module,
exe_out,
self.config.emit_exe_nyrt.as_deref(),
self.config.emit_exe_libs.as_deref(),
groups.emit.emit_exe_nyrt.as_deref(),
groups.emit.emit_exe_libs.as_deref(),
) {
eprintln!("{}", e);
std::process::exit(1);

View File

@ -54,6 +54,7 @@ impl NyashRunner {
/// Run Nyash based on the configuration
pub fn run(&self) {
let groups = self.config.as_groups();
// Build system (MVP): nyash --build <nyash.toml>
if let Some(cfg_path) = self.config.build_path.clone() {
if let Err(e) = self.run_build_mvp(&cfg_path) {
@ -346,41 +347,41 @@ impl NyashRunner {
}
// Optional: enable VM stats via CLI flags
if self.config.vm_stats {
if groups.backend.vm_stats {
std::env::set_var("NYASH_VM_STATS", "1");
}
if self.config.vm_stats_json {
if groups.backend.vm_stats_json {
// Prefer explicit JSON flag over any default
std::env::set_var("NYASH_VM_STATS_JSON", "1");
}
// Optional: JIT controls via CLI flags (centralized)
{
// CLI opt-in for JSONL events
if self.config.jit_events {
if groups.backend.jit.events {
std::env::set_var("NYASH_JIT_EVENTS", "1");
}
if self.config.jit_events_compile {
if groups.backend.jit.events_compile {
std::env::set_var("NYASH_JIT_EVENTS_COMPILE", "1");
}
if self.config.jit_events_runtime {
if groups.backend.jit.events_runtime {
std::env::set_var("NYASH_JIT_EVENTS_RUNTIME", "1");
}
if let Some(ref p) = self.config.jit_events_path {
if let Some(ref p) = groups.backend.jit.events_path {
std::env::set_var("NYASH_JIT_EVENTS_PATH", p);
}
let mut jc = nyash_rust::jit::config::JitConfig::from_env();
jc.exec |= self.config.jit_exec;
jc.stats |= self.config.jit_stats;
jc.stats_json |= self.config.jit_stats_json;
jc.dump |= self.config.jit_dump;
if self.config.jit_threshold.is_some() {
jc.threshold = self.config.jit_threshold;
jc.exec |= groups.backend.jit.exec;
jc.stats |= groups.backend.jit.stats;
jc.stats_json |= groups.backend.jit.stats_json;
jc.dump |= groups.backend.jit.dump;
if groups.backend.jit.threshold.is_some() {
jc.threshold = groups.backend.jit.threshold;
}
jc.phi_min |= self.config.jit_phi_min;
jc.hostcall |= self.config.jit_hostcall;
jc.handle_debug |= self.config.jit_handle_debug;
jc.native_f64 |= self.config.jit_native_f64;
jc.native_bool |= self.config.jit_native_bool;
jc.phi_min |= groups.backend.jit.phi_min;
jc.hostcall |= groups.backend.jit.hostcall;
jc.handle_debug |= groups.backend.jit.handle_debug;
jc.native_f64 |= groups.backend.jit.native_f64;
jc.native_bool |= groups.backend.jit.native_bool;
// If observability is enabled and no threshold is provided, force threshold=1 so lowering runs and emits events
let events_on = std::env::var("NYASH_JIT_EVENTS").ok().as_deref() == Some("1")
|| std::env::var("NYASH_JIT_EVENTS_COMPILE").ok().as_deref() == Some("1")
@ -388,14 +389,14 @@ impl NyashRunner {
if events_on && jc.threshold.is_none() {
jc.threshold = Some(1);
}
if self.config.jit_only {
if groups.backend.jit.only {
std::env::set_var("NYASH_JIT_ONLY", "1");
}
// Apply runtime capability probe (e.g., disable b1 ABI if unsupported)
let caps = nyash_rust::jit::config::probe_capabilities();
jc = nyash_rust::jit::config::apply_runtime_caps(jc, caps);
// Optional DOT emit via CLI (ensures dump is on when path specified)
if let Some(path) = &self.config.emit_cfg {
if let Some(path) = &groups.emit.emit_cfg {
std::env::set_var("NYASH_JIT_DOT", path);
jc.dump = true;
}

View File

@ -176,10 +176,11 @@ impl NyashRunner {
// Force JIT mode for this run
std::env::set_var("NYASH_JIT_EXEC", "1");
std::env::set_var("NYASH_JIT_THRESHOLD", "1");
if self.config.jit_stats {
let groups = self.config.as_groups();
if groups.backend.jit.stats {
std::env::set_var("NYASH_JIT_STATS", "1");
}
if self.config.jit_stats_json {
if groups.backend.jit.stats_json {
std::env::set_var("NYASH_JIT_STATS_JSON", "1");
}
let start = std::time::Instant::now();

View File

@ -27,7 +27,8 @@ impl NyashRunner {
}
}
// Direct v0 bridge when requested via CLI/env
let use_ny_parser = self.config.parser_ny || std::env::var("NYASH_USE_NY_PARSER").ok().as_deref() == Some("1");
let groups = self.config.as_groups();
let use_ny_parser = groups.parser.parser_ny || std::env::var("NYASH_USE_NY_PARSER").ok().as_deref() == Some("1");
if use_ny_parser {
let code = match fs::read_to_string(filename) {
Ok(content) => content,
@ -45,7 +46,7 @@ impl NyashRunner {
}
}
// AST dump mode
if self.config.dump_ast {
if groups.debug.dump_ast {
println!("🧠 Nyash AST Dump - Processing file: {}", filename);
let code = match fs::read_to_string(filename) {
Ok(content) => content,
@ -60,20 +61,20 @@ impl NyashRunner {
}
// MIR dump/verify
if self.config.dump_mir || self.config.verify_mir {
if groups.debug.dump_mir || groups.debug.verify_mir {
crate::cli_v!("🚀 Nyash MIR Compiler - Processing file: {} 🚀", filename);
self.execute_mir_mode(filename);
return;
}
// WASM / AOT (feature-gated)
if self.config.compile_wasm {
if groups.compile_wasm {
#[cfg(feature = "wasm-backend")]
{ self.execute_wasm_mode(filename); return; }
#[cfg(not(feature = "wasm-backend"))]
{ eprintln!("❌ WASM backend not available. Please rebuild with: cargo build --features wasm-backend"); process::exit(1); }
}
if self.config.compile_native {
if groups.compile_native {
#[cfg(feature = "cranelift-jit")]
{ self.execute_aot_mode(filename); return; }
#[cfg(not(feature = "cranelift-jit"))]
@ -81,7 +82,7 @@ impl NyashRunner {
}
// Backend selection
match self.config.backend.as_str() {
match groups.backend.backend.as_str() {
"mir" => {
crate::cli_v!("🚀 Nyash MIR Interpreter - Executing file: {} 🚀", filename);
self.execute_mir_interpreter_mode(filename);

View File

@ -36,8 +36,9 @@ impl NyashRunner {
}
};
let groups = self.config.as_groups();
// Verify MIR if requested
if self.config.verify_mir {
if groups.debug.verify_mir {
println!("🔍 Verifying MIR...");
match &compile_result.verification_result {
Ok(()) => println!("✅ MIR verification passed!"),
@ -52,13 +53,13 @@ impl NyashRunner {
}
// Dump MIR if requested
if self.config.dump_mir {
let mut printer = if self.config.mir_verbose {
if groups.debug.dump_mir {
let mut printer = if groups.debug.mir_verbose {
MirPrinter::verbose()
} else {
MirPrinter::new()
};
if self.config.mir_verbose_effects {
if groups.debug.mir_verbose_effects {
printer.set_show_effects_inline(true);
}
println!("🚀 MIR Output for {}:", filename);
@ -66,7 +67,7 @@ impl NyashRunner {
}
// Emit MIR JSON if requested and exit
if let Some(path) = self.config.emit_mir_json.as_ref() {
if let Some(path) = groups.emit.emit_mir_json.as_ref() {
let p = std::path::Path::new(path);
if let Err(e) = crate::runner::mir_json_emit::emit_mir_json_for_harness(&compile_result.module, p) {
eprintln!("❌ MIR JSON emit error: {}", e);
@ -77,12 +78,12 @@ impl NyashRunner {
}
// Emit native executable via ny-llvmc (crate) and exit
if let Some(exe_out) = self.config.emit_exe.as_ref() {
if let Some(exe_out) = groups.emit.emit_exe.as_ref() {
if let Err(e) = crate::runner::modes::common_util::exec::ny_llvmc_emit_exe_lib(
&compile_result.module,
exe_out,
self.config.emit_exe_nyrt.as_deref(),
self.config.emit_exe_libs.as_deref(),
groups.emit.emit_exe_nyrt.as_deref(),
groups.emit.emit_exe_libs.as_deref(),
) {
eprintln!("{}", e);
std::process::exit(1);

View File

@ -15,10 +15,11 @@ impl NyashRunner {
/// Try to handle `--ny-parser-pipe` / `--json-file` flow.
/// Returns true if the request was handled (program should return early).
pub(super) fn try_run_json_v0_pipe(&self) -> bool {
if !(self.config.ny_parser_pipe || self.config.json_file.is_some()) {
let groups = self.config.as_groups();
if !(groups.parser.ny_parser_pipe || groups.parser.json_file.is_some()) {
return false;
}
let json = if let Some(path) = &self.config.json_file {
let json = if let Some(path) = &groups.parser.json_file {
match std::fs::read_to_string(path) {
Ok(s) => s,
Err(e) => {