欢迎来到天天文库
浏览记录
ID:4147870
大小:461.59 KB
页数:30页
时间:2017-11-29
《清华大学软件学院编译原理课件》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、THSS341001622009/4301Chap.2JAVAC王朝坤chaokun@tsinghua.edu.cnOutlineTHSS341001622009/4301•ImplementationofJava•Bytecode•TheJavaClassfileformat•JVM•RelatedTools(demo)22.1Java的解释THSS341001622009/4301•Java语言的一种实现编译,没有绝对地址调入包含:继承所用到的所有类,内存分配确定等Java源程序Bytecode载入器(.java文件)限定对象存取权限Bytecode校验器预防系统堆栈溢出
2、Java编译器保持参数类型一致Bytecode解释器JavaBytecode程序执行系统(.class文件)系统平台3Java的实现THSS341001622009/4301•Java虚拟机不同的操作系统有不同的虚拟机.它类似一个小巧而高效的CPU.JavaJust-in-timebyte-code代码是与平台无关的interpretercompiler是虚拟机的机器指令.Java字节代码运行的几种方式:RuntimeSysteminterpreter(解释方式)Ahead-of-time(预先编译)Just-in-time(即时编译):由代码OperatingSystem生
3、成器将字节代码转换成本机的机器代码,然后可以以较高速度执行.HardwareDynamicAdaptiveCompiler(动态自适应编译)4JavaVirtualMachineFrameTHSS341001622009/4301•JVMisastack-basedmachine•EachthreadhasastackthatstoresframesJVMFrameTHSS341001622009/4301•Aframeiscreatedforeachmethodinvoked•Eachframehas:1.An“operandstack”tousetoexecuteinst
4、ructionsinthemethod,itssizeshouldbespecified2.Arrayoflocalvariablesthatactasregistersandtheirnumbershouldbefixedasthestartofthemethod3.Referencetotheconstantpoolofthecurrentclass4.Argumentsaresuppliedinthelocalvariablesarrayandtheirnumberisgiven(thisvariableisusuallyinposition0)2.2BytecodeT
5、HSS341001622009/4301classTest{publicintf(intx){inty=x*2;returny+x;}}UsingjavapTHSS341001622009/4301•javapisaprogramthatcangiveyouinformationon.classfiles.•Ifyougetaclassfilewithoutdocumentation,javapcantellyouwhatmethodsyoucancall.•javapcanshowyouhowjavaccompilesyourprogramintosimpleinstruc
6、tions.•Goodforhigh-performanceoptimizationsorifyoudon’thaveaccesstoanApplicationProgrammingInterface(API).BytecodeTHSS341001622009/4301classTest{publicintf(int);publicintf(intx){Code:inty=x*2;0:iload_1returny+x;1:iconst_2}2:imul}3:istore_24:iload_25:iload_16:iadd7:ireturnjavap–cTestJVMBytec
7、odeTHSS341001622009/4301•JVMloadsaclassfile:1streamofbytecodesforeachmethod•Aninstructionis1-byteopcodefollowedby0ormoreoperands•Eachtypeofopcodehasamnemonic•Thebytecodeinstructionsetwasdesignedtobecompact•256bytecodeinstructions(0~255)•Thetotalnumberofo
此文档下载收益归作者所有