<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div> Hi All,</div><div><br></div><div>I'm beginning to learn & explore the LLVM API via the C bindings. Am running into</div><div>some troubles but at the moment cannot tell if this is just my misunderstanding/misuse </div><div>of the API, or a bug somewhere.</div><div><br></div><div>I have a bitcode file generated with 'clang --emit-llvm' and I know this is good because</div><div>I can run the 'test' function inside with lli, no problems.</div><div><br></div><div>However I run into issues when I try to get the argument types to the function. The basic </div><div>gist of my code is as follows. (Note I snipped the error checking, but everything is returning</div><div>OK):</div><div><br></div><div>My understanding is that I must get a handle (LLVMValueRef) to the function and from that</div><div>pull it's type as a LLVMTypeRef. This would be a 'function type', from which I can pull info about</div><div>the return type and iterate the arguments, etc. At least, that's what I'm trying to do in my code:</div><div><br></div><div> LLVMContextRef context = LLVMContextCreate();</div><div> LLVMMemoryBufferRef fileMemBuff;</div><div> LLVMCreateMemoryBufferWithContentsOfFile(</div><div> "test.bin", &fileMemBuff, &msg);</div><div> </div><div> LLVMModuleRef module;</div><div> LLVMGetBitcodeModuleInContext(</div><div> context, fileMemBuff, &module, &msg);</div><div><br></div><div> LLVMValueRef testFunc = LLVMGetNamedFunction(module, "test");</div><div> assert(testFunc);</div><div><br></div><div> if (LLVMIsAFunction(testFunc)) printf("Is a Function\n"); // <-- I do see this message, so far so good</div><div> else printf("Not a Func\n");</div><div><br></div><div><div> LLVMTypeRef testType;</div><div> testType = LLVMTypeOf(testFunc);</div><div> printf("Type is: %d\n", LLVMGetTypeKind(testType));</div><div> printf("For reference: Function: %d Pointer: %d\n",</div><div> LLVMFunctionTypeKind, LLVMPointerTypeKind);</div><div><br></div><div> if( LLVMIsFunctionVarArg(testType)) printf("is vaarg\n");</div><div> else printf("no vararg\n");</div><div><br></div></div><div>And at runtime I see that the LLVMTypeOf is corresponds to LLVMPointerTypeKind.</div><div><br></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div style="text-align: left;">./app</div><div><div style="text-align: left;">Is a Function</div></div><div><div style="text-align: left;">Type is: 12</div></div><div><div style="text-align: left;">For reference: Function: 9 Pointer: 12</div></div><div><div style="text-align: left;">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.</div></div></blockquote><div><br></div><div>Shouldn't this be a function type?</div><div><br></div><div>I also can't seem to find how to learn what type it points to - LLVMPointerType() appears to create a </div><div>new pointer-to-X but is there no getter to find what X is for this pointer in hand?</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div style="text-align: left;"><br></div><div style="text-align: left;"><br></div></blockquote>Am on SVN tip on macos.<div>LLVMDumpModule(module) says:</div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div>; ModuleID = 'test.bin'</div></div><div><div>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"</div></div><div><div>target triple = "x86_64-apple-macosx10.8.0"</div></div><div><div><br></div></div><div><div>@foo = global i32 0, align 4</div></div><div><div><br></div></div><div><div>; Materializable</div></div><div><div>; Function Attrs: nounwind ssp uwtable</div></div><div><div>declare void @test() #0</div></div><div><div><br></div></div><div><div>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" }</div></div></blockquote><div><div><br></div><div>And LLVMDumpValue(testFunc):</div></div><div><div><span class="Apple-tab-span" style="white-space:pre"> </span>; Materializable</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>; Function Attrs: nounwind ssp uwtable</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>declare void @test() #0</div></div><div><br></div><div>I've tried with various signatures for the test function to no effect, at the moment it just happens to be void test(void);.</div><div><br></div><div>What am I doing or thinking wrong? It's possible I'm missing some init calls</div><div><br></div><div>Thanks,</div><div>DavidM</div></body></html>