Cast specific pointer type to generic one

Hi,

Suppose I have a pointer to “something” (a structure I defined) and I want to pass the pointer to a generic function, that gets a 64-bit address pointer.

How do I do that?

For instance:

The function is:

void Foo (void *);

I get the specific pointer using getPointerOperand() on a store instruction that store to it:

inst->getPointerOperand()->getType()

Now I want to call Foo with the pointer, but the types of the arguments are mismatches - one is specific (%“class.XXX::YYY”*) and the other generic.

How should I cast it so there won’t be a mismatch?

Thanks in advance,

Tehila.

Hi,

Suppose I have a pointer to "something" (a structure I defined) and I want
to pass the pointer to a generic function, that gets a 64-bit address
pointer.

How do I do that?

For instance:

The function is:

void Foo (void *);

I get the specific pointer using getPointerOperand() on a store instruction
that store to it:

inst->getPointerOperand()->getType()

Now I want to call Foo with the pointer, but the types of the arguments are
mismatches - one is specific (%"class.XXX::YYY"*) and the other generic.

How should I cast it so there won't be a mismatch?

Perhaps compiling similar code with Clang and looking at Clang's
generated IR would give you some insight into how that might be done.

Plugging void foo(void p) { } int main() { struct foo { int a; } fum; foo(&fum); } into (the demo) with optimization set to none and selecting “LLVM C++ API code” gives (among a lot of other stuff): // Function: main (func_main) { BasicBlock label_entry_10 = BasicBlock::Create(mod->getContext(), “entry”,func_main,0); // Block entry (label_entry_10) AllocaInst* ptr_fum = new AllocaInst(StructTy_struct_foo, “fum”, label_entry_10); ptr_fum->setAlignment(4); CastInst* ptr_11 = new BitCastInst(ptr_fum, PointerTy_1, “”, label_entry_10); CallInst* void_12 = CallInst::Create(func_foo, ptr_11, “”, label_entry_10); void_12->setCallingConv(CallingConv::C); void_12->setTailCall(false); AttributeSet void_12_PAL; void_12->setAttributes(void_12_PAL); ReturnInst::Create(mod->getContext(), const_int32_7, label_entry_10); } return mod; } The BitCastInst is the key. -Rich