Java编译原理
代碼Test.java
public class Test {public static void main(String[] args) {int a = 0xae;int b = 0x10;int c = a + b;int d = c + 1;String s;s = "hello";}}
# javac ?Test.java
# javap -c Test
參考:http://codemacro.com/2015/03/31/intro-java-bytecode/?
*.class文件就已經(jīng)是編譯好的byte code文件,就像C/C++編譯出來(lái)的目標(biāo)文件一樣,已經(jīng)是各種二進(jìn)制指令了。這個(gè)時(shí)候可以通過(guò)JDK中帶的javap工具來(lái)反匯編,以查看對(duì)應(yīng)的byte code。
JVM中每個(gè)指令只占一個(gè)字節(jié),操作數(shù)是變長(zhǎng)的,所以其一條完整的指令(操作碼+操作數(shù))也是變長(zhǎng)的。上面每條指令前都有一個(gè)偏移,實(shí)際是按字節(jié)來(lái)偏移的。
從上面的byte code中,以x86匯編的角度來(lái)看會(huì)發(fā)現(xiàn)一些不同的東西:
局部變量竟是以索引來(lái)區(qū)分:istore_1 寫(xiě)第一個(gè)局部變量,istore_2寫(xiě)第二個(gè)局部變量,第4個(gè)局部變量則需要用操作數(shù)來(lái)指定了:istore 4
函數(shù)調(diào)用invokespecial #1竟然也是類(lèi)似的索引,這里調(diào)用的是Object基類(lèi)構(gòu)造函數(shù)
常量字符串也是類(lèi)似的索引:ldc #2
整個(gè)class文件完全可以用以下結(jié)構(gòu)來(lái)描述:
ClassFile {
? ? u4 magic; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//魔數(shù)
? ? u2 minor_version; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//次版本號(hào)
? ? u2 major_version; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//主版本號(hào)
? ? u2 constant_pool_count; ? ? ? ? ? ? ? ? ? ? ? ? ?//常量池大小
? ? cp_info constant_pool[constant_pool_count-1]; ? ?//常量池
? ? u2 access_flags; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //類(lèi)和接口層次的訪問(wèn)標(biāo)志(通過(guò)|運(yùn)算得到)
? ? u2 this_class; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //類(lèi)索引(指向常量池中的類(lèi)常量)
? ? u2 super_class; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//父類(lèi)索引(指向常量池中的類(lèi)常量)
? ? u2 interfaces_count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? //接口索引計(jì)數(shù)器
? ? u2 interfaces[interfaces_count]; ? ? ? ? ? ? ? ? //接口索引集合
? ? u2 fields_count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //字段數(shù)量計(jì)數(shù)器
? ? field_info fields[fields_count]; ? ? ? ? ? ? ? ? //字段表集合
? ? u2 methods_count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//方法數(shù)量計(jì)數(shù)器
? ? method_info methods[methods_count]; ? ? ? ? ? ? ?//方法表集合
? ? u2 attributes_count; ? ? ? ? ? ? ? ? ? ? ? ? ? ? //屬性個(gè)數(shù)
? ? attribute_info attributes[attributes_count]; ? ? //屬性表
}
這明顯已經(jīng)不是以區(qū)段來(lái)分的格式了,上面提到的函數(shù)索引、常量字符串索引,都是保存在constant_pool常量池中。常量池中存儲(chǔ)了很多信息,包括:
各種字面常量,例如字符串
類(lèi)、數(shù)據(jù)成員、接口引用
常量池的索引從1開(kāi)始。
Java Class文件詳解?【寫(xiě)的極好】
常量池的索引從1開(kāi)始。對(duì)于上面例子Test.java,可以使用
#?javap -v Test
查看其中的常量池
代碼Foo.java
<pre name="code" class="java">public class Foo{private static final int MAX_COUNT=1000;private static int count=0;public int bar() throws Exception{if(++count >= MAX_COUNT){count=0;throw new Exception("count overflow");}return count;}
# javac -g Foo.java
# javap -c -s -l -verbose Foo
總結(jié)
- 上一篇: 使用GoAccess分析Nginx日志
- 下一篇: Tsar安装使用