Code conversion method, device and equipment, readable storage medium and program product

By obtaining the syntax tree information of the source code file, the device type of the code node is automatically determined and converted, which solves the problem of low efficiency in traditional methods and achieves efficient code conversion and correct device execution.

CN121233115APending Publication Date: 2025-12-30SUGON INFORMATION IND +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410865661.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-28
Publication Date
2025-12-30

AI Technical Summary

Technical Problem

Traditional code conversion methods are inefficient, especially when converting source code written in OpenACC and Fortran programming languages ​​to run on specific computer components, or when converting source code to code written in a mix of Fortran and HIP C, or vice versa.

Method used

By obtaining the syntax tree information of the source code file, the device type of each code node is determined, and the code is automatically converted according to the device type to generate the device-side file, avoiding manual line-by-line conversion.

Benefits of technology

It improves the efficiency of code conversion, ensures that the code runs correctly on the device, and reduces the time and errors of manual conversion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121233115A_ABST
    Figure CN121233115A_ABST
Patent Text Reader

Abstract

The invention relates to a code conversion method and device, equipment, a readable storage medium and a program product. The method comprises the steps that a to-be-converted source code file is obtained, syntax tree information corresponding to the source code file is obtained according to the source code file, the syntax tree information comprises at least one code node, for each code node, a device type corresponding to the code node is determined, and if the device type is a device end type, the source code file is converted. If yes, code conversion processing is carried out on the target source code corresponding to the code node to obtain a converted code, the converted code is written into a device end file, and the device end file is used for the device end to execute the converted code. By adopting the method, the code conversion efficiency can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computers, and in particular to a code conversion method and device, equipment, readable storage medium and program product. BACKGROUND

[0002] With the development of computer technology, heterogeneous computing emerges as the times require, which refers to running code in parallel in a computing environment composed of computer components of different types and architectures. Traditional heterogeneous computing usually uses Fortran programming language to write code, declares the code region running in parallel through OpenACC (Open Computing Language Acceleration Interface), and thus realizes heterogeneous computing of the code. However, the source code written by OpenACC and Fortran programming language is not applicable on some specific heterogeneous accelerators. At this time, it is necessary to convert the source code into code that can run on specific computer components, that is, to convert the source code into code mixed with Fortran and HIP C language.

[0003] The traditional code conversion method usually converts the source code file line by line manually, and has the problem of low conversion efficiency. SUMMARY

[0004] Therefore, it is necessary to provide a code conversion method, device, equipment, readable storage medium and program product capable of improving the code conversion efficiency in view of the above technical problems.

[0005] In a first aspect, the present application provides a code conversion method for a host end, comprising:

[0006] obtaining a source code file to be converted, and obtaining syntax tree information corresponding to the source code file according to the source code file, the syntax tree information comprising at least one code node;

[0007] For each code node, determining a device type corresponding to the code node;

[0008] If the device type is a device end type, performing code conversion processing on target source code corresponding to the code node to obtain converted code, and writing the converted code into a device end file, the device end file being used for the device end to execute the converted code.

[0009] In the above embodiment, by obtaining the syntax tree information corresponding to the source code file, the target source code that needs to run on the device end can be determined according to each code node in the syntax tree information, and the target source code that needs to run on the device end is converted and processed to automatically obtain the device end file that can run on the device end, avoiding the problem of low conversion efficiency of manually converting the source code file line by line in the traditional technology. The technical solution provided by the present application has higher code conversion efficiency.

[0010] In one of the embodiments, the code node comprises a code label of the target source code, and for each code node, a device type corresponding to the code node is determined, comprising:

[0011] If the code label does not comprise the first preset field, the device type is determined as the host type;

[0012] If the code label comprises the first preset field, the device type is determined as the device type.

[0013] In the above embodiments, the device type corresponding to the code node can be determined by judging whether the first preset field is included in the code label, the method for determining the device type is simple, and the efficiency of code conversion is further improved.

[0014] In one of the embodiments, the method further comprises:

[0015] A code interface is generated according to the target source code, and the code interface is written into the host file, and the code interface is used to access the target source code from the host.

[0016] In the above embodiments, in the case where the device type is the device type, the code interface is generated according to the target source code while the code conversion processing of the target source code is performed, and the code interface is written into the host file, so that when the host file is run on the host, if the code interface is encountered, the corresponding target source code can be called on the device, and the heterogeneous computing of the source code file is effectively realized.

[0017] In one of the embodiments, the method further comprises:

[0018] If the device type is the host type, the target source code is written into the host file, and the host file is used for the host to execute the converted code.

[0019] In the above embodiments, for the target source code of the device type of the host type, the target source code is directly written into the new host file, so that only the target source code of the device type of the host type is included in the host file, and the target source code of the device type of the device is not included, which ensures the correctness of the code included in the host file and avoids the situation that the host still runs the target source code of the device type of the device.

[0020] In one of the embodiments, the syntax tree information corresponding to the source code file is obtained according to the source code file, comprising:

[0021] The codes included in the source code file are preprocessed to obtain at least one line of source code;

[0022] According to each source code, dictionary information corresponding to each source code is determined, and the dictionary information comprises code attribute information corresponding to the source code.

[0023] Generate syntax tree information based on the information from each dictionary.

[0024] In the above embodiments, the code in the source code files is preprocessed before generating the syntax tree information, which makes the source code cleaner and improves the efficiency of generating the syntax tree.

[0025] In one embodiment, generating syntax tree information based on dictionary information includes:

[0026] For a given dictionary information, the source code included in the dictionary information is scanned according to the second preset field to determine the code tags corresponding to the source code;

[0027] Generate code nodes based on the code attribute information and code tags included in the dictionary;

[0028] Generate syntax tree information based on each code node.

[0029] In the above embodiments, a syntax tree is generated based on the preprocessed dictionary information, which makes the source code for generating the syntax tree more standardized and improves the correctness of the final generated syntax tree information.

[0030] In one embodiment, the method further includes:

[0031] Based on the information from each dictionary, determine at least one variable identifier and the variable information corresponding to the variable identifier;

[0032] An index table is generated based on the variable identifiers and variable information.

[0033] In the above embodiments, an index table including multiple variable identifiers and the variable information corresponding to the variable identifiers is generated based on the preprocessed dictionary information, ensuring the accuracy of the index table.

[0034] In one embodiment, the target source code corresponding to the code node is subjected to code transformation processing to obtain transformed code, including:

[0035] If the target source code includes target variables, then the target variable information corresponding to the target variable is retrieved from the index table using the target variable.

[0036] If the target variable information is successfully retrieved, the target source code is converted to obtain the converted code.

[0037] If no target variable information is found, a prompt message will be output, and code conversion processing of the target source code will be prohibited. The prompt message is used to indicate that there is an error in the target source code.

[0038] In the above embodiments, during the code conversion process, the host side performs table lookup verification on the target variables in the target source code based on the index table. The correctness of the target source code is judged by whether the target variable can be found in the index table. The code conversion will continue only if the target variable can be found. If the target variable cannot be found, the code conversion will stop and an error will be indicated in the target source code, thereby improving the correctness of the code conversion.

[0039] Secondly, this application also provides a code conversion apparatus for a host side, comprising:

[0040] The acquisition module is used to acquire the source code file to be converted and to obtain the syntax tree information corresponding to the source code file. The syntax tree information includes at least one code node.

[0041] The determination module is used to determine the device type corresponding to each code node;

[0042] The conversion module is used to perform code conversion on the target source code corresponding to the code node if the device type is device-side type, to obtain the converted code, and write the converted code to the device-side file. The device-side file is used by the device to execute the converted code.

[0043] Thirdly, embodiments of this application provide a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the method described in the first aspect above.

[0044] Fourthly, embodiments of this application provide a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the steps of the method described in the first aspect above.

[0045] Fifthly, this application also provides a computer program product. The computer program product includes a computer program that, when executed by a processor, implements the steps of the method described in the first aspect above.

[0046] The aforementioned code conversion method, apparatus, device, readable storage medium, and program product obtain the source code file to be converted and retrieve the corresponding syntax tree information based on the source code file. The syntax tree information includes at least one code node. For each code node, the device type corresponding to the code node is determined. If the device type is a device-side type, the target source code corresponding to the code node is processed for code conversion to obtain converted code. The converted code is then written to a device-side file, which is used by the device to execute the converted code. In this way, by obtaining the syntax tree information corresponding to the source code file, the target source code that needs to run on the device can be determined based on each code node in the syntax tree information. The target source code that needs to run on the device is then converted, automatically obtaining a device-side file that can run on the device. This avoids the low conversion efficiency problem of manually converting source code files line by line in traditional technologies. The technical solution provided in this application has higher code conversion efficiency. Attached Figure Description

[0047] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0048] Figure 1 This is a diagram illustrating the application environment of the code conversion method in one embodiment;

[0049] Figure 2 This is a flowchart illustrating a code conversion method in one embodiment;

[0050] Figure 3 This is a flowchart illustrating step 202 in another embodiment;

[0051] Figure 4 A block diagram illustrating source code corresponding to code tags in another embodiment;

[0052] Figure 5 This is a flowchart illustrating step 201 in another embodiment;

[0053] Figure 6 This is a flowchart illustrating the process of generating an index table based on dictionary information in another embodiment;

[0054] Figure 7 This is a flowchart illustrating an exemplary code conversion method in one embodiment;

[0055] Figure 8 This is a structural block diagram of a code conversion device in one embodiment;

[0056] Figure 9 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation

[0057] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0058] With the development of computer technology, heterogeneous computing has emerged. Heterogeneous computing refers to the parallel execution of code in a computing environment composed of computer components of different types and architectures, such as CPU (Central Processing Unit), GPU (graphics processing unit), and GPGPU (General-Purpose Graphics Processing Unit).

[0059] To enable heterogeneous computing, OpenACC (Open Computing Language Acceleration Interface) was developed. By writing code in the Fortran programming language and declaring parallel execution regions in the code through OpenACC, heterogeneous computing can be achieved.

[0060] However, source code written in OpenACC and Fortran programming languages ​​is not suitable for some specific heterogeneous accelerators. In this case, it is necessary to convert the source code into code that can run on specific computer components, that is, to convert the source code into code written in a mixed Fortran and HIP C language.

[0061] Therefore, traditional code conversion methods usually involve manually converting source code files line by line, which results in low conversion efficiency.

[0062] In view of this, embodiments of this application provide a code conversion method, apparatus, device, readable storage medium, and program product. The method involves obtaining a source code file to be converted and acquiring the corresponding syntax tree information based on the source code file. The syntax tree information includes at least one code node. For each code node, the device type corresponding to the code node is determined. If the device type is a device-side type, the target source code corresponding to the code node is subjected to code conversion processing to obtain converted code. The converted code is then written to a device-side file, which is used by the device to execute the converted code. Thus, by obtaining the syntax tree information corresponding to the source code file, the target source code that needs to run on the device can be determined based on each code node in the syntax tree information. The target source code that needs to run on the device is then converted, automatically obtaining a device-side file that can run on the device. This avoids the low conversion efficiency problem of manually converting source code files line by line in traditional technologies. The technical solution provided by this application has higher code conversion efficiency.

[0063] The code conversion method provided in this application embodiment can be applied to, for example, Figure 1 The application environment shown is as follows. In this environment, the host 101 and the device 102 are deployed in the same computer device, which can be a server. The host 101 and the device 102 can communicate with each other. The host can be a processor such as a CPU, and the device can be a computer component such as a heterogeneous accelerator that cannot run code written in the OpenACC and Fortran programming languages.

[0064] In one exemplary embodiment, such as Figure 2 As shown, a code conversion method is provided, which can be applied to... Figure 1 Taking host 101 as an example, the explanation includes the following steps 201 to 203. Wherein:

[0065] Step 201: Obtain the source code file to be converted, and obtain the syntax tree information corresponding to the source code file.

[0066] The source code file can be a code file that runs on the host side. The source code file includes unprocessed code. In the embodiments of this application, the code included in the source code file can be code written based on the OpenACC and Fortran programming languages.

[0067] Optionally, the host can obtain the source code file input to the host through an external device; alternatively, the host can obtain a pre-archived source code file from a preset storage location.

[0068] Syntax tree information is a file generated based on the code included in the source code file, corresponding to the source code file. The syntax tree information includes at least one code node, which is a node that makes up the syntax tree information. A code node corresponds to a piece of source code, and the code node can include the attribute information of the source code. The attribute information can include the name of the source code file corresponding to the source code segment, the address of the source code file, the line number of the source code segment in the source code file, etc.

[0069] Regarding the process of obtaining syntax tree information, optionally, the host can generate the syntax tree information corresponding to the source code file itself based on the content of the source code file. For example, the host can input the content of the source code file into a pre-trained syntax tree information generation model to obtain the syntax tree information output by the syntax tree information generation model; optionally, the host can obtain the syntax tree information input to the host through an external device.

[0070] Step 202: For each code node, determine the device type corresponding to the code node.

[0071] Device type includes host type and device type. In this embodiment, if the device type corresponding to the code node is host type, the host can determine that the source code corresponding to the code node needs to be run on the host. If the device type corresponding to the code node is device type, the host can determine that the code corresponding to the code node needs to be run on the device.

[0072] Different code transformation methods are required for source code that runs on different types of devices. Therefore, the host needs to determine the device type corresponding to the code node.

[0073] Regarding the method for determining the device type, optionally, the code node includes a type label, and the host can determine the device type corresponding to the code node based on the type label; optionally, the host can scan the code node based on preset fields. If the code node includes preset fields, the device type is determined to be a device type; if the code node does not include preset fields, the device type is determined to be a host type.

[0074] Step 203: If the device type is a device-side type, then perform code conversion processing on the target source code corresponding to the code node to obtain the converted code, and write the converted code into the device-side file.

[0075] Among them, the device-side files are used by the device to execute the converted code.

[0076] If the device type is device-side type, then the code corresponding to the code node needs to run on the device side. However, the device side is a computer component that cannot run code written in OpenACC and Fortran programming languages. Therefore, the host side needs to convert the target source code corresponding to the code node with device type device-side type. The converted code is written in HIP C language.

[0077] Optionally, the host can input the target source code into a pre-trained code conversion model to obtain the converted code output by the code conversion model; alternatively, the host can convert the target source code according to a preset code conversion rule to obtain the converted code, wherein the preset code conversion rule is determined according to the writing rules of the HIP C language.

[0078] For the converted code, the host can write each converted code into a file on the device side, where the device side file is used to execute the converted code on the device side.

[0079] In the above embodiments, by obtaining the syntax tree information corresponding to the source code file, the target source code that needs to run on the device can be determined according to each code node in the syntax tree information. The target source code that needs to run on the device is converted and processed to automatically obtain the device file that can run on the device. This avoids the problem of low conversion efficiency in the traditional technology of manually converting source code files line by line. The code conversion method, device, equipment, readable storage medium and program product provided in this application have higher efficiency in code conversion.

[0080] In one embodiment, based on the above Figure 2 The illustrated embodiment can be found in [reference]. Figure 3 This embodiment relates to the process of determining the device type corresponding to each code node, where the code node includes code tags from the target source code. For example... Figure 3 As shown, step 202 may include steps 301 and 302.

[0081] Step 301: If the code tag does not include the first preset field, then the device type is determined to be the host type.

[0082] Step 302: If the code tag includes the first preset field, then the device type is determined to be the device end type.

[0083] In this embodiment, the code node includes a code tag corresponding to the target source code. The code tag is used to identify the code attributes of the target source code, as shown below. Figure 4The left frame shows the source code file, and the code tag corresponding to each source code is shown in the right frame. For example, the code tag corresponding to the source code "program main" is "STProgram".

[0084] Code tags can be customized to include specific fields based on the actual application scenario.

[0085] The first preset field is a preset field. In this embodiment of the application, it is possible to determine whether the device type corresponding to the code node is a device type by judging whether the code tag includes the first preset field.

[0086] Reference Figure 4 For example, in this embodiment of the application, the first preset field is set to "Acc". If the code tag does not include "Acc", the host can determine that the device type corresponding to the code node is the host. If the code tag includes "Acc", the host can determine that the device type corresponding to the code node is the device. For example, the code tag corresponding to the target source code "integer:: i" is "STDeclaration". Since the code tag does not include "Acc", the host can determine that the device type corresponding to the code node is the host type. The code tag corresponding to the target source code "!$acc end data" is "STAccDirective". Since the code tag includes "Acc", the host can determine that the device type corresponding to the code stage is the device type.

[0087] In one possible implementation, once the host determines the device type corresponding to each code node, the target source code corresponding to each code node can be converted. Optionally, if the device type is the host type, the host writes the target source code into a host file. In this embodiment, the host file is a newly created file that is different from the source code file. The host file can be used by the host to execute the converted code.

[0088] Optionally, if the device type is a device-side type, the host can generate a code interface based on the target source code while performing code conversion processing on the target source code corresponding to the code node, and write the code interface into the host file. The code interface is used to access the target source code from the host.

[0089] In the above embodiments, the device type corresponding to the code node can be determined by judging whether the code tag includes the first preset field. The method of determining the device type is simple and further improves the efficiency of code conversion.

[0090] In one embodiment, based on the above Figure 2 The illustrated embodiment can be found in [reference]. Figure 5This embodiment involves the process of obtaining the syntax tree information corresponding to the source code file. For example... Figure 5 As shown, step 201 may include steps 501 to 503.

[0091] Step 501: Preprocess the code included in the source file to obtain at least one line of source code.

[0092] In this embodiment of the application, the code included in the source code file is unprocessed code. To facilitate processing, the host can preprocess the code included in the source code file. Preprocessing includes merging multiple lines of code into a single line (one line of code distributed across multiple lines), splitting a single line of code into multiple lines (one line contains multiple lines of code), removing newline characters or whitespace characters from the code, and processing macro statements, etc., to obtain at least one line of processed source code.

[0093] Step 502: Determine the dictionary information corresponding to each source code based on each source code.

[0094] For each line of source code, the host can scan each source code to determine the corresponding dictionary information. This dictionary information includes code attribute information, such as the source file name, the storage address of the source file, the line number of the source code within the source file, the code before preprocessing, the source code content, whether it contains preprocessing directives, whether it is active code, and whether it has undergone code transformation. Figure 4 Taking the source code "program main" in the left frame as an example, refer to Table 1 for the code attribute information corresponding to "program main".

[0095]

[0096] Table 1

[0097] Step 503: Generate syntax tree information based on the dictionary information.

[0098] After the host obtains the dictionary information corresponding to each line of source code, it can generate syntax tree information based on the dictionary information. In one possible implementation, the host can input the dictionary information into a pre-trained node generation model to obtain the code nodes corresponding to each dictionary information output by the node generation model. Then, the code nodes are sequentially assembled into syntax tree information.

[0099] In another possible implementation, for a dictionary of information, the host can scan the source code included in the dictionary information according to a second preset field to determine the code tag corresponding to the source code.

[0100] The second preset field is a predefined field based on different code elements, such as module, program, function, subroutine, integer, etc. Different second preset fields correspond to different code tags, see reference. Figure 4 The host scans each line of source code based on the preset second field. When the first line of source code contains "program", the code tag corresponding to that line of source code is determined to be "STProgram". If the second line of source code does not contain the preset second field, then the source code does not have a corresponding code tag. If the source code of the third to fifth lines contains "integer", then the code tag corresponding to that line of source code is determined to be "STDeclaration", indicating that the source code of that line of source code is a variable declaration. In addition to these code tags, code tags can also include DoLoopStart node (start of loop statement), DoLoopEnd node (end of loop statement), Assignment node (assignment data), STAccDirective node, and STAccComputeConstruct node, etc.

[0101] After determining the code tags corresponding to the source code, the host can generate code nodes based on the code attribute information and code tags included in the dictionary information. In this way, the code nodes can include code attribute information and code tags, and the host can generate syntax tree information based on each code node. In this embodiment, the host can connect each code node according to the line order corresponding to each line of source code. That is, the predecessor node of a code node is the code node corresponding to the previous line of source code of the source code of that code node, and the successor node is the code node corresponding to the next line of source code of the source code.

[0102] In the above embodiments, the code in the source code files is preprocessed before generating the syntax tree information, which makes the source code cleaner and improves the efficiency of generating the syntax tree.

[0103] In one embodiment, based on the above Figure 5 The illustrated embodiment can be found in [reference]. Figure 6 This embodiment involves the process of generating an index table based on dictionary information. For example... Figure 6 As shown, the process may include steps 601 and 602.

[0104] Step 601: Based on the dictionary information, determine at least one variable identifier and the variable information corresponding to the variable identifier.

[0105] Step 602: Generate an index table based on the variable identifiers and variable information.

[0106] In this embodiment of the application, the source code file includes at least one variable, and the execution of the source code file involves operations on the variables included in the source code file. In order to ensure the correctness of the code conversion process, the dictionary information can be scanned to determine at least one variable in the source code file.

[0107] In one possible implementation, the host can scan the source code included in each dictionary information according to a preset syntax format to determine at least one variable in the source code file. For each variable, the host can use the variable name corresponding to each variable as the variable identifier.

[0108] The host can also identify the variable information corresponding to each variable based on the source code. The variable information can include variable type, dimension, name, and scope.

[0109] After obtaining the variable representation and information corresponding to each variable, the host can store the variable representation and information together to generate an index table. Based on the index table, the variable information corresponding to the variable identifier can be queried using the variable identifier.

[0110] In the above embodiments, an index table including multiple variable identifiers and the variable information corresponding to the variable identifiers is generated based on the preprocessed dictionary information, ensuring the accuracy of the index table.

[0111] In one possible implementation, after obtaining the index table, code conversion is performed on the target source code corresponding to the code node to obtain the converted code. During this process, the host can perform a lookup check on the variables in the target source code based on the index table. For example, if the target source code includes a target variable, the host can use the target variable to query the target variable information corresponding to the target variable in the index table. If the target variable information is successfully found, the target source code is converted to obtain the converted code. If the target variable information is not found, a prompt message is output, and code conversion is prohibited for the target source code. The prompt message is used to indicate that there is an error in the target source code.

[0112] In this way, during the code conversion process, the host side performs table lookup verification on the target variables in the target source code based on the index table. The correctness of the target source code is judged by whether the target variable can be found in the index table. The code conversion will continue only if it can be found. If it cannot be found, the code conversion will stop and an error will be indicated in the target source code, thus improving the correctness of the code conversion.

[0113] In one embodiment, refer to Figure 7 This provides an exemplary method for host-side code transformation, which can be applied to... Figure 1 The implementation environment shown.

[0114] Step 701: Preprocess the code included in the source file to obtain at least one line of source code.

[0115] Step 702: Determine the dictionary information corresponding to each source code based on each source code.

[0116] The dictionary information includes code attribute information corresponding to the source code.

[0117] Step 703: For a dictionary information, scan the source code included in the dictionary information according to the second preset field to determine the code tag corresponding to the source code.

[0118] Step 704: Generate code nodes based on the code attribute information and code tags included in the dictionary information.

[0119] Step 705: Generate syntax tree information based on each code node.

[0120] The syntax tree information includes at least one code node, and the code node includes code tags from the target source code.

[0121] Step 706: Based on the dictionary information, determine at least one variable identifier and the variable information corresponding to the variable identifier.

[0122] Step 707: Generate an index table based on the variable identifiers and variable information.

[0123] Step 708: If the code tag does not include the first preset field, then the device type is determined to be the host type.

[0124] Step 709: If the code tag includes the first preset field, then the device type is determined to be the device end type.

[0125] Step 710: If the device type is host-side type, then write the target source code into the host-side file.

[0126] The host-side file is used to execute the converted code on the host side.

[0127] Step 711: If the device type is a device-side type, then perform code conversion processing on the target source code corresponding to the code node to obtain the converted code, and write the converted code into the device-side file.

[0128] Among them, the device-side files are used by the device to execute the converted code.

[0129] Step 712: Generate a code interface based on the target source code and write the code interface into a host file.

[0130] The code interface is used to access the target source code from the host.

[0131] Step 713: If the target source code includes a target variable, then use the target variable to query the target variable information corresponding to the target variable in the index table.

[0132] Step 714: If the target variable information is successfully retrieved, the target source code is converted to obtain the converted code.

[0133] Step 715: If no target variable information is found, output a prompt message and prohibit code conversion processing of the target source code.

[0134] The error message is used to indicate that there are errors in the target source code.

[0135] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.

[0136] Based on the same inventive concept, this application also provides a code conversion apparatus for implementing the code conversion method described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more code conversion apparatus embodiments provided below can be found in the limitations of the code conversion method described above, and will not be repeated here.

[0137] In one exemplary embodiment, such as Figure 8 As shown, a code conversion device is provided for use on a host side, comprising: an acquisition module 801, a determination module 802, and a conversion module 803, wherein:

[0138] The acquisition module 801 is used to acquire the source code file to be converted, and to acquire the syntax tree information corresponding to the source code file based on the source code file. The syntax tree information includes at least one code node.

[0139] The determining module 802 is used to determine the device type corresponding to each code node;

[0140] The conversion module 803 is used to perform code conversion processing on the target source code corresponding to the code node if the device type is a device-side type, to obtain the converted code, and to write the converted code into a device-side file, wherein the device-side file is used by the device to execute the converted code.

[0141] In one embodiment, the code node includes code tags of the target source code, and the determining module 802 includes:

[0142] A host-side determination unit is used to determine that the device type is a host-side type if the code tag does not include a first preset field;

[0143] The device-side determination unit is configured to determine the device type as the device-side type if the code tag includes the first preset field.

[0144] In one embodiment, the apparatus further includes:

[0145] The host-side file writing module is used to generate a code interface based on the target source code and write the code interface into a host-side file. The code interface is used to access the target source code from the host.

[0146] In one embodiment, the apparatus further includes:

[0147] An interface writing module is used to write the target source code into a host file if the device type is the host type. The host file is used by the host to execute the converted code.

[0148] In one embodiment, the acquisition module 801 includes:

[0149] A preprocessing unit is used to preprocess the code included in the source code file to obtain at least one line of source code;

[0150] A dictionary unit is used to determine dictionary information corresponding to each of the source codes based on each of the source codes, wherein the dictionary information includes code attribute information corresponding to the source code;

[0151] The tree generation unit is used to generate the syntax tree information based on the dictionary information.

[0152] In one embodiment, the tree generation unit is also used to perform the following steps:

[0153] For a given dictionary information, the source code included in the dictionary information is scanned according to a second preset field to determine the code tag corresponding to the source code;

[0154] The code node is generated based on the code attribute information and the code tag included in the dictionary information;

[0155] The syntax tree information is generated based on each of the code nodes.

[0156] In one embodiment, the acquisition module 801 further includes:

[0157] The variable determination unit is used to determine at least one variable identifier and the variable information corresponding to the variable identifier based on the dictionary information.

[0158] The index table generation unit is used to generate an index table based on the variable identifiers and variable information.

[0159] In one embodiment, the conversion module 803 further includes:

[0160] A query unit is used to query the target variable information corresponding to the target variable in the index table if the target source code includes a target variable;

[0161] The successful query unit is used to perform code conversion processing on the target source code if the target variable information is successfully found, so as to obtain the converted code;

[0162] The failure query unit is used to output a prompt message if the target variable information is not found, and to prohibit code conversion processing of the target source code. The prompt message is used to indicate that there is an error in the target source code.

[0163] Each module in the aforementioned code conversion device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the operations corresponding to each module.

[0164] In one exemplary embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows: Figure 9As shown, this computer device includes a processor, memory, input / output interfaces (I / O), and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores code conversion data. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communication with external terminals via a network connection. When the computer program is executed by the processor, it implements a code conversion method.

[0165] Those skilled in the art will understand that Figure 9 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 computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0166] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to perform the following steps:

[0167] Obtain the source code file to be converted, and obtain the syntax tree information corresponding to the source code file based on the source code file. The syntax tree information includes at least one code node.

[0168] For each code node, determine the device type corresponding to that code node;

[0169] If the device type is a device-side type, then the target source code corresponding to the code node is subjected to code conversion processing to obtain converted code, and the converted code is written into a device-side file, which is used by the device to execute the converted code.

[0170] In one embodiment, the code node includes a code tag for the target source code, and the processor further performs the following steps when executing the computer program:

[0171] If the code tag does not include the first preset field, then the device type is determined to be a host type;

[0172] If the code tag includes the first preset field, then the device type is determined to be the device end type.

[0173] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0174] A code interface is generated based on the target source code, and the code interface is written into a host file. The code interface is used to access the target source code from the host.

[0175] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0176] If the device type is the host type, then the target source code is written into the host file, which is used by the host to execute the converted code.

[0177] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0178] The source code file is preprocessed to obtain at least one line of source code;

[0179] Based on each of the source codes, determine the dictionary information corresponding to each of the source codes, wherein the dictionary information includes the code attribute information corresponding to the source code;

[0180] The syntax tree information is generated based on the dictionary information.

[0181] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0182] For a given dictionary information, the source code included in the dictionary information is scanned according to a second preset field to determine the code tag corresponding to the source code;

[0183] The code node is generated based on the code attribute information and the code tag included in the dictionary information;

[0184] The syntax tree information is generated based on each of the code nodes.

[0185] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0186] Based on the dictionary information, at least one variable identifier and the variable information corresponding to the variable identifier are determined;

[0187] An index table is generated based on the variable identifiers and variable information described above.

[0188] In one embodiment, the processor, when executing a computer program, also performs the following steps:

[0189] If the target source code includes a target variable, then the target variable information corresponding to the target variable is queried in the index table using the target variable.

[0190] If the target variable information is successfully retrieved, the target source code is converted to obtain the converted code.

[0191] If the target variable information is not found, a prompt message will be output, and code conversion processing of the target source code will be prohibited. The prompt message is used to indicate that there is an error in the target source code.

[0192] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, the computer program performing the following steps when executed by a processor:

[0193] Obtain the source code file to be converted, and obtain the syntax tree information corresponding to the source code file based on the source code file. The syntax tree information includes at least one code node.

[0194] For each code node, determine the device type corresponding to that code node;

[0195] If the device type is a device-side type, then the target source code corresponding to the code node is subjected to code conversion processing to obtain converted code, and the converted code is written into a device-side file, which is used by the device to execute the converted code.

[0196] In one embodiment, the code node includes a code tag for the target source code, and the computer program, when executed by a processor, further performs the following steps:

[0197] If the code tag does not include the first preset field, then the device type is determined to be a host type;

[0198] If the code tag includes the first preset field, then the device type is determined to be the device end type.

[0199] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0200] A code interface is generated based on the target source code, and the code interface is written into a host file. The code interface is used to access the target source code from the host.

[0201] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0202] If the device type is the host type, the target source code is written into the host file, which is used by the host to execute the converted code.

[0203] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0204] The source code file is preprocessed to obtain at least one line of source code;

[0205] Based on each of the source codes, determine the dictionary information corresponding to each of the source codes, wherein the dictionary information includes the code attribute information corresponding to the source code;

[0206] The syntax tree information is generated based on the dictionary information.

[0207] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0208] For a given dictionary information, the source code included in the dictionary information is scanned according to a second preset field to determine the code tag corresponding to the source code;

[0209] The code node is generated based on the code attribute information and the code tag included in the dictionary information;

[0210] The syntax tree information is generated based on each of the code nodes.

[0211] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0212] Based on the dictionary information, at least one variable identifier and the variable information corresponding to the variable identifier are determined;

[0213] An index table is generated based on the variable identifiers and variable information.

[0214] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0215] If the target source code includes a target variable, then the target variable information corresponding to the target variable is queried in the index table using the target variable.

[0216] If the target variable information is successfully retrieved, the target source code is converted to obtain the converted code.

[0217] If the target variable information is not found, a prompt message will be output, and code conversion processing of the target source code will be prohibited. The prompt message is used to indicate that there is an error in the target source code.

[0218] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, performs the following steps:

[0219] Obtain the source code file to be converted, and obtain the syntax tree information corresponding to the source code file based on the source code file. The syntax tree information includes at least one code node.

[0220] For each code node, determine the device type corresponding to that code node;

[0221] If the device type is a device-side type, then the target source code corresponding to the code node is subjected to code conversion processing to obtain converted code, and the converted code is written into a device-side file, which is used by the device to execute the converted code.

[0222] In one embodiment, the code node includes a code tag for the target source code, and the computer program, when executed by a processor, further performs the following steps:

[0223] If the code tag does not include the first preset field, then the device type is determined to be a host type;

[0224] If the code tag includes the first preset field, then the device type is determined to be the device end type.

[0225] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0226] A code interface is generated based on the target source code, and the code interface is written into a host file. The code interface is used to access the target source code from the host.

[0227] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0228] If the device type is the host type, the target source code is written into the host file, which is used by the host to execute the converted code.

[0229] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0230] The source code file is preprocessed to obtain at least one line of source code;

[0231] Based on each of the source codes, determine the dictionary information corresponding to each of the source codes, wherein the dictionary information includes the code attribute information corresponding to the source code;

[0232] The syntax tree information is generated based on the dictionary information.

[0233] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0234] For a given dictionary information, the source code included in the dictionary information is scanned according to a second preset field to determine the code tag corresponding to the source code;

[0235] The code node is generated based on the code attribute information and the code tag included in the dictionary information;

[0236] The syntax tree information is generated based on each of the code nodes.

[0237] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0238] Based on the dictionary information, at least one variable identifier and the variable information corresponding to the variable identifier are determined;

[0239] An index table is generated based on the variable identifiers and variable information.

[0240] In one embodiment, when the computer program is executed by a processor, it also performs the following steps:

[0241] If the target source code includes a target variable, then the target variable information corresponding to the target variable is queried in the index table using the target variable.

[0242] If the target variable information is successfully retrieved, the target source code is converted to obtain the converted code.

[0243] If the target variable information is not found, a prompt message will be output, and code conversion processing of the target source code will be prohibited. The prompt message is used to indicate that there is an error in the target source code.

[0244] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.

[0245] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium, and when executed, it can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.

[0246] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.

[0247] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.

Claims

1. A transcoding method, characterized by, The method for the host side comprises: acquiring a source code file to be converted, and acquiring syntax tree information corresponding to the source code file according to the source code file, the syntax tree information comprising at least one code node; for each code node, determining a device type corresponding to the code node; if the device type is a device side type, performing code conversion processing on target source code corresponding to the code node to obtain converted code, and writing the converted code into a device side file, the device side file being used for the device side to execute the converted code.

2. The method of claim 1, wherein, The code node comprises a code label of the target source code, and the determination of the device type corresponding to the code node comprises: if the code label does not comprise a first preset field, the device type is determined to be a host side type; if the code label comprises the first preset field, the device type is determined to be the device side type.

3. The method of claim 2, wherein, The method further comprises: generating a code interface according to the target source code, and writing the code interface into a host side file, the code interface being used for accessing the target source code from the host side.

4. The method of claim 2, wherein, The method further comprises: if the device type is the host side type, writing the target source code into a host side file, the host side file being used for the host side to execute the converted code.

5. The method of claim 1, wherein, The acquisition of the syntax tree information corresponding to the source code file comprises: preprocessing code included in the source code file to obtain at least one line of source code; determining dictionary information corresponding to each source code according to each source code, the dictionary information comprising code attribute information corresponding to the source code; generating the syntax tree information according to each dictionary information.

6. The method of claim 5, wherein, The generation of the syntax tree information according to each dictionary information comprises: for one dictionary information, scanning the source code included in the dictionary information according to a second preset field to determine a code label corresponding to the source code; generating the code node according to the code attribute information included in the dictionary information and the code label; generating the syntax tree information according to each code node.

7. The method of claim 5, wherein, The method further comprises: determining at least one variable identifier and variable information corresponding to the variable identifier according to each dictionary information; generating an index table according to each variable identifier and each variable information.

8. The method of claim 7, wherein, The code conversion processing on the target source code corresponding to the code node to obtain the converted code comprises: if a target variable is included in the target source code, querying target variable information corresponding to the target variable in the index table by using the target variable; if the target variable information is successfully queried, performing code conversion processing on the target source code to obtain the converted code; if the target variable information is not queried, outputting prompt information and prohibiting the code conversion processing on the target source code, the prompt information being used for prompting that the target source code has an error.

9. A transcoding device, characterized by The device for the host side comprises: An acquisition module is configured to acquire a source code file to be converted, and acquire syntax tree information corresponding to the source code file according to the source code file, the syntax tree information including at least one code node; A determination module is configured to determine, for each code node, a device type corresponding to the code node; A conversion module is configured to, if the device type is a device end type, perform code conversion processing on target source code corresponding to the code node, to obtain converted code, and write the converted code into a device end file, the device end file being used to execute the converted code on a device end. 10.A computer device, comprising a memory and a processor, wherein the memory stores a computer program, and the computer device is configured to perform the method according to any one of claims 1-9. The processor executes the computer program to implement the steps of the method in any one of claims 1 to 8.

11. A computer readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 8.

12. A computer program product comprising a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 8. The computer program is executed by the processor to implement the steps of the method in any one of claims 1 to 8.