From 46ced3980a5fc3418dc163f6d9b167c32edd73b7 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Fri, 26 Jun 2026 18:22:56 -0500 Subject: [PATCH] Some e2e tests passing again. Yay! --- dmc-lib/src/ir/assemble.rs | 36 +++++++++---------- dmc-lib/src/ir/ir_function.rs | 8 +---- dmc-lib/src/ir/mod.rs | 10 +----- dmc-lib/src/ir/register_allocation.rs | 9 ++--- dmc-lib/src/lib.rs | 2 +- .../src/semantic_analysis/collect_symbols.rs | 7 +++- 6 files changed, 30 insertions(+), 42 deletions(-) diff --git a/dmc-lib/src/ir/assemble.rs b/dmc-lib/src/ir/assemble.rs index ae69801..8c39539 100644 --- a/dmc-lib/src/ir/assemble.rs +++ b/dmc-lib/src/ir/assemble.rs @@ -10,7 +10,7 @@ use crate::ir::ir_parameter::IrParameter; use crate::ir::ir_return::IrReturn; use crate::ir::ir_statement::IrStatement; use crate::ir::ir_type_info::{IrTypeInfo, IrTypeInfoId}; -use crate::ir::ir_variable::IrVariableId; +use crate::ir::ir_variable::{IrVariable, IrVariableId}; use crate::ir::variable_locations::{VariableLocation, VariableLocations}; use dvm_lib::instruction::{ AddOperand, Instruction, Location, LocationOrInteger, LocationOrNumber, MoveOperand, @@ -19,27 +19,23 @@ use dvm_lib::instruction::{ use std::collections::HashMap; struct FunctionAssemblyContext<'a> { - type_infos: &'a [IrTypeInfo], - variables_to_type_infos: &'a HashMap, variable_locations: &'a VariableLocations, parameters: &'a [IrParameter], + variables: &'a [IrVariable], constants_table: &'a mut ConstantsTable, instructions: Vec, } pub fn assemble_ir_function( ir_function: &IrFunction, - type_infos: &[IrTypeInfo], - variables_to_type_infos: &HashMap, variable_locations: &VariableLocations, constants_table: &mut ConstantsTable, ) -> Vec { let mut ctx = FunctionAssemblyContext { - type_infos, - variables_to_type_infos, variable_locations, constants_table, parameters: ir_function.parameters(), + variables: ir_function.variables(), instructions: Vec::new(), }; for block in ir_function.blocks() { @@ -210,15 +206,17 @@ fn to_multiply_operand( Location::StackFrameOffset(ctx.parameters[*ir_parameter_id].stack_offset()), ), IrExpression::Variable(ir_variable_id) => { - let ir_type_info_id = ctx.variables_to_type_infos[ir_variable_id]; - let ir_type_info = &ctx.type_infos[ir_type_info_id]; - match ir_type_info { + let ir_variable = &ctx.variables[*ir_variable_id]; + match ir_variable.type_info() { IrTypeInfo::Int | IrTypeInfo::Double => { let location = to_location(ctx.variable_locations.get_variable_location(ir_variable_id)); MultiplyOperand::Location(location) } - _ => panic!("Attempt to multiply non-number (found {})", ir_type_info), + _ => panic!( + "Attempt to multiply non-number (found {})", + ir_variable.type_info() + ), } } IrExpression::Int(i) => MultiplyOperand::Int(*i), @@ -240,9 +238,8 @@ fn to_add_operand(ir_expression: &IrExpression, ctx: &mut FunctionAssemblyContex } } IrExpression::Variable(ir_variable_id) => { - let ir_type_info_id = ctx.variables_to_type_infos[ir_variable_id]; - let ir_type_info = &ctx.type_infos[ir_type_info_id]; - match ir_type_info { + let ir_variable = &ctx.variables[*ir_variable_id]; + match ir_variable.type_info() { IrTypeInfo::Int | IrTypeInfo::Double | IrTypeInfo::String => AddOperand::Location( to_location(ctx.variable_locations.get_variable_location(ir_variable_id)), ), @@ -274,16 +271,15 @@ fn to_subtract_operand( ), } } - IrExpression::Variable(ir_variable) => { - let ir_type_info_id = ctx.variables_to_type_infos[ir_variable]; - let ir_type_info = &ctx.type_infos[ir_type_info_id]; - match ir_type_info { + IrExpression::Variable(ir_variable_id) => { + let ir_variable = &ctx.variables[*ir_variable_id]; + match ir_variable.type_info() { IrTypeInfo::Int | IrTypeInfo::Double => SubtractOperand::Location(to_location( - ctx.variable_locations.get_variable_location(ir_variable), + ctx.variable_locations.get_variable_location(ir_variable_id), )), _ => panic!( "Attempt to subtract with non-number type (found {})", - ir_type_info + ir_variable.type_info() ), } } diff --git a/dmc-lib/src/ir/ir_function.rs b/dmc-lib/src/ir/ir_function.rs index fdd37eb..3082e28 100644 --- a/dmc-lib/src/ir/ir_function.rs +++ b/dmc-lib/src/ir/ir_function.rs @@ -75,13 +75,7 @@ impl IrFunction { variable_locations: &VariableLocations, constants_table: &mut ConstantsTable, ) -> Function { - let instructions = assemble_ir_function( - self, - type_infos, - variables_to_type_infos, - variable_locations, - constants_table, - ); + let instructions = assemble_ir_function(self, variable_locations, constants_table); Function::new( self.fqn.clone(), self.parameters.len(), diff --git a/dmc-lib/src/ir/mod.rs b/dmc-lib/src/ir/mod.rs index 2a9c61a..bcf1f86 100644 --- a/dmc-lib/src/ir/mod.rs +++ b/dmc-lib/src/ir/mod.rs @@ -35,18 +35,10 @@ mod variable_locations; pub fn compile_dvm_function( ir_function: &IrFunction, - ir_type_infos: &[IrTypeInfo], - ir_variables_to_ir_type_infos: &HashMap, constants_table: &mut ConstantsTable, ) -> Function { let variable_locations = assign_registers(ir_function); - let instructions = assemble_ir_function( - ir_function, - ir_type_infos, - ir_variables_to_ir_type_infos, - &variable_locations, - constants_table, - ); + let instructions = assemble_ir_function(ir_function, &variable_locations, constants_table); Function::new( ir_function.fqn().into(), ir_function.parameters().len(), diff --git a/dmc-lib/src/ir/register_allocation.rs b/dmc-lib/src/ir/register_allocation.rs index 6386a49..a8fcf76 100644 --- a/dmc-lib/src/ir/register_allocation.rs +++ b/dmc-lib/src/ir/register_allocation.rs @@ -24,10 +24,11 @@ pub fn block_live_in_live_out(ir_block: &IrBlock) -> (LivenessSets, LivenessSets // for now, a statement can only have one successor // this will need to be updated when we add jumps let statement_live_out = &mut live_out[statement_index]; - let successor_live_in = &live_in[statement_index + 1]; - for ir_variable_id in successor_live_in { - if statement_live_out.insert(*ir_variable_id) { - did_work = true; + if let Some(successor_live_in) = live_in.get(statement_index + 1) { + for ir_variable_id in successor_live_in { + if statement_live_out.insert(*ir_variable_id) { + did_work = true; + } } } diff --git a/dmc-lib/src/lib.rs b/dmc-lib/src/lib.rs index c0bcaa2..c4714f1 100644 --- a/dmc-lib/src/lib.rs +++ b/dmc-lib/src/lib.rs @@ -63,7 +63,7 @@ pub fn compile_compilation_unit( let mut dvm_functions = HashMap::new(); for ir_function in &lower_to_ir_result.functions { - let dvm_function = compile_dvm_function(ir_function, todo!(), todo!(), constants_table); + let dvm_function = compile_dvm_function(ir_function, constants_table); dvm_functions.insert(dvm_function.name_owned(), dvm_function); } diff --git a/dmc-lib/src/semantic_analysis/collect_symbols.rs b/dmc-lib/src/semantic_analysis/collect_symbols.rs index c922276..2a56e0a 100644 --- a/dmc-lib/src/semantic_analysis/collect_symbols.rs +++ b/dmc-lib/src/semantic_analysis/collect_symbols.rs @@ -70,7 +70,12 @@ impl<'a> SymbolCollectionContext<'a> { } pub fn resolve_fqn(&self, suffix: &Rc) -> String { - Self::join_fqn_parts(&[self.get_fqn_base().into(), suffix.clone()]) + let base = self.get_fqn_base(); + if base.is_empty() { + suffix.to_string() + } else { + Self::join_fqn_parts(&[base.into(), suffix.clone()]) + } } }