Language compiling method and device, electronic equipment and storage medium
The method of converting P4 language files to C language files and using a modified GCC compiler with RISC-V runtime headers addresses the inefficiency of existing P4 language compilation for general-purpose processing chips, achieving efficient and accurate compilation for RISC-V chips.
Patent Information
- Application Number
- CN202311237708.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-22
- Publication Date
- 2025-03-25
AI Technical Summary
Current methods lack an efficient way to compile P4 language for general-purpose processing chips, leading to increased complexity and time due to the need for specific hardware structures like JSON intermediate languages.
A method involving a P4 compiler that converts P4 language files into C language files, which are then compiled using a modified GCC compiler with RISC-V runtime headers to produce executable files for RISC-V chips, bypassing the need for JSON intermediate languages.
This approach enables efficient and accurate mapping of P4 language logic onto RISC-V chips, reducing compilation complexity and time while ensuring compatibility with general-purpose processing chips.
Smart Images

Figure CN119690441A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of network communication technology, and in particular to a language compilation method, device, electronic equipment and storage medium. Background Art
[0002] P4 is a programming language that can be used to describe traffic offloading logic. Before executing the traffic offloading logic through the chip, the P4 language file describing the traffic offloading logic needs to be compiled into a language file executable by the current chip. At present, traffic offloading is generally processed by Application Specific Integrated Circuit (ASIC) chips, and many ASIC chips are a kind of table lookup engine, and the message processing action is limited by the hardware structure. In theory, general-purpose processing chips can process any customized messages without being restricted by hardware.
[0003] In the related art, the P4 language is usually converted into the JSON intermediate language first, and then the JSON intermediate language is converted into a language file executable by the ASIC chip. The JSON intermediate language is suitable for the back end of a specific hardware structure, such as a table lookup engine. Application to general-purpose processing chips will increase the compilation complexity and increase the compilation time. Therefore, there is currently a lack of an efficient P4 language compilation method for general-purpose processing chips. Summary of the invention
[0004] The present invention provides a language compilation method, device, electronic device and storage medium to solve the technical problem of lack of a P4 language compilation method that is efficiently applied to a general-purpose processing chip.
[0005] According to one aspect of the present invention, a language compilation method is provided, wherein the method comprises:
[0006] Obtain a first language file, wherein the first language file is a file that records traffic offloading logic based on the P4 language;
[0007] Compiling the first language file input by a P4 compiler to obtain a second language file, wherein the second language file is a file recording the traffic offloading logic based on C language;
[0008] The input second language file is compiled by the modified GCC compiler to obtain a target file, wherein the target file is an executable file of the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend.
[0009] According to another aspect of the present invention, a language compilation device is provided, wherein the device comprises:
[0010] An original file acquisition module, used to acquire a first language file, wherein the first language file is a file recording traffic offloading logic based on the P4 language;
[0011] A first compiling module, configured to compile the first language file inputted by a P4 compiler to obtain a second language file, wherein the second language file is a file recording the traffic offloading logic based on the C language;
[0012] The second compilation module is used to compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file of the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend.
[0013] According to another aspect of the present invention, an electronic device is provided, the electronic device comprising:
[0014] at least one processor; and
[0015] a memory communicatively connected to the at least one processor; wherein,
[0016] The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the language compilation method described in any embodiment of the present invention.
[0017] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the language compilation method described in any embodiment of the present invention when executed.
[0018] The technical solution of the embodiment of the present invention is to obtain a first language file, wherein the first language file is a file that records the traffic unloading logic based on the P4 language; compile the input first language file through the P4 compiler to obtain a second language file, wherein the second language file is a file that records the traffic unloading logic based on the C language; compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file for the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend. In the process of compiling the P4 language file into an executable file for the RISC-V chip backend, the present application uses the C language file as an intermediate output and calls the runtime header file of the RISC-V chip backend to modify the GCC compiler, so that the traffic unloading logic recorded by the P4 language can be more efficiently and accurately mapped to the RISC-V chip backend, ensuring the accuracy and efficiency of language compilation.
[0019] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0021] Figure 1 is a flowchart of a language compilation method provided according to Embodiment 1 of the present invention;
[0022] Figure 2 is a flowchart of a GCC compiler compilation provided according to an embodiment of the present invention;
[0023] Figure 3 It is a scene graph of operation code and function code occupation provided according to an embodiment of the present invention.
[0024] Figure 4 is a flow chart of a language compilation method provided according to Embodiment 2 of the present invention;
[0025] Figure 5 is a flowchart of a P4 compiler compiled according to an embodiment of the present invention;
[0026] Figure 6It is a flow chart of variable definition in a C language compiled Parser package provided according to an embodiment of the present invention;
[0027] Figure 7 It is a flowchart of the states in a C language compiled Parser package provided according to an embodiment of the present invention;
[0028] Figure 8 is a flowchart of a C language compiling Control package provided according to an embodiment of the present invention;
[0029] Fig. 9 This is a flowchart of a C language compiling Table package according to an embodiment of the present invention;
[0030] Fig.10 is an overall flow chart of a language compilation method provided according to an embodiment of the present invention;
[0031] Fig.11 is a schematic diagram of the structure of a language compiling device provided according to Embodiment 3 of the present invention;
[0032] Fig.12 It is a schematic diagram of the structure of an electronic device for implementing the language compilation method according to an embodiment of the present invention. DETAILED DESCRIPTION
[0033] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0034] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention 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 interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof 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.
[0035] Embodiment 1
[0036] Figure 1A flow chart of a language compilation method is provided for the first embodiment of the present invention. This embodiment is applicable to the case of P4 language translation. The method can be executed by a language compilation device. The language compilation device can be implemented in the form of software. The language compilation device can be configured in computer software. Figure 1 As shown, the method includes:
[0037] S110. Obtain a first language file, wherein the first language file is a file that records traffic offloading logic based on the P4 language.
[0038] The file format of the first language file may be .p4.
[0039] S120. Compile the input first language file through a P4 compiler to obtain a second language file, wherein the second language file is a file that records the traffic offloading logic based on C language.
[0040] The P4 compiler can be understood as a compiler that compiles the P4 language into the C language.
[0041] The file format of the second language file may be .c / .h.
[0042] S130. Compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file of the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend.
[0043] The GCC compiler can be understood as an open source compiler. Optionally, the GCC compiler is a compiler that compiles the C language into the RISC-V chip back-end executable language.
[0044] The target file can be understood as a file executable by the RISC-V chip backend. Optionally, the target file can be a RISC-V chip backend binary file. The file format of the target file can be .IMG.
[0045] The RISC-V chip can be understood as a general-purpose processing chip.
[0046] The runtime header file can be understood as a file that records the assembly instructions of the RISC-V chip backend.
[0047] Optionally, the GCC compiler includes at least one of a cc1 tool, an as tool, and a collect2 tool. Before the modified GCC compiler is used to compile the input second language file, the following steps are further included:
[0048] The assembly instructions recorded in the runtime header file are obtained, and the original as tool in the original GCC compiler is modified based on the assembly instructions to obtain the modified as tool and the modified GCC compiler.
[0049] The assembly instructions may be understood as an assembly language instruction set.
[0050] The cc1 tool can be understood as a tool with language preprocessing and compiling functions. The as tool can be understood as a tool with language assembly function. The collect2 tool can be understood as a tool with language linking function.
[0051] Figure 2 1 is a flowchart of a GCC compiler compiled according to an embodiment of the present invention. Figure 2 As shown, the second language file input is compiled by the modified GCC compiler to obtain a target file, which may include:
[0052] Preprocessing and compiling the second language file by the cc1 tool to obtain a first intermediate file;
[0053] Compile the first intermediate file by using the as tool to obtain a second intermediate file;
[0054] The second intermediate file is linked by the collect2 tool to obtain the target file.
[0055] In the embodiment of the present invention, the tool for linking the second intermediate file may also be an Id tool.
[0056] Optionally, modifying the original as tool in the original GCC compiler based on the assembly instruction to obtain the modified as tool includes:
[0057] Based on the assembly instruction and the instruction format of the assembly instruction, determining modification reference data, and modifying the original as tool based on the modification reference data to obtain the modified as tool;
[0058] Among them, the instruction format includes an opcode and / or a function code, and the modified reference data includes a matching code, a mask, a register type definition, an immediate type definition, a target register type, a target immediate type, and at least one item of the reverse parsing process corresponding to the target operand type.
[0059] The instruction format may be understood as the format of the assembly instruction. Optionally, the instruction format may include an operation code and / or a function code. The operation code and the function code may be used to identify the assembly instruction, and occupy 28 to 31 bits and 0 to 6 bits of a 32-bit assembly instruction, respectively (see Figure 3 ), it is important to understand that the address codes or immediate values of different assembly instructions are distributed differently.
[0060] The modified reference data may be understood as data required for modifying the AS tool.
[0061] Optionally, the determining the modification reference data based on the assembly instruction and the instruction format of the assembly instruction may include:
[0062] Generate a matching code and a mask according to the instruction format of the assembly instruction;
[0063] Adding a definition of the instruction format according to the assembly instruction, wherein the definition of the instruction format includes a register type definition and / or an immediate value type definition;
[0064] Parsing the target register type corresponding to the register type definition and the target immediate value type corresponding to the immediate value type definition according to the assembly instruction;
[0065] Add a reverse parsing process corresponding to the target operand type according to the assembly instruction.
[0066] The technical solution of the embodiment of the present invention is to obtain a first language file, wherein the first language file is a file that records the traffic unloading logic based on the P4 language; compile the input first language file through the P4 compiler to obtain a second language file, wherein the second language file is a file that records the traffic unloading logic based on the C language; compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file for the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend. In the process of compiling the P4 language file into an executable file for the RISC-V chip backend, the present application uses the C language file as an intermediate output and calls the runtime header file of the RISC-V chip backend to modify the GCC compiler, so that the traffic unloading logic recorded by the P4 language can be more efficiently and accurately mapped to the RISC-V chip backend, ensuring the accuracy and efficiency of language compilation.
[0067] Embodiment 2
[0068] Figure 4This is a flow chart of a language compilation method provided in the second embodiment of the present invention. This embodiment is to compile the first language file input by the P4 compiler as described in the above embodiment to obtain a second language file for refinement. Figure 4 As shown, the method includes:
[0069] S210: Obtain a first language file.
[0070] Optionally, the P4 compiler includes at least one of an architecture-independent front end, an architecture-dependent mid end, and an architecture-dependent back end.
[0071] S220: Compile the first language file through the architecture-independent front end to obtain an intermediate IR tree.
[0072] Optionally, the architecture-independent front end performs lexical, grammatical and semantic analysis on the P4 language file to generate the intermediate IR tree.
[0073] S230 , compile the intermediate IR tree through the system-related middle end to obtain an optimized data packet.
[0074] Optionally, the intermediate IR tree is optimized by the system-related middle end to generate an optimized data packet, wherein the optimized data packet includes at least one of a Parser packet, a Control packet, and a Table packet.
[0075] The Parser package can be understood as a finite state machine that defines the optimized data packet parsing process. Optionally, the Parser package can be composed of variable definitions and / or a series of states.
[0076] The Control package can be understood as a model that defines the optimized data packet processing flow. The processing flow may include rough message classification, process query, message encapsulation, message decapsulation, and message delivery. Optionally, the Control package may consist of at least one of a method call, a table definition, a conditional statement, an assignment statement, and an expression.
[0077] The Table package can be understood as a model that defines the table lookup logic of the optimized data packet. Optionally, the Table package can be composed of at least one of a table lookup key, a table lookup result, and a table lookup application logic.
[0078] S240: compile the optimized data packet through the system-related backend to obtain the second language file.
[0079] Figure 5 1 is a flowchart of a P4 compiler compiled according to an embodiment of the present invention. Figure 5As shown, the .P4 file represents the first language file, and the IR node represents the optimized data packet. Specifically, the compilation process of the P4 compiler is: parsing the .P4 language file to generate an intermediate IR tree; optimizing the intermediate IR tree to generate an optimized data packet; respectively compiling and emitting the Parser package, Control package, and Table package included in the optimized data packet in C language to obtain a C language file, i.e., the second language file.
[0080] Optionally, the optimizing data packet includes compiling the optimizing data packet through the system-related backend to obtain the second language file, including:
[0081] Compiling the variable definition and / or state included in the Parser package in C language to obtain first C language data;
[0082] Compiling at least one of the method call, table definition, conditional statement, assignment statement and expression included in the Control package in C language to obtain second C language data;
[0083] Compile at least one of the table lookup key, the table lookup result, and the table lookup application logic included in the Table package in C language to obtain third C language data;
[0084] The second language file is obtained based on the first C language data, the second C language data and the third C language data.
[0085] The first C language data can be understood as data obtained by compiling the Parser package in C language. The second C language data can be understood as data obtained by compiling the Control package in C language. The third C language data can be understood as data obtained by compiling the Table package in C language.
[0086] Optionally, the compiling the variable definitions included in the Parser package in C language includes:
[0087] Determine the variable type of each variable definition, and compile each variable definition in C language based on the variable compilation rule corresponding to the variable type,
[0088] The variable type includes at least one of Header type, struct type, Enum type, scalar type, List type and bool type, and the variable compilation rule includes constructing and emitting a C structure definition corresponding to the current variable type, constructing and emitting a C language instance corresponding to the current variable definition, or directly constructing and emitting a C language instance corresponding to the current variable definition.
[0089] In an embodiment of the present invention, the Parser package may include one or more variable definitions.
[0090] Figure 6 1 is a flow chart of variable definition in a C language compiled Parser package according to an embodiment of the present invention. Figure 6 As shown, the process of compiling variable definitions in the Parser package in C language is:
[0091] extracting the variable definitions and determining the variable type of each of the variable definitions;
[0092] In the case where the variable type of the variable definition is a Header type, a struct type or an Enum type, a C structure definition corresponding to the current variable type is constructed and emitted, and a C language instance corresponding to the current variable definition is constructed and emitted;
[0093] In the case that the variable type of the variable definition is a scalar type, a List type or a bool type, a C language instance corresponding to the current variable definition is directly constructed and emitted.
[0094] In the embodiment of the present invention, when the variable type of the variable definition is not any one of the Header type, struct type, Enum type, scalar type, List type and bool type, an error is returned.
[0095] In an embodiment of the present invention, variable definitions are compiled in C language based on variable types and variable compilation rules corresponding to the variable types, thereby achieving personalized compilation of variable definitions of different variable types and ensuring the accuracy of C language compilation of variable definitions of different variable types.
[0096] Figure 7 1 is a flow chart of the states in a C language compiled Parser package provided according to an embodiment of the present invention. Figure 7 As shown, Extract represents the expansion of the original package, Select represents the selection of the package field, and case represents the value matching and state selection. Optionally, the state includes at least one of the expansion of the original package, the selection of the package field, and the value matching and state selection, and the C language compilation of the state included in the Parser package includes:
[0097] Expand and compile the original packet into a RISC-V packet expansion interface, wherein the RISC-V packet expansion interface is the packet expansion interface recorded in the runtime header file of the RISC-V chip backend;
[0098] The packet field selection is compiled into the conditional statement left value of the if statement corresponding to the C language, and the value matching and state selection is compiled into the execution body of the if statement corresponding to the C language.
[0099] In the embodiment of the present invention, the Parser package may include one or more of the states.
[0100] Optionally, compiling the package field selection into a conditional statement left value of an if statement corresponding to the C language includes:
[0101] Declare a variable;
[0102] Call the runtime interface of the RISC-V chip backend to read the selection domain value;
[0103] The selection domain value is assigned to the variable to obtain the left value of the conditional statement of the if statement.
[0104] Figure 8 1 is a flowchart of a C language compiling Control package according to an embodiment of the present invention. Figure 8 As shown, optionally, the C language compiling at least one of the method call, table definition, conditional statement, assignment statement and expression included in the Control package includes:
[0105] For the method call, if the method call is a normal function call, compile the current method call into a C language interface, wherein the C language interface is an interface supported by the RISC-V chip backend;
[0106] For the conditional statement, if the conditional statement is a function, a new temporary variable is generated by calling and the return value of the current function call is assigned to the current new temporary variable, otherwise it is directly translated into C language;
[0107] For the assignment statement, determine the C language left value corresponding to the current assignment statement, construct an assignment relationship, and determine the C language right value corresponding to the current assignment statement;
[0108] The table definition and the expression are directly translated into C language.
[0109] Optionally, the expression includes logical operations and / or arithmetic operations.
[0110] Optionally, the method call includes a common function call and / or Table.Apply. Specifically, the C language compilation of the method call included in the Control package also includes:
[0111] In the case where the method call is Table.Apply, determining a key attribute corresponding to Table.Apply and emitting a C language code based on the key attribute;
[0112] Emit the C language table lookup function call corresponding to Table.Apply, and construct the C language table lookup result processing flow corresponding to Table.Apply.
[0113] Fig. 9 This is a flow chart of compiling a Table package in C language according to an embodiment of the present invention. Fig. 9 As shown, wherein key represents the table lookup key, Action represents the table lookup result, Range_check represents the C language attribute of the current table lookup key, key size represents the length of the table lookup key, Action id represents the result identifier, and actionsize represents the length of the table lookup result. Optionally, the C language compilation of at least one of the table lookup key, table lookup result, and table lookup application logic included in the Table package includes:
[0114] Expand the table lookup application logic in the Table package, and extract the table lookup key and / or the table lookup result;
[0115] For the table lookup key, if the key type of the table lookup key is Range, construct the C language attribute of the current table lookup key; otherwise, count the length of the current table lookup key, determine the matching type of the current table lookup key, and construct the C language table lookup type based on the matching type;
[0116] For the table lookup result, construct a result identifier corresponding to the current table lookup result, and if the current table lookup result has an input parameter, construct a C language input parameter type definition corresponding to the result identifier, and count the length of the current table lookup result.
[0117] In the embodiment of the present invention, when the table lookup application logic is expanded, table attribute data and result attribute description are generated, wherein the table attribute data includes the table lookup key, and the result attribute description includes the table lookup result.
[0118] S250, compile the input second language file by using the modified GCC compiler to obtain a target file.
[0119] The technical solution of the embodiment of the present invention is to compile the first language file through the system-independent front end to obtain an intermediate IR tree; to compile the intermediate IR tree through the system-related middle end to obtain an optimized data packet; and to compile the optimized data packet through the system-related back end to obtain the second language file. This realizes efficient and accurate compilation of the traffic unloading logic recorded in the P4 language into the traffic unloading logic recorded in the C language.
[0120] Fig.10 FIG. 1 is an overall flow chart of a language compilation method provided according to an embodiment of the present invention. Fig.10 As shown, the overall process of the language compilation method is: according to the runtime header file of the RISC-V chip backend and the original GCC compiler, a modified GCC compiler is obtained; the input P4 language file is compiled by the P4 compiler to obtain a C language file; the input C language file is compiled by the modified GCC compiler to obtain an executable file for the RISC-V chip backend. This application uses the C language as an intermediate output, which can more efficiently and accurately map the P4 language to the RISC-V chip backend, and avoids the situation in which the JSON language is used as an intermediate language in the traditional method, and there are too many calls to the runtime interface, thereby simplifying the language compilation process and reducing the complexity of language compilation.
[0121] Embodiment 3
[0122] Fig.11 This is a schematic diagram of the structure of a language compilation device provided in Embodiment 3 of the present invention. Fig.11 As shown, the device includes: an original file acquisition module 310, a first compilation module 320 and a second compilation module 330; wherein,
[0123] The original file acquisition module 310 is used to obtain a first language file, wherein the first language file is a file that records the traffic unloading logic based on the P4 language; the first compilation module 320 is used to compile the input first language file through the P4 compiler to obtain a second language file, wherein the second language file is a file that records the traffic unloading logic based on the C language; the second compilation module 330 is used to compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file of the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend.
[0124] The technical solution of the embodiment of the present invention is to obtain a first language file, wherein the first language file is a file that records the traffic unloading logic based on the P4 language; compile the input first language file through the P4 compiler to obtain a second language file, wherein the second language file is a file that records the traffic unloading logic based on the C language; compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file for the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend. In the process of compiling the P4 language file into an executable file for the RISC-V chip backend, the present application uses the C language file as an intermediate output and calls the runtime header file of the RISC-V chip backend to modify the GCC compiler, so that the traffic unloading logic recorded by the P4 language can be more efficiently and accurately mapped to the RISC-V chip backend, ensuring the accuracy and efficiency of language compilation.
[0125] Optionally, the GCC compiler includes at least one of a cc1 tool, an as tool and a collect2 tool, and the language compilation device further includes a tool modification module, which is used to obtain the assembly instructions recorded in the runtime header file before compiling the input second language file by the modified GCC compiler, and modify the original as tool in the original GCC compiler based on the assembly instructions to obtain the modified as tool, and obtain the modified GCC compiler.
[0126] Optionally, the tool modification module is used to:
[0127] Based on the assembly instruction and the instruction format of the assembly instruction, determining modification reference data, and modifying the original as tool based on the modification reference data to obtain the modified as tool;
[0128] Among them, the instruction format includes an opcode and / or a function code, and the modified reference data includes a matching code, a mask, a register type definition, an immediate type definition, a target register type, a target immediate type, and at least one item of the reverse parsing process corresponding to the target operand type.
[0129] Optionally, the P4 compiler includes at least one of an architecture-independent front end, an architecture-dependent mid-end, and an architecture-dependent back end. The first compilation module 320 includes: a front-end compilation unit, a mid-end compilation unit, and a back-end compilation unit; wherein,
[0130] The front-end compiling unit is used to compile the first language file through the architecture-independent front-end to obtain an intermediate IR tree;
[0131] The mid-end compilation unit is used to compile the intermediate IR tree through the system-related mid-end to obtain an optimized data packet;
[0132] The back-end compilation unit is used to compile the optimized data packet through the system-related back-end to obtain the second language file.
[0133] Optionally, the optimized data packet includes at least one of a Parser package, a Control package, and a Table package, and the backend compilation unit includes: a Parser compilation subunit, a Control compilation subunit, a Table compilation subunit, and a file determination subunit; wherein,
[0134] The Parser compiling subunit is used to compile the variable definition and / or status included in the Parser package in C language to obtain first C language data;
[0135] The Control compilation subunit is used to compile at least one of the method call, table definition, conditional statement, assignment statement and expression included in the Control package into C language to obtain second C language data;
[0136] The Table compiling subunit is used to compile at least one of the table lookup key, the table lookup result and the table lookup application logic included in the Table package in C language to obtain third C language data;
[0137] The file determination subunit is used to obtain the second language file based on the first C language data, the second C language data and the third C language data.
[0138] Optionally, the Parser compilation subunit is used to:
[0139] Determine the variable type of each variable definition, and compile each variable definition in C language based on the variable compilation rule corresponding to the variable type,
[0140] The variable type includes at least one of Header type, struct type, Enum type, scalar type, List type and bool type, and the variable compilation rule includes constructing and emitting a C structure definition corresponding to the current variable type, constructing and emitting a C language instance corresponding to the current variable definition, or directly constructing and emitting a C language instance corresponding to the current variable definition.
[0141] Optionally, the state includes at least one of original packet expansion, packet field selection, and value matching and state selection, and the Parser compilation subunit is used to:
[0142] Expand and compile the original packet into a RISC-V packet expansion interface, wherein the RISC-V packet expansion interface is the packet expansion interface recorded in the runtime header file of the RISC-V chip backend;
[0143] The packet field selection is compiled into the conditional statement left value of the if statement corresponding to the C language, and the value matching and state selection is compiled into the execution body of the if statement corresponding to the C language.
[0144] Optionally, the Control compilation subunit is used to:
[0145] For the method call, if the method call is a normal function call, compile the current method call into a C language interface, wherein the C language interface is an interface supported by the RISC-V chip backend;
[0146] For the conditional statement, if the conditional statement is a function, a new temporary variable is generated by calling and the return value of the current function call is assigned to the current new temporary variable, otherwise it is directly translated into C language;
[0147] For the assignment statement, determine the C language left value corresponding to the current assignment statement, construct an assignment relationship, and determine the C language right value corresponding to the current assignment statement;
[0148] The table definition and the expression are directly translated into C language.
[0149] Optionally, the Table compilation subunit is used to:
[0150] Expand the table lookup application logic in the Table package, and extract the table lookup key and / or the table lookup result;
[0151] For the table lookup key, if the key type of the table lookup key is Range, construct the C language attribute of the current table lookup key; otherwise, count the length of the current table lookup key, determine the matching type of the current table lookup key, and construct the C language table lookup type based on the matching type;
[0152] For the table lookup result, construct a result identifier corresponding to the current table lookup result, and if the current table lookup result has an input parameter, construct a C language input parameter type definition corresponding to the result identifier, and count the length of the current table lookup result.
[0153] The language compilation device provided in the embodiment of the present invention can execute the language compilation method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0154] Embodiment 4
[0155] Fig.12 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or required herein.
[0156] like Fig.12 As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0157] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0158] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as a language compilation method.
[0159] In some embodiments, the language compilation method may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the language compilation method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to execute the language compilation method in any other appropriate manner (e.g., by means of firmware).
[0160] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0161] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0162] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0163] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).
[0164] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0165] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.
[0166] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.
[0167] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A language compilation method, characterized in that: include: Obtain a first language file, wherein the first language file is a file that records traffic offloading logic based on the P4 language; Compiling the first language file input by a P4 compiler to obtain a second language file, wherein the second language file is a file recording the traffic offloading logic based on C language; The input second language file is compiled by the modified GCC compiler to obtain a target file, wherein the target file is an executable file of the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend.
2. The method according to claim 1, characterized in that The GCC compiler includes at least one of a cc1 tool, an as tool, and a collect2 tool. Before the modified GCC compiler is used to compile the input second language file, the method further includes: The assembly instructions recorded in the runtime header file are obtained, and the original as tool in the original GCC compiler is modified based on the assembly instructions to obtain the modified as tool and the modified GCC compiler.
3. The method according to claim 2, characterized in that The step of modifying the original as tool in the original GCC compiler based on the assembly instruction to obtain the modified as tool includes: Based on the assembly instruction and the instruction format of the assembly instruction, determining modification reference data, and modifying the original as tool based on the modification reference data to obtain the modified as tool; Among them, the instruction format includes an opcode and / or a function code, and the modified reference data includes a matching code, a mask, a register type definition, an immediate type definition, a target register type, a target immediate type, and at least one item of the reverse parsing process corresponding to the target operand type.
4. The method according to claim 1, characterized in that: The P4 compiler includes at least one of an architecture-independent front end, an architecture-dependent middle end, and an architecture-dependent back end. The P4 compiler is used to compile the first language file to obtain the second language file, including: Compiling the first language file by the architecture-independent front end to obtain an intermediate IR tree; Compiling the intermediate IR tree through the system-related middle end to obtain an optimized data packet; The optimized data packet is compiled through the system-related backend to obtain the second language file.
5. The method according to claim 4, characterized in that The optimized data packet includes at least one of a Parser package, a Control package, and a Table package, and the optimized data packet is compiled by the system-related backend to obtain the second language file, including: Compiling the variable definition and / or state included in the Parser package in C language to obtain first C language data; Compiling at least one of the method call, table definition, conditional statement, assignment statement and expression included in the Control package in C language to obtain second C language data; Compile at least one of the table lookup key, the table lookup result, and the table lookup application logic included in the Table package in C language to obtain third C language data; The second language file is obtained based on the first C language data, the second C language data and the third C language data.
6. The method according to claim 5, characterized in that The C language compilation of the variable definitions included in the Parser package includes: Determine the variable type of each variable definition, and compile each variable definition in C language based on the variable compilation rule corresponding to the variable type, The variable type includes at least one of Header type, struct type, Enum type, scalar type, List type and bool type, and the variable compilation rule includes constructing and emitting a C structure definition corresponding to the current variable type, constructing and emitting a C language instance corresponding to the current variable definition, or directly constructing and emitting a C language instance corresponding to the current variable definition.
7. The method according to claim 5, characterized in that The state includes at least one of original packet expansion, packet field selection, and value matching and state selection, and the C language compiling of the state included in the Parser packet includes: Expand and compile the original packet into a RISC-V packet expansion interface, wherein the RISC-V packet expansion interface is the packet expansion interface recorded in the runtime header file of the RISC-V chip backend; The packet field selection is compiled into the conditional statement left value of the if statement corresponding to the C language, and the value matching and state selection is compiled into the execution body of the if statement corresponding to the C language.
8. The method according to claim 5, characterized in that The C language compiling of at least one of the method call, table definition, conditional statement, assignment statement and expression included in the Control package comprises: For the method call, if the method call is a normal function call, compile the current method call into a C language interface, wherein the C language interface is an interface supported by the RISC-V chip backend; For the conditional statement, if the conditional statement is a function, a new temporary variable is generated by calling and the return value of the current function call is assigned to the current new temporary variable, otherwise it is directly translated into C language; For the assignment statement, determine the C language left value corresponding to the current assignment statement, construct an assignment relationship, and determine the C language right value corresponding to the current assignment statement; The table definition and the expression are directly translated into C language.
9. The method according to claim 5, characterized in that The step of compiling at least one of the table lookup key, the table lookup result, and the table lookup application logic included in the Table package in C language includes: Expand the table lookup application logic in the Table package, and extract the table lookup key and / or the table lookup result; For the table lookup key, if the key type of the table lookup key is Range, construct the C language attribute of the current table lookup key; otherwise, count the length of the current table lookup key, determine the matching type of the current table lookup key, and construct the C language table lookup type based on the matching type; For the table lookup result, construct a result identifier corresponding to the current table lookup result, and if the current table lookup result has an input parameter, construct a C language input parameter type definition corresponding to the result identifier, and count the length of the current table lookup result.
10. A language compiling device, characterized in that: include: An original file acquisition module, used to acquire a first language file, wherein the first language file is a file recording traffic offloading logic based on the P4 language; A first compiling module, configured to compile the first language file inputted by a P4 compiler to obtain a second language file, wherein the second language file is a file recording the traffic offloading logic based on the C language; The second compilation module is used to compile the input second language file through the modified GCC compiler to obtain a target file, wherein the target file is an executable file of the RISC-V chip backend, and the modified GCC compiler is obtained by modifying the original GCC compiler based on the runtime header file of the RISC-V chip backend.
11. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the language compilation method according to any one of claims 1 to 9.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the language compilation method according to any one of claims 1 to 9 when executed.