[LLVMdev] Is it valid to add parameters to a function once it is created

Duncan Sands baldrick at free.fr
Wed May 22 01:03:28 PDT 2013


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.



More information about the llvm-dev mailing list