mirror of
https://github.com/Noratrieb/uwucc.git
synced 2026-01-14 16:45:07 +01:00
make stack work
This commit is contained in:
parent
e28469fcc0
commit
46e5662aab
15 changed files with 122 additions and 117 deletions
|
|
@ -131,8 +131,8 @@ pub struct Statement {
|
|||
pub enum StatementKind {
|
||||
Alloca {
|
||||
result: Register,
|
||||
size: Operand,
|
||||
align: Operand,
|
||||
size: u64,
|
||||
align: u64,
|
||||
},
|
||||
Store {
|
||||
ptr: Operand,
|
||||
|
|
@ -234,6 +234,8 @@ impl Func<'_> {
|
|||
}
|
||||
|
||||
impl BbIdx {
|
||||
pub const ZERO: Self = Self(0);
|
||||
|
||||
pub fn from_usize(n: usize) -> Self {
|
||||
Self(n.try_into().unwrap())
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use super::{BbIdx, Branch, Func, Location, Operand};
|
|||
use crate::ir::visit::Visitor;
|
||||
|
||||
pub fn traverse_postorder(func: &Func<'_>) -> Vec<BbIdx> {
|
||||
// the final traversial, backwards.
|
||||
// the final traversal, backwards.
|
||||
// the starting bb has to be visited last.
|
||||
let mut traversal = vec![BbIdx(0)];
|
||||
let mut i = 0;
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ impl<'a> Customizer<'a> for DefaultCustomizer<'a> {
|
|||
self.0.set(Some(func));
|
||||
}
|
||||
|
||||
fn fmt_reg(&self, reg: Register, f: &mut fmt::Formatter<'_>, loc: Location) -> fmt::Result {
|
||||
fn fmt_reg(&self, reg: Register, f: &mut fmt::Formatter<'_>, _loc: Location) -> fmt::Result {
|
||||
match self.0.get().unwrap().regs[reg.0 as usize].name {
|
||||
None => write!(f, "%{}", reg.0),
|
||||
Some(name) => write!(f, "%{name}"),
|
||||
|
|
@ -101,10 +101,8 @@ impl<W: Write> PrettyPrinter<W> {
|
|||
} => {
|
||||
writeln!(
|
||||
self.out,
|
||||
" {} = alloca, size={}, align={}",
|
||||
" {} = alloca, size={size}, align={align}",
|
||||
print_reg(reg),
|
||||
print_op(size),
|
||||
print_op(align)
|
||||
)
|
||||
}
|
||||
StatementKind::Store {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use rustc_hash::FxHashSet;
|
||||
|
||||
use super::{visit::Visitor, Branch, Func, Register};
|
||||
use super::{visit::Visitor, Branch, Func, Register, StatementKind};
|
||||
use crate::ir::BbIdx;
|
||||
|
||||
pub fn validate(func: &Func<'_>) {
|
||||
|
|
@ -14,6 +14,16 @@ pub fn validate(func: &Func<'_>) {
|
|||
}
|
||||
}
|
||||
|
||||
for (i, bb) in func.bbs.iter().enumerate().skip(1) {
|
||||
if bb
|
||||
.statements
|
||||
.iter()
|
||||
.any(|stmt| matches!(stmt.kind, StatementKind::Alloca { .. }))
|
||||
{
|
||||
panic!("alloca is only allowed in first block, found in block {i}")
|
||||
}
|
||||
}
|
||||
|
||||
let mut reg_names = FxHashSet::default();
|
||||
for reg in &func.regs {
|
||||
if let Some(name) = reg.name {
|
||||
|
|
|
|||
|
|
@ -32,12 +32,10 @@ pub trait Visitor {
|
|||
match stmt.kind {
|
||||
StatementKind::Alloca {
|
||||
result,
|
||||
size,
|
||||
align,
|
||||
size: _,
|
||||
align: _,
|
||||
} => {
|
||||
self.visit_reg(result);
|
||||
self.visit_operand(size);
|
||||
self.visit_operand(align);
|
||||
}
|
||||
StatementKind::Store {
|
||||
ptr,
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ impl<'a, 'cx> FnLoweringCtxt<'a, 'cx> {
|
|||
for (var, def_span) in &decl.init_declarators {
|
||||
let tyl = self.lcx.layout_of(ty);
|
||||
let (name, name_span) = var.declarator.decl.name();
|
||||
let ptr_to = self.build.alloca(tyl.layout, Some(name), span);
|
||||
let ptr_to = self.build.reserve_local(tyl.layout, name, span);
|
||||
|
||||
let variable_info = VariableInfo {
|
||||
def_span: *def_span,
|
||||
|
|
@ -478,7 +478,7 @@ fn lower_func<'cx>(
|
|||
let span = param.declarator.1;
|
||||
|
||||
let alloca_name = Symbol::intern(&format!("{}.local", name));
|
||||
let ptr_to = cx.build.alloca(tyl.layout, Some(alloca_name), span);
|
||||
let ptr_to = cx.build.reserve_local(tyl.layout, alloca_name, span);
|
||||
|
||||
let variable_info = VariableInfo {
|
||||
def_span: span,
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use parser::{Span, Symbol};
|
|||
use super::LoweringCx;
|
||||
use crate::{
|
||||
ir::{
|
||||
self, BasicBlock, BbIdx, BinKind, Branch, ConstValue, Func, Layout, Operand, Register,
|
||||
RegisterData, Statement, StatementKind, TyLayout, UnaryKind,
|
||||
self, BasicBlock, BbIdx, BinKind, Branch, Func, Layout, Operand, Register, RegisterData,
|
||||
Statement, StatementKind, TyLayout, UnaryKind,
|
||||
},
|
||||
ty::{Ty, TyKind},
|
||||
};
|
||||
|
|
@ -47,6 +47,15 @@ impl<'a, 'cx> FuncBuilder<'a, 'cx> {
|
|||
reg
|
||||
}
|
||||
|
||||
pub fn reserve_local(&mut self, layout: &Layout, name: Symbol, span: Span) -> Register {
|
||||
// Every local is a singleton.
|
||||
let prev = self.current_bb;
|
||||
self.current_bb = BbIdx(0);
|
||||
let reg = self.alloca(layout, Some(name), span);
|
||||
self.current_bb = prev;
|
||||
reg
|
||||
}
|
||||
|
||||
pub fn alloca(&mut self, layout: &Layout, name: Option<Symbol>, span: Span) -> Register {
|
||||
let void_ptr = self
|
||||
.lcx
|
||||
|
|
@ -56,8 +65,8 @@ impl<'a, 'cx> FuncBuilder<'a, 'cx> {
|
|||
span,
|
||||
kind: StatementKind::Alloca {
|
||||
result: reg,
|
||||
size: Operand::Const(ConstValue::u64(layout.size)),
|
||||
align: Operand::Const(ConstValue::u64(layout.align)),
|
||||
size: layout.size,
|
||||
align: layout.align,
|
||||
},
|
||||
};
|
||||
self.cur_bb_mut().statements.push(stmt);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue