Search code examples
c++ccompiler-constructionllvmlanguage-design

Implementing a dynamic typed language with LLVM IR


I'm trying to build a JIT compiler for a dynamic language using LLVM C API but I'm stuck at implementing dynamic types, for example in function definition, LLVM needs types for each argument but the type is unknown until runtime based on what the user passes, i googled this for a while but no any good resources about it anywhere, I also tried looking at Julia's source code to see how they did it, unfortunetely the code is big and complex and i have to eye jump everywhere to find such a small detail, from what i seen so far in it is they represent their types as an empty LLVM struct pointers and a func sig type that holds some extra data, but I'm very unsure of how that works or even if I'm explaining it right, any resources can be helpful, an example code is most appreciated, the example doesn't have to be in C API, C++ is also fine I'll convert it myself one way or the other.

Thanks in advance.


Solution

  • LLVM IR has nothing to do with this. It's already a language. Statically typed one (of course). It has no impact on what can be compiled down to it.

    Where are many ways to go about this, but ultimately, it's no different than implementing JIT-compiler for any dynamically-typed language.

    Think about how you want it to work. How would it function. How other dynamically-typed langues handle this.

    Hint: they are all typed, even if they only have 3 types: Object, String and Number, if not less. They just use some way to figure out the referenced type at run time.

    Do you have an existing language in mind or are you in the process of designing it? If it's the former, than study existing implementations/specifications, if it's the former - than you can do anything you want really, you have all the power.