ST language-based abstract syntax tree optimization method

Build a custom abstract syntax tree through the antlr tool and perform constant propagation, merging and dead code elimination, solving the problem of insufficient optimization of abstract syntax tree in compiler front-end technology, improving compilation speed and execution efficiency, and achieving a more efficient compilation process .

CN119127208APending Publication Date: 2024-12-13NANJING SCIYON AUTOMATION GRP
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
CN202411174118.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-26
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

In the existing compiler front-end technology, the abstract syntax tree optimization method has problems such as insufficient node relationship, slow compilation speed and low controllability. Especially when dealing with ST language, the generated executable files are large and the execution efficiency is low. .

Method used

The antlr tool is used to parse ST language files, build a custom abstract syntax tree, and traverse nodes to propagate constants, merge and eliminate dead codes. The syntax tree is optimized using eigenvalue calculations to reduce the complexity of the tree and optimize it directly on the front end of the compiler.

Benefits of technology

It improves compilation speed and execution efficiency, reduces the generated executable program instructions, improves the execution performance after compilation, and achieves a more efficient and controllable compilation process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127208A_ABST
    Figure CN119127208A_ABST
Patent Text Reader

Abstract

According to the abstract syntax tree optimization method based on the ST language, the ST programming language is optimized through the compiler front-end technology, the code structure is optimized from the source, the compiling efficiency is improved, and the execution efficiency of the finally generated instruction is improved. An abstract syntax tree optimization method based on an ST language comprises the steps that 1, an antlr tool is used for conducting lexical analysis and grammatical analysis of the ST language, and a complete abstract syntax tree is created; (2) traversing the abstract syntax tree to carry out constant merging, calculating constant tree nodes in advance, and merging the constant tree nodes into a tree node; (3) traversing the abstract syntax tree to eliminate dead codes, and deleting useless tree nodes; and (4) traversing the abstract syntax tree to calculate characteristic values of the tree nodes, and eliminating sub-expressions according to the similarity of the tree nodes, thereby reducing redundant calculation and improving execution efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of compiler front-ends, and in particular to an abstract syntax tree optimization method based on ST language. Background Art

[0002] Compiler front-end technology is an important part of the compiler. It is mainly responsible for lexical analysis, syntax analysis, semantic analysis of the source code, generation of abstract syntax trees, symbol checking, generation of intermediate codes, etc. In the current compiler front-end field, it is possible to convert the programming language into corresponding machine instructions, but there will be problems such as large executable files and low execution efficiency. How to improve the execution efficiency of the compiler-generated instructions is crucial to the design of the compiler.

[0003] Currently, most compilers use compiler backend optimization technology to further optimize the intermediate code generated by the abstract syntax tree. This part is mainly completed by GCC or LLVM, which may lead to uncontrollable secondary development and incomplete optimization. Optimizing the syntax tree level directly at the compiler front end is the most efficient and complete optimization.

[0004] Among the existing abstract syntax tree optimization methods, the most advanced ones use algorithms such as word vectors in natural language processing to extract nodes from the abstract syntax tree, form a text composed of node words, and then use methods such as word feature extraction in natural language processing to obtain feature vector identifiers of all nodes. Problems When using depth-first traversal of nodes, sibling nodes may be excluded from the context, while using breadth-first traversal, child nodes may be excluded, which results in insufficient connection between context nodes.

[0005] ST language is a high-level programming language defined in IEC61131-3. There are few compilers for ST language in China. Most of them convert ST language into C language and then call GCC or clang for compilation. The compilation speed is slow and the controllability is low. A small number of them use self-developed ST language compilers to generate corresponding machine instructions for PLC execution based on ST language. The size and execution efficiency of the generated machine instructions directly affect the scale and performance of PLC. Summary of the invention

[0006] Purpose of the invention: The technical problem to be solved by the present invention is to provide an abstract syntax tree optimization method based on the ST language in view of the deficiencies of the prior art. It can realize the front-end optimization of the compiler compilation process, and put the optimization that can be processed in front as much as possible, eliminating the optimization through complex algorithms in the back-end of the compiler, which depends on the computing power of the computer. The lower the computer performance, the slower the compilation speed. Compared with back-end optimization, front-end optimization is more direct, more efficient, more convenient, more controllable, and can greatly improve the compilation speed.

[0007] The method of the present invention specifically comprises the following steps:

[0008] Step 1, use the antlr tool as the ST language parser, parse the ST language file, and automatically build the antlr parse tree;

[0009] Step 2, traverse the parse tree of antlr to build a custom abstract syntax tree and a custom abstract syntax tree node;

[0010] Step 3, traverse the variable definition tree nodes of the abstract syntax tree and build a variable symbol table;

[0011] Step 4, traverse the execution body nodes of the abstract syntax tree, perform constant type resolution, constant value resolution, and data type judgment on the symbolic name of each child node, and assign whether the node is a constant, constant value, and data type attributes.

[0012] Further, in step 2, the parse tree traversal method provided by antlr is used to traverse from top to bottom, and an abstract syntax tree is constructed according to the parse tree node type and content of antlr. The abstract syntax tree tree node constructs a specific node class according to the node type. All node classes inherit from the same base class, and the base class has the following attributes: node type, symbol name, whether it is a constant, constant value, data type; after the node is created, it is added to the parent node member list. In the present invention, antlr is used as the ST language parser to parse the ST language file. The ST program uniformly defines the variables in the header, which is collectively referred to as the variable definition tree node. Immediately following the header is the executable body of the ST program, which is collectively referred to as the executable body tree node.

[0013] Furthermore, in step 3, the variable symbol table has the following attributes: symbol name, data type, initial value, variable type, and whether to maintain.

[0014] Furthermore, step 4 includes: propagating the constant value and constant type to the parent node according to whether the child node is a constant, and then performing constant merging calculations according to the symbol type or operator between nodes, removing the calculated node from the parent node, creating a constant tree node type according to the calculated constant value, assigning the constant value to the attribute corresponding to the node, and adding it to the corresponding parent node, so as to achieve the purpose of node merging and constant merging, reduce the complexity of the tree, and reduce the instruction generation of the compiler backend.

[0015] Furthermore, step 4 also includes: traversing the tree nodes corresponding to the execution control conditions of specific child member conditional statement nodes and loop statement nodes of the execution body node of the abstract syntax tree, judging whether they are constants and whether the constant value is zero; if they are zero, the conditional statement or loop statement is determined to be invalid, and the conditional statement or loop statement can never be executed; the conditional statement node or loop statement node is removed as a whole from the parent node, dead code elimination is completed, the syntax tree is optimized, and instruction generation of the compiler backend is reduced from the source.

[0016] Furthermore, step 4 also includes: traversing the optimized syntax tree, and performing characteristic value calculation on the expression in the assignment statement (symbol name:=expression;) in the process of traversing the optimized syntax tree, and the calculation formula of the characteristic value is:

[0017]

[0018] Among them, n is the node number, W is the parameter matrix, T n is the nth node type information, S n is the nth node symbol information, C n is the context information obtained by the nth node using the mechanism. Symbol: is the vector concatenation operation. f(x) is the specific value corresponding to the calculated assignment statement. This eigenvalue calculation method eliminates the case relationship of symbol names, the relationship between symbol names, and the relationship between operators.

[0019] Furthermore, in step 4, the establishment of a hash table for management includes: after the eigenvalue calculation is completed, the eigenvalue calculation result is used as the key, and the symbol name on the left side of the assignment statement is used as the value. If the key already exists in the hash table, the key value is replaced.

[0020] Furthermore, in step 4, while traversing the syntax tree, permutation and combination characteristic values ​​are calculated for the child nodes of all expressions, and the calculation results are used to index the corresponding key in the hash table. If the corresponding key exists, the calculated child node is removed from the parent node, the corresponding key value is taken, a symbol node is created, and it is added to the corresponding parent node for node replacement.

[0021] Furthermore, the present invention also provides an electronic device, including a processor and a memory, wherein the memory stores program code, and when the program code is executed by the processor, the steps of the described method are performed.

[0022] Furthermore, the present invention also provides a storage medium storing a computer program or instruction, which executes the steps of the method when the computer program or instruction is run on a computer.

[0023] Beneficial effects:

[0024] The abstract syntax tree optimized by the present invention has perfect symbol information, the syntax tree is more concise, and the optimization process of the original compiler backend is advanced to the compiler frontend for processing. Compared with the backend processing, the frontend processing is more efficient and direct, and the required computer computing power is lower, which can greatly improve the compilation speed. The processed syntax tree generates fewer executable program instructions and has higher execution efficiency, which can greatly improve the execution performance of the compiled executable file. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] The present invention will be further described in detail below in conjunction with the accompanying drawings and specific embodiments, and the above and other advantages of the present invention will become more clear.

[0026] Figure 1 It is a flow chart of an implementation of an abstract syntax tree optimization method based on ST language provided by the present invention.

[0027] Figure 2 It is a schematic diagram of the abstract syntax tree of the preliminary construction of the present invention.

[0028] Figure 3 The present invention constructs a symbolic representation based on an abstract syntax tree.

[0029] Figure 4 It is a schematic diagram of constant propagation and merging of the present invention.

[0030] Figure 5 It is a schematic diagram of the abstract syntax tree of the conditional statement before dead code elimination of the present invention.

[0031] Figure 6 It is a schematic diagram of the abstract syntax tree after dead code elimination of the present invention.

[0032] Figure 7 It is a schematic diagram of the abstract syntax tree before sub-expression elimination of the present invention.

[0033] Figure 8 It is a schematic diagram of the abstract syntax tree after sub-expressions are eliminated according to the present invention.

[0034] The corresponding meanings of the English in the figure are as follows: "Prog" represents the program, "prog_name" represents the program name, "var_decls" represents the variable definition, "var_declinit" represents the variable definition initialization, "simple_specinit" represents the simple declaration initialization, "datatype" represents the data type, "constant" represents the constant, "execute_body" represents the execution body, "stmt_list" represents the statement list, "stmt" represents the statement, "assign_stmt" represents the assignment statement, "varibale" represents the variable name, "expression" represents the expression, "xor_expr" represents the or expression, "and_expr" represents the and expression, "compare_expr" represents the comparison judgment expression, "equ_expr" represents the equality judgment expression, "add_expr" represents the addition and subtraction operation expression, "term" represents the sub-expression, "power_expr" represents the multiplication and division operation expression, "unary_expr" represents the power operation expression, "primary_expr" represents the variable name or constant, and "IF_stmt" represents the IF conditional statement expression. DETAILED DESCRIPTION

[0035] The present invention provides an abstract syntax tree optimization method based on ST language, comprising the following steps:

[0036] Step 1, use the antlr tool as the ST language parser, parse the ST language file, and automatically build the antlr parsing tree;

[0037] Step 2, traverse the parse tree of antlr to build a custom abstract syntax tree and a custom abstract syntax tree node;

[0038] Step 3, traverse the variable definition tree nodes of the abstract syntax tree and build a variable symbol table;

[0039] Step 4, traverse the execution body nodes of the abstract syntax tree, perform constant type resolution, constant value resolution, and data type judgment on the symbolic name of each child node, and assign whether the node is a constant, constant value, and data type attributes.

[0040] In step 2, the parse tree traversal method provided by antlr is used to traverse from top to bottom, and an abstract syntax tree is constructed according to the parse tree node type and content of antlr. The abstract syntax tree tree node constructs a specific node class according to the node type. All node classes inherit from the same base class, and the base class has the following attributes: node type, symbol name, whether it is a constant, constant value, data type; after the node is created, it is added to the parent node member list. In the present invention, antlr is used as the ST language parser to parse the ST language file. The ST program uniformly defines the variables in the header, which is collectively referred to as the variable definition tree node. Immediately following the header is the executable body of the ST program, which is collectively referred to as the executable body tree node.

[0041] In step 3, the variable symbol table has the following attributes: symbol name, data type, initial value, variable type, and whether to maintain.

[0042] Step 4 includes: propagating the constant value and constant type to the parent node according to whether the child node is a constant, and then performing constant merging calculations according to the symbol type or operator between nodes, removing the calculated node from the parent node, creating a constant tree node type according to the calculated constant value, assigning the constant value to the attribute corresponding to the node, and adding it to the corresponding parent node to achieve the purpose of node merging and constant merging, reduce the complexity of the tree, and reduce the instruction generation of the compiler backend.

[0043] Step 4 also includes: traversing the tree nodes corresponding to the execution control conditions of the specific child member conditional statement nodes and loop statement nodes of the execution body node of the abstract syntax tree, judging whether they are constants and whether the constant value is zero. If it is zero, the conditional statement or loop statement is determined to be invalid, and the conditional statement or loop statement can never be executed. The entire conditional statement node or loop statement node is removed from the parent node to complete dead code elimination, realize syntax tree optimization, and reduce the instruction generation of the compiler backend from the source.

[0044] Step 4 also includes: traversing the optimized syntax tree, and performing characteristic value calculation on the expression in the assignment statement (symbol name:=expression;) during the traversal of the optimized syntax tree, and the characteristic value calculation formula is:

[0045]

[0046] Among them, n is the node number, W is the parameter matrix, T n is the nth node type information, S n is the nth node symbol information, C nis the context information obtained by the nth node using the mechanism. Symbol: is the vector concatenation operation. f(x) is the specific value corresponding to the calculated assignment statement. This eigenvalue calculation method eliminates the case relationship of symbol names, the relationship between symbol names, and the relationship between operators.

[0047] In step 4, the establishment of a hash table for management includes: after the eigenvalue calculation is completed, the eigenvalue calculation result is used as the key and the symbol name on the left side of the assignment statement is used as the value. If the key already exists in the hash table, the key value is replaced.

[0048] In step 4, while traversing the syntax tree, the permutation and combination characteristic values ​​of all the child nodes of the expression are calculated, and the calculation results are used to index the corresponding key in the hash table. If the corresponding key exists, the calculated child node is removed from the parent node, the corresponding key value is taken, a symbol node is created, and it is added to the corresponding parent node for node replacement.

[0049] The present invention also provides an electronic device, comprising a processor and a memory, wherein the memory stores program code, and when the program code is executed by the processor, the steps of the described method are performed.

[0050] The present invention also provides a storage medium storing a computer program or instruction. When the computer program or instruction is run on a computer, the steps of the method described are executed.

[0051] like Figure 2 As shown, in a specific embodiment of the present invention, a method for generating an abstract syntax tree is provided. The syntax tree has rich node information, including node type, line number corresponding to the node, symbol information of the node, data type of the node, etc. Figure 2 The corresponding ST language source file is:

[0052]

[0053] like Figure 3 ,exist Figure 2 Based on the syntax tree, the variable definition tree nodes of the abstract syntax tree are traversed, the child nodes of the variable definition tree nodes are parsed, and a variable symbol table is constructed. The variable table has attributes such as attribute symbol name, data type, initial value, variable type, and whether to maintain.

[0054] like Figure 4 The embodiment of the present invention provides a tree node optimization method for constant propagation and merging. Figure 2Based on the syntax tree, the execution body nodes of the abstract syntax tree are traversed, and the data type operation, whether it is a constant, constant type analysis, constant value analysis, etc. are performed on each child node according to the symbol name, and various attributes of the tree node are filled, such as whether it is a constant, constant value, data type, etc. After this optimization, the constants are calculated in advance, and there is no need for the compiler backend to generate corresponding calculation instructions, which improves the execution efficiency of the final generated instructions.

[0055] like Figure 5 , Figure 6 , an embodiment of the present invention provides a method for eliminating dead code of tree nodes, which reduces the complexity of the tree and reduces the instruction generation of the compiler backend. Figure 5 , Figure 6 The dependent ST language source files are as follows:

[0056]

[0057]

[0058] The ST language source file is a conditional statement with two branches. By traversing the tree nodes corresponding to the statement, it is determined that the execution condition of the first branch is 0, which means it can never be executed. The execution condition of the second branch is 1, which means it can be executed. The tree nodes that cannot be executed are removed from the syntax tree. After simplification, the syntax tree becomes a conditional syntax with only one branch, such as Figure 6 As shown on the left. It is determined that the corresponding execution body of the simplified conditional statement will always be executed because the corresponding execution condition is always true, so the conditional judgment is removed and the conditional statement is changed into a simple execution syntax. After optimization, the original conditional statement only needs to execute one assignment statement: "tag1:=2;".

[0059] like Figure 7 , 8 The present invention provides a common sub-expression elimination method to reduce redundant calculations and improve execution efficiency. Figure 7 , Figure 8 The dependent ST language source files are as follows:

[0060]

[0061] The ST language source file has two assignment statements. It can be clearly seen that the two assignment statements have a common expression "tag2+tag3". When traversing the first assignment statement, the characteristic value of "tag2+tag3" is calculated. The calculated characteristic value excludes the influence of the relationship between tag2 and tag3. The characteristic value is used as the key and the symbol name "tag1" is used as the value to store it in the expression characteristic value relationship table. Then traverse the second assignment statement, and check the table for "tag3+tag2", "tag3+tag4", and "tag3+tag2+tag4" respectively. It is found that there are consistent characteristic values. After obtaining the corresponding symbol name from the characteristic value relationship table, the tree node is replaced. Finally, "tag5:=tag3+tag2+tag4;" is optimized to "tag5:=tag1+tag4;".

[0062] The present invention provides an abstract syntax tree optimization method based on ST language. There are many methods and ways to implement the technical solution. The above is only a preferred implementation of the present invention. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principle of the present invention. These improvements and modifications should also be regarded as the protection scope of the present invention. All components not specified in this embodiment can be implemented by existing technologies.

Claims

1. An abstract syntax tree optimization method based on ST language, It is characterized in that The following steps are involved: Step 1, use the antlr tool as the ST language parser, parse the ST language file, and automatically build the antlr parsing tree; Step 2, traverse the parse tree of antlr to build a custom abstract syntax tree and a custom abstract syntax tree node; Step 3, traverse the variable definition tree nodes of the abstract syntax tree and build a variable symbol table; Step 4, traverse the execution body nodes of the abstract syntax tree, perform constant type resolution, constant value resolution, and data type judgment on the symbolic name of each child node, and assign whether the node is a constant, constant value, and data type attributes.

2. The method according to claim 1, It is characterized in that In step 2, the parse tree traversal method provided by antlr is used to traverse from top to bottom. The abstract syntax tree is constructed according to the parse tree node type and content of antlr. The abstract syntax tree node constructs a specific node class according to the node type. All node classes inherit from the same base class. The base class has the following properties: node type, symbol name, whether it is a constant, constant value, data type; after the node is created, it is added to the parent node member list.

3. The method according to claim 2, It is characterized in that In step 3, the variable symbol table has the following attributes: symbol name, data type, initial value, variable type, and whether to maintain.

4. The method according to claim 3, It is characterized in that Step 4 includes: propagating the constant value and constant type to the parent node according to whether the child node is a constant, and then performing constant merging calculations according to the symbol type or operator between nodes, removing the calculated node from the parent node, creating a constant tree node type according to the calculated constant value, assigning the constant value to the corresponding attribute of the node, and adding it to the corresponding parent node to achieve the purpose of node merging and constant merging.

5. The method according to claim 4, It is characterized in that Step 4 also includes: traversing the tree nodes corresponding to the execution control conditions of the specific child member conditional statement nodes and loop statement nodes of the execution body node of the abstract syntax tree, judging whether they are constants and whether the constant value is zero. If it is zero, the conditional statement or loop statement is determined to be invalid, and the conditional statement or loop statement can never be executed. The entire conditional statement node or loop statement node is removed from the parent node to complete dead code elimination and realize the optimization of the syntax tree.

6. The method according to claim 5, It is characterized in that Step 4 also includes: traversing the optimized syntax tree, and performing characteristic value calculation on the expression in the assignment statement (symbol name:=expression;) during the traversal of the optimized syntax tree, and the characteristic value calculation formula is: Among them, n is the node number, W is the parameter matrix, T n is the nth node type information, S n is the nth node symbol information, C n It is the context information obtained by the n-th node using the mechanism. The symbol: is the vector concatenation operation, and f(x) is the specific value corresponding to the calculated assignment statement.

7. The method according to claim 6, It is characterized in that In step 4, the establishment of a hash table for management includes: after the eigenvalue calculation is completed, the eigenvalue calculation result is used as the key and the symbol name on the left side of the assignment statement is used as the value. If the key already exists in the hash table, the key value is replaced.

8. The method according to claim 7, It is characterized in that In step 4, while traversing the syntax tree, the permutation and combination characteristic values ​​of all the child nodes of the expression are calculated, and the calculation results are used to index the corresponding key in the hash table. If the corresponding key exists, the calculated child node is removed from the parent node, the corresponding key value is taken, a symbol node is created, and it is added to the corresponding parent node for node replacement.

9. An electronic device, It is characterized in that The method comprises a processor and a memory, wherein the memory stores program codes, and when the program codes are executed by the processor, the processor executes the steps of the method according to any one of claims 1 to 8.

10. A storage medium, It is characterized in that A computer program or instruction is stored, and when the computer program or instruction is run on a computer, the steps of the method according to any one of claims 1 to 8 are executed.

Citation Information

Cited By

  • Automated code activity graph generation system and method

    CN119690440A

  • Incremental compiling method and system for Modelica model

    CN119759362A

  • Tensor structure information analysis method and system

    CN120066572A

  • C + + code coverage rate visual detection method

    CN120234241A