Skip to content

Commit

Permalink
[CODE GEN] implementing assign
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Aug 29, 2024
1 parent 72db96f commit db2a084
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 25 deletions.
74 changes: 65 additions & 9 deletions src/CodeGen/compilation/assign.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,77 @@
use crate::prelude::ConstAssign;
use crate::prelude::{Assign, Ir};
use crate::IR::{Block, Const, Type, Var};
use super::CompilationHelper;
use crate::CodeGen::MachineInstr;
use super::{CompilationHelper, VarLocation};
use crate::CodeGen::{MachineInstr, MachineMnemonic, MachineOperand};

impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_assign_var_type(&mut self, node: &ConstAssign<Var, Type>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
todo!()
pub fn compile_assign_var_type(&mut self, node: &Assign<Var, Type>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
let boxed: Box<dyn Ir> = Box::new(node.clone());

if !block.isVarUsedAfterNode(&boxed,&node.inner1) {
return; // dead code elimination
}

let location = self.alloc(&node.inner1);

let mut instr = MachineInstr::new(MachineMnemonic::Move);

match location {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(reg)),
}

instr.add_operand(MachineOperand::Imm(node.inner2.val() as i64));

mc_sink.push( instr );

}

#[allow(missing_docs)]
pub fn compile_assign_var_var(&mut self, node: &ConstAssign<Var, Var>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
todo!()
pub fn compile_assign_var_var(&mut self, node: &Assign<Var, Var>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
let src1 = *self.vars.get(&node.inner1).expect("expected valid variable");

let boxed: Box<dyn Ir> = Box::new(node.clone());

if !block.isVarUsedAfterNode(&boxed, &node.inner2) {
self.free(&node.inner2);
}
if !block.isVarUsedAfterNode(&boxed,&node.inner1) {
return; // dead code elimination
}

let location = self.alloc(&node.inner1);

let mut instr = MachineInstr::new(MachineMnemonic::Move);

match location {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(reg)),
}

match src1 {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(reg)),
}

mc_sink.push( instr );
}

#[allow(missing_docs)]
pub fn compile_assign_var_const(&mut self, node: &ConstAssign<Var, Const>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
todo!()
pub fn compile_assign_var_const(&mut self, node: &Assign<Var, Const>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
let boxed: Box<dyn Ir> = Box::new(node.clone());

if !block.isVarUsedAfterNode(&boxed,&node.inner1) {
return; // dead code elimination
}

let location = self.alloc(&node.inner1);

let mut instr = MachineInstr::new(
MachineMnemonic::AdressLoad(node.inner2.name.to_string())
);

match location {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(reg)),
}

mc_sink.push( instr );
}
}
4 changes: 3 additions & 1 deletion src/CodeGen/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub enum MachineOperand {

/// The mnemonic to use
#[allow(missing_docs)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MachineMnemonic {
Move,

Expand All @@ -59,6 +59,8 @@ pub enum MachineMnemonic {

Call,
Return,

AdressLoad(String),
}

/// a platform specifc instruction
Expand Down
12 changes: 6 additions & 6 deletions src/IR/nodes/assign.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

impl Ir for ConstAssign<Var, Type> {
impl Ir for Assign<Var, Type> {
fn dump(&self) -> String {
let meta: TypeMetadata = self.inner2.into();
format!("{} = {} {}", self.inner1.name, meta, self.inner2.val())
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Ir for ConstAssign<Var, Type> {
}
}

impl Ir for ConstAssign<Var, Var> {
impl Ir for Assign<Var, Var> {
fn dump(&self) -> String {
let meta: TypeMetadata = self.inner2.ty;
format!("{} = {} {}", self.inner1.name, meta, self.inner2.name)
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Ir for ConstAssign<Var, Var> {
}
}

impl Ir for ConstAssign<Var, Const> {
impl Ir for Assign<Var, Const> {
fn dump(&self) -> String {
format!("{} = ptr {}", self.inner1.name, self.inner2.name)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ impl BuildAssign<Type> for IRBuilder<'_> {

let out = Var::new(block, value.into());

block.push_ir(ConstAssign::new(out.clone(), value));
block.push_ir(Assign::new(out.clone(), value));

out
}
Expand All @@ -145,7 +145,7 @@ impl BuildAssign<Var> for IRBuilder<'_> {

let out = Var::new(block, value.ty);

block.push_ir(ConstAssign::new(out.clone(), value));
block.push_ir(Assign::new(out.clone(), value));

out
}
Expand All @@ -157,7 +157,7 @@ impl BuildAssign<&Const> for IRBuilder<'_> {

let out = Var::new(block, TypeMetadata::ptr);

block.push_ir(ConstAssign::new(out.clone(), value.clone()));
block.push_ir(Assign::new(out.clone(), value.clone()));

out
}
Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ macro_rules! IrTypeWith1 {

IrTypeWith1!(Return, T);
IrTypeWith3!(Call, T, U, Z);
IrTypeWith2!(ConstAssign, T, U);
IrTypeWith2!(Assign, T, U);
IrTypeWith3!(Cast, T, U, Z);
IrTypeWith3!(Add, T, U, Z);
IrTypeWith3!(Sub, T, U, Z);
Expand Down
10 changes: 5 additions & 5 deletions src/Optimizations/Passes/MathEval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Pass for PreComputeValue {
for node in block.nodes.iter_mut() {
if let Some(or) = node.as_any().downcast_ref::<Add<Type, Type, Var>>() {
let res = or.inner1.val() + or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
node.replace(Assign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
Expand All @@ -29,7 +29,7 @@ impl Pass for PreComputeValue {
}
if let Some(or) = node.as_any().downcast_ref::<Sub<Type, Type, Var>>() {
let res = or.inner1.val() - or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
node.replace(Assign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
Expand All @@ -44,7 +44,7 @@ impl Pass for PreComputeValue {
}
if let Some(or) = node.as_any().downcast_ref::<And<Type, Type, Var>>() {
let res = or.inner1.val() & or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
node.replace(Assign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
Expand All @@ -59,7 +59,7 @@ impl Pass for PreComputeValue {
}
if let Some(or) = node.as_any().downcast_ref::<Xor<Type, Type, Var>>() {
let res = or.inner1.val() ^ or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
node.replace(Assign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
Expand All @@ -74,7 +74,7 @@ impl Pass for PreComputeValue {
}
if let Some(or) = node.as_any().downcast_ref::<Or<Type, Type, Var>>() {
let res = or.inner1.val() | or.inner2.val();
node.replace(ConstAssign::new(or.inner3.clone(), {
node.replace(Assign::new(or.inner3.clone(), {
match or.inner3.ty {
TypeMetadata::u16 => Type::u16(res as u16),
TypeMetadata::u32 => Type::u32(res as u32),
Expand Down
6 changes: 3 additions & 3 deletions src/Target/target_descr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,6 @@ compile_func!(compile_cast_var, compile_cast, Cast<Var, TypeMetadata, Var>);

compile_func!(compile_call, compile_call, Call<Function, Vec<Var>, Var>);

compile_func!(compile_assign_var_type, compile_assign_var_type, ConstAssign<Var, Type>);
compile_func!(compile_assign_var_var, compile_assign_var_var, ConstAssign<Var, Var>);
compile_func!(compile_assign_var_const, compile_assign_var_const, ConstAssign<Var, Const>);
compile_func!(compile_assign_var_type, compile_assign_var_type, Assign<Var, Type>);
compile_func!(compile_assign_var_var, compile_assign_var_var, Assign<Var, Var>);
compile_func!(compile_assign_var_const, compile_assign_var_const, Assign<Var, Const>);

0 comments on commit db2a084

Please sign in to comment.