• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Forum moderator applications are now open! Click here for details.
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

How to compile optimized C, C++ Code for GBA

knizz

192
Posts
16
Years
  • Seen Oct 28, 2020
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.

Code:
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
 
Last edited:

IIMarckus

J946@5488AA97464
402
Posts
16
Years
  • Seen Feb 21, 2024
Fortunately the LLVM-Output is platform/cpu/os-independent.
This is not correct. 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

192
Posts
16
Years
  • Seen Oct 28, 2020
This is not correct. 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

C(++) Developer.
810
Posts
16
Years
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

Celestial Shine
40
Posts
13
Years
  • Age 28
  • Seen Aug 29, 2012
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

C(++) Developer.
810
Posts
16
Years
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. :)
 
Back
Top