📄 xpr.c
字号:
return lvalue; case COMMA_OP: return NULL; case UN_OP: case LEFTP_OP: case RIGHTP_OP: return get_lvalue(t->left,tk,chain_id,array_seen); case DOT_OP: name[0] = '\0'; c.chain_id = -1; c.no_output = 0; assemble_dot (name,t,&c); if (c.chain_id != -1){ var = c.fields; if(x_opt)printf ("\n\nField search\n"); if(x_opt)while (var){ printf ("\t%s\n",var->token->text); var = var -> next; } var = look_up_id (c.fields,name,&scope); if (!var) printf ("%s not member at %d\n",name,lineno); if (!var) return NULL; ++c.current_seq; fprintf (outfile,"%d(%d,%d,%d,%s)", LIF_FIELD,c.chain_id,c.current_seq, var->id,name); if (z_opt)fprintf(outfile, " field %d (%s) of chain %d", c.current_seq,var->token->text,c.chain_id); fprintf (outfile,"\n"); *chain_id = c.chain_id; if (*chain_id) return PTR_LVALUE; return NULL; } /* printf ("DOT found: (%s)\n",name); */ new = alloc_token(); new->text = find_string(name); left = leftmost(t); right = rightmost(t); new -> at.line_start = left -> at.line_start; new -> at.col_start = left -> at.col_start; new -> at.line_end = right -> at.line_end; new -> at.col_end = right -> at.col_end; *tk = new; return ID_LVALUE; case POINTER_OP: *chain_id = get_chain(t); if (*chain_id) return PTR_LVALUE; return NULL; case ARRAY_OP: *array_seen += 1; return get_lvalue(t->left,tk,chain_id,array_seen); case BIN_OP: case PRE_OP: case POST_OP: case RELASGN_OP: case ASGN_OP: case CALL_OP: case ADDR_OP: case CAST_OP: case CON_OP: default: break; } } return NULL;}static scan_tree (t,node_id,from_node,upto_node,to_node) tree_ptr t; int node_id; int *from_node,*to_node,*upto_node;{ static tree_ptr recent_typename = NULL; int new_from,new_to,new_upto; int lvalue,new_node; token_ptr tk; int chain_id,proc_id; int is_array = 0; addr_tab_ptr addr; if (t){ /* printf ("scan %d op %d left %d right %d\n",t, t->op_code,t->left,t->right); */ switch (t->op_code){ case CON_OP: case ID_OP: return; case COMMA_OP: new_node = get_stmt_no (); /* printf ("split %d from %d\n",new_node,node_id); printf ("node %d from %d upto %d to %d\n", node_id,*from_node,*upto_node,*to_node); */ source_map(new_node,leftmost(t->left),rightmost(t->left)); lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,new_node,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,new_node,chain_id); if(t->left){ new_from = 0; new_to = 0; new_upto = 0; scan_tree (t->left,new_node, &new_from,&new_upto,&new_to); /* printf ("new %d from %d upto %d to %d ", new_node,new_from,new_upto,new_to); */ if (!new_from) new_from = new_node; if (!new_upto) new_upto = new_node; else connect_nodes (new_upto,new_node); if (!new_to) new_to = new_node; if (*from_node) connect_nodes (new_to,*from_node); *from_node = new_from; if(!(*upto_node)) *upto_node = new_to; } /* printf ("node %d from %d upto %d to %d\n", node_id,*from_node,*upto_node,*to_node); */ lvalue = get_lvalue (t->right,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); return; case LEFTP_OP: case RIGHTP_OP: if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); return; case ARG_OP: case BIN_OP: lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); lvalue = get_lvalue (t->right,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); return; case ASGN_OP: is_array = 0; lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (DEF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(DEF,node_id,chain_id); if (is_array){ if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); } lvalue = get_lvalue (t->right,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); return; case UN_OP: /* lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); printf ("un_op: lvalue %d\n",lvalue); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); */ if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); return; case PRE_OP: new_node = get_stmt_no (); if (*upto_node){ connect_nodes (*upto_node,new_node); } else { *from_node = new_node; } *upto_node = new_node; source_map(new_node,leftmost(t),rightmost(t->left)); lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE){ ref_def_var (REF,new_node,tk,is_array); ref_def_var (DEF,new_node,tk,is_array); ref_def_var (REF,node_id,tk,is_array); } else if (lvalue == PTR_LVALUE){ cref_def(REF,node_id,chain_id); cref_def(REF,new_node,chain_id); cref_def(DEF,new_node,chain_id); } if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); return; case POST_OP: new_node = get_stmt_no (); /* printf ("split (post) %d from %d\n",new_node,node_id); */ if (*to_node == 0)connect_nodes (node_id,new_node); else connect_nodes (*to_node,new_node); *to_node = new_node; source_map(new_node,leftmost(t->left),rightmost(t)); lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE){ ref_def_var (REF,new_node,tk,is_array); ref_def_var (DEF,new_node,tk,is_array); ref_def_var (REF,node_id,tk,is_array); } else if (lvalue == PTR_LVALUE){ cref_def(REF,new_node,chain_id); cref_def(DEF,new_node,chain_id); cref_def(REF,node_id,chain_id); } if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); return; case RELASGN_OP: lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE){ ref_def_var (DEF,node_id,tk,is_array); ref_def_var (REF,node_id,tk,is_array); } else if (lvalue == PTR_LVALUE){ cref_def(REF,node_id,chain_id); cref_def(DEF,node_id,chain_id); } lvalue = get_lvalue (t->right,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); return; case ADDR_OP: lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); if ((lvalue == ID_LVALUE)&&tk){ addr = addr_of (tk); 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"); } } if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); return; case TYPENAME_OP: recent_typename = t; return; case CAST_OP: case ARRAY_OP: lvalue = get_lvalue (t->right,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); recent_typename = NULL; return; case CALL_OP: lvalue = get_lvalue (t->left,&tk,&chain_id,&is_array); /* printf ("call lvalue %d\n",lvalue); */ if (lvalue == ID_LVALUE){ proc_id = call_to(tk); /* printf ("call %s proc id %d\n",tk->text,proc_id); */ if (!proc_id) return; if (is_malloc(tk->text)){ make_malloc(node_id,recent_typename); } fprintf (outfile,"%d(%d,%d)",LIF_CALL_START, node_id,proc_id); if(z_opt){ fprintf (outfile," call to %s at %d", tk->text,node_id); } fprintf (outfile,"\n"); assemble_actuals (t->right,node_id, from_node,upto_node,to_node); fprintf (outfile,"%d",LIF_CALL_END); if(z_opt) fprintf (outfile," end call to %s at %d", tk->text,node_id); fprintf (outfile,"\n"); } return; case DEREF_OP: if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); return; case DOT_OP: case POINTER_OP: default: if(t->left) scan_tree (t->left,node_id,from_node,upto_node,to_node); if(t->right) scan_tree (t->right,node_id, from_node,upto_node,to_node); break; } } return;}make_arg(t,node_id,from,upto,to) tree_ptr t; int node_id; int *from,*to,*upto;{ token_ptr tk; int lvalue; int chain_id; int is_array = 0; lvalue = get_lvalue (t,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); scan_tree (t,node_id,from,upto,to);}assemble_actuals (t,node_id,from,upto,to) tree_ptr t; int node_id; int *from,*to,*upto;{ if (!t) return; if (t->op_code == ARG_OP){ assemble_actuals (t->left,node_id,from,upto,to); fprintf(outfile,"%d%s\n",LIF_ACTUAL_SEP,z_opt? " Actual seperator":""); make_arg (t->right,node_id,from,upto,to); } else make_arg (t,node_id,from,upto,to);}int xpr_gen (t,from,to) tree_ptr t; int *from,*to;{ token_ptr tk; int lvalue; int node_id,upto; int chain_id; int is_array; *from = *to = 0; node_id = get_stmt_no (); print_tree(t); upto = 0; lvalue = get_lvalue (t,&tk,&chain_id,&is_array); if (lvalue == ID_LVALUE) ref_def_var (REF,node_id,tk,is_array); else if (lvalue == PTR_LVALUE) cref_def(REF,node_id,chain_id); scan_tree (t,node_id,from,&upto,to); /* q = leftmost(t); r = rightmost(t); printf ("leftmost token: (%d,%d) rightmost (%d,%d)", q->at.line_start,q->at.col_start, r->at.line_end,r->at.col_end); source_map (node_id,leftmost(t),rightmost(t)); */ if(upto) connect_nodes(upto,node_id); if (*from == 0) *from = node_id; if (*to == 0) *to = node_id; return node_id; /* return the main stmt node */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -