TweetFollow Us on Twitter

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 doesn’t 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 doesn’t 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, let’s 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 won’t 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. Let’s 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 class’s 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 doesn’t 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++ doesn’t 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 don’t 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 don’t 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, let’s 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 wouldn’t 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, don’t be afraid to look under the hood.

Dan Weston, Nerdworks

3410 SW Water Ave.

Portland, OR 97201

503-274-9577

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.