[LLVMdev] cross compiling with the C backend

Nick Lewycky
Mon, 18 Feb 2008 23:48:03 -0000

Kevin André wrote:
> For my master's thesis, I am trying to cross compile programs for the
> PSP (PlayStation Portable) with LLVM and llvm-gcc.
>
> This is what I do:
>
> (1) compile a program and the libraries it uses (libpng etc.) with llvm-gcc
> (2) link the bitcode files with llvm-ld into one file
> (3) run "llc -march=c" on the result
> (4) compile the resulting C source with the PSP toolchain
>
> It seems to work with a very simple program (Pi_Calc, calculates Pi
> and prints it), but it fails when I try it with another program that
> is a bit more advanced: the PSP hangs when I run the program.
>
> Am I using the right approach here? I have compiled llvmgcc-4.2-2.2 on
> my machine as a native compiler (i686-pc-linux-gnu). Does llvm-gcc
> have an impact on the portability of the resulting LLVM bitcode? And
> is the C code generated by the C backend really portable? I use the
> "-nostdinc" option for llvm-gcc and specify the include paths of
the
> PSP SDK instead. But I still get warnings with llvm-gcc that do not
> appear with psp-gcc ("passing argument 2 of 'xyz' discards
qualifiers
> from pointer target type"). And the output of the C back end generates
> lots of warnings as well, or is that expected?

No, it's not portable like that. Here's what goes on.

C is portable in the sense that it gives you enough tools to inspect
your environment. So an 'int' might be any number of chars (which
themselves may be any number of bits >= 8), but that's okay because C
provides 'sizeof(int)'. If you have code like "char *x =
malloc(sizeof(int));" you're going to get different LLVM bytecode
depending on what platform your llvm-gcc is set to.

LLVM is portable in the sense that the bytecode will behave the same way
on every platform. So if the above code becomes "%x_addr = malloc i32"
then you get a 32-bit integer regardless of the abilities of the
underlying system.

Finally, the C backend's output isn't portable in the sense that it uses
GCC extensions to get the correct output. Which is fine so long as
you're compiling its output with GCC.

Building llvm-gcc as a cross-compiler would help, if there's a platform
you can select that matches the PSP more closely. Besides that, the
warning you gave as an example is probably because llvm-gcc 4.2 is a
newer version of gcc than psp-gcc.

Nick