#include "llvm/LLVMContext.h"#include "llvm/Module.h"#include "llvm/Consta - Pastebin.com (original) (raw)

Guest User

Untitled

a guest

Jan 1st, 2011

999

0

Never

Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

  1. #include "llvm/LLVMContext.h"
  2. #include "llvm/Module.h"
  3. #include "llvm/Constants.h"
  4. #include "llvm/Function.h"
  5. #include "llvm/BasicBlock.h"
  6. #include "llvm/ExecutionEngine/ExecutionEngine.h"
  7. #include "llvm/ExecutionEngine/GenericValue.h"
  8. #include "llvm/ExecutionEngine/JIT.h"
  9. #include "llvm/Target/TargetSelect.h"
  10. #include "llvm/Support/IRBuilder.h"
  11. #include
  12. #include
  13. #include
  14. int main() {
  15. llvm::LLVMContext & context = llvm::getGlobalContext();
  16. llvm::Module *module = new llvm::Module("asdf", context);
  17. // Create the function we're going to write code into
  18. llvm::IRBuilder<> builder(context);
  19. llvm::FunctionType *funcType = llvm::FunctionType::get(builder.getVoidTy(), false);
  20. llvm::Function *mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", module);
  21. llvm::BasicBlock *entry = llvm::BasicBlock::Create(context, "entry", mainFunc);
  22. builder.SetInsertPoint(entry);
  23. // Get a function declaration for printf
  24. std::vector<const llvm::Type *> printfArgs;
  25. printfArgs.push_back(builder.getInt8Ty()->getPointerTo());
  26. llvm::FunctionType *printfType = llvm::FunctionType::get(builder.getInt32Ty(), printfArgs, true);
  27. llvm::Constant *printfFunc = module->getOrInsertFunction("printf", printfType);
  28. // Add our "hello world" string to the module
  29. llvm::Value *helloWorld = builder.CreateGlobalStringPtr("hello world!\n");
  30. // Now generate a call to printf and return out of the function
  31. builder.CreateCall(printfFunc, helloWorld);
  32. builder.CreateRetVoid();
  33. // Initialize the native target, which is required before we can JIT anything
  34. llvm::InitializeNativeTarget();
  35. // Create the execution engine, this takes ownership of the module
  36. std::string err_str;
  37. llvm::ExecutionEngine *engine = llvm::EngineBuilder(module).setErrorStr(&err_str).create();
  38. if (!engine) {
  39. fprintf(stderr, "err: %s\n", err_str.c_str());
  40. exit(1);
  41. }
  42. std::vectorllvm::GenericValue\ void_arg;
  43. engine->runFunction(mainFunc, void_arg);
  44. // Delete the engine and not the module
  45. delete engine;
  46. return 0;
  47. }