Skip to content

Commit

Permalink
[IR] implementing ir parsing for br instr + [FIX] fixing a bit of cod…
Browse files Browse the repository at this point in the history
…e generation and only identifing var locations using their name
  • Loading branch information
Cr0a3 committed Sep 11, 2024
1 parent 5ea4ecf commit 360e894
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CompilationHelper {

#[allow(missing_docs)]
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.inner2).expect(&format!("{} has no variable location", node.inner2));
let src1 = *self.vars.get(&node.inner2.name).expect(&format!("{} has no variable location", node.inner2));

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

Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl CompilationHelper {
self.free(&node.inner1);
}

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

let src = match src {
super::VarLocation::Reg(reg) => MachineOperand::Reg(reg),
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl CompilationHelper {
todo!("implemt arguments which are passed over the stack");
}

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

match src {
VarLocation::Reg(reg) => instr.add_operand(MachineOperand::Reg(*reg)),
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::CodeGen::{MachineInstr, MachineMnemonic, MachineOperand};
impl CompilationHelper {
#[allow(missing_docs)]
pub fn compile_cast(&mut self, node: &Cast<Var, TypeMetadata, Var>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
let src1 = *self.vars.get(&node.inner1).expect("expected valid variable");
let src1 = *self.vars.get(&node.inner1.name).expect("expected valid variable");

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

Expand Down
6 changes: 3 additions & 3 deletions src/CodeGen/compilation/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ macro_rules! MathVarVar {
impl CompilationHelper {
#[allow(missing_docs)]
pub(crate) fn $func(&mut self, node: &$node<Var, Var, Var>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
let src1 = *self.vars.get(&node.inner1).expect("expected valid variable");
let src2 = *self.vars.get(&node.inner2).expect("expected valid variable");
let src1 = *self.vars.get(&node.inner1.name).expect("expected valid variable");
let src2 = *self.vars.get(&node.inner2.name).expect("expected valid variable");

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

Expand Down Expand Up @@ -62,7 +62,7 @@ macro_rules! MathVarType {
impl CompilationHelper {
#[allow(missing_docs)]
pub fn $func(&mut self, node: &$node<Var, Type, Var>, mc_sink: &mut Vec<MachineInstr>, block: &Block) {
let src1 = *self.vars.get(&node.inner1).expect("expected valid variable");
let src1 = *self.vars.get(&node.inner1.name).expect("expected valid variable");

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

Expand Down
8 changes: 4 additions & 4 deletions src/CodeGen/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct CompilationHelper {

pub(crate) call: MachineCallingConvention,

pub(crate) vars: HashMap<Var, VarLocation>,
pub(crate) vars: HashMap<String, VarLocation>,
}

impl CompilationHelper {
Expand All @@ -35,7 +35,7 @@ impl CompilationHelper {

/// frees the resources of the variable
pub(crate) fn free(&mut self, var: &Var) {
if let Some(location) = self.vars.get(var) {
if let Some(location) = self.vars.get(&var.name) {
match location {
VarLocation::Reg(reg) => self.regs.push(reg.arch(), reg.clone()),
}
Expand All @@ -52,7 +52,7 @@ impl CompilationHelper {
todo!("Registers ran out. And memory variables are currently not implemented")
};

self.vars.insert(var.clone(), location);
self.vars.insert(var.name.to_owned(), location);

location
}
Expand All @@ -75,7 +75,7 @@ impl CompilationHelper {
};

self.vars.insert(
func.arg(num),
func.arg(num).name,
location
);

Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen/compilation/ret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl CompilationHelper {

#[allow(missing_docs)]
pub fn compile_ret_var(&mut self, node: &Return<Var>, mc_sink: &mut Vec<MachineInstr>, _: &Block) {
let src = *self.vars.get(&node.inner1).expect("expected valid variable");
let src = *self.vars.get(&node.inner1.name).expect("expected valid variable");

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

Expand Down
8 changes: 8 additions & 0 deletions src/IR/nodes/br.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ impl Ir for BrCond<Var, Block, Block> {
fn compile(&self, registry: &mut crate::Target::TargetBackendDescr) {
registry.compile_br_cond(self)
}

fn uses(&self, var: &Var) -> bool {
if self.inner1.name.to_owned() == var.name.to_owned() {
true
} else {
false
}
}
}

/// This trait is used for building br nodes
Expand Down
5 changes: 5 additions & 0 deletions src/IR/parser/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ pub enum TokenType {

/// block:
Block(String),

/// cond
Cond,
}

impl TokenType {
Expand All @@ -99,6 +102,7 @@ impl TokenType {
TokenType::Func(_) => "func",
TokenType::TripleDot => "...",
TokenType::Block(_) => "block",
TokenType::Cond => "cond",
}.to_string()
}
}
Expand Down Expand Up @@ -145,6 +149,7 @@ impl IrLexer {
keys.insert("declare".into(), TokenType::Declare);
keys.insert("define".into(), TokenType::Define);
keys.insert("const".into(), TokenType::Const);
keys.insert("cond".into(), TokenType::Cond);


let lines = input
Expand Down
70 changes: 59 additions & 11 deletions src/IR/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,19 +558,67 @@ impl IrParser {
fn parse_br(&mut self) -> Result<Box<dyn Ir>, IrError> {
self.input.pop_front(); // br

self.expect( TokenType::Ident(String::new()) )?; // block names are idents

let block = if let TokenType::Ident(name) = &self.current_token()?.typ {
name.to_owned()
} else { unreachable!() };
let current = self.current_token()?;

self.input.pop_front();
match &current.typ {
TokenType::Ident(block) => {
let block = block.to_owned();

self.input.pop_front();

Ok(ir::Br::new(Box::from(Block {
name: block,
nodes: vec![],
varCount: 0
})))
},
TokenType::Cond => {
self.input.pop_front(); // cond

let var = self.expect(TokenType::Var(String::new()))?;

let var = if let TokenType::Var(var) = var.typ
{ var } else { unreachable!() };

self.input.pop_front();

self.expect(TokenType::Ident(String::new()))?;

let iftrue;

Ok(ir::Br::new(Box::from(Block {
name: block,
nodes: vec![],
varCount: 0
})))
if let TokenType::Ident(ident) = &self.current_token()?.typ {
iftrue = ident.to_owned();
} else { unreachable!() }

self.input.pop_front();

self.expect(TokenType::Comma)?;
self.input.pop_front();

self.expect(TokenType::Ident(String::new()))?;

let iffalse;

if let TokenType::Ident(ident) = &self.current_token()?.typ {
iffalse = ident.to_owned();
} else { unreachable!() }
self.input.pop_front();

Ok(ir::BrCond::new(Var {
name: var,
ty: TypeMetadata::Void,
}, Block {
name: iftrue,
nodes: vec![],
varCount: 0,
}, Block {
name: iffalse,
nodes: vec![],
varCount: 0,
}))
},
_ => Err(IrError::UnexpectedToken(self.current_token()?.clone())),
}
}

fn parse_cast(&mut self, var: String) -> Result<Box<dyn Ir>, IrError> {
Expand Down
32 changes: 32 additions & 0 deletions src/IR/parser/semnatic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ impl<'a> IrSemnatic<'a> {
self.analaysiz_div_var_var(&mut vars, node, loc)?;
} else if let Some(node) = any.downcast_ref::<Cast<Var, TypeMetadata, Var>>() {
self.analaysiz_cast(&mut vars, node, loc)?;
} else if let Some(node) = any.downcast_ref::<BrCond<Var, Block, Block>>() {
self.analaysiz_br_cond(func, &mut vars, node, loc)?;
}
}
}
Expand Down Expand Up @@ -388,6 +390,36 @@ impl<'a> IrSemnatic<'a> {
Ok(())
}

fn analaysiz_br_cond(&mut self, func: &String, vars: &mut HashMap<String, TypeMetadata>, node: &BrCond<Var, Block, Block>, loc: Loc) -> Result<(), IrError> {
let (_, _, blocks) = self.func_sigs.get(func).unwrap();

if !blocks.contains(&node.inner2.name) {
Err(IrError::Unkown {
what: "block".to_owned(),
name: node.inner2.name.to_owned(),
loc: loc.to_owned()
})?
}

if !blocks.contains(&node.inner3.name) {
Err(IrError::Unkown {
what: "block".to_owned(),
name: node.inner3.name.to_owned(),
loc: loc.to_owned()
})?
}

if !vars.contains_key(&node.inner1.name) {
Err(IrError::Unkown {
what: "variable".to_owned(),
name: node.inner1.name.to_owned(),
loc: loc
})?
}

Ok(())
}

fn analyize_const(&mut self, _: &String, _: &Vec<u8>, _: &Loc, _: Linkage) -> Result<(), IrError> {
Ok(()) // what can go wrong on constants?
}
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions tests/IR/br1.yl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# RUN:
cargo run -p ylc -- -in=%s -o=out.o
gcc out.o -o a.exe
./a.exe
# IN:
define u32 @main() {
entry:
%1 = u64 1
br cond %1 istrue, isfalse

istrue:
ret u32 235

isfalse:
ret u32 54
}

# EXIT_CODE=235

0 comments on commit 360e894

Please sign in to comment.