Java Virtual machine schematic diagram 4.JVM machine instruction Set

Source: Internet
Author: User


0. Preface

Java virtual machines, like real computers, perform binary machine codes, and we compile. Java source code into a. class file, and the class file is the binary machine code that the Java virtual machine can recognize. Java can recognize the information and machine instructions in the class file and execute these machine instructions.

So how does a Java Virtual machine execute these binary machine codes?

This article will take you through a very easy example of how the Java virtual machine executes the machine code and the fundamentals of its work.


After reading this article, you will learn:

1. Java Virtual machine-to-Execute virtual machine stack (JVM stack) organization

2. how the method invocation procedure is represented in the JVM

3. TheJVM's basic strategy for a method to run

4. Format of JVM machine instructions

5. The operating mode of the machine instruction---the mode based on the operand stack


1. Java Virtual machine-to-Execute virtual machine stack (JVM stack) organization

At execution time, a Java virtual machine allocates a virtual machine stack in memory for each thread to represent the execution state and information of the thread. The elements in the virtual machine stack are called stack frames (JVM stack frame), and each stack frame represents the invocation information for a method. For example, see the following:



The description described above may be somewhat abstract. In order to give the reader an intuitive feeling. We define a simple Java class and then execute this class of execution. Progressively analyze the organization of the entire Java Virtual machine execution-time information.

2. How the method invocation procedure is represented in the JVM

We will define, for example, the following simple class Org.louis.jvm.codeset.Bootstrap.java with the main method, and step through the analysis of how the class is represented in the JVM. How the method is executed step-by:

Package org.louis.jvm.codeset;/** * JVM principle Simple use case * @author Louis * */public class Bootstrap {public static void main (string[ ] args) {String name = "Louis"; greeting (name);} public static void Greeting (String name) {System.out.println ("Hello," +name);}}


When we compile Bootstrap.java into Bootstrap.class and execute this program, there are a few steps in the JVM's complex execution logic:

1. The JVM first loads this bootstrap.class information into the in-memory method area.

The bootstrap.class includes the constant pool information, the definition of the method, and the binary form of the machine instruction implemented by the compiled method. All threads share a method area from which to read the instruction set of the method definition and method.

2. Next. The JVM creates a class<bootstrap> instance for Bootstrap.class on the heap heap to represent bootstrap.class instances of the class.

3. The JVM starts running the main method. A stack frame is created for the main method. To represent the entire run of the main method (I'll expand the process specifically in later chapters).

4. In the process of running, the main method calls the greeting static method, then the JVM creates a stack frame for the greeting method and pushes it to the top of the virtual machine stack (which I will specifically expand in later chapters).

5. When the greeting method finishes executing, the greeting method is out of the stack. The main method continues execution;

The process of a JVM method call is implemented through a stack frame, then. How is the instruction of the method executed? Before we figure this out. Let's start by understanding what the structure of the method is for the JVM.

We know. Class file is a binary file that the JVM can recognize, in which the definition of each method is described by a specific structure description.

The JVM is in the process of compiling bootstrap.java. At the same time that the source code is compiled into binary machine code, the three messages of each method are inferred:

1) The number of local variables that will be used at execution time (function: When the JVM creates stack frames for methods). Create a local variable table for the method in the stack frame. To store the local variable value of the method instruction at the time of operation)

2). The maximum number of operand stacks required to run the machine instruction (when the JVM creates a stack frame for the method). Creates an operand stack for the method in the stack frame. Guarantee that the instructions within the method will be completed.)

3). Number of parameters of the method


After compiling. We are able to get information for the main method and the greeting method such as the following:

Note: All of the above compiled information is stored in the Bootstrap.class file and is stored in the form of this class file in terms of the definition of the class file format. I have made a very detailed introduction in the previous articles, assuming that you have read all of them. Then believe that you have been able to "read" the class file. How to read the organization of method and its corresponding machine code in a class binary file. Please read the Java Virtual machine schematic 1.5, class file method table collection how the--method method is organized in the class file.



The process by which the JVM executes the main method:

1. Create a stack frame for the Main method:

The JVM parses the main method and finds that the number of its local variables is 2. The number of operand stacks is 1, and a stack frame (VM stack) is created for the main method and added to the virtual machine stack:



2. Completion of stack frame initialization:

After the main stack frame is created, the stack frame is pushed to the virtual machine stack, and now there are two important things to do:

a). Calculate the PC value.

The PC is the instruction counter. The internal value determines which machine instruction the JVM virtual machine should run next. And the machine instructions are stored in the method area. We need to point the value of the PC to the main method of the method area;

Initialize the PC = Main method at address +0 of the method area directive.

b). The initialization of the local variable. The Main method has an entry (string[] args), and the JVM has vacated a slot in the local variable table of the stack frame where main is located. We need to initialize the reference value of args to the local lit table;



      1. The JVM then starts reading the machine instructions that the PC points to.

        As you can see. The instruction sequence of the Main method: 12 10 4c 2b b8 20 12 b1 through the JVM virtual machine instruction set specification. Able to parse this instruction sequence into the following Java assembly language:

Machine instructions Assembly Explain Impact on Stack Frames
0x12 0x10 LDC #16 Pushes the 16th constant pool item reference in a constant pool to the top of the operand stack.
Chang the 16th item is the Constant_utf-8_info item. Represents the "Louis" string
0x4c Astore_1 The stack top element of the operand stack is stacked and the value of the top element of the stack is assigned to the Index=1 local variable TABLE element.



This is equivalent to: name = "Louis".

0x2b Aload_1 Pushes the value of the index=1 element in the local variable table to the top of the operand stack
0xb8 0x20 0x12 Invokestatic #18 0xb8 indicates the machine instruction invokestatic, the operand is 0x20 << 8| 0x12 = 18. The operand 18 indicates a pointer to the Chang 18th item, which is a symbolic reference to the Main method:
org/louis/jvm/codeset/bootstrap.greeting: (ljava/lang/string;) V
when the JVM runs this statement, there are a few things to do:
a). method symbol Reference check.

the symbol reference for this method is verified. If the definition of this method is found in the constant pool according to this symbol rule, the resolution is successful. If the method is greeting:(Ljava/lang/String;)V not found, the JVM throws an errorNoSuchMethodError
b). Create a new stack frame for the new method call. the JVM then creates a new stack frame (VM stack) for this method greeting, and creates an operand stack of the corresponding size based on the size of the operand stack and the number of local variables in the greeting, and pushes the stack frame to the top of the virtual machine stack.


c). Update the value of the PC instruction counter.

Logs the value of the current PC program counter to the greeting stack frame. When the greeting is finished running, the PC value is restored. Update the value of the PC so that the next running instruction address points to the beginning of the instruction of the greeting method.
This statement causes the current main method to pause, allowing the JVM to run into the greeting method, where the value of the PC program counter is restored to the current next instruction when the greeting method has finished running.

0xb1 Return Return

When the Main method calls greeting (). The JVM creates a stack frame for the greeting method that represents the call to the greeting method. Detailed stack frame information such as the following:

The detailed greeting method of the machine code means the meaning of the representation as seen in:


Machine instructions Assembly Explain Chang references
B2 1a Getstatic #26 Gets the static field of the specified class and pushes its value to the top of the stack.
Push the 26th symbol reference in a constant pool into the operand stack:
#26:
Field Java/lang/system.out:ljava/io/printstream;
BB 20 20 New #32 Creates an object. and press its reference value into the top of the stack.
Create a Java/lang/stringbuider instance and press it into the top of the stack.
#32:
Class Java/lang/stringbuilder
59 Dup Copy the value of the stack top of the operand and insert it into the top of the stack
12 22 LDC #34 Extracting data from the execution-time pool to push the operand stack
Copy the "Hello" string reference into the operand stack
#34:
String Hello,
B7 20 24 Invokespecial #36 Call the superclass constructor method, instance initialization method, private method.
The StringBuilder (String) constructor method is called here. and push the results to the top of the stack
#36:
Method Java/lang/stringbuilder. " <init> ":(ljava/lang/string;) V
2a Aload_0 Pushes a reference to the first local variable to the top of the stack.
The first local variable reference to the current local variable table is: "Louis", which will push Louis to the top of the stack
B6 20 26 Invokevirtual #38 Call the superclass constructor method. Instance initialization method, private method.
The Append (String) method of the StringBuilder instance. Said:
"Hello," + "Louis".
Method java/lang/stringbuilder.append: (ljava/lang/string;) Ljava/lang/stringbuilder;
B6 2a Invokevirtual #42 Call the superclass constructor method, instance initialization method, private method.
The ToString () method of the StringBuilder instance is called, and the result remains at the top of the stack.
Method java/lang/stringbuilder.tostring: () ljava/lang/string;
B6 2e Invokevirtual #46 Call the superclass constructor method, instance initialization method. Private methods.
Call the System.out.println (String) method
Method java/io/printstream.println: (ljava/lang/string;) V
B1 Return End return




3. The JVM's basic strategy for running a method

In general, for the execution of a Java method, the JVM allocates a local variable table for the method in the virtual machine stack (JVM stack) of one of its particular threads. An operand stack that stores intermediate values during the execution of a method.

Because the JVM's instructions are stack-based, that is, most of the instructions run. are accompanied by the stack and stack of operands. So when you learn the machine instructions from the JVM. Be sure to remember one thing:

Each machine instruction is run. With the influence of the operand stack and the local variables, the mechanism is fully understood and you are able to read the binary machine instructions in the class file very smoothly.

For example, the following is a simplified diagram of the stack frame information, in the analysis of the JVM instructions, the stack frame in mind a clear understanding:



4. Format of machine Instructions

The so-called machine instructions are binary codes that only machines can recognize. A machine instruction consists of two parts:

Note:

a). As seen in the JVM virtual machine opcode is composed of one byte, that is, for the JVM virtual machine, the number of its instructions is 2^8, that is, 256;

b). The opcode such as: b2,bb,59 .... etc. are all representations of a particular machine instruction, in order to facilitate our identification, it has a corresponding mnemonic: Getstatic,new,dup .... This is convenient for us to understand.


5. The operating mode of the machine instruction---the mode based on the operand stack


for a traditional physical machine. Most of the machine instructions are designed with registers. A number of registers are set within the physical machine to store values during machine instruction execution. The number of registers and the number of supported instructions determine the processing power of the machine.

However, the mechanism for designing Java virtual machines is not such that the Java Virtual machine uses the operand stack to store values in the operation of the machine instruction. All operands must follow the rules of the stack and the stack. So in the Java Virtual Machine specification, you'll find that there are a lot of machine instructions that are about the stack-in-stack operation.


The purpose of this article is to introduce how the JVM virtual machine directives are executed, assuming you want to gain a deeper understanding of the instruction set's information and usage considerations, read the Java Virtual Machine specification (Java specification) specific definition of the machine instruction set.


Java_virtual_machine_specification_java_se_7_ Chinese Version




Java Virtual machine schematic diagram 4.JVM machine instruction Set

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.