output.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* #define CODEDEBUG */ /* print readable code */
  6. #ifdef CODEDEBUG
  7. int code_in_c=0; /* put readable code in "code" */
  8. int tabledebug=1; /* generate code for table debugging */
  9. #else
  10. int code_in_c=1; /* put code in "tables.c" */
  11. int tabledebug=0; /* do not generate code for table debugging */
  12. #endif
  13. int verbose=0; /* print all statistics */
  14. int use_tes; /* use top element size information */
  15. char *c_file= "tables.c";
  16. char *h_file= "tables.H";
  17. char *cd_file= "code";
  18. #ifndef NORCSID
  19. static char rcsid[]= "$Id$";
  20. #endif
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include "assert.h"
  24. #include "varinfo.h"
  25. #include "param.h"
  26. #include "reg.h"
  27. #include "property.h"
  28. #include "token.h"
  29. #include "set.h"
  30. #include "instruct.h"
  31. #include "lookup.h"
  32. #include <cgg_cg.h>
  33. #include "pseudo.h"
  34. #include "regvar.h"
  35. #include "extern.h"
  36. #define BMASK 0xFF
  37. #define BSHIFT 8
  38. FILE *ctable,*htable;
  39. FILE *code;
  40. short *lineset;
  41. int maxline;
  42. extern int nstrings;
  43. extern char *l_strings[];
  44. extern int ninstances;
  45. extern inst_t l_instances[];
  46. extern int nmoves;
  47. extern move_t l_moves[];
  48. extern int ntests;
  49. extern test_t l_tests[];
  50. extern int nstacks;
  51. extern c1_t l_stacks[];
  52. extern int ncoercs;
  53. extern c3_t l_coercs[];
  54. extern int nsplit,maxsplit;
  55. extern c2_t l_split[];
  56. extern set_t l_sets[];
  57. int maxallreg=0;
  58. int maxregvars=0;
  59. int setsize;
  60. void pat(int n);
  61. void patbyte(int n);
  62. void patshort(int n);
  63. void opnfile(FILE **f, char *s)
  64. {
  65. if ((*f=fopen(s,"w"))==NULL)
  66. fatal("Can't create %s",s);
  67. }
  68. void unlfile(FILE *f, char *s)
  69. {
  70. if (f) fclose(f);
  71. if (unlink(s)<0)
  72. error("%s incorrect, must be removed!!",s);
  73. }
  74. void initio()
  75. {
  76. opnfile(&ctable,c_file);
  77. opnfile(&htable,h_file);
  78. if (code_in_c)
  79. fprintf(ctable,"char coderules[] = {");
  80. else
  81. opnfile(&code,cd_file);
  82. patbyte(0);
  83. if (tabledebug)
  84. lineset = (short *) myalloc(SZOFSET(MAXSOURCELINES)*sizeof(short));
  85. }
  86. void finishcode()
  87. {
  88. if (code_in_c)
  89. fprintf(ctable,"\n};\n\n");
  90. fprintf(ctable, "int allsetno = %d;\n", allsetno);
  91. if (tabledebug) {
  92. int fd;
  93. int sz;
  94. if ((fd=creat("lineset",0666))>=0) {
  95. sz = SZOFSET(maxline)*2;
  96. write(fd,&sz,sizeof(int));
  97. write(fd,lineset,sz);
  98. close(fd);
  99. } else
  100. error("Can't create lineset");
  101. }
  102. }
  103. void errorexit()
  104. {
  105. unlfile(ctable,c_file);
  106. unlfile(htable,h_file);
  107. if (!code_in_c)
  108. unlfile(code,cd_file);
  109. }
  110. #ifdef CODEDEBUG
  111. #define code8(x) fprintf(code,"%s","x")
  112. #define code8nl(x) fprintf(code,"%s\n","x")
  113. #define code53(x,y) fprintf(code,"%s-%d","x",y)
  114. #define codeint(x) fprintf(code," %d",x)
  115. #define codenl() fprintf(code,"\n")
  116. #else /* CODEDEBUG */
  117. #define codenl()
  118. #define code8nl(x) code8(x)
  119. void code8(int x)
  120. {
  121. codeindex++;
  122. if (code_in_c)
  123. fprintf(ctable,"%d,",x&0377);
  124. else
  125. putc(x,code);
  126. }
  127. void code53(int x, int y)
  128. {
  129. code8(x+(y<<5));
  130. }
  131. void codeint(int x)
  132. {
  133. assert(x>=0 && x<=32767);
  134. if (x<128) {
  135. code8(x);
  136. } else {
  137. code8(x/256+128);
  138. code8(x%256);
  139. }
  140. }
  141. #endif /* CODEDEBUG */
  142. int prevind=0;
  143. int npatbytes= -1;
  144. char pattern[MAXPATBYTES];
  145. int pathash[256];
  146. void outpatterns()
  147. {
  148. extern int npatterns;
  149. extern int patindex[];
  150. extern int empatlen;
  151. extern int emmnem[];
  152. extern int empatexpr;
  153. int i;
  154. if (!inproc) {
  155. patbyte(0);
  156. patshort(prevind);
  157. prevind = npatbytes-2;
  158. patbyte(empatlen);
  159. for(i=0;i<empatlen;i++)
  160. patbyte(emmnem[i]);
  161. pat(empatexpr);
  162. }
  163. if (callproc==0) {
  164. patbyte(npatterns);
  165. for(i=0;i<npatterns;i++)
  166. pat(patindex[i]);
  167. } else {
  168. patbyte(0);
  169. pat(callproc);
  170. pat(nprocargs);
  171. for (i = 0; i < nprocargs; i++) pat(procarg[i]);
  172. }
  173. }
  174. void pat(int n)
  175. {
  176. assert(n>=0);
  177. if (n<128)
  178. patbyte(n);
  179. else {
  180. patbyte(n/256+128);
  181. patbyte(n%256);
  182. }
  183. }
  184. void patshort(int n)
  185. {
  186. patbyte(n%256);
  187. patbyte(n/256);
  188. }
  189. void patbyte(int n)
  190. {
  191. NEXT(npatbytes, MAXPATBYTES, "Pattern bytes");
  192. pattern[npatbytes]=n;
  193. }
  194. void hashpatterns()
  195. {
  196. short index;
  197. char *bp,*tp;
  198. short i;
  199. short hashvalue;
  200. int patlen;
  201. index = prevind;
  202. while (index != 0) {
  203. bp = &pattern[index];
  204. tp = &bp[PO_MATCH];
  205. i = *tp++&BMASK;
  206. if (i==BMASK) {
  207. i = *tp++&BMASK;
  208. i |= (*tp++&BMASK)<<BSHIFT;
  209. }
  210. patlen = i;
  211. hashvalue = 0;
  212. switch(patlen) {
  213. default: /* 3 or more */
  214. hashvalue = (hashvalue<<4)^(*tp++&BMASK);
  215. case 2:
  216. hashvalue = (hashvalue<<4)^(*tp++&BMASK);
  217. case 1:
  218. hashvalue = (hashvalue<<4)^(*tp++&BMASK);
  219. }
  220. assert(hashvalue!= ILLHASH);
  221. i=index;
  222. index = (bp[PO_NEXT]&BMASK)|(bp[PO_NEXT+1]<<BSHIFT);
  223. bp[PO_HASH] = hashvalue>>BSHIFT;
  224. hashvalue &= BMASK;
  225. bp[PO_NEXT] = pathash[hashvalue]&BMASK;
  226. bp[PO_NEXT+1] = pathash[hashvalue]>>BSHIFT;
  227. pathash[hashvalue] = i;
  228. }
  229. }
  230. void outincludes()
  231. {
  232. fprintf(ctable,"#include \"param.h\"\n");
  233. fprintf(ctable,"#include \"tables.h\"\n");
  234. fprintf(ctable,"#include \"types.h\"\n");
  235. fprintf(ctable,"#include <cgg_cg.h>\n");
  236. fprintf(ctable,"#include \"data.h\"\n");
  237. }
  238. void outregs()
  239. {
  240. int i,j,k;
  241. short rset[SZOFSET(MAXREGS)];
  242. short clashlist[MAXREGS*MAXREGS];
  243. int iclashlist = 0;
  244. int t,ready;
  245. fprintf(ctable,"char stregclass[] = {\n");
  246. for (i=0;i<nregs;i++)
  247. fprintf(ctable,"\t%d,\n",l_regs[i].ri_class);
  248. fprintf(ctable,"};\n\nstruct reginfo machregs[] = {\n{0},\n");
  249. for (i=1;i<nregs;i++) {
  250. fprintf(ctable,"{%d,%d",strlookup(l_regs[i].ri_repr),
  251. l_regs[i].ri_size);
  252. if (maxmembers!=0) {
  253. fprintf(ctable,",{");
  254. for(j=0;j<maxmembers;j++)
  255. fprintf(ctable,"%d,",l_regs[i].ri_memb[j]);
  256. /* now compute and print set of registers
  257. * that clashes with this register.
  258. * A register clashes with al its children (and theirs)
  259. * and with all their parents.
  260. */
  261. for (j=0;j<SZOFSET(MAXREGS);j++)
  262. rset[j]=0;
  263. BIS(rset,i);
  264. do {
  265. ready=1;
  266. for (j=1;j<nregs;j++)
  267. if (BIT(rset,j))
  268. for (k=0;k<2;k++)
  269. if ((t=l_regs[j].ri_memb[k])!=0) {
  270. if (BIT(rset,t)==0)
  271. ready=0;
  272. BIS(rset,t);
  273. }
  274. } while (!ready);
  275. do {
  276. ready=1;
  277. for (j=1;j<nregs;j++)
  278. for (k=0;k<2;k++)
  279. if ((t=l_regs[j].ri_memb[k])!=0)
  280. if (BIT(rset,t)) {
  281. if (BIT(rset,j)==0)
  282. ready=0;
  283. BIS(rset,j);
  284. }
  285. } while (!ready);
  286. fprintf(ctable,"},{");
  287. for (j=0;j<SZOFSET(nregs);j++)
  288. fprintf(ctable,"0%o,",rset[j]&0xFFFF);
  289. fprintf(ctable,"}, %d", iclashlist);
  290. for (j = 1; j < nregs; j++) {
  291. if (BIT(rset, j)) clashlist[iclashlist++] = j;
  292. }
  293. clashlist[iclashlist++] = 0;
  294. }
  295. fprintf(ctable,",%d",l_regs[i].ri_rregvar>=0);
  296. fprintf(ctable,"},\n");
  297. }
  298. fprintf(ctable,"};\n\n short clashlist[] = {\n\t");
  299. for (i = 0; i < iclashlist; i++) {
  300. fprintf(ctable, "%d, ", clashlist[i]);
  301. if (clashlist[i] == 0) fprintf(ctable, "\n\t");
  302. }
  303. fprintf(ctable, "0};\n\n");
  304. }
  305. void outregvars()
  306. {
  307. int i,j;
  308. fprintf(htable,"#define REGVARS\n");
  309. fprintf(ctable,"#include \"regvar.h\"\n");
  310. fprintf(ctable,"int nregvar[4] = { ");
  311. for (i=0;i<4;i++) {
  312. fprintf(ctable,"%d, ",nregvar[i]);
  313. if (nregvar[i]>maxregvars)
  314. maxregvars = nregvar[i];
  315. }
  316. fprintf(ctable,"};\n");
  317. for (i=0;i<4;i++)
  318. if (nregvar[i]>0)
  319. fprintf(ctable,"struct regassigned ratar%d[%d];\n",
  320. i,nregvar[i]);
  321. for (i=0;i<4;i++) if (nregvar[i]>0) {
  322. fprintf(ctable,"int rvtar%d[] = {",i);
  323. for (j=0;j<nregvar[i];j++)
  324. fprintf(ctable,"%d,",rvnumbers[i][j]);
  325. fprintf(ctable,"};\n");
  326. }
  327. fprintf(ctable,"\nint *rvnumbers[] = {\n");
  328. for (i=0;i<4;i++)
  329. if (nregvar[i]>0)
  330. fprintf(ctable,"\trvtar%d,\n",i);
  331. else
  332. fprintf(ctable,"\t0,\n");
  333. fprintf(ctable,"};\n\nstruct regassigned *regassigned[] = {\n");
  334. for (i=0;i<4;i++)
  335. if (nregvar[i]>0)
  336. fprintf(ctable,"\tratar%d,\n",i);
  337. else
  338. fprintf(ctable,"\t0,\n");
  339. fprintf(ctable,"};\n");
  340. }
  341. int typeconv(int n)
  342. {
  343. if (n>=0) return(2);
  344. if (n== -1) return(1);
  345. if (n== -2) return(3);
  346. assert (n== -3);
  347. return(0);
  348. }
  349. void outfmt(char *p)
  350. {
  351. register int c;
  352. fprintf(ctable,"\"");
  353. while ((c= (*p++&0377))!=0) {
  354. if (! isascii(c) || iscntrl(c)) {
  355. fprintf(ctable,"\\%c%c%c",
  356. ((c&~0300)>>6) + '0', ((c&070)>>3)+'0',
  357. (c&07)+'0');
  358. }
  359. else fprintf(ctable, "%c",c);
  360. }
  361. fprintf(ctable,"\"");
  362. }
  363. void outtokens()
  364. {
  365. int tokno,i;
  366. token_p tp;
  367. fprintf(ctable,"tkdef_t tokens[] = {{0},\n");
  368. for (tokno=1;tokno<ntokens;tokno++) {
  369. tp = l_tokens[tokno];
  370. fprintf(ctable,"/* %3d */{%d,{%d,%d},{", tokno,
  371. tp->tk_size, tp->tk_cost.ct_space, tp->tk_cost.ct_time);
  372. for(i=0;i<maxtokensize;i++)
  373. fprintf(ctable,"%d,",typeconv(tp->tk_att[i].ta_type));
  374. fprintf(ctable,"},%d},\t/* ",tp->tk_format);
  375. if (tp->tk_format >= 0) outfmt(l_strings[tp->tk_format]);
  376. else fprintf(ctable, "(no format)");
  377. fprintf(ctable," */\n");
  378. }
  379. fprintf(ctable,"{0}};\n\n");
  380. }
  381. void outenodes()
  382. {
  383. node_p np;
  384. extern node_t nodes[];
  385. extern int nnodes;
  386. fprintf(ctable,"node_t enodes[] = {\n");
  387. for (np=nodes;np<&nodes[nnodes];np++)
  388. fprintf(ctable,"{%d,%d,%d},\n",
  389. np->ex_operator,np->ex_lnode,np->ex_rnode);
  390. fprintf(ctable,"};\n\n");
  391. }
  392. void outstrings()
  393. {
  394. int i;
  395. char *p;
  396. int c;
  397. extern char * filename;
  398. if (tabledebug)
  399. fprintf(ctable,"char *tablename = \"%s\";\n",filename);
  400. fprintf(ctable,"string codestrings[] = {\n");
  401. for(i=0;i<nstrings;i++) {
  402. fprintf(ctable,"\t");
  403. outfmt(l_strings[i]);
  404. #if 0
  405. while ((c= (*p++&0377))!=0) {
  406. if (! isascii(c) || iscntrl(c)) {
  407. fprintf(ctable,"\\%c%c%c",
  408. ((c&~0300)>>6) + '0', ((c&070)>>3)+'0',
  409. (c&07)+'0');
  410. }
  411. else fprintf(ctable, "%c",c);
  412. }
  413. fprintf(ctable,"\",\n");
  414. #endif
  415. fprintf(ctable,",\n");
  416. }
  417. fprintf(ctable,"};\n\n");
  418. }
  419. extern set_t unstackset;
  420. void outsets()
  421. {
  422. int i;
  423. set_p sp;
  424. fprintf(ctable,"set_t machsets[] = {\n");
  425. for (sp=l_sets;sp< &l_sets[nsets]; sp++) {
  426. fprintf(ctable,"/* %3ld */ {%3d,{",(long)(sp-l_sets),sp->set_size);
  427. for (i=0;i<setsize;i++)
  428. fprintf(ctable,"0x%x,",sp->set_val[i]&0xFFFF);
  429. fprintf(ctable,"}},\n");
  430. }
  431. fprintf(ctable,"};\n\n");
  432. fprintf(ctable, "set_t unstackset = { %3d,{\n", unstackset.set_size);
  433. for (i = 0; i<setsize;i++)
  434. fprintf(ctable,"0x%x,",unstackset.set_val[i]&0xFFFF);
  435. fprintf(ctable,"}};\n\n");
  436. }
  437. void outinstances()
  438. {
  439. inst_p ip;
  440. int i;
  441. fprintf(ctable,"inst_t tokeninstances[] = {\n");
  442. for (ip=l_instances;ip< &l_instances[ninstances]; ip++) {
  443. fprintf(ctable,"{ %d, {",ip->in_which);
  444. for(i=0;i<=maxtokensize;i++)
  445. fprintf(ctable,"%d,",ip->in_info[i]);
  446. fprintf(ctable,"}},\n");
  447. }
  448. fprintf(ctable,"};\n\n");
  449. }
  450. void outmoves()
  451. {
  452. move_p mp;
  453. fprintf(ctable,"move_t moves[] = {\n");
  454. for (mp=l_moves; mp< &l_moves[nmoves]; mp++)
  455. fprintf(ctable,"{%d,%d,%d,%d,%d},\n",
  456. mp->m_set1, mp->m_expr1,
  457. mp->m_set2, mp->m_expr2,
  458. mp->m_cindex);
  459. fprintf(ctable,"{-1}\n};\n\n");
  460. }
  461. void outtests()
  462. {
  463. test_p tp;
  464. fprintf(ctable,"test_t tests[] = {\n");
  465. for (tp=l_tests; tp< &l_tests[ntests]; tp++)
  466. fprintf(ctable,"{%d,%d,%d},\n",
  467. tp->t_set, tp->t_expr,
  468. tp->t_cindex);
  469. fprintf(ctable,"{-1}\n};\n\n");
  470. }
  471. void outstacks()
  472. {
  473. c1_p cp;
  474. fprintf(ctable,"c1_t c1coercs[] = {\n");
  475. for (cp=l_stacks; cp< &l_stacks[nstacks]; cp++)
  476. fprintf(ctable,"{%d,%d,%d,%d},\n",
  477. cp->c1_texpno, cp->c1_expr,
  478. cp->c1_prop, cp->c1_codep);
  479. fprintf(ctable,"{-1}\n};\n\n");
  480. }
  481. void outsplits()
  482. {
  483. c2_p cp;
  484. int i;
  485. fprintf(ctable,"c2_t c2coercs[] = {\n");
  486. for (cp=l_split; cp< &l_split[nsplit]; cp++) {
  487. fprintf(ctable,"{%d,%d,%d,{",
  488. cp->c2_texpno, cp->c2_expr, cp->c2_nsplit);
  489. for (i=0;i<maxsplit;i++)
  490. fprintf(ctable,"%d,",cp->c2_repl[i]);
  491. fprintf(ctable,"},%d},\n",cp->c2_codep);
  492. }
  493. fprintf(ctable,"{-1}\n};\n\n");
  494. }
  495. void outcoercs()
  496. {
  497. c3_p cp;
  498. fprintf(ctable,"c3_t c3coercs[] = {\n");
  499. for (cp=l_coercs; cp< &l_coercs[ncoercs]; cp++)
  500. fprintf(ctable,"{%d,%d,%d,%d,%d},\n",
  501. cp->c3_texpno, cp->c3_expr,
  502. cp->c3_prop, cp->c3_repl, cp->c3_codep);
  503. fprintf(ctable,"{-1}\n};\n\n");
  504. }
  505. void outproplists()
  506. {
  507. int propno;
  508. int regno;
  509. for(propno=0;propno<nprops;propno++) {
  510. fprintf(ctable,"struct reginfo *rlist%d[] = {\n",propno);
  511. for(regno=1;regno<nregs;regno++)
  512. if (BIT(l_props[propno].pr_regset,regno))
  513. fprintf(ctable,"&machregs[%d],\n",regno);
  514. fprintf(ctable,"0\n};\n");
  515. }
  516. fprintf(ctable,"struct reginfo **reglist[] = {\n");
  517. for(propno=0;propno<nprops;propno++)
  518. fprintf(ctable,"rlist%d,\n",propno);
  519. fprintf(ctable,"};\n\n");
  520. }
  521. void outconsts()
  522. {
  523. fprintf(ctable,"unsigned cc1 = %u;\n",fc1);
  524. fprintf(ctable,"unsigned cc2 = %u;\n",fc2);
  525. fprintf(ctable,"unsigned cc3 = %u;\n",fc3);
  526. fprintf(ctable,"unsigned cc4 = %u;\n",fc4);
  527. }
  528. void cdef(char *s, int n)
  529. {
  530. fprintf(htable,"#define %s %d\n",s,n);
  531. }
  532. void passon(char *s)
  533. {
  534. char buf[32];
  535. sprintf(buf,"T%s",s);
  536. cdef(buf,cmustbeset(s));
  537. }
  538. void outdefs()
  539. {
  540. symbol *sy_p;
  541. extern int maxempatlen,maxrule;
  542. char *wrdfmt;
  543. passon("EM_WSIZE");
  544. passon("EM_PSIZE");
  545. passon("EM_BSIZE");
  546. if ((sy_p=lookup("FORMAT",symsconst,justlooking))!=0) {
  547. wrdfmt = l_strings[sy_p->sy_value.syv_stringno];
  548. fprintf(htable,"#define WRD_FMT \"%s\"\n",wrdfmt);
  549. }
  550. cdef("MAXALLREG",maxallreg);
  551. cdef("SETSIZE",setsize);
  552. cdef("NREGS",nregs);
  553. cdef("REGSETSIZE",SZOFSET(nregs));
  554. cdef("TOKENSIZE",maxtokensize);
  555. cdef("MAXMEMBERS",maxmembers);
  556. cdef("LONGESTPATTERN",maxempatlen);
  557. cdef("MAXPATLEN",maxtokpatlen);
  558. cdef("MAXREPLLEN",maxtokrepllen);
  559. cdef("MAXEMREPLLEN",maxemrepllen);
  560. cdef("MAXPROCARG",maxprocargs);
  561. cdef("MAXRULE",maxrule<16 ? 16 : maxrule);
  562. if (nsplit>0) {
  563. cdef("MAXSPLIT",maxsplit);
  564. }
  565. if (tabledebug)
  566. cdef("TABLEDEBUG",1);
  567. if (use_tes)
  568. cdef("USE_TES",1);
  569. }
  570. void outars()
  571. {
  572. int i;
  573. if (code_in_c)
  574. fprintf(htable,"#define CODEINC 1\n");
  575. else {
  576. fprintf(ctable,"char coderules[%d];\n",codeindex);
  577. fprintf(ctable,"int ncodebytes=%d;\n",codeindex);
  578. }
  579. fprintf(ctable,"char pattern[%d]={\n",npatbytes+1);
  580. for(i=0;i<=npatbytes;i++) {
  581. fprintf(ctable,"%d,%c",pattern[i]&BMASK,i%16==15 ? '\n' : ' ');
  582. }
  583. fprintf(ctable,"};\n\n");
  584. fprintf(ctable,"int pathash[256]={\n");
  585. for(i=0;i<256;i++) {
  586. fprintf(ctable,"%d,%c",pathash[i]&0xFFFF,i%10==9 ? '\n' : ' ');
  587. }
  588. fprintf(ctable,"};\n");
  589. }
  590. void finishio()
  591. {
  592. int nregs;
  593. finishcode();
  594. hashpatterns();
  595. setsize = SZOFSET(nregs+ntokens);
  596. outdefs();
  597. outincludes();
  598. outregs();
  599. outtokens();
  600. outenodes();
  601. outstrings();
  602. outsets();
  603. outinstances();
  604. outmoves();
  605. outtests();
  606. outstacks();
  607. if (nsplit>0)
  608. outsplits();
  609. outcoercs();
  610. outproplists();
  611. outconsts();
  612. if (rvused)
  613. outregvars();
  614. outars();
  615. }
  616. void codecoco(cocono)
  617. {
  618. if (cocono== -1)
  619. return;
  620. code8(DO_SETCC);
  621. codeint(cocono);
  622. codenl();
  623. }
  624. void dopattern(int stackcoerc, varinfo *kills, varinfo *allocates, varinfo *generates, varinfo *yields, varinfo *leaving)
  625. {
  626. int i;
  627. int n,nops;
  628. struct varinfo *vp,*vivp;
  629. instr_p instp;
  630. int al,deal;
  631. int vil;
  632. int cocono= -1;
  633. cost_t totcost;
  634. int nremoves;
  635. int removelist[100];
  636. static char tlab[] = "0:";
  637. extern int optexact,optstack,startline;
  638. extern char *filename;
  639. extern int lineno;
  640. #ifdef CODEDEBUG
  641. fprintf(code,"Code(%d) at \"%s\", line %d\n",stackcoerc,filename,lineno);
  642. #endif
  643. if (code_in_c)
  644. fprintf(ctable,"\n/* \"%s\", line %d */ ",filename,lineno);
  645. if (tabledebug) {
  646. code8(DO_DLINE);
  647. codeint(startline);
  648. codenl();
  649. if (startline<MAXSOURCELINES) {
  650. if (startline>maxline)
  651. maxline=startline;
  652. BIS(lineset,startline);
  653. } else {
  654. static int beenhere=0;
  655. if (!beenhere) {
  656. beenhere++;
  657. error("Too many source lines for table debug");
  658. }
  659. }
  660. }
  661. /* MATCH part */
  662. if (tokpatlen) {
  663. if (optexact)
  664. if (optstack)
  665. code53(DO_XXMATCH,tokpatlen);
  666. else
  667. code53(DO_XMATCH,tokpatlen);
  668. else
  669. code53(DO_MATCH,tokpatlen);
  670. for (i=0;i<tokpatlen;i++)
  671. codeint(tokpatset[i]);
  672. codenl();
  673. } else if (stackcoerc)
  674. code8nl(DO_COERC);
  675. if (optstack) {
  676. code53(DO_TOSTACK,0);
  677. codeint(allsetno);
  678. codenl();
  679. }
  680. /* The kills */
  681. for (vp=kills;vp!=0;vp=vp->vi_next) {
  682. if (vp->vi_int[1] != 0) {
  683. code53(DO_REMOVE,1);
  684. codeint(vp->vi_int[0]);
  685. codeint(vp->vi_int[1]);
  686. } else if (vp->vi_int[0] >= 0) {
  687. code53(DO_REMOVE,0);
  688. codeint(vp->vi_int[0]);
  689. } else {
  690. code8(DO_KILLREG);
  691. codeint(-vp->vi_int[0] - 1);
  692. }
  693. codenl();
  694. }
  695. nremoves=0;
  696. for(vp=generates;vp!=0;vp=vp->vi_next) {
  697. if (vp->vi_int[0] != INSREMOVE)
  698. continue;
  699. for(i=0;i<nremoves;i++)
  700. if (vp->vi_int[1]==removelist[i])
  701. break;
  702. if (i==nremoves) {
  703. assert(nremoves<(sizeof(removelist)/sizeof(int)));
  704. removelist[nremoves++] = vp->vi_int[1];
  705. }
  706. }
  707. for(i=0;i<nremoves;i++) {
  708. code8(DO_RREMOVE);
  709. codeint(removelist[i]);
  710. codenl();
  711. }
  712. /* allocate part */
  713. deal=0;al=0;
  714. for (vp=allocates;vp!=0;vp=vp->vi_next) {
  715. if (vp->vi_int[0] == -1) { /* Deallocate */
  716. deal++;
  717. code8(DO_DEALLOCATE);
  718. codeint(vp->vi_int[1]);
  719. codenl();
  720. } else {
  721. if (vp->vi_int[1]==0) {
  722. code53(DO_ALLOCATE,0);
  723. codeint(vp->vi_int[0]);
  724. codenl();
  725. } else {
  726. code53(DO_ALLOCATE,1);
  727. codeint(vp->vi_int[0]);
  728. codeint(vp->vi_int[1]);
  729. codenl();
  730. }
  731. al++;
  732. }
  733. }
  734. if (deal)
  735. code8nl(DO_REALLOCATE);
  736. if (al>maxallreg)
  737. maxallreg=al;
  738. totcost.ct_space = 0;
  739. totcost.ct_time = 0;
  740. for(vp=generates;vp!=0;vp=vp->vi_next) {
  741. n= vp->vi_int[0];
  742. switch(n) {
  743. default:
  744. assert(n>=0);
  745. instp = &l_instr[n];
  746. nops=instp->i_nops;
  747. code53(DO_INSTR,nops);
  748. if (vp->vi_int[1]==0) {
  749. codeint(instp->i_asname);
  750. } else {
  751. codeint(10000+vp->vi_int[1]);
  752. }
  753. vivp=vp->vi_vi;
  754. for(i=0;i<nops;i++) {
  755. codeint(vivp->vi_int[0]);
  756. vivp = vivp->vi_vi;
  757. }
  758. codenl();
  759. totcost.ct_space += instp->i_cost.ct_space;
  760. totcost.ct_time += instp->i_cost.ct_time ;
  761. break;
  762. case INSREMOVE:
  763. break;
  764. case INSMOVE:
  765. codecoco(cocono);
  766. code8(DO_MOVE);
  767. codeint(vp->vi_int[1]);
  768. codeint(vp->vi_int[2]);
  769. codenl();
  770. break;
  771. case INSTEST:
  772. codecoco(cocono);
  773. code8(DO_TEST);
  774. codeint(vp->vi_int[1]);
  775. codenl();
  776. break;
  777. case INSPRETURN:
  778. code8(DO_PRETURN);
  779. codenl();
  780. break;
  781. case INSTLAB:
  782. cocono = 0;
  783. tlab[0] = vp->vi_int[1] + '0';
  784. code53(DO_INSTR,0);
  785. codeint(strlookup(tlab));
  786. codenl();
  787. break;
  788. case INSSETCC:
  789. cocono=vp->vi_int[1];
  790. break;
  791. case INSERASE:
  792. code8(DO_ERASE);
  793. codeint(vp->vi_int[1]);
  794. codenl();
  795. break;
  796. case INSLABDEF:
  797. cocono = 0;
  798. code8(DO_LABDEF);
  799. codeint(vp->vi_int[1]);
  800. codenl();
  801. break;
  802. }
  803. }
  804. codecoco(cocono);
  805. vil = vilength(yields);
  806. if (vil!=0 || tokpatlen!=0 || allocates!=0) {
  807. code53(DO_TOKREPLACE,vilength(yields));
  808. for(vp=yields;vp!=0;vp=vp->vi_next) {
  809. codeint(vp->vi_int[0]);
  810. }
  811. codenl();
  812. }
  813. if (leaving!=0) {
  814. code53(DO_EMREPLACE,vilength(leaving));
  815. while (leaving!=0) {
  816. codeint(leaving->vi_int[0]);
  817. codeint(leaving->vi_int[1]);
  818. leaving = leaving->vi_next;
  819. }
  820. codenl();
  821. }
  822. if (totcost.ct_space!=0 || totcost.ct_time!=0) {
  823. code8(DO_COST);
  824. codeint(totcost.ct_space);
  825. codeint(totcost.ct_time);
  826. codenl();
  827. }
  828. if (empatlen==0 && !inproc)
  829. code8nl(DO_RETURN);
  830. else
  831. code8nl(DO_NEXTEM);
  832. }
  833. void used(char *resource, int use, int max)
  834. {
  835. if (verbose || 4*use > 3*max)
  836. fprintf(stderr,"%s %d(%d)\n",resource,use,max);
  837. }
  838. void statistics()
  839. {
  840. extern char *beg_sbrk,*sbrk();
  841. extern int nnodes, maxempatlen,maxrule;
  842. used("Registers",nregs,MAXREGS);
  843. used("Properties",nprops,MAXPROPS);
  844. used("Tokens",ntokens,MAXTOKENS);
  845. used("Tokensize",maxtokensize,MAXATT);
  846. used("Sets",nsets,MAXSETS);
  847. used("Instructions",ninstr,MAXINSTR);
  848. used("Strings",nstrings,MAXSTRINGS);
  849. used("Exp-nodes",nnodes,MAXNODES);
  850. used("EM-pat length",maxempatlen,EMPATMAX);
  851. used("rules/EM-pattern",maxrule,MAXPATTERNS);
  852. used("Allocates/rule",maxallreg,MAXALLREG);
  853. used("Instances",ninstances,MAXINSTANCES);
  854. used("Moves",nmoves,MAXMOVES);
  855. used("Tests",ntests,MAXTESTS);
  856. used("Stacks",nstacks,MAXSTACKS);
  857. used("1->1 Coercions",ncoercs,MAXCOERCS);
  858. used("Splitting coercions",nsplit,MAXSPLCOERC);
  859. used("Register variables",maxregvars,MAXREGVAR);
  860. used("Pat bytes",npatbytes+1,MAXPATBYTES);
  861. if (tabledebug)
  862. used("Source lines",maxline,MAXSOURCELINES);
  863. fprintf(stderr,"%ldK heap used\n",((long) (sbrk(0)-beg_sbrk+1023))/1024);
  864. }