4.1. JAVA
public class calc
{
public static int mult_add(int a, int b, int c)
{
return a*b+c;
}
}
public static int mult_add(int, int, int);
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=3
0: iload_0
1: iload_1
2: imul
3: iload_2
4: iadd
5: ireturn
The first step is multiplication. The product is left at theTOS:
+---------+
TOS ->| product |
+---------+
iload_2loads the third argument (c) in the stack:
+---------+
TOS ->| c |
+---------+
| product |
+---------+
Now theiaddinstruction can add the two values.
4.1.4 JVMmemory model
x86 and other low-level environments use the stack for argument passing and as a local variables storage.
JVMis slightly different.
It has:
- Local variable array (LVA^7 ). Used as storage for incoming function arguments and local variables.
Instructions likeiload_0load values from it.
istorestores values in it. At the beginning the function arguments are stored: starting at 0 or at 1
(if the zeroth argument is occupied bythispointer).
Then the local variables are allocated.
Each slot has size of 32-bit.
Hence, values oflonganddoubledata types occupy two slots.
- Operandstack(orjust“stack”). It’susedforcomputationsandpassingargumentswhilecallingother
functions.
Unlikelow-levelenvironmentslikex86, it’snotpossibletoaccessthestackwithoutusinginstructions
which explicitly pushes or pops values to/from it.
- Heap. It is used as storage for objects and arrays.
These 3 areas are isolated from each other.
(^7) (Java) Local Variable Array