UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
Java security (in a nutshell)
Memory Management Case study (JVM & CLR) KIRAN KUMAR V KIRAN KUMAR V LENIN THUMMALAPALLI LENIN THUMMALAPALLI.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
JAVA Processors and JIT Scheduling. Overview & Literature n Formulation of the problem n JAVA introduction n Description of Caffeine * Literature: “Java.
Lab#1 (14/3/1431h) Introduction To java programming cs425
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Lab 9 Java Bytecode & The Jasmin Assembler
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
JVM-1 Java Virtual Machine Reading Assignment: Chapter 1: All Chapter 3: Sections.
Java Virtual Machine (JVM). Lecture Objectives Learn about the Java Virtual Machine (JVM) Understand the functionalities of the class loader subsystem.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Unit 061 Java Virtual Machine (JVM) What is Java Virtual Machine? The Class Loader Subsystem Linking oVerification oPreparation oResolution Class Initialization.
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
OOP Languages: Java vs C++
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
Java Programming Robert Chatley William Lee
1 The Java Virtual Machine Yearly Programming Project.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Implement High-level Program Language on JVM CSCE 531 ZHONGHAO LIU ZHONGHAO LIU XIAO LIN.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
10/12/2015© Hal Perkins & UW CSEV-1 CSE P 501 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Winter 2008.
1 Comp 104: Operating Systems Concepts Java Development and Run-Time Store Organisation.
1 Introduction to JVM Based on material produced by Bill Venners.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
POS 406 Java Technology And Beginning Java Code
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Java 2 security model Valentina Casola. Components of Java the development environment –development lifecycle –Java language features –class files and.
12/10/2002© 2002 Hal Perkins & UW CSET-1 CSE 582 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Autumn 2002.
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
1 Lecture 12 Java Virtual Machine:.class file Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Programming Languages and Paradigms Activation Records in Java.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
RealTimeSystems Lab Jong-Koo, Lim
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
Internet and Java Foundations, Programming and Practice
Java security (in a nutshell)
Topic: JAVA .Class File Structure
Java Virtual Machine Complete subject details are available at:
An Introduction to Java – Part I
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Java Programming Language
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
Java Implementation – JVMs, JITs &c Hal Perkins Summer 2004
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
Presentation transcript:

UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009

Compiling for Different Platforms 2 Program written in some high-level language (C, Fortran, ML,...) Compiled to intermediate form Optimized Code generated for various platforms (machine architecture + operating system) Consumers download code for their platform

Problem: Too Many Platforms! 3 Operating systems – DOS, Win95, 98, NT, ME, 2K, XP, Vista,... – Unix, Linux, FreeBSD, Aix,... – VM/CMS, OS/2, Solaris, Mac OS X,... Architectures – Pentium, PowerPC, Alpha, SPARC, MIPS,...

Compiling for Different Platforms 4 High-Level Program Intermediate-Level Program Compiler Code Gen 1Code Gen 2Code Gen 3 Machine Code 1Machine Code 2Machine Code 3 producer consumers

Dream: Platform Independence 5 Compiler produces one low-level program for all platforms Executed on a virtual machine (VM) A different VM implementation needed for each platform, but installed once and for all

Platform Independence with Java 6 Java Program Bytecode Program Compiler JVM 1JVM 2JVM 3 producer consumers Java Virtual Machines

Platform Independence with Java 7 Java Program Bytecode Program Compiler JIT 1JIT 2JIT 3 Machine Code 1Machine Code 2Machine Code 3 producer consumers Just-in-Time Compilers

Java Bytecode 8  Low-level compiled form of Java  Platform-independent  Compact  Suitable for mobile code, applets  Easy to interpret  Java virtual machine (JVM) in your browser  Simple stack-based semantics  Support for objects

Class Files 9 class Foo {... } class Bar {... } class Baz {... }... Compiler InputFile.java Foo.class... Bar.class... Baz.class

What’s in a Class File? 10  Magic number, version info  Constant pool  Super class  Access flags (public, private,...)  Interfaces  Fields  Name and type  Access flags (public, private, static,...)  Methods  Name and signature (argument and return types)  Access flags (public, private, static,...)  Bytecode  Exception tables  Other stuff (source file, line number table,...)

Class File Format 11 magic number4 bytes 0xCAFEBABE major version2 bytes 0x0021 minor version2 bytes 0x0000 magic number identifies the file as a Java class file version numbers inform the JVM whether it is able to execute the code in the file

Constant Pool 12 CP length2 bytes CP entry 1(variable) CP entry 2(variable)... constant pool consists of up to = 2 16 entries entries can be of various types, thus of variable length

Constant Pool Entries 13 Utf8 (unicode)literal string (2 bytes length, characters) IntegerJava int (4 bytes) FloatJava float (4 bytes) LongJava long (8 bytes) DoubleJava double (8 bytes) Classclass name StringString constant -- index of a Utf8 entry Fieldreffield reference -- name and type, class Methodrefmethod reference -- name and type, class InterfaceMethodrefinterface method reference NameAndTypeName and Type of a field or method

Constant Pool Entries 14 Many constant pool entries refer to other constant pool entries Fieldref index to a Class index to a Utf8 (name of class containing it) index to a NameAndType index to a Utf8 (name of field) index to a Utf8 (type descriptor) Simple text (Utf8) names used to identify classes, fields, methods -- simplifies linking

Example 15 class Foo { public static void main(String[] args) { System.out.println("Hello world"); } A) 33 Q) How many entries in the constant pool?

16 1)CONSTANT_Methodref[10](class_index = 6, name_and_type_index = 20) 2)CONSTANT_Fieldref[9](class_index = 21, name_and_type_index = 22) 3)CONSTANT_String[8](string_index = 23) 4)CONSTANT_Methodref[10](class_index = 24, name_and_type_index = 25) 5)CONSTANT_Class[7](name_index = 26) 6)CONSTANT_Class[7](name_index = 27) 7)CONSTANT_Utf8[1](" ") 8)CONSTANT_Utf8[1]("()V") 9)CONSTANT_Utf8[1]("Code") 10)CONSTANT_Utf8[1]("LineNumberTable") 11)CONSTANT_Utf8[1]("LocalVariableTable") 12)CONSTANT_Utf8[1]("this") 13)CONSTANT_Utf8[1]("LFoo;") 14)CONSTANT_Utf8[1]("main") 15)CONSTANT_Utf8[1]("([Ljava/lang/String;)V") 16)CONSTANT_Utf8[1]("args") 17)CONSTANT_Utf8[1]("[Ljava/lang/String;") 18)CONSTANT_Utf8[1]("SourceFile") 19)CONSTANT_Utf8[1]("Foo.java") 20)CONSTANT_NameAndType[12](name_index = 7, signature_index = 8) 21)CONSTANT_Class[7](name_index = 28) 22)CONSTANT_NameAndType[12](name_index = 29, signature_index = 30) 23)CONSTANT_Utf8[1]("Hello world") 24)CONSTANT_Class[7](name_index = 31) 25)CONSTANT_NameAndType[12](name_index = 32, signature_index = 33) 26)CONSTANT_Utf8[1]("Foo") 27)CONSTANT_Utf8[1]("java/lang/Object") 28)CONSTANT_Utf8[1]("java/lang/System") 29)CONSTANT_Utf8[1]("out") 30)CONSTANT_Utf8[1]("Ljava/io/PrintStream;") 31)CONSTANT_Utf8[1]("java/io/PrintStream") 32)CONSTANT_Utf8[1]("println") 33)CONSTANT_Utf8[1]("(Ljava/lang/String;)V")

Field Table 17 count2 byteslength of table Field Table 1variableindex into CP Field Table 2variableindex into CP... table of field table entries, one for each field defined in the class

Field Table Entry 18 access flags2 bytese.g. public, static,... name index2 bytesindex of a Utf8 descriptor index2 bytesindex of a Utf8 attributes count2 bytesnumber of attributes attribute 1variablee.g. constant value attribute 2variable...

Method Table 19 count2 byteslength of table Method Table 1variableindex into CP Method Table 2variableindex into CP... table of method table entries, one for each method defined in the class

Method Table Entry 20 access flags2 bytese.g. public, static,... name index2 bytesindex of a Utf8 descriptor index2 bytesindex of a Utf8 attributes count2 bytesnumber of attributes code attributevariable... attribute 2variable...

Code Attribute of a Method 21 maxStack2 bytesmax operand stack depth maxLocals2 bytesnumber of local variables codeLength2 byteslength of bytecode array codecodeLengththe executable bytecode excTableLength2 bytesnumber of exception handlers exceptionTableexcTableLengthexception handler info attributesCount2 bytesnumber of attributes attributesvariablee.g. LineNumberTable

Stack Frame of a Method 22 local variable array operand stack thisp0p0 p1p1 p2p2 parameters other locals maxLocals maxStack = reference type = integer (boolean, byte,...) = continuation String Hash- table Object String- Buffer User- Class int[ ] = useless

Example Bytecode 23 5: iload_1//load b 6: ifeq 16//if false, goto else 9: iload_3//load y 10: iconst_1//load 1 11: iadd//y+1 12: istore_2//save x 13: goto 19//skip else 16: iload 4//load z 18: istore_2//save x 19:... else clause then clause if (b) x = y + 1; else x = z;

Examples 24 class Foo { public static void main(String[] args) { System.out.println("Hello world"); } A) 2 Q) How many methods?

25 public static void main (String[] args) Code: maxStack=2 maxLocals=1 length=9 exceptions=0 attributes=2 source lines=2 local variables=1 java/lang/String[] args startPC=0 length=9 index= : getstatic java/lang/System.out 3: ldc "Hello world" 5: invokevirtual java/io/PrintStream.println(Ljava/lang/String;)V 8: return ================================================================ void () Code: maxStack=1 maxLocals=1 length=5 exceptions=0 attributes=2 source lines=1 local variables=1 Foo this startPC=0 length=5 index= : aload_0 1: invokespecial java/lang/Object. ()V 4: return

Exception Table Entry 26 start2 bytesstart of range handler is in effect end2 bytesend of range handler is in effect entry2 bytesentry point of exception handler catchType2 bytestype of exception handled An exception handler is just a designated block of code When an exception is thrown, table is searched in order for a handler that can handle the exception

Class Loading 27 Java class loading is lazy A class is loaded and initialized when it (or a subclass) is first accessed Classname must match filename so class loader can find it Superclasses are loaded and initialized before subclasses Loading = reading in class file, verifying bytecode, integrating into the JVM

Class Initialization 28 Prepare static fields with default values –0 for primitive types – null for reference types Run static initializer –performs programmer-defined initializations –only time is ever run –only the JVM can call it

Class Initialization 29 class Staff { static Staff Dexter = new Staff(); static Staff Parvati = new Staff(); static Staff Anthony = new Staff(); static Map h = new HashMap (); static { h.put(Dexter, INSTRUCTOR); h.put(Parvati, TA); h.put(Anthony, TA); }... } Compiled to Staff.

Initialization Dependencies 30 class A { static int a = B.b + 1; //code in A. } class B { static int b = 42; //code in B. } Initialization of A will be suspended while B is loaded and initialized

Initialization Dependencies 31 class A { static int a = B.b + 1; //code in A. } class B { static int b = A.a + 1; //code in B. } A) yes and yes Q) Is this legal Java? If so, does it halt?

Initialization Dependencies 32 class A { static int a = B.b + 1; //code in A. } class B { static int b = A.a + 1; //code in B. } Q) So what are the values of A.a and B.b ? A) A.a = 1 B.b = 2 2 1

Object Initialization 33 Object creation initiated by new (sometimes implicitly, e.g. by +) JVM allocates heap space for object – room for all instance (non-static) fields of the class, including inherited fields, dynamic type info Instance fields prepared with default values – 0 for primitive types – null for reference types

Object Initialization 34 Call to object initializer (...) explicit in the compiled code – compiled from constructor –if none provided, use default () –first operation of must be a call to the corresponding of superclass –either done explicitly by the programmer using super(...) or implicitly by the compiler

Object Initialization 35 class A { String name; A(String s) { name = s; } (java.lang.String)V 0: aload_0 //this 1: invokespecial java.lang.Object. ()V 4: aload_0 //this 5: aload_1 //parameter s 6: putfield A.name 9: return

Next Time 36 Class file format Class loading and initialization Object initialization Method dispatch Exception handling Bytecode verification & stack inspection