Lua1.1 print bytecode

Source: Internet
Author: User

How to print the bytecode:
Modify the Code as follows to enable the macro function for printing bytecode.
Y. Tab. c
18 rows
# Define listing 0
Change
# Define Listing 1

Because the definition of printcode is called, a predeclaration is added:
Make the following changes:
Y. Tab. c
329 rows
Add
Static void printcode (byte * code, byte * End );
Ensure that it has been declared when lua_parse calls it.

After opening the printed bytecode, it is printed out first when the script is executed.
Let's take a look at what the bytecode is like. Let's say hello, world.
--------------------------------
Print ("lua1.1 show bytecode ")
Print ("Hello, world ")
--------------------------------
Print the bytecode as follows:
Code
0 pushglobal 4
3 pushmark
4 pushstring 8
7 callfunc
9 adjust 0
10 pushglobal 4
13 pushmark
14 pushstring 9
17 callfunc
19 adjust 0
20 halt
Lua1.1 show bytecode
Hello, world
In a simple explanation, the last two lines are the execution results of the program, that is, two strings are output.
Bytecode is used until the end of "code" and "halt.
The relative offset of the leftmost label bytecode of each line, and the right side is the bytecode.
--------------------------------
Now, we can solve the $ debug problem mentioned in lua1.0.
The Code is the same as the above, except that "$ debug" is added to the first line ".
--------------------------------
$ Debug
Print ("lua1.1 show bytecode ")
Print ("Hello, world ")
--------------------------------
Print the bytecode as follows:
Code
0 setline 2
3 pushglobal 4
6 pushmark
7 pushstring 8
10 callfunc
12 adjust 0
13 setline 3
16 pushglobal 4
19 pushmark
20 pushstring 9
23 callfunc
25 adjust 0
26 halt
Lua1.1 show bytecode
Hello, world
We can see the difference by comparing the printed bytecode above. The following bytecode is 6 more bytes than above. We can see the following two more bytecode sentences,
0 setline 2
13 setline 3
The number next to this setline corresponds to the line number in the source code. In this way, it is easier to compare the bytecode and source code. Unless otherwise specified, we add "$ debug" to the header of each script to generate bytecode with source code line numbers.

After reading the bytecode, we can see some problems. Where are the strings like "hello, World" running? It cannot be reflected in bytecode.
In this version, you cannot directly execute the compiled bytecode. Every time you execute the script, you have to honestly compile and execute it from the Lua script source code. The reason is here, in the bytecode, data such as this string is lost. After the source code is converted to bytecode, the information carried by the bytecode itself is smaller than that carried by the source code. We know that when writing assembler programs, programs are generally divided into data segments and code segments. In bytecode, the data segment is missing. Where is the data? The data is compiled into bytecode in the source code and saved to the environment. In this case, the bytecode can be saved until lua2.4, and is directly executed by the interpreter. What is such a bytecode? Will I analyze it? There is no such plan yet. But maybe. We plan to analyze a lua4.0 before lua5.x, because it is the last version of the Lua stack-based virtual machine. Lua5.0 becomes a register-type virtual machine.

I have also tried to enable the function of printing bytecode in lua1.0, but the effect is poor. Many of the examples that come with it seem like an endless loop, and there are a lot of empty commands (NOP, this is an instruction in lua1.0. The intention of introducing a blank instruction is to fill in the system architecture that requires byte alignment. In fact, it seems useless. This command is no longer available in lua1.1 .) And output.

Lua1.1 print bytecode

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.