🐤 A tiny C language compiler (x86-64) (WIP)
$ stack build
$ stack build --fast # no optimized
$ stack exec htcc -- -h
Usage: htcc [--visualize-ast] [--img-resolution RESOLUTION] file [-o|--out file]
[-w|--supress-warns]
Available options:
-h,--help Show this help text
--visualize-ast Visualize an AST built from source code
--img-resolution RESOLUTION
Specify the resolution of the AST graph to be
generated (default: 640x480)
file Specify the input file name
-o,--out file Specify the output destination file name, supported
only svg (default: ./out.svg)
-w,--supress-warns Disable all warning messages
Simple compilation:
$ echo 'int printf(); int main() { printf("hello world!\n"); }' > t.c
$ stack exec htcc -- t.c > t.s
$ gcc -no-pie t.s -o out
For one liner:
$ echo 'int printf(); int main() { printf("hello world!\n"); }' | stack exec htcc -- /dev/stdin | gcc -xassembler -no-pie -o out -
htcc has the ability to visualize ASTs built from loaded C code. This option allows to specify the resolution and output file. Examples are shown in the following table.
If you want to run outside the Linux environment, if docker and docker-compose are installed, you can run tests inside the docker container by specifying docker as an argument.
$ stack test --test-arguments --help
htcc> test (suite: htcc-test, args: --help)
Usage: htcc-test [--clean] COMMAND
The htcc unit tester
Available options:
-h,--help Show this help text
--clean clean the docker container
Available commands:
subp run tests with subprocess
docker run tests in docker container
self run the test using htcc's processing power
htcc> Test suite htcc-test passed
$ stack test --test-arguments self
$ stack test --test-arguments subp
$ stack test --test-arguments docker # For running outside the linux environment. It requires docker and docker-compose.
If you want to delete the created test container and its image, execute as follows:
$ stack test --test-arguments docker --test-arguments --clean
Source files that can be compiled by htcc are placed under the example/.
$ cd example
$ make
For the same reason, when running in docker (lifegame is not supported because it need to clear standard output):
$ cd example
$ make docker
$ make clean_docker # Stop and delete docker container, and delete image
$ stack bench
The implementation description is available in here.
htcc outputs x86_64 assembly according to System V ABI [2] and GCC 7.4.0 is used for assemble. Perhaps a newer version of GCC will work, but not checked currently.
The emoji included in the commit message is used according to gitmoji.
I know 😕
This is a compiler made for research, not for practical purposes and the author also developed the compiler for the first time. If you can suggest improvements, please submit issues or send PRs. Thanks in advance for all the improvements.
Check your permissions. The answer on stack overflow may be useful.
- JTC1/SC22/WG14. (2011). N1570 Commitee Draft [online]. Available from: PDF, HTML.
- H.J. Lu, Michael Matz, Milind Girkar, Jan Hubicka, Andreas Jaeger and Mark Mitchell. (2018). System V Application Binary Interface AMD64 Architecture Processor Supplement (With LP64 and ILP32 Programming Models) Version 1.0 [online]. Available from: PDF.
- Rui Ueyama. (2019). 低レイヤを知りたい人のためのCコンパイラ作成入門 [online]. Available from: https://www.sigbus.info/compilerbook.
- 前橋和弥. (2009). プログラミング言語を作る. 技術評論社.