Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Managed Runtime Systems: Lecture 02 - The Java Virtual Machine

zakkak
February 13, 2018

Managed Runtime Systems: Lecture 02 - The Java Virtual Machine

zakkak

February 13, 2018
Tweet

More Decks by zakkak

Other Decks in Programming

Transcript

  1. Managed Runtime Systems The Java Virtual Machine Foivos Zakkak https://foivos.zakkak.net

    Except where otherwise noted, this presentation is licensed under the Creative Commons Attribution 4.0 International License. Third party marks and brands are the property of their respective holders.
  2. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 2 The JVM architecture Data

    Locations Class Loader Class Loader Execution Engine Execution Engine Stack Area Stack Area PC register PC register Native Stack Native Stack Heap Area Heap Area Method Area Method Area Native Methods Interface Native Methods Interface Native Libraries Native Libraries .java .java .class .class javac java
  3. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 3 Class Loading 1) Verification

    2) Preparation of JVM internal data structures 3) Resolution of references (might load other classes) 4) Initialization
  4. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 4 Java Class File ClassFile

    { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; } ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[constant_pool_count-1]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[attributes_count]; }
  5. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 5 Field Info field_info {

    u2 access_flags; // public, private, protected, static, final, // volatile, transient, synthetic, enum u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Deprecated } field_info { u2 access_flags; // public, private, protected, static, final, // volatile, transient, synthetic, enum u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Deprecated }
  6. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 6 Java fields ▪ Reference

    (Size is implementation-dependent) ▪ Primitive – 8, 16, 32, or 64-bit Integer (byte, short, int, long, boolean, char) – 32 or 64-bit IEEE 754 floating-point (float, double)
  7. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 7 Field Descriptors Abbreviation Type

    Example B byte C char D double F float I int J long L ClassName; reference Ljava/lang/String; S short Z boolean [ array [I [[[D [Ljava/lang/Object;
  8. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 8 Method Info Method Descriptors:

    method_info { u2 access_flags; // public, private, native, var args, synthetic ... u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Code, Exceptions } method_info { u2 access_flags; // public, private, native, var args, synthetic ... u2 name_index; u2 descriptor_index; u2 attributes_count; attribute_info attributes[attributes_count]; // e.g., Code, Exceptions } void foo(int a, Object o) => (ILjava/lang/Object;)V String bar(int[][] i) => ([[I)Ljava/lang/String;
  9. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 9 Code attribute Code_attribute {

    u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; // The actual bytecode u2 exception_table_length; { u2 start_pc; // if catch_type is thrown between start_pc and end_pc, u2 end_pc; // execute handler at handler_pc u2 handler_pc; u2 catch_type } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; // line numbers, stack maps ... } Code_attribute { u2 attribute_name_index; u4 attribute_length; u2 max_stack; u2 max_locals; u4 code_length; u1 code[code_length]; // The actual bytecode u2 exception_table_length; { u2 start_pc; // if catch_type is thrown between start_pc and end_pc, u2 end_pc; // execute handler at handler_pc u2 handler_pc; u2 catch_type } exception_table[exception_table_length]; u2 attributes_count; attribute_info attributes[attributes_count]; // line numbers, stack maps ... }
  10. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 10 Java Stack vs Native

    Stack Java Stack frame frame frame Frame operand stack 2 operand stack 1 operand stack 0 local variable n . . . local variable 1 local variable 0 return address
  11. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 11 Java Threads ▪ A

    Program Counter – Class – Method – bytecode-offset or native code pointer ▪ A Java Stack ▪ Native Stack
  12. Managed Runtime Systems CC-BY https://Foivos.Zakkak.net 12 Java Objects and Arrays

    Object Array header field 0 field 1 . . . field n header length element 0 element 1 . . . element n