Giter VIP home page Giter VIP logo

htcc's Introduction

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). ăƒ—ăƒ­ă‚°ăƒ©ăƒŸăƒłă‚°èš€èȘžă‚’äœœă‚‹. æŠ€èĄ“è©•è«–ç€Ÿ.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.