utah rcsid 1.8 87/09/10 12:54:29: The last fix was too liberal with the
[unix-history] / usr / src / old / pcc / c2.vax / c21.c
CommitLineData
41feb04a 1#ifndef lint
91c626f2 2static char sccsid[] = "@(#)c21.c 4.22 %G%";
41feb04a 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
15int ioflag;
16int biti[NUSE] = {1,2,4,8,16,32};
0594fe30 17int bitsize[] = { /* index by type codes */
a24e0abe
RH
18 0, /* 0 not allocated */
19 8, /* 1 BYTE */
20 16, /* 2 WORD */
21 32, /* 3 LONG */
a2897263 22 32, /* 4 FFLOAT */
a24e0abe
RH
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
33int pos,siz; long f; /* for bit field communication */
34struct node *uses[NUSE]; /* for backwards flow analysis */
35char *lastrand; /* last operand of instruction */
36struct node *bflow();
37struct node *bicopt();
38char *findcon();
41feb04a 39char *strcpy();
f00d91fc
BJ
40
41redun3(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
56bmove() {
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 = &regs[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(&regs[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(&regs[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++;
725aa107 262 goto std;
f00d91fc 263 }
f00d91fc
BJ
264ashadd:
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(&regs[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(&regs[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(&regs[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 = &regs[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
415rmove()
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 }
448mov:
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
554char *
555byondrd(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
571struct node *
572bflow(p)
573register 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) {
a2897263
DS
580 if ((p->op==CVT || p->op==MOVZ)
581 && (p->forw->op==CVT || p->forw->op==MOVZ)
582 && p->forw->subop&0xf
91c626f2 583 && equtype(p->subop,p->forw->subop)
a2897263
DS
584 && !source(cp1=regs[RT1])
585 && !indexa(cp1)) goto movit;
f00d91fc
BJ
586 if (equtype(p->subop,regs[r][0])
587 || ((p->op==CVT || p->op==MOVZ)
588 && 0xf&regs[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) {
589 register int r2;
590 if (regs[r][1]!=0) {/* send directly to destination */
591 if (p->op==INC || p->op==DEC) {
592 if (p->op==DEC) p->op=SUB; else p->op=ADD;
593 p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */
594 p->pop=0;
595 cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */
596 cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0;
597 }
598 cp1=regs[r]+1; cp2=lastrand;
599 if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */
600 p->pop=0;
601 p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3];
602 }
603 while (*cp2++= *cp1++);
604 if (p->op==MOVA && p->forw->op==PUSH) {
605 p->op=PUSHA; *regs[RT2]=0; p->pop=0;
606 } else if (p->op==MOV && p->forw->op==PUSH) {
607 p->op=PUSH ; *regs[RT2]=0; p->pop=0;
608 }
609 delnode(p->forw);
610 if (0<=(r2=isreg(lastrand)) && r2<NUSE) {
611 uses[r2]=uses[r]; uses[r]=0;
612 }
41feb04a 613 (void) redun3(p,0);
f00d91fc
BJ
614 newcode(p); redunm++; flow=r;
615 } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) {
616 /* superfluous fetch */
617 int nmatch;
033bb901 618 char src[C2_ASIZE];
f00d91fc
BJ
619 movit:
620 cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++);
621 splitrand(p->forw);
622 if (p->forw->op != INC && p->forw->op != DEC)
623 lastrand=byondrd(p->forw);
624 nmatch=0;
625 for (preg=regs+RT1;*preg!=lastrand;preg++)
626 if (r==isreg(*preg)) {
627 cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch;
628 }
629 if (nmatch==1) {
630 if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) {
631 p->forw->pop=0;
632 p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3];
633 *cp1++='r'; *cp1++=r+'0'; *cp1=0;
634 }
635 delnode(p); p=p->forw;
636 if (0<=(r2=isreg(src)) && r2<NUSE) {
637 uses[r2]=uses[r]; uses[r]=0;
638 }
41feb04a 639 (void) redun3(p,0);
f00d91fc
BJ
640 newcode(p); redunm++; flow=r;
641 } else splitrand(p);
642 }
643 } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ)
644 && p->forw->subop&0xf /* if base or index, then forget it */
645 && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1])
646 && !indexa(cp1)) goto movit;
647 }
648 /* adjust 'lastrand' past any 'read' or 'modify' operands. */
649 lastrand=byondrd(p);
650 /* a 'write' clobbers the register. */
651 if (0<=(r=isreg(lastrand)) && r<NUSE
652 || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) {
653 /* writing a dead register is useless, but watch side effects */
654 switch (p->op) {
35307d30 655 case ACB:
f00d91fc 656 case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break;
35307d30 657 default:
f00d91fc
BJ
658 if (uses[r]==0) {/* no direct uses, check for use of condition codes */
659 register struct node *q=p;
660 while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */
661 if (q->op!=CBR) {/* ... and destroyed */
662 preg=regs+RT1;
663 while (cp1= *preg++) {
664 if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);}
665 if (source(cp1) || equstr(cp1,lastrand)) break;
666 }
667 }
668 }
669 flow=r;
670 }
671 }
41feb04a
RC
672 if (0<=(r=flow)) {
673 olduse=uses[r];
674 uses[r]=0;
675 regs[r][0]=regs[r][1]=0;
676 }
f00d91fc
BJ
677 /* these two are here, rather than in bmove(),
678 /* because I decided that it was better to go for 3-address code
679 /* (save time) rather than fancy jbxx (save 1 byte)
680 /* on sequences like bisl2 $64,r0; movl r0,foo
681 */
682 if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);}
41feb04a 683 if (p->op==BIS) {(void) bixprep(p,JBSS); lastrand=byondrd(p);}
f00d91fc
BJ
684 /* now look for 'read' or 'modify' (read & write) uses */
685 preg=regs+RT1;
686 while (*(cp1= *preg++)) {
687 /* check for r */
688 if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) {
689 uses[r]=p; cp2=regs[r]; *cp2++=p->subop;
d1580951 690 if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */
f00d91fc
BJ
691 if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) {
692 if (p->op==PUSH) cp1="-(sp)";
693 else {
694 cp1=regs[RT2];
695 if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0)
696 uses[r]=olduse; /* reincarnation!! */
697 /* as in addl2 r0,r1; movl r1,r0; ret */
698 if (p->op!=MOV) cp1=0;
699 }
700 if (cp1) while (*cp2++= *cp1++);
d1580951
BJ
701 else *cp2=0;
702 } else *cp2=0;
f00d91fc
BJ
703 continue;
704 }
705 /* check for (r),(r)+,-(r),[r] */
706 do if (*cp1=='(' || *cp1=='[') {/* get register number */
707 char t;
708 cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0;
709 if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) {
710 uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4);
711 }
712 *cp1=t;
713 } while (*++cp1);
714 }
715 /* pushax or movax possibility? */
716 cp1=regs[RT1];
717 if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) {
718 if (p->combop==T(MOV,LONG)) {
719 if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) {
720 cp1=p->code; while (*cp1++!=','); p->code= --cp1;
721 }
722 p->combop=T(MOVA,LONG); ++p->code; p->pop=0;
723 } else if (p->combop==T(PUSH,LONG)) {
724 p->combop=T(PUSHA,LONG); ++p->code; p->pop=0;
725 } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3))
726 && 0<=(r=isreg(regs[RT2]))) {
727 cp1=cp2=p->code; ++cp1;
728 do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='[';
729 do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']';
730 if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE);
731 else {p->combop=T(PUSHA,BYTE); *cp2=0;}
3464f46e
KM
732 if (r < NUSE && uses[r] == 0) {
733 uses[r]=p;
734 regs[r][0]=OPX<<4;
735 }
f00d91fc
BJ
736 p->pop=0;
737 }
738 }
739 return(p);
740}
741
742ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */
743 register int log;
744 if (n==0 || n&(n-1)) return(-1); log=0;
745 for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);}
746}
747
748bitopt(p) register struct node *p; {
749 /* change "bitx $<power_of_2>,a" followed by JEQ or JNE
750 /* into JBC or JBS. watch out for I/O registers. (?)
751 /* assumes that 'splitrand' has already been called.
752 */
753 register char *cp1,*cp2; int b;
754 cp1=regs[RT1]; cp2=regs[RT2];
755 if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 ||
f69fb1e7
DS
756 0>(b=ispow2(getnum(cp1)))) return;
757 if (p->subop!=BYTE && !(b==0 && p->subop==LONG) &&
758 (source(cp2) || indexa(cp2))) return;
f00d91fc
BJ
759 if (b>=bitsize[p->subop]) {/* you dummy! */
760 if (source(cp2)) {/* side effect: auto increment or decrement */
761 p->pop=0;
762 p->op=TST; --cp1; while (*cp1++= *cp2++);
763 regs[RT2][0]=0; newcode(p);
764 } else delnode(p);
765 p = p->forw;
766 if (p->subop==JEQ) {p->combop=JBR; p->pop=0;}
767 else delnode(p);
768 nchange++; nbj++; return;
769 }
770 if (cp1=p->forw->code) {/* destination is not an internal label */
771 cp2=regs[RT3]; while (*cp2++= *cp1++);
772 }
f69fb1e7
DS
773 if (b==0 && (p->subop==LONG || !(source(regs[RT2]) || indexa(regs[RT2])))) {
774 /* JLB optimization, ala BLISS */
f00d91fc
BJ
775 cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++);
776 cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++);
777 *(regs[RT3])=0; p->forw->subop += JLBC-JBC;
778 p->forw->pop=0;
779 } else {
780 cp1=regs[RT1]+1;
781 if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */
782 }
783 nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8);
784 p->labno = p->forw->labno; delnode(p->forw);
785 p->pop=0;
786}
787
788isfield(n) register long n; {/* -1 -> no; else -> position of low bit */
41feb04a 789 register int p; register long t;
f00d91fc 790 t= ((n-1)|n) +1;
eea63b82 791 if (n!=0 && (0==t || 0<=ispow2(t))) {
41feb04a 792 p=0; while(!(n&1)) {n >>= 1; ++p;} return(p);
f00d91fc
BJ
793 } else return(-1);
794}
795
796bixprep(p,bix) register struct node *p; {
797/* initial setup, single-bit checking for bisopt, bicopt.
798/* return: 0->don't bother any more; 1->worthwhile trying
799*/
800 register char *cp1,*cp2;
801 splitrand(p); cp1=regs[RT1]; cp2=regs[RT2];
802 if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1)))
803 || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0);
804 f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos;
805 if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE
806 && pos<bitsize[p->subop&0xF]) {
807 p->ref = insertl(p->forw); p->combop = CBR | (bix<<8);
808 p->pop=0;
809 p->labno = p->ref->labno;
810 if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;}
811 *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0);
812 }
813 return(1);
814}
815
816
817struct node *
818bicopt(p) register struct node *p; {
819/* use field operations or MOVZ if possible. done as part of 'bflow'.
820*/
821 register char *cp1,*cp2; int r;
3b60efae 822 char src[C2_ASIZE];
41feb04a 823 char lhssiz, sop;
f00d91fc
BJ
824 if (!bixprep(p,JBCC)) return(p);
825 if (f==0) {/* the BIC isolates low order bits */
826 siz=pos; pos=0;
827 if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */
828 /* save source of BICL in 'src' */
829 cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++);
830 if (p->back->op==ASH) {/* try for more */
831 splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3];
832 if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2])
833 && 0>(f=getnum(cp1)) && equstr(src,cp2)
eea63b82
RC
834 && 0<=(r=isreg(cp2)) && r<NUSE
835 && siz-f <= 32) { /* a good ASH */
f00d91fc
BJ
836 pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++);
837 delnode(p->back);
838 }
839 }
49c27f9c
RC
840 /*
841 * 'pos', 'siz' known; find out the size of the
842 * left-hand operand of what the bicl will turn into.
843 */
844 if (pos==0 && siz==16)
845 lhssiz = WORD; /* movzwl */
846 else
847 lhssiz = BYTE; /* movzbl or extzvl */
f00d91fc
BJ
848 if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */
849 splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2];
49c27f9c
RC
850 /*
851 * If indexa(cp1) || autoid(cp1), the fold may
852 * still be OK if the CVT/MOVZ has the same
853 * size operand on its left size as what we
854 * will turn the bicl into.
855 * However, if the CVT is from a float or
856 * double, forget it!
857 */
41feb04a 858 sop = p->back->subop&0xF; /* type of LHS of CVT/MOVZ */
49c27f9c 859 if (equstr(src,cp2) && okio(cp1)
41feb04a
RC
860 && sop != FFLOAT && sop != DFLOAT
861 && sop != GFLOAT && sop != HFLOAT
862 && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == sop)
f00d91fc 863 && 0<=(r=isreg(cp2)) && r<NUSE
41feb04a 864 && bitsize[sop]>=(pos+siz)
f00d91fc
BJ
865 && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */
866 cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++);
867 delnode(p->back);
868 }
869 }
870 /* 'pos', 'siz' known; source of field is in 'src' */
871 splitrand(p); /* retrieve destination of BICL */
49c27f9c
RC
872 if ((siz==8 || siz==16) && pos==0) {
873 p->combop = T(MOVZ,U(lhssiz,LONG));
f00d91fc
BJ
874 sprintf(line,"%s,%s",src,lastrand);
875 } else {
876 p->combop = T(EXTZV,LONG);
877 sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand);
878 }
879 p->pop=0;
880 p->code = copy(line); nfield++; return(p);
881 }/* end EXTZV possibility */
882 }/* end low order bits */
883/* unfortunately, INSV clears the condition codes, thus cannot be used */
884/* else {/* see if BICL2 of positive field should be INSV $0 */
885/* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) {
886/* p->combop = INSV;
887/* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand);
888/* p->code = copy(line); nfield++; return(p);
889/* }
890/* }
891*/
892 return(p);
893}
894
895jumpsw()
896{
897 register struct node *p, *p1;
41feb04a
RC
898 register struct node *tp;
899 long tl;
900 char *tcp;
901 int ti;
f00d91fc
BJ
902 int nj;
903
41feb04a 904 ti = 0;
f00d91fc
BJ
905 nj = 0;
906 for (p=first.forw; p!=0; p = p->forw)
41feb04a 907 p->seq = ++ti;
f00d91fc
BJ
908 for (p=first.forw; p!=0; p = p1) {
909 p1 = p->forw;
910 if (p->op == CBR && p1->op==JBR && p->ref && p1->ref
911 && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) {
912 if (p->ref==p1->ref)
913 continue;
914 p->subop = revbr[p->subop];
915 p->pop=0;
41feb04a 916 tp = p1->ref;
f00d91fc 917 p1->ref = p->ref;
41feb04a
RC
918 p->ref = tp;
919 tl = p1->labno;
f00d91fc 920 p1->labno = p->labno;
41feb04a 921 p->labno = tl;
f00d91fc
BJ
922#ifdef COPYCODE
923 if (p->labno == 0) {
41feb04a
RC
924 tcp = p1->code;
925 p1->code = p->code;
926 p->code = tcp;
f00d91fc
BJ
927 }
928#endif
929 nrevbr++;
930 nj++;
931 }
932 }
933 return(nj);
934}
935
936addsob()
937{
938 register struct node *p, *p1, *p2, *p3;
939
940 for (p = &first; (p1 = p->forw)!=0; p = p1) {
941 if (p->combop==T(DEC,LONG) && p1->op==CBR) {
3b60efae 942 if (abs(p->seq - p1->ref->seq) > 8) continue;
f00d91fc
BJ
943 if (p1->subop==JGE || p1->subop==JGT) {
944 if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR;
945 p->pop=0;
946 p->labno = p1->labno; delnode(p1); nsob++;
947 }
948 } else if (p->combop==T(INC,LONG)) {
949 if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG)
950 && (p2=p1->forw->forw)->combop==T(CBR,JLE)
951 && (p3=p2->ref->back)->combop==JBR && p3->ref==p1
952 && p3->forw->op==LABEL && p3->forw==p2->ref) {
953 /* change INC LAB: CMP to LAB: INC CMP */
954 p->back->forw=p1; p1->back=p->back;
955 p->forw=p1->forw; p1->forw->back=p;
956 p->back=p1; p1->forw=p;
957 p1=p->forw;
958 /* adjust beginning value by 1 */
959 p2=alloc(sizeof first); p2->combop=T(DEC,LONG);
960 p2->pop=0;
961 p2->forw=p3; p2->back=p3->back; p3->back->forw=p2;
962 p3->back=p2; p2->code=p->code; p2->labno=0;
963 }
964 if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) {
965 register char *cp1,*cp2;
966 splitrand(p1); if (!equstr(p->code,regs[RT1])) continue;
3b60efae 967 if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */
f00d91fc
BJ
968 if (p2->subop!=JLE) continue;
969 p->combop=T(ACB,LONG);
970 cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
971 cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */
972 cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */
973 p->pop=0; newcode(p);
974 p->labno = p2->labno; delnode(p2); delnode(p1); nsob++;
975 } else if (p2->subop==JLE || p2->subop==JLT) {
976 if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS;
977 cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */
978 cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */
979 p->pop=0; newcode(p);
980 p->labno = p2->labno; delnode(p2); delnode(p1); nsob++;
981 }
982 }
983 }
984 }
985}
986
f00d91fc
BJ
987equop(p1, p2)
988register struct node *p1;
989struct node *p2;
990{
991 register char *cp1, *cp2;
992
993 if (p1->combop != p2->combop)
994 return(0);
d5bdb635
DS
995 if (p1->combop == 0 && p1->pop != p2->pop)
996 return(0);
f00d91fc
BJ
997 if (p1->op>0 && p1->op<MOV)
998 return(0);
41feb04a
RC
999 switch (p1->combop) {
1000 case EROU: case JSW: case TEXT: case DATA:
1001 case BSS: case ALIGN: case WGEN: case END:
1002 /*
1003 * Consider all pseudo-ops to be unique.
1004 */
1005 return(0);
1006 }
f00d91fc
BJ
1007 if (p1->op==MOVA && p1->labno!=p2->labno) return(0);
1008 cp1 = p1->code;
1009 cp2 = p2->code;
1010 if (cp1==0 && cp2==0)
1011 return(1);
1012 if (cp1==0 || cp2==0)
1013 return(0);
1014 while (*cp1 == *cp2++)
1015 if (*cp1++ == 0)
1016 return(1);
1017 return(0);
1018}
1019
41feb04a 1020#ifndef delnode
f00d91fc
BJ
1021delnode(p) register struct node *p; {
1022 p->back->forw = p->forw;
1023 p->forw->back = p->back;
1024}
41feb04a 1025#endif
f00d91fc 1026
41feb04a 1027#ifndef decref
f00d91fc
BJ
1028decref(p)
1029register struct node *p;
1030{
1031 if (p && --p->refc <= 0) {
1032 nrlab++;
1033 delnode(p);
1034 }
1035}
41feb04a 1036#endif
f00d91fc
BJ
1037
1038struct node *
1039nonlab(ap)
1040struct node *ap;
1041{
1042 register struct node *p;
1043
1044 p = ap;
1045 while (p && p->op==LABEL)
1046 p = p->forw;
1047 return(p);
1048}
1049
1050clearuse() {
1051 register struct node **i;
1052 for (i=uses+NUSE; i>uses;) *--i=0;
1053}
1054
1055clearreg() {
41feb04a
RC
1056 register char **i;
1057 for (i=regs; i<regs+NREG; ++i) {
1058 **i = 0;
1059 *(*i+1) = 0;
1060 }
1061 conloc[0] = 0;
1062 ccloc[0] = 0;
f00d91fc
BJ
1063}
1064
1065savereg(ai, s, type)
1066register char *s;
1067{
1068 register char *p, *sp;
1069
1070 sp = p = regs[ai];
1071 if (source(s)) /* side effects in addressing */
1072 return;
1073 /* if any indexing, must be parameter or local */
1074 /* indirection (as in "*-4(fp)") is ok, however */
1075 *p++ = type;
1076 while (*p++ = *s)
1077 if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;}
1078}
1079
1080dest(s,type)
1081register char *s;
1082{
1083 register int i;
1084
41feb04a 1085 (void) source(s); /* handle addressing side effects */
6b028d93
RC
1086 if (!natural(s)) {
1087 /* wild store, everything except constants vanishes */
1088 for (i=NREG; --i>=0;)
41feb04a
RC
1089 if (regs[i][1] != '$')
1090 regs[i][0] = regs[i][1] = 0;
6b028d93
RC
1091 conloc[0] = 0; ccloc[0] = 0;
1092 return;
1093 }
f00d91fc 1094 if ((i = isreg(s)) >= 0) {
41feb04a
RC
1095 /* if register destination, that reg is a goner */
1096 regs[i][0] = regs[i][1] = 0;
0594fe30
BJ
1097 switch(type & 0xF){
1098 case DFLOAT: /* clobber two at once */
1099 /*FALLTHROUGH*/
1100 case GFLOAT:
41feb04a 1101 regs[i+1][0] = regs[i+1][1] = 0;
0594fe30
BJ
1102 break;
1103 case HFLOAT: /* clobber four at once */
41feb04a
RC
1104 regs[i+1][0] = regs[i+1][1] = 0;
1105 regs[i+2][0] = regs[i+2][1] = 0;
1106 regs[i+3][0] = regs[i+3][1] = 0;
0594fe30
BJ
1107 break;
1108 }
1109 switch((type>>4)&0xF){
1110 case DFLOAT: /* clobber two at once */
1111 /*FALLTHROUGH*/
1112 case GFLOAT:
41feb04a 1113 regs[i+1][0] = regs[i+1][1] = 0;
0594fe30
BJ
1114 break;
1115 case HFLOAT: /* clobber four at once */
41feb04a
RC
1116 regs[i+1][0] = regs[i+1][1] = 0;
1117 regs[i+2][0] = regs[i+2][1] = 0;
1118 regs[i+3][0] = regs[i+3][1] = 0;
0594fe30
BJ
1119 break;
1120 }
f00d91fc
BJ
1121 }
1122 for (i=NREG; --i>=0;)
1123 if (regs[i][1]=='*' && equstr(s, regs[i]+2))
41feb04a 1124 regs[i][0] = regs[i][1] = 0; /* previous indirection through destination is invalid */
f00d91fc 1125 while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */
41feb04a 1126 regs[i][0] = regs[i][1] = 0;
6b028d93
RC
1127 if (*conloc && equstr(conloc, s))
1128 conloc[0] = 0;
1129 setcc(s, type); /* natural destinations set condition codes */
f00d91fc
BJ
1130}
1131
f00d91fc 1132/* separate operands at commas, set up 'regs' and 'lastrand' */
6b028d93
RC
1133splitrand(p) struct node *p; {
1134 register char *p1, *p2;
1135 register char **preg;
1136
1137 preg = regs+RT1;
1138 if (p1 = p->code)
1139 while (*p1) {
1140 lastrand = p2 = *preg++;
1141 while (*p1)
1142 if (',' == (*p2++ = *p1++)) {
1143 --p2;
1144 break;
1145 }
1146 *p2 = 0;
1147 }
1148 while (preg < (regs+RT1+5))
1149 *(*preg++) = 0;
f00d91fc
BJ
1150}
1151
1152compat(have, want) {
1153register int hsrc, hdst;
1154if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */
cdd0bda3
DS
1155hsrc=have&0xF;
1156if (0==(hdst=((have>>4)&0xF)) || (hdst>=OP2 && hdst<=OPX)) hdst=hsrc;
0594fe30
BJ
1157if (want>=FFLOAT) return(hdst==want && hsrc==want);
1158 /* FLOAT, DFLOAT not compat: rounding */
1159return(hsrc>=want && hdst>=want && hdst<FFLOAT);
f00d91fc
BJ
1160}
1161
1162equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));}
1163
1164findrand(as, type)
1165char *as;
1166{
1167 register char **i;
1168 for (i = regs+NREG; --i>=regs;) {
1169 if (**i && equstr(*i+1, as) && compat(**i,type))
1170 return(i-regs);
1171 }
1172 return(-1);
1173}
1174
1175isreg(s)
1176register char *s;
1177{
1178 if (*s++!='r' || !isdigit(*s++)) return(-1);
1179 if (*s==0) return(*--s-'0');
1180 if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0');
1181 return(-1);
1182}
1183
1184check()
1185{
1186 register struct node *p, *lp;
1187
1188 lp = &first;
1189 for (p=first.forw; p!=0; p = p->forw) {
41feb04a
RC
1190 if (p->back != lp) {
1191 fprintf(stderr, "c2: failed internal consistency check -- help!\n");
1192 exit(-1);
1193 }
f00d91fc
BJ
1194 lp = p;
1195 }
1196}
1197
1198source(ap)
1199char *ap;
1200{
1201 register char *p1, *p2;
1202
1203 p1 = ap;
1204 p2 = p1;
1205 if (*p1==0)
1206 return(0);
1207 while (*p2++ && *(p2-1)!='[');
1208 if (*p1=='-' && *(p1+1)=='('
1209 || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='('
1210 || *(p2-2)=='+') {
1211 while (*p1 && *p1++!='r');
1212 if (isdigit(*p1++))
41feb04a
RC
1213 if (isdigit(*p1))
1214 regs[10+*p1-'0'][0] = regs[10+*p1-'0'][1] = 0;
1215 else {
1216 --p1;
1217 regs[*p1-'0'][0] = regs[*p1-'0'][1] = 0;
1218 }
f00d91fc
BJ
1219 return(1);
1220 }
1221 return(0);
1222}
1223
1224newcode(p) struct node *p; {
1225 register char *p1,*p2,**preg;
1226 preg=regs+RT1; p2=line;
1227 while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';}
1228 *--p2=0;
1229 p->code=copy(line);
1230}
1231
1232repladdr(p)
1233struct node *p;
1234{
1235 register r;
41feb04a 1236 register char *p1;
f00d91fc
BJ
1237 char **preg; int nrepl;
1238
1239 preg=regs+RT1; nrepl=0;
1240 while (lastrand!=(p1= *preg++))
1241 if (!source(p1) && 0<=(r=findrand(p1,p->subop))) {
1242 *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0;
1243 nrepl++; nsaddr++;
1244 }
1245 if (nrepl) newcode(p);
1246}
1247
1248/* movedat()
1249/* {
1250/* register struct node *p1, *p2;
1251/* struct node *p3;
1252/* register seg;
1253/* struct node data;
1254/* struct node *datp;
1255/*
1256/* if (first.forw == 0)
1257/* return;
1258/* datp = &data;
1259/* for (p1 = first.forw; p1!=0; p1 = p1->forw) {
1260/* if (p1->op == DATA) {
1261/* p2 = p1->forw;
1262/* while (p2 && p2->op!=TEXT)
1263/* p2 = p2->forw;
1264/* if (p2==0)
1265/* break;
1266/* p3 = p1->back;
1267/* p1->back->forw = p2->forw;
1268/* p2->forw->back = p3;
1269/* p2->forw = 0;
1270/* datp->forw = p1;
1271/* p1->back = datp;
1272/* p1 = p3;
1273/* datp = p2;
1274/* }
1275/* }
1276/* if (data.forw) {
1277/* datp->forw = first.forw;
1278/* first.forw->back = datp;
1279/* data.forw->back = &first;
1280/* first.forw = data.forw;
1281/* }
1282/* seg = -1;
1283/* for (p1 = first.forw; p1!=0; p1 = p1->forw) {
1284/* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) {
1285/* if (p1->op == seg || p1->forw&&p1->forw->op==seg) {
1286/* p1->back->forw = p1->forw;
1287/* p1->forw->back = p1->back;
1288/* p1 = p1->back;
1289/* continue;
1290/* }
1291/* seg = p1->op;
1292/* }
1293/* }
1294/* }
1295*/
1296
1297redunbr(p)
1298register struct node *p;
1299{
1300 register struct node *p1;
1301 register char *ap1;
1302 char *ap2;
1303
1304 if ((p1 = p->ref) == 0)
1305 return;
1306 p1 = nonlab(p1);
1307 if (p1->op==TST) {
1308 splitrand(p1);
1309 savereg(RT2, "$0", p1->subop);
1310 } else if (p1->op==CMP)
1311 splitrand(p1);
1312 else
1313 return;
1314 if (p1->forw->op==CBR) {
1315 ap1 = findcon(RT1, p1->subop);
1316 ap2 = findcon(RT2, p1->subop);
1317 p1 = p1->forw;
1318 if (compare(p1->subop, ap1, ap2)) {
1319 nredunj++;
1320 nchange++;
1321 decref(p->ref);
1322 p->ref = p1->ref;
1323 p->labno = p1->labno;
1324#ifdef COPYCODE
1325 if (p->labno == 0)
1326 p->code = p1->code;
f00d91fc 1327#endif
6b028d93 1328 if (p->ref)
f00d91fc
BJ
1329 p->ref->refc++;
1330 }
1331 } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) &&
1332 equtype(ccloc[0],p1->subop)) {
1333 p1=insertl(p1->forw); decref(p->ref); p->ref=p1;
1334 nrtst++; nchange++;
1335 }
1336}
1337
1338char *
1339findcon(i, type)
1340{
1341 register char *p;
1342 register r;
1343
1344 p = regs[i];
1345 if (*p=='$')
1346 return(p);
1347 if ((r = isreg(p)) >= 0 && compat(regs[r][0],type))
1348 return(regs[r]+1);
1349 if (equstr(p, conloc))
1350 return(conval+1);
1351 return(p);
1352}
1353
41feb04a 1354compare(opc, acp1, acp2)
f00d91fc
BJ
1355char *acp1, *acp2;
1356{
1357 register char *cp1, *cp2;
1358 register n1;
1359 int n2; int sign;
1360
1361 cp1 = acp1;
1362 cp2 = acp2;
1363 if (*cp1++ != '$' || *cp2++ != '$')
1364 return(0);
1365 n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;}
1366 while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;}
1367 n2 = n1;
1368 n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;}
1369 while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;}
1370 if (*cp1=='+')
1371 cp1++;
1372 if (*cp2=='+')
1373 cp2++;
1374 do {
1375 if (*cp1++ != *cp2)
1376 return(0);
1377 } while (*cp2++);
41feb04a 1378 switch(opc) {
f00d91fc
BJ
1379
1380 case JEQ:
41feb04a 1381 return(n1 == n2);
f00d91fc 1382 case JNE:
41feb04a 1383 return(n1 != n2);
f00d91fc 1384 case JLE:
41feb04a 1385 return(n1 <= n2);
f00d91fc 1386 case JGE:
41feb04a 1387 return(n1 >= n2);
f00d91fc 1388 case JLT:
41feb04a 1389 return(n1 < n2);
f00d91fc 1390 case JGT:
41feb04a 1391 return(n1 > n2);
f00d91fc 1392 case JLO:
41feb04a 1393 return((unsigned) n1 < (unsigned) n2);
f00d91fc 1394 case JHI:
41feb04a 1395 return((unsigned) n1 > (unsigned) n2);
f00d91fc 1396 case JLOS:
41feb04a 1397 return((unsigned) n1 <= (unsigned) n2);
f00d91fc 1398 case JHIS:
41feb04a 1399 return((unsigned) n1 >= (unsigned) n2);
f00d91fc
BJ
1400 }
1401 return(0);
1402}
1403
1404setcon(cv, cl, type)
1405register char *cv, *cl;
1406{
1407 register char *p;
1408
1409 if (*cv != '$')
1410 return;
1411 if (!natural(cl))
1412 return;
1413 p = conloc;
1414 while (*p++ = *cl++);
1415 p = conval;
1416 *p++ = type;
1417 while (*p++ = *cv++);
1418}
1419
1420equstr(p1, p2)
1421register char *p1, *p2;
1422{
1423 do {
1424 if (*p1++ != *p2)
1425 return(0);
1426 } while (*p2++);
1427 return(1);
1428}
1429
1430setcc(ap,type)
1431char *ap;
1432{
1433 register char *p, *p1;
1434
1435 p = ap;
1436 if (!natural(p)) {
1437 ccloc[0] = 0;
1438 return;
1439 }
1440 p1 = ccloc;
1441 *p1++ = type;
1442 while (*p1++ = *p++);
1443}
1444
1445okio(p) register char *p; {/* 0->probable I/O space address; 1->not */
1446 if (ioflag && (!natural(p) || 0>getnum(p))) return(0);
1447 return(1);
1448}
1449
1450indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */
1451 while (*p) if (*p++=='[') return(1);
1452 return(0);
1453}
1454
1455natural(p)
1456register char *p;
1457{/* 1->simple local, parameter, global, or register; 0->otherwise */
6b028d93 1458 if (*p=='*' || *p=='(' || *p=='-'&&p[1]=='(' || *p=='$'&&getnum(p+1))
f00d91fc
BJ
1459 return(0);
1460 while (*p++);
1461 p--;
6b028d93 1462 if (*--p=='+' || *p==']' || *p==')' && p[-2]!='a' && p[-2]!='f')
f00d91fc
BJ
1463 return(0);
1464 return(1);
1465}
1466
1467/*
1468** Tell if an argument is most likely static.
1469*/
1470
1471isstatic(cp)
1472register char *cp;
1473{
1474 if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.'))
1475 return (1);
1476 return (0);
1477}
49c27f9c
RC
1478
1479autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */
1480 if (*p == '-' && *(p+1) == '(') return(1);
1481 while (*p) p++;
1482 if (*--p == '+' && *--p == ')') return(1);
1483 return(0);
1484 }