Fix some ir-related things with fields.
This commit is contained in:
parent
8759c3be27
commit
5be778ba80
@ -10,6 +10,8 @@ use crate::ir::ir_function::IrFunction;
|
|||||||
use crate::ir::ir_get_field_ref_mut::IrGetFieldRefMut;
|
use crate::ir::ir_get_field_ref_mut::IrGetFieldRefMut;
|
||||||
use crate::ir::ir_operation::IrOperation;
|
use crate::ir::ir_operation::IrOperation;
|
||||||
use crate::ir::ir_parameter::IrParameter;
|
use crate::ir::ir_parameter::IrParameter;
|
||||||
|
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
||||||
|
use crate::ir::ir_return::IrReturn;
|
||||||
use crate::ir::ir_set_field::IrSetField;
|
use crate::ir::ir_set_field::IrSetField;
|
||||||
use crate::ir::ir_statement::IrStatement;
|
use crate::ir::ir_statement::IrStatement;
|
||||||
use crate::ir::ir_variable::IrVariable;
|
use crate::ir::ir_variable::IrVariable;
|
||||||
@ -223,7 +225,7 @@ impl Constructor {
|
|||||||
if let Some(initializer) = field.initializer() {
|
if let Some(initializer) = field.initializer() {
|
||||||
// get a mut ref to the field
|
// get a mut ref to the field
|
||||||
let ir_get_field_ref_mut = IrGetFieldRefMut::new(
|
let ir_get_field_ref_mut = IrGetFieldRefMut::new(
|
||||||
self_variable.clone(),
|
IrParameterOrVariable::Variable(self_variable.clone()),
|
||||||
field.field_symbol().borrow().field_index(),
|
field.field_symbol().borrow().field_index(),
|
||||||
);
|
);
|
||||||
let field_mut_ref_variable_name: Rc<str> = ir_builder.new_t_var().into();
|
let field_mut_ref_variable_name: Rc<str> = ir_builder.new_t_var().into();
|
||||||
@ -263,6 +265,14 @@ impl Constructor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return complete self object
|
||||||
|
let ir_return_statement = IrStatement::Return(IrReturn::new(Some(IrExpression::Variable(
|
||||||
|
self_variable.clone(),
|
||||||
|
))));
|
||||||
|
ir_builder
|
||||||
|
.current_block_mut()
|
||||||
|
.add_statement(ir_return_statement);
|
||||||
|
|
||||||
ir_builder.finish_block();
|
ir_builder.finish_block();
|
||||||
let entry_block = ir_builder.get_block(entry_block_id);
|
let entry_block = ir_builder.get_block(entry_block_id);
|
||||||
|
|
||||||
|
|||||||
@ -3,17 +3,13 @@ use crate::ast::parameter::Parameter;
|
|||||||
use crate::ast::statement::Statement;
|
use crate::ast::statement::Statement;
|
||||||
use crate::ast::type_use::TypeUse;
|
use crate::ast::type_use::TypeUse;
|
||||||
use crate::diagnostic::Diagnostic;
|
use crate::diagnostic::Diagnostic;
|
||||||
use crate::ir::ir_assign::IrAssign;
|
|
||||||
use crate::ir::ir_expression::IrExpression;
|
|
||||||
use crate::ir::ir_function::IrFunction;
|
use crate::ir::ir_function::IrFunction;
|
||||||
use crate::ir::ir_operation::IrOperation;
|
|
||||||
use crate::ir::ir_parameter::IrParameter;
|
use crate::ir::ir_parameter::IrParameter;
|
||||||
use crate::ir::ir_statement::IrStatement;
|
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
||||||
use crate::ir::ir_variable::IrVariable;
|
|
||||||
use crate::source_range::SourceRange;
|
use crate::source_range::SourceRange;
|
||||||
use crate::symbol::Symbol;
|
|
||||||
use crate::symbol::class_symbol::ClassSymbol;
|
use crate::symbol::class_symbol::ClassSymbol;
|
||||||
use crate::symbol::function_symbol::FunctionSymbol;
|
use crate::symbol::function_symbol::FunctionSymbol;
|
||||||
|
use crate::symbol::Symbol;
|
||||||
use crate::symbol_table::{SymbolInsertError, SymbolTable};
|
use crate::symbol_table::{SymbolInsertError, SymbolTable};
|
||||||
use crate::type_info::TypeInfo;
|
use crate::type_info::TypeInfo;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -288,23 +284,11 @@ impl Function {
|
|||||||
let entry_block_id = builder.new_block();
|
let entry_block_id = builder.new_block();
|
||||||
|
|
||||||
// preamble
|
// preamble
|
||||||
// if we are a method, we need to put parameter 0 in a register
|
// if we are a method, we need to set the self parameter on the builder
|
||||||
if let Some(class_symbol) = class_context {
|
if let Some(_) = class_context {
|
||||||
let self_variable = IrVariable::new_vr(
|
|
||||||
"__self".into(),
|
|
||||||
entry_block_id,
|
|
||||||
&TypeInfo::ClassInstance(class_symbol.clone()),
|
|
||||||
);
|
|
||||||
let as_rc = Rc::new(RefCell::new(self_variable));
|
|
||||||
let parameter_0 = builder.parameters()[0].clone();
|
let parameter_0 = builder.parameters()[0].clone();
|
||||||
// put it in the self variable
|
// put it in the self parameter
|
||||||
builder
|
builder.set_self_parameter_or_variable(IrParameterOrVariable::IrParameter(parameter_0));
|
||||||
.current_block_mut()
|
|
||||||
.add_statement(IrStatement::Assign(IrAssign::new(
|
|
||||||
as_rc.clone(),
|
|
||||||
IrOperation::Load(IrExpression::Parameter(parameter_0)),
|
|
||||||
)));
|
|
||||||
builder.set_self_variable(as_rc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let function_symbol = self.function_symbol.as_ref().unwrap().borrow();
|
let function_symbol = self.function_symbol.as_ref().unwrap().borrow();
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::ir::ir_block::IrBlock;
|
use crate::ir::ir_block::IrBlock;
|
||||||
use crate::ir::ir_parameter::IrParameter;
|
use crate::ir::ir_parameter::IrParameter;
|
||||||
|
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
||||||
use crate::ir::ir_statement::IrStatement;
|
use crate::ir::ir_statement::IrStatement;
|
||||||
use crate::ir::ir_variable::IrVariable;
|
use crate::ir::ir_variable::IrVariable;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
@ -12,7 +13,7 @@ pub struct IrBuilder {
|
|||||||
t_var_counter: usize,
|
t_var_counter: usize,
|
||||||
blocks: HashMap<usize, Rc<RefCell<IrBlock>>>,
|
blocks: HashMap<usize, Rc<RefCell<IrBlock>>>,
|
||||||
current_block_builder: Option<IrBlockBuilder>,
|
current_block_builder: Option<IrBlockBuilder>,
|
||||||
self_variable: Option<Rc<RefCell<IrVariable>>>,
|
self_parameter_or_variable: Option<IrParameterOrVariable>,
|
||||||
field_variables: HashMap<Rc<str>, Rc<RefCell<IrVariable>>>,
|
field_variables: HashMap<Rc<str>, Rc<RefCell<IrVariable>>>,
|
||||||
mut_field_variables: HashMap<Rc<str>, Rc<RefCell<IrVariable>>>,
|
mut_field_variables: HashMap<Rc<str>, Rc<RefCell<IrVariable>>>,
|
||||||
}
|
}
|
||||||
@ -25,7 +26,7 @@ impl IrBuilder {
|
|||||||
t_var_counter: 0,
|
t_var_counter: 0,
|
||||||
blocks: HashMap::new(),
|
blocks: HashMap::new(),
|
||||||
current_block_builder: None,
|
current_block_builder: None,
|
||||||
self_variable: None,
|
self_parameter_or_variable: None,
|
||||||
field_variables: HashMap::new(),
|
field_variables: HashMap::new(),
|
||||||
mut_field_variables: HashMap::new(),
|
mut_field_variables: HashMap::new(),
|
||||||
}
|
}
|
||||||
@ -80,12 +81,15 @@ impl IrBuilder {
|
|||||||
format!("t{}", id)
|
format!("t{}", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_self_variable(&mut self, self_variable: Rc<RefCell<IrVariable>>) {
|
pub fn set_self_parameter_or_variable(
|
||||||
self.self_variable = Some(self_variable);
|
&mut self,
|
||||||
|
self_parameter_or_variable: IrParameterOrVariable,
|
||||||
|
) {
|
||||||
|
self.self_parameter_or_variable = Some(self_parameter_or_variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn self_variable(&self) -> &Rc<RefCell<IrVariable>> {
|
pub fn self_parameter_or_variable(&self) -> &IrParameterOrVariable {
|
||||||
self.self_variable.as_ref().unwrap()
|
self.self_parameter_or_variable.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn field_pointer_variables(&self) -> &HashMap<Rc<str>, Rc<RefCell<IrVariable>>> {
|
pub fn field_pointer_variables(&self) -> &HashMap<Rc<str>, Rc<RefCell<IrVariable>>> {
|
||||||
|
|||||||
@ -83,26 +83,20 @@ impl Assemble for IrAssign {
|
|||||||
|
|
||||||
match self.initializer.as_ref() {
|
match self.initializer.as_ref() {
|
||||||
IrOperation::GetFieldRef(ir_get_field_ref) => {
|
IrOperation::GetFieldRef(ir_get_field_ref) => {
|
||||||
let self_location = ir_get_field_ref
|
let self_location = ir_get_field_ref.self_parameter_or_variable().as_location();
|
||||||
.self_variable()
|
|
||||||
.borrow()
|
|
||||||
.descriptor()
|
|
||||||
.as_location();
|
|
||||||
builder.push(Instruction::GetFieldPointer(
|
builder.push(Instruction::GetFieldPointer(
|
||||||
self_location,
|
self_location,
|
||||||
ir_get_field_ref.field_index(),
|
ir_get_field_ref.field_index(),
|
||||||
destination,
|
destination,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
IrOperation::GetFieldRefMut(ir_get_field_ref) => {
|
IrOperation::GetFieldRefMut(ir_get_field_ref_mut) => {
|
||||||
let self_location = ir_get_field_ref
|
let self_location = ir_get_field_ref_mut
|
||||||
.self_variable()
|
.self_parameter_or_variable()
|
||||||
.borrow()
|
|
||||||
.descriptor()
|
|
||||||
.as_location();
|
.as_location();
|
||||||
builder.push(Instruction::GetFieldPointerMut(
|
builder.push(Instruction::GetFieldPointerMut(
|
||||||
self_location,
|
self_location,
|
||||||
ir_get_field_ref.field_index(),
|
ir_get_field_ref_mut.field_index(),
|
||||||
destination,
|
destination,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +1,24 @@
|
|||||||
use crate::ir::ir_variable::{IrVariable, IrVariableDescriptor, IrVrVariableDescriptor};
|
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
||||||
|
use crate::ir::ir_variable::IrVrVariableDescriptor;
|
||||||
use crate::ir::register_allocation::{OffsetCounter, VrUser};
|
use crate::ir::register_allocation::{OffsetCounter, VrUser};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
pub struct IrGetFieldRef {
|
pub struct IrGetFieldRef {
|
||||||
self_variable: Rc<RefCell<IrVariable>>,
|
self_parameter_or_variable: IrParameterOrVariable,
|
||||||
field_index: usize,
|
field_index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IrGetFieldRef {
|
impl IrGetFieldRef {
|
||||||
pub fn new(self_variable: Rc<RefCell<IrVariable>>, field_index: usize) -> Self {
|
pub fn new(self_parameter_or_variable: IrParameterOrVariable, field_index: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
self_variable,
|
self_parameter_or_variable,
|
||||||
field_index,
|
field_index,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn self_variable(&self) -> &Rc<RefCell<IrVariable>> {
|
pub fn self_parameter_or_variable(&self) -> &IrParameterOrVariable {
|
||||||
&self.self_variable
|
&self.self_parameter_or_variable
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn field_index(&self) -> usize {
|
pub fn field_index(&self) -> usize {
|
||||||
@ -29,7 +28,11 @@ impl IrGetFieldRef {
|
|||||||
|
|
||||||
impl Display for IrGetFieldRef {
|
impl Display for IrGetFieldRef {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(f, "&{}.{}", self.self_variable.borrow(), self.field_index)
|
write!(
|
||||||
|
f,
|
||||||
|
"&{}.{}",
|
||||||
|
self.self_parameter_or_variable, self.field_index
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,12 +42,7 @@ impl VrUser for IrGetFieldRef {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn vr_uses(&self) -> HashSet<IrVrVariableDescriptor> {
|
fn vr_uses(&self) -> HashSet<IrVrVariableDescriptor> {
|
||||||
match self.self_variable.borrow().descriptor() {
|
HashSet::new()
|
||||||
IrVariableDescriptor::VirtualRegister(vr_variable) => {
|
|
||||||
HashSet::from([vr_variable.clone()])
|
|
||||||
}
|
|
||||||
IrVariableDescriptor::Stack(_) => HashSet::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn propagate_spills(&mut self, spills: &HashSet<IrVrVariableDescriptor>) {}
|
fn propagate_spills(&mut self, spills: &HashSet<IrVrVariableDescriptor>) {}
|
||||||
|
|||||||
@ -1,25 +1,24 @@
|
|||||||
use crate::ir::ir_variable::{IrVariable, IrVariableDescriptor, IrVrVariableDescriptor};
|
use crate::ir::ir_parameter_or_variable::IrParameterOrVariable;
|
||||||
|
use crate::ir::ir_variable::IrVrVariableDescriptor;
|
||||||
use crate::ir::register_allocation::{OffsetCounter, VrUser};
|
use crate::ir::register_allocation::{OffsetCounter, VrUser};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
use std::rc::Rc;
|
|
||||||
|
|
||||||
pub struct IrGetFieldRefMut {
|
pub struct IrGetFieldRefMut {
|
||||||
self_variable: Rc<RefCell<IrVariable>>,
|
self_parameter_or_variable: IrParameterOrVariable,
|
||||||
field_index: usize,
|
field_index: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IrGetFieldRefMut {
|
impl IrGetFieldRefMut {
|
||||||
pub fn new(self_variable: Rc<RefCell<IrVariable>>, field_index: usize) -> Self {
|
pub fn new(self_parameter_or_variable: IrParameterOrVariable, field_index: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
self_variable,
|
self_parameter_or_variable,
|
||||||
field_index,
|
field_index,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn self_variable(&self) -> &Rc<RefCell<IrVariable>> {
|
pub fn self_parameter_or_variable(&self) -> &IrParameterOrVariable {
|
||||||
&self.self_variable
|
&self.self_parameter_or_variable
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn field_index(&self) -> usize {
|
pub fn field_index(&self) -> usize {
|
||||||
@ -32,7 +31,7 @@ impl Display for IrGetFieldRefMut {
|
|||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"&mut {}.{}",
|
"&mut {}.{}",
|
||||||
self.self_variable.borrow(),
|
self.self_parameter_or_variable,
|
||||||
self.field_index()
|
self.field_index()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@ -44,12 +43,7 @@ impl VrUser for IrGetFieldRefMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn vr_uses(&self) -> HashSet<IrVrVariableDescriptor> {
|
fn vr_uses(&self) -> HashSet<IrVrVariableDescriptor> {
|
||||||
match self.self_variable.borrow().descriptor() {
|
HashSet::new()
|
||||||
IrVariableDescriptor::VirtualRegister(vr_variable) => {
|
|
||||||
HashSet::from([vr_variable.clone()])
|
|
||||||
}
|
|
||||||
IrVariableDescriptor::Stack(stack_variable) => HashSet::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn propagate_spills(&mut self, spills: &HashSet<IrVrVariableDescriptor>) {}
|
fn propagate_spills(&mut self, spills: &HashSet<IrVrVariableDescriptor>) {}
|
||||||
|
|||||||
38
dmc-lib/src/ir/ir_parameter_or_variable.rs
Normal file
38
dmc-lib/src/ir/ir_parameter_or_variable.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
use crate::ir::ir_parameter::IrParameter;
|
||||||
|
use crate::ir::ir_variable::IrVariable;
|
||||||
|
use dvm_lib::instruction::Location;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum IrParameterOrVariable {
|
||||||
|
IrParameter(Rc<IrParameter>),
|
||||||
|
Variable(Rc<RefCell<IrVariable>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IrParameterOrVariable {
|
||||||
|
pub fn as_location(&self) -> Location {
|
||||||
|
match self {
|
||||||
|
IrParameterOrVariable::IrParameter(ir_parameter) => {
|
||||||
|
Location::StackFrameOffset(ir_parameter.stack_offset())
|
||||||
|
}
|
||||||
|
IrParameterOrVariable::Variable(ir_variable) => {
|
||||||
|
ir_variable.borrow().descriptor().as_location()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for IrParameterOrVariable {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
IrParameterOrVariable::IrParameter(ir_parameter) => {
|
||||||
|
write!(f, "{}", ir_parameter)
|
||||||
|
}
|
||||||
|
IrParameterOrVariable::Variable(ir_variable) => {
|
||||||
|
write!(f, "{}", ir_variable.borrow())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -11,6 +11,7 @@ pub mod ir_get_field_ref_mut;
|
|||||||
pub mod ir_multiply;
|
pub mod ir_multiply;
|
||||||
pub mod ir_operation;
|
pub mod ir_operation;
|
||||||
pub mod ir_parameter;
|
pub mod ir_parameter;
|
||||||
|
pub mod ir_parameter_or_variable;
|
||||||
pub mod ir_read_field;
|
pub mod ir_read_field;
|
||||||
pub mod ir_return;
|
pub mod ir_return;
|
||||||
pub mod ir_set_field;
|
pub mod ir_set_field;
|
||||||
|
|||||||
@ -61,13 +61,13 @@ impl ExpressibleSymbol {
|
|||||||
);
|
);
|
||||||
let as_rc = Rc::new(RefCell::new(field_ref_variable));
|
let as_rc = Rc::new(RefCell::new(field_ref_variable));
|
||||||
let to_insert = as_rc.clone();
|
let to_insert = as_rc.clone();
|
||||||
let self_variable = builder.self_variable().clone();
|
let self_parameter_or_variable = builder.self_parameter_or_variable().clone();
|
||||||
builder
|
builder
|
||||||
.current_block_mut()
|
.current_block_mut()
|
||||||
.add_statement(IrStatement::Assign(IrAssign::new(
|
.add_statement(IrStatement::Assign(IrAssign::new(
|
||||||
as_rc,
|
as_rc,
|
||||||
IrOperation::GetFieldRef(IrGetFieldRef::new(
|
IrOperation::GetFieldRef(IrGetFieldRef::new(
|
||||||
self_variable,
|
self_parameter_or_variable.clone(),
|
||||||
field_symbol.borrow().field_index(),
|
field_symbol.borrow().field_index(),
|
||||||
)),
|
)),
|
||||||
)));
|
)));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user