output.c 20 KB

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