One weekend, CPU and JVM chatted...
Brother, I heard that you are very popular recently
Brother laughed, how dare you dare
Dare you? I think you dare to make a set of virtual machines yourself, it almost emptied me
Brother, you see what you said, I am a Java virtual machine, and eventually I have to send the code instructions to you for execution. I can’t do without you at all, so how can I take you up?
I am very curious. Since I still have to execute instructions in the end, what is the meaning of your existence?
You don't know this. As a middleman, I provide programs with the best quality and convenient service. If you let them directly deal with you, you have to understand your instruction set, your I/O method, and your memory management method, which can be troublesome.
Don't fool me, what you said, the operating system is packaged, and the programs can be called directly. How can it be so troublesome?
。。。。。。
You are right, but there is a fatal problem, that is, the operating system cannot help.
what is the problem?
Cross-platform porting
Are you talking about porting to different operating systems?
Not only the operating system, but also may be ported to other architecture CPUs. In this way, the original program cannot be executed.
It is easy to ask questions, so what good solutions do you have?
My solution is not to compile the program code into instructions of your instruction set, but to generate an intermediate code defined by me. When running, I will be responsible for translating them into the corresponding CPU instructions under the current platform and handing them to your CPU for execution.
Good guy, you didn't say that you didn't take me empty, so that you, the middleman, can make the difference.
But there is no loss to you, anyway, you still can't avoid your level in the end.
I listen to you, if other languages can be compiled into your intermediate code, you can also execute it?
Well, in addition to Java, we can also execute Groovy, Scala, and yes, we can also execute Python. Isn’t this the same as you? No matter what high-level language it is, it will be compiled into your instruction set and you can execute it. Isn’t this a truth?
It's a bit 6. What is your middle code like? It is so powerful, how is it different from my instruction set?
It is very similar to your instruction set. The difference is that my "instructions" are not as long or short as your instructions. My unity is all one byte, so it is also called bytecode!
Are all one byte? Don't fool me, the memory address is more than one byte.
Really, I basically use zero-address instructions. The data to be used is on the top of the stack. Just operate directly.
All on the top of the stack? How to perform data operations without registers?
Who said there must be registers? I don't need registers, you are based on the register architecture, and I am based on the stack architecture. You can directly manipulate the data on the top of the stack during calculations.
Based on the stack? Are you saying that the data used in your instructions is placed on the stack? This stack is in memory, so if data keeps coming in and out, it will frequently access the memory. It must be slow?
It must be slower than you, but I also have an optimization method.
Any way, let me hear
Top-Of-Stack-CAching, TOSCA for short, is the top-of-stack cache. Although the data at the top of the stack is nominally at the top of the stack, but in actual implementation, I secretly put it in your register, which not only reduces the number of memory accesses, but also accesses it faster when you execute it. many.
Good guy, you guys have a set in front of you and a set behind your back.
You started talking about "translation". How do you translate and execute these bytecodes?
We have prepared corresponding assembly instructions for each bytecode to complete the behavior defined by this bytecode. Then integrate all the assembler instruction entries of the bytecode into one table, and I will dispatch them for distribution. These assembly instructions will be directly handed over to your CPU for execution.
I see, the key to your cross-platform is to generate corresponding assembly instructions on different platforms, right?
But this sounds a bit like interpretation and execution, which is similar to interpretation and execution of scripting languages.
The bottom layer of the scripting language you mentioned is executed in a "simulation" with a high-level language, and I directly translated the bytecode into assembly instructions, which is much faster than them.
No matter how fast it is, that is also interpretation execution
Well, it looks like a unique skill!
What?
JIT, just-in-time compilation, just-in-time compilation technology.
What is this? It looks amazing
In the process of dispatching and distributing, I find that there are frequently executed functions or code blocks. I will start the just-in-time compilation mechanism to directly compile this part of the code into instructions of your CPU. There is no need to use the bytecode dispatch table to execute later.
Frequently executed functions are easy to understand. What does the frequently executed code block mean?
For example, a loop body in a function is executed many times in a loop. Although the function may not be called many times, if the loop is executed a lot, it will also trigger my just-in-time compilation mechanism.
How do you determine whether it is frequent? Isn’t it intuitive?
Of course not. I don’t know how to count them frequently. This number is the counter. I will count the number of function calls and the number of executions of the loop body.
Good boy, there are two brushes, and I didn’t spend much time
I'm not a fool
Yeah! Stop talking, the programmer is turned on and is going to work
Isn't it weekend today? Why is he here?