Source code verification method and non-transitory computer readable storage medium device
By generating mapping files and determining whether the calls conform to the pagination switching writing format, the problem that the C language compiler for small devices cannot automatically check the source code format was solved, ensuring the stability of the 8051 hardware and product quality, and improving development efficiency.
Patent Information
- Application Number
- CN202210288737.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-22
- Publication Date
- 2026-07-24
- Estimated Expiration
- 2042-03-22
AI Technical Summary
When developing 8051 source code using a small device C language compiler, the compiler cannot automatically check whether the source code conforms to the pagination switching writing format, which may cause the hardware program to freeze or other major problems, affecting product quality.
The compiler generates mapping files, obtains target function information and call information, determines whether the call conforms to the pagination switching writing format, and generates a verification failure result when it does not conform, ensuring that the necessary call keywords are added to the high-order source code file.
It effectively avoids hardware freezing caused by incorrect or missing keyword calls, improves source code development efficiency and product quality, and provides a fast verification mechanism to help programmers correct errors.
Smart Images

Figure CN116820419B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to source code verification, and more particularly to a source code verification method based on a pagination-switching writing format and a non-transitory computer-readable storage medium device. Background Technology
[0002] The Small Device C Compiler (SDCC) is an open-source tool specifically designed for developing 8051 source code. The SDCC follows a predefined format: if a function is called across pages, the keyword "__banked" must be added to both the definition of the called function and the caller's declaration. However, when developing 8051 source code using the SDCC, it does not automatically check for violations of this format. Therefore, even if a violation occurs, no compilation warnings or errors will be generated. This can lead to hardware crashes or other serious problems when executing erroneous source code, ultimately affecting the quality of 8051 products. Summary of the Invention
[0003] This invention provides a source code verification method. In one embodiment, the source code verification method includes: using a compiler to generate a mapping file and at least one low-level source code file based on at least one high-level source code file; obtaining target function information from the mapping file, wherein the target function information includes the target function name, the target module name to which the target function name belongs, and a first storage area where the entry address of the target function name is located; finding the corresponding target source code file from the at least one low-level source code file according to the target module name; obtaining a first return instruction corresponding to the target function name from the target source code file; traversing the at least one low-level source code file to obtain at least one calling module name that calls the target function name and a second return instruction corresponding to the at least one calling function name; obtaining a second storage area where the corresponding at least one calling module name is located from the mapping file; determining whether the call of each calling function name to the target function name conforms to the paging switching writing format based on the first storage area, the first return instruction, each second storage area, and each second return instruction; and generating a verification failure result when it is determined that the call does not conform to the paging switching writing format.
[0004] The present invention also provides a non-transitory computer-readable storage medium device. In one embodiment, the non-transitory computer-readable storage medium device stores at least one program. When an electronic device loads and executes at least one program stored in a non-transitory computer-readable storage medium, the at least one program causes the electronic device to perform the following steps: using a compiler to generate a mapping file and at least one low-level source code file based on at least one high-level source code file; obtaining target function information from the mapping file, wherein the target function information includes the target function name, the target module name to which the target function name belongs, and a first storage area where the entry address of the target function name is located; finding the corresponding target source code file from the at least one low-level source code file based on the target module name; obtaining a first return instruction corresponding to the target function name from the target source code file; traversing the at least one low-level source code file to obtain at least one calling module name that calls the target function name and a second return instruction corresponding to the at least one calling function name; obtaining a second storage area where the corresponding at least one calling module name is located from the mapping file; determining whether the call of each calling function name to the target function name conforms to the paging switching writing format based on the first storage area, the first return instruction, each second storage area, and each second return instruction; and generating a verification failure result when it is determined that the call does not conform to the paging switching writing format.
[0005] The detailed features and advantages of the present invention are described below in specific embodiments. The content is sufficient to enable any person skilled in the art to understand the technical content of the present invention and implement it accordingly. Based on the content disclosed in this specification, the scope of the patent application, and the drawings, any person skilled in the art can easily understand the relevant objectives and advantages of the present invention. Attached Figure Description
[0006] Figure 1 This is a block diagram of an embodiment of a non-transitory computer-readable storage medium device and an electronic device.
[0007] Figure 2 This is a schematic diagram of one embodiment of multiple pages.
[0008] Figure 3 This is a flowchart illustrating one embodiment of a program code verification method.
[0009] Figure 4 This is a flowchart illustrating one embodiment of step S70.
[0010] Figure 5 This is a schematic diagram of an embodiment of a compiler generating a mapping file and a low-level code file based on a high-level code file.
[0011] Figure 6 This is a schematic diagram of an embodiment of the shared area portion of a mapping file.
[0012] Figure 7 This is a schematic diagram of an embodiment of the Bank1 section in the mapping file.
[0013] Figure 8 This is a schematic diagram of an embodiment of the Bank7 section in the mapping file.
[0014] Figure 9 This is a schematic diagram of an example of adding a calling keyword to the definition of a C language function.
[0015] Figure 10 for Figure 9 A schematic diagram of an embodiment of the corresponding low-level code file.
[0016] Figure 11 This is a schematic diagram of an embodiment where a call keyword is added at the call declaration point.
[0017] Figure 12 for Figure 11 A schematic diagram of an embodiment of the corresponding low-level code file.
[0018] Figure 13 This is a schematic diagram of an example of a low-level code file corresponding to a C language function when no calling keyword is added at the definition.
[0019] Figure 14 This is a schematic diagram of an embodiment of the low-level code file when no call keyword is added at the call declaration.
[0020] Explanation of reference numerals in the attached figures:
[0021] 100: Non-transitory computer-readable storage medium device; 200: Electronic device.
[0022] 210: Compiler; 220: Storage; A1-AN: Low-level source code files
[0023] Bank1-Bank32: Storage area; C1-CN: High-level source code files; COM: Common area.
[0024] K1: Call keyword M1: Mapping file P1-P32: Paging
[0025] R1: First return instruction; R2: Second return instruction; R3: General return instruction.
[0026] R4: General call instruction T1: Target function information T11: Target function name
[0027] T12: Target module name; T13: First storage area; TF: Target source code file
[0028] S10-S120: Step S71: Step S73-S74: Step Detailed Implementation
[0029] To make the above-described objects, features and advantages of the embodiments of the present invention more apparent and understandable, a detailed description is provided below in conjunction with the accompanying drawings.
[0030] Please see Figure 1 The source code verification method of any embodiment of the present invention can be implemented by a non-transitory computer-readable storage medium device 100. The non-transitory computer-readable storage medium device 100 is used to store at least one program, so that when the electronic device 200 loads and executes this at least one program, this at least one program can cause the electronic device 200 to execute the source code verification method of any embodiment of the present invention. After executing the source code verification method of any embodiment of the present invention, as... Figure 5 , Figure 9 and Figure 11 As shown, the electronic device 200 can effectively verify whether the calling keyword K1 (i.e., "__banked") required for calling a function located in another storage area is missing in at least one high-level source code file C1-CN. This prevents the processor from freezing or experiencing other major problems due to the missing calling keyword K1 in the high-level source code file C1-CN after the low-level source code files A1-AN are assembled into machine code and executed by the processor (e.g., an 8051 microcontroller). Therefore, the source code verification method of any embodiment of the present invention can improve the development efficiency of source code and the quality of application products that execute this source code.
[0031] In some embodiments, the non-transitory computer-readable storage medium device 100 may be a storage device 220 internal to the electronic device 200. In other embodiments, the non-transitory computer-readable storage medium device 100 may be a remote storage element and communicate with the electronic device 200 via a wired or wireless means. In still other embodiments, the non-transitory computer-readable storage medium device 100 may be a storage element external to the electronic device 200 and connected to and accessing the source code of this storage element via a reader or connector of the electronic device 200. In some embodiments, the storage device 220 may be implemented by one or more storage elements, and each storage element may be, but is not limited to, non-volatile memory, such as read-only memory (ROM) or flash memory, or volatile memory, such as random access memory (RAM). Furthermore, the electronic device 200 may be implemented using, but is not limited to, a computer, a central processing unit (CPU), or an application-specific integrated circuit (ASIC).
[0032] Please see Figures 1 to 5In one embodiment of the source code verification method, the electronic device 200 may first use a compiler 210 to read at least one high-level source code file C1-CN and compile it to form at least one low-level source code file A1-AN (step S10). The high-level source code file C1 and the low-level source code file A1-AN are stored in storage 220. In some embodiments, each high-level source code file C1 may be a source code file written in C language by a programmer. Each high-level source code file C1 includes at least one C language function. The compiler 210 may be a Small Device C Compiler (SDCC) specifically designed for developing 8051 source code. The compiler 210 compiles each module using an assembly language to generate a low-level source code file A1-AN corresponding to the high-level source code file C1-CN. The low-level source code file A1-AN is an assembly file (asmfile). Each high-level source code file C1-CN and each low-level source code file A1-AN constitutes a software module. Generally, each high-level source code file C1-CN shares the same module name (filename) as its corresponding low-level source code file A1-AN. In other words, knowing the filename (module name) of a high-level source code file C1-CN allows you to determine the filename (module name) of its corresponding low-level source code file A1-AN, and vice versa. Therefore, the term "module name" as used below in this specification refers to the module name of the corresponding high-level source code file C1-CN and / or low-level source code file A1-AN.
[0033] Generally, processors have limited capabilities and can only support a limited addressable range of source code. For example, the 8051's addressable range is only 64 kilobytes (KB). If the source code exceeds this addressable range, codebanking is typically used to expand the program's storage space. Codebanking divides the program's storage space into multiple pages. For example, if the program's storage space is 2 megabytes (MB), and you want to divide all the program's storage space into 64 kilobytes (KB) pages, it can be divided into 32 pages, P1-P32. Figure 2 As shown. It should be noted that the program's storage space does not necessarily need to be entirely divided into pages; some areas can be left undivided. Each page, P1-P32, may include the shared area COM and the storage areas Bank1-Bank32.
[0034] During the compilation process, compiler 210 analyzes at least one high-order source code file C1-CN to generate a map file M1 (step S10).
[0035] In some embodiments, a mapping file M1 is stored in storage 220. The mapping file M1 records the function names defined in each common area COM and storage areas Bank1-Bank32, the module names of the modules defining each function name, and the entry address of each function name. Each function name indicates either a component language function or a C language function. Generally, corresponding component language functions and C language functions have almost identical function names, the only difference being that component language functions have an additional underscore ("_"). In other words, knowing a component language function name indicates knowing the corresponding C language function, and vice versa. Therefore, the term "function name" as used below in this specification refers generally to the name of the corresponding component language function and / or C language function.
[0036] After obtaining the mapping file M1, the electronic device 200 can retrieve target function information T1, which is the current inspection target, from the mapping file M1 (step S20). Here, the target function information T1 may include the target function name T11, the target module name T12 defining this target function name T11, and a first storage area T13 where the entry address of the target function name T11 is located. For example, as... Figure 8 As shown, "_rtk_gpio_output" is the target function name T11. "_rtk_gpio" is the target module name T12. "00074FE8" is the entry address, where "0007" is the code of storage area Bank7. Therefore, based on "0007", we can know that its first storage area T13 is storage area Bank7.
[0037] In one embodiment of step S20, the electronic device 200 may first analyze the mapping file M1 to generate a function list based on the mapping file M1. The function list includes information on a number of callable complex functions. Then, the electronic device 200 may retrieve one of these function information from the function list as the target function information T1 for the current inspection target. Here, each function information may include a function name, the filename (module name) of the module defining this function name, and the storage area where the entry address of this function name is located. Furthermore, the target function name T11, the target module name T12 defining the target function name T11, and the first storage area T13 where the entry address of the target function name T11 is located are respectively the storage areas where the function name, the module name defining the function name, and the entry address of the function name of the retrieved function information are located.
[0038] In one embodiment of step S20, the electronic device 200 selects a function information as the target function information T1 according to a predetermined method. The predetermined method may be sequential, random, or other suitable selection methods.
[0039] After obtaining the target function information T1, the electronic device 200 can locate a corresponding target source code file TF from the low-level source code files A1-AN according to the target module name T12 of the target function information T1 (step S30). That is, it searches for files in the low-level source code files A1-AN that have the same filename as the target module name T12. Next, it retrieves a first return instruction R1 from the target source code file TF in the function definition corresponding to the target function name T11 (step S40). Figure 10 As shown, this is the target source code file TF with the target module name "_rtk_gpio". We can see that the first return instruction R1 is "ljmpsdcc_banked_ret". Here, only the part concerning the function with the target function name T11 (i.e., "_rtk_gpio_output") is presented. The first return instruction R1 is located on the last line of the function definition for the target function name T11.
[0040] Next, the electronic device 200 traverses the low-level source code files A1-AN to obtain the calling module name that calls the target function name T11 and the second return instruction R2 corresponding to the calling function name (step S50), and obtains a second storage area containing the corresponding calling module name from the mapping file M1 (step S60). The calling function name is a function that calls the target function name T11. The calling module name is the filename (module name) of the low-level source code files A1-AN containing this calling function name. For example, the electronic device 200 can... Figure 12 In the low-level source code file shown, the module called is named "flow_system", the function called is named "_high_voltage_power_enable", and the corresponding second return instruction R2 is found (here, as...). Figure 12 As shown, the second return instruction R2 is “lcall__sdcc_banked_call”. Furthermore, the electronic device 200 can, as shown... Figure 7 The corresponding second storage area in the mapping file M1 is storage area Bank1 (because the corresponding code is "0001"). The second return instruction R2 is located on the last line of the call content of the called function name.
[0041] Then, the electronic device 200 can determine whether the call of each called function name to the target function name T11 conforms to a page-switching writing format based on the first storage area T13, the first return instruction R1, each second storage area, and each second return instruction (step S70).
[0042] In some embodiments of step S70, such as Figure 4As shown, the electronic device 200 can determine whether the first storage area T13 is a shared area COM (step S71). When it is determined that the first storage area T13 is a shared area COM, it means that the C language function corresponding to the target function name T11 is defined in the shared area COM of one of the pages P1-P32. Figure 6 As shown in the example, the keyword "CSEG" indicates the shared area COM. All functions listed here are defined in the shared area COM. For example, if the target function name T11 is "_timer0_isr", then the first storage area T13 is the shared area COM. Due to the characteristics of the shared area COM, regardless of whether the functions corresponding to the calling function names that call this target function name T11 are defined in the shared area COM or storage areas Bank1-Bank32, the electronic device 200 will determine that the calls to the target function name T11 by each calling function name conform to the paging switching writing format (step S80).
[0043] When the electronic device 200 determines in step S71 that the first storage area T13 is not a shared area COM, the electronic device 200 further determines whether the first storage area T13 is the same as each of the second storage areas (step S73). That is, it determines whether the first storage area T13 and each of the second storage areas are the same among storage areas Bank1-Bank32. When it is determined that the first storage area T13 is the same as each of the second storage areas, it means that no cross-page function call has occurred. Therefore, the electronic device 200 will determine that the call of each function name to the target function name T11 conforms to the page switching writing format (step S80).
[0044] like Figure 4 As shown, when the electronic device 200 determines in step S73 that any second storage area is different from the first storage area T13 (i.e., located in different pages P1-P32), it indicates that a cross-page function call has occurred. For example, assuming the first storage area T13 is storage area Bank1, and a second storage area is, for example, storage area Bank3 or shared area COM, the electronic device 200 will consider that a cross-page function call has occurred. At this time, the electronic device 200 will determine whether the first return instruction R1 is a page return instruction, and determine whether the corresponding second return instruction R2 (i.e., the second return instruction R2 corresponding to a calling module name different from the first storage area T13) is a page call instruction (step S74). In some embodiments of step S74, the electronic device 200 may determine whether the second return instruction R2 corresponding to each second storage area that was determined to be different from the first storage area T13 in step S73 is a page call instruction. In other words, the second return instruction R2 corresponding to each second storage area that has been determined to be the same as the first storage area T13 may not be evaluated.
[0045] In some embodiments, the pagination return instruction may be "ljmp__sdcc_banked_ret" used to return to the current function, and the pagination call instruction may be "lcall__sdcc_banked_call" used to call a function located on another page.
[0046] When the electronic device 200 determines in step S74 that the first return instruction R1 is a paging return instruction and the corresponding second return instruction R2 is a paging call instruction, it indicates that the C language function corresponding to the target function name T11 has the calling keyword K1 (e.g., "__banked") added during definition. Figure 9 As shown), and the C language function corresponding to the function name T11 is also declared with the calling keyword K1 (e.g., "__banked"). Figure 11 (As shown). Therefore, the electronic device 200 determines that the call of this function name to the target function name T11 conforms to the paging switching writing format. In some embodiments of step S74, if there are other second return instructions R2 corresponding to the second storage area that need to be determined as paging call instructions, the electronic device 200 will continue to determine whether each second return instruction R2 is a paging call instruction. Furthermore, the electronic device 200 can determine that the call of each function name to the target function name T11 conforms to the paging switching writing format when it determines that the first return instruction R1 is a paging return instruction and that all second return instructions R2 corresponding to the second storage area are paging call instructions (step S80).
[0047] When the electronic device 200 determines in step S74 that the first return instruction R1 is not a page return instruction (for example, it is a general return instruction R3, such as...), Figure 13 As shown, "ret" is a general return instruction R3) or the corresponding second return instruction R2 is determined to be not a paging call instruction (e.g., a general call instruction R4, such as...). Figure 14 As shown, when "lcall_rtk_gpio_output" is a general call instruction, it indicates that the C language function corresponding to the target function name T11 was not defined with the call keyword K1 (e.g., "__banked") (i.e., when the first return instruction R1 is not a paging return instruction), or the C language function corresponding to this calling function name did not add the call keyword K1 (e.g., "__banked") when declaring the call to the C language function corresponding to this target function name T11 (i.e., when the second return instruction R2 is not a paging call instruction). Therefore, the electronic device 200 will determine that the call of this calling function name to the target function name T11 does not conform to the paging switching writing format (step S90). Figure 3As shown, after determining that the page switching writing format does not conform to the format, the electronic device 200 will generate a verification failure result (step S100).
[0048] In one embodiment of the source code verification method, the electronic device 200 may also cause the compiler 210 to stop its compilation process based on the verification failure result (step S110), and display the verification failure result (step S120). The verification failure result may include the target function name T11 and the target module name T12 that caused the verification failure (if it is determined in step S74 that the first return instruction R1 is not a paging return instruction), and / or the calling module name and the calling function name (if it is determined in step S74 that the second return instruction R2 is not a paging call instruction). In this way, the programmer can quickly determine where in the high-level source code file C1 needs to be corrected based on the verification failure result.
[0049] In some embodiments, the verification failure result can be displayed on the electronic device 200's own display. In other embodiments, the verification failure result can also be displayed via a display device connected to the electronic device 200 by a wired or wireless connection.
[0050] In one embodiment of the source code verification method, after the electronic device 200 determines in step S80 that the calls of each function name to this target function name conform to the pagination switching writing format, the electronic device 200 may return to step S20 to obtain another function information as the target function information T1 for the current inspection target, and then execute subsequent steps. In some embodiments, if the electronic device 200 has passed the verification of all function information in the function list by the source code verification method of an embodiment of the present invention, the electronic device 200 may end the entire verification process for at least one high-order source code file C1.
[0051] In summary, the source code verification method and non-transitory computer-readable storage medium device of this invention determine whether the call of each function name to the target function name conforms to the paging switching format by using a first storage area obtained from a mapping file, a first return instruction obtained from the target source code file, each second storage area obtained from the mapping file, and each second return instruction obtained from the low-level source code file. In this way, this invention can effectively confirm whether the calling keywords required when calling a function located in another storage area are missing in the high-level source code file. This avoids the processor from freezing or causing other major problems due to missing calling keywords in the high-level source code file after the low-level source code file is translated into machine code and executed by the processor. This improves the development efficiency of the source code and the quality of the application product executing the source code. Furthermore, the source code verification method and non-transitory computer-readable storage medium device of this invention can generate and display a verification failure result when it is determined that the paging switching format is not conforming, so that the programmer can quickly correct the source code accordingly.
[0052] Although the technical content of the present invention has been disclosed above with reference to preferred embodiments, it is not intended to limit the present invention. Any modifications and refinements made by those skilled in the art without departing from the spirit of the present invention should fall within the protection scope of the present invention. Therefore, the protection scope of the present invention should be determined by the claims of the present invention.
Claims
1. A source code verification method, comprising: Use a compiler to generate a mapping file and at least a low-level source code file from at least one high-level source code file; Obtain target function information from the mapping file, wherein the target function information includes a target function name, a target module name that defines the target function name, and a first storage area where an entry address of the target function name is located; Based on the target module name, find the corresponding target source code file from the at least one low-level source code file; Obtain a first return instruction corresponding to the target function name from the target source code file; Traverse the at least one low-level source code file to obtain at least one calling module name that calls the target function name and a second return instruction for at least one calling function name; Obtain a second storage area corresponding to the name of the at least one calling module from the mapping file; Based on the first storage area, the first return instruction, each second storage area, and each second return instruction, determine whether the call of each called function name to the target function name conforms to a page-switching writing format; as well as If the page switching format is not conformed to, a verification failure result is generated.
2. The source code verification method according to claim 1, characterized in that, The steps for obtaining the objective function information from the mapping file include: A function list is generated based on the mapping file, wherein the function list includes information on multiple functions; and One of the multiple function information from the function list is taken as the target function information.
3. The source code verification method according to claim 1, characterized in that, The steps for determining whether each of the called function names conforms to the pagination switching format include: Determine whether the first storage area is a shared area; and When the first storage area is determined to be the shared area, it is determined to conform to the pagination switching writing format.
4. The source code verification method according to claim 3, characterized in that, The step of determining whether the calls to the target function name by each of the aforementioned function names conform to the pagination switching format further includes: When it is determined that the first storage area is not the shared area, it is determined whether the first storage area is the same as each of the second storage areas; and When it is determined that the first storage area is the same as each of the second storage areas, it is determined that the pagination switching writing format is met.
5. The source code verification method according to claim 4, characterized in that, The step of determining whether the calls to the target function name by each of the aforementioned function names conform to the pagination switching format further includes: When it is determined that any of the second storage areas is different from the first storage area, it is determined whether the first return instruction is a page return instruction and whether the corresponding second return instruction is a page call instruction; and When it is determined that the first return instruction is the pagination return instruction and the corresponding second return instruction is the pagination call instruction, it is determined that the pagination switching writing format is met.
6. The source code verification method according to claim 5, characterized in that, Also includes: If it is determined that the first return instruction is not the pagination return instruction or the corresponding second return instruction is not the pagination call instruction, it is determined that the pagination switching writing format is not met.
7. The source code verification method according to claim 1, characterized in that, Also includes: The compiler stops compiling based on the verification failure result; as well as Display the verification failure result, wherein the verification failure result includes: the target function name and the target module name that caused the verification failure result, and / or the calling function name and the calling module name.
8. A non-transitory computer-readable storage medium device storing at least one program, which, when loaded and executed by an electronic device, causes the electronic device to perform the following steps: Use a compiler to generate a mapping file and at least a low-level source code file from at least one high-level source code file; Obtain target function information from the mapping file, wherein the target function information includes a target function name, a target module name that defines the target function name, and a first storage area where an entry address of the target function name is located; Based on the target module name, find the corresponding target source code file from the at least one low-level source code file; Obtain a first return instruction corresponding to the target function name from the target source code file; Traverse the at least one low-level source code file to obtain at least one calling module name that calls the target function name and a second return instruction for at least one calling function name; Obtain a second storage area corresponding to the name of the at least one calling module from the mapping file; Based on the first storage area, the first return instruction, each second storage area, and each second return instruction, determine whether the call of each called function name to the target function name conforms to a page-switching writing format; as well as If the page switching format is not conformed to, a verification failure result is generated.
9. The non-transitory computer-readable storage medium device according to claim 8, characterized in that, The steps for obtaining the objective function information from the mapping file include: A function list is generated based on the mapping file, wherein the function list includes information on multiple functions; and One of the multiple function information from the function list is taken as the target function information.
10. The non-transitory computer-readable storage medium device according to claim 8, characterized in that, The steps for determining whether each of the called function names conforms to the pagination switching format include: Determine whether the first storage area is a shared area; and When the first storage area is determined to be the shared area, it is determined to conform to the pagination switching writing format.
Citation Information
Patent Citations
Memory leak detection method
CN101814049A
Automatic detection and correction method for array out-of-bound errors
CN103778061A