Don't you have the same problem with other atoms, e.g. variable names? This sounds like something that should be implemented in the language's runtime library. On May 4, 2014 7:15 PM, "Alexander Popolitov" <popolit at gmail.com> wrote:> Hi everyone! > > I'm trying to implement lisp's funcall function, which roughly calls a > function, name of which > is known only at runtime. > I know that LLVM IR 'call' directive can accept function pointers, so the > question is, > is there a simple way to get a function pointer from a function name > (represented as e.g. i8*)? > > BTW, I'm using llvmpy to generate LLVM IR, so if there is a way to do this > using llvmpy tools, it would also do. > > Yours sincerely, > Alexandr Popolitov > _______________________________________________ > 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/20140504/dc833792/attachment.html>
Well, the only place I need to get a variable pointer (pointer to a symbol) from its name, is in intern function. And symbols are implemented as Python classes, so to LLVM IR they appear as PyObjects, or, more precisely, as i8*'s. But all the elementary functions, that operate on symbols (e.g. cons) are written with the in mind. Then, for functions I could have done just that: write a wrapper around get_function_by_name method of Module object. However, this will give me PyObject*, not a pointer to an actual function, which I suppose should be fed to call instruction. Then, maybe, question can be rephrased as: "how to obtain function pointer from Function llvmpy's object"? On 05/04/2014 05:24 PM, Alexander Potapenko wrote:> > Don't you have the same problem with other atoms, e.g. variable names? > This sounds like something that should be implemented in the > language's runtime library. > > On May 4, 2014 7:15 PM, "Alexander Popolitov" <popolit at gmail.com > <mailto:popolit at gmail.com>> wrote: > > Hi everyone! > > I'm trying to implement lisp's funcall function, which roughly > calls a function, name of which > is known only at runtime. > I know that LLVM IR 'call' directive can accept function pointers, > so the question is, > is there a simple way to get a function pointer from a function > name (represented as e.g. i8*)? > > BTW, I'm using llvmpy to generate LLVM IR, so if there is a way to > do this using llvmpy tools, it would also do. > > Yours sincerely, > Alexandr Popolitov > _______________________________________________ > 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140504/75522217/attachment.html>
Hi,> define i8* @bar(i8* %x) { > entry: > %0 = alloca [4 x i8], align 1 > store [4 x i8] c"foo\00", [4 x i8]* %0, align 1 > %calltmp = call i8* @intern([4 x i8]* %0) > %ptrfind = call i8* @find_llvm_function(i8* %calltmp) > %cast = bitcast i8* %ptrfind to i8* (i8*)* > %calltmp1 = call i8* %cast(i8* %x) > ret i8* %calltmp1 > } > > except last two lines (with %calltmp1 and ret) do not actually get > generated, because generation of call instruction fails with > error 'argument is neither a function nor a function pointer'Since this function works when put into a text file and run through llc manually (& opt & ....), it looks like the problem is that you're not creating the IR you think you are. What gets output when you call "dump" on the callee parameter to CreateCall (or whatever the python version you're using is)?> Also, there is some possibility, that I got the guts of @find_llvm_function > wrong, however, in that case I'd expect, that code would still be generated, > and problems would manifestate only at runtime.Agreed. Cheers. Tim.
Ok, I've managed to get pointer to a function via ptr attribute of Function object. now (defun foo (x) x) (defun bar (x) (funcall 'foo x)) generates the following LLVM IR: define i8* @foo(i8* %x) { entry: ret i8* %x } define i8* @bar(i8* %x) { entry: %0 = alloca [4 x i8], align 1 store [4 x i8] c"foo\00", [4 x i8]* %0, align 1 %calltmp = call i8* @intern([4 x i8]* %0) %ptrfind = call i8* @find_llvm_function(i8* %calltmp) %cast = bitcast i8* %ptrfind to i8* (i8*)* %calltmp1 = call i8* %cast(i8* %x) ret i8* %calltmp1 } except last two lines (with %calltmp1 and ret) do not actually get generated, because generation of call instruction fails with error 'argument is neither a function nor a function pointer' I thought, that bitcast instruction before should do the trick of transforming return value of @find_llvm_function (which is a wrapper around Python code, which searches in the Module object) to a function pointer. Also, there is some possibility, that I got the guts of @find_llvm_function wrong, however, in that case I'd expect, that code would still be generated, and problems would manifestate only at runtime. Any ideas what am I doing wrong? On 05/04/2014 05:24 PM, Alexander Potapenko wrote:> > Don't you have the same problem with other atoms, e.g. variable names? > This sounds like something that should be implemented in the > language's runtime library. > > On May 4, 2014 7:15 PM, "Alexander Popolitov" <popolit at gmail.com > <mailto:popolit at gmail.com>> wrote: > > Hi everyone! > > I'm trying to implement lisp's funcall function, which roughly > calls a function, name of which > is known only at runtime. > I know that LLVM IR 'call' directive can accept function pointers, > so the question is, > is there a simple way to get a function pointer from a function > name (represented as e.g. i8*)? > > BTW, I'm using llvmpy to generate LLVM IR, so if there is a way to > do this using llvmpy tools, it would also do. > > Yours sincerely, > Alexandr Popolitov > _______________________________________________ > 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 >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140504/f9dfbad0/attachment.html>