static void name_variable(Symbol *symbol, LString * file_name) { static LString k_orig_name = "orig_name"; if (symbol->get_name() != emptyLString) return; String base_name; String orig_name = get_orig_sym_name(symbol); if (orig_name != emptyString) { base_name = orig_name; } else { base_name = "_"; String source_file_name = condition_file_name(file_name); // add up to 10 characters at the end of the source file name // name Are you KIDDING! base_name += source_file_name.Right(10); base_name += "Tmp"; } // guaranteed to complete because of the IInteger. // Of course it would be faster if we randomized. for (IInteger ii = 0; ; ii++) { String next_name = base_name + ii.to_String(); if (!LString::exists(next_name)) { SymbolTable *st = symbol->get_symbol_table(); suif_assert_message(st != NULL, ("attempt to name unattached symbol")); st->change_name(symbol,next_name); return; } } suif_assert_message(0, ("Could not form a unique name")); }
FileSetBlock *build_the_file_set_block(SuifEnv *suif) { BasicObjectFactory *basic_of = (BasicObjectFactory *)suif->get_object_factory(BasicObjectFactory::get_class_name()); suif_assert_message(basic_of,("initialization error in basic")); SuifObjectFactory *suif_of = (SuifObjectFactory *)suif->get_object_factory(SuifObjectFactory::get_class_name()); suif_assert_message(suif_of,("initialization error in suif")); BasicSymbolTable *external_symbol_table = create_basic_symbol_table(suif, NULL); BasicSymbolTable *file_symbol_table = create_basic_symbol_table(suif, NULL); FileSetBlock *the_file_set_block = create_file_set_block(suif, external_symbol_table,file_symbol_table); return(the_file_set_block); }
VariableSymbol *build_initialized_variable( SuifEnv *env, const LString &name, DataType *type, ValueBlock *vb, bool make_static) { TypeBuilder *tb = (TypeBuilder *) env->get_object_factory(TypeBuilder::get_class_name()); FileSetBlock *fsb = env->get_file_set_block(); suif_assert_message(fsb->get_file_block_count() == 1,("File is ambiguous")); FileBlock *fb = fsb->get_file_block(0); BasicSymbolTable *symtab = to<BasicSymbolTable>(fb->get_symbol_table()); DefinitionBlock *def = fb->get_definition_block (); QualifiedType *q_type = tb->get_qualified_type(type); VariableSymbol *var = create_variable_symbol( env,q_type, name, false ); symtab->append_symbol_table_object(var); VariableDefinition *vardef = create_variable_definition(env,var,type->get_bit_alignment(), vb,make_static); def->append_variable_definition(vardef); return var; }
size_t ObjectTags::get_tag(const ObjectWrapper &obj) { TagMap::iterator iter = _tags->find(obj.get_address()); suif_assert_message(iter != _tags->end(), ("invalid tag retrieval")); return((*iter).second); }
String to_cstring(const SuifObject *obj) { if (obj == NULL) return("(nil)"); SuifEnv* suif_env = obj->get_suif_env(); ModuleSubSystem *ms = suif_env->get_module_subsystem(); CPrintStyleModule* cprint = (CPrintStyleModule*) ms->retrieve_module("cprint"); suif_assert_message(cprint, ("CPrintStyleModule is not loaded")); return cprint->print_to_string(obj); }
static QualifiedType* unwrap_ptr_ref_type(DataType* type){ if (is_kind_of<PointerType>(type)){ return (QualifiedType*)to<PointerType>(type)->get_reference_type(); }else if (is_kind_of<ReferenceType>(type)){ return (QualifiedType*)to<ReferenceType>(type)->get_reference_type(); }else suif_assert_message(false, ("Expecting either a pointer or a reference type")); return NULL; };
FILE *open_c_file(const String &filespec) { if (filespec == String("-")) { return(stdout); } FILE *f = fopen(filespec.c_str(), "w"); if (f == NULL) { suif_assert_message(f == NULL, ("Could not open %s for writing.\n", filespec.c_str())); } return f; }
void RequireProcedureReturns:: do_procedure_definition( ProcedureDefinition *pd) { if (is_kind_of<Statement>(pd->get_body())) { Statement *st = to<Statement>(pd->get_body()); Statement *last_st = find_last_statement(st); suif_assert_message(last_st != NULL, ("Only found a NULL last statement for procedure body")); if (is_kind_of<ReturnStatement>(last_st)) return; // If it is a Statement List, add a new return into the // statement. ReturnStatement *ret = create_return_statement(get_suif_env(), NULL); // We leave the "expression" blank here. // Use the normalize_procedure_returns pass to add the value. insert_statement_after(last_st, ret); } }
static void do_go_cmd(const event& e, void*) { binding *b; char *node_name; char *node_info; vmenu *menu = (vmenu *) e.get_source(); menu->clear("Go"); b = new binding((bfun) &do_go_back_cmd, 0); menu->add_command(b, "Go", "Back"); menu->add_separator("Go"); /* construct list of previously selected objects */ vnode_list *hist = vman->get_selection_history(); for ( s_count_t i=1; /* jump over current object */ (i<20) && (i<hist->size()); i++ ) { vnode *vn = (*hist)[i]; char *tag = vn->get_tag(); if ( tag == tag_suif_object ) { SuifObject *obj = (SuifObject *) vn->get_object(); node_info = (char*)obj->get_meta_class()->get_class_name().c_str(); } else if (tag == tag_code_fragment) { node_info = ""; } else { suif_assert_message( false, ("Unknown tag") ); } node_name = new char[strlen(tag)+strlen(node_info)+100 /*just to be safe*/]; sprintf(node_name, "[%s] (0x%p) %s", tag, vn->get_object(), node_info); b = new binding((bfun) &do_go_to_node_cmd, vn); menu->add_command(b, "Go", node_name); delete [] node_name; } }
static Expression *build_empty_expression(DataType *t) { SuifEnv *s = t->get_suif_env(); suif_assert_message(!is_kind_of<GroupType>(t), ("build_empty_expression:: can not handle GroupType")); if (is_kind_of<VoidType>(t)) return(NULL); if (is_kind_of<IntegerType>(t)) { IntConstant *c = create_int_constant(s, t, IInteger(0)); return c; } // This works for Int, Float, Enum, pointers TypeBuilder *tb = get_type_builder(s); IntegerType *it = tb->get_integer_type(); IntConstant *c = create_int_constant(s, it, IInteger(0)); Expression *cvt = create_unary_expression(s, t, k_convert, c); return(cvt); }
FileSetBlock *CreateSuifComplexInputPass::build_file_set_block() { SuifEnv *suif = get_suif_env(); // Build a blank fileset FileSetBlock *the_file_set_block = build_the_file_set_block(suif); _suif_env->set_file_set_block( the_file_set_block ); SymbolTable* ext_table = the_file_set_block->get_external_symbol_table(); _tb = (TypeBuilder*)suif->get_object_factory(TypeBuilder::get_class_name()); suif_assert_message(_tb, ("Could not initialize typebuilder")); // build the types _char_type = _tb->get_integer_type( sizeof(char),sizeof(char),true); // ext_table -> append_symbol_table_object( _char_type ); _q_char_type = _tb->get_qualified_type(_char_type); _fpt = _tb->get_floating_point_type( 64 ,64); _q_fpt = _tb->get_qualified_type(_fpt); // ext_table -> append_symbol_table_object( _fpt ); _argc_type = _tb->get_integer_type(sizeof(int),sizeof(int)*8,true); _q_argc_type = _tb->get_qualified_type(_argc_type); list<QualifiedType *> ql; _printf_type = _tb->get_c_procedure_type(_argc_type, ql, false,true,sizeof(int)); // ext_table -> append_symbol_table_object( _printf_type ); // Expression *i0 = create_int_constant(suif, 0, 0); // Expression *i1 = create_int_constant(suif, 0, strlen(HELLO_STRING) + 1); _string_literal_type = _tb->get_array_type( //IInteger((strlen(HELLO_STRING) + 1) * sizeof(char)), // (int)sizeof(char), _q_char_type, 0, strlen(HELLO_STRING)+1); // i0,i1); // ext_table -> append_symbol_table_object( _string_literal_type ); // _argc_type = create_integer_type(suif, sizeof(int),sizeof(int)*8,true); // ext_table -> append_symbol_table_object( _argc_type ); // _q_argc_type = tb->get_qualified_type(_argc_type); _argv_type = _tb->get_pointer_type(_tb->get_pointer_type(_char_type)); _q_argv_type = _tb->get_qualified_type(_argv_type); list<QualifiedType *> qlist; qlist.push_back(_q_argc_type); qlist.push_back(_q_argv_type); _main_type = _tb->get_c_procedure_type( _argc_type, qlist, false,true,sizeof(int)); // _main_type->append_argument( _q_argc_type ); // _main_type->append_argument( _q_argv_type ); // ext_table -> append_symbol_table_object( _main_type ); // Build a blank main routine. ProcedureDefinition *main_definition = build_main( "complex_input.c"); // Pull info out of main. ParameterSymbol *argc_symbol = main_definition->get_formal_parameter(0); CProcedureType *main_type = to<CProcedureType>(main_definition->get_procedure_symbol()->get_type()); StatementList *main_body = to<StatementList>(main_definition->get_body()); // Now create the printf statement and add it in main_type->append_argument(_tb->get_qualified_type(_tb->get_pointer_type(_char_type))); ProcedureSymbol *printf_symbol = create_procedure_symbol(suif, _printf_type, "printf", true ); the_file_set_block->get_external_symbol_table()-> add_symbol(printf_symbol); VariableSymbol *float_literal_symbol = create_variable_symbol( suif, _q_fpt, " float symbol", true ); ext_table -> add_symbol(float_literal_symbol ); MultiValueBlock *float_literal_initialization = create_multi_value_block( _suif_env, _fpt); float_literal_initialization->add_sub_block(0, create_expression_value_block( _suif_env, create_float_constant(_suif_env, _fpt, 7.0))); VariableDefinition *float_literal_definition = create_variable_definition(suif, float_literal_symbol,sizeof(float), float_literal_initialization, true); main_definition->get_definition_block()-> append_variable_definition(float_literal_definition); VariableSymbol *string_literal_symbol = create_variable_symbol(suif, _q_string_literal_type, emptyLString, true ); main_definition->get_symbol_table()->append_symbol_table_object(string_literal_symbol); MultiValueBlock *string_literal_initialization = create_multi_value_block(suif, _string_literal_type); for (size_t char_num = 0; char_num <= strlen(HELLO_STRING); ++char_num) { string_literal_initialization-> add_sub_block(char_num, create_expression_value_block( _suif_env, create_int_constant( _suif_env, _char_type, HELLO_STRING[char_num] ))); } VariableDefinition *string_literal_definition = create_variable_definition( _suif_env, string_literal_symbol,sizeof(char), string_literal_initialization, true); main_definition->get_definition_block()-> append_variable_definition(string_literal_definition); SymbolAddressExpression* printf_address_expression = create_symbol_address_expression( _suif_env, pointer_to( _suif_env, _printf_type), printf_symbol ); SymbolAddressExpression* printf_argument_expression = create_symbol_address_expression( _suif_env, pointer_to(_suif_env, _char_type), string_literal_symbol ); CallStatement *printf_call = create_call_statement( _suif_env, NULL, // _argc_type, printf_address_expression ); printf_call->append_argument(printf_argument_expression); // EvalStatement *printf_statement = create_eval_statement( _suif_env ); // printf_statement->append_expression(printf_call); main_body->append_statement(printf_call); Expression *condition = create_load_variable_expression(_suif_env, unqualify_data_type(argc_symbol->get_type()), argc_symbol); StatementList *then_part = create_statement_list(suif); Statement* s = deep_suif_clone( _suif_env, printf_call); then_part->append_statement( s ); StatementList *else_part = create_statement_list( _suif_env ); else_part->append_statement(deep_suif_clone<Statement>( _suif_env, printf_call)); else_part->append_statement(deep_suif_clone<Statement>( _suif_env, printf_call)); IfStatement *the_if = create_if_statement( _suif_env, condition, then_part, else_part); main_body->append_statement(the_if); return the_file_set_block; }
/*-------------------------------------------------------------------- * info_viewer::view * */ void info_viewer::view(vnode* vn) { if ( !vn ) return; text->clear(); text->tag_begin( vn ); fstream& fout = text->fout(); char *tag = vn->get_tag(); text->tag_style(BOLD_BEGIN); fout << "Object: 0x" << (void*)vn->get_object() << '(' << tag << ")\n"; text->tag_style(BOLD_END); /* properties */ text->tag_style( BOLD_BEGIN ); fout << "Properties:\n"; text->tag_style( BOLD_END ); list<vprop *> *plist = vn->get_prop_list(); if ( !plist || plist->empty() ) { fout << "<No properties defined>\n"; } else { for ( s_count_t i = 0; i < plist->size(); i++ ) { vprop* p = (*plist)[i]; if ( p->name() ) { char* desc = p->description(); fout << '[' << p->name() << "]: " << (desc ? desc : "") << endl; } } } fout << endl; SuifObject* obj; if ( tag == tag_suif_object ) { obj = (SuifObject*) vn->get_object(); } else if ( tag == tag_code_fragment ) { code_fragment* f = (code_fragment*) vn->get_object(); obj = (SuifObject*) f->node(); } else { suif_assert_message(false, ("Unknown tag")); } text->tag_style( BOLD_BEGIN ); const MetaClass *m = obj->get_meta_class(); fout << "Suif object type: " << m->get_class_name().c_str() << "\n\n"; text->tag_style( BOLD_END ); // if obj is a file_set_block => print the whole file_set_block // in all other cases just print the obj formater f(suif_env, text, -1 ); //is_file_set_block( obj ) ? -1 : 1 ); if ( is_kind_of<FileSetBlock>( obj ) ) { // print only the annotes FileSetBlock *fb = to<FileSetBlock>(obj); Iter<Annote*> annote_iter = fb->get_annote_iterator(); for ( int i = 0; i < (int)fb->get_annote_count(); i++ ) { Annote* ann = annote_iter.current(); annote_iter.next(); vn = create_vnode( ann ); text->tag_begin( vn ); f.print_zot( ann, fout ); text->tag_end( vn ); fout << endl; } } else { f.print_zot( obj, fout ); fout << endl; } text->tag_end( vn ); text->update(); }
void One2MultiArrayExpressionPass::do_procedure_definition(ProcedureDefinition* proc_def) { bool kill_all = !(_preserve_one_dim->is_set()); // access all array type declarations and create corresponding multi array types SuifEnv* suif_env = proc_def->get_suif_env(); TypeBuilder* tb = (TypeBuilder*)suif_env-> get_object_factory(TypeBuilder::get_class_name()); (void) tb; // avoid warning #ifdef CONVERT_TYPES for (Iter<ArrayType> at_iter = object_iterator<ArrayType>(proc_def); at_iter.is_valid();at_iter.next()) { MultiDimArrayType* multi_type = converter->array_type2multi_array_type(&at_iter.current()); } #endif //CONVERT_TYPES // collect tops of array access chains into this list list<ArrayReferenceExpression*> ref_exprs; for (Iter<ArrayReferenceExpression> are_iter = object_iterator<ArrayReferenceExpression>(proc_def); are_iter.is_valid(); are_iter.next()) { // itself an array and parent is *not* an array ArrayReferenceExpression* are = &are_iter.current(); if((kill_all || is_kind_of<ArrayReferenceExpression>(are->get_base_array_address())) && !is_kind_of<ArrayReferenceExpression>(are->get_parent())) { //printf("%p \t", are);are->print_to_default(); ref_exprs.push_back(are); } } // for top all expressions, convert them to multi-exprs for(list<ArrayReferenceExpression*>::iterator ref_iter = ref_exprs.begin(); ref_iter != ref_exprs.end(); ref_iter++) { ArrayReferenceExpression* top_array = *ref_iter; converter->convert_array_expr2multi_array_expr(top_array); } #ifdef CONVERT_TYPES // replace the types of all array variables for (Iter<VariableSymbol> iter = object_iterator<VariableSymbol>(proc_def); iter.is_valid();iter.next()) { VariableSymbol* vd = &iter.current(); DataType *vtype = tb->unqualify_data_type(vd->get_type()); if (is_kind_of<ArrayType>(vtype)) { MultiDimArrayType* multi_type = converter->array_type2multi_array_type(to<ArrayType>(vtype)); vd->replace(vd->get_type(), tb->get_qualified_type(multi_type)); } } // remove the remaining one-dim array types converter->remove_all_one_dim_array_types(); #endif //CONVERT_TYPES // make sure no traces of single-dim arrays are left if(kill_all){ {for(Iter<ArrayReferenceExpression> iter = object_iterator<ArrayReferenceExpression>(proc_def); iter.is_valid(); iter.next()) { // ArrayReferenceExpression* are = &iter.current(); //are->print_to_default(); printf("at %p \t", are); suif_assert_message(false, ("ARE not eliminated")); } } #ifdef CONVERT_TYPES {for(Iter<ArrayType> iter = object_iterator<ArrayType>(proc_def); iter.is_valid(); iter.next()) {suif_assert_message(false, ("ArrayType not eliminated"));}} #endif } }
/** Convert ArrayReferenceExpression \a top_array to a MultiDimArrayExpression. */ void OneDimArrayConverter:: convert_array_expr2multi_array_expr(ArrayReferenceExpression* top_array){ Expression* expr = top_array; unsigned int i = 0; suif_vector<Expression*> lower_bounds; suif_vector<Expression*> upper_bounds; list<Expression*> indices; IInteger bit_size, bit_alignment; suif_vector<ArrayReferenceExpression *> exprs; do{ ArrayReferenceExpression* are = to<ArrayReferenceExpression>(expr); expr = are->get_base_array_address(); exprs.push_back(are); } while(is_kind_of<ArrayReferenceExpression>(expr)); // collect bounds and indeces for (unsigned int exp_ind = 0; exp_ind < exprs.size();exp_ind++) { ArrayReferenceExpression* are = exprs[exp_ind]; DataType* type = are->get_base_array_address()->get_result_type(); ArrayType* array_type; if(is_kind_of<ArrayType>(type)){ array_type = to<ArrayType>(type); }else{ array_type = to<ArrayType>( unwrap_ptr_ref_type(type)->get_base_type()); } bit_size = array_type->get_element_type()-> get_base_type()->get_bit_size(); bit_alignment = array_type->get_element_type()-> get_base_type()->get_bit_alignment(); // What happens to ArrayType?? Does it become garbage? suif_assert(array_type->get_lower_bound()); suif_assert(array_type->get_upper_bound()); // clone bounds and add to the lists lower_bounds.push_back(array_type->get_lower_bound()); upper_bounds.push_back(array_type->get_upper_bound()); // save the index Expression* index = are->get_index(); remove_suif_object(index); index->set_parent(NULL); indices.push_back(index); suif_assert(upper_bounds[i]); suif_assert(lower_bounds[i]); suif_assert(indices[i]); i++; } // Build the offset for the expression. We have to traverse upwards to do this Expression *top_expr_base = exprs[exprs.size()-1]->get_base_array_address(); DataType* top_expr_base_type = top_expr_base->get_result_type(); ArrayType* top_array_type; if(is_kind_of<ArrayType>(top_expr_base_type)){ top_array_type = to<ArrayType>(top_expr_base_type); }else{ top_array_type = to<ArrayType>(tb->unqualify_data_type(unwrap_ptr_ref_type( top_expr_base_type))); } Expression* inc = create_int_constant(suif_env, 1); Expression* offset = create_int_constant(suif_env, 0); suif_vector<Expression *> elements; for (unsigned int ind = 0;ind < lower_bounds.size(); ind ++) { Expression *lower = lower_bounds[ind]; Expression *upper = upper_bounds[ind]; offset = build_dyadic_expression(k_add, offset, build_dyadic_expression( k_multiply, deep_suif_clone(inc), deep_suif_clone(lower))); Expression *element = build_dyadic_expression(k_add, build_dyadic_expression(k_subtract, deep_suif_clone(upper), deep_suif_clone(lower)), create_int_constant(suif_env,1)); inc = build_dyadic_expression(k_multiply, inc, deep_suif_clone(element)); elements.push_back(element); } // Now, inc and offset are ready // retrieve the multi-type MultiDimArrayType* multi_type; suif_map<ArrayType*, MultiDimArrayType*>::iterator type_iter = type_map->find(top_array_type); #ifdef CONVERT_TYPES suif_assert_message((type_iter != type_map->end()), ("Array type never translated")); #else if(type_iter == type_map->end()){ multi_type = array_type2multi_array_type(top_array_type); }else #endif //CONVERT_TYPES multi_type = (*type_iter).second; remove_suif_object(top_expr_base); // add a convert to the necessary type top_expr_base = create_unary_expression(suif_env, tb->get_pointer_type(multi_type), k_convert, top_expr_base); //top_expr_base->print_to_default(); // construct the expression to be returned MultiDimArrayExpression* mae = create_multi_dim_array_expression(suif_env, top_array->get_result_type(), top_expr_base, offset); // now when we have the expression, set the indices for (list<Expression*>::iterator ind_iter = indices.begin(); ind_iter!=indices.end(); ind_iter++) { Expression* index = *ind_iter; //printf("%p \t", index);index->print_to_default(); mae->append_index(index); } for (suif_vector<Expression *>::iterator eiter = elements.begin(); eiter != elements.end(); eiter ++) { Expression* element = *eiter; mae->append_element(element); } replace_expression(top_array, mae); }