r/C_Programming • • Oct 22 '26

Question Has anyone ever tried out the TinyC compiler before?

Musl gcc is small but tinyc caught me eye due to how small it is and the fact that it can self host.

It supports C99 standards out of the box and it only takes up a few hundred kilobytes. I wonder if anyone tried it.

80 Upvotes

56 comments sorted by

10

u/cKGunslinger Oct 22 '26

It's fast and best when used for fast compilation. It does not generate highly-optimized code, but it's amazing how often most things don't need to be highly optimized.

And I always forget if it's TCC or PCC that doesn't support thread-local storage. But I think it's TCC.

5

u/thecratedigger_25 Oct 22 '26

I agree. When you write good code and you don't bring an entire mountain in your binary, the results speak for themselves.

6

u/sciencekm Oct 23 '26

This is my goto compiler because it is very fast. I only switch to other compilers when it is time to build release/optimized binaries.

2

u/thecratedigger_25 Oct 23 '26

Glad to hear that. I'm dabbling with raylib at the moment with the tinyc compiler and it is a ton of fun.

96

u/runningOverA Oct 22 '26

Written by the same author that wrote ffmpeg, quickjs, QEMU.

TinyC is used by some scripting languages to compile and then run the program in one go, making it feel like a scripting language while its not.

67

u/FlippingGerman Oct 22 '26

That man (Fabrice Bellard) is incredible. He once held the record for most digits of pi computed. He just constantly churns out amazing new creations.

35

u/amarukhan Oct 22 '26

Even John Carmack (Doom, Quake) conceded Fabrice is a better programmer than he is.

6

u/gremolata Oct 22 '26

Got a source? Not that I disagree :)

3

u/FransFaase Oct 22 '26

That he created some incredible software does not mean his code is of high quality can I tell has some who tried to develop a minimal C compiler to compile TCC.

23

u/FlippingGerman Oct 22 '26

Sure. Yet every few years he creates something better than many programmers, even those whose code may be beautiful, might in their whole lives.

5

u/veeloth Oct 25 '26

"code quality" often refers to how good your code is in an Enterprise environment: in which you need to share with others and adapt quickly to whatever the higher ups demand.

In my opinion, a programmer's quality is purely about how good is the software they can make, so "high code quality" ≠ "amazing programmer". Amazing software however seems like a much more relevant criterion to consider.

1

u/FransFaase Oct 25 '26

He is a very smart person, who is very fast at writing code, but because he is fast he makes all kind of short cuts (hacks) to fix bugs instead of implementing the right solution. That leads to something that works, but is rather fragile, meaning that if you want to add something, such as support for x86_64 (amd64), you run into wierd bugs that are difficult to fix. Or there is a memory allocation function that always returns on 32 bit boundaries on a 64 bit architecture, which does work, but is a bit strange.

The quality of the code is not low because is does not meet a commercial standards but because the author implemented many quick fixes instead of real fixes.

I sometimes also do this when I have not much time. Often it fails at some point and I decide to start from scratch and do it it in the right way. At other times I manage to get it working for my application, but I usually do not publish those programs because I feel ashamed of the bad quality of the code.

4

u/runningOverA Oct 25 '26

but I usually do not publish those programs because I feel ashamed

I am using his software far more than your's as a result.

1

u/FransFaase Oct 25 '26

I do publish most of my code. See:

- https://github.com/FransFaase

- https://www.iwriteiam.nl/Programs.html

But I guess they are less general applicable than the code that Fabrice Bellard wrote.

50

u/TheChief275 Oct 22 '26 edited Oct 23 '26

TCC is absolutely fantastic. One cute trick you could do is put this at the top of your C code:

#if 0
tcc "$0" -o./.cscript
exec ./.cscript "$@"
#endif

and make it executable. This works (1) because # is treated as a comment in Shell/Bash script and exec replaces the process so the rest of the file doesn't get executed, and (2) because TCC is so incredibly fast at compiling (try it; it's unbelievably fast), running e.g. a Hello World C program this way will be faster than running one in Python.

Another thing I want to try for my next project is using TCC as a compiler backend, i.e. producing C code but also having TCC so you can also produce libraries/executables directly, which most compilers are still expected to do. Not sure how well this works, but the fast compilation time of TCC seems enticing here as a debug build option during development. Actual release builds could just be fed into GCC or Clang separately

11

u/PA694205 Oct 23 '26

alternatively you can use

`//usr/bin/tcc -run "$0" "$@"; exit`

which acts as a c comment or a valid bash path. That way LSPs dont get confused.

3

u/Scolmann Oct 23 '26

Use three slashes, two at the start can mean reuse the same protocol, though in most cases it will just work, but 3+ is defined to act like 1

1

u/TheChief275 Oct 23 '26

ohh fun, great addition!

4

u/EatFapSleepFap Oct 23 '26

Wouldn't you also need a shebang line? Also, if the program fails the compile it may not handle it well.

3

u/TheChief275 Oct 23 '26

You don't really need shebang. It will just take the system's default shell interpreter and use it. But also, this is why I said it's a cute trick. As soon as you actually expect more sophisticated things from it, it will probably fall apart.

If the program fails to compile you will still see the errors, but it will execute a previous version. One thing you could do is to check whether the first command exited with errors and in that case, just don't exec the executable

2

u/hesusruiz Oct 24 '26

That's what the V language did at the beginning (it may still do it today, as an optional backend target, I have not followed it lately). TCC may not produce code as optimized as other C compilers, but it is incredible fast and it may be fast enough for some use cases.

1

u/TheChief275 Oct 25 '26

I think the focus on just runtime performance at the general expense of compile-time performance has been a big mistake for development. Even 'gcc/clang -O0' has significant extra costs from them being giant projects that have to launch numerous components; this is also where the majority of Clang's overhead is and where the modularity of the LLVM project becomes its downfall.

We're seeing a shift in mentality with modern backends (motivated as 'being sick of LLVM'), such as QBE, TB, CraneLift, where the codebases are to be kept simple and only optimisations are applied that do not bloat the codebase or skyrocket compile-time too much. IMO, these backends are simply superior for 99% of the cases. Only for release or when dabbling into too much UB (to check whether something still works under heavy optimisation) should you use such a slow, heavily optimising backend, but it should not be your default.

Also, I have not heard of V in a while. Is it actually in a usable state nowadays? Instead of the over-promising vaporware it was when it was heavily marketed

1

u/hesusruiz Oct 26 '26

Fully agree. Fast compilations when developing allows very good DX, and heavy optimizations (with associated slow compilation) should be optional.
Regarding V, I agree on the over-promising, but I have to accept that the language was very nice, ticking many boxes in what I would like in a language for some use cases.
I don't know how mature is it now, but I checked the website, and right there:
"V compiles ≈100k LOC (Clang backend) and ≈700k LOC (arm64 and tcc backends) per second."

So they are still using TCC as a backend, which makes a lot of sense to me.

I also experimented with my own simple language with TCC as a backend. It was fantastic, and I also saw that in many cases I was not going to need further optimizations, at least until huge scalability was needed: the applications are OLTP, where the bottleneck is the database/disk, so I could get easily over 10K tx/sec, saturating the database. Considering that 100-200 tx/sec was already more than enough for those applications, TCC "slowness" was not a problem.

Unfortunately, I could not dedicate time to continue ...

17

u/vectorj Oct 22 '26 edited Oct 22 '26

For what it’s worth, I’ve seen YouTube/twitch Tsoding use tinyc, including changing tinyc itself. Worth a watch.

14

u/chibuku_chauya Oct 22 '26

Yes, I use TCC. It’s *fast*. An engineering marvel.

26

u/kansetsupanikku Oct 22 '26

I think many people did. It is popular and the author is - deservedly so - a highly respected figure in the community. It can even be used to build Linux from source on boot.

But the only reasonable use cases are ones where compilation speed is the primary objective. Or "compiler size", I guess, but that sounds highly atypical. TCC is not designed to optimize. The results just might make you realize how much the modern compilers are doing for you be default - as, in comparison, the performance is going to be kinda horrible.

5

u/LordRybec Oct 23 '26

My primary use case right now is a system with 8MB of RAM. Modern GCC will end up swapping even on fairly small files with that little RAM, pushing compile times into 1980s or 1970s compile times. TinyC, on the other hand, should be able to do even larger files on that, fast, because it's a single pass compiler and doesn't need to keep track of past stuff for optimization purposes.

Ironically, the same platform I'm using it on would probably benefit more from optimization than the typical platform GCC is used on. GCC isn't really an option though. If I need optimization on something, I can still cross compile with GCC on something with more resources.

3

u/aieidotch Oct 22 '26

build and boot kernel that was 2.x

calling its performance horrible is plain wrong

2

u/UnrealHallucinator Oct 23 '26

You're saying it could be comparable to gcc -O3? Or even O2?

1

u/aieidotch Oct 23 '26

No I am saying, guessing horribly is 50% slower, which it is not. The performance impact is 25% maximum… and most probably less. But Well do your own benchmarks.

1

u/UnrealHallucinator Oct 23 '26

Hmmm that's interesting I guess. I feel like it probably depends on the program. It's also an unfair comparison bc gcc O3 is hella optimised and has seen years of work whereas tcc is just chilling. I honestly think half as fast or 50% as you called is probably true for the more complex programs.

1

u/kansetsupanikku Oct 23 '26

Suggesting it might be consistent across all the code out there is wild. And by using sections that gcc would skip even with O1 and TCC would not, I can get you any number possible. You can only measure this per usecase.

1

u/flatfinger Oct 23 '26

Additionally, some people such as myself view the performance of code that needs to actually exist is far more important than the "performance" of code that doesn't need to exist, and that the programmer could have simply omitted if executing it would have unacceptably affected performance.

1

u/xpusostomos Oct 24 '26

What architectures does it support?

1

u/thecratedigger_25 Oct 24 '26

x86_64,ARM, and Risc V if I remember right.

1

u/KseandI Oct 27 '26

Tbh, I don't really like it. I know using freestanding mode in OS environment isn't a great idea, but if mainstream compilers can at least do something there, tcc just can't. Its __asm__ directive has many problems with r8-r15 registers selection on amd64, both in inline assembly and register selector for variables (but at least you can write register loading by hand because optimization is non-existent there and your code written by hand will probably be even faster than tcc's prologue). And va_begin builtin is always an intrinsic on all compilers expect on tcc, where it's a standard function for some reason, that won't link in freestanding mode and that depends on memset with abort from libc. All of these problems are mostly C being uncertain, but I still find it frustrating more on tcc.

1

u/thecratedigger_25 Oct 27 '26

From what I've seen, tcc itself usually targets x86, arm, and risc-v. It's essentially like the linux from scratch of c compilers.

3

u/LordRybec Oct 23 '26

Haven't used it yet, but I'm looking into trying to setup an extremely bare bones dev system on a low resource microcontroller, and this was suggested by AI, since GCC uses too much memory for my target system.

The main downsides with TinyC is lack of optimization and advanced compiler extensions. GCC has extensions that can do pretty cool things (optimizations and more, for example, GCC has anonymous unions in C99 (if you don't enable strict mode), which weren't officially added to C until C11), and it goes through multiple compile steps to allow for advanced optimization. TinyC is a single pass compiler, which makes it fast and low memory, but it doesn't produce optimal machine code.

For applications where there are enough resources, I'd use GCC, but for resource constrained environments, TinyC seems like a really good option.

2

u/pedersenk Oct 23 '26

I use the TinyC combined busybox often for some of my embedded Linux projects. Absolutely fantastic.

The one disadvantage of TinyC is that not too many architectures are supported.

2

u/montymole123 Oct 23 '26

It claims to support C89 fully. Supports only some features of C99. I'm impressed it supports passing and returning structs from functions many stripped down C compilers don't

1

u/Evil-Twin-Skippy Oct 23 '26

There is actually an extension to the Tcl language that uses tinycc to compile extensions for the language inside the language.

1

u/wak6817-2 29d ago

I tried it and for me; IT'S PERFECT

1

u/JoK3rOp Oct 23 '26

Chibiccnkooks better than tinyc according to tsoding. I've not used it personally thought.

-2

u/FransFaase Oct 22 '26

As someone who wrote a C compiler to compile TCC, I can say that the code is its own unit test, as it contains many edge cases, such as goto's inside switch statement and an obscure memory allocator returning memory at 32 bit boundaries for 64 bit targets. TCC 0.9.26/27 do not support 64 targets by default. Some patches are needed. TCC is not an example of quality code.

3

u/LordRybec Oct 23 '26

Some people say the same about the Linux kernel, because it uses gotos in some places. Quality is more than just "Fits my personal ideal of well formed, elegant code". Sometimes quality means doing the most, the fastest, with the least code, and that will always appear to be low quality to someone who doesn't understand exactly why each design decision was made. If the code looks horrible but works extremely well, odds are it's just higher quality than you understand how to understand.

0

u/FransFaase Oct 23 '26

I use gotos myself if needed. I am not the only one in the live bootstrap community that complained about the quality of the code.

2

u/LordRybec Oct 23 '26

3

u/FransFaase Oct 24 '26

Great story. I remember reading this a long time ago.

1

u/LordRybec Oct 24 '26

The point is, even if lots of people claim the code is bad, that doesn't make it so. It may be bad (I haven't looked it at it personally), but it may be so ingenious that you just think it is bad when it is actually totally awesome. Truth isn't democratic. It doesn't matter if the entire community has complained about the quality of the code or just one person.

1

u/FransFaase Oct 25 '26

Have you read 'Zen and the Art of Motorcycle Maintenance'? The author argues that quality is not objective and not subjective.

I consider the code of low quality because it is hard to add new features, such as support for a new target. I think that one of the reasons is that he often decided to add a quick fix instead of a real fix. It is often tempted to implement quick fixes, and often they do work, but it results in hard to read and modify code. Why, for example, in many place the value of -2 does need a special treatment while in one place not? Was it intentional or does that value never occure in that location? Or only for targets other than 32 bits.

2

u/LordRybec Oct 25 '26

If the project is intended to be finished it doesn't matter. One massive problem I see in this industry is people continually treating projects as unfinished. You are viewing this as an eternally unfinished project. From that point of view, suboptimal code that is easy to read and modify is "high quality", no matter how badly it performs, how bloated it is, etc... That's a stupid point of view. How would you feel if someone gave you a new car that gets terrible gas mileage, takes up two lanes on the road, and constantly needs someone to come to your house and make changes to keep it from falling apart on the road, because all of the parts were designed to be spaced out and formed to be accessible and easily modifiable just in case you wanted to modify it? And yet, this is exactly how this industry designs software, and it's a plague that keeps things "maintainable" but never finished and always low quality. Sure, the code is considered high quality, but the actual software is garbage. I think if the software is garbage, so is the code, no matter how "maintainable" it is.

If his goal was to make finished software, maintainability is utterly and completely irrelevant. The code was designed to make the software itself good, not to make the code readable or maintainable. Any additions after that obviously would have to hacked on, just like you'd have to do hacky stuff to add a turbocharger to finished car that wasn't designed for it.

That's not bad quality code. That's people expecting eternally poor quality code so that they can easily hack and modify, at the cost of overall program quality. Why does -2 need special treatment in some places and not others? Does it matter? If it worse well it shouldn't matter. Did you ever consider that just because you aren't smart enough to figure it out, that doesn't mean there wasn't a good reason? You keep making complaints about the code, and in each and every case, I can see potential reasons someone might choose to do it that way. The fact that you can't doesn't mean it was a bad coding choice. It means that you don't understand this stuff as well as you seem to think you do.

I've been in the position of the guy you are attacking before. I've written code designed to finished and never be changed that was elegant, extremely performant, and sufficiently flexible to handle every use case it would be needed for. And then several far less experienced programmers I worked with tore it apart, complaining that it was bad code, because they struggled to understand it. The fact, however, was that the code was not designed for them to understand. It was designed for them to use the application, and it provided the perfect API for people with their level of experience. Eventually I gave up and abandoned the code I had so carefully and thoughtfully written, because those who the application was for could not understand that the code was written to do one specific job and not to be hacked or messed with by others. It was complete, but they couldn't understand the concept of a program being finished. You call it bad code, because you insist on messing with something that was never designed to be messed with! That's not bad code. If it does the job and does it well, and it very much does, then it is very good code. Your lack of comprehension is a personal problem. Your insistence on something finished being understandable to you and modifiable to you is your problem. The code isn't the problem. Your unrealistic expectations are the problem.

1

u/FransFaase Oct 25 '26 edited Oct 25 '26

I understand you are talking about external and internal quality of code. I too have written code that others do not understand or complain about.

Would you use `(global_array + OFFSET)[i]` instead of `global_array[OFFSET + i]` if you are not sure about how good the compiler is at constant folding?

Because I wrote a compiler to compile TCC, I had to look under the hood, especially when targeting 64 bits architectures.

I had some trouble understanding the function `tal_realloc_impl`. Would you have used a goto here? Could it not be easily avoided using a `for (;;)` and an `else` and/or a `break`?

I guess you do understand the `(size + 3) & -4` and understand why when TCC is compiled for a 64 bit architecture is possible less optimal.

2

u/LordRybec Oct 26 '26

I can't say what I would without looking at the code, doing some profiling, and looking at the assembly produced by the compiler it was original compiled with. If it wasn't designed to be compiled for 64-bit architectures, criticizing it based on how it doesn't just isn't valid. That's outside of the intended use case. I do know that gotos often are more optimal than empty for loops, else's, or breaks. I have a reasonable amount of assembly experience, and while a good optimizing compiler now days might emit the same assembly for one of those as a goto, that's not always the case and may not have been the case when TCC was originally written. I've written multiple display drivers for microcontrollers (and I've just ordered some parts for a project where I'll have to write another), and if you want good performance and small code size you have to code to the platform. The ST7789 driver I wrote for the RP2350 a few months ago wouldn't function at all on anything else (maybe it would on an RP2040, since they are intentionally very similar). The SSD1306 driver I wrote for the CH552 wouldn't work on the RP2350, in part because the CH552 is an 8-bit system and the RP2350 is 32-bit. Even non-driver code written optimally for one wouldn't be optimal on the other.

If TCC was written to be ultra small and ultra fast on a specific kind of system, it would naturally have a lot of system specific optimizations that wouldn't work as well on other systems, and that might look like poor coding if you aren't familiar with the target system and target compiler. I haven't done a ton of comparison in how different code constructs compile to assembly with a given compiler, but I know they do. Even a really good optimizing compiler can't always make optimizations that seem obvious to us, because there is context that the compiler doesn't and can't know. So something that looks like a nop might might actually emit code that does nothing but still takes time to execute. In fact, I had to write an ultra precise timing routine for the CH552 for an I2C driver, and I did have to look at the assembly produced. In the end I rewrote the timing routine in assembly, where I could calculate the exact delay including function call overhead, but if you are writing a C compiler designed to be compiled with another C compiler, doing a bunch of stuff in assembly isn't always an option. So instead you might write the same piece of code several different ways, compile them all, look at the assembly produced, and then use the one that does the closest to what you want. And when you do it that way, you'll find gotos tend to produce cleaner assembly a lot of the time, You'll also find that the C code that produces the most optimal results is often ugly and hard to read. But if maintainability is less important than optimal functionality, that ugly code is the best quality code you can use.

There's a reason software development doesn't have well established best practices the way other fields of engineering do. It's because different goals demand different best practices. Like I said before, a lot of programmers today view maintainability is the highest goal, and in that context nice, clean, readable code is critical. But that code almost always produces poor results under-the-hood. Optimizing compilers are largely designed to try to compensate for that, to allow maintainable code to also be optimal, but because compilers can't know your intent (unless you are using a functional language like Haskell), their ability to optimize maintainable code is limited. If your goal is a solid finished product, with no intention of future modification though, it doesn't matter if your code is readable or not. What matters is how well the compiled product does its job on the systems it is intended to be use with.

I do a lot of embedded systems work, and most of the time you don't have the luxury of being able to update embedded devices once they leave your possession. So you have one chance to get it right. Code maintainability matters way less than the code working very well on the target system. And the better you can optimize it, the more money you can save. I don't do large scale stuff right now, but if you are mass producing a device, and you can use a CH552 (8-bit 8051 architecture chip, that's under 25¢ a chip at bulk prices. If you can make your code efficient enough to run on that, that's a huge savings over something like the RP2040 at 70¢ at bulk pricing. It doesn't matter if your code looks horrifically ugly, so long as it works the way it is intended on the chip it is written for, and that often means writing parts of C code multiple ways and figuring out what produces the best assembly code. (It also often means assembly optimization, both for speed and for size.) Personally, I try to write critical parts in assembly from the outset, because checking what C code produces the best assembly can be a pain, but it's not always possible to do it that way.

If you really want to understand whether TCC's code is low quality or not, you'd have to start by asking the author what the intent was. If it was to produce a compiler that is size and speed optimized for a specific platform, as a finished project with no intent for future modification, what looks like low quality code could in fact be the highest quality code the author could have written. If instead, the objective was to write a more general C compiler, as small and fast as possible but also highly portable and designed to be modified over time, then yeah, you might be justified in calling the code low quality. But that intent is critical. Too many people have a single idea of what makes code quality good or bad, but the reality is that it depends very heavily on the original purpose of the code. It's not really fair to call code low quality without first determining what the goals of the author were, because as I said, what seems poor quality in one context might be the best possible quality in another. So long as the code does what it should where it was intended to run, you need more than just looking at the code to determine quality.