1 / 198

Wen-mei Hwu Department of ECE University of Illinois

Java Virtual Machine: VM Architecture, Software Architecture, Implementations, and Application Programming Interfaces. Wen-mei Hwu Department of ECE University of Illinois. Objective of the Course. In-depth understanding of unique aspects of Java-based systems

Download Presentation

Wen-mei Hwu Department of ECE University of Illinois

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Virtual Machine: VM Architecture, Software Architecture, Implementations, and Application Programming Interfaces Wen-mei Hwu Department of ECE University of Illinois

  2. Objective of the Course • In-depth understanding of unique aspects of Java-based systems • Benefit of Java language features for various applications • Complexities of the Java Bytecode architecture • Functionalities of the Java API and runtime

  3. Outline • Introduction • Basic Bytecode Architecture • Java Objects • Java Runtime • Java Core API • Related Technologies • Java Related Developments

  4. Java Security • Bytecode Level • Disallows pointer manipulation • Provides array bounds checking • Checks for NULL references (pointers) • Eliminates illegal type casting

  5. Java Security • API Level • Prohibits untrusted code from accessing local disk, creating processes, connecting to other hosts, calling native code, etc. • Authentication: verify that bytecode is from a trusted vendor

  6. Java Portability/Mobility • Bytecode Level • Binary compatibility: abstracts machine architecture and compiler • Standard module format and compact representation • Dynamic linking and loading

  7. Java Portability/Mobility • API Level • Operating system abstraction • Consistent support for • Threads • I/O and Network interface • Database Interface • Graphical User interface

  8. Development Environment • Language features • Automatic memory management • Structured exception handling • Object-oriented design

  9. Development Environment • Modular development and 3rd-party software components • Standard, dynamically linked modules • Packages (related class file naming) • Avoid naming conflicts • Control access between modules

  10. Key Application Areas • Network-based devices • Mobility - applet downloading and software agents • Portability - code server has no knowledge of client platform • Security - code cannot be trusted

  11. Key Application Areas • Distributed applications • Common development environment across tiers • Same code can run on all machines • Software can be repartitioned as hardware/requirements change

  12. Database Application Development • Can safely extend server software with Java bytecode • Traditional approach: may crash server or corrupt database • Java worst case: query fails or ties-up resources

  13. Database Application Development • Removes barriers between client and server development (same language/tools on both platforms) • Easy linkage with server code (no binary compatibility issues) • Run server on multiple platforms/easy migration

  14. Incorporating Object Data Types • Objects can be serialized and delivered to client • Same code can manipulate an object on both server and client • Different situations may call for either approach • Bytecode can be loaded directly from the database

  15. Database Example • Each employee record contains a Java address object • Custom logic can be incorporated in methods of the Java class Address • Example: Using just street and zip, the Address initialization method computes city and state to ensure consistent address

  16. Database Example • Database designers can exploit inheritance to handle address idiosyncrasies • Subclasses of type US_Address and CanadianAddress • A different Java method computes short zip code depending on type

  17. Database Example Insert new rows using inheritance: INSERT INTO employees (id, name, address) VALUES (1001, “Bill Clinton”, new US_Address (‘1600 Pennsylvania Ave’, ‘22042-1234’)) INSERT INTO employees (id, name, address) VALUES (1002, “Jim Carrey”, new CanadianAddress(‘75 John Street’, ‘N2L 3X2’))

  18. Database Example • Query cities and short zip codes • Method to calculate short zip code runs on server SELECT name, address.city, address.shortZip() FROM employees

  19. Database Example • Query an address • Short zip method runs on client SELECT name, address FROM employees WHERE ID = ‘1001’ INTO cust_name, cust_address IF cust_address.state = “IL” THEN PRINT “Local Customer:” + cust_name + “ Zip:” + cust_address.shortZip()

  20. Challenges Presented By Java • Translation to native architecture • Instruction set translation • Stack based to register based conversion • Dynamic Linking - delay translation or manage binary dependencies

  21. Challenges Presented By Java • Barriers to optimization • Run-time checks (null ptr/array bounds) and exception handling constrain code motion • Heavily object-oriented code - unable to resolve calls/inline • Dynamic loading inhibits analysis (code base extended at runtime)

  22. Challenges Presented By Java • Security features add overhead • Overhead of run-time checking instructions (null ptr/array bounds) • Additional code and control flow to protect resource access

  23. Bytecode Architecture Overview • Load-store stack architecture • All operands pushed on value stack before use • Source operands are fetched from stack and the result is pushed on • Equivalent to register file in load-store architectures

  24. Bytecode Architecture Overview • Extensive support for Java Language and Dynamic Linking • Object manipulation instructions • Method invocation instructions • Exception handling support

  25. Bytecode Architecture Overview • Opcode space • 202 assigned, 25 quick, 3 reserved • Load-store stack architecture • avoid register file size assumption • smaller code size • 73.3% of the assigned opcodes are a single byte

  26. Stack Computation Model Example: add Translated code push A push B r2 ¬ pop(B) r1¬ pop(A) r3 ¬ r1+r2 push r3 Stack operation push A push B add

  27. Bytecode Modules (Class Files) • Java language compiler produces separate module (class file) for each class defined in program • All references across classes (and most within a class) • compiled as symbolic references • stored in a data structure inside class file ( constant pool)

  28. Bytecode Modules (Class Files) • Bytecode with operands (27.7% of total assigned opcodes) • typically indexes into the constant pool or local variable table

  29. Bytecode operands • As VM executes a method its stack frame holds a pointer to the constant pool of the method’s class • When constant pool entry is first referenced by an instruction, the Java VM resolves the symbolic link

  30. Simple example Invokevirtual 0x0005 Constant Pool ... ... Method ref 5 Constant pool ... ... 8 class ref Stack frame ... Name & type a ... d “add” ... ‘(‘ ‘I’ ‘I’ ‘)’ ‘I’ 11

  31. Bytecode operation types • Stack Manipulations • push, pop, duplicate, swap • Local Variable accessing • load, store • Arrays • create, store, retrieve

  32. Bytecode operation types • Objects • create, access, set • Arithmetic and Type Conversion • add, subtract, multiply, divide, shift, AND, OR, XOR

  33. Bytecode operation types • Control Flow and Exceptions • conditional, unconditional, goto, local returns, tables, throws • Method Calls and Returns • virtual, special, static, interface, returns

  34. Quick Opcodes • Patented by Sun • Interpreter based runtime optimization • Not part of VM spec, never appear in class files • Patched over normal instructions at runtime the first time they are executed

  35. Quick Opcodes • When a constant pool entry is resolved, the symbolic is reference replaced with direct reference • Quick opcode signifies constant pool entry already contains a direct reference to target, and VM does not have to perform certain checks

  36. Java Objects Outline • Classes and Interfaces • Polymorphism and Dynamic Method Resolution • Method Tables • Bytecode Ops for Method Invocation • Bytecode Ops for Object Manipulation

  37. Language Terms • Class: Data structure and associated functions to manipulate the data • Object: An instance of a class (Object creation=Class instantiation) • Method: Behavior associated with a class (function to manipulate object) • Field: Individual elements of data defined by a class (object state)

  38. Language Terms • Polymorphism: Objects of different classes can be passed to the same client code. When the client code invokes a method on the object, the code executed depends on the object’s run-time class (requires dynamic method resolution, or late binding)

  39. Basic Java Types • Primitive Types • byte, short, int, long, char, float, double, boolean • range of values for each type constant across platforms • Reference Types (non-primitive) • Declare pointers to objects on the Java Heap

  40. Reference Types • Java language requires that each reference variable has a declared type (statically typed) • At runtime a reference may be assigned to different types of objects • If a type-cast is potentially unsafe, compiler inserts run-time checks to insure type “compatibility”

  41. Reference Types • Interface: Set of guaranteed method declarations without any implementation • Class: Set of guaranteed methods and fields including method implementation • Only classes can be instantiated (objects always belong to a class)

  42. Class Hierarchy • A class may extend one other class, its superclass. A class inherits the fields and methods of its superclass including their implementation. • A class may implement multiple interfaces, and must provide code for all methods guaranteed by those interfaces.

  43. Extending Classes: Method Overriding • A subclass contains both the methods “inherited” from its superclass and any new methods defined in the class • A subclass may override a method in its superclass by redefining the method with same name, parameter types and return type

  44. Abstract Classes • A class may defer implementing a method by declaring it abstract • A class with abstract methods cannot be instantiated • Any non-abstract subclass that has an abstract class ancestor must provide the implementation for the ancestor’s abstract methods

  45. Inheritance Hierarchy Printer • A class has only one superclass, but may have many ancestors • Implementation of a single class can then be treated as multiple interfaces Class Interface DotMatrix LaserPrinter Copier FaxMachine Extends Implements OfficeMatePlus

  46. Class/Interface Definition Example // Interfaces contain no code interface Copier { void makeCopies(); } interface FaxMachine { void sendFax(); } // Provide code for Copier and // FaxMachine interfaces // Inherit code from LaserPrinter class OfficeMatePlus extends LaserPrinter implements Copier, FaxMachine { void makeCopies() { // code to make copies ...} void sendFax() { // code to send a fax ... } } // Defer Printer implementation abstract class Printer { abstract void print(); } // Override Printer.print() class DotMatrix extends Printer void print() { // dot matrix printer code ... }} // Override Printer.print() class LaserPrinter extends Printer { void print() { // laser printer code ... }}

  47. Dynamic Method Resolution • Dynamic resolution ensures that the correct code will be invoked based on the object’s runtime class • Interface Calls • If reference type is an interface, method resolution locates the method’s implementation based on the object’s runtime class

  48. Dynamic Method Resolution • Virtual Calls • If the reference type is a class, method resolution must determine which implementation to use - the object’s runtime class may override a method defined in a base class

  49. Revisiting Class/Interface Example // More office equipment class QuickCopier implements Copier { public void makeCopies() { … } } class EasyFax implements FaxMachine { public void sendFax() { … } } // Can use three types of machines class TrainedEmployee { void PrintDocument( Printer p) { p.print(); // Virtual call } void replicateDocument( Copier c) { // Interface call c.makeCopies(); } void faxDocument( FaxMachine f) { // Interface call f.sendFax(); } }

  50. Revisiting Class/Interface Example // Upgrade to the OfficeMatePlus OfficeMatePlus o = new OfficeMatePlus(); // Trained employee can do // everything with one machine // by using different interfaces // depending on the situation e.printDocument(o); e.replicateDocument(o); e.faxDocument(o); // Polymorphism means you // don’t have to retrain your // employee // Create a new TrainedEmployee TrainedEmployee e = new TrainedEmployee(); // Create office equipment objects DotMatrix dm = new DotMatrix(); QuickCopier qc = new QuickCopier(); EasyFax ef = new EasyFax(); // TrainedEmployee can use all // three machines e.printDocument(dm); e.replicateDocument(qc); e.faxDocument(ef);

More Related