Skip to content

Commit

Permalink
[YCC] ycc is now able to compile ifs
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Nov 24, 2024
1 parent 74d14ab commit 547769a
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 222 deletions.
27 changes: 27 additions & 0 deletions src/IR/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ pub struct Function {

pub(crate) linkage: Linkage,
pub(crate) blocks: VecDeque<Block>,

pub(crate) curr_block: usize,
}

impl Function {
Expand All @@ -76,6 +78,8 @@ impl Function {
name: name,

linkage: Linkage::Internal,

curr_block: 0,
}
}

Expand All @@ -97,9 +101,32 @@ impl Function {
/// Adds a new block to the function
pub fn addBlock(&mut self, name: &str) -> BlockId {
self.blocks.push_back(Block::new(name, &self));
self.curr_block = self.blocks.len() - 1;
BlockId(name.to_owned())
}

/// Returns the current block
pub fn currentBlock(&self) -> BlockId {
let curr_block = self.blocks.back().expect("expected current block");
let name = curr_block.name.to_owned();

BlockId(name)
}

/// Sets the current block (Time: `O(n)`)
pub fn setCurrBlock(&mut self, curr_block: BlockId) {
let mut index = 0;

for block in &self.blocks {
if block.name == curr_block.name {
self.curr_block = index;
break;
}

index += 1;
}
}

/// Emits the Ir of the function into a string
pub fn dump(&self) -> String {
if self.linkage == Linkage::Extern {
Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/alloca.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Alloca {
impl Function {
/// Builds an stack allocation (the out var is the pointer to the allocated stack region)
pub fn BuildAlloca(&mut self, ty: TypeMetadata) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
6 changes: 3 additions & 3 deletions src/IR/nodes/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub trait BuildAssign<T> {
}
impl BuildAssign<Type> for Function {
fn BuildAssign(&mut self, value: Type) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -259,7 +259,7 @@ impl BuildAssign<Type> for Function {

impl BuildAssign<Var> for Function {
fn BuildAssign(&mut self, value: Var) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -271,7 +271,7 @@ impl BuildAssign<Var> for Function {

impl BuildAssign<&Const> for Function {
fn BuildAssign(&mut self, value: &Const) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
4 changes: 2 additions & 2 deletions src/IR/nodes/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub trait BuildBr<T> {

impl BuildBr<&BlockId> for Function {
fn BuildBr(&mut self, to: &BlockId) {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

block.push_ir(Br::new( to.to_owned() ));
}
Expand All @@ -198,7 +198,7 @@ pub trait BuildBrCond<T, U, Z> {

impl BuildBrCond<Var, &BlockId, &BlockId> for Function {
fn BuildBrCond(&mut self, val: Var, iftrue: &BlockId, iffalse: &BlockId) {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

block.push_ir( BrCond::new(val, iftrue.to_owned(), iffalse.to_owned()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub trait BuildCall<T, U> {
}
impl BuildCall<&FuncId, Vec<IROperand>> for Function {
fn BuildCall(&mut self, func: &FuncId, args: Vec<IROperand>) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
4 changes: 2 additions & 2 deletions src/IR/nodes/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub trait BuildCast<T, U> {

impl BuildCast<Var, TypeMetadata> for Function {
fn BuildCast(&mut self, var: Var, ty: TypeMetadata) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -156,7 +156,7 @@ impl BuildCast<Var, TypeMetadata> for Function {

impl BuildCast<Type, TypeMetadata> for Function {
fn BuildCast(&mut self, value: Type, ty: TypeMetadata) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
8 changes: 4 additions & 4 deletions src/IR/nodes/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub trait BuildCmp<T, U> {

impl BuildCmp<Var, Var> for Function {
fn BuildCmp(&mut self, mode: CmpMode, ls: Var, rs: Var) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -230,7 +230,7 @@ impl BuildCmp<Var, Var> for Function {

impl BuildCmp<Var, Type> for Function {
fn BuildCmp(&mut self, mode: CmpMode, ls: Var, rs: Type) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -242,7 +242,7 @@ impl BuildCmp<Var, Type> for Function {

impl BuildCmp<Type, Var> for Function {
fn BuildCmp(&mut self, mode: CmpMode, ls: Type, rs: Var) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -254,7 +254,7 @@ impl BuildCmp<Type, Var> for Function {

impl BuildCmp<Type, Type> for Function {
fn BuildCmp(&mut self, mode: CmpMode, ls: Type, rs: Type) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl IsNode for DebugNode {
impl Function {
/// Sets the source location for debugging (all of the ir nodes will respond to the location till an new location is set)
pub fn BuildDebug(&mut self, line: i64, coloumn: i64, file: PathBuf) {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

block.push_ir( Box::new( DebugNode {
line: line,
Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/getelemptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl Function {
/// }
/// ```
pub fn BuildGetelemptr(&mut self, var: Var, index: Var, ty: TypeMetadata) -> Var {
let block = self.blocks.back_mut().expect("expected current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl EvalOptVisitor for Load {
impl Function {
/// the load instruction loads an value from an pointer into a normal variable
pub fn BuildLoad(&mut self, ptr: Var, ty: TypeMetadata) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
8 changes: 4 additions & 4 deletions src/IR/nodes/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ macro_rules! MathIrNode {

impl $build_trait<Type, Type> for Function {
fn $build_func(&mut self, op0: Type, op1: Type) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

let op0Ty: TypeMetadata = op0.into();

Expand All @@ -25,7 +25,7 @@ macro_rules! MathIrNode {

impl $build_trait<Var, Type> for Function {
fn $build_func(&mut self, op0: Var, op1: Type) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

let op0Ty: TypeMetadata = op0.ty.into();

Expand All @@ -40,7 +40,7 @@ macro_rules! MathIrNode {

impl $build_trait<Type, Var> for Function {
fn $build_func(&mut self, op0: Type, op1: Var) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

let op0Ty: TypeMetadata = op0.into();

Expand All @@ -55,7 +55,7 @@ macro_rules! MathIrNode {

impl $build_trait<Var, Var> for Function {
fn $build_func(&mut self, op0: Var, op1: Var) -> Var {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

let op0Ty: TypeMetadata = op0.ty.into();

Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/neg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Function {
panic!("variables need to be signed to get negated");
}

let block = self.blocks.back_mut().expect("expects current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/phi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl IsNode for Phi {
impl Function {
/// Builds the phi node which recives variables from different blocks
pub fn BuildPhi(&mut self, typ: TypeMetadata, recipients: Vec<(&Block, Var)>) -> Var {
let block = self.blocks.back_mut().expect("expected valid current block.\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

let mut owned_recipients = Vec::new();

Expand Down
4 changes: 2 additions & 2 deletions src/IR/nodes/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ pub trait BuildReturn<T> {

impl BuildReturn<Type> for Function {
fn BuildRet(&mut self, val: Type) {
self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one")
self.blocks.get_mut(self.curr_block).expect("invalid current block")
.push_ir(Return::new(IROperand::Type(val)))
}
}

impl BuildReturn<Var> for Function {
fn BuildRet(&mut self, var: Var) {
self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one")
self.blocks.get_mut(self.curr_block).expect("invalid current block")
.push_ir(Return::new(IROperand::Var(var)))
}
}
8 changes: 4 additions & 4 deletions src/IR/nodes/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub trait BuildSelect<T, U> {

impl BuildSelect<Type, Type> for Function {
fn BuildSelect(&mut self, cond: Var, yes: Type, no: Type) -> Var {
let block = self.blocks.back_mut().expect("expected valid current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -253,7 +253,7 @@ impl BuildSelect<Type, Type> for Function {

impl BuildSelect<Type, Var> for Function {
fn BuildSelect(&mut self, cond: Var, yes: Type, no: Var) -> Var {
let block = self.blocks.back_mut().expect("expected valid current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -270,7 +270,7 @@ impl BuildSelect<Type, Var> for Function {

impl BuildSelect<Var, Type> for Function {
fn BuildSelect(&mut self, cond: Var, yes: Var, no: Type) -> Var {
let block = self.blocks.back_mut().expect("expected valid current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand All @@ -287,7 +287,7 @@ impl BuildSelect<Var, Type> for Function {

impl BuildSelect<Var, Var> for Function {
fn BuildSelect(&mut self, cond: Var, yes: Var, no: Var) -> Var {
let block = self.blocks.back_mut().expect("expected valid current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

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

Expand Down
4 changes: 2 additions & 2 deletions src/IR/nodes/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ pub trait BuildStore<T, U> {

impl BuildStore<Var, Var> for Function {
fn BuildStore(&mut self, target: Var, value: Var) {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

block.push_ir( Store::new(target, IROperand::Var(value)) );
}
}

impl BuildStore<Var, Type> for Function {
fn BuildStore(&mut self, target: Var, value: Type) {
let block = self.blocks.back_mut().expect("the IRBuilder needs to have an current block\nConsider creating one");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

block.push_ir( Store::new(target, IROperand::Type(value)) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/IR/nodes/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl IsNode for Switch {
impl Function {
/// Builds an switch statement
pub fn BuildSwitch(&mut self, source: Var, default: &BlockId, cases: HashMap<Type, &BlockId>) {
let block = self.blocks.back_mut().expect("expected current block");
let block = self.blocks.get_mut(self.curr_block).expect("invalid current block");

let mut owned_cases = HashMap::new();

Expand Down
1 change: 1 addition & 0 deletions src/IR/parser/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ impl IrGen {
name: name,
linkage: scope,
blocks: VecDeque::new(),
curr_block: 0,
};

for (name, block) in body {
Expand Down
Loading

0 comments on commit 547769a

Please sign in to comment.