Skip to content

Commit

Permalink
Restructure backend (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-hartl authored Mar 28, 2024
1 parent be77b30 commit 5dfe58d
Show file tree
Hide file tree
Showing 31 changed files with 2,640 additions and 2,232 deletions.
2 changes: 1 addition & 1 deletion ir/crates/back/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
index_vec = "0.1.3"
tracing = "0.1.40"
rustc-hash = "1.1.0"
strum = { version = "0.26.1",features = ["derive"]}
strum = { version = "0.26.1", features = ["derive"] }
strum_macros = "0.26.1"
tracing-test = "0.2.4"
smallvec = "1.13.1"
Expand Down
23 changes: 0 additions & 23 deletions ir/crates/back/src/codegen/isa/mod.rs

This file was deleted.

12 changes: 7 additions & 5 deletions ir/crates/back/src/codegen/machine/abi/calling_convention.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use natrix_middle::Type;
use crate::codegen::machine;
use crate::codegen::machine::Size;
use crate::codegen::{
machine,
machine::Size,
};

pub trait CallingConvention {
type Reg: machine::PhysicalRegister;

fn parameter_slots(params: impl Iterator<Item=Size>) -> impl Iterator<Item=Slot<Self::Reg>>;
fn parameter_slots(params: impl Iterator<Item=Size>)
-> impl Iterator<Item=Slot<Self::Reg>>;

fn return_slot(size: Size) -> Slot<Self::Reg>;
}

pub enum Slot<R: machine::PhysicalRegister> {
Register(R),
Stack
Stack,
}
23 changes: 1 addition & 22 deletions ir/crates/back/src/codegen/machine/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
use std::fmt::Debug;
use std::hash::Hash;

pub use calling_convention::CallingConvention;

use crate::codegen::machine;
use crate::codegen::machine::asm::Assembler;

pub mod calling_convention;

pub trait Abi: Debug + Default + Clone + PartialEq + Eq + Hash {
type I: machine::MachineInstr<Abi=Self>;

type REG: machine::PhysicalRegister + 'static + Hash + Copy;

type ASSEMBLER: Assembler<Self>;

type CallingConvention: CallingConvention<Reg=Self::REG>;

fn get_assembler(base_addr: u64) -> Self::ASSEMBLER {
Self::ASSEMBLER::new(base_addr)
}
}

pub mod calling_convention;
17 changes: 10 additions & 7 deletions ir/crates/back/src/codegen/machine/asm.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::codegen::machine::{Abi, BasicBlockId};
use crate::codegen::machine::{
function::cfg::BasicBlockId,
TargetMachine,
};

pub trait Assembler {
type TM: TargetMachine;

fn new(base_addr: u64) -> Self;

pub trait Assembler<A: Abi> {
fn new(
base_addr: u64,
) -> Self;

fn begin_basic_block(&mut self, bb_id: BasicBlockId);

fn assemble(&mut self, instr: &A::I);
fn assemble(&mut self, instr: &<Self::TM as TargetMachine>::Instr);

fn finish(self) -> Vec<u8>;

Expand Down
49 changes: 49 additions & 0 deletions ir/crates/back/src/codegen/machine/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::fmt::Debug;

use smallvec::SmallVec;

use crate::codegen::{
machine::{
function::{
builder::{
MatchedPattern,
PatternIn,
},
Function,
},
TargetMachine,
},
selection_dag::Immediate,
};
use crate::codegen::machine::Instr;

type Reg<B> = <<B as Backend>::TM as TargetMachine>::Reg;
type BackInstr<B> = <<B as Backend>::TM as TargetMachine>::Instr;

pub trait Backend {
type TM: TargetMachine;

type P: Pattern<TM=Self::TM>;

fn patterns() -> &'static [Self::P];

fn mov(dest: Reg<Self>, src: Reg<Self>) -> BackInstr<Self>;

fn mov_imm(dest: Reg<Self>, imm: Immediate) -> BackInstr<Self>;

fn ret() -> BackInstr<Self>;

fn new() -> Self;
}

pub trait Pattern: Sized + Debug + Clone + PartialEq + Eq + 'static {
type TM: TargetMachine;

fn in_(&self) -> PatternIn;

fn into_instr(
self,
function: &mut Function<Self::TM>,
matched: MatchedPattern<Self::TM>,
) -> SmallVec<[Instr<Self::TM>; 2]>;
}
Loading

0 comments on commit 5dfe58d

Please sign in to comment.