Synchronous data stream language translation method containing initialization function
By generating initialization functions in the synchronous data stream language translation method, the possible wild pointer problem before the initialization of the context structure in model-based code generation is solved, and the reliability of code generation and the ease of model simulation are improved.
Patent Information
- Application Number
- CN202510237269.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-02
- Publication Date
- 2025-05-30
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In model-based code generation, the context structure created for each node in the synchronous data flow language translation framework may have wild pointer problems before initialization, affecting the development of simulation work.
A synchronous data stream language translation method containing initialization functions is proposed. By calling the synchronous data stream language translation tool to generate a context structure and generate an initialization function for it, ensuring that the context structure is correctly initialized before simulation.
By generating initialization functions, pointer reference errors are avoided, code generation reliability is improved, model simulation process is simplified, and simulation needs are met.
Smart Images

Figure CN120066524A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of code generation, and particularly relates to a method for translating a synchronous data flow language including an initialization function. Background Art
[0002] Model-based code generation is an advanced programming technology that can automatically generate code through a model. The core advantage of such technology lies in its ability to effectively improve development efficiency and reduce human programming errors.
[0003] In the actual software development process, it is not only necessary to generate code that can correspond to the model and can be correctly compiled and run according to the model, but also to generate code that can meet the simulation requirements. In the current model-based software development, in the current synchronous data flow language translation framework, a context structure including output variables is created for each node (function). When a node is called, the return value is assigned to the pointer of the input context structure. To facilitate the development of model simulation work, it is necessary to initialize the context structure of the function before simulation to avoid the occurrence of referencing wild pointers. Summary of the Invention
[0004] (1) Technical Problem to be Solved
[0005] The technical problem to be solved by the present invention is how to provide a method for translating a synchronous data flow language including an initialization function to solve the problem of wild pointers in model-based code generation.
[0006] (2) Technical Solution
[0007] To solve the above technical problem, the present invention proposes a method for translating a synchronous data flow language including an initialization function, and the method includes the following steps:
[0008] S1. Call a synchronous data flow language translation tool to generate a context structure
[0009] Call a synchronous data flow language translation tool to translate a Lustre program into a LustreF abstract syntax tree, and call OutStructGen to generate a context structure for the nodes in the program;
[0010] S2. Generate an initialization function for the context structure
[0011] Traverse the nodes in LustreF and generate corresponding initialization functions according to their context structures;
[0012] S3. Complete the translation process
[0013] Add the initialization function definition to the list of LustreF program function definitions, call the L2C partial translation step, translate the program into C language, and make partial adjustments to the C language output function so that it can output the initialization function information to the corresponding.c and.h files in the correct order.
[0014] (III) Beneficial effects
[0015] The present invention proposes a method for translating a synchronous data flow language including an initialization function, and a method for generating an initialization function for a context structure in the translation of a synchronous data flow language. This method has at least one or a part of the following beneficial effects:
[0016] (1) Solve the construction of the initialization function at the code generation level, facilitate the writing of loop functions, and provide more choices for users who want to customize loop functions.
[0017] (2) Create an initialization function for the context structure in the generated code. The loop function can directly call the initialization function in the entry function to complete the initialization work of the context structure, thus avoiding possible pointer reference errors.
[0018] (3) Before performing model simulation, it is necessary to initialize the context structure. The initialization function for the context structure can make the simulation work easier to carry out. Description of the drawings
[0019] Figure 1 is a four-layer syntax tree structure;
[0020] Figure 2 is the complete translation process;
[0021] Figure 3 is the process of initializing variables;
[0022] Figure 4 is the graphical model of the embodiment in the OnModel modeling tool. Detailed implementation manners
[0023] To make the objectives, contents, and advantages of the present invention clearer, the following further describes in detail the specific implementation manners of the present invention with reference to the drawings and embodiments.
[0024] The technical problem to be solved by the present invention is how to implement a method for generating an initialization function for a node context structure during the translation of a synchronous data flow language, so that the generated code includes the context for nodes. To solve the above problem, this paper proposes a method for translating a synchronous data flow language supporting model simulation. Before elaborating on this method, it is necessary to introduce the concepts and proper nouns appearing in the following text:
[0025] The main work of the present invention is carried out in Coq. Coq is one of the currently popular interactive theorem proving tools and is widely used in mathematical theorem proving and formal verification of computer programs. Compared with other interactive theorem proving tools, the terms and functions written in Coq can be extracted into the Ocaml language, making it possible to directly develop formally verified programs.
[0026] The present invention is based on a trusted translation tool for synchronous data flow languages developed using Coq: L2C. The main work of the present invention is carried out at the level of the LustreF abstract syntax tree, which is the intermediate language in the L2C translation process. LustreF hereinafter refers to the LustreF abstract syntax tree. The LustreF abstract syntax tree is formally defined in Coq and is divided into four layers from top to bottom: program layer, node layer, statement layer, and expression layer, as Figure 1 shown.
[0027] Among them, the nodes in the node layer include two types: function and node. A function can be regarded as a simplified node, which does not contain temporal operators and node history clock information.
[0028] To implement the feature that a node in the Lustre language can have multiple return values, in the C code generated by translating the Lustre language, a context structure will be defined for each node. This structure is passed as a pointer parameter to the function, and it includes all the return variables of the corresponding node in the Lustre source code. In the generated C code, the corresponding return variables participate in operations and returns in the form of a context structure pointer. The context structure of a node contains all the return variables of the function, and at the same time, it also contains elements of the context structure type of the functions called by this function. This element is passed as a pointer parameter to the called function in the function call statement.
[0029] The following is an example of the context structure of the function Operator1 in the generated code:
[0030]
[0031] This context structure indicates that this function has a return variable named Output1 of type acg_char and calls a function named Operator2.
[0032] Now, specifically introduce the synchronous data flow language translation method involving an initialization function of the present invention. This method includes the following steps:
[0033] S1. Call the synchronous data flow language translation tool to generate a context structure
[0034] Call the synchronous data flow language translation tool to translate the Lustre program into LustreF, and call OutStructGen to generate context structures for the nodes in the program.
[0035] S2. Generate an initialization function for the context structure.
[0036] Traverse the nodes in LustreF and generate corresponding initialization functions according to their context structures.
[0037] S3. Complete the translation process.
[0038] Add the definition of the initialization function to the function definition list of the LustreF program. Call part of the translation steps of L2C to translate the program into C language. Make partial adjustments to the C language output function so that it can output the initialization function information to the corresponding.c and.h files in the correct order.
[0039] Specifically, S2 includes the following steps:
[0040] S21. Generate an initialization function structure, which has an input variable of the pointer type corresponding to the function context structure.
[0041] S22. Create assignment statements for the elements of the context structure inside the initialization function. For the built-in type variables in the abstract syntax tree, assign values according to the default values.
[0042] For the enumeration type elements existing in the global environment, take the first enumeration class as the default value.
[0043] For the array type elements, recursively assign each bit to the default value of this data type.
[0044] For the structure type elements (not other context structures) existing in the global environment: Recursively traverse its structure until all elements in it are assigned default values according to the rules mentioned above or this rule.
[0045] S23. For the elements of the context structure type of other functions existing in the context structure, call the corresponding context structure initialization function to initialize them.
[0046] Embodiment 1:
[0047] To make the purpose, content and advantages of the present invention clearer, the following further describes in detail the specific implementation manners of the present invention with reference to the drawings and embodiments.
[0048] The present invention relates to a method for translating a synchronous data flow language including an initialization function, belonging to the field of code generation, and particularly relates to a method for generating an initialization function of a node context structure during the translation of a synchronous data flow language. By calling the synchronous data flow language translation tool L2C to generate a corresponding context structure for each node, a method for generating an initialization function for the context structure is designed to ensure that each return value and the relevant context structure are correctly initialized during the simulation process, avoiding the problem of unassigned pointer references. Finally, the generated initialization function is added to the translated C language code to support model simulation. Through this method, the reliability of the generated code can be improved, effectively meeting the requirements of model simulation.
[0049] S1: Modify the function output to return local variables of the function:
[0050] Call the synchronous data flow language translation tool L2C to translate the Lustre program to LustreF, and call the OutStructGen function to generate context structures for the nodes in the program
[0051] S2: Generate an initialization function for the context structure
[0052] To meet the initialization requirements of the context structure, the structure initialization function designed by this method has the following characteristics:
[0053] The context structure initialization function accepts a parameter of the structure pointer type and assigns default values to all elements in the structure.
[0054] For the simple data type elements existing in L2C, assign values to them according to Table 1:
[0055] Table 1
[0056] Data type Default value Tint__ CintInt.zero Tlong_ ClongInt64.zero TfloatF32 CsingleFloats.Float32.zero TfloatF64 CfloatFloats.Float.zero
[0057] For the enumeration type elements existing in the global environment, take the first enumeration class as the default value.
[0058] For array type elements, recursively assign each bit to the default value of the data type.
[0059] For the structure type elements existing in the global environment: Recursively structure it until all elements in it are assigned default values according to the rules mentioned above or this rule.
[0060] For the context structure elements of the called function: Call the initialization function of the called function to initialize them.
[0061] According to the above rules, write an initialization function creation function and add it to the translation process to generate an initialization function for each context structure.
[0062] S3: Complete the translation process
[0063] Add the generated initialization function to the function list. Call the remaining steps of L2C to translate the program from LustreF to C language. Adjust the C program printing function to ensure that it can correctly output the initialization function and its call information to the.c and.h files. The complete translation process is as Figure 2 shown.
[0064] Example 2:
[0065] The following introduces the specific process of this method with examples:
[0066] Table 2 shows an excerpt of the Lustre source code used in the test:
[0067] Table 2 Lustre source code
[0068]
[0069] First, translate the program to LustreF through step S1 and add the context structure. In this example, it is implemented by calling the translation function of L2C up to OutStructGen.
[0070] After that, according to the method described in S2 above, write the corresponding Coq function to add the initialization function to the program. In this example, the init_func_gen function is written to implement this function. This function takes a structure in the abstract syntax tree as input, traverses the constituent elements of the structure, and writes assignment statements for each element. The assignment rules for various elements are as described in steps S21, S22, and S23. Functions type_default_stmt and init_func_call_gen are written to implement S22 and S23 respectively.
[0071] An excerpt of the LustreF syntax tree obtained after translation through steps S1 and S2 is shown in Table 3:
[0072] Table 3 LustreF source code
[0073]
[0074]
[0075] It can be seen that after translation through steps S1 and S2, the program already has the initialization function of the context structure. Next, it only needs to be translated into a C language program through step S3.
[0076] In S3, to ensure that the initialization function is correctly output, an initialization function output function needs to be added. In this embodiment, it is implemented using the Ocaml language. The code is as follows:
[0077] Table 4 Ocaml language
[0078]
[0079] After step S3, the finally generated C language code is shown in Table 5:
[0080] Table 5 C language code generated by the present invention (excerpt)
[0081]
[0082]
[0083] The C code generated by the original tool (L2C) is shown in Table 6:
[0084] Table 6 C code generated by L2C (excerpt)
[0085]
[0086]
[0087] It can be seen that compared with the code generated by the original tool, the code generated by the present invention includes a context structure initialization function, thus meeting the requirement of initializing the context structure.
[0088] Embodiment 3:
[0089] A synchronous data flow language translation method including an initialization function, the method comprising the following steps:
[0090] S1: Call the synchronous data flow language translation tool L2C to translate the Lustre program into a LustreF abstract syntax tree, and call OutStructGen to generate a context structure for the nodes in the program;
[0091] S2: Generate an initialization function for the context structure, the initialization function including initialization assignment statements for all elements in the context structure, and call its initialization function according to the context structure of the called function;
[0092] S3: Add the generated initialization function to the function list, and translate the program into C language through the remaining steps of L2C, ensuring that the initialization function and its call information are correctly output to the C language program.
[0093] Further, in step S2, the initialization function accepts a parameter of the structure pointer type and assigns default values to the elements in the structure according to a predetermined rule. The rule includes: assigning default values to basic data type elements, recursively assigning default values to array type elements, taking the first enumeration value of the enumeration type elements as the default value, and recursively assigning default values to structure type elements.
[0094] Further, the initialization function will recursively call the initialization functions of other called functions to ensure that nested context structures can be correctly initialized.
[0095] Further, the generated initialization function is added to the function list, and in the final C code, it is ensured that the initialization function and its call information are correctly output.
[0096] Further, through the generated initialization function, it can be ensured that the node context structure is correctly initialized before simulation, avoiding errors caused by unassigned pointers.
[0097] Further, the finally generated C code contains the context structure and its initialization function to meet the requirements of model simulation.
[0098] Further, a synchronous data flow language translation method including an initialization function is formally developed through the theorem prover Coq based on a trusted synchronous data flow language translation tool.
[0099] Beneficial effects:
[0100] The present invention proposes a method for generating an initialization function of a context structure in synchronous data flow language translation. This method has at least one or some of the following beneficial effects:
[0101] (1) At the code generation level, the construction of the initialization function is solved, which provides convenience for writing loop functions and offers more choices for users who want to customize loop functions.
[0102] (2) An initialization function of the context structure is created in the generated code. The loop function can directly call the initialization function in the entry function to complete the initialization work of the context structure, thus avoiding possible pointer reference errors.
[0103] (3) Before model simulation, initializing the context structure is a necessary task. The context structure initialization function can make the simulation work easier to carry out.
[0104] The above are only the preferred embodiments of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and modifications can be made, and these improvements and modifications should also be regarded as the protection scope of the present invention.
Claims
1. A synchronous data stream language translation method including an initialization function, characterized in that: The method comprises the following steps: S1. Call the synchronous data flow language translation tool to generate a context structure Call the synchronous data flow language translation tool to translate the Lustre program into the LustreF abstract syntax tree, and call OutStructGen to generate context structures for the nodes in the program; S2. Generate an initialization function for the context structure Traverse the nodes in LustreF and generate the corresponding initialization function according to its context structure; S3. Complete the translation process Add the initialization function definition to the LustreF program function definition list, call the L2C partial translation step, translate the program into C language, and make some adjustments to the C language output function so that it can output the initialization function information to the corresponding .c and .h files in the correct order.
2. The synchronous data stream language translation method including the initialization function as claimed in claim 1, characterized in that: The approach is based on a trusted translation tool for synchronous dataflow languages developed using Coq: L2C.
3. The synchronous data stream language translation method including the initialization function as claimed in claim 1, characterized in that: The LustreF abstract syntax tree is divided into four layers from top to bottom: program layer, node layer, statement layer, and expression layer. The nodes in the node layer include two types: function and node. Function is regarded as a simplified node, which does not contain temporal operators and node historical clock information.
4. The synchronous data stream language translation method including the initialization function as claimed in claim 1, characterized in that: The S2 includes: S21, generating an initialization function structure, which has an input variable of a corresponding function context structure pointer type; S22. Create a context structure element assignment statement in the initialization function; assign a default value to the built-in type variable in the abstract syntax tree; S23. For context structure type elements of other functions existing in the context structure, the corresponding context structure initialization function is called to initialize them.
5. The synchronous data stream language translation method including the initialization function as claimed in claim 4, characterized in that: The S22 includes: For enumeration type elements existing in the global environment, take the first enumeration class as the default value; For array type elements, recursively assign each bit to the default value of the data type; For structure type elements existing in the global environment: recurse its structure until all elements are assigned default values according to the rules mentioned above or this rule.
6. The synchronous data stream language translation method including the initialization function as claimed in claim 5, characterized in that: The S22 further includes: directly assigning a default value to the L2C simple data type element, specifically including: Tint__ is assigned CintInt.zero; Tlong_assigned to Clong Int64.zero; TfloatF32 is assigned to Csingle Floats.Float32.zero; TfloatF64 is assigned to CfloatFloats.Float.zero.
7. The synchronous data stream language translation method including the initialization function as claimed in claim 6, characterized in that: In S2, a Coq function is written to add an initialization function.
8. The synchronous data flow language translation method including the initialization function as claimed in claim 1, characterized in that: In S3, the generated initialization function is added to the function list, and in the final C language code, it is ensured that the initialization function and its calling information are correctly output.
9. The synchronous data flow language translation method including the initialization function as claimed in claim 1, characterized in that: In S3, partially adjusting the C language output function includes: adding an initialization function output function, which is implemented using Ocaml language.
10. The synchronous data flow language translation method including the initialization function as claimed in claim 1, characterized in that: The C language code finally generated by S3 includes a context structure and its initialization function.
Citation Information
Patent Citations
Formal automatic translation method and device of state machine model and computing equipment
CN114661302A
Method and device for translating intelligent contract language and electronic equipment
CN116149671A