Live data from Hacker News

SectorC: A C Compiler in 512 bytes

xorvoid.com

31–40 of 82 posts

Re: SectorC: A C Compiler in 512 bytes

#31
post #2

This reminded me the idea of compilers bootstrapping ( https://news.ycombinator.com/item?id=35714194 ). That is, now you can code in SectorC some slightly more advanced version of C capable of compiling TCC ( https://bellard.org/tcc/ ), and then with TCC you can go forward to GCC and so on.

I’ve considered that. I’ve long been interested in bootstrapping. Lots of unpublished projects in the archive that I should also write up.

I considered writing a self-hosting compiler. It should be doable without too much work. I was going to do one in BarelyC originally.

SectorC -> TCC -> GCC sounds fun. C all the way down, haha

Re: SectorC: A C Compiler in 512 bytes

#32
post #27

wow, this is impressive. I wrote a similar x86-16 assembler in https://github.com/kvakil/0asm/ >. I did find a lot of similar tricks were helpful: using gadgets and hashes. Once trick I don't see in sectorc which shaved quite a bit off of 0asm was self-modifying code, which 0asm uses to "change" to the second-pass of the assembler. (I wrote some other techniques here: https://kvakil.me/posts/asmkoan.html >.) bootOS (…

I considered self-modifying code, but somehow I kept finding more ways to squeeze bytes out. I’m half convinced that you could condense it another 50 ish bytes and add operator precedence or even local vars. But.. frankly.. I was ready to switch my attention to a new project.

Re: SectorC: A C Compiler in 512 bytes

#33
post #22
post #3

I'm wondering if you can build an actual "Linux from scratch" with this as the lowest level, without the need to use a host system at all.

I can't help but wonder if this is written in x86-16 bit mode to implicitly use Real Mode BIOS functions and platform interfaces as well. There's something to be said for taking advantage of that existing code; but if that's a dependency it should be part of the environment manifest. Everything has (most things have) a context where it might be useful and that should be included in the explanation so a tool isn't mis…

It’s written in x86-16 real mode because all immediates are smaller and switching to 32-bit mode takes a fair amount of code.

I would have preferred 32-bit mode because real mode is awkward with its seg:off addressing scheme.

Using the bios functions just allows it to have “not-boring” examples easily.

Re: SectorC: A C Compiler in 512 bytes

#34

I saw the repeating 'A' at the end of the base64 text and thought "it's not even 512 bytes; it's smaller!" That said, the title is just a little clickbaity --- it's a C-subset compiler, and more accurately a JIT interpreter. There also appears to be no attempt at operator precedence. Nonetheless, it's still an impressive technical achievement and shows the value of questioning common assumptions. Finally, I feel temp…

Good tip! Yeah, there’s ~20 bytes unused at the end. I kept finding ways to squeeze out a few more and had to tell myself to stop and just publish it already. You could take this further if you really wanted. But it’s already sufficiently absurd.

Re: SectorC: A C Compiler in 512 bytes

#35
post #8

Pretty nifty, nice work! I'll point out to any passerby that this C doesn't support structs, so it's unlikely you'd actually want to build anything in it.

The C Star (C*) language from the selfie project also does not support structs, yet in 12KLOC of code they implemented a C Star compiler that can compile selfie (and outputs ELF files), an emulator that runs RISC-U (RISC-V subset), and a hypervisor.

Re: SectorC: A C Compiler in 512 bytes

#36

I saw the repeating 'A' at the end of the base64 text and thought "it's not even 512 bytes; it's smaller!" That said, the title is just a little clickbaity --- it's a C-subset compiler, and more accurately a JIT interpreter. There also appears to be no attempt at operator precedence. Nonetheless, it's still an impressive technical achievement and shows the value of questioning common assumptions. Finally, I feel temp…

What does Just In Time mean for an interpreter?

Compiling to machine instructions and then executing the compiled output, instead of executing the AST directly.

Re: SectorC: A C Compiler in 512 bytes

#38
post #33
post #22

Earlier quoted context omitted.

I can't help but wonder if this is written in x86-16 bit mode to implicitly use Real Mode BIOS functions and platform interfaces as well. There's something to be said for taking advantage of that existing code; but if that's a dependency it should be part of the environment manifest. Everything has (most things have) a context where it might be useful and that should be included in the explanation so a tool isn't mis…

It’s written in x86-16 real mode because all immediates are smaller and switching to 32-bit mode takes a fair amount of code. I would have preferred 32-bit mode because real mode is awkward with its seg:off addressing scheme. Using the bios functions just allows it to have “not-boring” examples easily.

That's true, but it's still a sort of library dependency. Even for very tiny computers with a lot of hardware assistance, the basic ROM for communicating with the world in a useful way is bigger.
Post reply on HN