Byte code interpretation execution engine

Source: Internet
Author: User
Tags compact

How a virtual machine executes a bytecode directive in a method. The execution engine of many Java virtual machines has the option of interpreting execution (through the interpreter) and compiling (generating local code execution via an instant compiler) when executing Java code, and how the virtual machine execution engine works when interpreting execution.

Interpreting execution

Whether it's explaining or compiling, or whether it's a physical or virtual machine, for an application, the machine can't read, understand, and then get execution. Most of the program code will need to go through the steps in the diagram before the target code of the physical machine or the instruction set executed by the virtual function. It is very easy to find the following branch of the diagram, that is, the traditional compilation principle of the program code to the target machine code generation process, and the middle of the branch, the nature is to explain the implementation of the process. Today, the language of a physical machine, Java Virtual machine, or other high-level language virtual machine (HLLVM) Other than Java, mostly follow this idea based on the modern classic compiling principle, before executing the procedure source code for lexical analysis and parsing processing, Convert the source code into an abstract syntax tree (abstract Syntax tree,ast). For the implementation of a specific language, lexical analysis, syntax analysis, and the subsequent optimizer and target code generator can choose to be independent of the execution engine, to form a full-meaning compiler to implement, such representatives are C/s language. You can also choose to implement a subset of the steps (such as the steps preceding the generation of the abstract syntax tree) as a semi-independent compiler, which is the Java language. Or all of these steps and execution engines are encapsulated in a closed black box, such as most JavaScript actuators.

Stack-based instruction set and register-based instruction set

the Java compiler output the instruction stream, basically ( using "basically", because some bytecode instructions will have parameters, and the purely stack-based instruction set schema should be all 0 address instructions, that is, there is no explicit parameters.) Java such implementation is mainly to consider the code of the checksum is a stack-based instruction set schema (instruction set Architecture,isa), instructions in the flow of instructions are mostly 0 address instructions, They depend on the operand stack to work. The other set of commonly used instruction set architecture is a register-based instruction set, the most typical is the x86 two address instruction set, the most popular, is now our mainstream PC directly support the instruction set architecture, these instructions rely on registers to work. So what's the difference between a stack-based instruction set and a register-based instruction set?

For the simplest example, using the two instruction sets to calculate the result of "+", the stack-based command assembly is this:

Iconst_1

Iconst_1

Iadd

Istore_0

Two iconst_1 instruction continuously put two constant 1 into the stack, iadd instruction to stack the top two values stack, add, and then put the results back to the top of the stack, and finally istore_0 the value of the top of the stack into the No. 0 slot of the local variable table.

If you are based on a register, the program might look like this:

MOV eax,1

Add eax,1

The MOV instruction sets the value of the EAX register to 1, then the Add command adds 1 to the value, and the result is stored in the EAX register.

Knowing the difference between a stack-based instruction set and a register-based instruction set, who is better with these two sets of instructions?

It should be said that since the two sets of command assembly coexist and develop at the same time, it is certainly advantageous that if a set of instructions is better than the other, there will be no question of choice.

The main advantage of the stack-based instruction set is portability , the register is provided directly by the hardware , and the program relies on these hardware registers inevitably to be constrained by the hardware. For example, the 32-bit 80x86 system now has 8 32-bit registers in the processor, while the CPU of the arm system (a processor that is quite popular in the current phone and PDA) provides 16 32-bit general-purpose registers. If you use the instruction set of the stack schema, the user program does not directly use these registers, it can be implemented by the virtual machine to make some of the most frequently accessed data (program counters, stack top cache, etc.) into the register to obtain the best possible performance, so it is easier to implement. The instruction set of the stack architecture also has some other advantages, such as the relatively compact code (one instruction per byte in the bytecode, and the need to store parameters in the multi-address instruction set), the compiler implementation is simpler (no need to consider the problem of spatial allocation, the space required to operate on the stack) and so on. The main disadvantage of the stack schema instruction set is that execution speed is relatively slow. the instruction set for all major physical machines is the register architecture, which also confirms this from the side. Although the code of the stack schema instruction set is very compact, the number of instructions required to complete the same function is generally more than the register schema, because the stack and stack operations themselves produce a considerable number of instructions. More importantly, the stack is implemented in memory, and frequent stack accesses mean frequent memory accesses, and memory is always the bottleneck of execution speed relative to the processor. While virtual machines can take the top-of-the-stack cache to map the most common operations to registers to avoid direct memory access, this can only be an optimization rather than a solution to an essential problem. Because of the number of instructions and the reason for memory access, the execution speed of the stack schema instruction set is relatively slow.

Stack-based interpreter execution process

See how this is actually done in the virtual machine.

 Public int Calc () {    int a=100;    int b=200;    int c=300;     return(a+b) *c;}

From the Java language point of view, this code does not have any explanation necessary, you can use the JAVAP command directly to see its bytecode instructions

 Public int Calc (); Code:stack=2,locals=4,args_size=10:bipush 1002: Istore_13:sipush 2006: istore_2 7:sipush 30010: istore_3:iload_1: iload_2: Iadd 15 : Imul: Ireturn

JAVAP hints that this code requires a depth of 2 operand stack and a local variable space of 4 slots.

The execution of the above is only a conceptual model, the virtual machine will eventually do some optimization of the execution process to improve performance, the actual operation process does not necessarily fully conform to the conceptual model description ... More precisely, the actual situation will be very different from the conceptual model described above, which is due to the fact that both the parser and the instant compiler in the virtual machine optimize the input bytecode, for example, in a hotspot virtual machine, there are many non-standard bytecode instructions that start with "Fast_" for merging, Replace the input bytecode to improve interpretation execution performance, while the instant compiler's optimizations are more versatile.

Byte code interpretation execution engine

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.