|
|
1.1 root 1: /* ../common/cgen.c */
2: extern struct _iobuf {
3: int _cnt;
4: unsigned char *_ptr;
5: unsigned char *_base;
6: short _flag;
7: char _file;
8: } _iob[20];
9: struct _iobuf *fopen();
10: struct _iobuf *fdopen();
11: struct _iobuf *freopen();
12: long ftell();
13: char *fgets();
14: char *hash();
15: char *savestr();
16: char *tstr();
17: extern int tstrused;
18: extern char *tstrbuf[];
19: extern char **curtstr;
20: extern int nerrors;
21: typedef union ndu NODE;
22: typedef unsigned int TWORD;
23: typedef long CONSZ;
24: extern int dope[];
25: extern char *opst[];
26:
27: union ndu {
28: struct {
29: int op;
30: int goal;
31: TWORD type;
32: int cst[(6+4)];
33: char * name;
34: char pad[8 -sizeof(char *)];
35: NODE *left;
36: NODE *right;
37: }in;
38:
39: struct {
40: int op;
41: int goal;
42: TWORD type;
43: int cst[(6+4)];
44: char * name;
45: char pad[8 -sizeof(char *)];
46: CONSZ lval;
47: int rval;
48: }tn;
49:
50: struct {
51: int op;
52: int goal;
53: TWORD type;
54: int cst[(6+4)];
55: int label;
56: int lop;
57: }bn;
58: struct {
59: int op;
60: int goal;
61: TWORD type;
62: int cst[(6+4)];
63: int stsize;
64: short stalign;
65: short argsize;
66: }stn;
67: struct {
68: int op;
69: int goal;
70: TWORD type;
71: int cdim;
72: int csiz;
73: }fn;
74:
75: struct {
76:
77: int op;
78: int goal;
79: TWORD type;
80: int cdim;
81: int csiz;
82: double dval;
83: }fpn;
84: };
85:
86:
87:
88:
89:
90:
91:
92:
93: extern int busy[];
94: extern NODE node[];
95: typedef struct shape SHAPE;
96:
97: extern struct shape {
98: int op;
99: SHAPE *sl;
100: SHAPE *sr;
101: int sh;
102: int sc;
103: } shapes[];
104: extern SHAPE *pshape[];
105: typedef struct optab OPTAB;
106: struct optab {
107: int op;
108: int tyop;
109: OPTAB *nextop;
110: SHAPE **lshape;
111: int ltype;
112: SHAPE **rshape;
113: int rtype;
114: int needs;
115: int rewrite;
116: char *cstring;
117: int cost;
118: int lcount;
119: int rcount;
120: int stinline;
121: };
122: extern OPTAB
123: *match(),
124: *ophead[],
125: table[];
126: extern NODE resc[];
127: extern int tmpoff;
128: extern int maxboff;
129: extern int maxtemp;
130: extern int maxarg;
131: extern int ftnno;
132: extern int sideff;
133: extern NODE
134: *talloc(),
135: *ind2type(),
136: *tcopy(),
137: *getadr(),
138: *getlr();
139: extern CONSZ rdin();
140: extern char *rnames[];
141: extern int lineno;
142: extern char ftitle[];
143: extern int fldshf, fldsz;
144: extern int lflag, udebug, e2debug, odebug, rdebug, radebug, sdebug;
145: extern int fast;
146:
147:
148:
149:
150: typedef struct inst INST;
151: struct inst {
152: NODE *p;
153: OPTAB *q;
154: int goal;
155: };
156: extern INST inst[300];
157: extern nins;
158:
159:
160: typedef SHAPE *SHTABLE[2][20];
161: extern SHTABLE sha;
162: rewcom( p, goal )
163: NODE *p;
164: {
165:
166:
167:
168: int o, ty, g1, g2;
169: NODE *l, *r, *ql, *qr;
170: o = p->tn.op;
171: g1 = g2 = 6;
172: p->tn.goal = goal;
173:
174: switch( o )
175: {
176: case 97:
177: cerror( "rewcom(%d) is FREE", p-node );
178: case 110:
179: g1 = (6+3);
180: break;
181: case 59:
182: g1 = (6+1);
183: g2 = goal;
184: break;
185: case 22:
186: case 112:
187: case 56:
188: case 108:
189: g1 = g2 = goal;
190: break;
191: case 70:
192: case 100:
193: case 73:
194: g2 = (6+1);
195: break;
196: }
197: switch( ty = (dope[o]&016) )
198: {
199: case 010:
200: rewcom( r = p->in.right, g2 );
201: case 04:
202: rewcom( l = p->in.left, g1 );
203: break;
204: case 02:
205: return;
206: }
207: if( o==59 || o==22 || o==108 ) return;
208:
209:
210:
211:
212: if( l->tn.op == 59 && l->in.left->tn.op != 110 )
213: {
214:
215:
216:
217: ql = l->in.left;
218: qr = l->in.right;
219: *l = *p;
220: l->in.left = qr;
221: p->in.right = l;
222: p->in.left = ql;
223: p->tn.op = 59;
224: rewcom( p, p->tn.goal );
225: }
226: if( ty == 04 ) return;
227: if( r->tn.op == 59 && r->in.right->tn.op != 110 )
228: {
229:
230:
231:
232: ql = r->in.left;
233: qr = r->in.right;
234: *r = *p;
235: p->tn.op = 59;
236: p->in.left = ql;
237: r->in.right = qr;
238: rewcom( p, p->tn.goal );
239: }
240: }
241: rewlhs(p)
242: NODE *p;
243: {
244:
245:
246: NODE *q, *t;
247: q = talloc();
248: *q = *p;
249: t = tcopy( p->in.left, 0 );
250: p->in.left = q;
251: p->in.right = t;
252: p->tn.op = 59;
253: return;
254: }
255: rewsto(p)
256: NODE *p;
257: {
258:
259:
260: int o, ao;
261: NODE *t, *q;
262:
263: while( (o=p->tn.op) == 59 ) p = p->in.right;
264: if( o == 95 ) return(0);
265: if( o == 99 )
266: {
267:
268: t = talloc();
269: *t = *p->in.left;
270: q = talloc();
271: *q = *t;
272: t->tn.op = 95;
273: t->tn.lval = freetemp(argsize(p)/32 );
274: t->tn.lval = ((t->tn.lval)/8);
275: t->tn.name = (char *) 0;
276: t->tn.type = 02000 ;
277: q->tn.op = 2+ 14;
278: q->in.left = t;
279:
280: t = talloc();
281: *t = *p;
282: t->in.left = q;
283: t->in.right = p->in.left;
284: t->tn.op = 98;
285:
286: p->in.left = talloc();
287: p->in.left->tn.op = 59;
288: p->in.left->in.left = t;
289: p->in.left->in.right = t = talloc();
290: *t = *q;
291: t->in.left = talloc();
292: *t->in.left = *q->in.left;
293:
294:
295: if( odebug>1 ) e2print( p );
296: return( 1 );
297: }
298: if( odebug>1 )
299: {
300: e2print( p );
301: printf( "\nrewritten by rewsto as:\n" );
302: }
303: if( (dope[o]&01) && o!=78 && o!=79 && lhsok( p->in.left ) ) {
304:
305: rewlhs( p );
306: return( 1 );
307: }
308: ao = 1+ o;
309: if( (dope[ao]&020000) )
310: {
311: if( p->in.left->tn.op == 95 )
312: {
313: p->tn.op = ao;
314: rewlhs( p );
315: if( odebug>1 ) e2print( p );
316: return( 1 );
317: }
318: }
319:
320:
321: t = talloc();
322: *t = *p;
323: q = talloc();
324: *q = *p;
325: t->tn.op = 95;
326: t->tn.lval = freetemp(argsize(p)/32 );
327: t->tn.lval = ((t->tn.lval)/8);
328: t->tn.name = (char *) 0;
329: q->tn.op = 58;
330: q->in.left = t;
331: q->in.right = talloc();
332: *(q->in.right) = *p;
333:
334: p->in.right = talloc();
335: *(p->in.right) = *t;
336: p->tn.op = 59;
337: p->in.left = q;
338:
339: if( odebug>1 ) e2print( p );
340: return( 1 );
341: }
342: iseff( p )
343: NODE *p;
344: {
345:
346: int o;
347: o = p->tn.op;
348: if( (dope[o]&02000) || (dope[o]&01) ) return( 1 );
349: switch( (dope[ o ]&016) )
350: {
351: case 010:
352: if( iseff( p->in.right ) ) return( 1 );
353: case 04:
354: return( iseff( p->in.left ) );
355: }
356: return( 0 );
357: }
358: NODE *
359: lhsto( p )
360: NODE *p;
361: {
362:
363:
364: NODE *q;
365: int o;
366: for( q = p->in.left; (o=q->tn.op)!=(2+ 11); q=q->in.left )
367: {
368: if( (dope[o]&016) == 02 ) return( (NODE *)0);
369: }
370:
371: q = q->in.left;
372: o = q->tn.op;
373: if( (dope[o]&016) == 02 ) return( (NODE *)0 );
374: else return( q );
375: }
376: static int
377: c2bigger( p ) NODE *p; {
378:
379: register TWORD t, tl;
380: t = p->tn.type;
381: tl = p->in.left->tn.type;
382: if( (t|tl)& 04000 ) return( 0 );
383: if( t&040 ) return( 1 );
384: if( tl&040 ) return( 0 );
385: if( t& 020 ) return( 1 );
386: if( tl& 020 ) return( 0 );
387: if( t&( 010| 01000) ) return( 1 );
388: if( tl&( 010| 01000) ) return( 0 );
389: if( t&( ( 04| 010)|(0400| 01000)) ) return( 1 );
390: if( tl&( ( 04| 010)|(0400| 01000)) ) return( 0 );
391: if( t &( 02|0200) ) return( 1 );
392: return( 0 );
393: }
394: NODE *
395: ind2type( p )
396: register NODE *p;
397: {
398:
399: register TWORD t;
400: NODE *q;
401: t = p->tn.type;
402: if( t == 01 || t == 02 ) t = ( 04| 010);
403: else if( t == 0100 || t == 0200 ) t = (0400| 01000);
404: else if( t == 020 ) t = 040;
405: else return( p );
406: if( p->tn.op == 104 && c2bigger(p) )
407: {
408: p->tn.type = t;
409: return( p );
410: }
411: q = talloc();
412: q->tn.op = 104;
413: q->in.left = p;
414: q->in.right = 0;
415: q->tn.name = (char *) 0;
416: q->tn.type = t;
417: q->tn.goal = 6;
418: return( q );
419: }
420: NODE *
421: reweop( p )
422: register NODE *p;
423: {
424:
425:
426:
427:
428:
429:
430: register NODE *q, *t;
431: register TWORD ty;
432: if( odebug>1 )
433: {
434: e2print( p );
435: printf( "\nrewritten by reweop as:\n" );
436: }
437: q = tcopy( p, 0 );
438: if( p->in.left->tn.op == 104 )
439: {
440:
441:
442:
443: t = p->in.left;
444: ty = t->in.left->tn.type;
445: p->in.left = t->in.left;
446: tfree( p->in.right );
447: p->in.right = t;
448: t->in.left = q;
449:
450: t->tn.type = ty;
451: }
452: else
453: {
454: tfree( p->in.right );
455: p->in.right = q;
456: }
457:
458: p->tn.op = 58;
459: q->tn.op = (-1)+ q->tn.op;
460: p->tn.type = p->in.left->tn.type;
461: q->tn.type = q->in.right->tn.type;
462: if( odebug>1 ) e2print( p );
463: }
464: rewass( p )
465: NODE *p;
466: {
467: NODE *q;
468: int o;
469:
470: if( odebug )
471: {
472: printf( "rewass called with:\n" );
473: e2print( p );
474: }
475: o = p->tn.op;
476: if( o == 2+ 14 )
477: {
478: if( p->in.left->tn.op == 118 )
479: {
480:
481: q = p->in.left;
482: q->tn.op = 4;
483: *p = *q;
484: q->tn.op = 97;
485: return(0);
486: }
487:
488: rewsto( p->in.left );
489:
490: if( odebug )
491: {
492: printf( "\nrewritten by rewass as:\n" );
493: e2print( p );
494: }
495: return(1);
496: }
497: if( ! (dope[o]&01) || o==58 )
498: {
499: if( o==58 )
500: {
501:
502: o = p->in.left->tn.op;
503: if( o==118 || o==120 || o==119 )
504: {
505:
506: p->in.left->tn.op = 94;
507: p->in.left->tn.rval = 0;
508: if( odebug )
509: {
510: printf( "funny node redone\n" );
511: e2print(p);
512: }
513: return(0);
514: }
515: }
516: else
517: {
518: TWORD t = p->in.left->tn.type;
519:
520:
521: p->in.left = ind2type( p->in.left );
522: p->in.right = ind2type( p->in.right );
523: if( odebug ) {
524: printf( "conversions inserted" );
525: e2print(p);
526: }
527:
528: if( t != p->in.left->tn.type ) {
529:
530: return( 0 );
531: }
532: }
533: e2print(p);
534: cerror( "can't deal with op %s", opst[o] );
535: }
536: if( o == 78 || o == 79 )
537: {
538:
539: if( odebug>1 )
540: {
541: e2print( p );
542: printf( "\nrewritten by rewass as:\n" );
543: }
544: if( p->in.goal == (6+1) )
545: {
546: p->in.op = ((o==78)?1+ 6:1+ 8);
547: }
548: else
549: {
550: q = tcopy(p, 0);
551: regrcl( p->in.left );
552: tfree( p->in.left );
553: p->in.left = q;
554: q->tn.op = ((o==78)?1+ 6:1+ 8);
555: p->tn.op = ((o==78)?8:6);
556: }
557: if( odebug )
558: {
559: printf( "\nrewritten by rewass as:\n" );
560: e2print( p );
561: }
562: return(1);
563: }
564:
565: if( q = lhsto(p) )
566: {
567: if( !rewsto( q ) ) cerror( "rewass0" );
568: rewcom( p, p->tn.goal );
569: if( p->tn.op != 59 ) cerror( "rewass1" );
570: if( ! (dope[ p->in.right->tn.op ]&01) ) cerror( "rewass2" );
571: (void)reweop( p->in.right );
572: }
573: else (void)reweop( p );
574: return(1);
575: }
576: outshp( pp )
577: SHAPE **pp;
578: {
579: SHAPE *p;
580: if (pp == 0)
581: return;
582: for( ; p = *pp; ++pp )
583: {
584: printf("\t\t");
585: shpr(p);
586: printf( " (%d)\n", p->sc );
587: }
588: }
589: tabpr()
590: {
591: register OPTAB *p;
592: for (p =table; ;p++)
593: {
594: printf("Dump of table[%d] (stinline %d)\n", p-table, p->stinline );
595: printf("\top = %s\n", opst[p->op]);
596: printf("\tnextop = %d\n", p->nextop?p->nextop-table:-1 );
597: printf("\tlshape = %d\n", p->lshape-pshape);
598: printf("\tltype = 0%o\n", p->ltype);
599: printf("\trshape = %d\n", p->rshape-pshape);
600: printf("\trtype = 0%o\n", p->rtype);
601: printf("\tneeds = %d\n", p->needs);
602: printf("\trewrite = %d\n", p->rewrite);
603: printf("\tcstring = %s", p->cstring);
604: printf("\tcost = %d\n", p->cost);
605: printf("\tLeft:\n");
606: outshp(p->lshape);
607: printf("\tRight:\n");
608: outshp(p->rshape);
609: printf("\n");
610: }
611: }
612: codgen( p )
613: NODE *p;
614: {
615:
616: int i, flag;
617: if (odebug > 5)
618: {
619: tabpr();
620:
621: }
622:
623:
624:
625:
626: again:
627:
628: rewcom( p, (6+1) );
629: if( odebug )
630: {
631: printf( "After goals are computed:" );
632: e2print( p );
633: }
634:
635: if( costs( p ) ) goto again;
636: if( odebug )
637: {
638: printf( "After costs are computed:" );
639: e2print( p );
640: }
641:
642: nins = 0;
643: insout( p, (6+1) );
644:
645: flag = 0;
646: for( i=0; i<nins; ++i )
647: {
648: if( inst[i].goal == (6+2) )
649: {
650: if( odebug )
651: {
652: printf( "subtree is stored in temp:\n" );
653: e2print( inst[i].p );
654: }
655: if( rewsto( inst[i].p ) ) {
656: if( !fast ) goto again;
657:
658: flag = 1;
659: }
660: }
661: }
662: if( flag ) goto again;
663: if( odebug ) e2print(p);
664:
665: insprt();
666: }
667: INST inst[300];
668: int nins;
669: insprt()
670: {
671: int i;
672: register INST *pi;
673: register NODE *p;
674: register OPTAB *q;
675: register c, goal;
676: for( pi=inst,i=0; i<nins; ++i,++pi )
677: {
678: p = pi->p;
679: q = pi->q;
680: c = pi->goal;
681: if( c == (6+3) && (q->rewrite&040) ) goal = 04 ;
682: else if( c == (6+1) ) goal = 01 ;
683: else goal = 02 ;
684: if(odebug > 4)
685: {
686: printf("INSOUT: %d c=",i);
687: preff(c);
688: printf(" goal=");
689: prgoal(goal);
690: printf("\n");
691: e2print(p);
692: }
693: allo( p, q );
694: expand( p, goal, q->cstring, q );
695: reclaim( p, q->rewrite, goal );
696:
697: if( c == (6+3) && p->tn.op != 96 )
698: {
699: cfix( p, (6+3) );
700: if( p->tn.op != 96 ) cerror( "ctest fails" );
701: }
702: if( c>=0 && c<=6 && ! ( p ->in.op==94 && (( p ->tn.rval)<6)) )
703: {
704: cfix( p, 6 );
705: }
706: }
707: }
708: SHTABLE sha;
709: int odebug = 0;
710: cfix( p, goal )
711: NODE *p;
712: {
713:
714: OPTAB *q;
715: NODE *pp;
716: int r;
717: if(odebug > 4)
718: {
719: printf("CFIX: goal=");
720: prgoal(goal);
721: printf("\n");
722: e2print(p);
723: }
724: if( goal == (6+3) )
725: {
726: r = 040;
727: p->tn.goal = (6+3);
728: }
729: else
730: {
731: r = (04|010|020);
732: pp = ( ( (( (dope[ p ->tn.op]&016))==02? p : p ->in.left)));
733: if( ( pp ->in.op==94 && (( pp ->tn.rval)<6)) ) r |= 01;
734: pp = ( ( (( (dope[ p ->tn.op]&016))==010? p ->in.right: p )));
735: if( ( pp ->in.op==94 && (( pp ->tn.rval)<6)) ) r |= 02;
736: }
737: if( goal == (6+3) ) goal = 04 ;
738: else goal = 02 ;
739: for( q=0; q = match( p, q ); )
740: {
741:
742:
743: if( q->rewrite & r )
744: {
745:
746: allo( p, q );
747: expand( p, goal, q->cstring, q );
748: reclaim( p, q->rewrite, goal );
749: return;
750: }
751: }
752: e2print(p);
753: cerror( "cfix trouble" );
754: }
755: preff(c)
756: {
757: char buf[20];
758: register char *p;
759: p = c==(6+3) ? "CCC" : c==(6+2) ? "CTEMP" : c==(6+1) ? "CEFF" : 0;
760: if(!p)
761: {
762: sprintf(buf,"0%o",c);
763: p = buf;
764: }
765: printf("%s",p);
766: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.