📄 stmt.c
字号:
# include "ansi_parse.h"# include "control.h"# include "lif.h" static char sccsid[] = "@(#)stmt.c 1.9 8/11/95";list_ptr returns = NULL;static int max_proc = 0;static proc_ptr procs = NULL;static int pid = 0;label_ptr labels = NULL;label_ptr find_label(label) char *label;{ label_ptr at; at = labels; while (at){ if (strcmp (label,at->label)== 0) return at; at = at -> next; } return NULL;}label_def (label_token,node) token_ptr label_token; int node;{ label_ptr new; list_ptr list; new = find_label (label_token->text); if (new){ new->node = node; list = new -> gotos; while (list){ connect_nodes (list->node,node); list = list -> next; } new -> gotos = NULL; } else { new = alloc_label(); new -> next = labels; labels = new; new->label = label_token->text; new->node = node; new->gotos = NULL; }}label_ref (label_token,goto_node) token_ptr label_token; int goto_node;{ label_ptr new; list_ptr list; new = find_label (label_token->text); if (new){ if (new->node) connect_nodes (goto_node,new->node); else { list = alloc_list(); list -> node = goto_node; list -> next = new -> gotos; new -> gotos = list; } } else { new = alloc_label(); new -> next = labels; labels = new; new->label = label_token->text; new->node = 0; new->gotos = alloc_list(); new->gotos->node = goto_node; new->gotos->next = NULL; }}int current_proc_id() { return pid;}output_proc_list(t_file) FILE *t_file;{ int n_procs = 0; proc_ptr p; p = procs; while (p){n_procs++; p = p -> next; } fprintf(t_file,"%d %d\n",n_procs,stmtno); p = procs; while (p){ fprintf (t_file,"%3d %4d %4d %4d %c %s\n", p->proc_id,p->entry_node, p->exit_node,p->n_locals, p->static_or_extern,p->proc->text); p = p -> next; }}list_procs(){ proc_ptr p; printf ("\nProcedures\n"); p = procs; while (p){ printf ("%2d %4d %s\n", p->proc_id,p->entry_node,p->proc->text); p = p -> next; }}proc_ptr find_proc (t) token_ptr t;{ proc_ptr p; if (!t) return NULL; p = procs; while (p){ if (0 == strcmp (t->text,p->proc->text)){ return p; } p = p->next; } return NULL;}add_proc (t,node,pid) token_ptr t; int node,pid;{ proc_ptr p,q; if (!t) return; p = procs; while (p){ if (0 == strcmp (t->text,p->proc->text)){ if ((p->entry_node == -1) && (node != -1)) { p->entry_node = node; current_proc = p; } return; } p = p->next; } p = alloc_proc(); p -> next = NULL; p -> proc = t; p -> proc_id = pid; p -> entry_node = node; p -> exit_node = 0; p -> n_locals = 0; p -> static_or_extern = 'X'; p -> has_return_xpr = 0; q = procs; if (q){ while (q->next) q = q->next; q -> next = p; } else procs = p; if (node != -1) current_proc = p;}int call_to(t) token_ptr t;{ proc_ptr p; if (!t) return 0; p = find_proc(t); if (!p){ add_proc (t,-1,++max_proc); return max_proc; } return p->proc_id;}int get_stmt_no (){ return ++stmtno;}connect_nodes (from,to) int from,to;{ fprintf (outfile,"%d(%d,%d)",LIF_SUCC,from,to); if (z_opt) fprintf (outfile," connect from %d to %d",from,to); fprintf (outfile,"\n");}source_map (n,from,to) int n; token_ptr from,to;{ /* printf ("source map %d %d \n",from,to); */ if (from && to){ /* printf ("tokens: (%s) (%s)\n",from->text,to->text); */ fprintf (outfile,"%d(%d,%d,%d,%d,%d)",LIF_SOURCE_MAP,n, from->at.line_start,from->at.col_start, to->at.line_end,to->at.col_end); if (z_opt){ fprintf (outfile," source for node %d",n); if (from->at.line_start == to->at.line_start) fprintf (outfile," line %d cols %d-%d", from->at.line_start,from->at.col_start, to->at.col_end); else fprintf (outfile," line %d col %d to line %d col %d", from->at.line_start,from->at.col_start, to->at.line_end,to->at.col_end); } fprintf (outfile,"\n"); }}stmt_ptr make_stmt (from,to,entry_node,exit_node) int from,to,entry_node,exit_node;{ stmt_ptr new; new = alloc_stmt(); new -> from = from; new -> to = to; new -> entry = entry_node; new -> exit = exit_node; new -> breaks = NULL; new -> cases = NULL; new -> continues = NULL; new -> is_jump = 0; new -> next = NULL; new -> last = new;; return new;}static merge_bc (from,to) stmt_ptr from,to;{ list_ptr nodes; if (from->breaks){ nodes = from->breaks; while (nodes->next) nodes = nodes->next; nodes -> next = to -> breaks; } else from->breaks = to -> breaks; if (from->continues){ nodes = from->continues; while (nodes->next) nodes = nodes->next; nodes -> next = to -> continues; } else from->continues = to -> continues; if (from->cases){ nodes = from->cases; while (nodes->next) nodes = nodes->next; nodes -> next = to -> cases; } else from->cases = to -> cases;}stmt_ptr join_stmt (from,to) stmt_ptr from,to;{ list_ptr c; if (!from) return to; if (!to) return from; if (from->is_jump && to->cases && from->cases){ c = from->cases; while (c){ c->is_marked = True; c = c -> next; } /* from->cases = NULL; */ } c = from->cases; while (c){ if(!c->is_marked) gen_require (c->node,to->entry,to->entry); c = c -> next; } if ((!from->is_jump) && to->cases && from->cases){ c = from->cases; while (c){ c->is_marked = True; c = c -> next; } } if (!from->is_jump)connect_nodes (from->exit,to->entry); from->exit = to->exit; /* from->to = to->to; */ from->is_jump = to->is_jump; from->last->next = to; from->last = to; to->next = NULL; merge_bc (from,to); return from;}gen_require (node_required,from,to) int node_required,from,to;{ if ((node_required == from) && (node_required == to)) return; fprintf (outfile,"%d(%d,%d",LIF_REQUIRES,node_required,from); if (from != to) fprintf (outfile,",%d",to); fprintf (outfile,")"); if(z_opt) fprintf (outfile," nodes %d-%d require node %d", from,to,node_required); fprintf (outfile,"\n");}require_stmt (node_required,stmt) int node_required; stmt_ptr stmt;{ stmt_ptr at; int start,end,try; if (!stmt) return; at = stmt; start = at->from; end = at->to; at = at->next; while (at){ try = at->from; if (end + 1 == try){ } else { gen_require (node_required,start,end); start = at->from; } end = at->to; at = at -> next; } gen_require (node_required,start,end);}stmt_ptr stmt_label (label_token,stmt) token_ptr label_token; stmt_ptr stmt;{ stmt_ptr new; int node; node = get_stmt_no(); source_map (node,label_token,label_token); new = make_stmt (node,node,node,node); label_def (label_token,node); connect_nodes (node,stmt->entry); stmt->to = node; stmt->entry = node; return stmt;}stmt_ptr goto_stmt (continue_token,label_token,semi_token) token_ptr continue_token,semi_token,label_token;{ stmt_ptr new; int node; node = get_stmt_no(); source_map (node,continue_token,semi_token); new = make_stmt (node,node,node,node); new -> is_jump = True; fprintf (outfile,"%d(%d,%s)",LIF_GOTO,node,"G"); if(z_opt)fprintf (outfile," go to %s",label_token->text); fprintf (outfile,"\n"); label_ref (label_token,node); return new;}stmt_ptr continue_stmt (continue_token,semi_token) token_ptr continue_token,semi_token;{ stmt_ptr new; int node; list_ptr list; node = get_stmt_no(); source_map (node,continue_token,semi_token); new = make_stmt (node,node,node,node); list = alloc_list(); list -> node = node; list -> next = NULL; new -> continues = list; new -> is_jump = True; fprintf (outfile,"%d(%d,%s)%s",LIF_GOTO,node,"C", z_opt?" continue\n":"\n"); return new;}stmt_ptr break_stmt (break_token,semi_token) token_ptr break_token,semi_token;{ stmt_ptr new; int node; list_ptr list; node = get_stmt_no(); source_map (node,break_token,semi_token); new = make_stmt (node,node,node,node); list = alloc_list(); list -> node = node; list -> next = NULL; new -> breaks = list; new -> is_jump = True; fprintf (outfile,"%d(%d,%s)%s",LIF_GOTO,node,"B", z_opt?" break\n":"\n"); return new;}stmt_ptr brace_stmt (left_token,stmt,right_token) token_ptr left_token,right_token; stmt_ptr stmt;{ int left,right; left = get_stmt_no (); right = get_stmt_no (); source_map (left,left_token,left_token); source_map (right,right_token,right_token); if (!stmt) { connect_nodes (left,right); stmt = make_stmt (left,right,left,right); } else { connect_nodes (left,stmt->entry); if (!stmt->is_jump)connect_nodes (stmt->exit,right); stmt->entry = left; stmt->exit = right; require_stmt(left,stmt); stmt->to = right; stmt->from = right; } gen_require (right,left,left); return stmt;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -