output.c 17 KB

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