Sie sind auf Seite 1von 17

The Java Virtual Machine (JVM)

Pat Morin
COMP 3002
2
Outline

Topic 1

Topic 2
Subtopic 2.1
Subtopic 2.2

Topic 3
3
What is the JVM?

The JVM is a specification of a computing


machine
Instruction set

Primitive data types


Memory ayout of primitive data types

The JVM ao!s portabe binary soft!are


"uthors need ony !rite code for the JVM and it !i
run on any machine that has a JVM interpreter

#ard!are impementations of the JVM aso


e$ist

picoJava
4
Bytecodes: JVM Instructions

" JVM instruction is a opcode foo!ed by a


variabe number of operands

"n opcode is one byte

22% different opcodes

JVM byte codes are sma compared to other


instruction sets

&n average' a JVM instruction is 1.( bytes ong


)IS* instructions typicay re+uire , bytes
5
Distribution of Bytecode en!ths

Source- Sun Microsystems


6
JVM Dataty"es

Integers-

byte .( bits/
short .10 bits/
int .32 bits/

ong .0, bits/

1oating point
foat .32 bits/

doube .0, bits/

&thers
char .10 bits2/
reference .not defined/

return"ddress .not defined/


7
#arts of the JVM

pc )egister

3eeps trac4 of the current instruction

Stac4
Stores stac4 frames

#eap
Stores dynamicay aocated memory .garbage
coected/

Method area

Stores program code .JVM instructions/

)untime constant poo


Stores constants and method5fied references
8
What$ no re!isters?

The JVM has ony one register' used to 4eep


trac4 of the program counter

The JVM is a stack machine

" operations operate on the top fe! eements of the


stac4

This is !hat ao!s for such short bytecodes


6g. "n integer division operation

in a 32 register )IS* machine re+uires 327318992


different opcodes .one for each pair of registers/
in a stac4 machine re+uires ony 1 opcode .it
a!ays operates on the top 2 eements/
9
The Jas%in &sse%bler

)ather than manipuate bytecodes directy'


!e generate assemby anguage and use an
assember to assembe the byte codes

The assember !e use is Jasmin


http-55:asmin.sourceforge.net5
'ello World
; public static void main(String args[]) {
; System.out.println(Hello World!);
; !
.met"od public static main([#$ava%lang%String;)&
.limit stac' ()
; pus" System.out onto t"e stac'
getstatic $ava%lang%System%out #$ava%io%*rintStream;
; pus" a string constant onto t"e stac'
ldc +Hello World!+
; ,all System.out.println
invo'evirtual $ava%io%*rintStream%println(#$ava%lang%String;)&
return
.end met"od
11
Observe

.method defines a ne! method

The parameters and return vaue are defined

.imit stac4 defines the si;e .in 32 bit !ords/


of the stac4 frame for this method

getstatic oads a static .cass/ variabe onto


the stac4

invo4evirtua cas a virtua .instance/


method

<e return !ith an e$picit return statement


#rintin! a (loat
; public static void print-loat(.loat /) {
; System.out.println( -loat.toString(/) );
; !
.met"od public static print-loat(-)&
.limit stac' 0
.limit locals 0
getstatic $ava%lang%System%out #$ava%io%*rintStream;
.load )
invo'estatic $ava%lang%-loat%toString(-)#$ava%lang%String;
invo'evirtual $ava%io%*rintStream%println(#$ava%lang%String;)&
return
.end met"od
13
ocal Variables

.ocas specifies the number of oca


variabes
This incudes function parameters

6ach oca variabe is a 32 bit +uantity'


inde$ed starting at =

>oca variabes are oaded onto and stored


from the stac4 using 7oad and 7store
instructions
14
load and store o"erations

The JVM has severa oad and store


operations
xoad ?inde$@

$ defines the type of operand .f' i' ' d' a .../


inde$ defines its inde$ in the current stac4 frame

The stac4 frame is inde$ed starting at =


Starting !ith function arguments

6ach inde$ specifies a , byte +uantity


byte' char' short' int' foat' reference
A!ideB data types are oaded in 2 steps

6.g.' doadC= = doadC1 1

store operations store the top eement into


the specified oca variabe
15
O"erations

The JVM incudes many operations for


arithmetic and ogic
"dd- iadd' add' fadd' dadd.

Subtract- isub' sub' fsub' dsub.


Mutipy- imu' mu' fmu' dmu.
Divide- idiv' div' fdiv' ddiv.

)emainder- irem' rem' frem' drem.

Eegate- ineg' neg' fneg' dneg.


*omparison- dcmpg' dcmp' fcmpg' fcmp' cmp.
and more ....

They a operate on the top 1 or 2 stac4


eements and eave their resut on the top
of the stac4
abels and Ju%" Instructions
;;; return true (1) i. arg1 23 arg0 and .alse ()) ot"er4ise
.met"od public static cmp56(--)7
.limit locals 8
.limit stac' 8
.load )
.load 1
.cmpl
i.ge true9label
ldc )
goto done
true9label:
ldc 1
done:
ireturn
.end met"od
17
)u%%ary

The JVM is a stac4Fbased virtua machine

Geing stac4Fbased ao!s for compact instruction


encoding
Geing a virtua machine ma4es it portabe
#ard!are impementations e$ist' but are not the
fastest computers around

<e have not covered


"rray instructions
Dynamic memory aocation

6$ceptions
Interfaces
Type conversions

Threads

Das könnte Ihnen auch gefallen