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

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.