|
|
1.1 root 1: From rabbit!groucho!dcb Tue Sep 4 15:59 EDT 1984
2: #define unsafe 1 /* pretend killing all but C is as good as killing
3: * all condition codes */
4: #ifdef unsafe
5: #define E (C|K)
6: #else
7: #define E K
8: #endif
9: #include "stdio.h"
10: #include "instr.c"
11: #include "ctype.h"
12: struct inst *index[128];
13:
14: #define SBBLK 1 /* looking for the start of a basic block */
15: #define SINST 2 /* thinking about generating a tally */
16: #define SPRO 3 /* thinking about generating prolog code */
17: #define SMAYBE 4 /* seen _, thinking about SPRO */
18:
19: #define MAXLA 5 /* max no. of lines that can be read for lookahead */
20:
21: FILE *outs, *fd, *outl; /* outs goes to assembler, outl is for listing */
22: extern FILE *popen();
23: char line[256], fname[256]; /* file names must fit in fname */
24:
25: char labuf[5][256]; /* look ahead needed for 5.0 stab entries */
26: int lastla, lookahead;
27: char begfcn[] = " .def .bf"; /* 5.0 stab entry */
28:
29: int lineno, lastline;
30: int base=0;
31:
32: int cnt;
33: int state = SBBLK;
34: char *ptr, *curarg;
35: char curdir[256];
36:
37: main(argc, argv)
38: char **argv;
39: { int i;
40: if(argc <= 1) {
41: fprintf(stderr, "no files given\n");
42: exit(1);
43: }
44: for(i = 0; insts[i].iname; i++)
45: ;
46: for(; i >= 0; i--)
47: index[insts[i].iname[0]] = insts + i;
48: fd = popen("pwd", "r");
49: for(i = 0; i < sizeof(curdir) && !feof(fd); i++)
50: curdir[i] = getc(fd);
51: curdir[i-2] = 0; /* thisdir\n */
52: fclose(fd);
53: for(i = 1; i < argc; i++) {
54: if(setfd(argv[i])) /* fix fd, outs, outl */
55: doarg(); /* do the work */
56: }
57: exit(0);
58: }
59:
60: setfd(s)
61: char *s;
62: { char outnams[24], outnaml[24];
63: fname[0] = 0;
64: cnt = 3;
65: if(fd != NULL)
66: fclose(fd);
67: if(outs != NULL)
68: fclose(outs);
69: if(outl != NULL)
70: fclose(outl);
71: sprintf(outnams, "X%s", s);
72: sprintf(outnaml, "%sL", s);
73: lastline = lineno = 0;
74: fd = fopen(s, "r");
75: if(fd == NULL) {
76: perror(s);
77: return(0);
78: }
79: outs = fopen(outnams, "w");
80: if(outs == NULL) {
81: perror(outnams);
82: return(0);
83: }
84: outl = fopen(outnaml, "w");
85: if(outl == NULL) {
86: perror(outnaml);
87: return(0);
88: }
89: curarg = s;
90: return(1);
91: }
92:
93: doarg()
94: { struct inst *x, *firstword();
95:
96: state = SBBLK;
97: lookahead = 0; /* empty buffer */
98: lastla = 0;
99: for(;;) {
100: if (lookahead != lastla){
101: strcpy(line,labuf[ (lookahead++) % MAXLA ] );
102: if (lookahead == lastla) lookahead = lastla = 0;
103: }
104: else
105: (void) fgets(line, sizeof(line), fd);
106: if(feof(fd))
107: break;
108: for(ptr = line; isspace(*ptr); *ptr++)
109: ;
110: if(*ptr == 0 || *ptr == '#')
111: continue;
112: testlabel();
113: /* deal with symbol table info */
114: if(*ptr == '.') {
115: stab();
116: fprintf(outs, " %s", ptr);
117: continue;
118: }
119: if(*ptr == 0 || *ptr == '\n')
120: continue;
121: x = firstword();
122: if(x == 0){
123: printf("unknown inst: %s\n",ptr);
124: continue;
125: }
126: #ifdef u3b
127: if ((state == SMAYBE) && !strncmp(ptr,"save",4)){
128: state = SPRO;
129: getlnum(); /* 5.0 true line # */
130: }
131: #endif
132: if(state == SPRO)
133: prolog(x);
134: if(state == SINST)
135: tally(x);
136: if(state == SBBLK && (x->type & JUMP))
137: state = SINST;
138: outinstr();
139: if(x->type & BYTE)
140: fixinstr(x);
141: fprintf(outs, " %s", ptr);
142: }
143: finish();
144: }
145:
146:
147: getlnum(){ /* get true line number from 5.0 sdb info */
148: char *la;
149: int l;
150:
151: /* get true line number by looking ahead */
152: /* this is necessary for 5.0 sdb output */
153: do
154: (void) fgets(labuf[l = ((lastla++)%MAXLA)], sizeof(line), fd);
155: while (!feof(fd) && strncmp(begfcn, labuf[l],
156: 9) && (lookahead != (lastla)%MAXLA ));
157: if ( strncmp(begfcn, labuf[l], 9) )
158: return; /* bad input or not 5.0 */
159: la = labuf[l];
160: while ( *la != '\0' ){
161: while ( *la != ';' && *la != '\0') la++;
162: if ( *la == ';' ) la++;
163: while ( isspace(*la) ) la++;
164: if ( !strncmp(la, ".line" , 5) ){
165: lineno = base = atoi( la + 5 );
166: return;
167: }
168: }
169: }
170:
171:
172: /* unbelievable variability in sdb info */
173: stab()
174: { char buf[128];
175: int i, j, k;
176:
177: #ifndef u3b
178: if(state == SMAYBE && strncmp(ptr, ".word", 5) == 0) {
179: state = SPRO;
180: getlnum();
181: return;
182: }
183: #endif
184:
185: if((i = *(ptr + 1)) != 's' && i != 'f' && i != 'l')
186: return;
187: /* real compiler output */
188: if(sscanf(ptr, ".stabs \"%[^\"]\", %o", buf, &i) == 2 && i == 0144)
189: strcat(fname, buf);
190: else if(sscanf(ptr, ".stabd %o,%o,%o", &i, &j, &k) == 3 && i == 0104)
191: lineno = k;
192: /* pwb 3.0 */
193: else if(sscanf(ptr, ".stab %[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%d",
194: buf, buf+2, buf+4, buf+6, buf+8, buf+10, buf+12, buf+14, &i)
195: == 9 && i == 144) {
196: for(i = k = 0; buf[i] ; i++)
197: if(buf[i] == '\'')
198: buf[k++] = buf[++i];
199: buf[k] = 0;
200: strcat(fname, buf);
201: }
202: else if(sscanf(ptr, ".stab %[0,]%d,%d,%d", buf,&i, &j, &k) == 4
203: && i == 104)
204: lineno = k;
205: /* pwb 5.0 */
206: else if(sscanf(ptr," .file \"%[^\"]\"", buf) == 1)
207: strcat(fname, buf);
208: else if(sscanf(ptr, " .ln %d",&k) == 1)
209: lineno = k + base - 1;
210: }
211:
212: testlabel()
213: { char *p;
214: int i;
215:
216: for(;;){
217: for(p = ptr; *p; p++) {
218: if(*p == ':')
219: break;
220: if(!isalnum(*p) && *p != '_' && *p != '.')
221: return;
222: }
223: if(*p == 0)
224: return;
225: *p++ = 0; /* that is overwriting the : */
226: fprintf(outs, "%s:\n", ptr);
227: if (lineno != lastline){
228: for (i=lastline+1; i < lineno ; i++) /* allign label with right inst */
229: fprintf(outl, "%d %s: %d\n", 4*(cnt-1), fname, i);
230: lastline = lineno - 1;
231: }
232: fprintf(outl, "%d %s:\n", 4*(cnt - 1), ptr);
233: #ifdef u3b
234: if(*ptr != '.')
235: #else
236: if(*ptr == '_' )
237: #endif
238: state = SMAYBE;
239: else
240: if(state != SPRO)
241: state = SINST;
242: for(ptr = p; isspace(*ptr); ptr++)
243: ;
244: } /* L68:L70: ... */
245: }
246:
247: struct inst *
248: firstword()
249: { char buf[sizeof(line)], *p, *q;
250: struct inst *x;
251: for(p = buf, q = ptr; isalnum(*q); )
252: *p++ = *q++;
253: if(p == buf)
254: return((struct inst *)0);
255: *p = 0;
256: for(x = index[buf[0]]; x && x->iname[0] == buf[0]; x++)
257: if(strcmp(buf, x->iname) == 0)
258: return(x);
259: return(0);
260: }
261:
262: outinstr()
263: { int i;
264: for(i = lastline + 1; i < lineno; i++)
265: fprintf(outl, "%d %s: %d\n", 4*(cnt - 1), fname, i);
266: if (lastline != lineno){
267: fprintf(outl, "%d %s: %d\n", 4*(cnt - 1), fname, lineno);
268: lastline = lineno;
269: }
270: fprintf(outl, "%d %s", 4*(cnt - 1), ptr);
271: }
272:
273: #ifdef u3b
274:
275: /* 3b code ***********************************/
276: tally(x)
277: struct inst *x;
278: {
279: if(x->type & E)
280: fprintf(outs, " addw2 &1,locprof+%d\n", 4*cnt++);
281: else {
282: fprintf(outs, " stsm &1,savecc\n");
283: fprintf(outs, " gcc %%r0\n");
284: fprintf(outs, " addw2 &1,locprof+%d\n", 4*cnt++);
285: fprintf(outs, " scc %%r0\n");
286: fprintf(outs, " lsm &1,savecc\n");
287: }
288: state = SBBLK;
289: }
290:
291: prolog(x) /* no liveness test, presumes can't get here by jump */
292: struct inst *x;
293: { int i;
294: fprintf(outs, " .data\n");
295: fprintf(outs, " .globl proFptr\n"); /* the global chain */
296: fprintf(outs, " .globl savecc\n");
297: fprintf(outs, " .text\n");
298: fprintf(outs, " cmpw &0,locprof+4\n");
299: fprintf(outs, " jne .L%da\n", i = cnt);
300: fprintf(outs, " movw proFptr,locprof+4\n");
301: fprintf(outs, " movaw locprof,proFptr\n");
302: fprintf(outs, ".L%da: addw2 &1,locprof+%d\n", i, 4*cnt++);
303: state = SBBLK;
304: }
305:
306: finish()
307: { int i;
308: fprintf(outs, " .data\n");
309: fprintf(outs, " .align 4\n");
310: fprintf(outs, "locprof:\n");
311: fprintf(outs, " .word %d\n", cnt);
312: fprintf(outs, " .word 0\n");
313: fprintf(outs, " .word .L%db\n", cnt);
314: fprintf(outs, " .zero %d\n", 4 * cnt);
315: fprintf(outs, ".L%db: .byte ", cnt);
316: for(i = 0; curdir[i]; i++)
317: fprintf(outs, " 0x%x,", curdir[i]);
318: fprintf(outs, " 0x%x\n", '/');
319: fprintf(outs, " .byte ");
320: if(fname[0])
321: for(i = 0; fname[i]; i++)
322: fprintf(outs, " 0x%x,", fname[i]);
323: else
324: for(i = 0; curarg[i]; i++)
325: fprintf(outs, " 0x%x,", curarg[i]);
326: fprintf(outs, "0\n");
327: }
328:
329: #else
330:
331: /* Vax code **************************/
332: tally(x)
333: struct inst *x;
334: {
335: if(x->type & E)
336: fprintf(outs, " incl locprof+%d\n", 4*cnt++);
337: else {
338: fprintf(outs, " movpsl -(sp)\n");
339: fprintf(outs, " incl locprof+%d\n", 4*cnt++);
340: fprintf(outs, " movw (sp)+,(sp)\n");
341: fprintf(outs, " bicpsw $0xff\n");
342: fprintf(outs, " bispsw (sp)+\n");
343: /* thanks to kirk mckusick */
344: }
345: state = SBBLK;
346: }
347:
348:
349: prolog(x) /* no liveness test, presumes can't get here by jump */
350: struct inst *x;
351: { int i;
352: fprintf(outs, " .data\n");
353: fprintf(outs, " .comm _proFptr,4\n"); /* the global chain */
354: fprintf(outs, " .text\n");
355: /*if(!(x->type & E))
356: fprintf(outs, " movpsl -(sp)\n");*/
357: fprintf(outs, " tstl locprof+4\n");
358: fprintf(outs, " bneq .L%da\n", i = cnt);
359: fprintf(outs, " movl _proFptr,locprof+4\n");
360: fprintf(outs, " moval locprof,_proFptr\n");
361: fprintf(outs, ".L%da: incl locprof+%d\n", i, 4*cnt++);
362: /*if(!(x->type & E)) {
363: fprintf(outs, " movw (sp)+,(sp)\n");
364: fprintf(outs, " bicpsw $0xff\n");
365: fprintf(outs, " bispsw (sp)+\n");
366: }*/
367: state = SBBLK;
368: }
369:
370: finish()
371: { int i;
372: fprintf(outs, " .data\n");
373: fprintf(outs, "locprof: .long %d\n", cnt);
374: fprintf(outs, " .long 0\n");
375: fprintf(outs, " .long .L%db\n", cnt);
376: fprintf(outs, " .space %d\n", 4 * cnt);
377: fprintf(outs, ".L%db: .byte ", cnt);
378: for(i = 0; curdir[i]; i++)
379: fprintf(outs, "0x%x,", curdir[i]);
380: fprintf(outs, "0x%x\n", '/');
381: fprintf(outs, " .byte ");
382: if(fname[0])
383: for(i = 0; fname[i]; i++)
384: fprintf(outs, "0x%x,", fname[i]);
385: else
386: for(i = 0; curarg[i]; i++)
387: fprintf(outs, "0x%x,", curarg[i]);
388: fprintf(outs, "0\n");
389: }
390:
391: #endif
392:
393:
394: fixinstr(x)
395: struct inst *x;
396: {
397: #ifndef u3b
398: if(x->iname[0] == 'b')
399: *ptr = 'j'; /* let assembler worry about branches */
400: /* this is where the code for aob and sob goes */
401: #endif
402: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.