📄 main.cc
字号:
} else { steptree = new ctree(ctree_addassign); steptree->addchild(ctree_for_for_index(tfor->index())); steptree->addchild(operand_to_tree(tfor->step_op())); } assert(steptree); tree->addchild(steptree); ctree *body_blk = new ctree(ctree_block); body_blk->addchild(process_node_list(tfor->body(), NULL, NULL)); tree->addchild(body_blk); current_continue = old_continue; current_break = old_break; if ((tfor->landing_pad() != NULL) && (!tfor->landing_pad()->is_empty())) { ctree *bound_test = new ctree(comparison_op); bound_test->addchild(operand_to_tree(tfor->lb_op())); bound_test->addchild(operand_to_tree(tfor->ub_op())); ctree *body_tree = new ctree(ctree_semi); body_tree->addchild(process_node_list(tfor->landing_pad(), NULL, NULL)); body_tree->addchild(tree); ctree *then_tree = new ctree(ctree_block); then_tree->addchild(body_tree); ctree *new_if = new ctree(ctree_if); new_if->addchild(bound_test); new_if->addchild(then_tree); tree = new_if; } return tree;}/* * Generate C code for an if */ctree *process_if(tree_if *treeif){ ctree *semi_tree = new ctree(ctree_semi); ctree *conditional_expr = NULL; semi_tree->addchild(process_node_list(treeif->header(), treeif->jumpto(), &conditional_expr)); ctree *tree = new ctree(ctree_if); tree->add_object_comments(treeif); if (conditional_expr == NULL) { conditional_expr = ldc_tree(type_signed, immed(1)); } else { ctree *not_expr = new ctree(ctree_not); not_expr->addchild(conditional_expr); conditional_expr = not_expr; } tree->addchild(conditional_expr); ctree *then_tree = new ctree(ctree_block); then_tree->addchild(process_node_list(treeif->then_part(), NULL, NULL)); tree->addchild(then_tree); semi_tree->addchild(tree); if (!treeif->else_part()->is_empty()) { ctree *else_tree = new ctree(ctree_block); ctree *else_semi = new ctree(ctree_semi); if (treeif->jumpto()->get_annote(k_s2c_label_name_used) != NULL) else_semi->addchild(new ctree(ctree_label, treeif->jumpto())); else_semi->addchild(process_node_list(treeif->else_part(), NULL, NULL)); else_tree->addchild(else_semi); tree->addchild(else_tree); } else { if (treeif->jumpto()->get_annote(k_s2c_label_name_used) != NULL) semi_tree->addchild(new ctree(ctree_label, treeif->jumpto())); } return semi_tree;}/* * Process a nested block */ctree *process_block(tree_block *block){ preprocess_symtab(block->symtab()); ctree *ret = new ctree(ctree_block); ret->add_object_comments(block); ctree *retsemi = new ctree(ctree_semi); ret->addchild(retsemi); add_typedecls(retsemi, block->symtab()); add_vardecls(retsemi, block->symtab()); add_symtab_annotes(retsemi, block->symtab()); add_pound_lines_for_object(block->symtab(), retsemi); retsemi->addchild(process_node_list(block->body(), NULL, NULL)); return ret;}static void add_typedecls(ctree *ret, base_symtab *the_symtab) { assert((ret != NULL) && (the_symtab != NULL)); type_node_list_iter first_iter(the_symtab->types()); while (!first_iter.is_empty()) { type_node *type = first_iter.step(); if (type->annotes()->peek_annote(k_s2c_needs_forward_declaration) != NULL) { ctree *typedecl = new ctree(ctree_typeforward, type); ret->addchild(typedecl); } } type_node_list_iter second_iter(the_symtab->types()); while (!second_iter.is_empty()) { type_node *type = second_iter.step(); if (type->is_named()) { ctree *typedecl = new ctree(ctree_typedecl, type); ret->addchild(typedecl); } } }static void add_vardecls(ctree *ret, base_symtab *the_symtab) { assert((ret != NULL) && (the_symtab != NULL)); sym_node_list_iter sym_iter(the_symtab->symbols()); while (!sym_iter.is_empty()) { sym_node *this_symbol = sym_iter.step(); if (this_symbol->is_var()) { var_sym *the_var = (var_sym *)this_symbol; if (the_var->parent_var() != NULL) continue; if ((!the_var->is_param()) && the_var->is_global() && (!the_var->is_private())) { ctree *the_vardecl = new ctree(ctree_vardecl, the_var); ret->addchild(the_vardecl); } else if ((!the_var->is_param()) && (!the_var->is_global()) && the_var->is_auto()) { ctree *the_vardef = new ctree(ctree_vardef, the_var); ret->addchild(the_vardef); } } else if (this_symbol->is_proc()) { proc_sym *this_proc = (proc_sym *)this_symbol; ctree *the_funcdecl = new ctree(ctree_funcdecl, this_proc); ret->addchild(the_funcdecl); } } var_def_list_iter def_iter(the_symtab->var_defs()); while (!def_iter.is_empty()) { var_def *this_def = def_iter.step(); assert(this_def->variable() != NULL); var_sym *this_var = this_def->variable(); if (this_var->annotes()->peek_annote(k_s2c_one_use) == NULL) { ctree *the_vardef = new ctree(ctree_vardef, this_var); the_vardef->add_object_comments(this_def); ret->addchild(the_vardef); } } }static void add_symtab_annotes(ctree *ret, base_symtab *the_symtab) { if (the_symtab->peek_annote(k_s2c_comments) == NULL) return; char *comment = (the_symtab->is_global() ? (the_symtab->is_file() ? (char *) " file symtab annotes: " : (char *) " global symtab annotes: ") : (char *) " symtab annotes: "); ctree *symtab_annote_tree = new ctree(ctree_blank_line); symtab_annote_tree->add_comment(comment); symtab_annote_tree->add_object_comments(the_symtab); ret->addchild(symtab_annote_tree); ret->addchild(new ctree(ctree_blank_line)); }static boolean operand_is_intconst(operand the_operand, int *the_const) { if (!the_operand.is_expr()) return FALSE; instruction *the_instr = the_operand.instr(); if (the_instr->opcode() != io_ldc) return FALSE; in_ldc *the_ldc = (in_ldc *)the_instr; immed value = the_ldc->value(); if (!value.is_integer()) return FALSE; *the_const = value.integer(); return TRUE; }static ctree *create_goto(label_sym *the_label) { if (the_label == current_break) { return new ctree(ctree_break); } else if (the_label == current_continue) { return new ctree(ctree_continue); } else { the_label->append_annote(k_s2c_label_name_used, the_label); return new ctree(ctree_goto, the_label); } }static ctree *create_branch(label_sym *the_label, ctree *condition) { ctree *the_goto = create_goto(the_label); i_integer value; boolean is_const = condition->is_int_const(&value); if (is_const && (value == 1)) { the_goto->copy_comments_from(condition); delete condition; return the_goto; } ctree *result = new ctree(ctree_if); result->addchild(condition); result->addchild(the_goto); return result; }/* * If a sub-variable occurs as the index of a tree_for, we can't * replace it in the SUIF code, because we need a variable as an * index. So here we deal with it as we're converting to C code. */static ctree *ctree_for_for_index(var_sym *index) { if (index->parent_var() == NULL) return new ctree(ctree_symconst, index); in_rrr *new_copy = new in_rrr(io_cpy, index->type()->unqual(), operand(), operand(index)); instr_no_sub_vars(new_copy); ctree *result = operand_to_tree(operand(new_copy)); delete new_copy; return result; }static void print_pound_lines_for_preamble(suif_object *the_object) { instruction *pound_hanger = NULL; annote_list_iter annote_iter(the_object->annotes()); while (!annote_iter.is_empty()) { annote *this_annote = annote_iter.step(); if (strcmp(this_annote->name(), k_s2c_preamble_pragma) == 0) { if (pound_hanger == NULL) pound_hanger = new in_rrr(io_mrk); immed_list *new_data = new immed_list; immed_list_iter data_iter(this_annote->immeds()); while (!data_iter.is_empty()) { immed this_immed = data_iter.step(); new_data->append(this_immed); } pound_hanger->append_annote(k_s2c_pragma, new_data); } } if (pound_hanger != NULL) { ctree *dummy_ctree = new ctree(ctree_semi); add_pound_lines_for_object(pound_hanger, dummy_ctree); file_io the_io(c_file); transform_and_print_ctree(&the_io, dummy_ctree); delete dummy_ctree; delete pound_hanger; } }static void add_pound_lines_for_object(suif_object *the_object, ctree *parent_ctree) { annote_list_iter first_iter(the_object->annotes()); while (!first_iter.is_empty()) { annote *this_annote = first_iter.step(); if (strcmp(this_annote->name(), k_s2c_pragma) == 0) { this_annote->immeds()->push("pragma"); this_annote->set_name(k_s2c_pound_line); } } annote_list_iter annote_iter(the_object->annotes()); while (!annote_iter.is_empty()) { annote *this_annote = annote_iter.step(); if (strcmp(this_annote->name(), k_s2c_pound_line) == 0) { char *pound_string; string_io *the_io = new string_io(£_string); immed_list *data = this_annote->immeds(); immed_list_iter data_iter(data); boolean first = TRUE; while (!data_iter.is_empty()) { if (first) first = FALSE; else 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_pound_line = in_pound_line; in_pound_line = 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_pound_line = old_in_pound_line; break; } case im_undef: the_io->printf("?"); break; defaul
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -