[LLVMdev] Calling functions

Álvaro Castro alvcastro at yahoo.es
Mon Feb 25 05:42:08 PST 2008




----- Mensaje original ----

De: Benjamin Smedberg <benjamin at smedbergs.us>

Para: LLVM Developers Mailing List <llvmdev at cs.uiuc.edu>

Enviado: viernes, 22 de febrero, 2008 22:36:40

Asunto: Re: [LLVMdev] Calling functions



-----BEGIN  PGP  SIGNED  MESSAGE-----

Hash:  SHA1



Álvaro  Castro  wrote:

|  Hello!

|

|

|  I'm  trying  LLVM  for  generating  code  and  I  found  something  I  cannot  figure

out  or  find  in  the  documentation:

|

|  I'm  doing  something  like  this  to  call  "sin"  from  std:

|

|     std::vector<const  Type*>  params;

|     params.push_back(  Type::FloatTy  );

|     FunctionType  *FT  =  FunctionType::get(  Type::FloatTy,  params,  false);

|     Function  *F  =  new  Function(  FT,  Function::ExternalLinkage,  "sin",  M  );

|     CallInst  *CallExternal  =  new  CallInst(  F,  SomeValue,  "external_call",  BB   );

|

|

|  I  can  make  some  calls  to  functions  like  glclear()  as  I'm  using  opengl:

|  ...

|     Function  *F  =  new  Function(  FT,  Function::ExternalLinkage,  "glclear",  M  );

|  ...

|

|  But  if  I  want  to  call  any  function  inside  a  C++  namespace  I  don't  find  the

way  to  do  it.  Imagine  you  want  to  call

|

|  othernamespace::globalFunction()



See  this  older  message:



http://lists.cs.uiuc.edu/pipermail/llvmdev/2007-December/011616.html





*************************



It seems that the previous message was sent with some corruption, sorry for it. It it happens again I'll use another e-mail.



Thanks for your reply, Benjamin.



I've tried that approach to the c++ function calls. I ended up with this code:



#######

float test( float input )

{

  return 0.0;

}



 [...]



  typedef float (*P_FUNCTION) ( float );

  P_FUNCTION pt2Function = &test;

  intptr_t functionInt = (intptr_t)pt2Function;

  Constant *functionConstant = ConstantInt::get( APInt( sizeof(intptr_t), functionInt) );
  const Type *ArgTy = Type::FloatTy;

  PointerType* arg = PointerType::get( ArgTy, 0 );

  functionConstant = ConstantExpr::getIntToPtr( functionConstant, arg );



[...] (BB and CoefficientOne definition and other stuff)



  CallInst *CallExternal = new CallInst(functionConstant, CoefficientOne, "external_call", BB );

  CallExternal->setTailCall();



#######

The runtime error was:

/var/tmp/portage/sys-devel/llvm-base-2.2/work/llvm-2.2/include/llvm/Support/Casting.h:199: typename llvm::cast_retty<To, From>::ret_type llvm::cast(const Y&) [with X = llvm::FunctionType, Y = const llvm::Type*]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.




I must admit I used AddressSpace = 0, but I have no idea of what to put there. I don't understand such a problem of types, as everything is a float except the integer holding the function address. I suppose the problem is elsewhere.

Thanks a lot for your help.

.alvaro.castro.




       
______________________________________________ 
¿Con Mascota por primera vez? Sé un mejor Amigo. Entra en Yahoo! Respuestas http://es.answers.yahoo.com/info/welcome




More information about the llvm-dev mailing list