Function calling method, computing device and storage medium
By generating a function signature serial number table and modifying the calling instructions, the problems of heavy workload, low performance and memory overhead of the function calling scheme in the existing technology are solved, efficient function calling across platforms and architectures is achieved, and flexible calling of static and dynamic functions is supported.
Patent Information
- Application Number
- CN202111554184.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-17
- Publication Date
- 2025-09-23
- Estimated Expiration
- 2041-12-17
AI Technical Summary
Existing cross-platform and cross-architecture function calling solutions have problems such as large manual organization workload, low performance, and excessive memory overhead, which makes it difficult to meet the performance requirements of large-scale programs and the memory needs of embedded devices.
By generating a function signature serial number table, modifying the calling instruction to a static function file, and calling the function according to the serial number table during compilation and runtime, manual organization and additional memory overhead at runtime are avoided, and calling schemes for static and dynamic functions are provided.
It reduces the workload of manual sorting, improves operational efficiency, reduces error rates, supports flexible expansion of different platforms and architectures, and improves program fluency and performance.
Smart Images

Figure CN114217886B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a function calling method, a computing device, and a storage medium. Background Art
[0002] The current mainstream cross-platform and cross-architecture function calling solution is implemented using dyncall (a dynamic call library). Specifically, the specific function signature is manually entered before the call. After the function signature is recognized and processed, the parameters to be processed are manually pushed to the internal virtual stack to implement ABI (Application Binary Interface) conversion. The corresponding parameters and type information are then parsed at runtime and passed in according to the function calling convention of the machine architecture. After the function is executed, the return value is obtained and returned according to the function signature.
[0003] The above scheme has three major flaws: (1) In the pre-compilation stage, a lot of work needs to be manually organized and packaged, and function signatures cannot be automatically identified. When the program code volume is large and the number of functions is large, the workload of manual entry and organization is very huge, and it is difficult to ensure accuracy during large-scale operations; (2) The function signature parsing process is cumbersome and inefficient. Before running the function, the caller will parse the function signature and pass in parameters in sequence, that is, each time a function call occurs in the program, a parsing and parameter passing process will occur. When running large-scale programs such as games that have relatively high performance requirements, the performance degradation is quite obvious, resulting in slow program running speed, freezes, and other phenomena that extremely affect the user experience; (3) When building the call stack, some additional overhead will be generated. For embedded devices with relatively scarce memory, it is easy to encounter fatal problems such as program crashes due to insufficient memory overhead.
[0004] Therefore, a new function calling method is needed to optimize the above processing process. Summary of the Invention
[0005] To this end, the present invention provides a function calling solution to try to solve or at least alleviate the above problems.
[0006] According to one aspect of the present invention, a function calling method is provided, comprising the following steps: first, identifying each to-be-called function in a program to be run to generate a function signature serial number table; modifying the calling instruction of each to-be-called function; generating a static function file based on the function signature serial number table and compiling the file; running the program to be run, and if a function call occurs during the running process, calling the corresponding function according to the modified calling instruction based on the function signature serial number table.
[0007] Optionally, in the function calling method according to the present invention, the step of identifying each function to be called in the program to be run to generate a function signature serial number table includes: scanning each function to be called in the program to be run to identify the function signature of each function to be called; deduplicating the identified function signatures to generate a function signature serial number table.
[0008] Optionally, in the function calling method according to the present invention, the function signature serial number table includes multiple function signature serial numbers, each function signature serial number is associated with a different function signature, and the function signature includes the function calling convention, the number of call input parameters, the parameter type and the return value information.
[0009] Optionally, in the function calling method according to the present invention, the step of modifying the calling instructions of each function to be called includes: modifying the calling instructions of each function to be called into the calling instructions of the function wrapper in the intermediate representation layer, and adding parameters for indexing the function signature.
[0010] Optionally, in the function calling method according to the present invention, the steps of generating and compiling a static function file based on the function signature serial number table include: generating a corresponding static function processing process based on the function signature corresponding to each function signature serial number in the function signature serial number table; constructing a static function table and a running dependent environment, generating a static function file in combination with the static function processing process, and compiling the static function file.
[0011] Optionally, in the function calling method according to the present invention, after the step of generating a static function file based on the function signature serial number table and compiling it, it also includes: if the static function file is used as a static library, linking the static function file.
[0012] Optionally, in the function calling method according to the present invention, the step of calling the corresponding function according to the modified calling instruction according to the function signature serial number table includes: obtaining the function signature of the called function through the function signature serial number table; collecting and reorganizing parameter information based on the function signature of the called function according to the current architecture and platform type; and calling the called function according to the modified calling instruction using the reorganized parameter information.
[0013] Optionally, in the function calling method according to the present invention, the step of obtaining the function signature of the called function through the function signature serial number table includes: searching for the function signature serial number of the called function from the function signature serial number table; and obtaining the function signature of the called function through the function signature serial number of the called function.
[0014] Optionally, in the function calling method according to the present invention, the step of using the reorganized parameter information to call the called function according to the modified calling instruction includes: if the called function is a static function, passing the reorganized parameter information as a parameter and calling the called function according to the modified calling instruction.
[0015] Optionally, in the function calling method according to the present invention, the step of calling the called function according to the modified calling instruction using the reorganized parameter information includes: if the called function is a dynamic function, determining whether there is a formatting string in the function signature of the called function; when it is determined that there is a formatting string, parsing the formatting string to obtain parameter information, dynamically generating machine code corresponding to the current architecture and platform based on the obtained parameter information, and calling the machine code according to the modified calling instruction.
[0016] According to another aspect of the present invention, a computing device is provided, comprising: at least one processor; and a memory storing program instructions, wherein the program instructions are configured to be suitable for execution by the at least one processor, and the program instructions include instructions for executing the function call method described above.
[0017] According to another aspect of the present invention, a readable storage medium storing program instructions is provided. When the program instructions are read and executed by a computing device, the computing device executes the function calling method described above.
[0018] According to the function calling scheme of the present invention, during compilation, each function to be called in the running program is identified to generate a function signature serial number table, and the calling instructions of each function to be called are modified. Based on the function signature serial number table, a static function file is generated and compiled. By automatically building function signatures, manual participation in large-scale manual sorting and packaging is avoided, saving manpower while reducing the error rate, and the compilation process is simpler and more intelligent. During operation, if a function call occurs, the corresponding function is called according to the modified calling instruction based on the function signature serial number table. Depending on the current platform and architecture, the calling method is implemented separately. Static function calls avoid the additional memory overhead caused by a large amount of function signature parsing and call construction during operation, making the operation more efficient and the program smoother.
[0019] In addition, different calling schemes are provided for static functions and dynamic functions in the call processing, which can effectively improve the efficiency of processing different parameters. The above schemes are highly scalable and are not limited to mainstream architectures. They can be expanded arbitrarily by adding architecture-related codes, and are highly flexible. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] To achieve the above and related purposes, certain illustrative aspects are described herein in conjunction with the following description and accompanying drawings, which indicate various ways in which the principles disclosed herein may be practiced, and all aspects and their equivalents are intended to fall within the scope of the claimed subject matter. The above and other objects, features, and advantages of the present disclosure will become more apparent by reading the following detailed description in conjunction with the accompanying drawings. Throughout this disclosure, the same reference numerals generally refer to the same parts or elements.
[0021] Figure 1 shows a structural block diagram of a computing device 100 according to one embodiment of the present invention;
[0022] Figure 2 A schematic diagram of an architecture for implementing function calls according to an embodiment of the present invention is shown; and
[0023] Figure 3 FIG. 3 is a flowchart of a function calling method 300 according to an embodiment of the present invention. DETAILED DESCRIPTION
[0024] Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0025] Figure 1 FIG. 1 shows a structural block diagram of a computing device 100 according to an embodiment of the present invention.
[0026] like Figure 1 As shown, in a basic configuration 102, computing device 100 typically includes system memory 106 and one or more processors 104. A memory bus 108 may be used for communication between processor 104 and system memory 106.
[0027] Depending on the desired configuration, the processor 104 can be any type of processor, including but not limited to: a microprocessor (UP), a microcontroller (UC), a digital signal processing unit (DSP), or any combination thereof. The processor 104 can include one or more levels of cache, such as a level 1 cache 110 and a level 2 cache 112, a processor core 114, and registers 116. An example processor core 114 can include an arithmetic logic unit (ALU), a floating point unit (FPU), a digital signal processing core (DSP core), or any combination thereof. An example memory controller 118 can be used with the processor 104, or in some implementations, the memory controller 118 can be an internal part of the processor 104.
[0028] Depending on the desired configuration, system memory 106 can be any type of memory, including but not limited to volatile memory (such as RAM), non-volatile memory (such as ROM, flash memory, etc.), or any combination thereof. System memory 106 can include an operating system 120, one or more applications 122, and program data 124. In some embodiments, application 122 can be arranged to execute instructions on the operating system by one or more processors 104 using program data 124.
[0029] Computing device 100 also includes storage 132 , which includes removable storage 136 and non-removable storage 138 .
[0030] The computing device 100 may also include a storage interface bus 134. The storage interface bus 134 enables communication from storage devices 132 (e.g., removable storage 136 and non-removable storage 138) to the basic configuration 102 via the bus / interface controller 130. At least a portion of the operating system 120, applications 122, and program data 124 may be stored on the removable storage 136 and / or the non-removable storage 138 and loaded into the system memory 106 via the storage interface bus 134 when the computing device 100 is powered on or when the application 122 is to be executed, and executed by the one or more processors 104.
[0031] The computing device 100 may also include an interface bus 140 that facilitates communication from various interface devices (e.g., output devices 142, peripheral interfaces 144, and communication devices 146) to the basic configuration 102 via the bus / interface controller 130. Example output devices 142 include a graphics processing unit 148 and an audio processing unit 150. These can be configured to facilitate communication with various external devices such as a display or speakers via one or more A / V ports 152. Example peripheral interfaces 144 may include a serial interface controller 154 and a parallel interface controller 156, which can be configured to facilitate communication with external devices such as input devices (e.g., a keyboard, mouse, pen, voice input device, touch input device) or other peripherals (e.g., a printer, scanner, etc.) via one or more I / O ports 158. Example communication devices 146 may include a network controller 160, which can be arranged to facilitate communication with one or more other computing devices 162 via a network communication link via one or more communication ports 164.
[0032] A network communication link can be an example of a communication medium. Communication media can generally be embodied as computer-readable instructions, data structures, program modules in a modulated data signal such as a carrier wave or other transmission mechanism, and can include any information delivery medium. A "modulated data signal" can be a signal in which one or more of a data set or a change therein can be carried out in a manner that encodes information in the signal. As non-limiting examples, communication media can include wired media such as a wired network or a dedicated line network, and various wireless media such as sound, radio frequency (RF), microwave, infrared (IR) or other wireless media. The term computer-readable medium as used herein can include both storage media and communication media.
[0033] The computing device 100 can be implemented as a personal computer including desktop and notebook computer configurations. Of course, the computing device 100 can also be implemented as part of a small-sized portable (or mobile) electronic device, such as a cellular phone, a digital camera, a personal digital assistant (PDA), a personal media player device, a wireless network browsing device, a personal head-mounted device, an application-specific device, or a hybrid device that can include any of the above functions. It can even be implemented as a server, such as a file server, a database server, an application server, and a web server. The embodiments of the present invention are not limited to this.
[0034] In an embodiment according to the present invention, the computing device 100 is configured to execute the function calling method 300 according to the present invention. The application 122 deployed on the operating system includes multiple program instructions for executing the function calling method 300 according to the present invention. These program instructions can instruct the processor 104 to execute the function calling method 300 according to the present invention, so that the computing device 100 calls a function by executing the function calling method 300 according to the present invention.
[0035] To facilitate understanding, the design architecture of the function call scheme is first explained below. Figure 2 FIG. 1 shows a schematic diagram of an architecture for implementing a function call according to an embodiment of the present invention. Figure 2 As shown, during compilation, the compiler will call the compiler plug-in module to collect function signatures of the corresponding platform and architecture, and process the collected function signatures to generate a function signature serial number table, and organize the collected function signatures into a location that can be accepted by the static function generator, such as a block of shared memory. In the process of collecting function signatures, the call of the function signature is modified to a call to the function wrapper at the IR (Intermediate Representation) layer, and a parameter for indexing the function signature is added while maintaining the original parameters. The collection interface of the compiler plug-in module is provided by the platform architecture selector. The compiler plug-in module can choose llvm-plugin. llvm is a series of modular and reusable compilation tool chains that provides a well-written IR that can be used as a backend for multiple languages. It can also provide language-independent optimization and code generation functions for multiple CPUs (Central Processing Units).
[0036] The static function generator uses the function signature sequence table provided by the platform architecture selector to generate the required static function files using the static function generation interface provided by the platform architecture selector. The compiler then compiles the static function files. If the static function files are used as static libraries, the linking process must also be completed; otherwise, they exist as dynamic libraries.
[0037] The platform architecture selector is a set of interfaces designed to provide different interfaces for different platforms and architectures. These interfaces are used by compiler plugin modules and static function generators. It serves as an adapter model to centrally handle platform and architecture differences. For other modules, the interfaces provided by the platform architecture selector remain unchanged regardless of platform and architecture changes.
[0038] The platform architecture selector provides instructions about the target platform. This allows subsequent modules to determine which registers are appropriate for input and output parameters. For example, when running on an ARM (Advanced RISC Machines, a reduced instruction set computer microprocessor) environment, calls should follow the principle of using the general-purpose registers R0-R3 and the SP (Stack Pointer) register. When running on an x86 (a general term for a series of central processing unit instruction set architectures based on the Intel 8086 and backward-compatible), specific calling conventions and default parameter passing rules for the ESP (Extended Stack Pointer) register and general-purpose registers should be followed.
[0039] The function wrapper itself is a specific function that exists as a public interface. During compilation, all function calls whose function signatures are collected are replaced with calls to the function wrapper according to the rules described above. The function wrapper further calls interfaces such as the parameter reorganization module. The function wrapper's own code is generated by the static function generator.
[0040] Cross-platform, cross-architecture, and cross-machine calls are specific application scenarios and belong to the application layer. Function wrappers belong to the service layer, while parameter reorganization modules, dispatch modules (including static and dynamic function components), and return value processing modules all belong to the support layer. The support layer requires different processing methods for different platforms and architectures, so its own code is generated during compilation using the platform architecture selector.
[0041] During runtime, if a function call occurs in the program, and the function is a replaced function (a function whose function signature has been collected), the function wrapper is called, which in turn calls the parameter reassembly module. The parameter reassembly module obtains the function signature based on the index of the appended function signature and then reassembles the collected parameters based on the function signature. Because the reassembly process differs between platforms and architectures, the parameter reassembly module needs to generate different versions for different platforms and architectures, but its purpose remains the same.
[0042] The runtime parameter reorganization module provides unpacking of upper-level cross-platform and cross-architecture function calls, reorganizes the original platform and architecture parameters into a linear storage structure, and stores the structure and the original function pointer to be called into the runtime environment block. The runtime environment block is a section of memory space visible during the call process at runtime. When a function call request comes from the upper layer, the function signature number of the function call is first obtained from the function signature number table, and the number and type of the function call input parameters are obtained through the function signature number. The parameters are reorganized through the architecture type provided by the platform architecture selector to ensure that all parameters are stored in the corresponding positions in the correct order before calling the original function pointer to be called, that is, after calling the original function pointer to be called, the values of the various parameters of the function are correct.
[0043] After the parameter reorganization is completed, the obtained parameter information will be brought into the distribution module. Similarly, the distribution module will also obtain the function signature of the function (including the original function address, etc.) based on the index of the additional function signature, and then complete the function call according to the function signature. The function call can be the original function call, or it can be a remote function call or a function call that matches other function signatures. The distribution module mainly includes two parts: static functions and dynamic functions. Static functions are usually function calls with fixed parameters. The function call can be called with a unified template, that is, through the static function table and function signature index, the corresponding static function processing process is found and called. This process is common to all platform architectures. Dynamic functions are mainly used to process functions with variable parameters, such as printf, sprintf, etc. Since the parameters of variable parameter functions are not fixed, special processing is required.
[0044] Finally, the return value processing module completes the function call's return process, primarily by assigning the return value and balancing the stack. This eliminates the impact of calling the function wrapper, ensuring that the program interprets the function call as it was before the replacement. The return value processing module's implementation varies across platforms and architectures, requiring different versions to be generated for each platform and architecture.
[0045] Figure 3 FIG2 is a flowchart of a function calling method 300 according to an embodiment of the present invention. The function calling method 300 may be executed in a computing device (eg, the aforementioned computing device 100).
[0046] like Figure 3As shown, method 300 starts at step S310. In step S310, each function to be called in the program to be run is identified to generate a function signature serial number table. According to one embodiment of the present invention, the function signature serial number table can be generated in the following manner. First, each function to be called in the program to be run is scanned to identify the function signature of each function to be called, and then, the identified function signatures are deduplicated to generate a function signature serial number table. The function signature serial number table includes multiple function signature serial numbers, each function signature serial number is associated with a different function signature, and the function signature includes a function calling convention, the number of call input parameters, the parameter type, and the return value information.
[0047] In this embodiment, the function signature actually refers to the extraction and simplified representation of the function type information, filtering the identifier information therein. For example, the function int cdecl foo(int bar) has a calling convention of cdecl, denoted as c, its return value type is int type, denoted as i, the function name foo is an identifier, which is discarded, the parameter bar is int type, denoted as i, and the parameter name bar is an identifier, which is discarded. The function type information finally obtained is spliced into c_i_i, which is the function signature. Similarly, when identifying the function int cdecl foo2(int bar), the function signature c_i_i can also be obtained. Since the function signatures of the two are the same, only one function signature needs to be saved.
[0048] After completely scanning all functions to be called, a list without duplicate function signatures can be generated. During the construction of the function signature list itself, an index value will be provided for each function signature in the list. The index value is unique and is used as the function signature serial number, eventually forming a function signature serial number table.
[0049] Then, step S320 is entered to modify the calling instructions of each function to be called. According to one embodiment of the present invention, the calling instructions of each function to be called are modified into calling instructions of a function wrapper in the intermediate representation layer, and parameters for indexing the function signature are added.
[0050] In this implementation, after generating the function signature sequence number table, the call instructions for each function to be called are replaced at the IR layer with the call instructions for the parameter reassembly module entry function. This is equivalent to executing the function entry with the function signature attached. The parameter reassembly module entry function is a function wrapper, meaning a new function encapsulating the function wrapper structure, including the original function pointer to be called and the function signature sequence number. The function signature sequence number can be used to determine the ABI format of the function, facilitating subsequent calling tasks.
[0051] For example, a program calls function foo. Because foo is replaced during compilation with a function wrapper call, the wrapper is called with the function signature index, the original function address, and the original function parameters. The function signature index is an additional parameter used to index the function signature, and its value is the function signature sequence number.
[0052] In step S330, based on the function signature serial number table, a static function file is generated and compiled. According to one embodiment of the present invention, a static function file can be generated and compiled in the following manner. In this embodiment, based on the function signature corresponding to each function signature serial number in the function signature serial number table, a corresponding static function processing procedure is generated, a static function table and an operating dependency environment are constructed, and a static function file is generated in combination with the static function processing procedure, and the static function file is compiled.
[0053] The principle of generating static function files is that the implementation inside the framework does not depend on the implementation outside the framework (the implementation outside the framework includes platform-related implementations such as function calling conventions and parameter passing). In order not to rely on external details, that is, some architecture-related content, the core is constructed in the form of macros.
[0054] For example, if there are two function signatures c_i_i and s_i_i in the function signature serial number table, then it is necessary to generate corresponding static function processing procedures for these two function signatures, that is, generate function f_c_i_i and function f_s_i_i respectively. Since the function signatures are different, it means that the two calling methods and type information are different, so code should be generated separately for this difference. Use the function signature index corresponding to c_i_i as the index of the static function table, insert f_c_i_i into the static function table, and use the function signature index corresponding to s_i_i as the index of the static function table, insert f_s_i_i into the static function table. That is, using the function signature index can index both the function signature and the static function processing procedure corresponding to the function signature.
[0055] During static function processing, it is necessary to interact with the current program's runtime environment, such as reading and writing register information or the stack, or global information such as environment variables and the runtime path. These operations and information are encapsulated into a structure called the runtime dependency environment, which is used only by static function processing. The process for generating a static function processing process corresponding to a specific function signature is as follows:
[0056] (1) Scan the function signature from left to right;
[0057] (2) Extract calling convention information and generate corresponding processing code;
[0058] (3) Extract parameter information and generate corresponding processing code;
[0059] (4) Extract the return value information and generate the corresponding processing code;
[0060] (5) Complete the static function generation process.
[0061] On different platforms and architectures, the corresponding processing codes generated by steps (2), (3), and (4) are also inconsistent. Here, it is also necessary to select the corresponding method for generating processing codes according to the specific platform and architecture (such as using the platform architecture selector).
[0062] After the static function file is generated and compiled based on the function signature serial number table, according to one embodiment of the present invention, if the static function file is used in a static library mode, the static function file is linked.
[0063] Finally, step S340 is executed to run the program to be run. If a function call occurs during the running process, the corresponding function is called according to the modified call instruction based on the function signature serial number table. According to one embodiment of the present invention, the corresponding function can be called according to the modified call instruction based on the function signature serial number table in the following manner. First, the function signature of the called function is obtained through the function signature serial number table. Then, according to the current architecture and platform type, parameter information is collected and reorganized based on the function signature of the called function. The reorganized parameter information is used to call the called function according to the modified call instruction.
[0064] In this embodiment, when obtaining the function signature of the called function, the function signature serial number of the called function is first searched from the function signature serial number table, and then the function signature of the called function is obtained through the function signature serial number of the called function.
[0065] In conjunction with the description of the parameter reorganization module above, let's assume that a program currently exists. This program is a general-purpose software virtual machine. The current platform can be either A or B, and the architecture can be either C or D, which can be combined into four platform architecture modes: AC, AD, BC, and BD. A procedure executing within the virtual machine calls function foo, which itself can also be either A or B, and C or D. To forward the call to function foo from within the virtual machine to the outside, a total of sixteen conversions must be completed: AC→AC, AC→AD, AC→BC, AC→BD, AD→AC, ..., BD→BD.
[0066] To meet these conversion requirements, the function call parameters need to be extracted into an intermediate representation using a method supported by the corresponding platform and architecture for subsequent processing. Assuming that the current virtual machine uses the AC platform architecture combination, it is necessary to use AC's ABI processing method to read the parameters from the corresponding registers or stack according to the function signature, and then place them in an array space or other available storage structure for subsequent use.
[0067] After the parameters of the original platform architecture are reorganized into a linear storage structure, this structure and the original function pointer to be called are stored in the runtime environment block. The original function pointer to be called is essentially a pointer used to describe the address of the function to be called. Assuming that the current program is running on the AD platform architecture combination, this original function pointer to be called will eventually be converted into a pointer pointing to the corresponding processing function address of the AD platform architecture. The specific conversion process can be achieved through methods such as hash tables, or the binary codes of the AC and AD platform architectures in this architectural practice solution can be mixed into the same executable file, so that the original function pointer to be called directly points to the pointer to the corresponding processing function address of the AD platform architecture. This is a more special scenario.
[0068] According to one embodiment of the present invention, the reorganized parameter information can be used to call the called function according to the modified call instruction in the following manner: If the called function is a static function, the reorganized parameter information is passed as parameters, for example, the function signature sequence number and the original function address are passed as parameters, and the called function is called according to the modified call instruction.
[0069] If the called function is a dynamic function, determine whether there is a formatting string in the function signature of the called function. When it is determined that there is a formatting string, parse the formatting string to obtain parameter information, dynamically generate machine code corresponding to the current architecture and platform based on the obtained parameter information, and call the machine code according to the modified calling instruction.
[0070] Static functions and dynamic functions together constitute the static function processing process. Dynamic functions can also be considered special static functions. They are essentially the same, except that dynamic functions (variable parameter functions) are specially recorded in the function signature. When this special function signature record is recognized, special processing code is generated for this type of function. Because this special processing code generates binary executable code during program execution, this special static function processing process is called a dynamic function.
[0071] What is actually run is the wrapped function, and its return value is not the return value of the actual called function. Therefore, after the original function pointer is called, the return value of the original function is read from the specific location of the running environment block and provided for subsequent use, which ensures the continuity of program operation and does not affect the actual functions implemented by the function.
[0072] According to the function calling scheme of the embodiment of the present invention, during compilation, each function to be called in the running program is identified to generate a function signature serial number table, and the calling instructions of each function to be called are modified. Based on the function signature serial number table, a static function file is generated and compiled. By automatically building function signatures, manual participation in large-scale manual sorting and packaging is avoided, which saves manpower while reducing the error rate, and the compilation process is simpler and more intelligent. During operation, if a function call occurs, the corresponding function is called according to the modified calling instruction according to the function signature serial number table. Depending on the current platform and architecture, the calling method is implemented separately. Static function calls avoid the extra memory overhead caused by a large amount of function signature parsing and call construction at runtime, making the operation more efficient and the program smoother.
[0073] In addition, different calling schemes are provided for static functions and dynamic functions in the call processing, which can effectively improve the efficiency of processing different parameters. The above schemes are highly scalable and are not limited to mainstream architectures such as x86 and ARM. They can be expanded arbitrarily by adding architecture-related code, that is, by implementing the calling convention under a specific architecture, the content under this architecture can be expanded, which is highly flexible.
[0074] The various techniques described herein may be implemented in conjunction with hardware or software, or a combination thereof. Thus, the methods and apparatus of the present invention, or certain aspects or portions of the methods and apparatus of the present invention, may be implemented in the form of program codes (i.e., instructions) embedded in a tangible medium, such as a removable hard disk, a USB flash drive, a floppy disk, a CD-ROM, or any other machine-readable storage medium, wherein when the program is loaded into a machine such as a computer and executed by the machine, the machine becomes an apparatus for practicing the present invention.
[0075] When program code is executed on a programmable computer, the computing device generally includes a processor, a storage medium readable by the processor (including volatile and non-volatile memory and / or storage elements), at least one input device, and at least one output device. The memory is configured to store the program code; the processor is configured to execute the function call method of the present invention according to instructions in the program code stored in the memory.
[0076] By way of example and not limitation, readable media include readable storage media and communication media. Readable storage media store information such as computer-readable instructions, data structures, program modules, or other data. Communication media typically embody computer-readable instructions, data structures, program modules, or other data in a modulated data signal such as a carrier wave or other transport mechanism, and include any information delivery medium. Combinations of any of the above are also included within the scope of readable media.
[0077] In the description provided herein, the algorithms and displays are not inherently related to any particular computer, virtual system, or other device. Various general-purpose systems may also be used in conjunction with the examples of the present invention. Based on the above description, it is apparent that the structure required for constructing such systems is well understood. In addition, the present invention is not directed to any specific programming language. It should be understood that various programming languages may be utilized to implement the present invention described herein, and the description of specific languages above is provided for the purpose of disclosing the preferred embodiment of the present invention.
[0078] In the description provided herein, a large number of specific details are described. However, it is understood that embodiments of the present invention can be practiced without these specific details. In some instances, well-known methods, structures, and techniques are not shown in detail so as not to obscure the understanding of this description.
[0079] Similarly, it should be understood that in order to streamline the present disclosure and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of the invention, various features of the invention are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as reflected in the claims below, inventive aspects lie in less than all the features of the individual embodiments disclosed above. Accordingly, the claims that follow the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of the invention.
[0080] Those skilled in the art will appreciate that the modules, units, or components of the devices in the examples disclosed herein may be arranged in the device described in the embodiment, or alternatively may be located in one or more devices different from the devices in the examples. The modules in the foregoing examples may be combined into one module or further divided into multiple submodules.
[0081] Those skilled in the art will appreciate that the modules in the devices in the embodiments may be adaptively changed and arranged in one or more devices different from the embodiments. The modules or units or components in the embodiments may be combined into one module or unit or component, and in addition may be divided into multiple submodules or subunits or subcomponents. All features disclosed in this specification (including the accompanying claims, abstracts and drawings) and all processes or units of any method or device disclosed herein may be combined in any combination, except that at least some of such features and / or processes or units are mutually exclusive. Unless expressly stated otherwise, each feature disclosed in this specification (including the accompanying claims, abstracts and drawings) may be replaced by an alternative feature providing the same, equivalent or similar purpose.
[0082] Furthermore, those skilled in the art will appreciate that although some embodiments described herein include certain features included in other embodiments but not other features, combinations of features from different embodiments are intended to be within the scope of the present invention and to form different embodiments. For example, in the claims below, any of the claimed embodiments may be used in any combination.
[0083] In addition, some of the embodiments are described herein as methods or combinations of method elements that can be implemented by a processor of a computer system or by other devices that perform the functions described. Thus, a processor having the necessary instructions for implementing the method or method element forms a device for implementing the method or method element. Furthermore, the elements described herein of the device embodiments are examples of devices for implementing the functions performed by the elements for the purpose of implementing the invention.
[0084] As used herein, unless otherwise specified, the use of ordinal numbers "first," "second," "third," etc. to describe common objects merely indicates that different instances of similar objects are involved and are not intended to imply that the objects so described must have a given order in time, space, ranking, or in any other manner.
[0085] Although the present invention has been described with respect to a limited number of embodiments, it will be apparent to those skilled in the art, having benefit of the foregoing description, that other embodiments are contemplated within the scope of the invention thus described. Furthermore, it should be noted that the language used in this specification has been selected primarily for readability and didactic purposes, rather than for the purpose of explaining or limiting the subject matter of the present invention. Consequently, many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the appended claims. The disclosure of the present invention is intended to be illustrative rather than restrictive of the scope of the invention, which is defined by the appended claims.
Claims
1. A function calling method, comprising: Identify each to-be-called function in the running program to generate a function signature serial number table, wherein the function signature serial number table includes multiple function signature serial numbers, each function signature serial number is associated with a different function signature, and the function signature includes a function calling convention, a number of call input parameters, parameter types, and return value information; Modifying the calling instructions of each to-be-called function, including modifying the calling instructions of each to-be-called function into calling instructions of a function wrapper in an intermediate representation layer, and adding parameters for indexing a function signature; Based on the function signature serial number table, a static function file is generated and compiled, including generating a corresponding static function processing process based on the function signature corresponding to each function signature serial number in the function signature serial number table, constructing a static function table and a running dependent environment, generating a static function file in combination with the static function processing process, and compiling the static function file; running the program to be run, if a function call occurs during the running, calling the corresponding function according to the modified calling instruction according to the function signature serial number table, including obtaining the function signature of the called function through the function signature serial number table, collecting and reorganizing parameter information based on the function signature of the called function according to the current architecture and platform type, and using the reorganized parameter information to call the called function according to the modified calling instruction.
2. The method according to claim 1, wherein The step of identifying each function to be called in the program to be run to generate a function signature sequence number table includes: Scan each to-be-called function in the program to be run, and identify the function signature of each to-be-called function; The identified function signatures are deduplicated to generate a function signature serial number table.
3. The method according to claim 1, further comprising, after the step of generating a static function file based on the function signature sequence number table and compiling the static function file, If the static function file is used in a static library mode, the static function file is linked.
4. The method according to claim 1, wherein The step of obtaining the function signature of the called function through the function signature serial number table includes: Searching the function signature serial number of the called function from the function signature serial number table; Obtain the function signature of the called function through the function signature serial number of the called function.
5. The method according to claim 4, wherein: The step of using the reorganized parameter information to call the called function according to the modified calling instruction includes: If the called function is a static function, the reorganized parameter information is passed as a parameter, and the called function is called according to the modified calling instruction.
6. The method according to claim 1 or 5, wherein: The step of using the reorganized parameter information to call the called function according to the modified calling instruction includes: If the called function is a dynamic function, determining whether a formatting string exists in the function signature of the called function; When it is determined that a formatting string exists, the formatting string is parsed to obtain parameter information, a machine code corresponding to the current architecture and platform is dynamically generated according to the obtained parameter information, and the machine code is called according to the modified calling instruction.
7. A computing device comprising: at least one processor; as well as A memory storing program instructions, wherein the program instructions are configured to be executed by the at least one processor, and the program instructions include instructions for executing the method according to any one of claims 1 to 6.
8. A readable storage medium storing program instructions, wherein when the program instructions are read and executed by a computing device, the computing device executes the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
JNI-based calling method for JAVA function in C++
CN107832056A
Method for an interpreter to control a native function call based on a signature of the native function
US7360206B1