Author Options:

Custom C Compiler for homemade instruction set? Answered

I'm designing a relay based computer.  It will be an 8-bit cpu made entirely out of relays, except for the RAM and program memory.

I need an instruction set, but I am not crazy enough to try and make it run the x86 instruction set or anything like that, so I am making my own.  It is a fairly basic set, only 60 instructions, with a quarter of them being data movement. I tried compiling some basic computation programs, like a 16-bit add and an 8-bit multiply program. It is a pain trying to compile it so I was wondering if there is a way to plug my instruction set into a compiler, such as gnu's gcc.  I don't even need it to create a machine code file, I just need the assembly.

Is something like this possible??? and how hard would it be to implement???



Best Answer 6 years ago

It's certainly possible. You could even consider doing it by starting from the GCC source code and modifying it. But you probably want to write your compiler using YACC, not try to code it yourself in C.

How hard would it be to try and do that and how would I go about doing it??

Thanks for any help

Note: Another solution would be to simply build up a set of assembler/hardware-level subroutines and/or macros you could plug into your applications. Not as pretty as a compiler, but it'll save you from having to rewrite things like the add and multiply once you get them running -- save them as useful routines and plug them in when needed. On a small and slow machine, where you're going to need all the performance you can get, there's a lot to be said for coding down at the assembler level... or at worst in a language like C, which can be handled almost as a "high-level assembler" if you restrict yourself to a subset of the language.

Will your machine have a call/return/parameter stack and subroutines, or will it all be inline code? I strongly suspect that GCC, or any other existing compiler, will be designed around stack machines. If you don't have a stack you can write code to implement one -- we sorta had to do that for serious 6502 programming, since its hardware stacks were very small and really intended for local/microcode use.

Thank you so much! That is definitely a possible route, and one I will likely follow.

I'm still designing it and I've made like a million changes to the designs :D, but so far I've been tossing the idea of possible stack. It won't be pretty and it would probably be overly complex yet ridiculously simple, but hopefully it will work. If I can't get a working stack, then it will obviously be inline, so I would just use a bunch of jumps to all of the addresses.

You are probably right that GCC is based around stack machines. Most everything uses a stack...hmmm...

If you don't know, then follow Steve's advice.

Building a viable computer is tough enough, writing a compiler is an order of magnitude harder. Use Yacc - and get some books on writing compilers.