The method invocatio..

Download Report

Transcript The method invocatio..

The method invocation
mechanism and the System Stack
Introduction
• We have now learned all
material necessary to
understand how the method
invocation mechanism
works.
• Here is a method
invocation:
Introduction (cont.)
• In this webpage, I will show you what happens behind the
scene in a method invocation
• Know what is going on will help you understand the
concept of the parameter passing mechanism (and why
there is no side effect)
Previously discussed
• Execution units:
• Statement = the smallest execution unit in a Java program
• A statement does very little "work"
• A complex task requires multiple statements
to complete.
• Method = contains multiple statements that is identified by
a (method) name
• A method is a suitable unit of execution for
programming activities
• A method usually performs (solves) one
complex task.
What happens when you invoke a method
• Purpose of calling (invoking) a method:
• We want the (invoked) method to perform (solve) a
(complex) task for us (Because the method was
designed to perform (solve) a (complex) task)
What happens when you invoke a method
(cont.)
• Phases of a method invocation:
1. Give the necessary information to do its job.
Example:
• If you want to find the minimum of 2
numbers, then you need to know the values of
the numbers
During this phase, the parameter variables are created
and initialized (given) with the proper value
What happens when you invoke a method
(cont.)
2. The method performs the task
• In order to perform the task, the method may
need some scratch space to perform the necessary
computations.
• In other words:
• The method may need to create some
local variables
What happens when you invoke a method
(cont.)
• As you have learned previously, these local
variables are privately owned and used exclusively
by the method. No other method can access them.
3. The method may return back a return value to be used at
the point of invocation
The mechanism to "return a value" has been discussed
previously – See:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/0
8/life-time.html#return
What happens when you invoke a method
(cont.)
Summary:
• The return value is saved in a register inside the
CPU:
What happens when you invoke a method
(cont.)
• When the execution returns back to the point of
invocation, the computer will use the value in the
return location:
What happens when you invoke a method
(cont.)
4. The method returns back to point of invocation
• Notice that a method can be invoked (called)
from different program locations:
What happens when you invoke a method
(cont.)
• Important fact on method invocation:
• In order to know where the method must
return back to, the method invocation
must give (= pass) the point (location) of
invocation to the method
What happens when you invoke a method
(cont.)
• Example:
What happens when you invoke a method
(cont.)
• Now you know the theory of method invocation
• I will now show you exactly what happens with an
example of method invocation
Preparation for an example of method
invocation
• I will use the following program:
Preparation for an example of method
invocation (cont.)
• Note:
• The program on the left are written in Java
• The Java program must be compiled (translated) into
machine (computer) instruction first and store in RAM
memory for execution
• The program (in RAM memory) on the left are the
compiled machine (computer) instruction
Preparation for an example of method
invocation (cont.)
• The point A and point B are 2 memory addresses where
the method ToolBox.min are invoked
Notice that the locations have different addresses because
they are different locations in RAM memory.
Preparation for an example of method
invocation (cont.)
• Attention:
• Pay attention to the order in which the parameter
variables and local variables are created and destroyed
• You should discover the LIFO ordering and realize that
we are using a stack
OK, let's rock and roll...
The method invocation mechanism explained by an example
• We begin with the start of the program:
• The execution begins in the main method:
The method invocation mechanism explained by an example (cont.)
• When execution reaches double r, some space is reserve on
the System Stack for the variable:
The method invocation mechanism explained by an example (cont.)
• When execution reaches r = ToolBox.min(1.0, 4.0);, it
triggers the method invocation mechanism that we will
discuss next:
The method invocation mechanism explained by an example (cont.)
• Phase 1: give (pass) the necessary information to the
method ToolBox.min
• The value 1.0 and 4.0 are passed as follows:
• Create a parameter variable for each value on
the System Stack
• Copy the value into the parameter variables
The method invocation mechanism explained by an example (cont.)
Schematically:
The method invocation mechanism explained by an example (cont.)
• There is one more thing that the method invocation
mechanism must do:
• We must make sure that the method ToolBox.min can
return back to the proper invocation point !!!
• To enable the method ToolBox.min to return back to the
proper invocation point, we must also pass (copy) the
invocation location A on the System Stack !!!
The method invocation mechanism explained by an example (cont.)
Schematically:
The method invocation mechanism explained by an example (cont.)
• Phase 1 is now complete and the execution continues with
the method ToolBox.min:
The method invocation mechanism explained by an example (cont.)
• Phase 2: the method ToolBox.min performs its task
• When execution reaches double m = 0;, some space is
reserve on the System Stack for the variable:
The method invocation mechanism explained by an example (cont.)
• When execution reaches the if-else statement, it uses the
variables on the System Stack to compute the result:
The method invocation mechanism explained by an example (cont.)
• Phase 3: the method ToolBox.min returns back a return
value
• When execution reaches return(m), the value in variable m is
copied into a return location (which is usually a register inside the
CPU):
The method invocation mechanism explained by an example (cont.)
• When execution reaches the end of the method, the local
variable(s) m is destroyed:
The method invocation mechanism explained by an example (cont.)
Note:
• Pay attention to the fact that the variables are
destroyed in the opposite order of their creation !!!
• This make is possible to use a stack to reserve
space for parameter and local variables.
The method invocation mechanism explained by an example (cont.)
• Phase 4: the method ToolBox.min returns back to the point
of invocation A
• The program will now use the value saved return location
to return to the point of invocation (A):
The method invocation mechanism explained by an example (cont.)
• The execution will then destroy the return location and
parameter variables that it had created at the beginning of the
method invocation:
The method invocation mechanism explained by an example (cont.)
• The method invocation mechanism is now completed !!
The method invocation mechanism explained by an example (cont.)
• Continued:
• The execution will continue will the assignment statement
which will use the value stored in the return location:
The method invocation mechanism explained by an example (cont.)
• Conclusion:
• The variable r is assigned the value 1.0 which is the
minimum of the 2 input values....
Terminology: stack frame, procedure
activation record
• The method invocation mechanism always constructs the
following structure on the System Stack:
This structure is called a "stack frame" or (older term) a
"procedure activation record".
Terminology: stack frame, procedure
activation record (cont.)
• Example: (see above in Phase 2)