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-08-30 22:52:16 +09:00
|
|
|
|
use std::fs;
|
2025-09-03 01:37:38 +09:00
|
|
|
|
mod builder_calls;
|
🎉 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)
|
🎉 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 exprs_include; // include lowering
|
2025-09-17 07:43:07 +09:00
|
|
|
|
mod exprs_lambda; // lambda lowering
|
|
|
|
|
|
mod exprs_peek; // peek expression
|
|
|
|
|
|
mod exprs_qmark; // ?-propagate
|
|
|
|
|
|
mod fields; // field access/assignment lowering split
|
|
|
|
|
|
pub(crate) mod loops;
|
|
|
|
|
|
mod ops;
|
|
|
|
|
|
mod phi;
|
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-03 05:04:56 +09:00
|
|
|
|
// moved helpers to builder/utils.rs
|
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>>,
|
|
|
|
|
|
|
|
|
|
|
|
/// 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-08-30 23:47:08 +09:00
|
|
|
|
|
|
|
|
|
|
/// Include guards: currently loading file canonical paths
|
|
|
|
|
|
include_loading: HashSet<String>,
|
|
|
|
|
|
/// Include visited cache: canonical path -> box name
|
|
|
|
|
|
include_box_map: HashMap<String, String>,
|
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
|
|
|
|
|
|
|
|
|
|
/// Whether PHI emission is disabled (edge-copy mode)
|
|
|
|
|
|
pub(super) no_phi_mode: bool,
|
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-16 23:49:36 +09:00
|
|
|
|
let no_phi_mode = crate::config::env::mir_no_phi();
|
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(),
|
|
|
|
|
|
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-08-30 23:47:08 +09:00
|
|
|
|
include_loading: HashSet::new(),
|
|
|
|
|
|
include_box_map: HashMap::new(),
|
2025-09-15 22:14:42 +09:00
|
|
|
|
loop_header_stack: Vec::new(),
|
|
|
|
|
|
loop_exit_stack: Vec::new(),
|
2025-09-16 23:49:36 +09:00
|
|
|
|
no_phi_mode,
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-20 18:57:10 +09:00
|
|
|
|
|
2025-09-03 01:37:38 +09:00
|
|
|
|
// moved to builder_calls.rs: lower_method_as_function
|
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 10:58:12 +09:00
|
|
|
|
// prepare_module/lower_root/finalize_module moved to builder/lifecycle.rs
|
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-09-04 06:27:39 +09:00
|
|
|
|
// Moved implementation to exprs.rs; keeping a small shim here improves readability
|
|
|
|
|
|
pub(super) fn build_expression_impl_legacy(&mut self, ast: ASTNode) -> Result<ValueId, String> {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
match ast {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
ASTNode::Literal { value, .. } => self.build_literal(value),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::BinaryOp {
|
|
|
|
|
|
left,
|
|
|
|
|
|
operator,
|
|
|
|
|
|
right,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => self.build_binary_op(*left, operator, *right),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::UnaryOp {
|
|
|
|
|
|
operator, operand, ..
|
|
|
|
|
|
} => {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let op_string = match operator {
|
|
|
|
|
|
crate::ast::UnaryOperator::Minus => "-".to_string(),
|
|
|
|
|
|
crate::ast::UnaryOperator::Not => "not".to_string(),
|
|
|
|
|
|
};
|
|
|
|
|
|
self.build_unary_op(op_string, *operand)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Variable { name, .. } => self.build_variable_access(name.clone()),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Me { .. } => self.build_me_expression(),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::MethodCall {
|
|
|
|
|
|
object,
|
|
|
|
|
|
method,
|
|
|
|
|
|
arguments,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => {
|
2025-08-24 00:05:12 +09:00
|
|
|
|
// Early TypeOp lowering for method-style is()/as()
|
|
|
|
|
|
if (method == "is" || method == "as") && arguments.len() == 1 {
|
|
|
|
|
|
if let Some(type_name) = Self::extract_string_literal(&arguments[0]) {
|
|
|
|
|
|
let obj_val = self.build_expression(*object.clone())?;
|
|
|
|
|
|
let ty = Self::parse_type_name_to_mir(&type_name);
|
|
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
let op = if method == "is" {
|
|
|
|
|
|
super::TypeOpKind::Check
|
|
|
|
|
|
} else {
|
|
|
|
|
|
super::TypeOpKind::Cast
|
|
|
|
|
|
};
|
|
|
|
|
|
self.emit_instruction(MirInstruction::TypeOp {
|
|
|
|
|
|
dst,
|
|
|
|
|
|
op,
|
|
|
|
|
|
value: obj_val,
|
|
|
|
|
|
ty,
|
|
|
|
|
|
})?;
|
2025-08-24 00:05:12 +09:00
|
|
|
|
return Ok(dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-14 01:48:40 +00:00
|
|
|
|
self.build_method_call(*object.clone(), method.clone(), arguments.clone())
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::FromCall {
|
|
|
|
|
|
parent,
|
|
|
|
|
|
method,
|
|
|
|
|
|
arguments,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => self.build_from_expression(parent.clone(), method.clone(), arguments.clone()),
|
|
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
ASTNode::Assignment { target, value, .. } => {
|
2025-08-13 10:20:37 +00:00
|
|
|
|
// Check if target is a field access for RefSet
|
|
|
|
|
|
if let ASTNode::FieldAccess { object, field, .. } = target.as_ref() {
|
|
|
|
|
|
self.build_field_assignment(*object.clone(), field.clone(), *value.clone())
|
|
|
|
|
|
} else if let ASTNode::Variable { name, .. } = target.as_ref() {
|
|
|
|
|
|
// Plain variable assignment - existing behavior
|
2025-08-12 11:33:48 +00:00
|
|
|
|
self.build_assignment(name.clone(), *value.clone())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err("Complex assignment targets not yet supported in MIR".to_string())
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::FunctionCall {
|
|
|
|
|
|
name, arguments, ..
|
|
|
|
|
|
} => {
|
2025-08-24 00:05:12 +09:00
|
|
|
|
// Early TypeOp lowering for function-style isType()/asType()
|
|
|
|
|
|
if (name == "isType" || name == "asType") && arguments.len() == 2 {
|
|
|
|
|
|
if let Some(type_name) = Self::extract_string_literal(&arguments[1]) {
|
|
|
|
|
|
let val = self.build_expression(arguments[0].clone())?;
|
|
|
|
|
|
let ty = Self::parse_type_name_to_mir(&type_name);
|
|
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
let op = if name == "isType" {
|
|
|
|
|
|
super::TypeOpKind::Check
|
|
|
|
|
|
} else {
|
|
|
|
|
|
super::TypeOpKind::Cast
|
|
|
|
|
|
};
|
|
|
|
|
|
self.emit_instruction(MirInstruction::TypeOp {
|
|
|
|
|
|
dst,
|
|
|
|
|
|
op,
|
|
|
|
|
|
value: val,
|
|
|
|
|
|
ty,
|
|
|
|
|
|
})?;
|
2025-08-24 00:05:12 +09:00
|
|
|
|
return Ok(dst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-12 11:33:48 +00:00
|
|
|
|
self.build_function_call(name.clone(), arguments.clone())
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
ASTNode::Call {
|
|
|
|
|
|
callee, arguments, ..
|
|
|
|
|
|
} => {
|
2025-09-04 03:41:02 +09:00
|
|
|
|
// P1.5: Lambdaはインライン、それ以外は Call に正規化
|
2025-09-03 20:03:45 +09:00
|
|
|
|
if let ASTNode::Lambda { params, body, .. } = callee.as_ref() {
|
|
|
|
|
|
if params.len() != arguments.len() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
return Err(format!(
|
|
|
|
|
|
"Lambda expects {} args, got {}",
|
|
|
|
|
|
params.len(),
|
|
|
|
|
|
arguments.len()
|
|
|
|
|
|
));
|
2025-09-03 20:03:45 +09:00
|
|
|
|
}
|
|
|
|
|
|
let mut arg_vals: Vec<ValueId> = Vec::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
for a in arguments {
|
|
|
|
|
|
arg_vals.push(self.build_expression(a)?);
|
|
|
|
|
|
}
|
2025-09-03 20:03:45 +09:00
|
|
|
|
let saved_vars = self.variable_map.clone();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
for (p, v) in params.iter().zip(arg_vals.iter()) {
|
|
|
|
|
|
self.variable_map.insert(p.clone(), *v);
|
|
|
|
|
|
}
|
|
|
|
|
|
let prog = ASTNode::Program {
|
|
|
|
|
|
statements: body.clone(),
|
|
|
|
|
|
span: crate::ast::Span::unknown(),
|
|
|
|
|
|
};
|
2025-09-03 20:03:45 +09:00
|
|
|
|
let out = self.build_expression(prog)?;
|
|
|
|
|
|
self.variable_map = saved_vars;
|
|
|
|
|
|
Ok(out)
|
|
|
|
|
|
} else {
|
2025-09-04 03:41:02 +09:00
|
|
|
|
// callee/args を評価し、Call を発行(VM 側で FunctionBox/関数名の両対応)
|
|
|
|
|
|
let callee_id = self.build_expression(*callee.clone())?;
|
|
|
|
|
|
let mut arg_ids = Vec::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
for a in arguments {
|
|
|
|
|
|
arg_ids.push(self.build_expression(a)?);
|
|
|
|
|
|
}
|
2025-09-04 03:41:02 +09:00
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::Call {
|
|
|
|
|
|
dst: Some(dst),
|
|
|
|
|
|
func: callee_id,
|
|
|
|
|
|
args: arg_ids,
|
|
|
|
|
|
effects: EffectMask::PURE,
|
|
|
|
|
|
})?;
|
2025-09-04 03:41:02 +09:00
|
|
|
|
Ok(dst)
|
2025-09-03 20:03:45 +09:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 20:03:45 +09:00
|
|
|
|
ASTNode::QMarkPropagate { expression, .. } => {
|
|
|
|
|
|
// Lower: ok = expr.isOk(); br ok then else; else => return expr; then => expr.getValue()
|
|
|
|
|
|
let res_val = self.build_expression(*expression.clone())?;
|
|
|
|
|
|
let ok_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::BoxCall {
|
|
|
|
|
|
dst: Some(ok_id),
|
|
|
|
|
|
box_val: res_val,
|
|
|
|
|
|
method: "isOk".to_string(),
|
|
|
|
|
|
method_id: None,
|
|
|
|
|
|
args: vec![],
|
|
|
|
|
|
effects: EffectMask::PURE,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
let then_block = self.block_gen.next();
|
|
|
|
|
|
let else_block = self.block_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::Branch {
|
|
|
|
|
|
condition: ok_id,
|
|
|
|
|
|
then_bb: then_block,
|
|
|
|
|
|
else_bb: else_block,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
// else: return res_val
|
|
|
|
|
|
self.current_block = Some(else_block);
|
|
|
|
|
|
self.ensure_block_exists(else_block)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::Return {
|
|
|
|
|
|
value: Some(res_val),
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
// then: getValue()
|
|
|
|
|
|
self.current_block = Some(then_block);
|
|
|
|
|
|
self.ensure_block_exists(then_block)?;
|
|
|
|
|
|
let val_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::BoxCall {
|
|
|
|
|
|
dst: Some(val_id),
|
|
|
|
|
|
box_val: res_val,
|
|
|
|
|
|
method: "getValue".to_string(),
|
|
|
|
|
|
method_id: None,
|
|
|
|
|
|
args: vec![],
|
|
|
|
|
|
effects: EffectMask::PURE,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
self.value_types.insert(val_id, super::MirType::Unknown);
|
|
|
|
|
|
Ok(val_id)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Print { expression, .. } => self.build_print_statement(*expression.clone()),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Program { statements, .. } => self.build_block(statements.clone()),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::If {
|
|
|
|
|
|
condition,
|
|
|
|
|
|
then_body,
|
|
|
|
|
|
else_body,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let else_ast = if let Some(else_statements) = else_body {
|
|
|
|
|
|
Some(ASTNode::Program {
|
|
|
|
|
|
statements: else_statements.clone(),
|
|
|
|
|
|
span: crate::ast::Span::unknown(),
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
self.build_if_statement(
|
2025-09-17 07:43:07 +09:00
|
|
|
|
*condition.clone(),
|
2025-08-12 11:33:48 +00:00
|
|
|
|
ASTNode::Program {
|
|
|
|
|
|
statements: then_body.clone(),
|
|
|
|
|
|
span: crate::ast::Span::unknown(),
|
|
|
|
|
|
},
|
2025-09-17 07:43:07 +09:00
|
|
|
|
else_ast,
|
2025-08-12 11:33:48 +00:00
|
|
|
|
)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Loop {
|
|
|
|
|
|
condition, body, ..
|
|
|
|
|
|
} => self.build_loop_statement(*condition.clone(), body.clone()),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::TryCatch {
|
|
|
|
|
|
try_body,
|
|
|
|
|
|
catch_clauses,
|
|
|
|
|
|
finally_body,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => self.build_try_catch_statement(
|
|
|
|
|
|
try_body.clone(),
|
|
|
|
|
|
catch_clauses.clone(),
|
|
|
|
|
|
finally_body.clone(),
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Throw { expression, .. } => self.build_throw_statement(*expression.clone()),
|
2025-09-03 20:03:45 +09:00
|
|
|
|
|
|
|
|
|
|
// P1: Lower peek expression into if-else chain with phi
|
2025-09-17 07:43:07 +09:00
|
|
|
|
ASTNode::PeekExpr {
|
|
|
|
|
|
scrutinee,
|
|
|
|
|
|
arms,
|
|
|
|
|
|
else_expr,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => {
|
2025-09-03 20:03:45 +09:00
|
|
|
|
// Evaluate scrutinee once
|
|
|
|
|
|
let scr_val = self.build_expression(*scrutinee.clone())?;
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare a merge block and collect phi inputs
|
|
|
|
|
|
let merge_block = self.block_gen.next();
|
|
|
|
|
|
let mut phi_inputs: Vec<(super::BasicBlockId, super::ValueId)> = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
// Start chaining from the current block
|
|
|
|
|
|
for (lit, arm_expr) in arms.into_iter() {
|
|
|
|
|
|
// Build condition: scr_val == lit
|
|
|
|
|
|
let lit_id = self.build_literal(lit)?;
|
|
|
|
|
|
let cond_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(super::MirInstruction::Compare {
|
|
|
|
|
|
dst: cond_id,
|
|
|
|
|
|
op: super::CompareOp::Eq,
|
|
|
|
|
|
lhs: scr_val,
|
|
|
|
|
|
rhs: lit_id,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
|
|
|
|
|
|
// Create then and next blocks
|
|
|
|
|
|
let then_block = self.block_gen.next();
|
|
|
|
|
|
let next_block = self.block_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(super::MirInstruction::Branch {
|
|
|
|
|
|
condition: cond_id,
|
|
|
|
|
|
then_bb: then_block,
|
|
|
|
|
|
else_bb: next_block,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
|
|
|
|
|
|
// then: evaluate arm expr, jump to merge
|
|
|
|
|
|
self.current_block = Some(then_block);
|
|
|
|
|
|
self.ensure_block_exists(then_block)?;
|
|
|
|
|
|
let then_val = self.build_expression(arm_expr)?;
|
|
|
|
|
|
if !self.is_current_block_terminated() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(super::MirInstruction::Jump {
|
|
|
|
|
|
target: merge_block,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
}
|
|
|
|
|
|
phi_inputs.push((then_block, then_val));
|
|
|
|
|
|
|
|
|
|
|
|
// else path continues chaining
|
|
|
|
|
|
self.current_block = Some(next_block);
|
|
|
|
|
|
self.ensure_block_exists(next_block)?;
|
|
|
|
|
|
// Loop continues from next_block
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Final else branch
|
|
|
|
|
|
let cur_block = self.current_block.ok_or("No current basic block")?;
|
|
|
|
|
|
let else_val = self.build_expression(*else_expr.clone())?;
|
|
|
|
|
|
if !self.is_current_block_terminated() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(super::MirInstruction::Jump {
|
|
|
|
|
|
target: merge_block,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
}
|
|
|
|
|
|
phi_inputs.push((cur_block, else_val));
|
|
|
|
|
|
|
|
|
|
|
|
// Merge and phi
|
|
|
|
|
|
self.current_block = Some(merge_block);
|
|
|
|
|
|
self.ensure_block_exists(merge_block)?;
|
|
|
|
|
|
let result_val = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(super::MirInstruction::Phi {
|
|
|
|
|
|
dst: result_val,
|
|
|
|
|
|
inputs: phi_inputs,
|
|
|
|
|
|
})?;
|
2025-09-03 20:03:45 +09:00
|
|
|
|
Ok(result_val)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 20:03:45 +09:00
|
|
|
|
ASTNode::Lambda { params, body, .. } => {
|
2025-09-04 03:41:02 +09:00
|
|
|
|
// Lambda→FunctionBox 値 Lower(最小 + 簡易キャプチャ解析)
|
2025-09-03 20:03:45 +09:00
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-04 03:41:02 +09:00
|
|
|
|
// Collect free variable names: variables used in body but not in params, and not 'me'/'this'
|
|
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
let mut used: HashSet<String> = HashSet::new();
|
|
|
|
|
|
let mut locals: HashSet<String> = HashSet::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
for p in params.iter() {
|
|
|
|
|
|
locals.insert(p.clone());
|
|
|
|
|
|
}
|
|
|
|
|
|
for st in body.iter() {
|
|
|
|
|
|
vars::collect_free_vars(st, &mut used, &mut locals);
|
|
|
|
|
|
}
|
2025-09-04 03:41:02 +09:00
|
|
|
|
// Materialize captures from current variable_map if known
|
|
|
|
|
|
let mut captures: Vec<(String, ValueId)> = Vec::new();
|
|
|
|
|
|
for name in used.into_iter() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if let Some(&vid) = self.variable_map.get(&name) {
|
|
|
|
|
|
captures.push((name, vid));
|
|
|
|
|
|
}
|
2025-09-04 03:41:02 +09:00
|
|
|
|
}
|
|
|
|
|
|
// me capture(存在すれば)
|
|
|
|
|
|
let me = self.variable_map.get("me").copied();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::FunctionNew {
|
|
|
|
|
|
dst,
|
|
|
|
|
|
params: params.clone(),
|
|
|
|
|
|
body: body.clone(),
|
|
|
|
|
|
captures,
|
|
|
|
|
|
me,
|
|
|
|
|
|
})?;
|
|
|
|
|
|
self.value_types
|
|
|
|
|
|
.insert(dst, super::MirType::Box("FunctionBox".to_string()));
|
2025-09-03 20:03:45 +09:00
|
|
|
|
Ok(dst)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Return { value, .. } => self.build_return_statement(value.clone()),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::Local {
|
|
|
|
|
|
variables,
|
|
|
|
|
|
initial_values,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => self.build_local_statement(variables.clone(), initial_values.clone()),
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::BoxDeclaration {
|
|
|
|
|
|
name,
|
|
|
|
|
|
methods,
|
|
|
|
|
|
is_static,
|
|
|
|
|
|
fields,
|
|
|
|
|
|
constructors,
|
|
|
|
|
|
weak_fields,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => {
|
2025-08-13 07:53:14 +00:00
|
|
|
|
if is_static && name == "Main" {
|
2025-08-29 21:39:47 +09:00
|
|
|
|
self.build_static_main_box(name.clone(), methods.clone())
|
2025-08-13 07:53:14 +00:00
|
|
|
|
} else {
|
2025-08-14 01:48:40 +00:00
|
|
|
|
// Support user-defined boxes - handle as statement, return void
|
2025-08-21 01:18:25 +09:00
|
|
|
|
// Track as user-defined (eligible for method lowering)
|
|
|
|
|
|
self.user_defined_boxes.insert(name.clone());
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.build_box_declaration(
|
|
|
|
|
|
name.clone(),
|
|
|
|
|
|
methods.clone(),
|
|
|
|
|
|
fields.clone(),
|
|
|
|
|
|
weak_fields.clone(),
|
|
|
|
|
|
)?;
|
2025-08-20 18:57:10 +09:00
|
|
|
|
|
|
|
|
|
|
// Phase 2: Lower constructors (birth/N) into MIR functions
|
|
|
|
|
|
// Function name pattern: "{BoxName}.{constructor_key}" (e.g., "Person.birth/1")
|
|
|
|
|
|
for (ctor_key, ctor_ast) in constructors.clone() {
|
|
|
|
|
|
if let ASTNode::FunctionDeclaration { params, body, .. } = ctor_ast {
|
|
|
|
|
|
let func_name = format!("{}.{}", name, ctor_key);
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.lower_method_as_function(
|
|
|
|
|
|
func_name,
|
|
|
|
|
|
name.clone(),
|
|
|
|
|
|
params.clone(),
|
|
|
|
|
|
body.clone(),
|
|
|
|
|
|
)?;
|
2025-08-20 18:57:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Phase 3: Lower instance methods into MIR functions
|
|
|
|
|
|
// Function name pattern: "{BoxName}.{method}/{N}"
|
|
|
|
|
|
for (method_name, method_ast) in methods.clone() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if let ASTNode::FunctionDeclaration {
|
|
|
|
|
|
params,
|
|
|
|
|
|
body,
|
|
|
|
|
|
is_static,
|
|
|
|
|
|
..
|
|
|
|
|
|
} = method_ast
|
|
|
|
|
|
{
|
2025-08-20 18:57:10 +09:00
|
|
|
|
if !is_static {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
let func_name = format!(
|
|
|
|
|
|
"{}.{}{}",
|
|
|
|
|
|
name,
|
|
|
|
|
|
method_name,
|
|
|
|
|
|
format!("/{}", params.len())
|
|
|
|
|
|
);
|
|
|
|
|
|
self.lower_method_as_function(
|
|
|
|
|
|
func_name,
|
|
|
|
|
|
name.clone(),
|
|
|
|
|
|
params.clone(),
|
|
|
|
|
|
body.clone(),
|
|
|
|
|
|
)?;
|
2025-08-20 18:57:10 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-14 01:48:40 +00:00
|
|
|
|
// Return a void value since this is a statement
|
|
|
|
|
|
let void_val = self.value_gen.next();
|
|
|
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
|
dst: void_val,
|
|
|
|
|
|
value: ConstValue::Void,
|
|
|
|
|
|
})?;
|
|
|
|
|
|
Ok(void_val)
|
2025-08-13 07:53:14 +00:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 09:45:22 +00:00
|
|
|
|
ASTNode::FieldAccess { object, field, .. } => {
|
|
|
|
|
|
self.build_field_access(*object.clone(), field.clone())
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASTNode::New {
|
|
|
|
|
|
class, arguments, ..
|
|
|
|
|
|
} => self.build_new_expression(class.clone(), arguments.clone()),
|
|
|
|
|
|
|
2025-09-16 06:13:44 +09:00
|
|
|
|
ASTNode::ArrayLiteral { elements, .. } => {
|
|
|
|
|
|
// Lower: new ArrayBox(); for each elem: .push(elem)
|
|
|
|
|
|
let arr_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::NewBox {
|
|
|
|
|
|
dst: arr_id,
|
|
|
|
|
|
box_type: "ArrayBox".to_string(),
|
|
|
|
|
|
args: vec![],
|
|
|
|
|
|
})?;
|
2025-09-16 06:13:44 +09:00
|
|
|
|
for e in elements {
|
|
|
|
|
|
let v = self.build_expression(e)?;
|
2025-09-17 07:43:07 +09:00
|
|
|
|
self.emit_instruction(MirInstruction::BoxCall {
|
|
|
|
|
|
dst: None,
|
|
|
|
|
|
box_val: arr_id,
|
|
|
|
|
|
method: "push".to_string(),
|
|
|
|
|
|
method_id: None,
|
|
|
|
|
|
args: vec![v],
|
|
|
|
|
|
effects: super::EffectMask::MUT,
|
|
|
|
|
|
})?;
|
2025-09-16 06:13:44 +09:00
|
|
|
|
}
|
|
|
|
|
|
Ok(arr_id)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-13 11:48:53 +00:00
|
|
|
|
// Phase 7: Async operations
|
2025-09-17 07:43:07 +09:00
|
|
|
|
ASTNode::Nowait {
|
|
|
|
|
|
variable,
|
|
|
|
|
|
expression,
|
|
|
|
|
|
..
|
|
|
|
|
|
} => self.build_nowait_statement(variable.clone(), *expression.clone()),
|
|
|
|
|
|
|
2025-08-13 11:48:53 +00:00
|
|
|
|
ASTNode::AwaitExpression { expression, .. } => {
|
|
|
|
|
|
self.build_await_expression(*expression.clone())
|
2025-09-17 07:43:07 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-30 22:52:16 +09:00
|
|
|
|
ASTNode::Include { filename, .. } => {
|
|
|
|
|
|
// Resolve and read included file
|
2025-09-03 05:04:56 +09:00
|
|
|
|
let mut path = utils::resolve_include_path_builder(&filename);
|
2025-08-30 22:52:16 +09:00
|
|
|
|
if std::path::Path::new(&path).is_dir() {
|
|
|
|
|
|
path = format!("{}/index.nyash", path.trim_end_matches('/'));
|
|
|
|
|
|
} else if std::path::Path::new(&path).extension().is_none() {
|
|
|
|
|
|
path.push_str(".nyash");
|
|
|
|
|
|
}
|
2025-08-30 23:47:08 +09:00
|
|
|
|
// Cycle detection
|
|
|
|
|
|
if self.include_loading.contains(&path) {
|
|
|
|
|
|
return Err(format!("Circular include detected: {}", path));
|
|
|
|
|
|
}
|
|
|
|
|
|
// Cache hit: build only the instance
|
|
|
|
|
|
if let Some(name) = self.include_box_map.get(&path).cloned() {
|
|
|
|
|
|
return self.build_new_expression(name, vec![]);
|
|
|
|
|
|
}
|
|
|
|
|
|
self.include_loading.insert(path.clone());
|
2025-08-30 22:52:16 +09:00
|
|
|
|
let content = fs::read_to_string(&path)
|
|
|
|
|
|
.map_err(|e| format!("Include read error '{}': {}", filename, e))?;
|
|
|
|
|
|
// Parse to AST
|
|
|
|
|
|
let included_ast = crate::parser::NyashParser::parse_from_string(&content)
|
|
|
|
|
|
.map_err(|e| format!("Include parse error '{}': {:?}", filename, e))?;
|
|
|
|
|
|
// Find first static box name
|
|
|
|
|
|
let mut box_name: Option<String> = None;
|
|
|
|
|
|
if let crate::ast::ASTNode::Program { statements, .. } = &included_ast {
|
|
|
|
|
|
for st in statements {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if let crate::ast::ASTNode::BoxDeclaration {
|
|
|
|
|
|
name, is_static, ..
|
|
|
|
|
|
} = st
|
|
|
|
|
|
{
|
|
|
|
|
|
if *is_static {
|
|
|
|
|
|
box_name = Some(name.clone());
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
let bname = box_name
|
|
|
|
|
|
.ok_or_else(|| format!("Include target '{}' has no static box", filename))?;
|
2025-08-30 22:52:16 +09:00
|
|
|
|
// Lower included AST into current MIR (register types/methods)
|
|
|
|
|
|
let _ = self.build_expression(included_ast)?;
|
2025-08-30 23:47:08 +09:00
|
|
|
|
// Mark caches
|
|
|
|
|
|
self.include_loading.remove(&path);
|
|
|
|
|
|
self.include_box_map.insert(path.clone(), bname.clone());
|
2025-08-30 22:52:16 +09:00
|
|
|
|
// Return a new instance of included box (no args)
|
|
|
|
|
|
self.build_new_expression(bname, vec![])
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
|
|
|
|
|
_ => Err(format!("Unsupported AST node type: {:?}", ast)),
|
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 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-09-03 05:04:56 +09:00
|
|
|
|
// build_binary_op moved to builder/ops.rs
|
|
|
|
|
|
// build_unary_op moved to builder/ops.rs
|
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 {
|
|
|
|
|
|
Err(format!("Undefined variable: {}", name))
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
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-09-03 05:04:56 +09:00
|
|
|
|
// build_function_call_legacy removed (use builder_calls::build_function_call)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_print_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_block_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_if_statement_legacy moved to builder/stmts.rs
|
2025-08-23 06:51:49 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// extract_assigned_var moved to builder/stmts.rs (as module helper)
|
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
|
|
|
|
|
|
|
|
|
|
pub(super) fn is_no_phi_mode(&self) -> bool {
|
|
|
|
|
|
self.no_phi_mode
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Insert a Copy instruction into `block_id`, defining `dst` from `src`.
|
|
|
|
|
|
/// Skips blocks that terminate via return/throw, and avoids duplicate copies.
|
|
|
|
|
|
pub(super) fn insert_edge_copy(
|
|
|
|
|
|
&mut self,
|
|
|
|
|
|
block_id: BasicBlockId,
|
|
|
|
|
|
dst: ValueId,
|
|
|
|
|
|
src: ValueId,
|
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
|
if let Some(ref mut function) = self.current_function {
|
|
|
|
|
|
let block = function
|
|
|
|
|
|
.get_block_mut(block_id)
|
|
|
|
|
|
.ok_or_else(|| format!("Basic block {} does not exist", block_id))?;
|
|
|
|
|
|
if let Some(term) = &block.terminator {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
if matches!(
|
|
|
|
|
|
term,
|
|
|
|
|
|
MirInstruction::Return { .. } | MirInstruction::Throw { .. }
|
|
|
|
|
|
) {
|
2025-09-16 23:49:36 +09:00
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
let already_present = block.instructions.iter().any(|inst| {
|
|
|
|
|
|
matches!(inst, MirInstruction::Copy { dst: existing_dst, .. } if *existing_dst == dst)
|
|
|
|
|
|
});
|
|
|
|
|
|
if !already_present {
|
|
|
|
|
|
block.add_instruction(MirInstruction::Copy { dst, src });
|
|
|
|
|
|
}
|
|
|
|
|
|
if let Some(ty) = self.value_types.get(&src).cloned() {
|
|
|
|
|
|
self.value_types.insert(dst, ty);
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Err("No current function".to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
🎉 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
|
|
|
|
// moved to builder/utils.rs: ensure_block_exists
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_loop_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_try_catch_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_throw_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_local_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_return_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
🎉 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
|
|
|
|
// moved to builder/decls.rs: build_static_main_box
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
🎉 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
|
|
|
|
// moved to builder/fields.rs: build_field_access
|
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-08-28 22:31:51 +09:00
|
|
|
|
// Annotate primitive boxes
|
|
|
|
|
|
match class.as_str() {
|
2025-09-17 07:43:07 +09:00
|
|
|
|
"IntegerBox" => {
|
|
|
|
|
|
self.value_types.insert(dst, super::MirType::Integer);
|
|
|
|
|
|
}
|
|
|
|
|
|
"FloatBox" => {
|
|
|
|
|
|
self.value_types.insert(dst, super::MirType::Float);
|
|
|
|
|
|
}
|
|
|
|
|
|
"BoolBox" => {
|
|
|
|
|
|
self.value_types.insert(dst, super::MirType::Bool);
|
|
|
|
|
|
}
|
|
|
|
|
|
"StringBox" => {
|
|
|
|
|
|
self.value_types.insert(dst, super::MirType::String);
|
|
|
|
|
|
}
|
|
|
|
|
|
other => {
|
|
|
|
|
|
self.value_types
|
|
|
|
|
|
.insert(dst, super::MirType::Box(other.to_string()));
|
|
|
|
|
|
}
|
2025-08-28 22:31:51 +09:00
|
|
|
|
}
|
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
|
|
|
|
|
🎉 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
|
|
|
|
// moved to builder/fields.rs: build_field_assignment
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
🎉 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
|
|
|
|
// moved to builder/utils.rs: start_new_block
|
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-09-03 05:04:56 +09:00
|
|
|
|
// convert_binary_operator moved to builder/ops.rs
|
|
|
|
|
|
// convert_unary_operator moved to builder/ops.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_nowait_statement_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_await_expression_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_me_expression_legacy moved to builder/stmts.rs
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// build_method_call_legacy removed (use builder_calls::build_method_call)
|
2025-08-29 21:39:47 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// parse_type_name_to_mir_legacy removed (use builder_calls::parse_type_name_to_mir)
|
|
|
|
|
|
// extract_string_literal_legacy removed (use builder_calls::extract_string_literal)
|
|
|
|
|
|
// build_from_expression_legacy removed (use builder_calls::build_from_expression)
|
2025-08-29 21:39:47 +09:00
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// lower_static_method_as_function_legacy removed (use builder_calls::lower_static_method_as_function)
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
🎉 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
|
|
|
|
// moved to builder/decls.rs: build_box_declaration
|
2025-08-12 11:33:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
|
// BinaryOpType moved to builder/ops.rs
|
2025-08-12 11:33:48 +00:00
|
|
|
|
|
|
|
|
|
|
impl Default for MirBuilder {
|
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
Self::new()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
|
mod tests {
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
use crate::ast::{ASTNode, LiteralValue, Span};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_literal_building() {
|
|
|
|
|
|
let mut builder = MirBuilder::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let ast = ASTNode::Literal {
|
|
|
|
|
|
value: LiteralValue::Integer(42),
|
|
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let result = builder.build_module(ast);
|
|
|
|
|
|
assert!(result.is_ok());
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let module = result.unwrap();
|
|
|
|
|
|
assert_eq!(module.function_names().len(), 1);
|
|
|
|
|
|
assert!(module.get_function("main").is_some());
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_binary_op_building() {
|
|
|
|
|
|
let mut builder = MirBuilder::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let ast = ASTNode::BinaryOp {
|
|
|
|
|
|
left: Box::new(ASTNode::Literal {
|
|
|
|
|
|
value: LiteralValue::Integer(10),
|
|
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
}),
|
|
|
|
|
|
operator: BinaryOperator::Add,
|
|
|
|
|
|
right: Box::new(ASTNode::Literal {
|
|
|
|
|
|
value: LiteralValue::Integer(32),
|
|
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
}),
|
|
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let result = builder.build_module(ast);
|
|
|
|
|
|
assert!(result.is_ok());
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let module = result.unwrap();
|
|
|
|
|
|
let function = module.get_function("main").unwrap();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
// Should have constants and binary operation
|
|
|
|
|
|
let stats = function.stats();
|
|
|
|
|
|
assert!(stats.instruction_count >= 3); // 2 constants + 1 binop + 1 return
|
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
#[test]
|
|
|
|
|
|
fn test_if_statement_building() {
|
|
|
|
|
|
let mut builder = MirBuilder::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-13 11:53:34 +09:00
|
|
|
|
// Adapt test to current AST: If with statement bodies
|
|
|
|
|
|
let ast = ASTNode::If {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
condition: Box::new(ASTNode::Literal {
|
2025-08-13 11:53:34 +09:00
|
|
|
|
value: LiteralValue::Bool(true),
|
2025-08-12 11:33:48 +00:00
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
}),
|
2025-08-13 11:53:34 +09:00
|
|
|
|
then_body: vec![ASTNode::Literal {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
value: LiteralValue::Integer(1),
|
|
|
|
|
|
span: Span::unknown(),
|
2025-08-13 11:53:34 +09:00
|
|
|
|
}],
|
|
|
|
|
|
else_body: Some(vec![ASTNode::Literal {
|
2025-08-12 11:33:48 +00:00
|
|
|
|
value: LiteralValue::Integer(2),
|
|
|
|
|
|
span: Span::unknown(),
|
2025-08-13 11:53:34 +09:00
|
|
|
|
}]),
|
2025-08-12 11:33:48 +00:00
|
|
|
|
span: Span::unknown(),
|
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let result = builder.build_module(ast);
|
|
|
|
|
|
assert!(result.is_ok());
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
let module = result.unwrap();
|
|
|
|
|
|
let function = module.get_function("main").unwrap();
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
// Should have multiple blocks for if/then/else/merge
|
|
|
|
|
|
assert!(function.blocks.len() >= 3);
|
2025-09-17 07:43:07 +09:00
|
|
|
|
|
2025-08-12 11:33:48 +00:00
|
|
|
|
// Should have phi function in merge block
|
|
|
|
|
|
let stats = function.stats();
|
|
|
|
|
|
assert!(stats.phi_count >= 1);
|
|
|
|
|
|
}
|
2025-08-13 11:53:34 +09:00
|
|
|
|
}
|