Statement* multi_way_branch_statement_walker::dismantle_multi_way_branch_statement(MultiWayBranchStatement *the_case){ StatementList *replacement = create_statement_list(the_case->get_suif_env()); Expression *operand = the_case->get_decision_operand (); remove_suif_object(operand); DataType *type = operand->get_result_type(); CodeLabelSymbol *default_lab = the_case->get_default_target(); the_case->set_default_target(0); Iter<MultiWayBranchStatement::case_pair > iter = the_case->get_case_iterator(); while (iter.is_valid()) { MultiWayBranchStatement::case_pair pair = iter.current(); IInteger value = pair.first; CodeLabelSymbol *lab = pair.second; IntConstant *exp = create_int_constant(the_case->get_suif_env(),type, value); // Expression *exp = create_load_constant_expression(get_env(),type,iconst); TypeBuilder *type_builder = (TypeBuilder *) the_case->get_suif_env()->get_object_factory(TypeBuilder::get_class_name()); Expression *compare = create_binary_expression(the_case->get_suif_env(),type_builder->get_boolean_type(), k_is_equal_to, deep_suif_clone(operand), exp); replacement->append_statement(create_branch_statement(the_case->get_suif_env(),compare,lab)); iter.next(); } delete operand; replacement->append_statement(create_jump_statement(the_case->get_suif_env(),default_lab)); the_case->get_parent()->replace(the_case,replacement); return replacement; }
void GenTree::gen_block( const StatementList &list, int depth, const char *prefix ) { if( list.empty() ) return; if ( prefix != 0 ) { String line; line.writeNumber( (int64) list.front()->line() ); int pos = 0; while( pos + line.length() < 5 ) { m_out->writeString( " " ); pos++; } m_out->writeString( line + " : " ); for (int i = 0; i < depth; i++ ) m_out->writeString( " " ); m_out->writeString( prefix ); m_out->writeString( "\n" ); } const Statement *stmt = list.front(); while( stmt != 0 ) { generate( stmt, 0, false, depth + 1 ); stmt = static_cast<const Statement *>(stmt->next()); } }
void CodeGenVisitor::JIT(Expression* e) { StatementList* sl = new StatementList(); sl->addStatement(new ReturnStatement(e)); FunctionDefinition* fd = new FunctionDefinition(Type::INT, "", new ParameterList(), sl); value_ = 0; fd->accept(this); if (!value_) { delete fd; throw "error evaluating expression"; } llvm::Function* f = dynamic_cast<llvm::Function*>(value_); void* fPtr = ee_->getPointerToFunction(f); // some casting ... because we like magic int (*fP)() = (int (*)())(intptr_t)fPtr; std::cout << "Evaluated to: " << fP() << std::endl; // throw it away f->eraseFromParent(); }
void TransformSystemsToModules::ReplaceUses(VariableSymbol* original, VariableSymbol* replacement) { assert(procDef != NULL) ; StatementList* bodyList = dynamic_cast<StatementList*>(procDef->get_body()) ; assert(bodyList != NULL) ; for (int i = 0 ; i < bodyList->get_statement_count() ; ++i) { list<LoadVariableExpression*>* allLoads = collect_objects<LoadVariableExpression>(bodyList->get_statement(i)) ; list<LoadVariableExpression*>::iterator loadIter = allLoads->begin() ; while (loadIter != allLoads->end()) { if ((*loadIter)->get_source() == original) { (*loadIter)->set_source(replacement) ; } ++loadIter ; } delete allLoads ; if (IsDefinition(bodyList->get_statement(i), original)) { // We no longer have to replace the value return ; } } }
bool IfConversionPass2::VerifyIf(IfStatement* toConvert) { Statement* thenPart = toConvert->get_then_part() ; Statement* elsePart = toConvert->get_else_part() ; if (thenPart == NULL || elsePart == NULL) { return false ; } StatementList* thenList = dynamic_cast<StatementList*>(thenPart) ; StatementList* elseList = dynamic_cast<StatementList*>(elsePart) ; if (thenList != NULL && thenList->get_statement_count() != 1) { return false ; } if (elseList != NULL && elseList->get_statement_count() != 1) { return false ; } thenPart = Denormalize(thenPart) ; elsePart = Denormalize(elsePart) ; return CorrectTypes(thenPart, elsePart) ; }
void StatementList::makeIsect(StatementList &a, LocationSet &b) { if (this == &a) { // *this = *this isect b for (auto it = a.begin(); it != a.end();) { assert((*it)->isAssignment()); Assignment *as = static_cast<Assignment *>(*it); if (!b.contains(as->getLeft())) { it = m_list.erase(it); } else { it++; } } } else { // normal assignment clear(); for (Statement *stmt : a) { assert(stmt->isAssignment()); Assignment *as = static_cast<Assignment *>(stmt); if (b.contains(as->getLeft())) { append(as); } } } }
void SemanticAnalysis::visitBlockStatement(BlockStatement *node) { StatementList *stmts = node->statements(); for (size_t i = 0; i < stmts->length(); i++) { Statement *stmt = stmts->at(i); stmt->accept(this); } }
void TransformSystemsToModules::ReplaceDefinition(VariableSymbol* original, VariableSymbol* replacement) { assert(procDef != NULL) ; std::cout << "Replacing a definition: " << original << " " << replacement << std::endl ; StatementList* bodyList = dynamic_cast<StatementList*>(procDef->get_body()) ; assert(bodyList != NULL) ; // From the back, find the last definition for (int i = bodyList->get_statement_count() - 1 ; i > 0 ; --i) { Statement* currentStatement = bodyList->get_statement(i) ; if (IsDefinition(currentStatement, original)) { // This is either going to be a store variable statement or // a symbol address expression (if the definition was in a call) list<StoreVariableStatement*>* allStores = collect_objects<StoreVariableStatement>(currentStatement) ; list<StoreVariableStatement*>::iterator storeIter = allStores->begin() ; while (storeIter != allStores->end()) { if ((*storeIter)->get_destination() == original) { (*storeIter)->set_destination(replacement) ; } ++storeIter ; } delete allStores ; list<SymbolAddressExpression*>* allSymAddr = collect_objects<SymbolAddressExpression>(currentStatement) ; list<SymbolAddressExpression*>::iterator symAddrIter = allSymAddr->begin(); while (symAddrIter != allSymAddr->end()) { if ((*symAddrIter)->get_addressed_symbol() == original) { (*symAddrIter)->set_addressed_symbol(replacement) ; } ++symAddrIter ; } delete allSymAddr ; return ; } } }
// 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) } }
void removeRedundantBlocks(BlockPtr b) { StatementList& children = b->getChildren(); for (size_t i = 0; i < children.size(); ++i) { if (REN_DYNAMIC_CAST_PTR(ib, Block, children[i])) { removeRedundantBlocks(ib); StatementList sl = ib->getChildren(); children.erase(children.begin() + i); children.insert(children.begin() + i, sl.begin(), sl.end()); --i; } } }
Walker::ApplyStatus multi_way_branch_statement_compactor::operator () (SuifObject *x) { MultiWayBranchStatement *the_case = to<MultiWayBranchStatement>(x); // is the table already compact? if (is_compact(the_case)) return Walker::Continue; SymbolTable *scope = find_scope(x); if (!scope) return Walker::Continue; CodeLabelSymbol *default_lab = the_case->get_default_target(); // very special case - the case list is empty, so just jump to the default label if (the_case->get_case_count() == 0) { Statement *replacement = create_jump_statement(get_env(),default_lab); the_case->get_parent()->replace(the_case,replacement); set_address(replacement); return Walker::Replaced; } StatementList *replacement = create_statement_list(get_env()); Expression *operand = the_case->get_decision_operand (); remove_suif_object(operand); DataType *type = operand->get_result_type(); VariableSymbol *decision = create_variable_symbol(get_env(),get_type_builder(get_env())->get_qualified_type(type)); scope->add_symbol(decision); replacement->append_statement(create_store_variable_statement(get_env(),decision,operand)); the_case->set_default_target(0); MultiWayGroupList jump_list(get_env(),default_lab,decision);; Iter<MultiWayBranchStatement::case_pair > iter = the_case->get_case_iterator(); while (iter.is_valid()) { MultiWayBranchStatement::case_pair pair = iter.current(); jump_list.add_element(pair.first,pair.second); iter.next(); } // we have built the new structure, now need to generate code for it jump_list.generate_code(replacement); the_case->get_parent()->replace(the_case,replacement); set_address(replacement); return Walker::Replaced; }
Statement* IfConversionPass2::Denormalize(Statement* x) { if (dynamic_cast<StatementList*>(x) != NULL) { StatementList* theList = dynamic_cast<StatementList*>(x) ; assert(theList->get_statement_count() == 1 && "Unsupported if detected!") ; return theList->get_statement(0) ; } else { return x ; } }
void StatementList::append(const StatementList &sl) { if (&sl == this) { const size_t oldSize = m_list.size(); auto it = m_list.begin(); for (size_t i = 0; i < oldSize; i++) { m_list.push_back(*it++); } } else { m_list.insert(end(), sl.begin(), sl.end()); } }
bool SemanticAnalysis::analyze() { ParseTree *tree = tu_->tree(); StatementList *statements = tree->statements(); for (size_t i = 0; i < statements->length(); i++) { Statement *stmt = statements->at(i); if (stmt->isFunctionStatement()) stmt->accept(this); if (!cc_.canContinueProcessing()) return false; } return cc_.phasePassed(); }
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; }
void RtlTest::testSetConscripts() { // m[1000] = m[1000] + 1000 Statement* s1 = new Assign( Location::memOf( new Const(1000), 0), new Binary(opPlus, Location::memOf( new Const(1000), NULL), new Const(1000))); // "printf("max is %d", (local0 > 0) ? local0 : global1) CallStatement* s2 = new CallStatement(); std::string name("printf"); Proc* proc = new UserProc(new Prog(), name, 0x2000); // Making a true LibProc is problematic s2->setDestProc(proc); s2->setCalleeReturn(new ReturnStatement); // So it's not a childless call Exp* e1 = new Const("max is %d"); Exp* e2 = new Ternary(opTern, new Binary(opGtr, Location::local("local0", NULL), new Const(0)), Location::local("local0", NULL), Location::global("global1", NULL)); StatementList args; args.append(new Assign(Location::regOf(8), e1)); args.append(new Assign(Location::regOf(9), e2)); s2->setArguments(args); std::list<Statement*> list; list.push_back(s1); list.push_back(s2); RTL* rtl = new RTL(0x1000, &list); rtl->setConscripts(0, false); std::string expected( "00001000 0 *v* m[1000\\1\\] := m[1000\\2\\] + 1000\\3\\\n" " 0 CALL printf(\n" " *v* r8 := \"max is %d\"\\4\\\n" " *v* r9 := (local0 > 0\\5\\) ? local0 : global1\n" " )\n" " Reaching definitions: \n" " Live variables: \n"); std::ostringstream ost; rtl->print(ost); std::string actual = ost.str(); CPPUNIT_ASSERT_EQUAL(expected, actual); }
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"); }
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 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; }
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); } }
static void append_flattened(StatementList *slist,Statement *stat) { if (!stat) return; if (is_kind_of<StatementList>(stat)) { StatementList *sl = to<StatementList>(stat); int len = sl->get_statement_count(); for (int i = 0; i < len; i++) { Statement *s = sl->get_statement(i); s->set_parent(0); slist->append_statement(s); } } else { stat->set_parent(0); slist->append_statement(stat); } }
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); } }
Statement *while_statement_walker::dismantle_while_statement(WhileStatement *the_while){ Statement *result = 0; Expression *condition = the_while->get_condition(); // we are going to reuse the condition so we must remove it // from the if statement first to avoid ownership conflicts // the_while->set_condition(SourceOp()); // remove_source_op(condition); remove_suif_object(condition); Statement *body = the_while->get_body(); CodeLabelSymbol *break_label = the_while->get_break_label(); CodeLabelSymbol *continue_label = the_while->get_continue_label(); // likewise of these parts - remove from if. the_while->set_body(0); the_while->set_break_label(0); the_while->set_continue_label(0); if (body != 0) { StatementList *replacement = create_statement_list(body->get_suif_env()); result = replacement; suif_assert(continue_label != 0); UnaryExpression* negated_condition = create_unary_expression(body->get_suif_env(), condition->get_result_type(), k_logical_not, condition ); replacement-> append_statement(create_label_location_statement(body->get_suif_env(), continue_label)); replacement-> append_statement(create_branch_statement(body->get_suif_env(), negated_condition, break_label)); replacement->append_statement(body); suif_assert(break_label != 0); replacement->append_statement(create_jump_statement(body->get_suif_env(),continue_label)); replacement-> append_statement(create_label_location_statement(body->get_suif_env(), break_label)); } the_while->get_parent()->replace(the_while,result); return result; }
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(); }
void ScalarReplacementPass2::AppendStores() { assert(procDef != NULL) ; StatementList* innermost = InnermostList(procDef) ; assert(innermost != NULL) ; list<std::pair<Expression*, VariableSymbol*> >::iterator storeIter = IdentifiedStores.begin() ; while (storeIter != IdentifiedStores.end()) { (*storeIter).first->set_parent(NULL) ; LoadVariableExpression* loadTmp = create_load_variable_expression(theEnv, (*storeIter).second->get_type()->get_base_type(), (*storeIter).second) ; StoreStatement* postStore = create_store_statement(theEnv, loadTmp, (*storeIter).first) ; innermost->append_statement(postStore) ; ++storeIter ; } }
void ScalarReplacementPass2::PrependLoads() { assert(procDef != NULL) ; StatementList* innermost = InnermostList(procDef) ; assert(innermost != NULL) ; list<std::pair<Expression*, VariableSymbol*> >::iterator loadIter = IdentifiedLoads.begin() ; while (loadIter != IdentifiedLoads.end()) { (*loadIter).first->set_parent(NULL) ; LoadExpression* loadTmp = create_load_expression(theEnv, (*loadIter).first->get_result_type(), (*loadIter).first) ; StoreVariableStatement* prependedLoad = create_store_variable_statement(theEnv, (*loadIter).second, loadTmp) ; innermost->insert_statement(0, prependedLoad) ; ++loadIter ; } }
Walker::ApplyStatus dw2wtp_do_while_statement_walker::operator () (SuifObject *x) { SuifEnv *env = get_env(); DoWhileStatement *do_while_stmt = to<DoWhileStatement>(x); Expression *do_while_condition = do_while_stmt->get_condition(); do_while_stmt->set_condition(0); Statement *do_while_body = do_while_stmt->get_body(); do_while_stmt->set_body(0); IfStatement *first_iteration = create_if_statement(env, to<Expression>(deep_suif_clone(do_while_condition)), to<Statement>(deep_suif_clone(do_while_body))); WhileStatement *remaining_iterations = create_while_statement(env, do_while_condition, do_while_body); StatementList *replacement = create_statement_list(env); replacement->append_statement(first_iteration); replacement->append_statement(remaining_iterations); do_while_stmt->get_parent()->replace(do_while_stmt, replacement); set_address(replacement); return Walker::Replaced; }
void RemoveUnsupportedStatements::do_procedure_definition(ProcedureDefinition* p) { procDef = p ; assert(procDef != NULL) ; OutputInformation("Remove Unsupported statements begins") ; CForStatement* innermost = InnermostLoop(procDef) ; if (innermost == NULL) { OutputInformation("Remove Unsupported statements ends") ; return ; } StatementList* parentList = dynamic_cast<StatementList*>(innermost->get_parent()) ; CForStatement* outerLoops = dynamic_cast<CForStatement*>(innermost->get_parent()) ; CForStatement* outermostLoop = innermost ; while (parentList == NULL && outerLoops != NULL) { parentList = dynamic_cast<StatementList*>(outerLoops->get_parent()) ; outermostLoop = outerLoops ; outerLoops = dynamic_cast<CForStatement*>(outerLoops->get_parent()) ; } assert(parentList != NULL) ; assert(outermostLoop != NULL) ; int position = DeterminePosition(parentList, outermostLoop) ; assert(position >= 0) ; for (int i = 0 ; i < position ; ++i) { StatementList* blankList = create_statement_list(theEnv) ; Statement* toReplace = parentList->get_statement(i) ; parentList->replace(toReplace, blankList) ; // Will this delete kill me? //delete toReplace ; } for (int i = position + 1 ; i < parentList->get_statement_count() ; ++i) { StatementList* blankList = create_statement_list(theEnv) ; Statement* toReplace = parentList->get_statement(i) ; parentList->replace(toReplace, blankList) ; // Will this delete kill me?... yep! or not... //delete toReplace ; } OutputInformation("Remove Unsupported statments ends") ; }