📄 main.cc
字号:
/* file "main.cc" *//* Copyright (c) 1994 Stanford University All rights reserved. This software is provided under the terms described in the "suif_copyright.h" include file. */#include <suif_copyright.h>/* * SUIF to C converter * * R. French * T. Smith * C. Wilson */#include "s2c.h"#include <time.h>#include <string.h>int numindent = 0; /* Number of spaces to indent */boolean in_comment = FALSE;boolean in_pound_line = FALSE;boolean log_simp = FALSE;boolean write_pseudo = FALSE;boolean keep_casts = FALSE;boolean omit_header = FALSE;boolean no_warn = FALSE;boolean always_intnum = FALSE;boolean array_exprs = FALSE;boolean all_annotes_to_comments = FALSE;boolean gcc_bug_flag = FALSE;boolean drop_bounds = FALSE;boolean ll_suffix = FALSE;boolean explicit_zero_init = FALSE;boolean limit_escape_sequences = FALSE;boolean fill_field_space_with_char_arrays = FALSE;char **show_annote_names = NULL;boolean show_annote_opcodes[io_last];boolean show_annote_places[ap_last];FILE *c_file = stdout;static label_sym *current_break = NULL;static label_sym *current_continue = NULL;char *k_s2c_label_name_used;char *k_s2c_needs_forward_declaration;char *k_s2c_comments;char *k_s2c_genop_format;char *k_s2c_one_use;char *k_s2c_multi_use;char *k_s2c_init_field;char *k_s2c_original_field_names;char *k_s2c_pragma;char *k_s2c_preamble_pragma;char *k_s2c_pound_line;char *k_builtin_args;char *reserved_words[] = { "auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned", "void", "volatile", "while", NULL };macro_op_data macro_table[] = { {io_min, "__suif_min", "((x)<(y)?(x):(y))", 2, FALSE}, {io_max, "__suif_max", "((x)>(y)?(x):(y))", 2, FALSE}, {io_abs, "__suif_abs", "((x)<0?-(x):(x))", 1, FALSE}, {io_divfloor, "__suif_divfloor", "(((x)<0)^((y)<0) ? ((x) < 0 ? ((x)-(y)+1)/(y) : ((x)-(y)-1)/(y)) : " "(x)/(y))", 2, FALSE}, {io_divceil, "__suif_divceil", "(((x)<0)^((y)<0) ? (x)/(y) : " "((x) < 0 ? ((x)+(y)+1)/(y) : ((x)+(y)-1)/(y)))", 2, FALSE}, {io_rot, "__suif_rotate", "((x << y) | (x >> (sizeof(x) * %d - y)))", 2, FALSE}, {io_mod, "__suif_mod", "((((x) % (y)) < 0) ? " "(((y) < 0) ? (((x) % (y)) - (y)) : (((x) % (y)) + (y))) : ((x) % (y)))", 2, FALSE}, {io_mrk, NULL, NULL, 0, FALSE} };static char *place_names[] = {#define place2(x, y) #x,#define place(x) place2(x, x)#include "places.h"#undef place#undef place2 NULL };/* * Now set up the output C style parameters. */#include "style_defaults.h"c_style_type c_style = { DEFAULT_STYLE_SPACE_AROUND_BINOPS, DEFAULT_STYLE_SPACE_AROUND_ASSIGNMENTS, DEFAULT_STYLE_SPACE_AFTER_COMMAS, DEFAULT_STYLE_NEW_LINE_FOR_OPEN_BRACE, DEFAULT_STYLE_NEW_LINE_FOR_ELSE, DEFAULT_STYLE_INDENT_SINGLE_STATEMENT_BODIES, DEFAULT_STYLE_STATEMENT_INDENT, DEFAULT_STYLE_SINGLE_STATEMENT_BODY_INDENT, DEFAULT_STYLE_BRACE_INDENT, DEFAULT_STYLE_PARAM_INDENT, DEFAULT_STYLE_LABEL_INDENT, DEFAULT_STYLE_EXTERN_ON_FUNC_DECL, DEFAULT_STYLE_MAX_COMMA_ITEMS_PER_LINE };static void parse_arguments(int argc, char *argv[], char **input_filespec, char **output_filespec);static void usage(void);static void initialize_macro_table(void);static void read_global_annotations(global_symtab *the_globals);static void print_header(void);static void add_typedecls(ctree *ret, base_symtab *the_symtab);static void add_vardecls(ctree *ret, base_symtab *the_symtab);static void add_symtab_annotes(ctree *ret, base_symtab *the_symtab);static boolean operand_is_intconst(operand the_operand, int *the_const);static ctree *create_goto(label_sym *the_label);static ctree *create_branch(label_sym *the_label, ctree *condition);static ctree *ctree_for_for_index(var_sym *index);static void print_pound_lines_for_preamble(suif_object *the_object);static void add_pound_lines_for_object(suif_object *the_object, ctree *parent_ctree);static void fix_macro_name(macro_op_data *the_macro);static boolean is_interfile_name(char *the_name);static void fix_relational_op(in_rrr *the_rrr);static boolean is_null_pointer_const(operand the_op);FILE *open_c_file(char *name){ FILE *f = fopen(name, "w"); if (f == NULL) { fprintf(stderr, "Could not open %s for writing.\n", name); exit(1); } return f;}extern int main(int argc, char **argv){ start_suif(argc, argv); k_s2c_label_name_used = lexicon->enter("s2c label name used")->sp; ANNOTE(k_s2c_needs_forward_declaration, "s2c needs forward declaration", FALSE); ANNOTE(k_s2c_comments, "s2c comments", TRUE); ANNOTE(k_s2c_genop_format, "s2c genop format", TRUE); ANNOTE(k_s2c_one_use, "s2c one use", FALSE); ANNOTE(k_s2c_multi_use, "s2c multi use", FALSE); ANNOTE(k_s2c_init_field, "s2c init field", FALSE); ANNOTE(k_s2c_original_field_names, "s2c original field names", FALSE); ANNOTE(k_s2c_pragma, "s2c pragma", TRUE); ANNOTE(k_s2c_preamble_pragma, "s2c preamble pragma", TRUE); ANNOTE(k_s2c_pound_line, "s2c pound line", TRUE); ANNOTE(k_builtin_args, "builtin args", TRUE); init_group_layout(); init_initialization(); char *input_filespec, *output_filespec; parse_arguments(argc, argv, &input_filespec, &output_filespec); assert(input_filespec != NULL); fileset->add_file(input_filespec, NULL); if (always_intnum) { for (int type_num = 0; type_num < num_C_types; ++type_num) target.size[type_num] = 0; type_signed = fileset->globals()->install_type(new base_type(TYPE_INT, 0, TRUE)); } if (output_filespec != NULL) c_file = open_c_file(output_filespec); setbuf(c_file, NULL); initialize_macro_table(); // only one file; no need to loop through --- just get it fileset->reset_iter(); file_set_entry *fse = fileset->next_file(); read_global_annotations(fileset->globals()); pass1_on_symtab(fileset->globals()); pass1_on_symtab(fse->symtab()); fse->reset_proc_iter(); while (TRUE) { proc_sym *this_proc = fse->next_proc(); if (this_proc == NULL) break; assert(!this_proc->is_in_memory()); this_proc->read_proc(); pass1_on_proc(this_proc->block()); this_proc->flush_proc(); } print_header(); ctree *globals_tree = new ctree(ctree_semi); comment_object(fse); if (fse->peek_annote(k_s2c_comments) != NULL) { ctree *annote_tree = new ctree(ctree_blank_line); annote_tree->add_comment(" file set entry annotes: "); annote_tree->add_object_comments(fse); globals_tree->addchild(annote_tree); globals_tree->addchild(new ctree(ctree_blank_line)); } process_globals(globals_tree, fileset->globals()); print_pound_lines_for_preamble(fileset->globals()); process_globals(globals_tree, fse->symtab()); add_pound_lines_for_object(fse, globals_tree); file_io the_io(c_file); transform_and_print_ctree(&the_io, globals_tree); delete globals_tree; fse->reset_proc_iter(); while (TRUE) { proc_sym *this_proc = fse->next_proc(); if (this_proc == NULL) break; assert(!this_proc->is_in_memory()); this_proc->read_proc(); ctree *c = process_proc(this_proc); fprintf(c_file, "\n"); transform_and_print_ctree(&the_io, c); delete c; this_proc->flush_proc(); } exit_suif(); exit(0); return 0;}static void parse_arguments(int argc, char *argv[], char **input_filespec, char **output_filespec) { static char **annote_name_strings = new char *[argc]; static char **annote_opcode_strings = new char *[argc]; static char **annote_place_strings = new char *[argc]; cmd_line_option option_table[] = { {CLO_NOARG, "-logic-simp", NULL, &log_simp}, {CLO_NOARG, "-pseudo", NULL, &write_pseudo}, {CLO_NOARG, "-keep-casts", NULL, &keep_casts}, {CLO_NOARG, "-omit-header", NULL, &omit_header}, {CLO_NOARG, "-no-warn", NULL, &no_warn}, {CLO_NOARG, "-always-intnum", NULL, &always_intnum}, {CLO_NOARG, "-array-exprs", NULL, &array_exprs}, {CLO_NOARG, "-annotes-all", NULL, &all_annotes_to_comments}, {CLO_MULTI_STRING, "-annotes-named", NULL, &annote_name_strings}, {CLO_MULTI_STRING, "-annotes-opcode", NULL, &annote_opcode_strings}, {CLO_MULTI_STRING, "-annotes-object-kind", NULL, &annote_place_strings}, {CLO_NOARG, "-gcc-bug", NULL, &gcc_bug_flag}, {CLO_NOARG, "-drop-bounds", NULL, &drop_bounds}, {CLO_NOARG, "-ll-suffix", NULL, &ll_suffix}, {CLO_NOARG, "-explicit-zero-init", NULL, &explicit_zero_init}, {CLO_NOARG, "-limit-escape-sequences", NULL, &limit_escape_sequences}, {CLO_NOARG, "-fill-field-space-with-char-arrays", NULL, &fill_field_space_with_char_arrays} }; assert((input_filespec != NULL) && (output_filespec != NULL)); parse_cmd_line(argc, argv, option_table, sizeof(option_table) / sizeof(cmd_line_option)); if (annote_name_strings[0] != NULL) { int num_strings = 0; while (annote_name_strings[num_strings] != NULL) ++num_strings; show_annote_names = new char *[num_strings + 1]; for (int string_num = 0; string_num < num_strings; ++string_num) show_annote_names[string_num] = annote_name_strings[string_num]; show_annote_names[num_strings] = NULL; } int string_num; for (string_num = 0; annote_opcode_strings[string_num] != NULL; ++string_num) { char *this_name = annote_opcode_strings[string_num]; int opcode; for (opcode = 0; opcode < io_last; ++opcode) { if (strcmp(if_ops_name((enum if_ops)opcode), this_name) == 0) break; } if (opcode == io_last) { error_line(1, NULL, "\"%s\" is not a valid opcode name", this_name); } show_annote_opcodes[opcode] = TRUE; } for (string_num = 0; annote_place_strings[string_num] != NULL; ++string_num) { char *this_name = annote_place_strings[string_num]; int place_num; for (place_num = 0; place_num < ap_last; ++place_num) { if (strcmp(place_names[place_num], this_name) == 0) break; } if (place_num == ap_last) { error_line(1, NULL, "\"%s\" is not a valid object kind", this_name); } show_annote_places[place_num] = TRUE; if (place_num == ap_nodes) { show_annote_places[ap_loops] = TRUE; show_annote_places[ap_fors] = TRUE; show_annote_places[ap_ifs] = TRUE; show_annote_places[ap_blocks] = TRUE; } if (place_num == ap_all_syms) { show_annote_places[ap_vars] = TRUE; show_annote_places[ap_proc_syms] = TRUE; show_annote_places[ap_labels] = TRUE; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -