[LLVMdev] llvm-c: Types of functions

David Mirabito david.mirabito at gmail.com
Sun May 5 16:11:03 PDT 2013

 Hi All,

I'm beginning to learn & explore the LLVM API via the C bindings. Am running into
some troubles but at the moment cannot tell if this is just my misunderstanding/misuse 
of the API, or a bug somewhere.

I have a bitcode file generated with 'clang --emit-llvm' and I know this is good because
I can run the 'test' function inside with lli, no problems.

However I run into issues when I try to get the argument types to the function. The basic 
gist of my code is as follows. (Note I snipped the error checking, but everything is returning

My understanding is that I must get a handle (LLVMValueRef) to the function and from that
pull it's type as a LLVMTypeRef. This would be a 'function type', from which I can pull info about
the return type and iterate the arguments, etc. At least, that's what I'm trying to do in my code:

    LLVMContextRef context = LLVMContextCreate();
    LLVMMemoryBufferRef fileMemBuff;
            "test.bin", &fileMemBuff, &msg);
    LLVMModuleRef module;
            context, fileMemBuff, &module, &msg);

    LLVMValueRef testFunc  = LLVMGetNamedFunction(module, "test");

    if (LLVMIsAFunction(testFunc)) printf("Is a Function\n"); // <-- I do see this message, so far so good
    else printf("Not a Func\n");

    LLVMTypeRef testType;
    testType = LLVMTypeOf(testFunc);
    printf("Type is: %d\n", LLVMGetTypeKind(testType));
    printf("For reference: Function: %d Pointer: %d\n",
           LLVMFunctionTypeKind, LLVMPointerTypeKind);

    if( LLVMIsFunctionVarArg(testType)) printf("is vaarg\n");
    else printf("no vararg\n");

And at runtime I see that the LLVMTypeOf is corresponds to LLVMPointerTypeKind.

Is a Function
Type is: 12
For reference: Function: 9 Pointer: 12
Assertion failed: (isa<X>(Val) && "cast<Ty>() argument of incompatible type!"), function cast, file /Users/davidm/code/contrib/llvm/llvm/include/llvm/Support/Casting.h, line 237.

Shouldn't this be a function type?

I also can't seem to find how to learn what type it points to - LLVMPointerType() appears to create a 
new pointer-to-X but is there no getter to find what X is for this pointer in hand?

Am on SVN tip on macos.
LLVMDumpModule(module) says:
; ModuleID = 'test.bin'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.8.0"

@foo = global i32 0, align 4

; Materializable
; Function Attrs: nounwind ssp uwtable
declare void @test() #0

attributes #0 = { nounwind ssp uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }

And LLVMDumpValue(testFunc):
	; Materializable
	; Function Attrs: nounwind ssp uwtable
	declare void @test() #0

I've tried with various signatures for the test function to no effect, at the moment it just happens to be void test(void);.

What am I doing or thinking  wrong? It's possible I'm missing some init calls

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20130506/b5ac4aed/attachment.html>

More information about the llvm-dev mailing list