title: Function pointer as Argument to call
body: <p>I am trying to create a call to a function <code>foo</code> that receives a function pointer (with signature <code>void bar(void)</code>) as argument. From <a href="https://stackoverflow.com/questions/19803848/llvm-insert-pthread-function-calls-into-ir">This</a> post I got the basic Idea to accomplish this.
  First I register the Foo function. Bar is actually a function that has been compiled by LLVM so there is no registration neccessary.</p>
  
  <pre><code>FunctionType* BarType =  FunctionType::get(Type::getVoidTy(getGlobalContext()), false);
  
  Type* FooType[1];
  FooType[0] = static_cast&lt;Type*&gt;(BarType)-&gt;getPointerTo();
  ArrayRef&lt;Type*&gt; FooTypeARef(FooType, 1);
  
  FunctionType* signature = FunctionType::get(Type::getInt32Ty(getGlobalContext()), FooTypeARef, false);
  Function* func = Function::Create(signature, Function::ExternalLinkage, "Foo", TheModule);
  LLVM_ExecutionEngine()-&gt;addGlobalMapping(func, const_cast&lt;void*&gt;(&amp;Foo));
  </code></pre>
  
  <p>This is how the actual call is inserted (<strong>Case I</strong>)</p>
  
  <pre><code>std::vector&lt;Value*&gt; ArgsV_Foo;
  Function *FooFun= LLVM_Module()-&gt;getFunction("Foo");
  Function* BarFun = LLVM_Module()-&gt;getFunction("Bar");
  
  ArgsV_Foo.push_back( BarFun );
  
  LLVM_Builder()-&gt;CreateCall(FooFun, ArgsV, "calltmp")
  </code></pre>
  
  <p>However, this aborts inside CreateCall with the reason "Calling a function with a bad signature". I am not sure if the line <code>ArgsV_Foo.push_back( BarFun )</code> is correct. </p>
  
  <p>A different approach that seems possible to me is to use ExecutionEngine to get a pointer to Bar, but I do not understand how to convert the resulting function pointer to a <code>llvm::Value*</code> (<strong>Case II</strong>)</p>
  
  <pre><code>std::vector&lt;Value*&gt; ArgsV_Foo;
  Function *FooFun= LLVM_Module()-&gt;getFunction("Foo");
  Function* BarFun = LLVM_Module()-&gt;getFunction("Bar");
  
  void* BarFunPtr = LLVM_ExecutionEngine()-&gt;getPointerToFunction(BarFun);
  Value* val = ??
  ArgsV_FEFork.push_back(val);
  
  LLVM_Builder()-&gt;CreateCall(FooFun, ArgsV, "calltmp")
  </code></pre>
  
  <p>Maybe someone has an idea how to accomplish the second case or confirm that the assignment in the first case is correct (and the problem sits elsewhere).</p>