麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > ASM > 正文

【JVM】模板解釋器--如何根據字節碼生成匯編碼?

2019-11-15 01:01:45
字體:
來源:轉載
供稿:網友
【JVM】模板解釋器--如何根據字節碼生成匯編碼?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,-0x18(%rcx,%rbx,4)

如果這句匯編也不太明白,那么配合下面的:

-- Base + (Index ? Scale) + Displacement -- Using all the addressing components together allows efficientindexing 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條將在我們的示例中用到,所以這里留意下:

  1. The [--][--] nomenclature means a SIB follows the ModR/M byte.
  2. The disp32 nomenclature denotes a 32-bit displacement that follows the ModR/M byte (or the SIB byte if one is present) and that isadded to the index.
  3. 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)0x89);  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) << 3;  int indexenc = index->is_valid() ? encode(index) << 3 : 0;  int baseenc = base->is_valid() ? encode(base) : 0;  if (base->is_valid()) {    if (index->is_valid()) {      assert(scale != Address::no_scale, "inconsistent address");      // [base + index*scale + disp]      if (disp == 0 && 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(0x04 | regenc);        emit_int8(scale << 6 | 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(0x84 | regenc);        emit_int8(scale << 6 | 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) 0x89)emit_int8(0x04 | regenc);emit_int8(scale << 6 | indexenc | baseenc);

最終其生成了如下的匯編代碼(64位機器):

mov    %eax,(%rcx,%rbx,1)

好了,問題來了:

上面這句匯編怎么得出的?

6.2、計算過程

我們給個下面的值:

regenc = 0x0,scale << 6 | indexenc | baseenc = 25

進行簡單的運算就可以得到:

emit_int8((unsigned char) 0x89) //得到0x89emit_int8(0x04 | regenc); //得到0x04emit_int8(scale << 6 | indexenc | baseenc); //得到0x19

合起來就是三個字節:

0x89 0x04 0x19

1、0x89對應什么?

這里寫圖片描述

從上表可以看出因為JVM工作在64位下,所以需要配合REX.W來“起頭”,不過在我們這個例子中,其恰好是0。

主要看那個89/r:

MOV r/m64,r64 //64位,將寄存器中的值給到寄存器或者內存地址中

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 = ECXscaled index = EBX

4、匯編代碼:

//32位mov %eax,%(ecx,ebx,1)//64位mov %rax,%(rcx,rbx,1)
7、結語

本文簡要探討了:

如何根據opcode和尋址模式,將bytecode生成匯編碼。

終。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 色偷偷一区 | 精品国产一区二区三区久久久蜜月 | 欧美一区二区片 | 午夜精品久久久久久中宇 | 久久久入口 | 国产精品www | 日本高清在线免费 | 国产一级淫片免费看 | 久久久线视频 | 日韩视频在线观看免费视频 | 国产一级大片 | 本站只有精品 | 综合在线一区 | 日本高清无遮挡 | caoporn国产一区二区 | 国产一区精品视频 | 久久久久久久国产视频 | 999久久久国产999久久久 | 一区二区三区在线观看av | 国产精品啪一品二区三区粉嫩 | 国内精品久久久久久影视8 嫩草影院在线观看网站成人 | 欧洲狠狠鲁| 亚洲一区二区三区视频免费 | 亚洲人成在线播放网站 | 永久免费不卡在线观看黄网站 | 日韩欧美电影一区二区三区 | 黄色毛片观看 | h视频在线免费看 | 色97在线| 最新av在线免费观看 | 天天色狠狠干 | 久草手机在线视频 | 99精品国产成人一区二区 | 久久综合九色综合久久久精品综合 | 国产分类视频 | 久久国产成人午夜av浪潮 | 亚洲午夜在线观看 | 亚州成人在线观看 | 一级黄色影片在线观看 | 日日草夜夜操 | 最新欧美精品一区二区三区 |