main.c 22 KB

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