5.1.1. The Prelinker Phase. Altium TriCore
Reklamë
Reklamë
TASKING VX-toolset for TriCore User Guide
5.1.1. The Prelinker Phase
The C++ compiler provides a complete prototype implementation of an automatic instantiation mechanism.
The automatic instantiation mechanism is a "linker feedback" mechanism. It works by providing additional information in the object file that is used by a "prelinker" to determine which template entities require instantiation so that the program can be linked successfully. Unlike most aspects of the C++ compiler the automatic instantiation mechanism is, by its nature, dependent on certain operating system and object file format properties. In particular, the prelinker is a separate program that accesses information about the symbols defined in object files.
Template information and instantiation request files
At the end of each compilation, the C++ compiler determines whether any template entities were referenced in the translation unit. If so, a template information file (
.ti
) is created (or an instantiation request file
(
.ii
), if template information files are not being used). If no template entities were referenced in the translation unit, the file will not be created and any existing file will be removed. When template information files are being used, the instantiation request file is also removed if the template information file is removed.
If an error occurs during compilation, the state of the
.ii
file is unchanged.
Prelinker
Once a complete set of object files has been generated, including the appropriate flags, the prelinker is invoked to determine whether any new instantiations are required or if any existing instantiations are no longer required. The command line arguments to the prelinker include a list of input files to be analyzed.
The input files are the object files and libraries that constitute the application. The prelinker begins by looking for template information files for each of the object files (or instantiation request files, if template information files are not being used). If no template information (or instantiation request) files are present, the prelinker concludes that no further action is required.
194
Using the C++ Compiler
If there are template information (or instantiation request) files, the prelinker reads the template information file and the current instantiation list from the instantiation request file. The instantiation list is the list of instantiations assigned to a given source file by a previous invocation of the prelinker. The prelinker produces a list of the global symbols that are referenced or defined by each of the input files. The prelinker then simulates a link operation to determine which symbols must be defined for the application to link successfully.
When the link simulation has been completed, the prelinker processes each input file to determine whether any new instantiations should be assigned to the input file or if any existing instantiations should be removed. The prelinker goes through the current instantiation list from the instantiation request file to determine whether any of the existing instantiations are no longer needed. An instantiation may be no longer needed because the template entity is no longer referenced by the program or because a user supplied specialization has been provided. If the instantiation is no longer needed, it is removed from the list (internally; the file will be updated later) and the file is flagged as requiring recompilation.
The prelinker then examines any symbols referenced by the input file. The responsibility for generating an instantiation of a given entity that has not already been defined is assigned to the first file that is capable of generating that instantiation.
If a given file contains instantiations of exported templates, the template information file contains dependency information. If a source file that is part of an exported template translation unit changes, the dependency information is used to detect such a change so that the prelinker can recompile the file and regenerate the instantiations of the exported templates.
Recompilation
Once all of the assignments have been updated, the prelinker once again goes through the list of object files. For each, if the corresponding instantiation request file must be updated, the new file is written. Only source files whose corresponding .ii file has been modified will be recompiled.
At this point the combination of the template information and instantiation request files contains the information needed to recompile the source file and a list of instantiations assigned to the source file, in the form of mangled function and static data member names.
When the prelinker invokes the C++ compiler, it provides a "definition list file", which contains a list of all the external definitions found in the object files and libraries specified on the prelinker command line. The
C++ compiler reads the definition list file and determines whether each of the entries on its instantiations required list has already been defined elsewhere. The definition list file permits the C++ compiler to perform instantiations that have become necessary as a result of other instantiations that were assigned by the prelinker, without requiring that the prelinker invoke the C++ compiler an additional time to perform those instantiations. This reduces the number of iterations of the prelinker and C++ compiler that are required to generate a complete set of instantiations for a program.
If an error occurs during a recompilation, the prelinker exits without updating the remaining information files and without attempting any additional compilations.
Iteration and termination
If all recompilations complete without error, the prelink process is repeated, since an instantiation can produce the demand for another instantiation. This prelink cycle (finding uninstantiated templates, updating
195
Shkarko
Reklamë