A code generation system based on a template engine
By automating the processing of binding relationships and logical expressions, hardware description language code is generated, solving the problem of cumbersome modification of variable assignment statements in existing technologies, and improving code generation efficiency and user experience.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- METAX INTEGRATED CIRCUITS (SHANGHAI) CO LTD
- Filing Date
- 2026-04-10
- Publication Date
- 2026-06-30
AI Technical Summary
In existing technologies, when using template engines to generate hardware description language code, the process of modifying variable assignment statements is cumbersome, resulting in high resource consumption and a poor user experience.
By acquiring user-defined binding relationships, performing syntax analysis, and automatically processing the binding relationships of variables and macros, target code is generated, invalid variables are simplified, and logical expressions are iteratively updated to achieve automatic code generation.
It improves code generation efficiency, optimizes user experience, and reduces the complexity of manual modification for users.
Smart Images

Figure CN122308804A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of integrated circuit design technology, and in particular to a code generation system based on a template engine. Background Technology
[0002] In both the software and hardware industries, there are needs to modify a portion of a main codebase to adapt to different application scenarios or for use with different chips. Template engines can be used to achieve this. Template engine instructions, such as branching and looping, are added to the main code, and different code is generated by judging predefined macros or input parameters.
[0003] Hardware description languages typically contain variable assignment statements, which can contain arbitrary logical expressions. If only template engine instructions are used to implement branches, it becomes very cumbersome. Users need to manually modify the main code into branch code and then select the required branch code through template engine instructions. This results in a large amount of resources being consumed in code generation, requiring users to perform complex and tedious coding, which reduces the user experience.
[0004] Therefore, improving code generation efficiency and thus optimizing user experience has become an urgent problem to be solved. Summary of the Invention
[0005] To address the aforementioned technical problems, the technical solution adopted by this invention is as follows: A code generation system based on a template engine, the system comprising: a processor and a memory storing a computer program, wherein when the computer program is executed by the processor, the following steps are performed: S101 retrieves several user-defined binding relationships, with each binding relationship corresponding to a variable and a macro.
[0006] S102, perform syntax analysis on the obtained initial source code of the target module to obtain all variables in the initial source code and their corresponding assignment logic expressions.
[0007] S103, for any variable, use that variable as the target variable.
[0008] S104. If the target variable does not have a binding relationship, then the target variable is determined to be retained.
[0009] S105. If the target variable has a binding relationship to which it belongs, and the macro in the binding relationship to which the target variable belongs exists, then the target variable is determined to be retained.
[0010] S106. If the target variable has a binding relationship to which it belongs, and the macro in the binding relationship to which the target variable belongs does not exist, then execute step S107.
[0011] S107, the assignment logic expression containing the target variable is used as the reference expression.
[0012] S108: For any reference expression, mark the target variable as 0 and then recalculate the reference expression to obtain the update expression corresponding to the reference expression.
[0013] S109, update all variables and their corresponding assignment logic expressions, and search for variables with a value of 0 from all the updated variables and their corresponding assignment logic expressions.
[0014] S110, delete the target variable, take any variable with a value of 0 as the target variable, return to step S107, until there are no variables with a value of 0, and obtain the first target code.
[0015] Compared with the prior art, the present invention has significant advantages. Through the above technical solution, the template engine-based code generation system provided by the present invention achieves considerable technological progress and practicality, and has broad industrial application value. It has at least the following advantages: This invention provides a code generation system based on a template engine. The system includes a processor and a memory storing a computer program. When the computer program is executed by the processor, the following steps are implemented: S101, obtaining several user-defined binding relationships, where each binding relationship corresponds to a variable and a macro; S102, performing syntax analysis on the obtained initial source code of the target module to obtain all variables in the initial source code and their corresponding assignment logical expressions; S103, for any variable, using that variable as the target variable; S104, if the target variable does not have a corresponding binding relationship, then the target variable is determined to be retained; S105, if the target variable has a corresponding binding relationship, and the macro in the binding relationship to which the target variable belongs exists, then the target variable is determined to be retained. The target variable is retained. In step S106, if the target variable has a binding relationship and the macro in the binding relationship to which the target variable belongs does not exist, then step S107 is executed. In step S107, the assignment logic expression containing the target variable is used as a reference expression. In step S108, for any reference expression, the target variable is marked as 0 and then the reference expression is recalculated to obtain the update expression corresponding to the reference expression. In step S109, all variables and their corresponding assignment logic expressions are updated. From all the updated variables and their corresponding assignment logic expressions, variables with a value of 0 are searched out. In step S110, the target variable is deleted. Any variable with a value of 0 is used as the target variable, and step S107 is returned to be executed until there are no variables with a value of 0, and the first target code is obtained.
[0016] As can be seen, by simply having the user define a few binding relationships between variables and macros, the processing method of variables can be determined based on the binding relationships. The code simplifies the initial source code automatically based on the target variable that has a binding relationship but the macro in the binding relationship does not exist. The completeness of the code modification is ensured through iteration. Thus, the first target code can be automatically generated and directly applied to the template engine without the need for the user to manually modify the code, thereby improving the efficiency of code generation and optimizing the user experience. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This is a flowchart illustrating the process of a computer program being executed by a processor in a template engine-based code generation system, as provided in an embodiment of the present invention. Detailed Implementation
[0019] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0020] This embodiment provides a code generation system based on a template engine. See [link to documentation]. Figure 1 This is a flowchart illustrating the execution of a computer program by a processor in a template engine-based code generation system according to an embodiment of the present invention. The system includes a processor and a memory storing the computer program. When the computer program is executed by the processor, the following steps are implemented: S101, retrieve several user-defined binding relationships, one binding relationship corresponds to one variable and one macro; S102, perform syntax analysis on the obtained initial source code of the target module to obtain all variables in the initial source code and their corresponding assignment logic expressions; S103, for any variable, use that variable as the target variable; S104, If the target variable does not have a binding relationship, then the target variable is determined to be retained; S105, If the target variable has a binding relationship to which it belongs, and the macro in the binding relationship to which the target variable belongs exists, then the target variable is determined to be retained; S106, If the target variable has a binding relationship to which it belongs, and the macro in the binding relationship to which the target variable belongs does not exist, then execute step S107. S107, the assignment logic expression containing the target variable is used as the reference expression; S108: For any reference expression, mark the target variable as 0 and then recalculate the reference expression to obtain the update expression corresponding to the reference expression; S109, Update all variables and their corresponding assignment logic expressions, and search for variables with a value of 0 from all the updated variables and their corresponding assignment logic expressions; S110, delete the target variable, take any variable with a value of 0 as the target variable, return to step S107, until there are no variables with a value of 0, and obtain the first target code.
[0021] The binding relationship can be used to represent the binding relationship between a variable and a macro. That is, the variable is only valid when the macro exists. The target module can refer to the module that needs to be modified in the initial source code. This embodiment uses a single module as the target module for description. In practical applications, implementers can process multiple modules as target modules in sequence.
[0022] Syntax analysis can employ methods such as recursive descent parsing, operator precedence parsing, and LR parsing.
[0023] The variables in the initial source code can include input variables, output variables, intermediate variables, etc., and each variable usually has its corresponding assignment logic expression.
[0024] Specifically, if the target variable does not have a binding relationship, it means that the target variable will not be affected regardless of whether there is a macro or which macro exists, and will always exist. Therefore, the target variable is determined to be retained.
[0025] If the target variable has a binding relationship and the macro in the binding relationship exists, then the target variable is valid and should be retained.
[0026] If the target variable has a binding relationship, and the macro in the binding relationship to which the target variable belongs does not exist, then the target variable is invalid and the initial source code needs to be simplified.
[0027] Updating all variables and their corresponding assignment logic expressions can refer to iterating through all reference expressions, updating each reference expression to an update expression, and using the update expression as the assignment logic expression for the corresponding variable.
[0028] Since the target variable is marked as 0, the result of the reference expression may be 0, which may result in a variable with a value of 0. Therefore, it is necessary to search for the variable with a value of 0. At this time, there may be multiple variables with a value of 0. Take any one of them as the target variable and return to step S107. At this time, the variables with a value of 0 that are not used as target variables still exist in all the updated variables and their corresponding assignment logic expressions. They may still be processed as target variables in the next iteration.
[0029] In one specific implementation, the step of marking the target variable as 0 and then recalculating the reference expression to obtain the updated expression corresponding to the reference expression includes: The first truth table of the reference expression is calculated; After marking the target variable as 0, the first truth table is simplified to obtain the second truth table; The second truth table is simplified using the QM algorithm to obtain the update expression corresponding to the reference expression.
[0030] Each logical expression can have its corresponding truth table. In this embodiment, the reference expression corresponds to the first truth table. By marking the target variable as 0, the rows where the target variable is 1 can be deleted from the first truth table to obtain the second truth table. Then, the update expression corresponding to the reference expression can be obtained by simplifying it using the QM algorithm (Quine-McLuskey algorithm).
[0031] In one specific implementation, the system further includes a chip, which comprises several modules; When the computer program is executed by the processor, the following steps are also performed: S111, traverse each module, take the current module as the target module and execute steps S102 to S109 to obtain the first target code corresponding to each module; S112, for any output port corresponding to each module, if the output port is not assigned a value in the corresponding first target code, then delete the output port from the corresponding first target code; S113, traverse all output ports to obtain the second target code corresponding to each module.
[0032] In this case, "no value assigned to the output port" means that the variable corresponding to the output port does not exist. This indicates that the variable corresponding to the output port has been deleted, so the output port is removed from the corresponding first target code.
[0033] It should be noted that the first target code can already be used normally in the template engine. In order to improve the conciseness of the code, the output ports without assignment are deleted in this embodiment to obtain the second target code corresponding to each module.
[0034] In one specific implementation, after the output port is deleted from the corresponding first target code, the output port is recorded in the first deleted port table.
[0035] Each module may have its own first deletion port table, or all modules may have a single first deletion port table.
[0036] In one specific implementation, when the computer program is executed by the processor, the following steps are also performed: S114, Scan all connection relationships between modules. If at least one output port connected to any connection relationship is included in the first deletion port table, then delete the connection relationship.
[0037] The design also includes port connection relationships between modules. When at least one output port connected to a connection relationship is included in the first deletion port table, it indicates that the connection relationship is invalid and can be deleted.
[0038] In one specific implementation, when the computer program is executed by the processor, the following steps are also performed: S115, Scan all input ports of each module. If any input port is connected to an output port that is included in the first deletion port table, then delete the input port from the corresponding second target code. S116, traverse all input ports to obtain the third target code corresponding to each module, and use the third target code corresponding to each module as the corresponding updated first target code.
[0039] In the design, the port connection relationship between modules can be that the input port of one module is connected to the output port of another module. When the output port connected to the input port is included in the first deletion port table, the input port cannot obtain the required data or signal, so the input port is deleted from the corresponding second target code.
[0040] In one specific implementation, after the input port is deleted from the corresponding second target code, the input port is recorded in the second deleted port table of the corresponding module.
[0041] Each module can have its own corresponding second deletion port table.
[0042] In one specific implementation, when the computer program is executed by the processor, the following steps are also performed: S117. For any module, if the second deletion port table of the module is not empty, then determine the variable corresponding to any input port in the second deletion port table of the module as the target variable, and return to execute steps S107 to S110 until the regenerated first target code corresponding to the module is obtained. S118, return to steps S112 to S117 until the second deletion port table of all modules is empty, and obtain the final code corresponding to each module.
[0043] If the second deletion port table of the module is not empty, it means that there are deleted input ports in the module. The variables corresponding to the deleted input ports will also become invalid. Therefore, the variable corresponding to any input port in the second deletion port table of the module is taken as the target variable, and the execution steps S107 to S110 are returned until the first target code corresponding to the module is obtained.
[0044] After the first target code is regenerated, the first target code for each module changes. The first target code for each module includes the regenerated first target code for that module and the updated first target code for other modules.
[0045] In this embodiment, only a few binding relationships between user-defined variables and macros are required. The processing method of variables can be determined based on the binding relationships. The initial source code is automatically simplified based on the target variable that has a binding relationship but the macro in the binding relationship does not exist. The completeness of code modification is ensured through iteration. Thus, the first target code can be automatically generated and directly applied to the template engine without the need for user manual code modification, which improves the efficiency of code generation and optimizes the user experience.
[0046] While specific embodiments of the invention have been described in detail by way of example, those skilled in the art should understand that the examples are for illustrative purposes only and not intended to limit the scope of the invention. Those skilled in the art should also understand that various modifications can be made to the embodiments without departing from the scope and spirit of the invention. The scope of this invention is defined by the appended claims.
Claims
1. A code generation system based on a template engine, characterized in that, The system includes a processor and a memory storing a computer program, which, when executed by the processor, performs the following steps: S101, retrieve several user-defined binding relationships, one binding relationship corresponds to one variable and one macro; S102, perform syntax analysis on the obtained initial source code of the target module to obtain all variables in the initial source code and their corresponding assignment logic expressions; S103, for any variable, use that variable as the target variable; S104, If the target variable does not have a binding relationship, then the target variable is determined to be retained; S105, If the target variable has a binding relationship to which it belongs, and the macro in the binding relationship to which the target variable belongs exists, then the target variable is determined to be retained; S106, If the target variable has a binding relationship to which it belongs, and the macro in the binding relationship to which the target variable belongs does not exist, then execute step S107. S107, the assignment logic expression containing the target variable is used as the reference expression; S108: For any reference expression, mark the target variable as 0 and then recalculate the reference expression to obtain the update expression corresponding to the reference expression; S109, Update all variables and their corresponding assignment logic expressions, and search for variables with a value of 0 from all the updated variables and their corresponding assignment logic expressions; S110, delete the target variable, take any variable with a value of 0 as the target variable, return to step S107, until there are no variables with a value of 0, and obtain the first target code.
2. The code generation system based on a template engine according to claim 1, characterized in that, The step of marking the target variable as 0 and then recalculating the reference expression to obtain the corresponding update expression includes: The first truth table of the reference expression is calculated; After marking the target variable as 0, the first truth table is simplified to obtain the second truth table; The second truth table is simplified using the QM algorithm to obtain the update expression corresponding to the reference expression.
3. The code generation system based on a template engine according to claim 1, characterized in that, The system also includes a chip, which comprises several modules; When the computer program is executed by the processor, the following steps are also performed: S111, traverse each module, take the current module as the target module and execute steps S102 to S109 to obtain the first target code corresponding to each module; S112, for any output port corresponding to each module, if the output port is not assigned a value in the corresponding first target code, then delete the output port from the corresponding first target code; S113, traverse all output ports to obtain the second target code corresponding to each module.
4. The code generation system based on a template engine according to claim 3, characterized in that, After deleting the output port from the corresponding first target code, the output port is recorded in the first deleted port table.
5. The code generation system based on a template engine according to claim 4, characterized in that, When the computer program is executed by the processor, the following steps are also performed: S114, Scan all connection relationships between modules. If at least one output port connected to any connection relationship is included in the first deletion port table, then delete the connection relationship.
6. The code generation system based on a template engine according to claim 4, characterized in that, When the computer program is executed by the processor, the following steps are also performed: S115, Scan all input ports of each module. If any input port is connected to an output port that is included in the first deletion port table, then delete the input port from the corresponding second target code. S116, traverse all input ports to obtain the third target code corresponding to each module, and use the third target code corresponding to each module as the corresponding updated first target code.
7. The code generation system based on a template engine according to claim 6, characterized in that, After deleting the input port from the corresponding second target code, the input port is recorded in the second deleted port table of the corresponding module.
8. The code generation system based on a template engine according to claim 7, characterized in that, When the computer program is executed by the processor, the following steps are also performed: S117. For any module, if the second deletion port table of the module is not empty, then determine the variable corresponding to any input port in the second deletion port table of the module as the target variable, and return to execute steps S107 to S110 until the regenerated first target code corresponding to the module is obtained. S118, return to steps S112 to S117 until the second deletion port table of all modules is empty, and obtain the final code corresponding to each module.