Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void CodeGenContext::generateCode(NBlock& root)
- {
- std::cout << "Generating code...\n";
- /* Create the top level interpreter function to call as entry */
- vector<llvm::Type*> argTypes;
- llvm::FunctionType *ftype = llvm::FunctionType::get(llvm::Type::getVoidTy(llvm::getGlobalContext()), makeArrayRef(argTypes), false);
- mainFunction = llvm::Function::Create(ftype, llvm::GlobalValue::InternalLinkage, "main", module);
- llvm::BasicBlock *bblock = llvm::BasicBlock::Create(llvm::getGlobalContext(), "entry", mainFunction, 0);
- /* Push a new variable/block context */
- pushBlock(bblock);
- root.CodeGen(*this); /* emit bytecode for the toplevel block */
- llvm::ReturnInst::Create(llvm::getGlobalContext(), bblock);
- popBlock();
- /* Print the bytecode in a human-readable format
- to see if our program compiled properly
- */
- std::cout << "Code is generated.\n";
- llvm::PassManager pm;
- std::string type_str;
- llvm::raw_string_ostream ostr(type_str);
- pm.add(llvm::createPrintModulePass(&ostr));
- pm.run(*module);
- std::string temp(ostr.str());
- m_llvmIR = temp;
- }
- void CodeGenContext::generateSparcsCode(NBlock& root)
- {
- std::string err;
- LLVMInitializeSparcTargetInfo();
- LLVMInitializeSparcTarget();
- LLVMInitializeSparcTargetMC();
- llvm::Triple SparcTriple;
- const llvm::Target* pSparcsTarget = llvm::TargetRegistry::lookupTarget("sparc", err);
- if (!pSparcsTarget)
- {
- std::cout << "Can't find target" << std::endl;
- system("pause");
- exit(-1);
- }
- llvm::Triple::ArchType Arch = llvm::Triple::getArchTypeForLLVMName("sparc");
- if (Arch == llvm::Triple::UnknownArch)
- {
- std::cout << "Unknown Architecture" << std::endl;
- system("pause");
- exit(-1);
- }
- SparcTriple.setArch(Arch);
- std::string FS;
- std::auto_ptr<llvm::TargetMachine> tm(pSparcsTarget->createTargetMachine(
- SparcTriple.getTriple(), "v9", FS,
- llvm::Reloc::Default,
- llvm::CodeModel::Default
- ));
- llvm::TargetMachine &myTargetMachine = *tm.get();
- llvm::PassManager PM;
- PM.add(new llvm::TargetData(*myTargetMachine.getTargetData()));
- // PM.add(llvm::createPromoteMemoryToRegistryPass());
- myTargetMachine.setAsmVerbosityDefault(true);
- // std::string CodeString;
- llvm::raw_string_ostream oStream(m_llvmIR);
- llvm::formatted_raw_ostream out(oStream);
- PM.run(*module);
- out.flush();
- std::string &data = oStream.str();
- std::cout << data << std::endl;
- }
- /* Executes the AST by running the main function */
- llvm::GenericValue CodeGenContext::runCode() {
- std::cout << "Running code...\n";
- llvm::ExecutionEngine *ee = llvm::EngineBuilder(module).create();
- vector<llvm::GenericValue> noargs;
- llvm::GenericValue v = ee->runFunction(mainFunction, noargs);
- std::cout << "Code was run.\n";
- return v;
- }
- llvm::Value* NBlock::CodeGen(CodeGenContext& context)
- {
- StatementList::const_iterator it;
- llvm::Value *last = NULL;
- for (it = m_statements.begin(); it != m_statements.end(); it++)
- {
- std::cout << "Generating code for: " << typeid(**it).name() << endl;
- last = (**it).CodeGen(context);
- }
- std::cout << "Creating block" << endl;
- return last;
- }
- void pushBlock(llvm::BasicBlock *block) { blocks.push(new CodeGenBlock()); blocks.top()->block = block; }
- void popBlock() { CodeGenBlock *top = blocks.top(); blocks.pop(); delete top; }
Advertisement
Add Comment
Please, Sign In to add comment