JVM Tutorials - Herong's Tutorial Examples - v5.14, by Herong Yang
Lilac Macro Example - HelloWorldMacro.jasm
This section provides a tutorial example on how to write a simple program with Lilac macro statements: HelloWorldMacro.jasm.
Lilac macro statements are really helpful, if you want to write a JVM assembly program. Here is an example, CalculatorMacro.jasm, converted from Jasmin version of my calculator program:
// CalculatorMacro.jasm
// Copyright (c) HerongYang.com. All Rights Reserved.
//
public class {
version 45.0;
// classref constants: class name become constant names
const classref /CalculatorMacro; // "/" flags classref macro
const classref /java/lang/Object; // same as java/lang/Object
const classref java/lang/Double;
const classref java/lang/String;
const classref java/lang/System;
const classref java/io/PrintStream;
const classref java/lang/NumberFormatException;
// string constants
const string errDivByZero "Error: Division by zero is not allowed.";
const string errBadVal "Error: Please provide valid numeric values.";
const string errBadOp "Error: Invalid operator. Use +, -, *, or /.";
const string errBadArgs "Error: Invalid number of arguments.";
const string msgUsage "Usage: java CalculatorMacro <num> <op> <num>";
// fieldref/methodref constants
const fieldref PrintStream out from System as System.out;
const methodref double parseDouble(String s) from Double
as Double.parseDouble;
const methodref char charAt(int i) from String as String.charAt;
const methodref void println(String s) from PrintStream
as PrintStream.println_String;
const methodref void println(double d) from PrintStream
as PrintStream.println_double;
// utf8 constants
const utf8 source_file_name "CalculatorMacro.jasm";
const utf8 Code_utf8 "Code";
// class properties
name CalculatorMacro;
extends Object;
source file source_file_name;
// no fields
// 1 methods
public static void main (String[] args) {
maxstack 4;
// maxlocals 8; variable slots calculated by Lilac
// args = local variable #0 - declared by macro method statement
var double x; // local variable #1 and #2, 2-slot
var int op; // local variable #3
var double y; // local variable #4 and #5, 2-slot
var double r; // local variable #6 and #7, 2-slot
// count arguments
aload args;
arraylength;
bipush 3;
if_icmpeq Label_Parse; // skip the error message
getstatic System.out;
ldc errBadArgs;
invokevirtual PrintStream.println_String;
getstatic System.out;
ldc msgUsage;
invokevirtual PrintStream.println_String;
return;
// parse arguments
// label on try statement is not supported
Label_Parse: nop;
try Label_Try_Start -> Label_Try_End
catch NumberFormatException
go to Label_Catch;
Label_Try_Start: // start of try block
aload args;
iconst_0;
aaload;
invokestatic Double.parseDouble;
dstore x;
aload args;
iconst_1;
aaload;
iconst_0;
invokevirtual String.charAt;
istore op;
aload args;
iconst_2;
aaload;
invokestatic Double.parseDouble;
dstore y;
Label_Try_End: nop; // end of try block
// must label the last statement of the block
goto Label_Switch; // skip the catch block
Label_Catch: pop;
getstatic System.out;
ldc errBadVal;
invokevirtual PrintStream.println_String;
return;
// switch on operator
Label_Switch: iload op;
lookupswitch
42 -> Label_Mul,
43 -> Label_Add,
45 -> Label_Sub,
47 -> Label_Div,
default -> Label_BadOp;
Label_Add:
dload x;
dload y;
dadd;
dstore r;
goto Label_Result;
Label_Sub:
dload x;
dload y;
dsub;
dstore r;
goto Label_Result;
Label_Mul:
dload x;
dload y;
dmul;
dstore r;
goto Label_Result;
Label_Div:
dload y;
dconst_0;
dcmpl;
ifeq Label_DivByZero;
dload x;
dload y;
ddiv;
dstore r;
goto Label_Result;
// print result
Label_Result:
getstatic System.out;
dload r;
invokevirtual PrintStream.println_double;
return;
Label_BadOp:
getstatic System.out;
ldc errBadOp;
invokevirtual PrintStream.println_String;
return;
Label_DivByZero:
getstatic System.out;
ldc errDivByZero;
invokevirtual PrintStream.println_String;
return;
}
}
Compile it by the Lilac jasm.sh script:
herong> ./jasm/jasm.sh -input src -output cls \ -classpath ./java.base/classes warning: java runtime jar not found Created 1 class files in 0 secs
Test it on JVM. It works!
herong> java -cp cls CalculatorMacro 0.9999 + 0.0001 1.0 herong> java -cp cls CalculatorMacro 0.9999 - 0.0001 0.9998 herong> java -cp cls CalculatorMacro 0.9999 '*' 0.0001 9.999000000000001E-5 herong> java -cp cls CalculatorMacro 0.9999 / 2 0.49995 herong> java -cp cls CalculatorMacro 0.9999 2 Error: Invalid number of arguments. Usage: java Calculator <number> <operator> <number> herong> java -cp cls CalculatorMacro 0.9999 / two Error: Please provide valid numeric values. herong> java -cp cls CalculatorMacro 0.9999 % 2 Error: Invalid operator. Use +, -, *, or /. herong> java -cp cls CalculatorMacro 0.9999 / 0 Error: Division by zero is not allowed.
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"