Skip to content

Commit

Permalink
Fix build errors && most of the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-hartl committed Jul 7, 2024
1 parent b45096b commit bce65f5
Show file tree
Hide file tree
Showing 34 changed files with 7,682 additions and 6,872 deletions.
4 changes: 2 additions & 2 deletions ir/crates/back/src/codegen/machine/function/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::codegen::{
pub struct FunctionBuilder<TM: TargetMachine> {
function: Function<TM>,
backend: TM::Backend,
bb_mapping: FxHashMap<natrix_middle::cfg::BasicBlockId, BasicBlockId>,
bb_mapping: FxHashMap<natrix_middle::cfg::BasicBlockRef, BasicBlockId>,
}

impl<TM: TargetMachine> FunctionBuilder<TM> {
Expand Down Expand Up @@ -222,7 +222,7 @@ impl<TM: TargetMachine> FunctionBuilder<TM> {
self.function
}

fn create_bb(&mut self, bb: natrix_middle::cfg::BasicBlockId) -> BasicBlockId {
fn create_bb(&mut self, bb: natrix_middle::cfg::BasicBlockRef) -> BasicBlockId {
let mbb = self.function.create_bb();
self.bb_mapping.insert(bb, mbb);
mbb
Expand Down
20 changes: 10 additions & 10 deletions ir/crates/back/src/codegen/selection_dag/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use daggy::{
use iter_tools::Itertools;
use natrix_middle::{
cfg::{
BasicBlockId,
BasicBlockRef,
BranchTerm,
JumpTarget,
Terminator,
Expand Down Expand Up @@ -53,7 +53,7 @@ pub struct Builder<'func, TM: TargetMachine> {
function: &'func mut Function<TM>,
sel_dag: SelectionDAG<TM>,
reg_mapping: SecondaryMap<natrix_middle::VReg, Option<VReg>>,
defining_nodes: FxHashMap<(VReg, BasicBlockId), NodeIndex>,
defining_nodes: FxHashMap<(VReg, BasicBlockRef), NodeIndex>,
}

impl<'func, TM: TargetMachine> Builder<'func, TM> {
Expand All @@ -71,7 +71,7 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {
let basic_blocks = func
.cfg
.basic_block_ids()
.filter(|bb_id| *bb_id != func.cfg.entry_block())
.filter(|bb_id| *bb_id != func.cfg.entry_block_ref())
.collect::<Vec<_>>();

for bb_id in basic_blocks {
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {
}
for (bb_id, bb) in func.cfg.basic_blocks() {
debug!("Building SelectionDAG for basic block {}", bb_id);
if bb_id == func.cfg.entry_block() {
if bb_id == func.cfg.entry_block_ref() {
for arg in bb.arguments() {
let mapped_reg = self.map_vreg(arg, func);
self.function.params.push(mapped_reg);
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {
func: &natrix_middle::Function,
) -> Operand<TM> {
match op {
natrix_middle::instruction::Op::Vreg(vreg) => {
natrix_middle::instruction::Op::Value(vreg) => {
Operand::Reg(Register::Virtual(self.map_vreg(*vreg, func)))
}
natrix_middle::instruction::Op::Const(constant) => Operand::Imm(match constant {
Expand All @@ -279,7 +279,7 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {

fn add_dependency(
&mut self,
bb_id: BasicBlockId,
bb_id: BasicBlockRef,
depending_node: NodeIndex,
producing_node: NodeIndex,
) {
Expand All @@ -290,7 +290,7 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {
.unwrap();
}

fn define_node(&mut self, bb_id: natrix_middle::cfg::BasicBlockId, op: Op<TM>) -> NodeIndex {
fn define_node(&mut self, bb_id: natrix_middle::cfg::BasicBlockRef, op: Op<TM>) -> NodeIndex {
let used_regs = op.consumed_regs();
let out_reg = op.out().and_then(|reg| reg.try_as_virtual());
debug!(
Expand All @@ -315,7 +315,7 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {

fn define_term_node(
&mut self,
bb_id: natrix_middle::cfg::BasicBlockId,
bb_id: natrix_middle::cfg::BasicBlockRef,
op: Op<TM>,
) -> NodeIndex {
let term_node = self.define_node(bb_id, op);
Expand All @@ -341,11 +341,11 @@ impl<'func, TM: TargetMachine> Builder<'func, TM> {
}
}

fn define_out_val(&mut self, node: NodeIndex, reg: VReg, bb_id: BasicBlockId) {
fn define_out_val(&mut self, node: NodeIndex, reg: VReg, bb_id: BasicBlockRef) {
self.defining_nodes.insert((reg, bb_id), node);
}

fn get_defining_node(&self, vreg: VReg, bb_id: BasicBlockId) -> Option<NodeIndex> {
fn get_defining_node(&self, vreg: VReg, bb_id: BasicBlockRef) -> Option<NodeIndex> {
self.defining_nodes.get(&(vreg, bb_id)).copied()
}
}
16 changes: 8 additions & 8 deletions ir/crates/back/src/codegen/selection_dag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use daggy::petgraph::dot::{
Dot,
};
use natrix_middle::{
cfg::BasicBlockId,
cfg::BasicBlockRef,
instruction::CmpOp,
};
use rustc_hash::FxHashMap;
Expand All @@ -38,11 +38,11 @@ type Dag<A> = daggy::Dag<Op<A>, Edge>;
pub struct BasicBlockDAG<TM: TargetMachine> {
dag: Dag<TM>,
term_node: Option<daggy::NodeIndex>,
bb: natrix_middle::cfg::BasicBlockId,
bb: natrix_middle::cfg::BasicBlockRef,
}

impl<TM: TargetMachine> BasicBlockDAG<TM> {
pub fn new(bb: natrix_middle::cfg::BasicBlockId) -> Self {
pub fn new(bb: natrix_middle::cfg::BasicBlockRef) -> Self {
Self {
dag: Dag::new(),
term_node: None,
Expand Down Expand Up @@ -86,13 +86,13 @@ impl<TM: TargetMachine> DerefMut for BasicBlockDAG<TM> {

#[derive(Debug, Default)]
pub struct SelectionDAG<TM: TargetMachine> {
pub basic_blocks: FxHashMap<natrix_middle::cfg::BasicBlockId, BasicBlockDAG<TM>>,
pub basic_blocks: FxHashMap<natrix_middle::cfg::BasicBlockRef, BasicBlockDAG<TM>>,
}

impl<TM: TargetMachine> SelectionDAG<TM> {
pub fn get_bb_dag(
&mut self,
basic_block: natrix_middle::cfg::BasicBlockId,
basic_block: natrix_middle::cfg::BasicBlockRef,
) -> &mut BasicBlockDAG<TM> {
self.basic_blocks
.entry(basic_block)
Expand Down Expand Up @@ -241,7 +241,7 @@ pub enum PseudoOp<TM: TargetMachine> {
Def(VReg),
Copy(Register<TM>, Register<TM>),
Ret(Option<Operand<TM>>),
Phi(Register<TM>, Vec<(Register<TM>, BasicBlockId)>),
Phi(Register<TM>, Vec<(Register<TM>, BasicBlockRef)>),
}

impl<TM: TargetMachine> PseudoOp<TM> {
Expand Down Expand Up @@ -273,8 +273,8 @@ pub enum MachineOp<TM: TargetMachine> {
Sub(Register<TM>, Operand<TM>, Operand<TM>),
Add(Register<TM>, Operand<TM>, Operand<TM>),
Cmp(Register<TM>, CmpOp, Operand<TM>, Operand<TM>),
Br(BasicBlockId),
CondBr(Operand<TM>, BasicBlockId, BasicBlockId),
Br(BasicBlockRef),
CondBr(Operand<TM>, BasicBlockRef, BasicBlockRef),
}

impl<TM: TargetMachine> MachineOp<TM> {
Expand Down
31 changes: 18 additions & 13 deletions ir/crates/front/src/grammar.lalrpop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::str::FromStr;
use crate::module::{Instruction, Operand, Literal, BasicBlock, Function, Type, Arg, Module, RegId, Target, BasicBlockId, CmpOp};
use crate::module::{Instruction, Operand, Literal, BasicBlock, Function, Type, Arg, Module, Target, CmpOp, Identifier};
grammar;

pub Module: Module = {
Expand All @@ -18,7 +18,7 @@ pub Function: Function = {
}

pub BasicBlock: BasicBlock = {
<id:BasicBlockId> <args:BasicBlockArgList?> ":" <instructions: Instruction*> => BasicBlock {
<id:Identifier> <args:BasicBlockArgList?> ":" <instructions: Instruction*> => BasicBlock {
args: args.unwrap_or_default(),
id,
instructions,
Expand All @@ -30,7 +30,7 @@ BasicBlockArgList: Vec<Arg> = {
}

Arg: Arg = {
<ty:Type> <id:RegId> => Arg {
<ty:Type> "%" <id:Identifier> => Arg {
ty,
id,
}
Expand All @@ -41,13 +41,13 @@ pub Instruction: Instruction = {
}

pub InstructionInner: Instruction = {
<decl:RegId> "=" "add" <ty: Type> <op1: Operand> "," <op2: Operand> => Instruction::Add(decl, ty, op1, op2),
<decl:RegId> "=" "sub" <ty: Type> <op1: Operand> "," <op2: Operand> => Instruction::Sub(decl, ty, op1, op2),
<decl:RegId> "=" <ty: Type> <op: Operand> => Instruction::Op(decl, ty, op),
<decl:RegId> "=" "icmp" <op: CmpOp> <ty: Type> <op1: Operand> "," <op2: Operand> => Instruction::ICmp(decl, op, ty, op1, op2),
<ty:Type> "%" <decl:Identifier> "=" "add" <op1: Operand> "," <op2: Operand> => Instruction::Add(decl, ty, op1, op2),
<ty:Type> "%" <decl:Identifier> "=" "sub" <op1: Operand> "," <op2: Operand> => Instruction::Sub(decl, ty, op1, op2),
<ty:Type> "%" <decl:Identifier> "=" <op: Operand> => Instruction::Op(decl, ty, op),
<ty:Type> "%" <decl:Identifier> "=" "cmp" <op: CmpOp> <op1: Operand> "," <op2: Operand> => Instruction::Cmp(decl, op, ty, op1, op2),
"condbr" <condition: Operand> <true_target: Target> "," <false_target: Target> => Instruction::Condbr(condition, true_target, false_target),
"br" <target: Target> => Instruction::Br(target),
"ret" <ty: Type> <op: Operand?> => Instruction::Ret(ty, op),
"ret" <op: Operand?> => Instruction::Ret(op),
}

pub CmpOp: CmpOp = {
Expand All @@ -56,7 +56,7 @@ pub CmpOp: CmpOp = {
}

pub Target: Target = {
<id: BasicBlockId> <args: TargetArgList?> => Target(id, args)
<id: Identifier> <args: TargetArgList?> => Target(id, args)
}

TargetArgList: Vec<Operand> = {
Expand All @@ -66,11 +66,13 @@ TargetArgList: Vec<Operand> = {

pub Operand: Operand = {
Literal => Operand::Literal(<>),
RegId => Operand::Register(<>)
"%" <id: Identifier> => Operand::Value(id),
}

Literal: Literal = {
SignedNum => Literal::Int(<>),
<n:SignedNum><ty:Type> => Literal::Int(n, ty),
"true" => Literal::Bool(true),
"false" => Literal::Bool(false),
}

Type: Type = {
Expand All @@ -84,11 +86,14 @@ Type: Type = {
"i64" => Type::I64,
"void" => Type::Void,
"bool" => Type::Bool,
"&" <ty:Type> => Type::Ptr(Box::new(ty)),
}

FunId: String = <id:r"@[a-zA-Z_][a-zA-Z0-9_]*"> => id[1..].to_string();
RegId: RegId = <id:r"v(0|([1-9][0-9]*))"> => RegId(u32::from_str(&id[1..]).unwrap());
BasicBlockId: BasicBlockId = <id:r"bb(0|([1-9][0-9]*))"> => BasicBlockId(u32::from_str(&id[2..]).unwrap());
Identifier: Identifier = {
<id:UnsignedNum> => id.to_string(),
<id:r"[a-zA-Z_][a-zA-Z0-9_]*"> => id.to_string()
}
SignedNum: i64 = <s: "-"?> <num:UnsignedNum> => match s {
Some(_) => -(num as i64),
None => num as i64
Expand Down
Loading

0 comments on commit bce65f5

Please sign in to comment.