more lowering

This commit is contained in:
nora 2023-05-22 22:14:44 +02:00
parent b9a2f939c4
commit e68ec920f5

View file

@ -1,36 +1,40 @@
/// A low level IR used for codegen. //! A low level IR used for codegen.
/// //!
/// The following expression is lowered to the following IR: //! The following expression is lowered to the following IR:
/// //!
/// ```c //! ```c
/// int i = 0; //! int i = 0;
/// long l = 1; //! long l = 1;
/// if (true) { //! if (true) {
/// i = 1; //! i = 1;
/// } else { //! } else {
/// i = 2; //! i = 2;
/// } //! }
/// yeet(i); //! yeet(i);
/// ``` //! ```
/// //!
/// ```c //! ```c
/// bb0: //! bb0:
/// %0 = alloca 4, 4 //! %0 = alloca 4, 4
/// store _0, 0 //! store _0, 0
/// %1 = alloca 8, 8 //! %1 = alloca 8, 8
/// store %1, 1 //! store %1, 1
/// branch true, bb1, bb2 //! branch true, bb1, bb2
/// bb1: //! bb1:
/// store %0, 1 //! store %0, 1
/// branch bb3 //! branch bb3
/// bb2: //! bb2:
/// store %0, 2 //! store %0, 2
/// branch bb3 //! branch bb3
/// bb3: //! bb3:
/// %val = load %0 //! %val = load %0
/// call yeet(%val) //! call yeet(%val)
/// ``` //! ```
mod pretty;
use parser::{Span, Symbol}; use parser::{Span, Symbol};
pub use pretty::{func_to_string, ir_to_string};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use crate::ty::Ty; use crate::ty::Ty;
@ -50,14 +54,15 @@ pub struct Ir {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Func { pub struct Func {
pub regs: Vec<RegisterData>,
pub bbs: Vec<BasicBlock>, pub bbs: Vec<BasicBlock>,
pub name: Symbol,
pub def_span: Span, pub def_span: Span,
pub ret_ty: Ty, pub ret_ty: Ty,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct BasicBlock { pub struct BasicBlock {
pub regs: Vec<RegisterData>,
pub statements: Vec<Statement>, pub statements: Vec<Statement>,
pub term: Branch, pub term: Branch,
} }
@ -85,6 +90,7 @@ pub enum StatementKind {
}, },
Store { Store {
ptr_reg: Register, ptr_reg: Register,
value: Operand,
size: Operand, size: Operand,
align: Operand, align: Operand,
}, },
@ -94,25 +100,20 @@ pub enum StatementKind {
size: Operand, size: Operand,
align: Operand, align: Operand,
}, },
Arith { BinOp {
kind: ArithKind, kind: BinKind,
lhs: Register, lhs: Operand,
rhs: Register, rhs: Operand,
result: Register,
},
Comp {
kind: CompKind,
lhs: Register,
rhs: Register,
result: Register, result: Register,
}, },
PtrOffset { PtrOffset {
result: Register,
reg: Register, reg: Register,
amount: Operand, amount: Operand,
}, },
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum Operand { pub enum Operand {
Reg(Register), Reg(Register),
Const(ConstValue), Const(ConstValue),
@ -121,24 +122,17 @@ pub enum Operand {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Branch { pub enum Branch {
Goto(u32), Goto(u32),
Switch { Switch { cond: Operand, yes: u32, no: u32 },
cond: Option<RValue>, Ret(Operand),
yes: u32,
no: u32,
},
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum ArithKind { pub enum BinKind {
Add, Add,
Sub, Sub,
Mul, Mul,
Div, Div,
Mod, Mod,
}
#[derive(Debug, Clone)]
pub enum CompKind {
Eq, Eq,
Neq, Neq,
Gt, Gt,
@ -147,14 +141,9 @@ pub enum CompKind {
Leq, Leq,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, Copy)]
pub enum RValue {
Register(u32),
Constant(ConstValue),
}
#[derive(Debug, Clone)]
pub enum ConstValue { pub enum ConstValue {
Void,
Int(u128), Int(u128),
} }
@ -169,3 +158,9 @@ impl ConstValue {
Self::Int(int.into()) Self::Int(int.into())
} }
} }
impl Operand {
pub fn const_u64(int: u64) -> Self {
Self::Const(ConstValue::u64(int))
}
}