main.c 22 KB

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