1、背景
僅針對JVM的模闆解釋器:
如何根據opcode和尋址模式,将bytecode生成彙編碼。
本文的示例中所使用的位元組碼和彙編碼,請參見上篇博文:按值傳遞還是按引用?
2、尋址模式
本文不打算深入展開尋址模式的闡述,我們聚焦Intel的IA32-64架構的指令格式:
簡要說明下,更多的請參考intel的手冊:
– Prefixes : 用于修飾操作碼Opcode,賦予其lock、repeat等的語義.
– REX Prefix:
—- Specify GPRs and SSE registers.
—- Specify 64-bit operand size.
—- Specify extended control registers.
–Opcode:操作碼,如mov、push.
–Mod R/M:尋址相關,具體見手冊。
–SIB:和Mod R/M結合起來指定尋址。
–Displacement:配合Mod R/M和SIB指定尋址。
–Immediate:立即數。
對上面的Opcode、Mod R/W、SIB、disp、imm如果不明白,看句彙編有個概念:
%mov %eax , %rax,-(%rcx,%rbx,)
如果這句彙編也不太明白,那麼配合下面的:
– Base + (Index ∗ Scale) + Displacement – Using all the addressing components together allows efficient
indexing of a two-dimensional array when the elements of the array are 2, 4, or 8 bytes in size.
3、合法的值(64位)
關注下這4個參數的合法取值:
• Displacement — An 8-bit, 16-bit, or 32-bit value.
• Base — The value in a 64-bit general-purpose register.
• Index — The value in a 64-bit general-purpose register.
• Scale factor — A value of 2, 4, or 8 that is multiplied by the index value.
4、Mod R/M(32位尋址)
我們在後文将會用到Mod R/M位元組,是以将32位尋址的格式貼在這裡:
上表的備注,其中第1條将在我們的示例中用到,是以這裡留意下:
- The [–][–] nomenclature means a SIB follows the ModR/M byte.
-
The disp32 nomenclature denotes a 32-bit displacement that follows the ModR/M byte (or the SIB byte if one is present) and that is
added to the index.
- The disp8 nomenclature denotes an 8-bit
5、SIB(32位尋址)
同樣,因為用到了Mod R/M位元組,那麼SIB位元組也可能要用到:
6、示例
6.1、準備工作
有了上面的基礎,來看個實際的例子。
下面的代碼是生成mov彙編碼:
void Assembler::movl(Address dst, Register src) {
InstructionMark im(this);
prefix(dst, src);
emit_int8((unsigned char));
emit_operand(src, dst);
}
prefix(dst,src)
就是處理prefix和REX prefix,這裡我們不關注。
emit_int8((unsigned char) 0x89)
故名思意就是生成了一個位元組,那位元組的内容0x89代碼什麼呢?
先不急,還有一句
emit_operand(src,dst)
,這是一段很長的代碼,我們大概看下:
void Assembler::emit_operand(Register reg, Register base, Register index,
Address::ScaleFactor scale, int disp,
RelocationHolder const& rspec,
int rip_relative_correction) {
relocInfo::relocType rtype = (relocInfo::relocType) rspec.type();
// Encode the registers as needed in the fields they are used in
int regenc = encode(reg) << ;
int indexenc = index->is_valid() ? encode(index) << : ;
int baseenc = base->is_valid() ? encode(base) : ;
if (base->is_valid()) {
if (index->is_valid()) {
assert(scale != Address::no_scale, "inconsistent address");
// [base + index*scale + disp]
if (disp == && rtype == relocInfo::none &&
base != rbp LP64_ONLY(&& base != r13)) {
// [base + index*scale]
// [00 reg 100][ss index base]
/**************************
* 關鍵點:關注這裡
**************************/
assert(index != rsp, "illegal addressing mode");
emit_int8( | regenc);
emit_int8(scale << | indexenc | baseenc);
} else if (is8bit(disp) && rtype == relocInfo::none) {
// ...
} else {
// [base + index*scale + disp32]
// [10 reg 100][ss index base] disp32
assert(index != rsp, "illegal addressing mode");
emit_int8( | regenc);
emit_int8(scale << | indexenc | baseenc);
emit_data(disp, rspec, disp32_operand);
}
} else if (base == rsp LP64_ONLY(|| base == r12)) {
// ...
} else {
// ...
}
} else {
// ...
}
}
上面的代碼的關注點已經标出,這裡我們将其抽出,并将前文中的
emit_int8((unsigned char) 0x89)
結合起來:
emit_int8((unsigned char) )
emit_int8( | regenc);
emit_int8(scale << | indexenc | baseenc);
最終其生成了如下的彙編代碼(64位機器):
mov %eax,(%rcx,%rbx,)
好了,問題來了:
上面這句彙編怎麼得出的?
6.2、計算過程
我們給個下面的值:
進行簡單的運算就可以得到:
emit_int8((unsigned char) ) //得到0x89
emit_int8( | regenc); //得到0x04
emit_int8(scale << | indexenc | baseenc); //得到0x19
合起來就是三個位元組:
0x89 0x04 0x19
1、0x89對應什麼?
從上表可以看出因為JVM工作在64位下,是以需要配合REX.W來“起頭”,不過在我們這個例子中,其恰好是0。
主要看那個89/r,其後面跟的指令是什麼呢?
2、0x04代表什麼?
現在我們要用到上面的Mod R/M表和SIB表了。
用第二個位元組0x04查Mod R/M表,可知源操作數是寄存器EAX,同時可知尋址類型是[–][–]類型,含義為:
The [–][–] nomenclature means a SIB follows the ModR/M byte.
3、0x19代表什麼?
繼續查SIB表,對應位元組0x19的是:
base = ECX
scaled index = EBX
4、彙編代碼:
//32位
mov %eax,%(ecx,ebx,)
//64位
mov %rax,%(rcx,rbx,)
7、結語
本文簡要探讨了:
如何根據opcode和尋址模式,将bytecode生成彙編碼。
終。