Walker::ApplyStatus fssp_scope_statement_walker::operator () (SuifObject *x){ ScopeStatement *scope_stmt = to<ScopeStatement>(x); Statement *scope_stmt_body = scope_stmt->get_body(); SymbolTable *scope_stmt_sym_table = scope_stmt->get_symbol_table(); DefinitionBlock *scope_stmt_def_block = scope_stmt->get_definition_block(); ProcedureDefinition *enclosing_proc_def = get_procedure_definition(scope_stmt); SymbolTable *enclosing_proc_def_sym_table = enclosing_proc_def->get_symbol_table(); DefinitionBlock *enclosing_proc_def_def_block = enclosing_proc_def->get_definition_block(); while(scope_stmt_sym_table->get_symbol_table_object_count() > 0){ SymbolTableObject *sym_tab_obj = scope_stmt_sym_table->remove_symbol_table_object(0); rename_symbol(sym_tab_obj, "" ); // String("renamed_") + String(sym_tab_obj->get_name())); enclosing_proc_def_sym_table->append_symbol_table_object(sym_tab_obj); } while(scope_stmt_def_block->get_variable_definition_count() > 0){ VariableDefinition *var_def = scope_stmt_def_block->remove_variable_definition(0); enclosing_proc_def_def_block->append_variable_definition(var_def); } scope_stmt->set_body(0); scope_stmt->get_parent()->replace(scope_stmt, scope_stmt_body); set_address(scope_stmt_body); return Walker::Replaced; };
/*-------------------------------------------------------------------- * remove_annote * */ static void do_remove_annote(const event &, void *) { vnode *vn = vman->get_selection(); if (vn && ((SuifObject*)vn->get_object())->isKindOf(Annote::get_class_name())) { Annote *ann = (Annote *) vn->get_object(); AnnotableObject *parent = (AnnotableObject*) ann->get_parent(); ProcedureDefinition *proc = get_procedure_definition( ann ); parent->remove_annote(ann); delete vn; delete ann; /* post event, notifying that the object has been modified */ if ( proc ) { vnode *p = create_vnode(proc); post_event(event(p, PROC_MODIFIED, 0)); } else { FileBlock *fb = get_file_block( parent ); if (fb) { post_event(event( create_vnode(fb), FSE_MODIFIED, 0)); } else { assert(false); // this should not happen.. } } } else { display_message(0, "No annote selected. Please select an annote first."); return; } }
Walker::ApplyStatus hsbm_call_statement_walker::operator () (SuifObject *x) { SuifEnv *env = get_env(); CallStatement *call_stmt = to<CallStatement>(x); SymbolAddressExpression *callee_address = to<SymbolAddressExpression>(call_stmt->get_callee_address()); ProcedureSymbol *proc_symbol = to<ProcedureSymbol>(callee_address->get_addressed_symbol()); String called_proc_name = proc_symbol->get_name(); MarkStatement *mark_stmt = NULL; ProcedureDefinition* proc_def = get_procedure_definition(call_stmt); if(called_proc_name == String("begin_hw")){ mark_stmt = create_mark_statement(env); BrickAnnote *ba = to<BrickAnnote>(proc_def->lookup_annote_by_name("begin_hw")); ba->append_brick(create_suif_object_brick(env, mark_stmt)); }else if(called_proc_name == String("end_hw")){ mark_stmt = create_mark_statement(env); BrickAnnote *ba = to<BrickAnnote>(proc_def->lookup_annote_by_name("end_hw")); ba->append_brick(create_suif_object_brick(env, mark_stmt)); } if(mark_stmt){ call_stmt->get_parent()->replace(call_stmt, mark_stmt); set_address(mark_stmt); return Walker::Replaced; } return Walker::Continue; }
/*-------------------------------------------------------------------- * output_viewer::view * * view a SuifObject */ void output_viewer::view( SuifObject* tn, bool select) { if (!tn) return; if (tn->isKindOf(ProcedureSymbol::get_class_name())) tn = get_procedure_definition( tn ); if (!tn) return; FileBlock *fb = get_file_block( tn ); if ( fb ) view( fb ); if ( !outtree ) return; code_fragment* f = outtree->lookup(tn); if ( f ) { text->view(f->first_line(), 0); text->select_clear(); if ( select ) { f->select_code_fragments( text, true ); } #ifdef AG if (select) { vnode *vn = vman->find_vnode(f); text->select(vn); } #endif } }
/*-------------------------------------------------------------------- * output_viewer::map_tree_node * * This is a mapping function from a tree-node to the corresponding * line number on the output file */ code_range output_viewer::map_tree_node(SuifObject* tn, output_viewer* viewer) { /* find the src file and src line */ String file; int line = find_source_line( tn, file ).c_int(); if (line) { int possible_line = 0; ProcedureDefinition *proc = get_procedure_definition( tn ); if (!proc) return code_range( INT_MAX, 0 ); /* iterate through the procedures */ for ( s_count_t _cnt=0; _cnt<viewer->proc_scopes->size(); _cnt++ ) { scope_node *scope = (*viewer->proc_scopes)[_cnt]; if (scope->proc == proc) { /* this is the correct procedure */ if (tn->isKindOf(ProcedureDefinition::get_class_name()) ) { return code_range( scope->first_line, scope->last_line ); } for ( s_count_t _cnt1=0; _cnt1 < scope->nodes.size(); _cnt1++ ) { output_node *node = scope->nodes[_cnt1]; if (node->src_line == line && !strcmp( node->src_file, file.c_str() ) ) { if (!node->mapped) { node->mapped = true; return code_range(node->out_line, node->out_line); } else { possible_line = node->out_line; } } } } } if ( possible_line ) { return code_range( possible_line, possible_line ); } else { return code_range( INT_MAX, 0 ); } } return code_range( INT_MAX, 0 ); }
/*-------------------------------------------------------------------- * src_viewer::view * * view a tree node */ void src_viewer::view( SuifObject* tn, bool select ) { if ( !tn ) return; ProcedureDefinition* pdef = get_procedure_definition( tn ); if ( !pdef ) return; String file; int line = find_source_line( tn, file ).c_int(); (void) line; FileBlock* fb = get_file_block( pdef ); view( fb, file ); /* is there a source file ? */ if ( !stree ) return; /* look up tree node */ code_fragment* f = stree->lookup( tn ); if ( f ) { vnode* vn = vman->find_vnode( f ); text->view( f->first_line(), 0 ); if ( select ) { text->select( vn ); } } else { String file; int line = find_source_line( tn, file ).c_int(); if ( line ) { text->view( line, 0); if ( select ) { text->select_line( line ); } } } }
/*-------------------------------------------------------------------- * output_viewer::print_output * */ bool output_viewer::print_output(String file) { const char* filename = file.c_str(); post_progress(this, "Loading output file ...", 0); text->clear(); FILE *fd = fopen( filename, "r" ); if (!fd) { text->fout() << "Cannot open file " << filename << endl; text->update(); unpost_progress( this ); return false; } /* read the file */ fseek(fd, 0, SEEK_END); long length = ftell(fd); fseek(fd, 0, SEEK_SET); char *buffer = new char[length]; int l = fread(buffer, 1, length, fd); buffer[l] = 0; fclose(fd); int current_line = 1; int current_src_line = -1; bool new_src_line = false; bool parse_error = false; int scope = 0; scope_node *current_scope = 0; int state = NORMAL_STATE; erase_mappings(); // erase previous mappings char *next_line; char *current_src_file = 0; for (char *line = buffer; line; line = next_line) { next_line = strchr(line, '\n'); if (next_line) { *next_line++ = 0; } /* * Preprocess C */ bool found_line_num; char *ppc = preprocess_c(line, state, current_src_file, current_src_line, found_line_num); /* * Scan for "{" and "}" to determine current scope. */ for (char *p = ppc; *p; p++) { if (*p == '{') { if (scope == 0) { current_scope = new scope_node; current_scope->first_line = current_line; current_scope->proc = 0; proc_scopes->push_back(current_scope); } scope++; } else if (*p == '}') { scope--; if (scope < 0) { parse_error = true; } if (scope == 0) { current_scope->last_line = current_line; } } } delete (ppc); /* Check if current line has line number annotation */ if (found_line_num) { new_src_line = true; continue; // don't print it out } /* * Record the line, if nec. */ if (new_src_line) { /* enter this line as a node */ output_node *node = new output_node(current_src_line, current_src_file, current_line); if (current_scope) { current_scope->nodes.push_back(node); /* match current scope to procedure */ if (current_scope->proc == 0) { SuifObject *tn = map_line_to_tree_node( find_file_block(String(current_src_file)), current_src_line); if (tn) { current_scope->proc = get_procedure_definition( tn ); } } } new_src_line = false; } /* print the line out */ text->fout() << line << endl; current_line++; } delete (buffer); text->update(); if (scope != 0 || parse_error) { display_message(this, "Warning: unable to parse the file `%s'.", filename); } if ( proc_scopes->empty()) { display_message(this, "Warning: cannot find line number information in " "the file `%s'.", filename); unpost_progress( this ); return false; } /* * Get the file set entry of this file */ if (!current_fse) { if ( current_src_file ) { current_fse = find_file_block(current_src_file); } if (!current_fse) { display_message(this, "Warning: cannot find the corresponding file set entry."); unpost_progress(this); return false; } } /* * Create new output tree */ delete outtree; outtree = new code_tree; outtree->set_map_fn( (map_tn_fn) &map_tree_node, this ); // iterate over the procedures DefinitionBlock *def_block = current_fse->get_definition_block(); int num_procs = def_block->get_procedure_definition_count(); for (int i = 0; i < num_procs; ++i) { ProcedureDefinition *proc = def_block->get_procedure_definition(i); ExecutionObject *body = proc->get_body(); if ( body ) { outtree->build( proc ); } post_progress(this, "Loading output file..", ((float) (i+1))/num_procs*100); } /* create tags */ outtree->create_tags(text); unpost_progress(this); return true; }
Statement* if_statement_walker::dismantle_if_statement(IfStatement* the_if){ Statement *result = 0; ProcedureDefinition* proc_def = get_procedure_definition(the_if); Expression *condition = the_if->get_condition(); // we are going to reuse the condition so we must remove it // from the if statement first to avoid ownership conflicts remove_suif_object(condition); // the_if->set_condition(SourceOp()); // remove_source_op(condition); Statement *then_part = the_if->get_then_part(); Statement *else_part = the_if->get_else_part(); // likewise of these parts - remove from if. the_if->set_then_part(0); the_if->set_else_part(0); remove_suif_object(then_part); remove_suif_object(else_part); if (then_part != 0) { StatementList *replacement = create_statement_list( the_if->get_suif_env() ); result = replacement; UnaryExpression* negated_condition = create_unary_expression( the_if->get_suif_env(), condition->get_result_type(), k_logical_not, condition ); if (else_part != 0) { CodeLabelSymbol *else_label = new_anonymous_label(proc_def->get_symbol_table()); CodeLabelSymbol *done_label = new_anonymous_label(proc_def->get_symbol_table()); replacement->append_statement(create_branch_statement(the_if->get_suif_env(), negated_condition, else_label)); replacement->append_statement(then_part); replacement->append_statement(create_jump_statement(the_if->get_suif_env(),done_label)); replacement->append_statement(create_label_location_statement(the_if->get_suif_env(), else_label)); replacement->append_statement(else_part); replacement->append_statement(create_label_location_statement(the_if->get_suif_env(), done_label)); } else { CodeLabelSymbol *done_label = create_new_label(get_procedure_definition(the_if)); replacement-> append_statement(create_branch_statement(the_if->get_suif_env(), negated_condition, done_label)); replacement->append_statement(then_part); replacement-> append_statement(create_label_location_statement(the_if->get_suif_env(), done_label)); } } else { if (else_part != 0) { StatementList *replacement = create_statement_list(the_if->get_suif_env()); result = replacement; CodeLabelSymbol *done_label = create_new_label(proc_def); replacement->append_statement(create_branch_statement(the_if->get_suif_env(), condition, done_label)); replacement->append_statement(else_part); replacement-> append_statement(create_label_location_statement(the_if->get_suif_env(), done_label)); } else { EvalStatement *replacement = create_eval_statement(the_if->get_suif_env()); result = replacement; replacement->append_expression(condition); } } the_if->get_parent()->replace(the_if, result); return result; }
Statement* scope_statement_walker::dismantle_scope_statement(ScopeStatement *the_scope_stat){ ProcedureDefinition* proc_def = get_procedure_definition(the_scope_stat); Statement *body = the_scope_stat->get_body(); if(body==NULL){ the_scope_stat->print_to_default(); } body->set_parent(0); the_scope_stat->set_body(0); // This is a bug? // remove_suif_object(body); SymbolTable * symbol_table = the_scope_stat->get_symbol_table(); DefinitionBlock * definition_block = the_scope_stat->get_definition_block(); SymbolTable *new_symbol_table = proc_def->get_symbol_table(); DefinitionBlock *new_definition_block = proc_def->get_definition_block(); if (symbol_table != 0) { // start by creating a name for the symbol in the new symbol table Iter<SymbolTable::lookup_table_pair> piter = symbol_table->get_lookup_table_iterator(); while (piter.is_valid()) { indexed_list<LString,SymbolTableObject*>::pair p = piter.current(); SymbolTableObject * obj = p.second; const LString &name = p.first; new_symbol_table->add_lookup_table(name,obj); piter.next(); } // now move all symbols into the symbol table for the procedure scope // at the same time, we delete them from the current symbol table and // remove all references to this symbol table from the name list attached // to the symbol // DLH // I modifed this to build a list so we aren't iterating over // a changing object (which didn't work. and I don't expect it to) list<SymbolTableObject*> l; {for (Iter<SymbolTableObject*> iter = symbol_table->get_symbol_table_object_iterator(); iter.is_valid(); iter.next()) { l.push_back(iter.current()); }} for (list<SymbolTableObject*>::iterator iter = l.begin(); iter != l.end(); iter++) { SymbolTableObject *object = *iter; symbol_table->remove_symbol_table_object(object); rename_if_collision(object, new_symbol_table); new_symbol_table->add_symbol(object); // symbol_table->remove_all_from_lookup_table(object); // object->remove_all_from_name(symbol_table); } } if (definition_block != 0) { // move all definition block entries int i = definition_block->get_variable_definition_count(); while (i > 0){ i--; VariableDefinition *next = definition_block->remove_variable_definition(i); new_definition_block->append_variable_definition(next); } i = definition_block->get_procedure_definition_count(); while (i > 0){ i--; ProcedureDefinition *next = definition_block->remove_procedure_definition(i); new_definition_block->append_procedure_definition(next); } } the_scope_stat->get_parent()->replace(the_scope_stat, body); return body; }