Frank Winter
2013-May-21 19:19 UTC
[LLVMdev] Is it valid to add parameters to a function once it is created
I create a function with an empty signature: void llvm_start_new_function() { llvm::outs() << "Staring new LLVM function ...\n"; Mod = new llvm::Module("module", llvm::getGlobalContext()); builder = new llvm::IRBuilder<>(llvm::getGlobalContext()); llvm::FunctionType *funcType = llvm::FunctionType::get(builder->getVoidTy(), false); mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", Mod); llvm::BasicBlock* entry = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entrypoint", mainFunc); builder->SetInsertPoint(entry); llvm_counters::param_counter = 0; llvm_counters::label_counter = 0; } and then add parameters as needed with something like llvm::Argument * u8 = new llvm::Argument( llvm::Type::getInt8Ty(llvm::getGlobalContext()) , param_next() , mainFunc ); Is this valid?
Duncan Sands
2013-May-22 08:03 UTC
[LLVMdev] Is it valid to add parameters to a function once it is created
Hi Frank, On 21/05/13 21:19, Frank Winter wrote:> I create a function with an empty signature: > > void llvm_start_new_function() { > llvm::outs() << "Staring new LLVM function ...\n"; > > Mod = new llvm::Module("module", llvm::getGlobalContext()); > builder = new llvm::IRBuilder<>(llvm::getGlobalContext()); > > llvm::FunctionType *funcType > llvm::FunctionType::get(builder->getVoidTy(), false); > mainFunc = llvm::Function::Create(funcType, > llvm::Function::ExternalLinkage, "main", Mod); > > llvm::BasicBlock* entry > llvm::BasicBlock::Create(llvm::getGlobalContext(), "entrypoint", mainFunc); > builder->SetInsertPoint(entry); > > llvm_counters::param_counter = 0; > llvm_counters::label_counter = 0; > } > > and then add parameters as needed with something like > > llvm::Argument * u8 = new llvm::Argument( > llvm::Type::getInt8Ty(llvm::getGlobalContext()) , param_next() , mainFunc ); > > Is this valid?no. The arguments have to match the function type. If you had declared funcType like this "void ()(i32, i32)" then the call to llvm::Function::Create would have created a function with two arguments, i.e. the arguments are already there. However by declaring it "void ()(void)" then it is created with no arguments and has to stay that way. Maybe you should declare the type to be varargs instead (the second parameter to FunctionType::get)? Ciao, Duncan. PS: If you build LLVM with assertions enabled and run the verifier on your generated bitcode, you should find that your bitcode is rejected.
Seemingly Similar Threads
- [LLVMdev] noob IR builder question
- [LLVMdev] Best strategy to add a parameter to a function
- LLVM 3.8 change in function argument lists?
- Functions within functions in R and S-Plus
- [LLVMdev] Code crashing in CreateGlobalStringPtr, passes when I add code for main routine + entry