JVM Tutorials - Herong's Tutorial Examples - v5.14, by Herong Yang
Lilac Assembly Statements
This section provides a quick introduction on Lilac assembly statements, including macro statements.
The Lilac assembly language consists of different types statements, one for each type of entities in the JVM bytecode class structure. Here are some examples:
Constant Statements - Constant statements specify constants in the constant pool as shown below:
Syntax: const <type> <name> <value>; Examples: const classref Hello Hello_UTF8; const utf8 Hello_UTF8 "Hello"; const classref java.lang.Object Object_UTF8; const utf8 Object_UTF8 "java/lang/Object"; const utf8 mainName_UTF8 "main"; const utf8 mainDesc_UTF8 "([Ljava/lang/String;)V";
Declaration Statements - Declaration statements declare classes, fields or methods as shown below:
Syntax:
<modifier> class | field | method {
name ...;
descriptor ...;
...
}
Examples:
public class {
version 52.0;
name Hello;
extends java.lang.Object;
...
}
public static field {
name piName_UTF8; // "pi"
descriptor piDesc_UTF8; // "D"
constant value piValue_double; // 3.1415927
}
public static method {
name mainName_UTF8;
descriptor mainDesc_UTF8;
var object args;
...
}
Variable Statements - Variable statements declares symbolic names for local variables as shown below:
Syntax:
// declare a variable at the next slot
var <type> <name>;
// declare a variable at the given slot
var <type> <name> at <index>;
// declare a variable with a relative offset to another variable
var <type> <name> at <name> +|- <offset>;
Examples:
var object this; // declare "this" for variable 0
var int count; // declare "count" for variable 1
var double pi; // declare "pi" for variable 2 and 3
var double r at 4; // declare "r" for variable 4 and 5
var float f at 1; // declare "f" for variable 1, shared with "i"
Instruction Statements - Instruction statements execute JVM instructions as shown below:
Syntax:
[<label>:] <mnemonic> [<parameter> ...];
Examples:
aload this; // load address from variable "this"
getstatic java.lang.System.out; // load field by constant name
ldc Hello_String; // load constant by constant name
return;
tableswitch
11->Label_l1,
12->Label_l2,
...
default->Label_Others;
try Label_Try_Start -> Label_Try_End
catch RuntimeException
go to Label_Catch;
Macro Statements - Macro statements bundle multiple regular statements into one statement as shown below:
// Macro string statement flagged by the string literal "..."
const string Hello_String "Hello world!";
// Is a bundle of 2 regular statements
const string Hello_String Hello_UTF8;
const utf8 Hello_UTF8 "Hello world!";
// Macro class reference statement flagged by the class path "/"
// defining the class name "java.lang.Object" as the constant name
const classref java/lang/Object as java.lang.Object
// Is a bundle of 2 regular statements
const classref java.lang.Object Object_UTF8;
const utf8 Object_UTF8 "java/lang/Object";
// Macro array reference statement flagged by the array sign "[]"
// defining the class name "Array_Int" as the constant name
const classref int[] as Array_Int
// Is a bundle of 2 regular statements
const classref Array_Int Array_Int_UTF8;
const utf8 Array_Int_UTF8 "[I";
// Macro field reference statement flagged by the keyword "from"
const fieldref java/lang/String model from com/example/Car as Car.model;
// Is a bundle of 6 regular statements
const fieldref Car.model com.example.Car, modelNaT;
const classref com.example.Car Car_UTF8;
const utf8 Car_UTF8 "com/example/Car";
const nameandtype modelNaT modelName_UTF8, modelDesc_UTF8;
const utf8 modelName_UTF8 "model";
const utf8 modelDesc_UTF8 "Ljava/lang/String;";
// Macro method reference statement flagged by the keyword "from"
const methodref void println(java/lang/String)
from java/io/PrintStream as PrintStream.println;
// Is a bundle of 6 regular statements
const methodref PrintStream.println PrintStream, printlnNaT;
const classref PrintStream PrintStream_UTF8;
const utf8 PrintStream_UTF8 "java/io/PrintStream";
const nameandtype printlnNaT printlnName_UTF8, printlnDesc_UTF8;
const utf8 printlnName_UTF8 "println";
const utf8 printlnDesc_UTF8 "(Ljava/lang/String;)V";
// Macro field statement flagged by an access modifier
public static double pi {
constant value piValue_double; // 3.1415927
}
// Is a bundle of 7 regular statements
const utf8 piName_UTF8 "pi";
const utf8 piDesc_UTF8 "D";
const double piValue_double 3.1415927;
...
public static field {
name piName_UTF8; // "pi"
descriptor piDesc_UTF8; // "D"
constant value piValue_double; // 3.1415927
}
// Macro method statement flagged by an access modifier and ()
public static main(java/lang/String[] args) {
...
}
// Is a bundle of 4 regular statements
const utf8 mainName_UTF8 "main";
const utf8 mainDesc_UTF8 "([Ljava/lang/String;)V";
...
public static method {
name mainName_UTF8;
descriptor mainDesc_UTF8;
var object args;
...
}
// Macro .getfield() instruction statement
.getfield(this, Car.model); // aload this; getfield Car.model;
.getfield(System.out); // getstatic System.out;
// Macro .invokevirtual() instruction statement
.invokevirtual(Car.run, this, duration);
// is a bundle of 3 regular instructions
aload this;
iload duration;
invokevirtual Car.run;
// Macro .new() instruction statement
.new(Integer, Integer.init, 5);
// is a bundle of 4 regular instructions
new Integer;
dup;
iconst 5;
invokespecial Integer.init;
// Macro .new() array instruction statement
.new(intArray, 10);
// is a bundle of 2 regular instructions
iconst 10;
newarray intArray;
// Macro .concat() instruction statement
// returning return address of concatenated string
.concat("Name = ", name, ", size = ", 5);
// Nested macro instructions
.println(.getfield(System.out), "Name = ", name, ", size = ", 5);
Table of Contents
JVM (Java Virtual Machine) Specification
Java HotSpot VM - JVM by Oracle/Sun
java.lang.Runtime Class - The JVM Instance
java.lang.System Class - The Operating System
ClassLoader Class - Class Loaders
Class Class - Class Reflections
JVM Stack, Frame and Stack Overflow
Thread Testing Program and Result
CPU Impact of Multi-Thread Applications
I/O Impact of Multi-Thread Applications
Micro Benchmark Runner and JVM Options
Micro Benchmark Tests on "int" Operations
Micro Benchmark Tests on "long" Operations
Micro Benchmark Tests in JIT Compilation Mode
Micro Benchmark Tests on "float" and "double" Operations
JRockit JVM 28.2.7 by Oracle Corporation
JVM Bytecode and Instruction Set
►JVM Assembler/Disassembler Tools
Compare HelloWorld.j with "javap" Output
JASM Example - Calculator.jasm
AsmTools/JASM Assembly Statements
AsmTools/JASM Example - Calculator.jasm
AsmTools/JASM Disassembler: "jdis"
AsmTools/JCOD Assembly File Format
AsmTools/JCOD Example - HelloWorld.jcod
AsmTools/JCOD Disassembler: "jdec"