refactor: 大規模なモジュールを分割し、コードの構造を改善
runner, mir/builder, backend/llvm の各モジュールが肥大化していたため、責務ごとにファイルを分割し、見通しを改善するリファクタリングを実施。
### `runner`
- `mod.rs` に集中していたロジックを、以下のモジュールに分割:
- `tasks.rs`: `nyash.toml` のタスク実行処理
- `build.rs`: AOTビルドパイプラインの実装
- `pipeline.rs`: `using` の解決など、パイプライン中のユーティリティ
- `demos.rs`: デモの実行処理
### `mir/builder`
- `if/else` 文のPHIノード生成ロジックを `stmts.rs` から `phi.rs` へ切り出し。
- `utils.rs` にあったPHI関連のヘルパーも `phi.rs` に集約。
- ASTから自由変数を収集するロジックを `vars.rs` へ切り出し。
### `backend/llvm/compiler/codegen`
- 巨大だった `lower_one_function` 関数を、`function.rs` モジュールとして分離。
- `sanitize_symbol` などのヘルパー関数を `utils.rs` へ移動。
This commit is contained in:
@ -16,6 +16,7 @@ use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
mod builder_calls;
|
||||
mod phi;
|
||||
mod stmts;
|
||||
mod ops;
|
||||
mod utils;
|
||||
@ -28,6 +29,7 @@ mod exprs_peek; // peek expression
|
||||
mod exprs_lambda; // lambda lowering
|
||||
mod exprs_include; // include lowering
|
||||
mod plugin_sigs; // plugin signature loader
|
||||
mod vars; // variables/scope helpers
|
||||
|
||||
// moved helpers to builder/utils.rs
|
||||
|
||||
@ -284,12 +286,12 @@ impl MirBuilder {
|
||||
if let super::MirInstruction::Return { value: Some(v) } = inst {
|
||||
if let Some(mt) = self.value_types.get(v).cloned() { inferred = Some(mt); break 'outer; }
|
||||
// 追加: v が PHI の場合は入力側の型から推定
|
||||
if let Some(mt) = utils::infer_type_from_phi(&function, *v, &self.value_types) { inferred = Some(mt); break 'outer; }
|
||||
if let Some(mt) = phi::infer_type_from_phi(&function, *v, &self.value_types) { inferred = Some(mt); break 'outer; }
|
||||
}
|
||||
}
|
||||
if let Some(super::MirInstruction::Return { value: Some(v) }) = &bb.terminator {
|
||||
if let Some(mt) = self.value_types.get(v).cloned() { inferred = Some(mt); break; }
|
||||
if let Some(mt) = utils::infer_type_from_phi(&function, *v, &self.value_types) { inferred = Some(mt); break; }
|
||||
if let Some(mt) = phi::infer_type_from_phi(&function, *v, &self.value_types) { inferred = Some(mt); break; }
|
||||
}
|
||||
}
|
||||
if let Some(mt) = inferred { function.signature.return_type = mt; }
|
||||
@ -524,52 +526,7 @@ impl MirBuilder {
|
||||
let mut used: HashSet<String> = HashSet::new();
|
||||
let mut locals: HashSet<String> = HashSet::new();
|
||||
for p in params.iter() { locals.insert(p.clone()); }
|
||||
fn collect_vars(node: &crate::ast::ASTNode, used: &mut HashSet<String>, locals: &mut HashSet<String>) {
|
||||
match node {
|
||||
crate::ast::ASTNode::Variable { name, .. } => {
|
||||
if name != "me" && name != "this" && !locals.contains(name) {
|
||||
used.insert(name.clone());
|
||||
}
|
||||
}
|
||||
crate::ast::ASTNode::Local { variables, .. } => { for v in variables { locals.insert(v.clone()); } }
|
||||
crate::ast::ASTNode::Assignment { target, value, .. } => { collect_vars(target, used, locals); collect_vars(value, used, locals); }
|
||||
crate::ast::ASTNode::BinaryOp { left, right, .. } => { collect_vars(left, used, locals); collect_vars(right, used, locals); }
|
||||
crate::ast::ASTNode::UnaryOp { operand, .. } => { collect_vars(operand, used, locals); }
|
||||
crate::ast::ASTNode::MethodCall { object, arguments, .. } => { collect_vars(object, used, locals); for a in arguments { collect_vars(a, used, locals); } }
|
||||
crate::ast::ASTNode::FunctionCall { arguments, .. } => { for a in arguments { collect_vars(a, used, locals); } }
|
||||
crate::ast::ASTNode::Call { callee, arguments, .. } => { collect_vars(callee, used, locals); for a in arguments { collect_vars(a, used, locals); } }
|
||||
crate::ast::ASTNode::FieldAccess { object, .. } => { collect_vars(object, used, locals); }
|
||||
crate::ast::ASTNode::New { arguments, .. } => { for a in arguments { collect_vars(a, used, locals); } }
|
||||
crate::ast::ASTNode::If { condition, then_body, else_body, .. } => {
|
||||
collect_vars(condition, used, locals);
|
||||
for st in then_body { collect_vars(st, used, locals); }
|
||||
if let Some(eb) = else_body { for st in eb { collect_vars(st, used, locals); } }
|
||||
}
|
||||
crate::ast::ASTNode::Loop { condition, body, .. } => { collect_vars(condition, used, locals); for st in body { collect_vars(st, used, locals); } }
|
||||
crate::ast::ASTNode::TryCatch { try_body, catch_clauses, finally_body, .. } => {
|
||||
for st in try_body { collect_vars(st, used, locals); }
|
||||
for c in catch_clauses { for st in &c.body { collect_vars(st, used, locals); } }
|
||||
if let Some(fb) = finally_body { for st in fb { collect_vars(st, used, locals); } }
|
||||
}
|
||||
crate::ast::ASTNode::Throw { expression, .. } => { collect_vars(expression, used, locals); }
|
||||
crate::ast::ASTNode::Print { expression, .. } => { collect_vars(expression, used, locals); }
|
||||
crate::ast::ASTNode::Return { value, .. } => { if let Some(v) = value { collect_vars(v, used, locals); } }
|
||||
crate::ast::ASTNode::AwaitExpression { expression, .. } => { collect_vars(expression, used, locals); }
|
||||
crate::ast::ASTNode::PeekExpr { scrutinee, arms, else_expr, .. } => {
|
||||
collect_vars(scrutinee, used, locals);
|
||||
for (_, e) in arms { collect_vars(e, used, locals); }
|
||||
collect_vars(else_expr, used, locals);
|
||||
}
|
||||
crate::ast::ASTNode::Program { statements, .. } => { for st in statements { collect_vars(st, used, locals); } }
|
||||
crate::ast::ASTNode::FunctionDeclaration { params, body, .. } => {
|
||||
let mut inner = locals.clone();
|
||||
for p in params { inner.insert(p.clone()); }
|
||||
for st in body { collect_vars(st, used, &mut inner); }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
for st in body.iter() { collect_vars(st, &mut used, &mut locals); }
|
||||
for st in body.iter() { vars::collect_free_vars(st, &mut used, &mut locals); }
|
||||
// Materialize captures from current variable_map if known
|
||||
let mut captures: Vec<(String, ValueId)> = Vec::new();
|
||||
for name in used.into_iter() {
|
||||
|
||||
99
src/mir/builder/phi.rs
Normal file
99
src/mir/builder/phi.rs
Normal file
@ -0,0 +1,99 @@
|
||||
|
||||
use crate::mir::{MirFunction, ValueId, MirType, MirInstruction, BasicBlockId};
|
||||
use std::collections::HashMap;
|
||||
use crate::ast::ASTNode;
|
||||
use super::MirBuilder;
|
||||
|
||||
// PHI-based return type inference helper
|
||||
pub(super) fn infer_type_from_phi(
|
||||
function: &MirFunction,
|
||||
ret_val: ValueId,
|
||||
types: &HashMap<ValueId, MirType>,
|
||||
) -> Option<MirType> {
|
||||
for (_bid, bb) in function.blocks.iter() {
|
||||
for inst in bb.instructions.iter() {
|
||||
if let MirInstruction::Phi { dst, inputs } = inst {
|
||||
if *dst == ret_val {
|
||||
let mut it = inputs.iter().filter_map(|(_, v)| types.get(v));
|
||||
if let Some(first) = it.next() {
|
||||
if it.all(|mt| mt == first) {
|
||||
return Some(first.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// Local helper for if-statement analysis (moved from stmts.rs)
|
||||
pub(super) fn extract_assigned_var(ast: &ASTNode) -> Option<String> {
|
||||
match ast {
|
||||
ASTNode::Assignment { target, .. } => {
|
||||
if let ASTNode::Variable { name, .. } = target.as_ref() { Some(name.clone()) } else { None }
|
||||
}
|
||||
ASTNode::Program { statements, .. } => statements.last().and_then(|st| extract_assigned_var(st)),
|
||||
ASTNode::If { then_body, else_body, .. } => {
|
||||
// Look into nested if: if both sides assign the same variable, propagate that name upward.
|
||||
let then_prog = ASTNode::Program { statements: then_body.clone(), span: crate::ast::Span::unknown() };
|
||||
let tvar = extract_assigned_var(&then_prog);
|
||||
let evar = else_body.as_ref().and_then(|eb| {
|
||||
let ep = ASTNode::Program { statements: eb.clone(), span: crate::ast::Span::unknown() };
|
||||
extract_assigned_var(&ep)
|
||||
});
|
||||
match (tvar, evar) {
|
||||
(Some(tv), Some(ev)) if tv == ev => Some(tv),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
impl MirBuilder {
|
||||
/// Normalize Phi creation for if/else constructs.
|
||||
/// This handles variable reassignment patterns and ensures a single exit value.
|
||||
pub(super) fn normalize_if_else_phi(
|
||||
&mut self,
|
||||
then_block: BasicBlockId,
|
||||
else_block: BasicBlockId,
|
||||
then_value_raw: ValueId,
|
||||
else_value_raw: ValueId,
|
||||
pre_if_var_map: &HashMap<String, ValueId>,
|
||||
then_ast_for_analysis: &ASTNode,
|
||||
else_ast_for_analysis: &Option<ASTNode>,
|
||||
then_var_map_end: &HashMap<String, ValueId>,
|
||||
else_var_map_end_opt: &Option<HashMap<String, ValueId>>,
|
||||
pre_then_var_value: Option<ValueId>,
|
||||
) -> Result<ValueId, String> {
|
||||
// If only the then-branch assigns a variable (e.g., `if c { x = ... }`) and the else
|
||||
// does not assign the same variable, bind that variable to a Phi of (then_value, pre_if_value).
|
||||
let assigned_var_then = extract_assigned_var(then_ast_for_analysis);
|
||||
let assigned_var_else = else_ast_for_analysis.as_ref().and_then(|a| extract_assigned_var(a));
|
||||
let result_val = self.value_gen.next();
|
||||
|
||||
if let Some(var_name) = assigned_var_then.clone() {
|
||||
let else_assigns_same = assigned_var_else.as_ref().map(|s| s == &var_name).unwrap_or(false);
|
||||
// Resolve branch-end values for the assigned variable
|
||||
let then_value_for_var = then_var_map_end.get(&var_name).copied().unwrap_or(then_value_raw);
|
||||
let else_value_for_var = if else_assigns_same {
|
||||
else_var_map_end_opt.as_ref().and_then(|m| m.get(&var_name).copied()).unwrap_or(else_value_raw)
|
||||
} else {
|
||||
// Else doesn't assign: use pre-if value if available
|
||||
pre_then_var_value.unwrap_or(else_value_raw)
|
||||
};
|
||||
// Emit Phi for the assigned variable and bind it
|
||||
self.emit_instruction(MirInstruction::Phi { dst: result_val, inputs: vec![(then_block, then_value_for_var), (else_block, else_value_for_var)] })?;
|
||||
self.variable_map = pre_if_var_map.clone();
|
||||
self.variable_map.insert(var_name, result_val);
|
||||
} else {
|
||||
// No variable assignment pattern detected – just emit Phi for expression result
|
||||
self.emit_instruction(MirInstruction::Phi { dst: result_val, inputs: vec![(then_block, then_value_raw), (else_block, else_value_raw)] })?;
|
||||
// Merge variable map conservatively to pre-if snapshot (no new bindings)
|
||||
self.variable_map = pre_if_var_map.clone();
|
||||
}
|
||||
|
||||
Ok(result_val)
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ use super::{MirInstruction, EffectMask, Effect, ConstValue, ValueId};
|
||||
use crate::mir::TypeOpKind;
|
||||
use crate::mir::loop_builder::LoopBuilder;
|
||||
use crate::ast::ASTNode;
|
||||
use super::phi::extract_assigned_var;
|
||||
|
||||
impl super::MirBuilder {
|
||||
// Print statement: env.console.log(value) with early TypeOp handling
|
||||
@ -140,31 +141,18 @@ impl super::MirBuilder {
|
||||
// merge + phi
|
||||
self.current_block = Some(merge_block);
|
||||
self.ensure_block_exists(merge_block)?;
|
||||
// If only the then-branch assigns a variable (e.g., `if c { x = ... }`) and the else
|
||||
// does not assign the same variable, bind that variable to a Phi of (then_value, pre_if_value).
|
||||
let assigned_var_then = extract_assigned_var(&then_ast_for_analysis);
|
||||
let assigned_var_else = else_ast_for_analysis.as_ref().and_then(|a| extract_assigned_var(a));
|
||||
let result_val = self.value_gen.next();
|
||||
if let Some(var_name) = assigned_var_then.clone() {
|
||||
let else_assigns_same = assigned_var_else.as_ref().map(|s| s == &var_name).unwrap_or(false);
|
||||
// Resolve branch-end values for the assigned variable
|
||||
let then_value_for_var = then_var_map_end.get(&var_name).copied().unwrap_or(then_value_raw);
|
||||
let else_value_for_var = if else_assigns_same {
|
||||
else_var_map_end_opt.as_ref().and_then(|m| m.get(&var_name).copied()).unwrap_or(else_value_raw)
|
||||
} else {
|
||||
// Else doesn't assign: use pre-if value if available
|
||||
pre_then_var_value.unwrap_or(else_value_raw)
|
||||
};
|
||||
// Emit Phi for the assigned variable and bind it
|
||||
self.emit_instruction(MirInstruction::Phi { dst: result_val, inputs: vec![(then_block, then_value_for_var), (else_block, else_value_for_var)] })?;
|
||||
self.variable_map = pre_if_var_map.clone();
|
||||
self.variable_map.insert(var_name, result_val);
|
||||
} else {
|
||||
// No variable assignment pattern detected – just emit Phi for expression result
|
||||
self.emit_instruction(MirInstruction::Phi { dst: result_val, inputs: vec![(then_block, then_value_raw), (else_block, else_value_raw)] })?;
|
||||
// Merge variable map conservatively to pre-if snapshot (no new bindings)
|
||||
self.variable_map = pre_if_var_map.clone();
|
||||
}
|
||||
let result_val = self.normalize_if_else_phi(
|
||||
then_block,
|
||||
else_block,
|
||||
then_value_raw,
|
||||
else_value_raw,
|
||||
&pre_if_var_map,
|
||||
&then_ast_for_analysis,
|
||||
&else_ast_for_analysis,
|
||||
&then_var_map_end,
|
||||
&else_var_map_end_opt,
|
||||
pre_then_var_value,
|
||||
)?;
|
||||
|
||||
Ok(result_val)
|
||||
}
|
||||
@ -328,26 +316,4 @@ impl super::MirBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
// Local helper for if-statement analysis
|
||||
fn extract_assigned_var(ast: &ASTNode) -> Option<String> {
|
||||
match ast {
|
||||
ASTNode::Assignment { target, .. } => {
|
||||
if let ASTNode::Variable { name, .. } = target.as_ref() { Some(name.clone()) } else { None }
|
||||
}
|
||||
ASTNode::Program { statements, .. } => statements.last().and_then(|st| extract_assigned_var(st)),
|
||||
ASTNode::If { then_body, else_body, .. } => {
|
||||
// Look into nested if: if both sides assign the same variable, propagate that name upward.
|
||||
let then_prog = ASTNode::Program { statements: then_body.clone(), span: crate::ast::Span::unknown() };
|
||||
let tvar = extract_assigned_var(&then_prog);
|
||||
let evar = else_body.as_ref().and_then(|eb| {
|
||||
let ep = ASTNode::Program { statements: eb.clone(), span: crate::ast::Span::unknown() };
|
||||
extract_assigned_var(&ep)
|
||||
});
|
||||
match (tvar, evar) {
|
||||
(Some(tv), Some(ev)) if tv == ev => Some(tv),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -41,28 +41,7 @@ pub(super) fn builder_debug_log(msg: &str) {
|
||||
}
|
||||
}
|
||||
|
||||
// PHI-based return type inference helper
|
||||
pub(super) fn infer_type_from_phi(
|
||||
function: &super::MirFunction,
|
||||
ret_val: super::ValueId,
|
||||
types: &std::collections::HashMap<super::ValueId, super::MirType>,
|
||||
) -> Option<super::MirType> {
|
||||
for (_bid, bb) in function.blocks.iter() {
|
||||
for inst in bb.instructions.iter() {
|
||||
if let super::MirInstruction::Phi { dst, inputs } = inst {
|
||||
if *dst == ret_val {
|
||||
let mut it = inputs.iter().filter_map(|(_, v)| types.get(v));
|
||||
if let Some(first) = it.next() {
|
||||
if it.all(|mt| mt == first) {
|
||||
return Some(first.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
// Lightweight helpers moved from builder.rs to reduce file size
|
||||
impl super::MirBuilder {
|
||||
|
||||
74
src/mir/builder/vars.rs
Normal file
74
src/mir/builder/vars.rs
Normal file
@ -0,0 +1,74 @@
|
||||
use crate::ast::ASTNode;
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// Collect free variables used in `node` into `used`, excluding names present in `locals`.
|
||||
/// `locals` is updated as new local declarations are encountered.
|
||||
pub(super) fn collect_free_vars(node: &ASTNode, used: &mut HashSet<String>, locals: &mut HashSet<String>) {
|
||||
match node {
|
||||
ASTNode::Variable { name, .. } => {
|
||||
if name != "me" && name != "this" && !locals.contains(name) {
|
||||
used.insert(name.clone());
|
||||
}
|
||||
}
|
||||
ASTNode::Local { variables, .. } => {
|
||||
for v in variables { locals.insert(v.clone()); }
|
||||
}
|
||||
ASTNode::Assignment { target, value, .. } => {
|
||||
collect_free_vars(target, used, locals);
|
||||
collect_free_vars(value, used, locals);
|
||||
}
|
||||
ASTNode::BinaryOp { left, right, .. } => {
|
||||
collect_free_vars(left, used, locals);
|
||||
collect_free_vars(right, used, locals);
|
||||
}
|
||||
ASTNode::UnaryOp { operand, .. } => { collect_free_vars(operand, used, locals); }
|
||||
ASTNode::MethodCall { object, arguments, .. } => {
|
||||
collect_free_vars(object, used, locals);
|
||||
for a in arguments { collect_free_vars(a, used, locals); }
|
||||
}
|
||||
ASTNode::FunctionCall { arguments, .. } => {
|
||||
for a in arguments { collect_free_vars(a, used, locals); }
|
||||
}
|
||||
ASTNode::Call { callee, arguments, .. } => {
|
||||
collect_free_vars(callee, used, locals);
|
||||
for a in arguments { collect_free_vars(a, used, locals); }
|
||||
}
|
||||
ASTNode::FieldAccess { object, .. } => { collect_free_vars(object, used, locals); }
|
||||
ASTNode::New { arguments, .. } => {
|
||||
for a in arguments { collect_free_vars(a, used, locals); }
|
||||
}
|
||||
ASTNode::If { condition, then_body, else_body, .. } => {
|
||||
collect_free_vars(condition, used, locals);
|
||||
for st in then_body { collect_free_vars(st, used, locals); }
|
||||
if let Some(eb) = else_body { for st in eb { collect_free_vars(st, used, locals); } }
|
||||
}
|
||||
ASTNode::Loop { condition, body, .. } => {
|
||||
collect_free_vars(condition, used, locals);
|
||||
for st in body { collect_free_vars(st, used, locals); }
|
||||
}
|
||||
ASTNode::TryCatch { try_body, catch_clauses, finally_body, .. } => {
|
||||
for st in try_body { collect_free_vars(st, used, locals); }
|
||||
for c in catch_clauses { for st in &c.body { collect_free_vars(st, used, locals); } }
|
||||
if let Some(fb) = finally_body { for st in fb { collect_free_vars(st, used, locals); } }
|
||||
}
|
||||
ASTNode::Throw { expression, .. } => { collect_free_vars(expression, used, locals); }
|
||||
ASTNode::Print { expression, .. } => { collect_free_vars(expression, used, locals); }
|
||||
ASTNode::Return { value, .. } => { if let Some(v) = value { collect_free_vars(v, used, locals); } }
|
||||
ASTNode::AwaitExpression { expression, .. } => { collect_free_vars(expression, used, locals); }
|
||||
ASTNode::PeekExpr { scrutinee, arms, else_expr, .. } => {
|
||||
collect_free_vars(scrutinee, used, locals);
|
||||
for (_, e) in arms { collect_free_vars(e, used, locals); }
|
||||
collect_free_vars(else_expr, used, locals);
|
||||
}
|
||||
ASTNode::Program { statements, .. } => {
|
||||
for st in statements { collect_free_vars(st, used, locals); }
|
||||
}
|
||||
ASTNode::FunctionDeclaration { params, body, .. } => {
|
||||
let mut inner = locals.clone();
|
||||
for p in params { inner.insert(p.clone()); }
|
||||
for st in body { collect_free_vars(st, used, &mut inner); }
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user