Skip to content

msd/llvm-comp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compiler using LLVM-IR

Program to parse and compile source files of a C-like language (called "the language" henceforth). The language supports int, float and bool which map exactly to the corresponding C types.

On ubuntu 22.04

Installing requirements

sudo apt install build-essential clang llvm

Compiling the compiler

This project uses the common out-of-source build layout. Execute the following to produce the language compiler

mkdir build
cd build
cmake ../src
cmake --build .

This generates the executable my_compiler in the folder build.

You can add this compiler to the PATH environment variable for your convenience. (The next section assumes you have done so.)

which can be used to compile a single source file of the language, producing output.ll.

Using the output.ll and some driver code (see tests folder) one can produce an executable

Compiling a program to LLVM IR

Suppose you are trying to compile the language source file program.c

my_compiler program.c

should produce the LLVM intermediate representation (IR) program.ll with all functions having external linkage.

Driver

At the moment separate driver file is needed to print the results. Simply add the program's functions' signatures in driver.cpp and then you can use them like regular functions.

For example

// program.c
int hello(float x, bool y){
    // ...
}
// driver.cpp
#include <iostream>

int hello(float x, bool y);

int main()
{
    std::cout << "ans=" << hello(10.0f, false) << std::endl;
}

Note: you can find more examples in the tests folder.

Generate executable

clang++ program.ll driver.cpp -o my_executable

About

llvm frontend for a c-like language

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published