Advertisement
Guest User

Untitled

a guest
May 28th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <string>
  4.  
  5. #include "llvm/ExecutionEngine/JIT.h"
  6. #include "llvm/IR/IRBuilder.h"
  7. #include "llvm/IR/Module.h"
  8. #include "llvm/PassManager.h"
  9. #include "llvm/Support/TargetSelect.h"
  10. #include "llvm/Analysis/Verifier.h"
  11.  
  12. using namespace llvm;
  13.  
  14. int main(int argc, char * argv[]) {
  15. InitializeNativeTarget();
  16.  
  17. /* LLVM Global Context */
  18. LLVMContext & ctx = getGlobalContext();
  19.  
  20. /* Module */
  21. Module * module = new Module("MyModule", ctx);
  22.  
  23. /* IRBuilder */
  24. IRBuilder<> builder(ctx);
  25.  
  26. /* Create execution engine */
  27. std::string err_str;
  28. CodeGenOpt::Level opt = CodeGenOpt::Default;
  29. ExecutionEngine * engine = ExecutionEngine::createJIT(module, &err_str,
  30. 0, opt);
  31. if (!engine) {
  32. std::cerr << "Could not create ExecutionEngine:"
  33. << err_str << std::endl;
  34. return 1;
  35. }
  36.  
  37. /* LLVM Type of int64_t* */
  38. PointerType * int64_ptr_ty = Type::getInt64PtrTy(ctx, false);
  39.  
  40. /* LLVM Type of int64_t */
  41. IntegerType * int64_ty = Type::getInt64Ty(ctx);
  42.  
  43. /* Create prototype of result function - int64_t() */
  44. std::vector<Type *> arguments;
  45. FunctionType * proto = FunctionType::get(int64_ty, arguments, false);
  46.  
  47. /* Create function body */
  48. Function * fce = Function::Create(proto, Function::ExternalLinkage,
  49. "MyFunction", module);
  50. BasicBlock * bb = BasicBlock::Create(ctx, "entry", fce);
  51.  
  52. /* Point builder to add instructions to function body */
  53. builder.SetInsertPoint(bb);
  54.  
  55. /* Actual value to return */
  56. int64_t value = 765;
  57.  
  58. /* Address of value */
  59. Value * addr = ConstantInt::get(int64_ty, APInt(64, (uint64_t)&value));
  60.  
  61. /* LLVM Pointer to int64_t value */
  62. Value * value_ptr = builder.CreateIntToPtr(addr,
  63. int64_ptr_ty,
  64. "int_to_ptr_cast");
  65.  
  66. /* LLVM Value */
  67. Value * v = builder.CreateLoad(value_ptr, "load_value");
  68.  
  69. /* Return value */
  70. builder.CreateRet(v);
  71.  
  72. /* Function pass manager */
  73. FunctionPassManager pass_manager(module);
  74.  
  75. /* Add data layout */
  76. pass_manager.add(new DataLayout(*engine->getDataLayout()));
  77. pass_manager.doInitialization();
  78.  
  79. /* Do optimizations */
  80. pass_manager.run(*fce);
  81.  
  82. /* JIT it */
  83. void * fce_ptr = engine->getPointerToFunction(fce);
  84. int64_t (*fce_typed_ptr)() = (int64_t(*)())fce_ptr;
  85.  
  86. /* Excute JITed function */
  87. std::cout << "Result: " << (*fce_typed_ptr)()
  88. << std::endl << std::endl;
  89.  
  90. /* Dump LLVM IR */
  91. std::cout << "LLVM IR dump: " << std::endl;
  92. module->dump();
  93.  
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement