CN112558984A - Code compiling method and device, electronic equipment and server - Google Patents

Code compiling method and device, electronic equipment and server Download PDF

Info

Publication number
CN112558984A
CN112558984A CN202011595414.5A CN202011595414A CN112558984A CN 112558984 A CN112558984 A CN 112558984A CN 202011595414 A CN202011595414 A CN 202011595414A CN 112558984 A CN112558984 A CN 112558984A
Authority
CN
China
Prior art keywords
code
basic block
compiling
llvm
byte
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202011595414.5A
Other languages
Chinese (zh)
Inventor
胡岳伟
吴价
刘圆圆
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Guangdong Oppo Mobile Telecommunications Corp Ltd
Original Assignee
Guangdong Oppo Mobile Telecommunications Corp Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Guangdong Oppo Mobile Telecommunications Corp Ltd filed Critical Guangdong Oppo Mobile Telecommunications Corp Ltd
Priority to CN202011595414.5A priority Critical patent/CN112558984A/en
Publication of CN112558984A publication Critical patent/CN112558984A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

The embodiment of the application discloses a code compiling method and device, electronic equipment and a server. The method comprises the following steps: acquiring a source code to be compiled in an operating system; calling a first compiling module to compile the source code into intermediate byte codes; generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to the input data of the second compiling module, and the second compiling module is an LLVM compiling module; and calling a second compiling module to compile the intermediate code into machine code. Therefore, the generated intermediate byte code can be compiled into the machine code by the LLVM compiling module in a mode of converting the intermediate byte code obtained by compiling the source code into the intermediate code in the corresponding format of the input data of the LLVM compiling module, so that the quality of the obtained machine code is promoted by an excellent optimizer in the LLVM compiling module, the execution efficiency of electronic equipment executing the machine code is promoted, and the probability of the machine code being stuck in the execution process is reduced.

Description

Code compiling method and device, electronic equipment and server
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method and an apparatus for code compiling, an electronic device, and a server.
Background
The code of the operating system may be composed of a variety of codes. For example, the code of the Android system mainly consists of C/C + + and Java, for example, the code of the Android Framework layer in the Android system is mainly written in Java language. To facilitate the electronic device running an operating system, the code of the operating system may be compiled to obtain machine code that the electronic device can execute. However, the machine code obtained by the related compiling method has a problem of poor execution efficiency.
Disclosure of Invention
In view of the above problems, the present application provides a method, an apparatus, an electronic device, and a server for code compilation to improve the above problems.
In a first aspect, the present application provides a method for compiling code, applied to a server, the method including: acquiring a source code to be compiled in an operating system; calling a first compiling module to compile the source code into intermediate byte codes; generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module; and calling the second compiling module to compile the intermediate code into machine code.
In a second aspect, the present application provides an apparatus for compiling code, running on a server, the apparatus comprising: the source code acquisition unit is used for acquiring a source code to be compiled in an operating system; the first compiling unit is used for calling a first compiling module to compile the source code into intermediate byte codes; the intermediate code generating unit is used for generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module; and the second compiling unit is used for calling the second compiling module to compile the intermediate code into machine code.
In a third aspect, the present application provides a server comprising one or more processors and a memory; one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs configured to perform the methods described above.
In a fourth aspect, the present application provides a computer-readable storage medium having program code stored therein, wherein the method described above is performed when the program code is executed.
According to the code compiling method, the code compiling device, the electronic equipment and the server, a source code to be compiled in an operating system is obtained firstly, a first compiling module is called to compile the source code into an intermediate byte code, then based on the intermediate byte code, an intermediate code in a specified format corresponding to input data of a second compiling module is generated, wherein the second compiling module is an LLVM compiling module, and then the second compiling module is called to compile the intermediate code into machine code. Therefore, the generated intermediate byte code can be compiled into the machine code by the LLVM compiling module in a mode of converting the intermediate byte code obtained by compiling the source code into the intermediate code in the corresponding format of the input data of the LLVM compiling module, so that the quality of the obtained machine code is favorably improved, and the execution efficiency of the electronic equipment executing the machine code is improved.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed to be used in the description of the embodiments are briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and it is obvious for those skilled in the art to obtain other drawings based on these drawings without creative efforts.
Fig. 1 is a schematic diagram illustrating an operating environment of a method for compiling code according to an embodiment of the present application;
FIG. 2 is a flow chart illustrating a method for compiling code according to an embodiment of the present disclosure;
FIG. 3 is a schematic diagram illustrating a process from source code to machine code in an embodiment of the present application;
FIG. 4 is a flow chart illustrating a method of code compilation as set forth in another embodiment of the present application;
FIG. 5 is a diagram illustrating the conversion of subcodes in basic blocks in an embodiment of the present application;
FIG. 6 is a flow chart illustrating a method of code compilation according to yet another embodiment of the present application;
FIG. 7 is a flow chart illustrating a method of code compilation as set forth in another embodiment of the present application;
FIG. 8 is a block diagram of a code compiling apparatus according to the present application;
fig. 9 shows a block diagram of an electronic device for executing a method of code compilation according to an embodiment of the present application.
Fig. 10 is a storage unit for storing or carrying program codes for implementing an image processing method according to an embodiment of the present application.
Detailed Description
The technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
An Operating System (OS) is a computer program that manages computer hardware and software resources. The operating system needs to handle basic transactions such as managing and configuring memory, prioritizing system resources, controlling input devices and output devices, operating the network, and managing the file system. And various codes may be included in the operating system. For example, the code of the Android system mainly consists of C/C + + and Java, for example, the code of the Android Framework layer in the Android system is mainly written in Java language.
In order to facilitate the electronic device with the operating system to run the operating system, source codes in the operating system can be compiled to obtain machine codes which can be directly executed by the electronic device. For example, for Java code included in the operating system, Java Compiler front end (Java Compiler, Java) may be used to compile the Java code into class (Java bytecode), then use dx/d8 to translate the class (Java bytecode) into dex bytecode, and then use dex2oat to perform advanced compilation on the dex bytecode to generate machine code.
However, the inventor has found that, in the research on the related compiling process, the machine code obtained based on the related compiling method has a problem of poor execution efficiency. Specifically, in the related compiling mode, the dex2oat is used to compile the dex byte code in advance to generate the machine code, but the dex2oat is designed for the electronic device, and considering the problem of limited resources of the mobile phone, the optimization of the machine code generated in the compiling process of the dex2oat is insufficient, which causes poor quality of the generated machine code, and the electronic device has a problem of high probability of being stuck in the executing process.
Therefore, a method, a device, an electronic device and a server for applying for code compilation are provided, in which a source code to be compiled in an operating system is obtained first, a first compiling module is called to compile the source code into an intermediate byte code, and then based on the intermediate byte code, an intermediate code in a specified format corresponding to input data of a second compiling module is generated, wherein the second compiling module is an LLVM compiling module, and then the second compiling module is called to compile the intermediate code into a machine code. Therefore, the generated intermediate byte code can be compiled into the machine code by the LLVM compiling module in a mode of converting the intermediate byte code obtained by compiling the source code into the intermediate code in the corresponding format of the input data of the LLVM compiling module, so that the quality of the obtained machine code is favorably improved, and the execution efficiency of the electronic equipment executing the machine code is improved.
A scenario related to the embodiment of the present application will be described below.
The code compiling method provided by the embodiment of the present application may be executed in a single server, or may be executed by multiple servers, for example, as shown in fig. 1, the code compiling method may be executed by the first server 110 and the second server 120 together. In this manner, the first server 110 and the second server 120 collectively operate to obtain the source code to be compiled in the operating system; calling a first compiling module to compile the source code into intermediate byte codes; then, the second server 120 executes subsequent intermediate codes based on the intermediate byte codes to generate intermediate codes in a specified format, where the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module; and calling the second compiling module to compile the intermediate code into machine code. In addition, it may also be configured that each step of the method for compiling codes provided by the embodiment is executed by a separate server.
The first server 110 and the second server 120 may be independent physical servers, may also be a server cluster or a distributed system formed by a plurality of physical servers, and may also be cloud servers providing basic cloud computing services such as cloud service, cloud databases, cloud computing, cloud functions, cloud storage, network service, cloud communication, middleware service, domain name service, security service, CDN, big data and artificial intelligence platforms, and the like. The electronic device running the finally output machine code may be external to the smart phone, or may be a tablet computer, a notebook computer, a desktop computer, a smart watch, or the like, but is not limited thereto.
The present disclosure will now be described with reference to specific embodiments.
Referring to fig. 2, the present application provides a method for code compilation, applied to a server, the method including:
s110: and acquiring a source code to be compiled in an operating system.
It can be understood that the operating system may be understood as an operating system to be compiled, and the code compiling method provided in this embodiment is compiled for a source code included in the operating system, so that the source code to be compiled in the operating system to be compiled may be obtained first at a start stage.
Optionally, in this embodiment, the source code to be compiled in the operating system may be a part of the source code included in the operating system, or may be all the source codes included in the operating system.
S120: and calling a first compiling module to compile the source code into intermediate byte codes.
Where bytes are understood to be units of data volume in a computer, and Byte-code (Byte-code) is a binary file containing an executive, consisting of a sequence of op code/data pairs, an intermediate code. The intermediate bytecode in this embodiment may be class (Java bytecode) or dex bytecode.
It should be noted that, if the type of the middle bytecode is different, the corresponding first compiling module is also different. Alternatively, if the middle bytecode is class (Java bytecode), the first compiling module may be Java, and if the middle bytecode is dex bytecode, the first compiling module may be dx/d 8.
S130: and generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to the input data of the second compiling module, and the second compiling module is an LLVM compiling module.
It should be noted that the compiling modules used in different compiling stages in this embodiment are different. Correspondingly, the format of the input data may be different for different compiling modules. Then, if the format requirement of the input data of the LLVM (low Level Virtual machine) compiling module is met, an intermediate code of a corresponding specified format may be generated based on the intermediate bytecode, where the intermediate code of the specified format in this embodiment may be LLVM IR.
The LLVM is a framework system for constructing a compiler (compiler), is written in C + +, and is used to optimize the compile-time, link-time, run-time, and idle-time of a program written in any programming language. The LLVM core library provides compiler-related support that can be used as the background for multi-language compilers. The method can perform compilation period optimization, link optimization, online compilation optimization and code generation of the program language.
Then in this embodiment, based on the foregoing, the intermediate code that generates the specified format may be LLVM IR.
S140: and calling the second compiling module to compile the intermediate code into machine code.
The machine code is a code that can be directly recognized and executed subsequently by the electronic device. Optionally, the machine code generated in this embodiment may be a file in an odex format.
It should be noted that, in this embodiment, the first compiling module and the second compiling module may be two compilers independent of each other. It may also be two modules of the same compiler, in this way two modules of the same compiler, the first compiler module may be understood as the compiler front-end and the second compiler module as the compiler back-end.
As shown in fig. 3, in the embodiment of the present application, a process from source code to machine code may include performing lexical analysis, syntactic analysis, and semantic analysis on the source code in sequence to obtain a syntax tree. Then, the high-order IR (LLVM IR) is obtained by inputting the high-order IR into a high-order optimizer based on the syntax tree for processing, then the high-order IR is input into a low-order optimizer for processing to obtain a low-order IR, and further, the machine code is obtained based on the low-order IR. Wherein the stages from the source code to the syntax tree may be performed by the first compiling module. Subsequent optimizers of higher order and lower order belong to the second compilation module.
In the method for code compilation provided by this embodiment, a source code to be compiled in an operating system is obtained first, a first compiling module is called to compile the source code into an intermediate bytecode, and then, based on the intermediate bytecode, an intermediate code in a specified format corresponding to input data of a second compiling module is generated, where the second compiling module is an LLVM compiling module, and then, the second compiling module is called to compile the intermediate code into a machine code. Therefore, the generated intermediate byte code can be compiled into the machine code by the LLVM compiling module in a mode of converting the intermediate byte code obtained by compiling the source code into the intermediate code in the corresponding format of the input data of the LLVM compiling module, so that the quality of the obtained machine code is favorably improved, and the execution efficiency of the electronic equipment executing the machine code is improved.
Referring to fig. 4, the present application provides a method for code compilation, applied to a server, where the method includes:
s210: and acquiring a source code to be compiled in an operating system.
S220: and calling a first compiling module to compile the source code into intermediate byte codes.
S230: a LLVM basic block is created.
It should be noted that the basic block may be a basic unit in a compiling process, and may also be understood as a statement sequence executed in the maximum order in a program, where there is only one entry and one exit, the entry is the first statement of the entry, and the exit is the last statement of the entry. Wherein, the entry statement of the basic block can be the first statement of the program, the target statement of the jump or the next statement of the conditional jump. The end statement of the basic block may be a halt statement, a jump statement, or a statement (lexical order) preceding the jump target statement. In the present embodiment, the intermediate code in the customized format generated for the intermediate bytecode is LLVM IR, and then the LLVM basic blocks constituting the LLVM IR can be obtained first in order to obtain the LLVM IR.
As one way, the creating the LLVM basic block includes: traversing each Basic Block (Basic Block) in the intermediate bytecode, creating a corresponding LLVM Basic Block for each Basic Block. It should be noted that the intermediate bytecode includes a plurality of basic blocks, and then the plurality of basic blocks included in the intermediate bytecode may be compiled in sequence, and a corresponding LLVM basic block is established for the traversed basic block. Optionally, the base blocks corresponding to each other and the LLVM base block may be associated in an index-based manner.
If a first basic block exists, creating a storage area in the LLVM basic block corresponding to the first basic block, wherein the first basic block is a basic block in which an operand stack exists in an intermediate byte code; and if a second basic block exists, creating a storage area in the LLVM basic block corresponding to the second basic block, wherein the second basic block is a basic block with a local variable in the middle byte code. Optionally, the storage area may be an alloca area.
The Local variables are stored in a Local Variable Table (Local Variable Table). The local variable table is a set of variable value storage spaces for storing method parameters and local variables defined within the method. The capacity of the local Variable table is in a minimum unit of Variable Slot (Variable Slot). A local variable may hold a data type of bootean, byte, char, short, int, float, reference, and return Address. Taking a java virtual machine as an example, the java virtual machine searches for a corresponding local variable by an index positioning method, wherein the index range is from 0 to the maximum capacity of a local variable table. If the Slot is 32-bit, a variable of 64-bit data type (e.g., long or double) is encountered, and two consecutive slots are used consecutively for storage.
It should be noted that the Operand Stack (Operand Stack) is also often called an operation Stack, and is a last in first out Stack (LIFO). As with the local variable table, the maximum depth of the operand stack is also written into the max _ stacks data entry of the Code attribute of the method at compile time. Each element of the operand stack may be any Java data type, with a 32-bit data type accounting for one stack size and a 64-bit data type accounting for 2 stack sizes, and at any time during method execution, the depth of the operand stack may not exceed the maximum set in max _ stacks. When a method code just starts to execute, the operand stack of the basic block where the method code is located can be empty, as the method code executes and the bytecode instruction executes, a constant or variable is copied from a field of a local variable table or an object instance and written into the operand stack, and then elements in the stack are popped into the local variable table or returned to a method code caller as the calculation is performed, namely, a pop/push operation. Wherein, the execution period of a complete method code often comprises a plurality of such pop/push procedures.
It should be noted that, in the process of maintaining the operand stack, Java operand stack modeling may be performed according to a Java computation model. Two slots (slots) are occupied when local variables or operand stacks of long and double are stored in the operand stack corresponding to the Java code. In order to simplify instruction operation, when the operands are stored in the Slot, the type information can not be contained, so that special processing of the Slot positions of long and double can be omitted, and further, the data processing complexity in the compiling process is reduced.
S240: and generating an intermediate code in a specified format corresponding to the intermediate byte code based on the LLVM basic block, wherein the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module.
As one way, the generating of the intermediate code of the specified format corresponding to the intermediate bytecode based on the LLVM basic block includes: converting the subcode in each basic block in the intermediate bytecode into a respectively corresponding subcode of a specified format (e.g., LLVM IR), the subcode comprising at least one of an operand stack, local variables, and method code; writing the subcodes with the specified format corresponding to each basic block into the LLVM basic block corresponding to each basic block to obtain the LLVM basic block with the subcodes written in and corresponding to each basic block; and obtaining the intermediate code in the specified format based on the LLVM basic block, in which the subcode is written, corresponding to each basic block.
Illustratively, as shown in fig. 5, the intermediate bytecode may include a plurality of basic blocks 10, and the subcode in the basic block 10 may be converted into a subcode 21 in a specified format, and then the subcode 21 in the specified format is written into the LLVM basic block 20 corresponding to the basic block 10, thereby obtaining the LLVM basic block 20 corresponding to the basic block 10 and having the subcode written therein.
It should be noted that, when converting the method code in the subcode, a finder mode may be adopted, and a translation operation is performed according to the instruction, so as to convert the method code into the instruction in the LLVM IR.
S250: and calling the second compiling module to compile the intermediate code into machine code.
In the method for compiling codes provided by this embodiment, by converting the intermediate bytecode obtained by compiling the source code into the intermediate code in the format corresponding to the input data of the LLVM compiling module, the generated intermediate bytecode can be compiled into the machine code by the LLVM compiling module, which is favorable for improving the quality of the obtained machine code, and further improving the execution efficiency of the electronic device executing the machine code. In addition, in this embodiment, each basic block in the intermediate bytecode is traversed first, a corresponding LLVM basic block is created for each basic block, and for a basic block corresponding to an operand stack or a local variable, a storage area is also created in the LLVM basic block corresponding to the operand stack or the local variable, so that the operand stack or the local variable can be maintained, and after the LLVM basic block is created, a subcode in the basic block of the intermediate bytecode can be converted into a specified format and then written into the LLVM basic block, so as to obtain the LLVM basic block into which the subcode is written, and further, an intermediate code in the specified format is obtained based on the LLVM basic block into which the subcode is written, which corresponds to each basic block.
Referring to fig. 6, the present application provides a method for code compilation, which is applied to a server, and the method includes:
s310: and acquiring a source code to be compiled in an operating system.
S320: and calling a first compiling module to compile the source code into an intermediate byte code, wherein the intermediate byte code compiled by the first compiling module is the intermediate byte code in the first byte format.
S330: and acquiring target compiling conditions.
It should be noted that, in this embodiment, the target compiling condition may characterize the running requirement of the compiled machine code. The machine code obtained by compiling the intermediate code finally and converted from different byte formats has different operating characteristics. For example, there is better compatibility for class (bytecode), and then there is higher compatibility for machine code obtained based on intermediate code converted from class (bytecode). Then, as one way, the target compilation condition in the present embodiment is that the generated machine code has compatibility. In one mode, the intermediate bytecode of the first byte format is a class bytecode, and the intermediate bytecode of the second byte format is a dex bytecode. The class bytecode in this embodiment may be understood as a java bytecode.
S331: detecting whether the intermediate bytecode matches the target compilation condition.
S340: and if the intermediate byte code is matched with the target compiling condition, generating an intermediate code in a specified format based on the intermediate byte code.
S350: and if the intermediate byte code is not matched with the target compiling condition, converting the intermediate byte code into an intermediate byte code in a second byte format.
S360: generating an intermediate code in a specified format based on the intermediate bytecode in the second byte format, wherein the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module;
s370: and calling the second compiling module to compile the intermediate code into machine code.
In the method for compiling codes provided by this embodiment, by converting the intermediate bytecode obtained by compiling the source code into the intermediate code in the format corresponding to the input data of the LLVM compiling module, the generated intermediate bytecode can be compiled into the machine code by the LLVM compiling module, which is favorable for improving the quality of the obtained machine code, and further improving the execution efficiency of the electronic device executing the machine code. In addition, in the embodiment of the present invention, the intermediate bytecode compiled by the first compiling module is the intermediate bytecode in the first byte format, and after the intermediate bytecode in the first byte format is obtained, it is first detected whether the intermediate bytecode in the first byte format matches the target compiling condition, and after matching, based on the intermediate bytecode in the first byte format, an intermediate code in a specified format is generated, and if not, the intermediate bytecode in the first byte format is converted into the intermediate bytecode in the second byte format, and then based on the intermediate bytecode in the second byte format, the intermediate code in the specified format is generated, so that in this embodiment, it can be determined according to the target compiling condition that the intermediate bytecode in that format is specifically converted into the intermediate code, and thus, the flexibility of the whole compiling process is improved.
Referring to fig. 7, the present application provides a method for code compilation, applied to a server, the method including:
s410: and acquiring a source code to be compiled in an operating system.
S420: and calling a first compiling module to compile the source code into intermediate byte codes.
S430: and generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to the input data of the second compiling module, and the second compiling module is an LLVM compiling module.
S440: and acquiring a target platform, wherein the target platform is the platform for operating the machine code.
Optionally, the target platform in this embodiment may be a smart phone, a tablet computer, or a computer.
S450: and calling the second compiling module to compile the intermediate code into the machine code corresponding to the target platform.
It should be noted that, in this embodiment, a second compilation module with adaptability may be generated according to characteristics of hardware included in the target platform, and then, in the process of generating the machine code, the second compilation module matched with the characteristics of the hardware of the target platform may be invoked to compile the intermediate code into the machine code corresponding to the target platform, so that the generated machine code may be adapted to the target platform more highly. Alternatively, the hardware characteristic in the present embodiment may be a model of a hardware device included.
It should be noted that, in the embodiment of the present application, the machine code compiled by the second compiling module may be used as the machine code in the flash package (ROM package) corresponding to the operating system. Correspondingly, the quality of the generated flashing package is improved by the method provided by the embodiment of the application.
In the method for compiling codes provided by this embodiment, by converting the intermediate bytecode obtained by compiling the source code into the intermediate code in the format corresponding to the input data of the LLVM compiling module, the generated intermediate bytecode can be compiled into the machine code by the LLVM compiling module, which is favorable for improving the quality of the obtained machine code, and further improving the execution efficiency of the electronic device executing the machine code. In addition, in this embodiment, a machine code in a corresponding format may be generated according to a target platform, which improves the wildcard property and adaptability of the code compiling method in this embodiment.
Referring to fig. 8, an apparatus 500 for code compilation provided by the present application runs on a server, and the apparatus 500 includes:
a source code obtaining unit 510, configured to obtain a source code to be compiled in an operating system;
a first compiling unit 520, configured to invoke a first compiling module to compile the source code into an intermediate bytecode;
an intermediate code generating unit 530, configured to generate an intermediate code in a specified format based on the intermediate bytecode, where the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module;
a second compiling unit 540, configured to invoke the second compiling module to compile the intermediate code into machine code.
By one approach, the intermediate code generation unit 530 is specifically configured to create an LLVM basic block; and generating an intermediate code in a specified format corresponding to the intermediate byte code based on the LLVM basic block. Optionally, the intermediate code generating unit 530 is specifically configured to traverse each basic block in the intermediate bytecode, and create a corresponding LLVM basic block for each basic block; if a first basic block exists, creating a storage area in the LLVM basic block corresponding to the first basic block, wherein the first basic block is a basic block in which an operand stack exists in an intermediate byte code; and if a second basic block exists, creating a storage area in the LLVM basic block corresponding to the second basic block, wherein the second basic block is a basic block with a local variable in the middle byte code.
Optionally, the intermediate code generating unit 530 is specifically configured to convert the subcode in each basic block in the intermediate bytecode into a corresponding subcode in a specified format, where the subcode includes at least one of an operand stack, a local variable, and a method code; writing the subcodes with the specified format corresponding to each basic block into the LLVM basic block corresponding to each basic block to obtain the LLVM basic block with the subcodes written in and corresponding to each basic block; and obtaining the intermediate code in the specified format based on the LLVM basic block, in which the subcode is written, corresponding to each basic block.
In one mode, the intermediate bytecode compiled by the first compiling module is an intermediate bytecode in a first byte format. In this manner, the intermediate code generation unit 530 is specifically configured to obtain the target compilation condition; if the intermediate byte code is matched with the target compiling condition, generating an intermediate code in a specified format based on the intermediate byte code; if the intermediate byte code is not matched with the target compiling condition, converting the intermediate byte code into an intermediate byte code in a second byte format; and generating an intermediate code in a specified format based on the intermediate byte code in the second byte format. Optionally, the intermediate bytecode of the first byte format is a class bytecode, and the intermediate bytecode of the second byte format is a dex bytecode.
As a manner, the second compiling unit 540 is specifically configured to obtain a target platform, where the target platform is the platform running the machine code; and calling the second compiling module to compile the intermediate code into the machine code corresponding to the target platform.
The apparatus for code compilation according to this embodiment first obtains a source code to be compiled in an operating system, invokes a first compiling module to compile the source code into an intermediate bytecode, and then generates an intermediate code in a specified format corresponding to input data of a second compiling module based on the intermediate bytecode, where the second compiling module is an LLVM compiling module, and then invokes the second compiling module to compile the intermediate code into a machine code. Therefore, the generated intermediate byte code can be compiled into the machine code by the LLVM compiling module in a mode of converting the intermediate byte code obtained by compiling the source code into the intermediate code in the corresponding format of the input data of the LLVM compiling module, so that the quality of the obtained machine code is favorably improved, and the execution efficiency of the electronic equipment executing the machine code is improved.
It should be noted that the device embodiment and the method embodiment in the present application correspond to each other, and specific principles in the device embodiment may refer to the contents in the method embodiment, which is not described herein again.
An electronic device provided by the present application will be described below with reference to fig. 9.
Referring to fig. 9, based on the image processing method and apparatus, another electronic device 200 capable of executing the code compiling method is further provided in the embodiment of the present application. The electronic device 200 includes one or more processors 102 (only one shown), memory 104, and network module 106 coupled to each other. The memory 104 stores programs that can execute the content of the foregoing embodiments, and the processor 102 can execute the programs stored in the memory 104.
Processor 102 may include one or more processing cores, among other things. The processor 102 interfaces with various components throughout the electronic device 100 using various interfaces and circuitry to perform various functions of the electronic device 100 and process data by executing or executing instructions, programs, code sets, or instruction sets stored in the memory 104 and invoking data stored in the memory 104. Alternatively, the processor 102 may be implemented in hardware using at least one of Digital Signal Processing (DSP), Field-Programmable Gate Array (FPGA), and Programmable Logic Array (PLA). The processor 102 may integrate one or more of a Central Processing Unit (CPU), a Graphics Processing Unit (GPU), a modem, and the like. Wherein, the CPU mainly processes an operating system, a user interface, an application program and the like; the GPU is used for rendering and drawing display content; the modem is used to handle wireless communications. It is understood that the modem may not be integrated into the processor 102, but may be implemented by a communication chip.
The Memory 104 may include a Random Access Memory (RAM) or a Read-Only Memory (Read-Only Memory). The memory 104 may be used to store instructions, programs, code sets, or instruction sets. The memory 104 may include a stored program area and a stored data area, wherein the stored program area may store instructions for implementing an operating system, instructions for implementing at least one function (such as a touch function, a sound playing function, an image playing function, etc.), instructions for implementing various method embodiments described below, and the like. The storage data area may also store data created by the terminal 100 in use, such as a phonebook, audio-video data, chat log data, and the like.
The network module 106 is configured to receive and transmit electromagnetic waves, and achieve interconversion between the electromagnetic waves and electrical signals, so as to communicate with a communication network or other devices, such as a wireless access point. The network module 106 may include various existing circuit elements for performing these functions, such as an antenna, a radio frequency transceiver, a digital signal processor, an encryption/decryption chip, a Subscriber Identity Module (SIM) card, memory, and so forth. The network module 106 may also be used as a network adapter for the electronic device 200 to access the network directly through a line connection. The network may comprise a cellular telephone network, a wireless local area network, or a metropolitan area network.
Referring to fig. 10, a block diagram of a computer-readable storage medium according to an embodiment of the present application is shown. The computer-readable medium 800 has stored therein a program code that can be called by a processor to execute the method described in the above-described method embodiments.
The computer-readable storage medium 800 may be an electronic memory such as a flash memory, an EEPROM (electrically erasable programmable read only memory), an EPROM, a hard disk, or a ROM. Alternatively, the computer-readable storage medium 800 includes a non-volatile computer-readable storage medium. The computer readable storage medium 800 has storage space for program code 810 to perform any of the method steps of the method described above. The program code can be read from or written to one or more computer program products. The program code 810 may be compressed, for example, in a suitable form.
According to the code compiling method, the code compiling device, the electronic equipment and the server, a source code to be compiled in an operating system is obtained firstly, a first compiling module is called to compile the source code into an intermediate byte code, then based on the intermediate byte code, an intermediate code in a specified format corresponding to input data of a second compiling module is generated, wherein the second compiling module is an LLVM compiling module, and then the second compiling module is called to compile the intermediate code into machine code. Therefore, the generated intermediate byte code can be compiled into the machine code by the LLVM compiling module in a mode of converting the intermediate byte code obtained by compiling the source code into the intermediate code in the corresponding format of the input data of the LLVM compiling module, so that the quality of the obtained machine code is promoted by an excellent optimizer in the LLVM compiling module, the execution efficiency of electronic equipment executing the machine code is promoted, and the probability of the machine code being stuck in the execution process is reduced.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solutions of the present application, and not to limit the same; although the present application has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some technical features may be equivalently replaced; such modifications and substitutions do not necessarily depart from the spirit and scope of the corresponding technical solutions in the embodiments of the present application.

Claims (10)

1. A method of code compilation, applied to a server, the method comprising:
acquiring a source code to be compiled in an operating system;
calling a first compiling module to compile the source code into intermediate byte codes;
generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module;
and calling the second compiling module to compile the intermediate code into machine code.
2. The method of claim 1, wherein generating intermediate code in a specified format based on the intermediate bytecode comprises:
creating an LLVM basic block;
and generating an intermediate code in a specified format corresponding to the intermediate byte code based on the LLVM basic block.
3. The method of claim 2, wherein creating the LLVM basic block comprises:
traversing each basic block in the intermediate byte code, and creating a corresponding LLVM basic block for each basic block;
if a first basic block exists, creating a storage area in the LLVM basic block corresponding to the first basic block, wherein the first basic block is a basic block in which an operand stack exists in an intermediate byte code;
and if a second basic block exists, creating a storage area in the LLVM basic block corresponding to the second basic block, wherein the second basic block is a basic block with a local variable in the middle byte code.
4. The method of claim 3, wherein generating intermediate code of a specified format corresponding to the intermediate bytecode based on the LLVM basic block comprises:
converting the subcode in each basic block in the intermediate bytecode into a corresponding subcode in a specified format, wherein the subcode comprises at least one of an operand stack, a local variable and a method code;
writing the subcodes with the specified format corresponding to each basic block into the LLVM basic block corresponding to each basic block to obtain the LLVM basic block with the subcodes written in and corresponding to each basic block;
and obtaining the intermediate code in the specified format based on the LLVM basic block, in which the subcode is written, corresponding to each basic block.
5. The method according to any one of claims 1 to 4, wherein the intermediate bytecode compiled by the first compiling module is an intermediate bytecode in a first byte format, and the generating an intermediate code in a specified format based on the intermediate bytecode includes:
acquiring a target compiling condition;
if the intermediate byte code is matched with the target compiling condition, generating an intermediate code in a specified format based on the intermediate byte code;
if the intermediate byte code is not matched with the target compiling condition, converting the intermediate byte code into an intermediate byte code in a second byte format;
and generating an intermediate code in a specified format based on the intermediate byte code in the second byte format.
6. The method as claimed in claim 5, wherein the first byte format of the intermediate bytecode is class bytecode, and the second byte format of the intermediate bytecode is dex bytecode.
7. The method of any of claims 1-4, wherein invoking the second compilation module to compile the intermediate code into machine code comprises:
acquiring a target platform, wherein the target platform is the platform for operating the machine code;
and calling the second compiling module to compile the intermediate code into the machine code corresponding to the target platform.
8. An apparatus for code compilation, running on a server, the apparatus comprising:
the source code acquisition unit is used for acquiring a source code to be compiled in an operating system;
the first compiling unit is used for calling a first compiling module to compile the source code into intermediate byte codes;
the intermediate code generating unit is used for generating an intermediate code with a specified format based on the intermediate byte code, wherein the specified format is a format corresponding to input data of a second compiling module, and the second compiling module is an LLVM compiling module;
and the second compiling unit is used for calling the second compiling module to compile the intermediate code into machine code.
9. A server comprising one or more processors, a video codec, and a memory;
one or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, the one or more programs configured to perform the method of any of claims 1-7.
10. A computer-readable storage medium, having program code stored therein, wherein the method of any of claims 1-7 is performed when the program code is run.
CN202011595414.5A 2020-12-29 2020-12-29 Code compiling method and device, electronic equipment and server Pending CN112558984A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202011595414.5A CN112558984A (en) 2020-12-29 2020-12-29 Code compiling method and device, electronic equipment and server

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202011595414.5A CN112558984A (en) 2020-12-29 2020-12-29 Code compiling method and device, electronic equipment and server

Publications (1)

Publication Number Publication Date
CN112558984A true CN112558984A (en) 2021-03-26

Family

ID=75032775

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202011595414.5A Pending CN112558984A (en) 2020-12-29 2020-12-29 Code compiling method and device, electronic equipment and server

Country Status (1)

Country Link
CN (1) CN112558984A (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114115900A (en) * 2021-11-25 2022-03-01 北京力控元通科技有限公司 Script compiling method and device and electronic equipment
CN114327415A (en) * 2022-03-17 2022-04-12 武汉天喻信息产业股份有限公司 Compiling method and device for compiling java file
CN117093227A (en) * 2023-10-10 2023-11-21 杭州趣链科技有限公司 Method and device for executing intelligent contract

Cited By (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114115900A (en) * 2021-11-25 2022-03-01 北京力控元通科技有限公司 Script compiling method and device and electronic equipment
CN114327415A (en) * 2022-03-17 2022-04-12 武汉天喻信息产业股份有限公司 Compiling method and device for compiling java file
CN114327415B (en) * 2022-03-17 2022-05-31 武汉天喻信息产业股份有限公司 Compiling method and device for compiling java file
CN117093227A (en) * 2023-10-10 2023-11-21 杭州趣链科技有限公司 Method and device for executing intelligent contract
CN117093227B (en) * 2023-10-10 2024-04-02 杭州趣链科技有限公司 Method and device for executing intelligent contract

Similar Documents

Publication Publication Date Title
CN111708539B (en) Application program code conversion method and device, electronic equipment and storage medium
CN112558984A (en) Code compiling method and device, electronic equipment and server
CN112527316A (en) Code compiling method and device, electronic equipment and storage medium
CN114327477A (en) Intelligent contract execution method and device, electronic device and storage medium
CN112559088A (en) Configuration file optimization method, device, server and storage medium
CN112732321A (en) Firmware modification method and device, computer readable storage medium and equipment
CN114780100B (en) Compiling method, electronic device and storage medium
CN110673851B (en) Intelligent contract operation method and device and electronic equipment
CN115145574A (en) Code generation method and device, storage medium and server
CN113703779B (en) Cross-platform multi-language compiling method and ultra-light Internet of things virtual machine
US10416971B2 (en) Method of creating the balanced parse tree having optimized height
JP4768984B2 (en) Compiling method, compiling program, and compiling device
CN113220306A (en) Operation execution method and device and electronic equipment
US20100023924A1 (en) Non-constant data encoding for table-driven systems
CN110941443B (en) Method and device for modifying file name in SDK (software development kit) and electronic equipment
US8806460B2 (en) Method and apparatus for avoiding register interference
CN110018831B (en) Program processing method, program processing apparatus, and computer-readable storage medium
CN111880801A (en) Application program dynamic method and device and electronic equipment
CN111338633B (en) Method and device for generating installation-free file and electronic equipment
CN114911541A (en) Configuration information processing method and device, electronic equipment and storage medium
CN112905271A (en) Calling method and device of application program interface API and electronic equipment
CN111880785A (en) Program code conversion method and device and electronic equipment
CN113031952A (en) Method and device for determining execution code of deep learning model and storage medium
CN117075912B (en) Method for program language conversion, compiling method and related equipment
CN112463214B (en) Data processing method and device, computer readable storage medium and electronic equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination