Skip to content

Commit

Permalink
[CODE GEN] adding type size hint
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Oct 27, 2024
1 parent b4bd0bd commit 9f9670e
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 135 deletions.
36 changes: 11 additions & 25 deletions src/CodeGen/compilation/assign.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
use crate::prelude::Assign;
use crate::IR::{Block, Const, Type, Var};
use super::{CompilationHelper, VarLocation};
use super::CompilationHelper;
use crate::CodeGen::{MachineInstr, MachineMnemonic, MachineOperand};

impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_assign_var_type(&mut self, node: &Assign<Var, Type>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let location = *self.vars.get(&node.inner1.name).unwrap();

let out = *self.vars.get(&node.inner1.name).unwrap();
let mut instr = MachineInstr::new(MachineMnemonic::Move);

match location {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}
instr.set_out(out.into());

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

Expand All @@ -24,7 +20,7 @@ impl CompilationHelper {
if let Some(phi_loc) = self.alloc.phi_vars.get(&node.inner1.name) {
let mut instr = MachineInstr::new(MachineMnemonic::Move);
instr.set_out((*phi_loc).into());
instr.add_operand(location.into());
instr.add_operand(out.into());

mc_sink.push(instr);
}
Expand All @@ -34,20 +30,13 @@ impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_assign_var_var(&mut self, node: &Assign<Var, Var>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let src1 = *self.vars.get(&node.inner2.name).expect(&format!("{} has no variable location", node.inner2));

let location = *self.vars.get(&node.inner1.name).unwrap();
let out = *self.vars.get(&node.inner1.name).unwrap();

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

match location {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}
instr.set_out(out.into());

match src1 {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.add_operand( MachineOperand::Stack(stack) ),
}
instr.add_operand(src1.into());

instr.meta = node.inner1.ty;

Expand All @@ -56,24 +45,21 @@ impl CompilationHelper {
if let Some(phi_loc) = self.alloc.phi_vars.get(&node.inner1.name) {
let mut instr = MachineInstr::new(MachineMnemonic::Move);
instr.set_out((*phi_loc).into());
instr.add_operand(location.into());
instr.add_operand(out.into());

mc_sink.push(instr);
}
}

#[allow(missing_docs)]
pub fn compile_assign_var_const(&mut self, node: &Assign<Var, Const>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let location = *self.vars.get(&node.inner1.name).unwrap();
let out = *self.vars.get(&node.inner1.name).unwrap();

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

match location {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}
instr.set_out(out.into());


instr.meta = node.inner1.ty; // is a pointer but i just wrote it here
Expand All @@ -83,7 +69,7 @@ impl CompilationHelper {
if let Some(phi_loc) = self.alloc.phi_vars.get(&node.inner1.name) {
let mut instr = MachineInstr::new(MachineMnemonic::Move);
instr.set_out((*phi_loc).into());
instr.add_operand(location.into());
instr.add_operand(out.into());
mc_sink.push(instr);
}
}
Expand Down
6 changes: 1 addition & 5 deletions src/CodeGen/compilation/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ impl CompilationHelper {
let iffalse = node.inner3.name.to_owned();

let src = *self.vars.get(&node.inner1.name).expect("expected valid variable");

let src = match src {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
super::VarLocation::Mem(stack) => MachineOperand::Stack(stack),
};
let src = src.into();

let mut cmp = MachineInstr::new(
MachineMnemonic::BrCond(iftrue, iffalse)
Expand Down
15 changes: 6 additions & 9 deletions src/CodeGen/compilation/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ impl CompilationHelper {
let mut save = MachineInstr::new( MachineMnemonic::Move );

let off = match self.alloc.alloc_stack(typ) {
VarLocation::Mem(off) => off,
VarLocation::Mem(off, ty) => (off, ty),
_ => unreachable!(),
};
saved.insert(name.to_owned(), (off, loc));

save.set_out(MachineOperand::Stack(off));
save.set_out(MachineOperand::Stack(off.0, off.1));
save.add_operand(loc.into());

mc_sink.push(save);
}
},
VarLocation::Mem(_) => {},
VarLocation::Mem(_, _) => {},
}
}

Expand Down Expand Up @@ -67,7 +67,7 @@ impl CompilationHelper {
let mut op = src.into();

if let Some((save, _)) = saved.get(&arg.name) {
op = MachineOperand::Stack(*save);
op = MachineOperand::Stack(save.0, save.1);
}

instr.set_out(MachineOperand::Reg(reg));
Expand Down Expand Up @@ -129,7 +129,7 @@ impl CompilationHelper {
let mut restore = MachineInstr::new( MachineMnemonic::Move);

restore.set_out(original.into());
restore.add_operand(MachineOperand::Stack(stack));
restore.add_operand(MachineOperand::Stack(stack.0, stack.1));

mc_sink.push( restore );
}
Expand All @@ -153,10 +153,7 @@ impl CompilationHelper {

instr.meta = node.inner1.ty.ret;

match loc {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}
instr.set_out(loc.into());

mc_sink.push(instr);

Expand Down
15 changes: 4 additions & 11 deletions src/CodeGen/compilation/cast.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::Cast;
use crate::IR::{Block, TypeMetadata, Var};
use super::{CompilationHelper, VarLocation};
use crate::CodeGen::{MachineInstr, MachineMnemonic, MachineOperand};
use super::CompilationHelper;
use crate::CodeGen::{MachineInstr, MachineMnemonic};

impl CompilationHelper {
#[allow(missing_docs)]
Expand All @@ -24,15 +24,8 @@ impl CompilationHelper {

let mut instr = MachineInstr::new(op);

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

match out {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.add_operand( MachineOperand::Stack(stack) ),
}
instr.add_operand(src1.into());
instr.set_out(out.into());

instr.meta = node.inner3.ty;

Expand Down
19 changes: 5 additions & 14 deletions src/CodeGen/compilation/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{prelude::Cmp, CodeGen::{MachineInstr, MachineMnemonic, MachineOperand}, IR::{Block, TypeMetadata}};
use crate::{prelude::Cmp, CodeGen::{MachineInstr, MachineMnemonic}, IR::{Block, TypeMetadata}};

use super::CompilationHelper;

Expand All @@ -7,21 +7,12 @@ impl CompilationHelper {
pub fn compile_cmp(&mut self, node: &Cmp, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let ls = *self.vars.get(&node.ls.name).expect("expected valid variable");
let rs = *self.vars.get(&node.rs.name).expect("expected valid variable");
let out = *self.vars.get(&node.out.name).unwrap();

let ls = match ls {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
super::VarLocation::Mem(stack) => MachineOperand::Stack(stack),
};
let ls = ls.into();
let rs = rs.into();

let rs = match rs {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
super::VarLocation::Mem(stack) => MachineOperand::Stack(stack),
};

let out = match *self.vars.get(&node.out.name).unwrap() {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
super::VarLocation::Mem(stack) => MachineOperand::Stack(stack),
};
let out = out.into();

let mut cmp = MachineInstr::new(MachineMnemonic::Compare(node.mode) );

Expand Down
12 changes: 3 additions & 9 deletions src/CodeGen/compilation/load.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::CodeGen::{MachineInstr, MachineMnemonic, MachineOperand};
use crate::CodeGen::{MachineInstr, MachineMnemonic};
use crate::IR::{Block, TypeMetadata, Var, ir::Load};

use super::CompilationHelper;
Expand All @@ -7,16 +7,10 @@ impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_load(&mut self, node: &Load<Var, Var, TypeMetadata>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let ptr = *self.vars.get(&node.inner2.name).expect("expected valid variable");
let ptr = match ptr {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
super::VarLocation::Mem(mem) => MachineOperand::Stack(mem),
};
let ptr = ptr.into();

let out = *self.vars.get(&node.inner1.name).unwrap();
let out = match out {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
super::VarLocation::Mem(mem) => MachineOperand::Stack(mem),
};
let out = out.into();

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

Expand Down
55 changes: 23 additions & 32 deletions src/CodeGen/compilation/math.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::CodeGen::instr::{MachineMnemonic, MachineOperand, MachineInstr};
use crate::IR::ir::*;
use crate::prelude::{Var, Block, Type};
use super::{CompilationHelper, VarLocation};
use super::CompilationHelper;

macro_rules! MathVarVar {
($func:ident, $node:ident, $mnemonic:expr) => {
Expand All @@ -15,21 +15,10 @@ macro_rules! MathVarVar {

let mut instr = MachineInstr::new($mnemonic);

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

match src2 {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.add_operand( MachineOperand::Stack(stack) ),
}
instr.add_operand(src1.into());
instr.add_operand(src2.into());
instr.set_out(out.into());

match out {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}

instr.meta = node.inner3.ty;

mc_sink.push(instr);
Expand Down Expand Up @@ -67,22 +56,21 @@ macro_rules! MathVarType {

let mut instr = MachineInstr::new($mnemonic);

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


instr.add_operand(src1.into());
instr.add_operand(MachineOperand::Imm(node.inner2.val()));

match out {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}

instr.set_out(out.into());

instr.meta = node.inner3.ty;

mc_sink.push(instr);

if let Some(phi_loc) = self.alloc.phi_vars.get(&node.inner3.name) {
let mut instr = MachineInstr::new(MachineMnemonic::Move);
instr.set_out((*phi_loc).into());
instr.add_operand(out.into());
mc_sink.push(instr);
}
}
}
};
Expand All @@ -109,17 +97,20 @@ macro_rules! MathTypeType {
let mut instr = MachineInstr::new($mnemonic);

instr.add_operand(MachineOperand::Imm(node.inner1.val()));

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

match out {
VarLocation::Reg(reg) => instr.set_out(MachineOperand::Reg(reg)),
VarLocation::Mem(stack) => instr.set_out( MachineOperand::Stack(stack) ),
}

instr.set_out(out.into());

instr.meta = node.inner3.ty;

mc_sink.push(instr);

if let Some(phi_loc) = self.alloc.phi_vars.get(&node.inner3.name) {
let mut instr = MachineInstr::new(MachineMnemonic::Move);
instr.set_out((*phi_loc).into());
instr.add_operand(out.into());
mc_sink.push(instr);
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ impl CompilationHelper {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum VarLocation {
Reg(Reg),
Mem(i64),
Mem(i64, TypeMetadata),
}
18 changes: 4 additions & 14 deletions src/CodeGen/compilation/store.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
use crate::prelude::{Store, Var, Type, Block};
use crate::CodeGen::{MachineInstr, MachineMnemonic, MachineOperand};
use crate::IR::TypeMetadata;

use super::CompilationHelper;

impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_store(&mut self, node: &Store<Var, Var>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let in_var = self.vars.get(&node.inner2.name).expect("expected valid variable");
let in_var = match in_var {
super::VarLocation::Reg(reg) => MachineOperand::Reg(*reg),
super::VarLocation::Mem(mem) => MachineOperand::Stack(*mem),
};
let in_var = (*in_var).into();

let ptr = self.vars.get(&node.inner1.name).expect("expected valid variable");
let ptr = match ptr {
super::VarLocation::Reg(reg) => MachineOperand::Reg(*reg),
super::VarLocation::Mem(mem) => MachineOperand::Stack(*mem),
};
let ptr = (*ptr).into();

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

Expand All @@ -32,17 +25,14 @@ impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_store_ty(&mut self, node: &Store<Var, Type>, mc_sink: &mut Vec<MachineInstr>, _: &Block, _: &mut crate::prelude::Module) {
let ptr = self.vars.get(&node.inner1.name).expect("expected valid variable");
let ptr = match ptr {
super::VarLocation::Reg(reg) => MachineOperand::Reg(*reg),
super::VarLocation::Mem(mem) => MachineOperand::Stack(*mem),
};
let ptr = (*ptr).into();

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

instr.set_out( ptr );
instr.add_operand(MachineOperand::Imm(node.inner2.val()));

instr.meta = TypeMetadata::ptr;
instr.meta = node.inner2.into();

mc_sink.push( instr );
}
Expand Down
Loading

0 comments on commit 9f9670e

Please sign in to comment.