LLVM Z80: Building

Steps

  1. Clone the github repo (This is quite big, so make some disk space)
git clone https://github.com/gt-retro-computing/llvm-project
  1. Setup the cmake project by going into the llvm sub-directory and create a build directory
# ...../llvm-project/llvm>
mkdir cmake-build
cd cmake-build

cmake -DLLVM_USE_LINKER="lld" -DLLVM_TARGETS_TO_BUILD="" -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="Z80" -DLLVM_ENABLE_PROJECTS="clang" ../

make clang llvm-mc llc lld -j$(nproc)
  1. Step 2 will compile a clang for Z80 in the "build"/bin directory. P.S. Feel free to use an IDE like CLion, it might be very helpful when reading LLVM source code.

  2. You can try to compile a simple C program for example

char testFn(char var1, char var2) {
  return var1 + var2;
}

Save it as test.c then you can do

clang --target=z80-unknown-none-code16 -fintegrated-as -O3 -c -S -o - test.c

It should output something like this

  .text
  .file "test.c"
  .globl  _testFn                         ; -- Begin function testFn
_testFn:                                ; @testFn
; %bb.0:                                ; %entry
  ld  l, a
  ld  a, b
  add a, l
  ret
                                        ; -- End function
  .addrsig
  extern  __Unwind_SjLj_Register
  extern  __Unwind_SjLj_Unregister

Compared to the same code generated by sdcc

;test.c:1: char testFn(char var1, char var2) {
; ---------------------------------
; Function testFn
; ---------------------------------
_testFn:
;test.c:2: return var1 + var2;
  ld  hl, #3
  add hl, sp
  ld  iy, #2
  add iy, sp
  ld  a, 0 (iy)
  add a, (hl)
  ld  l, a
;test.c:3: }
  ret
  .area _CODE
  .area _INITIALIZER
  .area _CABS (ABS)