Java Virtual Machine (JVM) Alan Dingwall dingwall@gmx.net Übersicht Was ist die Java Virtual Machine DasClass File Format Aufbau der JVM Kompilation.java.class Fazit Quellennachweise
Was ist die JVM Abstrakter Prozessor Führt Class Files aus Grund für die Plattformunabhängigkeit von Java JVM kennt die Sprache Java nicht Alternative Programierspachen für die JVM Das Class File Format Kompilierte Java-Klassen und Interfaces werden in Class Files gespeichert (*.class). Daten sind big-endian
Struktur einer Class File Name Size Name magic 4 interfacescount minorversion interfaces majorversion fieldscount constantpoolcount fields constantpool N/A methodscount accessflags methods thisclass attributescount superclass attributes magic ist immer 0xCAFEBABE Size N/A N/A N/A N/A Der Constant Pool Enthält alle Konstanten einer Klasse Eintragstypen: Tag Name Utf8 Integer... Class String Fieldref Methodref InterfaceMethodref NameAndType Value 0x01 0x03... 0x07 0x08 0x09 0x0A 0x0B 0x0C Followed by u length, u1 data[length] integer... u name u string u class, u nameandtype u class, u nameandtype u class, u nameandtype u name, u descriptor
Die Attributes Tabelle Enthält zusätzliche Informationen z.b. Instruktionen,Source File, Zeilentabellen Struktur der Einträge: attributeinfo Name nameindex length info Size 4 lenght Die Fields Tabelle Enthält Deklaration der Felder Struktur der Einträge: Name fieldinfo accessflags nameindex descriptorindex attributescount attributesinfo Size N/A
Die Methods Tabelle Enthält Deklarationen der Methoden Struktur der Einträge: Name methodinfo accessflags nameindex descriptorindex attributescount attributesinfo Size N/A Beispiel HelloWorld(1) HelloWorld.java public class HelloWorld { public static void main(string args[]) { System.out.println("Hello World!"); } }
Beispiel HelloWorld() Hex Dump CA FE BA BE 00 03 00 D 00 1D 0A 00 06 00 0F 09...-... 00 10 00 11 08 00 1 0A 00 13 00 14 07 00 15 07... 00 16 01 00 06 3C 69 6E 69 74 3E 01 00 03 8 9...<init>...() 56 01 00 04 43 6F 64 65 01 00 0F 4C 69 6E 65 4E V...Code...LineN 75 6D 6 65 7 54 61 6 6C 65 01 00 04 6D 61 69 umbertable...mai 6E 01 00 16 8 5B 4C 6A 61 76 61 F 6C 61 6E 67 n...([ljava/lang F 53 74 7 69 6E 67 3B 9 56 01 00 0A 53 6F 75 /String;)V...Sou 7 63 65 46 69 6C 65 01 00 0F 48 65 6C 6C 6F 57 rcefile...hellow 6F 7 6C 64 E 6A 61 76 61 0C 00 07 00 08 07 00 orld.java... 17 0C 00 18 00 19 01 00 0C 48 65 6C 6C 6F 0 57...Hello W 6F 7 6C 64 1 07 00 1A 0C 00 1B 00 1C 01 00 0A orld!... 48 65 6C 6C 6F 57 6F 7 6C 64 01 00 10 6A 61 76 HelloWorld...jav Aufbau der JVM Exception Manager Security Manager Class Loader Excecution Engine Memory Manager Native Interface Thread Scheduler
Excecution Engine Virtueller Prozessor Stack-Orientiert Instruktionssatz mit 01 versch. Opcodes Datentypen der Execution Engine Prefix b c d f i l s a Type byte char double float int long Short Reference ReturnAddress Interpretation signed byte unicode character double-precision floating point value single-precision floating point value integer long integer signed short references an object Used by jsr,ret,...
Datenbereiche Java Stack Frame Frame Frame Frame Register Programmzähler Aktuelles Frame Oberster Operand Lokale Variablen Garbage Collected Heap Object 1 Aktueller Frame Methodenbereich Aktuelle Klasse Aktuelle Methode Operanden Stack Lokale Variablen Constant pool Befehlssatz 1 Byte langer Opcode Je nach Opcode gefolgt von Operanden Typisiert Nicht orthogonal
Instruktionsklassen Konstanten auf den Stack schieben Lokale Variablen laden und speichern Stackmanipulationen Arithmetische und Logische Operationen Flusskontrolle Arrayoperationen Castingoperationen Manipulieren von Feldern Methodenaufruf und Rücksprung Andere Instruktionen (1) iadd Addiert zwei Integer auf dem Stack int int1 Operanden Stack int1 + int Operanden Stack Andere arithmetische Befehle: ladd, fadd, dadd, isub, lsub, fsub, dsub, imul, lmul, fmul, dmul, idiv, ldiv, fdiv, ddiv, irem, lrem, frem, drem, ineg, lneg, fneg, dneg
Instruktionen () dup Oberstes Stackelement duplizieren objref Operanden Stack objref objref Operanden Stack Andere Stack-Befehle: dup, pop, pop, swap... Instruktionen (3) bipush n ldc #n iconst_<n> byte-konstante als Integer auf den Stack schieben 3bit Constant Pool Konstante auf den Stack schieben Int-Konstante auf den Stack schieben <n>=m1,0,1,,3,4,5 Andere Stack-Befehle: sipush, ldc_w, ldc_w, lconst_0, lconst_1, fconst_0, fconst_1, fconst_, dconst_0, dconst_1
Instruktionen (4) iload n iload_<n> istore n istore_<n> lokale Variable auf den Stack pushen lokale Variable auf den Stack pushen <n>=0..3 lokale Variable vom Stack holen lokale Variable vom Stack holen <n>=0..3 Andere Befehle: lload, lload_<n>, fload, fload_<n>, dload, dload_<n>, aload, aload_<n>, lstore, lstore_<n>, fstore, fstore_<n>, dstore, dstore_<n>, astore, astore_<n>, Instruktionen (5) iinc v n lokale Variable v um n erhöhen iload v n wide iload vhigh vlow nhigh nlow
invokevirtual #n return Instruktionen (6) Methode aufrufen Rücksprung aus einer Methode invokestatic, invokespecial, invokeinterface, ireturn, lreturn, freturn, dreturn, areturn old frame new frame arg arg1 objref objref arg1 arg stack local vars stack local vars Wie funktioniert die Execution Engine... while(!finished) switch(bytecode[pc]) {... case IADD: int i=stack.popint(); int j=stack.popint(); stack.push(i+j); pc++; break;... }...
Ablauf beim Start der JVM java HelloWorld VM versucht HelloWorld.main auszuführen ClassLoader (lädt HelloWorld.class) Linking Verifikation (Testet die Gültigkeit der Klasse) Preparation (Speicher für statische Var. allozieren,...) (Resolution) (Symbolische Referenzen überprüfen) Initialisation (Statische Init-Funktionen ausführen) Ausführen von HelloWorld.main Wenn kein Thead mehr läuft wird die VM beendet Verifikation Durchgang 1 File befolgt das Class File Format Durchgang Klasse besitzt Superklasse (nicht final) Indexe im Constant Pool verweisen auf den korrekten Typ Namen und Typen von Methoden und Feldern sind erlaubt Durchgang 3 (Bytecode Verifier) Überprüft Methodencode
Bytecode Verifier Sprungziele innerhalb der Methode Deklarierte Anzahl lokaler Variablen wird eingehalten Constant Pool Indexe verweisen auf korrekten Typ Data-flow Analyser Stackgrösse wird eingehalten Lokale Variablen und Stack beinhalten die von der Instruktion benötigten Typen Für jeden Weg zu einer Instruktion sind die Anzahl und der Typ der Elemente des Stacks identisch Java Bytecode (1) public int addmethod(int a,int b) { return a+b; } Method int addmethod(int,int) 0 iload_1 1 iload_ iadd 3 ireturn
Java Bytecode () public void objectmethod() { Compiler a=new Compiler(); int i=a.addmethod(10,0); } Constant Pool: # <Class Compiler> #3 <Method Compiler()> #4 <Method int addmethod(int, int)> Method void objectmethod() 0 new # 3 dup 4 invokespecial #3 7 astore_1 8 aload_1 9 bipush 10 11 bipush 0 13 invokevirtual #4 16 istore_ 17 return Java Bytecode (3) public void formethod() { int s=1; } for(int i=0;i<10;i++) s*=i; Method void formethod() 0 iconst_1 1 istore_1 iconst_0 3 istore_ 4 goto 14 7 iload_1 8 iload_ 9 imul 10 istore_1 11 iinc 1 14 iload_ 15 bipush 10 17 if_icmplt 7 0 return
Java Bytecode (4) public void arraymethod() { int a[]=new int[10]; } a[4]=a[3]; Method void arraymethod() 0 bipush 10 newarray int 4 astore_1 5 aload_1 6 iconst_4 7 aload_1 8 iconst_3 9 iaload 10 iastore 11 return Fazit Grosses Thema Zeigt mögliche Code-Optimierungen Information im Internet ist oft fehlerbehaftet
Quellenangaben Java Virtual Machine Specification http://java.sun.com/docs/books/vmspec/index.html Languages for the Java Virtual Machine http://grunge.cs.tu-berlin.de/~tolk/vmlanguages.html JAVA Virtual Machine (Meyer, Downing) Developing Professional Java Applets (Hopson, Ingram) http://www.kaposnet.hu/books/profiapplet/appa.htm