Runtime Semantics
All globally visible objects in C++ are placed within the program data segment.
In C, a global object can be initialized only by a constant expression, that is, one that can be evaluated at compile time.
6.1 Object Construction and Destruction
Global Objects
When cfront was the only C++ implementation and portability across machines was more important than efficiency, the following munch strategy emerged:
- Within each file that requires static initialization, generate an __sti() function containing the necessary constructor invocations or inline expansions. identity, for example, would cause the following __sti() function to be generated within the file matrix.c:
__sti__matrix_c__identity() {
// Pseudo C++ Code
identity.Matrix::Matrix();
}
where __matrix_c is an encoding of the file name and __identity represents the first nonstatic object defined within the file. Appending these two names to __sti
provided a unique identifier within the executable. (Andy Koenig and Bjarne worked out this “fake static” encoding scheme in response to name-clash agonies reported by Jim Coplien.)
-
Similarly, within each file that requires a static deallocation, generate an
__std()
function containing the necessary destructor invocations or inline expansions. In our example, an__std()
function is generated to invoke the Matrix destructor on identity. -
Provide a set of runtime library munch functions: a _main() function to invoke all the __sti() functions within the executable and an exit() function to analogously invoke all the __std() functions.
cfront inserted a _main()call as the new first statement within main(). The exit() function rather than the C library exit() function was linked in by cfront’s CC command by placing the C++ standard library first on the command line.
Our solution was to use the nm command. (nm dumps the object file symbol table entries.) An executable was generated from the .o files. nm was then run on the resulting executable. Its output was piped into the munch program. (I think Rob Murray wrote munch, but nobody any longer claims to remember.) munch munched the symbol table names, looking for names that began with __sti or __std. (Yes, periodically to amuse ourselves we would begin a function with __sti such as __sti_ha_fooled_you). It then added the function names to a jump table of __sti() and __std() functions. Next, it wrote the tables into a small program text file. Then, odd as it may soun