All Projects → keiichiw → Constexpr 8cc

keiichiw / Constexpr 8cc

Licence: mit
Compile-time C Compiler implemented as C++14 constant expressions

Programming Languages

c
50402 projects - #5 most used programming language

Labels

Projects that are alternatives of or similar to Constexpr 8cc

Clio
Clio is a functional, parallel, distributed programming language.
Stars: ✭ 555 (-19.21%)
Mutual labels:  compiler
Llvmswift
A Swift wrapper for the LLVM C API (version 9.0.1)
Stars: ✭ 641 (-6.7%)
Mutual labels:  compiler
Wirefilter
An execution engine for Wireshark-like filters
Stars: ✭ 677 (-1.46%)
Mutual labels:  compiler
Sultan
Sultan: Command and Rule over your Shell
Stars: ✭ 625 (-9.02%)
Mutual labels:  compiler
Fastexpressioncompiler
Fast ExpressionTree compiler to delegate
Stars: ✭ 631 (-8.15%)
Mutual labels:  compiler
Cakeml
CakeML: A Verified Implementation of ML
Stars: ✭ 651 (-5.24%)
Mutual labels:  compiler
Rezoom.sql
Statically typechecks a common SQL dialect and translates it to various RDBMS backends
Stars: ✭ 621 (-9.61%)
Mutual labels:  compiler
Mlton
The MLton repository
Stars: ✭ 683 (-0.58%)
Mutual labels:  compiler
Cyclone
🌀 A brand-new compiler that allows practical application development using R7RS Scheme. We provide modern features and a stable system capable of generating fast native binaries.
Stars: ✭ 634 (-7.71%)
Mutual labels:  compiler
Rustc codegen cranelift
Cranelift based backend for rustc
Stars: ✭ 675 (-1.75%)
Mutual labels:  compiler
Minic Hosting
A simple stack-based virtual machine that runs C in the browser.
Stars: ✭ 628 (-8.59%)
Mutual labels:  compiler
Lebab
Turn your ES5 code into readable ES6. Lebab does the opposite of what Babel does.
Stars: ✭ 5,479 (+697.53%)
Mutual labels:  compiler
Gocaml
🐫 Practical statically typed functional programming language implementation with Go and LLVM
Stars: ✭ 653 (-4.95%)
Mutual labels:  compiler
Lyo
📦 Node.js to browser - The easy way
Stars: ✭ 624 (-9.17%)
Mutual labels:  compiler
Tiny Compiler
A tiny evaluator and compiler of arithmetic expressions.
Stars: ✭ 680 (-1.02%)
Mutual labels:  compiler
Rhine Ml
🏞 an OCaml compiler for an untyped lisp
Stars: ✭ 621 (-9.61%)
Mutual labels:  compiler
Js of ocaml
Compiler from OCaml to Javascript.
Stars: ✭ 643 (-6.4%)
Mutual labels:  compiler
Tiramisu
A polyhedral compiler for expressing fast and portable data parallel algorithms
Stars: ✭ 685 (-0.29%)
Mutual labels:  compiler
Esper
Esper Complex Event Processing, Streaming SQL and Event Series Analysis
Stars: ✭ 680 (-1.02%)
Mutual labels:  compiler
Amacc
Small C Compiler generating ELF executable Arm architecture, supporting JIT execution
Stars: ✭ 661 (-3.78%)
Mutual labels:  compiler

constexpr-8cc: Compile-time C Compiler Build Status

constexpr-8cc is a compile-time C compiler implemented as C++14 constant expressions. This enables you to compile while you compile! This project is a port of 8cc built on ELVM Infrastructure.

Constant expressions in C++ are expressions that can be evaluated at compile-time. In C++14, by relaxing constrains, constant expressions became so powerful that a C compiler can be implemented in!

In constexpr-8cc, the main routine for compilations of C programs is implemented in a C++14 constexpr function. Therefore, 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. In this sense, constexpr-8cc is a compile-time C compiler.

The following is the main function in 8cc.cpp.

int main() {
  // Compile-time
  constexpr buffer buf = eight_cc(); // Compile C code into ELVM IR
  constexpr unsigned int output_size = buf.size;

  static_assert(0 <= output_size && output_size < EIGHT_CC_OUTPUT_LIMIT, "8cc: Error");

  // Run-time
  for(int i = 0; i < output_size; ++i) {
    putchar(buf.b[i]);
  }
}

In this program, the return value of eight_cc is stored into the variable buf with a constexpr specifier. Thus, you will find that the compilation of a C program is done in compile-time.

Usage

constexpr-8cc works on Linux and OS X and requires g++ 6.2. (The version of g++ is important!)

Compilation by run_8cc.py

In order to try constexpr-8cc easily, use run_8cc.py.

$ ./run_8cc.py x86 ./test/hello.c -o ./hello.exe # It takes about 3 minutes on my laptop
$ chmod +x ./hello.exe                           # 'hello.exe' is i386-linux binary
$ ./hello.exe
Hello, world!

You can change the target language of compilations like the following:

$ ./run_8cc.py py ./test/hello.c -o ./hello.py # target language is Python
$ python ./hello.py
Hello, world!

For more information about this script, type $ ./run_8cc.py -h.

Compilation by hand

If you want to compile 8cc.cpp manually, please look at config.hpp. In this file, the variable EIGHT_CC_INPUT_FILE is defined. EIGHT_CC_INPUT_FILE should be a name of a file that contains a source C program as a C++ string literal. This string will be embedded in 8cc.cpp at pre-processing-time and used as an input of the compile-time computation.

So, before compiling 8cc.cpp manually, you have to convert a raw program to a string literal like the following:

$ sed '1s/^/R"(/' ./test/hello.c | sed '$s/$/\n)"/' > ./test/hello.c.txt # Convert C to string literal
$ g++-6 ./8cc.cpp -o eir_gen.out
$ ./eir_gen.out > ./test/hello.eir       # eir_gen.out outputs ELVM IR
$ sed -i '1s/^/R"(x86/' ./test/hello.eir # Convert IR to string literal
$ sed -i '$s/$/\n)"/' ./test/hello.eir
$ g++-6 ./elc.cpp -o exe_gen.out
$ ./exe_gen.out > ./hello.exe            # exe_gen.out outputs i386-linux binary
$ chmod +x ./hello.exe
$ ./hello.exe
Hello, world!

How was constexpr-8cc generated?

When you see 8cc.hpp, you will know this program was not written by hand. Actually, I used ELVM Compiler Infrastructure to generate it. I just implemented a translator from ELVM IR to C++14 constexpr here.

Author

Keiichi Watanabe (udon.watanabe [at] gmail.com)

Links

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].