Hello, everyone. I’m the hardcore guy who steals everyone’s job when he can live by his face. I want to stand in the Java circle of the most rolling position, small grandma with me, don’t fall behind.

This article is the third in a series called “Writing your JVM in Java,” which gives your JVM its own data types. The articles in this column are interlocking, so if you haven’t read the previous articles, you’re advised to do so, or you might not understand this one. The article is the whole network hair, some articles and some platforms do not know what reason did not pass, the complete column can be read in my public number [hard nuclear teeth].

Java currently has these data types: Boolean, byte, CHAR, int, Long, float, Double, oop. Let me outline these data types:

  1. These Java data types are provided by the JVM, which is written primarily in C++, which has no Boolean types. So how do booleans in Java come from? Most cases are defined using C++ syntax. Here’s the code. That’s why false is equal to 0 and true is equal to 1.

  1. There is no byte type in C++. Use char as byte in C++. Where does Java char come from? Use short as char in C++. I don’t really understand why the JVM is designed this way. So in Java, byte is one byte and char is two bytes. Here for learning C language, C++ partners must remember. I used char as a byte in the early days.
  2. This is an OOP type that some of you may not recognize. Oop refers to object Pointers. Every object in Java has a corresponding C++ object in the JVM, which is an oop object. If you are interested, you can watch my previous SERIES of videos on THE JVM.
  3. The JVM is still running on a 32-bit virtual stack due to legacy issues. This raises the question, how to store double and long? A cell in the virtual machine stack is called a Slot, 32 bits, 4B. So 8B is stored in two slots. But data is not stored in one place, it needs to flow. So it’s about combining and dividing data. Do you feel complicated and excited to know? Well, keep watching.

While the other data types are relatively simple to implement, I won’t go into detail. This article will focus on understanding the nature of double in the JVM and supporting double in our own JVM.

Course objectives

Again, the goal of this article is to run this Java program successfully on our own JVM. There isn’t much Java code, but it’s not that easy to implement.

Take a look at the bytecode instructions. Enabling our own JVM to run this program is essentially enabling our own JVM to process these bytecode instructions.

Let’s look at the results

Implementation details

Now that you have a target, it’s all about how to do it. To implement the Java code above, these are all places to add or write code:

  1. Constant pool parsing and storage
  2. Storage and retrieval of local variables
  3. Operand stack store and fetch
  4. When the value is

Let me elaborate on these four points.

Dealing with constant pools

Let’s see what the generated constant pool looks like with a double in the code, above

Doubles are also stored in two cells in the constant pool, in small-endian mode, with the highest in front and the lowest in rank.

Once the pattern is found, the bytecode parser is ready to write the logical code to parse the constant pool, as follows

When I implemented it, when I parsed the constant pool, I read 8 bytes at a time in small-endian mode, then converted it to a double and stored it in the container as an Object. Note here that Java implementations can do this, but C++ implementations can only be implemented as bytecode files.

Note that the following code is to split a double into two ints to support the operation of the program, so the order of the split into two ints should be paid attention to when writing the code. This is the most confusing part of implementing this feature. It is recommended to draw the following stack diagram under writing code to clear your mind.

Deal with the operand stack

Considering that many friends learn handwritten JVM with me in order to be able to better study Hotspot source code, so in the real run time, I still split a double into two int to operate. Take a look at the core code of the JVM executing the LDC2_W instruction and the post-execution operand stack

Deal with local variable scale

The JVM then executes dstore_1, which writes the value of the operand stack to the local variable table, reading two slots at a time. Look at the core code and the stack after execution

Notice that the operand stack is empty at this point

Processing parameters

Finally, the JVM executes Invokevirtual. As mentioned in the previous article, the JVM executes this instruction in two steps, the first by building the environment and the second by executing the bytecode instructions for the method. Building the environment involves taking parameters. Take a look at the core code. Take the two slots from the operand stack and merge them into a double. Because value1 is the last four bytes and value2 is the first four bytes, pushInt means value2 and value1.

That concludes the core of the nature of double and getting your JVM to handle double the way Hotspot source code does. The rest of the corners you can read through the handwritten JVM code I provide to appreciate. Focus on the public number [hard nuclear teeth] reply [handwritten JVM code] get code.

practice

1. Make your JVM support basic data types

2. Get your JVM moving to support computation

3. Extend the exercise: when getting your JVM to support longs, switch to Hotspot logic and work with two slots instead of using longs or 8-byte arrays

Column catalog

1. Introduction: The significance of this column course

2, implement bytecode file parser (not approved, interested, can go to my public number to see)

3, Build JVM framework, output Hello World

conclusion

In this world, we are not usually presented with right choices and wrong choices. It’s the right choice, and the easy choice. The right choice, usually is to pay first, as for the return, there is lag, or even no return. The easy choice, on the other hand, usually shows results quickly. In human terms, it’s easier to pick things that offer immediate rewards. Work and entrepreneurship are the best examples. A monthly salary for working; Entrepreneurship, on the other hand, can be a very long time, or a very long time, with no return. But we all know which is the right choice. And you know why you’re afraid to do the right thing.

The technical road is the same, oriented to salary learning, is a natural choice. But most people get lost or slack off when you learn enough to do your job. At this time, as a veteran, I advise you to fill in the bottom. It’s hard to imagine how much fun it can be without becoming an expert.

Work, is life, is to realize the boss’s dream. To be a Coder, don’t you have something you want to do? I’ll show you some of the low-level projects THAT I wrote

Volume, not others like volume, just because of their precipitation to that step, not handwritten JVM that learn what? I hope you can find your original intention to become a programmer through writing JVM.