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