Code compilation method, device, equipment and medium
By using a hash search algorithm in the C51 language compiler to determine and encapsulate the target code variable block as a macro function, the problem of too many compilation instruction bytes is solved and a more efficient compilation process is achieved.
Patent Information
- Application Number
- CN202210919429.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-01
- Publication Date
- 2025-09-30
- Estimated Expiration
- 2042-08-01
AI Technical Summary
The existing C51 language compiler has problems such as too many compilation instruction bytes, large compilation files, large program space occupation, slow running speed, etc. during the compilation process, resulting in low compilation efficiency.
By obtaining the target code variable block that meets the preset standards in the assembly code, a preset search algorithm such as a hash search algorithm is used to determine the consistent target code variable block, and it is encapsulated as a macro function for compilation, the number of code instruction bytes is reduced and the compilation efficiency is improved.
It reduces program memory usage, increases running speed, and improves compilation efficiency.
Smart Images

Figure CN115454437B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of compiler technology, and in particular to a code compilation method, device, equipment and medium. Background Art
[0002] The C51 language is a structured programming language with a rich set of operators and data types, making it easy to implement various complex data structures. In addition, it can directly access the physical address of the memory and perform bit-level operations. Because the C51 language implements hardware programming operations, it combines the functions of high-level and low-level languages. In addition, the C51 language has the characteristics of high efficiency and strong portability. Therefore, it can be widely ported to computers or single-chip microcomputers on various platforms.
[0003] The compilation efficiency of the C51 compiler is an important indicator to measure the quality of a compiler. The existing C51 language will repeatedly compile the same code blocks during use, which causes the compiler to compile too many instruction bytes, the compiled binary files are large, the program occupies a large space, the running speed is slow, and the compiler performance is low. Furthermore, the compilation efficiency of the compiled language for program compilation is low. Summary of the Invention
[0004] The main purpose of the present invention is to provide a code compilation method, device, equipment and medium, aiming to improve the compilation efficiency of program compilation.
[0005] To achieve the above object, the present invention provides a code compilation method, comprising:
[0006] Get the assembly code to be compiled;
[0007] Based on a preset search algorithm, obtaining a target code variable block that meets a preset standard in the assembly code;
[0008] Encapsulate the target code variable block and determine the corresponding macro function;
[0009] Based on the macro function, code compilation is performed.
[0010] Preferably, the step of obtaining a target code variable block that meets a preset standard in the assembly code based on a preset search algorithm includes:
[0011] Obtaining a code line hash value and a code block corresponding to each line of code in the assembly code, and determining a first order for sorting the code blocks;
[0012] Based on the first order, the code lines are compared and searched according to the code line hash values to determine a first code variable block in the code block that meets a first preset standard;
[0013] generating a first code variable block Hash value corresponding to the first code variable block, and determining a second order in which the first code variable blocks are arranged;
[0014] Based on the second order, comparing and searching the first code variable block with the code block according to the hash value of the first code variable block to determine a second code variable block in the first code variable block that meets a second preset standard;
[0015] The Hash values corresponding to the second code variable block and the first code variable block are compared and searched to determine the corresponding target code variable block.
[0016] Preferably, before the step of obtaining a target code variable block that meets a preset standard in the assembly code based on a preset search algorithm, the code compilation method further includes:
[0017] Parsing the assembly code to obtain parsing information of the assembly code;
[0018] Divide the assembly code according to the functional statements in the parsed information to generate a code block containing a plurality of code lines;
[0019] Generate a corresponding code line Hash value for each code line in the code block;
[0020] The number of instruction bytes in each line of the code block is obtained, and the code blocks are sorted according to the number of instruction bytes.
[0021] Preferably, the step of generating a corresponding code line Hash value for each code line in the code block includes:
[0022] Mapping the assembly instructions corresponding to the code lines into binary integers;
[0023] Based on the binary integer, an XOR operation is performed with a preset seed SEED to obtain a Hash value corresponding to the assembly instruction of the code line.
[0024] Preferably, after the step of determining the corresponding target code variable block, the code compiling method further includes:
[0025] Performing optimization evaluation on the target code variable block;
[0026] If the number of instruction bytes of the target code variable block is greater than the number of instruction bytes of the original code block, deleting the target code block;
[0027] If the number of instruction bytes of the target code variable block is less than the number of instruction bytes of the original code block, the target code block is retained.
[0028] Preferably, the step of encapsulating the target code variable block and determining the corresponding macro function includes:
[0029] Obtain the target code variable block ID;
[0030] Based on the target code variable block ID, the function entry of the macro function is determined, and the target code variable block is connected according to the function entry of the macro function to determine the macro function corresponding to the target code variable block.
[0031] Preferably, the step of executing code compilation based on the macro function includes:
[0032] Connecting a macro function entry of the macro function based on a preset jump instruction, and executing an internal assembly instruction of the macro function through the macro function entry;
[0033] Based on the preset return instruction, the assembly statement corresponding to the preset code block jump instruction is returned, and the assembly statement is executed.
[0034] In addition, to achieve the above-mentioned purpose, an embodiment of the present invention further provides a code compilation device, the code compilation device comprising:
[0035] Code acquisition module, used to obtain the assembly code to be compiled;
[0036] A code search and merging module is used to obtain target code variable blocks that meet preset standards in the assembly code based on a preset search algorithm;
[0037] A code encapsulation module is used to encapsulate the target code variable block and determine the corresponding macro function;
[0038] The execution compilation module is used to execute code compilation based on the macro function.
[0039] Preferably, the code acquisition module includes:
[0040] Parsing the assembly code to obtain parsing information of the assembly code;
[0041] Divide the assembly code according to the functional statements in the parsed information to generate a code block containing a plurality of code lines;
[0042] Generate a corresponding code line Hash value for each code line in the code block;
[0043] The number of instruction bytes in each line of the code block is obtained, and the code blocks are sorted according to the number of instruction bytes.
[0044] Preferably, the code acquisition module further includes:
[0045] Mapping the assembly instructions corresponding to the code lines into binary integers;
[0046] Based on the binary integer, an XOR operation is performed with a preset seed SEED to obtain a Hash value corresponding to the assembly instruction of the code line.
[0047] Preferably, the code search and merging module includes:
[0048] Obtaining a code line hash value and a code block corresponding to each line of code in the assembly code, and determining a first order for sorting the code blocks;
[0049] Based on the first order, the code lines are compared and searched according to the code line hash values to determine a first code variable block in the code block that meets a first preset standard;
[0050] generating a first code variable block Hash value corresponding to the first code variable block, and determining a second order in which the first code variable blocks are arranged;
[0051] Based on the second order, comparing and searching the first code variable block with the code block according to the hash value of the first code variable block to determine a second code variable block in the first code variable block that meets a second preset standard;
[0052] The Hash values corresponding to the second code variable block and the first code variable block are compared and searched to determine the corresponding target code variable block.
[0053] Preferably, the code encapsulation module includes:
[0054] Obtain the target code variable block ID;
[0055] Based on the target code variable block ID, the function entry of the macro function is determined, and the target code variable block is connected according to the function entry of the macro function to determine the macro function corresponding to the target code variable block.
[0056] Preferably, the execution compilation module includes:
[0057] Connecting a macro function entry of the macro function based on a preset jump instruction, and executing an internal assembly instruction of the macro function through the macro function entry;
[0058] Based on the preset return instruction, the assembly statement corresponding to the preset code block jump instruction is returned, and the assembly statement is executed.
[0059] In addition, to achieve the above-mentioned purpose, an embodiment of the present invention also proposes a device, which includes a memory, a processor, and a code compiler stored in the memory and runnable on the processor, wherein the code compiler is executed by the processor to implement the steps of the code compilation method described above.
[0060] In addition, to achieve the above-mentioned purpose, the present invention also provides a medium, which is a computer-readable storage medium, and a code compiler is stored on the computer-readable storage medium. When the code compiler is executed by the processor, the steps of the code compilation method described above are implemented.
[0061] The code compilation method, apparatus, device and medium proposed in the present invention include the following steps: obtaining assembly code to be compiled; obtaining target code variable blocks that meet preset standards in the assembly code based on a preset search algorithm; encapsulating the target code variable blocks and determining corresponding macro functions; and executing code compilation based on the macro functions.
[0062] Compared with the prior art in which the compiler usually inputs the entire assembly code into the compiler for compilation when compiling the assembly instructions of the C51 language, resulting in problems such as a large number of compiled code bytes, a large space occupation, and a slow running speed, the present invention optimizes the assembly code to be compiled by merging the same target code variable blocks, adopts a preset search algorithm to determine the target code variable blocks with consistency, and encapsulates and determines the macro function to be compiled based on the target code variable blocks, thereby greatly improving the speed of program search, reducing the total number of code instruction bytes, reducing program memory, accelerating the running speed, and further improving the compilation efficiency of the assembly language. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] Figure 1 A schematic diagram of the device structure of the hardware operating environment involved in the embodiment of the code compilation method of the present invention;
[0064] Figure 2 This is a flow chart of a first embodiment of a code compiling method according to the present invention;
[0065] Figure 3 This is a schematic diagram of the specific flow of the overall operation of algorithm optimization in the first embodiment of the code compilation method of the present invention;
[0066] Figure 4 This is a flow chart of a second embodiment of the code compiling method of the present invention;
[0067] Figure 5This is a schematic diagram of a specific process of generating the first code variable block in the second embodiment of the code compilation method of the present invention;
[0068] Figure 6 This is a schematic diagram of a specific flow chart of generating the second code variable block in the second embodiment of the code compilation method of the present invention;
[0069] Figure 7 This is a schematic diagram of a specific process for generating a target code variable block in the second embodiment of the code compilation method of the present invention;
[0070] Figure 8 This is a schematic diagram of the structure of comparing code blocks in the second embodiment of the code compilation method of the present invention;
[0071] Figure 9 This is a schematic diagram of the structure of comparing variable blocks in the second embodiment of the code compilation method of the present invention;
[0072] Figure 10 This is a schematic diagram of the structure of comparing target code variable blocks in the second embodiment of the code compilation method of the present invention;
[0073] Figure 11 This is a flow chart of a third embodiment of the code compiling method of the present invention;
[0074] Figure 12 This is a schematic diagram of a specific flow chart of preprocessing assembly code in the third embodiment of the code compiling method of the present invention;
[0075] Figure 13 This is a flow chart of a fourth embodiment of the code compiling method of the present invention;
[0076] Figure 14 This is a schematic diagram of the structure of optimizing and evaluating the code in the fourth embodiment of the code compilation method of the present invention;
[0077] Figure 15 This is a schematic diagram of a specific process of performing code encapsulation on a target code variable block in the fourth embodiment of the code compilation method of the present invention;
[0078] Figure 16 Schematic diagram of the functional modules of the code compiling device of the code compiling method of the present invention.
[0079] The purpose, features and advantages of the present invention will be further described with reference to the accompanying drawings and in conjunction with the embodiments. DETAILED DESCRIPTION
[0080] It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0081] Specifically, refer to Figure 1 , Figure 1This is a schematic diagram of the device structure of the hardware operating environment involved in the embodiment of the code compilation method of the present invention.
[0082] like Figure 1 As shown, the device may include: a processor 1001, such as a CPU, a network interface 1004, a user interface 1003, a memory 1005, and a communication bus 1002. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface). The memory 1005 may be a high-speed RAM memory, or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.
[0083] like Figure 1 As shown, memory 1005, a computer storage medium, may include an operating system, a network communication module, a user interface module, and a code compiler. The operating system is a program that manages and controls device hardware and software resources and supports the execution of the code compiler and other software or programs. The network communication module is used to manage and control network interface 1002. User interface 1003 is primarily used for data communication with a client. Network interface 1004 is primarily used for establishing a communication connection with a server. Processor 1001 may be used to invoke the code compiler stored in memory 1005 and execute the operations described in the various embodiments of the code compilation method described below.
[0084] Those skilled in the art will understand that Figure 1 The device structure shown in the figure does not constitute a limitation of the device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0085] To better understand the above technical solution, an exemplary embodiment of the code compilation method of the present invention will be described in more detail below with reference to the accompanying drawings. Although the accompanying drawings show exemplary embodiments of the code compilation method of the present invention, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments described herein. Instead, these embodiments are provided to enable a more thorough understanding of the present invention and to fully convey the scope of the present invention to those skilled in the art.
[0086] Based on the above terminal device architecture but not limited to the above architecture, an embodiment of the code compilation method of the present invention is proposed.
[0087] Specifically, refer to Figure 2 , Figure 2 This is a flow chart of a first embodiment of a code compiling method according to the present invention, wherein the code compiling method comprises:
[0088] Step S10, obtaining the assembly code to be compiled;
[0089] Step S20, based on a preset search algorithm, obtaining a target code variable block that meets a preset standard in the assembly code;
[0090] Step S30, encapsulating the target code variable block and determining the corresponding macro function;
[0091] Step S40: executing code compilation based on the macro function.
[0092] The code compilation method of the embodiment of the present application optimizes the assembly code to be compiled by merging the same target code variable blocks, uses a preset search algorithm to determine the target code variable blocks with consistency, and encapsulates the target code variable blocks to determine the macro function to be compiled, and finally calls the macro function to execute the corresponding code compilation.
[0093] The following describes each step in detail:
[0094] Step S10, obtaining the assembly code to be compiled;
[0095] In a specific embodiment, a device capable of executing assembly code compilation, such as a single-chip microcomputer and a compiler, is used as the execution device for code compilation in the present invention. Furthermore, the assembly code can be various types of assembly languages, specifically the C51 language applied to the C51 platform. It should be specifically explained that the C51 language is a structured programming language with a rich set of operators and data types, which facilitates the implementation of various complex data structures. It can also directly access physical addresses in memory and perform bit-level operations. Because the C51 language implements hardware programming operations, it integrates the functions of high-level and low-level languages, and has the characteristics of high efficiency and strong portability. Therefore, the C51 language can be widely ported to computers or single-chip microcomputers on various platforms.
[0096] Furthermore, the above-mentioned assembly code to be compiled is a complete code with execution functions and business logic, which can be composed of assembly instructions, executing the functions and business logic corresponding to the assembly instructions. The above-mentioned functions and business logic can be assembly codes with short assembly instruction steps, or assembly codes with more assembly instruction steps, more complex programs, and more blocks of identical target code variables.
[0097] Step S20, based on a preset search algorithm, obtaining a target code variable block that meets a preset standard in the assembly code;
[0098] In a specific embodiment, the above-mentioned preset search algorithm can be a hash search algorithm. Furthermore, the same target code variable blocks in the above-mentioned assembly code containing more identical code blocks are searched out through the hash search algorithm to determine the target code variable blocks with consistency in the above-mentioned assembly code.
[0099] Furthermore, the core idea of the hash search algorithm is to generate a key-value pair for each search object, with each key corresponding to a unique value, and then retrieve the value corresponding to the search object using the key. In this embodiment, each line of the assembly code is assigned a corresponding hash value. The hash value identifies the line of code, and when the code lines are consistent, the corresponding hash values are also consistent.
[0100] Furthermore, the code lines are compared according to the Hash values corresponding to the code lines, and the same code lines are searched. After the same code lines are determined, multiple valid statement lines in the assembly code are used as code fragments according to the code characteristics, and the code fragments are used as variable blocks to perform pairwise comparisons on the code fragments again based on the hash search algorithm. By repeatedly generating variable blocks and sorting them from large to small according to the number of bytes, the functional blocks with consistent Hash values, i.e., the target code variable blocks, are determined.
[0101] Furthermore, as a specific embodiment, this embodiment searches for the same code blocks in the same function by comparing the hash values line by line between N assembly code blocks CODE_BLOCK, finding the same code fragments, and generating M variable blocks CODE_VAR_BLOCK, where the above variable blocks are multiple lines of valid statements.
[0102] Furthermore, the generated variable blocks CODE_VAR_BLOCK are sorted from large to small according to the number of instruction bytes, and the hash values are compared line by line with the N CODE_BLOCKs again to find the same code fragments, and P variable blocks CODE_VAR_BLOCK are generated until all N code blocks are compared.
[0103] Furthermore, the generated P variable blocks CODE_VAR_BLOCK are sorted from largest to smallest according to the number of instruction bytes; then the hash values of the variable blocks CODE_VAR_BLOCK are compared line by line to find the same code fragments, and Q variable blocks CODE_VAR_BLOCK are generated;
[0104] Furthermore, the above variable blocks are sorted from large to small according to the number of instruction bytes, and the optimized number of bytes is evaluated. If the number of bytes is less than the number of bytes of the original code, it is encapsulated into a macro function. If the number of bytes is greater than the number of bytes of the original code, the variable block is deleted.
[0105] This embodiment generates as complete a CODE_VAR_BLOCK as possible through multiple hash value comparisons, then further simplifies the CODE_VAR_BLOCK by comparing the variable blocks with each other, and finally retains the CODE_VAR_BLOCK that meets the conditions by comparing the number of bytes, completing the screening of the CODE_VAR_BLOCK and achieving the purpose of reducing the number of code instruction bytes.
[0106] Step S30, encapsulating the target code variable block and determining the corresponding macro function;
[0107] In a specific embodiment, the step of encapsulating the target code variable block and determining the corresponding macro function includes:
[0108] Obtain the target code variable block ID;
[0109] Based on the target code variable block ID, the function entry of the macro function is determined, and the target code variable block is connected according to the function entry of the macro function to determine the macro function corresponding to the target code variable block.
[0110] In a specific embodiment, the above-mentioned method of encapsulating the target code variable block and determining the corresponding macro function can be the three function macro encapsulation methods of {}, do{...}while(0) and ({}), which can encapsulate a frequently called function statement into a macro function to reduce the difficulty in the calling process. Furthermore, if these functional modules of the assembly language are encapsulated into functions, additional pop and push operations are required to pass function entry parameters, and registers need to be protected, which will increase the number of assembly bytes. Compared with encapsulating the same target code variable block into a function, encapsulating these commonly used target code variable blocks into macro functions is more concise and convenient, reducing the number of assembly code lines and bytes, and well achieving the purpose of optimizing the assembly code.
[0111] Step S40: executing code compilation based on the macro function.
[0112] In a specific embodiment, the step of executing code compilation based on the macro function includes:
[0113] Connecting a macro function entry of the macro function based on a preset jump instruction, and executing an internal assembly instruction of the macro function through the macro function entry;
[0114] Based on the preset return instruction, the assembly statement corresponding to the preset code block jump instruction is returned, and the assembly statement is executed.
[0115] In a specific embodiment, an encapsulated macro function is composed of a function entry, the same or similar target code variable block and a ret return statement, and a preset jump instruction, such as: lcall, acall, scall, etc., is used to make the compilation device jump to a function label. The function label marks the entry of the macro function, rather than the function name, which is the variable target code variable block ID of the macro function.
[0116] Specifically, when the assembly code in the compilation device executes a jump instruction, such as lcall, acall, or scall statements, it jumps to the function entry of the corresponding macro function, and enters the macro function through the above macro function entry. After executing the operation code in the above macro function, it returns to the next statement indicated and connected by the jump instructions lcall, acall, or scall through a preset return instruction, such as ret, and continues to execute the code.
[0117] Among them, in the above-mentioned preset jump instructions, the label address used by lcall to jump to is within the range of less than 64K bytes from the address pointed to by the current PC; the label address used by acall to jump to is within the range of less than 2K bytes from the address pointed to by the current PC; and the label address used by scall to jump to is within the range of less than 256 bytes from the address pointed to by the current PC.
[0118] Reference Figure 3 , Figure 3 This is the overall operation flow of the algorithm optimization of this embodiment. Specifically, the overall operation flow of the algorithm optimization of this embodiment takes a section of assembly code function as a unit. Operation 101 preprocesses the assembly code; Operation 102 compares the hash values between the code blocks CODE_BLOCK line by line to generate the variable block CODE_VAR_BLOCK; Operation 103 compares the hash values between the code blocks CODE_BLOCK and the generated variable blocks CODE_VAR_BLOCK line by line to generate the variable block CODE_VAR_BLOCK; Operation 104 compares the hash values between the generated variable blocks CODE_VAR_BLOCK line by line to generate the variable block CODE_VAR_BLOCK; Operation 105 evaluates the number of bytes after optimization for the generated variable block CODE_VAR_BLOCK. If the number of bytes after optimization is less than the number of bytes in the original code, it is encapsulated as a macro function and the macro function is called through a preset jump instruction. If the number of bytes after optimization is greater than the number of bytes in the original code, the variable block is deleted and not used.
[0119] In this embodiment, the assembly code to be compiled is optimized by merging the same target code variable blocks, a preset search algorithm is used to determine the target code variable blocks with consistency, and the macro function to be compiled is determined by encapsulating the target code variable blocks. This greatly improves the speed of program search, reduces the total number of code instruction bytes, reduces program memory, speeds up the running speed, and further improves the compilation efficiency of the assembly language.
[0120] Furthermore, based on the first embodiment of the code compilation method of the embodiment of the present application, a second embodiment of the code compilation method of the embodiment of the present application is proposed.
[0121] The difference between the second embodiment of the code compilation method and the first embodiment of the code compilation method is that this embodiment is a refinement of step S20 "obtaining a target code variable block that meets a preset standard in the assembly code based on a preset search algorithm". Figure 4 , specifically including:
[0122] Step S21, obtaining a code line Hash value and a code block corresponding to each line of code in the assembly code, and determining a first order for sorting the code blocks;
[0123] In a specific embodiment, the code line of the above-mentioned assembly code is the smallest unit of the assembly code of the code compilation method in this embodiment. Each line of assembly code is assigned a corresponding hash value according to a preset assignment algorithm to achieve the search of the same code. Specifically, the above-mentioned assignment algorithm can be a preset hash algorithm, and the above-mentioned method of assigning corresponding hash values to code lines can be based on a preset hash table for assignment.
[0124] Furthermore, in the process of executing the above-mentioned preset search algorithm, the above-mentioned code lines arrange the code blocks containing the code lines in a preset arrangement order. Specifically, the above-mentioned preset arrangement order can be arranged according to the number of instruction bytes of the code block, and the specific arrangement direction can be from small to large in the number of instruction bytes of the code block.
[0125] Step S22: Based on the first order, the code lines are compared and searched according to the code line hash values to determine a first code variable block in the code block that meets a first preset standard;
[0126] Step S23, generating a first code variable block Hash value corresponding to the first code variable block, and determining a second order of arrangement of the first code variable blocks;
[0127] Step S24: Based on the second order, compare and search the first code variable block with the code block according to the hash value of the first code variable block to determine a second code variable block in the first code variable block that meets a second preset standard;
[0128] Step S25: Compare and search the Hash values corresponding to the second code variable block and the first code variable block to determine the corresponding target code variable block.
[0129] In a specific embodiment, searching for the same target code variable block in the same function is performed by searching for a first code variable block with the same hash value in the code line, and searching for a second code variable block with the same hash value in the first code variable block. The specific search process can be to determine the first code variable block by searching in the code line, determine the second code variable block by comparing and searching in the first code variable block, and finally determine the corresponding target code variable block by comparing and searching between the second variable blocks. Specifically, Hash values are compared line by line between N assembly code blocks CODE_BLOCK to find the same code fragments and generate M variable blocks CODE_VAR_BLOCK. The above variable blocks are multiple lines of valid statements.
[0130] Furthermore, the generated variable blocks CODE_VAR_BLOCK are sorted from large to small according to the number of instruction bytes, and the hash values are compared line by line with the N CODE_BLOCKs again to find the same code fragments, and P variable blocks CODE_VAR_BLOCK are generated until all N code blocks are compared.
[0131] Furthermore, the generated P variable blocks CODE_VAR_BLOCK are sorted from large to small according to the number of instruction bytes; then the hash values of the variable blocks CODE_VAR_BLOCK are compared line by line to find the same code fragments, and Q variable blocks CODE_VAR_BLOCK are generated.
[0132] Reference Figure 5 , Figure 5 This is the specific process of generating the first code variable block in this embodiment. The Hash value comparison between the generated N CODE_BLOCKs is performed line by line to find the same code blocks, and M new variable blocks CODE_VAR_BLOCK are generated until all N code blocks are compared. Each variable block is assigned a corresponding ID, the start line, end line, number of lines and total number of instruction bytes of the identification block, and is sorted from large to small according to the number of instruction bytes.
[0133] Reference Figure 6 , Figure 6This is the specific process for generating the second code variable block in this embodiment. The generated M variable blocks CODE_VAR_BLOCK are compared row-by-row with the N CODE_BLOCKs in a loop, generating P new variable blocks CODE_VAR_BLOCK until all N variable blocks are compared. Each variable block is assigned an ID, identifying the block's start and end rows, number of rows, and total number of instruction bytes. The blocks are then sorted from largest to smallest by instruction byte count.
[0134] Reference Figure 7 , Figure 7 The specific process for generating target code variable blocks in this embodiment involves performing a row-by-row hash value comparison between the generated P variable blocks CODE_VAR_BLOCK and CODE_VAR_BLOCK, generating Q new target code variable blocks CODE_VAR_BLOCK until all Q target code variable blocks have been compared. Each code block is then assigned an ID, identifying its start and end lines, number of lines, and total number of instruction bytes. The blocks are then sorted from largest to smallest by instruction byte count.
[0135] Furthermore, as a specific embodiment, assume that there are 3 code blocks code_block={C1, C2, C3} in a function, where C1={a,b,c,d,e,n}, C2={f,a,b,c,g,h}, C3={a,b,c,d,e,i,j}, the number of assembly instruction lines corresponding to each code block is Row={6,6,7}, the number of instruction bytes corresponding to each code block is Size={27,30,35}, and the hash value corresponding to the C1 code block is Hash_C1 = {Hash1, Hash2, Hash3, Hash4, Hash5, Hash6}, the hash value corresponding to the C2 code block is Hash_C2 = {Hash7, Hash1, Hash2, Hash3, Hash8, Hash9}, and the hash value corresponding to the C3 code block is Hash_C3 = {Hash1, Hash2, Hash3, Hash4, Hash5, Hash10, Hash11}.
[0136] Further, refer to Figure 8 , according to the Hash value, the C1, C2, and C3 code blocks are compared and searched in pairs to determine the code blocks with the same Hash value, and the variable blocks V11 = {a, b, c}, V12 = {a, b, c, d, e}, and V13 = {a, b, c} are generated.
[0137] Further, refer to Figure 9According to the Hash value, the variable blocks V11, V12, and V13 are compared and searched with the C1, C2, and C3 code blocks respectively to determine the variable blocks with the same Hash value and generate variable blocks V21 to V29 to further ensure the integrity and universality of the variable blocks.
[0138] Further, refer to Figure 10 According to the hash value, the variable blocks V21 to V29 are compared and searched in pairs, and the same target code variable blocks are searched again to simplify the variable blocks and generate target code variable blocks V31 = {a, b, c, d, e} and V32 = {a, b, c}.
[0139] This embodiment searches for code lines with the same assembly code hash value based on hash values, searches for the same variable blocks in the code lines by comparing hash values pairwise, and further searches for the same target code variable blocks in the variable blocks by comparing hash values pairwise, and generates corresponding functional modules to encapsulate macro functions for code compilation, which greatly improves the speed of program search, reduces the total number of code instruction bytes, reduces program memory, speeds up the running speed, and further improves the compilation efficiency of the assembly language.
[0140] Furthermore, based on the first and second embodiments of the code compilation method of the embodiment of the present application, a third embodiment of the code compilation method of the embodiment of the present application is proposed.
[0141] The difference between the third embodiment of the code compilation method and the first and second embodiments is that, before step S20, the code compilation method further includes a scheme for preprocessing the assembly code. Figure 11 , specifically including:
[0142] Step S201, parsing the assembly code to obtain parsing information of the assembly code;
[0143] Step S202, dividing the assembly code according to the functional statements in the parsed information to generate a code block containing several code lines;
[0144] Step S203, generating a corresponding code line Hash value for each code line in the code block;
[0145] The step of generating a corresponding code line Hash value for each code line in the code block includes:
[0146] Mapping the assembly instructions corresponding to the code lines into binary integers;
[0147] Based on the binary integer, an XOR operation is performed with a preset seed SEED to obtain a Hash value corresponding to the assembly instruction of the code line.
[0148] Step S204 , obtaining the number of instruction bytes in each line of the code block, and sorting the code blocks according to the number of instruction bytes.
[0149] Reference Figure 12 The step of preprocessing the above assembly code can be to parse the assembly code to obtain the instructions, parameter information and instruction byte number of the above assembly code; divide the assembly code into blocks according to the original code to generate N code blocks, and each code block includes a variable code ID, a start line, an end line, the number of lines and the total number of instruction bytes; generate a hash value for the assembly code, and associate it with each code line in the assembly code, and arrange the code blocks in the above assembly code from large to small according to the number of instruction bytes.
[0150] This embodiment preprocesses the assembly code, generates a corresponding hash value for each line of code in the assembly code, and associates the hash value with the code line, thereby improving the feasibility of the preset search algorithm for searching the same target code variable block and improving the efficiency of code merging in the assembly code.
[0151] Furthermore, based on the first, second and third embodiments of the code compilation method of the embodiment of the present application, a fourth embodiment of the code compilation method of the embodiment of the present application is proposed.
[0152] The difference between the fourth embodiment of the code compilation method and the first, second and third embodiments is that, in this embodiment, after step S20, the code compilation method further includes a scheme for optimizing and evaluating the target code variable block, referring to Figure 13 , specifically including:
[0153] Step A, optimizing and evaluating the target code variable block;
[0154] Step B, if the number of instruction bytes of the target code variable block is greater than the number of instruction bytes of the original code block, deleting the target code block;
[0155] Step C: If the number of instruction bytes of the target code variable block is less than the number of instruction bytes of the original code block, retain the target code block.
[0156] In a specific embodiment, the variable blocks V31 and V32 are sorted from large to small according to the number of instruction bytes, and the total number of bytes SUM1 of the variable block V31 and the code blocks C1, C2, and C3 after optimization, the total number of bytes SUM2 of V32 and the code blocks C1, C2, and C3 after optimization, and the total number of bytes SUM3 of V31, V32 and the code blocks C1, C2, and C3 after optimization are evaluated.
[0157] Reference Figure 14 As shown, Figure 14 In the figure, SUM1 is 68 bytes in size, SUM2 and SUM3 are 69 bytes in size, and SUM1, SUM2 and SUM3 are all smaller than the original byte number of the assembly code (92 bytes). In comparison, SUM1 is the smallest, that is, the number of instruction bytes is the smallest and the degree of optimization is the highest. Therefore, the variable block V31 is encapsulated as a macro function to call the target code variable block, and the variable block V32 is deleted.
[0158] Reference Figure 15 , Figure 15 This is a specific operation flow chart of the present invention for encapsulating the target code variable block into a macro function. The optimized number of bytes of the generated Q variable blocks CODE_VAR_BLOCK is evaluated. If it is less than the original number of bytes, it is retained. If it is greater than the original number of bytes, the variable block is deleted. Finally, all retained variable blocks are encapsulated into a macro function, and the lcall, acall, or scall instruction is used to jump to the macro function entry to perform the corresponding operation.
[0159] Furthermore, assuming the variable block has a byte count of X, and that the variable block is composed of three code blocks or variable blocks, the number of bytes before optimization is 3*X, and the number of bytes after optimization is X+1+3*3, where 1 is the number of bytes of the ret instruction and 3 is the number of bytes of the lcall, acall, or scall instruction. Therefore, the method for evaluating the optimized byte count is to determine whether the following condition is true: (3*X)>(X+1+3*3). If so, retain the variable block; otherwise, delete it. Finally, move all code associated with the variable block to the end of the function and mark the beginning of the block with a label generated by the variable block ID. Replace the code range associated with the variable block in the code block with the lcall, acall, or scall instruction.
[0160] This embodiment performs optimization evaluation on the optimized target code variable block and determines that the number of instruction bytes of the optimized target code variable block is smaller than that of the original assembly code, thereby improving the effectiveness of optimizing the assembly code.
[0161] In addition, the embodiment of the present invention also provides a code compilation device, referring to Figure 16 , Figure 16This is a functional module diagram of the code compiling device involved in the embodiment of the code compiling method of the present invention. Figure 16 As shown, the code compiling device includes:
[0162] The code acquisition module 10 is used to obtain the assembly code to be compiled;
[0163] A code search and merging module 20 is configured to obtain target code variable blocks that meet preset standards in the assembly code based on a preset search algorithm;
[0164] A code encapsulation module 30 is used to encapsulate the target code variable block and determine the corresponding macro function;
[0165] The execution compilation module 40 is used to execute code compilation based on the macro function.
[0166] The principles and implementation process of the code compilation method implemented in this embodiment can be referred to the above embodiments and will not be described in detail here.
[0167] In addition, an embodiment of the present invention also proposes a device, which includes a memory, a processor, and a code compiler stored in the memory and runnable on the processor. When the code compiler is executed by the processor, the steps of the code compilation method described in the above embodiment are implemented.
[0168] In addition, to achieve the above-mentioned purpose, the present invention also provides a medium, which is a computer-readable storage medium, and a code compiler is stored on the computer-readable storage medium. When the code compiler is executed by the processor, the steps of the code compilation method described above are implemented.
[0169] Since this code compiler adopts all the technical solutions of all the aforementioned embodiments when executed by the processor, it has at least all the beneficial effects brought by all the technical solutions of all the aforementioned embodiments, which will not be described one by one here.
[0170] It should be noted that, in this document, the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or apparatus 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 apparatus. 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 apparatus comprising the element.
[0171] The serial numbers of the above embodiments of the present invention are for description only and do not represent the advantages or disadvantages of the embodiments.
[0172] Through the description of the above embodiments, those skilled in the art can clearly understand that the above embodiment methods can be implemented by means of software plus the necessary general hardware platform, and of course can also be implemented by hardware, but in many cases the former is a better embodiment. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) as described above, and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present invention.
[0173] The above are only preferred embodiments of the present invention and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention description and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.
Claims
1. A code compilation method, characterized in that: The code compilation method comprises: Get the assembly code to be compiled; Based on a preset search algorithm, a target code variable block that meets a preset standard in the assembly code is obtained, including: obtaining a code line Hash value and a code block corresponding to each line of code in the assembly code, and determining a first order for sorting the code blocks; based on the first order, comparing and searching the code lines according to the code line Hash values to determine a first code variable block that meets the first preset standard in the code blocks; generating a first code variable block Hash value corresponding to the first code variable block, and determining a second order for arranging the first code variable block; based on the second order, comparing and searching the first code variable block with the code block according to the first code variable block Hash value to determine a second code variable block that meets the second preset standard in the first code variable block; and comparing and searching the second code variable block with the Hash value corresponding to the first code variable block to determine the corresponding target code variable block. Performing optimization evaluation on the target code variable block; If the number of instruction bytes of the target code variable block is greater than the number of instruction bytes of the original code block, the target code block is deleted; If the number of instruction bytes of the target code variable block is less than the number of instruction bytes of the original code block, retaining the target code block; Encapsulate the target code variable block and determine the corresponding macro function; Based on the macro function, code compilation is performed.
2. The code compiling method according to claim 1, wherein: Before the step of obtaining the target code variable block that meets the preset standard in the assembly code based on the preset search algorithm, the code compilation method further includes: Parsing the assembly code to obtain parsing information of the assembly code; Divide the assembly code according to the functional statements in the parsed information to generate a code block containing a plurality of code lines; Generate a corresponding code line Hash value for each code line in the code block; The number of instruction bytes in each line of the code block is obtained, and the code blocks are sorted according to the number of instruction bytes.
3. The code compiling method according to claim 2, wherein: The step of generating a corresponding code line Hash value for each code line in the code block includes: Mapping the assembly instructions corresponding to the code lines into binary integers; Based on the binary integer, an XOR operation is performed with a preset seed SEED to obtain a Hash value corresponding to the assembly instruction of the code line.
4. The code compiling method according to claim 1, wherein: The step of encapsulating the target code variable block and determining the corresponding macro function includes: Obtain the target code variable block ID; Based on the target code variable block ID, the function entry of the macro function is determined, and the target code variable block is connected according to the function entry of the macro function to determine the macro function corresponding to the target code variable block.
5. The code compiling method according to claim 4, wherein: The step of executing code compilation based on the macro function includes: Connecting a macro function entry of the macro function based on a preset jump instruction, and executing an internal assembly instruction of the macro function through the macro function entry; Based on the preset return instruction, the assembly statement corresponding to the preset code block jump instruction is returned, and the assembly statement is executed.
6. A code compiling device, characterized in that: The code compiling device comprises: Code acquisition module, used to obtain the assembly code to be compiled; A code search and merging module is configured to obtain a target code variable block that meets a preset standard in the assembly code based on a preset search algorithm, including: obtaining a code line Hash value and a code block corresponding to each line of code in the assembly code, and determining a first order for sorting the code blocks; based on the first order, comparing and searching the code lines according to the code line Hash values to determine a first code variable block that meets the first preset standard in the code blocks; generating a first code variable block Hash value corresponding to the first code variable block, and determining a second order for arranging the first code variable blocks; based on the second order, comparing and searching the first code variable block with the code block according to the first code variable block Hash value to determine a second code variable block that meets the second preset standard in the first code variable block; and comparing and searching the second code variable block with the Hash value corresponding to the first code variable block to determine the corresponding target code variable block. Performing optimization evaluation on the target code variable block; if the number of instruction bytes of the target code variable block is greater than the number of instruction bytes of the original code block, deleting the target code block; if the number of instruction bytes of the target code variable block is less than the number of instruction bytes of the original code block, retaining the target code block; A code encapsulation module is used to encapsulate the target code variable block and determine the corresponding macro function; The execution compilation module is used to execute code compilation based on the macro function.
7. A device, characterized in that The device includes a memory, a processor, and a code compilation program stored in the memory and executable on the processor. When the code compilation program is executed by the processor, the code compilation method according to any one of claims 1 to 5 is implemented.
8. A medium, which is a computer-readable storage medium, characterized in that: The computer-readable storage medium stores a code compilation program, which, when executed by a processor, implements the steps of the code compilation method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Reusable code base creation method, rapid tracing method and system for reusable codes
CN106126235A
Compiling device, compiling method, and program
JP2009134523A
Bytecode instruction set simplification method and system
WO2022134536A1