Some e2e tests passing again. Yay!
This commit is contained in:
parent
c59fbe4033
commit
46ced3980a
@ -10,7 +10,7 @@ use crate::ir::ir_parameter::IrParameter;
|
|||||||
use crate::ir::ir_return::IrReturn;
|
use crate::ir::ir_return::IrReturn;
|
||||||
use crate::ir::ir_statement::IrStatement;
|
use crate::ir::ir_statement::IrStatement;
|
||||||
use crate::ir::ir_type_info::{IrTypeInfo, IrTypeInfoId};
|
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 crate::ir::variable_locations::{VariableLocation, VariableLocations};
|
||||||
use dvm_lib::instruction::{
|
use dvm_lib::instruction::{
|
||||||
AddOperand, Instruction, Location, LocationOrInteger, LocationOrNumber, MoveOperand,
|
AddOperand, Instruction, Location, LocationOrInteger, LocationOrNumber, MoveOperand,
|
||||||
@ -19,27 +19,23 @@ use dvm_lib::instruction::{
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
struct FunctionAssemblyContext<'a> {
|
struct FunctionAssemblyContext<'a> {
|
||||||
type_infos: &'a [IrTypeInfo],
|
|
||||||
variables_to_type_infos: &'a HashMap<IrVariableId, IrTypeInfoId>,
|
|
||||||
variable_locations: &'a VariableLocations,
|
variable_locations: &'a VariableLocations,
|
||||||
parameters: &'a [IrParameter],
|
parameters: &'a [IrParameter],
|
||||||
|
variables: &'a [IrVariable],
|
||||||
constants_table: &'a mut ConstantsTable,
|
constants_table: &'a mut ConstantsTable,
|
||||||
instructions: Vec<Instruction>,
|
instructions: Vec<Instruction>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn assemble_ir_function(
|
pub fn assemble_ir_function(
|
||||||
ir_function: &IrFunction,
|
ir_function: &IrFunction,
|
||||||
type_infos: &[IrTypeInfo],
|
|
||||||
variables_to_type_infos: &HashMap<IrVariableId, IrTypeInfoId>,
|
|
||||||
variable_locations: &VariableLocations,
|
variable_locations: &VariableLocations,
|
||||||
constants_table: &mut ConstantsTable,
|
constants_table: &mut ConstantsTable,
|
||||||
) -> Vec<Instruction> {
|
) -> Vec<Instruction> {
|
||||||
let mut ctx = FunctionAssemblyContext {
|
let mut ctx = FunctionAssemblyContext {
|
||||||
type_infos,
|
|
||||||
variables_to_type_infos,
|
|
||||||
variable_locations,
|
variable_locations,
|
||||||
constants_table,
|
constants_table,
|
||||||
parameters: ir_function.parameters(),
|
parameters: ir_function.parameters(),
|
||||||
|
variables: ir_function.variables(),
|
||||||
instructions: Vec::new(),
|
instructions: Vec::new(),
|
||||||
};
|
};
|
||||||
for block in ir_function.blocks() {
|
for block in ir_function.blocks() {
|
||||||
@ -210,15 +206,17 @@ fn to_multiply_operand(
|
|||||||
Location::StackFrameOffset(ctx.parameters[*ir_parameter_id].stack_offset()),
|
Location::StackFrameOffset(ctx.parameters[*ir_parameter_id].stack_offset()),
|
||||||
),
|
),
|
||||||
IrExpression::Variable(ir_variable_id) => {
|
IrExpression::Variable(ir_variable_id) => {
|
||||||
let ir_type_info_id = ctx.variables_to_type_infos[ir_variable_id];
|
let ir_variable = &ctx.variables[*ir_variable_id];
|
||||||
let ir_type_info = &ctx.type_infos[ir_type_info_id];
|
match ir_variable.type_info() {
|
||||||
match ir_type_info {
|
|
||||||
IrTypeInfo::Int | IrTypeInfo::Double => {
|
IrTypeInfo::Int | IrTypeInfo::Double => {
|
||||||
let location =
|
let location =
|
||||||
to_location(ctx.variable_locations.get_variable_location(ir_variable_id));
|
to_location(ctx.variable_locations.get_variable_location(ir_variable_id));
|
||||||
MultiplyOperand::Location(location)
|
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),
|
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) => {
|
IrExpression::Variable(ir_variable_id) => {
|
||||||
let ir_type_info_id = ctx.variables_to_type_infos[ir_variable_id];
|
let ir_variable = &ctx.variables[*ir_variable_id];
|
||||||
let ir_type_info = &ctx.type_infos[ir_type_info_id];
|
match ir_variable.type_info() {
|
||||||
match ir_type_info {
|
|
||||||
IrTypeInfo::Int | IrTypeInfo::Double | IrTypeInfo::String => AddOperand::Location(
|
IrTypeInfo::Int | IrTypeInfo::Double | IrTypeInfo::String => AddOperand::Location(
|
||||||
to_location(ctx.variable_locations.get_variable_location(ir_variable_id)),
|
to_location(ctx.variable_locations.get_variable_location(ir_variable_id)),
|
||||||
),
|
),
|
||||||
@ -274,16 +271,15 @@ fn to_subtract_operand(
|
|||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
IrExpression::Variable(ir_variable) => {
|
IrExpression::Variable(ir_variable_id) => {
|
||||||
let ir_type_info_id = ctx.variables_to_type_infos[ir_variable];
|
let ir_variable = &ctx.variables[*ir_variable_id];
|
||||||
let ir_type_info = &ctx.type_infos[ir_type_info_id];
|
match ir_variable.type_info() {
|
||||||
match ir_type_info {
|
|
||||||
IrTypeInfo::Int | IrTypeInfo::Double => SubtractOperand::Location(to_location(
|
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!(
|
_ => panic!(
|
||||||
"Attempt to subtract with non-number type (found {})",
|
"Attempt to subtract with non-number type (found {})",
|
||||||
ir_type_info
|
ir_variable.type_info()
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,13 +75,7 @@ impl IrFunction {
|
|||||||
variable_locations: &VariableLocations,
|
variable_locations: &VariableLocations,
|
||||||
constants_table: &mut ConstantsTable,
|
constants_table: &mut ConstantsTable,
|
||||||
) -> Function {
|
) -> Function {
|
||||||
let instructions = assemble_ir_function(
|
let instructions = assemble_ir_function(self, variable_locations, constants_table);
|
||||||
self,
|
|
||||||
type_infos,
|
|
||||||
variables_to_type_infos,
|
|
||||||
variable_locations,
|
|
||||||
constants_table,
|
|
||||||
);
|
|
||||||
Function::new(
|
Function::new(
|
||||||
self.fqn.clone(),
|
self.fqn.clone(),
|
||||||
self.parameters.len(),
|
self.parameters.len(),
|
||||||
|
|||||||
@ -35,18 +35,10 @@ mod variable_locations;
|
|||||||
|
|
||||||
pub fn compile_dvm_function(
|
pub fn compile_dvm_function(
|
||||||
ir_function: &IrFunction,
|
ir_function: &IrFunction,
|
||||||
ir_type_infos: &[IrTypeInfo],
|
|
||||||
ir_variables_to_ir_type_infos: &HashMap<IrVariableId, IrTypeInfoId>,
|
|
||||||
constants_table: &mut ConstantsTable,
|
constants_table: &mut ConstantsTable,
|
||||||
) -> Function {
|
) -> Function {
|
||||||
let variable_locations = assign_registers(ir_function);
|
let variable_locations = assign_registers(ir_function);
|
||||||
let instructions = assemble_ir_function(
|
let instructions = assemble_ir_function(ir_function, &variable_locations, constants_table);
|
||||||
ir_function,
|
|
||||||
ir_type_infos,
|
|
||||||
ir_variables_to_ir_type_infos,
|
|
||||||
&variable_locations,
|
|
||||||
constants_table,
|
|
||||||
);
|
|
||||||
Function::new(
|
Function::new(
|
||||||
ir_function.fqn().into(),
|
ir_function.fqn().into(),
|
||||||
ir_function.parameters().len(),
|
ir_function.parameters().len(),
|
||||||
|
|||||||
@ -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
|
// for now, a statement can only have one successor
|
||||||
// this will need to be updated when we add jumps
|
// this will need to be updated when we add jumps
|
||||||
let statement_live_out = &mut live_out[statement_index];
|
let statement_live_out = &mut live_out[statement_index];
|
||||||
let successor_live_in = &live_in[statement_index + 1];
|
if let Some(successor_live_in) = live_in.get(statement_index + 1) {
|
||||||
for ir_variable_id in successor_live_in {
|
for ir_variable_id in successor_live_in {
|
||||||
if statement_live_out.insert(*ir_variable_id) {
|
if statement_live_out.insert(*ir_variable_id) {
|
||||||
did_work = true;
|
did_work = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -63,7 +63,7 @@ pub fn compile_compilation_unit(
|
|||||||
let mut dvm_functions = HashMap::new();
|
let mut dvm_functions = HashMap::new();
|
||||||
|
|
||||||
for ir_function in &lower_to_ir_result.functions {
|
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);
|
dvm_functions.insert(dvm_function.name_owned(), dvm_function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -70,7 +70,12 @@ impl<'a> SymbolCollectionContext<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn resolve_fqn(&self, suffix: &Rc<str>) -> String {
|
pub fn resolve_fqn(&self, suffix: &Rc<str>) -> 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()])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user