229 points · by swatson741 · 3y ago · 65 comments
Either by adding complexity (more features to the compiler) or dropping complexity (fewer C features in the implementation).
Did you ever look at that?
Edit: functions, enum, struct, arrays and maybe make all variables/functions a-z?
Edit2: https://joyofsource.com/projects/bootstrappable-tcc.html
How did you use this in your teaching? It seems like it could the basis for a longer term project that students could take in many directions.
What I'd love to do someday is to write a compiler for a fairly complete C subset in POSIX shell. The goal would be to use only a POSIX shell and this compiler to compile TCC, and then use TCC to bootstrap gcc, all from source. This would be a great tool for reproducible builds from source. If someone here finds this interesting and would like to help out, please reach out to me.
Optimizing for storage space is…better?
#!/usr/bin/tcc -run
You can do that with gcc/clang too (e.g. #if 0, #endif to wrap a block of shell script to compile the current file and execute the result) but a primary value of tcc is that it compiles fast.On a more philosophical note, the suckless approach is to optimise for simplicity not storage. It's perfectly valid to disagree with that of course, but if simplicitly of the system as a whole is a consideration gcc and clang doesn't really fit.
(tiny C) compiler --> "This is a compiler for the Tiny-C language"
vs
Tiny (C compiler) --> "TinyCC [...] is a small but hyper fast C compiler"
That's it! ;-)
For details, check their current working repository [1] and mailing list [2].
Here's why real languages are better than stripped down languages: Anyone with programming knowledge can implement a Brainfuck interpreter in a few hours and run any Brainfuck program. Anyone with a tiny bit of CS knowledge can implement a Tiny BASIC interpreter in just a day and then you can run any real Tiny BASIC program from the late 70s. It's cool to run real programs people actually used. With this stripped down C, there are no pre-made real programs...
0:https://en.wikipedia.org/wiki/Brainfuck 1:https://en.wikipedia.org/wiki/Tiny_BASIC
Also related to small language implementations I find notable PicoC [3] which is a C interpreter written in around 3k LOC of C. Past discussion about it here 13 years ago [4].
[0]: https://www.lua.org/about.html
[1]: https://www.lua.org/spe.html
[2]: https://en.wikipedia.org/wiki/Lua_(programming_language)
1. You use the example of a tiny basic of a 'real language' and I don't see how tiny basic is a 'real language', but tiny C is a stripped down language.
2. You can build on this to make a full c implementation. A minimal c implementation that can potentially bootstrap a full c environment is more useful than a brainfuck interpreter.
2. Good point, but it’s several orders of magnitude more work to go from this to C, so it depends if you’re doing a one off project or have much bigger ambitions. I think you get more bang for the buck out of Tiny BASIC if it’s a one-off project is my point.
https://news.ycombinator.com/item?id=8558822
The 26 predefined integer variables make this look like a variant of minimal BASIC, except with structured control flow instead of only GOTO.
Calling it a compiler is (to me) really stretching things, I can't see any code to emit any other form of the code, it's all aimed at evaluating (executing) it.
Edit: oops, I didn't read the code closely enough, it does emit code but only internally, that code is what gets executed. Thanks for the corrections!
Compiler for the Tiny-C Language (2001)
In fact, that is exactly how the source code describes itself in the comments.
The second assignment was writing a Scheme interpreter.
References:
Dr. Dobb's Journal #32 (Feb 1979) page 41, review of Tiny-C User Manual by Ted Shapin [0]
Dr. Dobb's Journal #35 (May 1979) page 37, "Tiny-C Interpreter on C-Dos" by Ray Duncan[1]
Tiny-C Associates incorporated in Holmdel, NJ, March 1978 [2]
"Tiny C" trademark application filed 1979, cancelled 1987 [3]
There was also a "Small C", see DDJ #45 (May 1980), "A Small C Compiler for the 8080s" by Ron Cain[4]. Cain references buying a copy of "the Tiny-C interpreter from Tiny-C Associates" and finding it too slow, so he bootstraps his own C compiler, writing it in C, compiles it on a UNIX system, then using it to compile itself to get the 8080 machine code.
See also DDJ #69 (July 1982) p. 66, "Small C for the 9900" by Matthew Halfant[5], porting Cain's compiler to another platform.
[0] https://archive.org/details/dr_dobbs_journal_vol_04_201803/p...
[1] https://archive.org/details/dr_dobbs_journal_vol_04_201803/p...
[2] https://www.bizapedia.com/nj/tiny-c-associates.html
[3] https://alter.com/trademarks/tiny-c-73219160
[4]https://archive.org/details/dr_dobbs_journal_vol_05_201803/p...
[5] https://archive.org/details/dr_dobbs_journal_vol_07_201803/p...
I hacked in support for ">", ">=", and "<=" to match the "<" support, but I just noticed that ints are truncated, so the maximum value stored in a variable is 127.
$ cat hello.scm
(display "hello!\n")
$ gsc hello.scm
$ gsi hello.o1
hello!
$ ls -l hello.o1 # this is generated by gcc
-rwxrwxr-x 1 feeley feeley 18152 Mar 13 17:16 hello.o1
$ rm hello.o1
$ gsc -cc "tcc -shared" hello.scm
$ gsi hello.o1
hello!
$ ls -l hello.o1 # this is generated by tcc
-rwxrwxr-x 1 feeley feeley 4432 Mar 13 17:17 hello.o1I admit I have trouble understanding how the VM run() function works... anybody can give some insight?