A C language source code cross-platform transplantation system and a transplantation method

CN116594687BActive Publication Date: 2026-10-27青岛本原微电子有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202310511500.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-08
Publication Date
2026-10-27
Estimated Expiration
2043-05-08

AI Technical Summary

Technical Problem

源代码中需要修改的内容比较多,工作量比较大,人工转换效率低,且存在错误操作的可能,使用移植系统就很好的解决了这些问题

Benefits of technology

[0026]1、当发现新的移植规则时,本发明无需修改移植系统代码,并重新编译,只需要在规则模块的规则文件中定义新规则即可,兼具易修改性、灵活性和高效性。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116594687B_ABST
    Figure CN116594687B_ABST
Patent Text Reader

Abstract

The application discloses a C language source code cross-platform transplantation system and a transplantation method, and relates to the technical field of computer software development. The system comprises a rule module, a checking module, a C language code analysis module and an execution module. The rule module is used for analyzing a rule file and converting transplantation rules in the rule file into a data structure. The checking module is used for checking whether the C language code file and the rule file exist. If not, an error is reported and the running is terminated. Then, it is checked whether a target file can be created. If not, an error is reported and the running is terminated. The C language code analysis module is used for reading C language file content and performing statement type determination on each C language statement. The execution module is used for performing transplantation operation according to the statement type and the transplantation rule. The transplantation system and the transplantation method disclosed by the application have the advantages of easy modification, flexibility and high efficiency. They not only support bidirectional transplantation between operating systems, but also support bidirectional transplantation between different architecture hardware, and are widely applicable and more universal.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to software porting methods and systems, and particularly to a cross-platform porting system and method for C language source code. Background Technology

[0002] When porting C source code from one platform to another, it's necessary to convert the features of the original platform to methods supported by the new platform. This involves numerous modifications to the source code, a significant workload, and manual conversion is inefficient and prone to errors. Using a porting system effectively solves these problems.

[0003] Traditional conversion systems are only suitable for one-way conversion between two fixed platforms and lack universality. In addition, every time a new conversion rule is added, the source code of the conversion system must be modified and a new version of the conversion system must be compiled, which shows that its scalability is poor. Summary of the Invention

[0004] To address the aforementioned technical problems, this invention provides a cross-platform porting system and method for C language source code, aiming to achieve flexibility, efficiency, and universality.

[0005] To achieve the above objectives, the technical solution of the present invention is as follows:

[0006] A cross-platform porting system for C language source code includes a rules module, a validation module, a C language code parsing module, and an execution module;

[0007] The rule module is used to parse the rule file, convert the migration rules in the rule file into a data structure, and store it in the computer memory for later use;

[0008] The verification module is used to verify whether the C language code file and rule file exist. If they do not exist, an error is reported and the process terminates. It then verifies whether the target file can be created. If not, an error is reported and the process terminates.

[0009] The C language code parsing module is used to read the contents of the C language file and perform statement type determination for each C language statement.

[0010] The execution module is used to obtain the migration rules for the statement type parsed by the C language code parsing module from the rule module; check whether the content to be matched in each migration rule matches the current C language code statement content; if they match, execute the operation of the migration rule and output the executed content to the target file.

[0011] In the above scheme, the rule file defines many migration rules, which describe the content to be matched and the operation to be performed on it.

[0012] In the above scheme, the rule module defines rule types, including new rules, comment rules, replacement rules, and deletion rules.

[0013] In a further technical solution, the newly added rule includes information such as operation type, added content, added location, and execution count.

[0014] In a further technical solution, the annotation rules include operation type, execution object, execution count, matching template, and matching type information.

[0015] In a further technical solution, the replacement rule includes operation type, execution object, execution count, matching template, and target template information.

[0016] In a further technical solution, the deletion rule includes operation type, execution object, execution count, matching template, and matching type information.

[0017] A method for cross-platform porting of C language source code, employing a cross-platform porting system for C language source code as described above, includes the following process:

[0018] When this system is running, the user first needs to specify the rule file, C language file, and the path to the target file after the porting.

[0019] Then, the verification module checks whether the user-specified C language file and rule file exist. If they do not exist, an error is reported and the migration system terminates. Then, it tests whether the target file can be created. If it fails, an error is reported and the migration system terminates.

[0020] The rules module parses the rule file, converting the rules from text descriptions into data structures, namely rule class object data, which are stored in the rule map according to the execution object. The rules module can also provide rules of specified statement types to other modules.

[0021] The C language code parsing module reads the contents of the C language file and performs the following operations on each line: it determines whether the current content is a comment; if it is a comment, it outputs it to the object file; if it is not a comment, it first removes the comments from the content and then determines its type; if the type cannot be determined, it performs statement type analysis based on the next line of the C language file until its statement type is determined; and then it passes the statement and statement type information to the execution module.

[0022] The execution module retrieves migration rules for the statement type from the rules module, filters and retains migration rules whose actual execution count is less than the number of executions, and then performs the following operations on each of these rules:

[0023] Check if the matching template in the rule matches the statement content. If it matches, execute the operation of the rule and output the result to the target file, incrementing the actual execution count of the rule by one; if it does not match, do not process it.

[0024] After the execution module processes the entire C language file, it saves the target file, and the porting system finishes running.

[0025] Through the above technical solution, the C language source code cross-platform porting system and porting method provided by the present invention have the following beneficial effects:

[0026] 1. When a new migration rule is discovered, this invention does not require modification of the migration system code and recompilation. It only requires defining the new rule in the rule file of the rule module, which combines ease of modification, flexibility and efficiency.

[0027] 2. Compared to the limitations of previous porting systems that could only be ported unidirectionally to two fixed platforms, this invention not only supports bidirectional porting between operating systems, but also bidirectional porting between hardware with different architectures, making it more widely applicable and universal. Attached Figure Description

[0028] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.

[0029] Figure 1 This is a schematic diagram of a C language source code cross-platform porting system disclosed in an embodiment of the present invention. Detailed Implementation

[0030] The technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention.

[0031] This invention provides a cross-platform porting system for C language source code, such as... Figure 1 As shown, it includes a rules module, a validation module, a C language code parsing module, and an execution module. In this embodiment, the porting system is written in Java.

[0032] The rules module is used to parse the rules file, convert the migration rules in the rules file into data structures, and store them in the computer memory for later use. The rules file defines many migration rules, which describe the content to be matched and the operations to be performed on it.

[0033] The rule module defines rule types, as shown in Table 1. Rule types include add rules, comment rules, replacement rules, and delete rules. Add rules include information such as operation type, added content, insertion location, and execution count. Comment rules include information such as operation type, execution object, execution count, matching template, and matching type. Replacement rules include information such as operation type, execution object, execution count, matching template, and target template. Delete rules include information such as operation type, execution object, execution count, matching template, and matching type. Wherein, "Operation Type" indicates the operation type of the current rule; "Execution Object" indicates the type of C language statement; "Execution Count" indicates the number of times the rule is executed; "Matching Template" supports regular expressions and represents the template for matching C language code; "Target Template" supports regular expressions and represents how to edit C language code; "Matching Type" indicates whether it is a partial or complete match; and "Insertion Location" indicates the insertion location.

[0034] Table 1 Rule Types

[0035]

[0036]

[0037]

[0038] The verification module is used to verify whether the C language code file and rule file exist. If they do not exist, an error is reported and the process terminates. It then verifies whether the target file can be created. If not, an error is reported and the process terminates.

[0039] The C language code parsing module is used to read the contents of C language files and determine the statement type for each C language statement. The statement type is an element that constitutes C language code, such as macro definitions, global variables, structures, function declarations and definitions.

[0040] The execution module retrieves the migration rules for the statement type parsed by the C language code parsing module from the rules module. It then checks if the content to be matched in each migration rule matches the current C language code statement. If a match is found, the migration rule is executed, and the result is output to the target file. This process is repeated until the end of the C language code file. Finally, the target file content is saved, and the system terminates.

[0041] A method for cross-platform porting of C language source code, using the aforementioned cross-platform porting system for C language source code, includes the following process:

[0042] When this system is running, the user first needs to specify the rule file, C language file, and the path to the target file after the porting.

[0043] Then, the verification module checks whether the user-specified C language file and rule file exist. If they do not exist, an error is reported and the migration system terminates. Then, it tests whether the target file can be created. If it fails, an error is reported and the migration system terminates.

[0044] The rules module parses the rule file, converting the rules from text descriptions into data structures, namely rule class object data, which are stored in the rule map according to the execution object. The rules module provides rules of a specified statement type (i.e. execution object) to other modules.

[0045] The C language code parsing module reads the contents of the C language file and performs the following operations on each line: it determines whether the current content is a comment; if it is a comment, it outputs it to the object file; if it is not a comment, it first removes the comments from the content and then determines its type; if the type cannot be determined, it performs statement type analysis based on the next line of the C language file until its statement type is determined; and then it passes the statement and statement type information to the execution module.

[0046] The execution module retrieves migration rules for the statement type from the rules module, filters and retains migration rules whose actual execution count is less than the "execution count" (the "times" attribute of the rule), and then performs the following operation on each of these rules:

[0047] Check if the matching template in the rule matches the statement content. If it matches, execute the operation of the rule and output the result to the target file, incrementing the actual execution count of the rule by one; if it does not match, do not process it.

[0048] After the execution module processes the entire C language file, it saves the target file, and the porting system finishes running.

[0049] The above description of the disclosed embodiments enables those skilled in the art to make or use the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A method for cross-platform porting of C language source code, comprising a cross-platform porting system for C language source code, characterized in that, The system includes a rules module, a validation module, a C language code parsing module, and an execution module; The rule module is used to parse the rule file, convert the migration rules in the rule file into data structures, and store them in computer memory for later use; The verification module is used to verify whether the C language code file and rule file exist. If they do not exist, an error is reported and the process terminates. It then verifies whether the target file can be created. If not, an error is reported and the process terminates. The C language code parsing module is used to read the contents of the C language file and perform statement type determination for each C language statement. The execution module is used to obtain the migration rules for the statement type parsed by the C language code parsing module from the rule module; check whether the content to be matched in each migration rule matches the current C language code statement content; if they match, execute the operation of the migration rule and output the executed content to the target file. The rules module defines rule types, including add rules, comment rules, replacement rules, and delete rules. Add rules include operation type, added content, added location, and execution count information. Comment rules include operation type, execution object, execution count, matching template, and matching type information. Replacement rules include operation type, execution object, execution count, matching template, and target template information. Delete rules include operation type, execution object, execution count, matching template, and matching type information. The method includes the following steps: When this system is running, the user first needs to specify the rule file, C language file, and the path to the target file after the porting. Then, the verification module checks whether the user-specified C language file and rule file exist. If they do not exist, an error is reported and the migration system terminates. Then, it tests whether the target file can be created. If it fails, an error is reported and the migration system terminates. The rules module parses the rule file, converting the rules from text descriptions into data structures, namely rule class object data, which are stored in the rule map according to the execution object. The rules module can also provide rules of specified statement types to other modules. The C language code parsing module reads the contents of a C language file and performs the following operations on each line of content: it determines whether the current content is a comment; if it is a comment, it outputs it to the object file; if it is not a comment, it first removes the comments from the content and then determines its type. If the type cannot be determined, perform statement type analysis by combining the next line of the C language file until the statement type is determined. Pass the statement and statement type information to the execution module; The execution module retrieves migration rules for the statement type from the rules module, filters and retains migration rules whose actual execution count is less than the number of executions, and then performs the following operations on each of these rules: Check if the matching template in the rule matches the statement content. If it matches, execute the operation of the rule and output the result to the target file, incrementing the actual execution count of the rule by one; if it does not match, do not process it. After the execution module processes the entire C language file, it saves the target file, and the porting system finishes running.

2. The method for cross-platform porting of C language source code according to claim 1, characterized in that, The rule file defines many migration rules, which describe the content to be matched and the operation to be performed on it.

Citation Information

Patent Citations

  • Cross-platform code conversion method and equipment

    CN113535184A