Java Virtual Machine - bytecode execution engine

Outline

Java Virtual Machine specification developed a conceptual model of a virtual machine byte code execution engine, to become a unified appearance of a variety of virtual machine execution engine (Facade). Different virtual machines Engines include two execution modes, interpreted and compiled execution.

Runtime stack frame structure

Stack frame (Stack Frame) supports virtual machine data structures, method calls and method of execution, it is the stack of elements in the data area of the virtual machine stack (Virtual Machine Stack) virtual machine is running. Storing a local variable stack frame method, the operand stack, and a method for dynamic link return address and other information. Method calls start to execution completion, which corresponds to a frame in the stack and push the stack inside the virtual machine process stack.
A method call chain threads may be very long, many ways while being executed, but only the top of the stack stack frame is valid for execution engines, called the current stack frame (Current Stack Frame), associated the method is called the current method (current method).

Local variable table

Local variables table (Local Variable Table) variable value storage space for storing local variables inside method parameters and method definitions. The capacity of the variable groove (Variable Slot) as a minimum unit, a Slot can store data type 32 within, a Slot 64-bit physical memory in 64-bit virtual machine may use the alignment and padding means to enable the Slot appearance the looks and 32-bit virtual machine consistency. For 64-bit data types, the high order virtual machine assigned an aligned manner two consecutive Slot space, where the data types long and double split bank practices the "long and double non-agreement of the atomic" in the long time and double data type read two 32-bit write bit partition approach somewhat similar.
Whether the local variable table of the object to be depends on whether there is also a reference to the object Slot root cause of garbage collection. If there is a method, the rear end of the code have a very time-consuming operation, and the front end and take up a lot of memory is defined, for the variable is no longer manually set the actual use so that it is able to Null garbage, or even out of scope, but the local variable table as part of the GC Roots remained significantly associated to it, cause memory footprint has been unable to release.

Operand stack

Operand stack (Operand Stack) often referred to as stack operation, a first-after he is out (Last In First Out, LIFO) stack.
In the conceptual model, two stack frames are completely independent of each element as a virtual machine. However, when implementing a virtual machine which will do most of some optimization process, so that two stack frames partially overlap occurs, so that following portions of the stack frame portion of the operand stack and local variable stack frame table above overlap, so that method invocation you can share part of the data, without additional copy transfer parameters.
Java virtual machine interpretation execution engine called "stack-based execution engine", which refers to the stack operand stack.

Dynamic Link

Each stack frame contains a reference to the runtime constant pool of the stack frame associated method, it is to hold a reference to the connection support method for dynamic (Dynamic Linking) during the call. Class file constant pool exists a large number of reference symbols, the method bytecode instruction to swap the red symbols directed to a method reference constant pool as a parameter. These symbols will translate to a portion of a reference class or loading stage is used for the first time a direct reference, such conversion is known as static resolution. Another part will be converted during each run directly references, this part is called dynamic linking.

Methods return address

After a method to begin, there are two ways to exit. The first is to perform any engine encounters a bytecode instruction method returns, the return is normal, this method is called normal completion exit outlet (Normal Method Invocation Completion).
Another type of exit is encountered during the execution of exception process, this exception is not handled in vivo method, the exception table as long as this method does not search for matching exception handler, the method will lead to exit, this exit mode called (abrupt Method Invocation Completion).
Either way exit after exit program should return to the position in order to continue to implement the method is called, the method returns when required stack frame holds information used to help restore the state of the implementation of its upper method. In general method exits normally counter PC value of the caller as the return address, the stack frame could save the counter values. When abnormal exit, the return address is to be confirmed by the exception handling table, stack frame generally will not save this part of the information.
Method exit procedure is virtually identical to the current stack frame of the stack, so the ejecting operation may include: local variables and operand stack table top recovery method, the return value (if any) onto the operand stack caller frame stack, to adjust the value of the counter PC to point to a command instruction to call back method or the like.

Method Invocation

Method calls does not mean execution method, the method call in order to determine the called method version (that is, which method call), being not related to specific operational processes within the method. Class file compilation process symbol stored in the referenced entry in the address memory layout (direct references) are not actual operation. Java method calls required during class loading, even during the operation in order to determine the method of direct reference to the target.

Resolve

Classloading analytical phase, some symbolic reference method call into a direct reference, based on the premise: there is a method that can be determined before the program actually run a version called, and can not be changed during operation. In other words the calling object code is written, it must be finalized editor compile time. Such methods called call parsing (Resolution).
Java language in line with "compile-known, immutable run" approach this requirement, including static methods and private methods into two categories, the former is associated with a type, which can not be accessed outside of the two methods determine the characteristics of each they can not by inheritance or otherwise override the other version, they are suitable for parsing the class loading phase.
Corresponding thereto, Java virtual machine provides a method call five bytecode instructions:
invokestatic: call the static method.
invokespecial: call an instance constructor Methods, private methods and the parent class method.
invokevirtual: call all virtual methods
invokeinterface: call interface method will then determine an object of this interface at runtime.
invokedynamic: dynamically resolved at runtime to a method call site referenced qualifiers, then execute the method, four again before this call instruction, dispatch logic when cured inside a Java virtual machine, and the instruction dispatch logic is invokedynamic the method set by the user to guide the decision.
And as long as the method can be invokestatic invokespecial command called, can be determined only in a version called resolution phase, when the class will be loaded symbolic references resolved to the method of direct reference, these methods can be called non-virtual methods, other methods It referred to as virtual methods.

Assignment

Dispatch actually explained as "severe" and "rewrite" in the Java Virtual Machine is how to achieve.

  1. Static assignment
    example there are three categories:
    static abstract class Human
    static class Human Man the extends
    static class the extends Woman Human
    wherein the static type (Static Type) Human variable called, or type of look (Apparent Type), later called the actual variables of Man type (Actual type), the compile phase, the Javac compile determine which overloaded version, and the symbols correspond to the reference parameter method in accordance with written instructions invokevirtual parameters of the static type.
    All rely on static type positioning method executed version of the dispatch operation is called static assignment. Typical applications are assigned a static method overload. Static assignment occurs at compile time, so the action is not static assignment executed by the virtual machine. Further, although the compiler can determine the overloaded version of the method, but in many cases this is not overloaded version "only", can only determine a "more appropriate" version, such as char type variable 'a' may be understood as char, can be understood as int, long, Object, char ... and so on, so at compile time "to select a more appropriate" means in this.
  2. Dynamic assignment
    invokevirtual instruction runtime resolution process is roughly divided into about several steps:
    1) find the actual type of the first element of the operand stack pointed object, referred to as C
    2) If the type found C with constant method descriptor and simple names are consistent, then the access rights verification, if this method returns a direct reference to find the end of the process; if not passed, the java.lang.IllegalAccessError abnormal returns.
    3) otherwise, in accordance with the inheritance up once for each parent class C of the second step of the process from the search and verification.
    4) If you never find the right way to throw java.lang, AbstractMethodError exception.
    As the first step invokevirtual instruction execution is to determine the actual recipient of the type at runtime, this process is actually a method to rewrite the Java language in nature. We call this run is called dynamic dispatch method of determining the actual type of assignment process executed version.
  3. Single and multiple dispatch dispatch
    parameters and methods recipients method referred to as variables in the method of this definition should come first "Java and model," a book. According assigned amount based on how many cases can be divided into single-dispatch and dispatch multiple dispatch. Single-dispatch is a selection of variables in a target method, dispatch is selected plurality of target variables in more than one method.
    Before Java1.7 and, still more than a static assignment, dynamic multi-language dispatch.
  4. Dynamic assignment of virtual machines to achieve
    those previously described in the virtual machine at the time of assignment "will do", but virtual machine "How specific do it," different virtual machine implementations are some differences.
    Due to the dynamic dispatch is a very frequent behavior, and dynamic method dispatch method metadata version of the selection process needs to be run again when class search for a suitable target method, therefore based on performance considerations, most of the virtual machine implementation will not be so true frequent search, the most common means of optimization is to stabilize the class to create a virtual method table (virtual method table, referred to in the vtable methods zone, use the interface method table --Interface method table when the corresponding invokeinterface execution, called itable ).
    In addition to the method table, in a case where conditions allow, also provided with associative cache (Inline Cache) and "analysis type inheritance" (Class Hierarchy Analysis, CHA) daemon inline art (Guarded Inlining) based on two non- stable means of aggressive optimization to get higher performance.

Dynamically typed language support

Java virtual machine from the first virtual machine before JDK7 more than ten years, have not changed bytecode instruction set, add the class invokedynamic instruction in JDK7, which is to achieve "dynamic language type" (Dynamic Typed Language) one of the improvements support, but also to achieve a smooth JDK8 Lambda expressions make technical preparations.

  1. Dynamically typed language
    key feature dynamically typed language is its type checking theme process at runtime rather than compile time, for example: Groovy, JavaScript, PHP, Lisp, etc., relative to the language type of the inspection process at compile time ( such as C ++ and Java, etc.) is statically typed languages.
    "Untyped variables and variable values have type" This feature is also an important feature of dynamically typed languages.
  2. java.lang.invoke package
    JDK1.7 implements JSR-292, joined the java.lang, invoke the package, the main purpose of this package is before relying solely symbolic reference to determine the target method call outside, provide a new determining a target dynamic method call mechanism MethodHandle.
    Reflection and MethodHandle method calls are analog, but the analog Reflection is a method called the Java code level, and is called the analog MethodHandle bytecode levels method, MethodHandles.lookup three methods --findStatic (), findVirtual ( ), findSpecial () corresponds precisely to invodestatic invokevirtual & invokeinterface behavior and execute permissions check invokespecial bytecode instructions that several of these low-level details are not required when using ReflectionAPI concern.

Guess you like

Origin www.cnblogs.com/pluto4596/p/12080436.html