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

The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »

Price Scanner via MacPrices.net

Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more
Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more

Jobs Board

Licensed Practical Nurse - Womens Imaging *A...
Licensed Practical Nurse - Womens Imaging Apple Hill - PRN Location: York Hospital, York, PA Schedule: PRN/Per Diem Sign-On Bonus Eligible Remote/Hybrid Regular Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.