The blog post I linked to implied that adding a symbol in the form lle_X_FUNCTIONNAME would allow you to call a function called FUNCTIONNAME. Is this not the case? On Thu, Aug 19, 2010 at 6:46 PM, <o.j.sivart at gmail.com> wrote:> You are adding the symbol as "lle_X_create_number_object" yet your error > message implies you have tried to lookup and use "create_number_object". Can > you provide the code for the lookup? > > On 20/08/2010, at 8:07 AM, Alec Benzer wrote: > > Is there documentation somewhere on how to call external functions from > llvm? The only guide I found was this: > http://www.gearleaf.com/blog/post/44, and it doesn't seem to be working > for me. > > I have a function: > > llvm::GenericValue lle_X_create_number_object(llvm::FunctionType* ft, const > std::vector<llvm::GenericValue>& args) > { > llvm_object_structure* result = new llvm_object_structure; > result->typeIdx = TypeSystem::number; > result->data = reinterpret_cast<unsigned char*>(new > double(args[0].DoubleVal)); > llvm::GenericValue gv; > gv.PointerVal = reinterpret_cast<void*>(result); > return gv; > } > > defined in an extern "C" block. I add it as a symbol with: > > > llvm::sys::DynamicLibrary::AddSymbol("lle_X_create_number_object",(void*)lle_X_create_number_object); > > yet when attempting to call it I still get: "LLVM ERROR: Program used > external function 'create_number_object' which could not be resolved!" > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100819/81b65942/attachment.html>
Ok, so calling lle_X_FUNCTIONNAME instead of FUNCTIONNAME works (is there any reason the post used the lle_X_ prefix other than convention? was it once possible to call external functions as I was originally trying, or, why did the blog post think it was possible?) I'm now running into trouble when trying to access arguments however. printing args.size() from the function yields 12105250188025543488. The generated call to it looks like: %object_tmp = call %object_structure* @lle_X_create_number_object(double 5.000000e+00) ; <%object_structure*> [#uses=1] On Thu, Aug 19, 2010 at 6:56 PM, Alec Benzer <alecbenzer at gmail.com> wrote:> The blog post I linked to implied that adding a symbol in the form > lle_X_FUNCTIONNAME would allow you to call a function called FUNCTIONNAME. > Is this not the case? > > > On Thu, Aug 19, 2010 at 6:46 PM, <o.j.sivart at gmail.com> wrote: > >> You are adding the symbol as "lle_X_create_number_object" yet your error >> message implies you have tried to lookup and use "create_number_object". Can >> you provide the code for the lookup? >> >> On 20/08/2010, at 8:07 AM, Alec Benzer wrote: >> >> Is there documentation somewhere on how to call external functions from >> llvm? The only guide I found was this: >> http://www.gearleaf.com/blog/post/44, and it doesn't seem to be working >> for me. >> >> I have a function: >> >> llvm::GenericValue lle_X_create_number_object(llvm::FunctionType* ft, >> const std::vector<llvm::GenericValue>& args) >> { >> llvm_object_structure* result = new llvm_object_structure; >> result->typeIdx = TypeSystem::number; >> result->data = reinterpret_cast<unsigned char*>(new >> double(args[0].DoubleVal)); >> llvm::GenericValue gv; >> gv.PointerVal = reinterpret_cast<void*>(result); >> return gv; >> } >> >> defined in an extern "C" block. I add it as a symbol with: >> >> >> llvm::sys::DynamicLibrary::AddSymbol("lle_X_create_number_object",(void*)lle_X_create_number_object); >> >> yet when attempting to call it I still get: "LLVM ERROR: Program used >> external function 'create_number_object' which could not be resolved!" >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >> >> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100819/9962a17a/attachment.html>
Alec Benzer <alecbenzer at gmail.com> writes:> Ok, so calling lle_X_FUNCTIONNAME instead of FUNCTIONNAME works (is there > any reason the post used the lle_X_ prefix other than convention? was it > once possible to call external functions as I was originally trying, or, why > did the blog post think it was possible?)AFAIK you can name the function as you please (with some reasonable restrictions on the character set, etc)> I'm now running into trouble when trying to access arguments however. > printing args.size() from the function yields 12105250188025543488. The > generated call to it looks like: > > %object_tmp = call %object_structure* @lle_X_create_number_object(double > 5.000000e+00) ; <%object_structure*> [#uses=1]You are calling a function that takes a single argument of type `double'. But on a previous post you showed the function with this signature: llvm::GenericValue lle_X_create_number_object(llvm::FunctionType* ft, const std::vector<llvm::GenericValue>& args) so as the execution enters the function above from your call, the stack can be considered corrupt. Also, the return type of the `call' might not be right (it depends on the ABI of your platform.) It is strange to generate GenericValue's from JITted code. Are you doing some kind of meta-interpreter? I'm afraid that you might be utterly confused about how to use LLVM.
Alec Benzer wrote:> The blog post I linked to implied that adding a symbol in the form > lle_X_FUNCTIONNAME would allow you to call a function called > FUNCTIONNAME. Is this not the case?It is not. I haven't read the blog post, but I recognize the lle_X_ format as part of the internals of the llvm interpreter. If you were modifying the internals of the interpreter, then this might work. If you want to JIT (or even use the interpreter with libffi) and call functions declared in your program, you write your function *normally*. No renaming, certainly no GenericValue cruft. Please see the tutorial at llvm.org/docs/tutorial/, in particular chapter 4 where they demo doing exactly this. Nick> On Thu, Aug 19, 2010 at 6:46 PM, <o.j.sivart at gmail.com > <mailto:o.j.sivart at gmail.com>> wrote: > > You are adding the symbol as "lle_X_create_number_object" yet your > error message implies you have tried to lookup and use > "create_number_object". Can you provide the code for the lookup? > > On 20/08/2010, at 8:07 AM, Alec Benzer wrote: > >> Is there documentation somewhere on how to call external functions >> from llvm? The only guide I found was this: >> http://www.gearleaf.com/blog/post/44, and it doesn't seem to be >> working for me. >> >> I have a function: >> >> llvm::GenericValue lle_X_create_number_object(llvm::FunctionType* >> ft, const std::vector<llvm::GenericValue>& args) >> { >> llvm_object_structure* result = new llvm_object_structure; >> result->typeIdx = TypeSystem::number; >> result->data = reinterpret_cast<unsigned char*>(new >> double(args[0].DoubleVal)); >> llvm::GenericValue gv; >> gv.PointerVal = reinterpret_cast<void*>(result); >> return gv; >> } >> >> defined in an extern "C" block. I add it as a symbol with: >> >> llvm::sys::DynamicLibrary::AddSymbol("lle_X_create_number_object",(void*)lle_X_create_number_object); >> >> yet when attempting to call it I still get: "LLVM ERROR: Program >> used external function 'create_number_object' which could not be >> resolved!" >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> >> http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev