Skip to content

Z80 and 8080

The fork has a backend for the Zilog Z80 and the Intel 8080 and 8085 (llvm/lib/Target/Z80), one backend registered as two targets: z80 and i8080 (i8085 is a name for i8080). It is experimental: build it with -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=Z80. It is used for CP/M-80, with z80-unknown-cpm and i8080-unknown-cpm (cpm-sdk).

ABI

  • char is signed, short and int are 16 bits, long 32 and long long 64; pointers are 16 bits. Everything is aligned to 1 byte. float, double and long double are all IEEE single, in software (compiler-rt), as in most compilers for 8-bit processors and avr-gcc; -mdouble=64 makes double and long double IEEE double, which needs a C library built the same way.
  • The first two arguments are in HL and DE (L and E for a char); the others, and all arguments of a variadic function, are pushed, the last first, and the caller pops them. Values come back in HL, with a 32-bit value in HL (low) and DE (high); bigger values are returned through a pointer that the caller passes. Structures are passed by pointer.
  • BC is kept across a call: a function that uses it pushes it on entry and pops it before returning. IX is kept too: on the Z80 it is the frame pointer of a function whose stack frame, with its stack arguments, fits in 256 bytes, reached as (ix+d) with IX pointing up to 128 bytes into the frame. Larger frames, and all frames on the 8080, are reached through HL (ld hl,d; add hl,sp). A, DE, HL, IY and the flags are not kept.
  • 16-bit multiplication and division, shifts by a variable count, and indirect calls (__z80_call_bc: the address in BC) are calls into compiler-rt's builtins, which have 8080 assembly versions of them.

Assembler and objects

The assembler takes Zilog syntax for both processors, and refuses the Z80's instructions (IX and IY, the CB and ED groups, jr, djnz) for the 8080. Objects are ELF with EM_Z80 and the binutils relocations (R_Z80_8, R_Z80_16, R_Z80_8_PCREL and the others), so GNU binutils' tools can read them. lld links them (-m elf32z80); a CP/M-80 program is linked at 0100h with cpm80.ld and written as a .COM file by llvm-objcopy -O binary.

Code

Instruction selection works on 8- and 16-bit values; the 16-bit operations that the processors lack (subtraction on the 8080, logic, compares, shifts) are pseudo-instructions expanded after register allocation, a byte at a time through A, with the Z80's sbc hl, adc hl, srl and rr where they apply. Loads and stores through BC, DE or an address are pseudo-instructions too, so that values do not all have to go through HL. The printf and scanf calls whose format string allows it call picolibc's integer variants, as on the 8086.

The code is correct on the tests the SDK runs (random expressions checked against the host, compiler-rt, picolibc's CP/M-80 layer) but large: the backend does not yet use the Z80's block instructions or 8-bit loops with djnz, and 64-bit arithmetic is big.