C++ Intermediate
Volume Number: | | 6
|
Issue Number: | | 7
|
Column Tag: | | MacOOPs!
|
C++ Intermediate Code
By Dan Weston, Portland, OR
Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.
[Dan Weston is a long-time Macintosh developer and author. His new book, Elements of C++ Macintosh Programming, will be published by Addison-Wesley in June 1990.]
What is C++ Doing?
C++ is a new version of C with object-oriented extensions. Most versions of C++ are currently implemented as preprocessors. That is, you write C++ source code, then the C++ preprocessor (called CFront) takes the C++ code and translates it into standard C code. The resulting C code is then fed to a regular C compiler for compilation into executable code. The programmer usually doesnt see the intermediate C code produced by CFront since the whole process is wrapped in a script (called CPlus in MPW) that invokes CFront and then pipes the intermediate code directly to the C compiler.
This article will look at some examples of intermediate code to give you a better idea of what CFront is doing to your C++ code. Knowing what the intermediate code looks like will help you to understand the cryptic error messages that you sometimes get during a build of a C++ program and will also help you to avoid certain C++ constructions that result in inefficient C code.
How can you Spy on C++
The first thing to know is how to get a look at the intermediate code generated by CFront. If you pass the -c option to CPlus, it will write the intermediate C code to standard output without invoking the C compiler. You can redirect the output stream to a file to capture the intermediate code for further inspections, as shown by the following MPW command.
#1
CPlus test.cp -c > test.cp.c
The intermediate file will be filled with information from any header files that you have included in your C++ code. You can usually ignore most of the header information, and search toward the end of the file for the C code that corresponds to your C++ code. Each line in the intermediate code is marked with a comment that identifies the line number and file name for the corresponding C++ statement in the original source code. You can search for your C++ source file name to skip over the code produced by the headers.
Simple Function Calls
Lets look at some simple C++ programs and the intermediate code that they produce. First, look at a C++ program that doesnt use any object-oriented features, such as the following.
//2
short funcA(long x)
{
return x;
}
void funcB(double z)
{
}
int main(void)
{
short r = funcA(100);
funcB(2.5);
}
The program above defines two simple functions and calls them from main. Nothing object-oriented about that. Now take a look at the intermediate code that CFront generates. (The intermediate code is heavily edited here to make it more readable).
//3
#line 2 test.cp
short funcA__Fl(long x)
{
return (short )x;
}
#line 9 test.cp
void funcB__Fd(double z)
{ }
#line 14 test.cp
int main(void ){
{
short r;
r= funcA__Fl( (long )100) ;
funcB__Fd( 2.5) ;
}
}
Notice that CFront has renamed the functions funcA and funcB. The extra characters added to the function names tell CFront about the arguments of the functions so that it can do type checking when the functions are called. The name mangling that C++ does allows it to do what is called type-safe linking. Notice also how CFront has added explicit type conversion statements to make sure that function arguments and return values are of the correct type.
Unmangle
The way that CFront changes the names of functions is called mangling. It can be very confusing, especially during the link stage of a build, to get error messages that use the mangled name of a function. In MPW, the unmangle tool can be used to translate a mangled name back into its original unmangled form. For example, look at the following MPW command and response.
// 4
unmangle funcA__Fl
Unmangled symbol: funcA(long)
Preventing Mangling
Mangled function names can be especially troublesome if you are trying to link with a library of existing C functions. For example, looking back at our first example, lets assume that funcB is part of a C library. To use that function you would simply include a function prototype for funcB and then link with the appropriate library. Look at the the following C++ code
//5
short funcA(long x)
{
return x;
}
// this function is defined somewhere else
extern void funcB(double z);
int main(void)
{
short r = funcA(100);
funcB(2.5);
}
and the resulting C intermediate code.
//6
#line 2 test.cp
short funcA__Fl(long x)
{
return (short )x;
}
extern void funcB__Fd(double z);
#line 12 test.cp
int main(void ){
{
short r;
r= funcA__Fl( (long )100) ;
funcB__Fd( 2.5) ;
}
}
Notice that CFront still mangles the name of funcB. This will cause the link phase to fail because the linker wont be able to find a function named func__Fd in the C library. The C library contains a function named funcB, so the mangled name will not match up. To prevent this type of conflict when linking with existing C libraries, you must prevent CFront from mangling the names of external functions. Since this is a common problem, C++ provides a way to turn name mangling off. If you enclose your external function declarations in a block preceded by the statement extern C, then CFront will not mangle any function names within the block. Look at the previous example again, this time with mangling disabled for funcB, shown as follows.
//7
short funcA(long x)
{
return x;
}
// this function is defined somewhere else
extern C
{
void funcB(double z);
}
int main(void)
{
short r = funcA(100);
funcB(2.5);
}
And notice that the name of funcB has not been mangled in the resulting intermediate code, shown as follows.
// 8
#line 2 test.cp
short funcA__Fl(long x)
{
return (short )x;
}
extern void funcB(double z);
#line 12 test.cp
int main(void ){
{
short r;
r= funcA__Fl( (long )100) ;
funcB( 2.5) ;
}
}
Using the extern C syntax will allow you to link with existing C libraries. If you look through the Macintosh header files in CIncludes, you will notice that they all bracket the function prototypes for ROM calls within an extern C block so that CFront will not mangle the toolbox function names.
Object Oriented Code
The above section shows how CFront mangles function names even for non object-oriented code. For object-oriented code, the situation gets even worse. Consider the following C++ program.
//9
class sample {
public:
double B;
int LessThanB(double z);
};
int sample::LessThanB(double z)
{
return (z < B);
}
int main(void)
{
sample aSample;
aSample.B = 2.5;
int lt = aSample.LessThanB(1.5);
}
And now look at the intermediate code that is produced by CFront.
// 10
#line 2 test.cp
struct sample {
/*sizeof sample == 8 */
double B;
};
#line 8 test.cp
int LessThanB__6sampleFd(struct sample *this,
double z)
{
return (z< this-> B);
}
#line 13 test.cp
int main(void ){
{
struct sample aSample;
int lt;
aSample. B= 2.5;
lt= LessThanB__6sampleFd( & aSample, 1.5) ;
}
}
Notice how CFront creates a struct to correspond to the class, but the struct includes only the data member for the class and not the member function. The member function for the class is defined as a separate function with a mangled name that identifies it as a member function of the class. The member function is invoked with a simple function call, so there is no performance penalty for using this sort of object-oriented approach. Later sections of this article will show how other types of object-oriented techniques do impose a performance penalty.
Notice also that whereas we defined the LessThanB member function with only one argument, CFront has added another argument, this, which is a pointer to the struct that represents the object. All member functions have this additional argument (except static member functions, but you can ignore them for now). The additional argument is used to access data members of the object, as seen by the expression this->B in the function body. You can see how this is passed to the member function in the last line of the intermediate code, where the address operator is applied to the object that is being used to make the member function call. Compare the original C++ code to call the member function and the corresponding intermediate code, shown as follows.
//11
// C++ code
int lt = aSample.LessThanB(1.5);
// C intermediate code
lt= LessThanB__6sampleFd( & aSample, 1.5) ;
Just as we did for the simple function names, we can unmangle member function names, as shown by the following MPW command and response.
//12
unmangle LessThanB__6sampleFd
Unmangled symbol: sample::LessThanB(double)
Because data members are referenced through a pointer to the struct that represents the object, there is no need for CFront to mangle data member names.
Virtual Member Function Calls
The previous section showed how CFront creates separate functions with mangled names to correspond to member functions. Those functions are invoked with a simple function call. The situation changes when you use virtual member functions. Virtual member functions are used extensively when you are deriving one class from another and overriding individual member functions to change the behavior of the derived class, but you should be aware that virtual functions are less efficient that non-virtual functions, as shown by the following example.
Consider the sample class described in the previous section. Lets make its member function virtual, as shown by the following C++ code.
//13
class sample{
public:
double B;
virtual int LessThanB(double z);
};
int sample::LessThanB(double z)
{
return (z < B);
}
int main(void)
{
sample aSample;
aSample.B = 2.5;
int lt = aSample.LessThanB(1.5);
}
Now look at the intermediate code that is generated by CFront, piece by piece. First, notice that the struct defined for the class has an additional field to hold a pointer to a virtual function jump table, often called a vtable. The vtable holds pointers to each of the classs virtual functions (there is only on entry in the table for this class.)
//14
#line 2 test.cp
struct sample { /* sizeof sample == 12 */
double B;
struct __mptr *__vptr;
};
Next, see how the virtual member function is defined with its mangled name, just as it was in the previous section when it was a non-virtual function. Although the function is defined in the same way, it is called in a radically different manner, as you will see.
//15
#line 8 test.cp
int LessThanB__6sampleFd(struct sample *this,
double z)
{
return (z< this-> B);
}
Look at the vtable that CFront builds for the class. An array of vtable entries is defined and initialized, as shown below.
//16
struct __mptr __vtbl__6sample[]=
{0,0,0,0,0,
(__vptp)LessThanB__6sampleFd,
0,0,0};
The statement shown above initializes just one element of the jump table since there is only one virtual function for this class. All the zeros in the initialization fill in the various fields of the vtable entry structure. The important thing to notice is that one of the fields is initialized to point to the LessThanB member function (using its mangled name, of course.) Later, this pointer will be used whenever a call is made to that function. Once the vtable is defined and initialized, CFront also defines a pointer variable and sets it to point to the vtable, as shown in the following statement that appears in the intermediate code.
//17
struct __mptr *__ptbl__6sample=__vtbl__6sample;
Now look at the intermediate code for main. It defines the object and initializes the __vptr member to point to the vtable described previously.
//18
#line 13 test.cp
int main(void ){
{
struct sample aSample;
int lt;
( ((& aSample)-> __vptr=
(struct __mptr *)__ptbl__6sample),
(& aSample)) ;
The expression that initializes the __vptr member is actually two expressions separated by a comma operator (see K & R, page 192). The C compiler will evaluate the first expression and discard its value. The value of the combined comma expression is the value of the second expression (after the comma). In this particular context, the value of the second expression serves no purpose and it can be ignored. It is probably an artifact of a multi-purpose code generation process that CFront would also use if you were creating the object with the new operator. CFront often generates code that uses obscure features of C that will send you scrambling to your C reference book.
After initializing the object, CFront goes on to assign a value to the B member, following closely our original C++ code.
//19
aSample. B= 2.5;
Finally, look at the intermediate code generated to call the virtual function.
//20
lt= LessThanB__6sampleFd( & aSample, 1.5) ;
But wait a minute, where is the virtual call? The intermediate code shown above is a simple function call using the mangled name. This doesnt look any different than the code generated for the non-virtual version of the function. The problem is that we used an instance of the object, rather than a pointer to the object, to call the function. The virtual function calling mechanism is employed by C++ only when you use a pointer to an object to call the member function. A little explanation may make this clear.
If you use an object of a specific type to call a function, then C++ can determine at compile time exactly the type of the object and thus can determine which version of the virtual function to use. Therefore, C++ will use a direct function call to the the member function that matches the type of the object that you are using to call the function. If, on the other hand, you use a pointer to an object to make the function call, then C++ doesnt know if the pointer is pointing to the specified object type or to one of its derived types. The fact that a pointer to a particular object type might point to a derived object type forces C++ to use the virtual function lookup mechanism when it calls a function that is invoked through a pointer reference. Look at the following modification to the main function.
//21
int main(void)
{
sample aSample;
sample *paSample = &aSample;
paSample->B = 2.5;
int lt = paSample->LessThanB(1.5);
}
In the modified version, we allocate an object of the type sample, and then initialize a pointer to the object. (Normally you would probably use the new operator to create the object pointer directly, but I wanted to avoid explaining the intermediate code generated by a new operation to keep this explanation on track.) We then use the pointer to initialize the data member and call the virtual function. Examine the intermediate code generated to call the virtual function through the pointer.
//22
lt= ((*(((int (*)(struct sample *this,
double z))
(paSample-> __vptr[1]).f))))
( ((struct sample *)
((((char*)paSample))
+ (paSample-> __vptr[1]).d)), 1.5) ;
Looks a little more complicated than the simple function call that we saw previously. The first two lines are a typecast to convince the compiler that the pointer we are extracting from the vtable is a pointer to the right kind of function. The third line accesses the function pointer from the f field of the vtable entry structure in the first slot in the vtable. Notice that if there were more than one virtual function for this class, the array index might be something other than 1. The next two lines start setting up the arguments for the function. The first argument to the member function is a pointer to the object. If the object has been derived from multiple parent classes, then the pointer might need to be offset to get to the correct portion of the combined class structures to access the data members of the appropriate parent class. To account for this possible offset, CFront adds the value from the d field of the vtable entry structure to the pointer to the object, as shown by the last line above. Since this class does not use multiple inheritance, the offset is zero. The last line also passes the other argument, 1.5, to the function.
You can see that there is a substantial amount of overhead for virtual functions. In most cases, however, you will not be able to notice this overhead as your program is executing. My personal strategy is to make all member functions virtual to begin with so that I can have the most flexibility when deriving new classes and overriding member functions. If performance becomes a problem, then I look at my design to see where the bottlenecks are and try to make those function non-virtual. If, on the other hand, you are defining a class that will not be used to derive other classes, then you should definitely make those member functions non-virtual.
SingleObject Virtual Member Functions
If you want more efficient code but dont want to give up virtual functions, you can derive your class from the built-in class SingleObject. This parent class tells CFront that you will not be using multiple inheritance for the derived class and thus CFront does not need to generate extra code to add the offset for multiple base classes when calculating the address of the data members to pass as the this argument to the function. A look at the generated code will show you the gain in efficiency.
Redefine the class so that it is derived from SingleObject, as shown here.
//23
class sample : public SingleObject{
public:
double B;
virtual int LessThanB(double z);
};
The resulting intermediate code to call the virtual function, shown as follows, is simpler because it simply passes a pointer to the object rather than having to calculate offsets to account for possible multiple parent classes. Notice also that the vtable entry is simply a pointer to the function rather than the structure that we saw in the previous section.
//24
lt= ((*(((int (*)(struct sample *this,
double z))
(paSample-> __vptr[1])))))
( paSample, 1.5) ;
If you dont need multiple inheritance, then it is a good idea to base all your classes on SingleObject. If you find that you need still more efficient function calls, then you will have to go to a non-virtual member function.
Static Member References
Lastly, lets look at the way C++ handles static members. A static member is a data member that is shared by all objects of a particular class. To define a static member, just precede the member declaration with the keyword static, as shown by the following class declaration.
//25
class sample{
public:
static short X;
double B;
virtual int LessThanB(double z);
};
Now look at the structure that CFront creates to represent the class.
//26
#line 2 test.cp
struct sample { /* sizeof sample == 12 */
double B;
struct __mptr *__vptr;
};
extern short X__6sample;
Notice that the static member is not a field in the class structure. Instead, it is declared as an extern variable with a mangled name indicating its connection to the class. Because it is declared as extern, but not defined, it is up to you to actually define the static member so that space is allocated for its storage. Static members are typically defined just like global variables, as shown by the following C++ code. Notice that the definition of the static member qualifies its name with the name of the class and two colons. It is common to initialize the static member at the time that you define it.
//27
// define static like global
short sample::X = 0;
int main(void)
{
sample aSample;
sample *paSample = &aSample;
paSample->B = 2.5;
paSample->X = 1;
int lt = paSample->LessThanB(1.5);
}
Looking at the intermediate code that is generated from the previous C++ code, notice
that the name of the static member is mangled when CFront defines the variable.
//28
#line 14 test.cp
short X__6sample = 0;
Also compare the code generated for accessing a regular object member versus a static member. The regular member is accessed through an object pointer. The static member, on the other hand, is accessed a simply by its name, just like any non-object variable.
//29
#line 21 test.cp
paSample-> B= 2.5;
#line 22 test.cp
X__6sample= 1;
In essence, a static member is just like a regular, non-object variable that obeys access rules as if it was a class member. Static members are most useful as replacements for global variables. The private, protected, and public protection levels of a class allow you to control access to the static member, making it safer than an unprotected variable.
Summary
I hope this article gives you some idea of what CFront is doing when it translates your C++ code into C intermediate code. Looking at the intermediate code can be very instructive and somewhat disheartening when you set the amount of overhead that is involved in all those nifty object-oriented concepts like virtual functions. But I wouldnt worry about performance until you have to. Of course, there are things you can do, such as using SingleObject derivation, to reduce the overhead without giving up too many object-oriented features. Most of all, dont be afraid to look under the hood.
Dan Weston, Nerdworks
3410 SW Water Ave.
Portland, OR 97201
503-274-9577