Hacker News new | past | comments | ask | show | jobs | submit login
Show HN: Simple virtual machine, assembler, and C compiler (github.com)
99 points by ronsor on Dec 7, 2016 | hide | past | web | favorite | 14 comments



Nice tiny little project, but the headline is a bit clickbait. I wouldn't really call it a C compiler, all it does is read a file, look for 3 or 4 hardcoded strings like "int" or "char*" or "return" and generate some really simple labels/movs in consequence.


No kidding, it's barely got a parser.

C4[1] on the other hand, is still tiny enough to comprehend, does have a pretty complete parser, and probably wouldn't be too difficult to modify to generate code for this VM.

[1] https://news.ycombinator.com/item?id=8558822


Wow, calling yourself "C in 4 functions" when those functions are effectively minimized and with not a whole bunch of whitespace is a bit cheeky. Might as well strip out all but four newlines and call it "C in 4 lines". :P

Still looks well worth a read, though.


They're not minimized. They use slightly short variable names, but there's few of them and they make sense very quickly. It does use a few unnecessarily clever tricks, like the opcode selection as indexes into a string, but other than that, as is pointed out in that thread: if you've read a Wirth-style compiler before (or Wirths books on compile construction, which will have you read a Wirth-style compiler...) it becomes fairly simple to understand.


The code style really hurts to read, but this is a pretty cool demo of how to do this. Congrats ronsor, good work!

Don't try to run anything in the VM that's larger than 4K though right now... ;)


The style looks fine to me, it's typical C, although the indentation is a bit inconsistently weird in some places. Perhaps you're used to a different style/language?


It's the obtuse crappy macros -- I need to use fewer macros.


You aren't reading it very closely then. There's a ton more issues than indentation. Check the definition of the run() function and come back to me.


I'm on my phone so I didn't get a good look, but it looks like this compiles a subset of c? Is this accurate?


It emulates a cpu, and custom assembly code is compiled and run. A simple c compiler is in development though


Cool project. Virtual machines and assembly are always fun.

Not sure if the C compiler is quite ready for the headline, though. I was trying to get the test.c program to run on the vm, but after compiling and assembling, the vm just hangs. Is that expected?


Yes. Its a bug with 'ret' - try test2.c


I tried with test2.c too, it hangs there too. Could you please update the usage section and also if possible add screenshot of the output.


FYI, your first function in `rcc.c` returns a pointer to a stack-allocated array, which will likely cause you problems.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: