3

I'm busy writing a compiler by using ASM 4.1. I need to compile a intermediate language into java bytecode. I find that ASM 4.1 API says," In fact these nodes must(*) be inserted just before any instruction node i that follows an unconditionnal branch instruction such as GOTO or THROW, that is the target of a jump instruction, or that starts an exception handler block. " However, I can't find the usage of the Frame in the ASM guide("ASM 4.0 A Java bytecode engineering library"). Who knows the usage of the Frame? What's the meaning of every parameter of visitFrame/FrameNode? Why do I get a "visitFrame(Opcodes.F_APPEND,...)" at some time and get a "visitFrame(Opcodes.F_SAME,...)“ at other time by using ASMifier? I can't understand!! For example, the code like below:

    int a = 2;
    int b = 3;
    if(a == 3){
        System.out.println("hello");
    }else{
        System.out.println(a);

        if(b == 23){
            System.out.println("world");
        }else{
            System.out.println(b);
        }
    }

by using ASMifier, I can get the result(Sorry, I can't attach a picture for not enghou reputation), and there is a instruction like this:"visitFrame(Opcodes.F_APPEND,2, new Object[]{Opcodes.INTEGER, Opcodes.INTEGER}, 0, null)" . Could you tell me the meaning of the parameters?

Thanks a lot.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
evasnowind
  • 76
  • 1
  • 4

2 Answers2

4

You can find detailed answer at the link below.

Is there a better explanation of stack map frames

Java 1.7 introduced this option to speed up class verification. Frames have 2 parts: Variables types and Stack types. First frame is determined by the method type description, initial ?STORE calls and method return type.

After each GOTO / JUMP call you need to provide updated description of Stack Map Frames. In order to save space, you can use options like SAME, APPEND, etc. or you can describe all variables again by specifying FULL array of variable types.

For example in CATCH section your Retrun Stack Type is changed from method return type to Exception class type for caught exception.

If you jump betveen different loop blocks with goto commants, the execution block of each loop might use different frame description for variables used withing the loop.

Community
  • 1
  • 1
mindiga
  • 509
  • 3
  • 18
0

The frame is like local variables of a given types for a limited scope. Local variables are numbered, but if you decode the debug information you can see the names they were given.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thank you for your answer, but I know what the frame is and how the frame works in jvm. What I want to know is how to use visitFrame/FrameNode in ASM, since I can't understand the meanings of the parameters of visitFrame/FrameNode in ASM. – evasnowind Jul 19 '13 at 00:27
  • @evasnowind Why do you want to use visitFrame? Perhaps the confusion come about because you don't have a use for it. As stated it is for adding or changing the type of local variables. – Peter Lawrey Jul 19 '13 at 09:51
  • Yes, you asked a right question. I have taken some experiments and I found that it seems I don't need to use Frame. Before I asked this question in stackoverflow, I took some experiments to know how to use ASM. In those experiments I "found" visitFrame/FrameNode "was" required for jump instructions. I think I took wrong experiments. – evasnowind Jul 23 '13 at 14:11