Cross-platform code conversion method and device
In a cross-platform code conversion method, a conversion device obtains first source code that is configured to run on a first platform. The conversion device performs syntactic analysis on the first source code to generate a syntax tree corresponding to the first source code, and identifies a to-be-converted syntax block in the syntax tree according to a syntax rule provided by a rule library. The conversion device converts the to-be-converted syntax block according to a conversion rule provided by the rule library to obtain a converted syntax block. The conversion device then generates, based on the obtained converted syntax block, second source code for running on a second platform.
1 . A cross-platform code conversion method performed by a code conversion device, comprising:
obtaining first source code configured for running on a first platform having a first processor architecture;
performing syntactic analysis on the first source code to generate a syntax tree of the first source code;
analyzing the syntax tree of the first source code according to a syntax rule provided by a rule library to identify a to-be-converted syntax block;
analyzing semantics of the to-be-migrated syntax block to obtain a constraint;
selecting a conversion rule from the rule library, based on the constraint, to convert the to-be-migrated syntax block, the rule library comprising:
a built-in function syntax rule and a built-in function conversion rule corresponding to the built-in function syntax rule;
an inline assembly code syntax rule and an inline assembly code conversion rule corresponding to the inline assembly code syntax rule; and
an intrinsic header file syntax rule and an intrinsic header file conversion rule corresponding to the intrinsic header file syntax rule;
converting the to-be-converted syntax block according to a conversion rule provided by the rule library to obtain a converted syntax block; and
generating, based on the converted syntax block, second source code for running on a second platform having a second processor architecture, wherein the second processor architecture is different from the first processor architecture, and the second processor architecture is an ARM architecture.
2 . The method according to claim 1 , wherein the step of converting the to-be-converted syntax block according to the conversion rule comprises:
analyzing the to-be-converted syntax block to obtain a semantic condition of the to-be-converted syntax block;
selecting, from the rule library based on the semantic condition of the to-be-converted syntax block, a conversion rule that matches the semantic condition of the to-be-converted syntax block; and
modifying the to-be-converted syntax block to generate the converted syntax block.
3 . The method according to claim 1 , wherein the to-be-converted syntax block in the syntax tree of the first source code comprises a built-in function syntax block, an inline assembly code syntax block, or an intrinsic header file syntax block.
4 . The method according to claim 1 , wherein the built-in function syntax rule comprises a first syntax structure of the built-in function syntax block, a name of a built-in function corresponding to the built-in function syntax block, and a first attribute of the built-in function, the first syntax structure indicates syntax composition of the built-in function syntax block, and the first attribute indicates that the built-in function is called by a user file;
wherein the step of analyzing the syntax tree of the first source code to identify the to-be-converted syntax block comprises:
identifying the built-in function syntax block based on the first syntax structure of the built-in function syntax block, the name of the built-in function corresponding to the built-in function syntax block, and the first attribute of the built-in function; and
wherein the step of converting the to-be-converted syntax block comprises:
analyzing the built-in function syntax block to obtain a semantic condition of the built-in function syntax block;
selecting, from the rule library based on the semantic condition of the built-in function syntax block, a built-in function conversion rule that matches the semantic condition of the built-in function syntax block; and
modifying the built-in function syntax block to obtain the converted syntax block.
5 . The method according to claim 1 , wherein the inline assembly code syntax rule comprises a second syntax structure of the inline assembly code syntax block and a second attribute of inline assembly code corresponding to the inline assembly code syntax block, the second syntax structure indicates syntax composition of the embedded assembly code syntax block, and the second attribute indicates that the inline assembly code is comprised in the user file;
wherein the step of analyzing the syntax tree of the first source code to identify the to-be-converted syntax block comprises:
identifying the inline assembly code syntax block based on the second syntax structure of the inline assembly code syntax block and the second attribute of the inline assembly code corresponding to the inline assembly code syntax block; and
wherein the step of converting the to-be-converted syntax block comprises:
analyzing the inline assembly code syntax block to obtain a semantic condition of the inline assembly code syntax block;
selecting, from the rule library based on the semantic condition of the inline assembly code syntax block, an inline assembly code conversion rule that matches the semantic condition of the inline assembly code syntax block; and
modifying the inline assembly code syntax block to generate the converted syntax block.
6 . The method according to claim 1 , wherein the intrinsic header file syntax rule comprises a third syntax structure of the intrinsic header file syntax block, and the third syntax structure indicates syntax composition of the intrinsic header file syntax block;
wherein the step of analyzing the syntax tree comprises:
identifying the intrinsic header file syntax block based on the third syntax structure of the intrinsic header file syntax block; and
wherein the step of converting the to-be-converted syntax block to obtain the converted syntax block comprises:
analyzing the intrinsic header file syntax block to obtain a semantic condition of the intrinsic header file syntax block;
selecting, from the rule library based on the semantic condition of the intrinsic header file syntax block, an intrinsic header file conversion rule that matches the semantic condition of the intrinsic header file syntax block, and
modifying the intrinsic header file syntax block to generate the converted syntax block.
7 . The method according to claim 1 , wherein the rule library further comprises a built-in function association rule and a built-in function association conversion rule corresponding to the built-in function association rule, the built-in function association rule comprises searching for a built-in function association syntax block associated with the built-in function syntax block, and wherein the step of converting the to-be-converted syntax block to obtain the converted syntax block comprises:
determining that the to-be-converted syntax block is the built-in function syntax block;
determining the built-in function association syntax block according to the built-in function association rule, wherein the built-in function association syntax block is a declaration of the built-in function or a definition of the built-in function;
selecting, from the rule library based on a semantic condition of the built-in function association syntax block, a built-in function association conversion rule that matches the semantic condition of the built-in function association syntax block; and
modifying the built-in function association syntax block to generate the converted syntax block.
8 . The method according to claim 1 , wherein the rule library further comprises an inline assembly code association rule and an inline assembly code association conversion rule corresponding to the inline assembly code association rule, the inline assembly code association rule comprises searching for an inline assembly code association syntax block associated with the inline assembly code syntax block, and
wherein the step of converting the to-be-converted syntax block to obtain the converted syntax block comprises:
determining that the to-be-converted syntax block is the inline assembly code syntax block;
determining the inline assembly code association syntax block according to the inline assembly code association rule, wherein the inline assembly code association syntax block is a definition of a variable or a declaration of a function in the inline assembly code;
selecting, from the rule library based on a semantic condition of the inline assembly code association syntax block, an inline assembly code association conversion rule that matches the semantic condition of the inline assembly code association syntax block; and
modifying the inline assembly code association syntax block to generate the converted syntax block.
9 . The method according to claim 4 , wherein before generating the second source code, the method further comprises:
generating a semi-structured result file based on the to-be-converted syntax block and the converted syntax block, wherein the semi-structured result file comprises a location of the to-be-converted syntax block in the first source code and source code corresponding to the converted syntax block.
10 . The method according to claim 9 , further comprising:
receiving a modification made by a user to the semi-structured result file; and
generating, based on a semi-structured result file obtained after the modification made by the user, the second source code for running on the second platform.
11 . A cross-platform code conversion apparatus comprising:
a memory storing instructions, and
at least one processor in communication with the memory, the at least one processor configured, upon execution of the instructions, to perform the following steps:
obtaining first source code configured for running on a first platform having a first processor architecture;
performing syntactic analysis on the first source code to generate a syntax tree of the first source code;
analyzing the syntax tree of the first source code according to a syntax rule provided by a rule library, and identifying a to-be-converted syntax block;
analyzing semantics of the to-be-migrated syntax block to obtain a constraint;
selecting a conversion rule from the rule library, based on the constraint, to convert the to-be-migrated syntax block, the rule library comprising:
a built-in function syntax rule and a built-in function conversion rule corresponding to the built-in function syntax rule;
an inline assembly code syntax rule and an inline assembly code conversion rule corresponding to the inline assembly code syntax rule; and
an intrinsic header file syntax rule and an intrinsic header file conversion rule corresponding to the intrinsic header file syntax rule;
converting the to-be-converted syntax block according to a conversion rule provided by the rule library, to obtain a converted syntax block; and
generating, based on the converted syntax block, second source code for running on a second platform having a second processor architecture, wherein the second processor architecture is different from the first processor architecture, and the second processor architecture is an ARM architecture.
12 . The apparatus according to claim 11 , wherein the operation of converting the to-be-converted syntax block comprise:
analyzing the to-be-converted syntax block to obtain a semantic condition of the to-be-converted syntax block;
selecting, from the rule library based on the semantic condition of the to-be-converted syntax block, a conversion rule that matches the semantic condition of the to-be-converted syntax block; and
modifying the to-be-converted syntax block to generate the converted syntax block.
13 . The apparatus according to claim 11 , wherein the to-be-converted syntax block in the syntax tree of the first source code comprises a built-in function syntax block, an inline assembly code syntax block, or an intrinsic header file syntax block.
14 . The apparatus according to claim 11 , wherein the built-in function syntax rule comprises a first syntax structure of the built-in function syntax block, a name of a built-in function corresponding to the built-in function syntax block, and a first attribute of the built-in function, the first syntax structure indicates syntax composition of the built-in function syntax block, and the first attribute indicates that the built-in function is called by a user file; and
wherein the operation of analyzing the syntax tree comprises:
identifying the built-in function syntax block based on the first syntax structure of the built-in function syntax block, the name of the built-in function corresponding to the built-in function syntax block, and the first attribute of the built-in function; and
wherein the operation of converting the to-be-converted syntax box comprises:
analyzing the built-in function syntax block to obtain a semantic condition of the built-in function syntax block;
selecting, from the rule library based on the semantic condition of the built-in function syntax block, a built-in function conversion rule that matches the semantic condition of the built-in function syntax block; and
modifying the built-in function syntax block to generate the converted syntax block.
15 . The apparatus according to claim 11 , wherein the inline assembly code syntax rule comprises a second syntax structure of the inline assembly code syntax block and a second attribute of inline assembly code corresponding to the inline assembly code syntax block, the second syntax structure indicates syntax composition of the inline assembly syntax block, and the second attribute indicates that the inline assembly code is comprised in the user file; and
wherein the operation of analyzing the syntax tree comprises:
identifying the inline assembly code syntax block based on the second syntax structure of the inline assembly code syntax block and the second attribute of the inline assembly code corresponding to the inline assembly code syntax block; and
wherein the operation of converting the to-be-converted syntax block comprises:
analyzing the inline assembly code syntax block to obtain a semantic condition of the inline assembly code syntax block;
selecting, from the rule library based on the semantic condition of the inline assembly code syntax block, an inline assembly code conversion rule that matches the semantic condition of the inline assembly code syntax block; and
modifying the inline assembly code syntax block to generate the converted syntax block.
16 . The apparatus according to claim 11 , wherein the intrinsic header file syntax rule comprises a third syntax structure of the intrinsic header file syntax block, and the third syntax structure indicates syntax composition of the intrinsic header file syntax block; and
wherein the operation of analyzing the syntax tree comprises:
identifying the intrinsic header file syntax block based on the third syntax structure of the intrinsic header file syntax block; and
wherein the operation of converting the to-be-converted syntax block comprise:
analyzing the intrinsic header file syntax block to obtain a semantic condition of the intrinsic header file syntax block;
selecting, from the rule library based on the semantic condition of the intrinsic header file syntax block, an intrinsic header file conversion rule that matches the semantic condition of the intrinsic header file syntax block; and
modifying the intrinsic header file syntax block, to generate the converted syntax block.
17 . The apparatus according to a claim 11 , wherein the rule library further comprises a built-in function association rule and a built-in function association conversion rule corresponding to the built-in function association rule, and the built-in function association rule comprises searching for a built-in function association syntax block associated with the built-in function syntax block; and
wherein the operation of converting the to-be-converted syntax block comprise:
determining that the to-be-converted syntax block is the built-in function syntax block, and determining the built-in function association syntax block according to the built-in function association rule, wherein the built-in function association syntax block is a declaration of the built-in function or a definition of the built-in function;
selecting, from the rule library based on a semantic condition of the built-in function association syntax block, a built-in function association conversion rule that matches the semantic condition of the built-in function association syntax block; and
modifying the built-in function association syntax block to generate the converted syntax block.
18 . A non-transitory computer-readable media storing computer instructions that configure at least one processor, upon execution of the instructions, to perform the following steps:
obtaining first source code configured to run on a first platform having a first processor architecture;
performing syntactic analysis on the first source code to generate a syntax tree of the first source code;
analyzing the syntax tree of the first source code according to a syntax rule provided by a rule library, to identify a to-be-converted syntax block;
analyzing semantics of the to-be-migrated syntax block to obtain a constraint;
selecting a conversion rule from the rule library, based on the constraint, to convert the to-be-migrated syntax block, the rule library comprising:
a built-in function syntax rule and a built-in function conversion rule corresponding to the built-in function syntax rule;
an inline assembly code syntax rule and an inline assembly code conversion rule corresponding to the inline assembly code syntax rule; and
an intrinsic header file syntax rule and an intrinsic header file conversion rule corresponding to the intrinsic header file syntax rule;
converting the to-be-converted syntax block according to a conversion rule provided by the rule library to obtain a converted syntax block; and
generating, based on the converted syntax block, second source code for running on a second platform having a second processor architecture, wherein the second processor architecture is different from the first processor architecture, and the second processor architecture is an ARM architecture.