Guest User

test.cpp

a guest
Nov 2nd, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <llvm/Pass.h>
  2. #include <llvm/PassManager.h>
  3. #include <llvm/IR/BasicBlock.h>
  4. #include <llvm/IR/Constants.h>
  5. #include <llvm/IR/DerivedTypes.h>
  6. #include <llvm/IR/Function.h>
  7. #include <llvm/IR/GlobalVariable.h>
  8. #include <llvm/IR/IRPrintingPasses.h>
  9. #include <llvm/IR/Instructions.h>
  10. #include <llvm/IR/LLVMContext.h>
  11. #include <llvm/IR/Module.h>
  12. #include <llvm/IR/Verifier.h>
  13. #include <llvm/Support/FormattedStream.h>
  14. #include <vector>
  15.  
  16. using namespace llvm;
  17.  
  18. Module *makeLLVMModule();
  19.  
  20. int main(int argc, char **argv) {
  21.   Module *Mod = makeLLVMModule();
  22.   PassManager PM;
  23.   PM.add(createVerifierPass());
  24.   PM.add(createPrintModulePass(outs()));
  25.   PM.run(*Mod);
  26.   return 0;
  27. }
  28.  
  29. Module *makeLLVMModule() {
  30.   // Module Construction
  31.   Module *mod = new Module("test.ll", getGlobalContext());
  32.   mod->setDataLayout("e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128");
  33.   mod->setTargetTriple("i386-pc-linux-gnu");
  34.  
  35.   // Type Definitions
  36.   std::vector<Type *> FuncTy_0_args;
  37.   FuncTy_0_args.push_back(IntegerType::get(mod->getContext(), 32));
  38.   FuncTy_0_args.push_back(IntegerType::get(mod->getContext(), 32));
  39.   FunctionType *FuncTy_0 = FunctionType::get(
  40.       /*Result=*/IntegerType::get(mod->getContext(), 32),
  41.       /*Params=*/FuncTy_0_args,
  42.       /*isVarArg=*/false);
  43.  
  44.   // Function Declaration
  45.   Function *func_sum = mod->getFunction("sum");
  46.   if (!func_sum) {
  47.     func_sum = Function::Create(
  48.         /*Type=*/FuncTy_0,
  49.         /*Linkage=*/GlobalValue::ExternalLinkage,
  50.         /*Name=*/"sum", mod);
  51.     func_sum->setCallingConv(CallingConv::C);
  52.   }
  53.  
  54.   // Function Definition
  55.   {
  56.     Function::arg_iterator args = func_sum->arg_begin();
  57.     Value *int32_x = args++;
  58.     int32_x->setName("x");
  59.     Value *int32_y = args++;
  60.     int32_y->setName("y");
  61.  
  62.     BasicBlock *label_entry =
  63.         BasicBlock::Create(mod->getContext(), "entry", func_sum, 0);
  64.  
  65.     // Block entry (label_entry)
  66.     BinaryOperator *int32_add = BinaryOperator::Create(
  67.         Instruction::Add, int32_x, int32_y, "add", label_entry);
  68.     ReturnInst::Create(mod->getContext(), int32_add, label_entry);
  69.   }
  70.  
  71.   return mod;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment