Skip to content

Commit 0b73a47

Browse files
committed
interpreter: Do not emit two handlers for load_imm_and_jump
Instead we emit a single handler that does both loading the immediate and jumping to the target. Signed-off-by: Aman <[email protected]>
1 parent 5850b0e commit 0b73a47

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

crates/polkavm/src/interpreter.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,25 @@ macro_rules! define_interpreter {
18291829
$body
18301830
}};
18311831

1832+
(@define $handler_name:ident $body:block $self:ident, $a0:ident: Reg, $a1:ident: u32, $a2:ident: Target) => {{
1833+
impl Args {
1834+
pub fn $handler_name(a0: impl Into<Reg>, a1: u32, a2: Target) -> Args {
1835+
Args {
1836+
a0: a0.into().to_u32(),
1837+
a1,
1838+
a2,
1839+
..Args::default()
1840+
}
1841+
}
1842+
}
1843+
1844+
let args = $self.inner.compiled_args[cast($self.inner.compiled_offset).to_usize()];
1845+
let $a0 = transmute_reg(args.a0);
1846+
let $a1 = args.a1;
1847+
let $a2 = args.a2;
1848+
$body
1849+
}};
1850+
18321851
(@define $handler_name:ident $body:block $self:ident, $a0:ident: Reg, $a1:ident: Reg, $a2:ident: Target) => {{
18331852
impl Args {
18341853
pub fn $handler_name(a0: impl Into<Reg>, a1: impl Into<Reg>, a2: Target) -> Args {
@@ -3685,6 +3704,15 @@ define_interpreter! {
36853704
visitor.jump_indirect_impl::<DEBUG>(program_counter, dynamic_address)
36863705
}
36873706

3707+
fn load_imm_and_jump<const DEBUG: bool>(visitor: &mut Visitor, ra: Reg, value: u32, target: Target) -> Option<Target> {
3708+
if DEBUG {
3709+
log::trace!("[{}]: {}", visitor.inner.compiled_offset, asm::load_imm_and_jump(ra, value, target));
3710+
}
3711+
3712+
visitor.set32::<DEBUG>(ra, value);
3713+
Some(target)
3714+
}
3715+
36883716
fn unresolved_branch_less_unsigned<const DEBUG: bool>(visitor: &mut Visitor, s1: Reg, s2: Reg, tt: ProgramCounter, tf: ProgramCounter) -> Option<Target> {
36893717
handle_unresolved_branch!("<u", visitor, s1, s2, tt, tf, branch_less_unsigned)
36903718
}
@@ -3779,6 +3807,30 @@ define_interpreter! {
37793807
}
37803808
}
37813809

3810+
fn unresolved_load_imm_and_jump<const DEBUG: bool>(visitor: &mut Visitor, program_counter: ProgramCounter, ra: Reg, value: u32, jump_to: u32) -> Option<Target> {
3811+
if DEBUG {
3812+
log::trace!("[{}]: unresolved {}", visitor.inner.compiled_offset, asm::load_imm_and_jump(ra, value, jump_to));
3813+
}
3814+
3815+
visitor.set32::<DEBUG>(ra, value);
3816+
3817+
let offset = visitor.inner.compiled_offset;
3818+
if let Some(target) = visitor.inner.resolve_jump::<DEBUG>(ProgramCounter(jump_to)) {
3819+
if DEBUG {
3820+
log::trace!(" -> resolved to jump");
3821+
}
3822+
visitor.inner.compiled_handlers[cast(offset).to_usize()] = cast_handler!(raw_handlers::load_imm_and_jump::<DEBUG>);
3823+
visitor.inner.compiled_args[cast(offset).to_usize()] = Args::load_imm_and_jump(ra, value, target);
3824+
3825+
Some(target)
3826+
} else {
3827+
if DEBUG {
3828+
log::trace!(" -> resolved to trap");
3829+
}
3830+
trap_impl::<DEBUG>(visitor, program_counter)
3831+
}
3832+
}
3833+
37823834
fn unresolved_fallthrough<const DEBUG: bool>(visitor: &mut Visitor, jump_to: ProgramCounter) -> Option<Target> {
37833835
if DEBUG {
37843836
log::trace!("[{}]: unresolved fallthrough {jump_to}", visitor.inner.compiled_offset);
@@ -4603,8 +4655,7 @@ impl<'a, const DEBUG: bool> InstructionVisitor for Compiler<'a, DEBUG> {
46034655
}
46044656

46054657
fn load_imm_and_jump(&mut self, dst: RawReg, imm: u32, target: u32) -> Self::ReturnTy {
4606-
emit!(self, load_imm(dst, imm));
4607-
emit!(self, unresolved_jump(self.program_counter, ProgramCounter(target)));
4658+
emit!(self, unresolved_load_imm_and_jump(self.program_counter, dst, imm, target));
46084659
}
46094660

46104661
fn load_imm_and_jump_indirect(&mut self, ra: RawReg, base: RawReg, value: u32, offset: u32) -> Self::ReturnTy {

0 commit comments

Comments
 (0)