Custom C Compiler for homemade instruction set?

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???

6 answers
sort by: active | newest | oldest
Jul 24, 2011. 10:22 PMkelseymh says:
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.
Jul 25, 2011. 11:03 AMorksecurity says:
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.
Jul 25, 2011. 11:08 AMkelseymh says:
If you don't know, then follow Steve's advice.
Jul 25, 2011. 2:29 AMsteveastrouk says:
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.

Steve

Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!