The present invention relates to a compilation method for converting a program described with a high-level language with template facilities into object codes.
In recent development of software, the scale of a program has become large, and therefore, an object-oriented language with high maintainability and reusability is regarded useful.
A typical example of the object-oriented language is the C++ language. The C++ language has template facilities and is usable for generic programming with high maintainability and reusability. Also, a standard template library designated as STL is generally provided to a compiler, and programming using the template facilities is expected to increase in the future.
As shown in source codes (a) and (b) of
The generated codes shown in
In the conventional compilation, when different template parameters are given to a template, functions (including a static function, an external function and a virtual or non-virtual member function) corresponding to the respective parameter values are always generated as codes with different definitions. For example, in
However, with respect to a function generated from a template, a different code is not always generated when a template parameter is different but an identical code may be generated. For example, in
As conventional technique to prevent such increase of the code size, for example, Japanese Laid-Open Patent Publication No. 9-231077 describes a program description method in which a common part of a program is separated, as an implementation class with no specified type, from an interface class with a specified type having a template with a dummy argument.
In this conventional technique, however, a programmer is forced to intentionally describe a common part of a program as an implementation class with no specified type, which increases the burden of the programmer.
An object of the invention is providing a template compilation method in which the code size increased due to template facilities is reduced without making a programmer conscious of optimum program description.
In order to achieve the object, according to the present invention, two or more function definition codes generated as an identical code among a plurality of function definition codes generated from a template are automatically determined by a CPU, so as to share the two or more identical function definition codes.
Specifically, the template compilation method of this invention for converting an input program described with a high-level language with template facilities into object codes, includes an identical function definition generation determining step of determining whether or not identical function definitions are generated, as definitions of a plurality of functions included in the input program, from a template by using different template parameters; a function definition sharing step, performed when it is determined that the identical function definitions are generated in the identical function definition generation determining step, of replacing the definitions of the plurality of functions generated as the identical function definitions with one common function definition; and a function call replacing step, performed when the common function definition is replaced in the function definition sharing step, of replacing calls of the plurality of functions obtained before replacing the common function definition with a call of the common function definition.
In an aspect of the template compilation method of this invention, it is analyzed in the identical function definition generation determining step whether or not any of the definitions of the plurality of functions uses a function template as the template and includes a portion depending upon a template parameter in a function defining portion thereof.
In another aspect of the template compilation method of this invention, in the identical function definition generation determining step, when it is determined as a result of analysis that any of the definitions of the plurality of functions using a function template as the template includes no portion depending upon a template parameter in the function defining portion thereof, nondependent function information composed of at least a file name, a template function name and an instantiated function name is generated.
In another aspect of the template compilation method of this invention, it is analyzed in the identical function definition generation determining step whether or not any of the definitions of the plurality of functions uses a class template as the template and includes a portion depending upon a template parameter in a member function defining portion thereof.
In another aspect of the template compilation method of this invention, in the identical function definition generation determining step, when it is determined as a result of analysis that any of the definitions of the plurality of functions using a class template as the template includes no portion depending upon the template parameter in the member function defining portion thereof, nondependent function information composed of at least a file name, a template function name and an instantiated function name is generated.
In another aspect of the template compilation method of this invention, it is analyzed in the identical function definition generation determining step whether or not any of the definitions of the plurality of functions uses a function template as the template and includes type conversion depending upon a template parameter in a function defining portion thereof, whether or not any of the definitions of the plurality of functions includes a function call using an argument depending upon a template parameter, and whether or not there already exists a function definition instantiated with a template parameter with an identical type size.
In another aspect of the template compilation method of this invention, in the identical function definition generation determining step, when it is determined as a result of analysis that none of the definitions of the plurality of functions includes type conversion depending upon a parameter in a function defining portion thereof, that none of the definitions of the plurality of functions includes a function call using an argument depending upon a template parameter and that there already exists a function definition instantiated with a template parameter with an identical type size, specific parameter identical function information composed of at least a file name, a template function name, a parameter size and an instantiated function name is generated.
In another aspect of the template compilation method of this invention, it is analyzed in the identical function definition generation determining step whether or not any of the definitions of the plurality of functions uses a class template as the template and includes type conversion depending upon a template parameter in a member function defining portion thereof, whether or not any of the definitions of the plurality of functions includes a function call using an argument depending upon a template parameter and whether or not there already exists a function definition instantiated with a template parameter with an identical type size.
In another aspect of the template compilation method of this invention, in the identical function definition generation determining step, when it is determined as a result of analysis that none of the definitions of the plurality of functions includes type conversion depending upon a parameter in a member function defining portion thereof, that none of the definitions of the plurality of functions includes a function call using an argument depending upon a template parameter and that there already exists a function definition instantiated with a template parameter with an identical type size, specific parameter identical function information composed of at least a file name, a template function name, a parameter size and an instantiated function name is generated.
In another aspect of the template compilation method of this invention, the function definition sharing step includes sub-steps of receiving the nondependent function information generated in the identical function definition generation determining step; registering names of instantiated functions generated from an identical template in the nondependent function information with a common function name; and changing function definition codes of the instantiated functions generated from the identical template into a function definition code of the common function definition.
In another aspect of the template compilation method of this invention, the function definition sharing step includes sub-steps of receiving the specific parameter identical function information generated in the identical function definition generation determining step; registering names of instantiated functions generated from an identical template with an identical parameter size in the specific parameter identical function information with a common function name; and changing function definition codes of the instantiated functions generated from the identical template with the identical parameter size into a function definition code of the common function definition.
In another aspect of the template compilation method of this invention, in the function call replacing step, a function call statement using a starting address of a function obtained before generating the common function definition is replaced with a function call statement using a starting address of the common function definition obtained in the function definition sharing step.
In another aspect of the template compilation method of this invention, in the function call replacing step, when the function call is a virtual function call, a starting address of a function obtained before generating the common function definition registered in a virtual function table is replaced with a starting address of the common function definition obtained in the function definition sharing step.
In another aspect of the template compilation method of this invention, in the function call replacing step, when a plurality of virtual function tables attain identical registration contents as a result of replacing the starting address of the function obtained before generating the common function definition registered in the virtual function table with the starting address of the common function definition, the plurality of virtual function tables are replaced with one common virtual function table.
As described above, according to the present invention, it is first determined whether or not identical function definitions are generated among a plurality of function definitions of an input program from a template by using different template parameters. In the case where it is determined as a result that the identical definitions are generated, these identical function definitions are replaced with one common function definition, and calls of these function definitions are replaced with a call of the common function definition.
Accordingly, even when the input program includes a plurality of function definitions generated by giving different parameters to the template, when these plural function definitions are generated as identical definition codes, the identical definition codes corresponding to the plural function definitions are not included in the resultant output codes. As a result, object codes can be generated efficiently with a small code size.
A preferred embodiment of the invention will now be described with reference to the accompanying drawings.
The compilation step of
A program storage D101 stores input programs to be compiled.
In the input step S102, all the programs stored in the program storage D101 are successively input to be passed to the syntactic analyzing step S103.
In the syntactic analyzing step S103, the syntax of each program passed from the input step S102 is analyzed, so as to generate a symbol table, a syntax tree and the like.
In the identical function definition generation determining step S105, information associated with a template is extracted from information generated in the syntactic analyzing step S103, and it is analyzed whether or not identical function definitions are generated from the template by using different template parameters.
In the function definition sharing step S106, information associated with function definition is extracted from the information generated in the syntactic analyzing step S103, the analysis result is passed from the identical function definition generation determining step S105, and a plurality of function definitions generated as the identical function definitions by using different template parameters are replaced with a single common function definition.
In the function call replacing step S107, information associated with function call is extracted from the information generated in the syntactic analyzing step S103, the replacement result is passed from the function definition sharing step S106, and function calls obtained before sharing the common definition are replaced with a call of the common function definition on the basis of the resultant common function definition.
In the intermediate code generating step S109, an intermediate code is generated on the basis of the syntax tree generated in the syntactic analyzing step S103. In generating an intermediate code associated with a function definition generated from the template or a virtual function table, the replaced information obtained through execution of the identical function definition generation determining step S105, the function definition sharing step S106 and the function call replacing step S107 is referred to to generate a corresponding intermediate code.
In the optimizing step S110, various optimization is performed on the intermediate code generated in the intermediate code generating step S1109.
In the resource allocating step S111, hardware resources such as a register and a memory are allocated to all variables included in the intermediate code having been optimized in the optimizing step S110.
In the outputting step S112, the intermediate code having been subjected to the resource allocation in the resource allocating step S111 is converted into an object code, which is output to a generated code storage D113.
The generated code storage D113 stores the object code having been converted through the compilation step including the steps S102 through S112.
The inputting step S102, the syntactic analyzing step S103, the intermediate code generating step S109, the optimizing step S110, the resource allocating step S111 and the outputting step S112 are not herein described in detail because they are not the principal point of the invention.
Next, processing of the identical function definition generation determining step S105 will be described with reference to
The processing of the identical function definition generation determining step S105 is performed through steps a1 through all, which will now be described in this order.
In the step a1, the information associated with a template is extracted from the information generated in the syntactic analyzing step S103. Since information of templates of all the input files has been analyzed in the syntactic analyzing step S103, merely the information associated with templates is extracted. Referring to the program shown in
In the step a2, in the whole extracted information of the templates, processing of a loop 1 (including the steps a2 through a6) is repeatedly performed on respective member functions of each class template. In
In the step a3, it is determined whether or not the member function has a template code not depending upon the template parameter at all. When NO, the flow proceeds to the step a4, and when YES, the flow proceeds to the step a5. For example, when the member function [2] of
Also, the code of the member function [3] is return y; regardless of the value of the template parameter T and hence does not depend upon the value of the template parameter T at all. Therefore, the determination made in the step a3 is YES, and hence the flow proceeds to the step as.
In the step a4, it is determined whether or not there is a function from which identical codes are generated with respect to a specific combination of template parameter values. The process of the step a4 is the same as that of the step a9 performed in a loop 2 of the step a7 described below. Since the member functions [2] and [3] of
In the step a5, parameter nondependent function information of the member function having been determined as YES in the step a4 is generated. Since the member functions [2] and [3] of
In the step a6, the flow returns to the step a2 so as to repeat the processing of the loop 1.
In the step a7, processing of the loop 2 is repeatedly performed on function templates included in the whole extracted information of the templates. In
In the step a8, it is determined whether or not the function has a template code not depending upon the template parameter at all. When NO, the flow proceeds to the step a9, and when YES, the flow proceeds to the step a10. When the function template [1]of
In the step a9, it is determined whether or not there is a function from which identical codes are generated with respect to a specific combination of template parameter values. The process of the step a9 is the same as that of the step a4 performed in the loop 1 of the step a2.
The process of the step a4 and the step a9 is performed through steps b1 through b4, which will now be described in this order.
In the step b1, it is determined whether or not there is explicit or implicit type conversion in the function code. When YES, the flow proceeds to the step b2, and when NO, the flow goes out of the process of the step a4 or the step a9. When the function template [1] of
In the step b2, it is determined whether or not there is a function call by using a parameter dependent argument. When YES, the flow proceeds to the step b3, and when NO, the flow goes out of the process of the step a4 or the step a9. When the function template [1] of
In the step b3, it is determined whether or not there are function definitions generated by using parameters with the same type size in the instantiated parameter values. When YES, the flow proceeds to the step b4, and when NO, the flow goes out of the process of the step a4 or the step a9. When the function template [1] of
In the step b4, specific parameter identical function information is generated on the basis of the function definitions having been determined as YES in all the determinations of the steps b1 through b3. The function template [1] of
In the step a10, parameter nondependent function information of the function having been determined as YES in the step a8 is generated. The function template [1] of
In the step all, the flow returns to the step a7, so as to repeatedly perform the processing of the loop 2.
In this manner, when the processing of the identical function definition generation determining step S105 is applied to the program of
Next, the processing of the function definition sharing step S106 will be described with reference to
The processing of the function definition sharing step S106 is performed through steps c1 through c8, which will now be described in this order.
In the step c1, in the information passed from the identical function definition generation determining step S105, the parameter nondependent function information is subjected to processing of a loop 1 (including the steps c1 through c4). Specifically, in this embodiment, the processing of the loop 1 is repeatedly performed on the parameter nondependent function information of
In the step c2, a common function name is registered with respect to the instantiated function names generated from the same template, and the flow proceeds to the step c3. For example, when the parameter nondependent function information of
In the step c3, the definitions of the respective instantiated functions are deleted from the syntactic analysis result, definition of a common function is registered in the syntactic analysis result, and the flow proceeds to the step c4. When the process of the step c3 is executed on the function definition name information [x] of
In the step c4, the flow returns to the step c1, so as to repeat the processing of the loop 1.
In the step c5, in the information passed from the identical function definition generation determining step S105, the specific parameter identical function information is repeatedly subjected to processing of a loop 2 (including the steps c5 through c8). The specific parameter identical function information of
In the step c6, a common function name is registered with respect to instantiated function names generated from the same template by using parameters with the same size, and the flow proceeds to the step c7. When the process of the step c6 is executed on the specific parameter identical function information of
In the step c7, the definitions of the respective instantiated functions are deleted from the syntactic analysis result, a definition of a common function is registered in a function definition part, and the flow proceeds to the step c8. When the process of the step c7 is executed on the definition name information [y] of the common function shown in
In the step c8, the flow returns to the step c5, so as to repeatedly perform the processing of the loop 2.
In this manner, when the processing of the function definition sharing step S106 is applied to the parameter nondependent function information of
Next, the processing of the function call replacing step S107 will be described with reference to
The processing of the function call replacing step S107 is performed through steps d1 through d13, which will now be described in this order.
In the step d1, information associated with function calls is extracted from the information generated in the syntactic analyzing step S103. In the case where there is virtual function table information of each class, this information is also extracted. Since the information associated with the function calls included in all the input files has been analyzed in the syntactic analyzing step S103, merely the information associated with function calls and the virtual function table information are extracted. In the program shown in
In the step d2, processing of a loop 1 is repeatedly performed on the whole function call information for calling functions generated from the templates. In
In the step d3, a call of a function whose common function name has been registered is replaced with a common function call. In
In the step d4, it is determined whether or not the function call is a virtual function call. When YES, the flow proceeds to the step d5, and when NO, the flow proceeds to the step d13. In
In the step d5, the processing of the loop 2 is repeatedly executed on all virtual function tables in which functions are registered with a common function name. In
In the step d6, the functions registered with the common function name in the virtual function table are replaced with a common function. An address of the function is actually registered, but it is herein described for simplification that the function is registered. In
In the step d7, the flow returns to the step d5, so as to repeat the processing of the loop 2.
In the step d8, processing of a loop 3 is repeatedly performed on the virtual function tables in which the functions have been replaced. In
In the step d9, it is determined whether or not any virtual function tables have attained the same contents through the replacement performed in the step d6. When YES, the flow proceeds to the step d10, and when NO, the flow proceeds to the step d12. In
In the step d10, the virtual function tables having the same contents are deleted, and a common virtual function table is generated. The virtual function tables vtbl_B<int> and vtbl_B<long> of
In the step d11, a virtual function table to be referred to in a virtual function call is replaced with a common virtual function table. The virtual function tables vtbl_B<int> and vtbl_B<long> referred to in the virtual calls in
In the step d12, the flow returns to the step d8, so as to repeat the processing of the loop 3.
In this manner, when the processing of the identical call replacing step S107 is applied to the program shown in
In the intermediate code generating step S109, not only the syntax analysis data generated in the syntactic analyzing step S103 but also data newly generated through the identical function definition determining step S105, the function definition sharing step S106 and the function call replacing step S107 are all read, so as to generate object codes to be ultimately stored in the generated code storage D113.
As described so far, even in the case where there are a plurality of function definitions resulting from different parameters given to a template in an input program, when identical definition codes are generated as the plural function definitions, resultant output codes include one identical definition code alone according to the present invention. Therefore, the invention is useful as a template compilation method or the like for efficiently generating output codes with a small code size.
Number | Date | Country | Kind |
---|---|---|---|
2003-375776 | Nov 2003 | JP | national |