2025-09-25 01:09:48 +09:00
|
|
|
/*!
|
|
|
|
|
* Minimal MIR Interpreter
|
|
|
|
|
*
|
|
|
|
|
* Executes a subset of MIR instructions for fast iteration without LLVM/JIT.
|
|
|
|
|
* Supported: Const, BinOp(Add/Sub/Mul/Div/Mod), Compare, Load/Store, Branch, Jump, Return,
|
|
|
|
|
* Print/Debug (best-effort), Barrier/Safepoint (no-op).
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
|
|
use crate::box_trait::NyashBox;
|
|
|
|
|
|
|
|
|
|
pub(super) use crate::backend::abi_util::{eq_vm, to_bool_vm};
|
|
|
|
|
pub(super) use crate::backend::vm::{VMError, VMValue};
|
|
|
|
|
pub(super) use crate::mir::{
|
|
|
|
|
BasicBlockId, BinaryOp, Callee, CompareOp, ConstValue, MirFunction, MirInstruction, MirModule,
|
|
|
|
|
ValueId,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
mod exec;
|
|
|
|
|
mod handlers;
|
|
|
|
|
mod helpers;
|
2025-09-28 01:33:58 +09:00
|
|
|
mod method_router;
|
refactor: add MIR interpreter utility helpers (Phase 1)
- Add destination write helpers (write_box_result, write_void, write_result)
- Add argument validation helpers (validate_args_exact/range/min)
- Add receiver conversion helper (convert_to_box)
- Update handlers to use new helpers
Reduces code duplication:
- Destination patterns: 37 call sites converted
- Each replacement saves 2-3 lines (74-111 lines saved)
- Helper infrastructure: 178 lines added
- Net improvement: Reduced duplication + better maintainability
Impact:
- Build: ✓ SUCCESS (0 errors, 146 warnings)
- Tests: ✓ 8/9 smoke tests PASS
- Functionality: ✓ PRESERVED (no behavior changes)
Files created:
- src/backend/mir_interpreter/utils/mod.rs
- src/backend/mir_interpreter/utils/destination_helpers.rs
- src/backend/mir_interpreter/utils/arg_validation.rs
- src/backend/mir_interpreter/utils/receiver_helpers.rs
Files modified: 15 handler files
- arithmetic.rs, boxes.rs, boxes_array.rs, boxes_instance.rs
- boxes_map.rs, boxes_object_fields.rs, boxes_plugin.rs
- boxes_string.rs, calls.rs, extern_provider.rs, externals.rs
- memory.rs, misc.rs, mod.rs
Related: Phase 21.0 refactoring
Risk: Low (pure refactoring, no behavior change)
2025-11-06 22:50:46 +09:00
|
|
|
mod utils;
|
2025-09-25 01:09:48 +09:00
|
|
|
|
|
|
|
|
pub struct MirInterpreter {
|
|
|
|
|
pub(super) regs: HashMap<ValueId, VMValue>,
|
|
|
|
|
pub(super) mem: HashMap<ValueId, VMValue>,
|
2025-09-26 14:34:42 +09:00
|
|
|
// Object field storage keyed by stable object identity (Arc ptr addr fallback)
|
|
|
|
|
pub(super) obj_fields: HashMap<u64, HashMap<String, VMValue>>,
|
2025-09-25 01:09:48 +09:00
|
|
|
pub(super) functions: HashMap<String, MirFunction>,
|
|
|
|
|
pub(super) cur_fn: Option<String>,
|
2025-09-26 03:30:59 +09:00
|
|
|
// Trace context (dev-only; enabled with NYASH_VM_TRACE=1)
|
|
|
|
|
pub(super) last_block: Option<BasicBlockId>,
|
|
|
|
|
pub(super) last_inst: Option<MirInstruction>,
|
2025-09-25 01:09:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MirInterpreter {
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
regs: HashMap::new(),
|
|
|
|
|
mem: HashMap::new(),
|
|
|
|
|
obj_fields: HashMap::new(),
|
|
|
|
|
functions: HashMap::new(),
|
|
|
|
|
cur_fn: None,
|
2025-09-26 03:30:59 +09:00
|
|
|
last_block: None,
|
|
|
|
|
last_inst: None,
|
2025-09-25 01:09:48 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Execute module entry (main) and return boxed result
|
|
|
|
|
pub fn execute_module(&mut self, module: &MirModule) -> Result<Box<dyn NyashBox>, VMError> {
|
|
|
|
|
// Snapshot functions for call resolution
|
|
|
|
|
self.functions = module.functions.clone();
|
|
|
|
|
let func = module
|
|
|
|
|
.functions
|
|
|
|
|
.get("main")
|
|
|
|
|
.ok_or_else(|| VMError::InvalidInstruction("missing main".into()))?;
|
|
|
|
|
let ret = self.execute_function(func)?;
|
|
|
|
|
Ok(ret.to_nyash_box())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn execute_function(&mut self, func: &MirFunction) -> Result<VMValue, VMError> {
|
|
|
|
|
self.exec_function_inner(func, None)
|
|
|
|
|
}
|
|
|
|
|
}
|