I'm playing around with LLVM, but now I got stuck at generating code for classes.
How would one create class functionality using LLVM?
A straightforward approach is to create structs, then model methods as regular functions that receive a pointer to a struct representing the containing class - in essence, a this
pointer - as the first parameter. Allocation could be modeled by allocating the struct and then calling a special initializing function - the constructor, really - on the allocated data.
Inheritance could be done by building a struct which contains a special "parent" field (or fields, for multiple inheritance), that has a type identical to the type of the struct for the base class.
Read about virtual tables; I think they're the best starting point. You could find that the compiler basically:
Write some code which uses classes in C++, then compile it to LLVM IR with Clang and look at the generated code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With