A macro definition processing method for hardware description language and related equipment

By looping through Verilog macro definitions, the problem of cumbersome macro definition management in complex hardware system design is solved, efficient utilization of code resources and readability improvement, and the efficiency of hardware design is improved.

CN119356688BActive Publication Date: 2025-04-22北京汤谷软件技术有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411921002.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-25
Publication Date
2025-04-22
Estimated Expiration
2044-12-25

AI Technical Summary

Technical Problem

In complex hardware system design, manual management and maintenance of Verilog macro definitions becomes cumbersome and error-prone, especially when project sizes are expanded, resulting in reduced code readability and increased project size.

Method used

A macro definition processing method for hardware description language is proposed. By obtaining the target file in the header file of the specified hardware description language file, the macro name judgment, code block storage and macro value processing are performed loop-by-cycle until the code block after multiple processing no longer contains conditional compilation instructions, and finally the code block after multiple processing is extracted.

Benefits of technology

This method effectively reduces unnecessary macro definitions, optimizes the utilization efficiency of code resources, improves the readability of code, and improves code reusability and development efficiency in the hardware design process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119356688B_ABST
    Figure CN119356688B_ABST
Patent Text Reader

Abstract

The present application discloses a method for processing macro definitions of a hardware description language and related equipment, including: obtaining a target file in a header file of a specified hardware description language; judging the target macro name corresponding to each conditional compilation instruction in the target file, and if there is a macro name identical to the target macro name in a preset parameter data structure, saving the code block after the target macro name, and processing the macro value in the code block to generate a processed code block; if the processed code block contains a conditional compilation instruction, continuing to perform macro name judgment, code block storage, and macro value processing on the processed code block until the code block after multiple processing no longer contains conditional compilation instructions; extracting macro definitions from the code block after multiple processing to obtain all macro definitions therein. The technical solution of the embodiment of the present application is conducive to improving code reusability and development efficiency in the hardware design process.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and more specifically, to a method for processing macro definitions of a hardware description language and related equipment. Background Art

[0002] In today's highly complex hardware system design field, Verilog is undoubtedly a widely used hardware description language. Its ability to define macros through define instructions plays a vital role in many key aspects of hardware system design, such as modular design and parameter configuration.

[0003] However, as the scale of the project expands, manual management and maintenance of these macro definitions becomes cumbersome and error-prone. Therefore, it is particularly important to develop an automated macro definition analysis tool. For Verilog projects of a certain scale, there is a considerable amount of code in the macro definition file, which is also pre-set in order to expand more functions. Usually, it is determined whether certain modules are enabled based on preset conditions. However, for users, the functions contained in the projects they use are relatively fixed, that is, the preset conditions will not change much. When there are too many conditional judgments, it will seriously affect the readability of the code and the scale of the project. Therefore, it is necessary to appropriately subtract unnecessary macro definitions. Summary of the invention

[0004] A series of simplified concepts are introduced in the Summary of the Invention section, which will be further described in detail in the Detailed Description of the Invention section. The Summary of the Invention section of this application does not mean to attempt to define the key features and essential technical features of the claimed technical solution, nor does it mean to attempt to determine the scope of protection of the claimed technical solution.

[0005] In a first aspect, the present application proposes a macro definition processing method of a hardware description language, comprising:

[0006] Get the target file in the header file of the specified hardware description language;

[0007] The target macro name corresponding to each conditional compilation instruction in the target file is judged, and if there is a macro name identical to the target macro name in the preset parameter data structure, the code block after the target macro name is saved, and the macro value in the code block is processed to generate a processed code block;

[0008] If the processed code block contains a conditional compilation instruction, continue to perform macro name determination, code block storage, and macro value processing on the processed code block until the code block after multiple processing no longer contains the conditional compilation instruction;

[0009] The macro definition is extracted from the code blocks after the multiple processing to obtain all macro definitions therein.

[0010] In a feasible implementation, the method further includes: reading the target file to obtain all relevant macro definitions, wherein the relevant macro definitions are macro definitions other than the macro definitions in all conditional compilation instructions of the target file; extracting each of the relevant macro definitions obtained to obtain the macro name and macro value in each of the relevant macro definitions; saving each of the relevant macro definitions obtained to the preset parameter data structure, wherein the macro name in the relevant macro definition serves as the key of the preset parameter data structure, and the macro value in the relevant macro definition serves as the value of the preset parameter data structure.

[0011] In a feasible implementation manner, the macro value in the code block is processed to obtain a processed code block, including: if the macro value in the code block contains a macro name, the macro value containing the macro name is processed to obtain a processed macro value, and the processed macro value does not contain the macro name; the format of the processed macro value is adjusted to obtain an adjusted macro value, and the processed code block is generated based on the adjusted macro value.

[0012] In a feasible implementation manner, macro definition extraction is performed on the code blocks after the multiple processing to obtain all macro definitions therein, including:

[0013] According to a predefined regular expression, a matching function is used to perform matching processing on the code block after the multiple processing; based on the matching object returned by the matching function, all macro definitions in the code block after the multiple processing are determined.

[0014] In a feasible implementation, the method further includes: according to the specified grouping in the regular expression, using a grouping method to group each macro definition in the code block after the multiple processing, to obtain a grouping result of the specified grouping, wherein the specified grouping includes two groups, wherein the macro name is one group and the macro value is one group; according to the grouping result, the macro name and macro value in each of the macro definitions are obtained.

[0015] In a feasible implementation, the method further includes: using a preset data structure to store all macro definitions in the code block after the multiple processing, wherein the macro name in each of the macro definitions serves as a key of the preset data structure, and the macro value in each of the macro definitions serves as a value of the preset data structure.

[0016] In a feasible implementation, the method further includes: when storing all macro definitions in the code block after the multiple processing, if the same macro name exists in the preset data structure, the macro value corresponding to the same macro name in the preset data structure is replaced with the macro value corresponding to the same macro name.

[0017] In a feasible implementation manner, all macro definitions in the code block after the multiple processing are exported using a print statement, and the exported file is saved as a new hardware description language file.

[0018] In a feasible implementation, obtaining a target file in a header file of a specified hardware description language includes: using an open function to open the header file of the specified hardware description language; reading the header file line by line, and storing the read content containing macro definitions in a separate file to form the target file.

[0019] In a second aspect, the present application proposes a macro definition processing device for a hardware description language, comprising:

[0020] An acquisition unit configured to acquire a target file in a header file of a specified hardware description language;

[0021] A first processing unit is configured to judge a target macro name corresponding to each conditional compilation instruction in the target file, and if a macro name identical to the target macro name exists in a preset parameter data structure, save a code block after the target macro name, and process a macro value in the code block to generate a processed code block;

[0022] The second processing unit is configured to continue to perform macro name determination, code block storage, and macro value processing on the processed code block if the processed code block contains a conditional compilation instruction, until the code block after multiple processing no longer contains the conditional compilation instruction;

[0023] The extraction unit is configured to extract macro definitions from the code blocks processed multiple times to obtain all macro definitions therein.

[0024] In a third aspect, an electronic device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor is used to implement the steps of the macro definition processing method of the hardware description language of any one of the first aspects described above when executing the computer program stored in the memory.

[0025] In a fourth aspect, the present application further proposes a computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the macro definition processing method of the hardware description language of any one of the first aspects is implemented.

[0026] In summary, the macro definition processing method of the hardware description language proposed in this application obtains the target file in the header file of the specified hardware description language file, continuously loops the operations of macro name judgment, code block preservation and macro value processing, until the code block after multiple processing no longer contains conditional compilation instructions, and finally extracts macro definitions from the code block after multiple processing to obtain all macro definitions therein. In the technical scheme of the embodiment of the present application, on the one hand, the code block will be saved only if the macro name judgment meets the conditions, which means that unnecessary code blocks will be excluded, greatly optimizing the utilization efficiency of code resources. On the other hand, too many conditional compilation instructions will lead to reduced code readability, and the code block after multiple processing of this application will no longer contain conditional compilation instructions, which greatly improves the readability of the code, and the macro definitions extracted from the code block after multiple processing obviously reduce unnecessary macro definitions, which is conducive to improving the code reusability and development efficiency in the hardware design process. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present specification. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:

[0028] Figure 1 A flowchart of a macro definition processing method for a hardware description language provided in an embodiment of the present application.

[0029] Figure 2 A flowchart of a macro definition processing method for a hardware description language provided in an embodiment of the present application.

[0030] Figure 3 A schematic diagram of the structure of a macro definition processing device of a hardware description language provided in an embodiment of the present application.

[0031] Figure 4 A schematic diagram of the structure of an electronic device for macro definition processing of a hardware description language provided in an embodiment of the present application. DETAILED DESCRIPTION

[0032] The terms "first", "second", "third", "fourth", etc. (if any) in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices. The technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments.

[0033] See also Figure 1 , Figure 1 A schematic diagram of a process flow of a macro definition processing method of a hardware description language provided in an embodiment of the present application, the method comprising:

[0034] Step S110, obtaining a target file in a header file of a specified hardware description language;

[0035] Step S120, judging the target macro name corresponding to each conditional compilation instruction in the target file, if there is a macro name identical to the target macro name in the preset parameter data structure, saving the code block after the target macro name, and processing the macro value in the code block to generate a processed code block;

[0036] Step S130: If the processed code block contains a conditional compilation instruction, the operations of macro name determination, code block storage, and macro value processing are continued for the processed code block until the code block after multiple processing no longer contains the conditional compilation instruction;

[0037] Step S140: extract macro definitions from the code blocks that have been processed multiple times to obtain all macro definitions therein.

[0038] These steps are described in detail below.

[0039] In step S110, an object file in a header file of a specified hardware description language is acquired.

[0040] Among them, hardware description language (HDL) is a programming language used to describe the structure and behavior of electronic circuits (especially digital circuits). Common hardware description languages ​​include VHDL (Very High Speed ​​Integrated Circuit Hardware Description Language) and Verilog. Hardware description language files are text files written in these languages ​​that contain descriptions of hardware circuit-related design content. Their content can define the modules, ports, signals, logical functions, etc. of the circuit. They are the basic files for subsequent operations such as hardware circuit design, simulation, and synthesis. Schematically, the hardware description language of this embodiment is specifically Verilog.

[0041] The embodiment of the present application provides a macro definition processing method for a hardware description language. In the technical solution of the embodiment of the present application, it is necessary to pre-acquire the target file in the header file of the specified hardware description language file. This is the first step in the entire processing process and provides a data source for subsequent operations.

[0042] In one embodiment of the present application, to obtain a target file in a header file of a specified hardware description language file, the open function can be first used to open the header file of the specified hardware description language; then, the header file is read line by line, and the read content containing macro definitions is stored in a separate file, thereby obtaining the target file.

[0043] Taking the hardware description language Verilog as an example, we can use Python's built-in open function to open the specified Verilog header file in read-only mode ('r'), and then read the contents of the header file to get the target file.

[0044] In this embodiment, a file iterator can be used (through a for line in file: loop) to read the contents of the header file line by line to ensure that each line in the header file can be processed in sequence. Since Verilog header files are usually large, if the entire header file is put into the memory at the same time, it may cause memory overflow. Therefore, a file iterator can be used to traverse line by line to effectively avoid this problem.

[0045] It should be noted that in this embodiment, what is obtained is not an arbitrary hardware description language header file, but a file that has been clearly determined and has a specific reference. This designation may be based on the file name, the file path, the file's related attributes (such as creation time, author, project, etc.), or a specific file determined by some screening conditions.

[0046] In step S120, the target macro name corresponding to each conditional compilation instruction in the target file is judged. If there is a macro name identical to the target macro name in the preset parameter data structure, the code block after the target macro name is saved, and the macro value in the code block is processed to generate a processed code block.

[0047] Specifically, there is at least one conditional compilation instruction in the target file. The conditional compilation instruction can be specifically an ifdef instruction, which is used to check whether the specified macro is defined. Its basic syntax is: ifdef <macro definition name> <code block> endif. Its main function is to decide whether to compile a code block based on whether the specified macro definition exists.

[0048] Accordingly, in this embodiment, a detailed judgment operation is performed on the target macro names corresponding to these conditional compilation instructions. This judgment process mainly compares the target macro name corresponding to each conditional compilation instruction with the macro name in the preset parameter data structure. In Verilog, a macro name is an identifier used to represent a constant, expression or code snippet.

[0049] When the comparison is performed and it is found that there is a macro name identical to the target macro name in the preset parameter data structure, it means that the target macro name has been defined. Therefore, the code block after the target macro name corresponding to each conditional compilation instruction can be saved to a new file. Otherwise, it means that the target macro name has not been defined and the save operation will not be performed.

[0050] In one embodiment of the present application, the macro names saved in the preset parameter data structure can be obtained by reading the target file and extracting the relevant macro definitions therein. After all relevant macro definitions are extracted, each relevant macro definition can be saved in the preset parameter data structure, wherein the macro name in the relevant macro definition serves as the key of the preset parameter data structure, and the macro value in the relevant macro definition serves as the value of the preset parameter data structure.

[0051] In hardware description languages, macro definition is a preprocessing directive. It allows a symbolic name to be defined for a constant, expression, or a code snippet. The main purpose of macro definition is to improve the readability, maintainability, and reusability of the code. By using macro definition, a complex constant or a reusable code snippet can be replaced by a meaningful name, which makes it easier to read and modify the code. For example, the macro definition content of Verilog can include constant definition and code snippet definition.

[0052] The following is a detailed explanation of the operation of extracting relevant macro definitions from the target file.

[0053] Specifically, after obtaining the target file, the target file can be read to obtain relevant macro definitions, which are macro definitions other than those in all conditional compilation instructions of the target file; each relevant macro definition obtained is extracted to obtain the macro name and macro value in each relevant macro definition. Among them, the method of obtaining all relevant macro definitions from the target file can be to use Python's re.match function and predefined regular expressions to try to match the define instruction in the target file. The re.match function is an important function in the re (regular expression) module of the Python standard library. It is used to try to match a regular expression pattern at the beginning of a string. If the match is successful, it returns a match object, which contains various information about the matching part; if the match fails, it returns None. The define instruction is usually in the form of: #define <macro name> <macro value>, where <macro name> is the name of the macro to be defined, which usually follows certain naming rules (such as consisting of letters, numbers and underscores, and the first letter cannot be a number, etc.), and <macro value> can be a constant, expression, code snippet, etc. Based on this grammatical form, a predefined regular expression can be designed to match the define instruction. Then the predefined regular expression and re.match function can be used to try to match each line in the target file one by one (assuming that the define instruction is one per line, the actual situation may be more complicated and the entire target file may need to be traversed). If the match is successful, the relevant macro definition can be captured.

[0054] After all relevant macro definitions are extracted according to the above method, each relevant macro definition can be grouped according to the specified group in the regular expression using the grouping method to obtain the grouping result of the specified group. The specified group includes two groups, where the macro name is one group and the macro value is one group; then, based on the grouping result, the macro name and macro value in each relevant macro definition can be obtained.

[0055] After the above macro name judgment operation, if the code block is saved in a new file, the macro value in the code block can be further processed to generate a processed code block. The purpose of this is to ensure the accuracy of subsequent processing and analysis of the code block. The final form of the macro value will directly affect the actual function of the code. By performing necessary processing on the macro value, it is ensured that the definition used by the code is clear and will not cause ambiguity during the actual compilation and synthesis process, thereby improving the accuracy of the code.

[0056] In one embodiment of the present application, processing the macro value in the code block to obtain the processed code block may specifically involve: if the macro value in the code block contains a macro name, then processing the macro value containing the macro name to obtain the processed macro value, and the processed macro value does not contain the macro name; further, formatting the processed macro value to obtain the adjusted macro value, and generating the processed code block based on the adjusted macro value. The formatting adjustment here includes removing the leading and trailing spaces of the macro value by using a string removal method (for example, Python's string strip method). The reason for formatting adjustment here is that unnecessary spaces before and after the macro value may cause problems when the macro definition is used later. Of course, in addition to removing leading and trailing spaces of the macro value, it is also possible to process the grammatical errors of the macro value, which may cause errors when the macro is expanded. Among them, the grammatical errors of the macro value may be arithmetic expression errors, logical expression errors, or code fragment grammatical errors.

[0057] The processing of the macro value containing the macro name before the format adjustment includes: if the macro value contains other macro names, the other macro names in the macro value will be replaced with the corresponding macro values. If the macro value is nested, a cyclic replacement method will be used to ensure that the macro value does not contain the macro name.

[0058] For example, suppose there are the following macro definitions: define WIDTH 8; define ADDRESS_RANGE (WIDTH-1):0. Here, the macro value ADDRESS_RANGE contains another macro name WIDTH. According to the rules, WIDTH in ADDRESS_RANGE needs to be replaced with the corresponding macro value 8. The result after replacement is (8 - 1):0, that is, 7:0. In this way, one layer of macro name replacement is completed, making the macro value clearer. For the nested case, suppose there are the following macro definitions: define A 2; define B (A + 1); define C (B * 2). For the macro value (B * 2) of macro C, it contains the macro name B. First, replace B with its corresponding macro value (A + 1), and the macro value of C becomes ((A + 1) * 2). However, this new macro value still contains the macro name A, so it needs to be replaced again. Replace A with its corresponding macro value 2, and get ((2 +1) * 2), and the calculation result is 6. This cyclic replacement method ensures that the final macro value of macro C no longer contains the macro name, making the macro value an expression that can be used directly.

[0059] The main purpose of processing macro values ​​in code blocks is to convert all macro names that may be indirectly referenced into direct values ​​or expressions in the preprocessing stage, so that the code can be processed in the most explicit way during subsequent compilation, synthesis or execution. This ensures the certainty and consistency of the code, improves the maintainability and readability of the code, and also facilitates the compiler and other tools to accurately process the code. If the macro name is still retained in the macro value, the actual meaning of the code may change in different compilation environments or after the macro definition is modified, or the compiler may not accurately understand the intent of the code.

[0060] In step S130, if the processed code block contains conditional compilation instructions, the operations of macro name determination, code block storage and macro value processing are continued on the processed code block until the code block after multiple processing no longer contains conditional compilation instructions.

[0061] After obtaining the processed code block, if the processed code block still includes conditional compilation instructions, the operations of macro name determination, code block storage, and macro value processing can be continued for the processed code block until the code block after multiple processing no longer includes conditional compilation instructions. It should be noted that the operations of macro name determination, code block storage, and macro value processing here are the same as those in the above step S120, and will not be elaborated here.

[0062] In this step, a loop processing method is adopted to solve the nesting problem of conditional compilation instructions, ensuring that the code block obtained after multiple processing no longer contains conditional compilation instructions. In the hardware description language, conditional compilation instructions can have outer and inner layers, and each macro name judgment is only for one layer. Therefore, if there are multiple layers of nesting, it is necessary to loop the macro name judgment, code block storage and macro value processing operations.

[0063] In step S140, macro definition extraction is performed on the code block after multiple processing to obtain all macro definitions therein.

[0064] After obtaining the code block after multiple processing, macro definition extraction can be further performed to obtain all macro definitions therein.

[0065] The macro definition processing method of the hardware description language proposed in the present application, by obtaining the target file in the header file of the specified hardware description language file, continuously looping the operation of macro name judgment, code block preservation and macro value processing, until the code block after multiple processing no longer contains conditional compilation instructions, and finally extracting macro definitions from the code block after multiple processing to obtain all macro definitions therein. In the technical scheme of the embodiment of the present application, on the one hand, the code block will be saved only if the macro name judgment meets the conditions, which means that unnecessary code blocks will be excluded, greatly optimizing the utilization efficiency of code resources. On the other hand, too many conditional compilation instructions will lead to reduced code readability, and the code block after multiple processing of the present application will no longer contain conditional compilation instructions, greatly improving the readability of the code, and the macro definition in the extracted code block after multiple processing obviously reduces unnecessary macro definitions, which is conducive to improving the code reusability and development efficiency in the hardware design process. The method of the embodiment of the present application has ease of use, high efficiency and scalability, can meet the needs of large-scale hardware design projects, and is of great significance for promoting technological progress in the field of embedded system design and development.

[0066] In one embodiment of the present application, the method of extracting macro definitions from the code block after multiple processing can be a line-by-line scanning method. Specifically, the macro definition in Verilog usually starts with the "define" keyword. By reading the code block after multiple processing line by line, when encountering a line starting with the keyword, the information related to the macro definition can be extracted. For each line, the starts with method of the string can be used to check whether it starts with "define". If so, the extraction operation is performed.

[0067] In one embodiment of the present application, the method of extracting macro definitions from the code block after multiple processing can also be to use a syntax parsing tool (such as Antlr), which is a tool that can generate a syntax parser based on syntax rules. For Verilog, Antlr can be used to generate a Verilog syntax parser. By defining the syntax rules of Verilog (including the rules of the macro definition part), and then using Antlr to generate parser code according to the defined syntax rules, the generated parser is further used to parse the Verilog file. During the parsing process, when encountering the syntax structure corresponding to the macro definition, the parser will build the corresponding AST node. By traversing these AST nodes, the content of the macro definition can be obtained.

[0068] In one embodiment of the present application, in addition to the method mentioned above for extracting all macro definitions from a code block after multiple processing, the extraction method may also be to use Python with regular expressions, such as Figure 2As shown, step S140 may specifically include steps S210 to S220, which are described in detail as follows:

[0069] Step S210: According to a predefined regular expression, a matching function is used to perform matching processing on the code block after multiple processing.

[0070] Before extracting all macro definitions, a key step is to predefine a suitable regular expression. Regular expressions are a powerful tool for describing text patterns, which can accurately define various text formats, character combinations, etc. that we expect to match.

[0071] For example, to extract macro definitions from a block of code that has been processed multiple times, you can write a regular expression pattern that matches Verilog macro definitions, such as r"define\s+(\w+)\s+(.*)". In programming languages ​​such as Python, the r prefix indicates a raw string. define is a string literal that matches the exact "`define" character sequence in the text, which is the starting mark of a macro definition in hardware description languages. \s is a metacharacter that represents whitespace characters, including space, tab (\t), newline (\n), etc. \w is a metacharacter that represents word characters, including letters (uppercase and lowercase), numbers, and underscores. Brackets () are used for grouping and also for capturing matched content. So (\w+) is used to capture the macro name in the macro definition, because the macro name is usually a sequence of one or more characters consisting of letters, numbers, and underscores. .* is a common combination. Among them, . is a metacharacter that represents any character except newline, and * is a quantifier that represents the preceding character (in this case, any character except newline) appears zero or more times. Brackets () are used to capture the matched content. So (.*) is used to capture the macro value part in the macro definition, because the macro value can be a combination of various characters.

[0072] After completing the pre-definition of the regular expression, you can then use the re.match function to match the code block after multiple processing. It will try to match the pre-defined regular expression with the given string (here is the code block after multiple processing). If the match is successful, it returns a match object, which contains various information about the matching result, such as the starting position of the match, the content of the matched string, etc.; if the match fails, it returns None.

[0073] Step S220: Based on the matching object returned by the matching function, all macro definitions in the code block after multiple processing are determined.

[0074] If the matching function returns a matching object, it means that a macro definition is extracted. Therefore, based on the matching object returned by the matching function, all macro definitions in the code block after multiple processing can be determined.

[0075] In some embodiments, for complex macro value situations, such as when the macro value part contains special characters (such as quotes, brackets, etc.) or newline characters, the above extraction method is still valid as long as the regular expression correctly defines the matching pattern. However, if the format of the macro value does not meet expectations (for example, the actual macro value format is more complex than that defined by the regular expression), it may be necessary to adjust the regular expression or use other methods to accurately extract the macro value. For example, if the macro value may contain newline characters and you want to extract it completely, you may need to modify the regular expression r"define\s+(\w+)\s+(.*)" to r"define\s+(\w+)\s+((?:.|\n))". In Python, this expression uses (?:.|\n) to match any character including newline characters.

[0076] In one embodiment of the present application, after extracting macro definitions using Python in conjunction with regular expressions, each macro definition can be further extracted to obtain the macro name and macro value in each macro definition. Specifically, first, each macro definition can be grouped according to the specified grouping in the regular expression using a grouping method to obtain a grouping result of the specified grouping, where the specified grouping includes two groups, wherein the macro name is one group and the macro value is one group; then, based on the grouping result, the macro name and macro value in each macro definition can be obtained.

[0077] Specified groups are defined by using parentheses () in regular expressions. In Python's re module (for regular expressions), the group method is used to extract the contents of a specific group from a matching object. When the regular expression matches successfully, the contents within each parenthesis constitute a group.

[0078] In this embodiment, the matching object is each macro definition, and the specified group in the regular expression includes two groups, wherein the macro name is one group and the macro value is one group. Therefore, when the regular expression is matched successfully, that is, the macro definition is extracted, the macro name and the macro value can be separated from the macro definition by using the grouping method.

[0079] Let's continue to use the regular expression r"define\s+(\w+)\s+(.*)" as an example. After using this regular expression to match the macro definition, you can use the group method to extract the macro name and macro value.

[0080] If a match is found via re.match, then match.group(1) can be used to extract the macro name, and match.group(2) can be used to extract the macro value.

[0081] In this embodiment, regular expressions provide an accurate way to describe the pattern of macro definitions. In hardware description languages ​​(such as Verilog or VHDL), macro definitions usually have a specific format, such as starting with "define" in Verilog. By writing a regular expression such as r"define\s+(\w+)\s+(.*)", all parts in the file that conform to the format of the beginning of the macro definition can be accurately located, avoiding the tedious process of manual line-by-line inspection and judgment, and greatly improving the efficiency of extracting macro definitions. Regular expressions can not only be used to locate macro definitions, but also to extract key information in macro definitions, such as macro names and macro values, through capture groups. For example, for the regular expression r"define\s+(\w+)\s+(.*)" mentioned above, (\w+) captures the macro name, and (.*) captures the macro value. In this way, the extracted macro name and macro value can be conveniently stored in a data structure (such as a dictionary) for subsequent analysis, replacement or other operations.

[0082] In one embodiment of the present application, after all macro definitions in the code block after multiple processing are obtained in step S140, all macro definitions in the code block after multiple processing can be stored using a preset data structure, wherein the macro name in each macro definition serves as a key of the preset data structure, and the macro value in each macro definition serves as a value of the preset data structure. The preset data structure here can be a dictionary. A dictionary is a common and very useful data structure, which has a unique key-value pair storage feature.

[0083] As an important identification part of the macro definition, the macro name has the characteristics of uniqueness and certainty. It can clearly represent a specific macro definition in the entire code system or related processing flow. By setting the macro name as the key of the preset data structure, the uniqueness of the key can be used to ensure that each macro definition has its own independent and clear storage location in the preset data structure, which is convenient for quickly locating the corresponding macro definition content according to the macro name. The macro value is the value of the preset data structure. The macro value is stored in association with the corresponding macro name (key) as the value of the preset data structure, forming a complete key-value pair storage mode.

[0084] For example, suppose the name of a new macro definition is "MAX_COUNT", and after processing its original macro value, the processed macro value is 100. Then when we use the preset data structure for storage, "MAX_COUNT" will be used as the key of the preset data structure, and 100 will be used as the value of the preset data structure corresponding to the key.

[0085] In this way, when you need to access a specific macro definition in the subsequent program development process, you only need to specify its macro name (that is, key) to quickly obtain the corresponding macro value (that is, value) from the preset data structure, thereby realizing a convenient and efficient storage and access mechanism for macro definitions.

[0086] This storage method of key-value pairs based on preset data structures can not only effectively organize and manage macro definitions, but also greatly improve the maintainability and overall operating efficiency of the code, making it clearer and more orderly when processing operations related to macro definitions.

[0087] In one embodiment of the present application, when a preset data structure is used to store macro definitions, this storage mechanism has the ability to handle duplicate macro names. When trying to store a macro definition in a preset data structure, it will first check whether the same macro name already exists in the preset data structure. If the same macro name does exist in the preset data structure, a specific processing logic will be triggered, that is, the macro value corresponding to the same macro name in the macro definition will be directly replaced with the existing macro value of the same macro name in the preset data structure.

[0088] This replacement mechanism has important significance and role. It ensures that in the entire macro definition storage system, for each macro name, only the latest and valid macro value information is always retained. For example, in a more complex code compilation preprocessing process, multiple macro definitions with the same name may be generated in different code modules or different code execution stages. However, due to changes in actual business logic or code optimization requirements, the macro values ​​of the subsequently generated macro definitions with the same name are more accurate or more in line with the current execution environment. Through such a replacement operation, it can be ensured that in any subsequent reference and use of the macro name, the latest, corrected and optimized macro values ​​are obtained, thereby avoiding potential errors and logical confusion caused by inconsistent or outdated macro values.

[0089] From the perspective of data management and code running efficiency, this processing method also helps to maintain the simplicity and consistency of the dictionary data structure. Because there is no need to make additional special marks for repeated macro names or store multiple redundant data, it is only necessary to overwrite the old macro value with the latest macro value, so that the storage of macro definitions in the preset data structure always maintains a relatively "pure" and "valid" state. In large-scale code projects, especially when involving many macro definitions, this can reduce the complexity of data search, comparison and processing, improve the overall running efficiency of the code in the macro definition processing link, and enable the entire code system to run more smoothly and stably when processing macro-related operations, reducing various difficult-to-troubleshoot problems caused by chaotic macro definition storage.

[0090] In one embodiment of the present application, the macro definitions stored in the preset data structure can also be effectively exported so that they can be conveniently used in the subsequent hardware design process. Specifically, the print statement can be fully utilized to complete the export of the macro definition. In different programming languages ​​or development environments, the specific form and syntax of the print statement may be different, but their core function is to output and display the specified information.

[0091] For the export of macro definitions, it is necessary to carefully organize the content of the print statement according to the specific data structure used to store the macro definition and the format requirements of the hardware description language. Assume that a dictionary is used to store macro definitions, where the key of the dictionary is the macro name and the value is the corresponding macro value. Then when printing and exporting, it is necessary to traverse this dictionary, and for each key-value pair (that is, each macro definition), the macro name and macro value are accurately output through the print statement according to the macro definition format specified by the hardware description language.

[0092] Furthermore, after exporting the macro definitions, the exported file can be saved as a new hardware description language file to ensure that these macro definitions can be applied in subsequent hardware design work. Specifically, the above printout content can be redirected to a new file, thereby creating a new hardware description language file containing the macro definitions.

[0093] In the second aspect, the present application also proposes a macro definition processing device for a hardware description language, such as Figure 3 As shown, it includes an acquisition unit 301, a first processing unit 302, a second processing unit 303 and an extraction unit 304.

[0094] Among them, the acquisition unit 301 is configured to acquire the target file in the header file of the specified hardware description language; the first processing unit 302 is configured to judge the target macro name corresponding to each conditional compilation instruction in the target file, and if there is a macro name identical to the target macro name in the preset parameter data structure, the code block after the target macro name is saved, and the macro value in the code block is processed to generate a processed code block; the second processing unit 303 is configured to continue to perform macro name judgment, code block storage and macro value processing on the processed code block if the processed code block contains conditional compilation instructions, until the code block after multiple processing no longer contains conditional compilation instructions; the extraction unit 304 is configured to perform macro definition extraction on the code block after the multiple processing to obtain all macro definitions therein.

[0095] In some embodiments, the first processing unit 302 is further configured to read the target file to obtain all relevant macro definitions, where the relevant macro definitions are macro definitions other than the macro definitions in all conditional compilation instructions of the target file; extract each of the relevant macro definitions obtained to obtain the macro name and macro value in each of the relevant macro definitions; and save each of the relevant macro definitions obtained to the preset parameter data structure, wherein the macro name in the relevant macro definition serves as the key of the preset parameter data structure, and the macro value in the relevant macro definition serves as the value of the preset parameter data structure.

[0096] In some embodiments, the first processing unit 302 is further configured to process the macro value containing the macro name to obtain a processed macro value if the macro value in the code block contains a macro name, and the processed macro value does not contain the macro name; perform format adjustment on the processed macro value to obtain an adjusted macro value, and generate the processed code block based on the adjusted macro value.

[0097] In some embodiments, the extraction unit 304 is further configured to perform matching processing on the multiply processed code block using a matching function according to a predefined regular expression; and determine all macro definitions in the multiply processed code block based on the matching object returned by the matching function.

[0098] In some embodiments, the extraction unit 304 is also configured to group each macro definition in the code block after the multiple processing using a grouping method according to the specified grouping in the regular expression, and obtain the grouping result of the specified grouping, wherein the specified grouping includes two groups, wherein the macro name is one group and the macro value is one group; based on the grouping result, the macro name and macro value in each of the macro definitions are obtained.

[0099] In some embodiments, the macro definition processing device of the hardware description language also includes a storage unit, configured to store all macro definitions in the code block after the multiple processing using a preset data structure, wherein the macro name in each of the macro definitions serves as a key of the preset data structure, and the macro value in each of the macro definitions serves as the value of the preset data structure.

[0100] In some embodiments, the macro definition processing device of the hardware description language also includes a replacement unit, which is configured to replace the macro value corresponding to the same macro name in the preset data structure with the macro value corresponding to the same macro name when storing all macro definitions in the code block after the multiple processing.

[0101] In some embodiments, the hardware description language macro definition processing device further includes an export unit configured to export all macro definitions in the code blocks processed multiple times using print statements, and save the exported file as a new hardware description language file.

[0102] In some embodiments, the acquisition unit 301 is further configured to use an open function to open a header file of the specified hardware description language; read the header file line by line, and store the read content containing macro definitions into a separate file to form the target file.

[0103] like Figure 4 As shown, an embodiment of the present application also provides an electronic device 400, including a memory 410, a processor 420, and a computer program 411 stored in the memory 410 and executable on the processor, and when the processor 420 executes the computer program 411, the steps of any of the above-mentioned multi-module aggregation methods are implemented.

[0104] Since the electronic device introduced in this embodiment is a device used to implement a multi-module aggregation device in the embodiment of the present application, based on the method introduced in the embodiment of the present application, technical personnel in this field can understand the specific implementation mode of the electronic device of this embodiment and its various variations. Therefore, how the electronic device implements the method in the embodiment of the present application is not introduced in detail here. As long as the equipment used by technical personnel in this field to implement the method in the embodiment of the present application is within the scope of protection of this application.

[0105] In the specific implementation process, when the computer program 411 is executed by the processor, it can achieve Figure 1 Any implementation manner in the corresponding embodiments.

[0106] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0107] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.

[0108] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0109] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0110] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0111] An embodiment of the present application also provides a computer program product, which includes computer software instructions. When the computer software instructions are executed on a processing device, the processing device executes the multi-module aggregation process in the corresponding embodiment.

[0112] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on the computer, the process or function according to the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from one website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid state disk (SSD)), etc.

[0113] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0114] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.

[0115] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0116] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0117] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program codes.

[0118] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.

Claims

1. A method for processing macro definitions of a hardware description language, characterized in that: include: Get the target file in the header file of the specified hardware description language; The target macro name corresponding to each conditional compilation instruction in the target file is judged, and if there is a macro name identical to the target macro name in the preset parameter data structure, the code block after the target macro name is saved, wherein the preset parameter data structure stores all relevant macro definitions in the target file, and the relevant macro definitions are macro definitions other than the macro definitions in all conditional compilation instructions of the target file; Processing the macro value in the code block to generate a processed code block, and if the processed code block contains a conditional compilation instruction, continuing to perform macro name determination, code block storage, and macro value processing operations on the processed code block until the code block after multiple processing no longer contains the conditional compilation instruction; The macro definition is extracted from the code blocks after the multiple processing to obtain all macro definitions therein.

2. The method according to claim 1, characterized in that The method further comprises: Each of the obtained related macro definitions is extracted to obtain a macro name and a macro value in each of the related macro definitions, wherein the macro name in the related macro definition is used as a key of the preset parameter data structure, and the macro value in the related macro definition is used as a value of the preset parameter data structure.

3. The method according to claim 1, characterized in that The macro value in the code block is processed to obtain a processed code block, including: If the macro value in the code block includes a macro name, the macro value including the macro name is processed to obtain a processed macro value, wherein the processed macro value does not include the macro name; The processed macro value is formatted to obtain an adjusted macro value, and the processed code block is generated based on the adjusted macro value.

4. The method according to claim 1, characterized in that: The macro definition extraction is performed on the code block after the multiple processing to obtain all macro definitions therein, including: According to a predefined regular expression, using a matching function to match the code blocks after the multiple processing; Based on the matching object returned by the matching function, all macro definitions in the code block after the multiple processing are determined.

5. The method according to claim 4, characterized in that The method further comprises: According to the specified grouping in the regular expression, each macro definition in the code block after the multiple processing is grouped by using a grouping method to obtain a grouping result of the specified grouping, wherein the specified grouping includes two groups, wherein the macro name is one group and the macro value is one group; According to the grouping result, a macro name and a macro value in each macro definition are obtained.

6. The method according to claim 5, characterized in that The method further comprises: A preset data structure is used to store all macro definitions in the code block after the multiple processing, wherein the macro name in each macro definition serves as a key of the preset data structure, and the macro value in each macro definition serves as a value of the preset data structure.

7. The method according to claim 6, characterized in that The method further comprises: When storing all macro definitions in the code blocks after the multiple processing, if the same macro name exists in the preset data structure, the macro value corresponding to the same macro name in the preset data structure is replaced by the macro value corresponding to the same macro name.

8. The method according to claim 6, characterized in that The method further comprises: All macro definitions in the code block after the multiple processing are exported using print statements, and the exported file is saved as a new hardware description language file.

9. The method according to claim 6, characterized in that Get the target file in the header file of the specified hardware description language, including: Using an open function, open the header file of the specified hardware description language; The header file is read line by line, and the read content including macro definitions is stored in a separate file to form the target file.

10. A macro definition processing device for a hardware description language, characterized in that: include: An acquisition unit configured to acquire a target file in a header file of a specified hardware description language; A first processing unit is configured to judge a target macro name corresponding to each conditional compilation instruction in the target file, and save a code block after the target macro name if a macro name identical to the target macro name exists in a preset parameter data structure, wherein the preset parameter data structure stores all relevant macro definitions in the target file, and the relevant macro definitions are macro definitions other than macro definitions in all conditional compilation instructions in the target file; A second processing unit is configured to process the macro value in the code block to generate a processed code block. If the processed code block contains a conditional compilation instruction, the processed code block continues to be subjected to macro name determination, code block storage, and macro value processing operations until the code block after multiple processing no longer contains the conditional compilation instruction; The extraction unit is configured to extract macro definitions from the code blocks processed multiple times to obtain all macro definitions therein.

11. An electronic device, comprising: A memory and a processor, wherein the processor is used to implement the macro definition processing method of the hardware description language according to any one of claims 1 to 9 when executing a computer program stored in the memory.

12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the macro definition processing method of the hardware description language according to any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Semantic analysis method and device for hardware description codes

    CN117406996A