Implementation of language features

W

W Karas

Is there a reference (besides the ARM and hopefully consisting of WWW page(s).) that outlines possible implementations (that is, generated object code) of C++ features? Something to point an experience C programmer to in response to comments like "I think calling a virtual function must involve a hash table lookup" or "I don't see how exception handling could not have execution time overhead even when no exceptions are thrown".
 
I

Ian Collins

W Karas wrote:

Please do something about your line length!
Is there a reference (besides the ARM and hopefully consisting of WWW
page(s).) that outlines possible implementations (that is, generated
object code) of C++ features? Something to point an experience C
programmer to in response to comments like "I think calling a virtual
function must involve a hash table lookup" or "I don't see how
exception handling could not have execution time overhead even when
no exceptions are thrown".

There isn't any definitive implementation standard.

There are a limited number of possible solutions for exceptions, but
each compiler has it's own implementation. Optimisation can also
obscure or remove details like virtual function calls.
 
W

W Karas

W Karas wrote:



Please do something about your line length!












There isn't any definitive implementation standard.



There are a limited number of possible solutions for exceptions, but

each compiler has it's own implementation. Optimisation can also

obscure or remove details like virtual function calls.

I believe there is a "model" approach for exception handling with no time overhead if no exception is thrown. It is based on generating a lookup table where the key is the return address found on the stack during stack unwind. The result of the table lookup is the address of a function that will properly destroy the known set of objects that will be active when that return address is present in the call stack.
 
Ö

Öö Tiib

Am 11.03.13 23:51, schrieb Stefan Ram:


Gcc's standard library is called libstdc++ and has it's own page:

http://gcc.gnu.org/libstdc++/

It's written there that libstdc++ is now part of gcc. So simply download
a copy of the source of gcc.

clang++ uses by default libstdc++, too (on OSX), but they have
developped there own version:

http://libcxx.llvm.org/

Thanks, Christian, you were very helpful. I suspect however that Stefan
was being sarcastic with his counter-question. Those sites are among first
hits when attempting to search for them.
 
8

88888 Dihedral

Stefan Ramæ–¼ 2013å¹´3月12日星期二UTC+8上åˆ6時51分39秒寫é“:
Maybe I may follow that with my similar question:



GCC is open source, so its source code should

be available. A part of it should be the source for

GCC's C++ standard library written in C++ itself

(well, maybe in the GCC dialect of it!). Does anyone

already know where to find this, before I start to

search it myself? Or/And possibly for Clang, too?
As far as I know, an assembler and a C compiler are implemented first
for some new CPU instruction set, then one can get the CPP compiler
in the BSD or LINUX platforms.
 
W

W Karas

Thanks, Christian, you were very helpful. I suspect however that Stefan

was being sarcastic with his counter-question. Those sites are among first

hits when attempting to search for them.

I guess I was not clear, I'm looking for summary descriptions in English accompanied by figures and/or bits of code. I think most people would find this preferable to reading compiler code and reverse-engineering the key strategies involved.
 
J

Jorgen Grahn

The C++ ABI defines several areas that are relevent to interoperability on
Unix systems - including the vtable layout and format.

http://mentorembedded.github.com/cxx-abi/
http://mentorembedded.github.com/cxx-abi/abi-eh.html

this applies to both itanium and x86_64 systems.

A good C programmer shouldn't feel uncomfortable disassembling code and
poking around in the guts of a program to see what the the compiler is
generating.

I suspect that W Karas' C programmers are of the kind who think it's
up to *him* to disprove their home-made theories about how C++ must
work ...

/Jorgen
 
D

Dombo

Op 11-Mar-13 23:39, W Karas schreef:
Is there a reference (besides the ARM and hopefully consisting of WWW
page(s).) that outlines possible implementations (that is, generated
object code) of C++ features? Something to point an experience C
programmer to in response to comments like "I think calling a virtual
function must involve a hash table lookup" or "I don't see how
exception handling could not have execution time overhead even when
no exceptions are thrown".

If the performance really matters and I really want to understand what
is going on I usually inspect the assembly output of the compiler and/or
step though the code with the debugger in disassembly mode.

Inspecting the assembly output can lead to sometimes surprising
insights. I see too often that people resort to tricks that make the
code hard to understand and/or brittle because they _believe_ it will
run more efficiently without bothering to actually measure it or
actually checking how it affects the code being generated. More often
than not the hack is no more efficient and quite often even less
efficient than the straightforward implementation.

There are many variables, such as the compiler, (optimization) settings
and the code being compiled, that affect what kind of code will be
generated. For example for virtual functions a vtable is typically used
(I have never seen a C++ compiler that used hash tables for virtual
functions, nor do I see any benefit with that approach), but sometimes
the compiler can skip the vtable lookup if knows at compile time exactly
which function is going to be called, it might even inline that function
in that case.

That being said the following articles should give some insight how
virtual functions are typically done by C++ compilers:
http://en.wikipedia.org/wiki/Virtual_method_table
http://tinydrblog.appspot.com/?p=89001

For exception handling the articles below may give you some insight how
C++ compilers may implement this feature:
http://preshing.com/20110807/the-cost-of-enabling-exception-handling
http://www.systemcall.org/blog/2010/10/zero-cost-exception-handling-in-cpp/
 
W

W Karas

I suspect that W Karas' C programmers are of the kind who think it's

up to *him* to disprove their home-made theories about how C++ must

work ...

Not necessarily. Unfortunately, even in scientific and technical matters, pride or greed or both can cause people to market rather than explain. They emphasize advantages and gloss over trade-offs. Skepticism is an unavoidable consequence of that. I'd find it highly believable if someone told meit would be hard for them to find the time to figure out how C++ worked bylooking at uncommented, disassembled code.
 
A

Anand Hariharan

(...)

I guess I was not clear, I'm looking for summary descriptions in English accompanied by figures and/or bits of code.  I think most people would find this preferable to reading compiler code and reverse-engineering the keystrategies involved.


Not what you are looking for in terms of "outlining generated
object
code from compiling C++ code", but methinks Lippman's "Inside C+
+
object model" could provide sufficiently technical explanations
in
English to refute people's common misconceptions about C+
+.

- Anand
 
W

W Karas

Op 11-Mar-13 23:39, W Karas schreef:










If the performance really matters and I really want to understand what

is going on I usually inspect the assembly output of the compiler and/or

step though the code with the debugger in disassembly mode.



Inspecting the assembly output can lead to sometimes surprising

insights. I see too often that people resort to tricks that make the

code hard to understand and/or brittle because they _believe_ it will

run more efficiently without bothering to actually measure it or

actually checking how it affects the code being generated. More often

than not the hack is no more efficient and quite often even less

efficient than the straightforward implementation.



There are many variables, such as the compiler, (optimization) settings

and the code being compiled, that affect what kind of code will be

generated. For example for virtual functions a vtable is typically used

(I have never seen a C++ compiler that used hash tables for virtual

functions, nor do I see any benefit with that approach), but sometimes

the compiler can skip the vtable lookup if knows at compile time exactly

which function is going to be called, it might even inline that function

in that case.



That being said the following articles should give some insight how

virtual functions are typically done by C++ compilers:

http://en.wikipedia.org/wiki/Virtual_method_table

http://tinydrblog.appspot.com/?p=89001



For exception handling the articles below may give you some insight how

C++ compilers may implement this feature:

http://preshing.com/20110807/the-cost-of-enabling-exception-handling

http://www.systemcall.org/blog/2010/10/zero-cost-exception-handling-in-cpp/

I think SmallTalk uses a hash table to bind function calls to functions at runtime. This implementation stems from the fact that formal parameters toSmallTalk methods (member functions) are only bound to duck types at compile time, not concrete types. The advantage is that the distinction betweena class and a templeted class is eliminated.
 
Ö

Öö Tiib

Not necessarily. Unfortunately, even in scientific and technical
matters, pride or greed or both can cause people to market rather
than explain. They emphasize advantages and gloss over
trade-offs. Skepticism is an unavoidable consequence of that. I'd
find it highly believable if someone told me it would be hard for them
to find the time to figure out how C++ worked by looking at
uncommented, disassembled code.

The usual reason for such discussions is that some software developers
(you?) try to make other developers (them?) to use or consider tools that
they do not like.

Nothing good can come out of it. Ever. Only the nonsense fan sites like
http://keithlea.com/javabench/ that contain some Python vs. Assembler
performance contest where Python wins by large margin.
 
W

W Karas

The usual reason for such discussions is that some software developers

(you?) try to make other developers (them?) to use or consider tools that

they do not like.



Nothing good can come out of it. Ever. Only the nonsense fan sites like

http://keithlea.com/javabench/ that contain some Python vs. Assembler

performance contest where Python wins by large margin.

Just as God would have saved Sodom and Gomorrah for the benefit of a handful of righteous men, I think what I'm asking for would be nice to have, evenif only for a handful of open-minded C programmers.
 
J

Jorgen Grahn

.
Just as God would have saved Sodom and Gomorrah for the benefit of a
handful of righteous men, I think what I'm asking for would be nice to
have, even if only for a handful of open-minded C programmers.

And just for the record, I agree. But unfortunately I have never seen
that information collected in one place.

For the "exceptions are for free unless thrown" idea, I have seen
people mention that such designs are feasible[1], but it's not clear
to me /how/ it's done, or if my favorite compiler does it. This
bothers me a bit.

/Jorgen

[1] It might even be mentioned in Stroustrup's /Design and Evolution
of C++/. That book probably answers some of your questions, but it's
now almost 20 years old.
 
Ö

Öö Tiib

Just as God would have saved Sodom and Gomorrah for the benefit of a
handful of righteous men, I think what I'm asking for would be nice to
have, even if only for a handful of open-minded C programmers.

It was angel. You are not angel of God. :) You have no message.

I suggest to measure such things and look at them. World changes but you
stay efficient and elegant. The modern implementations of exceptions
virtuals etc. are extremely efficient. Someone who is objecting against
those tools is still right, every tool can be used inefficiently. Examples ....

Virtual is bad: I have measured serious performance drop because a
pointless virtual did block vital optimizations. Do not ever have
pointless virtuals.

Virtual is good: I have measured large performance gain of replacing
sized switch-case with virtuals. Avoid huge switch case blocks, use
virtuals.

Exception is bad: I recently discovered that returning a value is only 300 times quicker than throwing it. It was way larger old times. 300 is still
a lot.

Exception is good: I have measured that passing and handling fallible,
nullable or optional in every cycle (that never falls, is null or missing)
is twice slower than returning a value. So, if the chance to fail is 1/600
or less then using exception is twice cheaper than using fallible, nullable
or optional.

Try-catch is bad: The try-catch block is simply *ugly*. I never put them
into every cycle despite it costs nothing (entering and leaving 10 millions
of those takes one millisecond).

Try-catch is good: Thanks to its ugliness I use RAII that makes it possible
to move the ugly try-catch up the call chain around the whole operation.
RAII looks extremely elegant as contrast. Same effect is with
'static_cast<Foo>(poo)'. It is so ugly that I have reduced casts to
minimum.
 
W

woodbrian77

I'm building clang ...

The getting started page says that make "builds both

LLVM and Clang for debug mode." Why do I want debug

mode? How to build it without debug stuff? Tia.

I managed to build Clang without debug stuff and it
helped me find a problem that neither G++ or MSVC
were pointing out.

Now I'm wondering about a warning clang gives. Here's
an example:

clang++ -std=c++0x -O3 -s -Wreorder -W -Wall -I. -DCMW_MAKEFILES -DSYSLOG_AVAILABLE -c -o remote_messages_middle.cg.o remote_messages_middle.cg.cc

clang: warning: argument unused during compilation: '-s'

I got the impression I could remove the -s and nothing
would change. But my executable is 64,816 bytes when
everything is built with -s and 79,648 when I take it out.
G++ doesn't give any warning when the -s is in there.
Any ideas? Thanks.

Brian
Ebenezer Enterprises - John 3:16.
http://webEbenezer.net
 
W

woodbrian77

Nevermind. My mistake. I think the -s isn't needed
for some fo the build, but is in the linking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top