weird function

Hi,
I have tried to compile a simple cpp file with llvm-gcc. I have attached
the assemble file here.
I see a weird thing there. There are some call instructions, they call the
function "@_Znwj". But this function was only declared in the file, there is
no definition of it. So, how can it work, if this function is "empty" ?

Thanks for any advice.

Quang

simple01.ll (15.2 KB)

I have tried to compile a simple cpp file with llvm-gcc. I have attached
the assemble file here.
I see a weird thing there. There are some call instructions, they call the
function "@_Znwj". But this function was only declared in the file, there is
no definition of it. So, how can it work, if this function is "empty" ?

When you create a final executable using llvm-g++ it links with some standard
C++ libraries. Probably @_Znwj is defined in one of those libraries.

Ciao,

Duncan.

_Znwj is aka "operator new". You need to link to libstdc++.

-Chris

Hi,
thank for your answer. Is there any document/tutorial about linking
bytecode file with libstdc++ avaiable ?

Quang

Hi,

thank for your answer. Is there any document/tutorial about linking
bytecode file with libstdc++ avaiable ?

first you need to compile the bitcode to assembler using llc.
For example:
  llc bitcode.bc
This produces bitcode.s. Turn this into a executable using:
  llvm-g++ -o bitcode bitcode.s
or
  g++ -o bitcode bitcode.s
It doesn't matter which g++ you use here. Using g++ means
that your program will automatically be linked with libstdc++.

I hope this helps,

Duncan.

Thank Duncan,
what I actually want, is to implement a simple processor simulator, which
can execute LLVM bitcode. So it must deal with the virtual LLVM instruction
set.
As I compiled a C or C++ program using llvm-gcc or llvm-g++, I have a
bitcode file. If a C++ library is used, some functions will be declared
inside it, but not defined there (like the @_Znwj). Is it possible to create
LLVM IR with all libraries defined inside (or linked with another bitcode
libraries) ?
Maybe it looks like that:

....
%tmp183 = tail call i32 (i8*, ...)* @printf( i8* getelementptr ([13 x i8]*
@.str1, i32 0, i32 0), i32 %tmp182 ) nounwind

tail call void @_ZdlPv( i8* %tmp432.i312 ) nounwind
....
define i32 @printf(i8*, ...) nounwind {
... // LLVM instructions
}
define i8* @_Znwj(i32) {
... // LLVM instructions
}

Thank for any advice
Quang

Hi Quang,

As I compiled a C or C++ program using llvm-gcc or llvm-g++, I have a
bitcode file. If a C++ library is used, some functions will be declared
inside it, but not defined there (like the @_Znwj). Is it possible to create
LLVM IR with all libraries defined inside (or linked with another bitcode
libraries) ?

I don't know how hard it would be to compile the C++ standard library to
bitcode. I've done this for Ada (which also has a substantial runtime
and standard library): compiling the runtime and standard libraries to
bitcode; then linking bitcode for some program with it, optimizing the
linked bitcode (LTO), and producing a final executable.

Ciao,

Duncan.

@duncan: thank for your advice :),

Has somebody already tried to compile libstdc++ to LLVM bitcode ? is there
any help document for it ?

Thanks for any help
Quang