NEO
$12.29 -1.56%
NEO · 1w

Compile AVM-Bytecode-Variables

Compile AVM-Bytecode-Variables

This article is written by Li Jianying(Light Li) in Chinese and translated into English by Robbie.

Reference source code: https://github.com/lightszero/neovmbook/tree/master/samples/compiler_il01

Now we discuss how to compile into another AVM from another assembly language. In fact, there is no difference.

Let’s take IL as an example. It is also a stack virtual machine. There are many similarities between IL instruction and AVM instruction.

Unlike AVM, IL still retains the modular structure of function level. IL does not connect to a large byte[], but each function corresponds to a byte[]. The call of IL is still at the function level, and each function corresponds to The IL instruction address starts from zero, and the IL jmp has been converted to the address.

Let us still use this 1+2 example

We compiled the above srccode into a c# dll, compiled in DEBUG mode, and its IL code should look like this.

Let’s explain them one by one.

nop is empty instruction

loc Is the PUSH of NEOVM

stloc puts the value in the variable list

ldloc takes the value from the variable list

add is same as NEOVM’s ADD

br is the JMP of NEOVM

ret is same as NEOVM’s RET

This br instruction is generated by DEBUG mode compilation. The RELEASE mode compilation will be optimized. The RELEASE mode will also generate many other optimization instructions. It is easy to explain. Let’s skip this br jump.

This br jump is a meaningless jump that jumps to the next instruction, ignoring it comes without any side effects.

Well, let’s re-organize it.

PUSH 1

STLOC 0

PUSH 2

STLOC 1

LDLOC 0

LDLOC 1

ADD

STLOC 2

LDLOC 2

RET

Think about the pseudo code that appeared in the previous article.

//int a=1

PUSH 1

STLOC 0

//int b=2

PUSH 2

STLOC...

Continue on medium.com
Recent news
No posts found