Code compilation method, device and electronic equipment

The compiler optimization generates assembly code that does not include registers to the stack, which solves the problem that local variables occupy a large amount of stack space, and improves the function execution efficiency and code compilation flexibility.

CN114879975BActive Publication Date: 2025-08-19NEW H3C SEMICON TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210681420.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-06-15
Publication Date
2025-08-19
Estimated Expiration
2042-06-15

AI Technical Summary

Technical Problem

Frequent use of local variables in functions causes registers to be put into the stack, occupying a large amount of stack space, resulting in insufficient high-speed memory, affecting the execution efficiency of function, and reducing efficiency when using external low-speed memory as stack space.

Method used

Through compiler optimization, assembly code that does not contain registers to stack is generated, preset target attributes are used to identify functions that do not need to restore register data, reduce the stack entry operation before local variables are written to registers, and reasonably arrange the use of stack space.

Benefits of technology

It effectively reduces the amount of data written to the stack, reduces the use of stack space, avoids external low-speed memory expansion, and improves function execution efficiency and code compilation flexibility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114879975B_ABST
    Figure CN114879975B_ABST
Patent Text Reader

Abstract

An embodiment of the present invention provides a code compilation method, device, and electronic device. The method includes: obtaining high-level language code written in a high-level language for implementing a target function; if the function attributes of the target function include a preset target attribute, compiling the high-level language code into a first assembly code for implementing the target function, wherein the first assembly code does not include code for controlling the stacking of a target register, and the target register is a register specified in the first assembly code for storing local variables in the target function. This effectively reduces the amount of data that needs to be written to the stack, reduces stack space usage, avoids the use of external low-speed memory to expand stack space, and improves the execution efficiency of functions during program execution.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a code compiling method, device and electronic equipment. Background Art

[0002] Functions often declare one or more local variables, and these variables are frequently used during function execution. Therefore, to improve function execution efficiency, related art stores local variables in registers. However, registers may already contain other data before storing local variables. Storing local variables in registers can corrupt this data, preventing the program to which the data belongs from executing properly. Therefore, related art stores the data already stored in registers onto a stack before storing local variables in registers. This process is called pushing.

[0003] In application scenarios with a large number of local variables, the number of registers that need to be pushed onto the stack is also large, which will use a large amount of stack space. As a result, the high-speed internal memory used as the stack space is insufficient, and the low-speed external low-speed memory is used as the stack space to continue pushing data onto the stack. Using the external low-speed memory as the stack space will reduce the efficiency of pushing and popping data when the program is running, thereby reducing the execution efficiency of the function. Summary of the Invention

[0004] The purpose of the embodiments of the present invention is to provide a code compilation method, device, and electronic device to improve the execution efficiency of functions. The specific technical solution is as follows:

[0005] A first aspect of the present invention provides a code compilation method, the method comprising:

[0006] obtaining a high-level language code written in a high-level language for implementing a target function;

[0007] If the function attributes of the target function include preset target attributes, the high-level language code is compiled into a first assembly code for implementing the target function, wherein the first assembly code does not include code for controlling the target register to be pushed onto the stack, and the target register is a register agreed in the first assembly code to store local variables in the target function.

[0008] In a possible embodiment, the method further includes:

[0009] Obtaining an initial mask, where the initial mask is used to identify the first register, wherein the first register is a register in the target register that is determined to be pushed onto the stack;

[0010] Modifying, according to the initial mask, code corresponding to the first register in an initial backend of the compiler to obtain a recompiled backend of the compiler, wherein the assembly code compiled by the recompiled backend does not include code for controlling stacking of the first register;

[0011] Compiling the high-level language code into a first assembly code for implementing the target function comprises:

[0012] The high-level language code is compiled using a compiler including the recompilation backend to obtain a first assembly code.

[0013] In a possible embodiment, the method further includes:

[0014] The number of the first registers in the target registers is determined according to the initial mask, so that the compiler adjusts allocation of stack space according to the number of the first registers.

[0015] In a possible embodiment, compiling the high-level language code with a compiler including the recompilation backend to obtain the first assembly code includes:

[0016] Converting the high-level language code into an intermediate representation code written in a preset intermediate representation language;

[0017] The intermediate representation code is compiled using the recompilation backend to obtain a first assembly code.

[0018] In a possible embodiment, the method further includes:

[0019] If the function attributes of the target function do not include preset target attributes, the high-level language code is compiled into a second assembly code for implementing the target function, wherein the second assembly code includes a code for controlling stacking of the target register.

[0020] A second aspect of the present invention provides a code compilation device, the device comprising:

[0021] A code acquisition module, used for acquiring high-level language codes written in a high-level language and used for realizing a target function;

[0022] A code compilation module is used to compile the high-level language code into a first assembly code for implementing the target function if the function attributes of the target function include preset target attributes, wherein the first assembly code does not include code for controlling the push of a target register, and the target register is a register agreed in the first assembly code to store local variables in the target function.

[0023] In a possible embodiment, the device further includes:

[0024] a mask acquisition module, configured to acquire an initial mask, wherein the initial mask is used to identify the first register, wherein the first register is a register in the target register that is determined to be pushed onto the stack;

[0025] a recompilation backend module, configured to modify, according to the initial mask, the code corresponding to the first register in the initial backend of the compiler to obtain a recompilation backend of the compiler, wherein the assembly code compiled by the recompilation backend does not include a code for controlling stacking of the first register;

[0026] The code compilation module is specifically used to compile the high-level language code using a compiler including the recompilation backend to obtain a first assembly code.

[0027] In a possible embodiment, the device further includes:

[0028] A space determination module is configured to determine the number of the first registers in the target registers according to the initial mask, so that the compiler adjusts allocation of stack space according to the number of the first registers.

[0029] In a possible embodiment, the code compilation module includes:

[0030] An intermediate representation acquisition submodule, configured to convert the high-level language code into an intermediate representation code written in a preset intermediate representation language;

[0031] The code compiling submodule is used to compile the intermediate representation code using the recompilation backend to obtain a first assembly code.

[0032] In a possible embodiment, the device further includes:

[0033] The second code compilation module is used to compile the high-level language code into a second assembly code for implementing the target function if the function attributes of the target function do not include a preset target attribute, wherein the second assembly code includes a code for controlling the stacking of the target register.

[0034] A third aspect of the present invention provides an electronic device, characterized in that it includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;

[0035] Memory for storing computer programs;

[0036] The processor is configured to implement any of the method steps described in the first aspect when executing a program stored in the memory.

[0037] A fourth aspect of the present invention provides a computer-readable storage medium, characterized in that a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, any method step described in the first aspect is implemented.

[0038] Beneficial effects of the embodiments of the present invention:

[0039] The code compilation method, device and electronic device provided by the embodiments of the present invention can determine whether the target function needs to restore the data originally stored in the register based on the function attributes of the target function. When the function attributes of the target function include preset target attributes, the high-level language code is compiled into a first assembly code. Since the first assembly code does not include code for controlling the pushing of the target register into the stack, and the target register is the register agreed in the first assembly code to store local variables in the target function, in the process of implementing the target function by executing the first assembly code, the register will not be pushed into the stack before the local variables are written to the register, which effectively reduces the amount of data that needs to be written to the stack, reduces the use of stack space, avoids the use of external low-speed memory to expand the stack space, and improves the execution efficiency of the function during program running.

[0040] Of course, it is not necessary to achieve all of the advantages described above simultaneously in order to implement any product or method of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other embodiments can also be obtained based on these drawings.

[0042] Figure 1 A schematic diagram of a flow chart of a code compilation method provided by an embodiment of the present invention;

[0043] Figure 2 Another flowchart of the code compilation method provided by the embodiment of the present invention;

[0044] Figure 3 Another flowchart of the code compilation method provided by the embodiment of the present invention;

[0045] Figure 4 Another flowchart of the code compilation method provided by the embodiment of the present invention;

[0046] Figure 5 A schematic diagram of the structure of a code compiling device provided by an embodiment of the present invention;

[0047] Figure 6A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0048] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field based on the present invention are within the scope of protection of the present invention.

[0049] Usually in the process of processing network data packets, in order to improve the processing speed, NP (Network Processor) chips usually use thousands of hardware threads to process network data packets in parallel. However, the area of the chip is limited. When the number of processors increases, it is inevitable to use other optimization solutions to balance performance and chip area. For example, in terms of memory, it will be considered to reduce the capacity of the high-speed memory inside the chip and expand the capacity of the external memory to meet the small chip area and huge memory space. In the arrangement of stack space, the high-speed memory inside the chip is used first. When the internal high-speed memory is exhausted, the external low-speed memory is used to expand the stack space. However, the access speed of the external memory is much lower than that of the internal memory. Using the external memory as stack space will greatly affect the processing speed of the network data packet. Therefore, to improve data processing efficiency, it is necessary to reduce the use of stack space to avoid exhausting the internal high-speed memory due to excessive use of stack space. Pushing registers to the stack requires a certain amount of stack space, so it is necessary to minimize the pushing of registers to the stack. Based on this, the present invention provides a code compilation method, such as Figure 1 The method comprises:

[0050] S101, obtaining a high-level language code written in a high-level language for implementing a target function.

[0051] S102: If the function attributes of the target function include preset target attributes, compile the high-level language code into a first assembly code for implementing the target function.

[0052] By selecting this embodiment, it is possible to determine whether the data originally stored in the register needs to be restored in the return phase of the target function based on the function attributes of the target function. When the function attributes of the target function include preset target attributes, it is considered that the target function does not need to restore the data originally stored in the target register in the return phase. Therefore, in the process of executing the target function, the register is pushed onto the stack before the stored data of the local variable written to the register is overwritten. Therefore, the high-level language code can be compiled into a first assembly code. Since the first assembly code does not include the code for controlling the pushing of the target register, and the target register is the register agreed in the first assembly code to store local variables in the target function, in the process of implementing the target function by executing the first assembly code, the register will not be pushed onto the stack before the local variable is written to the register, which effectively reduces the amount of data that needs to be written to the stack, reduces the use of stack space, avoids the use of external low-speed memory to expand the stack space, and improves the execution efficiency of the function during program running.

[0053] On the other hand, the first assembly code in this embodiment is compiled from high-level language code. That is, based on the function attributes of the target function, this embodiment can automatically and appropriately arrange the target function's use of stack space through the assembly code. This allows developers to appropriately arrange the program's use of stack space without having to master assembly language. They only need to appropriately set function attributes for each function in the program developed in the high-level language, effectively reducing the development difficulty.

[0054] Among them, in S101, a high-level language is a machine-independent, process-oriented or object-oriented language, that is, a high-level language cannot be directly recognized by a machine, and the code written in a high-level language needs to be compiled by a compiler before it can be executed by the machine. A high-level language is a language that is closer to natural language and mathematical formulas, and is written in a way that is easier for people to understand. For example, Java (a computer programming language), C language, C++ language, etc. are all high-level languages. Programs written in high-level languages are usually called source code, that is, high-level language codes used to implement the target function in the present invention. Depending on the high-level language used, the high-level language codes used to implement the same target function will also be different.

[0055] In S102 , the first assembly code does not include code for controlling the target register to be pushed onto the stack, and the target register is a register agreed in the first assembly code to store local variables in the target function.

[0056] Assembly code, as opposed to high-level language code, is code written in assembly language. Assembly language is any low-level language directly readable by a computer, microprocessor, microcontroller, or other programmable device. Assembly language provides instructions that allow for flexible use of registers and stack space, whereas high-level languages lack such instructions. Therefore, the obtained high-level language code is compiled into a first assembly code that implements the same target function, allowing for the use of stack space and registers to be arranged in the first assembly code.

[0057] Registers are small storage areas within the CPU (Central Processing Unit) used to store data. They are used to temporarily store data involved in calculations and / or calculation results. Local variables are variables within a function or compound statement, and their scope is the function or compound statement that defines the variable. The lifetime of a local variable is calculated from the moment the function is called to the end of the function's return phase. Because register access speed is much higher than memory access speed, local variables are stored in registers first. In this article, the registers used to store local variables in the target function are referred to as target registers.

[0058] Each function has its corresponding attributes. For example, a function in a program developed with javascript (a computer programming language) has a length attribute and a name attribute. The preset target attribute in the embodiment of the present invention is used to identify that the function does not need to restore the data originally stored in the register during the return phase, that is, the register does not need to be pushed onto the stack before the local variables of the function are stored in the register. The preset target attribute is pre-defined in the architecture corresponding to the target function. For example, the attributes of the function of the ARM (Advanced RISC Machines) architecture are defined in the gcc / config / arm directory, while the attributes of the function of the ARC architecture are defined in the gcc / config / arc directory. As an example, the attributes of the function of the ARC architecture are defined in the arc_attribute_table array of the arc.c file. The array has already defined several attributes of functions specific to the ARC (a processor) architecture. To add a preset target attribute, you only need to add a line to the array. The sample code is as follows:

[0059]

[0060] Among them, "{"no_call_saved",……}" is a line of code added to the array, and the name of the preset target attribute in this example is no_call_saved. It can be understood that the above code is just an example, in which the name of the preset target attribute can be defined by oneself and is not limited to "no_call_saved", as long as the name of the preset target attribute is not the same as the name of the pre-defined attribute. Based on this, for functions that do not need to restore the data originally stored in the register in the return stage, the preset target attribute can be used to identify them, so that in the subsequent compilation process, the preset target attribute can be used to determine whether to optimize the stack space and registers of the target function, that is, to determine whether to compile the target function into the first assembly code.

[0061] Specifically, determining whether the function attributes of the target function include the preset target attributes can be determined during execution by an execution subject (e.g., a computer, mobile phone, or other electronic device) of the code compilation method provided by an embodiment of the present invention. If the preset target attributes are included, the execution subject compiles the high-level language code into a first assembly code for implementing the target function. In one possible embodiment, if a compiler detects that the function attributes of the target function include the preset target attributes, the compiler compiles the high-level language code into the first assembly code for implementing the target function.

[0062] If the compiler detects that the function attributes of the target function include a preset target attribute, such as the aforementioned target attribute being "no_call_saved" as an example, then if the compiler detects the function attribute "no_call_saved", the high-level language code that implements the target function is compiled into a first assembly code, wherein the obtained first assembly code does not include code for controlling the target register to be pushed onto the stack, so that the target register is not pushed onto the stack during the execution of the high-level language code.

[0063] Still taking the name of the preset target attribute "no_call_saved" as an example, when detecting whether the function attributes of the target function include the preset target attribute, a function for determining whether the target function has the preset target attribute can be added to the compiler. As mentioned above, taking the name of the preset target attribute "no_call_saved" as an example, a corresponding function "arc_no_call_save_p" can be added to the compiler to detect whether the function attributes of the target function have the "no_call_saved" attribute. The code example is as follows:

[0064]

[0065] The arc_no_call_save_p function's parameter, fun, is the target function. It can be expressed in high-level language code or in an intermediate representation (IR). The IR is the representation of the target function after the high-level language code has been optimized by the compiler's front-end and intermediate stages. When executed, the line tree attr = TYPE_ATTRIBUTES(TREE_TYPE(decl)) extracts the target function's attribute definition from this IR. It's understood that a compiled target function can have multiple attributes. The lookup_attribute("no_call_saved",attr) function searches for the "no_call_saved" attribute in the compiled target function. If not, the compiler returns NULL_TREE, arc_no_call_save_p returns false, and the compiler does not subsequently optimize the number of registers pushed onto the stack for the target function. That is, the high-level language code corresponding to the target function is not compiled into the first assembly code. If there is a "no_call_saved" attribute, it returns true, and the compiler needs to further optimize the target function, that is, compile the high-level language code corresponding to the target function into the first assembly code.

[0066] By selecting this embodiment, the preset target attributes of the target function can be efficiently detected and compiled through the compiler, thereby improving the compilation efficiency of the code compilation method.

[0067] In the application scenario of network packet processing, the network packet processing function is usually an infinite loop that runs in parallel on thousands of hardware threads. This function usually does not return and has the following form:

[0068]

[0069] As mentioned above, the purpose of pushing the target register onto the stack is to restore the target register using the previously pushed value when the function returns, thereby ensuring that the program can run normally after the function returns. Since the network packet processing function does not need to return, there is no need to push the target register onto the stack. Therefore, by selecting the above embodiment, a preset target attribute can be added to the network packet processing function, allowing the compiler to recognize the preset target attribute, and when allocating stack space, there is no need to push the target register used by the network packet processing function to store local variables onto the stack, thereby reducing the use of stack space. For example, if there are 13 target registers that do not need to be pushed onto the stack, each target register is 32 bits wide, and there are 4096 hardware threads, then each hardware thread can save 32 / 8×13=52 bytes of stack space. The internal memory stack space of each hardware thread is generally relatively small, usually 256 bytes, so 20% of the internal memory stack space can be saved.

[0070] Therefore, by selecting an embodiment of the present invention, preset target attributes can be directly set for functions that do not need to return (such as network data packet processing functions) based on needs and the experience of technical personnel in this field to reduce their stack space usage, thereby improving data processing efficiency and improving the flexibility and applicability of code compilation.

[0071] Moreover, the use of assembly code allows for free arrangement of registers and stack space. Therefore, when faced with the need to optimize registers and stack space for a target function, the related art generally requires that the target function be written and maintained in assembly language, and then the target function is compiled using a corresponding compiler. For example, when writing a target function in assembly language, the target function is marked with the naked extension attribute of GCC (GNU Compiler Collection, GNU is an operating system), and the relevant code for register stacking will not be automatically generated during subsequent compilation. However, compared to complex functions such as network packet processing functions, it would be very difficult to directly use assembly language for function writing and maintenance. Therefore, the solutions in the related art cannot be applied to complex functions.

[0072] By selecting the above-mentioned embodiment of the present invention, complex functions can be written directly in a high-level language. The present invention identifies whether the target function needs to optimize register stacking by adding preset target attributes to the function attributes of the complex function, and optimizes it during the compilation process, thereby avoiding the difficulties in writing and maintaining complex functions caused by writing them in assembly language, and improving the applicability of code compilation.

[0073] In a possible embodiment, if the function attributes of the target function do not include preset target attributes, the high-level language code is compiled into a second assembly code for implementing the target function, wherein the second assembly code includes code for controlling pushing of the target register into the stack.

[0074] It can be understood that if the function attributes of the target function do not include the preset target attributes, it means that the target function needs to restore the data originally stored in the target register in the return stage. Therefore, when compiling the high-level language code corresponding to the target function, there is no need to adjust some target registers that were originally pushed onto the stack to not be pushed onto the stack, that is, the stacking status of the target registers of the target function is not adjusted. Therefore, the second assembly code compiled will include code that controls the pushing of the target registers onto the stack.

[0075] By selecting this embodiment, whether the target function needs to optimize the number of target registers pushed onto the stack is determined based on the function attributes of the target function. When the function attributes of the target function do not include preset target attributes, the stacking status of the target registers of the target function is not adjusted during the compilation of the high-level language code. Different compilation methods can be used for the target function according to needs and actual conditions, thereby improving the flexibility of the code compilation method.

[0076] Specifically, a compiler can be used to compile high-level language codes, but there is no solution in the related art to optimize the number of registers pushed into the stack of the target function corresponding to the high-level language code during the compilation process. Based on this, the present disclosure also provides a code compilation method, such as Figure 2 As shown, the method includes:

[0077] S201, obtaining an initial mask.

[0078] S202 : Modify the code corresponding to the first register in the initial backend of the compiler according to the initial mask to obtain a recompiled backend of the compiler.

[0079] S203: Obtain high-level language code written in a high-level language for implementing the target function.

[0080] Among them, S203 is the same as the aforementioned S101. For details, please refer to the relevant description in S101 and will not be repeated here.

[0081] S204: If the function attributes of the target function include preset target attributes, compile the high-level language code using a compiler including a recompilation backend to obtain a first assembly code.

[0082] Among them, in S201, the initial mask is used to identify the first register in the target register, wherein the first register is the register in the target register that is determined to be pushed onto the stack. Specifically, since the calculation of stack space usage in different architectures is concentrated in different functions, the initial mask corresponds to the functions and compilers of different architectures, and its location is also different. As an example, the calculation of stack space usage in the ARC architecture is concentrated in the arc_compute_frame_size function. This function will perform calculations according to the normal process, and the first register to be pushed onto the stack is marked in the gmask variable. The gmask variable is 32 bits, and each bit represents a register. If a bit position of the gmask variable is 1, it means that the register corresponding to the bit position needs to be pushed onto the stack. Therefore, for the target function under the ARC architecture, the first register represented by the initial mask is the register whose bit position in the gmask variable is 1. It is understandable that the initial mask will vary depending on the architecture. The above is just an example of the ARC architecture. The present invention does not limit the initial mask to only the value of each bit in the gmask variable in the ARC system.

[0083] Wherein, in S202, the assembly code obtained by recompiling the back-end does not include the code for controlling pushing the first register into the stack.

[0084] The compiler may be a GCC compiler, or other compilation tools except the GUN compilation tool chain, which is not limited in the present invention.

[0085] After years of development, compilers have developed a three-stage compilation process consisting of the front-end, optimization, and back-end. The front-end is used to convert different high-level language codes into a unified intermediate representation within the compiler, and each high-level language has a corresponding front-end. The optimization phase optimizes the intermediate representation, and the back-end is used to translate the optimized intermediate representation into architecture-specific assembly code. It can also perform special optimizations for specific architectures. Each architecture has a corresponding back-end, and the back-end of the compiler can generate assembly code for multiple architectures. Since assembly code can be used to freely adjust stack space and registers, and the back-end of the compiler is used to translate assembly code, if it is necessary to optimize the stacking of the first register of the target function through assembly language, it is necessary to adjust the initial back-end of the compiler accordingly, so that the compiler uses the adjusted recompilation back-end to optimize the stacking of the registers of the target function during the compilation of high-level language code.

[0086] Specifically, the initial mask identifies the first register to be pushed onto the stack in the target function. Therefore, when modifying the initial backend of the compiler, the code corresponding to the first register can be modified accordingly so that the first register in the first assembly code obtained after subsequent compilation is no longer pushed onto the stack. Taking the target function of the above-mentioned ARC architecture as an example, as mentioned above, if a bit in the gmask variable is 1, it means that the register corresponding to the bit is the first register and will be pushed onto the stack. It can be understood that the initial backend of the compiler will not modify the value of the gmask bit being 1 when compiling the high-level language code corresponding to the target function, so the assembly code obtained by subsequent compilation will push the first register onto the stack. If you want to prevent these first registers from being pushed onto the stack, you need to modify the code of the initial backend so that the resulting recompiled backend will clear the bit corresponding to the first register in gmask to 0 when compiling the high-level language code corresponding to the target function. The sample code is as follows:

[0087]

[0088]

[0089] Among them, as mentioned above, if(arc_no_call_save_p(cfun)) is used to determine whether the compiled function has the no_call_saved attribute. If this preset target attribute is present, the value of the gmask bit is determined bit by bit from bit 13 to bit 25 in the for(regno=13; regno<=25; regno++) loop. The ARC architecture stipulates that the r13-r25 registers must be pushed onto the stack if they are to be used in the called function and restored when the function returns. Therefore, when determining the first register in the target function of the ARC architecture, it is sufficient to determine in the r13-r25 registers.

[0090] In if (gmask & (1 << regno)), 1 << regno means shifting 1 to the left by regno bits. & is the bitwise AND operator. gmask & (1 << regno) means performing a bitwise AND operation between gmask and the shifted 1 << regno to determine if the result is 1. It can be understood that in bitwise AND operations, the result is 1 only when the corresponding bits of the two values are both 1. Exemplarily, if the value of gmask is 00111100000000000000 and the value of regno is 15, then the value of 1 << regno is 00000100000000000000. After performing a bitwise AND operation between gmask and 1 << regno, the result obtained is 00000100000000000000, indicating that the 15th bit value of the bit positions of gmask is 1, and its corresponding register is the first register. It can be seen that by shifting 1 between the 13th bit and the 25th bit in this way, and cyclically performing a bitwise AND operation with the bit values of the 13th bit to the 25th bit of gmask, it is determined whether there is a value of 1 in the 13th to 25th bits of gmask according to whether the obtained bitwise AND result contains 1. If it is 1, it means that the register corresponding to this bit position of gmask, which is the first register, will be pushed onto the stack, thereby determining the first register according to the initial mask.

[0091] gmask &= ~(1 << regno) is used to clear the bits that are determined to be 1 in gmask to 0 after determining which specific bit positions in gmask are 1. ~ represents bitwise negation, clearing the bits corresponding to the values of 1 in the determined bit positions of gmask, so that the first register corresponding to this bit position will not be pushed onto the stack subsequently. Based on the above code, the initial backend of the compiler is modified to obtain the recompiled backend of the compiler, so that the compiled assembly code does not include the code for controlling the pushing of the first register onto the stack.

[0092] It can be understood that the above example only takes the steps of modifying the initial backend of the compiler according to the initial mask in the ARC architecture to obtain the recompiled backend as an example. In fact, there is a corresponding initial backend in compilers of different architectures. Therefore, different recompiled backends will be obtained after modifying the initial backend according to the initial mask in different architectures, so that the compiler can generate assembly code corresponding to different architectures according to different architectures. Therefore, the solution and idea of the present invention can also be applied to other architectures (such as ARM). The present invention does not limit the method of specifically modifying the code corresponding to the first register in the initial backend of the compiler according to the initial mask to obtain the recompiled backend of the compiler.

[0093] Among them, in S204, since the compiler has been modified, the recompilation backend can compile the high-level language code corresponding to the first register in the target function that is about to be pushed onto the stack into the first assembly code in which the first register is not pushed onto the stack, thereby optimizing the number of the first register in the target function pushed onto the stack, saving stack space, and thereby improving the data processing efficiency of the function.

[0094] By selecting this embodiment, a compiler for compiling high-level language codes can be provided. When the compiler compiles a target function implemented by the high-level language code, the number of registers of the target function pushed into the stack can be optimized, thus saving stack space. This improves the data processing efficiency of the function and the code compilation efficiency.

[0095] In a possible embodiment, the backend does not directly convert the high-level language code into assembly code, but converts the intermediate representation corresponding to the high-level language code into assembly code. Based on this, the present invention also provides a code compilation method, such as Figure 3 As shown, the method includes:

[0096] S301: Obtain an initial mask.

[0097] This step is the same as S201. For details, please refer to the relevant description in S201 and will not be repeated here.

[0098] S302 : Modify the code corresponding to the first register in the initial backend of the compiler according to the initial mask to obtain a recompiled backend of the compiler.

[0099] This step is the same as S202. For details, please refer to the relevant description in S202 and will not be repeated here.

[0100] S303: Obtain high-level language code written in a high-level language for implementing the target function.

[0101] This step is the same as S101. For details, please refer to the relevant description in S101 and will not be repeated here.

[0102] S304: Convert the high-level language code into an intermediate representation code written in a preset intermediate representation language.

[0103] S305 , compile the intermediate representation code using the recompilation backend to obtain a first assembly code.

[0104] In S304, as previously described, the compiler includes three parts: the front-end, the optimization, and the back-end. The front-end is used to convert different high-level language codes into a unified intermediate representation code within the compiler using a preset intermediate representation language, and the optimization phase optimizes the intermediate representation code. It is understandable that there are many different types of high-level language codes, and the front-end and optimization phases of the compiler can convert the high-level language code into a unified intermediate representation code, which facilitates the subsequent recompilation back-end to perform unified and efficient processing of the intermediate representation code, so that the recompilation back-end does not need to perform targeted special compilation of high-level language codes of different languages.

[0105] In S305 , the recompilation backend compiles the unified intermediate representation code that it can understand to obtain a first assembly code.

[0106] By selecting this embodiment, different high-level language codes are converted into a unified intermediate representation code, so that the recompilation backend can compile the unified intermediate representation code to obtain assembly code, and there is no need to generate recompilation backends for high-level language codes of different languages separately, thereby improving the applicability of code compilation.

[0107] After reducing the registers that need to be pushed onto the stack, the storage location of the subsequently pushed data in the stack space will also change accordingly. Therefore, the stack pointer used to identify the storage location of these subsequently pushed data in the stack space needs to be changed accordingly. Therefore, if only the registers that need to be pushed onto the stack are reduced in the target function without changing the stack pointer, it may cause errors in the subsequent stacking of other data. Based on this, the present invention also provides a code compilation method, such as Figure 4 As shown, the method includes:

[0108] S401: Obtain an initial mask.

[0109] This step is the same as S201. For details, please refer to the relevant description in S201 and will not be repeated here.

[0110] S402 : Modify the code corresponding to the first register in the initial backend of the compiler according to the initial mask to obtain a recompiled backend of the compiler.

[0111] This step is the same as S202. For details, please refer to the relevant description in S202 and will not be repeated here.

[0112] S403: Obtain high-level language code written in a high-level language for implementing the target function.

[0113] This step is the same as S101. For details, please refer to the relevant description in S101 and will not be repeated here.

[0114] S404: If the function attributes of the target function include preset target attributes, compile the high-level language code using a compiler including a recompilation backend to obtain a first assembly code.

[0115] This step is the same as S204. For details, please refer to the relevant description in S204 and will not be repeated here.

[0116] S405 : Determine the number of the first registers in the target register according to the initial mask, so that the compiler adjusts the allocation of the stack space according to the number of the first registers.

[0117] In step S405, as previously described, the initial mask is used to represent the first register in the target registers, i.e., the register to be pushed onto the stack. Therefore, the number of the first registers originally to be pushed onto the stack can be determined based on the initial mask, thereby enabling the compiler to determine the size of the stack space occupied by the registers originally to be pushed onto the stack during the compilation process, so that the compiler can determine the stack pointer of the data to be subsequently pushed onto the stack based on the size of the stack space originally occupied. As an example, still taking the aforementioned ARC architecture as an example, a line of code "reg_size - = UNITS_PER_WORD" can be added to the compiler so that during the compilation process, the size of the stack space occupied by the first register not to be pushed onto the stack is calculated each time a bit in gmask with a value of 1 is cleared to zero. Wherein, reg_size is the calculated size of the stack space occupied by the first register to be pushed onto the stack. Since the first code obtained by the compilation does not include the code for pushing the first register onto the stack, the value of reg_size is reduced by the size of the stack space occupied by the first register via reg_size - = UNITS_PER_WORD, where UNITS_PER_WORD is a macro indicating the size of the stack space occupied by a register. The compiler can then determine the stack pointer of the data that needs to be pushed onto the stack based on reg_size. The complete code example is as follows:

[0118]

[0119] It can be understood that the above code is obtained only by taking the ARC architecture as an example. The above solution of the present invention is not limited to the ARC architecture. For other architectures, it can also determine the number of the first registers according to the initial mask in the architecture, and apply the corresponding function code in the architecture to calculate the corresponding stack space, and then allocate the correct stack pointer for subsequent data.

[0120] By selecting this embodiment, after optimizing the number of stacked first registers of the target function, subsequent code can be left unaffected and continue to be pushed or popped, thus allocating stack space reasonably, avoiding data storage errors, and thereby improving the accuracy of code compilation.

[0121] To better illustrate the specific implementation process of the above code compilation method, the following will be explained in conjunction with specific application scenarios:

[0122] Under the ARC architecture, the initial back-end code of the GCC compiler is concentrated in the gcc / config / arc directory. For the initial back-end of the compiler for the ARC architecture, the identification and processing of function attributes, and the use of function stack space are all implemented in the gcc / config / arc / arc.c file in the GCC source code tree. Therefore, when adding a detection function to the compiler for detecting the preset target attributes of the target function, it is necessary to process and add it in the arc_attribute_table array in the arc.c file.

[0123] The preset function attribute for the target function can be the aforementioned "no_call_saved", and correspondingly, the detection function added in the compiler to detect "no_call_saved" can be the aforementioned "arc_no_call_save_p". During the compilation process of the compiler, the high-level language code corresponding to the target function will first be converted into an intermediate representation code through the front end, and the intermediate representation code will be optimized in the optimization stage, and the back end is used to compile the optimized intermediate representation code into assembly code. If the function attribute of the target function contains "no_call_saved", the back end of the compiler can detect it through "arc_no_call_save_p" in the process of compiling the intermediate representation code corresponding to the target function, and determine that the target function needs to perform register stack optimization.

[0124] If you want to optimize the number of registers pushed onto the stack when compiling the intermediate representation in the compiler's backend, so that the resulting first assembly code does not push the first register onto the stack, you need to modify the original compiler's initial backend code accordingly. The ARC architecture's calculation of stack space usage is centralized in the arc_compute_frame_size function, which marks the registers to be pushed onto the stack in the gmask variable. As previously mentioned, the value of a bit in gmask can be used as an initial mask to identify whether the register corresponding to that bit is the first register. Therefore, the compiler can determine the first register based on gmask and modify the compiler's initial backend code that controls the pushing of the first register onto the stack when compiling the intermediate representation to code that controls the pushing of the first register onto the stack when compiling the intermediate representation. Furthermore, since other data will need to be pushed onto the stack later, if the originally pushed first register is not pushed onto the stack, its stack space occupation must be determined to determine the correct stack address for the subsequent data to be pushed onto the stack. Therefore, in the recompiled backend, you can also add the reg_size - = UNITS_PER_WORD code to calculate the space occupied by the first register when compiling the intermediate representation. The specific code example is as follows:

[0125]

[0126]

[0127] The above code has been explained in the aforementioned S405 and will not be repeated here. It is understandable that if the compiler detects that the corresponding target function contains preset target attributes during the compilation of the high-level language code, it will compile the intermediate representation corresponding to the target function according to the initial mask through the recompilation backend to obtain the first assembly code that controls the first register not to be pushed onto the stack. Correspondingly, if the compiler does not detect that the target function contains preset target attributes, the initial mask will not be obtained when the subsequent recompilation backend compiles the intermediate representation corresponding to the target function, nor will it make corresponding modifications to the assembly code corresponding to the first register that originally needed to be pushed onto the stack. Instead, a second assembly code is generated so that the code controls the first register in the target register to be pushed onto the stack.

[0128] By selecting this embodiment, the code of the GCC compiler is modified so that the target function of the ARC system implemented using high-level language code can optimize the number of registers pushed into the stack during the compilation process, saving stack space, making high-speed memory sufficient for use as stack space, and reducing the problem of low function data processing efficiency caused by using low-speed memory as stack space.

[0129] Corresponding to the code compiling method provided in the embodiment of the present invention, the embodiment of the present invention also provides a code compiling device, such as Figure 5 Shown, including:

[0130] A code acquisition module 501 is used to acquire a high-level language code written in a high-level language and used to implement the target function;

[0131] A code compilation module 502 is used to compile the high-level language code into a first assembly code for implementing the target function if the function attributes of the target function include preset target attributes, wherein the first assembly code does not include code for controlling the push of a target register onto the stack, and the target register is a register agreed in the first assembly code to store local variables in the target function.

[0132] In a possible embodiment, the device further includes:

[0133] a mask acquisition module, configured to acquire an initial mask, wherein the initial mask is used to identify the first register, wherein the first register is a register in the target register that is determined to be pushed onto the stack;

[0134] a recompilation backend module, configured to modify, according to the initial mask, the code corresponding to the first register in the initial backend of the compiler to obtain a recompilation backend of the compiler, wherein the assembly code compiled by the recompilation backend does not include a code for controlling stacking of the first register;

[0135] The code compiling module 502 is specifically configured to compile the high-level language code using a compiler including the recompilation backend to obtain a first assembly code.

[0136] In a possible embodiment, the device further includes:

[0137] A space determination module is configured to determine the number of the first registers in the target registers according to the initial mask, so that the compiler adjusts allocation of stack space according to the number of the first registers.

[0138] In a possible embodiment, the code compilation module 502 includes:

[0139] An intermediate representation acquisition submodule, configured to convert the high-level language code into an intermediate representation code written in a preset intermediate representation language;

[0140] The code compiling submodule is used to compile the intermediate representation code using the recompilation backend to obtain a first assembly code.

[0141] In a possible embodiment, the device further includes:

[0142] The second code compilation module is used to compile the high-level language code into a second assembly code for implementing the target function if the function attributes of the target function do not include a preset target attribute, wherein the second assembly code includes a code for controlling the stacking of the target register.

[0143] The embodiment of the present invention further provides an electronic device, such as Figure 6 As shown, it includes a processor 601, a communication interface 602, a memory 603 and a communication bus 604, wherein the processor 601, the communication interface 602, and the memory 603 communicate with each other through the communication bus 604.

[0144] Memory 603, used for storing computer programs;

[0145] The processor 601 is configured to execute the program stored in the memory 603 by performing the following steps:

[0146] obtaining a high-level language code written in a high-level language for implementing a target function;

[0147] If the function attributes of the target function include preset target attributes, the high-level language code is compiled into a first assembly code for implementing the target function, wherein the first assembly code does not include code for controlling the target register to be pushed onto the stack, and the target register is a register agreed in the first assembly code to store local variables in the target function.

[0148] The communication bus mentioned in the electronic device mentioned above may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.

[0149] The communication interface is used for communication between the above electronic device and other devices.

[0150] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk storage. Alternatively, the memory may be at least one storage device located away from the processor.

[0151] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.

[0152] In another embodiment of the present invention, a computer-readable storage medium is provided, which stores a computer program. When the computer program is executed by a processor, the steps of any of the above code compilation methods are implemented.

[0153] In another embodiment of the present invention, a computer program product including instructions is provided, which, when executed on a computer, enables the computer to execute any code compiling method in the above embodiments.

[0154] In the above embodiments, all or part of the embodiments can be implemented by software, hardware, firmware, or any combination thereof. When implemented using software, all or part of the embodiments can be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that includes one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)).

[0155] It should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply the existence of any such actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.

[0156] Each embodiment in this specification is described in a related manner. Similar portions between the various embodiments can be referenced to each other. Each embodiment focuses on the differences between the other embodiments. In particular, the device, electronic device, and computer-readable storage medium embodiments are generally similar to the method embodiments, so their descriptions are relatively simple. For related portions, reference can be made to the descriptions of the method embodiments.

[0157] The above description is only a preferred embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present invention are included in the scope of protection of the present invention.

Claims

1. A code compilation method, characterized in that: The method comprises: obtaining a high-level language code written in a high-level language for implementing a target function; If the function attributes of the target function include a preset target attribute, compiling the high-level language code into a first assembly code for implementing the target function, wherein the first assembly code does not include code for controlling a target register to be pushed onto a stack, the target register being a register designated in the first assembly code for storing local variables in the target function, and the preset target attribute being used to identify the target function as a function that does not require data originally stored in the target register to be restored during a return phase; The method further comprises: Obtaining an initial mask, where the initial mask is used to identify a first register, wherein the first register is a register in the target register that is determined to be pushed onto the stack; Modifying, according to the initial mask, code corresponding to the first register in an initial backend of the compiler to obtain a recompiled backend of the compiler, wherein the assembly code compiled by the recompiled backend does not include code for controlling stacking of the first register; Compiling the high-level language code into a first assembly code for implementing the target function comprises: The high-level language code is compiled using a compiler including the recompilation backend to obtain a first assembly code.

2. The method according to claim 1, characterized in that The method further comprises: The number of the first registers in the target registers is determined according to the initial mask, so that the compiler adjusts allocation of stack space according to the number of the first registers.

3. The method according to claim 1, characterized in that Compiling the high-level language code with a compiler including the recompilation backend to obtain a first assembly code includes: Converting the high-level language code into an intermediate representation code written in a preset intermediate representation language; The intermediate representation code is compiled using the recompilation backend to obtain a first assembly code.

4. The method according to claim 1, wherein The method further comprises: If the function attributes of the target function do not include preset target attributes, the high-level language code is compiled into a second assembly code for implementing the target function, wherein the second assembly code includes a code for controlling stacking of the target register.

5. A code compiling device, characterized in that: The device comprises: A code acquisition module, used for acquiring high-level language codes written in a high-level language and used for realizing a target function; a code compiling module, configured to, if the function attributes of the target function include a preset target attribute, compile the high-level language code into a first assembly code for implementing the target function, wherein the first assembly code does not include code for controlling a target register to be pushed onto a stack, the target register being a register agreed in the first assembly code for storing local variables in the target function, and the preset target attribute being used to identify the target function as a function that does not require restoration of data originally stored in the target register during a return phase; The device further comprises: a mask acquisition module, configured to acquire an initial mask, wherein the initial mask is used to identify a first register, wherein the first register is a register in the target register that is determined to be pushed onto the stack; a recompilation backend module, configured to modify, according to the initial mask, the code corresponding to the first register in the initial backend of the compiler to obtain a recompilation backend of the compiler, wherein the assembly code compiled by the recompilation backend does not include a code for controlling stacking of the first register; The code compilation module is specifically used to compile the high-level language code using a compiler including the recompilation backend to obtain a first assembly code.

6. The device according to claim 5, characterized in that The device further comprises: A space determination module is configured to determine the number of the first registers in the target registers according to the initial mask, so that the compiler adjusts allocation of stack space according to the number of the first registers.

7. The device according to claim 5, characterized in that The code compilation module includes: An intermediate representation acquisition submodule, configured to convert the high-level language code into an intermediate representation code written in a preset intermediate representation language; The code compiling submodule is used to compile the intermediate representation code using the recompilation backend to obtain a first assembly code.

8. The device according to claim 5, characterized in that The code compilation module is further used to compile the high-level language code into a second assembly code for implementing the target function if the function attributes of the target function do not include preset target attributes, wherein the second assembly code includes code for controlling pushing the target register into the stack.

9. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus; Memory for storing computer programs; A processor, configured to implement the method steps described in any one of claims 1 to 4 when executing a program stored in a memory.

10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps according to any one of claims 1 to 4 are implemented.

Citation Information

Patent Citations

  • Implementation method for general register reservation recovery

    CN112540871A

  • Method and apparatus for optimizition chaining post code

    CN1374585A