[LLVMdev] Re: standalone llvm

Simon Burton simon at arrowtheory.com
Fri Apr 14 01:54:18 PDT 2006


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;
}




More information about the llvm-dev mailing list