10 extern char *strncpy(), *strcpy();
11 extern char *malloc();
13 extern char *sprintf();
15 /* sprintf should be declared in stdio.h, as returning an int */
21 p= malloc((unsigned)n);
23 yyerror("Out of core");
31 if(e.expr_typ != TYPINT)
32 yyerror("Must be integer expression");
35 tstbool(e) expr_t e; {
37 if(e.expr_typ != TYPBOOL)
38 yyerror("Must be boolean expression");
41 structsize(s) register list2 s; {
47 l = s->l2list->l1next;
57 list2 lookstruct(ll) list2 ll; {
62 for (l3=structpool;l3 != 0;l3=l3->l3next) {
63 for (l21=l3->l3list,l22=ll;l21!=0 && l22!=0;
64 l21=l21->l2next,l22=l22->l2next) {
65 for(l11=l21->l2list,l12=l22->l2list;
66 l11!=0 && l12!=0 && strcmp(l11->l1name,l12->l1name)==0;
67 l11=l11->l1next,l12=l12->l1next)
76 l3 = (list3) myalloc(sizeof(struct list3str));
77 l3->l3next=structpool;
83 instno(inst) inst_t inst; {
86 for(i=1;i<narinstance;i++) {
87 if (arinstance[i].in_which != inst.in_which)
89 for(j=0;j<TOKENSIZE;j++)
90 if(arinstance[i].in_info[j] != inst.in_info[j])
95 chktabsiz(narinstance,MAXINSTANCE,"Instance table");
96 arinstance[narinstance] = inst;
97 return(narinstance++);
100 string scopy(s) string s; {
103 t = (char *) myalloc(strlen(s)+1);
108 strlookup(s) string s; {
111 for(i=0;i<ncodestrings;i++)
112 if(strcmp(s,codestrings[i])==0)
114 chktabsiz(ncodestrings,MAXSTRINGS,"string table");
115 codestrings[ncodestrings] = scopy(s);
116 return(ncodestrings++);
119 stringno(s) register string s; {
121 register char *p=buf;
123 while(*s != 0) switch(*s) {
131 yyerror("Bad character after $ in codestring");
144 *p++ = argtyp(patmnem[*s-'0']) == TYPINT ?
156 yyerror("Bad character following %% in codestring");
162 if (num<1 || num>tokpatlen)
163 yyerror("Number within %[] out of range");
168 } else if (*s++ != '.')
169 yyerror("Bad character following %%[digit in codestring");
172 register char *f=field;
175 while( *s != ']' && *s != 0)
179 yyerror("Unterminated %[] construction in codestring");
182 if (isdigit(field[0])) {
183 chkregexp(pattokexp[num]);
188 offset = findstructel(pattokexp[num],field,&type);
194 } else if (*s >= 'a' && *s < 'a'+nallreg) {
201 yyerror("Bad character following %%[x in codestring");
204 yyerror("Bad character following %%[x. in codestring");
208 yyerror("Bad character following %%[x.y in codestring");
215 yyerror("Bad character following %%[ in codestring");
218 return(strlookup(buf));
221 tabovf(tablename) string tablename; {
224 sprintf(buf,"%s overflow",tablename);
229 main(argc,argv) char *argv[]; {
233 if (argv[0][0]=='-') {
234 switch (argv[0][1]) {
242 fprintf(stderr,"Bad flag %s\n",argv[0]);
263 lookup(comm,operator,lnode,rnode) {
266 for (p=nodes+1;p<lastnode;p++) {
267 if (p->ex_operator != operator)
269 if (!(p->ex_lnode == lnode && p->ex_rnode == rnode ||
270 comm && p->ex_lnode == rnode && p->ex_rnode == lnode))
274 if (lastnode >= &nodes[MAXNODES])
275 yyerror("node table overflow");
277 p->ex_operator = operator;
286 for (i=1;i<nmachregs;i++) {
288 if (eqregclass(i,j)) {
289 stregclass[i] = stregclass[j];
293 stregclass[i] = nregclasses++;
298 register reginfo rp1,rp2;
300 short regbits[(MAXREGS+15)>>4];
303 rp1 = machregs[r1]; rp2 = machregs[r2];
304 for (i=0;i<((nprops+15)>>4);i++)
305 if (rp1->rprop[i] != rp2->rprop[i])
307 for (i=0;i<((MAXREGS+15)>>4);i++)
309 for (i=0;i<maxmembers;i++) {
310 if (member = rp1->rmembers[i])
311 regbits[member>>4] |= (1<<(member&017));
313 for (i=0;i<maxmembers;i++) {
314 member = rp2->rmembers[i];
315 if (regbits[member>>4]&(1<<(member&017)))
321 unsigned hash(name) register string name; {
322 register unsigned sum;
325 for (sum=i=0;*name;i+=3)
326 sum ^= (*name++)<<(i&07);
330 ident_p ilookup(name,enterf) string name; int enterf; {
331 register ident_p p,*pp;
333 pp = &identtab[hash(name)%ITABSIZE];
335 if (strcmp((*pp)->i_name,name)==0)
339 yyerror("Multiply defined symbol");
342 if (enterf == LOOKUP)
343 yyerror("Undefined symbol");
344 if (enterf == JUSTLOOKING)
346 p = *pp = (ident_p) myalloc(sizeof(ident_t));
355 if (iname!=0 && freopen(iname,"r",stdin)==NULL) {
356 fprintf(stderr,"Can't open %s\n",iname);
359 if ((cfile=fopen(cname,"w"))==NULL) {
360 fprintf(stderr,"Can't create %s\n",cname);
363 if ((hfile=fopen(hname,"w"))==NULL) {
364 fprintf(stderr,"Can't create %s\n",hname);
367 fprintf(cfile,"#include \"param.h\"\n");
368 fprintf(cfile,"#include \"tables.h\"\n");
369 fprintf(cfile,"#include \"types.h\"\n");
370 fprintf(cfile,"#include <cg_pattern.h>\n");
371 fprintf(cfile,"#include \"data.h\"\n");
372 fprintf(cfile,"\nbyte coderules[] = {\n");
376 exprlookup(sett) set_t sett; {
379 for(i=0;i<nmachsets;i++) {
380 ok= (sett.set_size == machsets[i].set_size);
381 for(j=0;j<SETSIZE;j++) {
382 if (sett.set_val[j] == machsets[i].set_val[j])
390 chktabsiz(nmachsets,MAXSETS,"Expression table");
391 machsets[nmachsets] = sett;
401 nodes[0].ex_operator=EX_CON;
405 r=(reginfo)myalloc(sizeof(struct reginfo));
410 for(i=0;i<MAXMEMBERS;i++)
412 for(i=0;i<PROPSETSIZE;i++)
414 r->rprop[cocopropno>>4] |= (1<<(cocopropno&017));
415 chktabsiz(nmachregs,MAXREGS,"Register table");
416 machregs[nmachregs++] = r;
417 inst.in_which = IN_RIDENT;
418 inst.in_info[0] = nmachregs-1;
419 for(i=1;i<TOKENSIZE;i++)
421 ccinstanceno=instno(inst);
422 ccregexpr=lookup(0,EX_REG,nmachregs-1,0);
424 for (i=0;i<SETSIZE;i++)
426 sett.set_val[nmachregs>>4] |= (01<<(nmachregs&017));
427 cocosetno=exprlookup(sett);
432 static short rset[(MAXREGS+15)>>4];
435 fprintf(cfile,"char stregclass[] = {\n");
436 for (i=0;i<nmachregs;i++)
437 fprintf(cfile,"\t%d,\n",stregclass[i]);
438 fprintf(cfile,"};\n\nstruct reginfo machregs[] = {\n{0},\n");
439 for (i=1;i<nmachregs;i++) {
440 fprintf(cfile,"{%d,%d",strlookup(machregs[i]->rrepr),
444 for(j=0;j<maxmembers;j++)
445 fprintf(cfile,"%d,",machregs[i]->rmembers[j]);
446 /* now compute and print set of registers
447 * that clashes with this register.
448 * A register clashes with al its children (and theirs)
449 * and with all their parents.
451 for (j=0;j<((MAXREGS+15)>>4);j++)
453 rset[i>>4] |= (1<<(i&017));
456 for (j=1;j<nmachregs;j++)
457 if (rset[j>>4]&(1<<(j&017)))
458 for (k=0;k<maxmembers;k++)
459 if ((t=machregs[j]->rmembers[k])!=0) {
460 if ((rset[t>>4]&(1<<(t&017)))==0)
462 rset[t>>4] |= (1<<(t&017));
467 for (j=1;j<nmachregs;j++)
468 for (k=0;k<maxmembers;k++)
469 if ((t=machregs[j]->rmembers[k])!=0)
470 if (rset[t>>4]&(1<<(t&017))) {
471 if (rset[j>>4]&(1<<(j&017))==0)
473 rset[j>>4] |= (1<<(j&017));
476 fprintf(cfile,"},{");
477 for (j=0;j<((nmachregs+15)>>4);j++)
478 fprintf(cfile,"%d,",rset[j]);
481 if (machregs[i]->rregvar>=0)
483 fprintf(cfile,"},\n");
485 fprintf(cfile,"};\n\n");
495 fprintf(cfile,"};\n\n");
497 fprintf(hfile,"#define TEM_WSIZE %d\n",wsize);
499 yyerror("Wordsize undefined");
501 fprintf(hfile,"#define TEM_PSIZE %d\n",psize);
503 yyerror("Pointersize undefined");
505 fprintf(hfile,"#define TEM_BSIZE %d\n",bsize);
507 yyerror("EM_BSIZE undefined");
509 fprintf(hfile,"#define WRD_FMT \"%s\"\n",fmt);
510 fprintf(hfile,"#define MAXALLREG %d\n",maxallreg);
511 setsize = (nmachregs+1 + nmachtokens + 15)>>4;
512 fprintf(hfile,"#define SETSIZE %d\n",setsize);
513 fprintf(hfile,"#define NPROPS %d\n",nprops);
514 fprintf(hfile,"#define NREGS %d\n",nmachregs);
515 fprintf(hfile,"#define REGSETSIZE %d\n",(nmachregs+15)>>4);
516 fprintf(hfile,"#define TOKENSIZE %d\n",maxtokensize);
517 fprintf(hfile,"#define MAXMEMBERS %d\n",maxmembers);
518 fprintf(hfile,"#define LONGESTPATTERN %d\n",maxempatlen);
519 fprintf(hfile,"#define MAXRULE %d\n",maxrule);
520 fprintf(hfile,"#define NMOVES %d\n",nmoves);
521 fprintf(hfile,"#define NC1 %d\n",nc1);
524 fprintf(hfile,"#define NC2 %d\n",nc2);
525 fprintf(hfile,"#define MAXSPLIT %d\n",maxsplit);
527 fprintf(hfile,"#define NC3 %d\n",nc3);
529 fprintf(cfile,"tkdef_t tokens[] = {\n");
530 for(i=0;i<nmachtokens;i++) {
531 fprintf(cfile,"{%d,{%d,%d},{",machtokens[i].t_size,
532 machtokens[i].t_cost.c_size,
533 machtokens[i].t_cost.c_time);
534 for(j=0;j<maxtokensize;j++)
535 fprintf(cfile,"%d,",machtokens[i].t_fields[j].t_type);
536 fprintf(cfile,"},%d},\n",machtokens[i].t_format);
538 fprintf(cfile,"};\n\nnode_t enodes[] = {\n");
539 for(np=nodes;np<lastnode;np++)
540 fprintf(cfile,"{%d,%d,%d},\n",np->ex_operator,np->ex_lnode,
542 fprintf(cfile,"};\n\nstring codestrings[] = {\n");
543 for(i=0;i<ncodestrings;i++) {
546 fprintf(cfile,"\t\"");
548 register int c = (*p) & BMASK;
549 if (! isascii(c) || iscntrl(c)) {
550 /* The next line used to have (c>>6)&03,
551 but this triggered a bug in GCC 2.4.5
554 fprintf(cfile,"\\%c%c%c",((*p>>6) &03)+'0',
555 ((c>>3)&07)+'0',(c&07)+'0');
560 fprintf(cfile,"\",\n");
562 fprintf(cfile,"};\n\nset_t machsets[] = {\n");
563 for(i=0;i<nmachsets;i++) {
564 fprintf(cfile,"{%d,{",machsets[i].set_size);
565 for(j=0;j<setsize;j++)
566 fprintf(cfile,"0%o,",machsets[i].set_val[j] & 0xFFFF);
567 fprintf(cfile,"}},\n");
569 fprintf(cfile,"};\n\ninst_t tokeninstances[] = {\n");
570 for(i=0;i<narinstance;i++) {
571 fprintf(cfile,"{ %d, {",arinstance[i].in_which);
572 for(j=0;j<=maxtokensize;j++)
573 fprintf(cfile,"%d,",arinstance[i].in_info[j]);
574 fprintf(cfile,"}},\n");
576 fprintf(cfile,"};\n\nmove_t moves[] = {\n");
577 for (i=0;i<nmoves;i++) {
579 fprintf(cfile,"{%d,%d,%d,%d,%d,{%d,%d}},\n",
580 mp->m_set1, mp->m_expr1,
581 mp->m_set2, mp->m_expr2,
583 mp->m_cost.c_size,mp->m_cost.c_time);
585 fprintf(cfile,"};\n\nbyte pattern[] = {\n");
586 for (i=0;i<npatbytes;i++) {
587 fprintf(cfile,"%3d,",pattern[i]&BMASK);
591 fprintf(cfile,"\n};\n\nint pathash[256] = {\n");
593 fprintf(cfile,"%6d,",pathash[i]);
597 fprintf(cfile,"};\n\nc1_t c1coercs[] = {\n");
599 fprintf(cfile,"{%d,%d,%d,%d,{%d,%d}},\n",
600 c1coercs[i].c1_texpno,
603 c1coercs[i].c1_codep,
604 c1coercs[i].c1_cost.c_size,
605 c1coercs[i].c1_cost.c_time);
607 fprintf(cfile,"};\n\nc2_t c2coercs[] = {\n");
608 for (i=0;i<nc2;i++) {
609 fprintf(cfile,"{%d,%d,{",
610 c2coercs[i].c2_texpno,
611 c2coercs[i].c2_nsplit);
612 for (j=0;j<maxsplit;j++)
613 fprintf(cfile,"%d,",c2coercs[i].c2_repl[j]);
614 fprintf(cfile,"},%d},\n",c2coercs[i].c2_codep);
616 fprintf(cfile,"};\n\nc3_t c3coercs[] = {\n");
618 fprintf(cfile,"{%d,%d,%d,%d},\n",
619 c3coercs[i].c3_texpno,
622 c3coercs[i].c3_codep);
623 fprintf(cfile,"};\n\n");
624 for (i=0;i<nprops;i++) {
625 fprintf(cfile,"struct reginfo *rlist%d[] = {\n",i);
626 for (j=2;j<=nmachregs;j++) {
627 if (machregs[j-1]->rregvar<0 &&
628 (machprops[i].propset.set_val[j>>4]&(1<<(j&017))))
629 fprintf(cfile,"\t&machregs[%d],\n",j-1);
631 fprintf(cfile,"\t0\n};\n");
633 fprintf(cfile,"struct reginfo **reglist[] = {\n");
634 for (i=0;i<nprops;i++) {
635 fprintf(cfile,"\trlist%d,\n",i);
637 fprintf(cfile,"};\n");
638 fprintf(cfile,"unsigned cc1 = %u;\n",cc1);
639 fprintf(cfile,"unsigned cc2 = %u;\n",cc2);
640 fprintf(cfile,"unsigned cc3 = %u;\n",cc3);
641 fprintf(cfile,"unsigned cc4 = %u;\n",cc4);
649 fprintf(hfile,"#define REGVARS\n");
650 fprintf(cfile,"#include \"regvar.h\"\n");
651 fprintf(cfile,"int nregvar[4] = { ");
652 for (i=0;i<4;i++) fprintf(cfile,"%d, ",nregvar[i]);
653 fprintf(cfile,"};\n");
656 fprintf(cfile,"struct regassigned ratar%d[%d];\n",
658 for (i=0;i<4;i++) if (nregvar[i]>0) {
659 fprintf(cfile,"int rvtar%d[] = {",i);
660 for (j=0;j<nregvar[i];j++)
661 fprintf(cfile,"%d,",rvnumbers[i][j]);
662 fprintf(cfile,"};\n");
664 fprintf(cfile,"\nint *rvnumbers[] = {\n");
667 fprintf(cfile,"\trvtar%d,\n",i);
669 fprintf(cfile,"\t0,\n");
670 fprintf(cfile,"};\n\nstruct regassigned *regassigned[] = {\n");
673 fprintf(cfile,"\tratar%d,\n",i);
675 fprintf(cfile,"\t0,\n");
676 fprintf(cfile,"};\n");
681 fprintf(stderr,"Codebytes %d\n",codebytes);
682 fprintf(stderr,"Registers %d(%d)\n",nmachregs,MAXREGS);
683 fprintf(stderr,"Properties %d(%d)\n",nprops,MAXPROPS);
684 fprintf(stderr,"Tokens %d(%d)\n",nmachtokens,MAXTOKENS);
685 fprintf(stderr,"Sets %d(%d)\n",nmachsets,MAXSETS);
686 fprintf(stderr,"Tokeninstances %d(%d)\n",narinstance,MAXINSTANCE);
687 fprintf(stderr,"Strings %d(%d)\n",ncodestrings,MAXSTRINGS);
688 fprintf(stderr,"Enodes %d(%d)\n",lastnode-nodes,MAXNODES);
689 fprintf(stderr,"Patbytes %d(%d)\n",npatbytes,MAXPATTERN);
697 lookident=1; /* for lexical analysis */
699 chktabsiz(nmachsets+1,MAXSETS,"Expressiontable");
700 for (i=0;i<SETSIZE;i++)
701 machsets[nmachsets].set_val[i] = 0xFFFF;
702 machsets[nmachsets].set_val[0] &= ~1;
703 machsets[nmachsets].set_size = 0;
704 ip=ilookup("SCRATCH",ENTER);
706 ip->i_i.i_expno = nmachsets++;
708 for (i=0;i<SETSIZE;i++)
709 machsets[nmachsets].set_val[i] = 0xFFFF;
710 machsets[nmachsets].set_size = 0;
711 ip=ilookup("ALL",ENTER);
713 allexpno = ip->i_i.i_expno = nmachsets++;
714 mp = &machmoves[nmoves++];
715 mp->m_set1 = cocosetno;
717 mp->m_set2 = nmachsets-1;
720 mp->m_cost.c_size = 0;
721 mp->m_cost.c_time = 0;
724 * Create sets of registers per property
727 for (i=0;i<nprops;i++) {
728 short *sp = machprops[i].propset.set_val;
731 for (j=2;j<=nmachregs;j++)
732 if (machregs[j-1]->rprop[i>>4]&(1<<(i&017)))
733 sp[j>>4] |= (1<<(j&017));
737 formconversion(p,tp) register char *p; register token_p tp; {
739 register char *q=buf;
746 while (*p) switch(*p) {
747 default: *q++ = *p++; continue;
757 yyerror("Bad character after % in format");
759 while (*p != 0 && *p != ']')
765 yyerror("Unterminated %[] construct in format");
766 for (i=0;i<TOKENSIZE-1;i++)
767 if (strcmp(field,tp->t_fields[i].t_sname)==0)
770 yyerror("Unknown field in %[] construct in format");
774 return(strlookup(buf));
777 setfields(tp,format) register token_p tp; string format; {
783 for(i=0;i<TOKENSIZE-1;i++)
784 tp->t_fields[i].t_type = 0;
786 for(ll=tp->t_struct;ll!=0;ll=ll->l2next) {
788 if(strcmp(l->l1name,"REGISTER")==0)
790 else if (strcmp(l->l1name,"INT")==0)
793 for(l=l->l1next;l!=0;l=l->l1next) {
794 tp->t_fields[i].t_type = type;
795 tp->t_fields[i].t_sname = l->l1name;
800 tp->t_format = formconversion(format,tp);
808 for(i=nmachregs+1;i<nmachregs+1+nmachtokens;i++)
809 if(machsets[number].set_val[i>>4]&(01<<(i&017)))
810 yyerror("No tokens allowed in this set");
813 findstructel(number,name,t) string name; int *t; {
816 register list2 structdecl;
819 for(i=1;i<=nmachregs;i++)
820 if (machsets[number].set_val[i>>4]&(01<<(i&017)))
821 yyerror("No registers allowed in this set");
823 for (i=nmachregs+1;i<nmachregs+1+nmachtokens;i++) {
824 if (machsets[number].set_val[i>>4]&(01<<(i&017))) {
825 if (structdecl == 0) {
826 structdecl = machtokens[i-(nmachregs+1)].t_struct;
827 tp = &machtokens[i-(nmachregs+1)];
828 } else if(structdecl != machtokens[i-(nmachregs+1)].t_struct)
829 yyerror("Multiple structs in this set");
832 if (structdecl == 0) {
833 yyerror("No structs in this set");
836 for(offset=0;offset<TOKENSIZE-1;offset++)
837 if(tp->t_fields[offset].t_type != 0 &&
838 strcmp(tp->t_fields[offset].t_sname,name)==0) {
839 *t = tp->t_fields[offset].t_type;
842 yyerror("No such field in this struct");
846 extern char em_flag[];
850 switch(em_flag[mn-sp_fmnem]&EM_PAR) {
866 commontype(e1,e2) expr_t e1,e2; {
868 if(e1.expr_typ != e2.expr_typ)
869 yyerror("Type incompatibility");
873 extern char em_mnem[][4];
875 #define HASHSIZE (2*(sp_lmnem-sp_fmnem))
880 } hashmnem[HASHSIZE];
885 for(i=0;i<=sp_lmnem-sp_fmnem;i++)
886 enter(em_mnem[i],i+sp_fmnem);
889 enter(name,value) char *name; {
892 h=hash(name)%HASHSIZE;
893 while (hashmnem[h].h_name[0] != 0)
895 strncpy(hashmnem[h].h_name,name,3);
896 hashmnem[h].h_value = value;
899 int mlookup(name) char *name; {
902 h = hash(name)%HASHSIZE;
903 while (strncmp(hashmnem[h].h_name,name,3) != 0 &&
904 hashmnem[h].h_name[0] != 0)
906 return(hashmnem[h].h_value&BMASK); /* 0 if not found */
911 register byte *bp,*tp;
913 unsigned short hashvalue;
918 bp = &pattern[index];
923 i |= (*tp++&BMASK)<<BSHIFT;
928 default: /* 3 or more */
929 hashvalue = (hashvalue<<4)^(*tp++&BMASK);
931 hashvalue = (hashvalue<<4)^(*tp++&BMASK);
933 hashvalue = (hashvalue<<4)^(*tp++&BMASK);
935 assert(hashvalue!= ILLHASH);
937 index = (bp[PO_NEXT]&BMASK)|(bp[PO_NEXT+1]<<BSHIFT);
938 bp[PO_HASH] = hashvalue>>BSHIFT;
940 bp[PO_NEXT] = pathash[hashvalue]&BMASK;
941 bp[PO_NEXT+1] = pathash[hashvalue]>>BSHIFT;
942 pathash[hashvalue] = i;
949 for(i=0;i<ITABSIZE;i++) {
951 for(ip=identtab[i];ip!=0;ip=ip->i_next)
952 printf("%-14s %1d %3d\n",ip->i_name,
953 ip->i_type,ip->i_i.i_regno);
956 for(i=2;i<nmachregs;i++) {
960 printf("%s = (\"%s\", %d",rp->rname,rp->rrepr,rp->rsize);
961 for(j=0;j<MAXMEMBERS;j++)
962 if(rp->rmembers[j] != 0)
963 printf(", %s",machregs[rp->rmembers[j]]->rname);
965 for(j=0;j<nprops;j++)
966 if(rp->rprop[j>>4]&(1<<(j&017)))
967 printf(", %s",machprops[j].propname->i_name);
985 fprintf(cfile,"%d, ",n&BMASK);
1008 chktabsiz(npatbytes,MAXPATTERN,"Pattern table");
1009 pattern[npatbytes++] = n;