[LLVMdev] Having JIT resolve extern "C" functions declared in executible
Óscar Fuentes
ofv at wanadoo.es
Sun Aug 23 06:27:16 PDT 2009
Renato Golin <rengolin at systemcall.org> writes:
> 2009/8/22 Bill Wendling <isanbard at gmail.com>:
>> I think you might have to provide an empty list if your function doesn't
>> take parameters. Maybe using an irbuilder would help?
>
> It does take one parameter. Here's the important bits:
>
> // My Function
> extern "C"
> void print(double X) {
> printf("%f\n", X);
> }
>
> // Args type
> std::vector<const Type*> args(1, Type::getDoubleTy(getGlobalContext()));
> // return void, 1 arg double, no varargs
> FT = FunctionType::get(Type::getVoidTy(getGlobalContext()), args, false);
> // creating stub for print function
> Function* printFunction = Function::Create(FT,
> Function::ExternalLinkage, "print", module);
>
> By doing this (and the variable declaration below), my IR is:
>
> ; ModuleID = 'example'
>
> define void @main() {
> entry:
> %var = alloca double ; <double*> [#uses=1]
> store double 1.000000e+01, double* %var
> }
>
> declare void @print(double)
> ; ================== END
>
> The function call is, then:
> // Number 10
> Value* value = ConstantFP::get(getGlobalContext(), APFloat(10.0));
> // Variable var is double
> Value* var = builder.CreateAlloca(Type::getDoubleTy(getGlobalContext()),
> 0, "var");
> // Assign it value 10
> builder.CreateStore(value, var);
>
> // Create a call to the function, passing a double var
> builder.CreateCall(printFunction, var, "print");
>
>
> If var is DoubleTy and the arg is expecting DoubleTy, why is the assert failing?
The alloca has type double*, i.e. you are passing the address of a
double to a function that expects a value. Forget about the alloca and
just pass the value you create with ConstantFP::get.
[snip]
--
Óscar
More information about the llvm-dev
mailing list