Guest User

llvm

a guest
May 17th, 2012
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. void CodeGenContext::generateCode(NBlock& root)
  2. {
  3.     std::cout << "Generating code...\n";
  4.  
  5.     /* Create the top level interpreter function to call as entry */
  6.     vector<llvm::Type*> argTypes;
  7.     llvm::FunctionType *ftype = llvm::FunctionType::get(llvm::Type::getVoidTy(llvm::getGlobalContext()), makeArrayRef(argTypes), false);
  8.     mainFunction = llvm::Function::Create(ftype, llvm::GlobalValue::InternalLinkage, "main", module);
  9.     llvm::BasicBlock *bblock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", mainFunction, 0);
  10.  
  11.     /* Push a new variable/block context */
  12.     pushBlock(bblock);
  13.     root.CodeGen(*this); /* emit bytecode for the toplevel block */
  14.     llvm::ReturnInst::Create(llvm::getGlobalContext(), bblock);
  15.     popBlock();
  16.  
  17.     /* Print the bytecode in a human-readable format
  18.        to see if our program compiled properly
  19.      */
  20.     std::cout << "Code is generated.\n";
  21.     llvm::PassManager pm;
  22.     std::string type_str;
  23.     llvm::raw_string_ostream ostr(type_str);
  24.     pm.add(llvm::createPrintModulePass(&ostr));
  25.     pm.run(*module);
  26.  
  27.     std::string temp(ostr.str());
  28.     m_llvmIR = temp;
  29. }
  30.  
  31.  
  32. void CodeGenContext::generateSparcsCode(NBlock& root)
  33. {
  34.     std::string err;
  35.     LLVMInitializeSparcTargetInfo();
  36.     LLVMInitializeSparcTarget();
  37.     LLVMInitializeSparcTargetMC();
  38.  
  39.     llvm::Triple SparcTriple;
  40.  
  41.     const llvm::Target* pSparcsTarget = llvm::TargetRegistry::lookupTarget("sparc", err);
  42.  
  43.     if (!pSparcsTarget)
  44.     {
  45.         std::cout << "Can't find target" << std::endl;
  46.         system("pause");
  47.         exit(-1);
  48.     }
  49.  
  50.     llvm::Triple::ArchType Arch = llvm::Triple::getArchTypeForLLVMName("sparc");
  51.  
  52.     if (Arch == llvm::Triple::UnknownArch)
  53.     {
  54.         std::cout << "Unknown Architecture" << std::endl;
  55.         system("pause");
  56.         exit(-1);
  57.     }
  58.  
  59.     SparcTriple.setArch(Arch);
  60.  
  61.     std::string FS;
  62.  
  63.     std::auto_ptr<llvm::TargetMachine> tm(pSparcsTarget->createTargetMachine(
  64.                                           SparcTriple.getTriple(), "v9", FS,
  65.                                           llvm::Reloc::Default,
  66.                                           llvm::CodeModel::Default
  67.                                           ));
  68.  
  69.     llvm::TargetMachine &myTargetMachine = *tm.get();
  70.     llvm::PassManager PM;
  71.  
  72.     PM.add(new llvm::TargetData(*myTargetMachine.getTargetData()));
  73.     // PM.add(llvm::createPromoteMemoryToRegistryPass());
  74.     myTargetMachine.setAsmVerbosityDefault(true);
  75.  
  76.     // std::string CodeString;
  77.     llvm::raw_string_ostream oStream(m_llvmIR);
  78.     llvm::formatted_raw_ostream out(oStream);
  79.    
  80.     PM.run(*module);
  81.  
  82.     out.flush();
  83.     std::string &data = oStream.str();
  84.  
  85.     std::cout << data << std::endl;
  86. }
  87.  
  88. /* Executes the AST by running the main function */
  89. llvm::GenericValue CodeGenContext::runCode() {
  90.     std::cout << "Running code...\n";
  91.     llvm::ExecutionEngine *ee = llvm::EngineBuilder(module).create();
  92.     vector<llvm::GenericValue> noargs;
  93.     llvm::GenericValue v = ee->runFunction(mainFunction, noargs);
  94.     std::cout << "Code was run.\n";
  95.     return v;
  96. }
  97.  
  98. llvm::Value* NBlock::CodeGen(CodeGenContext& context)
  99. {
  100.     StatementList::const_iterator it;
  101.     llvm::Value *last = NULL;
  102.     for (it = m_statements.begin(); it != m_statements.end(); it++)
  103.     {
  104.         std::cout << "Generating code for: " << typeid(**it).name() << endl;
  105.         last = (**it).CodeGen(context);
  106.     }
  107.    
  108.     std::cout << "Creating block" << endl;
  109.     return last;
  110. }
  111.     void pushBlock(llvm::BasicBlock *block) { blocks.push(new CodeGenBlock()); blocks.top()->block = block; }
  112.     void popBlock() { CodeGenBlock *top = blocks.top(); blocks.pop(); delete top; }
Advertisement
Add Comment
Please, Sign In to add comment