سؤال

I'm using llvm-py to create a DIY compiler for some artificial and need to have a virtual method table in the globe scope. My concept is to have several arrays of function pointers (one for each class). Unfortunately there's no LLVM IR Builder for a global scope and I cannot use ptrtoint in order to have uniform type of all array elements (otherwise I would store function addresses as 64-bit ints and cast them to appropriate types before calling). Do you know any reasonable solution? It can also be illustrated with C++ LLVM api, because llvm-py is very similar.

هل كانت مفيدة؟

المحلول

Indeed, IRBuilder does not expose an interface to do that, but you can create it manually - e.g. by using the constructors of GlobalVariable. You can store all the pointers in an array using conversion constant expressions, i.e. by generating:

@global = global [4 x i64*] [
  i64* bitcast (void()* @f to i64*),
  i64* bitcast (float(i32)* @g to i64*),
  ...
]

So, use ConstantExpr::getBitCast() to generate the casts from the Function to the array element type (which should preferrably be a pointer, I don't see the advantage in storing an i64). Then create a new GlobalVariable in the module and initialize it with all the constant expressions you've created.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top