Re: [ooc-compiler] PROPOSAL: Interface to C++ objects and methods
Brought to you by:
mva
|
From: Stewart G. <sgr...@ii...> - 2006-01-19 04:29:55
|
Stewart Greenhill wrote:
>> Would it not be simpler to have OOC mangle the symbol names the same
>> way as the C++ compiler? I realise that is compiler-dependant and I
>> believe g++ broke ABI recently by changing the mangling algorithm.
>> Even so the algorithms should be reasonably easy to discover
>> (especially for g++). There would remain the issue of telling OOC
>> which algorithm to use.
>
>
> I suppose that might work. Within the current oo2c implementation it
> would need to be possible to access the mangled symbols from C code. I'm
> not sure if that's possible - presumably the compiler writers take steps
> to prevent the mangled names colliding with the mangled symbols.
Ooops! What I meant was that the compiler should prevent "C" names
colliding with the mangled symbols.
> Certainly, it would be possible at the asm/linker level.
Looks like for GCC it is possible from "C". Given this definition:
class Test {
int add(int a, int b);
};
int Test::add(int a, int b) {
return a + b;
}
The compiler generates a symbol named "__ZN1T3addEii". With the
appropriate definition, this can be called from "C":
#include <stdio.h>
extern int _ZN1T3addEii(void * obj, int a, int b);
int main(int argc, char ** argv) {
printf("test -> %d\n", _ZN1T3addEii(NULL, 2, 3));
}
Some compiler mangle the names using symbols that are not valid within C
identifiers, which would prevent this approach from working. See some
examples here:
http://en.wikipedia.org/wiki/Name_mangling#How_different_compilers_mangle_the_same_functions
Cheers,
Stewart
|