Go to file
2025-07-09 22:32:47 +03:00
reid Fix old fibonacci not working due to last change 2025-07-09 22:32:47 +03:00
reid-llvm-lib Add RetVoid just in case 2025-07-09 22:01:32 +03:00
.gitignore Add rudamentary LLVM lib stuff, make a fully compiling executable 2025-06-24 23:10:44 +03:00
Cargo.lock Refactor a bunch of stuff, produce compiling MIR 2025-07-04 21:30:40 +03:00
Cargo.toml Add rudamentary LLVM lib stuff, make a fully compiling executable 2025-06-24 23:10:44 +03:00
libtest.sh Transform AST into MIR successfully, completing the chain 2025-07-05 01:34:57 +03:00
main.cpp Add rudamentary LLVM lib stuff, make a fully compiling executable 2025-06-24 23:10:44 +03:00
README.md Update README.md 2025-07-09 20:27:12 +03:00

Reid-LLVM

Reid is a toy-language compiler I'm working on to learn LLVM (and compiler development).

Reid only uses llvm-sys, which provide very minimal bindings from the LLVM C-API to Rust. reid_llvm-crate contains the relevant abstraction to produce a more Rust'y API from that.

Much of the syntax in Reid is directly inspired by rust, but mostly it is driven by simplicity.

Reid is currently able to (non-exhaustively):

  • Do basic algebra (e.g. Add, Sub, Mult)
  • Resolve complex one-liners correctly using PEDMAS (e.g. 5 + 2 * 5 - 5 * 5 is calculated correctly)
  • Declare and call functions with varying parameters and return types
  • Perform type-checking and type-inference such that return-types and parameter types must always match.
  • Do simple logic-operations (e.g. If/And/Or)

An example program of Reid, that calculates the 5th fibonacci number (and uses Rust for highlighting) is:

fn main() -> u16 {
    return fibonacci(5);
}
fn fibonacci(n: u16) -> u16 {
    if n <= 2 {
        return 1;
    }
    return fibonacci(n-1) + fibonacci(n-2);
}

Currently missing relevant features (TODOs) are:

  • Arrays
  • Structs (and custom types as such)
  • Extern functions
  • Strings
  • Loops

Why "Reid"

ᚱ is an Elder Futhark rune which means "ride" or "journey". As this language is meant for me to primarily learn language design and compiler development, it is more about the journey rather than the destination. ᚱ is written as "Reið" in Icelandic, which is the inspiration behind "Reid" here.

Why "Reid-LLVM"?

Because I have another project also called Reid, which only compiles to a Virtual Instruction Set Architecture (V-ISA) that is executed via a custom-made Virtual Machine. It is still hosted here, but do note that it is very old and not as representative of my skills as a programmer today as this one.

What is currently being tested?

Currently when testing the compiler I run ./libtest.sh fibonacci or ./libtest.sh arithmetic.

What ./libtest.sh $1 does, is it compiles and runs the rust example found with name $1, which may exist in any of the two projects. The two mentioned above are in reid/examples.

All examples currently end up producing a hello.o and hello.asm file to the root directory, which is then linked with ldd to produce a main, which is finally executed.

This is currently very work-in-progress and many things about this repository change erratically.

Various notes in order to get this working properly

This is what worked for me, might not (probably) work for you, depending on various versions of various libraries.

Compiling LLVM 16.0.0

Context

Context for my computer. I am on ArchLinux, and here are some libraries and their current versions that I have installed as of compiling, I'm not sure what of them are relevant, if any, but saving them here still feels like a good idea for the future:

  • cmake 3.27.0-1
  • lib32-llvm-libs 15.0.7-1
  • llvm 15.0.7-3
  • llvm-libs 15.0.7-3
  • gcc 13.1.1-2
  • gcc-libs 13.1.1-2
  • lib32-gcc-libs 13.1.1-2
  • lld 15.0.7-2
  • lldb 15.0.7-3
  • clang 15.0.7-9
  • make 4.4.1-2
  • automake 1.16.5-2

Commands

wget https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.0/llvm-16.0.0.src.tar.xz
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-16.0.0/cmake-16.0.0.src.tar.xz

tar xvf llvm-16.0.0.src.tar.xz
tar xvf cmake-16.0.0.src.tar.xz

mv cmake-16.0.0.src cmake

cd llvm-16.0.0.src

cmake -B build -DCMAKE_INSTALL_PREFIX=$HOME/llvm-16 -DCMAKE_BUILD_TYPE=MinSizeRel -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_INCLUDE_TESTS=OFF

make -j8

Also Note: Building LLVM with Ninja was not successful for me, but this method was. Ninja may be successful with you, to try it, add -G Ninja to the cmake-command, and instead of make run ninja install.

Building this crate itself

Assuming llvm-16.0.0.src from the previous step was at /path/llvm-16.0.0.src, building this crate can be done via the following command:

LLVM_SYS_160_PREFIX=/path/llvm-16.0.0.src/build cargo build

In conclusion

Good luck! It took me a good 10 hours to figure this out for myself, I sure hope these instructions help both myself and someone else in the future!