next up previous contents
Next: write and writeln Up: Statements Previous: Assignments

Procedure and Function Calls

For a procedure or function call you first have to load the parameters on the operand stack, in the order they are declared. For call-by-value parameters this means you have to load the value of the actual parameter on the stack, which is an expression. This means that the code to load this value on the stack is as described in section 10.2 about expressions.

The actual parameter of a call-by-reference parameter is the reference to the variable. For a local variable the instruction that does this is the aload instruction, which takes the index of the local variable in the local variable table of the method as its operand. For a global variable the getstatic instruction loads the reference to the variable on the operand stack. It takes the index of the CONSTANT_Fieldref_info in the constant_pool[], that describes the variable, as its operands.

Every actual parameter loaded on the operand stack has to be counted, so when all the parameters are on the stack you know how many words were pushed on the stack. These words will be popped from the stack by the instruction that invokes the method, so the variable of the current method in the compiler that contains the current stack size has to be updated with this value. If the method is a function, the variable has to be updated with the number of words of the parameters it pops from the operand stack, and the word containing the returnvalue of the function that is on the stack when the function returns. This means subtracting the value counter - 1 from the variable in case of a function and value counter when it is a procedure. Function returns will be described in section 10.7.

The JVM instruction that invokes a method is invokestatic. It takes as operands the index of the CONSTANT_Methodref_info entry in the constant_pool[] that describes the method. The invokestatic instruction is only used to invoke methods that are declared to be static, which are all the methods in our case.



next up previous contents
Next: write and writeln Up: Statements Previous: Assignments



mark@bottom.xs4all.nl