output.c 20 KB

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