Program code compilation method, device, electronic device and storage medium
By setting the instruction selection template in the instruction description file of the compiler and using the constant processing instructions of the architecture supported type for compilation, the problem of numerous instructions and performance consumption caused by constant operations in the existing technology is solved, and more efficient processor performance is achieved. .
Patent Information
- Application Number
- CN202111470696.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-03
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2041-12-03
AI Technical Summary
When existing compilers process constant operations, they have many instructions and involve memory access instructions, resulting in high processor performance consumption.
By querying the instruction selection template that matches the program code in the instruction description file, the program code is compiled using constant processing instructions predefined as architecture-supported types, and assembly code is generated, so that the executable file loads the target through constant processing instructions during execution. Constant, reducing memory access instructions.
It significantly reduces the consumption of processor performance, reduces the number of instructions when executing the generated assembly code, and improves processor performance.
Smart Images

Figure CN114237612B
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and in particular, relates to a method, device, electronic device and storage medium for compiling program codes. Background Art
[0002] During the program writing and compiling process, various scenarios involving constant data will arise. The compiler will use different solutions on different processor architectures to generate different instructions for these scenarios, and the performance of the compiled programs will also be different.
[0003] When a compiler in the related art compiles a program code involving constant operation processing, it usually puts the constant data from the corresponding memory address into a constant pool through a memory access instruction when executing it. Then, when the constant is needed, it is read from the constant pool. If the constant data needs to be processed, it is necessary to add processing instructions for the constant data. The whole process not only has many instructions, but also involves memory access instructions, which consumes a lot of processor performance. Summary of the invention
[0004] In view of this, the embodiments of the present application propose a method, device, electronic device and storage medium for compiling program codes, which are used to solve the problem in the prior art that when a program code involving constant operation processing is compiled and executed, there are many instructions and memory access instructions, which consumes a lot of processor performance.
[0005] The present application provides a method for compiling a program code, the method comprising:
[0006] When a program code containing a target constant is obtained, an instruction selection template matching the program code is searched in an instruction description file, wherein the target constant is a constant predefined as a type supported by the architecture;
[0007] The program code is compiled through the constant processing instructions corresponding to the instruction selection template to obtain assembly code, and the executable file of the assembly code loads the target constant through the constant processing instructions when being executed.
[0008] Optionally, searching the instruction description file for an instruction selection template that matches the program code includes:
[0009] The instruction description file is traversed to search for instruction selection templates whose described data operation mode is the same as that of the program code.
[0010] Optionally, when the data operation mode is a floating point number reciprocal operation mode, the target constant includes: a floating point number;
[0011] The process of compiling the program code by using the constant processing instruction corresponding to the instruction selection template to obtain the assembly code includes:
[0012] The floating point number and the variable data in the program code are compiled through the constant processing instruction corresponding to the instruction selection template to obtain the assembly code.
[0013] Optionally, the constant processing instruction includes: an architecture preset floating point number reciprocal processing instruction;
[0014] The constant processing instruction corresponding to the instruction selection template is used to compile the floating point number and the variable data in the program code to obtain the assembly code, including:
[0015] The variable data in the program code is added to the floating point number reciprocal processing instruction to obtain an assembly code.
[0016] Optionally, the constant processing instruction includes: a floating point number division instruction, and the constant processing instruction corresponding to the instruction selection template compiles the floating point number and the variable data in the program code to obtain an assembly code, including:
[0017] The floating point number division instruction is compiled into a floating point number 1.0 divided by the variable data in the program code to obtain an assembly code.
[0018] Optionally, when the data operation mode is a floating point immediate operation mode, the target constant includes: an integer, and the constant processing instruction includes an integer to floating point conversion instruction;
[0019] The process of compiling the program code by using the constant processing instruction corresponding to the instruction selection template to obtain the assembly code includes:
[0020] The integer is added to the integer-to-floating-point conversion instruction to obtain assembly code.
[0021] Optionally, before searching an instruction description file for an instruction selection template matching the program code when the program code containing the target constant is obtained, the method further includes:
[0022] Acquire the instruction description file including the instruction selection template;
[0023] When a constant exists in the instruction selection template, the constant is defined as a target constant of an architecture-supported type through a reduction interface preset by the compiler;
[0024] Rebuild the compiler.
[0025] According to a second aspect of the present application, a program code compiling device is provided, the device comprising:
[0026] An input module, configured to query an instruction selection template matching the program code in an instruction description file when a program code containing a target constant is obtained, wherein the target constant is a constant predefined as a type supported by the architecture;
[0027] A compiling module is used to compile the program code through the constant processing instruction corresponding to the instruction selection template to obtain the assembly code. When the executable file of the assembly code is executed, the target constant is loaded through the constant processing instruction.
[0028] Optionally, the input module is further used for:
[0029] The instruction description file is traversed to search for instruction selection templates whose described data operation mode is the same as that of the program code.
[0030] Optionally, when the data operation mode is a floating point number reciprocal operation mode, the target constant includes: a floating point number;
[0031] The input module is also used for:
[0032] The floating point number and the variable data in the program code are compiled through the constant processing instruction corresponding to the instruction selection template to obtain the assembly code.
[0033] Optionally, the constant processing instruction includes: an architecture preset floating point number reciprocal processing instruction;
[0034] The input module is also used for:
[0035] The variable data in the program code is added to the floating point number reciprocal processing instruction to obtain an assembly code.
[0036] Optionally, the constant processing instruction includes: a floating point division instruction, and the input module is further used to:
[0037] The floating point number division instruction is compiled into a floating point number 1.0 divided by the variable data in the program code to obtain an assembly code.
[0038] Optionally, when the data operation mode is a floating point immediate operation mode, the target constant includes: an integer, and the constant processing instruction includes an integer to floating point conversion instruction;
[0039] The input module is also used for:
[0040] The integer is added to the integer-to-floating-point conversion instruction to obtain assembly code.
[0041] Optionally, the device further includes: a configuration module, configured to:
[0042] Acquire the instruction description file including the instruction selection template;
[0043] When a constant exists in the instruction selection template, the constant is defined as a target constant of an architecture-supported type through a reduction interface preset by the compiler;
[0044] Rebuild the compiler.
[0045] The present application provides an electronic device, including a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the method for compiling the program code described in the first aspect above.
[0046] The present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the method for compiling the program code described in the first aspect above is implemented.
[0047] With respect to the prior art, this application has the following advantages:
[0048] The present application provides a method, device, electronic device and storage medium for compiling program codes. The scheme sets a target constant as an architecture support type in advance, and then sets an instruction selection template in the instruction description file of the compiler to set a corresponding constant processing instruction for the program code involving the target constant, so that the compiler uses the constant processing instruction to assemble the program code during the compilation process, so that the target constant can be loaded through the constant processing instruction when the executable file of the assembly code is executed, saving the memory access instruction for loading the target constant from the constant pool, and reducing the consumption of processor performance when the program involving the constant is executed.
[0049] The above description is only an overview of the technical solution of the present application. In order to more clearly understand the technical means of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0051] Figure 1It is a flowchart of the steps of a method for compiling a program code provided in an embodiment of the present application;
[0052] Figure 2 It is a flowchart of the steps of a compiler configuration method provided in an embodiment of the present application;
[0053] Figure 3 It is a structural block diagram of a program code compiling device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0054] The exemplary embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present application and to fully convey the scope of the present application to those skilled in the art.
[0055] Figure 1 The present invention provides a flowchart of a method for compiling a program code, the method comprising:
[0056] Step 101, when a program code containing a target constant is obtained, an instruction selection template matching the program code is searched in an instruction description file, wherein the target constant is a constant pre-defined as a type supported by the architecture.
[0057] In the related art, the assembly code obtained by compiling the program code involving constants needs to load the constants from the constant pool when executing, and then load the constants from the memory location corresponding to the constant pool to the register when the program is executed. If the constants need to be operated again, the constants need to be operated. For example, when the assembly code for taking the reciprocal of a floating-point number is compiled based on the MIPS architecture and the clang compiler, part of the assembly code obtained by the assembly method in the related art is as follows:
[0058] 1: lui $1,%hi(%neg(%gp_rel(test1)))
[0059] 2: daddu $1,$1,$25
[0060] 3: daddiu $1,$1,%lo(%neg(%gp_rel(test1)))
[0061] 4:ld $1,%got_page(.Lcpi0_0)($1)
[0062] 5:ld $f0,%got_ofst(.Lcpi0_0)($1)
[0063] 6: div.s $f0,$f0,$f12
[0064] First, instructions 1 to 4 are used to calculate the memory address of the constant pool. Then, instruction 5 loads the floating-point constant 1.0 from the corresponding memory into the floating-point register $f0. Five instructions are required just to load the floating-point constant. Further operations on the constants require a division instruction to be added afterwards. In other words, instruction 6 is used to take the reciprocal of the floating-point parameter a. There are a total of six instructions, which consumes more processor performance.
[0065] Therefore, the embodiment of the present application is to reduce the problem that the execution file of the obtained assembly code consumes more processor performance when being executed, and the assembly process of the compiler is improved. Specifically, the execution subject of the embodiment of the present application is a compiler. Usually the compiler includes a front end and a back end, wherein the front end is to parse the source code of the input high-level language such as C language, C++, etc., and then sequentially generate intermediate code through lexical analysis, syntax analysis, and semantic analysis, and the back end will further optimize the intermediate code and convert it into the assembly code of the assembly language. And the embodiment of the present application is implemented in the process of optimizing the intermediate code for the compiler, so the program code can refer to the intermediate code of the compiler. When the compiler optimizes the intermediate code, the instructions in the intermediate code can be mapped to the required instructions based on the instruction mapping relationship in the instruction description file, and the instruction mapping relationship can be specifically set by the developer for the compiler. Therefore, in order to improve the assembly code of the program code involving the operation processing of data by the target constant, or the instruction of the assembly code of the program code that needs to load the target constant, the instruction selection template for describing the specific data processing operation mode can be set in the instruction description file. The instruction selection template includes a correspondence between a program code template and a constant processing instruction, wherein the constant processing instruction is an original instruction in the compiler for operating and processing a constant.
[0066] It should be noted that the target constants pre-defined as architecture-supported types will be ignored during the compiler's enumeration process, so that the target constants will not be loaded from the constant pool, and there will be no process to access the content, and there is no need to execute access instructions. Specifically, the constants can be pre-defined through the public interface provided by the compiler, so that the target constants become target constants that are natively supported by the compiler's architecture. Of course, the target constants can be specifically set by the architecture default or by the user, and the embodiments of the present application do not limit them.
[0067] In actual applications, when the compiler compiles the instructions in the program code, it will query the target instructions to be mapped in the instruction description file. The embodiment of the present application sets an instruction selection template containing the correspondence between instruction style and constant processing in the instruction description file for the instructions involving the target constants. Therefore, when the instruction description file is traversed for the instructions containing the target constants, the instruction template matching the instruction will be queried, and the instruction selection template where the instruction template is located will be determined.
[0068] Step 102, compile the program code through the constant processing instruction corresponding to the instruction selection template to obtain assembly code, and when the executable file of the assembly code is executed, the target constant is loaded through the constant processing instruction.
[0069] In an embodiment of the present application, the compiler maps the original instructions in the program code by using the constant processing instructions in the instruction selection template to obtain the assembly code of the program code. When the executable file obtained by linking the assembly code is executed, the processor will ignore the target constant during the enumeration process, and directly load the target constant and / or the data obtained by operating the target constant by executing the constant processing instructions. There is no need to load the target constant from the constant pool to the register and no need to fetch the target constant from the memory through the memory access instruction.
[0070] Exemplarily, still taking the program code for obtaining the reciprocal of a floating point number as an example, the assembly code obtained by assembling the embodiment of the present application is as follows:
[0071] 1; addiu $1,$zero,1
[0072] 2: mtc1 $1, $f0
[0073] 3: cvt.sw $f0,$f0
[0074] 4: jr $ra
[0075] 5: recip.s $f0,$f12
[0076] Among them, the first instruction is to put the integer 1 into the No. 1 fixed-point register, the second instruction is to put the data in the No. 1 register into the f0 floating-point register, the third is to convert the data of the f0 floating-point register into a floating-point number, the fourth is a jump instruction, and the fifth is a constant processing instruction for taking the reciprocal of a floating-point number. It can be seen that the compiler of the embodiment of the present application is no longer loaded through the constant pool, but is loaded through the floating-point / integer conversion instruction (cvt.sw), and the reciprocal of the floating-point number is realized by using the recip.s instruction of MIPS (a processor architecture that adopts a reduced instruction set). The number of static instructions is reduced compared to before, specifically the instructions required to load floating-point constants are reduced from 5 to 3, and the instructions for processing floating-point constants to obtain data are reduced from 6 to 5 in total, and do not involve memory access instructions, which significantly reduces the processor performance required to consume.
[0077] Further, for program codes that need to call a smaller number of target constants more often, for example, when the number of calls to 5, 10, or other different target constants exceeds a threshold of 1,000 or 10,000 calls, the program code is compiled by the program code compilation method described in the embodiment of the present application. While the number of instructions that need to be executed during the operation of the program code can be significantly reduced, the traversal process of the instruction description file after the instruction selection template corresponding to different target constants is expanded due to the small number of target constants. The impact on processor performance is also relatively light, so that after such program code is compiled by the program code compilation method described in the embodiment of the present application, the processor performance consumed by the program code operation can be more significantly reduced. In addition, in addition to the above situation, for programs that are called and executed in units of functions, constant information is obtained for hot functions in the program, and the constants in the hot functions are used as target functions, which can also more significantly reduce the processor performance consumed by the program code operation.
[0078] The embodiment of the present application sets the target constant to the architecture support type in advance, and then sets the instruction selection template in the instruction description file of the compiler to set the corresponding constant processing instruction for the program code involving the target constant, so that the compiler uses the constant processing instruction to assemble the program code during the compilation process, so that when the executable file of the assembly code is executed, the target constant can be loaded through the constant processing instruction, which saves the memory access instruction for loading the target constant from the constant pool and reduces the consumption of processor performance when the program involving the constant is executed.
[0079] Optionally, the step 101 may include: traversing the instruction description file to search for an instruction selection template that describes a data operation mode that is the same as the program code.
[0080] In an embodiment of the present application, the data operation mode can be a processing function included in the instruction, for example, 1.0f / a is used to describe the division operation mode between the floating point number 1.0 and the variable a, and can also be 1.0f-2.0f is used to describe the subtraction operation mode between the floating point number 1.0 and the floating point number 2.0, and can also be 1.0f, only for the loading operation of the floating point number 1.0, of course, this is only an exemplary description, and is not limited here. The embodiment of the present application can accurately identify instructions involving constants in the program code by setting an instruction selection template that describes the data operation mode in the instruction description file.
[0081] Optionally, when the data operation mode is a floating-point reciprocal operation mode, the target constant includes: a floating-point number, and the step 101 may include: selecting a constant processing instruction corresponding to a template through an instruction, compiling the floating-point number and the variable data in the program code to obtain an assembly code.
[0082] In the embodiment of the present application, the floating point number reciprocal operation mode refers to the variable data for a specific floating point number for the reciprocal operation mode, for example, a / 2.0f is the variable a for the floating point number 2.0 for the reciprocal, a / 1.0f is the variable for the floating point number 1.0 for the reciprocal. When the data operation mode is the floating point number reciprocal operation mode, the assembly code of the program code will involve floating point numbers. In order to avoid loading floating point numbers from the constant pool, it is necessary to define the floating point number as an architecture support type in advance, so that the compiler ignores the floating point number when enumerating, avoiding the compiler from putting the floating point number into the constant pool.
[0083] The embodiment of the present application reduces the processor performance consumed when the program code executes the floating-point reciprocal operation by setting the data operation mode to the floating-point reciprocal operation mode.
[0084] Optionally, the constant processing instruction includes: an architecture-preset floating-point number reciprocal processing instruction, and the step 101 may include: adding variable data in the program code to the floating-point number reciprocal processing instruction to obtain an assembly code.
[0085] In the embodiment of the present application, the floating-point reciprocal processing instruction preset by the architecture is an instruction for taking the reciprocal of a floating-point number that is already in the instruction set provided by the processor architecture used by the compiler, for example, the recip.s instruction in the MIPS architecture can be used to directly take the reciprocal of a floating-point number. For this architecture, the program code can be traversed directly by taking the floating-point reciprocal processing instruction. Taking the recip.s instruction in the MIPS architecture as an example, for variables stored in registers, the assembly code can be written as "recip.s$f0,$f12", that is, the reciprocal of the floating-point number stored in the floating-point register $f12 is calculated, and the calculation result is stored in the floating-point register $f0.
[0086] The embodiment of the present application modifies the instruction description file through the floating point reciprocal processing instruction preset by the architecture, without the need for additional expansion of the data set, thereby reducing the workload required to implement the compilation method of the program code described in the present application.
[0087] Optionally, the constant processing instruction includes: a floating point division instruction, and the step 101 may include: compiling the floating point division instruction into a form of a floating point number 1.0 divided by variable data in the program code to obtain an assembly code.
[0088] In the embodiment of the present application, the floating point division instruction is a division instruction for floating point numbers provided by the processor architecture used by the compiler, and such simple arithmetic instructions are generally present in various processor architectures. For a processor architecture that is not used to obtain the reciprocal of a floating point number, a constant processing instruction can be set by utilizing the floating point division instruction provided in the processor architecture, that is, the processing operation of obtaining the reciprocal of a floating point number can be implemented by dividing the floating point number 1.0 by the variable data in the program code.
[0089] The embodiment of the present application is directed to a processor architecture whose instruction set does not include an available floating-point reciprocal processing instruction. The variable data in the program code is compiled through the floating-point division instruction in the instruction set, which not only reduces the workload required to implement the compilation method of the program code described in the present application, but also improves the scope of application of the compilation method of the program code described in the embodiment of the present application, and can be applicable to various different processor architectures.
[0090] Optionally, when the data operation mode is a floating point immediate operation mode, the target constant includes: an integer, and the step 101 may include: adding the integer to the integer to floating point conversion instruction to obtain assembly code.
[0091] In the embodiment of the present application, the operation mode of taking floating-point immediate numbers refers to the operation mode of loading and moving floating-point numbers. When loading floating-point immediate numbers, the floating-point numbers and the integers corresponding to the floating-point numbers can be defined in advance as architecture-supported types. Thus, the program code can be assembled through the integer-to-floating-point conversion instructions in the instruction set of the processor architecture, such as the cvt.sw and mtc1 instructions in the MIPS architecture, and the resulting assembly code can be "cvt.sw$f0,$f0", that is, the value in the $f0 floating-point register is converted to the corresponding floating-point number according to the integer, for example, the integer 1 is converted to the floating-point number 1.0.
[0092] The embodiment of the present application compiles the processing operation of fetching floating-point immediate numbers in the program code through the integer-to-floating-point instruction in the instruction set, thereby reducing the workload required to implement the program code compilation method described in the present application.
[0093] Optionally, Figure 2 A flowchart of a method for configuring a compiler provided by an embodiment of the present application is shown, the method comprising:
[0094] Step 201: Obtain the instruction description file including the instruction selection template.
[0095] In an embodiment of the present application, the developer may input the instruction description file by modifying the source code of the compiler or the internal implementation of the compiler to perform editing operations such as adding, deleting, modifying, and checking the instruction description file.
[0096] Step 202: When a constant exists in the instruction selection template, define the constant as a target constant of an architecture-supported type through a reduction interface preset by the compiler.
[0097] In an embodiment of the present application, when it is detected that an instruction description file is updated or a new instruction description file is detected, the instruction selection template in the instruction description file is traversed to see if there is a constant. If so, the constant is defined as an architecture support type through the compiler's reduction interface to legalize the target constant. For example: the reduction interface isFPImmLegal() provided by the LLVM (a framework system for building compilers) compiler, the interface isFPImmLegal() is used to describe which floating point numbers are natively supported by the target architecture and which floating point numbers need to be loaded through the constant pool. Of course, this is just an exemplary description, and the specific reduction interface selection can be set according to actual needs, which is not limited here.
[0098] Furthermore, the frequency of occurrence of different constants in the instruction selection template or the frequency of calls required in the program code can be counted, and the constants whose frequency of occurrence and / or call frequency is greater than the frequency threshold can be defined as target constants of the architecture-supported type through the reduction interface preset by the compiler, thereby controlling the number of legalized constants to avoid the situation where the processor performance required to traverse and query constants is too high due to the excessive number of legalized constants. Alternatively, the constant information in the hotspot functions in the program code can be counted, and the constant with the highest frequency of use in the constant information in the hotspot functions can be used as the target constant, and the constant can be defined as a target constant of the architecture-supported type through the reduction interface preset by the compiler.
[0099] Step 203, rebuild the compiler.
[0100] In the embodiment of the present application, after modifying the source code of the compiler or the internal implementation of the compiler, the compiler can be rebuilt to enable the compiler to run the compilation method of the program code described in the embodiment of the present application.
[0101] The embodiment of the present application sets the target constant to an architecture-supported type through a compiler lowering interface to achieve legalization of the target constant, so that the target constant does not need to be loaded from a constant pool, thereby reducing the processor performance consumed by the execution process of operation processing instructions involving constants.
[0102] Figure 3 1 is a structural block diagram of a program code compiling device 30 provided in an embodiment of the present application, the device comprising:
[0103] An input module 301 is used to query an instruction selection template matching the program code in an instruction description file when a program code containing a target constant is obtained, wherein the target constant is a constant predefined as a type supported by the architecture;
[0104] The compiling module 302 is used to compile the program code through the constant processing instructions corresponding to the instruction selection template to obtain assembly code. When the executable file of the assembly code is executed, the target constant is loaded through the constant processing instructions.
[0105] Optionally, the input module 301 is further used for:
[0106] The instruction description file is traversed to search for instruction selection templates whose described data operation mode is the same as that of the program code.
[0107] Optionally, when the data operation mode is a floating point number reciprocal operation mode, the target constant includes: a floating point number;
[0108] The input module 301 is further used for:
[0109] The floating point number and the variable data in the program code are compiled through the constant processing instruction corresponding to the instruction selection template to obtain the assembly code.
[0110] Optionally, the constant processing instruction includes: an architecture preset floating point number reciprocal processing instruction;
[0111] The input module 301 is further used for:
[0112] The variable data in the program code is added to the floating point number reciprocal processing instruction to obtain an assembly code.
[0113] Optionally, the constant processing instruction includes: a floating point division instruction, and the input module 301 is further used to:
[0114] The floating point number division instruction is compiled into a floating point number 1.0 divided by the variable data in the program code to obtain an assembly code.
[0115] Optionally, when the data operation mode is a floating point immediate operation mode, the target constant includes: an integer, and the constant processing instruction includes an integer to floating point conversion instruction;
[0116] The input module 301 is further used for:
[0117] The integer is added to the integer-to-floating-point conversion instruction to obtain assembly code.
[0118] Optionally, the device further includes: a configuration module, configured to:
[0119] Acquire the instruction description file including the instruction selection template;
[0120] When a constant exists in the instruction selection template, the constant is defined as a target constant of an architecture-supported type through a reduction interface preset by the compiler;
[0121] Rebuild the compiler.
[0122] The embodiment of the present application sets the target constant to the architecture support type in advance, and then sets the instruction selection template in the instruction description file of the compiler to set the corresponding constant processing instruction for the program code involving the target constant, so that the compiler uses the constant processing instruction to assemble the program code during the compilation process, so that when the executable file of the assembly code is executed, the target constant can be loaded through the constant processing instruction, which saves the memory access instruction for loading the target constant from the constant pool and reduces the consumption of processor performance when the program involving the constant is executed.
[0123] An embodiment of the present application provides an electronic device, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements any of the above-mentioned program code compilation methods.
[0124] An electronic device provided by an embodiment of the present application sets a target constant as an architecture support type in advance, and then sets an instruction selection template in an instruction description file of a compiler to set a corresponding constant processing instruction for a program code involving the target constant, so that the compiler assembles the program code using the constant processing instruction during the compilation process, so that when an executable file of the assembly code is executed, the target constant can be loaded through the constant processing instruction, thereby saving memory access instructions for loading the target constant from a constant pool and reducing the consumption of processor performance when executing programs involving constants.
[0125] An embodiment of the present application provides a computer storage medium on which a computer program is stored. When the computer program is executed by a processor, the above-mentioned program code compiling method can be implemented.
[0126] An embodiment of the present application provides a computer storage medium, which sets a target constant as an architecture support type in advance, and then sets an instruction selection template in an instruction description file of a compiler to set a corresponding constant processing instruction for a program code involving the target constant, so that the compiler uses the constant processing instruction to assemble the program code during the compilation process, so that when an executable file of the assembly code is executed, the target constant can be loaded through the constant processing instruction, thereby saving memory access instructions for loading the target constant from a constant pool and reducing the consumption of processor performance when executing programs involving constants.
[0127] It will be appreciated by those skilled in the art that the present application includes devices for performing one or more of the operations described in the present application. These devices may be specially designed and manufactured for the desired purpose, or may also include known devices in general-purpose computers. These devices have computer programs stored therein, which are selectively activated or reconstructed. Such computer programs may be stored in a storage medium of a device (e.g., a computer) or in any type of medium suitable for storing electronic instructions and coupled to a bus, respectively, the computer storage medium including but not limited to any type of disk (including floppy disk, hard disk, optical disk, CD-ROM and magneto-optical disk), ROM (Read-Only Memory), RAM (Random Access Memory), EPROM (Erasable Programmable Read-Only Memory), EEPROM (Electrically Erasable Programmable Read-Only Memory), flash memory, magnetic card or optical card. That is, the storage medium includes any medium that stores or transmits information in a readable form by a device (e.g., a computer).
[0128] Those skilled in the art will appreciate that each frame in these structure diagrams and / or block diagrams and / or flow charts and the combination of frames in these structure diagrams and / or block diagrams and / or flow charts can be implemented with computer program instructions. Those skilled in the art will appreciate that these computer program instructions can be provided to a processor of a general-purpose computer, a professional computer or other programmable program code compilation method to implement, thereby executing the scheme specified in the frame or multiple frames of the structure diagram and / or block diagram and / or flow chart disclosed in the present application through the processor of the computer or other programmable program code compilation method.
[0129] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A method for compiling a program code, characterized in that: The method comprises: When a program code containing a target constant is obtained, an instruction selection template matching the program code is searched in an instruction description file, wherein the target constant is a constant predefined as a type supported by an architecture, and the target constant will not be loaded from a constant pool during the enumeration process of the compiler; The program code is compiled by using the constant processing instruction corresponding to the instruction selection template to obtain assembly code, and the executable file of the assembly code loads the target constant by using the constant processing instruction when being executed; When the data operation mode is the reciprocal operation mode of taking a floating point number, the target constant includes: a floating point number; the program code is compiled through the constant processing instruction corresponding to the instruction selection template to obtain the assembly code, including: the floating point number and the variable data in the program code are compiled through the constant processing instruction corresponding to the instruction selection template to obtain the assembly code.
2. The method according to claim 1, characterized in that The step of searching the instruction description file for an instruction selection template that matches the program code includes: The instruction description file is traversed to search for instruction selection templates whose described data operation mode is the same as that of the program code.
3. The method according to claim 1, characterized in that The constant processing instructions include: a floating point number reciprocal processing instruction preset by the architecture; The constant processing instruction corresponding to the instruction selection template is used to compile the floating point number and the variable data in the program code to obtain the assembly code, including: The variable data in the program code is added to the floating point number reciprocal processing instruction to obtain an assembly code.
4. The method according to claim 1, characterized in that: The constant processing instruction includes: a floating point number division instruction, and the constant processing instruction corresponding to the instruction selection template compiles the floating point number and the variable data in the program code to obtain an assembly code, including: The floating point number division instruction is compiled into a floating point number 1.0 divided by the variable data in the program code to obtain an assembly code.
5. The method according to claim 2, characterized in that: When the data operation mode is a floating point immediate operation mode, the target constant includes: an integer, and the constant processing instruction includes an integer to floating point conversion instruction; The process of compiling the program code by using the constant processing instruction corresponding to the instruction selection template to obtain the assembly code includes: The integer is added to the integer-to-floating-point conversion instruction to obtain assembly code.
6. The method according to claim 1, characterized in that Before searching the instruction description file for an instruction selection template matching the program code when the program code containing the target constant is obtained, the method further includes: Acquire the instruction description file including the instruction selection template; When a constant exists in the instruction selection template, the constant is defined as a target constant of an architecture-supported type through a reduction interface preset by the compiler; Rebuild the compiler.
7. A program code compiling device, characterized in that: The device comprises: an input module, for searching an instruction selection template matching the program code in an instruction description file when a program code containing a target constant is obtained, wherein the target constant is a constant predefined as a type supported by the architecture, and the target constant will not be loaded from a constant pool during the enumeration process of the compiler; A compiling module, used for compiling the program code through the constant processing instruction corresponding to the instruction selection template to obtain an assembly code, wherein the executable file of the assembly code loads the target constant through the constant processing instruction when being executed; When the data operation mode is the reciprocal operation mode of taking floating point numbers, the target constant includes: a floating point number; the input module is also used to: compile the floating point number and the variable data in the program code through the constant processing instruction corresponding to the instruction selection template to obtain an assembly code.
8. An electronic device, characterized in that: The method comprises a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the method for compiling a program code as claimed in any one of claims 1 to 6.
9. 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 for compiling the program code according to any one of claims 1 to 6 is implemented.
