C++ source code compilation

S

Sheky

Could anyone please answer if every C++ programs get converted to
equivalent C code during compilation ? Thank you.
 
V

Victor Bazarov

It does not.

.... and generally speaking, compiler-specific questions should probably
be posted to the newsgroups dedicated to those compilers (gnu.g++.help
in this case). See the rest of section 5 of the FAQ.

V
 
P

Paul N

Could anyone please answer if every C++ programs get converted to
equivalent C code during compilation ? Thank you.

In addition to what others have said, FAQ 38.13 is probably worth
reading.
 
J

Juha Nieminen

Leigh Johnston said:
Some early C++ compilers worked this way but I suspect very few modern
C++ compilers produce C as an intermediate but rather create machine
code directly.

Actually compilers, especially those supporting many languages (such as
gcc) probably produce a kind of "intermediate" form (which is more or less
common to all the languages that the compiler supports), which is then
optimized and compiled into asm. (This intermediate form isn't a programming
language per se, but usually some tokenized and "half-compiled" tree data in
binary form, or similar.)

Anyways, converting a C++ program into C and then compiling that C would
be inefficient, complicated and produce a less efficient result (because
the compiler cannot use its knowledge of the C++ syntax in order to better
optimize the code), so it would make little sense.

(As an example, I'm guessing that exception handling is impossible to
convert into C code so that it would be as efficient as when compiling
C++ directly. C++ exceptions are designed so that if no exception is
thrown, the code will be as efficient as if no exceptions were in use
at all. I think this would be impossible to express in C.)
 
R

ralph

Are you suggesting that C++ compilers convert the code to asm, then this asm
code is convert to object code?

Not exactly what he is suggesting, but in process of compiling by
mainstream compilers (GCC, VC++, Borland, ...) there is a step in
which preprocessed C++ code is converted to assembly. The assembler
then converts the 'asm' code into object code.
Would the same not apply with asm?

Not really. Or rather there is no asm that couldn't be
reverse-engineered back into an equivalent expression in another
higher-level language.

Thus while performing a C++ to C conversion at the source code level
would likely lead to a complete mental collapse, a C++ to asm then asm
to C would be doable - though definitely non-trivial.

-ralph
 
8

88888 Dihedral

What the byte code compiler in C++ and C are different?

That is funny at all. A tree pass compiler in C++/C with local limited optimization only and can't be inserted with assembly code directly
is weired.

Objects are just decorated syntax sugars for structures and function pointers
as stated clearly in the FAQ in this news group.
 
J

Jorgen Grahn

Not exactly what he is suggesting, but in process of compiling by
mainstream compilers (GCC, VC++, Borland, ...) there is a step in
which preprocessed C++ code is converted to assembly. The assembler
then converts the 'asm' code into object code.

Are you sure of that? Going to (e.g.) x86 assembly language and then
invoking the assembler to get x86 object code seems unnecessary
complication. I don't see it mentioned in the "GCC internals" document.

/Jorgen
 
R

Rui Maciel

Jorgen said:
Are you sure of that? Going to (e.g.) x86 assembly language and then
invoking the assembler to get x86 object code seems unnecessary
complication. I don't see it mentioned in the "GCC internals" document.

He is right. GCC is notoriously organized in front-ends, which translate
any given programming language into an intermediate language which is
supported by GCC (it appears to support at least 3 intermediate languages),
and back-ends, which translate code written in those intermediate languages
into machine code for a specific target platform.

This approach may appear an "unnecessary complication" at first. Yet, this
approach means, at least in theory, that you only need to write a new GCC
front-end in order to get your new programming language to be supported in
every platform supported by GCC, and it also means that you only need to
write a new GCC back-end in order to offer support for each and every
programming language supported by GCC in a brand new target architecture.

Also, by relying on an intermediate implementation it is also possible to
perform all sorts of code optimization on a single piece of data and yet
reap benefits from that optimization when writing code in any programming
language supported by GCC and when targetting any platform supported by GCC.


Hope this helps,
Rui Maciel
 
R

ralph

So do, some don't. IIRC, GCC, for example, does, MSVC does not.

Yeah, I did make is sound like they all output .ASM files. MSVC
doesn't unless requested.

But here is where we will likely get into a semantics war over
"passes", "assembler", and "machine code". <g>

First off consider what is "asm"? It is merely decorated machine code
formatted into sections to be human or favorite Assembler readable and
output to a file. If that is your definition, then MSVC skips that.
What it produces from preprocessed source code is a formatted, binary
equivalent - the .obj. That pass is called the "assembler".

You can get MSVC to output formatted .ASM with a compiler switch. Not
a very complex process as it just re-formats its internal binary
representation to text. Furthermore, you can use that .ASM file as a
full replacement in later operations. Which to me demonstrates the
only difference is formatting.

The GCC outputs a documented file, MSVC keeps its cards a little
closer to the vest, but its there. They all go through an "assembly"
phase.

-ralph
 
J

John Bokma

J

Juha Nieminen

Paul said:
You can't implement C++ exception hadling in asm.

That's equivalent to saying that you can't implement C++ exception
handling in machine code. Or, in other words, that C++ cannot be compiled
(at least not C++ that has exception handling).
 
J

Juha Nieminen

Paul said:
Don't be an idiot, an object file is not machine code.

I still find it puzzling why you are deliberately doing this. You *know*
that you will look like a complete idiot, but you still do it nevertheless.
Either you find this amusing, or once you have made an error, you simply
cannot stop, no matter what.

Is it a mental illness? A birth defect affecting brain processes?
An extreme personality disorder? Mental retardation?

It clearly comes and goes in waves. Sometimes you act like a complete
retard, sometimes there are long relatively calm periods. Is this a bit
like a manic-depressive disorder?
 
8

88888 Dihedral

Don't be an idiot, an object file is not machine code.
Black is not white, a computer is not a dab radio , is not a telvevison , is
not a toaster.Stop being ridiculous please.
An object file contains machine code.
Thus, compiler and linker just emit machine code to run on a machine.

But the 3 layer separation of GCC is good for portabilities of new languages
to be supported in GCC to emit native machine code if desired.

The HW separation of GCC is more versatile than MSVC that sticks with X86 only.

Some compiler authors from cs maybe just don't think in HW very naturally.
 
N

Nick Keighley

no. Not all C++ compilers use C as an intermediate language. I
understand very few do. It's slow and (I understand- IANA compiler
writer) C isn't a particularly good intermediate language.

or directly into machine code.

Not exactly what he is suggesting, but in process of compiling by
mainstream compilers (GCC, VC++, Borland, ...) there is a step in
which preprocessed C++ code is converted to assembly. The assembler
then converts the 'asm' code into object code.





Not really. Or rather there is no asm that couldn't be
reverse-engineered back into an equivalent expression in another
higher-level language.

hardly. Compiling is a pretty information lossy process. The chances
of recovering usable source from machine code is pretty slim.
Thus while performing a C++ to C conversion at the source code level
would likely lead to a complete mental collapse, a C++ to asm then asm
to C would be doable - though definitely non-trivial.

I'm not convinced disassembly is easier than compiling C++ into C. We
know people can write C++ compilers there are loads around. C-Front
specifically compiled into C. Disassembly is a Hard problem.
 
8

88888 Dihedral

Except, as pointed out elsethread, when object files contain nothing
at all resembling machine code. In byte-code or
link-time-code-generation cases, for example.




MSVC currently compiles for x86, x86-64, IPF and ARM. And in the past
compiled for Alpha, MIPS and PPC. IPF will be moving to the "past"
category in the not too distant future.
Thanks for your information. I personally use MSVC in Wintel platforms.

I use GCC in linux/unix/solaris platforms.

I have co-linux installed under windows with a VNC that can be used to
test a lot GNU PLOTS, GNU GCC and etc..
 
R

Rui Maciel

wrote:
Compilers to not output machine code or asm , a compilers output is object
code.

rui@Kubuntu:tmp$ cat main.c
int main()
{
return 0;
}

rui@Kubuntu:tmp$ gcc -S main.c -o main.s
rui@Kubuntu:tmp$ cat main.s
.file "main.c"
.text
.globl main
.type main, @function
main:
..LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
..LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1"
.section .note.GNU-stack,"",@progbits


Is this an other idiot thats gonna try and argue that machine code is
object code ?

What's your definition of machine code? And what's your definition of
object code?


Rui Maciel
 
R

ralph

hardly. Compiling is a pretty information lossy process. The chances
of recovering usable source from machine code is pretty slim.

*Recovering* original source is definitely next to impossible.
Converting to *equivalent* source code in any language is not.

I'm not convinced disassembly is easier than compiling C++ into C. We
know people can write C++ compilers there are loads around. C-Front
specifically compiled into C. Disassembly is a Hard problem.

It seems most of my associates agree with you. They pointed out that
once one worked out a framework for fundamental C++ idioms, for
example a "class", it becomes a matter of updating essentially
boilerplate code. (Albeit, amazingly ugly code. <g>)

I find I must agree with them and you. My comment belongs in the realm
of opinion and not fact, and was based simply on thirty years of
viewing the asm output of C and translating asm back to C, compared to
far fewer attempts to convert C++ to C.

-ralph
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top