?? slink.c
字號:
for (pix = 0; pix < f[i].n_procs; pix++) if (f[i].procs[pix].proc_id == f[i].addrs[j].pid) break; f[i].addrs[j].new_pid = f[i].procs[pix].new_id; if ((pix < f[i].n_procs) && (f[i].procs[pix].localx)){ lx = f[i].procs[pix].localx; while (lx){ if (lx->local == f[i].addrs[j].id){ real_id = lx->global; f[i].addrs[j].new_id = real_id; addr_found = find_global_addr(last,addrs,real_id); if (addr_found){ f[i].addrs[j].new_addr = addr_found; } else { last++; f[i].addrs[j].new_addr = last; addrs[last] = &f[i].addrs[j]; } break; } lx = lx -> next; } if (lx == NULL){ last++; f[i].addrs[j].new_addr = last; addrs[last] = &f[i].addrs[j]; } } else { last++; f[i].addrs[j].new_addr = last; addrs[last] = &f[i].addrs[j]; } } else {/* global var */ real_id = f[i].addrs[j].id; real_id = otn_globals (f,i,real_id); f[i].addrs[j].new_id = real_id; addr_found = find_global_addr(last,addrs,real_id); if (addr_found){ f[i].addrs[j].new_addr = addr_found; } else { last++; f[i].addrs[j].new_addr = last; addrs[last] = &f[i].addrs[j]; } } } } /* printf ("addrs assigned:\n"); for (i = 1; i <= last; i++){ printf ("%2d %d %d %d=>%d %d\n",i,addrs[i]->id,addrs[i]->new_id, addrs[i]->addr,addrs[i]->new_addr, addrs[i]->pid); } */ last_addr = last; return addrs;}int really_global(f,i,j,id) int i,j,id; file_rec f[];{ local_ptr lx; lx = f[i].procs[j].localx; if (!lx) return 0; while (lx){ if (lx->local == id) return lx->global; lx = lx -> next; } return 0;}chain_ptr *shrink_chains(n_files,f) int n_files; file_rec f[];{ int i,j,k,global,pid,id,at = 0; chain_ptr *c = NULL,*new; int max = 0; int delta = 3; for (i = 0; i < n_files; i++){ for (j = 1; j <= f[i].n_chains; j++){ if (!f[i].chains[j].pid){ f[i].chains[j].new_id = f[i].globals[f[i].chains[j].id].new; } else { pid = f[i].chains[j].pid; id = f[i].chains[j].id; global = really_global (f,i, pid_to_index (n_files,f,i,pid),id); if (global){ f[i].chains[j].pid = 0; f[i].chains[j].new_id = global; } else f[i].chains[j].new_id = f[i].chains[j].id; } for (k = 1; k <= at; k++){ if (chains_are_equal(c[k], &f[i].chains[j])){ f[i].chains[j].new = c[k]->new; break; } } if (f[i].chains[j].new == 0){ ++at; f[i].chains[j].new = at; if (at >= max){ max += delta; new = (chain_ptr *) malloc (max*sizeof(chain_ptr)); for (k = 1; k < at; k++) new[k] = c[k]; if(c)free(c); c = new; } c[at] = &f[i].chains[j]; } } } last_chain = at; return c;}global_ptr assign_globals(n_files,f) int n_files; file_rec f[];{ int total = 0; int i,j,k = 0,last,at; global_rec *globals; char *right,*static_s; local_ptr localx; for (i = 0; i < n_files; i++) for (j = 0; j < f[i].n_procs; j++) if (f[i].procs[j].localx){ localx = f[i].procs[j].localx; while (localx){ total++; localx = localx -> next; } } for (i = 0; i < n_files; i++) total += f[i].n_globals; globals = (global_ptr) malloc (sizeof(global_rec)*(++total)); /* printf ("%d globals found (total)\n",total); for (j = 0; j < f[0].n_globals; j++){ k = j + 1; globals[k].new = k; globals[k].text = f[0].globals[k].text; globals[k].flags = f[0].globals[k].flags; f[0].globals[k].new = k; } last = k; */ last = 0; for (i = 0; i < n_files; i++){ for (j = 0,k = 1; j < f[i].n_globals; j++,k++){ right = strchr (f[i].globals[k].flags,')'); static_s = strchr (f[i].globals[k].flags,'S'); if ((static_s) && (static_s - f[i].globals[k].flags < right - f[i].globals[k].flags)) at = 0; else at = find_global (last,globals,f[i].globals[k].text); if (at){f[i].globals[k].new = at; } else { last++; globals[last].is_static = (static_s != NULL); globals[last].new = last; globals[last].text = f[i].globals[k].text; globals[last].flags = f[i].globals[k].flags; f[i].globals[k].new = last; } } } for (i = 0; i < n_files; i++){ for (j = 0; j < f[i].n_procs; j++) if (f[i].procs[j].localx){ localx = f[i].procs[j].localx; while (localx){ for (k = 1; k <= f[i].n_globals; k++){ if (0 == strcmp (f[i].globals[k].text, localx->text)){ /* found */ localx -> global = f[i].globals[k].new; break; } } if (!localx -> global){ at = find_global (last,globals,localx->text); if (at)localx -> global = at; else { last++; globals[last].is_static = 0; globals[last].new = last; globals[last].text = localx -> text; globals[last].flags = localx -> flags; localx -> global = last; } } localx = localx -> next; } } last_global = last; } return globals;}merge_lif (n,f,dir,globals,procs,addrs,chains) int n; chain_ptr *chains; global_ptr globals; proc_ptr *procs; addr_ptr *addrs; file_ptr f; char *dir;{ char out_name[2000],lif_name[2000]; char file[2000]; FILE *out_file,*lif_file; int i,j,id,pid,px,code,node,chain; int gid; char name[2000],flags[2000],buff[2000]; field_ptr field; sprintf (out_name,"%s/%sLINK",dir,f[0].name); out_file = fopen (out_name,"w"); if (!out_file){ fprintf (stderr,"could not create output file: %s\n",out_name); exit(1); } for (i = 1; i <= last_global; i++){ fprintf (out_file,"%d(%d,%s%s\n",LIF_GLOBAL_ID,globals[i].new, globals[i].text,globals[i].flags); } for (i = 0; i < n; i++){ fprintf (out_file,"%d(%d,%s)\n",LIF_FILE,i,f[i].name); sprintf (lif_name,"%s/%sLIF",dir,f[i].name); lif_file = fopen (lif_name,"r"); if (!lif_file){ fprintf (stderr,"could not open file: %s\n",lif_name); } while (fgets(buff,2000,lif_file)){ if (sscanf(buff,"%d",&code) == 0) code = 0; switch (code){ case LIF_GLOBAL_ID : break; case LIF_PROC_START : case LIF_CALL_START : sscanf (buff,"%*d(%d,%d%[^\n]",&node,&pid,flags); px = pid_to_index (n,f,i,pid); fprintf (out_file,"%d(%d,%d%s\n",code,node, f[i].procs[px].new_id,flags); break; case LIF_GREF : case LIF_GDEF : sscanf (buff,"%*d(%d,%d%[^\n]",&node,&id,flags); fprintf (out_file,"%d(%d,%d%s\n",code,node, f[i].globals[id].new, flags); break; case LIF_STRUCT : sscanf (buff,"%*d(%d,%d%[^\n]",&node,&id,flags); if (node){ px = pid_to_index (n,f,i,node); fprintf (out_file,"%d(%d,%d%s\n",code, f[i].procs[px].new_id,id,flags); } else { fprintf (out_file,"%d(%d,%d%s\n",code,node, f[i].globals[id].new,flags); } break; case LIF_AREF : sscanf (buff,"%*d(%d,%d%[^\n]",&node,&id,flags); fprintf (out_file,"%d(%d,%d%s\n",code,node, f[i].addrs[id].new_addr, flags); break; case LIF_REF : case LIF_DEF : sscanf (buff,"%*d(%d,%d%[^\n]",&node,&id,flags); gid = really_global (f,i,pid_to_index (n,f,i,pid),id); if (gid){ fprintf (out_file,"%d(%d,%d%s\n", code == LIF_REF?LIF_GREF:LIF_GDEF,node, gid,flags); } else { fputs(buff,out_file); } break; case LIF_FIELD : case LIF_ADDRESS : break; case LIF_CHAIN : case LIF_GCHAIN : sscanf (buff,"%*d(%d,%d%[^\n]",&chain,&id,flags); chain = f[i].chains[chain].new; if (chains[chain]->done) break; chains[chain]->done = 1; if (code == LIF_CHAIN) gid = really_global(f,i,pid_to_index(n,f,i,pid),id); else gid = 0; fprintf (out_file,"%d(%d,%d%s\n", gid?LIF_GCHAIN:code,chain, gid?gid:chains[chain]->new_id, flags); field = chains[chain]->field; while (field){ fprintf (out_file,"%d(%d,%d,%d%s\n", LIF_FIELD,chain, field->fid, field->offset, field->flags); field = field->next; } break; case LIF_CREF : case LIF_CDEF : sscanf (buff,"%*d(%d,%d%[^\n]",&node,&id,flags); fprintf (out_file,"%d(%d,%d%s\n",code,node, f[i].chains[id].new,flags); break; case LIF_LOCAL_ID : case LIF_FILE : case LIF_PROC_END : case LIF_FORMAL_ID : case LIF_ACTUAL_SEP : case LIF_CALL_END : case LIF_RETURN : case LIF_GOTO : case LIF_SUCC : case LIF_REQUIRES : case LIF_SOURCE_MAP : default : fputs(buff,out_file); } } fclose (lif_file); } for (i = 1; i <= last_addr; i++){ if (addrs[i]->pid){ id = addrs[i]->id; pid = addrs[i]->new_pid; } else { id = addrs[i]->new_id; pid = 0; } fprintf (out_file,"%d(%d,%d,%d)\n",LIF_ADDRESS,i,pid,id); } fclose (out_file);}var_ptr find_var (list,id) var_ptr list; int id;{ while (list){ if (list->id == id) return list; list = list -> next; } return NULL;}int look_up_var (i,f,name) int i; file_rec f[]; char *name;{ int k; for (k = 1; k <= f[i].n_globals; k++){ if (strcmp(name,f[i].globals[k].text) == 0) return f[i].globals[k].new; } return 0;}h_ptr find_h (list,name) h_ptr list; char *name;{ while (list){ if (strcmp (list->name,name) == 0) return list; list = list -> next; } return NULL;}merge_head (n,f,dir,procs,g) int n; file_rec f[]; proc_ptr procs[]; char *dir; global_rec g[];{ char buff[2000],name[2000],flags[2000]; FILE *h_file,*k_file; h_ptr new_h,h_list = NULL; h_ptr at_h = NULL; var_ptr v; int i,var,n_h = 0;; sprintf (buff,"%s/%sK",dir,f[0].name); k_file = fopen(buff,"w"); if (!k_file){ fprintf (stderr,"could not create %s\n",buff); exit(1); } for (i = 0; i < n; i++){ sprintf (buff,"%s/%sH",dir,f[i].name); h_file = fopen (buff,"r"); if (!h_file){ fprintf (stderr,"Warning (slink): could not open %s\n", buff); exit(1); } while (fgets(buff,2000,h_file)){ if ((buff[0] == '@') && (strlen(buff) > 2)){ sscanf(buff,"@ %[^\n]",name); at_h = find_h (h_list,name); if (!at_h){ new_h = (h_ptr) malloc (sizeof(h_rec)); new_h->next = h_list; new_h->vars = NULL; new_h->n_v = 0; new_h->name = malloc (strlen(name)+1); strcpy (new_h->name,name); h_list = new_h; at_h = new_h; } } else { sscanf(buff,"\t%[^\n]",name); var = look_up_var (i,f,name); if (var && at_h && (!find_var(at_h->vars,var))){ v = (var_ptr) malloc (sizeof(var_rec)); v->id = var; v->next = at_h->vars; at_h->vars = v; at_h->n_v++; } } } fclose (h_file); } new_h = h_list; while (new_h){ if (new_h->n_v)n_h++; new_h = new_h->next; } fprintf (k_file, "globals %d procs %d addrs %d chains %d headers %d files %d\n", last_global,last_proc,last_addr,last_chain,n_h,n); for (i = 1; i <= last_proc; i++){ fprintf (k_file,"%3d %4d %4d %4d %c %s\n", procs[i]->new_id, procs[i]->entry, procs[i]->exit, procs[i]->n_locals, procs[i]->s_or_x, procs[i]->name); } fprintf (k_file,"\n"); for (i = 0; i < n; i++){ fprintf (k_file,"%3d %3d %5d %4d %6d %s\n", i,f[i].n_procs,f[i].n_stmts,f[i].n_lines,f[i].n_chars, f[i].name); } new_h = h_list; while (new_h){ if (new_h->n_v){ v = new_h->vars; fprintf (k_file,"%d %s\n",new_h->n_v,new_h->name); while (v){ fprintf (k_file,"\t%3d %s\n",v->id,g[v->id].text); v = v -> next; } } new_h = new_h->next; } fclose(k_file);}main(np,p) int np; char *p[];{ FILE *sys; int n_files; file_ptr files; char *dir; char buff[2000]; global_ptr globals; proc_ptr *procs; chain_ptr *chains; addr_ptr *addrs; int t_chains,i,fx; char *slash,*main_name; if ((np < 2) || (np > 3)) { fprintf (stderr,"%s: wrong number of parms\n",p[0]); fprintf (stderr,"%s usage: %s [-v] main_file_name.c\n", p[0],p[0]); exit (1); } fx = 0; for (i = 1; i < np; i++){ if (p[i][0] == '-'){ if (strcmp("-v",p[i]) == 0)v_opt = 1; else { fprintf (stderr,"%s: option (%s) is not valid\n", p[0],p[i]); fprintf (stderr,"%s usage: %s [-v] main_file_name.c\n", p[0],p[0]); exit (1); } } else fx = i; } if ((fx == 0) || ((np == 3) && !v_opt)){ fprintf (stderr,"%s: one file name required\n",p[0]); fprintf (stderr,"%s usage: %s [-v] main_file_name.c\n", p[0],p[0]); exit (1); } slash = strrchr (p[fx],'/'); if (slash){ dir = p[fx]; main_name = slash + 1; *slash = '\0'; } else { dir = "."; main_name = p[fx]; } sprintf (buff,"%s/SYSTEM",dir); sys = fopen (buff,"r"); if (!sys) { fprintf (stderr,"%s: no 'SYSTEM' map file\n",p[0]); fprintf (stderr,"%s: run 'map' before 'slink'\n",p[0]); exit (1); } n_files = scan_system(sys,main_name); if (n_files){ files = (file_ptr) malloc (n_files*sizeof(file_rec)); get_t_files (sys,n_files,files,dir); get_lif_files (n_files,files,dir); globals = assign_globals (n_files,files); procs = assign_procs (n_files,files); addrs = assign_addrs (n_files,files); chains = shrink_chains (n_files,files); if(v_opt){ print_files (n_files,files); print_globals(globals); print_procs (procs); } merge_lif (n_files,files,dir,globals,procs,addrs,chains); merge_head (n_files,files,dir,procs,globals); } else { fprintf (stderr,"%s: %s is not a main program source file\n", p[0],main_name); exit (1); } exit (0);}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -