Skip to content

Commit

Permalink
fix(rust-irgen): Emit branch unconditional for jmp
Browse files Browse the repository at this point in the history
  • Loading branch information
chorman0773 committed Aug 27, 2023
1 parent 3ad3ffc commit c7efcac
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 7 additions & 0 deletions codegen-x86/src/mc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,13 @@ impl MCWriter for X86MCWriter {
MCInsn::Label(label) => {
sym_accepter(label.clone(), encoder.offset() as u64);
}
MCInsn::UnconditionalBranch(label) => encoder.write_insn(X86Instruction::new(
X86Opcode::Jmp,
vec![X86Operand::RelAddr(Address::Symbol {
name: label.clone(),
disp: 0,
})],
))?,
_ => todo!(),
}
}
Expand Down
12 changes: 12 additions & 0 deletions rust/src/irgen/xir_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,18 @@ impl<'a> JumpVisitor for XirJumpVisitor<'a> {
}
}

impl<'a> Drop for XirJumpVisitor<'a> {
fn drop(&mut self) {
self.body
.block
.items
.push(ir::BlockItem::Expr(ir::Expr::Branch {
cond: ir::BranchCondition::Always,
target: self.targ.unwrap().id(),
}))
}
}

pub struct XirExprVisitor<'a> {
defs: &'a Definitions,
names: &'a NameMap,
Expand Down
6 changes: 5 additions & 1 deletion xlang/xlang_backend/src/mc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ pub enum MCInsn<Loc> {

/// A Label
Label(String),
/// Unconditional Branch
UnconditionalBranch(String),
}

impl<Loc> Default for MCInsn<Loc> {
Expand Down Expand Up @@ -343,6 +345,7 @@ impl<F: MachineFeatures> FunctionRawCodegen for MCFunctionCodegen<F> {

fn write_target(&mut self, target: u32) {
let targ = format!("{}._T{}", self.fn_name, target);
self.mc_insns.push(MCInsn::Label(targ));
}

fn call_direct(&mut self, path: &xlang::ir::Path, realty: &xlang::ir::FnType) {
Expand Down Expand Up @@ -405,7 +408,8 @@ impl<F: MachineFeatures> FunctionRawCodegen for MCFunctionCodegen<F> {
}

fn branch_unconditional(&mut self, target: u32) {
todo!()
let targ = format!("{}._T{}", self.fn_name, target);
self.mc_insns.push(MCInsn::UnconditionalBranch(targ));
}

fn branch_indirect(&mut self, target: Self::Loc) {
Expand Down

0 comments on commit c7efcac

Please sign in to comment.