alexfru / SmallerC

Simple C compiler
BSD 2-Clause "Simplified" License
1.35k stars 155 forks source link

How much work would adding a new backend be? #52

Open AbleTheAbove opened 1 month ago

AbleTheAbove commented 1 month ago

I have been looking for a small c compiler to hack a backend into for my bytecode virtual machine and so far most are not approachable at all..

rofl0r commented 1 month ago

the amount of needed code seems to be roughly 2 KLOC judging by the removal of a backend: https://github.com/alexfru/SmallerC/commit/2a180411235df39bc9df7782b00ed199bedb1fd8 now divide that linecount by your average LOC/h and you know how much work it will be.

alexfru commented 1 month ago

If you're OK with everything (int, float, pointer/address) being 32-bit and smaller (16-bit and 8-bit for smaller integer types) and your architecture is sufficiently regular, it should be relatively straightforward. Look at that removed code generator mentioned above or the MIPS32 code generator (they're similar to one another). Supporting 64-bit types with the current design/implementation of SmallerC is quite hard.

stsp commented 2 weeks ago

the amount of needed code seems to be roughly 2 KLOC judging by the removal of a backend:

Sorry for an off-topic. I followed the link of a back-end removal, and started to wonder what was that Trillek back-end all about. So I've found this: https://trello.com/b/ONRIy13Q and this: https://github.com/trillek-team/trillek-computer And finally this: https://trillek.space/

So am I right that Trillek is a computer game about a space-ship, which has an on-board computer, for which they implemented an actual specs and a simulator, and @alexfru wrote a back-end in smallerc? For a virtual computer inside a computer game??? Come on. :) What was the story?

alexfru commented 2 weeks ago

I know nearly as much about their project.

stsp commented 2 weeks ago

I would believe such an answer if writing a new smallerc back-end could be done in 10 minutes, using only the (very vague) published specs. But oh well, let's keep the mystery. :)

rofl0r commented 2 weeks ago

afaik it's about the DCPU-16 designed for notch (minecraft author)'s planned game 0x10c. when specs of the CPU were released it caused quite a hype mainly due to the popularity of minecraft and there are dozens of emulators on github, including a C compiler in python, an assembler for vasm, and the one in this repo.

stsp commented 2 weeks ago

Interesting! I've heard people writing serious software for vintage computers, but the C back-end for the fictional computer - this is over my head. :)

alexfru commented 2 weeks ago

The CPU spec was sufficient and the instruction set was, AFAIR, close enough to MIPS, so the code generator took about a day to complete. No mystery.