Finally clause and re-relationship of Return in the Try clause - http:blog.9cbs.netzcjl

xiaoxiao2021-03-06  14

From: http://blog.9cbs.net/zcjl/archive/2004/12/214123.aspx Source: Jackyzgm (This Bar Author) at http://community.9cbs.net/expert/topic/3636///expert/topic/3636/ 3636856.xml? Temp = .9524347 The discussion of this content, brainstorming. http://blog.9cbs.net/zcjl/ The author publishes its own point of view, so it is extracted to this column as saving and learning.

============================================================================================================================================================================================================= ========================================================================================================================================================================00/ ============================================================================================================================================================================================================= ==========

This problem is due to a post from the Java area foundation: http://community.9cbs.net/expert/topic/3636/3636856.xml? Temp = .9524347

Here is the explanation I have learned from "in-depth Java Virtual Machine 2E" (Chinese version, Cao Xiaozhan Translation). Because of the current learning, there is a large number of excerned places, I hope to understand.

The JSR directive is an operation code that makes Java virtual machines to microcontrol [Note 1], and another instruction makes JSR_W, the latter supports longer than the forgoes (4 bytes long). When the Java virtual machine enters the JSR or the JSR_W instruction, it presses the return address into the stack and then continues from the beginning of the miniature subroutine.

After the miniature subroutine is executed (here, the last statement in the finally clause is completed normally, does not include the abnormality, or execute return, continue, break, etc.), the RET instruction, the function of the RET instruction is Perform the operation returned from the subroutine.

You may think that the RET instruction should pop up the address from the stack because the return address has been pressed into the stack by the JSR instruction. Not this, the RET instruction will not do this. At the beginning of each subroutine, the return address is popped up from the top of the stack and stored in a local variable. Later, the RET instruction will remove the return address from this part variable. This asymmetry mode of the return address is necessary because the Finally clause will throw an exception or contain a statement such as return, break, continue. Due to the presence of these possibilities, this additional return address that is pressed into the stack by the JSR command must be removed from the stack immediately. Therefore, when the finally clause is exited by Break, Continue, Return, or throw an exception, this problem does not have to take into account. First look at the sample code in the main post (for Bytecode, remove the unrelated print statement and capture the abnormal statement) // Test1.javapublic class test1 {public static void main (string [] args) {system.out.print Tt ());

Public static int TT () {INT B = 23; try {return b = 88;} finally {if (b> 25) {system.out.println ("b> 25:" b);}}}}

// Number code sequence obtained after calling Javap -c Test1 Compiled from "test1.java" public class test1 extends java.lang.Object {public test1 (); code: 0: aload_0 1: InvokeSpecial # 1; //Method Java / Lang / Object. "" :() v 4: Return

Public static void main (java.lang.string []); code: 0: getStatic # 2; // Field Java / Lang / System.out: Ljava / IO / PrintStream; 3: InvokeStatic # 3; //Method TT: () I 6: Invokevirtual # 4; // Method Java / IO / PrintStream.print: (i) v 9: Return

Public Static Int TT (); Code: 0: BipUSH 23 // Convert data 23 to an int type, then press it into the stack 2: iStore_0 // Pop-pop up the int type value from the stack, then save it to 0 Local variable // Perform int b = 23;

3: BIPUSH 88 // converts the data 88 into the int type, then press it into the stack 5: DUP // copy one word on top of the stack, then press the copy content into the stack // This is the execution B = 88 statement

6: iStore_0 // Pop-pop up the int type value from the stack, then save it to the local variable of 0 to 0 //, the value of the execution of 88, b = 88 statement, the value of the execution B

7: iStore_1 // Popked the int type value from the stack, then save it to the local variable of the position 1 // //, the execution result of the 88, b = 88 statement, will be returned by the RETURN statement 8: JSR 19 // Press the return address into the stack, jump to the offset specified location to perform the branch operation // First, the offset address of the instruction 8 is pressed into the stack, then jump to the command 19, the finally clause start

11: iLOAD_1 // Press the INT type local variable of the position 1 to press the stack // to press the return value of the previously deposited position to 1 local variable into the stack

12: IReturN // Returns the INT type data / /////simus that the last step in the TRY clause: returnifer the return value of the TT

13: ASRE_2 14: JSR 19 17: ALOAD_2 18: Athrow // Instructions 13-18 is a case where the TRY clause is abnormal, and there is no analysis.

19: ASTORE_3 / / Pop-in from the stack, then save it to a local variable of the position 3, the start // of the finally clause is put into the instruction 8 (without abnormal conditions). The stack returns the address, which has explained in the text of the previously extracted.

20: ILOAD_0 // Transfer the data 25 to the int type, then press it into the stack 23: if_icmple 51 26: GetStatic # 2; // Field Java / Lang / System.out: Ljava / IO / PrintStream; 29: New # 5; // Class StringBuffer 32: DUP 33: InvokeSpecial # 6; // Method Java / Lang / StringBuffer. "" :() V 36: LDC # 7; // String B> 25: 38: Invokevirtual # 8; // Method Java / Lang / StringBuffer.Append: (Ljava / Lang / String;) Ljava / Lang / StringBuffer; 41: iLoad_0 42: Invokevirtual # 9; // Method Java / Lang / StringBuffer.Append: (i) Ljava / Lang / StringBuffer; 45: Invokevirtual # 10; // Method Java / Lang / StringBuffer.tostring :() Ljava / Lang / String; 48 : Invokevirtual # 11; // Method Java / IO / PrintStream.println: (Ljava / Lang / String;) V // Directive 20-48 Execute an IF clause, slightly, do not analyze

51: Ret 3 // Return from the subroutine to the address // echo command 19, from the FinalLy clause to TRY clause Exception Table: from To target Type 3 11 13 ANY 13 17 13 any} ----------------------------------- ------------------ The above Chinese comment directly, directly corresponds to the instruction description of the "deep java virtual machine" behind the instruction, the next thing is what I understand the behavior of. It can be seen that a simple Return B = 88 in the source code is compiled into Bytecode, and the 3-12 instructions correspond to the 3-12 instructions. The command 8 is a split point, the instruction 3-7 executes the statement of B = 88, and the return value of the value and method of B after the assignment operation is stored in a local variable of 0 and 1, instructions 11, 12 The return value of the method is taken out from the local variable of the position 1 and returns. The command 8 is pressing his own offset address into the stack, then jumps to the command 19 and start executing the finally clause. The FinalLy clause first pop the offset address of the command 8 and saves the local variable of the position 3 and then starts the following statement. When the following statement is executed, the offset address of the command 8 is removed from the local variable of the position 3, and then the subsequent instruction of the execution instruction 8 is returned (the final returnction instruction in the Try clause). (Strive to renew the FINALLY clause to the FINALLY clause will be replenished tomorrow;

Note: 1. The FINALLY clause in the bytecode is very similar to the "miniature subroutine" inside the method, so "Microscopic" in this article refers to the Finally clause.

转载请注明原文地址:https://www.9cbs.com/read-47960.html

New Post(0)