📄 xpr.c
字号:
# include <stdio.h># include "ansi_parse.h"# include "lif.h"# include <string.h># include <malloc.h> static char sccsid[] = "@(#)xpr.c 1.4 4/26/95";# define MAX_MALLOC 100static int max_addr = 0;addr_tab_ptr addr_tab = NULL;int n_malloc = 0;char *malloc_funs[MAX_MALLOC];int current_addr(){ return max_addr;}int is_malloc(name) char *name;{ int i; for (i = 0; i < n_malloc; i++) if (strcmp(name,malloc_funs[i]) == 0) return 1; return 0;}print_malloc(){ int i; for (i = 0; i < n_malloc; i++) printf ("\t%s\n",malloc_funs[i]);}add_malloc(name) char *name;{ malloc_funs[n_malloc++] = name;}addr_tab_ptr addr_of (t) token_ptr t;{ var_ste_ptr var; int scope; addr_tab_ptr new; if (!t) return NULL; var = look_up_id ((var_ste_ptr)NULL,t->text,&scope); if (!var) return 0; if (var -> addr == NULL){ new = alloc_addr_tab(); new -> addr = ++max_addr; new->next = addr_tab; addr_tab = new; new->proc_id = scope > 1?current_proc_id():0; new->var_id = var->id; var -> addr = new; fprintf (outfile,"%d(%d,%d,%d)",LIF_ADDRESS, new->addr,new->proc_id,new->var_id); if (z_opt){ fprintf (outfile, " address %d is var %d (%s) of proc %d", new->addr,new->var_id, t->text,new->proc_id); } fprintf (outfile,"\n"); } return var->addr;}make_malloc(node_id,t) int node_id; tree_ptr t;{ token_ptr var_token; addr_tab_ptr addr; var_token = fake_var_decl(t?t->token:NULL); addr = addr_of (var_token); if(addr){ fprintf (outfile,"%d(%d,%d)",LIF_AREF, node_id,addr->addr); if (z_opt){ fprintf (outfile, " address %d refed at node %d", addr->addr,node_id); } fprintf (outfile,"\n"); }}tree_ptr make_leaf(code,token) token_ptr token; int code;{ tree_ptr new; new = alloc_tree(); new->left = NULL; new->right = NULL; new->op_code = code; new->token = token; return new;}tree_ptr make_tree (code,left,right) tree_ptr left,right; int code;{ tree_ptr new; new = alloc_tree(); new->left = left; new->right = right; new->op_code = code; new->token = NULL; return new;}print_tree (t) tree_ptr t;{ if (!x_opt) return; printf ("\nprint expression tree for line %d\n",lineno); print_trees(t); printf("\n");}static print_trees (t) tree_ptr t;{ static char *ops[] = { "!?","-","+","->","*",".","&","f()","--","++","CAST","[]","=", "+=","ID","K","{","}"}; if (t){ switch (t->op_code){ case UN_OP: case ADDR_OP: case DEREF_OP: case PRE_OP: printf ("(%s",ops[t->op_code]); print_trees(t->left); printf (")"); break; case POST_OP: printf ("("); print_trees(t->left); printf ("%s",ops[t->op_code]); printf (")"); break; case CALL_OP: print_trees(t->left); printf ("("); print_trees(t->right); printf (")"); break; case COMMA_OP: print_trees(t->left); printf (","); print_trees(t->right); break; case CAST_OP: case BIN_OP: case POINTER_OP: case DOT_OP: case ASGN_OP: case RELASGN_OP: printf ("("); print_trees(t->left); printf ("%s",ops[t->op_code]); print_trees(t->right); printf (")"); break; case ARRAY_OP: print_trees(t->left); printf ("["); print_trees(t->right); printf ("]"); break; case ID_OP: printf("%s",t->token->text); break; case CON_OP: printf("K"); break; case LEFTP_OP: case RIGHTP_OP: print_trees(t->left); print_trees(t->right); break; default: print_trees(t->left); print_trees(t->right); break; } }}# define REF 0# define DEF 1cref_def (is_def,node,chain) int is_def,node,chain;{ int code; if (is_def) code = LIF_CDEF; else code = LIF_CREF; fprintf (outfile,"%d(%d,%d)",code,node,chain); if (z_opt) fprintf (outfile, " %s to chain (%d) at stmt %d ", is_def?"def":"ref", chain,node); fprintf (outfile,"\n");}ref_def_id (is_def,node,id,scope) int is_def,node,id,scope;{ int code,level; if (is_def){ if (scope > 1) code = LIF_DEF; else code = LIF_GDEF; } else { if (scope > 1) code = LIF_REF; else code = LIF_GREF; } level = 0; fprintf (outfile,"%d(%d,%d",code,node,id); if (level)fprintf(outfile,",%d",level); fprintf (outfile,")"); if (z_opt) fprintf (outfile, " %s %s to (%d) at stmt %d ", scope > 1?"local":"global", is_def?"def":"ref", id,node); fprintf (outfile,"\n");}ref_def_var (is_def,node,t,used_as_array) int is_def,node,used_as_array; token_ptr t;{ int scope,code,id,level; var_ste_ptr var; addr_tab_ptr addr; /* printf ("ref def is(%d) node (%d) t (%d)\n",is_def,node,t); */ if (!t) return; /* empty */ var = look_up_id ((var_ste_ptr)NULL,t->text,&scope); if (is_var_array(var) && (!used_as_array)){ /* printf ("var %s is an array %s\n",t->text, used_as_array?"with subscript":"address"); */ addr = addr_of (t); if(addr){ fprintf (outfile,"%d(%d,%d)",LIF_AREF, node,addr->addr); if (z_opt){ fprintf (outfile, " array address %d refed at node %d", addr->addr,node); } fprintf (outfile,"\n"); } } else { if (var){ id = var->id; if (is_def){ if (scope > 1) code = LIF_DEF; else code = LIF_GDEF; } else { if (scope > 1) code = LIF_REF; else code = LIF_GREF; } level = t->deref_level; } else return; /* oops */ fprintf (outfile,"%d(%d,%d",code,node,id); if (level)fprintf(outfile,",%d",level); fprintf (outfile,")"); if (z_opt) fprintf (outfile, " %s %s to %s (%d) at stmt %d on line %d", scope > 1?"local":"global", is_def?"def":"ref", t->text,id,node,t->at.line_start); fprintf (outfile,"\n"); }}token_ptr leftmost (t) tree_ptr t;{ token_ptr left,right; if (t){ right = leftmost (t->right); if (t->op_code == COMMA_OP) left = right; else left = leftmost (t->left); if (!t->token) { if (!left) return right; if (!right) return left; if (left->at.line_start*1000+left->at.col_start < right->at.line_start*1000+right->at.col_start) return left; else return right; } else { if (!left && !right ) return t->token; else if (left){ if (left->at.line_start*1000+left->at.col_start < t->token->at.line_start*1000+t->token->at.col_start) return left; else return t->token; } else { return t->token; } } } return NULL;}static token_ptr rightmost (t) tree_ptr t;{ token_ptr left,right; if (t){ right = rightmost (t->right); if (t->op_code == COMMA_OP) left = right; else left = rightmost (t->left); if (!t->token) { if (!left) return right; if (!right) return left; if (left->at.line_end*1000+left->at.col_end < right->at.line_end*1000+right->at.col_end) return right; else return left; } else { if (!left && !right ) return t->token; else if (left){ if (left->at.line_end*1000+left->at.col_end > t->token->at.line_end*1000+t->token->at.col_end) return left; else return t->token; } else { return t->token; } } } return NULL;}# define ID_LVALUE 1# define PTR_LVALUE 2static int get_lvalue (t,tk,chain_id,array_seen) tree_ptr t; token_ptr *tk; int *chain_id; int *array_seen;{ int lvalue,scope; var_ste_ptr var; char name[5000]; token_ptr new,left,right; chain_rec c; if (t){ /* printf ("get lvalue op(%d)\n",t->op_code); */ switch (t->op_code){ case ID_OP: t->token->deref_level = 0; *tk = t->token; /* if (*tk) printf ("var (%s)\n",t->token->text); */ return ID_LVALUE; case DEREF_OP: lvalue = get_lvalue(t->left,tk,chain_id,array_seen); if (lvalue == ID_LVALUE) (*tk)->deref_level++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -