Coder Social home page Coder Social logo

mini-c's Introduction

mini-c

My (initial) experiments building compilers with LLVM.

Examples:

int fibonacci(int n) {
  if (n <= 1) {
    return 1;
  }
  return fibonacci(n - 2) + fibonacci(n - 1);
}

int factorial(int n) {
  if (n <= 1) {
    return 1;
  }
  return n * factorial(n - 1);
}

How

I'm generating LLVM-IR using inkwell, it's the only decent library I found.

After generating the IR, there are two options:

  1. Running in LLVM's JIT execution engine (commented out in the code).
  2. Dumping the IR into a file and using several utils to create the final binary.

I'm doing the latter, which is cumbersome but let's me inspect the generated assembly.

Here's the huge command I'm running:

clear \
    && cargo run -- file.c \
    && llc llvm-ir.ll -o assembly.s \
    && clang assembly.s -o a.out \
    && bat file.c \
    && (./a.out ; echo "\$? = $?")

Here's the oneliner:

clear && cargo run -- file.c && llc llvm-ir.ll -o assembly.s && clang assembly.s -o a.out && bat file.c && (./a.out ; echo "\$? = $?")
  1. My program compiles file.c to LLVM IR and dumps it to the file llvm-ir.ll.
  2. llc compiles llvm-ir.ll into the assembly file assembly.s.
  3. clang compiles assembly.s into machine-code, a.out.
  4. Show the source code, run the program (output appears here) and then print the return code.

You need llvm installed for lcc and clang.

You can also use gcc instead of clang here.

Dependencies for building

If you try running cargo run you'll meet a unpleasant linking error.

In some systems, installing llvm may solve, in Arch Linux you need to download a LLVM release tarball and unpack it.

https://github.com/llvm/llvm-project/releases/tag/llvmorg-15.0.0

Then point an env var to the resulting folder like so:

export LLVM_SYS_150_PREFIX=$(realpath ../llvm/clang+llvm-15.0.0-x86_64-linux-gnu-rhel-8.4)

Now llvm-sys and inkwell will compile fine, make sure you open your text editor of choice after setting this env var.

mini-c's People

Contributors

marcospb19 avatar

Stargazers

Doomsk avatar

Watchers

 avatar

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.