Posted 4 hours ago

/

18 comments

/

retrocomputing.stackexchange.com

4 hours ago by userbinator

As noted in the first post, the Z80 is still much easier to generate code for than the 6502.

I see everyone there seems to be referring to free/open-source compilers, but I know of one commercial compiler for Z80 (IAR) whose output is quite good, and I have linked to in this previous comment I made on a different article about Z80 C compilation:

https://news.ycombinator.com/item?id=18902501

If one didn't know better, it would look almost like handwritten Asm.

On the topic of architectures which are difficult to compile C to, I can also think of several more: 8051, low-end PIC series, and these: https://cpldcpu.wordpress.com/2019/08/12/the-terrible-3-cent...

...and most of those have C (or at least C-subset) compilers too.

3 hours ago by Someone

One thing that, I think, won’t work well is adding a backend to generate code for such much less powerful CPUs to a compiler-linker system designed for more powerful CPUs.

Because saving and restoring register values is fairly expensive (both in time and in memory) you shouldn’t do register allocation per function, or commit to a fixed ABI. The best assembly to generate for a function often will depend on what it calls or where it is called from. Let’s say you need a register or flag to be clear at some time. A function you just called might accidentally already do that, or it might be cheaper to change that function’s code or register allocation to give it the desired side-effect than to have the function that needs the register or flag cleared waste an entire byte to do that.

You basically need a whole-program (compiler-linker) to generate good code for these CPUs.

That also helps in moving local variables to global addresses (you likely will have to, because of limited stack space) and in having such variables share memory locations as much as possible (if foo doesn’t (indirectly) call bar and vice versa, ant hey aren’t recursively called, their local variables can share the same memory addresses. Again, you’ll likely have to, because memory is limited)

3 hours ago by MaxBarraclough

> You basically need a whole-program (compiler-linker) to generate good code for these CPUs.

Modern compilers can do this, 'link time optimisation'.

4 hours ago by smcl

So if C paradigm doesn't fit the Z80 architecture, does anyone know if there's a language (other than Z80 assembler) that does. Or do Z80 enthusiasts generally just write everything in asm?

21 minutes ago by masswerk

What about PL/M, the language invented by Gary Kildall in 1973 and used to implement CP/M?

(PL/M targets, like CP/M, the 8080 instruction set, of which the Z80 provides a superset.)

https://en.wikipedia.org/wiki/PL/M

3 hours ago by zozbot234

It's not so much the 'C paradigm' that doesn't fit these machines, but really the ABI features C relies on to support separate compilation units. You could have a C compiler that's restricted to handling complete programs with no "extern" references of any kind (other than those that might be specified by some custom, asm-like mechanism) and it could absolutely match hand-written asm, given a good optimizer.

3 hours ago by monocasa

Are there any Z80 compilers with link time optimization? This is the exact problem that lto is designed to help with. ie. rather than emitting machine code in the object files (or in addition to), emit compiler IR so you can do additional optimization passes with the whole binary present at link time.

3 hours ago by Gibbon1

One C compiler I used 30 years ago sacrificed reentrancy for variable folding. It did call chain analysis so that it could alias local variable and function parameters to single global scratch pad variables.

That allowed you to write non trivial programs in C for the 8031/32 without external RAM.

2 hours ago by CyberDildonics

It is probably pretty realistic at this point to compile any program for a Z80 into a single compilation unit.

2 hours ago by folmar

You can try Copilot https://copilot-language.github.io/ (which does use C as backend, but this C is restricted to be microcontroller friendly)

an hour ago by pvg

Turbo Pascal was born on Z80s. The Turbo Pascal wikipedia page says Anders Hejlsberg released his first Z80 Pascal in 1981. And BASIC, of course, runs on everything.

3 hours ago by s_gourichon

Well, while certainly imperfect, some like SDCC are good enough to write some games with nontrivial logic.

Here’s an open source game (disclosure: I wrote it) that compiles with SDCC and runs a game involving a flood fill algorithm, a tiny GUI with checkboxes and buttons and only one assembly routine, all running on the Amstrad CPC464 and above: https://github.com/cpcitor/color-flood-for-amstrad-cpc

It’s much easier to write and much faster (and probably smaller) to run than the same in BASIC.

4 hours ago by zabzonk

They produce about as good code as can be produced for any 8-bit (accumulator size) processor (there are some 16-bit instructions for some registers). The PDP-11, for which C was designed, has a 16-bit register architecture for all registers.

Daily Digest

Get a daily email with the the top stories from Hacker News. No spam, unsubscribe at any time.