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