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

The best scanner app on mobile
People always say that the best camera is the one you have with you. Well, the same is true with scanners, and your phone can be a pretty great tool for scanning receipts and other documents while you're on the go. [Read more] | Read more »
MARVEL Avengers Academy guide - How to g...
MARVEL Avengers Academy lets you build your own superhero school and fill it with heroes from the Marvel universe. It can be a little slow going to get your school's attendance up though, so we've gathered together somesome tips to help you do this... | Read more »
Shadow Blade: Reload guide - How to hack...
Shadow Blade: Reload is the kind of action-platformer that would have happily sucked up hours of your time on a console a few years back.Now, you can take it with you wherever you go, and its mobile conversion is not too shabby at all. To help you... | Read more »
Tomb of the Mask guide - How to increase...
Tomb of the Mask is a great endless arcade game from Happymagenta in which quick reflexes and a persistent attitude can go a long way toward earning a top score. Check out these tips to see if you can give yourself an edge on the leaderboards. [... | Read more »
Smooth Operator! (Games)
Smooth Operator! 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Smooth Operator is a weird, weird two-player kissing game. Squeeze in for 2 player fun on a single iPad, creating awkward... | Read more »
Sinless: Remastered (Games)
Sinless: Remastered 1.0 Device: iOS Universal Category: Games Price: $1.99, Version: 1.0 (iTunes) Description: | Read more »
_PRISM Guide - How to solve those puzzle...
_PRISM is a rather delightful puzzle game that’s been tailor made for touch screens. While part of the fun is figuring things out as you go along, we thought we’d offer you a helping hand at getting in the right mindset. Don’t worry about messing... | Read more »
Fractal Space (Games)
Fractal Space 1.3.1 Device: iOS Universal Category: Games Price: $.99, Version: 1.3.1 (iTunes) Description: Live the memorable experience of Fractal Space, a unique first person adventure & puzzle game by Haze Games! Will you... | Read more »
Set off on an adventure through the Cand...
Like match three puzzlers? If so, Jelly Blast, the innovative iOS and Android game which launched last year, is worth a look. Jelly Blast sees you head off on an epic adventure through the Candy Kingdom with your friends Lily, Mr. Hare, and Mr.... | Read more »
Ellipsis - Touch. Explore. Survive. (...
Ellipsis - Touch. Explore. Survive. 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: | Read more »

Price Scanner via MacPrices.net

Sale! 27-inch 3.2GHz 5K iMac for $1799, save...
B&H Photo has the 27″ 3.2GHz/1TB Fusion 5K iMac (MK472LL/A) on sale for $1799.99 including free shipping plus NY tax only. Their price is $200 off MSRP, and it’s the lowest price available for... Read more
cb Hardcase – Handmade and Premium Protective...
Baden-Baden, Germany based company cb innovations has introduced the new cb Hardcase for iPhone. Featuring fine Italian Premium leather that makes for a unique look and feel, the cb Hardcase... Read more
Free Quartz News Aggregation App Puts News Yo...
Quartz have released their new iPhone app. via the Apple App Store. The app, exclusive to iPhone (also compatible with iPad and iPod touch.), provides a whole new way to experience Quartz, which set... Read more
Apple Watch on sale for $100 off MSRP, free s...
B&H Photo has the Apple Watch on sale for $100 off MSRP. Shipping is free, and B&H charges NY sales tax only: - Apple Watch Sport: $100 off - Apple Watch: $100 off Read more
Sale! B&H Photo offers 12-inch Retina Mac...
B&H Photo has 12″ Retina MacBooks on sale for $300 off MSRP for a limited time. Shipping is free, and B&H charges NY tax only: - 12″ 1.1GHz Gray Retina MacBook: $999 $300 off MSRP - 12″ 1.... Read more
Save up to $470 with previous generation 27-i...
B&H Photo has previous-generation 27″ iMacs available for up to $470 off original MSRP, with prices starting at $1329. Each iMac includes free shipping, and B&H charges NY sales tax only: -... Read more
12-inch 1.1GHz Gray Retina MacBook on sale fo...
Musician’s Friend has the 12″ 1.1GHz Gray Retina MacBook on sale for $1199.99 including free shipping. Their price is $100 off MSRP, and it’s the lowest price available for this model. Read more
11-inch 256GB MacBook Air on sale for $999, $...
B&H Photo has the 11″ 1.6GHz/256GB MacBook Air on sale for $999.99 including free shipping plus NY sales tax only. That’s $100 off MSRP. MacMall has the 11″ 1.6GHz/256GB MacBook Air on sale for $... Read more
Free Reverso Context 3.1 For iOS Offers Conte...
Montreal, Canada based online and mobile translation and dictionary company Reverso has just released its Reverso Context 3.1 mobile app. The app provides its users with contextual translations of... Read more
Free SOSmart Automatic Car Crash Detection Ap...
Santiago, Chile based SOSmart SpA has announced release and immediate availably of SOSmart automatic car crash detection 2.4.8, an update to their app utility developed for iOS and Android devices.... Read more

Jobs Board

*Apple* Solution Specialist - Healthcare - C...
*Job Description* The Apple Solution Specialist - Healthcare proactively drives revenue and profit in the assigned sales segment, Healthcare, specific to Apple . This Read more
Infrastructure Engineer - *Apple* /Mac - Rem...
…part of a team Requires proven problem solving skills Preferred Additional: Apple Certified System Administrator (ACSA) Apple Certified Technical Coordinator (ACTC) Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Simply Mac *Apple* Specialist- Service Repa...
Simply Mac is the largest premier retailer of Apple products in the nation. In order to support our growing customer base, we are currently looking for a driven Read more
*Apple* Reporter - Business Insider, Inc. (U...
Business Insider is looking for a reporter to cover Apple , the biggest and arguably most important company in tech. As our primary Apple reporter, you will: * Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.