Guest User

LLVM example

a guest
Aug 25th, 2011
76
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. set -x # print commands
  2. set -e # abort on error
  3.  
  4. WORKS=works.bc
  5. WORKS2=works2.bc
  6. CRASHES=crashes.bc
  7. PROGRAM=program
  8.  
  9. clang++ $(llvm-config --cxxflags) -c -x c++ - -emit-llvm -o $WORKS <<EOF
  10.  
  11.   #include <iostream>
  12.  
  13.   class A {
  14.     public:
  15.        A(void) {
  16.           std::cout << "AAA\n";
  17.        }
  18.   };
  19.  
  20.   extern "C" {
  21.     void makeit() { A(); }
  22.   }
  23.  
  24. EOF
  25.  
  26. clang++ -O2 $(llvm-config --cxxflags) -c -x c++ - -emit-llvm -o $WORKS2 <<EOF
  27.  
  28.   #include <iostream>
  29.  
  30.   class B {
  31.     public:
  32.        B(void);
  33.   };
  34.  
  35.   B::B(void) {
  36.           std::cout << "BBB\n";
  37.   }
  38.  
  39.   extern "C" {
  40.     void makeit() { B(); }
  41.   }
  42.  
  43. EOF
  44.  
  45. clang++ $(llvm-config --cxxflags) -c -x c++ - -emit-llvm -o $CRASHES <<EOF
  46.  
  47.   #include <iostream>
  48.  
  49.   class C {
  50.     public:
  51.        C(void);
  52.   };
  53.  
  54.   C::C(void) {
  55.           std::cout << "CCC\n";
  56.   }
  57.  
  58.   extern "C" {
  59.     void makeit() { C(); }
  60.   }
  61.  
  62. EOF
  63.  
  64.  
  65. clang++ $(llvm-config --cxxflags) -x c++ - $(llvm-config --ldflags) $(llvm-config --libs jit native BitReader) -o $PROGRAM <<EOF
  66.  
  67.  #include <iostream>
  68.  
  69.  #include <llvm/LLVMContext.h>
  70.  #include <llvm/Target/TargetSelect.h>
  71.  #include <llvm/Bitcode/ReaderWriter.h>
  72.  #include <llvm/ExecutionEngine/ExecutionEngine.h>
  73.  #include <llvm/Support/MemoryBuffer.h>
  74.  #include <llvm/ExecutionEngine/JIT.h>
  75.  #include <llvm/ADT/OwningPtr.h>
  76.  #include "llvm/Support/system_error.h"
  77.  
  78.  using namespace std;
  79.  using namespace llvm;
  80.  
  81.  int main(int argc, char** argv)
  82.  {
  83.    if(argc<=1) {
  84.      return 1;
  85.    }
  86.  
  87.    const char* filename = argv[1];
  88.  
  89.    InitializeNativeTarget();
  90.    llvm_start_multithreaded();
  91.  
  92.    LLVMContext context;
  93.    OwningPtr<MemoryBuffer> buffer;
  94.  
  95.    if (error_code ec = MemoryBuffer::getFile(filename, buffer)) {
  96.      return 1;
  97.    } else {
  98.      std::string ParseErrorMessage;
  99.      Module* m = ParseBitcodeFile(buffer.get(), context, &ParseErrorMessage);
  100.      if(m) {
  101.        ExecutionEngine *ee = ExecutionEngine::create(m);
  102.        Function* func = ee->FindFunctionNamed("makeit");
  103.        if(func) {
  104.      typedef void (*PFN)();
  105.      cout << "starting to JIT makeit" << endl;
  106.      PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
  107.      pfn();
  108.        } else {
  109.      return 1;
  110.        }
  111.        delete ee;
  112.      } else {
  113.        return 1;
  114.      }
  115.    }
  116.    return 0;
  117.  }
  118.  
  119. EOF
  120.  
  121. ./$PROGRAM  $WORKS
  122. ./$PROGRAM  $WORKS2
  123. ./$PROGRAM  $CRASHES
  124.  
  125.  
  126. cat << EOF
  127. $ clang -v
  128. clang version 3.0 (http://llvm.org/git/clang.git ec5bc81fd55bfcc26fc4bde6d5e33113d94c2209)
  129. Target: i386-pc-linux-gnu
  130. Thread model: posix
  131. $ llvmc --version
  132. Low Level Virtual Machine (http://llvm.org/):
  133.   llvm version 3.0svn
  134.   DEBUG build with assertions.
  135.   Built Aug 23 2011 (12:10:27).
  136.   Host: i386-pc-linux-gnu
  137.   Host CPU: penryn
  138. EOF
RAW Paste Data