{"id":13418206,"url":"https://github.com/keiichiw/constexpr-8cc","last_synced_at":"2025-04-04T16:13:57.943Z","repository":{"id":47294898,"uuid":"74115784","full_name":"keiichiw/constexpr-8cc","owner":"keiichiw","description":"Compile-time C Compiler implemented as C++14 constant expressions","archived":false,"fork":false,"pushed_at":"2021-05-23T17:21:30.000Z","size":338,"stargazers_count":798,"open_issues_count":1,"forks_count":83,"subscribers_count":30,"default_branch":"master","last_synced_at":"2024-08-05T02:01:18.851Z","etag":null,"topics":["c","c-plus-plus","compiler","constexpr","joke"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/keiichiw.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-18T09:36:12.000Z","updated_at":"2024-08-03T23:52:42.000Z","dependencies_parsed_at":"2022-08-27T06:52:10.577Z","dependency_job_id":null,"html_url":"https://github.com/keiichiw/constexpr-8cc","commit_stats":null,"previous_names":["kw-udon/constexpr-8cc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keiichiw%2Fconstexpr-8cc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keiichiw%2Fconstexpr-8cc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keiichiw%2Fconstexpr-8cc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/keiichiw%2Fconstexpr-8cc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/keiichiw","download_url":"https://codeload.github.com/keiichiw/constexpr-8cc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246709872,"owners_count":20821298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","c-plus-plus","compiler","constexpr","joke"],"created_at":"2024-07-30T22:00:59.710Z","updated_at":"2025-04-04T16:13:57.929Z","avatar_url":"https://github.com/keiichiw.png","language":"C++","readme":"# constexpr-8cc: Compile-time C Compiler ![Build Status](https://github.com/keiichiw/constexpr-8cc/actions/workflows/main.yml/badge.svg)\n\n[constexpr-8cc](https://github.com/keiichiw/constexpr-8cc) is a compile-time C compiler implemented as C++14 constant expressions.\nThis enables you to **compile while you compile!**\nThis project is a port of [8cc](https://github.com/rui314/8cc) built on [ELVM Infrastructure](https://github.com/shinh/elvm).\n\n[Constant expressions in C++](http://en.cppreference.com/w/cpp/language/constant_expression) are expressions that can be evaluated at compile-time.\nIn C++14, [by relaxing constrains](https://isocpp.org/files/papers/N3652.html), constant expressions became so **powerful** that **a C compiler can be implemented in**!\n\nIn constexpr-8cc, the main routine for compilations of C programs is implemented in a C++14 `constexpr` function.\nTherefore, if you compile `8cc.cpp` to a binary file by g++, compilation of a C program will be performed as a compile-time computation and the result of this C compilation will be embedded into the generated binary.\nIn this sense, constexpr-8cc is a **compile-time C compiler**.\n\nThe following is the `main` function in [8cc.cpp](https://github.com/keiichiw/constexpr-8cc/blob/master/8cc.cpp).\n```c++\nint main() {\n  // Compile-time\n  constexpr buffer buf = eight_cc(); // Compile C code into ELVM IR\n  constexpr unsigned int output_size = buf.size;\n\n  static_assert(0 \u003c= output_size \u0026\u0026 output_size \u003c EIGHT_CC_OUTPUT_LIMIT, \"8cc: Error\");\n\n  // Run-time\n  for(int i = 0; i \u003c output_size; ++i) {\n    putchar(buf.b[i]);\n  }\n}\n```\nIn this program, the return value of `eight_cc` is stored into the variable `buf` with a `constexpr` specifier.\nThus, you will find that the compilation of a C program is done in compile-time.\n\n## Requirements\n\n`constexpr-8cc` requires Linux with \u003eg++-6.2. I confirmed `./test/hello.c` can be compiled with `g++-6.2`, `g++-8.3` and `g++-9.3` at least.\n\n* g++-6.2 worked without any extra flag related to constexpr as there was no limitation of constant's loop counts at this version.\n* With g++-8.3, I needed to enlarge constexpr's loop count with `-fconstexpr-loop-limit`. The maximum number we can specify is `2**31 - 1`.\n* With g++-9.3, in addition to `-fconstexpr-loop-limit`, enlarging `-fconstexpr-ops-limit` was needed.\n* There is no guarantee that it works with other versions of g++.\n* I couldn't make it work with clang++ as clang++ has more strict limitation of constexpr loop counts.\n\n## How to run\n\n### Compilation by `run_8cc.py`\nIn order to try constexpr-8cc easily, use `run_8cc.py`.\n```shell\n$ ./run_8cc.py x86 ./test/hello.c -o ./hello.exe # It takes about 3 minutes on my laptop\n$ chmod +x ./hello.exe                           # 'hello.exe' is i386-linux binary\n$ ./hello.exe\nHello, world!\n```\nYou can change the target language of compilations like the following:\n```shell\n$ ./run_8cc.py py ./test/hello.c -o ./hello.py # target language is Python\n$ python ./hello.py\nHello, world!\n```\nFor more information about this script, type `$ ./run_8cc.py -h`.\n\n### Compilation by hand\nIf you want to compile `8cc.cpp` manually, please look at `config.hpp`.\nIn this file, the variable `EIGHT_CC_INPUT_FILE` is defined.\n`EIGHT_CC_INPUT_FILE` should be a name of a file that contains a source C program as a C++ string literal.\nThis string will be embedded in 8cc.cpp at pre-processing-time and used as an input of the compile-time computation.\n\nSo, before compiling `8cc.cpp` manually, you have to convert a raw program to a string literal like the following:\n```shell\n$ sed '1s/^/R\"(/' ./test/hello.c | sed '$s/$/\\n)\"/' \u003e ./test/hello.c.txt # Convert C to string literal\n$ g++-6 ./8cc.cpp -o eir_gen.out\n$ ./eir_gen.out \u003e ./test/hello.eir       # eir_gen.out outputs ELVM IR\n$ sed -i '1s/^/R\"(x86/' ./test/hello.eir # Convert IR to string literal\n$ sed -i '$s/$/\\n)\"/' ./test/hello.eir\n$ g++-6 ./elc.cpp -o exe_gen.out\n$ ./exe_gen.out \u003e ./hello.exe            # exe_gen.out outputs i386-linux binary\n$ chmod +x ./hello.exe\n$ ./hello.exe\nHello, world!\n```\n## How was constexpr-8cc generated?\nWhen you see [`8cc.hpp`](https://github.com/keiichiw/constexpr-8cc/blob/master/8cc.hpp), you will know this program was not written by hand.\nActually, I used [ELVM Compiler Infrastructure](https://github.com/shinh/elvm) to generate it.\nI just implemented a translator from ELVM IR to C++14 constexpr [here](https://github.com/shinh/elvm/pull/15).\n\n## Author\n**Keiichi Watanabe** (udon.watanabe [at] gmail.com)\n\n## Links\n* [8cc](https://github.com/rui314/8cc) ([@rui314](https://github.com/rui314))\n  - A very cool C compiler. constexpr-8cc is a C++14's constexpr port of 8cc.\n* [ELVM](https://github.com/shinh/elvm) ([@shinh](https://github.com/shinh))\n  - ELVM(EsoLang Virtual Machine) is a parody project of LLVM, but dedicated to Esoteric Languages. constexpr-8cc is built on ELVM infrastructure.\n* [8cc.vim](https://github.com/rhysd/8cc.vim) ([@rhysd](https://github.com/rhysd)), [8cc.tex](https://github.com/hak7a3/8cc.tex) ([@hak7a3](https://github.com/hak7a3))\n  - constexpr-8cc is influenced by these projects.\n* [Compile-time Brainf\\*ck compiler](https://github.com/bolero-MURAKAMI/Sprout/blob/master/example/brainfuck/x86_compile.cpp) ([@bolero-MURAKAMI](https://github.com/bolero-MURAKAMI))\n  - I got several ideas from this program.\n\n* [TMP-8cc](https://github.com/keiichiw/TMP-8cc): Another Compile-time C Compiler\n  - Another compile-time C compiler I created, which is implemented in **C++ Template Metaprogramming**\n  - This is generated by [ELVM's C++ Template Metaprogramming backend](https://github.com/shinh/elvm/pull/39).\n  - Since `TMP` neads huge amount of memories, `TMP-8cc` unfortunately **does not work** on real machines.\n* [My blog post (Japanese)](http://kw-udon.hatenablog.com/entry/2016/12/03/201722)\n","funding_links":[],"categories":["TODO scan for Android support in followings"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeiichiw%2Fconstexpr-8cc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkeiichiw%2Fconstexpr-8cc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkeiichiw%2Fconstexpr-8cc/lists"}