Approach for creating a stdlib in a toy LLVM compiled language


I’m working on a toy language and want to implement a “standard library” of sorts, but I’m not sure what the best approach is.

I’ve been reading through the JIT section of the Kaleidoscope guide, and it has pretty much exactly what I was envisioning, where the “library methods” are defined inside the compiler itself, and then the JIT looks up symbols in memory (where those library methods reside) if it can’t find them inside the IR module during linking.

The problem is that I don’t have a just-in-time compiler in my language. Instead, I have a legacy PassManager that just emits the IR of a module to an object file, and then I compile that object file using clang. With my very limited knowledge of LLVM, I assume that this probably means that the above approach that Kaleidoscope takes is not possible for me.

My first guess as to what to do is have a “standard library file” somewhere that gets compiled and linked when I use clang to do that final linking of the object file. However, I’m not sure how this will work in practice. If I ever ship my compiler to a user, how will it know where the standard library file is (or if it even exists at all)? What I mean to say is that I ideally don’t want to hard-code the location of any files into my compiler itself.

Ideally, I want the library to be contained within the compiler source code, just as in Kaleidoscope. If that isn’t possible, what’s the best approach I can take here?

Many thanks

Depending on the size of the library, your compiler could embed it as a bitcode file and append it to the module.

This could have some composability issues if you wanted to combine multiple output object files in the same final binary.