/* Compile the AST into a module */ void CodeGenContext::generateCode(Block& root) { std::cout << "Generating code...\n"; std::cout << "Nodes: " << root.statements.size() << "\n"; /* Create the top level interpreter function to call as entry */ vector<const Type*> argTypes; FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), argTypes, false); mainFunction = Function::Create(ftype, GlobalValue::InternalLinkage, "main", module); BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0); /* Push a new variable/block context */ pushBlock(bblock); root.codeGen(*this); /* emit bytecode for the toplevel block */ ReturnInst::Create(getGlobalContext(), bblock); popBlock(); /* Print the bytecode in a human-readable format to see if our program compiled properly */ std::cout << "Code is generated.\n"; PassManager pm; pm.add(createPrintModulePass(&outs())); pm.run(*module); }
void run(){ Lexer* lexer; Perser* perser; if(debug){ std::cout<<"Debug option\n"; } lexer = new Lexer(debug); lexer->load(input_file); if(debug){ lexer->put_result(); } perser = new Perser(lexer->getTokens(), debug); if(!perser->perse()){ std::cout<<"Perse Error!!\n"; RELEASE(lexer); RELEASE(perser); return; } TranslationUnitAST* ast = perser->getAST(); CodeGen *codeGen = new CodeGen(debug); if(!codeGen->codeGen( ast, input_file)){ std::cout<<"CodeGen Error!!\n"; RELEASE(lexer); RELEASE(perser); return; } llvm::Module &module = codeGen->getModule(); if(module.empty()){ std::cout<<" Module is empty!!\n"; RELEASE(lexer); RELEASE(perser); return; } llvm::PassManager pm; pm.add(llvm::createPromoteMemoryToRegisterPass()); std::error_code error; llvm::StringRef out_filename("do.out"); llvm::raw_fd_ostream raw_stream( out_filename, error, llvm::sys::fs::OpenFlags::F_None ); pm.add(createPrintModulePass( raw_stream)); pm.run(module); raw_stream.close(); std::cout<<"Complate!!!!\n"; RELEASE(lexer); RELEASE(perser); }
/* Compile the AST into a module */ void CodeGenContext::generateCode(ast::Program& root) { std::cout << "Generating code...\n"; /* Create the top level interpreter function to call as entry */ std::vector<Type*> argTypes; FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(argTypes), false); // change GlobalValue::InternalLinkage into ExternalLinkage mainFunction = Function::Create(ftype, GlobalValue::ExternalLinkage, "main", module); BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0); CodeGenContext::printf = createPrintf(*this); /* Push a new variable/block context */ pushBlock(bblock); currentFunction = mainFunction; for (auto label:labels){ labelBlock[label]=BasicBlock::Create(getGlobalContext(), "label", mainFunction, 0); } root.CodeGen(*this); /* emit bytecode for the toplevel block */ ReturnInst::Create(getGlobalContext(), currentBlock()); popBlock(); // popBlock(); /* Print the bytecode in a human-readable format to see if our program compiled properly */ std::cout << "Code is generated.\n"; PassManager pm; pm.add(createPrintModulePass(outs())); //pm.run(*module); // write IR to stderr std::cout<<"code is gen~~~\n"; module->dump(); std::cout<<"code is gen~!~\n"; }