From 8fb99d076319dab9b85c4251e17bfd11012cce0a Mon Sep 17 00:00:00 2001 From: Moe Charm Date: Mon, 25 Aug 2025 19:52:42 +0900 Subject: [PATCH] MIR builder (modularized): fix Call emission to new schema; switch AwaitExpression; drop legacy Array* cases. Tests: fix optimizer test get_block signature. Interpreter: annotate unreachable legacy constructor path in execute_new(). --- src/interpreter/objects.rs | 2 + src/mir/builder_modularized/expressions.rs | 68 ++++++---------------- src/mir/optimizer.rs | 2 +- 3 files changed, 22 insertions(+), 50 deletions(-) diff --git a/src/interpreter/objects.rs b/src/interpreter/objects.rs index 0cea036d..22ae7aa1 100644 --- a/src/interpreter/objects.rs +++ b/src/interpreter/objects.rs @@ -106,6 +106,8 @@ impl NyashInterpreter { } // Unified registry is authoritative; legacy implementation removed + // NOTE: Legacy constructor path below is unreachable and kept only for reference during migration. + // TODO(migration): remove legacy block after all callsites are confirmed on Unified Registry. return Err(RuntimeError::UndefinedClass { name: class.to_string() }); // Try basic type constructors first diff --git a/src/mir/builder_modularized/expressions.rs b/src/mir/builder_modularized/expressions.rs index 8e8f0c6c..caae2f32 100644 --- a/src/mir/builder_modularized/expressions.rs +++ b/src/mir/builder_modularized/expressions.rs @@ -182,16 +182,24 @@ impl MirBuilder { for arg in args { arg_values.push(self.build_expression(arg)?); } - - let dst = self.value_gen.next(); - - // For now, treat all function calls as Box method calls - self.emit_instruction(MirInstruction::Call { - dst, - function: name, - arguments: arg_values, + // Prepare function identifier as a const String value id + let func_val = self.value_gen.next(); + self.emit_instruction(MirInstruction::Const { + dst: func_val, + value: ConstValue::String(name), })?; - + + // Destination for the call result + let dst = self.value_gen.next(); + + // Emit Call with conservative read effects + self.emit_instruction(MirInstruction::Call { + dst: Some(dst), + func: func_val, + args: arg_values, + effects: EffectMask::READ.add(Effect::ReadHeap), + })?; + Ok(dst) } @@ -461,49 +469,11 @@ impl MirBuilder { self.build_field_access(*object.clone(), field.clone()) }, - ASTNode::ArrayAccess { array, index, .. } => { - // Build array and index expressions - let array_val = self.build_expression(*array)?; - let index_val = self.build_expression(*index)?; - - // Generate ArrayGet instruction - let result = self.value_gen.next(); - self.emit_instruction(MirInstruction::ArrayGet { - dst: result, - array: array_val, - index: index_val, - effects: EffectMask::READ.add(Effect::ReadHeap), - })?; - - Ok(result) - }, - - ASTNode::ArrayLiteral { elements, .. } => { - // Create new ArrayBox - let array_val = self.build_new_expression("ArrayBox".to_string(), vec![])?; - - // Add each element - for elem in elements { - let elem_val = self.build_expression(elem)?; - - // array.push(elem) - self.emit_instruction(MirInstruction::BoxCall { - dst: None, - box_val: array_val, - method: "push".to_string(), - args: vec![elem_val], - effects: EffectMask::WRITE.add(Effect::WriteHeap), - })?; - } - - Ok(array_val) - }, - ASTNode::New { class, arguments, .. } => { self.build_new_expression(class.clone(), arguments.clone()) }, - ASTNode::Await { expression, .. } => { + ASTNode::AwaitExpression { expression, .. } => { self.build_await_expression(*expression) }, @@ -618,4 +588,4 @@ impl MirBuilder { Ok(result_id) } -} \ No newline at end of file +} diff --git a/src/mir/optimizer.rs b/src/mir/optimizer.rs index eaeaec5a..45737565 100644 --- a/src/mir/optimizer.rs +++ b/src/mir/optimizer.rs @@ -511,7 +511,7 @@ mod tests { // Ensure TypeOp remains in bb0 let f = module.get_function("main").unwrap(); - let block = f.get_block(&bb0).unwrap(); + let block = f.get_block(bb0).unwrap(); let has_typeop = block.all_instructions().any(|i| matches!(i, MirInstruction::TypeOp { .. })); assert!(has_typeop, "TypeOp should not be dropped by DCE when used by print"); }