Simon Burton <simon at arrowtheory.com> writes:> I'm using llvm CVS, and manage to compile/link OK. Yes I include JIT.h. > The program segfaults when it gets to calling the function pointer. > >>From the Makefile: > > llvmjit: llvmjit.o > g++ llvmjit.o /home//users//simonb//lib/LLVMAsmParser.o /home//users//simonb//lib/LLVMInterpreter.o `llvm-config --ldflags` `llvm-config --libs jit` -lpthread -ldl -o llvmjit > > llvmjit.o: llvmjit.cpp > g++ `llvm-config --cxxflags` -c llvmjit.cpp > > > Complete source (i added a call to verifyModule):[snip] Simon, With a fresh CVS checkout, I've tried your test case on Windows/VC++ and it works ok. Too bad that I have not access to a Linux machine right now. I'll like to see what's wrong with your test case. What you get from running the test case under gdb and inspecting the value of add1 just before the function invocation? There are several possibilities here: either add1 is assigned a NULL pointer, or LLVM was unable to use the JIT and generates bytecode instead of native code, or invalid native code was generated (unlikely). What happens when you execute your function the same way the Fibonacci example does? (See examples/Fibonacci/fibonacci.cpp line 112). -- Oscar
On Thu, 13 Apr 2006 18:07:42 +0200 Oscar Fuentes <oscarfv at telefonica.net> wrote:> > Simon Burton <simon at arrowtheory.com> writes: > > > I'm trying to take assembly and create machine code I can execute. > > How close am I ? > > Your test case is not complete. Besides, which version of llvm are you > using? What are the commands for compiling and linking your test case? > How it bombs? > > Do you #include "llvm/ExecutionEngine/JIT.h" ?Hi Oscar, I'm using llvm CVS, and manage to compile/link OK. Yes I include JIT.h. The program segfaults when it gets to calling the function pointer.>From the Makefile:llvmjit: llvmjit.o g++ llvmjit.o /home//users//simonb//lib/LLVMAsmParser.o /home//users//simonb//lib/LLVMInterpreter.o `llvm-config --ldflags` `llvm-config --libs jit` -lpthread -ldl -o llvmjit llvmjit.o: llvmjit.cpp g++ `llvm-config --cxxflags` -c llvmjit.cpp Complete source (i added a call to verifyModule): #include "llvm/Module.h" #include "llvm/Constants.h" #include "llvm/Type.h" #include "llvm/Instructions.h" #include "llvm/ModuleProvider.h" #include "llvm/ExecutionEngine/ExecutionEngine.h" #include "llvm/ExecutionEngine/JIT.h" #include "llvm/ExecutionEngine/GenericValue.h" #include "llvm/Assembly/Parser.h" #include "llvm/Analysis/Verifier.h" #include <iostream> using namespace llvm; int main() { Module *M = NULL; char *AsmString = "; ModuleID = 'test'\n\ \n\ implementation ; Functions:\n\ \n\ int %add1(int %AnArg) {\n\ EntryBlock:\n\ %addresult = add int 1, %AnArg ; <int> [#uses=1]\n\ ret int %addresult\n\ }\n\ "; M = ParseAssemblyString(AsmString, NULL); std::cout << "verifyModule: " << verifyModule( *M ) << "\n"; ExistingModuleProvider* MP = new ExistingModuleProvider(M); ExecutionEngine* EE = ExecutionEngine::create(MP, false); std::cout << "We just constructed this LLVM module:\n\n" << *M; Function *F = M->getNamedFunction("add1"); assert(F!=NULL); int (*add1)(int); add1 = (int (*)(int))EE->getPointerToFunction(F); std::cout << "Got:" << add1(55) << "\n"; return 0; }
On Fri, 14 Apr 2006 03:25:39 +0200 Oscar Fuentes <oscarfv at telefonica.net> wrote: ...> Simon, > > With a fresh CVS checkout, I've tried your test case on Windows/VC++ > and it works ok. Too bad that I have not access to a Linux machine > right now. I'll like to see what's wrong with your test case. > > What you get from running the test case under gdb and inspecting the > value of add1 just before the function invocation? > > There are several possibilities here: either add1 is assigned a NULL > pointer, or LLVM was unable to use the JIT and generates bytecode > instead of native code, or invalid native code was generated > (unlikely).Well, it's not NULL: (gdb) print add1 $1 = (int (*)(int)) 0x83e43b8 (gdb) print ((char*)add1) $2 = 0x83e43b8 "h\2245\b\002"> > What happens when you execute your function the same way the Fibonacci > example does? (See examples/Fibonacci/fibonacci.cpp line 112).That works OK. Simon. -- Simon Burton, B.Sc. Licensed PO Box 8066 ANU Canberra 2601 Australia Ph. 61 02 6249 6940 http://arrowtheory.com