main.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #include <em_spec.h>
  7. #include <em_flag.h>
  8. #include <em_reg.h>
  9. #include "booth.h"
  10. char * myalloc(n) {
  11. register char *p;
  12. p= malloc((unsigned)n);
  13. if (p==0) {
  14. yyerror("Out of core");
  15. exit(1);
  16. }
  17. return(p);
  18. }
  19. tstint(e) expr_t e; {
  20. if(e.expr_typ != TYPINT)
  21. yyerror("Must be integer expression");
  22. }
  23. tstbool(e) expr_t e; {
  24. if(e.expr_typ != TYPBOOL)
  25. yyerror("Must be boolean expression");
  26. }
  27. structsize(s) register list2 s; {
  28. register list1 l;
  29. register sum;
  30. sum = 0;
  31. while ( s != 0 ) {
  32. l = s->l2list->l1next;
  33. while ( l != 0 ) {
  34. sum++;
  35. l = l->l1next;
  36. }
  37. s = s->l2next;
  38. }
  39. return(sum);
  40. }
  41. list2 lookstruct(ll) list2 ll; {
  42. list3 l3;
  43. list2 l21,l22;
  44. list1 l11,l12;
  45. for (l3=structpool;l3 != 0;l3=l3->l3next) {
  46. for (l21=l3->l3list,l22=ll;l21!=0 && l22!=0;
  47. l21=l21->l2next,l22=l22->l2next) {
  48. for(l11=l21->l2list,l12=l22->l2list;
  49. l11!=0 && l12!=0 && strcmp(l11->l1name,l12->l1name)==0;
  50. l11=l11->l1next,l12=l12->l1next)
  51. ;
  52. if (l11!=0 || l12!=0)
  53. goto contin;
  54. }
  55. if(l21==0 && l22==0)
  56. return(l3->l3list);
  57. contin:;
  58. }
  59. l3 = (list3) myalloc(sizeof(struct list3str));
  60. l3->l3next=structpool;
  61. l3->l3list=ll;
  62. structpool=l3;
  63. return(ll);
  64. }
  65. instno(inst) inst_t inst; {
  66. register i,j;
  67. for(i=1;i<narinstance;i++) {
  68. if (arinstance[i].in_which != inst.in_which)
  69. continue;
  70. for(j=0;j<TOKENSIZE;j++)
  71. if(arinstance[i].in_info[j] != inst.in_info[j])
  72. goto cont;
  73. return(i);
  74. cont:;
  75. }
  76. chktabsiz(narinstance,MAXINSTANCE,"Instance table");
  77. arinstance[narinstance] = inst;
  78. return(narinstance++);
  79. }
  80. string scopy(s) string s; {
  81. register string t;
  82. t = (char *) myalloc(strlen(s)+1);
  83. strcpy(t,s);
  84. return(t);
  85. }
  86. strlookup(s) string s; {
  87. register i;
  88. for(i=0;i<ncodestrings;i++)
  89. if(strcmp(s,codestrings[i])==0)
  90. return(i);
  91. chktabsiz(ncodestrings,MAXSTRINGS,"string table");
  92. codestrings[ncodestrings] = scopy(s);
  93. return(ncodestrings++);
  94. }
  95. stringno(s) register string s; {
  96. char buf[256];
  97. register char *p=buf;
  98. while(*s != 0) switch(*s) {
  99. default:
  100. *p++ = *s++;
  101. continue;
  102. case '$':
  103. s++;
  104. switch(*s) {
  105. default:
  106. yyerror("Bad character after $ in codestring");
  107. case '$':
  108. *p++ = *s++;
  109. continue;
  110. case '1':
  111. case '2':
  112. case '3':
  113. case '4':
  114. case '5':
  115. case '6':
  116. case '7':
  117. case '8':
  118. case '9':
  119. *p++ = argtyp(patmnem[*s-'0']) == TYPINT ?
  120. PR_EMINT : PR_EMSTR;
  121. *p++ = *s++ -'0';
  122. continue;
  123. }
  124. case '%':
  125. s++;
  126. if (*s != '[') {
  127. if(*s == '%') {
  128. *p++ = *s++;
  129. continue;
  130. } else
  131. yyerror("Bad character following %% in codestring");
  132. } else
  133. s++;
  134. if(isdigit(*s)) {
  135. int num;
  136. num = *s++ - '0';
  137. if (num<1 || num>tokpatlen)
  138. yyerror("Number within %[] out of range");
  139. if (*s == ']') {
  140. s++;
  141. *p++ = PR_TOK;
  142. *p++ = num;
  143. } else if (*s++ != '.')
  144. yyerror("Bad character following %%[digit in codestring");
  145. else {
  146. char field[256];
  147. register char *f=field;
  148. int type,offset;
  149. while( *s != ']' && *s != 0)
  150. *f++ = *s++;
  151. *f++ = 0;
  152. if (*s != ']')
  153. yyerror("Unterminated %[] construction in codestring");
  154. else
  155. s++;
  156. if (isdigit(field[0])) {
  157. chkregexp(pattokexp[num]);
  158. *p++ = PR_SUBREG;
  159. *p++ = num;
  160. *p++ = atoi(field);
  161. } else {
  162. offset = findstructel(pattokexp[num],field,&type);
  163. *p++ = PR_TOKFLD;
  164. *p++ = num;
  165. *p++ = offset;
  166. }
  167. }
  168. } else if (*s >= 'a' && *s < 'a'+nallreg) {
  169. int reg,subreg;
  170. reg = *s++ -'a'+1;
  171. if(*s == ']')
  172. subreg = 255;
  173. else {
  174. if (*s != '.')
  175. yyerror("Bad character following %%[x in codestring");
  176. s++;
  177. if(!isdigit(*s))
  178. yyerror("Bad character following %%[x. in codestring");
  179. subreg = *s - '0';
  180. s++;
  181. if(*s != ']')
  182. yyerror("Bad character following %%[x.y in codestring");
  183. }
  184. s++;
  185. *p++ = PR_ALLREG;
  186. *p++ = reg;
  187. *p++ = subreg;
  188. } else
  189. yyerror("Bad character following %%[ in codestring");
  190. }
  191. *p++ = 0;
  192. return(strlookup(buf));
  193. }
  194. tabovf(tablename) string tablename; {
  195. char buf[256];
  196. sprintf(buf,"%s overflow",tablename);
  197. yyerror(buf);
  198. exit(-1);
  199. }
  200. main(argc,argv) char *argv[]; {
  201. while (--argc) {
  202. ++argv;
  203. if (argv[0][0]=='-') {
  204. switch (argv[0][1]) {
  205. case 'h':
  206. hname= &argv[0][2];
  207. break;
  208. case 'c':
  209. cname= &argv[0][2];
  210. break;
  211. default:
  212. fprintf(stderr,"Bad flag %s\n",argv[0]);
  213. break;
  214. }
  215. } else {
  216. iname= argv[0];
  217. }
  218. }
  219. inithash();
  220. initio();
  221. inittables();
  222. yyparse();
  223. if (nerrors==0) {
  224. compueq();
  225. hashpatterns();
  226. finishio();
  227. verbose();
  228. }
  229. debug();
  230. exit(nerrors);
  231. }
  232. lookup(comm,operator,lnode,rnode) {
  233. register node_p p;
  234. for (p=nodes+1;p<lastnode;p++) {
  235. if (p->ex_operator != operator)
  236. continue;
  237. if (!(p->ex_lnode == lnode && p->ex_rnode == rnode ||
  238. comm && p->ex_lnode == rnode && p->ex_rnode == lnode))
  239. continue;
  240. return(p-nodes);
  241. }
  242. if (lastnode >= &nodes[MAXNODES])
  243. yyerror("node table overflow");
  244. lastnode++;
  245. p->ex_operator = operator;
  246. p->ex_lnode = lnode;
  247. p->ex_rnode = rnode;
  248. return(p-nodes);
  249. }
  250. compueq() {
  251. register i,j;
  252. for (i=1;i<nmachregs;i++) {
  253. for (j=1;j<i;j++)
  254. if (eqregclass(i,j)) {
  255. stregclass[i] = stregclass[j];
  256. break;
  257. }
  258. if (j==i)
  259. stregclass[i] = nregclasses++;
  260. }
  261. }
  262. eqregclass(r1,r2) {
  263. register reginfo rp1,rp2;
  264. register i;
  265. short regbits[(MAXREGS+15)>>4];
  266. int member;
  267. rp1 = machregs[r1]; rp2 = machregs[r2];
  268. for (i=0;i<((nprops+15)>>4);i++)
  269. if (rp1->rprop[i] != rp2->rprop[i])
  270. return(0);
  271. for (i=0;i<((MAXREGS+15)>>4);i++)
  272. regbits[i] = 0;
  273. for (i=0;i<maxmembers;i++) {
  274. if (member = rp1->rmembers[i])
  275. regbits[member>>4] |= (1<<(member&017));
  276. }
  277. for (i=0;i<maxmembers;i++) {
  278. member = rp2->rmembers[i];
  279. if (regbits[member>>4]&(1<<(member&017)))
  280. return(0);
  281. }
  282. return(1);
  283. }
  284. unsigned hash(name) register string name; {
  285. register unsigned sum;
  286. register i;
  287. for (sum=i=0;*name;i+=3)
  288. sum ^= (*name++)<<(i&07);
  289. return(sum);
  290. }
  291. ident_p ilookup(name,enterf) string name; int enterf; {
  292. register ident_p p,*pp;
  293. pp = &identtab[hash(name)%ITABSIZE];
  294. while (*pp != 0) {
  295. if (strcmp((*pp)->i_name,name)==0)
  296. if (enterf != ENTER)
  297. return(*pp);
  298. else
  299. yyerror("Multiply defined symbol");
  300. pp = &(*pp)->i_next;
  301. }
  302. if (enterf == LOOKUP)
  303. yyerror("Undefined symbol");
  304. if (enterf == JUSTLOOKING)
  305. return(0);
  306. p = *pp = (ident_p) myalloc(sizeof(ident_t));
  307. p->i_name = name;
  308. p->i_next = 0;
  309. p->i_type = 0;
  310. return(p);
  311. }
  312. initio() {
  313. if (iname!=0 && freopen(iname,"r",stdin)==NULL) {
  314. fprintf(stderr,"Can't open %s\n",iname);
  315. exit(-1);
  316. }
  317. if ((cfile=fopen(cname,"w"))==NULL) {
  318. fprintf(stderr,"Can't create %s\n",cname);
  319. exit(-1);
  320. }
  321. if ((hfile=fopen(hname,"w"))==NULL) {
  322. fprintf(stderr,"Can't create %s\n",hname);
  323. exit(-1);
  324. }
  325. fprintf(cfile,"#include \"param.h\"\n");
  326. fprintf(cfile,"#include \"tables.h\"\n");
  327. fprintf(cfile,"#include \"types.h\"\n");
  328. fprintf(cfile,"#include <cg_pattern.h>\n");
  329. fprintf(cfile,"#include \"data.h\"\n");
  330. fprintf(cfile,"\nbyte coderules[] = {\n");
  331. patbyte(0);
  332. }
  333. exprlookup(sett) set_t sett; {
  334. register i,j,ok;
  335. for(i=0;i<nmachsets;i++) {
  336. ok= (sett.set_size == machsets[i].set_size);
  337. for(j=0;j<SETSIZE;j++) {
  338. if (sett.set_val[j] == machsets[i].set_val[j])
  339. continue;
  340. ok=0;
  341. break;
  342. }
  343. if (ok)
  344. return(i);
  345. }
  346. chktabsiz(nmachsets,MAXSETS,"Expression table");
  347. machsets[nmachsets] = sett;
  348. return(nmachsets++);
  349. }
  350. inittables() {
  351. register reginfo r;
  352. register i;
  353. inst_t inst;
  354. set_t sett;
  355. nodes[0].ex_operator=EX_CON;
  356. nodes[0].ex_lnode=0;
  357. nodes[0].ex_rnode=0;
  358. cocopropno=nprops++;
  359. r=(reginfo)myalloc(sizeof(struct reginfo));
  360. r->rname = "cc reg";
  361. r->rrepr = "CC";
  362. r->rsize = -1;
  363. r->rregvar= -1;
  364. for(i=0;i<MAXMEMBERS;i++)
  365. r->rmembers[i] = 0;
  366. for(i=0;i<PROPSETSIZE;i++)
  367. r->rprop[i] = 0;
  368. r->rprop[cocopropno>>4] |= (1<<(cocopropno&017));
  369. chktabsiz(nmachregs,MAXREGS,"Register table");
  370. machregs[nmachregs++] = r;
  371. inst.in_which = IN_RIDENT;
  372. inst.in_info[0] = nmachregs-1;
  373. for(i=1;i<TOKENSIZE;i++)
  374. inst.in_info[i]=0;
  375. ccinstanceno=instno(inst);
  376. ccregexpr=lookup(0,EX_REG,nmachregs-1,0);
  377. sett.set_size=0;
  378. for (i=0;i<SETSIZE;i++)
  379. sett.set_val[i]=0;
  380. sett.set_val[nmachregs>>4] |= (01<<(nmachregs&017));
  381. cocosetno=exprlookup(sett);
  382. }
  383. outregs() {
  384. register i,j,k;
  385. static short rset[(MAXREGS+15)>>4];
  386. int t,ready;
  387. fprintf(cfile,"char stregclass[] = {\n");
  388. for (i=0;i<nmachregs;i++)
  389. fprintf(cfile,"\t%d,\n",stregclass[i]);
  390. fprintf(cfile,"};\n\nstruct reginfo machregs[] = {\n{0},\n");
  391. for (i=1;i<nmachregs;i++) {
  392. fprintf(cfile,"{%d,%d",strlookup(machregs[i]->rrepr),
  393. machregs[i]->rsize);
  394. if (maxmembers!=0) {
  395. fprintf(cfile,",{");
  396. for(j=0;j<maxmembers;j++)
  397. fprintf(cfile,"%d,",machregs[i]->rmembers[j]);
  398. /* now compute and print set of registers
  399. * that clashes with this register.
  400. * A register clashes with al its children (and theirs)
  401. * and with all their parents.
  402. */
  403. for (j=0;j<((MAXREGS+15)>>4);j++)
  404. rset[j]=0;
  405. rset[i>>4] |= (1<<(i&017));
  406. do {
  407. ready=1;
  408. for (j=1;j<nmachregs;j++)
  409. if (rset[j>>4]&(1<<(j&017)))
  410. for (k=0;k<maxmembers;k++)
  411. if ((t=machregs[j]->rmembers[k])!=0) {
  412. if ((rset[t>>4]&(1<<(t&017)))==0)
  413. ready=0;
  414. rset[t>>4] |= (1<<(t&017));
  415. }
  416. } while (!ready);
  417. do {
  418. ready=1;
  419. for (j=1;j<nmachregs;j++)
  420. for (k=0;k<maxmembers;k++)
  421. if ((t=machregs[j]->rmembers[k])!=0)
  422. if (rset[t>>4]&(1<<(t&017))) {
  423. if (rset[j>>4]&(1<<(j&017))==0)
  424. ready=0;
  425. rset[j>>4] |= (1<<(j&017));
  426. }
  427. } while (!ready);
  428. fprintf(cfile,"},{");
  429. for (j=0;j<((nmachregs+15)>>4);j++)
  430. fprintf(cfile,"%d,",rset[j]);
  431. fprintf(cfile,"}");
  432. }
  433. if (machregs[i]->rregvar>=0)
  434. fprintf(cfile,",1");
  435. fprintf(cfile,"},\n");
  436. }
  437. fprintf(cfile,"};\n\n");
  438. }
  439. finishio() {
  440. register i;
  441. register node_p np;
  442. int j;
  443. int setsize;
  444. register move_p mp;
  445. fprintf(cfile,"};\n\n");
  446. if (wsize>0)
  447. fprintf(hfile,"#define TEM_WSIZE %d\n",wsize);
  448. else
  449. yyerror("Wordsize undefined");
  450. if (psize>0)
  451. fprintf(hfile,"#define TEM_PSIZE %d\n",psize);
  452. else
  453. yyerror("Pointersize undefined");
  454. if (bsize>=0)
  455. fprintf(hfile,"#define TEM_BSIZE %d\n",bsize);
  456. else
  457. yyerror("EM_BSIZE undefined");
  458. if (fmt!=0)
  459. fprintf(hfile,"#define WRD_FMT \"%s\"\n",fmt);
  460. fprintf(hfile,"#define MAXALLREG %d\n",maxallreg);
  461. setsize = (nmachregs+1 + nmachtokens + 15)>>4;
  462. fprintf(hfile,"#define SETSIZE %d\n",setsize);
  463. fprintf(hfile,"#define NPROPS %d\n",nprops);
  464. fprintf(hfile,"#define NREGS %d\n",nmachregs);
  465. fprintf(hfile,"#define REGSETSIZE %d\n",(nmachregs+15)>>4);
  466. fprintf(hfile,"#define TOKENSIZE %d\n",maxtokensize);
  467. fprintf(hfile,"#define MAXMEMBERS %d\n",maxmembers);
  468. fprintf(hfile,"#define LONGESTPATTERN %d\n",maxempatlen);
  469. fprintf(hfile,"#define MAXRULE %d\n",maxrule);
  470. fprintf(hfile,"#define NMOVES %d\n",nmoves);
  471. fprintf(hfile,"#define NC1 %d\n",nc1);
  472. if (nc2) {
  473. assert(maxsplit!=0);
  474. fprintf(hfile,"#define NC2 %d\n",nc2);
  475. fprintf(hfile,"#define MAXSPLIT %d\n",maxsplit);
  476. }
  477. fprintf(hfile,"#define NC3 %d\n",nc3);
  478. outregs();
  479. fprintf(cfile,"tkdef_t tokens[] = {\n");
  480. for(i=0;i<nmachtokens;i++) {
  481. fprintf(cfile,"{%d,{%d,%d},{",machtokens[i].t_size,
  482. machtokens[i].t_cost.c_size,
  483. machtokens[i].t_cost.c_time);
  484. for(j=0;j<maxtokensize;j++)
  485. fprintf(cfile,"%d,",machtokens[i].t_fields[j].t_type);
  486. fprintf(cfile,"},%d},\n",machtokens[i].t_format);
  487. }
  488. fprintf(cfile,"};\n\nnode_t enodes[] = {\n");
  489. for(np=nodes;np<lastnode;np++)
  490. fprintf(cfile,"{%d,%d,%d},\n",np->ex_operator,np->ex_lnode,
  491. np->ex_rnode);
  492. fprintf(cfile,"};\n\nstring codestrings[] = {\n");
  493. for(i=0;i<ncodestrings;i++) {
  494. register char *p;
  495. p=codestrings[i];
  496. fprintf(cfile,"\t\"");
  497. while (*p) {
  498. register int c = (*p) & BMASK;
  499. if (! isascii(c) || iscntrl(c)) {
  500. /* The next line used to have (c>>6)&03,
  501. but this triggered a bug in GCC 2.4.5
  502. on SPARC.
  503. */
  504. fprintf(cfile,"\\%c%c%c",((*p>>6) &03)+'0',
  505. ((c>>3)&07)+'0',(c&07)+'0');
  506. }
  507. else putc(c, cfile);
  508. p++;
  509. }
  510. fprintf(cfile,"\",\n");
  511. }
  512. fprintf(cfile,"};\n\nset_t machsets[] = {\n");
  513. for(i=0;i<nmachsets;i++) {
  514. fprintf(cfile,"{%d,{",machsets[i].set_size);
  515. for(j=0;j<setsize;j++)
  516. fprintf(cfile,"0%o,",machsets[i].set_val[j] & 0xFFFF);
  517. fprintf(cfile,"}},\n");
  518. }
  519. fprintf(cfile,"};\n\ninst_t tokeninstances[] = {\n");
  520. for(i=0;i<narinstance;i++) {
  521. fprintf(cfile,"{ %d, {",arinstance[i].in_which);
  522. for(j=0;j<=maxtokensize;j++)
  523. fprintf(cfile,"%d,",arinstance[i].in_info[j]);
  524. fprintf(cfile,"}},\n");
  525. }
  526. fprintf(cfile,"};\n\nmove_t moves[] = {\n");
  527. for (i=0;i<nmoves;i++) {
  528. mp = &machmoves[i];
  529. fprintf(cfile,"{%d,%d,%d,%d,%d,{%d,%d}},\n",
  530. mp->m_set1, mp->m_expr1,
  531. mp->m_set2, mp->m_expr2,
  532. mp->m_cindex,
  533. mp->m_cost.c_size,mp->m_cost.c_time);
  534. }
  535. fprintf(cfile,"};\n\nbyte pattern[] = {\n");
  536. for (i=0;i<npatbytes;i++) {
  537. fprintf(cfile,"%3d,",pattern[i]&BMASK);
  538. if ((i%10)==9)
  539. fprintf(cfile,"\n");
  540. }
  541. fprintf(cfile,"\n};\n\nint pathash[256] = {\n");
  542. for(i=0;i<256;i++) {
  543. fprintf(cfile,"%6d,",pathash[i]);
  544. if((i&07)==07)
  545. fprintf(cfile,"\n");
  546. }
  547. fprintf(cfile,"};\n\nc1_t c1coercs[] = {\n");
  548. for (i=0;i<nc1;i++)
  549. fprintf(cfile,"{%d,%d,%d,%d,{%d,%d}},\n",
  550. c1coercs[i].c1_texpno,
  551. c1coercs[i].c1_expr,
  552. c1coercs[i].c1_prop,
  553. c1coercs[i].c1_codep,
  554. c1coercs[i].c1_cost.c_size,
  555. c1coercs[i].c1_cost.c_time);
  556. if (nc2)
  557. fprintf(cfile,"};\n\nc2_t c2coercs[] = {\n");
  558. for (i=0;i<nc2;i++) {
  559. fprintf(cfile,"{%d,%d,{",
  560. c2coercs[i].c2_texpno,
  561. c2coercs[i].c2_nsplit);
  562. for (j=0;j<maxsplit;j++)
  563. fprintf(cfile,"%d,",c2coercs[i].c2_repl[j]);
  564. fprintf(cfile,"},%d},\n",c2coercs[i].c2_codep);
  565. }
  566. fprintf(cfile,"};\n\nc3_t c3coercs[] = {\n");
  567. for (i=0;i<nc3;i++)
  568. fprintf(cfile,"{%d,%d,%d,%d},\n",
  569. c3coercs[i].c3_texpno,
  570. c3coercs[i].c3_prop,
  571. c3coercs[i].c3_repl,
  572. c3coercs[i].c3_codep);
  573. fprintf(cfile,"};\n\n");
  574. for (i=0;i<nprops;i++) {
  575. fprintf(cfile,"struct reginfo *rlist%d[] = {\n",i);
  576. for (j=2;j<=nmachregs;j++) {
  577. if (machregs[j-1]->rregvar<0 &&
  578. (machprops[i].propset.set_val[j>>4]&(1<<(j&017))))
  579. fprintf(cfile,"\t&machregs[%d],\n",j-1);
  580. }
  581. fprintf(cfile,"\t0\n};\n");
  582. }
  583. fprintf(cfile,"struct reginfo **reglist[] = {\n");
  584. for (i=0;i<nprops;i++) {
  585. fprintf(cfile,"\trlist%d,\n",i);
  586. }
  587. fprintf(cfile,"};\n");
  588. fprintf(cfile,"unsigned cc1 = %u;\n",cc1);
  589. fprintf(cfile,"unsigned cc2 = %u;\n",cc2);
  590. fprintf(cfile,"unsigned cc3 = %u;\n",cc3);
  591. fprintf(cfile,"unsigned cc4 = %u;\n",cc4);
  592. if (rvused)
  593. outregvar();
  594. }
  595. outregvar() {
  596. register i,j;
  597. fprintf(hfile,"#define REGVARS\n");
  598. fprintf(cfile,"#include \"regvar.h\"\n");
  599. fprintf(cfile,"int nregvar[4] = { ");
  600. for (i=0;i<4;i++) fprintf(cfile,"%d, ",nregvar[i]);
  601. fprintf(cfile,"};\n");
  602. for (i=0;i<4;i++)
  603. if (nregvar[i]>0)
  604. fprintf(cfile,"struct regassigned ratar%d[%d];\n",
  605. i,nregvar[i]);
  606. for (i=0;i<4;i++) if (nregvar[i]>0) {
  607. fprintf(cfile,"int rvtar%d[] = {",i);
  608. for (j=0;j<nregvar[i];j++)
  609. fprintf(cfile,"%d,",rvnumbers[i][j]);
  610. fprintf(cfile,"};\n");
  611. }
  612. fprintf(cfile,"\nint *rvnumbers[] = {\n");
  613. for (i=0;i<4;i++)
  614. if (nregvar[i]>0)
  615. fprintf(cfile,"\trvtar%d,\n",i);
  616. else
  617. fprintf(cfile,"\t0,\n");
  618. fprintf(cfile,"};\n\nstruct regassigned *regassigned[] = {\n");
  619. for (i=0;i<4;i++)
  620. if (nregvar[i]>0)
  621. fprintf(cfile,"\tratar%d,\n",i);
  622. else
  623. fprintf(cfile,"\t0,\n");
  624. fprintf(cfile,"};\n");
  625. }
  626. verbose() {
  627. fprintf(stderr,"Codebytes %d\n",codebytes);
  628. fprintf(stderr,"Registers %d(%d)\n",nmachregs,MAXREGS);
  629. fprintf(stderr,"Properties %d(%d)\n",nprops,MAXPROPS);
  630. fprintf(stderr,"Tokens %d(%d)\n",nmachtokens,MAXTOKENS);
  631. fprintf(stderr,"Sets %d(%d)\n",nmachsets,MAXSETS);
  632. fprintf(stderr,"Tokeninstances %d(%d)\n",narinstance,MAXINSTANCE);
  633. fprintf(stderr,"Strings %d(%d)\n",ncodestrings,MAXSTRINGS);
  634. fprintf(stderr,"Enodes %d(%d)\n",lastnode-nodes,MAXNODES);
  635. fprintf(stderr,"Patbytes %d(%d)\n",npatbytes,MAXPATTERN);
  636. }
  637. inbetween() {
  638. register ident_p ip;
  639. register i,j;
  640. register move_p mp;
  641. lookident=1; /* for lexical analysis */
  642. chktabsiz(nmachsets+1,MAXSETS,"Expressiontable");
  643. for (i=0;i<SETSIZE;i++)
  644. machsets[nmachsets].set_val[i] = 0xFFFF;
  645. machsets[nmachsets].set_val[0] &= ~1;
  646. machsets[nmachsets].set_size = 0;
  647. ip=ilookup("SCRATCH",ENTER);
  648. ip->i_type=IEXP;
  649. ip->i_i.i_expno = nmachsets++;
  650. for (i=0;i<SETSIZE;i++)
  651. machsets[nmachsets].set_val[i] = 0xFFFF;
  652. machsets[nmachsets].set_size = 0;
  653. ip=ilookup("ALL",ENTER);
  654. ip->i_type=IEXP;
  655. allexpno = ip->i_i.i_expno = nmachsets++;
  656. mp = &machmoves[nmoves++];
  657. mp->m_set1 = cocosetno;
  658. mp->m_expr1 = 0;
  659. mp->m_set2 = nmachsets-1;
  660. mp->m_expr2 = 0;
  661. mp->m_cindex = 0;
  662. mp->m_cost.c_size = 0;
  663. mp->m_cost.c_time = 0;
  664. /*
  665. * Create sets of registers per property
  666. */
  667. for (i=0;i<nprops;i++) {
  668. short *sp = machprops[i].propset.set_val;
  669. sp[0] |= 1;
  670. for (j=2;j<=nmachregs;j++)
  671. if (machregs[j-1]->rprop[i>>4]&(1<<(i&017)))
  672. sp[j>>4] |= (1<<(j&017));
  673. }
  674. }
  675. formconversion(p,tp) register char *p; register token_p tp; {
  676. char buf[256];
  677. register char *q=buf;
  678. char field[256];
  679. register char *f;
  680. int i;
  681. if (p==0)
  682. return(0);
  683. while (*p) switch(*p) {
  684. default: *q++ = *p++; continue;
  685. case '%':
  686. p++;
  687. if(*p == '%') {
  688. *q++ = *p++;
  689. continue;
  690. }
  691. if (*p == '[')
  692. p++;
  693. else
  694. yyerror("Bad character after % in format");
  695. f=field;
  696. while (*p != 0 && *p != ']')
  697. *f++ = *p++;
  698. *f++ = 0;
  699. if (*p == ']')
  700. p++;
  701. else
  702. yyerror("Unterminated %[] construct in format");
  703. for (i=0;i<TOKENSIZE-1;i++)
  704. if (strcmp(field,tp->t_fields[i].t_sname)==0)
  705. break;
  706. if (i==TOKENSIZE-1)
  707. yyerror("Unknown field in %[] construct in format");
  708. *q++ = i+1;
  709. }
  710. *q++ = 0;
  711. return(strlookup(buf));
  712. }
  713. setfields(tp,format) register token_p tp; string format; {
  714. register i;
  715. list2 ll;
  716. register list1 l;
  717. int type;
  718. for(i=0;i<TOKENSIZE-1;i++)
  719. tp->t_fields[i].t_type = 0;
  720. i=0;
  721. for(ll=tp->t_struct;ll!=0;ll=ll->l2next) {
  722. l=ll->l2list;
  723. if(strcmp(l->l1name,"REGISTER")==0)
  724. type = TYPREG;
  725. else if (strcmp(l->l1name,"INT")==0)
  726. type = TYPINT;
  727. else type = TYPSTR;
  728. for(l=l->l1next;l!=0;l=l->l1next) {
  729. tp->t_fields[i].t_type = type;
  730. tp->t_fields[i].t_sname = l->l1name;
  731. i++;
  732. }
  733. }
  734. if (format != 0)
  735. tp->t_format = formconversion(format,tp);
  736. else
  737. tp->t_format = -1;
  738. }
  739. chkregexp(number) {
  740. register i;
  741. for(i=nmachregs+1;i<nmachregs+1+nmachtokens;i++)
  742. if(machsets[number].set_val[i>>4]&(01<<(i&017)))
  743. yyerror("No tokens allowed in this set");
  744. }
  745. findstructel(number,name,t) string name; int *t; {
  746. register i;
  747. register token_p tp;
  748. register list2 structdecl;
  749. int offset;
  750. for(i=1;i<=nmachregs;i++)
  751. if (machsets[number].set_val[i>>4]&(01<<(i&017)))
  752. yyerror("No registers allowed in this set");
  753. structdecl = 0;
  754. for (i=nmachregs+1;i<nmachregs+1+nmachtokens;i++) {
  755. if (machsets[number].set_val[i>>4]&(01<<(i&017))) {
  756. if (structdecl == 0) {
  757. structdecl = machtokens[i-(nmachregs+1)].t_struct;
  758. tp = &machtokens[i-(nmachregs+1)];
  759. } else if(structdecl != machtokens[i-(nmachregs+1)].t_struct)
  760. yyerror("Multiple structs in this set");
  761. }
  762. }
  763. if (structdecl == 0) {
  764. yyerror("No structs in this set");
  765. return(0);
  766. }
  767. for(offset=0;offset<TOKENSIZE-1;offset++)
  768. if(tp->t_fields[offset].t_type != 0 &&
  769. strcmp(tp->t_fields[offset].t_sname,name)==0) {
  770. *t = tp->t_fields[offset].t_type;
  771. return(offset+1);
  772. }
  773. yyerror("No such field in this struct");
  774. return(0);
  775. }
  776. extern char em_flag[];
  777. argtyp(mn) {
  778. switch(em_flag[mn-sp_fmnem]&EM_PAR) {
  779. case PAR_W:
  780. case PAR_S:
  781. case PAR_Z:
  782. case PAR_O:
  783. case PAR_N:
  784. case PAR_L:
  785. case PAR_F:
  786. case PAR_R:
  787. case PAR_C:
  788. return(TYPINT);
  789. default:
  790. return(TYPSTR);
  791. }
  792. }
  793. commontype(e1,e2) expr_t e1,e2; {
  794. if(e1.expr_typ != e2.expr_typ)
  795. yyerror("Type incompatibility");
  796. return(e1.expr_typ);
  797. }
  798. extern char em_mnem[][4];
  799. #define HASHSIZE (2*(sp_lmnem-sp_fmnem))
  800. struct hashmnem {
  801. char h_name[3];
  802. byte h_value;
  803. } hashmnem[HASHSIZE];
  804. inithash() {
  805. register i;
  806. for(i=0;i<=sp_lmnem-sp_fmnem;i++)
  807. enter(em_mnem[i],i+sp_fmnem);
  808. }
  809. enter(name,value) char *name; {
  810. register unsigned h;
  811. h=hash(name)%HASHSIZE;
  812. while (hashmnem[h].h_name[0] != 0)
  813. h = (h+1)%HASHSIZE;
  814. strncpy(hashmnem[h].h_name,name,3);
  815. hashmnem[h].h_value = value;
  816. }
  817. int mlookup(name) char *name; {
  818. register unsigned h;
  819. h = hash(name)%HASHSIZE;
  820. while (strncmp(hashmnem[h].h_name,name,3) != 0 &&
  821. hashmnem[h].h_name[0] != 0)
  822. h = (h+1)%HASHSIZE;
  823. return(hashmnem[h].h_value&BMASK); /* 0 if not found */
  824. }
  825. hashpatterns() {
  826. short index;
  827. register byte *bp,*tp;
  828. register short i;
  829. unsigned short hashvalue;
  830. int patlen;
  831. index = prevind;
  832. while (index != 0) {
  833. bp = &pattern[index];
  834. tp = &bp[PO_MATCH];
  835. i = *tp++&BMASK;
  836. if (i==BMASK) {
  837. i = *tp++&BMASK;
  838. i |= (*tp++&BMASK)<<BSHIFT;
  839. }
  840. patlen = i;
  841. hashvalue = 0;
  842. switch(patlen) {
  843. default: /* 3 or more */
  844. hashvalue = (hashvalue<<4)^(*tp++&BMASK);
  845. case 2:
  846. hashvalue = (hashvalue<<4)^(*tp++&BMASK);
  847. case 1:
  848. hashvalue = (hashvalue<<4)^(*tp++&BMASK);
  849. }
  850. assert(hashvalue!= ILLHASH);
  851. i=index;
  852. index = (bp[PO_NEXT]&BMASK)|(bp[PO_NEXT+1]<<BSHIFT);
  853. bp[PO_HASH] = hashvalue>>BSHIFT;
  854. hashvalue &= BMASK;
  855. bp[PO_NEXT] = pathash[hashvalue]&BMASK;
  856. bp[PO_NEXT+1] = pathash[hashvalue]>>BSHIFT;
  857. pathash[hashvalue] = i;
  858. }
  859. }
  860. debug() {
  861. register i,j;
  862. for(i=0;i<ITABSIZE;i++) {
  863. register ident_p ip;
  864. for(ip=identtab[i];ip!=0;ip=ip->i_next)
  865. printf("%-14s %1d %3d\n",ip->i_name,
  866. ip->i_type,ip->i_i.i_regno);
  867. }
  868. for(i=2;i<nmachregs;i++) {
  869. register reginfo rp;
  870. rp=machregs[i];
  871. printf("%s = (\"%s\", %d",rp->rname,rp->rrepr,rp->rsize);
  872. for(j=0;j<MAXMEMBERS;j++)
  873. if(rp->rmembers[j] != 0)
  874. printf(", %s",machregs[rp->rmembers[j]]->rname);
  875. printf(")");
  876. for(j=0;j<nprops;j++)
  877. if(rp->rprop[j>>4]&(1<<(j&017)))
  878. printf(", %s",machprops[j].propname->i_name);
  879. printf(".\n");
  880. }
  881. }
  882. out(n) {
  883. assert(n>=0);
  884. if (n<128)
  885. outbyte(n);
  886. else {
  887. outbyte(n/256+128);
  888. outbyte(n%256);
  889. }
  890. }
  891. outbyte(n) {
  892. fprintf(cfile,"%d, ",n&BMASK);
  893. codebytes++;
  894. }
  895. pat(n) {
  896. assert(n>=0);
  897. if (n<128)
  898. patbyte(n);
  899. else {
  900. patbyte(n/256+128);
  901. patbyte(n%256);
  902. }
  903. }
  904. patshort(n) {
  905. patbyte(n&BMASK);
  906. patbyte(n>>BSHIFT);
  907. }
  908. patbyte(n) {
  909. chktabsiz(npatbytes,MAXPATTERN,"Pattern table");
  910. pattern[npatbytes++] = n;
  911. }
  912. max(a,b) {
  913. if (a>b)
  914. return(a);
  915. return(b);
  916. }