void printTree(std::ostream& os, int depth =0){ os<<string(depth,' ')<<"NBlock: {"<<endl; for(int i=0;i<statements.size();i++){ statements[i]->printTree(os,depth+1); } os<<string(depth,' ')<<"}"<<endl; }
TType* typeChk(Symtable& t,TType* expected = NULL){ t.begScope(); bool err=false; TType* s; for(int i=0;i<statements.size();i++){ #ifdef DEBUG cout << "statement "<< i << " " << statements[i] <<endl; #endif s=statements[i]->typeChk(t,expected); if(s==NULL){ cerr<<"Error in the statement "<<i<<" of block."<<endl; err=true; } } t.endScope(); if(err) { #ifdef DEBUG cerr << "error in block"<<endl; #endif return NULL; } #ifdef DEBUG cout << "void " << t.lookupType("void") << endl; #endif return t.lookupType("void"); }
// Return a list of locations defined by library calls void PPCSignature::getLibraryDefines(StatementList &defs) { if (defs.size() > 0) { return; // Do only once } for (int r = REG_PPC_G3; r <= REG_PPC_G12; ++r) { defs.append( new ImplicitAssign(Location::regOf(r))); // Registers 3-12 are volatile (caller save) } }
int main(int argc, char **argv) { vector<Token> tokens = lexerFile(argv[1]); Parser parser(tokens, &std::cout); StatementList * list = parser.file(); Printer p(std::cout); cout << "Statements " << list->size() << endl; for( Statement *s : *list ) s->print(p); return 0; }
CodeNodePtr findInterpolatable(StatementPtr stmt) { if (CodeNodePtr e = stmt->getExpression()) { if (CodeNodePtr f = findInterpolatable(e)) { return f; } } StatementList children = stmt->getChildren(); for (size_t i = 0; i < children.size(); ++i) { if (CodeNodePtr f = findInterpolatable(children[i])) { return f; } } return CodeNodePtr(); }
void SPARCSignature::getLibraryDefines(StatementList &defs) { if (defs.size() > 0) { return; // Do only once } // o0-o7 (r8-r15) modified defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O0))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O1))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O2))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O3))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O4))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O5))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O6))); defs.append(new ImplicitAssign(Location::regOf(REG_SPARC_O7))); }
static void doCountReferences( StatementPtr statement, ReferenceMap& refs, ReferencePath path ) { assert(statement); path.push_back(statement); if (CodeNodePtr e = statement->getExpression()) { countReferences(e, refs, path); } StatementList children = statement->getChildren(); for (size_t i = 0; i < children.size(); ++i) { doCountReferences(children[i], refs, path); } }
void replace(StatementPtr st, CodeNodePtr node, CodeNodePtr with) { assert(st); assert(node); assert(with); if (CodeNodePtr e = st->getExpression()) { if (e == node) { st->setExpression(with); } else { replace(e, node, with); } } StatementList children = st->getChildren(); for (size_t i = 0; i < children.size(); ++i) { replace(children[i], node, with); } }
IfCodeNodePtr findBranch(StatementPtr stmt, StatementPtr& ref) { assert(stmt); if (CodeNodePtr e = stmt->getExpression()) { if (IfCodeNodePtr f = findBranch(e)) { ref = stmt; return f; } } StatementList children = stmt->getChildren(); for (size_t i = 0; i < children.size(); ++i) { if (IfCodeNodePtr f = findBranch(children[i], ref)) { return f; } } return IfCodeNodePtr(); }
CodeNodePtr findMultiplyReferenced( StatementPtr stmt, const ReferenceMap& refs ) { assert(stmt); if (CodeNodePtr e = stmt->getExpression()) { if (CodeNodePtr f = findMultiplyReferenced(e, refs)) { return f; } } StatementList children = stmt->getChildren(); for (size_t i = 0; i < children.size(); ++i) { if (CodeNodePtr f = findMultiplyReferenced(children[i], refs)) { return f; } } return CodeNodePtr(); }
int main(int argc, char **argv) { InitializeNativeTarget(); InitializeNativeTargetAsmPrinter(); InitializeNativeTargetAsmParser(); executor = mtexecutor_new(); vector<Token> tokens = lexerFile(argv[1]); Parser parser(tokens, &std::cout); StatementList * list = parser.file(); Printer p(std::cout); cout << "Statements " << list->size() << endl; for( Statement *s : *list ) s->print(p); std::cerr.flush(); std::cout.flush(); usleep(10000); llvm::LLVMContext &llvmContext = llvm::getGlobalContext(); SMDiagnostic error; //Module *m = parseIRFile("decl.ll", error, llvmContext); //std::unique_ptr<llvm::Module> Owner = llvm::make_unique<llvm::Module>("my cool jit", llvmContext); //std::unique_ptr<llvm::Module> Owner = std::unique_ptr<llvm::Module>(m); std::unique_ptr<llvm::Module> Owner = parseIRFile("decl.ll", error, llvmContext); Module *TheModule = Owner.get(); /*TheModule->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION);*/ /*if (Triple(sys::getProcessTriple()).isOSDarwin()) TheModule->addModuleFlag(llvm::Module::Warning, "Dwarf Version", 2);*/ ContextStorage s; Context ctx(&s); std::string ErrStr; TheExecutionEngine = EngineBuilder(std::move(Owner)) .setErrorStr(&ErrStr) .setMCJITMemoryManager(llvm::make_unique<SectionMemoryManager>()) .create(); if (!TheExecutionEngine) { fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str()); exit(1); } TheExecutionEngine->addGlobalMapping(TheModule->getFunction("system_init"), (void*)system_init); //FunctionPassManager OurFPM(TheModule); // Set up the optimizer pipeline. Start with registering info about how the // target lays out data structures. TheModule->setDataLayout(*TheExecutionEngine->getDataLayout()); #if 0 OurFPM.add(new DataLayoutPass()); // Provide basic AliasAnalysis support for GVN. OurFPM.add(createBasicAliasAnalysisPass()); // Do simple "peephole" optimizations and bit-twiddling optzns. OurFPM.add(createInstructionCombiningPass()); // Reassociate expressions. OurFPM.add(createReassociatePass()); // Eliminate Common SubExpressions. OurFPM.add(createGVNPass()); // Simplify the control flow graph (deleting unreachable blocks, etc). OurFPM.add(createCFGSimplificationPass()); OurFPM.doInitialization(); #endif // Set the global so the code gen can use this. //TheFPM = &OurFPM; s.module = TheModule; register_printf(TheModule); register_malloc(TheModule); stdio_register(&ctx); for( Statement *s : *list ) s->codegen(&ctx); //s.module->dump(); TheExecutionEngine->finalizeObject(); std::cerr.flush(); std::cout.flush(); usleep(1000); asyncIoInitialize(); // JIT the function, returning a function pointer. Function *LF = TheModule->getFunction("main"); void *FPtr = TheExecutionEngine->getPointerToFunction(LF); // Cast it to the right type (takes no arguments, returns a double) so we // can call it as a native function. void (*FP)() = (void (*)())(intptr_t)FPtr; FP(); executor->run(executor); return 0; }