410 likes | 571 Views
Procedure calls. The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are no more accessible when the procedure has returned. The problem: Where should these variables be kept in memory?.
E N D
Procedure calls The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are no more accessible when the procedure has returned. The problem: Where should these variables be kept in memory? <Computer Architecture WS 2005-2006, 18 January 2006> (1)
Solutions Solution 1: Give each variable its own memory address: but what if the procedures calls itself? Solution 2: Use a data structure called executionstack <Computer Architecture WS 2005-2006, 18 January 2006> (2)
Execution stack (1) The executionstack is stored in an area of memory It is reserved for storing variables It is possible to push values on the top of the stack or pop values from the top A special register (SP = Stack Pointer) always contains the address of the top of the stack <Computer Architecture WS 2005-2006, 18 January 2006> (3)
Execution stacks (2) How a stack solves the problem of procedure call ? A special register (LV) stores the base absolute address (position) in the stack from where local variables of the current procedure are stored. Local variables are referred by mean of a relative distance (offset) from the absolute address pointed by LV. The data structure between LV and SP is called local variable frame of the current procedure. <Computer Architecture WS 2005-2006, 18 January 2006> (4)
Example (1) A procedure 'A', which has local variables a1,a2 and a3 is executing: a1 is at addressLV, a2 is at LV+1, a3 is atLV+2 Memory Execution stack <Computer Architecture WS 2005-2006, 18 January 2006> (5)
Example (2) 'A' calls procedure 'B', which has local variables b1, b2, b3, b4: 'B' local variables are pushed on the stack. LV is updated to point to the address where 'B' local variables start. <Computer Architecture WS 2005-2006, 18 January 2006> (6)
Example (3) 'B' calls procedure 'C', which has local variables c1 and c2: <Computer Architecture WS 2005-2006, 18 January 2006> (7)
Example (4) 'A' calls procedure 'D', which has local variables d1, d2, d3, d4 and d5, which are stored at the same location of the “not still available” 'B' and 'C' local variables: <Computer Architecture WS 2005-2006, 18 January 2006> (8)
Stack based machine IJVM is a stack based machine The stack is also used tostore operandsduring the computation of arithmetic expressions as well as the result; such use is called operand stack Beside access to the main memory, the IJVM only exposes the stack structure to the programmer (no registers are available and in any way directly accessible with the IJVM instruction set) <Computer Architecture WS 2005-2006, 18 January 2006> (9)
Operand stack (1) Let a1, a2 and a3 be local variables representing integer numbers How to compute a1 = a2 + a3 on a stack based architecture? Note: We suppose to have an instruction that sums the two values on the top of the stack, and pushes the result back on the stack <Computer Architecture WS 2005-2006, 18 January 2006> (10)
Operand stack (2) First values of a2 and a3 are pushed on the stack: <Computer Architecture WS 2005-2006, 18 January 2006> (11)
Operand stack (3) Then we execute the instruction for the sum: it will pop the two top-most values on the stack, compute their sum and push the result back on the stack. <Computer Architecture WS 2005-2006, 18 January 2006> (12)
Operand stack (4) Finally we save back the value at the top of the stack in a1: <Computer Architecture WS 2005-2006, 18 January 2006> (13)
Memory organization CPP, LV and SP points to 4-byte words, PC points to a byte <Computer Architecture WS 2005-2006, 18 January 2006> (14)
IJVM ISA (1) 20 Instructions Integer Arithmetic Instruction are composed of an operation code (opcode) and an optional operand (a memory offset or a constant) <Computer Architecture WS 2005-2006, 18 January 2006> (15)
IJVM ISA (2) • Types of instruction: • Push a word on stack (LDC_W, ILOAD, BIPUSH) • Pop a word from the stack, assign its value to a local variable (POP, ISTORE) • Arithmetic (integer) and logic operations (IADD,ISUB,IAND,IOR) • Conditional Branching, Jumps (IFEQ, IFLT, IF_ICMPEQ, GOTO) • Swapping of values on top of the stack (SWAP) • Duplicate value on top of the stack (DUP) • Format conversion (prefix instruction WIDE) • Method/Procedure call (INVOKEVIRTUAL) • Return from a method (IRETURN) • NOP • Adding a constant to a local variable (INCC) <Computer Architecture WS 2005-2006, 18 January 2006> (16)
IJVM ISA (3) <Computer Architecture WS 2005-2006, 18 January 2006> (17)
Example Program Java Java assembly IJVM program Instructions are replaced by Hex opcodes Instructions are replaced by Hex opcodes Local variables are stored on the stack and referenced by mean of an offset starting from the LV pointer Labels in the assembly code are replaced by effective offsets in the IJVM code (0x0F = 15) F Operand Stack evolution during execution <Computer Architecture WS 2005-2006, 18 January 2006> (18)
INVOKEVIRTUAL INVOKEVIRTUAL is used to call another method. This instruction allocates the space for the reference of the object (OBJREF) to be called, for the parameters and local variables (Parameter 1, 2, and 3), for the return address (INVOKEVIRTUAL's following instruction) and a pointer to the Caller's LV (pointer to the previous local variable frame) Finally it changes the value of the PC register so that it points to the code of the called method. <Computer Architecture WS 2005-2006, 18 January 2006> (19)
INVOKEVIRTUAL: Example (1) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main • The program calls the cmp(p1,p2) method, which accepts two parameters, compares them and returns: • -1 if p1 < p2 • 0 if p1 = p2 • 1 if p1 > p2 • The method is called with p1 = -1 and p2 = -10 : we expect that it returns 1. SP ... LV Stack before INVOKEVIRTUAL, current procedure is main <Computer Architecture WS 2005-2006, 18 January 2006> (20)
INVOKEVIRTUAL: Example (2) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main objref is a reference to the method stored in the constant pool: it's use is related to the way the Java Virtual Machine operates, but it is not really necessary in our IJVM SP objref ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (21)
INVOKEVIRTUAL: Example (3) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main The first parameter is pushed on the stack: p1 = -1 SP -1 objref ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (22)
INVOKEVIRTUAL: Example (4) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main The second parameter is pushed on the stack: p2 = -10 SP -10 -1 objref ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (23)
INVOKEVIRTUAL: Example (5) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main The method cmp(-1, -10) is invoked SP -10 -1 objref ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (24)
INVOKEVIRTUAL: Example (6) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method Before executing the method, some operation must be done: allocate space for and assign values to local variables (temp), save the address of the instruction following the method call (return address) and the LV register (base pointer of the local variable frame of the caller), objref is overwritten with a pointer to address of the return address in the stack. LV is then updated to the current local variable frame start. SP LV return address temp caller's LV (in this case main's LV) -10 -1 LV link address of the instruction that follows INVOKEVIRTUAL ... <Computer Architecture WS 2005-2006, 18 January 2006> (25)
INVOKEVIRTUAL: Example (7) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method The value of the first parameter is loaded on the stack: p1 = -1 SP -1 LV return address temp -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (26)
INVOKEVIRTUAL: Example (8) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method The value of the second parameter is loaded on the stack: p2 = -10 SP -10 -1 LV return address temp -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (27)
INVOKEVIRTUAL: Example (9) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method Compute the difference (ISUB) between the two values on top of the stack. Push the result back on the stack. SP 9 LV return address temp -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (28)
INVOKEVIRTUAL: Example (10) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method Save the value on top of the stack in the temp variable. Note: This pulls the top-most value off the stack, so after we need to re-push it. 9 SP LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (29)
INVOKEVIRTUAL: Example (11) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method Load the value of temp on the stack SP 9 LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (30)
INVOKEVIRTUAL: Example (12) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method If the value at the top of the stack is negative (less than 0) jump to 'lt', else continue. The value on top is popped. In this case 9 > 0: we continue... 9 SP LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (31)
INVOKEVIRTUAL: Example (13) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method temp is loaded back on the stack SP 9 LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (32)
INVOKEVIRTUAL: Example (14) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method If the top of the stack is equal to zero jump to 'eq', else continue As we have 9 we continue... 9 SP LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (33)
INVOKEVIRTUAL: Example (14) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method The constant value 1 is pushed on the stack (this will be the return value of the cmp method) SP 1 LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (34)
INVOKEVIRTUAL: Example (15) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method We jump to 'done'. On top of the stack the return value 1 remains SP 1 LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (35)
IRETURN IRETURN is used to exit a method and return to the caller This instruction deallocates the stack space reserved for the INVOKEVIRTUAL call, restores the values of the LV and PC registers and assures that the return value is on top of the stack. <Computer Architecture WS 2005-2006, 18 January 2006> (36)
IRETURN: Example (16) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method We return to the caller... SP 1 LV return address temp: 9 -10 -1 LV link ... <Computer Architecture WS 2005-2006, 18 January 2006> (37)
IRETURN: Example (17) .method cmp(p1,p2) .var temp .end-var ILOAD p1 ILOAD p2 ISUB ISTORE temp ILOAD temp IFLT lt ILOAD temp IFEQ eq gt: BIPUSH 1 GOTO done lt: BIPUSH -1 GOTO done eq: BIPUSH 0 done: IRETURN .end-method Values of the LV and SP registers are restored, the top of the stack contains the return value of the method, PC is restored to the saved value (return address) so that execution continues from the instruction following the INVOKEVIRTUAL call. 1 LV return address temp: 9 -10 -1 SP 1 ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (38)
IRETURN: Example (18) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main We push the constant 1 on the top of the stack SP 1 1 ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (39)
IRETURN: Example (19) .main ... LDC_W objref BIPUSH -1 BIPUSH -10 INVOKEVIRTUAL cmp BIPUSH 1 IF_ICMPEQ EQ ... EQ: HALT .end-main We compare the two top-most values on the stack. If they are equal (that's the case in this example) jump to 'EQ'. 1 1 SP ... LV <Computer Architecture WS 2005-2006, 18 January 2006> (40)