Smart contract compilation method, device, terminal equipment and storage medium

By placing the stack variable corresponding to the IR instruction of the smart contract in a register and transferring it to the memory to provide an empty register, the problem of low execution efficiency caused by running out of registers in the existing technology is solved, and more efficient machine code execution is achieved.

CN118394351BActive Publication Date: 2024-09-27HANGZHOU HIGH-TECH ZONE (BINJIANG) INSTITUTE OF BLOCKCHAIN & DATA SECURITY
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410868701.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-01
Publication Date
2024-09-27
Estimated Expiration
2044-07-01

AI Technical Summary

Technical Problem

The machine code execution efficiency caused by the existing smart contract compilation method is low, mainly because the registers are quickly used up, so subsequent IR instructions can only be compiled according to the stack memory method, and the memory reading speed is slow, resulting in low execution efficiency.

Method used

By placing multiple stack variables corresponding to the IR instruction in registers for instruction compilation, and when there are no empty registers, the stack variables in the registers are transferred to the memory and converted into empty registers, thereby continuously providing empty registers for Use to improve the execution efficiency of machine code.

Benefits of technology

It improves the execution efficiency of machine code, because reading register data is faster than reading memory data, ensuring that empty registers are always available during the compilation process, avoiding the reduction in execution efficiency caused by insufficient registers.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118394351B
    Figure CN118394351B
Patent Text Reader

Abstract

The present application is applicable to the field of blockchain technology, and provides a compilation method, device, terminal device and storage medium for a smart contract, wherein the compilation method includes: according to multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in a register for instruction compilation to generate assembly instructions; one of the registers is selected as an empty register and one of the registers corresponding to the register stack element in the current compilation auxiliary stack, and the multiple IR instructions are obtained by compiling the bytecode of the smart contract to be compiled; the assembly instructions are compiled into corresponding machine codes; wherein, if one of the registers is selected as the empty register, the stack variables in another register are transferred to the memory to convert the other register into an empty register. Through the above scheme, it is possible to improve the execution efficiency of the machine code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of blockchain technology, and in particular, relates to a smart contract compilation method, apparatus, terminal device and storage medium. Background Art

[0002] Smart contracts are automated contracts based on blockchain technology that can execute contract terms without an intermediary and ensure the rights and interests of all parties to the contract. The compilation of smart contracts mainly converts the bytecode corresponding to the smart contract into machine code. However, the machine code obtained by the existing compilation method has a low execution efficiency, which needs to be solved urgently. Summary of the invention

[0003] The purpose of this application is to provide a smart contract compilation method, apparatus, terminal device and storage medium, aiming to improve the execution efficiency of machine code.

[0004] A first aspect of an embodiment of the present application provides a method for compiling a smart contract, the method comprising:

[0005] According to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions; one of the registers is selected as an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack, and the multiple IR instructions are obtained by compiling the bytecode of the smart contract to be compiled;

[0006] The assembly instruction is compiled into a corresponding machine code; wherein, if one of the registers is the empty register, the stack variables in the other register are transferred to the memory to convert the other register into an empty register.

[0007] In an optional embodiment, the IR instruction includes an arithmetic instruction, and correspondingly, placing multiple stack variables corresponding to each IR instruction in a corresponding register for instruction compilation according to the multiple IR instructions and the current compilation auxiliary stack to generate an assembly instruction includes:

[0008] According to the arithmetic instruction, a plurality of stack elements to be calculated are taken out from the current compilation auxiliary stack, and a calculation operation is performed on stack variables corresponding to the plurality of stack elements to be calculated to obtain a calculation result;

[0009] The operation result is stored in a corresponding register, a corresponding register stack element is generated, and the plurality of register stack elements are written to the top of a compilation auxiliary stack;

[0010] An assembly instruction is generated according to the operation and the register.

[0011] In an optional embodiment, the IR instruction includes an immediate value write instruction and an arithmetic instruction. Correspondingly, according to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions, including:

[0012] Storing the constant value corresponding to the immediate value write instruction into a plurality of constant stack elements, and writing the plurality of constant stack elements into the top of the compilation auxiliary stack;

[0013] According to the arithmetic instruction, stack variables corresponding to a plurality of stack elements to be calculated are taken out from the compilation auxiliary stack to perform calculation operations to obtain calculation results;

[0014] The operation result is stored in a corresponding register, a corresponding register stack element is generated, and the plurality of register stack elements are written to the top of a compilation auxiliary stack;

[0015] An assembly instruction is generated according to the operation and the register.

[0016] In an optional embodiment, the IR instruction includes an immediate value write instruction and an arithmetic instruction. Correspondingly, according to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions, including:

[0017] Storing the constant value corresponding to the immediate value write instruction into a plurality of register stack elements, and writing the plurality of register stack elements into the top of the compilation auxiliary stack;

[0018] According to the arithmetic instruction, stack variables corresponding to a plurality of stack elements to be calculated are taken out from the compilation auxiliary stack to perform calculation operations to obtain calculation results;

[0019] The operation result is stored in a corresponding register, a corresponding register stack element is generated, and the plurality of register stack elements are written to the top of a compilation auxiliary stack;

[0020] An assembly instruction is generated according to the operation and the register.

[0021] In an optional embodiment, taking out stack variables corresponding to a plurality of stack elements to be calculated from the compilation auxiliary stack according to the arithmetic instruction and performing a calculation operation to obtain a calculation result includes:

[0022] If the arithmetic instruction corresponds to an assembly instruction of an operand, taking out stack variables corresponding to a plurality of stack elements to be operated from the compilation auxiliary stack, performing a first operation, and obtaining an operation result;

[0023] If the arithmetic instruction corresponds to an assembly instruction of two operands, stack variables corresponding to a plurality of stack elements to be operated are taken out from the compilation auxiliary stack to perform a second operation to obtain an operation result.

[0024] In an optional embodiment, the arithmetic instruction corresponds to an assembly instruction of an operand, and correspondingly, storing the multiple operation results into a corresponding register includes:

[0025] If the stack element to be calculated is a constant stack element, apply for an empty register and store the calculation result in the empty register;

[0026] If the stack element to be calculated is a register stack element, storing the calculation result in the register corresponding to the register stack element;

[0027] If the stack element to be calculated is a memory stack element, an empty register is requested and the calculation result is stored in the empty register.

[0028] In an optional embodiment, the arithmetic instruction corresponds to an assembly instruction of two operands, and correspondingly, storing the multiple operation results into corresponding registers includes:

[0029] If both operands are register stack elements, the operation result is stored in a register corresponding to one of the register stack elements;

[0030] If one operand is a register stack element and the other operand is a constant stack element, the operation result is stored in the register corresponding to the register stack element;

[0031] If one operand is a register stack element and the other operand is a memory stack element, the operation result is stored in the register corresponding to the register stack element;

[0032] If both operands are memory stack elements or constant stack elements, an empty register is requested and the operation result is stored in the empty register.

[0033] A second aspect of an embodiment of the present application provides a smart contract compilation device, the smart contract compilation device comprising:

[0034] A first compiling module is used to place multiple stack variables corresponding to each IR instruction in a corresponding register for instruction compilation according to multiple IR instructions and a current compiling auxiliary stack, and generate an assembly instruction; one of the registers is an empty register and a register corresponding to a register stack element in the current compiling auxiliary stack, and the multiple IR instructions are obtained by compiling the bytecode of the smart contract to be compiled;

[0035] The second compiling module is used to compile the assembly instruction into a corresponding machine code; wherein, if one of the registers is the empty register, the stack variables in the other register are transferred to the memory to convert the other register into an empty register.

[0036] A third aspect of an embodiment of the present application provides a terminal device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method described in the first aspect when executing the computer program.

[0037] A fourth aspect of an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method described in the first aspect is implemented.

[0038] A fifth aspect of the embodiments of the present application provides a computer program product. When the computer program product is run on a terminal device, the terminal device executes the method described in the first aspect.

[0039] Beneficial effects of this application

[0040] The smart contract compilation method, apparatus, terminal device and storage medium provided in the present application place multiple stack variables corresponding to each IR instruction in a register for instruction compilation. In the case where there is no empty register, the present application transfers the stack variables in a register to the memory, so that the register is converted into an empty register, so that there are always empty registers for use during the compilation process. Since the speed of reading register data is faster than the speed of reading memory data, the execution efficiency of the machine code can be improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0042] Figure 1 One of the flow charts of a smart contract compilation method provided in an embodiment of the present application;

[0043] Figure 2 A second flow chart of a method for compiling a smart contract provided in an embodiment of the present application;

[0044] Figure 3 One of the structural diagrams of the compilation auxiliary stack provided in the embodiment of the present application;

[0045] Figure 4 A flowchart of a method for compiling a smart contract provided in an embodiment of the present application;

[0046] Figure 5 The second structural diagram of the compilation auxiliary stack provided in the embodiment of the present application;

[0047] Figure 6 A fourth flow chart of a method for compiling a smart contract provided in an embodiment of the present application;

[0048] Figure 7 The third structural diagram of the compilation auxiliary stack provided in the embodiment of the present application;

[0049] Figure 8 A schematic diagram of the structure of a smart contract compilation device provided in an embodiment of the present application;

[0050] Fig. 9 A schematic diagram of the structure of a terminal device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0051] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application may also be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present application.

[0052] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.

[0053] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0054] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.

[0055] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.

[0056] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.

[0057] It should be understood that the size of the serial numbers of the steps in this embodiment does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present application.

[0058] A smart contract is an automated contract based on blockchain technology that can execute contract terms without an intermediary and ensure the rights and interests of all parties to the contract. A smart contract can be seen as a piece of computer code written in a programming language, which is deployed on the blockchain and can automatically execute the contract rules defined therein. In a smart contract, the source code is compiled to generate bytecode, which is then executed by the Ethereum Virtual Machine (EVM). Bytecode is a series of hexadecimal values, each of which represents an opcode, and each opcode represents an instruction. The virtual machine executes the instructions in the bytecode one by one to realize the functions of the smart contract. In order to further optimize the execution efficiency, some blockchains will use compilers such as AOT and JIT to compile the contract bytecode into machine code for execution. Machine code is a machine language instruction, which is a binary instruction that can be directly executed by a computer. Machine code usually consists of two parts: an opcode and an address code. The opcode indicates the operation to be completed by the instruction, that is, the function of the instruction, and the address code indicates the object involved in the operation, as well as the location where the operation result is stored.

[0059] The existing machine code compilation process is as follows:

[0060] Step 1) The compiler parses the bytecode and compiles the contract bytecode into corresponding IR instructions;

[0061] Step 2) Convert IR instructions into assembly code instructions;

[0062] Step 3) Compile the assembly code instructions into machine code.

[0063] In the above step 2), when compiling IR instructions, it is necessary to query free registers. If there is an empty register, the register is bound to the IR instruction, and the value corresponding to the IR instruction is stored in the corresponding register. The subsequent compilation is carried out in the manner of operating the register. If there is no empty register, the value corresponding to the IR instruction is stored in the memory, and the subsequent compilation is carried out in the manner of stack memory. However, if there are many IR instructions, the registers will be bound by the first few IR instructions, resulting in no empty registers available when compiling other subsequent IR instructions, and they can only be compiled in the manner of stack memory. Since the speed of reading memory is slower than the speed of reading registers, the execution efficiency of the machine code compiled by this part is low. In addition, smart contracts include multiple languages, among which the operands of languages ​​such as Java, Rust, and C++ are usually 64 bits, and the operands of Solidity language are 256 bits. Taking Solidity language as an example, the number of bits of mainstream computers is 64 bits, and one operand needs to occupy 4 registers, which leads to the rapid use of empty registers, so that subsequent IR instructions can only be compiled in the manner of stack memory, which further leads to low execution efficiency of machine code.

[0064] In this regard, the present application provides a smart contract compilation method, apparatus, terminal device and storage medium, which place multiple stack variables corresponding to each IR instruction in a register for instruction compilation. In the case where there is no empty register, the present application transfers the stack variables in a register to the memory, so that the register is converted into an empty register, so that there are always empty registers for use during the compilation process. Since the speed of reading register data is faster than the speed of reading memory data, the execution efficiency of machine code can be improved.

[0065] In order to illustrate the technical solution of the present application, specific embodiments are provided below.

[0066] Figure 1 FIG. 1 shows one of the flow charts of a smart contract compilation method provided in an embodiment of the present application. Figure 1 , a smart contract compilation method provided in an embodiment of the present application includes the following steps:

[0067] Step S101: according to a plurality of IR instructions and the current compilation auxiliary stack, a plurality of stack variables corresponding to each IR instruction are placed in a corresponding register for instruction compilation to generate an assembly instruction; one of the registers is selected to be an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack, and the plurality of IR instructions are obtained by compiling the bytecode of the smart contract to be compiled;

[0068] Step S102: compile the assembly instruction into corresponding machine code; wherein, if one of the registers is the empty register, transfer the stack variables in the other register to the memory to convert the other register into an empty register.

[0069] It can be seen that in step S101 of this embodiment of the present application, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions, and then the assembly instructions are compiled in step S102 to obtain corresponding machine codes. The register in step S101 is selected to be one of an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack. If an empty register is selected, the stack variables in a used register are transferred to the memory, so that the register is converted into an empty register, so that there are always empty registers for use during the compilation process, thereby improving the execution efficiency of subsequent machine codes.

[0070] It should be noted that the register is a high-speed storage area within the CPU (central processing unit) that is used to temporarily store data and instructions. Compared with the main memory, the read and write speed is faster, allowing the CPU to process data and execute instructions more efficiently.

[0071] It should also be noted that, since the bytecode execution of smart contracts is based on a stack, a stack is needed to simulate the execution process during the compilation process. Therefore, this application sets up a compilation auxiliary stack, which stores the information of the elements in the stack when executing a certain instruction. This application sets the compilation auxiliary stack to include the following three types of stack elements: 1) Memory stack element: The value of the stack variable is stored in the specified memory, and the memory location information is recorded in the memory stack element; 2) Register stack element: The value of the stack variable is stored in the specified register, and the register name information is recorded in the register stack element; 3) Constant stack element: The value of the stack variable has not been stored anywhere, and the constant value of the stack variable is recorded in the constant stack element, so that the constant information can be directly written into the assembly instruction during the compilation stage. In this application, each IR instruction corresponds to multiple stack variables, and each stack variable corresponds to a stack element in the current compilation auxiliary stack. Therefore, each IR instruction corresponds to binding multiple stack elements in the current compilation stack.

[0072] In addition, it should be noted that the register in step S101 is selected to be one of an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack, which may specifically include the following situations:

[0073] 1) Select an empty register: If there is an idle empty register, put the empty register into use; if there is no idle empty register, transfer the stack variable in a used register to the memory, so that the register is converted into an empty register and put into use;

[0074] 2) Select the register corresponding to the register stack element in the current compilation auxiliary stack: If the IR instruction currently involved in the compilation is bound to a register stack element in the current compilation auxiliary stack, select the register corresponding to the register stack element and put it into use; if no register stack element is bound, select a register stack element in the current compilation auxiliary stack, transfer the stack variable in the register corresponding to the selected register stack element to the memory, and release the register before putting it into use.

[0075] Specifically, in an embodiment that can be implemented in the present application, the register in step S101 is selected as one of an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack. If an empty register is selected, a register that is not held by the current instruction and stack element, that is, an unused register, can be searched first and used as the empty register. If no unused register is found, a stack variable in a used register needs to be transferred to the memory to convert the register into an empty register.

[0076] In the above embodiment, it is necessary to select a used register to be converted into an empty register. Correspondingly, the selection of the used register can be specifically carried out in the following manner: 1) finding the first register stack element from the bottom of the current compilation auxiliary stack toward the top of the stack, and determining the register bound to the register stack element as the used register for subsequent conversion; 2) finding the first register stack element from the top of the current compilation auxiliary stack toward the bottom of the stack, and determining the register bound to the register stack element as the used register for subsequent conversion.

[0077] In another embodiment that can be implemented in the present application, the register in step S101 is selected as one of an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack. If the register corresponding to the register stack element in the current compilation auxiliary stack is selected, it is necessary to find out whether the IR instruction currently participating in the compilation is bound to a register stack element in the current compilation auxiliary stack. If so, the compiled result is stored in the register corresponding to the register stack element. If not, it is necessary to select a register stack element in the current compilation auxiliary stack, transfer the stack variable in the register corresponding to the selected register stack element to the memory, and release the register for the next step of use.

[0078] In the above embodiment, the register stack element is selected in the current compilation auxiliary stack, and specifically the following methods can be adopted: 1) randomly selecting a register stack element in the current compilation auxiliary stack; 2) searching from the bottom of the current compilation auxiliary stack toward the top of the stack, and selecting the first register stack element found; 3) searching from the top of the current compilation auxiliary stack toward the bottom of the stack, and selecting the first register stack element found.

[0079] The following is a detailed description of step S101.

[0080] The IR instructions in step S101 refer to an intermediate instruction set created by the compiler before converting the bytecode into machine code. IR instructions may include immediate value write instructions and arithmetic instructions. The compilation of IR instructions in this application is mainly for the above two types of IR instructions. Since the compilation methods corresponding to different instructions are different, they are described in detail below.

[0081] Figure 2 The second flowchart of a smart contract compilation method provided by the embodiment of the present application is shown. If the IR instruction in the embodiment of the present application includes an arithmetic instruction, correspondingly, step S101 specifically includes the following steps:

[0082] Step S201: according to the arithmetic instruction, taking out a plurality of stack elements to be calculated from the current compilation auxiliary stack, performing calculation operations on stack variables corresponding to the plurality of stack elements to be calculated, and obtaining calculation results;

[0083] Step S202: storing the operation result in a corresponding register, generating a corresponding register stack element, and writing the plurality of register stack elements into the top of the compilation auxiliary stack;

[0084] Step S203: Generate assembly instructions according to the calculation operation and the register.

[0085] In the above step S201, the stack elements that need to participate in the operation are taken out from the compilation auxiliary stack according to the arithmetic instruction, and the operation is performed to obtain the operation result. The operation result is stored in the corresponding register in step S202, so that subsequent operations can directly access the register without reading from other slow memories, in preparation for generating assembly instructions. In step S203, assembly instructions are generated according to the operation in step S202 and the register storing the operation result.

[0086] It should be noted that arithmetic instructions may be instructions such as ADD, SUB, MUL, etc. that perform operations based on stack elements. Usually, several operands are read from the stack, and after the operation, the operation results are written back to the top of the stack.

[0087] Taking the ADD instruction as an example, the current compilation auxiliary stack is as follows Figure 3 As shown, according to the ADD instruction, 8 elements to be calculated are taken out from the compilation auxiliary stack, and addition operations are performed on two of them to obtain 4 operation results, which are stored in 4 registers respectively, and the corresponding 4 register stack elements are generated and put back to the top of the stack. According to the operation and register, the assembly instruction is generated.

[0088] The above steps show the compilation process of arithmetic instructions in IR instructions. By using the compilation auxiliary stack and registers, arithmetic operations can be efficiently processed and optimized machine codes can be generated.

[0089] Figure 4 The third flowchart of a smart contract compilation method provided by the embodiment of the present application is shown. If the IR instruction in the embodiment of the present application includes an immediate value write instruction and an arithmetic instruction, correspondingly, step S101 specifically includes the following steps:

[0090] Step S301: storing the constant value corresponding to the immediate value write instruction into a plurality of constant stack elements, and writing the plurality of constant stack elements into the top of the compilation auxiliary stack;

[0091] Step S302: according to the arithmetic instruction, taking out stack variables corresponding to a plurality of stack elements to be calculated from the compilation auxiliary stack, performing calculation operations, and obtaining calculation results;

[0092] Step S303: storing the operation result in a corresponding register, generating a corresponding register stack element, and writing the plurality of register stack elements into the top of the compilation auxiliary stack;

[0093] Step S304: Generate assembly instructions according to the calculation operation and the register.

[0094] The above step S301 stores the constant value corresponding to the immediate write instruction into multiple constant stack elements, and writes it to the top of the compilation auxiliary stack so that subsequent instructions can access them. Step S302 takes out the stack elements that need to participate in the operation from the compilation auxiliary stack according to the arithmetic instruction, performs the operation to obtain the operation result, and stores the operation result in the corresponding register by step S303, so that subsequent operations can directly access the register without reading it from other slow memories, in preparation for generating assembly instructions. In step S304, assembly instructions are generated according to the operation in step S302 and the register storing the operation result in step S303.

[0095] Among them, the immediate value write instruction in EVM usually refers to the PUSH instruction, which writes an immediate constant value to the top of the stack.

[0096] For example, the current compilation auxiliary stack is as follows Figure 5 As shown, the PUSH instruction is first compiled. Since the immediate value after the PUSH instruction is known, which is 256 bits, and the number of bits of the stack elements of the compilation auxiliary stack is the same as the number of bits of the target machine code, usually 64 bits, the immediate constant value corresponding to the PUSH instruction is stored in four 64-bit constant stack elements, and the four constant stack elements are written to the top of the compilation auxiliary stack. The updated compilation auxiliary stack is as follows Figure 3Then compile the ADD instruction, take out 8 elements to be calculated from the compilation auxiliary stack, perform addition operations on them two by two, get 4 operation results, store them in 4 registers respectively, and generate assembly instructions according to the operation and registers.

[0097] Compared with the prior art that directly stores constant values ​​into registers, which causes the registers to be used up quickly, and only the instruction data can be stored in the memory, so that when the corresponding instruction data is used in the compilation process, it is necessary to read it from the memory, thereby causing the execution efficiency of the machine code to decrease. The above-mentioned embodiment of the present application improves the use efficiency of the register by first storing the constant value into a constant stack element, and then storing the calculation result into the register after calculation, thereby avoiding the shortage of available registers leading to the contention of other stack element registers. The corresponding value can be quickly read from the required register during compilation, thereby improving the execution efficiency of the machine code.

[0098] If the IR instruction in the embodiment of the present application includes an immediate value write instruction and an arithmetic instruction, Figure 6 The fourth flowchart of a smart contract compilation method provided in an embodiment of the present application is shown. Correspondingly, step S101 specifically includes the following steps:

[0099] Step S401: storing the constant value corresponding to the immediate value write instruction into a plurality of register stack elements, and writing the plurality of register stack elements into the top of the compilation auxiliary stack;

[0100] Step S402: according to the arithmetic instruction, taking out stack variables corresponding to a plurality of stack elements to be calculated from the compilation auxiliary stack, performing calculation operations, and obtaining calculation results;

[0101] Step S403: storing the operation result in a corresponding register, generating a corresponding register stack element, and writing the plurality of register stack elements into the top of the compilation auxiliary stack;

[0102] Step S404: Generate assembly instructions according to the calculation operation and the register.

[0103] The above step S401 stores the constant value corresponding to the immediate write instruction into multiple register stack elements, and writes it to the top of the compilation auxiliary stack so that subsequent instructions can access them. Step S402 takes out the stack elements that need to participate in the operation from the compilation auxiliary stack according to the arithmetic instruction, performs the operation to obtain the operation result, and stores the operation result in the corresponding register in step S403, so that subsequent operations can directly access the register without reading it from other slow memories, in preparation for generating assembly instructions. In step S404, assembly instructions are generated according to the operation in step S402 and the register storing the operation result in step S403.

[0104] Exemplarily, the PUSH instruction is first compiled, and the immediate constant value corresponding to the PUSH instruction is stored in four 64-bit register stack elements, and the four register stack elements are written to the top of the compilation auxiliary stack. Then the ADD instruction is compiled, and eight stack elements to be operated are taken out from the compilation auxiliary stack, and addition operations are performed on them in pairs to obtain four operation results, which are stored in four registers respectively. According to the operation and registers, an assembly instruction is generated.

[0105] It should be noted that the above-mentioned constant value corresponding to the immediate value writing instruction is directly stored in multiple register stack elements, so that the calculation result can be directly stored in the register corresponding to the register stack element without further searching for an empty register, thereby improving the instruction compilation efficiency.

[0106] In an embodiment that can be implemented in the present application, when compiling an IR instruction into an assembly instruction, an assembly instruction corresponding to the arithmetic instruction is selected in advance according to the arithmetic instruction, usually an assembly instruction with an operand of 1 or 2. Correspondingly, the above steps of taking out stack variables corresponding to multiple stack elements to be calculated from the compilation auxiliary stack to perform calculation operations and obtain multiple calculation results may specifically include the following steps:

[0107] Step S501: if the arithmetic instruction corresponds to an assembly instruction of an operand, multiple stack elements to be operated are taken out from the compilation auxiliary stack, and a first operation is performed on the corresponding stack variables to obtain an operation result;

[0108] Step S502: if the arithmetic instruction corresponds to an assembly instruction of two operands, multiple stack elements to be calculated are taken out from the compilation auxiliary stack, and a second calculation operation is performed on the corresponding stack variables to obtain a calculation result.

[0109] The first operation includes negation, increment, and decrement, and the second operation includes addition, subtraction, multiplication, and division.

[0110] For example, refer to Figure 3 A compilation auxiliary stack is used. If the arithmetic instruction corresponds to an assembly instruction of one operand, taking the negation operation as an example, four stack elements W1, W2, W3, and W4 to be calculated are taken out from the compilation auxiliary stack, and the stack variables corresponding to them are respectively negated to obtain four calculation results. If the arithmetic instruction corresponds to an assembly instruction of two operands, taking the addition operation as an example, eight stack elements to be calculated are taken out from the compilation auxiliary stack, and addition operations are performed on two of them to obtain four calculation results.

[0111] The above steps ensure that the corresponding operation can be accurately selected and executed according to the arithmetic instructions during the compilation process, thereby providing correct results and conditions for the subsequent assembly instruction generation.

[0112] In an embodiment that can be implemented in the present application, an arithmetic instruction is set to correspond to an assembly instruction of an operand, and correspondingly, the steps of storing the multiple operation results in the corresponding register in the above steps may specifically include the following steps:

[0113] Step S601: if the stack element to be calculated is a constant stack element, apply for an empty register and store the calculation result in the empty register;

[0114] Step S602: if the stack element to be calculated is a register stack element, storing the calculation result in the register corresponding to the register stack element;

[0115] Step S603: If the stack element to be calculated is a memory stack element, apply for an empty register and store the calculation result in the empty register.

[0116] In the above steps, if the selected stack element to be calculated is a register stack element, since the register stack element itself is bound to a register, the result can be stored in the register; if the selected stack element to be calculated is not a register stack element, but a constant stack element or a memory stack element, since neither the constant stack element nor the memory stack element is a register stack element, an empty register needs to be applied for to store the calculation result.

[0117] In this embodiment of the present application, the operation results are reasonably stored according to the types of stack elements to be operated, so as to optimize program execution efficiency and resource utilization.

[0118] In another embodiment that can be implemented in the present application, the arithmetic instruction is set to correspond to an assembly instruction of two operands. Correspondingly, the steps of storing the multiple operation results in the corresponding registers in the above steps may specifically include the following steps:

[0119] Step S701: if both operands are register stack elements, the operation result is stored in a register corresponding to one of the register stack elements;

[0120] Step S702: if one operand is a register stack element and the other operand is a constant stack element, the operation result is stored in the register corresponding to the register stack element;

[0121] Step S703: if one operand is a register stack element and the other operand is a memory stack element, the operation result is stored in the register corresponding to the register stack element;

[0122] Step S704: If both operands are memory stack elements or constant stack elements, apply for an empty register and store the operation result in the empty register.

[0123] Since registers are high-speed storage units inside the CPU, storing the calculation results directly in registers can speed up subsequent processing and reduce unnecessary memory accesses. Therefore, in the above steps, if one of the operands is a register stack element, the calculation result should be stored in the register bound to the register stack element. If both operands are not register stack elements, the calculation result should be stored in an empty register. This can avoid frequent data transfer between registers and memory and improve execution efficiency.

[0124] It should be noted that after the above embodiments store the calculation results into a register, it is necessary to create a register stack element corresponding to the register and bind it to the register, and then put the register stack element on the top of the compilation auxiliary stack to obtain an updated compilation auxiliary stack to facilitate subsequent instruction compilation.

[0125] In the embodiments that can be implemented in this application, when applying for an empty register in the above embodiments, the main application process is as follows:

[0126] First, check whether there is an empty register in the general register. If so, take the required empty register from the general register, mark it as used, and return; if there is no empty register, select a register stack element in the compilation auxiliary stack and grab its bound register. The process is as follows:

[0127] 1) Find a register stack element: Start from the bottom of the compiler auxiliary stack and search towards the top of the stack to get the first register stack element. Since the stack elements closer to the top of the stack will be used for calculation earlier, giving priority to register stack elements closer to the bottom of the stack can increase the frequency of register access.

[0128] 2) The compiler writes an instruction to transfer the value of the register bound to the register stack element to the specified memory, so that the register is converted into an empty register and the register stack element is converted into a memory stack element.

[0129] The above steps ensure that there are always free registers available during the compilation process, thereby improving the execution efficiency of the machine code.

[0130] In other embodiments that can be implemented in the present application, the IR instruction also includes a return instruction, which means ending the execution and returning the execution result of the contract. For the compiled machine code, all value records need to be saved in the memory for the blockchain process to obtain the execution result from the memory. When the return instruction is compiled, all constant stack elements and register stack elements in the current compilation auxiliary stack need to be converted into memory stack elements, that is, to ensure that all values ​​are saved in the memory.

[0131] Among them, for constant stack elements, compile an assembly instruction, write the constant value into the specified memory, and change the constant stack element into a memory stack element; for register stack elements, compile an assembly instruction, write the value of the target register into the specified memory, return the register, and change the register stack element into a memory stack element.

[0132] The following describes it in detail with reference to specific embodiments.

[0133] Taking the EVM contract as an example, this article introduces the compilation process under the amd64 architecture.

[0134] Set the IR instructions to be compiled to [..., PUSH, ADD, ...], and the state of the compilation auxiliary stack at this time is as follows Figure 5 shown.

[0135] When compiling the PUSH instruction, since the PUSH instruction is an immediate value write instruction and the immediate constant value after the PUSH instruction is known, the 256-bit number is split into four 64-bit numbers c1, c2, c3, c4 to form four constant stack elements W1, W2, W3, W4. Each constant stack element stores a part of the immediate constant value. The four constant stack elements are written to the top of the compilation auxiliary stack to form an updated compilation auxiliary stack, such as Figure 3 shown.

[0136] Then compile the next ADD instruction, select the assembly instruction of two operands, and correspondingly take out two 256-bit operands, that is, 8 stack elements, from the compilation auxiliary stack according to the ADD instruction, and obtain the first operand as [W4, W3, W2, W1] and the second operand as [V4, V3, V2, V1]. According to the ADD instruction, it is necessary to add the two operands from the low bit, store the operation results in 4 registers, and establish 4 register stack elements Z1, Z2, Z3, Z4 according to the name information of each register, and write the 4 register stack elements to the top of the compilation auxiliary stack to obtain the updated compilation auxiliary stack, as shown in FIG. Figure 7 shown.

[0137] Next, the operation process of the two operands is described in detail.

[0138] 1. Z1=V1+W1

[0139] Since neither V1 nor W1 is an element of the register stack, we first need to apply for an empty register R10; compile an assembly instruction to write the value of V1 memory to R10; and compile an assembly instruction to add W1 to R10 and record the result in R10.

[0140] The final compilation results in two assembly instructions:

[0141] MOV Memory(a) R10 / / Write the value of memory a to R10

[0142] ADD R10 c1 / / Add the value of constant c1 to R10 and save it in R10

[0143] 2. Z2=V2+W2+CARRY

[0144] Since neither V2 nor W2 is an element of the register stack, we first need to apply for an empty register R11; compile an assembly instruction to write the value of V2 memory to R11; compile an assembly instruction to add W2 to R11 and record the result in R11.

[0145] Finally, two assembly instructions are compiled:

[0146] MOV Memory(b) R11 / / Write the value of memory b to R11

[0147] ADC R11 c2 / / Add the value of constant c2 to R1 and the carry value, and save it in R11

[0148] 3. Z3=V3+W3+CARRY

[0149] Since V3 is a register stack element, we can directly operate on register R8 held by V3 and compile an assembly instruction that adds W3 to R8 and records the result in R8.

[0150] Finally compile an assembly instruction:

[0151] ADC R8 c3 / / Add the value of constant c3 to R8 and the carry value, and save them in R8

[0152] 4. Z4=V4+W4+CARRY

[0153] Since V4 is a register stack element, we can directly operate on register R9 held by V4 and compile an assembly instruction that adds W4 to R9 and records the result in R9.

[0154] Finally compile an assembly instruction:

[0155] ADC R9 c4 / / Add the value of constant c4 to R9 and the carry value, and save them in R9

[0156] After completing the above four groups of addition operations, the operation results are recorded in registers R9, R8, R11, and R10 respectively, and are bound to register stack elements Z1, Z2, Z3, and Z4 respectively. Finally, these four register stack elements are pushed to the top of the stack.

[0157] The assembly instructions obtained by the above compilation are combined to obtain an assembly instruction sequence, and the assembly instruction sequence is compiled into machine code for direct execution by a computer.

[0158] Figure 8 A schematic diagram of the structure of a smart contract compilation device provided in an embodiment of the present application. For ease of explanation, only the parts related to the embodiment of the present application are shown.

[0159] The smart contract compilation device may specifically include the following modules:

[0160] The first compiling module M1 is used to place multiple stack variables corresponding to each IR instruction in corresponding registers for instruction compilation according to multiple IR instructions and the current compiling auxiliary stack, and generate assembly instructions; one of the registers is an empty register and one of the registers corresponding to the register stack element in the current compiling auxiliary stack, and the multiple IR instructions are obtained by compiling the bytecode of the smart contract to be compiled;

[0161] The second compiling module M2 is used to compile the assembly instruction into a corresponding machine code; wherein, if one of the registers is the empty register, the stack variables in the other register are transferred to the memory to convert the other register into an empty register.

[0162] In the above, the first compiling module M1 places multiple stack variables corresponding to each IR instruction in corresponding registers for instruction compilation to generate assembly instructions, and then the second compiling module M2 compiles the assembly instructions to obtain corresponding machine codes. The register in the first compiling module M1 selects one of an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack. If the register is an empty register, the stack variables in a used register are transferred to the memory, so that the register is converted into an empty register, so that there are always empty registers for use during the compilation process, thereby improving the execution efficiency of subsequent machine codes.

[0163] Based on the same inventive concept, the first compiling module in the embodiment of the present application includes:

[0164] A first operation module, the first operation module is used to take out a plurality of to-be-calculated stack elements from the current compilation auxiliary stack according to the arithmetic instruction, perform operation operations on stack variables corresponding to the plurality of to-be-calculated stack elements, and obtain a plurality of operation results;

[0165] a second operation module, the second operation module being used to store the plurality of operation results into corresponding registers;

[0166] A third operation module is used to generate an assembly instruction according to the operation and the register.

[0167] Based on the same inventive concept, the first compiling module in the embodiment of the present application includes:

[0168] A fourth operation module, the fourth operation module is used to store the constant value corresponding to the immediate value write instruction into multiple constant stack elements, and write the multiple constant stack elements into the top of the compilation auxiliary stack;

[0169] a fifth operation module, the fifth operation module being used to fetch stack variables corresponding to a plurality of stack elements to be operated from the compilation auxiliary stack according to the arithmetic instruction, perform operation operations, and obtain a plurality of operation results;

[0170] a sixth operation module, the sixth operation module being used to store the plurality of operation results into corresponding registers;

[0171] A seventh operation module, wherein the seventh operation module is used to generate an assembly instruction according to the operation and the register.

[0172] Based on the same inventive concept, the first compiling module in the embodiment of the present application includes:

[0173] an eighth operation module, the eighth operation module being used to store the constant value corresponding to the immediate value write instruction into a plurality of register stack elements, and write the plurality of register stack elements into the top of the compilation auxiliary stack;

[0174] a ninth operation module, the ninth operation module being used to fetch stack variables corresponding to a plurality of stack elements to be operated from the compilation auxiliary stack according to the arithmetic instruction, perform operation operations, and obtain a plurality of operation results;

[0175] a tenth operation module, the tenth operation module being used to store the plurality of operation results into corresponding registers;

[0176] A generation module is used to generate assembly instructions according to the operation and the register.

[0177] Based on the same inventive concept, the first operating module, the fifth operating module and the ninth operating module in the embodiment of the present application include:

[0178] A first operation module, wherein the first operation module is used for taking out a plurality of stack elements to be operated from the compilation auxiliary stack, performing a negation operation on the corresponding stack variables, and obtaining a plurality of operation results if the arithmetic instruction corresponds to an assembly instruction of an operand;

[0179] The second operation module is used for taking out a plurality of stack elements to be operated from the compilation auxiliary stack, performing addition operations on corresponding stack variables, and obtaining a plurality of operation results if the arithmetic instruction corresponds to an assembly instruction of two operands.

[0180] Based on the same inventive concept, the first operation module in the embodiment of the present application includes:

[0181] A first storage module, wherein the first storage module is used to apply for an empty register if the stack element to be calculated is a constant stack element, and store the calculation result in the empty register;

[0182] a second storage module, the second storage module being configured to store the operation result in a register corresponding to the register stack element if the stack element to be operated is a register stack element;

[0183] The third storage module is used for applying for an empty register if the stack element to be calculated is a memory stack element, and storing the calculation result in the empty register.

[0184] Based on the same inventive concept, the second operation module in the embodiment of the present application includes:

[0185] a fourth storage module, wherein if both operands are register stack elements, the fourth storage module is used to store the operation result in a register corresponding to one of the register stack elements;

[0186] a fifth storage module, wherein if one operand is a register stack element and the other operand is a constant stack element, the operation result is stored in a register corresponding to the register stack element;

[0187] a sixth storage module, the sixth storage module being used for storing the operation result into a register corresponding to the register stack element if one operand is a register stack element and the other operand is a memory stack element;

[0188] The seventh storage module is used to apply for an empty register if both operands are memory stack elements or constant stack elements, and store the operation result in the empty register.

[0189] Fig. 9 A schematic diagram of the structure of a terminal device provided in an embodiment of the present application. The terminal device E1 includes: at least one processor E2 ( Fig. 9 Only one is shown in the figure) a processor, a memory E3, and a computer program E4 stored in the memory E3 and executable on the at least one processor E2, and when the processor E2 executes the computer program E4, the steps in the above method embodiment are implemented.

[0190] The terminal device E1 may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The terminal device may include, but is not limited to, a processor E2 and a memory E3. Those skilled in the art will understand that Fig. 9 This is only an example of the terminal device E1 and does not constitute a limitation on the terminal device E1. The terminal device E1 may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, it may also include input and output devices, network access devices, etc.

[0191] The processor E2 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0192] In some embodiments, the memory E3 may be an internal storage unit of the terminal device E1, such as a hard disk or memory of the terminal device E1. In other embodiments, the memory E3 may also be an external storage device of the terminal device E1, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the terminal device E1. Further, the memory E3 may also include both an internal storage unit and an external storage device of the terminal device E1. The memory E3 is used to store an operating system, an application program, a boot loader, data, and other programs, such as the program code of the computer program. The memory E3 may also be used to temporarily store data that has been output or is to be output.

[0193] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0194] In the above embodiments, the description of each embodiment has its own emphasis. If a rated part is not described or recorded in detail in a certain embodiment, reference may be made to the relevant description of other embodiments.

[0195] An embodiment of the present application provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the steps in the above-mentioned method embodiments can be implemented.

[0196] An embodiment of the present application provides a computer program product. When the computer program product runs on a mobile terminal, the mobile terminal can implement the steps in the above-mentioned method embodiments when executing the computer program product.

[0197] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0198] In the embodiments provided in the present application, it should be understood that the disclosed devices / network equipment and methods can be implemented in other ways. For example, the device / network equipment embodiments described above are merely schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0199] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0200] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above units may be implemented in the form of hardware or software.

[0201] If the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium. It should be noted that the content contained in the computer-readable medium can be appropriately increased or decreased according to the requirements of legislation and patent practice in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, computer-readable media do not include electric carrier signals and telecommunication signals.

[0202] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A method for compiling a smart contract, characterized in that: The compilation method comprises: According to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions; one of the registers is selected as an empty register and a register corresponding to a register stack element in the current compilation auxiliary stack, and the multiple IR instructions are obtained by compiling the bytecode of the smart contract to be compiled; Compile the assembly instruction into corresponding machine code; wherein, if one of the registers is an empty register, if there is an idle empty register currently, the idle empty register is selected, and if there is no idle empty register currently, the stack variable in the other register is transferred to the memory to convert the other register into an empty register; if one of the registers is a register corresponding to a register stack element in the current compilation auxiliary stack, if the IR instruction currently participating in the compilation is bound to a register stack element in the current compilation auxiliary stack, the register corresponding to the register stack element is selected, and if the IR instruction currently participating in the compilation is not bound to a register stack element in the current compilation auxiliary stack, a register stack element is selected in the current compilation auxiliary stack, the stack variable in the register corresponding to the register stack element is transferred to the memory, and the register corresponding to the register stack element is selected.

2. The method according to claim 1, characterized in that The IR instructions include arithmetic instructions. Correspondingly, according to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions, including: According to the arithmetic instruction, a plurality of stack elements to be calculated are taken out from the current compilation auxiliary stack, and a calculation operation is performed on the stack variables corresponding to the plurality of stack elements to be calculated to obtain a calculation result; The operation result is stored in a corresponding register, a corresponding register stack element is generated, and the register stack element is written to the top of the compilation auxiliary stack; An assembly instruction is generated according to the operation and the register.

3. The method according to claim 1, characterized in that The IR instructions include immediate value writing instructions and arithmetic instructions. Correspondingly, according to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions, including: Storing the constant value corresponding to the immediate value write instruction into a plurality of constant stack elements, and writing the plurality of constant stack elements into the top of the compilation auxiliary stack; According to the arithmetic instruction, taking out stack variables corresponding to a plurality of stack elements to be calculated from the compilation auxiliary stack to perform calculation operations and obtain calculation results; The operation result is stored in a corresponding register, a corresponding register stack element is generated, and the register stack element is written to the top of the compilation auxiliary stack; An assembly instruction is generated according to the operation and the register.

4. The method according to claim 1, characterized in that The IR instructions include immediate value writing instructions and arithmetic instructions. Correspondingly, according to the multiple IR instructions and the current compilation auxiliary stack, multiple stack variables corresponding to each IR instruction are placed in corresponding registers for instruction compilation to generate assembly instructions, including: Storing the constant value corresponding to the immediate value write instruction into a plurality of register stack elements, and writing the plurality of register stack elements into the top of the compilation auxiliary stack; According to the arithmetic instruction, taking out stack variables corresponding to a plurality of stack elements to be calculated from the compilation auxiliary stack to perform calculation operations and obtain calculation results; The operation result is stored in a corresponding register, a corresponding register stack element is generated, and the plurality of register stack elements are written to the top of a compilation auxiliary stack; An assembly instruction is generated according to the operation and the register.

5. The method according to claim 2, 3 or 4, characterized in that: The method of taking out stack variables corresponding to a plurality of stack elements to be calculated from the compilation auxiliary stack according to the arithmetic instruction and performing calculation operations to obtain calculation results includes: If the arithmetic instruction corresponds to an assembly instruction of an operand, taking out a plurality of stack elements to be operated from the compilation auxiliary stack, performing a first operation on the corresponding stack variables, and obtaining an operation result; If the arithmetic instruction corresponds to an assembly instruction of two operands, a plurality of stack elements to be operated are taken out from the compilation auxiliary stack, and a second operation is performed on the corresponding stack variables to obtain an operation result.

6. The method according to claim 5, characterized in that The arithmetic instruction corresponds to an assembly instruction of an operand, and correspondingly, storing the operation result in a corresponding register includes: If the stack element to be calculated is a constant stack element, apply for an empty register and store the calculation result in the empty register; If the stack element to be calculated is a register stack element, storing the calculation result in the register corresponding to the register stack element; If the stack element to be calculated is a memory stack element, an empty register is requested and the calculation result is stored in the empty register.

7. The method according to claim 5, characterized in that The arithmetic instruction corresponds to an assembly instruction of two operands, and correspondingly, storing the operation result in a corresponding register includes: If both operands are register stack elements, the operation result is stored in a register corresponding to one of the register stack elements; If one operand is a register stack element and the other operand is a constant stack element, the operation result is stored in the register corresponding to the register stack element; If one operand is a register stack element and the other operand is a memory stack element, the operation result is stored in the register corresponding to the register stack element; If both operands are memory stack elements or constant stack elements, an empty register is requested and the operation result is stored in the empty register.

8. A smart contract compilation device, characterized in that: The smart contract compilation device includes: A first compiling module is used to place multiple stack variables corresponding to each IR instruction in a corresponding register for instruction compilation according to multiple IR instructions and a current compiling auxiliary stack, and generate an assembly instruction; one of the registers is an empty register and a register corresponding to a register stack element in the current compiling auxiliary stack, and the multiple IR instructions are obtained by compiling the bytecode of the smart contract to be compiled; The second compilation module is used to compile the assembly instruction into corresponding machine code; wherein, if one of the registers is an empty register, if there is an idle empty register at present, the idle empty register is selected, and if there is no idle empty register at present, the stack variable in the other register is transferred to the memory to convert the other register into an empty register; if one of the registers is a register corresponding to a register stack element in the current compilation auxiliary stack, if the IR instruction currently participating in the compilation is bound to a register stack element in the current compilation auxiliary stack, the register corresponding to the register stack element is selected, and if the IR instruction currently participating in the compilation is not bound to a register stack element in the current compilation auxiliary stack, a register stack element is selected in the current compilation auxiliary stack, the stack variable in the register corresponding to the register stack element is transferred to the memory, and the register corresponding to the register stack element is selected.

9. A terminal device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 7 is implemented.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.