Skip to content

falgon/htcc

Repository files navigation

htcc

🐤 A tiny C language compiler (x86-64) (WIP)

Build

$ stack build
$ stack build --fast # no optimized

Usage

$ 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 -  

AST diagram generation

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.

Command Output
$ echo 'int main() { return 1 * 2 + 4; }' |\
    stack exec htcc -- /dev/stdin --visualize-ast
AST graph of the some calculation
$ echo 'int printf();
    void fizzbuzz(int n) { 
        for (int i = 1; i < n; ++i) { 
            if (!(i % 15)) printf("fizzbuzz\n"); 
            else if (!(i % 3)) printf("fizz\n"); 
            else if (!(i % 5)) printf("buzz\n"); 
            else printf("%d\n", i); 
        } 
    } 
    int main() { fizzbuzz(50); }' |\
    stack exec htcc -- /dev/stdin\
        --visualize-ast\
        --img-resolution 1280x720\
        --out fizzbuzz.svg
AST graph of FizzBuzz

Appearance of operations

an gif animation image of operations

Tests and run examples

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

Benchmark

$ stack bench

Documents

The implementation description is available in here.

Specification and Requirements

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.

About emoji of commit messages

The emoji included in the commit message is used according to gitmoji.

FAQ

Your compiler is inefficient :)

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.

When I try to play with ghci, I get a warning "WARNING:. is owned by someone else, IGNORING!"

Check your permissions. The answer on stack overflow may be useful.

License

FOSSA Status

References

  1. JTC1/SC22/WG14. (2011). N1570 Commitee Draft [online]. Available from: PDF, HTML.
  2. 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.
  3. Rui Ueyama. (2019). 低レイヤを知りたい人のためのCコンパイラ作成入門 [online]. Available from: https://www.sigbus.info/compilerbook.
  4. 前橋和弥. (2009). プログラミング言語を作る. 技術評論社.

About

🐤 A tiny C language compiler (x86-64) (WIP)

Topics

Resources

License

Stars

Watchers

Forks

Languages