Introduction
I haven't posted anything The Disintegrated Machine-related lately. Work and other projects got in the way, but now I have an update to share. I have a pretty functional toolchain ready. It includes binutils, gcc and newlib. At the moment only the C compiler is working, but from here, bringing up at least the C++ compiler should be a relatively short put (although GCCs exception handling is crazy enough that it would probably not work at all on the Disintegrated Machine).
How did I do it?
Porting GCC is not a simple task. I did it once for another CPU design, called Brew. That was a rather conventional RISC machine with some quirks that were not terribly relevant for a compiler. So, porting GCC was a relatively uneventful affair and it took me many many weeks. Maybe three months total?
This time around, I used Claude. I asked it to do the port for me and it completed it in less then a week, with me spending - maybe - a couple of hours reviewing its work. I have a subscription, so it costs me $17 a month to have this service. A week thus costs me a little over $4, or less then a cup of coffee these days.
During this work Claude have found (and fixed) several bugs in my MMU logic. It has found a few bugs in binutils and GCC itself. It suggested solutions to problems I haven't even though of.
My jaw is on the floor.
What did it take?
As I've said, I have already ported GCC for a different processor, so I have a feel for what it takes. At the same time, The Disintegrated Machine is quite a different beast from your roll of the mill RISC processor. It's closer to the 8-bit processors of the 70-s and 80-s, like the 6502. It has very few registers, but makes up for it by a huge pile of quirks.
The important ones to note are:
- It doesn't have a HW stack. It has a register that is ear-marked for the stack-pointer, but no dedicated push/pull logic
- It can do one memory access and one register access per instruction (with SWAP/SWAPI being the one exception).
- It can't load registers with arbitrary immediate values. This includes the
$PCas well, which means that there are short (relative) and long (absolute) jumps - The first 32 memory locations can be directly accessed by any instructions
Working around all these peculiarities means a few things. First off, I decided to use the first 32 memory locations (the ones that can be accessed directly) as special 'memory registers'. This helps to relieve register pressure in the compiler as otherwise it would only have access to two registers ($R0 and $R1). These 'memory registers' are named $MRxx, but of course they can be accessed simply as memory locations, in fact the 'memory register' is just an assembly trick, it compiles to exactly that: a memory access.
With that we end up with two register classes (one for real registers and one for memory registers). Every instruction can access one of each, but not two of the same kind. If a normal memory access is also involved, only $R0 or $R1 can be used. Even $R0 and $R1 aren't made exactly identical. You see how the instruction set becomes rather non-orthogonal even though you wouldn't immediately notice that just by looking at it.
Furthermore, register-offset addressing is supported, so 'please read the memory location 5 places away from $R0) is something the CPU can do, but only if the offset is within the -32 ... 31 range. This is great for accessing automatic (stack-based) variables, but only if the stack-frame is small. Again: a stumbling block. Depending on the stack-frame of the function loading and storing these variables need to be done differently. The same goes for struct member accesses (these would go though $R0 as the base register).
The fact that you can't really load any arbitrary value into a register means that you have to store the value in the vicinity of the load instruction, use a $PC-relative load and make sure you jump over the value that is now stored within the instruction stream. You can group such constants into 'pools', but you have to make sure the pools are reachable from all instructions that need it.
Pretty much all of these have precedence in the GCC code-base, but in a rather scatter-shot fashion, so a port would need to 'borrow' from many different implementations. Something I wasn't looking forward to dealing with.
Finally, here's the big kicker: the machine doesn't support byte-addressing. Every (machine) pointer is addressing a 16-bit quantity. So how would one implementation char *? Yet again, I've seen how others deal with this before: the Cray PVP machines had similar issues. The solution there just as in here is to differentiate C-style pointers from machine pointers and 'simulate' byte addressing. Shift the C pointers back and forth a bit to make the word addresses, then mask/shift the data from loads and stores according to the MSB of the pointer. This I knew. What I didn't appreciate, even though I should have as the Cray machines have the same problem that this means that C-style pointers are limited to 32kWord reach, half the address space.
This is not a big issue for user programs as - first - the MMU remaps their memory to somewhere within the core memory region, - second - they have to coexist with the OS in the total physical address space, which is 64kWord, so they can't really grow much larger than 32kWords anyways and - three - the core memory I have is only 16kWords large.
It is a much bigger problem for the system code (the OS) as that would really like to have access to the whole addressable memory of the machine.
This is a fairly large pile of problems, which really made me not looking forward to the GCC port. Which is why I thought, I might as well take Claude out to a spin and see how it performs.
Results
Let's look at a rather trivial piece of C code:
int f(int a, int b) { return a+b; }
int main(char **argc, int argv) {
return f(argv, *(int*)(argc));
}
Don't judge me (and don't try to run this). The reason for the pointer gymnastics is to make sure GCCs optimizer can't really reason its way around the code enough to eliminate everything. The reason it's int and not your typical 'hello world' printf is that char math on this machine is ridiculously expensive so I wasn't immediately interested in that.
Here's what the compiled code looks like in -O0:
.file "mini.c"
.text
.p2align 0
.global f
.type f, @function
f:
add $sp, -2
mov [$sp+1], $r0
mov $r0, [$sp+1]
add $r0, [$sp+3]
add $sp, 2
mov $pc, [$sp]
.size f, .-f
.p2align 0
.global main
.type main, @function
main:
add $sp, -3
mov [$sp+2], $r0
mov $r0, [$sp+2]
ror $r0
mov $r0, [$r0]
mov [$sp+1], $r0
mov $r0, [$sp+4]
mov $r1, [$pc+(.Llit1-.)]
mov [$sp], $r1
swap $pc, [$sp]
add $sp, 3
mov $pc, [$sp]
.Llit1:
.word f
.size main, .-main
.ident "GCC: (GNU) 17.0.0 20261023 (experimental)"
First off, you see the implementation of f. It creates a stack frame, loads the two parameters from the stack (the second one is fused with the addition), then the end result is returned in $R0 as it should be per the ABI on this machine.
The main routine needs to do some C-pointer-to-machine-pointer conversion (thur the ror $r0), push the arguments on the stack and call the function. The function call is a bit hard to find, it's the swap instruction. After that call returns, we can destroy our own stack-frame and return the return code in $R0.
To call f we need to do a far call (not really, but GCC can't tell that for some reason). This means the creation of a 'literal pool', called .Llit1. This contains the address of f and this value is loaded by the instruction mov $r1, [$pc+(.Llit1-.)].
Pretty neat. Even neater is the optimized version. With -O2, I get the following:
.file "mini.c"
.text
.p2align 0
.global f
.type f, @function
f:
add $r0, [$sp+1]
mov $pc, [$sp]
.size f, .-f
.section .text.startup,"ax",@progbits
.p2align 0
.global main
.type main, @function
main:
mov $r1, [$sp+1]
ror $r0
add $r1, [$r0]
rol $r0
mov [$sp-1], $r1
mov $r0, [$sp-1]
mov $pc, [$sp]
.size main, .-main
.ident "GCC: (GNU) 17.0.0 20261023 (experimental)"
You see how f got dramatically simplified: gone is the stack frame creation and destruction. The first argument arrives in $R0, the second on the stack. So a single instruction can compute the end result and a second one accomplishes the return. That's it: two instructions! Looking at main, you see some simplification as well. Most importantly, there's no call to f at all. It got inlined. This removes a whole lot of stack frame creation and destruction, the call, the return, all of it. What's left is the C-pointer conversion (the ror and rol), the inlined addition, the moving of the result to $R0 and the return. Seven instructions.
Conclusion
This obviously is one trivial program. I will keep looking at the generated code and try to find ways to make it better. For instance, I suspect that there's quite a lot of stack manipulation for handling the saving and restoring of the $MR registers, if they end up being used. I also suspect that having a function library for char* manipulations is going to cut down code size quite a bit, but I'm not sure how Claude has it implemented at the moment. I know that there are quite a few programs in the GCC test suite that don't fit in memory.
So, more work for sure, but given how little it took to get here and how well the end-result seems to work, I'm amazed.
P.S.
I should point out a few things, I think. One reason this project went so well was that I did a lot of upfront thinking about these problems already. I had the ISA of the CPU defined, documented and implemented. I had a simulator and even an assembler written in Python as well as an RTL implementation. These served for Claude as executable specs, it could verify its work against them.
A second reason is GCC is a very well understood, stable project. Thousands of commits, years and years of history, huge library of forum discussions and a diverse pool of ports exists, all of which was ingested by the underlying LLM, Opus 5.5 during training. GCC is also quite well documented and has a large pile of unit-tests and a framework to run them. This too helped Claude to start from a good understanding of how GCC works as well as a way to verify its output.
Lastly, I like to think that my experience of going through a GCC port in the past allowed me to guide the session well. I understood what Claude was doing, if not down to every patch, but at least at a high level. I understood what questions it asked and why, what steps it took and how those steps were implemented.
So, yes, this was a very positive experience with coding using AI. I'm not 100% certain the experience would carry over to other projects, but I have to point out something: Claude found several bugs in my MMU implementation. This only exists in RTL, using a Python, called Silicon, that I have written myself. It is not well documented, it is not widely used, in fact, I didn't even tell Claude what it was for. Still, it read the sources, figured out it was a hardware description language of sorts, treated as such and looked at source code through those lens. It found bugs that are only bugs in terms of HW implementation (signal setup violations and combinatorial loops) and suggested fixes that only make sense in the world of HW. It could have just as well suggested pure Python fixes, which would have eliminated the problems but would not have any meaningful HW implementation. It didn't. Which, I think, was rather amazing.
Leave a comment
Comments are reviewed before they are published, so yours will not show up right away.
You can use
**strong**and*italic*text,`code`, and lists (lines starting with-or1.). Everything else, HTML included, is shown as it was typed.