Commit | Line | Data |
---|---|---|
b9626436 | 1 | static char sccsid[] = "@(#)c21.c 4.11 %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; | |
eea63b82 | 762 | if (n!=0 && (0==t || 0<=ispow2(t))) { |
f00d91fc BJ |
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) | |
eea63b82 RC |
805 | && 0<=(r=isreg(cp2)) && r<NUSE |
806 | && siz-f <= 32) { /* a good ASH */ | |
f00d91fc BJ |
807 | pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); |
808 | delnode(p->back); | |
809 | } | |
810 | } | |
49c27f9c RC |
811 | /* |
812 | * 'pos', 'siz' known; find out the size of the | |
813 | * left-hand operand of what the bicl will turn into. | |
814 | */ | |
815 | if (pos==0 && siz==16) | |
816 | lhssiz = WORD; /* movzwl */ | |
817 | else | |
818 | lhssiz = BYTE; /* movzbl or extzvl */ | |
f00d91fc BJ |
819 | if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ |
820 | splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; | |
49c27f9c RC |
821 | /* |
822 | * If indexa(cp1) || autoid(cp1), the fold may | |
823 | * still be OK if the CVT/MOVZ has the same | |
824 | * size operand on its left size as what we | |
825 | * will turn the bicl into. | |
826 | * However, if the CVT is from a float or | |
827 | * double, forget it! | |
828 | */ | |
829 | subop = p->back->subop&0xF; /* type of LHS of CVT/MOVZ */ | |
830 | if (equstr(src,cp2) && okio(cp1) | |
831 | && subop != FFLOAT && subop != DFLOAT | |
832 | && subop != GFLOAT && subop != HFLOAT | |
833 | && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == subop) | |
f00d91fc BJ |
834 | && 0<=(r=isreg(cp2)) && r<NUSE |
835 | && bitsize[p->back->subop&0xF]>=(pos+siz) | |
836 | && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ | |
837 | cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); | |
838 | delnode(p->back); | |
839 | } | |
840 | } | |
841 | /* 'pos', 'siz' known; source of field is in 'src' */ | |
842 | splitrand(p); /* retrieve destination of BICL */ | |
49c27f9c RC |
843 | if ((siz==8 || siz==16) && pos==0) { |
844 | p->combop = T(MOVZ,U(lhssiz,LONG)); | |
f00d91fc BJ |
845 | sprintf(line,"%s,%s",src,lastrand); |
846 | } else { | |
847 | p->combop = T(EXTZV,LONG); | |
848 | sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); | |
849 | } | |
850 | p->pop=0; | |
851 | p->code = copy(line); nfield++; return(p); | |
852 | }/* end EXTZV possibility */ | |
853 | }/* end low order bits */ | |
854 | /* unfortunately, INSV clears the condition codes, thus cannot be used */ | |
855 | /* else {/* see if BICL2 of positive field should be INSV $0 */ | |
856 | /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { | |
857 | /* p->combop = INSV; | |
858 | /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); | |
859 | /* p->code = copy(line); nfield++; return(p); | |
860 | /* } | |
861 | /* } | |
862 | */ | |
863 | return(p); | |
864 | } | |
865 | ||
866 | jumpsw() | |
867 | { | |
868 | register struct node *p, *p1; | |
869 | register t; | |
870 | int nj; | |
871 | ||
872 | t = 0; | |
873 | nj = 0; | |
874 | for (p=first.forw; p!=0; p = p->forw) | |
875 | p->seq = ++t; | |
876 | for (p=first.forw; p!=0; p = p1) { | |
877 | p1 = p->forw; | |
878 | if (p->op == CBR && p1->op==JBR && p->ref && p1->ref | |
879 | && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { | |
880 | if (p->ref==p1->ref) | |
881 | continue; | |
882 | p->subop = revbr[p->subop]; | |
883 | p->pop=0; | |
884 | t = p1->ref; | |
885 | p1->ref = p->ref; | |
886 | p->ref = t; | |
887 | t = p1->labno; | |
888 | p1->labno = p->labno; | |
889 | p->labno = t; | |
890 | #ifdef COPYCODE | |
891 | if (p->labno == 0) { | |
892 | t = p1->code; p1->code = p->code; p->code = t; | |
893 | } | |
894 | #endif | |
895 | nrevbr++; | |
896 | nj++; | |
897 | } | |
898 | } | |
899 | return(nj); | |
900 | } | |
901 | ||
902 | addsob() | |
903 | { | |
904 | register struct node *p, *p1, *p2, *p3; | |
905 | ||
906 | for (p = &first; (p1 = p->forw)!=0; p = p1) { | |
907 | if (p->combop==T(DEC,LONG) && p1->op==CBR) { | |
3b60efae | 908 | if (abs(p->seq - p1->ref->seq) > 8) continue; |
f00d91fc BJ |
909 | if (p1->subop==JGE || p1->subop==JGT) { |
910 | if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; | |
911 | p->pop=0; | |
912 | p->labno = p1->labno; delnode(p1); nsob++; | |
913 | } | |
914 | } else if (p->combop==T(INC,LONG)) { | |
915 | if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) | |
916 | && (p2=p1->forw->forw)->combop==T(CBR,JLE) | |
917 | && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 | |
918 | && p3->forw->op==LABEL && p3->forw==p2->ref) { | |
919 | /* change INC LAB: CMP to LAB: INC CMP */ | |
920 | p->back->forw=p1; p1->back=p->back; | |
921 | p->forw=p1->forw; p1->forw->back=p; | |
922 | p->back=p1; p1->forw=p; | |
923 | p1=p->forw; | |
924 | /* adjust beginning value by 1 */ | |
925 | p2=alloc(sizeof first); p2->combop=T(DEC,LONG); | |
926 | p2->pop=0; | |
927 | p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; | |
928 | p3->back=p2; p2->code=p->code; p2->labno=0; | |
929 | } | |
930 | if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { | |
931 | register char *cp1,*cp2; | |
932 | splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; | |
3b60efae | 933 | if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ |
f00d91fc BJ |
934 | if (p2->subop!=JLE) continue; |
935 | p->combop=T(ACB,LONG); | |
936 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ | |
937 | cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ | |
938 | cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ | |
939 | p->pop=0; newcode(p); | |
940 | p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; | |
941 | } else if (p2->subop==JLE || p2->subop==JLT) { | |
942 | if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; | |
943 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ | |
944 | cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ | |
945 | p->pop=0; newcode(p); | |
946 | p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; | |
947 | } | |
948 | } | |
949 | } | |
950 | } | |
951 | } | |
952 | ||
953 | abs(x) | |
954 | { | |
955 | return(x<0? -x: x); | |
956 | } | |
957 | ||
958 | equop(p1, p2) | |
959 | register struct node *p1; | |
960 | struct node *p2; | |
961 | { | |
962 | register char *cp1, *cp2; | |
963 | ||
964 | if (p1->combop != p2->combop) | |
965 | return(0); | |
966 | if (p1->op>0 && p1->op<MOV) | |
967 | return(0); | |
968 | if (p1->op==MOVA && p1->labno!=p2->labno) return(0); | |
969 | cp1 = p1->code; | |
970 | cp2 = p2->code; | |
971 | if (cp1==0 && cp2==0) | |
972 | return(1); | |
973 | if (cp1==0 || cp2==0) | |
974 | return(0); | |
975 | while (*cp1 == *cp2++) | |
976 | if (*cp1++ == 0) | |
977 | return(1); | |
978 | return(0); | |
979 | } | |
980 | ||
981 | delnode(p) register struct node *p; { | |
982 | p->back->forw = p->forw; | |
983 | p->forw->back = p->back; | |
984 | } | |
985 | ||
986 | decref(p) | |
987 | register struct node *p; | |
988 | { | |
989 | if (p && --p->refc <= 0) { | |
990 | nrlab++; | |
991 | delnode(p); | |
992 | } | |
993 | } | |
994 | ||
995 | struct node * | |
996 | nonlab(ap) | |
997 | struct node *ap; | |
998 | { | |
999 | register struct node *p; | |
1000 | ||
1001 | p = ap; | |
1002 | while (p && p->op==LABEL) | |
1003 | p = p->forw; | |
1004 | return(p); | |
1005 | } | |
1006 | ||
1007 | clearuse() { | |
1008 | register struct node **i; | |
1009 | for (i=uses+NUSE; i>uses;) *--i=0; | |
1010 | } | |
1011 | ||
1012 | clearreg() { | |
1013 | register short **i; | |
1014 | for (i=regs+NREG; i>regs;) **--i=0; | |
1015 | conloc[0] = 0; ccloc[0] = 0; | |
1016 | } | |
1017 | ||
1018 | savereg(ai, s, type) | |
1019 | register char *s; | |
1020 | { | |
1021 | register char *p, *sp; | |
1022 | ||
1023 | sp = p = regs[ai]; | |
1024 | if (source(s)) /* side effects in addressing */ | |
1025 | return; | |
1026 | /* if any indexing, must be parameter or local */ | |
1027 | /* indirection (as in "*-4(fp)") is ok, however */ | |
1028 | *p++ = type; | |
1029 | while (*p++ = *s) | |
1030 | if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} | |
1031 | } | |
1032 | ||
1033 | dest(s,type) | |
1034 | register char *s; | |
1035 | { | |
1036 | register int i; | |
1037 | ||
1038 | source(s); /* handle addressing side effects */ | |
1039 | if ((i = isreg(s)) >= 0) { | |
1040 | *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ | |
0594fe30 BJ |
1041 | switch(type & 0xF){ |
1042 | case DFLOAT: /* clobber two at once */ | |
1043 | /*FALLTHROUGH*/ | |
1044 | case GFLOAT: | |
1045 | *(short *)(regs[i+1]) = 0; | |
1046 | break; | |
1047 | case HFLOAT: /* clobber four at once */ | |
1048 | *(short *)(regs[i+1]) = 0; | |
1049 | *(short *)(regs[i+2]) = 0; | |
1050 | *(short *)(regs[i+3]) = 0; | |
1051 | break; | |
1052 | } | |
1053 | switch((type>>4)&0xF){ | |
1054 | case DFLOAT: /* clobber two at once */ | |
1055 | /*FALLTHROUGH*/ | |
1056 | case GFLOAT: | |
1057 | *(short *)(regs[i+1]) = 0; | |
1058 | break; | |
1059 | case HFLOAT: /* clobber four at once */ | |
1060 | *(short *)(regs[i+1]) = 0; | |
1061 | *(short *)(regs[i+2]) = 0; | |
1062 | *(short *)(regs[i+3]) = 0; | |
1063 | break; | |
1064 | } | |
1065 | /* | |
1066 | if (DFLOAT==(type&0xF) || DFLOAT==((type>>4)&0xF)) | |
1067 | *(short *)(regs[i+1]) = 0; | |
1068 | */ | |
f00d91fc BJ |
1069 | } |
1070 | for (i=NREG; --i>=0;) | |
1071 | if (regs[i][1]=='*' && equstr(s, regs[i]+2)) | |
1072 | *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ | |
1073 | while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ | |
1074 | *(short *)(regs[i]) = 0; | |
1075 | if (!natural(s)) {/* wild store, everything except constants vanishes */ | |
1076 | for (i=NREG; --i>=0;) if (regs[i][1] != '$') *(short *)(regs[i]) = 0; | |
1077 | conloc[0] = 0; ccloc[0] = 0; | |
1078 | } else setcc(s,type); /* natural destinations set condition codes */ | |
1079 | } | |
1080 | ||
1081 | splitrand(p) struct node *p; { | |
1082 | /* separate operands at commas, set up 'regs' and 'lastrand' */ | |
1083 | register char *p1, *p2; register char **preg; | |
1084 | preg=regs+RT1; | |
1085 | if (p1=p->code) while (*p1) { | |
1086 | lastrand=p2= *preg++; | |
1087 | while (*p1) if (','==(*p2++= *p1++)) {--p2; break;} | |
1088 | *p2=0; | |
1089 | } | |
1090 | while (preg<(regs+RT1+5)) *(*preg++)=0; | |
1091 | } | |
1092 | ||
1093 | compat(have, want) { | |
1094 | register int hsrc, hdst; | |
1095 | if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ | |
1096 | hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; | |
0594fe30 BJ |
1097 | if (want>=FFLOAT) return(hdst==want && hsrc==want); |
1098 | /* FLOAT, DFLOAT not compat: rounding */ | |
1099 | return(hsrc>=want && hdst>=want && hdst<FFLOAT); | |
f00d91fc BJ |
1100 | } |
1101 | ||
1102 | equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} | |
1103 | ||
1104 | findrand(as, type) | |
1105 | char *as; | |
1106 | { | |
1107 | register char **i; | |
1108 | for (i = regs+NREG; --i>=regs;) { | |
1109 | if (**i && equstr(*i+1, as) && compat(**i,type)) | |
1110 | return(i-regs); | |
1111 | } | |
1112 | return(-1); | |
1113 | } | |
1114 | ||
1115 | isreg(s) | |
1116 | register char *s; | |
1117 | { | |
1118 | if (*s++!='r' || !isdigit(*s++)) return(-1); | |
1119 | if (*s==0) return(*--s-'0'); | |
1120 | if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); | |
1121 | return(-1); | |
1122 | } | |
1123 | ||
1124 | check() | |
1125 | { | |
1126 | register struct node *p, *lp; | |
1127 | ||
1128 | lp = &first; | |
1129 | for (p=first.forw; p!=0; p = p->forw) { | |
1130 | if (p->back != lp) | |
1131 | abort(-1); | |
1132 | lp = p; | |
1133 | } | |
1134 | } | |
1135 | ||
1136 | source(ap) | |
1137 | char *ap; | |
1138 | { | |
1139 | register char *p1, *p2; | |
1140 | ||
1141 | p1 = ap; | |
1142 | p2 = p1; | |
1143 | if (*p1==0) | |
1144 | return(0); | |
1145 | while (*p2++ && *(p2-1)!='['); | |
1146 | if (*p1=='-' && *(p1+1)=='(' | |
1147 | || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' | |
1148 | || *(p2-2)=='+') { | |
1149 | while (*p1 && *p1++!='r'); | |
1150 | if (isdigit(*p1++)) | |
1151 | if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; | |
1152 | else *(short *)(regs[*--p1-'0'])=0; | |
1153 | return(1); | |
1154 | } | |
1155 | return(0); | |
1156 | } | |
1157 | ||
1158 | newcode(p) struct node *p; { | |
1159 | register char *p1,*p2,**preg; | |
1160 | preg=regs+RT1; p2=line; | |
1161 | while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} | |
1162 | *--p2=0; | |
1163 | p->code=copy(line); | |
1164 | } | |
1165 | ||
1166 | repladdr(p) | |
1167 | struct node *p; | |
1168 | { | |
1169 | register r; | |
1170 | register char *p1, *p2; | |
1171 | char **preg; int nrepl; | |
1172 | ||
1173 | preg=regs+RT1; nrepl=0; | |
1174 | while (lastrand!=(p1= *preg++)) | |
1175 | if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { | |
1176 | *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; | |
1177 | nrepl++; nsaddr++; | |
1178 | } | |
1179 | if (nrepl) newcode(p); | |
1180 | } | |
1181 | ||
1182 | /* movedat() | |
1183 | /* { | |
1184 | /* register struct node *p1, *p2; | |
1185 | /* struct node *p3; | |
1186 | /* register seg; | |
1187 | /* struct node data; | |
1188 | /* struct node *datp; | |
1189 | /* | |
1190 | /* if (first.forw == 0) | |
1191 | /* return; | |
1192 | /* datp = &data; | |
1193 | /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { | |
1194 | /* if (p1->op == DATA) { | |
1195 | /* p2 = p1->forw; | |
1196 | /* while (p2 && p2->op!=TEXT) | |
1197 | /* p2 = p2->forw; | |
1198 | /* if (p2==0) | |
1199 | /* break; | |
1200 | /* p3 = p1->back; | |
1201 | /* p1->back->forw = p2->forw; | |
1202 | /* p2->forw->back = p3; | |
1203 | /* p2->forw = 0; | |
1204 | /* datp->forw = p1; | |
1205 | /* p1->back = datp; | |
1206 | /* p1 = p3; | |
1207 | /* datp = p2; | |
1208 | /* } | |
1209 | /* } | |
1210 | /* if (data.forw) { | |
1211 | /* datp->forw = first.forw; | |
1212 | /* first.forw->back = datp; | |
1213 | /* data.forw->back = &first; | |
1214 | /* first.forw = data.forw; | |
1215 | /* } | |
1216 | /* seg = -1; | |
1217 | /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { | |
1218 | /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { | |
1219 | /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { | |
1220 | /* p1->back->forw = p1->forw; | |
1221 | /* p1->forw->back = p1->back; | |
1222 | /* p1 = p1->back; | |
1223 | /* continue; | |
1224 | /* } | |
1225 | /* seg = p1->op; | |
1226 | /* } | |
1227 | /* } | |
1228 | /* } | |
1229 | */ | |
1230 | ||
1231 | redunbr(p) | |
1232 | register struct node *p; | |
1233 | { | |
1234 | register struct node *p1; | |
1235 | register char *ap1; | |
1236 | char *ap2; | |
1237 | ||
1238 | if ((p1 = p->ref) == 0) | |
1239 | return; | |
1240 | p1 = nonlab(p1); | |
1241 | if (p1->op==TST) { | |
1242 | splitrand(p1); | |
1243 | savereg(RT2, "$0", p1->subop); | |
1244 | } else if (p1->op==CMP) | |
1245 | splitrand(p1); | |
1246 | else | |
1247 | return; | |
1248 | if (p1->forw->op==CBR) { | |
1249 | ap1 = findcon(RT1, p1->subop); | |
1250 | ap2 = findcon(RT2, p1->subop); | |
1251 | p1 = p1->forw; | |
1252 | if (compare(p1->subop, ap1, ap2)) { | |
1253 | nredunj++; | |
1254 | nchange++; | |
1255 | decref(p->ref); | |
1256 | p->ref = p1->ref; | |
1257 | p->labno = p1->labno; | |
1258 | #ifdef COPYCODE | |
1259 | if (p->labno == 0) | |
1260 | p->code = p1->code; | |
1261 | if (p->ref) | |
1262 | #endif | |
1263 | p->ref->refc++; | |
1264 | } | |
1265 | } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && | |
1266 | equtype(ccloc[0],p1->subop)) { | |
1267 | p1=insertl(p1->forw); decref(p->ref); p->ref=p1; | |
1268 | nrtst++; nchange++; | |
1269 | } | |
1270 | } | |
1271 | ||
1272 | char * | |
1273 | findcon(i, type) | |
1274 | { | |
1275 | register char *p; | |
1276 | register r; | |
1277 | ||
1278 | p = regs[i]; | |
1279 | if (*p=='$') | |
1280 | return(p); | |
1281 | if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) | |
1282 | return(regs[r]+1); | |
1283 | if (equstr(p, conloc)) | |
1284 | return(conval+1); | |
1285 | return(p); | |
1286 | } | |
1287 | ||
1288 | compare(op, acp1, acp2) | |
1289 | char *acp1, *acp2; | |
1290 | { | |
1291 | register char *cp1, *cp2; | |
1292 | register n1; | |
1293 | int n2; int sign; | |
1294 | ||
1295 | cp1 = acp1; | |
1296 | cp2 = acp2; | |
1297 | if (*cp1++ != '$' || *cp2++ != '$') | |
1298 | return(0); | |
1299 | n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} | |
1300 | while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} | |
1301 | n2 = n1; | |
1302 | n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} | |
1303 | while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} | |
1304 | if (*cp1=='+') | |
1305 | cp1++; | |
1306 | if (*cp2=='+') | |
1307 | cp2++; | |
1308 | do { | |
1309 | if (*cp1++ != *cp2) | |
1310 | return(0); | |
1311 | } while (*cp2++); | |
1312 | cp1 = n1; | |
1313 | cp2 = n2; | |
1314 | switch(op) { | |
1315 | ||
1316 | case JEQ: | |
1317 | return(cp1 == cp2); | |
1318 | case JNE: | |
1319 | return(cp1 != cp2); | |
1320 | case JLE: | |
1321 | return(((int)cp1) <= ((int)cp2)); | |
1322 | case JGE: | |
1323 | return(((int)cp1) >= ((int)cp2)); | |
1324 | case JLT: | |
1325 | return(((int)cp1) < ((int)cp2)); | |
1326 | case JGT: | |
1327 | return(((int)cp1) > ((int)cp2)); | |
1328 | case JLO: | |
1329 | return(cp1 < cp2); | |
1330 | case JHI: | |
1331 | return(cp1 > cp2); | |
1332 | case JLOS: | |
1333 | return(cp1 <= cp2); | |
1334 | case JHIS: | |
1335 | return(cp1 >= cp2); | |
1336 | } | |
1337 | return(0); | |
1338 | } | |
1339 | ||
1340 | setcon(cv, cl, type) | |
1341 | register char *cv, *cl; | |
1342 | { | |
1343 | register char *p; | |
1344 | ||
1345 | if (*cv != '$') | |
1346 | return; | |
1347 | if (!natural(cl)) | |
1348 | return; | |
1349 | p = conloc; | |
1350 | while (*p++ = *cl++); | |
1351 | p = conval; | |
1352 | *p++ = type; | |
1353 | while (*p++ = *cv++); | |
1354 | } | |
1355 | ||
1356 | equstr(p1, p2) | |
1357 | register char *p1, *p2; | |
1358 | { | |
1359 | do { | |
1360 | if (*p1++ != *p2) | |
1361 | return(0); | |
1362 | } while (*p2++); | |
1363 | return(1); | |
1364 | } | |
1365 | ||
1366 | setcc(ap,type) | |
1367 | char *ap; | |
1368 | { | |
1369 | register char *p, *p1; | |
1370 | ||
1371 | p = ap; | |
1372 | if (!natural(p)) { | |
1373 | ccloc[0] = 0; | |
1374 | return; | |
1375 | } | |
1376 | p1 = ccloc; | |
1377 | *p1++ = type; | |
1378 | while (*p1++ = *p++); | |
1379 | } | |
1380 | ||
1381 | okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ | |
1382 | if (ioflag && (!natural(p) || 0>getnum(p))) return(0); | |
1383 | return(1); | |
1384 | } | |
1385 | ||
1386 | indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ | |
1387 | while (*p) if (*p++=='[') return(1); | |
1388 | return(0); | |
1389 | } | |
1390 | ||
1391 | natural(p) | |
1392 | register char *p; | |
1393 | {/* 1->simple local, parameter, global, or register; 0->otherwise */ | |
1394 | if (*p=='*' || *p=='(' || *p=='-'&&*(p+1)=='(' || *p=='$'&&getnum(p+1)) | |
1395 | return(0); | |
1396 | while (*p++); | |
1397 | p--; | |
1398 | if (*--p=='+' || *p==']' || *p==')' && *(p-2)!='a' && *(p-2)!='f') | |
1399 | return(0); | |
1400 | return(1); | |
1401 | } | |
1402 | ||
1403 | /* | |
1404 | ** Tell if an argument is most likely static. | |
1405 | */ | |
1406 | ||
1407 | isstatic(cp) | |
1408 | register char *cp; | |
1409 | { | |
1410 | if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) | |
1411 | return (1); | |
1412 | return (0); | |
1413 | } | |
49c27f9c RC |
1414 | |
1415 | autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */ | |
1416 | if (*p == '-' && *(p+1) == '(') return(1); | |
1417 | while (*p) p++; | |
1418 | if (*--p == '+' && *--p == ')') return(1); | |
1419 | return(0); | |
1420 | } |