Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
J. Xue✬
✫
✩
✪
COMP3131/9102: Programming Languages and Compilers
Jingling Xue
School of Computer Science and Engineering
The University of New South Wales
Sydney, NSW 2052, Australia
http://www.cse.unsw.edu.au/~cs3131
http://www.cse.unsw.edu.au/~cs9102
Copyright @2018, Jingling Xue
COMP3131/9102 Page 464 May 6, 2018
J. Xue✬
✫
✩
✪
Lecture 9: JVM
1. Our code generation
2. JVM:
• Data types
• Operand stack
• Local variable array (indices)
• Instructions (⇐⇒ Jasmin instructions)
• Parameter-passing (⇐⇒ Jasmin method invocations)
COMP3131/9102 Page 465 May 6, 2018
J. Xue✬
✫
✩
✪
An Example
void whileInt() {
int i = 0;
while (i < 100) {
i++;
}
}
is compiled to
Method void whileInt()
0 iconst_0
1 istore_1 // i’s index is 1
2 goto 8
5 iinc 1 1 // i++
8 iload_1
9 bipush 100
11 if_icmplt 5
14 return
COMP3131/9102 Page 466 May 6, 2018
J. Xue✬
✫
✩
✪
The VC Compiler
Source Code (gcd.vc)
Assignment 1: Scanner
Assignment 2 & 3: Parser
Assignment 4: Semantic Analyser
Assignment 5: Code Generator
Jasmin Assembly Code (gcd.j)
Jasmin Assembler
Java Virtual Machine
Results
V
C
.
l
a
n
g
.
S
y
s
t
e
m
.
c
l
a
s
s
Tokens
AST
Decorated AST
Java Byte Code (gcd.class)
COMP3131/9102 Page 467 May 6, 2018
J. Xue✬
✫
✩
✪
Standard Environment: Built-in Functions
/*
* System.java
*/
package VC.lang;
import java.io.*;
import java.util.StringTokenizer;
public class System {
private static BufferedReader reader =
new BufferedReader(new InputStreamReader(java.lang.System.in));
public final static int getInt() {
try {
java.lang.System.out.print("Enter an integer: ");
String s = reader.readLine();
StringTokenizer st = new StringTokenizer(s);
int i = Integer.parseInt(st.nextToken());
java.lang.System.out.println("You have entered " + i + ".");
return i;
} catch (java.io.IOException e) {
java.lang.System.out.println("Caught IOException: " + e.getMessage());
java.lang.System.exit(1);
return -1;
}
}
public final static void putBool(boolean b) {
java.lang.System.out.print(b);
}
COMP3131/9102 Page 468 May 6, 2018
J. Xue✬
✫
✩
✪
public final static void putBoolLn(boolean b) {
java.lang.System.out.println(b);
}
public final static void putInt(int i) {
java.lang.System.out.print(i);
}
public final static void putIntLn(int i) {
java.lang.System.out.println(i);
}
public final static float getFloat() {
try {
java.lang.System.out.print("Enter a float: ");
String s = reader.readLine();
StringTokenizer st = new StringTokenizer(s);
float f = Float.parseFloat(st.nextToken());
java.lang.System.out.println("You have entered " + f + ".");
return f;
} catch (java.io.IOException e) {
java.lang.System.out.println("Caught IOException: " + e.getMessage());
java.lang.System.exit(1);
return -1.0F;
}
}
public final static void putFloat(float f) {
java.lang.System.out.print(f);
}
public final static void putFloatLn(float f) {
java.lang.System.out.println(f);
}
COMP3131/9102 Page 469 May 6, 2018
J. Xue✬
✫
✩
✪
public final static void putString(String s) {
java.lang.System.out.print(s);
}
public final static void putStringLn(String s) {
java.lang.System.out.println(s);
}
public final static void putLn() {
java.lang.System.out.println();
}
}
COMP3131/9102 Page 470 May 6, 2018
J. Xue✬
✫
✩
✪
References
• Jasmin home page: http://jasmin.sourceforge.net/
• Tim Lindholm and Frank Yellin, The Java Virtual
Machine Specification, 2nd Edition, Addison-Wesley,
1999. (The entire book is available on-line; see the subject Resource Page.)
• Vill Venners, Inside the Java 2 Virtual Machine, 2nd
Edition, McGraw-Hill, 1999.
(Some chapters available on-line; see the subject Resource Page.)
COMP3131/9102 Page 471 May 6, 2018
J. Xue✬
✫
✩
✪
Jasmin Assembly Language
• Sun has not defined an assembler format
• Jasmin is a Java assembler, which has been installed in the
class account and can be invoked as follows:
% 3131
% jasmin gcd.j --> the output is gcd.class
% java gcd
• Install from http://jasmin.sourceforge.net/ on
your own computer
• Read also the Jasmin User Guide there:
http://jasmin.sourceforge.net/guide.html
• Jasmin page contains pointers to other assembly languages
COMP3131/9102 Page 472 May 6, 2018
J. Xue✬
✫
✩
✪
Jasmin Assembly Language v.s Java Byte Code
• 1-to-1 correspondence
– Operation codes (opcodes) represented by mnemonics
– Name indices written in symbolic form
– Local variables are encoded by indices (integers)
• Examples:
Jasmin Instructions Java Byte Code
---------------------------------------------
iload 0x60
bipush 20 0x1614
getstatic Test.i 0xb2????
--------------------------------------------- where ????
is an index into the constant pool entry for Test.i
• Constant pool will be discussed in Lecture 11 but its
understanding unnecessary for Assignment 5
COMP3131/9102 Page 473 May 6, 2018
J. Xue✬
✫
✩
✪
gcd.java
// find the greatest common divisor of two integers
public class gcd {
static int gcd(int a, int b) {
if (b == 0)
return a;
else
return gcd(b, a - (a/b) *b);
}
public static void main(String argv[]) {
int i = 2;
int j = 4;
System.out.println(gcd(i, j));
}
}
COMP3131/9102 Page 474 May 6, 2018
J. Xue✬
✫
✩
✪
gcd.j
;; Produced by JasminVisitor (BCEL)
;; http://www.inf.fu-berlin.de/~dahm/BCEL/
.source gcd.java
.class public gcd
.super java/lang/Object
.method public ()V
.limit stack 1
.limit locals 1
.var 0 is this Lgcd; from Label0 to Label1
Label0:
.line 3
aload_0
invokespecial java/lang/Object/()V
Label1:
return
.end method
.method static gcd(II)I
.limit stack 4
.limit locals 2
.var 0 is a I from Label1 to Label2
.var 1 is b I from Label1 to Label2
Label1:
.line 5
iload_1
ifne Label0
.line 6
iload_0
COMP3131/9102 Page 475 May 6, 2018
J. Xue✬
✫
✩
✪
ireturn
Label0:
.line 8
iload_1
iload_0
iload_0
iload_1
idiv
iload_1
imul
isub
invokestatic gcd/gcd(II)I
Label2:
ireturn
.end method
.method public static main([Ljava/lang/String;)V
.limit stack 3
.limit locals 3
.var 0 is argv [Ljava/lang/String; from Label0 to Label1
.var 1 is i I from Label2 to Label1
.var 2 is j I from Label4 to Label1
Label0:
.line 12
iconst_2
istore_1
Label2:
.line 13
iconst_4
istore_2
Label4:
.line 14
getstatic java.lang.System.out Ljava/io/PrintStream;
COMP3131/9102 Page 476 May 6, 2018
J. Xue✬
✫
✩
✪
iload_1
iload_2
invokestatic gcd/gcd(II)I
invokevirtual java/io/PrintStream/println(I)V
Label1:
.line 15
return
.end method
COMP3131/9102 Page 477 May 6, 2018
J. Xue✬
✫
✩
✪
Java Class File: gcd.class (Output of od -An -tx1 gcd.class)
ca fe ba be 00 03 00 2d 00 1e 0a 00 06 00 11 0a
00 05 00 12 09 00 13 00 14 0a 00 15 00 16 07 00
0b 07 00 17 01 00 06 3c 69 6e 69 74 3e 01 00 03
28 29 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e
65 4e 75 6d 62 65 72 54 61 62 6c 65 01 00 03 67
63 64 01 00 05 28 49 49 29 49 01 00 04 6d 61 69
6e 01 00 16 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67
2f 53 74 72 69 6e 67 3b 29 56 01 00 0a 53 6f 75
72 63 65 46 69 6c 65 01 00 08 67 63 64 2e 6a 61
76 61 0c 00 07 00 08 0c 00 0b 00 0c 07 00 18 0c
00 19 00 1a 07 00 1b 0c 00 1c 00 1d 01 00 10 6a
61 76 61 2f 6c 61 6e 67 2f 4f 62 6a 65 63 74 01
00 10 6a 61 76 61 2f 6c 61 6e 67 2f 53 79 73 74
65 6d 01 00 03 6f 75 74 01 00 15 4c 6a 61 76 61
2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 3b
01 00 13 6a 61 76 61 2f 69 6f 2f 50 72 69 6e 74
53 74 72 65 61 6d 01 00 07 70 72 69 6e 74 6c 6e
01 00 04 28 49 29 56 00 21 00 05 00 06 00 00 00
00 00 03 00 01 00 07 00 08 00 01 00 09 00 00 00
1d 00 01 00 01 00 00 00 05 2a b7 00 01 b1 00 00
00 01 00 0a 00 00 00 06 00 01 00 00 00 03 00 08
00 0b 00 0c 00 01 00 09 00 00 00 32 00 04 00 02
00 00 00 12 1b 9a 00 05 1a ac 1b 1a 1a 1b 6c 1b
68 64 b8 00 02 ac 00 00 00 01 00 0a 00 00 00 0e
00 03 00 00 00 05 00 04 00 06 00 06 00 08 00 09
00 0d 00 0e 00 01 00 09 00 00 00 34 00 03 00 03
00 00 00 10 05 3c 07 3d b2 00 03 1b 1c b8 00 02
b6 00 04 b1 00 00 00 01 00 0a 00 00 00 12 00 04
00 00 00 0c 00 02 00 0d 00 04 00 0e 00 0f 00 0f
00 01 00 0f 00 00 00 02 00 10
COMP3131/9102 Page 478 May 6, 2018
J. Xue✬
✫
✩
✪
BCEL (Byte Code Engineering Library)
• Home page: http://jakarta.apache.org/bcel/
• Formerly known as JavaClass
• BCEL comes with a Jasmin disassembler, which has been installed in
the class account and can be invoked as follows:
% 3131
% jasmind gcd.class --> this produces gcd.j
• jasmind is a shell command:
#!/bin/csh
#
# jasmind - runs the Jasmin disassembler
#
# Usage:
# jasmind classname.class
#
setenv CLASSPATH ~cs3131/JavaTools/JavaClass
exec java JasminVisitor $*
• Install from the BCEL home page on your own computer.
COMP3131/9102 Page 479 May 6, 2018
J. Xue✬
✫
✩
✪
Multi-Level Machine Model
JVM
Jasmin
VC
Java C
• There is a virtual machine and a language at each level
• Each level builds on the functionality of the level below
and provides the functionality to the level above
COMP3131/9102 Page 480 May 6, 2018
J. Xue✬
✫
✩
✪
Lecture 9: JVM
1. Our code generation
√
2. JVM:
• Data types
• Operand stack
• Local variable array (indices)
• Instructions (⇐⇒ Jasmin instructions)
• Parameter-passing (⇐⇒ Jasmin method invocations)
COMP3131/9102 Page 481 May 6, 2018
J. Xue✬
✫
✩
✪
JVM Data Types
TYPE RANGE FIELD DESC
boolean {0, 1} Z
byte 8 bit signed 2’s complement (−27 to 27 − 1) B
short 16 bit signed 2’s complement (−215 to 215−1) S
int 32 bit signed 2’s complement (−231 to 231−1) I
long 64 bit signed 2’s complement (−263 to 263−1) L
char 16 bit unsigned Unicode (0 to 216 − 1) C
float 32-bit IEEE 754 single-precision F
double 64-bit IEEE 754 double-precision D
reference 32 bit unsigned reference (0 to 232 − 1) Slide 483
returnAddress 32 bit unsigned reference (0 to 232 − 1) N/A
• All (except returnAddress) mapped 1-to-1 to Java’s primitive types
• returnAddress used with jsr/jsr w/ret for handling exceptions
• boolean, byte, char and short are all implemented as int, but arrays of
these types may be stored in arrays of less than 32 bits
COMP3131/9102 Page 482 May 6, 2018
J. Xue✬
✫
✩
✪
JVM Data Types (Cont’d)
TYPE FIELD DESCRIPTOR
class reference Lclass-name;
interface reference Linterface-name;
array reference [[· · · [ component-type
void V
• class and interface names are qualified names with ”.” replaced by ”/”
• The no. of [ is equal to the no. of dimensions of the array
Type Field Descriptor
-------------------------------------
Object Ljava/lang/Object;
String Ljava/lang/String;
String[] [Ljava/lang/String;
int [] [I
float [][] [[F
-------------------------------------
• See $4.3.2, The JVM Spec for a formal definition
COMP3131/9102 Page 483 May 6, 2018
J. Xue✬
✫
✩
✪
Boolean, Byte, Short and Char Represented as Int
public class IntTypes {
public static void main(String argv[]) {
boolean z = true;
byte b = 1;
short s = 2;
char c = ’a’;
}
}
.method public static main([Ljava/lang/String;)V
...
.line 3
iconst_1
istore_1
.line 4
iconst_1
istore_2
.line 5
iconst_2
istore_3
.line 6
bipush 97
istore 4
Label0:
.line 8
return
.end method
COMP3131/9102 Page 484 May 6, 2018
J. Xue✬
✫
✩
✪
An Example for Printing Data Type Descriptors
public class Desc {
public static void main(String argv[]) {
Object o = new Object();
int [] i = new int[10];
float [][] f = new float[10][10];
String s1 = "Hello World!";
String [] s2 = { "Hello", "World!"};
System.out.println("Th class name of Object is: " + o.getClass());
System.out.println("Th class name of int[] is: " + i.getClass());
System.out.println("Th class name of float[][] is: " + f.getClass());
System.out.println("Th class name of String: " + s1.getClass());
System.out.println("Th class name of String[]: " + s2.getClass());
}
}
COMP3131/9102 Page 485 May 6, 2018
J. Xue✬
✫
✩
✪
Method Descriptors
• (ParameterType∗) ReturnType
• Examples:
Method Declaration Method Descriptor
------------ -------------------------------------
int gcd(int i, int j) (II)I
void main(String argv[]) ([Ljava/lang/String;)V
char foo(float f, String) (FLjava/lang/String;)C
------------ -------------------------------------
• See $4.3.3, The JVM Spec for a formal definition
COMP3131/9102 Page 486 May 6, 2018
J. Xue✬
✫
✩
✪
Operand Stack
• Accessed by pushing and popping values
– storing operands and receiving the operations’ results
– passing arguments and receiving method results
– This unified view is one of the main reasons why code generation
for stack-based machines is easier than registers-based machines
• A new op stack is created every time a method is called
• Integral expression:
1 + 2 * 3 + 4
• Jasmin code (without being optimised):
iconst_1
iconst_2
iconst_3
imul
iadd
iconst_4
iadd
COMP3131/9102 Page 487 May 6, 2018
J. Xue✬
✫
✩
✪
Local Variable Array
1. A new local variable array is created each time a method is called
2. Local variables addressed by indexing, starting from 0
3. Instance methods:
• slot 0 given to this
• Parameters (if any) given consecutive indices, starting from 1
• The indices allocated to the other variables in any order
4. Class methods:
• Parameters (if any) given consecutive indices, starting from 0
• The indices allocated to the other variables in any order
5. One slot can hold a value of boolean, byte, char, short, int, float,
reference and returnAdrress
6. One pair of slots can hold a value of long and double
COMP3131/9102 Page 488 May 6, 2018
J. Xue✬
✫
✩
✪
Local Variable Indices: Class Methods
1. Class method:
public static void foo() {
int i1 = 1; // index 0
int i2 = 2; // index 1
int i3 = 3; // index 2
int i = i1 + i2 * i3; // index 3
}
2. Jasmin code:
iconst_1
istore_0
iconst_2
istore_1
iconst_3
istore_2
iload_0
iload_1
iload_2
imul
iadd
istore_3
COMP3131/9102 Page 489 May 6, 2018
J. Xue✬
✫
✩
✪
Local Variable Indices: Instance Methods
1. Instance method:
public void foo() { // "this" given index 0
int i1 = 1; // index 1
int i2 = 2; // index 2
int i3 = 3; // index 3
int i = i1 + i2 * i3; // index 4
}
2. Jasmin code:
iconst_1
istore_1
iconst_2
istore_2
iconst_3
istore_3
iload_1
iload_2
iload_3
imul
iadd
istore_4
COMP3131/9102 Page 490 May 6, 2018
J. Xue✬
✫
✩
✪
Local Variable Indices: Double Word variables
1. The Long type:
public static void foo() {
int i1 = 1; // index 0
long i2 = 2; // indices 1 and 2
int i3 = 3; // index 3
long i = i1 + i2 * i3; // indices 4 and 5
}
2. Jasmin code:
iconst_1
istore_0
ldc2_w 2
lstore_1
iconst_3
istore_3
iload_0
i2l
lload_1
iload_3
i2l
lmul
ladd
lstore 4
3. Accessing index 2 or 5 is disallowed
COMP3131/9102 Page 491 May 6, 2018
J. Xue✬
✫
✩
✪
Lecture 9: JVM
1. Our code generation
√
2. JVM:
• Data types √
• Operand stack √
• Local variable array (indices) √
• Instructions (⇐⇒ Jasmin instructions)
• Parameter-passing (⇐⇒ Jasmin method invocations)
COMP3131/9102 Page 492 May 6, 2018
J. Xue✬
✫
✩
✪
Jasmin (or JVM) Instructions
1. Arithmetic Instructions
2. Load and store instructions
3. Control transfer instructions
4. Type conversion instructions
5. Operand stack management instructions
6. Object creation and manipulation
7. Method invocation instructions
8. Throwing instructions (not used)
9. Implementing finally (not used)
10. Synchronisation (not used)
COMP3131/9102 Page 493 May 6, 2018
J. Xue✬
✫
✩
✪
Arithmetic Instructions ($3.11.3, JVM Spec)
• add: iadd, fadd,
• subtract: isub, fsub
• multiply: imul, fmul
• divide: idiv, fdiv
• negative: ineg, fneg
• comparison: fcmpg, fcmpl
• ...
COMP3131/9102 Page 494 May 6, 2018
J. Xue✬
✫
✩
✪
Load and Store Instructions
• Loading a local variable into the operand stack:
iload iload_0, ..., iload_3
fload fload_0, ..., fload_3
aload aload_0, ..., aload_3 // for array and object refs
iaload // load from an int array
faload // load from a float array
• Storing a value from the operand stack into a local variable:
istore istore_0, ..., istore_3
fstore fstore_0, ..., fstore_3
astore astore_0, ..., astore_3 // for array and object refs
iastore // store into an int array
fastore // store into a float array
• Load a constant into the operand stack:
bipush, sipush, ldc, iconst_m1, iconst_0, ..., iconst_5,
fconst_0, ..., fconst_2
COMP3131/9102 Page 495 May 6, 2018
J. Xue✬
✫
✩
✪
Example: Load and Stored (Slide 487 Repeated)
1. Integral expression:
1 + 2 * 3 + 4
2. Jasmin code:
iconst_1
iconst_2
iconst_3
imul
iadd
iconst_4
iadd
COMP3131/9102 Page 496 May 6, 2018
J. Xue✬
✫
✩
✪
Example: Load and Store (Cont’d)
1. Integral expression:
1 + 100 * 200 + 40000
2. Jasmin code:
iconst_1
bipush 100
sipush 200
imul
iadd
ldc 40000
iadd
3. bipush: −27 – 27 − 1
4. sipush: −215 – 215 − 1
5. ldc val, where val is an int, float or string
COMP3131/9102 Page 497 May 6, 2018
J. Xue✬
✫
✩
✪
Example: Load and Store (Cont’d)
1. Floating-point expression:
1.0F + 2.0F * 3.0F + 4.0F
2. Jasmin code:
fconst_1
fconst_2
ldc 3.0
fmul
fadd
ldc 4.0
fadd
COMP3131/9102 Page 498 May 6, 2018
J. Xue✬
✫
✩
✪
Example: Load and Store for Arrays
1. Array operations:
a[0] = 100;
i = a[0]
2. Jasmin code – (assuming a and i have the indices 1 and 2, resp.)
// a[0] = 100;
aload_1
iconst_0
bipush 100
iastore
// i = a[0];
aload_1
iconst_0
iaload
istore_2
COMP3131/9102 Page 499 May 6, 2018
J. Xue✬
✫
✩
✪
Control Transfer Instructions
1. Unconditional: goto
2. Instructions on int:
ifeq ifne ifle iflt ifne ifge
if_icmpeq if_icmpne if_icmple
if_icmplt if_icmpge if_icmpgt
3. For floating-point operands, use first
fcmpg or fcmpl
and then
ifeq ifne ifle iflt ifne ifge
COMP3131/9102 Page 500 May 6, 2018
J. Xue✬
✫
✩
✪
if icmge label
op stack (before)
+--------------------
| ... value1 value2
+--------------------
op stack (after)
+-----
| ...
+-----
1. Pop off the two ints off and compare them
2. If value1 >= value2, jump to label. Otherwise, execution
continues at the next instruction
Other if icmpxx instructions are similar.
COMP3131/9102 Page 501 May 6, 2018
J. Xue✬
✫
✩
✪
Example 1: Control Transfer Instructions
1. Ctrl1.java
public class Ctrl1 {
public static void main(String argv[]) {
int i = 1, int j = 2, int k;
if (i < j)
k = 0;
else
k = 1;
}
}
2. Ctrl1.j (irrelevant lines removed)
.method public static main([Ljava/lang/String;)V
iconst_1
istore_1
iconst_2
istore_2
iload_1
iload_2
if_icmpge Label0
iconst_0
istore_3
goto Label1
Label0:
iconst_1
istore_3
Label1:
return
.end method
COMP3131/9102 Page 502 May 6, 2018
J. Xue✬
✫
✩
✪
Example 2: Control Transfer Instructions
1. Ctrl2.java
public class Ctrl1 {
public static void main(String argv[]) {
float i = 1, float j = 2, float k;
if (i < j)
k = 0.0F;
else
k = 1.0F;
}
}
2. Ctrl2.j (irrelevant lines removed)
.method public static main([Ljava/lang/String;)V
fconst_1
fstore_1
fconst_2
fstore_2
fload_1
fload_2
fcmpg
ifge Label0
fconst_0
fstore_3
goto Label1
Label0:
fconst_1
fstore_3
Label1:
return
.end method
COMP3131/9102 Page 503 May 6, 2018
J. Xue✬
✫
✩
✪
fcmpg and fcmpl
op stack (before):
+--------------------
| ... value1 value2
+--------------------
op stack (after):
+-------- +--------- +----------
| ... 0 | ... -1 | ... 1
+-------- +--------- +---------
value1 = value2 value1 < value2 value1 > value2
• If either is NaN, fcmpg pushes 1 and fcmpl pushes -1.
• See JVM SPEC $7.5 for an explanation why the two
instructions are provided (https://docs.oracle.com/
javase/specs/jvms/se7/html/jvms-3.html#
jvms-3.5)
COMP3131/9102 Page 504 May 6, 2018
J. Xue✬
✫
✩
✪
Type Conversion Instructions
1. Source:
int i = 1; // index 1
float f = i; // index 2
2. Jasmin code:
iconst_1
istore_1
iload_1
i2f
fstore 2
3. Only i2f is used in the VC compiler
4. i2c, i2b, f2i, etc. not used
COMP3131/9102 Page 505 May 6, 2018
J. Xue✬
✫
✩
✪
Operand Stack Management Instructions
• Instructions:
– dup: duplicate the stack top operand
– pop: remove the stack top operand
– swap: swap the top two operands
– others: pop2, dup2, etc.
• Only the first two will be used in the VC compiler:
– dup: for translating a = b = ...
– pop: for translating expression statements such as 1;
COMP3131/9102 Page 506 May 6, 2018
J. Xue✬
✫
✩
✪
Example: dup in Translating Compound Assignments
1. Assignment expressions:
int i; \\ index 1
int j; \\ index 2
int k; \\ index 3
i = j = k = 1;
2. Jasmin code:
iconst_1
dup
istore_3
dup
istore_2
istore_1
COMP3131/9102 Page 507 May 6, 2018
J. Xue✬
✫
✩
✪
Example: pop in Translating VC Expression Statements
1. Assignment expressions:
int i; // index 1
1 + (i = 2);
2. Jasmin code:
iconst_1
iconst_2
dup
istore_1
iadd
pop
COMP3131/9102 Page 508 May 6, 2018
J. Xue✬
✫
✩
✪
Object Creation and Manipulation
• Create a new class instance: new
• Access fields of classes: getstatic, putstatic, getfield, putfield
• Create a new array: newarray, anewarray, multianewarray
(only the first is used)
• Load an array component onto the operand stack: iaload, faload,
baload, caload, ...
• Store a value from the operand stack as an array component: iastore,
fastore, bastore, castore, ...
• ...
COMP3131/9102 Page 509 May 6, 2018
J. Xue✬
✫
✩
✪
Example: Object Creation and Manipulation
• Static and field variables:
public class StaticField {
static int i = 1;
int j = 1;
public static void main(String argv[]) {
StaticField o = new StaticField();
System.out.println(i);
System.out.println(o.j);
}
}
• Jasmin code (irrelevant lines removed)
.field static i I
.field j I
.method public static main([Ljava/lang/String;)V
.line 5
new StaticField
dup
invokespecial StaticField/()V
COMP3131/9102 Page 510 May 6, 2018
J. Xue✬
✫
✩
✪
astore_1
.line 6
getstatic java.lang.System.out Ljava/io/PrintStream;
getstatic StaticField.i I
invokevirtual java/io/PrintStream/println(I)V
.line 7
getstatic java.lang.System.out Ljava/io/PrintStream;
aload_1
getfield StaticField.j I
invokevirtual java/io/PrintStream/println(I)V
Label0:
.line 8
return
.end method
COMP3131/9102 Page 511 May 6, 2018
J. Xue✬
✫
✩
✪
The Syntax for Field Instructions
• Syntax:
getstatic field-spec type-descriptor
where field-spec consists of a classname followed by a
field name.
• Examples:
getstatic java/lang/System/out Ljava/io/PrintStream;
getstatic StaticField/i I
getfield and putfield not used in the VC compiler
COMP3131/9102 Page 512 May 6, 2018
J. Xue✬
✫
✩
✪
Arrays
• Java Statements // assuming a is at slot 1 (by compiler)
int a[] = new int[10];
a[1] = a[2] + 1;
• Bytecode:
bipush 10
newarray int
astore_1
aload_1
iconst_1
aload_1
iconst_2
iaload
iconst_1
iadd
iastore
COMP3131/9102 Page 513 May 6, 2018
J. Xue✬
✫
✩
✪
Method Invocation Instructions
• Method calls:
invokestatic
invokevirtual
invokespecial // also known as invokenonvirtual
-- the instance initialisation method 
-- a private method of "this"
-- a method in a super class of "this"
invokeinterface
(why invokeinterface?
stackoverflow.com/questions/1504633/what-is-the-point-of-invokeinterface)
• Method returns:
return
ireturn
freturn
...
COMP3131/9102 Page 514 May 6, 2018
J. Xue✬
✫
✩
✪
The Syntax for Method Invocation Instructions
• The syntax for invokestatic/virtual/special
invokexx method-spec
where method-spec consists of a classname, a method
name and a descriptor.
• invokeinterface not used in the VC compiler
COMP3131/9102 Page 515 May 6, 2018
J. Xue✬
✫
✩
✪
Method Invocation Instructions (Cont’d)
• invokestatic
op stack after
+----------------------- +--------------------
| ... arg1 arg2 ... argn | ... result (if any)
+----------------------- +-------------------
before
• invokevirtual and invokespecial
op stack after
+-------------------------- +--------------------
| ... objref arg1 ... argn | ... result (if any)
+-------------------------- +-------------------
before
• invokevirtual: on the dynamic type of objref
• invokespecial: based on the static class of objref
• Note that programmer-defined operators (methods) are treated
exactly the same as the built-in operators (e.g., + and −)
COMP3131/9102 Page 516 May 6, 2018
J. Xue✬
✫
✩
✪
Example: Static Method Invocation
1. Met1.java:
public class Met1 {
static int add(int i1, int i2) {
return i1 + i2;
}
public static void main(String argv[]) {
add(1, 2);
}
}
2. Met1.j (irrelevant lines removed):
.method static add(II)I .limit stack 2 .limit locals 2
iload_0
iload_1
iadd
ireturn
.end method
.method public static main([Ljava/lang/String;)V .limit stack 2 .limit locals 1
iconst_1
iconst_2
invokestatic Met1/add(II)I
pop
return
.end method
COMP3131/9102 Page 517 May 6, 2018
J. Xue✬
✫
✩
✪
Example 9: Instance Method Invocation
1. Met2.java:
public class Met2 {
int add(int i1, int i2) {
return i1 + i2;
}
public static void main(String argv[]) {
Met2 m = new Met2();
m.add(1, 2);
}
}
2. Met2.j (irrelevant lines removed):
.method add(II)I .limit stack 2 .limit locals 3
iload_1
iload_2
iadd
ireturn
.end method
.method public static main([Ljava/lang/String;)V .limit stack 3 .limit locals 2
new Met2
dup
invokespecial Met2/()V
astore_1
aload_1
iconst_1
iconst_2
invokevirtual Met2/add(II)I
return
.end method
COMP3131/9102 Page 518 May 6, 2018
J. Xue✬
✫
✩
✪
Polymorphism: invokevirtual and invokespecial
public class Fruit {
public static void main(String argv[]) {
Apple apple = new Apple();
Fruit fruit = apple;
fruit.whoAmI();
}
void whoAmI() {
System.out.println("This is a fruit.");
}
}
class Apple extends Fruit {
void whoAmI() {
System.out.println("This is an apple.");
super.whoAmI();
}
}
COMP3131/9102 Page 519 May 6, 2018
J. Xue✬
✫
✩
✪
Fruit.j
;; Produced by JasminVisitor (BCEL package)
;; http://www.inf.fu-berlin.de/~dahm/JavaClass/
;; Mon Oct 09 16:09:47 GMT+10:00 2000
.source Fruit.java
.class public Fruit
.super java/lang/Object
.method public ()V
.limit stack 1
.limit locals 1
.var 0 is this LFruit; from Label0 to Label1
Label0:
.line 1
aload_0
invokespecial java/lang/Object/()V
Label1:
return
.end method
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 3
.var 0 is arg0 [Ljava/lang/String; from Label0 to Label1
Label0:
.line 4
new Apple
dup
invokespecial Apple/()V
astore_1
.line 6
aload_1
COMP3131/9102 Page 520 May 6, 2018
J. Xue✬
✫
✩
✪
astore_2
.line 7
aload_2
invokevirtual Fruit/whoAmI()V <=== a virtual call
Label1:
.line 3
return
.end method
.method whoAmI()V
.limit stack 2
.limit locals 1
.var 0 is this LFruit; from Label0 to Label1
Label0:
.line 11
getstatic java.lang.System.out Ljava/io/PrintStream;
ldc "This is a fruit."
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
Label1:
.line 10
return
.end method
COMP3131/9102 Page 521 May 6, 2018
J. Xue✬
✫
✩
✪
Apple.j
.source Fruit.java
.class Apple
.super Fruit
.method ()V
.limit stack 1
.limit locals 1
.var 0 is this LApple; from Label0 to Label1
Label0:
.line 16
aload_0
invokespecial Fruit/()V
Label1:
return
.end method
.method whoAmI()V
.limit stack 2
.limit locals 1
.var 0 is this LApple; from Label0 to Label1
Label0:
.line 18
getstatic java.lang.System.out Ljava/io/PrintStream;
ldc "This is an apple."
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
.line 19
aload_0
invokespecial Fruit/whoAmI()V <=== a static call
Label1:
.line 17
return
.end method
COMP3131/9102 Page 522 May 6, 2018
J. Xue✬
✫
✩
✪
Jasmin Directives
• Jasmin home page
.source .class .super .limit .method
.field static .end .var .line
• Example: gcd.j (Slide 475)
COMP3131/9102 Page 523 May 6, 2018
J. Xue✬
✫
✩
✪
Jasmin File Structure (syntax.bnf)
Jasmin Syntax Jonathan Meyer, April 1996
This file contains a simplified BNF version of the Jasmin syntax.
jasmin_file ::=
’.class’ [  ]  
’.super’  
[  ]
[  ]
 ::=  [  ... ]
 ::=
’.field’    [ =  ] 
 ::=  |  | 
 ::=  [  ... ]
COMP3131/9102 Page 524 May 6, 2018
J. Xue✬
✫
✩
✪
 ::=
’.method’   
[  ]
’.end’ ’method’ 
 ::=  [  ... ]
 ::=
 
|
 
|