Instruction format conversion method, device, system, conversion equipment and storage medium
By automatically generating instruction format conversion methods, constructing syntax trees, and generating read/write code files, the problems of high manpower consumption and low efficiency in complex external chip instruction processing are solved, achieving efficient instruction processing and anomaly detection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEI DOU ZHI LIAN KE JI YOU XIAN GONG SI
- Filing Date
- 2022-05-06
- Publication Date
- 2026-05-01
AI Technical Summary
In existing technologies, developers need to write a large number of packing and unpacking functions for control instructions and return results from complex external chips, which results in high manpower consumption, low efficiency, and is prone to code errors.
By acquiring the format file of the instructions to be processed, lexical analysis is performed to construct a syntax tree, generating read and write code files, and automatically generating packing and unpacking functions, reducing manual coding.
It eliminates the need for manually writing packing and unpacking functions, greatly saving manpower, improving efficiency, and detecting instruction exceptions, thus reducing runtime errors.
Smart Images

Figure CN115268922B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software development, and more specifically, to an instruction format conversion method, apparatus, system, conversion device, and storage medium. Background Technology
[0002] With the rapid development of automotive electronics technology, more and more electronic devices are connected to the main control chip via a bus. Some complex external chips have custom-formatted data packets. When the main control chip sends control commands to the external chip, it needs to package the data according to the chip's specified format before sending it; when obtaining the command execution results from the external chip, it also needs to unpack the received data according to the chip's specified format.
[0003] Currently, developers write separate packing and unpacking functions for each instruction and return result. For complex external chips, there may be hundreds of control instructions. This method of writing packing and unpacking functions requires a lot of manpower and is inefficient. Summary of the Invention
[0004] In view of this, the purpose of the present invention is to provide an instruction format conversion method, apparatus, system, conversion device and storage medium, which can improve the current problem that instruction packing and unpacking requires a lot of manpower and is inefficient.
[0005] To achieve the above objectives, the technical solutions adopted in the embodiments of the present invention are as follows.
[0006] In a first aspect, embodiments of the present invention provide an instruction format conversion method, which adopts the following technical solution.
[0007] An instruction format conversion method, the method comprising:
[0008] Obtain the format file of the instructions to be processed;
[0009] Lexical analysis is performed on the format file to obtain instruction information of the instruction to be processed, the instruction information including the field order of each field in the instruction to be processed;
[0010] Based on the instruction information, construct the syntax tree of the format file according to the order of the fields;
[0011] The syntax tree is traversed, and a read / write code file for the instruction to be processed is generated according to a preset code specification, so that the first device can use the read / write code file to read and write the instruction to be processed.
[0012] Furthermore, the instruction information also includes the instruction name of the instruction to be processed and field information for each field;
[0013] The step of constructing the syntax tree of the format file based on the instruction information and according to the field order includes:
[0014] Using the instruction name as the root node, and following the field order, the field information of each field is sequentially used as a child node to obtain the syntax tree of the format file;
[0015] The field information includes the field name and the number of bits occupied by the field.
[0016] Further, the step of traversing the syntax tree and generating a read / write code file for the instruction to be processed according to a preset code specification includes:
[0017] Based on the root node in the syntax tree, the code information is determined, which includes the structure name, the packing function name, and the unpacking function name;
[0018] Based on each child node in the syntax tree, the structure information is determined, which includes the member fields of the structure, the name of each member field, and the number of bits in each member field;
[0019] According to the preset code specifications and in the order of each of the child nodes, the read / write code file is generated by combining the code information and the structure information. The read / write code file includes a structure definition file, a declaration file and an implementation file of the packing function, and a declaration file and an implementation file of the unpacking function.
[0020] Further, the step of obtaining the format file of the instruction to be processed includes:
[0021] According to the instruction manual of the second device, the format file of the instruction to be processed is obtained;
[0022] The second device is the device that generates the instruction to be processed.
[0023] Further, the step of obtaining the format file of the instruction to be processed according to the instruction manual of the second device includes:
[0024] Extract the data packet format of the instruction to be processed from the instruction manual of the second device;
[0025] Based on the data packet format, the format file of the instruction to be processed is obtained.
[0026] Furthermore, the code specifications include programming language, packing function code specifications, and unpacking function code specifications.
[0027] Secondly, embodiments of the present invention provide an instruction format conversion device, which adopts the following technical solution.
[0028] An instruction format conversion device includes a file acquisition module, a file parsing module, and a code generation module;
[0029] The file acquisition module is used to acquire the format file of the instruction to be processed;
[0030] The file parsing module is used to perform lexical analysis on the format file to obtain the instruction information of the instruction to be processed, the instruction information including the field order of each field in the instruction to be processed;
[0031] The code generation module is used to construct the syntax tree of the format file based on the instruction information and in accordance with the field order, traverse the syntax tree, and generate a read / write code file for the instruction to be processed according to a preset code specification, so that the first device can use the read / write code file to read and write the instruction to be processed.
[0032] Thirdly, embodiments of the present invention provide an instruction format conversion system, which adopts the following technical solution.
[0033] An instruction format conversion system includes a first device, a second device, and a conversion device;
[0034] The conversion device is used to implement the instruction format conversion method as described in the first aspect;
[0035] The second device is used to send the generated instruction to be processed and the read / write code file generated by the conversion device to the first device;
[0036] The first device is used to read / write the instruction to be processed using the read / write code file.
[0037] Fourthly, embodiments of the present invention provide a conversion device, which adopts the following technical solution.
[0038] A conversion device includes a processor and a memory, the memory storing a computer program executable by the processor to implement the instruction format conversion method as described in the first aspect.
[0039] Fifthly, embodiments of the present invention provide a storage medium, which adopts the following technical solution.
[0040] A storage medium having a computer program stored thereon, which, when executed by a processor, implements the instruction format conversion method as described in the first aspect.
[0041] The instruction format conversion method, apparatus, system, conversion device, and storage medium provided in this invention perform lexical analysis on the format file of the instruction to be processed, construct a syntax tree for the format file, and then generate a read / write code file for the instruction to be processed according to the syntax tree and coding standards. This allows a first device that needs to read or write the instruction to be processed to use the read / write code file to read and write the instruction, eliminating the need for manually writing packing and unpacking functions, greatly saving manpower and improving efficiency.
[0042] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description
[0043] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.
[0044] Figure 1 A block diagram of the instruction format conversion system provided in an embodiment of the present invention is shown.
[0045] Figure 2 A flowchart illustrating the instruction format conversion method provided in an embodiment of the present invention is shown.
[0046] Figure 3 It shows Figure 2 A flowchart illustrating some sub-steps of step S101.
[0047] Figure 4 A schematic diagram of a syntax tree structure is shown.
[0048] Figure 5 It shows Figure 2 A flowchart illustrating some sub-steps of step S107.
[0049] Figure 6 A block diagram of an instruction format conversion device provided in an embodiment of the present invention is shown.
[0050] Figure 7 A block diagram of a conversion device provided in an embodiment of the present invention is shown.
[0051] Icons: 100 - Instruction format conversion system; 110 - First device; 120 - Second device; 130 - Conversion device; 140 - Instruction format conversion apparatus; 150 - File acquisition module; 160 - File parsing module; 170 - Code generation module. Detailed Implementation
[0052] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.
[0053] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.
[0054] It should be noted that relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0055] With the rapid development of automotive electronics technology, more and more electronic devices are connected to the main control chip (main processor) via a bus. Some complex external chips (external processors) use custom-formatted data packets. When the main control chip sends control commands to an external chip, it needs to package the data according to the external chip's specified format before sending it. When the main control chip obtains the command execution results from an external chip, it also needs to unpack the received data according to the external chip's specified format.
[0056] Currently, developers write separate packing and unpacking functions for each instruction and its return result. However, for external chips handling the load, there may be hundreds or more control instructions. Writing separate packing (write) and unpacking (read) functions requires significant manpower and is inefficient. Furthermore, this approach is prone to subtle errors and runtime exceptions.
[0057] Based on the above considerations, embodiments of the present invention provide an instruction format conversion scheme that can automatically generate packing and unpacking functions to improve efficiency, reduce manpower consumption, and detect whether instructions are abnormal. The following embodiments of the present invention will describe this scheme.
[0058] In one embodiment, an instruction format conversion system 100 is provided, with reference to... Figure 1 The instruction format conversion system 100 includes a first device 110, a second device 120, and a conversion device 130. The first device 110, the second device 120, and the conversion device 130 can communicate with each other via wired or wireless means.
[0059] The conversion device 130 can be used to implement the instruction format conversion method provided in the embodiments of the present invention to obtain a read / write code file for any instruction to be processed.
[0060] The second device 120 is used to send the generated instructions to be processed and the read / write code file generated by the conversion device 130 to the first device 110.
[0061] The code files for reading and writing include structure definition files, declaration and implementation files for packing functions, and declaration and implementation files for unpacking functions.
[0062] It should be understood that the declaration and implementation files of the packing function constitute the packing function, and the declaration and implementation files of the unpacking function constitute the packing function.
[0063] The first device 110 is used to read / write pending instructions using a read / write code file.
[0064] The first device 110 uses a structure definition and calls a packing function to read the instructions to be processed. Alternatively, the first device 110 uses a structure definition and calls an unpacking function to write the instructions to be processed into the second device 120.
[0065] It should be understood that the first device 110 may be, but is not limited to, a main control chip, a main control device, and a main processor, etc., and the second device 120 may be, but is not limited to, an external chip, a controlled chip, and a backup processor, etc.
[0066] In the aforementioned instruction format conversion system 100, the conversion device 130 generates a read / write code file for the instruction to be processed, so that the first device 110 can use the read / write code file to read and write the instruction to be processed without the need for manual writing of packing and unpacking functions, which greatly saves manpower and improves instruction processing efficiency.
[0067] It should be noted that the instruction format conversion method provided in this embodiment of the invention, implemented by the conversion device 130, is merely an example of one implementation method and not the only one. In other embodiments, it can be implemented by the first device 110 or the second device 120.
[0068] In one embodiment, refer to Figure 2 This paper provides a method for converting instruction formats. In this embodiment, the execution subject of the method is... Figure 1 Let's take the conversion device 130 in the example as an illustration.
[0069] S101, Obtain the format file of the instructions to be processed.
[0070] S103, perform lexical analysis on the format file to obtain the instruction information of the instruction to be processed.
[0071] The instruction information includes the field order of each field in the instruction to be processed.
[0072] After the conversion device 130 obtains the format file of the instruction to be processed, it uses a preset lexical analyzer to perform lexical analysis on the format file, splitting the text content in the format file into individual lexical units to obtain the instruction information.
[0073] Each lexical unit has a type attribute, such as a number, identifier, or comment.
[0074] Lexical analysis is the process in computer science of converting a sequence of characters into a sequence of tokens. This lexical analysis is accomplished using a lexical analyzer. A lexical analyzer is a program or function that performs lexical analysis; it is also known as a scanner.
[0075] S105 constructs the syntax tree of the format file based on the instruction information and in the order of the fields.
[0076] The syntax tree is constructed according to the field order of the instructions to be processed; therefore, the order of the syntax tree is related to the field order.
[0077] S107, Traverse the syntax tree and generate a read / write code file for the instruction to be processed according to the preset code specifications, so that the first device can use the read / write code file to read and write the instruction to be processed.
[0078] Since the order of the syntax tree is related to the order of the fields, the read / write code file generated after traversing the syntax tree also strictly follows the field order of the instructions to be processed.
[0079] Compared to manually writing packing and unpacking functions for each instruction, the above instruction format conversion method performs lexical analysis on the format file of the instruction to be processed, constructs a syntax tree for the format file, and then generates a read / write code file for the instruction to be processed according to the syntax tree and coding standards. The first device 110, which is to read and write the instruction, can then use the read / write code file to read and write the instruction, eliminating the need for manually writing packing and unpacking functions, greatly saving manpower and improving efficiency.
[0080] Regarding S101, the method for obtaining the format file of the instruction to be processed can be flexibly selected. For example, it can be input by the developer to the conversion device 130, or it can be sent to the conversion device 130 by the second device 120 or the first device 110. In one embodiment, step S101 is achieved by the following steps: obtaining the format file of the instruction to be processed according to the instruction manual of the second device 120.
[0081] Furthermore, referring to Figure 3 S101 can be achieved through the following steps.
[0082] S101-1, Extract the data packet format of the instruction to be processed from the instruction manual of the second device.
[0083] The instruction manual for the second device 120 can be pre-stored in the instruction library of the conversion device 130, which stores instruction manuals for various types of devices. Each instruction manual and the unique identifier of the device are stored as dictionary-type data, and the instruction manual for each device can be retrieved by querying the identifier.
[0084] The instruction manual includes the data packet format for each instruction of the device, allowing the data packet format of the instruction to be processed to be queried based on the name or type of the instruction.
[0085] It should be understood that the instruction manual can be a chip manual.
[0086] S101-2, Based on the data packet format, obtain the format file of the instruction to be processed.
[0087] By reading the data packet format, the format file of the instructions to be processed can be obtained.
[0088] The instruction information obtained in step S103 also includes the instruction name of the instruction to be processed and the field information of each field, including the field name and the bit length occupied by the field.
[0089] For example, when the definition format (format file) of a certain instruction to be processed is: "defSystemUP_not:
[0090] Handle:8
[0091] Command:8
[0092] Version:16
[0093] Status:6
[0094] Reserved:2
[0095] end".
[0096] The instruction to be processed is named SystemUP_not, and it has 5 fields in the following order: Handle, Command, Version, Status, and Reserved. The field names are Handle, Command, Version, Status, and Reserved, respectively. The bit lengths occupied by these fields are 8, 8, 16, 6, and 2, respectively.
[0097] After obtaining the instruction information, the syntax tree can be built in the following way, that is, step S105: take the instruction name as the root node, and take the field information of each field as the child node in the order of the fields to obtain the syntax tree of the format file.
[0098] Reference Figure 4 , Figure 4 This is the syntax tree for the aforementioned format file. In one implementation, the instruction name can be used as the root node, the field order as the child nodes, and the field information as the leaf nodes to build the syntax tree.
[0099] The constructed syntax tree can show both the order of the fields and the information about each field.
[0100] It should be understood that the syntax tree shown in the figure is one structural form and is not the only limitation. In practical applications, other forms of syntax trees can be constructed according to actual needs and preferences.
[0101] Furthermore, referring to Figure 5 Step S107 can be achieved through the following steps.
[0102] S107-1, Determine code information based on the root node in the syntax tree.
[0103] The code information includes the structure name, the packing function name, and the unpacking function name.
[0104] The code information can be determined according to preferences or needs; no specific restrictions are imposed here.
[0105] For example, with Figure 4Taking the syntax tree in the example, the root node is the instruction name SystemUP_not, the structure name can be structSystemUP_not, the packing function name is SystemUP_not_serialize, and the unpacking function name is SystemUP_not_serialize.
[0106] S107-2, determine the structure information based on each child node in the syntax tree.
[0107] The structure information includes the structure's member fields, the name of each member field, and the number of bits in each member field.
[0108] The form of structure information can vary depending on the programming language used, and no specific limitation is made here.
[0109] In one implementation, to Figure 4 Taking the syntax tree in the example, and the programming language as C, the structure information can include uint8_t Handle, uint8_t Command, uint8_t Version, uint8_tStatus, and uint8_tReserved.
[0110] S107-3 generates read / write code files according to the preset code specifications and the order of each child node, combining code information and structure information.
[0111] The code files for reading and writing include structure definition files, declaration and implementation files for packing functions, and declaration and implementation files for unpacking functions.
[0112] Code style guidelines include programming language style guidelines, packing function style guidelines, and unpacking function style guidelines. Code style guidelines can be flexibly configured; for example, packing function style guidelines can be packing function code rules or packing function code frameworks. Similarly, unpacking function style guidelines can be unpacking function code rules or unpacking function code frameworks.
[0113] If the code style guide is a set of code rules, then read and write code files can be generated according to the rules in the specified programming language. If the code style guide is a code framework, then the content values in the code framework can be updated with code information and structure information to obtain read and write code files.
[0114] by Figure 4 Taking the syntax tree as an example, the structure definition file for the generated read / write code file can be:
[0115] "typedefstructSystemUP_not{
[0116] uint8_t Handle;
[0117] uint8_t Command;
[0118] uint16_t Version;
[0119] uint8_t Status: 6;
[0120] uint8_t Reserved:2;
[0121] }SystemUP_not;”.
[0122] The generated packaged function declaration file can be:
[0123] "SystemUP_not_serialize(constSystemUP_not*st,uint8_t*buff,intbuff_size)".
[0124] The unpacking function declaration file can be:
[0125] "SystemUP_not*SystemUP_not_deserialize(uint8_t*buff,intbuff_size)".
[0126] The implementation file for the packaging function can be:
[0127] "intSystemUP_not_serialize(constSystemUP_not*st,uint8_t*buff,intbuff_size){
[0128] int ret = 0, bits = 0;
[0129] bits_writer_t*bw=bits_writer_init(buff,buff_size);
[0130] ret=bits_write(bw,8,st->Handle); assert(ret>=0); bits+=8;
[0131] ret=bits_write(bw,8,st->Command); assert(ret>=0); bits+=8;
[0132] ret = bits_write(bw, 16, st->Version); assert(ret >= 0); bits += 16;
[0133] ret = bits_write(bw, 6, st->Status); assert(ret >= 0); bits += 6;
[0134] ret = bits_write(bw, 2, st->Reserved); assert(ret >= 0); bits += 2;
[0135] bits_writer_deinit(&bw);
[0136] assert((bits % 8 == 0) && (bits <= buff_size * 8));
[0137] return (bits + 7) / 8; <00
[0148] st->Reserved=bits_read(br,2); bits+=2;
[0149] assert(((bits+7) / 8)<=buff_size);
[0150] bits_reader_deinit(&br);
[0151] returnst;
[0152] }".
[0153] By following the steps S107-1 to S107-3 above, a read / write code file can be generated.
[0154] In one implementation, when generating read / write code, the conversion device 130 can detect whether the instruction to be processed is abnormal by combining the syntax tree (or format file). That is, based on the number of bits in each member field, it can detect whether the instruction to be processed conforms to the data packet format. If they are different, it may be that the instruction to be processed is abnormal, or the selected format file is incorrect. When an abnormality occurs, the conversion device 130 can issue an alarm and provide a warning about the abnormality (e.g., sending the instruction to be processed and the selected format file together to the development department, or displaying the instruction to be processed or the selected format file together) to remind the developer to handle it in a timely manner.
[0155] When a device that needs to read or write instructions receives the instructions to be processed and the read / write code files (including structure definition files, declaration files and implementation files of packing and unpacking functions), it uses the structure definition to call the packing function (the declaration file and implementation file of the packing function) or the unpacking function (the declaration file and implementation file of the unpacking function) to read or write the instructions to be processed.
[0156] The instruction format conversion method provided in this invention generates a syntax tree based on the format file of the instruction to be processed, and then automatically generates read / write code files for the instruction to be processed. This eliminates the need for manual writing of read / write code; developers only need to focus on business logic and do not need to worry about the communication details of data packets (instructions), thus saving manpower and development time while improving efficiency. Furthermore, during the generation of the read / write code files, anomalies can be detected, allowing for timely error discovery and helping to reduce runtime exceptions.
[0157] Based on the inventive concept of the above-described instruction format conversion method, in one embodiment, referring to... Figure 6 A command format conversion device 140 is provided, including a file acquisition module 150, a file parsing module 160, and a code generation module 170.
[0158] The file acquisition module 150 is used to acquire the format file of the instruction to be processed.
[0159] The file parsing module 160 is used to perform lexical analysis on the format file to obtain the instruction information of the instruction to be processed.
[0160] The instruction information includes the field order of each field in the instruction to be processed.
[0161] The code generation module 170 is used to construct the syntax tree of the format file based on the instruction information and in the order of the fields, traverse the syntax tree, and generate a read / write code file for the instruction to be processed according to the preset code specification, so that the first device 110 can use the read / write code file to read and write the instruction to be processed.
[0162] The aforementioned instruction format conversion file can be applied to the conversion device 130 or to devices that process instructions. By generating a syntax tree from the instruction format file, the read / write code file for the instructions is automatically generated. Wireless developers can then write the read / write code themselves, thus improving development efficiency and saving manpower.
[0163] Specific limitations regarding the instruction format conversion device 140 can be found in the limitations of the instruction format conversion method described above, and will not be repeated here. Each module in the instruction format conversion device 140 can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in hardware within or independently of the processor in the conversion device 130, or stored in software in the memory of the conversion device 130, so that the processor can call and execute the operations corresponding to each module.
[0164] In one embodiment, a conversion device 130 is provided, which may be a terminal, and its internal structure diagram may be as follows: Figure 7As shown. The conversion device 130 includes a processor, memory, communication interface, display screen, and input device connected via a system bus. The processor of the conversion device 130 provides computing and control capabilities. The memory of the conversion device 130 includes a non-volatile storage medium and internal memory. The non-volatile storage medium stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The communication interface of the conversion device 130 is used for wired or wireless communication with external terminals. Wireless communication can be achieved through Wi-Fi, carrier networks, Near Field Communication (NFC), or other technologies. When the computer program is executed by the processor, it implements an instruction format conversion method. The display screen of the conversion device 130 can be a liquid crystal display (LCD) or an e-ink display. The input device of the conversion device 130 can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the casing of the conversion device 130, or an external keyboard, touchpad, or mouse.
[0165] Those skilled in the art will understand that Figure 7 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the conversion device 130 to which the present application is applied. The specific conversion device 130 may include, but is not limited to, the following: Figure 7 The diagram shows more or fewer components, or combinations of certain components, or different component arrangements.
[0166] In one embodiment, the instruction format conversion device 140 provided in this application can be implemented as a computer program, and the computer program can be implemented as follows: Figure 7 The conversion device 130 shown operates on this device. The memory of the conversion device 130 can store the various program modules that make up the instruction format conversion apparatus 140, for example, Figure 6 The file acquisition module 150, file parsing module 160, and code generation module 170 are shown. The computer program comprised of these modules causes the processor to execute the steps in the instruction format conversion methods of the various embodiments of this application described in this specification.
[0167] For example, Figure 7 The conversion device 130 shown can be used as follows Figure 6 The file acquisition module 150 in the instruction format conversion device 140 executes step S101. The conversion device 130 can execute step S103 through the file parsing module 160. The conversion device 130 can execute steps S105 and S107 through the code generation module 170.
[0168] In one embodiment, a conversion device 130 is provided, including a memory and a processor. The memory stores a computer program, and the processor executes the computer program to perform the following steps: acquiring a format file of instructions to be processed; performing lexical analysis on the format file to obtain instruction information of the instructions to be processed, the instruction information including the field order of each field in the instructions to be processed; constructing a syntax tree of the format file based on the instruction information and in the field order; traversing the syntax tree and generating a read / write code file for the instructions to be processed according to a preset code specification, so that a first device 110 can use the read / write code file to read and write the instructions to be processed.
[0169] In one embodiment, when the processor executes a computer program, it further performs the following steps: determining code information based on the root node in the syntax tree, the code information including the structure name, the packing function name, and the unpacking function name; determining structure information based on each child node in the syntax tree, the structure information including the member fields of the structure, the name of each member field, and the number of bits of each member field; and generating a read / write code file according to a preset code specification and in the order of each child node, combining the code information and the structure information, the read / write code file including the structure definition file, the declaration file and implementation file of the packing function, and the declaration file and implementation file of the unpacking function.
[0170] In one embodiment, a storage medium is provided on which a computer program is stored. When the computer program is executed by a processor, it performs the following steps: obtaining a format file of an instruction to be processed; performing lexical analysis on the format file to obtain instruction information of the instruction to be processed, the instruction information including the field order of each field in the instruction to be processed; constructing a syntax tree of the format file based on the instruction information and in the field order; traversing the syntax tree and generating a read / write code file for the instruction to be processed according to a preset code specification, so that the first device 110 can use the read / write code file to read and write the instruction to be processed.
[0171] In one embodiment, when the computer program is executed by the processor, it further performs the following steps: Based on the root node in the syntax tree, determine code information, including the structure name, packing function name, and unpacking function name; based on each child node in the syntax tree, determine structure information, including the member fields of the structure, the name of each member field, and the number of bits in each member field; according to a preset code specification and in the order of each child node, combine the code information and structure information to generate a read / write code file, including a structure definition file, a declaration file and implementation file for the packing function, and a declaration file and implementation file for the unpacking function.
[0172] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can also be implemented in other ways. The apparatus embodiments described above are merely illustrative; for example, the flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram and / or flowchart, and combinations of blocks in block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0173] In addition, the functional modules in the various embodiments of the present invention can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0174] If the aforementioned functions are implemented as software functional modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a conversion device 130 (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0175] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for converting instruction formats, characterized in that, The method includes: Obtain the format file of the instructions to be processed; Lexical analysis is performed on the format file to obtain instruction information of the instruction to be processed, the instruction information including the field order of each field in the instruction to be processed; Based on the instruction information, construct the syntax tree of the format file according to the order of the fields; Traverse the syntax tree and generate a read / write code file for the instruction to be processed according to a preset code specification, so that the first device can use the read / write code file to read and write the instruction to be processed; The read / write code files include a structure definition file, a declaration file and an implementation file for the packing function, and a declaration file and an implementation file for the unpacking function; the declaration file and implementation file of the packing function constitute the unpacking function, and the declaration file and implementation file of the unpacking function constitute the packing function; the structure definition is used to call the packing function or the unpacking function to read and write the instructions to be processed.
2. The instruction format conversion method according to claim 1, characterized in that, The instruction information also includes the instruction name of the instruction to be processed and the field information of each field; The step of constructing the syntax tree of the format file based on the instruction information and according to the field order includes: Using the instruction name as the root node, and following the field order, the field information of each field is sequentially used as a child node to obtain the syntax tree of the format file; The field information includes the field name and the number of bits occupied by the field.
3. The instruction format conversion method according to claim 2, characterized in that, The step of traversing the syntax tree and generating a read / write code file for the instruction to be processed according to a preset code specification includes: Based on the root node in the syntax tree, the code information is determined, which includes the structure name, the packing function name, and the unpacking function name; Based on each child node in the syntax tree, the structure information is determined, which includes the member fields of the structure, the name of each member field, and the number of bits in each member field; According to the preset code specifications and in the order of each of the child nodes, the read / write code file is generated by combining the code information and the structure information. The read / write code file includes a structure definition file, a declaration file and an implementation file of the packing function, and a declaration file and an implementation file of the unpacking function.
4. The instruction format conversion method according to any one of claims 1 to 3, characterized in that, The step of obtaining the format file of the instruction to be processed includes: According to the instruction manual of the second device, the format file of the instruction to be processed is obtained; The second device is the device that generates the instruction to be processed.
5. The instruction format conversion method according to claim 4, characterized in that, The step of obtaining the format file of the instruction to be processed according to the instruction manual of the second device includes: Extract the data packet format of the instruction to be processed from the instruction manual of the second device; Based on the data packet format, the format file of the instruction to be processed is obtained.
6. The instruction format conversion method according to any one of claims 1 to 3, characterized in that, The code standards include programming language, code standards for packing functions, and code standards for unpacking functions.
7. An instruction format conversion device, characterized in that, It includes a file acquisition module, a file parsing module, and a code generation module; The file acquisition module is used to acquire the format file of the instruction to be processed; The file parsing module is used to perform lexical analysis on the format file to obtain the instruction information of the instruction to be processed, the instruction information including the field order of each field in the instruction to be processed; The code generation module is used to construct the syntax tree of the format file based on the instruction information and according to the field order, traverse the syntax tree, and generate a read / write code file for the instruction to be processed according to a preset code specification, so that the first device can use the read / write code file to read and write the instruction to be processed. The read / write code files include a structure definition file, a declaration file and an implementation file for the packing function, and a declaration file and an implementation file for the unpacking function; the declaration file and implementation file of the packing function constitute the unpacking function, and the declaration file and implementation file of the unpacking function constitute the packing function; the structure definition is used to call the packing function or the unpacking function to read and write the instructions to be processed.
8. An instruction format conversion system, characterized in that, Includes the first device, the second device, and the conversion device; The conversion device is used to implement the instruction format conversion method as described in any one of claims 1 to 6; The second device is used to send the generated instruction to be processed and the read / write code file generated by the conversion device to the first device; The first device is used to read / write the instruction to be processed using the read / write code file.
9. A conversion device, characterized in that, It includes a processor and a memory, the memory storing a computer program executable by the processor, the processor being able to execute the computer program to implement the instruction format conversion method as described in any one of claims 1 to 6.
10. A storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the instruction format conversion method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Reading and conversion method for PLC (Programmable Logic Controller) instruction storage rule in PMW-format file
CN103218349A
Test code generation method and device, computer system and medium
CN113778848A