Commit | Line | Data |
---|---|---|
6b028d93 | 1 | static char sccsid[] = "@(#)c21.c 4.12 %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) { | |
6b028d93 RC |
414 | if (*conloc) { |
415 | r1=conval[0]; | |
416 | printf("Con %s = %d%d %s\n", conloc, r1&0xF, r1>>4, conval+1); | |
417 | } | |
f00d91fc BJ |
418 | printf("Regs:\n"); |
419 | for (r=0; r<NREG; r++) | |
420 | if (regs[r][0]) { | |
421 | r1=regs[r][0]; | |
422 | printf("%d: %d%d %s\n", r, r1&0xF, r1>>4, regs[r]+1); | |
423 | } | |
424 | printf("-\n"); | |
425 | } | |
426 | switch (p->op) { | |
427 | ||
428 | case CVT: | |
429 | splitrand(p); goto mov; | |
430 | ||
431 | case MOV: | |
432 | splitrand(p); | |
433 | if ((r = findrand(regs[RT1],p->subop)) >= 0) { | |
434 | if (r == isreg(regs[RT2]) && p->forw->op!=CBR) { | |
435 | delnode(p); redunm++; break; | |
436 | } | |
437 | } | |
438 | mov: | |
439 | repladdr(p); | |
440 | r = isreg(regs[RT1]); | |
441 | r1 = isreg(regs[RT2]); | |
442 | dest(regs[RT2],p->subop); | |
d1580951 BJ |
443 | if (r>=0) { |
444 | if (r1>=0) savereg(r1, regs[r]+1, p->subop); | |
445 | else if (p->op!=CVT) savereg(r, regs[RT2], p->subop); | |
446 | } else if (r1>=0) savereg(r1, regs[RT1], p->subop); | |
447 | else if (p->op!=CVT) setcon(regs[RT1], regs[RT2], p->subop); | |
f00d91fc BJ |
448 | break; |
449 | ||
450 | /* .rx,.wx */ | |
451 | case MFPR: | |
452 | case COM: | |
453 | case NEG: | |
454 | /* .rx,.wx or .rx,.rx,.wx */ | |
455 | case ADD: | |
456 | case SUB: | |
457 | case BIC: | |
458 | case BIS: | |
459 | case XOR: | |
460 | case MUL: | |
461 | case DIV: | |
462 | case ASH: | |
463 | case MOVZ: | |
464 | /* .rx,.rx,.rx,.wx */ | |
465 | case EXTV: | |
466 | case EXTZV: | |
467 | case INSV: | |
468 | splitrand(p); | |
469 | repladdr(p); | |
470 | dest(lastrand,p->subop); | |
471 | if (p->op==INSV) ccloc[0]=0; | |
472 | break; | |
473 | ||
474 | /* .mx or .wx */ | |
475 | case CLR: | |
476 | case INC: | |
477 | case DEC: | |
478 | splitrand(p); | |
479 | dest(lastrand,p->subop); | |
480 | if (p->op==CLR) | |
481 | if ((r = isreg(regs[RT1])) >= 0) | |
482 | savereg(r, "$0", p->subop); | |
483 | else | |
484 | setcon("$0", regs[RT1], p->subop); | |
485 | break; | |
486 | ||
487 | /* .rx */ | |
488 | case TST: | |
489 | case PUSH: | |
490 | splitrand(p); | |
491 | lastrand=regs[RT1+1]; /* fool repladdr into doing 1 operand */ | |
492 | repladdr(p); | |
493 | if (p->op==TST && equstr(lastrand=regs[RT1], ccloc+1) | |
494 | && ((0xf&(ccloc[0]>>4))==p->subop || equtype(ccloc[0],p->subop)) | |
495 | &&!source(lastrand)) { | |
496 | delnode(p); p = p->back; nrtst++; nchange++; | |
497 | } | |
498 | setcc(lastrand,p->subop); | |
499 | break; | |
500 | ||
501 | /* .rx,.rx,.rx */ | |
502 | case PROBER: | |
503 | case PROBEW: | |
504 | case CASE: | |
505 | case MOVC3: | |
506 | /* .rx,.rx */ | |
507 | case MTPR: | |
508 | case CALLS: | |
509 | case CMP: | |
510 | case BIT: | |
511 | splitrand(p); | |
512 | /* fool repladdr into doing right number of operands */ | |
9ad46604 | 513 | if (p->op==CASE || p->op==PROBER || p->op==PROBEW) lastrand=regs[RT4]; |
c2a86b7c | 514 | /* else if (p->op==CMPV || p->op==CMPZV) lastrand=regs[RT4+1]; */ |
9ad46604 | 515 | else if (p->op==MOVC3) lastrand=regs[RT1]; |
f00d91fc BJ |
516 | else lastrand=regs[RT3]; |
517 | repladdr(p); | |
518 | if (p->op==CALLS || p->op==MOVC3) clearreg(); | |
519 | if (p->op==BIT) bitopt(p); | |
520 | ccloc[0]=0; break; | |
521 | ||
522 | case CBR: | |
523 | if (p->subop>=JBC) { | |
524 | splitrand(p); | |
525 | if (p->subop<JBCC) lastrand=regs[RT3]; /* 2 operands can be optimized */ | |
526 | else lastrand=regs[RT2]; /* .mb destinations lose */ | |
527 | repladdr(p); | |
528 | } | |
529 | ccloc[0] = 0; | |
530 | break; | |
531 | ||
532 | case JBR: | |
533 | redunbr(p); | |
534 | ||
535 | /* .wx,.bb */ | |
536 | case SOB: | |
537 | ||
538 | default: | |
539 | clearreg(); | |
540 | } | |
541 | } | |
542 | } | |
543 | ||
544 | char * | |
545 | byondrd(p) register struct node *p; { | |
546 | /* return pointer to register which is "beyond last read/modify operand" */ | |
547 | if (OP2==(p->subop>>4)) return(regs[RT3]); | |
548 | switch (p->op) { | |
549 | case MFPR: | |
550 | case JSB: | |
551 | case PUSHA: | |
552 | case TST: case INC: case DEC: case PUSH: return(regs[RT2]); | |
553 | case MTPR: | |
554 | case BIT: case CMP: case CALLS: return(regs[RT3]); | |
555 | case PROBER: case PROBEW: | |
556 | case CASE: case MOVC3: return(regs[RT4]); | |
557 | } | |
558 | return(lastrand); | |
559 | } | |
560 | ||
561 | struct node * | |
562 | bflow(p) | |
563 | register struct node *p; | |
564 | { | |
565 | register char *cp1,*cp2,**preg; register int r; | |
566 | int flow= -1; | |
567 | struct node *olduse=0; | |
568 | splitrand(p); | |
569 | if (p->op!=PUSH && p->subop && 0<=(r=isreg(lastrand)) && r<NUSE && uses[r]==p->forw) { | |
570 | if (equtype(p->subop,regs[r][0]) | |
571 | || ((p->op==CVT || p->op==MOVZ) | |
572 | && 0xf®s[r][0] && compat(0xf&(p->subop>>4),regs[r][0]))) { | |
573 | register int r2; | |
574 | if (regs[r][1]!=0) {/* send directly to destination */ | |
575 | if (p->op==INC || p->op==DEC) { | |
576 | if (p->op==DEC) p->op=SUB; else p->op=ADD; | |
577 | p->subop=(OP2<<4)+(p->subop&0xF); /* use 2 now, convert to 3 later */ | |
578 | p->pop=0; | |
579 | cp1=lastrand; cp2=regs[RT2]; while (*cp2++= *cp1++); /* copy reg */ | |
580 | cp1=lastrand; *cp1++='$'; *cp1++='1'; *cp1=0; | |
581 | } | |
582 | cp1=regs[r]+1; cp2=lastrand; | |
583 | if (OP2==(p->subop>>4)) {/* use 3 operand form of instruction */ | |
584 | p->pop=0; | |
585 | p->subop += (OP3-OP2)<<4; lastrand=cp2=regs[RT3]; | |
586 | } | |
587 | while (*cp2++= *cp1++); | |
588 | if (p->op==MOVA && p->forw->op==PUSH) { | |
589 | p->op=PUSHA; *regs[RT2]=0; p->pop=0; | |
590 | } else if (p->op==MOV && p->forw->op==PUSH) { | |
591 | p->op=PUSH ; *regs[RT2]=0; p->pop=0; | |
592 | } | |
593 | delnode(p->forw); | |
594 | if (0<=(r2=isreg(lastrand)) && r2<NUSE) { | |
595 | uses[r2]=uses[r]; uses[r]=0; | |
596 | } | |
597 | redun3(p,0); | |
598 | newcode(p); redunm++; flow=r; | |
599 | } else if (p->op==MOV && p->forw->op!=EXTV && p->forw->op!=EXTZV) { | |
600 | /* superfluous fetch */ | |
601 | int nmatch; | |
033bb901 | 602 | char src[C2_ASIZE]; |
f00d91fc BJ |
603 | movit: |
604 | cp2=src; cp1=regs[RT1]; while (*cp2++= *cp1++); | |
605 | splitrand(p->forw); | |
606 | if (p->forw->op != INC && p->forw->op != DEC) | |
607 | lastrand=byondrd(p->forw); | |
608 | nmatch=0; | |
609 | for (preg=regs+RT1;*preg!=lastrand;preg++) | |
610 | if (r==isreg(*preg)) { | |
611 | cp2= *preg; cp1=src; while (*cp2++= *cp1++); ++nmatch; | |
612 | } | |
613 | if (nmatch==1) { | |
614 | if (OP2==(p->forw->subop>>4) && equstr(src,regs[RT2])) { | |
615 | p->forw->pop=0; | |
616 | p->forw->subop += (OP3-OP2)<<4; cp1=regs[RT3]; | |
617 | *cp1++='r'; *cp1++=r+'0'; *cp1=0; | |
618 | } | |
619 | delnode(p); p=p->forw; | |
620 | if (0<=(r2=isreg(src)) && r2<NUSE) { | |
621 | uses[r2]=uses[r]; uses[r]=0; | |
622 | } | |
623 | redun3(p,0); | |
624 | newcode(p); redunm++; flow=r; | |
625 | } else splitrand(p); | |
626 | } | |
627 | } else if (p->op==MOV && (p->forw->op==CVT || p->forw->op==MOVZ) | |
628 | && p->forw->subop&0xf /* if base or index, then forget it */ | |
629 | && compat(p->subop,p->forw->subop) && !source(cp1=regs[RT1]) | |
630 | && !indexa(cp1)) goto movit; | |
631 | } | |
632 | /* adjust 'lastrand' past any 'read' or 'modify' operands. */ | |
633 | lastrand=byondrd(p); | |
634 | /* a 'write' clobbers the register. */ | |
635 | if (0<=(r=isreg(lastrand)) && r<NUSE | |
636 | || OP2==(p->subop>>4) && 0<=(r=isreg(regs[RT2])) && r<NUSE && uses[r]==0) { | |
637 | /* writing a dead register is useless, but watch side effects */ | |
638 | switch (p->op) { | |
35307d30 | 639 | case ACB: |
f00d91fc | 640 | case AOBLEQ: case AOBLSS: case SOBGTR: case SOBGEQ: break; |
35307d30 | 641 | default: |
f00d91fc BJ |
642 | if (uses[r]==0) {/* no direct uses, check for use of condition codes */ |
643 | register struct node *q=p; | |
644 | while ((q=nonlab(q->forw))->combop==JBR) q=q->ref; /* cc unused, unchanged */ | |
645 | if (q->op!=CBR) {/* ... and destroyed */ | |
646 | preg=regs+RT1; | |
647 | while (cp1= *preg++) { | |
648 | if (cp1==lastrand) {redunm++; delnode(p); return(p->forw);} | |
649 | if (source(cp1) || equstr(cp1,lastrand)) break; | |
650 | } | |
651 | } | |
652 | } | |
653 | flow=r; | |
654 | } | |
655 | } | |
656 | if (0<=(r=flow)) {olduse=uses[r]; uses[r]=0; *(short *)(regs[r])=0;} | |
657 | /* these two are here, rather than in bmove(), | |
658 | /* because I decided that it was better to go for 3-address code | |
659 | /* (save time) rather than fancy jbxx (save 1 byte) | |
660 | /* on sequences like bisl2 $64,r0; movl r0,foo | |
661 | */ | |
662 | if (p->op==BIC) {p=bicopt(p); splitrand(p); lastrand=byondrd(p);} | |
663 | if (p->op==BIS) {bixprep(p,JBSS); lastrand=byondrd(p);} | |
664 | /* now look for 'read' or 'modify' (read & write) uses */ | |
665 | preg=regs+RT1; | |
666 | while (*(cp1= *preg++)) { | |
667 | /* check for r */ | |
668 | if (lastrand!=cp1 && 0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) { | |
669 | uses[r]=p; cp2=regs[r]; *cp2++=p->subop; | |
d1580951 | 670 | if (p->op==ASH && preg==(regs+RT1+1)) cp2[-1]=BYTE; /* stupid DEC */ |
f00d91fc BJ |
671 | if (p->op==MOV || p->op==PUSH || p->op==CVT || p->op==MOVZ || p->op==COM || p->op==NEG) { |
672 | if (p->op==PUSH) cp1="-(sp)"; | |
673 | else { | |
674 | cp1=regs[RT2]; | |
675 | if (0<=(r=isreg(cp1)) && r<NUSE && uses[r]==0) | |
676 | uses[r]=olduse; /* reincarnation!! */ | |
677 | /* as in addl2 r0,r1; movl r1,r0; ret */ | |
678 | if (p->op!=MOV) cp1=0; | |
679 | } | |
680 | if (cp1) while (*cp2++= *cp1++); | |
d1580951 BJ |
681 | else *cp2=0; |
682 | } else *cp2=0; | |
f00d91fc BJ |
683 | continue; |
684 | } | |
685 | /* check for (r),(r)+,-(r),[r] */ | |
686 | do if (*cp1=='(' || *cp1=='[') {/* get register number */ | |
687 | char t; | |
688 | cp2= ++cp1; while (*++cp1!=')' && *cp1!=']'); t= *cp1; *cp1=0; | |
689 | if (0<=(r=isreg(cp2)) && r<NUSE && (uses[r]==0 || uses[r]==p)) { | |
690 | uses[r]=p; regs[r][0]=(*--cp2=='[' ? OPX<<4 : OPB<<4); | |
691 | } | |
692 | *cp1=t; | |
693 | } while (*++cp1); | |
694 | } | |
695 | /* pushax or movax possibility? */ | |
696 | cp1=regs[RT1]; | |
697 | if (*cp1++=='$' && isstatic(cp1) && natural(regs[RT1])) { | |
698 | if (p->combop==T(MOV,LONG)) { | |
699 | if (regs[RT1][1]=='L' && 0!=(p->labno=getnum(regs[RT1]+2))) { | |
700 | cp1=p->code; while (*cp1++!=','); p->code= --cp1; | |
701 | } | |
702 | p->combop=T(MOVA,LONG); ++p->code; p->pop=0; | |
703 | } else if (p->combop==T(PUSH,LONG)) { | |
704 | p->combop=T(PUSHA,LONG); ++p->code; p->pop=0; | |
705 | } else if ((p->combop&0xFFFF)==T(ADD,U(LONG,OP3)) | |
706 | && 0<=(r=isreg(regs[RT2]))) { | |
707 | cp1=cp2=p->code; ++cp1; | |
708 | do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]='['; | |
709 | do *cp2++= *cp1; while (*cp1++!=','); cp2[-1]=']'; | |
710 | if (!equstr(regs[RT3],"-(sp)")) p->combop=T(MOVA,BYTE); | |
711 | else {p->combop=T(PUSHA,BYTE); *cp2=0;} | |
712 | if (uses[r]==0) {uses[r]=p; regs[r][0]=OPX<<4;} | |
713 | p->pop=0; | |
714 | } | |
715 | } | |
716 | return(p); | |
717 | } | |
718 | ||
719 | ispow2(n) register long n; {/* -1 -> no; else -> log to base 2 */ | |
720 | register int log; | |
721 | if (n==0 || n&(n-1)) return(-1); log=0; | |
722 | for (;;) {n >>= 1; if (n==0) return(log); ++log; if (n== -1) return(log);} | |
723 | } | |
724 | ||
725 | bitopt(p) register struct node *p; { | |
726 | /* change "bitx $<power_of_2>,a" followed by JEQ or JNE | |
727 | /* into JBC or JBS. watch out for I/O registers. (?) | |
728 | /* assumes that 'splitrand' has already been called. | |
729 | */ | |
730 | register char *cp1,*cp2; int b; | |
731 | cp1=regs[RT1]; cp2=regs[RT2]; | |
732 | if (*cp1++!='$' || !okio(cp2) || p->forw->op!=CBR || p->forw->subop&-2 || | |
733 | 0>(b=ispow2(getnum(cp1))) || | |
734 | p->subop!=BYTE && (source(cp2) || indexa(cp2))) return; | |
735 | if (b>=bitsize[p->subop]) {/* you dummy! */ | |
736 | if (source(cp2)) {/* side effect: auto increment or decrement */ | |
737 | p->pop=0; | |
738 | p->op=TST; --cp1; while (*cp1++= *cp2++); | |
739 | regs[RT2][0]=0; newcode(p); | |
740 | } else delnode(p); | |
741 | p = p->forw; | |
742 | if (p->subop==JEQ) {p->combop=JBR; p->pop=0;} | |
743 | else delnode(p); | |
744 | nchange++; nbj++; return; | |
745 | } | |
746 | if (cp1=p->forw->code) {/* destination is not an internal label */ | |
747 | cp2=regs[RT3]; while (*cp2++= *cp1++); | |
748 | } | |
749 | if (b==0 && (p->subop==LONG || !indexa(regs[RT2]))) {/* JLB optimization, ala BLISS */ | |
750 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); | |
751 | cp2=regs[RT2]; cp1=regs[RT3]; while (*cp2++= *cp1++); | |
752 | *(regs[RT3])=0; p->forw->subop += JLBC-JBC; | |
753 | p->forw->pop=0; | |
754 | } else { | |
755 | cp1=regs[RT1]+1; | |
756 | if (b>9) *cp1++= b/10 +'0'; *cp1++= b%10 +'0'; *cp1=0; /* $<bit_number> */ | |
757 | } | |
758 | nbj++; newcode(p); p->combop = p->forw->combop+((JBC-JEQ)<<8); | |
759 | p->labno = p->forw->labno; delnode(p->forw); | |
760 | p->pop=0; | |
761 | } | |
762 | ||
763 | isfield(n) register long n; {/* -1 -> no; else -> position of low bit */ | |
764 | register int pos; register long t; | |
765 | t= ((n-1)|n) +1; | |
eea63b82 | 766 | if (n!=0 && (0==t || 0<=ispow2(t))) { |
f00d91fc BJ |
767 | pos=0; while(!(n&1)) {n >>= 1; ++pos;} return(pos); |
768 | } else return(-1); | |
769 | } | |
770 | ||
771 | bixprep(p,bix) register struct node *p; { | |
772 | /* initial setup, single-bit checking for bisopt, bicopt. | |
773 | /* return: 0->don't bother any more; 1->worthwhile trying | |
774 | */ | |
775 | register char *cp1,*cp2; | |
776 | splitrand(p); cp1=regs[RT1]; cp2=regs[RT2]; | |
777 | if (*cp1++!='$' || 0>(pos=isfield(f=getnum(cp1))) | |
778 | || !okio(cp2) || indexa(cp2) || source(cp2) || !okio(lastrand)) return(0); | |
779 | f |= f-1; if (++f==0) siz=32-pos; else siz=ispow2(f)-pos; | |
780 | if (siz==1 && pos>5 && (p->subop>>4)==OP2 && (p->subop&0xF)!=BYTE | |
781 | && pos<bitsize[p->subop&0xF]) { | |
782 | p->ref = insertl(p->forw); p->combop = CBR | (bix<<8); | |
783 | p->pop=0; | |
784 | p->labno = p->ref->labno; | |
785 | if (pos>9) {*cp1++= pos/10 +'0'; pos %= 10;} | |
786 | *cp1++=pos+'0'; *cp1=0; newcode(p); nbj++; return(0); | |
787 | } | |
788 | return(1); | |
789 | } | |
790 | ||
791 | ||
792 | struct node * | |
793 | bicopt(p) register struct node *p; { | |
794 | /* use field operations or MOVZ if possible. done as part of 'bflow'. | |
795 | */ | |
796 | register char *cp1,*cp2; int r; | |
3b60efae | 797 | char src[C2_ASIZE]; |
49c27f9c | 798 | char lhssiz, subop; |
f00d91fc BJ |
799 | if (!bixprep(p,JBCC)) return(p); |
800 | if (f==0) {/* the BIC isolates low order bits */ | |
801 | siz=pos; pos=0; | |
802 | if ((p->subop&0xF)==LONG && *(regs[RT2])!='$') {/* result of EXTZV is long */ | |
803 | /* save source of BICL in 'src' */ | |
804 | cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); | |
805 | if (p->back->op==ASH) {/* try for more */ | |
806 | splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT3]; | |
807 | if (*cp1++=='$' && *(regs[RT2])!='$' && !indexa(regs[RT2]) | |
808 | && 0>(f=getnum(cp1)) && equstr(src,cp2) | |
eea63b82 RC |
809 | && 0<=(r=isreg(cp2)) && r<NUSE |
810 | && siz-f <= 32) { /* a good ASH */ | |
f00d91fc BJ |
811 | pos -= f; cp1=regs[RT2]; cp2=src; while (*cp2++= *cp1++); |
812 | delnode(p->back); | |
813 | } | |
814 | } | |
49c27f9c RC |
815 | /* |
816 | * 'pos', 'siz' known; find out the size of the | |
817 | * left-hand operand of what the bicl will turn into. | |
818 | */ | |
819 | if (pos==0 && siz==16) | |
820 | lhssiz = WORD; /* movzwl */ | |
821 | else | |
822 | lhssiz = BYTE; /* movzbl or extzvl */ | |
f00d91fc BJ |
823 | if (p->back->op==CVT || p->back->op==MOVZ) {/* greedy, aren't we? */ |
824 | splitrand(p->back); cp1=regs[RT1]; cp2=regs[RT2]; | |
49c27f9c RC |
825 | /* |
826 | * If indexa(cp1) || autoid(cp1), the fold may | |
827 | * still be OK if the CVT/MOVZ has the same | |
828 | * size operand on its left size as what we | |
829 | * will turn the bicl into. | |
830 | * However, if the CVT is from a float or | |
831 | * double, forget it! | |
832 | */ | |
833 | subop = p->back->subop&0xF; /* type of LHS of CVT/MOVZ */ | |
834 | if (equstr(src,cp2) && okio(cp1) | |
835 | && subop != FFLOAT && subop != DFLOAT | |
836 | && subop != GFLOAT && subop != HFLOAT | |
837 | && ((!indexa(cp1) && !autoid(cp1)) || lhssiz == subop) | |
f00d91fc BJ |
838 | && 0<=(r=isreg(cp2)) && r<NUSE |
839 | && bitsize[p->back->subop&0xF]>=(pos+siz) | |
840 | && bitsize[p->back->subop>>4]>=(pos+siz)) {/* good CVT */ | |
841 | cp1=regs[RT1]; cp2=src; while (*cp2++= *cp1++); | |
842 | delnode(p->back); | |
843 | } | |
844 | } | |
845 | /* 'pos', 'siz' known; source of field is in 'src' */ | |
846 | splitrand(p); /* retrieve destination of BICL */ | |
49c27f9c RC |
847 | if ((siz==8 || siz==16) && pos==0) { |
848 | p->combop = T(MOVZ,U(lhssiz,LONG)); | |
f00d91fc BJ |
849 | sprintf(line,"%s,%s",src,lastrand); |
850 | } else { | |
851 | p->combop = T(EXTZV,LONG); | |
852 | sprintf(line,"$%d,$%d,%s,%s",pos,siz,src,lastrand); | |
853 | } | |
854 | p->pop=0; | |
855 | p->code = copy(line); nfield++; return(p); | |
856 | }/* end EXTZV possibility */ | |
857 | }/* end low order bits */ | |
858 | /* unfortunately, INSV clears the condition codes, thus cannot be used */ | |
859 | /* else {/* see if BICL2 of positive field should be INSV $0 */ | |
860 | /* if (p->subop==(LONG | (OP2<<4)) && 6<=(pos+siz)) { | |
861 | /* p->combop = INSV; | |
862 | /* sprintf(line,"$0,$%d,$%d,%s",pos,siz,lastrand); | |
863 | /* p->code = copy(line); nfield++; return(p); | |
864 | /* } | |
865 | /* } | |
866 | */ | |
867 | return(p); | |
868 | } | |
869 | ||
870 | jumpsw() | |
871 | { | |
872 | register struct node *p, *p1; | |
873 | register t; | |
874 | int nj; | |
875 | ||
876 | t = 0; | |
877 | nj = 0; | |
878 | for (p=first.forw; p!=0; p = p->forw) | |
879 | p->seq = ++t; | |
880 | for (p=first.forw; p!=0; p = p1) { | |
881 | p1 = p->forw; | |
882 | if (p->op == CBR && p1->op==JBR && p->ref && p1->ref | |
883 | && abs(p->seq - p->ref->seq) > abs(p1->seq - p1->ref->seq)) { | |
884 | if (p->ref==p1->ref) | |
885 | continue; | |
886 | p->subop = revbr[p->subop]; | |
887 | p->pop=0; | |
888 | t = p1->ref; | |
889 | p1->ref = p->ref; | |
890 | p->ref = t; | |
891 | t = p1->labno; | |
892 | p1->labno = p->labno; | |
893 | p->labno = t; | |
894 | #ifdef COPYCODE | |
895 | if (p->labno == 0) { | |
896 | t = p1->code; p1->code = p->code; p->code = t; | |
897 | } | |
898 | #endif | |
899 | nrevbr++; | |
900 | nj++; | |
901 | } | |
902 | } | |
903 | return(nj); | |
904 | } | |
905 | ||
906 | addsob() | |
907 | { | |
908 | register struct node *p, *p1, *p2, *p3; | |
909 | ||
910 | for (p = &first; (p1 = p->forw)!=0; p = p1) { | |
911 | if (p->combop==T(DEC,LONG) && p1->op==CBR) { | |
3b60efae | 912 | if (abs(p->seq - p1->ref->seq) > 8) continue; |
f00d91fc BJ |
913 | if (p1->subop==JGE || p1->subop==JGT) { |
914 | if (p1->subop==JGE) p->combop=SOBGEQ; else p->combop=SOBGTR; | |
915 | p->pop=0; | |
916 | p->labno = p1->labno; delnode(p1); nsob++; | |
917 | } | |
918 | } else if (p->combop==T(INC,LONG)) { | |
919 | if (p1->op==LABEL && p1->refc==1 && p1->forw->combop==T(CMP,LONG) | |
920 | && (p2=p1->forw->forw)->combop==T(CBR,JLE) | |
921 | && (p3=p2->ref->back)->combop==JBR && p3->ref==p1 | |
922 | && p3->forw->op==LABEL && p3->forw==p2->ref) { | |
923 | /* change INC LAB: CMP to LAB: INC CMP */ | |
924 | p->back->forw=p1; p1->back=p->back; | |
925 | p->forw=p1->forw; p1->forw->back=p; | |
926 | p->back=p1; p1->forw=p; | |
927 | p1=p->forw; | |
928 | /* adjust beginning value by 1 */ | |
929 | p2=alloc(sizeof first); p2->combop=T(DEC,LONG); | |
930 | p2->pop=0; | |
931 | p2->forw=p3; p2->back=p3->back; p3->back->forw=p2; | |
932 | p3->back=p2; p2->code=p->code; p2->labno=0; | |
933 | } | |
934 | if (p1->combop==T(CMP,LONG) && (p2=p1->forw)->op==CBR) { | |
935 | register char *cp1,*cp2; | |
936 | splitrand(p1); if (!equstr(p->code,regs[RT1])) continue; | |
3b60efae | 937 | if (abs(p->seq - p2->ref->seq)>8) {/* outside byte displ range */ |
f00d91fc BJ |
938 | if (p2->subop!=JLE) continue; |
939 | p->combop=T(ACB,LONG); | |
940 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ | |
941 | cp2=regs[RT2]; cp1="$1"; while (*cp2++= *cp1++); /* increment */ | |
942 | cp2=regs[RT3]; cp1=p->code; while (*cp2++= *cp1++); /* index */ | |
943 | p->pop=0; newcode(p); | |
944 | p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; | |
945 | } else if (p2->subop==JLE || p2->subop==JLT) { | |
946 | if (p2->subop==JLE) p->combop=AOBLEQ; else p->combop=AOBLSS; | |
947 | cp2=regs[RT1]; cp1=regs[RT2]; while (*cp2++= *cp1++); /* limit */ | |
948 | cp2=regs[RT2]; cp1=p->code; while (*cp2++= *cp1++); /* index */ | |
949 | p->pop=0; newcode(p); | |
950 | p->labno = p2->labno; delnode(p2); delnode(p1); nsob++; | |
951 | } | |
952 | } | |
953 | } | |
954 | } | |
955 | } | |
956 | ||
957 | abs(x) | |
958 | { | |
959 | return(x<0? -x: x); | |
960 | } | |
961 | ||
962 | equop(p1, p2) | |
963 | register struct node *p1; | |
964 | struct node *p2; | |
965 | { | |
966 | register char *cp1, *cp2; | |
967 | ||
968 | if (p1->combop != p2->combop) | |
969 | return(0); | |
970 | if (p1->op>0 && p1->op<MOV) | |
971 | return(0); | |
972 | if (p1->op==MOVA && p1->labno!=p2->labno) return(0); | |
973 | cp1 = p1->code; | |
974 | cp2 = p2->code; | |
975 | if (cp1==0 && cp2==0) | |
976 | return(1); | |
977 | if (cp1==0 || cp2==0) | |
978 | return(0); | |
979 | while (*cp1 == *cp2++) | |
980 | if (*cp1++ == 0) | |
981 | return(1); | |
982 | return(0); | |
983 | } | |
984 | ||
985 | delnode(p) register struct node *p; { | |
986 | p->back->forw = p->forw; | |
987 | p->forw->back = p->back; | |
988 | } | |
989 | ||
990 | decref(p) | |
991 | register struct node *p; | |
992 | { | |
993 | if (p && --p->refc <= 0) { | |
994 | nrlab++; | |
995 | delnode(p); | |
996 | } | |
997 | } | |
998 | ||
999 | struct node * | |
1000 | nonlab(ap) | |
1001 | struct node *ap; | |
1002 | { | |
1003 | register struct node *p; | |
1004 | ||
1005 | p = ap; | |
1006 | while (p && p->op==LABEL) | |
1007 | p = p->forw; | |
1008 | return(p); | |
1009 | } | |
1010 | ||
1011 | clearuse() { | |
1012 | register struct node **i; | |
1013 | for (i=uses+NUSE; i>uses;) *--i=0; | |
1014 | } | |
1015 | ||
1016 | clearreg() { | |
1017 | register short **i; | |
1018 | for (i=regs+NREG; i>regs;) **--i=0; | |
1019 | conloc[0] = 0; ccloc[0] = 0; | |
1020 | } | |
1021 | ||
1022 | savereg(ai, s, type) | |
1023 | register char *s; | |
1024 | { | |
1025 | register char *p, *sp; | |
1026 | ||
1027 | sp = p = regs[ai]; | |
1028 | if (source(s)) /* side effects in addressing */ | |
1029 | return; | |
1030 | /* if any indexing, must be parameter or local */ | |
1031 | /* indirection (as in "*-4(fp)") is ok, however */ | |
1032 | *p++ = type; | |
1033 | while (*p++ = *s) | |
1034 | if (*s=='[' || *s++=='(' && *s!='a' && *s!='f') {*sp = 0; return;} | |
1035 | } | |
1036 | ||
1037 | dest(s,type) | |
1038 | register char *s; | |
1039 | { | |
1040 | register int i; | |
1041 | ||
1042 | source(s); /* handle addressing side effects */ | |
6b028d93 RC |
1043 | if (!natural(s)) { |
1044 | /* wild store, everything except constants vanishes */ | |
1045 | for (i=NREG; --i>=0;) | |
1046 | if (regs[i][1] != '$') *(short *)(regs[i]) = 0; | |
1047 | conloc[0] = 0; ccloc[0] = 0; | |
1048 | return; | |
1049 | } | |
f00d91fc BJ |
1050 | if ((i = isreg(s)) >= 0) { |
1051 | *(short *)(regs[i]) = 0; /* if register destination, that reg is a goner */ | |
0594fe30 BJ |
1052 | switch(type & 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 | switch((type>>4)&0xF){ | |
1065 | case DFLOAT: /* clobber two at once */ | |
1066 | /*FALLTHROUGH*/ | |
1067 | case GFLOAT: | |
1068 | *(short *)(regs[i+1]) = 0; | |
1069 | break; | |
1070 | case HFLOAT: /* clobber four at once */ | |
1071 | *(short *)(regs[i+1]) = 0; | |
1072 | *(short *)(regs[i+2]) = 0; | |
1073 | *(short *)(regs[i+3]) = 0; | |
1074 | break; | |
1075 | } | |
1076 | /* | |
1077 | if (DFLOAT==(type&0xF) || DFLOAT==((type>>4)&0xF)) | |
1078 | *(short *)(regs[i+1]) = 0; | |
1079 | */ | |
f00d91fc BJ |
1080 | } |
1081 | for (i=NREG; --i>=0;) | |
1082 | if (regs[i][1]=='*' && equstr(s, regs[i]+2)) | |
1083 | *(short *)(regs[i]) = 0; /* previous indirection through destination is invalid */ | |
1084 | while ((i = findrand(s,0)) >= 0) /* previous values of destination are invalid */ | |
1085 | *(short *)(regs[i]) = 0; | |
6b028d93 RC |
1086 | if (*conloc && equstr(conloc, s)) |
1087 | conloc[0] = 0; | |
1088 | setcc(s, type); /* natural destinations set condition codes */ | |
f00d91fc BJ |
1089 | } |
1090 | ||
f00d91fc | 1091 | /* separate operands at commas, set up 'regs' and 'lastrand' */ |
6b028d93 RC |
1092 | splitrand(p) struct node *p; { |
1093 | register char *p1, *p2; | |
1094 | register char **preg; | |
1095 | ||
1096 | preg = regs+RT1; | |
1097 | if (p1 = p->code) | |
1098 | while (*p1) { | |
1099 | lastrand = p2 = *preg++; | |
1100 | while (*p1) | |
1101 | if (',' == (*p2++ = *p1++)) { | |
1102 | --p2; | |
1103 | break; | |
1104 | } | |
1105 | *p2 = 0; | |
1106 | } | |
1107 | while (preg < (regs+RT1+5)) | |
1108 | *(*preg++) = 0; | |
f00d91fc BJ |
1109 | } |
1110 | ||
1111 | compat(have, want) { | |
1112 | register int hsrc, hdst; | |
1113 | if (0==(want &= 0xF)) return(1); /* anything satisfies a wildcard want */ | |
1114 | hsrc=have&0xF; if (0==(hdst=((have>>4)&0xF)) || hdst>=OP2) hdst=hsrc; | |
0594fe30 BJ |
1115 | if (want>=FFLOAT) return(hdst==want && hsrc==want); |
1116 | /* FLOAT, DFLOAT not compat: rounding */ | |
1117 | return(hsrc>=want && hdst>=want && hdst<FFLOAT); | |
f00d91fc BJ |
1118 | } |
1119 | ||
1120 | equtype(t1,t2) {return(compat(t1,t2) && compat(t2,t1));} | |
1121 | ||
1122 | findrand(as, type) | |
1123 | char *as; | |
1124 | { | |
1125 | register char **i; | |
1126 | for (i = regs+NREG; --i>=regs;) { | |
1127 | if (**i && equstr(*i+1, as) && compat(**i,type)) | |
1128 | return(i-regs); | |
1129 | } | |
1130 | return(-1); | |
1131 | } | |
1132 | ||
1133 | isreg(s) | |
1134 | register char *s; | |
1135 | { | |
1136 | if (*s++!='r' || !isdigit(*s++)) return(-1); | |
1137 | if (*s==0) return(*--s-'0'); | |
1138 | if (*(s-1)=='1' && isdigit(*s++) && *s==0) return(10+*--s-'0'); | |
1139 | return(-1); | |
1140 | } | |
1141 | ||
1142 | check() | |
1143 | { | |
1144 | register struct node *p, *lp; | |
1145 | ||
1146 | lp = &first; | |
1147 | for (p=first.forw; p!=0; p = p->forw) { | |
1148 | if (p->back != lp) | |
1149 | abort(-1); | |
1150 | lp = p; | |
1151 | } | |
1152 | } | |
1153 | ||
1154 | source(ap) | |
1155 | char *ap; | |
1156 | { | |
1157 | register char *p1, *p2; | |
1158 | ||
1159 | p1 = ap; | |
1160 | p2 = p1; | |
1161 | if (*p1==0) | |
1162 | return(0); | |
1163 | while (*p2++ && *(p2-1)!='['); | |
1164 | if (*p1=='-' && *(p1+1)=='(' | |
1165 | || *p1=='*' && *(p1+1)=='-' && *(p1+2)=='(' | |
1166 | || *(p2-2)=='+') { | |
1167 | while (*p1 && *p1++!='r'); | |
1168 | if (isdigit(*p1++)) | |
1169 | if (isdigit(*p1)) *(short *)(regs[10+*p1-'0'])=0; | |
1170 | else *(short *)(regs[*--p1-'0'])=0; | |
1171 | return(1); | |
1172 | } | |
1173 | return(0); | |
1174 | } | |
1175 | ||
1176 | newcode(p) struct node *p; { | |
1177 | register char *p1,*p2,**preg; | |
1178 | preg=regs+RT1; p2=line; | |
1179 | while (*(p1= *preg++)) {while (*p2++= *p1++); *(p2-1)=',';} | |
1180 | *--p2=0; | |
1181 | p->code=copy(line); | |
1182 | } | |
1183 | ||
1184 | repladdr(p) | |
1185 | struct node *p; | |
1186 | { | |
1187 | register r; | |
1188 | register char *p1, *p2; | |
1189 | char **preg; int nrepl; | |
1190 | ||
1191 | preg=regs+RT1; nrepl=0; | |
1192 | while (lastrand!=(p1= *preg++)) | |
1193 | if (!source(p1) && 0<=(r=findrand(p1,p->subop))) { | |
1194 | *p1++='r'; if (r>9) {*p1++='1'; r -= 10;} *p1++=r+'0'; *p1=0; | |
1195 | nrepl++; nsaddr++; | |
1196 | } | |
1197 | if (nrepl) newcode(p); | |
1198 | } | |
1199 | ||
1200 | /* movedat() | |
1201 | /* { | |
1202 | /* register struct node *p1, *p2; | |
1203 | /* struct node *p3; | |
1204 | /* register seg; | |
1205 | /* struct node data; | |
1206 | /* struct node *datp; | |
1207 | /* | |
1208 | /* if (first.forw == 0) | |
1209 | /* return; | |
1210 | /* datp = &data; | |
1211 | /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { | |
1212 | /* if (p1->op == DATA) { | |
1213 | /* p2 = p1->forw; | |
1214 | /* while (p2 && p2->op!=TEXT) | |
1215 | /* p2 = p2->forw; | |
1216 | /* if (p2==0) | |
1217 | /* break; | |
1218 | /* p3 = p1->back; | |
1219 | /* p1->back->forw = p2->forw; | |
1220 | /* p2->forw->back = p3; | |
1221 | /* p2->forw = 0; | |
1222 | /* datp->forw = p1; | |
1223 | /* p1->back = datp; | |
1224 | /* p1 = p3; | |
1225 | /* datp = p2; | |
1226 | /* } | |
1227 | /* } | |
1228 | /* if (data.forw) { | |
1229 | /* datp->forw = first.forw; | |
1230 | /* first.forw->back = datp; | |
1231 | /* data.forw->back = &first; | |
1232 | /* first.forw = data.forw; | |
1233 | /* } | |
1234 | /* seg = -1; | |
1235 | /* for (p1 = first.forw; p1!=0; p1 = p1->forw) { | |
1236 | /* if (p1->op==TEXT||p1->op==DATA||p1->op==BSS) { | |
1237 | /* if (p1->op == seg || p1->forw&&p1->forw->op==seg) { | |
1238 | /* p1->back->forw = p1->forw; | |
1239 | /* p1->forw->back = p1->back; | |
1240 | /* p1 = p1->back; | |
1241 | /* continue; | |
1242 | /* } | |
1243 | /* seg = p1->op; | |
1244 | /* } | |
1245 | /* } | |
1246 | /* } | |
1247 | */ | |
1248 | ||
1249 | redunbr(p) | |
1250 | register struct node *p; | |
1251 | { | |
1252 | register struct node *p1; | |
1253 | register char *ap1; | |
1254 | char *ap2; | |
1255 | ||
1256 | if ((p1 = p->ref) == 0) | |
1257 | return; | |
1258 | p1 = nonlab(p1); | |
1259 | if (p1->op==TST) { | |
1260 | splitrand(p1); | |
1261 | savereg(RT2, "$0", p1->subop); | |
1262 | } else if (p1->op==CMP) | |
1263 | splitrand(p1); | |
1264 | else | |
1265 | return; | |
1266 | if (p1->forw->op==CBR) { | |
1267 | ap1 = findcon(RT1, p1->subop); | |
1268 | ap2 = findcon(RT2, p1->subop); | |
1269 | p1 = p1->forw; | |
1270 | if (compare(p1->subop, ap1, ap2)) { | |
1271 | nredunj++; | |
1272 | nchange++; | |
1273 | decref(p->ref); | |
1274 | p->ref = p1->ref; | |
1275 | p->labno = p1->labno; | |
1276 | #ifdef COPYCODE | |
1277 | if (p->labno == 0) | |
1278 | p->code = p1->code; | |
f00d91fc | 1279 | #endif |
6b028d93 | 1280 | if (p->ref) |
f00d91fc BJ |
1281 | p->ref->refc++; |
1282 | } | |
1283 | } else if (p1->op==TST && equstr(regs[RT1],ccloc+1) && | |
1284 | equtype(ccloc[0],p1->subop)) { | |
1285 | p1=insertl(p1->forw); decref(p->ref); p->ref=p1; | |
1286 | nrtst++; nchange++; | |
1287 | } | |
1288 | } | |
1289 | ||
1290 | char * | |
1291 | findcon(i, type) | |
1292 | { | |
1293 | register char *p; | |
1294 | register r; | |
1295 | ||
1296 | p = regs[i]; | |
1297 | if (*p=='$') | |
1298 | return(p); | |
1299 | if ((r = isreg(p)) >= 0 && compat(regs[r][0],type)) | |
1300 | return(regs[r]+1); | |
1301 | if (equstr(p, conloc)) | |
1302 | return(conval+1); | |
1303 | return(p); | |
1304 | } | |
1305 | ||
1306 | compare(op, acp1, acp2) | |
1307 | char *acp1, *acp2; | |
1308 | { | |
1309 | register char *cp1, *cp2; | |
1310 | register n1; | |
1311 | int n2; int sign; | |
1312 | ||
1313 | cp1 = acp1; | |
1314 | cp2 = acp2; | |
1315 | if (*cp1++ != '$' || *cp2++ != '$') | |
1316 | return(0); | |
1317 | n1 = 0; sign=1; if (*cp2=='-') {++cp2; sign= -1;} | |
1318 | while (isdigit(*cp2)) {n1 *= 10; n1 += (*cp2++ - '0')*sign;} | |
1319 | n2 = n1; | |
1320 | n1 = 0; sign=1; if (*cp1=='-') {++cp1; sign= -1;} | |
1321 | while (isdigit(*cp1)) {n1 *= 10; n1 += (*cp1++ - '0')*sign;} | |
1322 | if (*cp1=='+') | |
1323 | cp1++; | |
1324 | if (*cp2=='+') | |
1325 | cp2++; | |
1326 | do { | |
1327 | if (*cp1++ != *cp2) | |
1328 | return(0); | |
1329 | } while (*cp2++); | |
1330 | cp1 = n1; | |
1331 | cp2 = n2; | |
1332 | switch(op) { | |
1333 | ||
1334 | case JEQ: | |
1335 | return(cp1 == cp2); | |
1336 | case JNE: | |
1337 | return(cp1 != cp2); | |
1338 | case JLE: | |
1339 | return(((int)cp1) <= ((int)cp2)); | |
1340 | case JGE: | |
1341 | return(((int)cp1) >= ((int)cp2)); | |
1342 | case JLT: | |
1343 | return(((int)cp1) < ((int)cp2)); | |
1344 | case JGT: | |
1345 | return(((int)cp1) > ((int)cp2)); | |
1346 | case JLO: | |
1347 | return(cp1 < cp2); | |
1348 | case JHI: | |
1349 | return(cp1 > cp2); | |
1350 | case JLOS: | |
1351 | return(cp1 <= cp2); | |
1352 | case JHIS: | |
1353 | return(cp1 >= cp2); | |
1354 | } | |
1355 | return(0); | |
1356 | } | |
1357 | ||
1358 | setcon(cv, cl, type) | |
1359 | register char *cv, *cl; | |
1360 | { | |
1361 | register char *p; | |
1362 | ||
1363 | if (*cv != '$') | |
1364 | return; | |
1365 | if (!natural(cl)) | |
1366 | return; | |
1367 | p = conloc; | |
1368 | while (*p++ = *cl++); | |
1369 | p = conval; | |
1370 | *p++ = type; | |
1371 | while (*p++ = *cv++); | |
1372 | } | |
1373 | ||
1374 | equstr(p1, p2) | |
1375 | register char *p1, *p2; | |
1376 | { | |
1377 | do { | |
1378 | if (*p1++ != *p2) | |
1379 | return(0); | |
1380 | } while (*p2++); | |
1381 | return(1); | |
1382 | } | |
1383 | ||
1384 | setcc(ap,type) | |
1385 | char *ap; | |
1386 | { | |
1387 | register char *p, *p1; | |
1388 | ||
1389 | p = ap; | |
1390 | if (!natural(p)) { | |
1391 | ccloc[0] = 0; | |
1392 | return; | |
1393 | } | |
1394 | p1 = ccloc; | |
1395 | *p1++ = type; | |
1396 | while (*p1++ = *p++); | |
1397 | } | |
1398 | ||
1399 | okio(p) register char *p; {/* 0->probable I/O space address; 1->not */ | |
1400 | if (ioflag && (!natural(p) || 0>getnum(p))) return(0); | |
1401 | return(1); | |
1402 | } | |
1403 | ||
1404 | indexa(p) register char *p; {/* 1-> uses [r] addressing mode; 0->doesn't */ | |
1405 | while (*p) if (*p++=='[') return(1); | |
1406 | return(0); | |
1407 | } | |
1408 | ||
1409 | natural(p) | |
1410 | register char *p; | |
1411 | {/* 1->simple local, parameter, global, or register; 0->otherwise */ | |
6b028d93 | 1412 | if (*p=='*' || *p=='(' || *p=='-'&&p[1]=='(' || *p=='$'&&getnum(p+1)) |
f00d91fc BJ |
1413 | return(0); |
1414 | while (*p++); | |
1415 | p--; | |
6b028d93 | 1416 | if (*--p=='+' || *p==']' || *p==')' && p[-2]!='a' && p[-2]!='f') |
f00d91fc BJ |
1417 | return(0); |
1418 | return(1); | |
1419 | } | |
1420 | ||
1421 | /* | |
1422 | ** Tell if an argument is most likely static. | |
1423 | */ | |
1424 | ||
1425 | isstatic(cp) | |
1426 | register char *cp; | |
1427 | { | |
1428 | if (*cp == '_' || *cp == 'L' || (*cp++ == 'v' && *cp == '.')) | |
1429 | return (1); | |
1430 | return (0); | |
1431 | } | |
49c27f9c RC |
1432 | |
1433 | autoid(p) register char *p; {/* 1-> uses autoincrement/autodecrement; 0->doesn't */ | |
1434 | if (*p == '-' && *(p+1) == '(') return(1); | |
1435 | while (*p) p++; | |
1436 | if (*--p == '+' && *--p == ')') return(1); | |
1437 | return(0); | |
1438 | } |