Top
Best
New

Posted by guerrilla 4 hours ago

Tiny C Compiler(bellard.org)
123 points | 55 comments
ZoomZoomZoom 3 minutes ago|
One of the coolest tricks is using tcc to compile "on demand." This allows you to use a compiled language like Nim for scripting, with almost no noticeable performance difference compared to interpreted languages.

  #!/usr/bin/env -S nim r --cc:tcc -d:useMalloc --verbosity:0 --hints:off --tlsEmulation:on --passL:-lm
  echo "Hello from Nim via TCC!"
Here's a comparison (bash script at [2]) of a minimal binary compiled this way with different interpreters. First line is the noise. Measured by tim[1] written by @cb321.

  1.151 +- 0.028 ms     (AlreadySubtracted)Overhead
  1.219 +- 0.037 ms     bash -c exit
  2.498 +- 0.040 ms     fish --no-config --private -c exit
  1.682 +- 0.058 ms     perl   -e 'exit 0'
  1.621 +- 0.043 ms     gawk   'BEGIN{exit 0}'
  15.8 +- 2.2 ms     python3 -c 'exit(0)'
  20.0 +- 5.7 ms     node   -e 'process.exit(0)'
  -2.384 +- 0.041 ms tcc -run x.c
  153.2 +- 4.6 ms     nim r --cc:tcc  x.nim
  164.5 +- 1.2 ms     nim r --cc:tcc -d:release x.nim

[1]: https://github.com/c-blake/bu/blob/main/doc/tim.md [2]: https://gist.github.com/ZoomRmc/58743a34d3bb222aa5ec02a5e2b6...
fjfaase 1 hour ago||
The code of TCC (0.9.26) is kind hard to compile, I have discovered in the past year, while developing a minimal C compiler to compile the TCC sources [1]. For that reason, I have concluded that TCC is its own test set. It uses the constant 0x80000000, which is an edge case for if you want to print it as a signed integer only using 32-bit operators. There is a switch statement with an post-increment operator in the switch expression. There are also switch statements with fall throughs and with goto statements in the cases. It uses the ## operator where the result is the name of a macro. Just to name a few.

[1] https://github.com/FransFaase/MES-replacement

ForOldHack 23 minutes ago|
You have simply made one tiny step, that the guys who used AI and $25,000 to write a C compiler in Rust, could not make:

You are using the compiler to compile itself.

"TCC is its own test set." Absolutely brilliant.

gnufx 2 hours ago||
Used in the impressive Guix bootstrap.

https://guix.gnu.org/manual/1.5.0/en/html_node/Full_002dSour...

guerrilla 2 hours ago|
Riiiight, I forgot about htat.
veltas 2 hours ago||
The unofficial repo continuing tcc has geoblocked the UK.

https://repo.or.cz/tinycc.git

jeremyjh 38 minutes ago|
This is the most rational response to poorly written laws. If everyone did it, maybe they would repeal that law.

https://repo.or.cz/uk-blocked.html

problynought 28 minutes ago||
The most rational response to poorly written laws is collective action against government that wrote them.

But that would require terminally online frogs acting in their collective interests, not isolating at home hoping the heat never reaches them.

zbentley 3 minutes ago||
The copy on the linked "UK geoblocking" page doesn't contradict that, though.

The authors say, basically, that there's a risk of prosecution in the UK that would financially devastate anyone that works on the project, and that the act of determining how to comply with UK laws is itself an extremely resource-intensive legal task that they can't or won't do. In other words, they're geoblocking the UK not out of activism but out of pragmatic self-preservation.

That's not in any way mutually exclusive with collective action.

...also, couldn't deciding to geoblock the UK be a form of collective action? If that's what you originally meant, I sincerely apologize for reading it backwards.

haunter 3 hours ago||
There is an actively maintained fork with RISC-V support and such

https://repo.or.cz/w/tinycc.git

https://github.com/TinyCC/tinycc

csb6 3 hours ago||
I've never seen another repo with public commit access like that. I guess the project is niche enough that you don't get spammed with bad or malicious commits.
haunter 3 hours ago|||
Yeah it's basically anarchy (to some extent)

https://repo.or.cz/h/mob.html

>The idea is to provide unmoderated side channel for random contributors to work on a project, with similar rationale as e.g. Wikipedia - that given enough interested people, the quality will grow rapidly and occassional "vandalism" will get fixed quickly. Of course this may not work nearly as well for software, but here we are, to give it a try.

riffraff 2 hours ago|||
When pugs (a perl6 implementation in Haskell) was a thing, you gained commit access by asking and it was immediately granted to everyone. It was insane and awesome.
veltas 2 hours ago|||
I would be interested in contributing to this but the UK is geoblocked.
einpoklum 3 hours ago||
It is also interesting to note that while the repository is quite active, there has not been any release for _8 years_, and the website is the same one at the top of this conversation, i.e. the one where the old maintainer says he quit and the benchmarks are from 20 years ago.

A small and minimalistic C compiler is actually a very important foundational project for the software world IMNSHO.

I'm definitely reminded of: https://xkcd.com/2347/

kristianp 2 hours ago||
Does anyone use libtcc for a scripting language backend? Smaller and faster than llvm. You'd have to transpile to a C ast I imagine.
kgeist 2 hours ago||
Years ago I built a scripting language that transpiled to TCC and then compiled to machine code in memory. It produced human-readable C code so it was very easy to get going: when debugging the compiler I could just look at the generated C code without having to learn any special infrastructure/ecosystem/syntax etc. Plus basically zero-overhead interop with C out of the box => immediate access to a lot of existing libraries (although a few differences in calling conventions between TCC and GCC did bite me once). Another feature I had was "inline C" if you wanted to go low level, it was super trivial to add, too. It was pretty fast, maybe two times slower than GCC, IIRC, but more than enough for a scripting language.
olivia-banks 2 hours ago||
libtcc doesn't give you much control AST wise, you basically just feed it strings. I'm using it for the purpose you mentioned though--scripting language backend--since for my current "scripting-language" project I can emit C89, and it's plenty fast enough for a REPL!

    /* add a file (either a C file, dll, an object, a library or an ld script). Return -1 if error. */
    int tcc_add_file(TCCState *s, const char *filename);

    /* compile a string containing a C source. Return non zero if error. */
    int tcc_compile_string(TCCState *s, const char *buf);
asdefghyk 2 hours ago||
I recall, there where similar items back in late 70s and early 80s .

Tiny C, Small C are names I seem to recall, buts its very fuzzy - Not sure if they were compilers, may have been interpreters....

pixelsort 56 minutes ago||
Currently striving towards my own TypeScript to native x86_64 physical compiler quine bootstrapped off of TCC and QuickJS. Bytecode and AST are there!
jrop 51 minutes ago|
This sounds like a really cool project. What challenges have you encountered so far?
pixelsort 41 minutes ago||
Thanks. The hardest part has been slogging through the segfaults and documenting all the unprincipled things I've had to add. Post-bootstrap, I have to undo it all because my IR is a semantically rich JSON format that is turing-incomplete by design. I'm building a substrate for rich applications over bounded computation, like eBPF but for applications and inference.
imwally 1 hour ago||
Anyone know a good resource for getting started writing a compiler? I'm not trying to write a new LLVM, but being a "software engineer" writing web-based APIs for a living is leaving me wanting more.
jazzpush2 1 hour ago||
https://craftinginterpreters.com/
jandrese 1 hour ago|||
Traditionally you would start with the Dragon Book[1]. Another starting point is reading the documentation for Yacc (aka Bison) and Lex.

[1] https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniq...

compiler-guy 34 minutes ago||
The dragon book is a classic, and was a terrific resource thirty and forty years ago. Today there are many far better resources. Several listed adjacent to this one.

It’s focused on theory and very heavy on parsing. All of that is fine, but not especially useful for the hobbies.

prydt 1 hour ago||
I would highly recommend Wirth's Compiler Construction. Great, short book with a lot to say.
senfiaj 1 hour ago|
There is even smaller C compiler that fits within the 512 bytes https://xorvoid.com/sectorc.html
More comments...