Download Oolong, the assembly language of JVM a

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Oolong, the assembly language
a
of JVM
Teodor Rus
[email protected]
The University of Iowa, Department of Computer Science
a
These slides have been developed by Teodor Rus using material published by Joshua Engel, Addison-Wesley 1999. They are copyrighted materials and may not be
used in other course settings outside of the University of Iowa in their current form or modified form without the express written permission of the copyright holder. During this
course, students are prohibited from selling notes to or being paid for taking notes by any person or commercial firm without the express written permission of the copyright
holder.
Introduction to System Software – p.1/39
Motivation
• JVM operates on a binary file called class file format, CFF;
• Because it is difficult to edit and visualize CFF, the Oolong
assembly language was created;
• The assembler OA : OolongP rograms → CF F was
implemented;
• OA is written in Java and thus it runs on any JVM implementation;
• Oolong assembly programs can be edited with any text editor;
by convention Oolong source file names end in .j.
Introduction to System Software – p.2/39
Oolong programs
Are composed of one or more classes:
; Name of the program (text following ; is a comment)
.class attributes ClassName (. prefixes directives)
.super SuperClassName (usually java/lang/Object)
.interface Name ; class whose methods are abstracts
.field attributes fieldName fieldType;
; more fields can be defined
.method attributes methodName (imputArgs;) Output
Bytecode
.end method
; more methods can be provided
.end class
Introduction to System Software – p.3/39
Example: hello, world
Let us look at the file hello.j:
.class public hello
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 1
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Hello, worl"
invokevirtual java/io/PrintStream/println (Ljava/lang/String;)V
return
.end method
.end class
Introduction to System Software – p.4/39
Oolong directives (pseudo-operations)
• Lines that begin with a period (.) are directives
• The first directive is .class tells OA the name of the class being
compiled (hello) in our case; by default the class name created by
OA is the name of the source file minus .j extension
• .class directive may contain a set of modifiers that control the
access to the class; hello is public
• The lines between the directives .class and .end class define
the elements of the class
• .end class is optional, if it is not there then class extend from
.class to end of file; if more classes are defined in the same file
they need to be in the bracket .class . . . .end class
Introduction to System Software – p.5/39
Superclass
• The directive .super defines the superclass of the class defined
by .class
• In the example the superclass of the class hello is
java/lang/Object
• If there is no directive .super then the superclass is
java/lang/Object
• .super works likes the extends in Java; hence .class and
.super in this example are equivalent to Java line:
public class hello extends java.lang.Object
Introduction to System Software – p.6/39
Note
In Oolong one must use the full names unlike in
Java where this could be
public class hello extends Object
Introduction to System Software – p.7/39
main Method
• The directive .method marks the beginning of a new method
• Lines between .method and .end method belong to the method
• The .method directive provides a list of access keywords (in any
order) followed by the name and the method descriptor which
expresses the method type
• The main method is marked public and static, i.e. it can be
accessed by anybody and it is not attached to any particular
object of the class
• The descriptor of main is ([Ljava/lang/String;)V
which says that main takes an array of pointers to strings ([L) and
return void (V)
Introduction to System Software – p.8/39
Limits
•
.limit directive tells OA how much space to
allocate for the execution of the method.
• Note:
1. .limit stack puts an upper limit on how many slots on the
operand stack the program will use
2. .limit locals specifies the number of local variable slots
that will be used
•
OA will guess if these directives are not given
Introduction to System Software – p.9/39
Oolong Instructions
• Lines between .method and .end method are Oolong
instructions; OA translates them into bytecodes
• Instruction structure is:
[Label:]
Mnemonic [Arguments] [;Comment]
• Mnemonic is mandatory and represents the name of an opcode
• Label is optional and if present ends with : (colon)
• Comment start with ; (semicolon) and ends with the new line
(a comment line start with ; and continue to end of line)
• A program is a sequence of Oolong directives and instructions
Introduction to System Software – p.10/39
Oolong instructions
getstatic java/lang/System/out Ljava/io/PrintStream;
tells JVM to push the reference to the field out, of type
java/io/PrintStream
ldc "Hello, world"
tells JVM to create a java/lang/String constant with the value
Hello, world and to place it on the stack
invokevirtual
java/io/PrintStream/println(Ljava/lang/String;)V
tells JVM to invoke method println on the stack contents
return
terminates the method and causes control to return
Introduction to System Software – p.11/39
Defining a class
•
The beginning of an Oolong file gives info
about the class it contains using .class
•
The .class directives has the structure
.class keywords name
where keywords is a blanc separated
sequence of keywords and name is a string of
the form id1/id2/. . ./idk/name
•
The string id1/id2/. . ./idk is the package
name and name is the class name.
Introduction to System Software – p.12/39
Note
Though Java import statement allows one to refer to a class by its name alone, JVM requires the
complete name
Introduction to System Software – p.13/39
The keywords
Table 1 contains .class keywords
Keyword
Meaning
public
class may be referenced from anywhere;
when absent only from the package
final
class may not have subclasses (no class may use it as superclass)
super
invokespecial in methods of this class has a special meaning
interface
this class is an interface; it may not be instantiated
abstract
there are abstract methods in this class
strictfp
all methods use strictly conforming floating point arithmetic
Table 1:
.class directive keywords
Introduction to System Software – p.14/39
Superclasses
•
The directive .super indicate the superclass
of the class containing it; if no .super is
given then java/lang/Object is the
superclass.
•
An instance of a class inherits all fields and
methods of superclass; if a field is static it is
not copied into the subclass.
•
The abstract methods of superclass can be
rewritten in the class;
Introduction to System Software – p.15/39
Declaring fields
•
To add a field to a class the directive .field
is used which has the structure
.field name type
where type is called the field descriptor
•
The type determines what kind of values may
be taken by the name
Introduction to System Software – p.16/39
Examples
.field age I
; Name: age, type: int (descriptor is I)
.field weight F ; Name: weight, type: float (descriptor F)
.field name Ljava/lang/String;
;Name name, type: string (descriptor Ljava/lang/String)
.field friends [LPerson ; Name: friends, type: Person array
Introduction to System Software – p.17/39
Descriptors
•
A descriptor is a written name for a type
•
Most descriptors are single letters such as I
for integer, F for float, Z for boolean, J for long
•
void is a method type not a field type
•
If the value of a field is a reference to an
object of type classname then the descriptor
is Lclassname; where ; marks the end of
the descriptor not the begin of a comment
Introduction to System Software – p.18/39
Example
.field out Ljava/io/PrintStream;
is a reference to a PrintStream
Introduction to System Software – p.19/39
Type descriptors
Type
Descriptor
array of type
[type
byte
B
boolean
Z
char
C
double
D
float
F
int
I
long
J
reference to classname
Lclassname;
short
S
void
V
Introduction to System Software – p.20/39
Observations
• In an array descriptor the number of dimensions is given by the
number of left brackets ([) used
Example: .field matrix [[F;
is an array of arrays of floats
• One may use two different fields with the same name but different
types
Example: .field grade C and .field grade F
• The general form of the .field directive is
.field mod1 mod2 . . . name type [= value]
where mod1, mode2, . . . are keywords in Table 2 defining the
access rights to the field
Introduction to System Software – p.21/39
Field directive keywords
Keyword
Access to the field is granted to
public
Anyone
private
Only the class containing it
protected
All subclasses of this class and anywhere in the package
static
This field is available in class not in the objects
final
Field value may not be changed once it is assigned
volatile
Field value should not be cached; it may change unexpectedly
transient
Field used for temporary purposes; value should not be saved
Table 2:
.field directive keywords
Introduction to System Software – p.22/39
Examples
.class public language/Greeting
.field protected introduction Ljava/lang/String;
.end class
.class DeadLanguage/OldEnglish
.super language/Greeting
.end class
.class language/Translator
.super java/lang/Object
.end class
.class language/linguistic/Greeting
.super java/lang/Object
.end class
Introduction to System Software – p.23/39
Note
The protected field
language/Greetings/introduction
may not be accessed from
language/linguistic/Greeting
since it is in a different package
Introduction to System Software – p.24/39
Note
A static field can be optionally initialized using
= value
Introduction to System Software – p.25/39
Examples
; A public String field initialized to "Hello"
.field public static introduction Ljava/lang/String = "Hello"
; An int field with default access initialized to 999
.field static Cheesburgers I = 999
; A protected PrintStream field initialized to null
.field protected static out Ljava/io/PrintStream;
; An ignored initialization (field not static)
.field public ColumbusDiscoverAmerica = 1492
Introduction to System Software – p.26/39
Method declaration
By directive .method
.methods public
.limit locals 3
.limit stack 2
fload_1
fload_2
fadd
freturn
.end method
ComputeSum (FF)F
;
;
;
;
Push the first argument
Push the second argument
Add arguments
Return the result
Introduction to System Software – p.27/39
Note
Method descriptor is written (type1 . . .)typereturn
where typei is the type of parameter i.
Examples method descriptors:
void main(String argv[])
---> (L/java/lang/String;)V
int read(byte[] data,int offset,int len) ---> ([BII)I
String ToString()
---> ()Ljava/lang/String;
void println()
---> ()V
Introduction to System Software – p.28/39
Method keywords
Keyword
Meaning
public
Method available to all classes
private
Method available only within this class
protected
available to any subclass or class in the same package
static
this is a class method
final
this method may not be overridden in subclasses
synchronized
JVM gets a lock on object before invoking method
if static then lock is on class
native
method implemented in native code
abstract
method has no implementation
strictfp
method uses strict floating-point semantic
Table 3:
.method directive keywords
Introduction to System Software – p.29/39
Limit directive
•
.limit directive tells OA how much space it
should allocate
.limit stack nr ; allocate nr slots for the stack
.limit locals nr ; allocate nr slots for the locals
•
nr in .limit must satisfy 1 ≤ nr ≤ 65, 536
•
A slot is large enough to hold an int, float, or
reference; it takes two slots to hold a long or a
double
•
If .limit is not given OA takes its best guess
Introduction to System Software – p.30/39
Observations
•
Methods are overloaded (same name and
different descriptors )
; A method to print a float value
.method println(F)V
; A method to print a String value
.method println(Ljava/lang/String;)V
; A method to print a long value
.method println (J)V
Introduction to System Software – p.31/39
Note
•
OA requires the entire descriptor
specification, including return type
•
In Java two methods that differ only by return
type is an error; OA can recognize them as
different methods
Introduction to System Software – p.32/39
Interfaces
•
Methods a class supports without
implementations
•
.interface directive is used to create an
interface
•
The structure of the .interface directive is
.interface Keywords InterfaceName
where Keywords are the same as for .class
directive
Introduction to System Software – p.33/39
Example
.interface Amiable
.method public abstract smile()V
; No implementation, hence no code here
.end method
.method public abstract ShakeHands(LAmiable;)V
; No implementation, hence no code here
.end method
Note, all methods are marked as abstract
Introduction to System Software – p.34/39
Interface usage
1. If a class implements an interface then it must provide
implementations for all methods of the interface
2. The implementation of an interface is specified by the directive
.implements which has the structure
.implements InterfaceName
3. If a method declared in the interface is not implemented in the
class then the class needs to be declared abstract
Introduction to System Software – p.35/39
Example interface usage
.class FriendlyPerson
.super Person
.implements Amiable
; Implementation of smile method
.method public smile ()V
; Code of smile method is here
.end method
.method public ShakeHands (LAmiable;)V
; Code of ShakeHands method is here
.end method
Introduction to System Software – p.36/39
Note
While there is only one .super directive in a
class, there may be more than one .implements
directives
Introduction to System Software – p.37/39
More on interfaces
•
Interfaces may implement other interfaces, as
in:
.interface Clever
.method public abstract SolvePuzzles()V
.end method
.end class
.interface JavaProgrammer
.implements Amiable
.implements Clever
.method public abstract WriteJavaPrograms ()V
.end method
.end class
Introduction to System Software – p.38/39
Observations
•
Directive .implements in an interface specifies
the inheritance of other interfaces rather than
implementation of methods
• .interface
is equivalent to
.class interface abstract
Introduction to System Software – p.39/39