Insert a basic epilogue.

This commit is contained in:
Tyler McMullen
2017-11-28 19:51:19 -08:00
committed by Jakob Stoklund Olesen
parent 60b6bc3ab7
commit c1a64a5dc7

View File

@@ -15,8 +15,9 @@ use self::registers::RU;
use ir; use ir;
use regalloc; use regalloc;
use result; use result;
use ir::InstBuilder; use ir::{InstBuilder, InstructionData, Opcode};
use legalizer; use stack_layout::layout_stack;
use cursor::{Cursor, EncCursor};
#[allow(dead_code)] #[allow(dead_code)]
@@ -118,10 +119,10 @@ impl TargetIsa for Isa {
} }
fn prologue_epilogue(&self, func: &mut ir::Function) -> result::CtonResult { fn prologue_epilogue(&self, func: &mut ir::Function) -> result::CtonResult {
use stack_layout::layout_stack;
use cursor::{Cursor, EncCursor};
let word_size = if self.flags().is_64bit() { 8 } else { 4 }; let word_size = if self.flags().is_64bit() { 8 } else { 4 };
// TODO: Insert stack slot for FP and CSRs we will push
let stack_size = layout_stack(&mut func.stack_slots, word_size)?; let stack_size = layout_stack(&mut func.stack_slots, word_size)?;
// Append frame pointer to function signature // Append frame pointer to function signature
@@ -131,6 +132,7 @@ impl TargetIsa for Isa {
RU::rbp as RegUnit, RU::rbp as RegUnit,
); );
func.signature.params.push(rbp_arg); func.signature.params.push(rbp_arg);
func.signature.returns.push(rbp_arg);
// Append param to entry EBB // Append param to entry EBB
let entry_ebb = func.layout.entry_block().expect("missing entry block"); let entry_ebb = func.layout.entry_block().expect("missing entry block");
@@ -144,6 +146,7 @@ impl TargetIsa for Isa {
func.locations[fp] = ir::ValueLoc::Reg(RU::rbp as RegUnit); func.locations[fp] = ir::ValueLoc::Reg(RU::rbp as RegUnit);
// Insert prologue // Insert prologue
{
let mut pos = EncCursor::new(func, self).at_first_insertion_point(entry_ebb); let mut pos = EncCursor::new(func, self).at_first_insertion_point(entry_ebb);
pos.ins().x86_push(fp); pos.ins().x86_push(fp);
pos.ins().copy_special( pos.ins().copy_special(
@@ -151,9 +154,39 @@ impl TargetIsa for Isa {
RU::rsp as RegUnit, RU::rsp as RegUnit,
); );
pos.ins().adjust_sp_imm(-(stack_size as i32)); pos.ins().adjust_sp_imm(-(stack_size as i32));
}
//legalizer::legalize_function(func, &mut func.cfg, self); let mut return_insts = Vec::new();
for ebb in func.layout.ebbs() {
for inst in func.layout.ebb_insts(ebb) {
if let InstructionData::MultiAry { opcode, .. } = func.dfg[inst] {
if opcode == Opcode::Return {
return_insts.push(inst);
}
}
}
}
for inst in return_insts {
let fp_ret = self.insert_epilogue(inst, stack_size as i32, func);
func.dfg.append_inst_arg(inst, fp_ret);
}
Ok(()) Ok(())
} }
} }
impl Isa {
fn insert_epilogue(
&self,
inst: ir::Inst,
stack_size: i32,
func: &mut ir::Function,
) -> ir::Value {
let mut pos = EncCursor::new(func, self).at_inst(inst);
pos.ins().adjust_sp_imm(stack_size);
pos.ins().x86_pop(ir::types::I64)
}
}