Skip to content

Commit

Permalink
[IR] creating alloca ir node
Browse files Browse the repository at this point in the history
  • Loading branch information
Cr0a3 committed Sep 20, 2024
1 parent 36f90fc commit f25060a
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
47 changes: 47 additions & 0 deletions src/IR/nodes/alloca.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use crate::IR::{IRBuilder, TypeMetadata, Var};
use crate::Support::ColorClass;

use super::{Alloca, Ir};

impl Ir for Alloca<Var, TypeMetadata> {
fn dump(&self) -> String {
format!("{} = alloca {}", self.inner1.name, self.inner2)
}

fn dumpColored(&self, profile: crate::Support::ColorProfile) -> String {
format!("{} = {} {}",
profile.markup(&self.inner1.name, ColorClass::Var),
profile.markup("alloca", ColorClass::Instr),
profile.markup(&self.inner2.to_string(), ColorClass::Ty),
)
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn verify(&self, _: crate::prelude::FunctionType) -> Result<(), crate::prelude::VerifyError> {
Ok(())
}

fn clone_box(&self) -> Box<dyn Ir> {
Box::new( self.clone() )
}

fn compile(&self, registry: &mut crate::Target::TargetBackendDescr) {
registry.compile_alloca(self)
}
}

impl IRBuilder<'_> {
/// 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.get_mut(self.curr).expect("the IRBuilder needs to have an current block\nConsider creating one");

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

block.push_ir( Alloca::new(out.clone(), ty) );

out
}
}
3 changes: 3 additions & 0 deletions src/IR/nodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod math;
mod ret;
mod br;
mod cmp;
mod alloca;

pub use assign::*;
pub use call::*;
Expand Down Expand Up @@ -112,6 +113,8 @@ IrTypeWith3!(Div, T, U, Z);
IrTypeWith1!(Br, T);
IrTypeWith3!(BrCond, T, U, Z);

IrTypeWith2!(Alloca, T, U);

/// The cmp node is used to compare values
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cmp {
Expand Down
7 changes: 5 additions & 2 deletions src/IR/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ impl IrLexer {
keys.insert("const".into(), TokenType::Const);
keys.insert("cond".into(), TokenType::Cond);


let input = format!("{}\n", input);

let lines = input
.split('\n')
.map(|x| x.to_string())
Expand Down Expand Up @@ -184,7 +185,7 @@ impl IrLexer {
}

fn is_at_end(&self) -> bool {
self.current >= (self.input_stream.chars().count()) as u64
self.current > (self.input_stream.chars().count() - 1) as u64
}

fn update_loc(&mut self) {
Expand Down Expand Up @@ -222,6 +223,8 @@ impl IrLexer {
out = peek;
}
} else {
println!("curr: {}", self.current);
println!("len: {}", self.input_stream.chars().count());
Err(IrError::OutOfChars)?
}

Expand Down
18 changes: 17 additions & 1 deletion src/IR/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, VecDeque};

use crate::prelude::{Cmp, CmpMode, Ir};
use crate::prelude::{Alloca, Cmp, CmpMode, Ir};
use crate::Obj::Linkage;
use crate::IR::{ir, Block, Const, FnTy, Function, Type, TypeMetadata, Var};

Expand Down Expand Up @@ -426,6 +426,7 @@ impl IrParser {
"div" => self.parse_div(name)?,
"call" => self.parse_call(name)?,
"cmp" => self.parse_cmp(name)?,
"alloca" => self.parse_alloca(name)?,
_ => {
let ty = self.parse_type()?;
self.input.pop_front(); // the type
Expand Down Expand Up @@ -658,6 +659,21 @@ impl IrParser {
Ok(ir::Cast::new(in_var, out_ty, out))
}

fn parse_alloca(&mut self, var: String) -> Result<Box<dyn Ir>, IrError> {
self.input.pop_front();

let ty = self.parse_type()?;

self.input.pop_front();

let out = Var {
name: var,
ty: TypeMetadata::ptr,
};

Ok( Alloca::new(out, ty) )
}

fn parse_data_array(&mut self) -> Result<Vec<u8>, IrError> {
self.expect(TokenType::LSquare)?;
self.input.pop_front();
Expand Down
15 changes: 15 additions & 0 deletions src/IR/parser/semnatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ impl<'a> IrSemnatic<'a> {
self.analaysiz_br_cond(func, &mut vars, node, loc)?;
} else if let Some(node) = any.downcast_ref::<Cmp>() {
self.analaysiz_cmp(&mut vars, node, loc)?;
} else if let Some(node) = any.downcast_ref::<Alloca<Var, TypeMetadata>>() {
self.analaysiz_alloca(&mut vars, node, loc)?;
}
}
}
Expand Down Expand Up @@ -451,6 +453,19 @@ impl<'a> IrSemnatic<'a> {
Ok(())
}

fn analaysiz_alloca(&mut self, vars: &mut HashMap<String, TypeMetadata>, node: &Alloca<Var, TypeMetadata>, loc: Loc) -> Result<(), IrError> {
if vars.contains_key(&node.inner1.name) {
Err(IrError::DefinedTwice {
loc: loc,
name: node.inner1.name.to_owned()
})?
}

vars.insert(node.inner1.name.to_owned(), node.inner1.ty);

Ok(())
}

fn analyize_const(&mut self, _: &String, _: &Vec<u8>, _: &Loc, _: Linkage) -> Result<(), IrError> {
Ok(()) // what can go wrong on constants?
}
Expand Down

0 comments on commit f25060a

Please sign in to comment.