java字节码之栈映射帧

来源:互联网 发布:环保数据造假 编辑:程序博客网 时间:2024/06/03 19:39

栈映射帧(stack map frame),先简单介绍下这个东东,它是在jdk 1.6之后才出现在java的字节码中的,顾名思义,它表示的是方法中的字节码指令在执行时栈的状态(这里说的栈包括了局部变量区和操作栈),这么做的目的是为了加快字节码校验的速度.好了,接下来,我来具体说下,这个东西在字节码的什么情况下出现,以及asm中的使用. 

    其实正常来说,应该是每个字节码指令都有一个栈状态的,但是为了节省空间,在实际生成的字节码中,只有在进行了跳转时(比如GOTO IFLT等指令时)才保存了栈的状态,其他帧的状态都可以通过它们算出来,而且一个方法的初始帧状态是不保存的,因为初始的状态可以通过方法中的签名算出来.而且为了进一步减少空间,又对栈映射帧进行一些类型的划分,因为有可能两个栈映射帧的状态是一样,或者说有一些类似,接下来我就介绍一下,具体有哪些类型.打开org.objectweb.asm.Opcodes类的源码,我们可以看到如下定义:

    代码

Java代码 
  1. /** 
  2.  * Represents a compressed frame with complete frame data. 
  3.  */  
  4. int F_FULL = 0;  
  5.   
  6. /** 
  7.  * Represents a compressed frame where locals are the same as the locals in 
  8.  * the previous frame, except that additional 1-3 locals are defined, and 
  9.  * with an empty stack. 
  10.  */  
  11. int F_APPEND = 1;  
  12.   
  13. /** 
  14.  * Represents a compressed frame where locals are the same as the locals in 
  15.  * the previous frame, except that the last 1-3 locals are absent and with 
  16.  * an empty stack. 
  17.  */  
  18. int F_CHOP = 2;  
  19.   
  20. /** 
  21.  * Represents a compressed frame with exactly the same locals as the 
  22.  * previous frame and with an empty stack. 
  23.  */  
  24. int F_SAME = 3;  
  25.   
  26. /** 
  27.  * Represents a compressed frame with exactly the same locals as the 
  28.  * previous frame and with a single value on the stack. 
  29.  */  
  30. int F_SAME1 = 4;  
 

    它已经对每个状态做出解释了,我这里就不说了,要理解这里的话不难,只要你对java字节码指令执行以及栈结构了解就行。

大神地址:http://hllvm.group.iteye.com/group/topic/26545;

1 0
原创粉丝点击