output.c 20 KB

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