Nikolay Igotti

pageicon Thursday Oct 23, 2008

Long absolute jumps on AMD64

Sometimes it may be required to perform calls and jumps to absolute address on 64-bit AMD. Unfortunately, x86_64 instruction set only allows 32-bit displacements, so traditional approach is to move desired address into register and call or jump using it. Unfortunately, it requires scratch register, or push/pop of register. In case of jump, it also problematic if we wish not touch registers. Here I suggest alternative approach, using ret instructions for long jumps. While not too complicated, this trick can help some compiler/JIT writers to handle very long jumps.
DECLINLINE(void) tcg_out_pushq(TCGContext *s, tcg_target_long val)
{
     tcg_out8(s, 0x68); /* push imm32, subs 8 from rsp */
     tcg_out32(s, val); /* imm32 */
     if ((val >> 32) != 0)
     {
         tcg_out8(s, 0xc7); /* mov imm32, 4(%rsp) */ 
         tcg_out8(s, 0x44); 
         tcg_out8(s, 0x24);
         tcg_out8(s, 0x04);
         tcg_out32(s, ((uint64_t)val) >> 32); /* imm32 */
     }
}
DECLINLINE(void) tcg_out_long_jmp(TCGContext *s, tcg_target_long dst)
{
    tcg_out_pushq(s, dst);
    tcg_out8(s, 0xc3); /* ret */
}

« October 2008 »
SunMonTueWedThuFriSat
   
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24
25
26
27
28
29
30
31
 
       
Today

Feeds

Search this blog

Links

Weblog menu

Today's referrers

Today's Page Hits: 41

Stats