Java virtual machine

zhaozj2021-02-16  59

First, what is Java Virtual Machine Java Virtual Machine is an imaginary machine, 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 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. Java virtual machine masks the information related to specific platforms, making the Java language compiler only to generate target code running on the Java virtual machine. (Byte), you can run in a variety of platforms to be modified. 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 the IADD, LADD, FADD, and DADD instructions plus two numbers, and its operand type, especially 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. Java instruction set Java virtual machine supports approximately 248 by node code. 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 flow is generally only byte alignment. The instruction TableSwitch and Lookup are exception, and the mandatory 4-byte boundary is required within these two instructions. Register Java Virtual Machine registers are 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. Stack of Java Virtual Machines has three regions: local variable area, running a environmental area, operating regions. The local variable area uses a fixed size local variable set for each Java method. 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 a 64-bit virtual machine that provides an instruction to the value in the local variable to the operator stack, and also provides instructions in writing a value in the operator stack. The information that the Running a Non-run environment is included in the dynamic link, the normal method returns and exception 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 method of call 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 dissemination is called Error or Exception in Java, which is the subclass of the Throwable class. The reason for errors in the program is: 1 Dynamic link error, if you can't find the required class file . 2 Running is wrong, such as the THROW statement using the reference program for an empty pointer. When an abnormality occurs, the Java virtual machine takes the following measures: Check the catch sub-sentence 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 comply with the following conditions: the abnormal instructions are within its instruction range, the abnormality type that occurs is a subtype of its ability to process an exception type. 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 proceeds 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. Block to process an abnormality that occurs at the program counter value.

If a matching catch clause is not found, the current method gets a "uncharacted exception" result and returns to the caller of the current method, as an exception just happens in its caller. If the corresponding exception is still found in the caller, the system will call a default exception handling block. Operation Count The machine command only takes the operand from the operator stack, and operates them and returns the result to 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 bits. 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, press the result back into the operator 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. The unique unit collection heap Java's heap is a runtime data area, an instance of the class (object) allocates space from it. 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 uses, which can use a wide variety of algorithms according to the needs of the system. The 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 the execution code file of the Java language. In order to ensure the platform independence of the class file, the format of the class file in the Java virtual machine specification has also been detailed. For details, please refer to Sun's Java Virtual Machine Specification. It can be said that the class file is the execution code file of the Java language. In order to ensure the platform independence of the class file, the format of the class file in the Java virtual machine specification has also been detailed. For details, please refer to Sun's Java virtual machine specification. Fourth, Java Chip Since the Java language is interpreted language, it is slow to run in a general general processor compared to compilation languages. In order to further promote Java applications, Sun has launched a Java chip. These chips can be said to be the hardware implementation of the Java virtual machine. Unlike the virtual machines composed of universal chips and interpreters, Java chips can perform bytecode directly, that is, bytecode is a Java chip's instruction set. This virtual machine implemented with hardware is of course much faster than the speed of the software simulation. SUN plans to launch a Java chip with three: Picojava, Mi-crojava and Ultrajava. Picojava is a small Java core chip with a silicon area of ​​only 25mm2. Sun is ready to transfer to other companies so that other companies can customize their Java chips, and their low-end products are estimated less than $ 25. Microjava is a Picojava-based microcontroller chip with a chip area of ​​approximately 50 mm2 for communication devices and other embedded control devices. Sun hopes that the Java chip can be one of the main technologies that drive Java development. Organize release:

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

New Post(0)