-2

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.

Free TNT
  • 61
  • 1
  • 3
  • 9
  • do you have a specific question? asking for examples or any off-site resources is offtopic – 463035818_is_not_an_ai Feb 07 '19 at 16:48
  • 1
    You probably want to implement the value of your language as some tagged union (or as some pointer), and your LLVM IR would return that type. – Basile Starynkevitch Feb 07 '19 at 17:07
  • That's 1 long sentence and no question in sight! – Robert Andrzejuk Feb 07 '19 at 17:11
  • The question is the "how" to do that. – Free TNT Feb 07 '19 at 17:34
  • The dynamic type is a property of the value you pass. The value itself has a static type nevertheless. For example, you can be passing `std::variant`: it's a dynamically typed value with static type. Its dynamic type is like a color: just because cars come in many colors (the dynamic aspect), they are still cars. Just because values come in many dynamic types, they are still values: this hints that you need a concrete `Value` type that represents a dynamically-typed value. If you'll make that concrete type fit in a `void*`, then you'll be passing around a suitably sized integer. – Kuba hasn't forgotten Monica Feb 11 '19 at 13:51

1 Answers1

5

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.

Dan M.
  • 3,818
  • 1
  • 23
  • 41
  • Thanks, i was thinking of something similar to ruby in my mind, i think I'll look into how MacRuby is done https://github.com/MacRuby/MacRuby its an implementation of ruby with LLVM, although its discontinued the code still might help. – Free TNT Feb 07 '19 at 17:02
  • @FreeTNT so you are creating your own language. In that case, you don't really need to think about LLVM (and especially IR). Think about how it'll work underneath. LLVM IR is more or less just higher-level asm. Implementation detail. You come up with how dynamic types would be implemented in your language. Then you just implement it via LLVM. Or it can be compiled C/C++. Or anything. That's just a back-end at this point. In your own language, you can convert `foo + 42` to anything you want it to be. – Dan M. Feb 07 '19 at 17:07