2025-08-12 11:33:48 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
* MIR Builder - Converts AST to MIR/SSA form
|
2025-09-17 07:43:07 +09:00
|
|
|
|
*
|
2025-08-12 11:33:48 +00:00
|
|
|
|
* Implements AST → MIR conversion with SSA construction
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
|
use super::slot_registry::resolve_slot_by_type_name;
|
2025-08-12 11:33:48 +00:00
|
|
|
|
use super::{
|
2025-09-17 07:43:07 +09:00
|
|
|
|
BasicBlock, BasicBlockId, BasicBlockIdGenerator, CompareOp, ConstValue, Effect, EffectMask,
|
|
|
|
|
|
FunctionSignature, MirFunction, MirInstruction, MirModule, MirType, ValueId, ValueIdGenerator,
|
2025-08-12 11:33:48 +00:00
|
|
|
|
};
|
2025-09-16 03:54:44 +09:00
|
|
|
|
use crate::ast::{ASTNode, LiteralValue};
|
2025-08-12 11:33:48 +00:00
|
|
|
|
use std::collections::HashMap;
|
2025-08-21 01:18:25 +09:00
|
|
|
|
use std::collections::HashSet;
|
2025-09-25 09:01:55 +09:00
|
|
|
|
mod calls; // Call system modules (refactored from builder_calls)
|
2025-09-03 01:37:38 +09:00
|
|
|
|
mod builder_calls;
|
2025-09-24 01:05:44 +09:00
|
|
|
|
mod call_resolution; // ChatGPT5 Pro: Type-safe call resolution utilities
|
2025-09-25 02:58:43 +09:00
|
|
|
|
mod method_call_handlers; // Method call handler separation (Phase 3)
|
🎉 Phase 11.8/12.7: MIR Core-13 完全実装 + 糖衣構文ドキュメント更新
主要な変更:
- MIR Core-13命令セット確定(Load/Store削除の革命的設計)
- Const, BinOp, Compare(値・計算)
- Jump, Branch, Return, Phi(制御)
- Call, BoxCall, ExternCall(呼び出し)
- TypeOp, Safepoint, Barrier(メタ)
- Phase 12.7糖衣構文ドキュメント整理(超圧縮重視、可逆変換保証)
- MIRビルダーのモジュール分割完了
- vtableテストスイート拡充
- AI協調開発ツール追加(並列リファクタリング支援)
詳細:
- src/mir/instruction_introspection.rs: core13_instruction_names()追加
- MIRビルダー分割: decls.rs, exprs_*.rs, fields.rs
- plugin_loader_v2: errors.rs, host_bridge.rs分離
- 論文用データ: mir13-final.md作成
🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-09-04 11:34:15 +09:00
|
|
|
|
mod decls; // declarations lowering split
|
2025-09-17 07:43:07 +09:00
|
|
|
|
mod exprs; // expression lowering split
|
|
|
|
|
|
mod exprs_call; // call(expr)
|
2025-09-25 00:41:56 +09:00
|
|
|
|
// include lowering removed (using is handled in runner)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
mod exprs_lambda; // lambda lowering
|
|
|
|
|
|
mod exprs_peek; // peek expression
|
|
|
|
|
|
mod exprs_qmark; // ?-propagate
|
2025-09-19 08:34:29 +09:00
|
|
|
|
mod exprs_legacy; // legacy big-match lowering
|
2025-09-17 07:43:07 +09:00
|
|
|
|
mod fields; // field access/assignment lowering split
|
|
|
|
|
|
pub(crate) mod loops;
|
|
|
|
|
|
mod ops;
|
|
|
|
|
|
mod phi;
|
2025-09-19 02:07:38 +09:00
|
|
|
|
mod if_form;
|
2025-09-19 08:34:29 +09:00
|
|
|
|
mod control_flow; // thin wrappers to centralize control-flow entrypoints
|
2025-09-17 10:58:12 +09:00
|
|
|
|
mod lifecycle; // prepare/lower_root/finalize split
|
|
|
|
|
|
// legacy large-match remains inline for now (planned extraction)
|
2025-09-11 04:20:28 +09:00
|
|
|
|
mod plugin_sigs; // plugin signature loader
|
2025-09-17 07:43:07 +09:00
|
|
|
|
mod stmts;
|
|
|
|
|
|
mod utils;
|
|
|
|
|
|
mod vars; // variables/scope helpers // small loop helpers (header/exit context)
|
2025-08-30 22:52:16 +09:00
|
|
|
|
|
2025-09-19 08:34:29 +09:00
|
|
|
|
// Unified member property kinds for computed/once/birth_once
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
|
|
|
|
pub(crate) enum PropertyKind {
|
|
|
|
|
|
Computed,
|
|
|
|
|
|
Once,
|
|
|
|
|
|
BirthOnce,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 23:44:34 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// MIR builder for converting AST to SSA form
|
|
|
|
|
|
pub struct MirBuilder {
|
|
|
|
|
|
/// Current module being built
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) current_module: Option<MirModule>,
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Current function being built
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) current_function: Option<MirFunction>,
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Current basic block being built
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) current_block: Option<BasicBlockId>,
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Value ID generator
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) value_gen: ValueIdGenerator,
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Basic block ID generator
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) block_gen: BasicBlockIdGenerator,
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Variable name to ValueId mapping (for SSA conversion)
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) variable_map: HashMap<String, ValueId>,
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Pending phi functions to be inserted
|
2025-08-16 17:39:04 +09:00
|
|
|
|
#[allow(dead_code)]
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) pending_phis: Vec<(BasicBlockId, ValueId, String)>,
|
2025-08-20 20:56:08 +09:00
|
|
|
|
|
|
|
|
|
|
/// Origin tracking for simple optimizations (e.g., object.method after new)
|
|
|
|
|
|
/// Maps a ValueId to the class name if it was produced by NewBox of that class
|
|
|
|
|
|
pub(super) value_origin_newbox: HashMap<ValueId, String>,
|
2025-08-21 01:18:25 +09:00
|
|
|
|
|
|
|
|
|
|
/// Names of user-defined boxes declared in the current module
|
|
|
|
|
|
pub(super) user_defined_boxes: HashSet<String>,
|
2025-08-24 00:05:12 +09:00
|
|
|
|
|
|
|
|
|
|
/// Weak field registry: BoxName -> {weak field names}
|
|
|
|
|
|
pub(super) weak_fields_by_box: HashMap<String, HashSet<String>>,
|
|
|
|
|
|
|
2025-09-19 08:34:29 +09:00
|
|
|
|
/// Unified members: BoxName -> {propName -> Kind}
|
|
|
|
|
|
pub(super) property_getters_by_box: HashMap<String, HashMap<String, PropertyKind>>,
|
|
|
|
|
|
|
2025-08-24 00:05:12 +09:00
|
|
|
|
/// Remember class of object fields after assignments: (base_id, field) -> class_name
|
|
|
|
|
|
pub(super) field_origin_class: HashMap<(ValueId, String), String>,
|
2025-08-28 22:31:51 +09:00
|
|
|
|
|
|
|
|
|
|
/// Optional per-value type annotations (MIR-level): ValueId -> MirType
|
|
|
|
|
|
pub(super) value_types: HashMap<ValueId, super::MirType>,
|
2025-09-11 03:33:33 +09:00
|
|
|
|
|
|
|
|
|
|
/// Plugin method return type signatures loaded from nyash_box.toml
|
|
|
|
|
|
plugin_method_sigs: HashMap<(String, String), super::MirType>,
|
2025-08-29 21:39:47 +09:00
|
|
|
|
/// Current static box name when lowering a static box body (e.g., "Main")
|
|
|
|
|
|
current_static_box: Option<String>,
|
2025-09-25 10:23:14 +09:00
|
|
|
|
/// Index of static methods seen during lowering: name -> [(BoxName, arity)]
|
|
|
|
|
|
pub(super) static_method_index: std::collections::HashMap<String, Vec<(String, usize)>>,
|
2025-08-30 23:47:08 +09:00
|
|
|
|
|
2025-09-25 00:41:56 +09:00
|
|
|
|
// include guards removed
|
2025-09-15 22:14:42 +09:00
|
|
|
|
|
|
|
|
|
|
/// Loop context stacks for lowering break/continue inside nested control flow
|
|
|
|
|
|
/// Top of stack corresponds to the innermost active loop
|
|
|
|
|
|
pub(super) loop_header_stack: Vec<BasicBlockId>,
|
|
|
|
|
|
pub(super) loop_exit_stack: Vec<BasicBlockId>,
|
2025-09-16 23:49:36 +09:00
|
|
|
|
|
2025-09-19 02:07:38 +09:00
|
|
|
|
/// If/merge context stack (innermost first). Used to make merge targets explicit
|
|
|
|
|
|
/// when lowering nested conditionals and to simplify jump generation.
|
|
|
|
|
|
pub(super) if_merge_stack: Vec<BasicBlockId>,
|
|
|
|
|
|
|
2025-09-23 07:25:58 +09:00
|
|
|
|
// フェーズM: no_phi_modeフィールド削除(常にPHI使用)
|
2025-09-19 02:07:38 +09:00
|
|
|
|
|
|
|
|
|
|
// ---- Try/Catch/Cleanup lowering context ----
|
|
|
|
|
|
/// When true, `return` statements are deferred: they assign to `return_defer_slot`
|
|
|
|
|
|
/// and jump to `return_defer_target` (typically the cleanup/exit block).
|
|
|
|
|
|
pub(super) return_defer_active: bool,
|
|
|
|
|
|
/// Slot value to receive deferred return values (edge-copy mode friendly).
|
|
|
|
|
|
pub(super) return_defer_slot: Option<ValueId>,
|
|
|
|
|
|
/// Target block to jump to on deferred return.
|
|
|
|
|
|
pub(super) return_defer_target: Option<BasicBlockId>,
|
|
|
|
|
|
/// Set to true when a deferred return has been emitted in the current context.
|
|
|
|
|
|
pub(super) return_deferred_emitted: bool,
|
|
|
|
|
|
/// True while lowering the cleanup block.
|
|
|
|
|
|
pub(super) in_cleanup_block: bool,
|
|
|
|
|
|
/// Policy flags (snapshotted at entry of try/catch lowering)
|
|
|
|
|
|
pub(super) cleanup_allow_return: bool,
|
|
|
|
|
|
pub(super) cleanup_allow_throw: bool,
|
2025-09-20 03:37:20 +09:00
|
|
|
|
|
|
|
|
|
|
/// Hint sink (zero-cost guidance; currently no-op)
|
|
|
|
|
|
pub(super) hint_sink: crate::mir::hints::HintSink,
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl MirBuilder {
|
|
|
|
|
|
/// Create a new MIR builder
|
|
|
|
|
|
pub fn new() -> Self {
|
2025-09-11 04:20:28 +09:00
|
|
|
|
let plugin_method_sigs = plugin_sigs::load_plugin_method_sigs();
|
2025-09-23 07:25:58 +09:00
|
|
|
|
// フェーズM: no_phi_mode初期化削除
|
2025-08-12 11:33:48 +00:00
|
|
|
|
Self {
|
|
|
|
|
|
current_module: None,
|
|
|
|
|
|
current_function: None,
|
|
|
|
|
|
current_block: None,
|
|
|
|
|
|
value_gen: ValueIdGenerator::new(),
|
|
|
|
|
|
block_gen: BasicBlockIdGenerator::new(),
|
|
|
|
|
|
variable_map: HashMap::new(),
|
|
|
|
|
|
pending_phis: Vec::new(),
|
2025-08-20 20:56:08 +09:00
|
|
|
|
value_origin_newbox: HashMap::new(),
|
2025-08-21 01:18:25 +09:00
|
|
|
|
user_defined_boxes: HashSet::new(),
|
2025-08-24 00:05:12 +09:00
|
|
|
|
weak_fields_by_box: HashMap::new(),
|
2025-09-19 08:34:29 +09:00
|
|
|
|
property_getters_by_box: HashMap::new(),
|
2025-08-24 00:05:12 +09:00
|
|
|
|
field_origin_class: HashMap::new(),
|
2025-08-28 22:31:51 +09:00
|
|
|
|
value_types: HashMap::new(),
|
2025-09-11 03:33:33 +09:00
|
|
|
|
plugin_method_sigs,
|
2025-08-29 21:39:47 +09:00
|
|
|
|
current_static_box: None,
|
2025-09-25 10:23:14 +09:00
|
|
|
|
static_method_index: std::collections::HashMap::new(),
|
2025-09-25 00:41:56 +09:00
|
|
|
|
|
2025-09-15 22:14:42 +09:00
|
|
|
|
loop_header_stack: Vec::new(),
|
|
|
|
|
|
loop_exit_stack: Vec::new(),
|
2025-09-19 02:07:38 +09:00
|
|
|
|
if_merge_stack: Vec::new(),
|
2025-09-23 07:25:58 +09:00
|
|
|
|
// フェーズM: no_phi_modeフィールド削除
|
2025-09-19 02:07:38 +09:00
|
|
|
|
return_defer_active: false,
|
|
|
|
|
|
return_defer_slot: None,
|
|
|
|
|
|
return_defer_target: None,
|
|
|
|
|
|
return_deferred_emitted: false,
|
|
|
|
|
|
in_cleanup_block: false,
|
|
|
|
|
|
cleanup_allow_return: false,
|
|
|
|
|
|
cleanup_allow_throw: false,
|
2025-09-20 03:37:20 +09:00
|
|
|
|
hint_sink: crate::mir::hints::HintSink::new(),
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-20 18:57:10 +09:00
|
|
|
|
|
2025-09-19 02:07:38 +09:00
|
|
|
|
/// Push/pop helpers for If merge context (best-effort; optional usage)
|
|
|
|
|
|
pub(super) fn push_if_merge(&mut self, bb: BasicBlockId) { self.if_merge_stack.push(bb); }
|
|
|
|
|
|
pub(super) fn pop_if_merge(&mut self) { let _ = self.if_merge_stack.pop(); }
|
|
|
|
|
|
|
2025-09-20 03:37:20 +09:00
|
|
|
|
// ---- Hint helpers (no-op by default) ----
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub(crate) fn hint_loop_header(&mut self) { self.hint_sink.loop_header(); }
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub(crate) fn hint_loop_latch(&mut self) { self.hint_sink.loop_latch(); }
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub(crate) fn hint_scope_enter(&mut self, id: u32) { self.hint_sink.scope_enter(id); }
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub(crate) fn hint_scope_leave(&mut self, id: u32) { self.hint_sink.scope_leave(id); }
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
pub(crate) fn hint_join_result<S: Into<String>>(&mut self, var: S) { self.hint_sink.join_result(var.into()); }
|
2025-09-20 06:24:33 +09:00
|
|
|
|
#[inline]
|
|
|
|
|
|
pub(crate) fn hint_loop_carrier<S: Into<String>>(&mut self, vars: impl IntoIterator<Item = S>) {
|
|
|
|
|
|
self.hint_sink.loop_carrier(vars.into_iter().map(|s| s.into()).collect::<Vec<_>>());
|
|
|
|
|
|
}
|
2025-09-20 03:37:20 +09:00
|
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Build a complete MIR module from AST
|
|
|
|
|
|
pub fn build_module(&mut self, ast: ASTNode) -> Result<MirModule, String> {
|
2025-09-17 07:29:28 +09:00
|
|
|
|
self.prepare_module()?;
|
|
|
|
|
|
let result_value = self.lower_root(ast)?;
|
|
|
|
|
|
self.finalize_module(result_value)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Build an expression and return its value ID
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) fn build_expression(&mut self, ast: ASTNode) -> Result<ValueId, String> {
|
2025-09-04 06:27:39 +09:00
|
|
|
|
// Delegated to exprs.rs to keep this file lean
|
|
|
|
|
|
self.build_expression_impl(ast)
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Build a literal value
|
2025-09-04 06:27:39 +09:00
|
|
|
|
pub(super) fn build_literal(&mut self, literal: LiteralValue) -> Result<ValueId, String> {
|
2025-08-28 22:31:51 +09:00
|
|
|
|
// Determine type without moving literal
|
|
|
|
|
|
let ty_for_dst = match &literal {
|
|
|
|
|
|
LiteralValue::Integer(_) => Some(super::MirType::Integer),
|
|
|
|
|
|
LiteralValue::Float(_) => Some(super::MirType::Float),
|
|
|
|
|
|
LiteralValue::Bool(_) => Some(super::MirType::Bool),
|
|
|
|
|
|
LiteralValue::String(_) => Some(super::MirType::String),
|
|
|
|
|
|
_ => None,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let const_value = match literal {
|
|
|
|
|
|
LiteralValue::Integer(n) => ConstValue::Integer(n),
|
|
|
|
|
|
LiteralValue::Float(f) => ConstValue::Float(f),
|
|
|
|
|
|
LiteralValue::String(s) => ConstValue::String(s),
|
|
|
|
|
|
LiteralValue::Bool(b) => ConstValue::Bool(b),
|
2025-08-15 07:36:00 +00:00
|
|
|
|
LiteralValue::Null => ConstValue::Null,
|
2025-08-12 11:33:48 +00:00
|
|
|
|
LiteralValue::Void => ConstValue::Void,
|
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let dst = self.value_gen.next();
|
|
|
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
|
dst,
|
|
|
|
|
|
value: const_value,
|
|
|
|
|
|
})?;
|
2025-08-28 22:31:51 +09:00
|
|
|
|
// Annotate type
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if let Some(ty) = ty_for_dst {
|
|
|
|
|
|
self.value_types.insert(dst, ty);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
Ok(dst)
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Build variable access
|
2025-09-04 06:27:39 +09:00
|
|
|
|
pub(super) fn build_variable_access(&mut self, name: String) -> Result<ValueId, String> {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
if let Some(&value_id) = self.variable_map.get(&name) {
|
|
|
|
|
|
Ok(value_id)
|
|
|
|
|
|
} else {
|
2025-09-24 21:45:27 +09:00
|
|
|
|
// Enhance diagnostics using Using simple registry (Phase 1)
|
|
|
|
|
|
let mut msg = format!("Undefined variable: {}", name);
|
|
|
|
|
|
let suggest = crate::using::simple_registry::suggest_using_for_symbol(&name);
|
|
|
|
|
|
if !suggest.is_empty() {
|
|
|
|
|
|
msg.push_str("\nHint: symbol appears in using module(s): ");
|
|
|
|
|
|
msg.push_str(&suggest.join(", "));
|
|
|
|
|
|
msg.push_str("\nConsider adding 'using <module> [as Alias]' or check nyash.toml [using].");
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(msg)
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Build assignment
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub(super) fn build_assignment(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
var_name: String,
|
|
|
|
|
|
value: ASTNode,
|
|
|
|
|
|
) -> Result<ValueId, String> {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let value_id = self.build_expression(value)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
// In SSA form, each assignment creates a new value
|
2025-08-18 22:04:50 +09:00
|
|
|
|
self.variable_map.insert(var_name.clone(), value_id);
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
Ok(value_id)
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
/// Emit an instruction to the current basic block
|
2025-08-18 23:36:40 +09:00
|
|
|
|
pub(super) fn emit_instruction(&mut self, instruction: MirInstruction) -> Result<(), String> {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let block_id = self.current_block.ok_or("No current basic block")?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
if let Some(ref mut function) = self.current_function {
|
|
|
|
|
|
if let Some(block) = function.get_block_mut(block_id) {
|
2025-09-03 05:04:56 +09:00
|
|
|
|
if utils::builder_debug_enabled() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
eprintln!(
|
|
|
|
|
|
"[BUILDER] emit @bb{} -> {}",
|
|
|
|
|
|
block_id,
|
|
|
|
|
|
match &instruction {
|
|
|
|
|
|
MirInstruction::TypeOp { dst, op, value, ty } =>
|
|
|
|
|
|
format!("typeop {:?} {} {:?} -> {}", op, value, ty, dst),
|
|
|
|
|
|
MirInstruction::Print { value, .. } => format!("print {}", value),
|
|
|
|
|
|
MirInstruction::BoxCall {
|
|
|
|
|
|
box_val,
|
|
|
|
|
|
method,
|
|
|
|
|
|
method_id,
|
|
|
|
|
|
args,
|
|
|
|
|
|
dst,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => {
|
|
|
|
|
|
if let Some(mid) = method_id {
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"boxcall {}.{}[#{}]({:?}) -> {:?}",
|
|
|
|
|
|
box_val, method, mid, args, dst
|
|
|
|
|
|
)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
format!(
|
|
|
|
|
|
"boxcall {}.{}({:?}) -> {:?}",
|
|
|
|
|
|
box_val, method, args, dst
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2025-08-26 20:48:48 +09:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
MirInstruction::Call {
|
|
|
|
|
|
func, args, dst, ..
|
|
|
|
|
|
} => format!("call {}({:?}) -> {:?}", func, args, dst),
|
|
|
|
|
|
MirInstruction::NewBox {
|
|
|
|
|
|
dst,
|
|
|
|
|
|
box_type,
|
|
|
|
|
|
args,
|
|
|
|
|
|
} => format!("new {}({:?}) -> {}", box_type, args, dst),
|
|
|
|
|
|
MirInstruction::Const { dst, value } =>
|
|
|
|
|
|
format!("const {:?} -> {}", value, dst),
|
|
|
|
|
|
MirInstruction::Branch {
|
|
|
|
|
|
condition,
|
|
|
|
|
|
then_bb,
|
|
|
|
|
|
else_bb,
|
|
|
|
|
|
} => format!("br {}, {}, {}", condition, then_bb, else_bb),
|
|
|
|
|
|
MirInstruction::Jump { target } => format!("br {}", target),
|
|
|
|
|
|
_ => format!("{:?}", instruction),
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
2025-08-24 01:58:41 +09:00
|
|
|
|
}
|
2025-08-12 11:33:48 +00:00
|
|
|
|
block.add_instruction(instruction);
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err(format!("Basic block {} does not exist", block_id))
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err("No current function".to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-16 23:49:36 +09:00
|
|
|
|
|
2025-09-23 07:25:58 +09:00
|
|
|
|
// フェーズM: is_no_phi_mode()メソッド削除
|
2025-09-16 23:49:36 +09:00
|
|
|
|
|
2025-09-23 07:25:58 +09:00
|
|
|
|
// フェーズM: insert_edge_copy()メソッド削除(no_phi_mode撤廃により不要)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-13 10:20:37 +00:00
|
|
|
|
/// Build new expression: new ClassName(arguments)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
pub(super) fn build_new_expression(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
class: String,
|
|
|
|
|
|
arguments: Vec<ASTNode>,
|
|
|
|
|
|
) -> Result<ValueId, String> {
|
2025-08-20 17:58:51 +09:00
|
|
|
|
// Phase 9.78a: Unified Box creation using NewBox instruction
|
2025-09-07 07:28:53 +09:00
|
|
|
|
// Core-13 pure mode: emit ExternCall(env.box.new) with type name const only
|
|
|
|
|
|
if crate::config::env::mir_core13_pure() {
|
|
|
|
|
|
// Emit Const String for type name
|
|
|
|
|
|
let ty_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
|
dst: ty_id,
|
|
|
|
|
|
value: ConstValue::String(class.clone()),
|
|
|
|
|
|
})?;
|
2025-09-07 07:28:53 +09:00
|
|
|
|
// Evaluate arguments (pass through to env.box.new shim)
|
|
|
|
|
|
let mut arg_vals: Vec<ValueId> = Vec::with_capacity(arguments.len());
|
2025-09-17 07:43:07 +09:00
|
|
|
|
for a in arguments {
|
|
|
|
|
|
arg_vals.push(self.build_expression(a)?);
|
|
|
|
|
|
}
|
2025-09-07 07:28:53 +09:00
|
|
|
|
// Build arg list: [type, a1, a2, ...]
|
|
|
|
|
|
let mut args: Vec<ValueId> = Vec::with_capacity(1 + arg_vals.len());
|
|
|
|
|
|
args.push(ty_id);
|
|
|
|
|
|
args.extend(arg_vals);
|
|
|
|
|
|
// Call env.box.new
|
|
|
|
|
|
let dst = self.value_gen.next();
|
|
|
|
|
|
self.emit_instruction(MirInstruction::ExternCall {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
dst: Some(dst),
|
|
|
|
|
|
iface_name: "env.box".to_string(),
|
|
|
|
|
|
method_name: "new".to_string(),
|
|
|
|
|
|
args,
|
|
|
|
|
|
effects: EffectMask::PURE,
|
2025-09-07 07:28:53 +09:00
|
|
|
|
})?;
|
|
|
|
|
|
// 型注釈(最小)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.value_types
|
|
|
|
|
|
.insert(dst, super::MirType::Box(class.clone()));
|
2025-09-07 07:28:53 +09:00
|
|
|
|
return Ok(dst);
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-30 22:52:16 +09:00
|
|
|
|
// Optimization: Primitive wrappers → emit Const directly when possible
|
|
|
|
|
|
if class == "IntegerBox" && arguments.len() == 1 {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if let ASTNode::Literal {
|
|
|
|
|
|
value: LiteralValue::Integer(n),
|
|
|
|
|
|
..
|
|
|
|
|
|
} = arguments[0].clone()
|
|
|
|
|
|
{
|
2025-08-30 22:52:16 +09:00
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
|
dst,
|
|
|
|
|
|
value: ConstValue::Integer(n),
|
|
|
|
|
|
})?;
|
2025-08-30 22:52:16 +09:00
|
|
|
|
self.value_types.insert(dst, super::MirType::Integer);
|
|
|
|
|
|
return Ok(dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-20 17:58:51 +09:00
|
|
|
|
// First, evaluate all arguments to get their ValueIds
|
|
|
|
|
|
let mut arg_values = Vec::new();
|
|
|
|
|
|
for arg in arguments {
|
|
|
|
|
|
let arg_value = self.build_expression(arg)?;
|
|
|
|
|
|
arg_values.push(arg_value);
|
2025-08-18 19:29:24 +09:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-20 17:58:51 +09:00
|
|
|
|
// Generate the destination ValueId
|
2025-08-13 10:20:37 +00:00
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-20 17:58:51 +09:00
|
|
|
|
// Emit NewBox instruction for all Box types
|
|
|
|
|
|
// VM will handle optimization for basic types internally
|
|
|
|
|
|
self.emit_instruction(MirInstruction::NewBox {
|
2025-08-13 10:20:37 +00:00
|
|
|
|
dst,
|
2025-08-20 20:56:08 +09:00
|
|
|
|
box_type: class.clone(),
|
2025-08-20 18:57:10 +09:00
|
|
|
|
args: arg_values.clone(),
|
|
|
|
|
|
})?;
|
2025-09-24 09:30:42 +09:00
|
|
|
|
// Phase 15.5: Unified box type handling
|
|
|
|
|
|
// All boxes (including former core boxes) are treated uniformly as Box types
|
|
|
|
|
|
self.value_types
|
|
|
|
|
|
.insert(dst, super::MirType::Box(class.clone()));
|
2025-08-20 18:57:10 +09:00
|
|
|
|
|
2025-08-20 20:56:08 +09:00
|
|
|
|
// Record origin for optimization: dst was created by NewBox of class
|
2025-08-26 20:48:48 +09:00
|
|
|
|
self.value_origin_newbox.insert(dst, class.clone());
|
2025-08-20 20:56:08 +09:00
|
|
|
|
|
2025-08-30 22:52:16 +09:00
|
|
|
|
// For plugin/builtin boxes, call birth(...). For user-defined boxes, skip (InstanceBox already constructed)
|
2025-09-11 20:18:53 +09:00
|
|
|
|
// Special-case: StringBox is already fully constructed via from_i8_string in LLVM lowering; skip birth
|
|
|
|
|
|
if !self.user_defined_boxes.contains(&class) && class != "StringBox" {
|
2025-08-30 22:52:16 +09:00
|
|
|
|
let birt_mid = resolve_slot_by_type_name(&class, "birth");
|
|
|
|
|
|
self.emit_box_or_plugin_call(
|
|
|
|
|
|
None,
|
|
|
|
|
|
dst,
|
|
|
|
|
|
"birth".to_string(),
|
|
|
|
|
|
birt_mid,
|
|
|
|
|
|
arg_values,
|
|
|
|
|
|
EffectMask::READ.add(Effect::ReadHeap),
|
|
|
|
|
|
)?;
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-13 10:20:37 +00:00
|
|
|
|
Ok(dst)
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-13 07:13:53 +00:00
|
|
|
|
/// Check if the current basic block is terminated
|
|
|
|
|
|
fn is_current_block_terminated(&self) -> bool {
|
|
|
|
|
|
if let (Some(block_id), Some(ref function)) = (self.current_block, &self.current_function) {
|
|
|
|
|
|
if let Some(block) = function.get_block(block_id) {
|
|
|
|
|
|
return block.is_terminated();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
false
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
|
2025-08-29 21:39:47 +09:00
|
|
|
|
|
|
|
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for MirBuilder {
|
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
Self::new()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|