pyvm: split op handlers into ops_core/ops_box/ops_ctrl; add ops_flow + intrinsic; delegate vm.py without behavior change

net-plugin: modularize constants (consts.rs) and sockets (sockets.rs); remove legacy commented socket code; fix unused imports
mir: move instruction unit tests to tests/mir_instruction_unit.rs (file lean-up); no semantic changes
runner/pyvm: ensure using pre-strip; misc docs updates

Build: cargo build ok; legacy cfg warnings remain as before
This commit is contained in:
Selfhosting Dev
2025-09-21 08:53:00 +09:00
parent ee17cfd979
commit c8063c9e41
247 changed files with 10187 additions and 23124 deletions

View File

@ -204,519 +204,7 @@ impl MirBuilder {
self.build_expression_impl(ast)
}
// build_expression_impl_legacy moved to builder/exprs_legacy.rs
/*
pub(super) fn build_expression_impl_legacy(&mut self, ast: ASTNode) -> Result<ValueId, String> {
match ast {
ASTNode::Literal { value, .. } => self.build_literal(value),
ASTNode::BinaryOp {
left,
operator,
right,
..
} => self.build_binary_op(*left, operator, *right),
ASTNode::UnaryOp {
operator, operand, ..
} => {
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)
}
ASTNode::Variable { name, .. } => self.build_variable_access(name.clone()),
ASTNode::Me { .. } => self.build_me_expression(),
ASTNode::MethodCall {
object,
method,
arguments,
..
} => {
// 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();
let op = if method == "is" {
super::TypeOpKind::Check
} else {
super::TypeOpKind::Cast
};
self.emit_instruction(MirInstruction::TypeOp {
dst,
op,
value: obj_val,
ty,
})?;
return Ok(dst);
}
}
self.build_method_call(*object.clone(), method.clone(), arguments.clone())
}
ASTNode::FromCall {
parent,
method,
arguments,
..
} => self.build_from_expression(parent.clone(), method.clone(), arguments.clone()),
ASTNode::Assignment { target, value, .. } => {
// 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
self.build_assignment(name.clone(), *value.clone())
} else {
Err("Complex assignment targets not yet supported in MIR".to_string())
}
}
ASTNode::FunctionCall {
name, arguments, ..
} => {
// 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();
let op = if name == "isType" {
super::TypeOpKind::Check
} else {
super::TypeOpKind::Cast
};
self.emit_instruction(MirInstruction::TypeOp {
dst,
op,
value: val,
ty,
})?;
return Ok(dst);
}
}
self.build_function_call(name.clone(), arguments.clone())
}
ASTNode::Call {
callee, arguments, ..
} => {
// P1.5: Lambdaはインライン、それ以外は Call に正規化
if let ASTNode::Lambda { params, body, .. } = callee.as_ref() {
if params.len() != arguments.len() {
return Err(format!(
"Lambda expects {} args, got {}",
params.len(),
arguments.len()
));
}
let mut arg_vals: Vec<ValueId> = Vec::new();
for a in arguments {
arg_vals.push(self.build_expression(a)?);
}
let saved_vars = self.variable_map.clone();
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(),
};
let out = self.build_expression(prog)?;
self.variable_map = saved_vars;
Ok(out)
} else {
// callee/args を評価し、Call を発行VM 側で FunctionBox/関数名の両対応)
let callee_id = self.build_expression(*callee.clone())?;
let mut arg_ids = Vec::new();
for a in arguments {
arg_ids.push(self.build_expression(a)?);
}
let dst = self.value_gen.next();
self.emit_instruction(MirInstruction::Call {
dst: Some(dst),
func: callee_id,
args: arg_ids,
effects: EffectMask::PURE,
})?;
Ok(dst)
}
}
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();
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,
})?;
let then_block = self.block_gen.next();
let else_block = self.block_gen.next();
self.emit_instruction(MirInstruction::Branch {
condition: ok_id,
then_bb: then_block,
else_bb: else_block,
})?;
// else: return res_val
self.current_block = Some(else_block);
self.ensure_block_exists(else_block)?;
self.emit_instruction(MirInstruction::Return {
value: Some(res_val),
})?;
// then: getValue()
self.current_block = Some(then_block);
self.ensure_block_exists(then_block)?;
let val_id = self.value_gen.next();
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,
})?;
self.value_types.insert(val_id, super::MirType::Unknown);
Ok(val_id)
}
ASTNode::Print { expression, .. } => self.build_print_statement(*expression.clone()),
ASTNode::Program { statements, .. } => self.build_block(statements.clone()),
ASTNode::If {
condition,
then_body,
else_body,
..
} => {
let else_ast = if let Some(else_statements) = else_body {
Some(ASTNode::Program {
statements: else_statements.clone(),
span: crate::ast::Span::unknown(),
})
} else {
None
};
self.build_if_statement(
*condition.clone(),
ASTNode::Program {
statements: then_body.clone(),
span: crate::ast::Span::unknown(),
},
else_ast,
)
}
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()),
// P1: Lower peek expression into if-else chain with phi
ASTNode::PeekExpr {
scrutinee,
arms,
else_expr,
..
} => {
// 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();
self.emit_instruction(super::MirInstruction::Compare {
dst: cond_id,
op: super::CompareOp::Eq,
lhs: scr_val,
rhs: lit_id,
})?;
// Create then and next blocks
let then_block = self.block_gen.next();
let next_block = self.block_gen.next();
self.emit_instruction(super::MirInstruction::Branch {
condition: cond_id,
then_bb: then_block,
else_bb: next_block,
})?;
// 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() {
self.emit_instruction(super::MirInstruction::Jump {
target: merge_block,
})?;
}
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() {
self.emit_instruction(super::MirInstruction::Jump {
target: merge_block,
})?;
}
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();
self.emit_instruction(super::MirInstruction::Phi {
dst: result_val,
inputs: phi_inputs,
})?;
Ok(result_val)
}
ASTNode::Lambda { params, body, .. } => {
// Lambda→FunctionBox 値 Lower最小 + 簡易キャプチャ解析)
let dst = self.value_gen.next();
// 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();
for p in params.iter() {
locals.insert(p.clone());
}
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() {
if let Some(&vid) = self.variable_map.get(&name) {
captures.push((name, vid));
}
}
// me capture存在すれば
let me = self.variable_map.get("me").copied();
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()));
Ok(dst)
}
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,
..
} => {
if is_static && name == "Main" {
self.build_static_main_box(name.clone(), methods.clone())
} else {
// Support user-defined boxes - handle as statement, return void
// Track as user-defined (eligible for method lowering)
self.user_defined_boxes.insert(name.clone());
self.build_box_declaration(
name.clone(),
methods.clone(),
fields.clone(),
weak_fields.clone(),
)?;
// 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);
self.lower_method_as_function(
func_name,
name.clone(),
params.clone(),
body.clone(),
)?;
}
}
// Phase 3: Lower instance methods into MIR functions
// Function name pattern: "{BoxName}.{method}/{N}"
for (method_name, method_ast) in methods.clone() {
if let ASTNode::FunctionDeclaration {
params,
body,
is_static,
..
} = method_ast
{
if !is_static {
let func_name = format!(
"{}.{}{}",
name,
method_name,
format!("/{}", params.len())
);
self.lower_method_as_function(
func_name,
name.clone(),
params.clone(),
body.clone(),
)?;
}
}
}
// 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)
}
}
ASTNode::FieldAccess { object, field, .. } => {
self.build_field_access(*object.clone(), field.clone())
}
ASTNode::New {
class, arguments, ..
} => self.build_new_expression(class.clone(), arguments.clone()),
ASTNode::ArrayLiteral { elements, .. } => {
// Lower: new ArrayBox(); for each elem: .push(elem)
let arr_id = self.value_gen.next();
self.emit_instruction(MirInstruction::NewBox {
dst: arr_id,
box_type: "ArrayBox".to_string(),
args: vec![],
})?;
for e in elements {
let v = self.build_expression(e)?;
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,
})?;
}
Ok(arr_id)
}
// Phase 7: Async operations
ASTNode::Nowait {
variable,
expression,
..
} => self.build_nowait_statement(variable.clone(), *expression.clone()),
ASTNode::AwaitExpression { expression, .. } => {
self.build_await_expression(*expression.clone())
}
ASTNode::Include { filename, .. } => {
// Resolve and read included file
let mut path = utils::resolve_include_path_builder(&filename);
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");
}
// 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());
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 {
if let crate::ast::ASTNode::BoxDeclaration {
name, is_static, ..
} = st
{
if *is_static {
box_name = Some(name.clone());
break;
}
}
}
}
let bname = box_name
.ok_or_else(|| format!("Include target '{}' has no static box", filename))?;
// Lower included AST into current MIR (register types/methods)
let _ = self.build_expression(included_ast)?;
// Mark caches
self.include_loading.remove(&path);
self.include_box_map.insert(path.clone(), bname.clone());
// Return a new instance of included box (no args)
self.build_new_expression(bname, vec![])
}
_ => Err(format!("Unsupported AST node type: {:?}", ast)),
}
}
*/
// build_expression_impl_legacy moved to builder/exprs_legacy.rs (legacy body removed)
/// Build a literal value
pub(super) fn build_literal(&mut self, literal: LiteralValue) -> Result<ValueId, String> {
@ -1065,88 +553,4 @@ impl Default for MirBuilder {
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::{ASTNode, LiteralValue, Span};
#[test]
fn test_literal_building() {
let mut builder = MirBuilder::new();
let ast = ASTNode::Literal {
value: LiteralValue::Integer(42),
span: Span::unknown(),
};
let result = builder.build_module(ast);
assert!(result.is_ok());
let module = result.unwrap();
assert_eq!(module.function_names().len(), 1);
assert!(module.get_function("main").is_some());
}
#[test]
fn test_binary_op_building() {
let mut builder = MirBuilder::new();
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(),
};
let result = builder.build_module(ast);
assert!(result.is_ok());
let module = result.unwrap();
let function = module.get_function("main").unwrap();
// Should have constants and binary operation
let stats = function.stats();
assert!(stats.instruction_count >= 3); // 2 constants + 1 binop + 1 return
}
#[test]
fn test_if_statement_building() {
let mut builder = MirBuilder::new();
// Adapt test to current AST: If with statement bodies
let ast = ASTNode::If {
condition: Box::new(ASTNode::Literal {
value: LiteralValue::Bool(true),
span: Span::unknown(),
}),
then_body: vec![ASTNode::Literal {
value: LiteralValue::Integer(1),
span: Span::unknown(),
}],
else_body: Some(vec![ASTNode::Literal {
value: LiteralValue::Integer(2),
span: Span::unknown(),
}]),
span: Span::unknown(),
};
let result = builder.build_module(ast);
assert!(result.is_ok());
let module = result.unwrap();
let function = module.get_function("main").unwrap();
// Should have multiple blocks for if/then/else/merge
assert!(function.blocks.len() >= 3);
// Should have phi function in merge block
let stats = function.stats();
assert!(stats.phi_count >= 1);
}
}
// Unit tests moved to `tests/mir_builder_unit.rs` to keep this file lean

View File

@ -28,6 +28,9 @@ impl super::MirBuilder {
// Look for the main() method
let out = if let Some(main_method) = methods.get("main") {
if let ASTNode::FunctionDeclaration { params, body, .. } = main_method {
// Also materialize a callable function entry "BoxName.main/N" for harness/PyVM
let func_name = format!("{}.{}", box_name, "main");
let _ = self.lower_static_method_as_function(func_name, params.clone(), body.clone());
// Convert the method body to a Program AST node and lower it
let program_ast = ASTNode::Program {
statements: body.clone(),

View File

@ -241,6 +241,7 @@ impl super::MirBuilder {
ASTNode::Include { filename, .. } => self.build_include_expression(filename.clone()),
ASTNode::Program { statements, .. } => self.cf_block(statements.clone()),
ASTNode::ScopeBox { body, .. } => self.cf_block(body.clone()),
ASTNode::Print { expression, .. } => self.build_print_statement(*expression.clone()),

View File

@ -31,18 +31,24 @@ impl MirBuilder {
// then
self.current_block = Some(then_block);
self.ensure_block_exists(then_block)?;
// Scope enter for then-branch
self.hint_scope_enter(0);
let then_ast_for_analysis = then_branch.clone();
self.variable_map = pre_if_var_map.clone();
let then_value_raw = self.build_expression(then_branch)?;
let then_exit_block = self.current_block()?;
let then_var_map_end = self.variable_map.clone();
if !self.is_current_block_terminated() {
// Scope leave for then-branch
self.hint_scope_leave(0);
self.emit_instruction(MirInstruction::Jump { target: merge_block })?;
}
// else
self.current_block = Some(else_block);
self.ensure_block_exists(else_block)?;
// Scope enter for else-branch
self.hint_scope_enter(0);
let (else_value_raw, else_ast_for_analysis, else_var_map_end_opt) = if let Some(else_ast) = else_branch {
self.variable_map = pre_if_var_map.clone();
let val = self.build_expression(else_ast.clone())?;
@ -54,6 +60,8 @@ impl MirBuilder {
};
let else_exit_block = self.current_block()?;
if !self.is_current_block_terminated() {
// Scope leave for else-branch
self.hint_scope_leave(0);
self.emit_instruction(MirInstruction::Jump { target: merge_block })?;
}
@ -86,10 +94,18 @@ impl MirBuilder {
pre_then_var_value,
)?;
// Hint: join result variable if both branches assign to the same variable name
// Hint: join result variable(s)
// 1) Primary: if both branches assign to the same variable name, emit a hint for that name
if let (Some(tn), Some(en)) = (assigned_then_pre.as_deref(), assigned_else_pre.as_deref()) {
if tn == en {
self.hint_join_result(tn);
if tn == en { self.hint_join_result(tn); }
}
// 2) Secondary: if both branches assign multiple variables, hint全件制限なし
if let Some(ref else_map_end) = else_var_map_end_opt {
for name in then_var_map_end.keys() {
if Some(name.as_str()) == assigned_then_pre.as_deref() { continue; }
if else_map_end.contains_key(name) {
self.hint_join_result(name.as_str());
}
}
}

View File

@ -133,6 +133,9 @@ impl super::MirBuilder {
// Block: sequentially build statements and return last value or Void
pub(super) fn build_block(&mut self, statements: Vec<ASTNode>) -> Result<ValueId, String> {
// Scope hint for bare block (Program)
let scope_id = self.current_block.map(|bb| bb.as_u32()).unwrap_or(0);
self.hint_scope_enter(scope_id);
let mut last_value = None;
for statement in statements {
last_value = Some(self.build_expression(statement)?);
@ -142,7 +145,7 @@ impl super::MirBuilder {
break;
}
}
Ok(last_value.unwrap_or_else(|| {
let out = last_value.unwrap_or_else(|| {
let void_val = self.value_gen.next();
self.emit_instruction(MirInstruction::Const {
dst: void_val,
@ -150,7 +153,12 @@ impl super::MirBuilder {
})
.unwrap();
void_val
}))
});
// Scope leave only if block not already terminated
if !self.is_current_block_terminated() {
self.hint_scope_leave(scope_id);
}
Ok(out)
}
// control-flow build_* moved to control_flow.rs (use cf_* instead)

View File

@ -27,21 +27,49 @@ impl HintSink {
pub fn new() -> Self { Self { enabled: false } }
pub fn with_enabled(mut self, enabled: bool) -> Self { self.enabled = enabled; self }
fn cfg() -> HintCfg {
// New unified env: NYASH_MIR_HINTS="<target>|<filters>"
// Examples:
// NYASH_MIR_HINTS=trace|all -> stderr + all kinds
// NYASH_MIR_HINTS=tmp/hints.jsonl|loop -> jsonl file + loop-only
// NYASH_MIR_HINTS=jsonl=tmp/h.jsonl|scope|join
// Back-compat: NYASH_MIR_TRACE_HINTS=1 -> stderr + all kinds
if let Ok(spec) = std::env::var("NYASH_MIR_HINTS") {
return HintCfg::parse(&spec);
}
if std::env::var("NYASH_MIR_TRACE_HINTS").ok().as_deref() == Some("1") {
return HintCfg { sink: HintSinkTarget::Stderr, kinds: HintKinds::All };
}
HintCfg { sink: HintSinkTarget::None, kinds: HintKinds::None }
}
#[inline]
pub fn record(&mut self, hint: HintKind) {
// Lightweight trace: print only when explicitly enabled via env
let enabled = self.enabled
|| std::env::var("NYASH_MIR_TRACE_HINTS").ok().as_deref() == Some("1");
if !enabled { return; }
match hint {
HintKind::ScopeEnter(id) => eprintln!("[mir][hint] ScopeEnter({})", id),
HintKind::ScopeLeave(id) => eprintln!("[mir][hint] ScopeLeave({})", id),
HintKind::Defer(calls) => eprintln!("[mir][hint] Defer({})", calls.join(";")),
HintKind::JoinResult(var) => eprintln!("[mir][hint] JoinResult({})", var),
HintKind::LoopCarrier(vars) => eprintln!("[mir][hint] LoopCarrier({})", vars.join(",")),
HintKind::LoopHeader => eprintln!("[mir][hint] LoopHeader"),
HintKind::LoopLatch => eprintln!("[mir][hint] LoopLatch"),
HintKind::NoEmptyPhi => eprintln!("[mir][hint] NoEmptyPhi"),
// Resolve config (env-based). Lightweight and robust; acceptable to parse per call.
let cfg = Self::cfg();
if matches!(cfg.sink, HintSinkTarget::None) { return; }
// Filter kinds
let k = hint_tag(&hint);
if !cfg.kinds.contains(k) { return; }
match cfg.sink {
HintSinkTarget::None => {}
HintSinkTarget::Stderr => {
match hint {
HintKind::ScopeEnter(id) => eprintln!("[mir][hint] ScopeEnter({})", id),
HintKind::ScopeLeave(id) => eprintln!("[mir][hint] ScopeLeave({})", id),
HintKind::Defer(calls) => eprintln!("[mir][hint] Defer({})", calls.join(";")),
HintKind::JoinResult(var) => eprintln!("[mir][hint] JoinResult({})", var),
HintKind::LoopCarrier(vars) => eprintln!("[mir][hint] LoopCarrier({})", vars.join(",")),
HintKind::LoopHeader => eprintln!("[mir][hint] LoopHeader"),
HintKind::LoopLatch => eprintln!("[mir][hint] LoopLatch"),
HintKind::NoEmptyPhi => eprintln!("[mir][hint] NoEmptyPhi"),
}
}
HintSinkTarget::Jsonl(ref path) => {
// Append one JSON object per line. Best-effort; ignore errors.
let _ = append_jsonl(path, &hint);
}
}
}
#[inline] pub fn scope_enter(&mut self, id: u32) { self.record(HintKind::ScopeEnter(id)); }
@ -57,3 +85,121 @@ impl HintSink {
#[inline] pub fn loop_latch(&mut self) { self.record(HintKind::LoopLatch); }
#[inline] pub fn no_empty_phi(&mut self) { self.record(HintKind::NoEmptyPhi); }
}
// ---- Unified hint config parser ----
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum HintTag { Scope, Join, Loop, Phi }
fn hint_tag(h: &HintKind) -> HintTag {
match h {
HintKind::ScopeEnter(_) | HintKind::ScopeLeave(_) | HintKind::Defer(_) => HintTag::Scope,
HintKind::JoinResult(_) => HintTag::Join,
HintKind::LoopCarrier(_) | HintKind::LoopHeader | HintKind::LoopLatch => HintTag::Loop,
HintKind::NoEmptyPhi => HintTag::Phi,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HintKinds { None, Some { scope: bool, join: bool, loopk: bool, phi: bool }, All }
impl HintKinds {
fn contains(&self, tag: HintTag) -> bool {
match self {
HintKinds::All => true,
HintKinds::None => false,
HintKinds::Some { scope, join, loopk, phi } => match tag {
HintTag::Scope => *scope,
HintTag::Join => *join,
HintTag::Loop => *loopk,
HintTag::Phi => *phi,
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum HintSinkTarget { None, Stderr, Jsonl(String) }
#[derive(Debug, Clone, PartialEq, Eq)]
struct HintCfg { sink: HintSinkTarget, kinds: HintKinds }
impl HintCfg {
fn parse(spec: &str) -> Self {
let mut sink = HintSinkTarget::None;
let mut kinds = HintKinds::None;
let mut saw_filter = false;
for tok in spec.split('|').map(|s| s.trim()).filter(|s| !s.is_empty()) {
let tl = tok.to_ascii_lowercase();
if tl == "off" { sink = HintSinkTarget::None; kinds = HintKinds::None; continue; }
if tl == "trace" || tl == "stderr" { sink = HintSinkTarget::Stderr; continue; }
if tl.starts_with("jsonl=") {
sink = HintSinkTarget::Jsonl(tok[6..].trim().to_string());
continue;
}
// Heuristic: token looks like a path → jsonl
if tok.contains('/') || tok.contains('.') {
sink = HintSinkTarget::Jsonl(tok.to_string());
continue;
}
// Filters
match tl.as_str() {
"all" => { kinds = HintKinds::All; saw_filter = true; }
"scope" => kinds = merge_kind(kinds, |k| HintKinds::Some { scope: true, join: matches!(k, HintKinds::Some{ join: true, .. } | HintKinds::All), loopk: matches!(k, HintKinds::Some{ loopk: true, .. } | HintKinds::All), phi: matches!(k, HintKinds::Some{ phi: true, .. } | HintKinds::All) }),
"join" => kinds = merge_kind(kinds, |k| HintKinds::Some { scope: matches!(k, HintKinds::Some{ scope: true, .. } | HintKinds::All), join: true, loopk: matches!(k, HintKinds::Some{ loopk: true, .. } | HintKinds::All), phi: matches!(k, HintKinds::Some{ phi: true, .. } | HintKinds::All) }),
"loop" => kinds = merge_kind(kinds, |k| HintKinds::Some { scope: matches!(k, HintKinds::Some{ scope: true, .. } | HintKinds::All), join: matches!(k, HintKinds::Some{ join: true, .. } | HintKinds::All), loopk: true, phi: matches!(k, HintKinds::Some{ phi: true, .. } | HintKinds::All) }),
"phi" => kinds = merge_kind(kinds, |k| HintKinds::Some { scope: matches!(k, HintKinds::Some{ scope: true, .. } | HintKinds::All), join: matches!(k, HintKinds::Some{ join: true, .. } | HintKinds::All), loopk: matches!(k, HintKinds::Some{ loopk: true, .. } | HintKinds::All), phi: true }),
_ => {}
}
}
if !saw_filter && !matches!(kinds, HintKinds::All) {
// default to all if no filter specified
kinds = HintKinds::All;
}
// default sink if only filters appear
if matches!(sink, HintSinkTarget::None) {
sink = HintSinkTarget::Stderr;
}
HintCfg { sink, kinds }
}
}
fn merge_kind<F: FnOnce(HintKinds) -> HintKinds>(k: HintKinds, f: F) -> HintKinds {
match k {
HintKinds::All => HintKinds::All,
x => f(x),
}
}
fn append_jsonl(path: &str, hint: &HintKind) -> std::io::Result<()> {
use std::io::Write;
let mut obj = serde_json::json!({ "kind": kind_name(hint) });
match hint {
HintKind::ScopeEnter(id) => obj["value"] = serde_json::json!({"enter": id}),
HintKind::ScopeLeave(id) => obj["value"] = serde_json::json!({"leave": id}),
HintKind::Defer(calls) => obj["value"] = serde_json::json!({"defer": calls}),
HintKind::JoinResult(v) => obj["value"] = serde_json::json!({"join": v}),
HintKind::LoopCarrier(vs) => obj["value"] = serde_json::json!({"carrier": vs}),
HintKind::LoopHeader => obj["value"] = serde_json::json!({"loop": "header"}),
HintKind::LoopLatch => obj["value"] = serde_json::json!({"loop": "latch"}),
HintKind::NoEmptyPhi => obj["value"] = serde_json::json!({"phi": "no_empty"}),
}
let line = obj.to_string();
if let Some(dir) = std::path::Path::new(path).parent() { let _ = std::fs::create_dir_all(dir); }
let mut f = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
writeln!(f, "{}", line)?;
Ok(())
}
fn kind_name(h: &HintKind) -> &'static str {
match h {
HintKind::ScopeEnter(_) => "ScopeEnter",
HintKind::ScopeLeave(_) => "ScopeLeave",
HintKind::Defer(_) => "Defer",
HintKind::JoinResult(_) => "JoinResult",
HintKind::LoopCarrier(_) => "LoopCarrier",
HintKind::LoopHeader => "LoopHeader",
HintKind::LoopLatch => "LoopLatch",
HintKind::NoEmptyPhi => "NoEmptyPhi",
}
}

View File

@ -780,6 +780,7 @@ impl fmt::Display for ConstValue {
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -208,6 +208,8 @@ impl<'a> LoopBuilder<'a> {
// 7. ループボディの構築
self.set_current_block(body_id)?;
// Scope enter for loop body
self.parent_builder.hint_scope_enter(0);
// Optional safepoint per loop-iteration
if std::env::var("NYASH_BUILDER_SAFEPOINT_LOOP")
.ok()
@ -226,6 +228,8 @@ impl<'a> LoopBuilder<'a> {
let latch_id = self.current_block()?;
// Hint: loop latch (no-op sink)
self.parent_builder.hint_loop_latch();
// Scope leave for loop body
self.parent_builder.hint_scope_leave(0);
let latch_snapshot = self.get_current_variable_map();
// 以前は body_id に保存していたが、複数ブロックのボディや continue 混在時に不正確になるため
// 実際の latch_id に対してスナップショットを紐づける
@ -456,6 +460,35 @@ impl<'a> LoopBuilder<'a> {
fn build_statement(&mut self, stmt: ASTNode) -> Result<ValueId, String> {
match stmt {
// Ensure nested bare blocks inside loops are lowered with loop-aware semantics
ASTNode::Program { statements, .. } => {
let mut last = None;
for s in statements.into_iter() {
last = Some(self.build_statement(s)?);
// Stop early if this block has been terminated (e.g., break/continue)
let cur_id = self.current_block()?;
let terminated = {
if let Some(ref fun_ro) = self.parent_builder.current_function {
if let Some(bb) = fun_ro.get_block(cur_id) {
bb.is_terminated()
} else {
false
}
} else {
false
}
};
if terminated {
break;
}
}
Ok(last.unwrap_or_else(|| {
let void_id = self.new_value();
// Emit a void const to keep SSA consistent when block is empty
let _ = self.emit_const(void_id, ConstValue::Void);
void_id
}))
}
ASTNode::If {
condition,
then_body,