Hi all, I am trying to extend a FunctionType to include new parameters. In particular, I want to ensure that the main function has been declared with both argsc and argsv. However there seems to be no easy way to accomplish this: llvm::Function::getFunctionType() returns a a reference to a const object, while modifying only the argument list yields an error during verification since the function type does not match its arguments. Is there any approach that I am missing or a simple workaround to this problem? Thanks in advance, and best regards, Gabriel -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110228/c27e9aec/attachment.html>
On 2/28/11 6:31 AM, Gabriel Rodríguez wrote:> Hi all, > > I am trying to extend a FunctionType to include new parameters. In > particular, I want to > ensure that the main function has been declared with both argsc and > argsv. However > there seems to be no easy way to accomplish this: > llvm::Function::getFunctionType() returns a > a reference to a const object, while modifying only the argument list > yields an error during verification > since the function type does not match its arguments. Is there any > approach that I am missing or > a simple workaround to this problem?If I understand correctly, you are trying to add a parameter to the main() function, correct? If so, then you can't just modify the existing main() function. Instead, you have to create a new function with an empty function body with the new parameter and then clone the body of the old main() function into the new main() function. There is code to do that in the poolalloc project. Check out http://llvm.org/svn/llvm-project/poolalloc/trunk and look for the code that does this in lib/PoolAllocate/PoolAllocate.cpp in the MakeFunctionClone() method. -- John T.> > Thanks in advance, and best regards, > Gabriel-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110228/9fb1b1be/attachment.html>
2011/2/28 John Criswell <criswell at illinois.edu>:> On 2/28/11 6:31 AM, Gabriel Rodríguez wrote: > > Hi all, > > I am trying to extend a FunctionType to include new parameters. In > particular, I want to > ensure that the main function has been declared with both argsc and argsv. > However > there seems to be no easy way to accomplish this: > llvm::Function::getFunctionType() returns a > a reference to a const object, while modifying only the argument list yields > an error during verification > since the function type does not match its arguments. Is there any approach > that I am missing or > a simple workaround to this problem? > > If I understand correctly, you are trying to add a parameter to the main() > function, correct? > > If so, then you can't just modify the existing main() function. Instead, > you have to create a new function with an empty function body with the new > parameter and then clone the body of the old main() function into the new > main() function.I don't think a full clone is necessary, since he wants to replace the function. He only needs to create the new function and splice in the body of the old one. Gabriel: look at Function::getBasicBlockList() and iplist<>::splice(iterator, iplist). Something like Function *NewF = Function::Create(NewFnType, OldF->getLinkage()); NewF->getBasicBlockList().splice(NewF->begin(), OldF->getBasicBlockList()); NewF->takeName(OldF); OldF->eraseFromParent(); is probably what you're looking for. (Note: completely untested)