PDA

View Full Version : [Tutorial] How to compile optimized C, C++ Code for GBA


knizz
February 12th, 2011, 06:49 AM
There is a compiler framework called LLVM. It's faster than GCC and opimizes code more aggresively and the llvm-frontend 'clang' has much better error-messages. I chose it to get rid of the devkit-dependency. (Which I didn't manage but ... at least I have fast code now.)

My clang version doesn't support arm (not because it can't but because the pre-compiled version doesn't include it.)
Fortunately the LLVM-Output is platform/cpu/os-independent. So I compile for x86 first and the override the cpu-type (i think clang ignores -march=thumb) when using llc.

http://llvm.org/docs/Passes.html lists all options for optimizations. They are supposed to be used with clang when it performs *all* actions. When you do linking, etc. yourself you probably have to give the optimization options to llvm-ld too.

clang -O4 -S -emit-llvm -march=thumb file1.c file2.cpp
llvm-as-2.8 file1.s
llvm-as-2.8 file2.s
llvm-ld-2.8 -o rom file1.s.bc file2.s.bc
llc-2.8 -O=3 -march=thumb -mcpu=arm7tdmi rom.bc
arm-thumb-elf-as -mthumb-interwork -o crt0.o crt0.S
arm-thumb-elf-as -mthumb-interwork -o rom.o rom.s
arm-thumb-elf-ld -q -Ttext=0x8000000 -Tbss=0x3000000 -o rom.elf crt0.o rom.o
arm-thumb-elf-strip -s rom.elf
arm-thumb-elf-objcopy -O binary rom.elf rom.gba
gbafix rom.gba

I use the crt0.S from http://velvetfr.ath.cx/gba/tutorial/gba-tutorial1.html

IIMarckus
February 12th, 2011, 02:13 PM
Fortunately the LLVM-Output is platform/cpu/os-independent.This is not correct. (http://llvm.org/docs/FAQ.html#platformindependent) But it may (or may not) be close enough for your purposes; I don’t know enough about LLVM to comment.

I do like LLVM. It’s nice to finally see some competition to GCC, which is a bloated mess and a nightmare to build.

knizz
February 12th, 2011, 02:30 PM
This is not correct. (http://llvm.org/docs/FAQ.html#platformindependent) But it may (or may not) be close enough for your purposes; I don’t know enough about LLVM to comment.

I do like LLVM. It’s nice to finally see some competition to GCC, which is a bloated mess and a nightmare to build.

It simply means that the only platform-limitation is in your own code and not the compiler or llvm-representation. In other words: Your code will only work on the GBA when you write for GBA.

Platform-independent enough for my purposes.

Full Metal
March 1st, 2011, 05:36 PM
ohai how did I not see this thread?
nifty nifty, but does it have things like malloc and new built in (for C/C++ respectively)?
I mean, making my own malloc in asm would be as simple as using my FS code and making it into code that...works on a GBA, but you know, it's nice to avoid that. haha. I sound like I don't know what I'm talking about, but I do! ... yea! XD

Silent Crest
March 4th, 2011, 06:32 PM
Okay. So LLVM is a compiler. So what?!?

There's a lot of C/ C++ compilers. When I used to write c/ c++ I used Bloodshed Dev. My question is (since I don't know a lot about rom hacking) what does the LLVM have to do with Rom Hacking?

Full Metal
June 12th, 2011, 05:09 PM
It takes C or C++ code...and compiles it.
You see, if you actually knew what you were talking about, you would know that devkitpro/advance generates HUGE and very terrible code ( as far as assembly and instructions go atleast ). Even optimizations don't do that much for it.
What this thing does is makes actual optimized code. IDK if it does anything about file-size compared to devkitpro ( I haven't used this thing just yet -- still -- ), but it definitely increases over-all performance of the .elf files generated. That's my comprehension anyways. :)