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