Disclosure of Invention
The invention mainly aims to provide a hybrid compiling method, a hybrid compiling system, a compiler and a storage medium, and aims to solve the technical problem that the compiler relies on target platform hardware and a system to perform compiling in the prior art.
To achieve the above object, the present invention provides a hybrid compilation method, including the steps of:
acquiring a source code through a text file of an editor;
performing standard correction according to the source code to generate a preprocessing code;
performing back-end analysis according to the preprocessed codes to generate mixed compiling information;
and performing machine language conversion according to the mixed compiling information to generate a target machine code.
Optionally, the performing standard modification according to the source code to generate a preprocessing code includes:
performing non-standard variable conversion according to the source code to generate a standard variable code;
carrying out error detection according to the standard variable code to generate error information;
and correcting the error of the standard address code according to the error information to generate a preprocessing code.
Optionally, the performing non-canonical variable conversion according to the source code to generate a standard variable code includes:
performing instruction extraction according to the source code to generate instruction information;
and performing non-standard variable conversion on the source code according to the instruction information to generate a standard variable code.
Optionally, the performing instruction extraction according to the source code to generate instruction information includes:
performing lexical analysis according to the source code to generate keyword information;
and performing instruction extraction on the source code according to the keyword information to generate instruction information.
Optionally, the performing error detection according to the standard variable code to generate error information includes:
writing the standard variable code into a forward statement to generate a standard definition code;
and carrying out error detection according to the standard definition code to generate error information.
Optionally, the performing error detection according to the standard definition code to generate error information includes:
performing address mapping conversion according to the standard definition code to generate a standard address code;
and carrying out error detection according to the standard address code to generate error information.
Optionally, the performing back-end analysis according to the preprocessed codes to generate hybrid compilation information includes:
performing lexical analysis according to the preprocessing codes to generate lexical information;
and carrying out syntax and semantic analysis on the preprocessed codes according to the lexical information to generate mixed compiling information.
In addition, to achieve the above object, the present invention further provides a hybrid compiling system that implements the hybrid compiling method described above, the hybrid compiling system including: the system comprises a preprocessing module, an analysis module and a machine language conversion module;
the preprocessing module is used for acquiring a source code through a text file of an editor;
the preprocessing module is also used for carrying out standard correction according to the source code to generate a preprocessing code;
the analysis module is used for carrying out back-end analysis according to the preprocessing codes to generate mixed compiling information;
and the machine language conversion module is used for performing machine language conversion according to the mixed compiling information to generate a target machine code.
In addition, to achieve the above object, the present invention further provides an editor, including: a memory, a processor, and a hybrid compiler stored on the memory and executable on the processor, the hybrid compiler configured to implement the steps of the hybrid compiling method as described above.
In addition, to achieve the above object, the present invention further provides a storage medium having a hybrid compiler stored thereon, wherein the hybrid compiler realizes the steps of the hybrid compiling method as described above when being executed by a processor.
The method comprises the steps of obtaining a source code through a text file of an editor, carrying out standard correction according to the source code to generate a preprocessing code, carrying out back-end analysis according to the preprocessing code to generate mixed compiling information, and carrying out machine language conversion according to the mixed compiling information to generate a target machine code. According to the invention, standard correction is carried out on the source code, the preprocessed code meets the standard specification and accords with the code execution requirements of different target platforms, mixed compiling information is generated through back-end analysis, the target machine code is generated according to the mixed compiling information, auxiliary compiling of the target platform is not needed, and mixed compiling adaptive to a plurality of target platforms is realized.
Detailed Description
It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
Referring to fig. 1, fig. 1 is a schematic diagram of an editor architecture of a hardware operating environment according to an embodiment of the present invention.
As shown in fig. 1, the editor may include: a processor 1001, such as a Central Processing Unit (CPU), a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Wherein a communication bus 1002 is used to enable connective communication between these components. The user interface 1003 may include a Display screen (Display), an input unit such as a Keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface, a wireless interface. The network interface 1004 may optionally include a standard wired interface, a Wireless interface (e.g., a Wireless-Fidelity (Wi-Fi) interface). The Memory 1005 may be a Random Access Memory (RAM) Memory, or may be a Non-Volatile Memory (NVM), such as a disk Memory. The memory 1005 may alternatively be a storage device separate from the processor 1001.
Those skilled in the art will appreciate that the configuration shown in FIG. 1 does not constitute a definition of an editor, and may include more or fewer components than shown, or some components in combination, or a different arrangement of components.
As shown in fig. 1, a memory 1005, which is a storage medium, may include therein an operating system, a network communication module, a user interface module, and a hybrid compiler program.
In the editor shown in fig. 1, the network interface 1004 is mainly used for data communication with a network server; the user interface 1003 is mainly used for data interaction with a user; the processor 1001 and the memory 1005 in the editor of the present invention may be disposed in the editor, and the editor calls the hybrid compiler stored in the memory 1005 through the processor 1001 and executes the XX method provided by the embodiment of the present invention.
An embodiment of the present invention provides a hybrid compiling method, and referring to fig. 2, fig. 2 is a flowchart illustrating a first embodiment of the hybrid compiling method according to the present invention.
In this embodiment, the hybrid compiling method includes the following steps:
step S10: the source code is obtained through a text file of the editor.
It should be noted that the execution subject of the hybrid compiling method may be a hybrid compiling system or a compiler, and during the hybrid compiling process, the source code needs to be acquired first. Source code is a tool that programmers write when creating a software program, the source code being written in a particular programming language and saved in a text-based, human-readable format so that the source code cannot be run directly by a computer.
It should be understood that, because there are many kinds of working platforms, i.e., target platforms, such as an X86 platform, an ARM platform, a RISC platform, etc., a conventional compiler cannot compile a software code into an executable machine code conforming to a plurality of target platforms at one time, and the compiling process needs to depend on hardware and systems of the target platforms (e.g., an interpreter of the target platform).
Step S20: and performing standard correction according to the source code to generate a preprocessing code.
It is easy to understand that the standard modification is performed according to the source code, and the generation of the pre-processing code is a pre-processing process of mixed compilation and is executed by the pre-processing module. In order to enable the programming code, namely the source code, to meet the standardized specification after one-time correction, the standardized specification of the standard correction is set according to the compiling and code program running requirements of the target platform, such as the requirements of correct syntax semantics or no unrecognizable characters and the like. After standard correction is performed on the source code, a preprocessing code can be generated, and the preprocessing code is a code generated after the source code is preprocessed.
Step S30: and performing back-end analysis according to the preprocessing codes to generate mixed compiling information.
It is understood that the back-end analysis process may include lexical analysis and semantic analysis, which are performed by the analysis module, wherein the analysis module may include a lexical analysis module based on FLEX and a semantic analysis module based on BISON, the lexical analysis may be performed by the lexical analysis module based on FLEX and the semantic analysis may be performed by the semantic analysis module based on BISON. The hybrid compilation information may include instruction labels, symbol tables, and a wrapper library, among other things, which may be used to convert symbols in the code file into external symbols for relocation by the target machine execution.
Step S40: and performing machine language conversion according to the mixed compiling information to generate a target machine code.
It is easy to understand that the machine language conversion according to the hybrid compilation information may be performed by a machine language conversion module, which may be an LLVM-based machine language conversion module. The machine language conversion module generates executable machine codes of different target platforms, namely target machine codes, through the application program interface, and outputs the target machine codes to the target platform so that the target platform executes related code programs to run.
In this embodiment, a source code is obtained through a text file of an editor, a standard correction is performed according to the source code to generate a preprocessing code, a back-end analysis is performed according to the preprocessing code to generate mixed compiling information, and a machine language conversion is performed according to the mixed compiling information to generate a target machine code. According to the embodiment, the source code is subjected to standard correction, the preprocessed code meets the standard specification and meets the code execution requirements of different target platforms, the mixed compiling information is generated through back-end analysis, the target machine code is generated according to the mixed compiling information, auxiliary compiling of the target platforms is not needed, and mixed compiling adaptive to a plurality of target platforms is achieved.
Referring to fig. 3, fig. 3 is a flowchart illustrating a hybrid compiling method according to a second embodiment of the invention.
Based on the first embodiment, the step S20 in the hybrid compilation method of this embodiment includes:
step S201: and performing lexical analysis according to the source code to generate keyword information.
It should be noted that lexical analysis is performed according to the source code, and generating keyword information is performed for the first time in a hybrid compiling process, so as to firstly eliminate the lexical problem, avoid useless subsequent preprocessing process caused by lexical errors and influence on compiling efficiency, the keyword information is obtained by scanning the source code and performing lexical analysis, and the keyword information may include keywords (such as include, define, undef, if, error, and the like) and keyword position information.
Step S202: and performing instruction extraction on the source code according to the keyword information to generate instruction information.
It can be understood that the position of the keyword can be determined according to the keyword information, and the statement where the keyword is located can be determined as the corresponding instruction according to the symbols before and after the keyword, so that the instruction information can be generated, and the instruction information can be important information for setting a command for a code file so as to enable the corresponding program to execute the command.
Step S203: and performing non-standard variable conversion on the source code according to the instruction information to generate a standard variable code.
It should be understood that, since the source code is edited in a high-level language, non-canonical variables, i.e., non-english canonical variables and functions, may exist in the source code, and the non-canonical variables need to be converted into canonical variables, i.e., english, so as to meet the standard specification of the target platform compilation.
Step S204: and writing a forward statement according to the standard variable code to generate a standard definition code.
It is easy to understand that the forward declaration is based on an auxiliary tool provided by the subsequent machine language conversion, and the forward declaration is written to define a variable or a function in the code file, so as to avoid that the machine language conversion process cannot correctly recognize the meaning of a symbol of the variable or the function in the code file.
Step S205: and performing address mapping conversion according to the standard definition code to generate a standard address code.
It is understood that the central processing unit receives logical addresses during execution of the program, and the logical addresses must be mapped into physical addresses to access the real physical memory. The mapping of the logical address of the standard definition code to the physical address of the target platform may include two steps, first, the mapping of the logical address of the standard definition code to the linear address needs to be converted, and the linear address needs to be mapped again to the physical address of the target platform.
Step S206: and carrying out error detection according to the standard address code to generate error information.
It is easy to understand that the preprocessing process of performing standard correction according to the source code may cause many problems due to source code errors or other information generation or conversion errors, which directly results in compilation far from the expected effect, therefore, error detection needs to be performed on the standard address code, the error detection may include error forms such as syntax errors, type errors, declaration errors, etc., and error information may be generated according to the detected structure, and the error information may include error forms and error positions.
Step S207: and correcting the error of the standard address code according to the error information to generate a preprocessing code.
It is easy to understand that the hybrid compiling system can return error information to the operator for modification, or the hybrid compiling system can scan the whole code, and the code file is modified in an auxiliary manner by combining the meaning of symbols such as variables and functions in the code file, and the standard address code can generate a preprocessing code after error correction, and the error position and the modification position can be marked when the code file is modified in an auxiliary manner, or the preprocessing code can be generated after the operator checks the modification content.
In this embodiment, the step S30 includes:
step S301: and performing lexical analysis according to the preprocessing codes to generate lexical information.
It can be understood that lexical analysis is performed according to the preprocessed codes, the lexical analysis module based on FLEX performs corresponding analysis in the process of generating lexical information, lexical analysis is performed for the second time in the mixed compiling process, and the compiling errors in the process of converting the high-level language into the machine language are further reduced by performing the lexical analysis on the code files again.
Step S302: and carrying out syntax and semantic analysis on the preprocessed codes according to the lexical information to generate mixed compiling information.
It is easy to understand that, syntax and semantic analysis are performed on the preprocessed codes according to the lexical information, the process of generating the hybrid compilation information can be correspondingly analyzed by a BISON-based syntax and semantic analysis module, if errors exist in the analyzed lexical, syntax and semantic, an error report can be generated to revise the preprocessed codes again to reduce compilation errors, and relevant information of the code file is extracted after no errors exist to generate the hybrid compilation information, which can include instruction labels, symbol tables, a packaging library and the like, and the packaging library can be used for converting symbols in the code file into external symbols for a target machine to perform relocation.
The embodiment is adapted to mixed compilation by preprocessing a source code, wherein the preprocessing process comprises the steps of firstly scanning the source code and acquiring keyword information through lexical analysis, finding out various types of instructions according to the keyword information, secondly converting non-standard variables and functions in a code file into standard variables, automatically adding forward declaration to the code file, namely the standard definition code, so as to ensure correct compilation during machine language conversion, mapping and converting variable logic addresses of the standard definition code and target platform physical addresses, finally carrying out error real-time monitoring, correction and output on compilation to obtain a preprocessed code, carrying out new lexical scanning and syntax semantic analysis on the preprocessed code to obtain mixed compilation information, generating a target machine code according to the mixed compilation information, and needing no target platform to assist in compilation, the accuracy and efficiency of hybrid compilation adapting to multiple target platforms are further improved.
Furthermore, an embodiment of the present invention further provides a storage medium, where a hybrid compiler program is stored, and when being executed by a processor, the hybrid compiler program implements the steps of the hybrid compiling method as described above.
Since the storage medium adopts all technical solutions of all the embodiments, at least all the beneficial effects brought by the technical solutions of the embodiments are achieved, and no further description is given here.
Referring to fig. 4, fig. 4 is a block diagram illustrating a hybrid compiling system according to a first embodiment of the invention.
As shown in fig. 4, the hybrid compiling system according to the embodiment of the present invention includes: the system comprises a preprocessing module, an analysis module and a machine language conversion module;
the preprocessing module 10 is configured to obtain a source code through a text file of an editor.
It should be understood that, because there are many kinds of working platforms, i.e., target platforms, such as an X86 platform, an ARM platform, a RISC platform, etc., a conventional compiler cannot compile a software code into an executable machine code conforming to a plurality of target platforms at one time, and the compiling process needs to depend on hardware and systems of the target platforms (e.g., an interpreter of the target platform).
The preprocessing module 10 is further configured to perform standard modification according to the source code to generate a preprocessing code.
It is easy to understand that the standard modification is performed according to the source code, and the generation of the pre-processing code is a pre-processing process of mixed compilation and is executed by the pre-processing module. In order to enable the programming code, namely the source code, to meet the standardized specification after one-time correction, the standardized specification of the standard correction is set according to the compiling and code program running requirements of the target platform, such as the requirements of correct syntax semantics or no unrecognizable characters and the like. After standard correction is performed on the source code, a preprocessing code can be generated, and the preprocessing code is a code generated after the source code is preprocessed.
The analysis module 20 is configured to perform back-end analysis according to the preprocessed codes, and generate hybrid compilation information.
It is understood that the back-end analysis process may include lexical analysis and semantic analysis, which are performed by the analysis module, wherein the analysis module may include a lexical analysis module based on FLEX and a semantic analysis module based on BISON, the lexical analysis may be performed by the lexical analysis module based on FLEX and the semantic analysis may be performed by the semantic analysis module based on BISON. The hybrid compilation information may include instruction labels, symbol tables, and a wrapper library, among other things, which may be used to convert symbols in the code file into external symbols for relocation by the target machine execution.
The machine language conversion module 30 is configured to perform machine language conversion according to the hybrid compilation information to generate a target machine code.
It is easy to understand that the machine language conversion according to the hybrid compilation information may be performed by a machine language conversion module, which may be an LLVM-based machine language conversion module. The machine language conversion module generates executable machine codes of different target platforms, namely target machine codes, through the application program interface, and outputs the target machine codes to the target platform so that the target platform executes related code programs to run.
In this embodiment, a source code is obtained through a text file of an editor, a standard correction is performed according to the source code to generate a preprocessing code, a back-end analysis is performed according to the preprocessing code to generate mixed compiling information, and a machine language conversion is performed according to the mixed compiling information to generate a target machine code. According to the embodiment, the source code is subjected to standard correction, the preprocessed code meets the standard specification and meets the code execution requirements of different target platforms, the mixed compiling information is generated through back-end analysis, the target machine code is generated according to the mixed compiling information, auxiliary compiling of the target platforms is not needed, and mixed compiling adaptive to a plurality of target platforms is achieved.
In an embodiment, the preprocessing module 10 is further configured to perform non-canonical variable conversion according to the source code, and generate a standard variable code; carrying out error detection according to the standard variable code to generate error information; and correcting the error of the standard address code according to the error information to generate a preprocessing code.
In an embodiment, the preprocessing module 10 is further configured to perform instruction extraction according to the source code, and generate instruction information; and performing non-standard variable conversion on the source code according to the instruction information to generate a standard variable code.
In an embodiment, the preprocessing module 10 is further configured to perform lexical analysis according to the source code to generate keyword information; and performing instruction extraction on the source code according to the keyword information to generate instruction information.
In an embodiment, the preprocessing module 10 is further configured to write a forward declaration according to the standard variable code, and generate a standard definition code; and carrying out error detection according to the standard definition code to generate error information.
In an embodiment, the preprocessing module 10 is further configured to perform address mapping conversion according to the standard definition code, and generate a standard address code; and carrying out error detection according to the standard address code to generate error information.
In an embodiment, the analysis module 20 is further configured to perform lexical analysis according to the preprocessed codes, and generate lexical information; and carrying out syntax and semantic analysis on the preprocessed codes according to the lexical information to generate mixed compiling information.
Since the hybrid compiling system adopts all the technical solutions of all the embodiments, at least all the beneficial effects brought by the technical solutions of the embodiments are achieved, and no further description is given here.
It should be understood that the above is only an example, and the technical solution of the present invention is not limited in any way, and in a specific application, a person skilled in the art may set the technical solution as needed, and the present invention is not limited thereto.
It should be noted that the above-described work flows are only exemplary, and do not limit the scope of the present invention, and in practical applications, a person skilled in the art may select some or all of them to achieve the purpose of the solution of the embodiment according to actual needs, and the present invention is not limited herein.
In addition, the technical details that are not described in detail in this embodiment may refer to the hybrid compiling method provided in any embodiment of the present invention, and are not described herein again.
Further, it is to be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or system that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or system. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or system that comprises the element.
The above-mentioned serial numbers of the embodiments of the present invention are merely for description and do not represent the merits of the embodiments.
Through the above description of the embodiments, those skilled in the art will clearly understand that the method of the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but in many cases, the former is a better implementation manner. Based on such understanding, the technical solution of the present invention or portions thereof that contribute to the prior art may be embodied in the form of a software product, where the computer software product is stored in a storage medium (e.g. Read Only Memory (ROM)/RAM, magnetic disk, optical disk), and includes several instructions for enabling a terminal device (e.g. a mobile phone, a computer, a server, or a network device) to execute the method according to the embodiments of the present invention.
The above description is only a preferred embodiment of the present invention, and not intended to limit the scope of the present invention, and all modifications of equivalent structures and equivalent processes, which are made by using the contents of the present specification and the accompanying drawings, or directly or indirectly applied to other related technical fields, are included in the scope of the present invention.