Java virtual machine

xiaoxiao2021-03-06  40

Java virtual machine, what is the Java Virtual Machine Java Virtual Machine is an imaginary machine that is implemented by software simulation on the actual computer. Java virtual machines have their own hardware, such as processors, stacks, registers, etc., have corresponding instruction systems. 1. Why use a very important feature of Java virtual machine Java language is that the platform 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. Java language usage mode Java virtual machine shielded information related to specific platforms, so that Java language compiler only needs to generate target code (bytecode) running on the Java virtual machine, you can 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 Java virtual machine is the basis for the underlying implementation of the Java language. People who are interested in Java should have a probably understanding of the Java virtual machine. 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 the Java language, or build other languages ​​into the Java language, you also need to learn more about Java virtual machines. 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 the IEEE format are not supported, which may be very slow when running a Java numerical calculation program. 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. As can be seen from the data type supported by Java Virtual Machines, Java strictly regulates the internal format of the data type, so that the implementation of various Java virtual machines is the same, thereby ensuring Java's and platform independence. 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 the Java virtual machine, which does not rely on any implementation techniques or organization, but their features must be implemented in a real machine. 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. Java Virtual Machines 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 the number of 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. The dynamic link translates the method of the symbol to the actual method call, load 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 skip the executed method call command, then continue execution in the caller's execution environment. Go down. · Exceptions and error disseminations in Java are called error (error) or Exception, which is the subclass of the Throwable class. The reason is: 1 dynamic link error, if you can't find the required 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 sub-sentence 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. Block to process an abnormality that occurs at the program counter value.

转载请注明原文地址:https://www.9cbs.com/read-52614.html

New Post(0)