When reading some other questions about compiling C for the Z80,
I am getting the impression that it is hard to compile C to Z80 and end up with well-optimised code. Is that the case, and why?
I know more about the 6502. Here are some examples that shows why C fits 6502 badly:
An array in C is indexed by an integer type. The 6502 is pretty quick at indexing arrays, but unfortunately only if the index is one byte wide. So something like
strlenmight need to actually do a 16-bit add per character.
A stack is an ideal data structure for passing function parameters. The stack's limited to 256 bytes and the 6502 has rather limited stack addressing modes as compared to the PDP-11, so cc65 uses a second stack, implemented in software, to pass parameters IIRC.
From what I understand of the Z80 these two examples do not apply. Z80 has these index registers and a much roomier stack. So what are the reasons C fits badly?
Summary of answers given thus far
Array indexing is inefficient since there's no reasonable
base + offsetaddressing mode that can use a variable offset, which means the compiler needs to generate code to do the address calculation.
Similarly, it's awkward to save and restore function arguments on the stack because there's no
stackpointer + offsetaddressing mode. The same goes for the local variables of non-leaf functions.
Paucity of truly general purpose registers. Most registers have specific uses which complicates register allocation.
The ISA makes it difficult to mix 8-bit and 16-bit operations.