Program analysis method and device, equipment and medium
By obtaining the abstract syntax tree and generating expression signatures and value numbers, the problem of high program analysis time and manpower costs is solved, enabling fast and accurate data shape analysis and supporting data out-of-bounds debugging and optimization.
Patent Information
- Application Number
- CN202511162698.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-19
- Publication Date
- 2025-11-21
AI Technical Summary
Existing technology-based program analysis solutions have high time and manpower costs, and their accuracy is difficult to guarantee.
By obtaining the abstract syntax tree of the program to be analyzed, a value number table and a signature number mapping table are created. The syntax tree is traversed to generate expression signatures and value numbers. These tables are managed using static syntax scope to achieve automatic analysis of data shape expressions.
It enables fast and accurate analysis of data shape expressions in program source code, reducing time and manpower costs, improving analysis accuracy, and supporting data out-of-bounds debugging and optimization operations.
Smart Images

Figure CN120994205A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer, and in particular, to a program analysis method and device, equipment and medium. BACKGROUND
[0002] With the development of artificial intelligence (AI) technology, more and more enterprises begin to run programs in AI chips to perform image processing, text processing or voice processing. Before loading the program for performing image processing, text processing or voice processing into the AI chip for running, the data shape expression generated and referenced in the source code of the program needs to be analyzed to obtain the related information of the data shape expression, so that the technical personnel can debug the data out-of-bound related information, perform redundancy elimination or specific optimization operation according to the related information of the data shape expression. The data shape expression refers to an expression related to the data shape, which usually includes an expression for defining an integer variable, an expression for defining a shape variable, and a calculation expression for calculating according to the defined integer variable and / or shape variable. The above expressions are all related to the data shape.
[0003] In the related art, a common program analysis scheme is that the technical personnel manually analyze the data shape expression generated and referenced in the source code of the program to obtain the related information of the data shape expression. The program analysis scheme in the related art relies on manual operation, which has high time cost and labor cost, and the accuracy is difficult to guarantee. SUMMARY
[0004] The present application provides a program analysis method, device, equipment and medium to solve the problem of high time cost and labor cost and difficult to guarantee the accuracy of the program analysis scheme in the related art.
[0005] According to an aspect of the present application, a program analysis method is provided, comprising:
[0006] An abstract syntax tree corresponding to a program to be analyzed is obtained; wherein the abstract syntax tree includes a plurality of nodes, and the node types of the abstract syntax tree include function nodes, control block nodes, statement nodes and expression nodes;
[0007] A value number table and a signature number mapping table corresponding to the program to be analyzed are created; wherein the value number table is used to store the value numbers of the expression signatures in the abstract syntax tree, the signature number mapping table is used to store the mapping relationship between the expression signatures and the value numbers in the abstract syntax tree, and the value number table and the signature number mapping table manage the current effective value number and signature number mapping set in the value number table and the signature number mapping table in the form of static syntax scope.
[0008] traversing the abstract syntax tree, for each data shape related expression node, determining the current stack frame where the value number table and the signature number mapping table are located according to the node type, generating an expression signature, generating a value number mapped with the expression signature, storing the value number mapped with the expression signature into the current effective field of the value number table, and storing the expression signature and the value number mapped with the expression signature into the current effective field of the signature number mapping table.
[0009] According to another aspect of the present application, a program analysis device is provided, comprising:
[0010] a syntax tree obtaining module configured to obtain an abstract syntax tree corresponding to a program to be analyzed, wherein the abstract syntax tree comprises a plurality of nodes, and node types of the abstract syntax tree include function nodes, control block nodes, statement nodes and expression nodes;
[0011] a data table creating and managing module configured to create a value number table and a signature number mapping table corresponding to the program to be analyzed, wherein the value number table is configured to store value numbers of expression signatures in the abstract syntax tree, the signature number mapping table is configured to store mapping relationships between expression signatures and value numbers in the abstract syntax tree, and the value number table and the signature number mapping table manage current effective value number and signature number mapping set in the value number table and the signature number mapping table in a static syntax scope manner;
[0012] a node traversing module configured to traverse the abstract syntax tree, for each data shape related expression node, determine the current stack frame where the value number table and the signature number mapping table are located according to the node type, generate an expression signature, generate a value number mapped with the expression signature, store the value number mapped with the expression signature into the current effective field of the value number table, and store the expression signature and the value number mapped with the expression signature into the current effective field of the signature number mapping table.
[0013] According to another aspect of the present application, an electronic device is provided, comprising:
[0014] at least one processor;
[0015] and a memory connected with the at least one processor in communication;
[0016] wherein the memory stores a computer program executed by the at least one processor, and the computer program is executed by the at least one processor to enable the at least one processor to execute the program analysis method according to any one of the embodiments of the present application.
[0017] According to another aspect of the present invention, a computer-readable storage medium is provided, the computer-readable storage medium storing computer instructions for causing a processor to execute and implement the program analysis method described in any embodiment of the present invention.
[0018] According to another aspect of the present invention, a computer program product is provided, the computer program product comprising a computer program that, when executed by a processor, implements the program analysis method described in any embodiment of the present invention.
[0019] The technical solution of this invention involves obtaining an abstract syntax tree (AST) corresponding to the program to be analyzed. The AST contains multiple nodes, including function nodes, control block nodes, statement nodes, and expression nodes. Then, a value number table and a signature number mapping table corresponding to the program to be analyzed are created. The value number table stores the value numbers of expression signatures in the AST, and the signature number mapping table stores the mapping relationship between expression signatures and value numbers in the AST. The value number table and signature number mapping table manage the currently valid value number and signature number mapping sets in the value number table and signature number mapping table using static syntactic scoping. The AST is traversed, and for each data-shape-related expression node, the current stack frame containing the value number table and signature number mapping table is determined based on the node type. An expression signature is generated, and a value number mapped to the expression signature is generated and stored. In the current valid field of the value number table, the expression signature and the value number mapped to the expression signature are stored in the current valid field of the signature number mapping table. This solves the problems of high time and manpower costs and difficulty in guaranteeing accuracy in related program analysis schemes. Based on the abstract syntax tree corresponding to the program, it can automatically analyze the data shape expressions generated and referenced in the program's source code, obtain the expression signature and value number, store the signature through the value number table, and store the mapping relationship between the signature and the value number through the signature number mapping table. This enables automatic analysis of the data shape expressions generated and referenced in the program's source code, quickly and accurately obtaining relevant information about the data shape expressions. This facilitates technicians to perform data out-of-bounds debugging, redundancy elimination, or specific optimization operations based on the relevant information of the data shape expressions, reducing the time and manpower costs of the program analysis process and improving the accuracy of the program analysis process.
[0020] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of the present invention, nor is it intended to limit the scope of the invention. Other features of the invention will become readily apparent from the following description. Attached Figure Description
[0021] 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.
[0022] Figure 1 This is a flowchart of a program analysis method provided in Embodiment 1 of the present invention.
[0023] Figure 2 This is a flowchart of a program analysis method provided in Embodiment 2 of the present invention.
[0024] Figure 3 This is a schematic diagram of the structure of a program analysis device provided in Embodiment 3 of the present invention.
[0025] Figure 4 A schematic diagram of the structure of an electronic device for implementing the program analysis method of this invention. Detailed Implementation
[0026] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments. 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 should fall within the scope of protection of the present invention.
[0027] It should be noted that the terms "target," "first," "second," etc., in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms "comprising," "including," and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0028] Example 1
[0029] Figure 1This is a flowchart illustrating a program analysis method provided in Embodiment 1 of the present invention. This embodiment is applicable to situations where, before loading a program used for image processing, text processing, or speech processing into an AI chip for execution, the data shape expressions generated and referenced in the program's source code are analyzed. This method can be executed by a program analysis device, which can be implemented in hardware and / or software and can be configured in an electronic device. Figure 1 As shown, the method includes:
[0030] Step 101: Obtain the abstract syntax tree corresponding to the program to be analyzed.
[0031] The abstract syntax tree contains multiple nodes, and the types of nodes in the abstract syntax tree include function nodes, control block nodes, statement nodes, and expression nodes.
[0032] Optionally, the program to be analyzed can be a program that needs to be loaded into the artificial intelligence chip to run for processing such as image processing, text processing, or speech processing.
[0033] Optionally, the abstract syntax tree (AST) corresponding to the program to be analyzed can be a tree-like representation of the abstract syntax structure of the program's source code. The AST contains multiple nodes, each representing a syntactic unit in the program's source code. The types of nodes in the AST include function nodes, control block nodes, statement nodes, and expression nodes. Function nodes can be multiple statements representing a specified function definition structure or function call structure in the program's source code. Control block nodes can be multiple statements representing a specified flow control structure in the program's source code. Statement nodes can be multiple statements representing changes in the execution state in the program's source code. Expression nodes can be statements representing specified computational logic or value operations in the program's source code.
[0034] Optionally, the abstract syntax tree corresponding to the program to be analyzed contains at least one function node. For each function node, there may be zero or more control block nodes, zero or more statement nodes, and zero or more expression nodes connected to the function node via connecting edges. Each control block node connected to the function node is a child node of the function node, and the function node is the parent node of each control block node. For each control block node, there may be zero or more statement nodes and zero or more expression nodes connected to the control block node via connecting edges. Each statement node connected to the control block node is a child node of the control block node, and the control block node is the parent node of each statement node. For each statement node, there may be zero or more expression nodes connected to the statement node via connecting edges as its child nodes; in this case, the expression node is the parent node of all its child expression nodes.
[0035] Optionally, the abstract syntax tree contains at least one scope. For each function node in the abstract syntax tree, a unique static syntax scope can be determined. Its child nodes are considered to be within the scope of that static syntax scope.
[0036] Optionally, it can detect whether the electronic device has received an abstract syntax tree (API) corresponding to the program to be analyzed. If it detects that the electronic device has received an API, it can acquire the API received corresponding to the program to be analyzed. The API corresponding to the program to be analyzed can be sent by the target user through a terminal device. The target user can be a technical personnel responsible for managing the program to be analyzed.
[0037] Optionally, it can detect whether the electronic device has received the source code of the program to be analyzed. If it detects that the electronic device has received the source code, a compiler is used to process the source code into an abstract syntax tree (AST), thereby obtaining the AST corresponding to the program to be analyzed. The source code of the program to be analyzed can be sent by the target user through a terminal device.
[0038] Step 102: Create a value number table and a signature number mapping table corresponding to the program to be analyzed.
[0039] The value number table stores the value numbers of expression signatures in the abstract syntax tree, and the signature number mapping table stores the mapping relationship between expression signatures and value numbers in the abstract syntax tree. The value number table and the signature number mapping table manage the currently valid value number and signature number mapping set in the value number table and the signature number mapping table in a static syntax scope manner.
[0040] Optionally, the expression can be a data shape-related expression in the abstract syntax tree, typically used to define and use integer and shape variables. For example, "int dim = 7" is an assignment expression that defines an integer variable named "dim". "a:[3,2]" is an assignment expression that defines a shape variable named "a" using the two-dimensional shape expression "[3,2]". "b:[a(0)]" defines a one-dimensional data shape variable named "b" using the shape expression "[a(0)]", which uses the 0th dimension of the shape variable "a" as its dimension value. "a + dim" is an addition expression that adds the shape variable "a" to the integer variable "dim" to obtain a new one-dimensional shape expression. All of the above expressions are related to data shape.
[0041] Optionally, the expression signature can be a character used to identify the expression. The value number of the expression signature can be a unique numeric number used to characterize the expression. The value number table can be a data table used to store the value numbers of expression signatures in the abstract syntax tree. The signature number mapping table can be a data table used to store the mapping relationship between expression signatures and value numbers in the abstract syntax tree.
[0042] Optionally, the value number table corresponding to the program to be analyzed is a value number table used to store the value numbers of the expression signatures in the abstract syntax tree corresponding to the program to be analyzed. The signature number mapping table corresponding to the program to be analyzed can be a signature number mapping table used to store the mapping relationship between expression signatures and value numbers in the abstract syntax tree corresponding to the program to be analyzed. The value number table and the signature number mapping table manage the currently valid value numbers and valid signature number mappings in a static syntactic scope manner. Specifically, during the analysis of the abstract syntax tree, the value numbers of each expression signature located in the same scope are stored in a stack frame of the value number table, and the mapping relationship between each expression signature and value number located in the same scope is stored in a stack frame of the signature number mapping table.
[0043] Optionally, a data table can be created in the electronic device, defining it as a value number table corresponding to the program to be analyzed. During the analysis of the abstract syntax tree, stack frames can be created and deleted from the value number table. The value numbers of various expression signatures within the same scope can be stored in a stack frame. A stack frame can be a cell used to store data.
[0044] Optionally, a data table can be created in the electronic device, which can then be used as a signature number mapping table corresponding to the program to be analyzed. During the analysis of the abstract syntax tree, stack frames can be created and deleted from the signature number mapping table, allowing the mapping relationship between the signatures and value numbers of various expressions within the same scope to be stored in a single stack frame.
[0045] Step 103: Traverse the abstract syntax tree. For each data shape-related expression node, determine the current stack frame where the value number table and the signature number mapping table are located according to the node type. Generate an expression signature and a value number mapped to the expression signature. Store the value number mapped to the expression signature in the current valid field of the value number table. Store the expression signature and the value number mapped to the expression signature in the current valid field of the signature number mapping table.
[0046] Optionally, a data shape-related expression node refers to a node containing expressions related to data shapes that generate or reference data shapes. A node generating a data shape may mean that the node or its child nodes contain definition statements for defining integer variables and / or shape variables. These definition statements typically contain expressions for defining integer variables and / or expressions for defining shape variables. The defined variables can be named variables such as "a" or anonymous variables. A node referencing a data shape may mean that the node or its child nodes contain computational expressions for defined integer variables and / or shape variables. For each node containing expressions related to data shapes, the expression signature characterizes the node's syntax and uniquely identifies the data shape-related expression contained within the node. The value number mapped to the expression signature characterizes the node's semantic features, and the numerical label represents the result generated by the data shape-related expression contained in the node at runtime.
[0047] Optionally, the nodes of the abstract syntax tree corresponding to the program to be analyzed can be traversed to determine whether the traversed node is a data shape-related expression node. If the traversed node is a data shape-related expression node, an expression signature is generated, and a value number mapped to the expression signature is generated. The value number mapped to the expression signature is stored in the current valid field of the value number table, and the expression signature and the value number mapped to the expression signature are stored in the current valid field of the signature number mapping table, and then other nodes are traversed. If the traversed node is not a data shape-related expression node, other nodes are traversed.
[0048] Optionally, traversing the abstract syntax tree includes: traversing the abstract syntax tree in a depth-first manner, and selecting nodes in the abstract syntax tree that generate and reference data shape expressions. Nodes that generate and reference data shape expressions refer to data shape-related expression nodes.
[0049] Optionally, determining the current stack frame containing the value number table and the signature number mapping table based on the node type includes: creating a new stack frame for the value number table and the signature number mapping table when entering a scope, the newly created stack frame being the current stack frame containing the value number table and the signature number mapping table; storing the data in the top stack frame of the value number table and the signature number mapping table to the data storage location corresponding to the scope when leaving a scope, deleting the top stack frame of the value number table and the signature number mapping table, and the new top stack frame being the current stack frame containing the value number table and the signature number mapping table; wherein, the current valid field of the value number table is the union of the value numbers in the current stack frame of the value number table and the value numbers in all frames from the previous frame to the bottom of the stack, and the current valid field of the signature number mapping table is the union of the mapping relationship between the expression signature and the value number in the current stack frame of the signature number mapping table and the mapping relationship between the expression signature and the value number in all frames from the previous frame to the bottom of the stack. The previous frame up to the bottom of the stack refers to all existing stack frames in the signature number mapping table, excluding the current stack frame.
[0050] Optionally, entering a scope can refer to traversing to any function node or control block node. Each time an arbitrary function node is reached, a new stack frame is created for the value number table, which is the current stack frame containing the value number table. A new stack frame is also created for the signature number mapping table, which is the current stack frame containing the signature number mapping table. The current stack frame containing the value number table stores the value numbers mapped to the expression signatures of the various data shape-related expression nodes within the function node and its child nodes. A function node and its child nodes belong to a scope. When traversing to various data shape-related expression nodes within this scope, the value numbers mapped to the expression signatures are stored in the current stack frame containing the value number table created when entering the scope. The current stack frame containing the signature number mapping table stores the mapping relationship between the expression signatures and value numbers of various data shape-related expression nodes within this scope. When traversing to various data shape-related expression nodes within this scope, the mapping relationship between the expression signatures and value numbers is stored in the current stack frame containing the signature number mapping table created when entering the scope. The processing method after traversing to any control block node is the same as the above processing method.
[0051] Optionally, leaving the scope can refer to completing the traversal of all nodes in the scope, i.e., when traversing the completed function node and control block node. The top frame of the stack for the value number table and signature number mapping table can refer to the stack frame in the value number table and signature number mapping table used to store the current mapping relationship. When all nodes in the scope are traversed, the stack frame containing the value number table and signature number mapping table storing the relevant data of that scope is the top frame of the stack for the value number table and signature number mapping table. The data storage location corresponding to this scope includes the number storage location and the mapping relationship storage location. The number storage location can be a file or memory used to store the value numbers mapped to the expression signatures of all data shape-related expression nodes in this scope. The mapping relationship storage location can be a file or memory used to store the mapping relationship between the expression signatures and value numbers of all data shape-related expression nodes in this scope. Upon completing each iteration of all nodes within any scope, the data from the top frame of the value number table is stored in the corresponding number storage location for that scope, and the top frame of the value number table is deleted. Similarly, the data from the top frame of the signature number mapping table is stored in the corresponding mapping relationship storage location for that scope, and the top frame of the signature number mapping table is deleted. After completing each iteration of all nodes within any scope, the current stack frame is deleted, and the previous frame becomes the top frame. This previous frame then becomes the current stack frame containing both the value number table and the signature number mapping table, and the previous stack frame containing both becomes the new top frame.
[0052] Optionally, after determining that the traversed function node or control block node is a data shape-related expression node, the stack frame of the newly created value number table when traversing the node is determined as the current stack frame where the value number table resides, and the stack frame of the newly created signature number mapping table when traversing the node is determined as the current stack frame where the signature number mapping table resides. An expression signature is generated, and a value number mapped to the expression signature is generated. The value number mapped to the expression signature is stored in the current stack frame of the value number table, and the expression signature and the value number mapped to the expression signature are stored correspondingly in the current stack frame where the signature number mapping table resides. Thus, the value number mapped to the expression signature is stored in the current valid field of the value number table, and the expression signature and the value number mapped to the expression signature are stored correspondingly in the current valid field of the signature number mapping table.
[0053] Optionally, after determining that any statement node or expression node encountered during traversal is a data shape-related expression node, the stack frame corresponding to the scope of the node in the value number table is determined as the current stack frame of the value number table, and the stack frame corresponding to the scope of the node in the signature number mapping table is determined as the current stack frame of the signature number mapping table. An expression signature is generated, and a value number mapped to the expression signature is generated. The value number mapped to the expression signature is stored in the current stack frame of the value number table, and the expression signature and the value number mapped to the expression signature are stored correspondingly in the current stack frame of the signature number mapping table. Thus, the value number mapped to the expression signature is stored in the current valid domain of the value number table, and the expression signature and the value number mapped to the expression signature are stored correspondingly in the current valid domain of the signature number mapping table. The stack frame corresponding to the scope of the control block node is a newly created stack frame when entering the scope of the control block node.
[0054] Optionally, the dimension of an expression typically refers to the number of degrees of freedom or independent parameters in the variable space involved in the expression. Each data shape expression can be a single data shape expression or a combination of multiple data shape expressions.
[0055] Optionally, for a given data shape expression composed of multiple data shape expressions, each data shape expression within that data shape expression is considered a sub-expression contained within the data shape expression. The dimension of a data shape expression can refer to the sum of the dimensions of its sub-expressions. When a data shape expression contains only sub-expressions with a dimension of 1, the dimension of the data shape is the sum of the number of its sub-expressions.
[0056] In a specific instance, "
[77] " is a single data shape expression with a dimension of 1. "a:[3+2,5]" is a data shape expression with a dimension of 2. "[a,13]" is a data shape expression with a dimension of 3 because it contains a data shape expression "a" with a dimension of 2 and a data shape expression "13" with a dimension of 1. The dimension of a data shape expression is always an integer greater than or equal to 0.
[0057] Optionally, if a node contains a single data shape expression, an expression signature is generated according to a preset signature rule. Generating the expression signature according to the preset signature rule includes: merging the scope representation statement and the data shape expression contained in the node, and determining the merged scope representation statement and data shape expression as the expression signature of the data shape expression. The scope representation statement is a statement contained in the node that represents the scope in which the data shape expression resides. In a specific instance, expressions "c" and "17" are single data shape expressions. The expression "c" and its scope representation statement "::my_namespace::my_func" can be merged using the concatenation operator "::" to obtain "::my_namespace::my_func::c", and "::my_namespace::my_func::c" is determined as the expression signature of the variable reference expression "c". Similarly, the expression "17" and its scope representation statement "literal" can be directly merged to obtain "literal 17", and "literal17" is determined as the expression signature of the immediate expression "17".
[0058] Optionally, if a node is a data shape expression consisting of multiple sub-expressions, then the expression signatures of each sub-expression in the data shape expression and the value numbers mapped to the expression signatures are generated, and the expression signature is synthesized. Synthesizing the expression signature includes: determining the obtained sequence of value numbers mapped to each expression signature as the expression signature of the data shape expression.
[0059] For each subexpression with a dimension of 1, the scope representation statement of the subexpression is merged with the subexpression itself. The merged scope representation statement and the subexpression are then used to determine the expression signature of the subexpression, and a value number is generated that maps to the expression signature of the subexpression.
[0060] If the dimension of the target subexpression is 2 in each subexpression, then for each nested expression in the target subexpression, the scope representation statement and expression of the expression are merged, the merged scope representation statement and expression are determined as the expression signature of the expression, a value number mapped to the expression signature of the expression is generated, the resulting value number sequence is determined as the expression signature of the target subexpression, and a value number mapped to the expression signature of the target subexpression is generated.
[0061] Therefore, the value number sequence mapped to the expression signature of each sub-expression is obtained, the value number used to synthesize the expression signature is deleted from the value number sequence, and the value number sequence mapped to the expression signature of each sub-expression after deletion is determined as the expression signature of the data shape expression.
[0062] Optionally, in a specific instance, the expression "[b,c+3]" contains the subexpressions "c", "b", "3", and "c+3".
[0063] The subexpression "c" and its scope representation statement "::my_namespace::my_func" are merged using the concatenation operator "::" to obtain "::my_namespace::my_func::c". "::my_namespace::my_func::c" is then determined as the expression signature of the subexpression "c" and a value number "0" is generated.
[0064] The subexpression "b" and its scope representation statement "::my_namespace::my_func" are merged using the concatenation operator "::" to obtain "::my_namespace::my_func::b". "::my_namespace::my_func::b" is then identified as the signature of the subexpression "b" and the value number "1" is generated.
[0065] The subexpression "3" and its scope representation statement "literal" are merged to obtain "literal3". "literal 3" is determined as the expression signature of the subexpression "3" and the value number "2" is generated.
[0066] The subexpression "c+3" contains the next-level expression "c" and the next-level expression "3". The value number sequence "0, 2" mapped to the expression "c" and the expression "3" is determined as the expression signature of the subexpression "c+3", and the value number "3" is generated.
[0067] Therefore, "1, 3" is determined as the expression signature of the expression "[b, c+3]", and the value numbers "0, 1, 2, 3" will be reused by subsequent value numbers.
[0068] Optionally, the expression signature can be optimized by commutation. For example, suppose the addition expression is "b+a", which has subexpressions "b" and "a". Since addition expressions are commutative, by applying the optimization rule (operands are sorted lexicographically), the expression is normalized to "a+b".
[0069] Optionally, the expression signature can be optimized by arithmetic simplification. As an example, suppose the division expression is "25 / 5", which has constant quantum expressions "25" and "5". It can be directly simplified by constant arithmetic. Therefore, by applying the optimization rule (operands are sorted in lexicographical order), the expression signature is normalized to "5".
[0070] Optionally, the multi-dimensional expression signature can be optimized. This involves performing the aforementioned swapping and arithmetic simplification optimizations on the signature for each dimension, and then merging the optimized signatures for each dimension into the optimized multi-dimensional expression signature.
[0071] Optionally, generating a value number mapped to the expression signature includes: querying whether a value number mapped to the expression signature exists in the current valid field of the signature number mapping table; if a value number mapped to the expression signature exists in the current valid field of the signature number mapping table, then obtaining the value number mapped to the expression signature for reuse.
[0072] Optionally, it also includes: if there is no value number mapped to the expression signature in the current valid field of the signature number mapping table, then a value number mapped to the expression signature is generated according to a preset value numbering rule.
[0073] Optionally, the signature number mapping table can be queried to see if a target signature identical to the expression signature exists among the signatures stored in the currently valid fields. If a target signature identical to the expression signature exists, the value number mapped to the target signature is determined as the value number mapped to the expression signature. The current valid fields of the signature number mapping table are then checked to see if a value number mapped to the expression signature exists, and this value number is retrieved for reuse. If no target signature identical to the expression signature exists, the current valid fields of the signature number mapping table are checked to see if a value number mapped to the expression signature exists, and a value number mapped to the expression signature is generated according to a preset value numbering rule.
[0074] Optionally, a value number mapped to the expression signature is generated according to a preset value numbering rule, including: if no value number is stored in the current valid field of the value number table, the value number mapped to the expression signature is determined to be 0; if a value number is stored in the current valid field of the value number table, the value number with the largest value in the current valid field of the value number table is obtained, and 1 is added to the value number with the largest value in the current valid field of the value number table, which is then used as the value number mapped to the expression signature.
[0075] Optionally, the method for generating the value number of the signature mapping to the subexpression is the same as the method for generating the value number of the signature mapping to the expression.
[0076] Optionally, after generating the expression signature and the value number mapped to it, the value number mapped to the expression signature is stored in the current stack frame of the value number table, and the expression signature and the value number mapped to it are stored in the current stack frame of the signature number mapping table. If the value number mapped to the expression signature is already stored in the current stack frame of the value number table, and the expression signature and the value number mapped to it are already stored in the current stack frame of the signature number mapping table, then this storage is not repeated.
[0077] Optionally, if a signature of a subexpression is generated during the signature generation process, and a value number mapped to the signature of the subexpression is also generated, then the value number mapped to the signature of the subexpression is stored in the current stack frame of the value number table, and the signature of the subexpression and the value number mapped to the signature of the subexpression are stored correspondingly in the current stack frame of the signature number mapping table. If the value number mapped to the signature of the subexpression is already stored in the current stack frame of the value number table, and the signature of the subexpression and the value number mapped to the signature of the subexpression are already stored correspondingly in the current stack frame of the signature number mapping table, then this storage is not repeated.
[0078] Optionally, value numbers can be reused in assignment expressions. In a specific example, in "a:[b,c+3];", if the value number of the subexpression "[b,c+3]" is "4", then the signature of the subexpression "a" "::my_namespace::my_func::a" is assigned the value "4", and the mapping relationship "::my_namespace::my_func::a->4" is stored in the current valid field of the signature number mapping table.
[0079] The technical solution of this invention involves obtaining an abstract syntax tree (AST) corresponding to the program to be analyzed. The AST contains multiple nodes, including function nodes, control block nodes, statement nodes, and expression nodes. Then, a value number table and a signature number mapping table corresponding to the program to be analyzed are created. The value number table stores the value numbers of expression signatures in the AST, and the signature number mapping table stores the mapping relationship between expression signatures and value numbers in the AST. The value number table and signature number mapping table manage the currently valid value number and signature number mapping sets in the value number table and signature number mapping table using static syntactic scoping. The AST is traversed, and for each data-shape-related expression node, the current stack frame containing the value number table and signature number mapping table is determined based on the node type. An expression signature is generated, and a value number mapped to the expression signature is generated and stored. In the current valid field of the value number table, the expression signature and the value number mapped to the expression signature are stored in the current valid field of the signature number mapping table. This solves the problems of high time and manpower costs and difficulty in guaranteeing accuracy in related program analysis schemes. Based on the abstract syntax tree corresponding to the program, it can automatically analyze the data shape expressions generated and referenced in the program's source code, obtain the expression signature and value number, store the signature through the value number table, and store the mapping relationship between the signature and the value number through the signature number mapping table. This enables automatic analysis of the data shape expressions generated and referenced in the program's source code, quickly and accurately obtaining relevant information about the data shape expressions. This facilitates technicians to perform data out-of-bounds debugging, redundancy elimination, or specific optimization operations based on the relevant information of the data shape expressions, reducing the time and manpower costs of the program analysis process and improving the accuracy of the program analysis process.
[0080] Example 2
[0081] Figure 2 This is a flowchart illustrating a program analysis method provided in Embodiment 2 of the present invention. This embodiment of the present invention can be combined with various optional solutions from one or more of the above embodiments. For example... Figure 2 As shown, the method includes:
[0082] Step 201: Obtain the abstract syntax tree corresponding to the program to be analyzed.
[0083] The abstract syntax tree contains multiple nodes, and the types of nodes in the abstract syntax tree include function nodes, control block nodes, statement nodes, and expression nodes.
[0084] Step 202: Create a value number table and a signature number mapping table corresponding to the program to be analyzed.
[0085] The value number table stores the value numbers of expression signatures in the abstract syntax tree, and the signature number mapping table stores the mapping relationship between expression signatures and value numbers in the abstract syntax tree. The value number table and the signature number mapping table manage the currently valid value number and signature number mapping set in the value number table and the signature number mapping table in a static syntax scope manner.
[0086] Step 203: Traverse the abstract syntax tree. For each data shape-related expression node, determine the current stack frame where the value number table and signature number mapping table are located based on the node type. Generate the expression signature. Query whether there is a value number mapped to the expression signature in the current valid field of the signature number mapping table. If so, obtain the value number mapped to the expression signature. Otherwise, generate the value number mapped to the expression signature according to the preset value numbering rules. Store the value number mapped to the expression signature in the current valid field of the value number table. Store the expression signature and the value number mapped to the expression signature in the current valid field of the signature number mapping table.
[0087] The technical solution of this invention can automatically analyze the data shape expressions generated and referenced in the source code of a program based on the abstract syntax tree corresponding to the program, obtain the expression signature and value number, store the signature through a value number table, and store the mapping relationship between the signature and the value number through a signature number mapping table. This enables automatic analysis of the data shape expressions generated and referenced in the source code of the program, quickly and accurately obtaining relevant information about the data shape expressions. This facilitates technicians to perform error checking related to data out-of-bounds errors, redundancy elimination, or specific optimization operations based on the relevant information of the data shape expressions, reducing the time and manpower costs of the program analysis process and improving the accuracy of the program analysis process.
[0088] Example 3
[0089] Figure 3 This is a schematic diagram of a program analysis device provided in Embodiment 3 of the present invention. The device can be configured in an electronic device. Figure 3 As shown, the device includes: a syntax tree acquisition module 301, a data table creation and management module 302, and a node traversal module 303.
[0090] The syntax tree acquisition module 301 is used to acquire the abstract syntax tree corresponding to the program to be analyzed. The abstract syntax tree contains multiple nodes, and the types of nodes include function nodes, control block nodes, statement nodes, and expression nodes. The data table creation and management module 302 is used to create a value number table and a signature number mapping table corresponding to the program to be analyzed. The value number table stores the value numbers of expression signatures in the abstract syntax tree, and the signature number mapping table stores the mapping relationship between expression signatures and value numbers in the abstract syntax tree. The value number table and the signature number mapping table are managed using static syntactic scoping. The node traversal module 303 is used to traverse the abstract syntax tree. For each data shape-related expression node, the current stack frame where the value number table and the signature number mapping table are located is determined according to the node type. An expression signature is generated, and a value number mapped to the expression signature is generated. The value number mapped to the expression signature is stored in the current valid field of the value number table, and the expression signature and the value number mapped to the expression signature are stored in the current valid field of the signature number mapping table.
[0091] The technical solution of this invention involves obtaining an abstract syntax tree (AST) corresponding to the program to be analyzed. The AST contains multiple nodes, including function nodes, control block nodes, statement nodes, and expression nodes. Then, a value number table and a signature number mapping table corresponding to the program to be analyzed are created. The value number table stores the value numbers of expression signatures in the AST, and the signature number mapping table stores the mapping relationship between expression signatures and value numbers in the AST. The value number table and signature number mapping table manage the currently valid value number and signature number mapping sets in the value number table and signature number mapping table using static syntactic scoping. The AST is traversed, and for each data-shape-related expression node, the current stack frame containing the value number table and signature number mapping table is determined based on the node type. An expression signature is generated, and a value number mapped to the expression signature is generated and stored. In the current valid field of the value number table, the expression signature and the value number mapped to the expression signature are stored in the current valid field of the signature number mapping table. This solves the problems of high time and manpower costs and difficulty in guaranteeing accuracy in related program analysis schemes. Based on the abstract syntax tree corresponding to the program, it can automatically analyze the data shape expressions generated and referenced in the program's source code, obtain the expression signature and value number, store the signature through the value number table, and store the mapping relationship between the signature and the value number through the signature number mapping table. This enables automatic analysis of the data shape expressions generated and referenced in the program's source code, quickly and accurately obtaining relevant information about the data shape expressions. This facilitates technicians to perform data out-of-bounds debugging, redundancy elimination, or specific optimization operations based on the relevant information of the data shape expressions, reducing the time and manpower costs of the program analysis process and improving the accuracy of the program analysis process.
[0092] In an optional embodiment of the present invention, the node traversal module 303, when performing the operation of traversing the abstract syntax tree, is specifically used to: traverse the abstract syntax tree in a depth-first manner and select the nodes in the abstract syntax tree that generate and reference data shape expressions.
[0093] In an optional embodiment of the present invention, the node traversal module 303 may, when performing the operation of generating an expression signature, specifically perform the following: if a node contains a single data shape expression, then generate an expression signature according to a preset signature rule.
[0094] In an optional embodiment of the present invention, the node traversal module 303 may, when performing the operation of generating expression signatures, further be used to: if a node is a data shape expression consisting of multiple sub-expressions, generate the expression signatures of each sub-expression in the data shape expression and the value number mapped to the expression signature, and synthesize the expression signature.
[0095] In an optional embodiment of the present invention, the node traversal module 303, when performing the operation of generating a value number mapped to the expression signature, is specifically configured to: query whether there is a value number mapped to the expression signature in the current valid field of the signature number mapping table; if there is a value number mapped to the expression signature in the current valid field of the signature number mapping table, then obtain the value number mapped to the expression signature for reuse.
[0096] In an optional embodiment of the present invention, the node traversal module 303, when performing the operation of generating a value number mapped to the expression signature, is further configured to: if there is no value number mapped to the expression signature in the current valid field of the signature number mapping table, generate a value number mapped to the expression signature according to a preset value numbering rule.
[0097] In an optional embodiment of the present invention, the node traversal module 303, when performing the operation of determining the current stack frame containing the value number table and the signature number mapping table according to the node type, is specifically configured to: create a new stack frame for the value number table and the signature number mapping table when entering the scope, the newly created stack frame being the current stack frame containing the value number table and the signature number mapping table; when leaving the scope, store the data in the stack top frame of the value number table and the signature number mapping table to the data storage location corresponding to the scope, delete the stack top frame of the value number table and the signature number mapping table, and the new stack top frame being the current stack frame containing the value number table and the signature number mapping table; wherein, the current valid field of the value number table is the union of the value number in the current stack frame of the value number table and the value number in all frames from the previous frame to the bottom of the stack, and the current valid field of the signature number mapping table is the union of the mapping relationship between the expression signature and the value number in the current stack frame of the signature number mapping table and the mapping relationship between the expression signature and the value number in all frames from the previous frame to the bottom of the stack.
[0098] The program analysis apparatus provided in the embodiments of the present invention can execute the program analysis method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0099] Example 4
[0100] Figure 4A schematic diagram of an electronic device 10, which can be used to implement the program analysis method of embodiments of the present invention, is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, electronic devices, blade electronic devices, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices (e.g., helmets, glasses, watches, etc.), and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.
[0101] like Figure 4 As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12 or a random access memory (RAM) 13, communicatively connected to the at least one processor 11. The memory stores computer programs executable by the at least one processor. The processor 11 can perform various appropriate actions and processes based on the computer program stored in the ROM 12 or loaded from storage unit 18 into the RAM 13. The RAM 13 may also store various programs and data required for the operation of the electronic device 10. The processor 11, ROM 12, and RAM 13 are interconnected via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0102] Multiple components in electronic device 10 are connected to I / O interface 15, including: input unit 16, such as keyboard, mouse, etc.; output unit 17, such as various types of displays, speakers, etc.; storage unit 18, such as disk, optical disk, etc.; and communication unit 19, such as network card, modem, wireless transceiver, etc. Communication unit 19 allows electronic device 10 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.
[0103] Processor 11 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. Processor 11 performs the various methods and processes described above, such as program analysis methods.
[0104] In some embodiments, the program analysis method may be implemented as a computer program tangibly contained in a computer-readable storage medium, such as a storage unit. In some embodiments, part or all of the computer program may be loaded and / or installed on a heterogeneous hardware accelerator via ROM and / or a communication unit. When the computer program is loaded into RAM and executed by a processor, one or more steps of the program analysis method described above may be performed. Alternatively, in other embodiments, the processor may be configured to execute the program analysis method by any other suitable means (e.g., by means of firmware).
[0105] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.
[0106] Computer programs used to implement the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when executed by the processor, the computer programs cause the functions / operations specified in the flowcharts and / or block diagrams to be performed. The computer programs may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or electronic device.
[0107] In the context of this invention, a computer-readable storage medium can be a tangible medium that may contain or store a computer program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable storage medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination thereof. Alternatively, a computer-readable storage medium can be a machine-readable signal medium. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0108] To provide user interaction, the systems and techniques described herein can be implemented on a heterogeneous hardware accelerator, which includes: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the heterogeneous hardware accelerator. Other types of devices can also be used to provide user interaction; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or haptic feedback); and input from the user can be received in any form (including sound input, voice input, or haptic input).
[0109] The systems and technologies described herein can be implemented in computing systems that include back-end components (e.g., as data electronic devices), or computing systems that include middleware components (e.g., application electronic devices), or computing systems that include front-end components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and technologies described herein), or any combination of such back-end, middleware, or front-end components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., a communication network). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.
[0110] A computing system can include clients and electronic devices. Clients and electronic devices are generally geographically separated and typically interact via communication networks. The client-electronic device relationship is created by computer programs running on the respective computers and establishing a client-electronic device relationship between them. Electronic devices can be cloud electronic devices, also known as cloud computing electronic devices or cloud servers, which are hosting products within the cloud computing service ecosystem. These address the shortcomings of traditional physical hosting and VPS services, such as high management difficulty and weak business scalability.
[0111] It should be understood that the various forms of processes shown above can be used, with steps reordered, added, or deleted. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution of this invention can be achieved, and this is not limited herein.
[0112] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this invention should be included within the scope of protection of this invention.
Claims
1. A program analysis method, characterized in that, include: Obtain the abstract syntax tree corresponding to the program to be analyzed; wherein, the abstract syntax tree contains multiple nodes, and the types of nodes in the abstract syntax tree include function nodes, control block nodes, statement nodes, and expression nodes; Create a value number table and a signature number mapping table corresponding to the program to be analyzed; wherein, the value number table is used to store the value number of the expression signature in the abstract syntax tree, and the signature number mapping table is used to store the mapping relationship between the expression signature and the value number in the abstract syntax tree, and the value number table and the signature number mapping table manage the currently valid value number and signature number mapping set in the value number table and the signature number mapping table in a static syntax scope manner; Traverse the abstract syntax tree. For each data shape-related expression node, determine the current stack frame where the value number table and the signature number mapping table are located according to the node type. Generate an expression signature and a value number mapped to the expression signature. Store the value number mapped to the expression signature in the current valid field of the value number table. Store the expression signature and the value number mapped to the expression signature in the current valid field of the signature number mapping table.
2. The program analysis method according to claim 1, characterized in that, Traversing the abstract syntax tree includes: The abstract syntax tree is traversed in a depth-first manner to select the nodes in the abstract syntax tree that generate and reference data shape expressions.
3. The program analysis method according to claim 1, characterized in that, Generate the expression signature, including: If a node contains a single data shape expression, an expression signature is generated according to the preset signature rules.
4. The program analysis method according to claim 3, characterized in that, Also includes: If a node is a data shape expression consisting of multiple sub-expressions, then the expression signatures of each sub-expression in the data shape expression and the value numbers mapped to the expression signatures are generated, and the expression signatures are synthesized.
5. The program analysis method according to claim 1, characterized in that, Generate value numbers that map to the expression signature, including: Query whether there exists a value number in the current valid field of the signature number mapping table that is mapped to the expression signature; If a value number mapped to the expression signature exists in the current valid field of the signature number mapping table, then the value number mapped to the expression signature is obtained and reused.
6. The program analysis method according to claim 5, characterized in that, Also includes: If there is no value number mapped to the expression signature in the current valid field of the signature number mapping table, then a value number mapped to the expression signature is generated according to the preset value numbering rules.
7. The program analysis method according to claim 1, characterized in that, Determining the current stack frame containing the value number table and the signature number mapping table based on the node type includes: When entering a scope, a new stack frame is created for the value number table and the signature number mapping table. The newly created stack frame is the current stack frame where the value number table and the signature number mapping table are located. When leaving the scope, the data in the stack top frame of the value number table and the signature number mapping table is stored in the data storage location corresponding to the scope, the stack top frame of the value number table and the signature number mapping table is deleted, and the new stack top frame is the current stack frame where the value number table and the signature number mapping table are located. Wherein, the current valid field of the value number table is the union of the value number in the current stack frame of the value number table and the value numbers in all frames from the previous frame to the bottom of the stack, and the current valid field of the signature number mapping table is the union of the mapping relationship between the expression signature and the value number in the current stack frame of the signature number mapping table and the mapping relationship between the expression signature and the value number in all frames from the previous frame to the bottom of the stack.
8. A program analysis device, characterized in that, include: The syntax tree acquisition module is used to acquire the abstract syntax tree corresponding to the program to be analyzed; wherein, the abstract syntax tree contains multiple nodes, and the types of nodes in the abstract syntax tree include function nodes, control block nodes, statement nodes, and expression nodes; The data table creation and management module is used to create a value number table and a signature number mapping table corresponding to the program to be analyzed. The value number table is used to store the value numbers of the expression signatures in the abstract syntax tree, and the signature number mapping table is used to store the mapping relationship between the expression signatures and value numbers in the abstract syntax tree. The value number table and the signature number mapping table manage the currently valid value number and signature number mapping set in the value number table and the signature number mapping table in a static syntax scope manner. The node traversal module is used to traverse the abstract syntax tree. For each data shape-related expression node, it determines the current stack frame where the value number table and the signature number mapping table are located according to the node type, generates an expression signature, generates a value number mapped to the expression signature, stores the value number mapped to the expression signature in the current valid field of the value number table, and stores the expression signature and the value number mapped to the expression signature in the current valid field of the signature number mapping table.
9. An electronic device, characterized in that, The electronic device includes: At least one processor; and a memory communicatively connected to the at least one processor; The memory stores a computer program that is executed by the at least one processor, which enables the at least one processor to perform the program analysis method according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that cause a processor to execute the program analysis method according to any one of claims 1-7.