All Projects → RobbeDGreef → carbon-ir

RobbeDGreef / carbon-ir

Licence: MIT license
The carbon intermediate representation language

Programming Languages

C++
36643 projects - #6 most used programming language
c
50402 projects - #5 most used programming language
python
139335 projects - #7 most used programming language
CMake
9771 projects

Projects that are alternatives of or similar to carbon-ir

Miasm
Reverse engineering framework in Python
Stars: ✭ 2,649 (+10937.5%)
Mutual labels:  intermediate-representation
Llvm
Project moved to: https://github.com/llvm/llvm-project
Stars: ✭ 4,461 (+18487.5%)
Mutual labels:  intermediate-representation
Write You A Haskell
Building a modern functional compiler from first principles. (http://dev.stephendiehl.com/fun/)
Stars: ✭ 3,064 (+12666.67%)
Mutual labels:  intermediate-representation
metadsl
Domain Specific Languages in Python
Stars: ✭ 86 (+258.33%)
Mutual labels:  intermediate-representation
finn-base
Open Source Compiler Framework using ONNX as Frontend and IR
Stars: ✭ 21 (-12.5%)
Mutual labels:  intermediate-representation
YaoBlocks.jl
Standard basic quantum circuit simulator building blocks. (archived, for it is moved to Yao.jl)
Stars: ✭ 26 (+8.33%)
Mutual labels:  intermediate-representation
rstatic
An R package for static analysis of R code.
Stars: ✭ 32 (+33.33%)
Mutual labels:  intermediate-representation
yair
🦜 yair - a high-level compiler IR entirely written in Rust
Stars: ✭ 34 (+41.67%)
Mutual labels:  intermediate-representation
calyx
Intermediate Language (IL) for Hardware Accelerator Generators
Stars: ✭ 157 (+554.17%)
Mutual labels:  intermediate-language
real-world-idris
Malfunction backend for Idris with a FFI to OCaml
Stars: ✭ 26 (+8.33%)
Mutual labels:  compiler-backend
regal86
Register Allocator for 8086
Stars: ✭ 62 (+158.33%)
Mutual labels:  compiler-backend
hascal
Hascal is a general purpose and open source programming language designed to build optimal, maintainable, reliable and efficient software.
Stars: ✭ 56 (+133.33%)
Mutual labels:  compiler-backend

Carbon Intermediate Representation

The carbon intermediate representation or carbon for short is a architecture independent optimizer and code generator designed to easily connect the front end of a compiler to multiple backend architectures.

Carbon can either be used as an standalone compiler via it's own assembly-like language or linked into your compiler and accessed via its C API.

Build

Before you build carbon, make sure you have

  • C++ 11 compiler
  • CMake >= 3.0
  • Bash compatible shell (though this is only to automate build processes etc)

And to run tests you will also need python 3

To build the carbon-ir compiler, in the main directory run ./build.sh.

To build the libcarbon-ir.a static library, in the main directory run ./buildlib.sh

Running the tests

To run carbon's tests you will first need to build carbon-ir. Once you have the compiler you can just run ./test.sh to run all tests at once and run ./test.sh testX to run a specific test (change X to the number of the test you want to run).

The C API testing framework is still being worked on so for now you can go into tests/capi/ and run the makefile to generate the test executables.

Examples of the language

The carbon-ir language is a very simple and staticly typed.

This function will add 1 and 5 together and return its result

function i32 main() <global:true>
{
    %0 = i32 1
    %1 = i32 5
    %2 = add i32 %0 %1
    return i32 %2
}

The C equivalent would then be:

int main()
{
    return 1 + 5;
}

However it could also very well be:

int main()
{
    int x = 1;
    int y = 5;
    return x + y;
}

As you can see when you compile C or any other language to carbon-ir you don't have to worry about allocating variables and carbon will handle all register allocation and spills for you.

For all the other operations and info check the operations.md in the docs folder

Usage of the compiler

The most basic usage of carbon would be:

carbon-ir ./file-to-compile.ir

This will just generate a binary for the file specified called a.out in the default architecture (x86). However to change that name you can utilise the -o flag

carbon-ir ./file-to-compile -o the-name

If you would like to generate for a different machine type use the -m flag

carbon-ir ./file-to-compile -o the-name -m aarch64

Currently the architectures you can compile for are: x86, z80, aarch64 (armv8) and JVM but please check the machines.md file in the docs folder for more information on that.

For more flags check the help flag

carbon-ir --help

Usage of the C API

Here follows a simple example of the carbon C API.

#include <api/carbon.h>
int main()
{
    struct carbon carb = init_carbon("test", "x86");
    struct cfunc func = c_create_func(carb, "main", CTYPE_I32), 0, 0);

    c_add_func_attribute(func, "global:true");

    c_push_op(func, INTLIT, 1, -1, c_reg(func, 0, CTYPE_I32), CTYPE_I32);
    c_push_op(func, RETURN, c_reg(func, 0, CTYPE_I32), -1, -1, CTYPE_I32);

    /* Generate all the operations and build the executable */
    c_gen_func(func);
    c_destroy_func(func);
    c_writeassembly(carb, "test.s");
    end_carbon(carb);
}

The API is very simple and everything can be accessed by including the <api/carbon.h> file. Most features of the carbon language are included in the API, if not, leave an issue and I will implement it as fast as I can.

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].