Commit | Line | Data |
---|---|---|
a24e0abe | 1 | static char sccsid[] = "@(#)c21.c 4.9 %G%"; |
35307d30 | 2 | /* char C21[] = {"@(#)c21.c 1.83 80/10/16 21:18:22 JFR"}; /* sccs ident */ |
f00d91fc BJ |
3 | |
4 | /* | |
5 | * C object code improver-- second part | |
6 | */ | |
7 | ||
8 | #include "c2.h" | |
9 | #include <stdio.h> | |
10 | #include <ctype.h> | |
11 | ||
12 | #define NUSE 6 | |
13 | int ioflag; | |
14 | int biti[NUSE] = {1,2,4,8,16,32}; | |
0594fe30 | 15 | int bitsize[] = { /* index by type codes */ |
a24e0abe RH |
16 | 0, /* 0 not allocated */ |
17 | 8, /* 1 BYTE */ | |
18 | 16, /* 2 WORD */ | |
19 | 32, /* 3 LONG */ | |
20 | 32, /* 4 FFLOAT / | |
21 | 64, /* 5 DFLOAT */ | |
22 | 64, /* 6 QUAD */ | |
23 | 0, /* 7 OP2 */ | |
24 | 0, /* 8 OP3 */ | |
25 | 0, /* 9 OPB */ | |
26 | 0, /* 10 OPX */ | |
27 | 64, /* 11 GFLOAT */ | |
28 | 128, /* 12 HFLOAT */ | |
29 | 128 /* 13 OCTA */ | |
0594fe30 | 30 | }; |
f00d91fc BJ |
31 | int pos,siz; long f; /* for bit field communication */ |
32 | struct node *uses[NUSE]; /* for backwards flow analysis */ | |
33 | char *lastrand; /* last operand of instruction */ | |
34 | struct node *bflow(); | |
35 | struct node *bicopt(); | |
36 | char *findcon(); | |
37 | ||
38 | redun3(p,split) register struct node *p; int split; { | |
39 | /* check for 3 addr instr which should be 2 addr */ | |
40 | if (OP3==((p->subop>>4)&0xF)) { | |
41 | if (split) splitrand(p); | |
42 | if (equstr(regs[RT1],regs[RT3]) | |
43 | && (p->op==ADD || p->op==MUL || p->op==BIS || p->op==XOR)) { | |
44 | register char *t=regs[RT1]; regs[RT1]=regs[RT2]; regs[RT2]=t; | |
45 | } | |
46 | if (equstr(regs[RT2],regs[RT3])) { | |
47 | p->subop=(p->subop&0xF)|(OP2<<4); p->pop=0; | |
48 | lastrand=regs[RT2]; *regs[RT3]=0; return(1); | |
49 | } | |
50 | } return(0); | |
51 | } | |
52 | ||
53 | bmove() { | |
54 | register struct node *p, *lastp; register char *cp1,*cp2; register int r; | |
55 | refcount(); | |
56 | for (p=lastp= &first; 0!=(p=p->forw); lastp=p); | |
57 | clearreg(); clearuse(); | |
58 | for (p=lastp; p!= &first; p=p->back) { | |
59 | if (debug) { | |
60 | printf("Uses:\n"); | |
61 | for (r=NUSE;--r>=0;) if (uses[r]) | |
62 | printf("%d: %s\n",r,uses[r]->code? uses[r]->code:""); | |
63 | printf("-\n"); | |
64 | } | |
65 | r=(p->subop>>4)&0xF; | |
66 | if (OP2==r && (cp1=p->code, *cp1++)=='$' && *cp1++=='0' && *cp1++==',' && | |
67 | !source(cp1)) {/* a no-op unless MUL or DIV */ | |
68 | if (p->op==MUL) {p->op=MOV; p->subop&=0xF; p->pop=0;} | |
69 | else if (p->op==DIV) fprintf(stderr,"c2: zero divide\n"); | |
70 | else {delnode(p); redunm++; continue;} | |
71 | } | |
72 | if (OP3==r && 0!=redun3(p,1)) {newcode(p); redunm++;} | |
73 | switch (p->op) { | |
74 | case LABEL: case DLABEL: | |
75 | for (r=NUSE; --r>=0;) | |
76 | if (uses[r]) p->ref=(struct node *) (((int)p->ref)|biti[r]); | |
77 | break; | |
78 | case CALLS: | |
79 | clearuse(); goto std; | |
80 | case 0: | |
81 | clearuse(); break; | |
82 | case SUB: | |
83 | if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; | |
84 | if (*cp1++!='$') goto std; splitrand(p); | |
85 | if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ | |
3b60efae | 86 | char buf[C2_ASIZE]; cp2=buf; *cp2++='-'; |
f00d91fc BJ |
87 | cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; |
88 | cp1="(fp),"; while (*cp2++= *cp1++); --cp2; | |
89 | cp1=regs[RT3]; while (*cp2++= *cp1++); | |
90 | p->code=copy(buf); p->combop=T(MOVA,LONG); p->pop=0; | |
91 | } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { | |
92 | p->op=ADD; p->pop=0; *--cp1='$'; p->code=cp1; | |
93 | } goto std; | |
94 | case ADD: | |
95 | if ((p->subop&0xF)!=LONG) goto std; cp1=p->code; | |
96 | if (*cp1++!='$') goto std; splitrand(p); | |
97 | if (isstatic(cp1) && (r=isreg(regs[RT2]))>=0 && r<NUSE && uses[r]==p->forw) | |
98 | { | |
99 | /* address comp: | |
100 | ** addl2 $_foo,r0 \ movab _foo[r0],bar | |
101 | ** movl r0,bar / | |
102 | */ | |
103 | register struct node *pnext = p->forw; | |
3b60efae | 104 | char buf[C2_ASIZE]; |
f00d91fc BJ |
105 | |
106 | if (pnext->op == MOV && pnext->subop == LONG) | |
107 | { | |
108 | cp1 = ®s[RT1][1]; cp2 = &buf[0]; | |
109 | while (*cp2++ = *cp1++) ; cp2--; | |
110 | splitrand(pnext); | |
111 | if (r == isreg(regs[RT1])) | |
112 | { | |
113 | delnode(p); p = pnext; | |
114 | p->op = MOVA; p->subop = BYTE; | |
115 | p->pop = 0; | |
116 | cp1 = regs[RT1]; *cp2++ = '['; | |
117 | while (*cp2++ = *cp1++) ; cp2--; | |
118 | *cp2++ = ']'; *cp2++ = ','; | |
119 | cp1 = regs[RT2]; | |
120 | while (*cp2++ = *cp1++) ; | |
121 | p->code = copy(buf); | |
122 | } | |
123 | } | |
124 | } | |
125 | else | |
126 | if (equstr(regs[RT2],"fp") && !indexa(regs[RT1])) {/* address comp. */ | |
127 | cp2=cp1-1; cp1=regs[RT1]+1; while (*cp2++= *cp1++); --cp2; | |
128 | cp1="(fp)"; while (*cp2++= *cp1++); *--cp2=','; | |
129 | p->combop=T(MOVA,LONG); p->pop=0; | |
130 | } else if (*cp1++=='-' && 0<=(r=getnum(cp1))) { | |
131 | p->op=SUB; p->pop=0; *--cp1='$'; p->code=cp1; | |
132 | } | |
133 | /* fall thru ... */ | |
134 | case CASE: | |
135 | default: std: | |
136 | p=bflow(p); break; | |
137 | case MUL: | |
138 | { | |
139 | /* | |
140 | ** Change multiplication by constant powers of 2 to | |
141 | ** shifts. | |
142 | */ | |
143 | splitrand(p); | |
144 | if (regs[RT1][0] != '$' || regs[RT1][1] == '-') goto std; | |
145 | if ((r = ispow2(getnum(®s[RT1][1]))) < 0) goto std; | |
146 | switch (r) | |
147 | { | |
148 | case 0: /* mull3 $1,x,y */ | |
149 | if (p->subop == U(LONG,OP3)) | |
150 | { | |
151 | if (equstr(regs[RT2], regs[RT3])) | |
152 | { | |
153 | delnode(p); p = p->forw; | |
154 | } | |
155 | else | |
156 | { | |
157 | p->op = MOV; p->subop = LONG; | |
158 | p->pop = 0; newcode(p); nchange++; | |
159 | } | |
160 | } | |
161 | else | |
162 | if (p->subop == U(LONG,OP2)) | |
163 | { | |
164 | delnode(p); p = p->forw; | |
165 | } | |
166 | goto std; | |
167 | ||
168 | case 1: /* mull2 $2,x */ | |
169 | if (p->subop == U(LONG, OP2) && !source(regs[RT2])) | |
170 | { | |
171 | strcpy(regs[RT1], regs[RT2]); | |
172 | p->op = ADD; p->pop = 0; newcode(p); nchange++; | |
173 | } | |
174 | goto std; | |
175 | } | |
176 | if(p->subop==U(LONG,OP3)||(p->subop==U(LONG,OP2)&&!source(regs[RT2]))) | |
177 | { | |
178 | if (p->subop == U(LONG,OP2)) | |
179 | strcpy(regs[RT3], regs[RT2]); | |
180 | sprintf(regs[RT1], "$%d", r); | |
181 | p->op = ASH; p->subop = LONG; | |
182 | p->pop = 0; newcode(p); nchange++; | |
183 | } | |
184 | goto std; | |
185 | } | |
186 | case ASH: | |
187 | { | |
188 | /* address comp: | |
189 | ** ashl $1,bar,r0 \ movl bar,r0 | |
190 | ** movab _foo[r0] / movaw _foo[r0] | |
191 | ** | |
192 | ** ashl $2,r0,r0 \ moval _foo[r0] | |
193 | ** movab _foo[r0] / | |
194 | */ | |
195 | register struct node *pf; | |
196 | register int shfrom, shto; | |
197 | long shcnt; | |
198 | char *regfrom; | |
199 | ||
200 | splitrand(p); | |
201 | if (regs[RT1][0] != '$') goto std; | |
202 | if ((shcnt = getnum(®s[RT1][1])) < 1 || shcnt > 3) goto std; | |
203 | if ((shfrom = isreg(regs[RT2])) >= 0) | |
204 | regfrom = copy(regs[RT2],"]"); | |
205 | if ((shto = isreg(regs[RT3])) >= 0 && shto<NUSE) | |
206 | { | |
207 | int regnum; | |
208 | ||
209 | if (uses[shto] != (pf = p->forw)) goto ashadd; | |
210 | if (pf->op != MOVA && pf->op != PUSHA) goto ashadd; | |
211 | if (pf->subop != BYTE) goto ashadd; | |
212 | splitrand(pf); | |
213 | if (!indexa(regs[RT1])) goto std; | |
214 | cp2 = regs[RT1]; | |
215 | if(!isstatic(cp2)) goto std; | |
216 | while (*cp2++ != '[') ; | |
217 | if (*cp2++ != 'r' || !isdigit(*cp2)) goto std; | |
218 | regnum = *cp2++ - '0'; | |
219 | if (isdigit(*cp2)) | |
220 | { | |
221 | if (cp2[1] != ']') goto std; | |
222 | regnum *= 10; regnum += *cp2 - '0'; | |
223 | } | |
224 | if (regnum != shto) goto std; | |
225 | if (shfrom >= 0) /* ashl $N,r*,r0 */ | |
226 | { | |
227 | delnode(p); | |
228 | if (shfrom != shto) | |
229 | { | |
230 | uses[shto] = NULL; splitrand(pf); | |
231 | cp2=regs[RT1]; while (*cp2++!='['); | |
232 | cp1=regfrom; while (*cp2++= *cp1++); | |
233 | newcode(pf); | |
234 | } | |
235 | } | |
236 | else | |
237 | { | |
238 | p->op = MOV; splitrand(p); | |
239 | strcpy(regs[RT1], regs[RT2]); | |
240 | strcpy(regs[RT2], regs[RT3]); | |
241 | regs[RT3][0] = '\0'; | |
242 | p->pop = 0; newcode(p); | |
243 | } | |
244 | switch (shcnt) | |
245 | { | |
246 | case 1: pf->subop = WORD; break; | |
247 | case 2: pf->subop = LONG; break; | |
248 | case 3: pf->subop = QUAD; break; | |
249 | } | |
250 | redunm++; nsaddr++; nchange++; | |
251 | } | |
252 | goto std; | |
253 | ashadd: | |
254 | /* at this point, RT2 and RT3 are guaranteed to be simple regs*/ | |
255 | if (shcnt == 1 && equstr(regs[RT2], regs[RT3])) | |
256 | { | |
257 | /* | |
258 | ** quickie: | |
259 | ** ashl $1,A,A > addl2 A,A | |
260 | */ | |
261 | p->op = ADD; p->subop = U(LONG,OP2); p->pop = 0; | |
262 | strcpy(regs[RT1], regs[RT2]); regs[RT3][0] = '\0'; | |
263 | newcode(p); nchange++; | |
264 | } | |
265 | goto std; | |
266 | } | |
267 | ||
268 | case EXTV: | |
269 | case EXTZV: | |
270 | { | |
271 | /* bit tests: | |
272 | ** extv A,$1,B,rC \ | |
273 | ** tstl rC > jbc A,B,D | |
274 | ** jeql D / | |
275 | ** | |
276 | ** also byte- and word-size fields: | |
277 | ** extv $n*8,$8,A,B > cvtbl n+A,B | |
278 | ** extv $n*16,$16,A,B > cvtwl n+A,B | |
279 | ** extzv $n*8,$8,A,B > movzbl n+A,B | |
280 | ** extzv $n*16,$16,A,B > movzwl n+A,B | |
281 | */ | |
282 | register struct node *pf; /* forward node */ | |
283 | register struct node *pn; /* next node (after pf) */ | |
284 | int flen; /* field length */ | |
285 | ||
286 | splitrand(p); | |
287 | if (regs[RT2][0] != '$') goto std; | |
288 | if ((flen = getnum(®s[RT2][1])) < 0) goto std; | |
289 | if (flen == 1) | |
290 | { | |
291 | register int extreg; /* reg extracted to */ | |
292 | ||
293 | extreg = isreg(regs[RT4]); | |
294 | if (extreg < 0 || extreg >= NUSE) goto std; | |
295 | if ((pf = p->forw)->op != TST) goto std; | |
296 | if (uses[extreg] && uses[extreg] != pf) goto std; | |
297 | splitrand(pf); | |
298 | if (extreg != isreg(regs[RT1])) goto std; | |
299 | if ((pn = pf->forw)->op != CBR) goto std; | |
300 | if (pn->subop != JEQ && pn->subop != JNE) goto std; | |
301 | delnode(p); delnode(pf); | |
302 | pn->subop = (pn->subop == JEQ) ? JBC : JBS; | |
303 | for(cp2=p->code; *cp2++!=',';); | |
304 | for(cp1=cp2; *cp1++!=',';); | |
305 | while (*cp1!=',') *cp2++= *cp1++; *cp2='\0'; | |
306 | pn->code = p->code; pn->pop = NULL; | |
307 | uses[extreg] = NULL; | |
308 | } | |
309 | else | |
310 | if (flen == 8 || flen == 16) | |
311 | { | |
312 | register int boff; /* bit offset */ | |
313 | register int coff; /* chunk (byte or word) offset*/ | |
314 | ||
315 | if (regs[RT1][0] != '$') goto std; | |
316 | if ((boff = getnum(®s[RT1][1])) < 0) goto std; | |
317 | coff = boff / flen; | |
318 | if (coff && (isreg(regs[RT3]) >= 0)) goto std; | |
319 | if (boff < 0 || (boff % flen) != 0) goto std; | |
320 | p->op = (p->op == EXTV) ? CVT : MOVZ; | |
321 | p->subop = U((flen == 8 ? BYTE : WORD), LONG); | |
322 | if (coff == 0) | |
323 | strcpy(regs[RT1], regs[RT3]); | |
324 | else | |
325 | sprintf(regs[RT1], "%d%s%s", coff, regs[RT3][0]=='(' ? "":"+", | |
326 | regs[RT3]); | |
327 | strcpy(regs[RT2], regs[RT4]); | |
328 | regs[RT3][0] = '\0'; regs[RT4][0] = '\0'; | |
329 | p->pop = 0; newcode(p); | |
330 | } | |
331 | nchange++; | |
332 | goto std; | |
333 | } | |
334 | ||
335 | case CMP: | |
336 | { | |
337 | /* comparison to -63 to -1: | |
338 | ** cmpl r0,$-1 > incl r0 | |
339 | ** jeql ... | |
340 | ** | |
341 | ** cmpl r0,$-63 > addl2 $63,r0 | |
342 | ** jeql ... | |
343 | */ | |
344 | register int num; | |
345 | register int reg; | |
346 | register struct node *regp = p->back; | |
347 | ||
348 | if (p->forw->op != CBR) goto std; | |
349 | if (p->forw->subop != JEQ && p->forw->subop != JNE) goto std; | |
350 | splitrand(p); | |
351 | if (strncmp(regs[RT2], "$-", 2) != 0) goto std; | |
352 | reg = r = isreg(regs[RT1]); | |
353 | if (r < 0) goto std; | |
354 | if (r < NUSE && uses[r] != 0) goto std; | |
355 | if (r >= NUSE && regp->op == MOV && p->subop == regp->subop) | |
356 | { | |
357 | if (*regp->code != 'r') goto std; | |
358 | reg = regp->code[1] - '0'; | |
359 | if (isdigit(regp->code[2]) || reg >= NUSE || uses[reg]) | |
360 | goto std; | |
361 | } | |
362 | if (r >= NUSE) goto std; | |
363 | if (reg != r) | |
364 | sprintf(regs[RT1], "r%d", reg); | |
365 | if ((num = getnum(®s[RT2][2])) <= 0 || num > 63) goto std; | |
366 | if (num == 1) | |
367 | { | |
368 | p->op = INC; regs[RT2][0] = '\0'; | |
369 | } | |
370 | else | |
371 | { | |
372 | register char *t; | |
373 | ||
374 | t=regs[RT1];regs[RT1]=regs[RT2];regs[RT2]=t; | |
375 | p->op = ADD; p->subop = U(p->subop, OP2); | |
376 | for (t = ®s[RT1][2]; t[-1] = *t; t++) ; | |
377 | } | |
378 | p->pop = 0; newcode(p); | |
379 | nchange++; | |
380 | goto std; | |
381 | } | |
382 | ||
383 | case JSB: | |
384 | if (equstr(p->code,"mcount")) {uses[0]=p; regs[0][0]= -1;} | |
385 | goto std; | |
386 | case JBR: case JMP: | |
387 | clearuse(); | |
388 | if (p->subop==RET || p->subop==RSB) {uses[0]=p; regs[0][0]= -1; break;} | |
389 | if (p->ref==0) goto std; /* jmp (r0) */ | |
390 | /* fall through */ | |
391 | case CBR: | |
392 | if (p->ref->ref!=0) for (r=NUSE;--r>=0;) | |
393 | if (biti[r] & (int)p->ref->ref) {uses[r]=p; regs[r][0]= -1;} | |
394 | case EROU: case JSW: | |
395 | case TEXT: case DATA: case BSS: case ALIGN: case WGEN: case END: ; | |
396 | } | |
397 | } | |
398 | for (p= &first; p!=0; p=p->forw) | |
399 | if (p->op==LABEL || p->op==DLABEL) p->ref=0; /* erase our tracks */ | |
400 | } | |
401 | ||
402 | rmove() | |
403 | { | |
404 | register struct node *p, *lastp; | |
405 | register int r; | |
406 | int r1; | |
407 | ||
408 | clearreg(); | |
409 | for (p=first.forw; p!=0; p = p->forw) { | |
410 | lastp=p; | |
411 | if (debug) { | |
412 | printf("Regs:\n"); | |
413 | for (r=0; r<NREG; r++) | |
414 | if (regs[r][0]) { | |
415 | r1=regs[r][0]; | |
416 | printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1); | |
417 | } | |
418 | printf("-\n"); | |
419 | } | |
420 | switch (p->op) { | |
421 | ||
422 | case CVT: | |
423 | splitrand(p); goto mov; | |
424 | ||
425 | case MOV: | |
426 | splitrand(p); | |
427 | if ((r = findrand(regs[RT1],p->subop)) >= 0) { | |
428 | if (r == isreg(regs[RT2]) && p->forw->op!=CBR) { | |
429 | delnode(p); redunm++; break; | |
430 | } | |
431 | } | |
432 | mov: | |
433 | repladdr(p); | |
434 | r = isreg(regs[RT1]); | |
435 | r1 = isreg(regs[RT2]); | |
436 | dest(regs[RT2],p->subop); | |
d1580951 BJ |
437 | if (r>=0) { |
438 | if (r1>=0) savereg(r1, regs[r]+1, p->subop); | |
439 | else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); | |
440 | } else if (r1>=0) savereg(r1, regs[RT1], p->subop); | |
441 | else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop); | |
f00d91fc BJ |
442 | break; |
443 | ||
444 | /* .rx,.wx */ | |
445 | case MFPR: | |
446 | case COM: | |
447 | case NEG: | |
448 | /* .rx,.wx or .rx,.rx,.wx */ | |
449 | case ADD: | |
450 | case SUB: | |
451 | case BIC: | |
452 | case BIS: | |
453 | case XOR: | |
454 | case MUL: | |
455 | case DIV: | |
456 | case ASH: | |
457 | case MOVZ: | |
458 | /* .rx,.rx,.rx,.wx */ | |
459 | case EXTV: | |
460 | case EXTZV: | |
461 | case INSV: | |
462 | splitrand(p); | |
463 | repladdr(p); | |
464 | dest(lastrand,p->subop); | |
465 | if (p->op==INSV) ccloc[0]=0; | |
466 | break; | |
467 | ||
468 | /* .mx or .wx */ | |
469 | case CLR: | |
470 | case INC: | |
471 | case DEC: | |
472 | splitrand(p); | |
473 | dest(lastrand,p->subop); | |
474 | if (p->op==CLR) | |
475 | if ((r = isreg(regs[RT1])) >= 0) | |
476 | savereg(r, "$0", p->subop); | |
477 | else | |
478 | setcon("$0", regs[RT1], p->subop); | |
479 | break; | |
480 | ||
481 | /* .rx */ | |
482 | case TST: | |
483 | case PUSH: | |
484 | splitrand(p); | |
485 | lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */ | |
486 | repladdr(p); | |
487 | if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1) | |
488 | && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop)) | |
489 | &&!source(lastrand)) { | |
490 | delnode(p); p = p->back; nrtst++; nchange++; | |
491 | } | |
492 | setcc(lastrand,p->subop); | |
493 | break; | |
494 | ||
495 | /* .rx,.rx,.rx */ | |
496 | case PROBER: | |
497 | case PROBEW: | |
498 | case CASE: | |
499 | case MOVC3: | |
500 | /* .rx,.rx */ | |
501 | case MTPR: | |
502 | case CALLS: | |
503 | case CMP: | |
504 | case BIT: | |
505 | splitrand(p); | |
506 | /* fool repladdr into doing right number of operands */ | |
9ad46604 | 507 | if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4]; |
c2a86b7c | 508 | /* else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */ |
9ad46604 | 509 | else if (p->op==MOVC3) lastrand=regs[RT1]; |
f00d91fc BJ |
510 | else lastrand=regs[RT3]; |
511 | repladdr(p); | |
512 | if (p->op==CALLS || p->op==MOVC3) clearreg(); | |
513 | if (p->op==BIT) bitopt(p); | |
514 | ccloc[0]=0; break; | |
515 | ||
516 | case CBR: | |
517 | if (p->subop>=JBC) { | |
518 | splitrand(p); | |
519 | if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ | |
520 | else lastrand=regs[RT2]; /* .mb destinations lose */ | |
521 | repladdr(p); | |
522 | } | |
523 | ccloc[0] = 0; | |
524 | break; | |
525 | ||
526 | case JBR: | |
527 | redunbr(p); | |
528 | ||
529 | /* .wx,.bb */ | |
530 | case SOB: | |
531 | ||
532 | default: | |
533 | clearreg(); | |
534 | } | |
535 | } | |
536 | } | |
537 | ||
538 | char * | |
539 | byondrd(p) register struct node *p; { | |
540 | /* return pointer to register which is "beyond last read/modify operand" */ | |
541 | if (OP2==(p->subop>>4)) return(regs[RT3]); | |
542 | switch (p->op) { | |
543 | case MFPR: | |
544 | case JSB: | |
545 | case PUSHA: | |
546 | case TST: case INC: case DEC: case PUSH: return(regs[RT2]); | |
547 | case MTPR: | |
548 | case BIT: case CMP: case CALLS: return(regs[RT3]); | |
549 | case PROBER: case PROBEW: | |
550 | case CASE: case MOVC3: return(regs[RT4]); | |
551 | } | |
552 | return(lastrand); | |
553 | } | |
554 | ||
555 | struct node * | |
556 | bflow(p) | |
557 | register struct node *p; | |
558 | { | |
559 | register char *cp1,*cp2,**preg; register int r; | |
560 | int flow= -1; | |
561 | struct node *olduse=0; | |
562 | splitrand(p); | |
563 | if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { | |
564 | if (equtype(p->subop,regs[r][0]) | |
565 | || ((p->op==CVT || p->op==MOVZ) | |
566 | && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { | |
567 | register int r2; | |
568 | if (regs[r][1]!=0) {/* send directly to destination */ | |
569 | if (p->op==INC || p->op==DEC) { | |
570 | if (p->op==DEC) p->op=SUB; else p->op=ADD; | |
571 | p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ | |
572 | p->pop=0; | |
573 | cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ | |
574 | cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; | |
575 | } | |
576 | cp1=regs[r]+1; cp2=lastrand; | |
577 | if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ | |
578 | p->pop=0; | |
579 | p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; | |
580 | } | |
581 | while (*cp2++= *cp1++); | |
582 | if (p->op==MOVA && p->forw->op==PUSH) { | |
583 | p->op=PUSHA; *regs[RT2]=0; p->pop=0; | |
584 | } else if (p->op==MOV && p->forw->op==PUSH) { | |
585 | p->op=PUSH ; *regs[RT2]=0; p->pop=0; | |
586 | } | |
587 | delnode(p->forw); | |
588 | if (0<=(r2=isreg(lastrand)) && r2<NUSE) { | |
589 | uses[r2]=uses[r]; uses[r]=0; | |
590 | } | |
591 | redun3(p,0); | |
592 | newcode(p); redunm++; flow=r; | |
593 | } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { | |
594 | /* superfluous fetch */ | |
595 | int nmatch; | |
033bb901 | 596 | char src[C2_ASIZE]; |
f00d91fc BJ |
597 | movit: |
598 | cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); | |
599 | splitrand(p->forw); | |
600 | if (p->forw->op != INC && p->forw->op != DEC) | |
601 | lastrand=byondrd(p->forw); | |
602 | nmatch=0; | |
603 | for (preg=regs+RT1;*preg!=lastrand;preg++) | |
604 | if (r==isreg(*preg)) { | |
605 | cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; | |
606 | } | |
607 | if (nmatch==1) { | |
608 | if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { | |
609 | p->forw->pop=0; | |
610 | p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; | |
611 | *cp1++='r'; *cp1++=r+'0'; *cp1=0; | |
612 | } | |
613 | delnode(p); p=p->forw; | |
614 | if (0<=(r2=isreg(src)) && r2<NUSE) { | |
615 | uses[r2]=uses[r]; uses[r]=0; | |
616 | } | |
617 | redun3(p,0); | |
618 | newcode(p); redunm++; flow=r; | |
619 | } else splitrand(p); | |
620 | } | |
621 | } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) | |
622 | && p->forw->subop&0xf /* if base or index, then forget it */ | |
623 | && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) | |
624 | && !indexa(cp1)) goto movit; | |
625 | } | |
626 | /* adjust 'lastrand' past any 'read' or 'modify' operands. */ | |
627 | lastrand=byondrd(p); | |
628 | /* a 'write' clobbers the register. */ | |
629 | if (0<=(r=isreg(lastrand)) && r<NUSE | |
630 | || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { | |
631 | /* writing a dead register is useless, but watch side effects */ | |
632 | switch (p->op) { | |
35307d30 | 633 | case ACB: |
f00d91fc | 634 | case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; |
35307d30 | 635 | default: |
f00d91fc BJ |
636 | if (uses[r]==0) {/* no direct uses, check for use of condition codes */ |
637 | register struct node *q=p; | |
638 | while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ | |
639 | if (q->op!=CBR) {/* ... and destroyed */ | |
640 | preg=regs+RT1; | |
641 | while (cp1= *preg++) { | |
642 | if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} | |
643 | if (source(cp1) || equstr(cp1,lastrand)) break; | |
644 | } | |
645 | } | |
646 | } | |
647 | flow=r; | |
648 | } | |
649 | } | |
650 | if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;} | |
651 | /* these two are here, rather than in bmove(), | |
652 | /* because I decided that it was better to go for 3-address code | |
653 | /* (save time) rather than fancy jbxx (save 1 byte) | |
654 | /* on sequences like bisl2 $64,r0; movl r0,foo | |
655 | */ | |
656 | if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} | |
657 | if (p->op==BIS) {bixprep(p,JBSS); lastrand=byondrd(p);} | |
658 | /* now look for 'read' or 'modify' (read & write) uses */ | |
659 | preg=regs+RT1; | |
660 | while (*(cp1= *preg++)) { | |
661 | /* check for r */ | |
662 | if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { | |
663 | uses[r]=p; cp2=regs[r]; *cp2++=p->subop; | |
d1580951 | 664 | if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ |
f00d91fc BJ |
665 | if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { |
666 | if (p->op==PUSH) cp1="-(sp)"; | |
667 | else { | |
668 | cp1=regs[RT2]; | |
669 | if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) | |
670 | uses[r]=olduse; /* reincarnation!! */ | |
671 | /* as in addl2 r0,r1; movl r1,r0; ret */ | |
672 | if (p->op!=MOV) cp1=0; | |
673 | } | |
674 | if (cp1) while (*cp2++= *cp1++); | |
d1580951 BJ |
675 | else *cp2=0; |
676 | } else *cp2=0; | |
f00d91fc BJ |
677 | continue; |
678 | } | |
679 | /* check for (r),(r)+,-(r),[r] */ | |
680 | do if (*cp1=='(' || *cp1=='[') {/* get register number */ | |
681 | char t; | |
682 | cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; | |
683 | if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { | |
684 | uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); | |
685 | } | |
686 | *cp1=t; | |
687 | } while (*++cp1); | |
688 | } | |
689 | /* pushax or movax possibility? */ | |
690 | cp1=regs[RT1]; | |
691 | if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { | |
692 | if (p->combop==T(MOV,LONG)) { | |
693 | if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { | |
694 | cp1=p->code; while (*cp1++!=','); p->code= --cp1; | |
695 | } | |
696 | p->combop=T(MOVA,LONG); ++p->code; p->pop=0; | |
697 | } else if (p->combop==T(PUSH,LONG)) { | |
698 | p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; | |
699 | } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) | |
700 | && 0<=(r=isreg(regs[RT2]))) { | |
701 | cp1=cp2=p->code; ++cp1; | |
702 | do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; | |
703 | do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; | |
704 | if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); | |
705 | else {p->combop=T(PUSHA,BYTE); *cp2=0;} | |
706 | if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;} | |
707 | p->pop=0; | |
708 | } | |
709 | } | |
710 | return(p); | |
711 | } | |
712 | ||
713 | ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ | |
714 | register int log; | |
715 | if (n==0 || n&(n-1)) return(-1); log=0; | |
716 | for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} | |
717 | } | |
718 | ||
719 | bitopt(p) register struct node *p; { | |
720 | /* change "bitx $<power_of_2>,a" followed by JEQ or JNE | |
721 | /* into JBC or JBS. watch out for I/O registers. (?) | |
722 | /* assumes that 'splitrand' has already been called. | |
723 | */ | |
724 | register char *cp1,*cp2; int b; | |
725 | cp1=regs[RT1]; cp2=regs[RT2]; | |
726 | if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || | |
727 | 0>(b=ispow2(getnum(cp1))) || | |
728 | p->subop!=BYTE && (source(cp2) || indexa(cp2))) return; | |
729 | if (b>=bitsize[p->subop]) {/* you dummy! */ | |
730 | if (source(cp2)) {/* side effect: auto increment or decrement */ | |
731 | p->pop=0; | |
732 | p->op=TST; --cp1; while (*cp1++= *cp2++); | |
733 | regs[RT2][0]=0; newcode(p); | |
734 | } else delnode(p); | |
735 | p = p->forw; | |
736 | if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} | |
737 | else delnode(p); | |
738 | nchange++; nbj++; return; | |
739 | } | |
740 | if (cp1=p->forw->code) {/* destination is not an internal label */ | |
741 | cp2=regs[RT3]; while (*cp2++= *cp1++); | |
742 | } | |
743 | if (b==0 && (p->subop==LONG || !indexa(regs[RT2]))) {/* JLB optimization, ala BLISS */ | |
744 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); | |
745 | cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); | |
746 | *(regs[RT3])=0; p->forw->subop += JLBC-JBC; | |
747 | p->forw->pop=0; | |
748 | } else { | |
749 | cp1=regs[RT1]+1; | |
750 | if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ | |
751 | } | |
752 | nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); | |
753 | p->labno = p->forw->labno; delnode(p->forw); | |
754 | p->pop=0; | |
755 | } | |
756 | ||
757 | isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ | |
758 | register int pos; register long t; | |
759 | t= ((n-1)|n) +1; | |
760 | if (n!=0 && (0==t || 0==n || 0<=ispow2(t))) { | |
761 | pos=0; while(!(n&1)) {n >>= 1; ++pos;} return(pos); | |
762 | } else return(-1); | |
763 | } | |
764 | ||
765 | bixprep(p,bix) register struct node *p; { | |
766 | /* initial setup, single-bit checking for bisopt, bicopt. | |
767 | /* return: 0->don't bother any more; 1->worthwhile trying | |
768 | */ | |
769 | register char *cp1,*cp2; | |
770 | splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; | |
771 | if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) | |
772 | || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); | |
773 | f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; | |
774 | if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE | |
775 | && pos<bitsize[p->subop&0xF]) { | |
776 | p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); | |
777 | p->pop=0; | |
778 | p->labno = p->ref->labno; | |
779 | if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} | |
780 | *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); | |
781 | } | |
782 | return(1); | |
783 | } | |
784 | ||
785 | ||
786 | struct node * | |
787 | bicopt(p) register struct node *p; { | |
788 | /* use field operations or MOVZ if possible. done as part of 'bflow'. | |
789 | */ | |
790 | register char *cp1,*cp2; int r; | |
3b60efae | 791 | char src[C2_ASIZE]; |
f00d91fc BJ |
792 | if (!bixprep(p,JBCC)) return(p); |
793 | if (f==0) {/* the BIC isolates low order bits */ | |
794 | siz=pos; pos=0; | |
795 | if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ | |
796 | /* save source of BICL in 'src' */ | |
797 | cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); | |
798 | if (p->back->op==ASH) {/* try for more */ | |
799 | splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; | |
800 | if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) | |
801 | && 0>(f=getnum(cp1)) && equstr(src,cp2) | |
802 | && 0<=(r=isreg(cp2)) && r<NUSE) {/* a good ASH */ | |
803 | pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); | |
804 | delnode(p->back); | |
805 | } | |
806 | } | |
807 | if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ | |
808 | splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; | |
809 | if (equstr(src,cp2) && okio(cp1) && !indexa(cp1) | |
810 | && 0<=(r=isreg(cp2)) && r<NUSE | |
811 | && bitsize[p->back->subop&0xF]>=(pos+siz) | |
812 | && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ | |
813 | cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); | |
814 | delnode(p->back); | |
815 | } | |
816 | } | |
817 | /* 'pos', 'siz' known; source of field is in 'src' */ | |
818 | splitrand(p); /* retrieve destination of BICL */ | |
819 | if (siz==8 && pos==0) { | |
820 | p->combop = T(MOVZ,U(BYTE,LONG)); | |
821 | sprintf(line,"%s,%s",src,lastrand); | |
822 | } else { | |
823 | p->combop = T(EXTZV,LONG); | |
824 | sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); | |
825 | } | |
826 | p->pop=0; | |
827 | p->code = copy(line); nfield++; return(p); | |
828 | }/* end EXTZV possibility */ | |
829 | }/* end low order bits */ | |
830 | /* unfortunately, INSV clears the condition codes, thus cannot be used */ | |
831 | /* else {/* see if BICL2 of positive field should be INSV $0 */ | |
832 | /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { | |
833 | /* p->combop = INSV; | |
834 | /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); | |
835 | /* p->code = copy(line); nfield++; return(p); | |
836 | /* } | |
837 | /* } | |
838 | */ | |
839 | return(p); | |
840 | } | |
841 | ||
842 | jumpsw() | |
843 | { | |
844 | register struct node *p, *p1; | |
845 | register t; | |
846 | int nj; | |
847 | ||
848 | t = 0; | |
849 | nj = 0; | |
850 | for (p=first.forw; p!=0; p = p->forw) | |
851 | p->seq = ++t; | |
852 | for (p=first.forw; p!=0; p = p1) { | |
853 | p1 = p->forw; | |
854 | if (p->op == CBR && p1->op==JBR && p->ref && p1->ref | |
855 | && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { | |
856 | if (p->ref==p1->ref) | |
857 | continue; | |
858 | p->subop = revbr[p->subop]; | |
859 | p->pop=0; | |
860 | t = p1->ref; | |
861 | p1->ref = p->ref; | |
862 | p->ref = t; | |
863 | t = p1->labno; | |
864 | p1->labno = p->labno; | |
865 | p->labno = t; | |
866 | #ifdef COPYCODE | |
867 | if (p->labno == 0) { | |
868 | t = p1->code; p1->code = p->code; p->code = t; | |
869 | } | |
870 | #endif | |
871 | nrevbr++; | |
872 | nj++; | |
873 | } | |
874 | } | |
875 | return(nj); | |
876 | } | |
877 | ||
878 | addsob() | |
879 | { | |
880 | register struct node *p, *p1, *p2, *p3; | |
881 | ||
882 | for (p = &first; (p1 = p->forw)!=0; p = p1) { | |
883 | if (p->combop==T(DEC,LONG) && p1->op==CBR) { | |
3b60efae | 884 | if (abs(p->seq - p1->ref->seq) > 8) continue; |
f00d91fc BJ |
885 | if (p1->subop==JGE || p1->subop==JGT) { |
886 | if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; | |
887 | p->pop=0; | |
888 | p->labno = p1->labno; delnode(p1); nsob++; | |
889 | } | |
890 | } else if (p->combop==T(INC,LONG)) { | |
891 | if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) | |
892 | && (p2=p1->forw->forw)->combop==T(CBR,JLE) | |
893 | && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 | |
894 | && p3->forw->op==LABEL && p3->forw==p2->ref) { | |
895 | /* change INC LAB: CMP to LAB: INC CMP */ | |
896 | p->back->forw=p1; p1->back=p->back; | |
897 | p->forw=p1->forw; p1->forw->back=p; | |
898 | p->back=p1; p1->forw=p; | |
899 | p1=p->forw; | |
900 | /* adjust beginning value by 1 */ | |
901 | p2=alloc(sizeof first); p2->combop=T(DEC,LONG); | |
902 | p2->pop=0; | |
903 | p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; | |
904 | p3->back=p2; p2->code=p->code; p2->labno=0; | |
905 | } | |
906 | if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { | |
907 | register char *cp1,*cp2; | |
908 | splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; | |
3b60efae | 909 | if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ |
f00d91fc BJ |
910 | if (p2->subop!=JLE) continue; |
911 | p->combop=T(ACB,LONG); | |
912 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ | |
913 | cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ | |
914 | cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ | |
915 | p->pop=0; newcode(p); | |
916 | p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; | |
917 | } else if (p2->subop==JLE || p2->subop==JLT) { | |
918 | if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; | |
919 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ | |
920 | cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ | |
921 | p->pop=0; newcode(p); | |
922 | p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; | |
923 | } | |
924 | } | |
925 | } | |
926 | } | |
927 | } | |
928 | ||
929 | abs(x) | |
930 | { | |
931 | return(x<0? -x: x); | |
932 | } | |
933 | ||
934 | equop(p1, p2) | |
935 | register struct node *p1; | |
936 | struct node *p2; | |
937 | { | |
938 | register char *cp1, *cp2; | |
939 | ||
940 | if (p1->combop != p2->combop) | |
941 | return(0); | |
942 | if (p1->op>0 && p1->op<MOV) | |
943 | return(0); | |
944 | if (p1->op==MOVA && p1->labno!=p2->labno) return(0); | |
945 | cp1 = p1->code; | |
946 | cp2 = p2->code; | |
947 | if (cp1==0 && cp2==0) | |
948 | return(1); | |
949 | if (cp1==0 || cp2==0) | |
950 | return(0); | |
951 | while (*cp1 == *cp2++) | |
952 | if (*cp1++ == 0) | |
953 | return(1); | |
954 | return(0); | |
955 | } | |
956 | ||
957 | delnode(p) register struct node *p; { | |
958 | p->back->forw = p->forw; | |
959 | p->forw->back = p->back; | |
960 | } | |
961 | ||
962 | decref(p) | |
963 | register struct node *p; | |
964 | { | |
965 | if (p && --p->refc <= 0) { | |
966 | nrlab++; | |
967 | delnode(p); | |
968 | } | |
969 | } | |
970 | ||
971 | struct node * | |
972 | nonlab(ap) | |
973 | struct node *ap; | |
974 | { | |
975 | register struct node *p; | |
976 | ||
977 | p = ap; | |
978 | while (p && p->op==LABEL) | |
979 | p = p->forw; | |
980 | return(p); | |
981 | } | |
982 | ||
983 | clearuse() { | |
984 | register struct node **i; | |
985 | for (i=uses+NUSE; i>uses;) *--i=0; | |
986 | } | |
987 | ||
988 | clearreg() { | |
989 | register short **i; | |
990 | for (i=regs+NREG; i>regs;) **--i=0; | |
991 | conloc[0] = 0; ccloc[0] = 0; | |
992 | } | |
993 | ||
994 | savereg(ai, s, type) | |
995 | register char *s; | |
996 | { | |
997 | register char *p, *sp; | |
998 | ||
999 | sp = p = regs[ai]; | |
1000 | if (source(s)) /* side effects in addressing */ | |
1001 | return; | |
1002 | /* if any indexing, must be parameter or local */ | |
1003 | /* indirection (as in "*-4(fp)") is ok, however */ | |
1004 | *p++ = type; | |
1005 | while (*p++ = *s) | |
1006 | if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} | |
1007 | } | |
1008 | ||
1009 | dest(s,type) | |
1010 | register char *s; | |
1011 | { | |
1012 | register int i; | |
1013 | ||
1014 | source(s); /* handle addressing side effects */ | |
1015 | if ((i = isreg(s)) >= 0) { | |
1016 | *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ | |
0594fe30 BJ |
1017 | switch(type & 0xF){ |
1018 | case DFLOAT: /* clobber two at once */ | |
1019 | /*FALLTHROUGH*/ | |
1020 | case GFLOAT: | |
1021 | *(short *)(regs[i+1]) = 0; | |
1022 | break; | |
1023 | case HFLOAT: /* clobber four at once */ | |
1024 | *(short *)(regs[i+1]) = 0; | |
1025 | *(short *)(regs[i+2]) = 0; | |
1026 | *(short *)(regs[i+3]) = 0; | |
1027 | break; | |
1028 | } | |
1029 | switch((type>>4)&0xF){ | |
1030 | case DFLOAT: /* clobber two at once */ | |
1031 | /*FALLTHROUGH*/ | |
1032 | case GFLOAT: | |
1033 | *(short *)(regs[i+1]) = 0; | |
1034 | break; | |
1035 | case HFLOAT: /* clobber four at once */ | |
1036 | *(short *)(regs[i+1]) = 0; | |
1037 | *(short *)(regs[i+2]) = 0; | |
1038 | *(short *)(regs[i+3]) = 0; | |
1039 | break; | |
1040 | } | |
1041 | /* | |
1042 | if (DFLOAT==(type&0xF) || DFLOAT==((type>>4)&0xF)) | |
1043 | *(short *)(regs[i+1]) = 0; | |
1044 | */ | |
f00d91fc BJ |
1045 | } |
1046 | for (i=NREG; --i>=0;) | |
1047 | if (regs[i][1]=='*' && equstr(s, regs[i]+2)) | |
1048 | *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ | |
1049 | while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ | |
1050 | *(short *)(regs[i]) = 0; | |
1051 | if (!natural(s)) {/* wild store, everything except constants vanishes */ | |
1052 | for (i=NREG; --i>=0;) if (regs[i][1] != '$') *(short *)(regs[i]) = 0; | |
1053 | conloc[0] = 0; ccloc[0] = 0; | |
1054 | } else setcc(s,type); /* natural destinations set condition codes */ | |
1055 | } | |
1056 | ||
1057 | splitrand(p) struct node *p; { | |
1058 | /* separate operands at commas, set up 'regs' and 'lastrand' */ | |
1059 | register char *p1, *p2; register char **preg; | |
1060 | preg=regs+RT1; | |
1061 | if (p1=p->code) while (*p1) { | |
1062 | lastrand=p2= *preg++; | |
1063 | while (*p1) if (','==(*p2++= *p1++)) {--p2; break;} | |
1064 | *p2=0; | |
1065 | } | |
1066 | while (preg<(regs+RT1+5)) *(*preg++)=0; | |
1067 | } | |
1068 | ||
1069 | compat(have, want) { | |
1070 | register int hsrc, hdst; | |
1071 | if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ | |
1072 | hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; | |
0594fe30 BJ |
1073 | if (want>=FFLOAT) return(hdst==want && hsrc==want); |
1074 | /* FLOAT, DFLOAT not compat: rounding */ | |
1075 | return(hsrc>=want && hdst>=want && hdst<FFLOAT); | |
f00d91fc BJ |
1076 | } |
1077 | ||
1078 | equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} | |
1079 | ||
1080 | findrand(as, type) | |
1081 | char *as; | |
1082 | { | |
1083 | register char **i; | |
1084 | for (i = regs+NREG; --i>=regs;) { | |
1085 | if (**i && equstr(*i+1, as) && compat(**i,type)) | |
1086 | return(i-regs); | |
1087 | } | |
1088 | return(-1); | |
1089 | } | |
1090 | ||
1091 | isreg(s) | |
1092 | register char *s; | |
1093 | { | |
1094 | if (*s++!='r' || !isdigit(*s++)) return(-1); | |
1095 | if (*s==0) return(*--s-'0'); | |
1096 | if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); | |
1097 | return(-1); | |
1098 | } | |
1099 | ||
1100 | check() | |
1101 | { | |
1102 | register struct node *p, *lp; | |
1103 | ||
1104 | lp = &first; | |
1105 | for (p=first.forw; p!=0; p = p->forw) { | |
1106 | if (p->back != lp) | |
1107 | abort(-1); | |
1108 | lp = p; | |
1109 | } | |
1110 | } | |
1111 | ||
1112 | source(ap) | |
1113 | char *ap; | |
1114 | { | |
1115 | register char *p1, *p2; | |
1116 | ||
1117 | p1 = ap; | |
1118 | p2 = p1; | |
1119 | if (*p1==0) | |
1120 | return(0); | |
1121 | while (*p2++ && *(p2-1)!='['); | |
1122 | if (*p1=='-' && *(p1+1)=='(' | |
1123 | || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' | |
1124 | || *(p2-2)=='+') { | |
1125 | while (*p1 && *p1++!='r'); | |
1126 | if (isdigit(*p1++)) | |
1127 | if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; | |
1128 | else *(short *)(regs[*--p1-'0'])=0; | |
1129 | return(1); | |
1130 | } | |
1131 | return(0); | |
1132 | } | |
1133 | ||
1134 | newcode(p) struct node *p; { | |
1135 | register char *p1,*p2,**preg; | |
1136 | preg=regs+RT1; p2=line; | |
1137 | while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} | |
1138 | *--p2=0; | |
1139 | p->code=copy(line); | |
1140 | } | |
1141 | ||
1142 | repladdr(p) | |
1143 | struct node *p; | |
1144 | { | |
1145 | register r; | |
1146 | register char *p1, *p2; | |
1147 | char **preg; int nrepl; | |
1148 | ||
1149 | preg=regs+RT1; nrepl=0; | |
1150 | while (lastrand!=(p1= *preg++)) | |
1151 | if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { | |
1152 | *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; | |
1153 | nrepl++; nsaddr++; | |
1154 | } | |
1155 | if (nrepl) newcode(p); | |
1156 | } | |
1157 | ||
1158 | /* movedat() | |
1159 | /* { | |
1160 | /* register struct node *p1, *p2; | |
1161 | /* struct node *p3; | |
1162 | /* register seg; | |
1163 | /* struct node data; | |
1164 | /* struct node *datp; | |
1165 | /* | |
1166 | /* if (first.forw == 0) | |
1167 | /* return; | |
1168 | /* datp = &data; | |
1169 | /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { | |
1170 | /* if (p1->op == DATA) { | |
1171 | /* p2 = p1->forw; | |
1172 | /* while (p2 && p2->op!=TEXT) | |
1173 | /* p2 = p2->forw; | |
1174 | /* if (p2==0) | |
1175 | /* break; | |
1176 | /* p3 = p1->back; | |
1177 | /* p1->back->forw = p2->forw; | |
1178 | /* p2->forw->back = p3; | |
1179 | /* p2->forw = 0; | |
1180 | /* datp->forw = p1; | |
1181 | /* p1->back = datp; | |
1182 | /* p1 = p3; | |
1183 | /* datp = p2; | |
1184 | /* } | |
1185 | /* } | |
1186 | /* if (data.forw) { | |
1187 | /* datp->forw = first.forw; | |
1188 | /* first.forw->back = datp; | |
1189 | /* data.forw->back = &first; | |
1190 | /* first.forw = data.forw; | |
1191 | /* } | |
1192 | /* seg = -1; | |
1193 | /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { | |
1194 | /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { | |
1195 | /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { | |
1196 | /* p1->back->forw = p1->forw; | |
1197 | /* p1->forw->back = p1->back; | |
1198 | /* p1 = p1->back; | |
1199 | /* continue; | |
1200 | /* } | |
1201 | /* seg = p1->op; | |
1202 | /* } | |
1203 | /* } | |
1204 | /* } | |
1205 | */ | |
1206 | ||
1207 | redunbr(p) | |
1208 | register struct node *p; | |
1209 | { | |
1210 | register struct node *p1; | |
1211 | register char *ap1; | |
1212 | char *ap2; | |
1213 | ||
1214 | if ((p1 = p->ref) == 0) | |
1215 | return; | |
1216 | p1 = nonlab(p1); | |
1217 | if (p1->op==TST) { | |
1218 | splitrand(p1); | |
1219 | savereg(RT2, "$0", p1->subop); | |
1220 | } else if (p1->op==CMP) | |
1221 | splitrand(p1); | |
1222 | else | |
1223 | return; | |
1224 | if (p1->forw->op==CBR) { | |
1225 | ap1 = findcon(RT1, p1->subop); | |
1226 | ap2 = findcon(RT2, p1->subop); | |
1227 | p1 = p1->forw; | |
1228 | if (compare(p1->subop, ap1, ap2)) { | |
1229 | nredunj++; | |
1230 | nchange++; | |
1231 | decref(p->ref); | |
1232 | p->ref = p1->ref; | |
1233 | p->labno = p1->labno; | |
1234 | #ifdef COPYCODE | |
1235 | if (p->labno == 0) | |
1236 | p->code = p1->code; | |
1237 | if (p->ref) | |
1238 | #endif | |
1239 | p->ref->refc++; | |
1240 | } | |
1241 | } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && | |
1242 | equtype(ccloc[0],p1->subop)) { | |
1243 | p1=insertl(p1->forw); decref(p->ref); p->ref=p1; | |
1244 | nrtst++; nchange++; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | char * | |
1249 | findcon(i, type) | |
1250 | { | |
1251 | register char *p; | |
1252 | register r; | |
1253 | ||
1254 | p = regs[i]; | |
1255 | if (*p=='$') | |
1256 | return(p); | |
1257 | if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) | |
1258 | return(regs[r]+1); | |
1259 | if (equstr(p, conloc)) | |
1260 | return(conval+1); | |
1261 | return(p); | |
1262 | } | |
1263 | ||
1264 | compare(op, acp1, acp2) | |
1265 | char *acp1, *acp2; | |
1266 | { | |
1267 | register char *cp1, *cp2; | |
1268 | register n1; | |
1269 | int n2; int sign; | |
1270 | ||
1271 | cp1 = acp1; | |
1272 | cp2 = acp2; | |
1273 | if (*cp1++ != '$' || *cp2++ != '$') | |
1274 | return(0); | |
1275 | n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} | |
1276 | while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} | |
1277 | n2 = n1; | |
1278 | n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} | |
1279 | while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} | |
1280 | if (*cp1=='+') | |
1281 | cp1++; | |
1282 | if (*cp2=='+') | |
1283 | cp2++; | |
1284 | do { | |
1285 | if (*cp1++ != *cp2) | |
1286 | return(0); | |
1287 | } while (*cp2++); | |
1288 | cp1 = n1; | |
1289 | cp2 = n2; | |
1290 | switch(op) { | |
1291 | ||
1292 | case JEQ: | |
1293 | return(cp1 == cp2); | |
1294 | case JNE: | |
1295 | return(cp1 != cp2); | |
1296 | case JLE: | |
1297 | return(((int)cp1) <= ((int)cp2)); | |
1298 | case JGE: | |
1299 | return(((int)cp1) >= ((int)cp2)); | |
1300 | case JLT: | |
1301 | return(((int)cp1) < ((int)cp2)); | |
1302 | case JGT: | |
1303 | return(((int)cp1) > ((int)cp2)); | |
1304 | case JLO: | |
1305 | return(cp1 < cp2); | |
1306 | case JHI: | |
1307 | return(cp1 > cp2); | |
1308 | case JLOS: | |
1309 | return(cp1 <= cp2); | |
1310 | case JHIS: | |
1311 | return(cp1 >= cp2); | |
1312 | } | |
1313 | return(0); | |
1314 | } | |
1315 | ||
1316 | setcon(cv, cl, type) | |
1317 | register char *cv, *cl; | |
1318 | { | |
1319 | register char *p; | |
1320 | ||
1321 | if (*cv != '$') | |
1322 | return; | |
1323 | if (!natural(cl)) | |
1324 | return; | |
1325 | p = conloc; | |
1326 | while (*p++ = *cl++); | |
1327 | p = conval; | |
1328 | *p++ = type; | |
1329 | while (*p++ = *cv++); | |
1330 | } | |
1331 | ||
1332 | equstr(p1, p2) | |
1333 | register char *p1, *p2; | |
1334 | { | |
1335 | do { | |
1336 | if (*p1++ != *p2) | |
1337 | return(0); | |
1338 | } while (*p2++); | |
1339 | return(1); | |
1340 | } | |
1341 | ||
1342 | setcc(ap,type) | |
1343 | char *ap; | |
1344 | { | |
1345 | register char *p, *p1; | |
1346 | ||
1347 | p = ap; | |
1348 | if (!natural(p)) { | |
1349 | ccloc[0] = 0; | |
1350 | return; | |
1351 | } | |
1352 | p1 = ccloc; | |
1353 | *p1++ = type; | |
1354 | while (*p1++ = *p++); | |
1355 | } | |
1356 | ||
1357 | okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ | |
1358 | if (ioflag && (!natural(p) || 0>getnum(p))) return(0); | |
1359 | return(1); | |
1360 | } | |
1361 | ||
1362 | indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ | |
1363 | while (*p) if (*p++=='[') return(1); | |
1364 | return(0); | |
1365 | } | |
1366 | ||
1367 | natural(p) | |
1368 | register char *p; | |
1369 | {/* 1->simple local, parameter, global, or register; 0->otherwise */ | |
1370 | if (*p=='*' || *p=='(' || *p=='-'&&*(p+1)=='(' || *p=='$'&&getnum(p+1)) | |
1371 | return(0); | |
1372 | while (*p++); | |
1373 | p--; | |
1374 | if (*--p=='+' || *p==']' || *p==')' && *(p-2)!='a' && *(p-2)!='f') | |
1375 | return(0); | |
1376 | return(1); | |
1377 | } | |
1378 | ||
1379 | /* | |
1380 | ** Tell if an argument is most likely static. | |
1381 | */ | |
1382 | ||
1383 | isstatic(cp) | |
1384 | register char *cp; | |
1385 | { | |
1386 | if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) | |
1387 | return (1); | |
1388 | return (0); | |
1389 | } |