output.c 20 KB

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