[LLVMdev] libstdc++ as bytecode, and compiling C++ to C

Reid Spencer rspencer at reidspencer.com
Mon Nov 20 08:01:23 PST 2006


On Mon, 2006-11-20 at 17:49 +1100, Emil Mikulic wrote:
> I've compiled all the object files that make up libstdc++ and libsupc++
> into LLVM bytecode:
> 	http://goanna.cs.rmit.edu.au/~emil/libstdcxx.tar.bz2 (438KB)
> 
> A simple test program, x.cpp:
> 
> 	#include <iostream>
> 	int main() { std::cout << "hello world\n"; return 0; }
> 
> $ llvm-g++ -emit-llvm -c x.cpp
> $ llvmc -o=out x.o std/*.o sup/*.o
> $ lli out.bc
> Segmentation fault (core dumped)
> $

llvmc isn't a completed tool. In particular it doesn't handle native
linking properly. However, I'm not sure what it would do in your example
above that would cause the program to seg fault. Can you get a stack
trace?

> 
> Oops, no go.  Try a different way:
> 
> $ llvm-g++ -emit-llvm -c x.cpp
> $ llvm-link -o=linked.o x.o std/*.o sup/*.o
> $ lli linked.o
> hello world
> $
> 
> So far, so good!  Compile to C:

llvm-link should work fine because its just dealing with bytecode. BTW,
You could put all the std/*.o and sup/*.o files into a bc archive using
llvm-ar and then just link with that archive.

> 
> $ llc -o=cbe.c -march=c linked.o
> WARNING: this target does not support the llvm.stacksave intrinsic.
> $ gcc cbe.c
> /var/tmp//ccVAM4W2.o(.text+0x329a): In function `operator new(unsigned int)':
> : undefined reference to `__cxa_throw'
> [...and more errors]
> $
> 
> But __cxa_throw is right there, in sup/eh_throw.o, and in linked.o, it
> just isn't being emitted as C code.  (at this point, I suspect the
> problem might be that I don't quite understand how some of the llvm
> tools are intended to work - e.g. llvm-link vs llvmc)

CBE doesn't support exceptions ?

> Strangely enough:
> 
> $ llvm-g++ -emit-llvm -c x.cpp
> $ llvm-link -o=linked.o x.o std/*.o sup/*.o
> $ llvmc -o=out linked.o
> $ llc -o=cbe.c -march=c out.bc
> WARNING: this target does not support the llvm.stacksave intrinsic.

CBE doesn't support llvm.stacksave

> $ gcc cbe.c
> $ ./a.out
> hello world
> $
> 
> Works!

However it doesn't seem to matter for this program.

> 
> --Emil
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list