Skip to content

Commit 85b9df4

Browse files
committed
refactor: code quality
1 parent 29b7300 commit 85b9df4

21 files changed

Lines changed: 424 additions & 556 deletions

src/asm/aarch64.rs

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod function_generator;
22
mod inst;
3+
mod phi_lowering;
34
mod printer;
45
mod register_allocator;
56
mod types;
@@ -205,34 +206,16 @@ impl<'a> AArch64AsmGenerator<'a> {
205206
let mut insts: Vec<Inst> = Vec::new();
206207
insts.extend(Self::handle_arguments(func)?);
207208

208-
let mut ctx = FunctionGenerator {
209-
func_id: &func.identifier,
210-
frame: &frame,
211-
layouts,
212-
insts: &mut insts,
213-
next_vreg: &mut next_vreg,
214-
cond_map: &mut cond_map,
215-
};
216-
217-
for block in blocks.iter() {
218-
ctx.emit_label(&block.label);
219-
for stmt in &block.stmts {
220-
use ir::stmt::StmtInner::*;
221-
match &stmt.inner {
222-
Label(l) => ctx.emit_label(&l.label),
223-
Alloca(_) => { /* Stack slots handled by frame layout */ }
224-
Store(s) => ctx.emit_store(s)?,
225-
Load(s) => ctx.emit_load(s)?,
226-
BiOp(s) => ctx.emit_biop(s)?,
227-
Cmp(s) => ctx.emit_cmp(s)?,
228-
CJump(s) => ctx.emit_cjump(s)?,
229-
Jump(s) => ctx.emit_jump(s),
230-
Gep(s) => ctx.emit_gep(s)?,
231-
Call(s) => ctx.emit_call(s)?,
232-
Return(s) => ctx.emit_return(s)?,
233-
_ => return Err(Error::Internal("unexpected statement in block".into())),
234-
}
235-
}
209+
{
210+
let mut ctx = FunctionGenerator {
211+
func_id: &func.identifier,
212+
frame: &frame,
213+
layouts,
214+
insts: &mut insts,
215+
next_vreg: &mut next_vreg,
216+
cond_map: &mut cond_map,
217+
};
218+
phi_lowering::lower_function_blocks(&mut ctx, blocks)?;
236219
}
237220

238221
let alloc = register_allocator::allocate(&insts);

src/asm/aarch64/function_generator.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> FunctionGenerator<'a> {
113113
let cond = *self
114114
.cond_map
115115
.get(&cond_v)
116-
.ok_or_else(|| Error::MissingCond { vreg: cond_v })?;
116+
.ok_or(Error::MissingCond { vreg: cond_v })?;
117117

118118
let true_label = self.mangle_block_label(&s.true_label);
119119
let false_label = self.mangle_block_label(&s.false_label);
@@ -600,6 +600,54 @@ impl<'a> FunctionGenerator<'a> {
600600
}
601601
}
602602

603+
pub fn emit_stmt(&mut self, stmt: &ir::stmt::Stmt) -> Result<(), Error> {
604+
use ir::stmt::StmtInner::*;
605+
match &stmt.inner {
606+
Label(l) => {
607+
self.emit_label(&l.label);
608+
Ok(())
609+
}
610+
Alloca(_) => Ok(()),
611+
Store(s) => self.emit_store(s),
612+
Load(s) => self.emit_load(s),
613+
BiOp(s) => self.emit_biop(s),
614+
Cmp(s) => self.emit_cmp(s),
615+
CJump(s) => self.emit_cjump(s),
616+
Jump(s) => {
617+
self.emit_jump(s);
618+
Ok(())
619+
}
620+
Gep(s) => self.emit_gep(s),
621+
Call(s) => self.emit_call(s),
622+
Return(s) => self.emit_return(s),
623+
Phi(_) => Err(Error::Internal(
624+
"phi nodes should be lowered before assembly emission".into(),
625+
)),
626+
}
627+
}
628+
629+
pub fn emit_copy(&mut self, dst: &ir::Operand, src: &ir::Operand) -> Result<(), Error> {
630+
let dst_vreg = Self::operand_vreg(dst)?;
631+
let size = dtype_to_regsize(dst.dtype())?;
632+
633+
let src_op = match src {
634+
ir::Operand::Integer(i) => Operand::Immediate(i.value as i64),
635+
ir::Operand::Local(l) => Operand::Register(Register::Virtual(l.index)),
636+
ir::Operand::Global(_) => {
637+
return Err(Error::UnsupportedOperand {
638+
what: "global variable in phi copy".into(),
639+
});
640+
}
641+
};
642+
643+
self.insts.push(Inst::Mov {
644+
size,
645+
dst: Register::Virtual(dst_vreg),
646+
src: src_op,
647+
});
648+
Ok(())
649+
}
650+
603651
fn mangle_block_label(&self, label: &ir::BlockLabel) -> String {
604652
match label {
605653
ir::BlockLabel::BasicBlock(n) => mangle_bb(self.func_id, *n),

0 commit comments

Comments
 (0)