[英]Java bytecode instruction of if-statements
我有以下Java類:
public class ArtClassInt {
public boolean foo(int x) {
if(x == 3956681)
return true;
else
return false;
}
public boolean boo(int x) {
if(x <= 952140568)
return true;
else
return false;
}
public boolean boo1(int x, int y) {
if(x <= y)
return true;
else
return false;
}
public boolean zoo(int x) {
if(x+1 < 1267)
return true;
else
return false;
}
}
當我編譯它並獲取其字節碼時,得到了與源代碼中的if語句相對應的以下語句:
ArtClassInt.boo1(II)Z: I4 Branch 3 IF_ICMPGT L17 - true
ArtClassInt.boo1(II)Z: I4 Branch 3 IF_ICMPGT L17 - false
ArtClassInt.boo(I)Z: I4 Branch 2 IF_ICMPGT L10 - true
ArtClassInt.boo(I)Z: I4 Branch 2 IF_ICMPGT L10 - false
ArtClassInt.foo(I)Z: I4 Branch 1 IF_ICMPNE L3 - true
ArtClassInt.foo(I)Z: I4 Branch 1 IF_ICMPNE L3 - false
ArtClassInt.zoo(I)Z: I6 Branch 4 IF_ICMPGE L24 - true
ArtClassInt.zoo(I)Z: I6 Branch 4 IF_ICMPGE L24 - false
我對助記符(即IF_ICMPGE
, IF_ICMPNE
等)完全感到困惑。 通過查看源代碼,我期望:
foo
方法中的if語句應為if_icmpeq
而不是IF_ICMPNE
boo
和boo1
方法中的if語句應為if_icmple
而不是IF_ICMPGT
。 zoo
方法中的一個應該是if_icmplt
而不是IF_ICMPGE
。 有人可以解釋其背后的原因嗎?
測試的條件被反轉,因為如果條件為真,它會跳轉(跳轉到else
塊),否則將繼續執行下一條指令。
我猜您不應該假定您的語句應逐字轉換為字節碼。
唯一相關的是語義應該相同。 在中間代碼中將條件轉換為對應條件的情況很常見,請考慮一下while循環:
while (x < y) {
code;
}
可以編譯為
START:
CMP x y
JMPGE END
CODE
JMP START
END:
...
您看到的是顛倒條件,這是一個常見的成語。 這是因為反轉的條件使您可以跳出一個塊,因此僅當相反情況為真時才執行它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.