Why are functions renamed for .cpp files with llvm-gcc?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Why are functions renamed for .cpp files with llvm-gcc?

Arto Vuori
Hello,

I wonder why there is a difference in how
llvm-gcc compiles .c and .cpp files.

Example:

---bar.cpp----
int bar() {
    return 42;
}
--------------

$ llvm-gcc -emit-llvm -c bar.cpp

Now running bar.o through llvm-dis gives:
--------------------------------
define i32 @_Z3barv() nounwind {
 < clip >
}
--------------------------------

Above, function 'bar' has been renamed to '_Z3barv'.

If, however, 'bar.cpp' is renamed 'bar.c',
we get 'define i32 @bar()' as expected.

I'm currently running precompiled llvm-gcc binaries (being
unable to compile fresh llvm-gcc at the moment):
llvm-gcc (GCC) 4.2.1 (Based on Apple Inc. build 5623) (LLVM build 2.4).

I apologize if the issue is due to my old llvm-gcc version.

--
Arto Vuori
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Fabian Scheler
Hi Arto,

> I wonder why there is a difference in how
> llvm-gcc compiles .c and .cpp files.
>
> Example:
>
> ---bar.cpp----
> int bar() {
>    return 42;
> }
> --------------
>
> $ llvm-gcc -emit-llvm -c bar.cpp
>
> Now running bar.o through llvm-dis gives:
> --------------------------------
> define i32 @_Z3barv() nounwind {
>  < clip >
> }
> --------------------------------
>
> Above, function 'bar' has been renamed to '_Z3barv'.
>
> If, however, 'bar.cpp' is renamed 'bar.c',
> we get 'define i32 @bar()' as expected.

The answer is: C++ name mangling

Ciao, Fabian
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Pertti Kellomäki-2
In reply to this post by Arto Vuori
A. Vuori kirjoitti:
> I wonder why there is a difference in how
> llvm-gcc compiles .c and .cpp files.
[...]
> Above, function 'bar' has been renamed to '_Z3barv'.
>
> If, however, 'bar.cpp' is renamed 'bar.c',
> we get 'define i32 @bar()' as expected.

Look at what c++filt says about _Z3barv:

$ c++filt _Z3barv
bar()

The reason is that .cpp is taken to be a C++ file, and
names are mangled accordingly.
--
Pertti
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Sylvere Teissier-3
In reply to this post by Arto Vuori
Le mercredi 17 juin 2009 à 11:16 +0300, A. Vuori a écrit :
> Hello,
>
> I wonder why there is a difference in how
> llvm-gcc compiles .c and .cpp files.

C++ support function overloading, C not.
For C++ you have to encode the types with the function name to not have
name collision



_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Owen Anderson-2
In reply to this post by Fabian Scheler

On Jun 17, 2009, at 1:39 AM, Fabian Scheler wrote:

> Hi Arto,
>
>> I wonder why there is a difference in how
>> llvm-gcc compiles .c and .cpp files.
>>
>> Example:
>>
>> ---bar.cpp----
>> int bar() {
>>   return 42;
>> }
>> --------------
>>
>> $ llvm-gcc -emit-llvm -c bar.cpp
>>
>> Now running bar.o through llvm-dis gives:
>> --------------------------------
>> define i32 @_Z3barv() nounwind {
>> < clip >
>> }
>> --------------------------------
>>
>> Above, function 'bar' has been renamed to '_Z3barv'.
>>
>> If, however, 'bar.cpp' is renamed 'bar.c',
>> we get 'define i32 @bar()' as expected.
>
> The answer is: C++ name mangling

To elaborate a little bit, it's how C++ encodes namespaces, function  
overloading, etc.  It's a standard thing, and necessary for cross-
compiler compatibility.  If you want your C++ functions to appear like  
C functions, you need to use extern "C" { }

--Owen
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

smime.p7s (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Frits van Bommel
In reply to this post by Arto Vuori
A. Vuori wrote:
> Above, function 'bar' has been renamed to '_Z3barv'.
>
> If, however, 'bar.cpp' is renamed 'bar.c',
> we get 'define i32 @bar()' as expected.

This is not llvm-gcc specific; it happens for regular gcc too. It's needed to
support overloading multiple functions called 'bar' with different parameters,
(which C++ supports but C does not). _Z3barv just means "bar()", i.e. a function
named 'bar' with no parameters (there's a program called c++filt to demangle
these names).

This is called name mangling. See also
http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Matthieu Moy
In reply to this post by Fabian Scheler
Fabian Scheler <[hidden email]> writes:

> The answer is: C++ name mangling

and the slightly longer answer is :

try

int foo(int x) { ... }
int foo(void) { ... }
int foo(whatever-else y) { ... }

(which doesn't compile in C)

--
Matthieu
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Mailing List lag

Sylvere Teissier-3
In reply to this post by Frits van Bommel
I think there is a latency problem with the mail server.
That create answer echo :D


> _______________________________________________
> LLVM Developers mailing list
> [hidden email]         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Arto Vuori
In reply to this post by Owen Anderson-2
On Wed, 17 Jun 2009, Owen Anderson wrote:

> To elaborate a little bit, it's how C++ encodes namespaces, function
> overloading, etc.  It's a standard thing, and necessary for cross-compiler
> compatibility.  If you want your C++ functions to appear like C functions, you
> need to use extern "C" { }

Thanks to all for elaborating. So this was just a hole in my C++
knowledge.

--
Arto Vuori
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Mailing List lag

Frits van Bommel
In reply to this post by Sylvere Teissier-3
Sylvere Teissier wrote:
> I think there is a latency problem with the mail server.
> That create answer echo :D

Yeah, I've noticed that before...
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Reply | Threaded
Open this post in threaded view
|

Re: Why are functions renamed for .cpp files with llvm-gcc?

Albert Graef
In reply to this post by Arto Vuori
A. Vuori wrote:
> ---bar.cpp----
> int bar() {
>     return 42;
> }
> --------------

Try extern "C" int bar() instead, then you get C binding and the name
mangling goes away.

--
Dr. Albert Gr"af
Dept. of Music-Informatics, University of Mainz, Germany
Email:  [hidden email], [hidden email]
WWW:    http://www.musikinformatik.uni-mainz.de/ag
_______________________________________________
LLVM Developers mailing list
[hidden email]         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev