The Java virtual machine is an imaginary machine that is implemented by software simulation on actual computers. Java virtual machines have their own hardware, such as processors, stacks, registers, etc., have corresponding instruction systems. 1. Why use Java virtual machine? A very important feature of Java language is that it is independent of the platform. It is the key to implementing this feature using Java virtual machines. General Advanced Language If you want to run on a different platform, at least you need to compile different target code. After introducing a Java language virtual machine, the Java language does not need to recompile when running on different platforms, as shown. Figure Java language usage mode Java virtual machine shielded information related to the specific platform, so that the Java language compiler only needs to generate the target code (byte code) running on the Java virtual machine, you can do not modify on a variety of platforms. Run. When the Java Virtual Machine is executed, the Java virtual machine interprets the field code as the machine command execution on the specific platform. 2. Who needs to understand the Java virtual machine (1) Java virtual machine is the foundation of the underlying implementation of the Java language, and people who are interested in Java should have a large number of Java virtual machines.
An understanding. This helps understand some of the nature of the Java language and help to use Java languages. For people who want to implement Java virtual machines on a particular platform, the Java language compiler author and who want to use hardware chips to implement Java virtual machines, you must deeply understand the specification of Java virtual machines. Also, if you want to expand
Java language, or by compiling other languages into a Java language byte code, you also need to go deep into the Java virtual machine.
3. Java Virtual Machine Supported Data Type Java Virtual Machine Support Java Language The basic data type is as follows: Byte: // 1 Byte Six complex hort: // 2 byte Sign Int: / / 4-byte with symbol integer complementing long: // 8 bytes Symbol integer complement FLOAT: // 4 byte IEEE754 single precision floating point number double: // 8 byte IEEE754 double precision floating point number char: / / 2 Bytes No symbols Unicode characters Almost all Java types are completed at compile time. The data of the original data type listed above does not require a hardware tag when Java is executed. The bytecode (instruction) of these raw data type data itself has pointed out the data type of the operand, such as IADD, LADD, FADD, and DADD instructions plus two numbers, and its operand type is int, LONG, FLOAT and DOUBLE. The virtual machine does not set a separate instruction to the Boolean type. The Boolean type data is processed by the Integer instruction, including Integer returns. The Boolean type array is handled with a Byte array. The virtual machine uses the floating point of the IEEE754 format. Older computers in IEEE format do not support, running Java numerical gauges
When the program is calculated, it may be very slow. Other data types supported by virtual machines include: Object // 4 bytes of a JavaObject (object) reference ReturnadDress // 4 bytes, used for JSR / RET / JSR-W / RET-W Note: Java array is Object processing. The specification of the virtual machine does not have any special requirements for the structure inside the Object. In Sun's implementation, reference to Object is a handle, which contains a pair of pointers: a pointer points to the object table of the object, and another point to the object of the object. The program indicated by the bytecode of the Java virtual machine should comply with the type regulations. The implementation of the Java virtual machine should refuse to perform a segment code program specified in the type. Java virtual machines seem to run on machines of 32-bit address spaces due to the limitations defined by the bytecode. But you can create a Java virtual machine that automatically converts the bytecode into a 64-bit form. It can be seen from the data type supported by Java virtual machines that Java has strictly regulated the internal format of the data type.
The implementation of various Java virtual machines is the same for the interpretation of the data, ensuring that Java is independent of the platform-independent and portability. Second, the Java Virtual Machine Architecture Java Virtual Machine consists of five parts: a set of instruction sets, a set of registers, a stack, a useless unit collection stack, a method area. These five parts are logical components of Java virtual machines, do not rely on? Implement technology or organization, but their functions must be implemented in some way in real machines. 1.Java instruction set Java virtual machine supports approximately 248 bytecodes. Each byte code performs a basic CPU operation, for example, add an integer to the register, subroutine transfer, and the like. The Java instruction set is equivalent to the assembly language of the Java program. The Java instruction set includes a single-byte operator that specifies the operation to be executed, and there are 0 or more operands, the parameters or data required to provide operations. Many instructions do not operate, consisting of only one single-byte operator. The execution process of the inner layer cycle of the virtual machine is as follows: do {Take an operator byte; execute an action according to the value of the operator;} While (the program is not ended) Due to the simplicity of the instruction system, it makes the process of virtual machine execution. Simple, thereby facilitating improving the efficiency of the implementation. The number and size of the operating number in the instruction are determined by the operator. If the operand is larger than one byte, then its stored order is a high byte priority. For example, a 16-bit parameter is stored in two bytes, its value is: the first byte * 256 second byte byte code instruction stream is generally only byte alignment. The instruction TableSwitch and Lookup are exception, and the mandatory 4-byte boundary is required within these two instructions. 2. Register Java Virtual Machine register is used to save the running status of the machine, similar to some of the dedicated registers in the microprocessor. There are four registers of the Java virtual machine: PC: Java program counter. Optop: Pointer to the top end of the operator count. Frame: Pointer to the execution environment of the current execution method. Vars: Pointer to the first variable of the first variable of the local variable area of the currently executing method. (Lower 107) Java Virtual Machine Java Virtual Machine is a stack type, it does not define or uses registers to pass or accept parameters, the purpose is to ensure the simplicity of the instruction set and the efficiency of the implementation (especially for There are not many processors that have many registers). All registers are 32 bits. 3. Stack of Java Virtual Machines has three regions: local variable area, running a environmental area, operative area. (1) Local variable zone Each Java method uses a fixed size local variable set. They are addressed in accordance with the word offset with the VARS register. Local variables are 32-bit. Long integer and double precision floating point occupies two local variables, but in accordance with the index of the first partial variable. (For example, a local variable with index N, if it is a double precision floating point number, then it actually occupies the storage space represented by index n 1.) The virtual machine specification does not require 64 bits in local variables. The value is 64-bit
. The virtual machine provides instructions that load the value in the local variable to the operator stack, and also provides instructions in writing a value in the operator stack. (2) The information that the runtime area is included in the operating environment is used for dynamic links, normal methods return and abnormal propagation. • The dynamic link running environment includes a pointer to the interpreter symbol table to the current class and the current method for supporting the dynamic link of the method code. The Class file code of the method uses symbols when referenced to the method to be called and the variable to be accessed. dynamic
The link translates the method of the symbol form into actual method calls, loads the necessary classes to explain the symbols that have not been defined.
And the variable access is translated into an offset address corresponding to the storage structure of these variables. Dynamic linking methods and variables make changes in other classes used in the method not affect the code of this program. · Normal method Returns if the current method is ended normally, when a return command with the correct type is executed, the call method will get a return value. The execution environment is used to recover the caller's register in the case of normal return, and add an appropriate value to the caller's program counter to skip the executed method call command, then continue to execute in the caller's execution environment.
Opened. · Exceptions and error communication abnormalities are called ERROR (error) or Exception in Java, is the subclass of the Throwable class, in the program?
The reason is: 1 Dynamic link error, if you can't find the desired Class file. 2 Running a wrong, such as a reference to an empty pointer, using the throw statement. When an abnormality occurs, the Java virtual machine takes the following measures: Check the catch clause table associated with the current method. Each Catch clause includes its valid instruction range, which can handle an exception type, and processing an exception code block address. · The CATCH clause that matches the abnormality should meet the following conditions: the abnormal instructions are within its instruction range, the abnormal type of abnormality is a subtype of its ability to process. If a matching catch clause is found, then the system shifts at the specified exception handling block; if the exception processing block is not found, repeat the process of matching the Catch clause, until all nested Catch clauses of the current method Have been checked. • Since the virtual machine continues from the first matching Catch clause, the order in the catch clause is important. Because the Java code is structured, there can always be arranged in a table in a table, and the value of any possible program counter can be used to find the appropriate exception handling in the order of linearity. Piece
To handle an abnormality that occurs under this program counter value. • If a matching catch clause is not found, the current method gets a "uncharacted exception" result and returns the caller of the current method, as if the exception has just happened in its caller. If there is still not found in the caller
The corresponding exception processing block, then this error propagation will continue. If the error is propagated to the top, then
The Universities will call a default exception handling block. (3) Operation counting machine command only takes an operand from the operating count stack, and operates them, and returns the result into the stack. The reason for selecting a stack structure is that the behavior of the virtual machine can also be efficiently simulated on only a small number of registers or non-universal registers (such as Intel486). The operator stack is 32-bit. It is used to transfer parameters to the method and receive the result from the method, also used to support the parameters of the operation, and save the results of the operation. For example, the IADD instruction adds two integers. The two integers that are added should be two words in the top of the operand. These two words are pressing the stack by the previous instruction. These two integers will pop up from the stack, add, and press the result back to the operation
In the stack. Each raw data type has a special instruction to perform a necessary operation. Each operand requires a storage location in the stack, in addition to the long and double types, they need two locations. The operand can only be operated for operators whose types. For example, it is illegal if it is pressed into the number of two int types, if they are used as a number of LONG types. In the virtual machine implementation of Sun, this limit is enforced by the bytecode verifier. However, there is a small number of operations (operators DUPE and SWAP), which are not considered when operating the runtime data area. 4. Unique unit Collection Heap of Java is a runtime data area, an instance of the class (object) allocates space. The Java language has a unused unit collection capability: it does not give programmers explicitly release the ability of the object. Java does not specify the unwanted unit collection algorithm for specific use
Various algorithms can be used depending on the needs of the system. 5. Method area method area is similar to the compiled code in the traditional language or the text segment in the UNIX process. It saves method code (compiled Java code) and symbolic table. In the current Java implementation, the method code is not included in the unused unit collection stack, but it is planned to be implemented in future versions. Each class file contains a Java class or a compiled code for a Java interface. It can be said that the class file is Java language.
Execute code file. In order to ensure the platform independence of the class file, the format of the class file in the Java virtual machine specification is also