← Back to context

Comment by MaxBarraclough

5 years ago

Seems like an IR would make sense here. Perhaps a stack-based one. If you need a compact program representation, that doesn't mean you have to use FORTH as your source language.

A Forth word is simple enough that defining any other level of handling just isn't necessary. Trying to put an IR in would just be another layer and an unnecessary complication.

Using Forth gets pretty close to being the most compact representation all by itself, there's literally no extra tooling needed. No compiler or other translations, it's all just there in the words.

I think the argument against a custom IR is that once you start optimising your IR for compactness you'll probably end up near Forth in the design space anyway. As you say perhaps making the IR stack-based.

Once you're at that point you may as well just use Forth, especially since it's got a proven ability to work in these kinds of resource contrained, self-hosting/self-bootstrapping environments.

  • > you'll probably end up near Forth in the design space anyway

    Good point. Related to this, FORTH can be treated as a target platform for compilers, although I don't think there are many mature compilers that do this.

    I wonder if CollapseOS will ever seriously target heavyweight platforms like x64 (and not just through emulation with libz80). I suppose that's out scope, but it would open the door to JIT.

    > you may as well just use Forth

    Presumably it could be a little more compact if a less human-readable variation were used, no? FORTH uses DROP and THEN, which could be shortened at the cost of readability.

Forth basically is IR between high level and whatever is your concept of "low level". The nature of forth is that forth words can be written as composition of other forth code or syscalls to native code or as chunk of native code.

I say "concept" because you can run forth code on an interpreter or any number of semi-compiled or compiled approaches that get more and more closer to assembly.

For a more optimizing compiler for a machine with more registers, you might, to be able to store the stack in the registers. The Z80 has few enough that I can't see it being an improvement.