📄 util.cc
字号:
va_list ap; va_start(ap, message); if (write_pseudo) { if ((!(*warned_var)) && !no_warn) { vwarning_line(location, message, ap); *warned_var = TRUE; } } else { verror_line(1, location, message, ap); } va_end(ap); }extern immed comment_for_annote(annote *the_annote) { static char buffer[100]; auto_string comment_string(" "); char *new_string; string_io *the_io = new string_io(&new_string); the_io->printf("%s", the_annote->name()); immed_list *data = the_annote->immeds(); if (!data->is_empty()) { the_io->printf(":"); immed_list_iter data_iter(data); while (!data_iter.is_empty()) { the_io->printf(" "); immed this_immed = data_iter.step(); switch (this_immed.kind()) { case im_int: the_io->printf("%d", this_immed.integer()); break; case im_extended_int: the_io->printf("%s", this_immed.ext_integer()); break; case im_string: the_io->printf("\"%s\"", this_immed.string()); break; case im_float: the_io->printf("%g", this_immed.flt()); break; case im_extended_float: the_io->printf("%s", this_immed.ext_flt()); break; case im_symbol: if (this_immed.offset() != 0) the_io->printf("<"); the_io->printf("%s", this_immed.symbol()->name()); if (this_immed.offset() != 0) the_io->printf(",%d>", this_immed.offset()); break; case im_type: the_io->printf("%s", make_c_type(this_immed.type())); break; case im_op: case im_instr: { boolean old_in_comment = in_comment; in_comment = TRUE; ctree *the_ctree; if (this_immed.is_op()) { comment_operand(this_immed.op()); the_ctree = operand_to_tree(this_immed.op()); } else { comment_instr(this_immed.instr()); the_ctree = process_solo_instr(this_immed.instr()); } transform_and_print_ctree(the_io, the_ctree); delete the_ctree; in_comment = old_in_comment; break; } case im_undef: the_io->printf("?"); break; default: assert(FALSE); } } } delete the_io; comment_string.append(new_string); delete[] new_string; comment_string.append(" "); auto_string filtered_string; char *follow = comment_string.value(); boolean was_star = FALSE; buffer[1] = 0; while (*follow != 0) { if (was_star && (*follow == '/')) filtered_string.append(" "); buffer[0] = *follow; filtered_string.append(buffer); if (*follow == '*') was_star = TRUE; else was_star = FALSE; ++follow; } return immed(filtered_string.value()); }extern char *lookup_gen_op(const char *op_name, int num_args) { if (record_list == NULL) record_list = new format_record_list; format_record_list_iter record_iter(record_list); while (!record_iter.is_empty()) { format_record this_record = record_iter.step(); if (strcmp(this_record.op_name, op_name) == 0) { format_unit_list_iter unit_iter(this_record.unit_list); while (!unit_iter.is_empty()) { format_unit this_unit = unit_iter.step(); if (this_unit.num_args == num_args) return this_unit.format_string; } return this_record.default_format; } } auto_string new_format; new_format.set(op_name); if (num_args > 0) new_format.append("(%n, %m)"); char *format_result = lexicon->enter(new_format.value())->sp; return format_result; }extern void register_gen_op(char *name, char *format, boolean is_default, int num_args) { if (record_list == NULL) record_list = new format_record_list; format_record_list_iter record_iter(record_list); while (!record_iter.is_empty()) { format_record this_record = record_iter.step(); if (strcmp(this_record.op_name, name) == 0) { if (is_default) { this_record.default_format = format; } else { format_unit_list_iter unit_iter(this_record.unit_list); while (!unit_iter.is_empty()) { format_unit this_unit = unit_iter.step(); if (this_unit.num_args == num_args) { this_unit.format_string = format; return; } } format_unit new_unit; new_unit.num_args = num_args; new_unit.format_string = format; this_record.unit_list->append(new_unit); } return; } } format_record new_record; new_record.op_name = name; new_record.unit_list = new format_unit_list; if (is_default) { new_record.default_format = format; } else { new_record.default_format = name; format_unit new_unit; new_unit.num_args = num_args; new_unit.format_string = format; new_record.unit_list->append(new_unit); } record_list->append(new_record); }/* * The following finds the ``composite'' of two types as specified in * ANSI/ISO 9899-1990 section 6.1.2.6. If the types are not ``compatible'', * NULL is returned. */extern type_node *composite(type_node *type_1, type_node *type_2) { boolean is_const = type_1->is_const(); boolean is_volatile = type_2->is_volatile(); if (is_const != type_2->is_const()) return NULL; if (is_volatile != type_2->is_volatile()) return NULL; type_node *unqual_1 = type_1->unqual(); type_node *unqual_2 = type_2->unqual(); if (unqual_1->is_enum()) { if (unqual_2->is_enum()) { if (unqual_1 != unqual_2) return NULL; } else { base_type *old_base = (base_type *)unqual_1; base_type *new_base = new base_type(TYPE_INT, old_base->size(), old_base->is_signed()); unqual_1 = unqual_1->parent()->install_type(new_base); } } else if (unqual_2->is_enum()) { base_type *old_base = (base_type *)unqual_2; base_type *new_base = new base_type(TYPE_INT, old_base->size(), old_base->is_signed()); unqual_2 = unqual_2->parent()->install_type(new_base); } type_node *result; if (unqual_1->is_same(unqual_2)) { result = unqual_1; } else { if (unqual_1->op() != unqual_2->op()) return NULL; switch (unqual_1->op()) { case TYPE_PTR: { ptr_type *ptr_1 = (ptr_type *)unqual_1; ptr_type *ptr_2 = (ptr_type *)unqual_2; result = composite(ptr_1->ref_type(), ptr_2->ref_type()); if (result == NULL) return NULL; result = result->ptr_to(); break; } case TYPE_ARRAY: { array_type *array_1 = (array_type *)unqual_1; array_type *array_2 = (array_type *)unqual_2; array_bound upper_bound; if (array_1->size() != 0) { if (array_2->size() != 0) return FALSE; upper_bound = array_bound(array_1->size() / array_1->elem_type()->size()); } else if (array_2->size() != 0) { upper_bound = array_bound(array_2->size() / array_2->elem_type()->size()); } type_node *elem_1 = array_1->elem_type(); type_node *elem_2 = array_2->elem_type(); while (elem_1->unqual()->is_array() && (elem_1->size() == 0)) { array_1 = (array_type *)(elem_1->unqual()); elem_1 = array_1->elem_type(); } while (elem_2->unqual()->is_array() && (elem_2->size() == 0)) { array_2 = (array_type *)(elem_2->unqual()); elem_2 = array_2->elem_type(); } result = composite(elem_1, elem_2); if (result == NULL) return NULL; array_type *new_array = new array_type(result, array_bound(0), upper_bound); result = result->parent()->install_type(new_array); break; } case TYPE_FUNC: { func_type *func_1 = (func_type *)unqual_1; func_type *func_2 = (func_type *)unqual_2; type_node *return_type = composite(func_1->return_type(), func_2->return_type()); if (return_type == NULL) return NULL; if (!func_1->args_known()) { func_type *temp_func = func_1; func_1 = func_2; func_2 = temp_func; } if (!func_1->args_known()) { func_type *new_func = new func_type(return_type); return return_type->parent()->install_type(new_func); } if (!func_2->args_known()) { unsigned num_args = func_1->num_args(); func_type *new_func = new func_type(return_type, num_args, func_1->has_varargs()); base_symtab *the_symtab = return_type->parent(); for (unsigned arg_num = 0; arg_num < num_args; ++arg_num) { type_node *arg_type = func_1->arg_type(arg_num); the_symtab = joint_symtab(the_symtab, arg_type->parent()); if (the_symtab == NULL) { delete new_func; return NULL; } new_func->set_arg_type(arg_num, arg_type); } return the_symtab->install_type(new_func); } unsigned num_args = func_1->num_args(); if (num_args != func_2->num_args()) return NULL; boolean has_varargs = func_1->has_varargs(); if (has_varargs != func_2->has_varargs()) return NULL; func_type *new_func = new func_type(return_type, num_args, has_varargs); base_symtab *the_symtab = return_type->parent(); for (unsigned arg_num = 0; arg_num < num_args; ++arg_num) { type_node *arg_type = composite(func_1->arg_type(arg_num), func_2->arg_type(arg_num)); if (arg_type == NULL) { delete new_func; return NULL; } the_symtab = joint_symtab(the_symtab, arg_type->parent()); if (the_symtab == NULL) { delete new_func; return NULL; } new_func->set_arg_type(arg_num, arg_type); } return the_symtab->install_type(new_func); } default: return NULL; } } if (is_const) { type_node *new_type = new modifier_type(TYPE_CONST, result); result = result->parent()->install_type(new_type); } if (is_volatile) { type_node *new_type = new modifier_type(TYPE_VOLATILE, result); result = result->parent()->install_type(new_type); } return result; }static void check_function_type(func_type *the_type) { type_node *return_type = the_type->return_type()->unqual(); if (return_type->is_func()) { static boolean warned = FALSE; mistake(&warned, the_type, "functions returning function type are illegal in C"); } if (return_type->is_array()) { static boolean warned = FALSE; mistake(&warned, the_type, "functions returning array type are illegal in C"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -