Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Compiler work of Neo

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to realize the Compiler work of Neo". In the daily operation, I believe that many people have doubts about how to realize the Compiler work of Neo. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the Compiler work of Neo". Next, please follow the editor to study!

Compiler action

In the Neo block chain system, the intelligent contract is a piece of code, which can complete some logic, and finally calculate the result of the contract. Now there are many specific applications, if you are interested, you can take a look at the projects based on Neo.

If you don't know anything about Bitcoin or smart contracts and don't know why you need these codes, take a look at the Bitcoin white paper. To put it simply, in a Bitcoin system, when a "person" (which can be seen as a public key) needs to make a transaction with another "person", this code is used to check the identity and assign Bitcoin. For more information, you can take a look at the open course at Princeton.

Let's move on to the introduction to Neo compiler, and the basics required above are not covered in this article.

The framework of Compiler

Basic process

Neo can be written in a variety of languages, but now it is mainly C #.

The compiler of Neo is mainly a translator

C # code is compiled into MSIL by C # compiler. For more information on MSIL, please see Standard ECMA-335 Common Language Infrastructure (CLI).

Neo compiler uses Mono.Cecil to read IL

The Neo compiler only focuses on static function in C #, so it is only a super castrated version of the C # language.

The compiler of Neo traverses IL and converts it to opcode of Neo virtual machine according to semantics.

As for how to transfer MSIL to neo.vm 's opcode, we need to carefully study the design of neo.vm 's opcode.

A concrete example of Compiler work

Let's take a look at a piece of intelligent contract code.

This code has no practical effect, except that it returns aaccounb, but main can accept parameters.

Using Neo.SmartContract.Framework;using Neo.SmartContract.Framework.Services.Neo;public class Sum: SmartContract {public static int Main (int a, int b) {return a + b;} MSIL

Main function IL code

IL_0000 NopIL_0001 Ldarg_0IL_0002 Ldarg_1IL_0003 AddIL_0004 Stloc_0IL_0005 Br_SIL_0007 Ldloc_0IL_0008 Ret

This code is very simple, just read the parameter Ldarg_0,Add and return. You can see that the virtual machine of CLR is also a stack virtual machine.

Take a look at these articles about stack-based virtual machines and register-based virtual machines:

Stack virtual machine and register virtual machine?

In addition, there is a very detailed article on virtual machine talk (1): interpreter, tree traversal interpreter, stack-based and register-based, hodgepodge

Neo.compiler

To get a perceptual understanding of what the neo compiler has done, we can take a look at what the above contract can only be translated into

Hex:53-C5-6B-6C-76-6B 7A-C4 0052-7A-C4-6C-76-6BMur51527A-C4-61-6C-76-6B-00-C3-6C-76-6B-51-C3-93-6C-76-6BLY 5252-7A-C4-62-03-00-6C-76-6B-52-C3-61-6C-75-66

It's actually a string of numbers, and each number corresponds to an vm opcode or numeric value. For better understanding, put out the assembly code.

PUSH4PUSH3RETPUSH3NEWARRAYTOTALSTACKFROMALSTACKDUPTOALTSTACKPUSH0PUSH2ROLLSETITEMFROMALSTACKDUPTOTALSTACKPUSH1PUSH2ROLLSETITEMNOPFROMALSTACKDUPTOTALSTACKPUSH0PICKITEMFROMALSTACKDUPTOTALSTACKPUSH1PICKITEMADDFROMALSTACKDUPTOTALSTACKPUSH2PUSH2ROLLSETITEMJMPFROMALSTACKDUPTOTALSTACKPUSH2PICKITEMNOPFROMALSTACKDROPret

For instructions compiled by neo, you can view this document.

We can find the following:

The code of MSIL is very short, and the code of Neo.VM is very long, which is due to the different instructions and capabilities of the virtual machine. All we need to note is that the assembly code deals with the storage and acquisition of local variables, the passing of parameters, the exit of the program, and add instructions.

The assembly code is related to the generation algorithm of compiler, so we need to study the compiler and virtual machine of neo at the same time in order to understand the specific details.

For the specific meaning of each line and how it is executed, you can view this document

Later, there will be an article on virtual machines, which will be explained in detail at that time.

Neo.compiler Code Reading Guide

It's still a headache to read the code, so I made two brain maps:

Compiler executive brain map

Compiler object relationship

Object relation

ILModule is a mapping of MSIL, including modules, types, functions, fields, functions and return values, parameters, and function bodies, which can be viewed layer by layer by clicking on the brain map.

Mono.Cecil is used to read MSIL, it is also a mapping to MSIL, because there is no documentation, can only look at the code to know its class structure, this part is not shown in the brain map, but it does not matter, compiler will be interested in the code into ILModule

ModuleConverter is used to traverse ILModule, convert the MSIL inside into Neo.VM code, and store it in NeoModule.

How the specific instructions on both sides correspond, it really needs to be understood one by one, which is very tedious, and there are a lot of instructions on both sides. You can look at this code.

At this point, the study on "how to achieve the Compiler work of Neo" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report