tl_trans.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /***** tl_spin: tl_trans.c *****/
  2. /* Copyright (c) 1995-2003 by Lucent Technologies, Bell Laboratories. */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* No guarantee whatsoever is expressed or implied by the distribution of */
  5. /* this code. Permission is given to distribute this code provided that */
  6. /* this introductory message is not removed and no monies are exchanged. */
  7. /* Software written by Gerard J. Holzmann. For tool documentation see: */
  8. /* http://spinroot.com/ */
  9. /* Send all bug-reports and/or questions to: bugs@spinroot.com */
  10. /* Based on the translation algorithm by Gerth, Peled, Vardi, and Wolper, */
  11. /* presented at the PSTV Conference, held in 1995, Warsaw, Poland 1995. */
  12. #include "tl.h"
  13. extern FILE *tl_out;
  14. extern int tl_errs, tl_verbose, tl_terse, newstates;
  15. int Stack_mx=0, Max_Red=0, Total=0;
  16. static Mapping *Mapped = (Mapping *) 0;
  17. static Graph *Nodes_Set = (Graph *) 0;
  18. static Graph *Nodes_Stack = (Graph *) 0;
  19. static char dumpbuf[2048];
  20. static int Red_cnt = 0;
  21. static int Lab_cnt = 0;
  22. static int Base = 0;
  23. static int Stack_sz = 0;
  24. static Graph *findgraph(char *);
  25. static Graph *pop_stack(void);
  26. static Node *Duplicate(Node *);
  27. static Node *flatten(Node *);
  28. static Symbol *catSlist(Symbol *, Symbol *);
  29. static Symbol *dupSlist(Symbol *);
  30. static char *newname(void);
  31. static int choueka(Graph *, int);
  32. static int not_new(Graph *);
  33. static int set_prefix(char *, int, Graph *);
  34. static void Addout(char *, char *);
  35. static void fsm_trans(Graph *, int, char *);
  36. static void mkbuchi(void);
  37. static void expand_g(Graph *);
  38. static void fixinit(Node *);
  39. static void liveness(Node *);
  40. static void mk_grn(Node *);
  41. static void mk_red(Node *);
  42. static void ng(Symbol *, Symbol *, Node *, Node *, Node *);
  43. static void push_stack(Graph *);
  44. static void sdump(Node *);
  45. static void
  46. dump_graph(Graph *g)
  47. { Node *n1;
  48. printf("\n\tnew:\t");
  49. for (n1 = g->New; n1; n1 = n1->nxt)
  50. { dump(n1); printf(", "); }
  51. printf("\n\told:\t");
  52. for (n1 = g->Old; n1; n1 = n1->nxt)
  53. { dump(n1); printf(", "); }
  54. printf("\n\tnxt:\t");
  55. for (n1 = g->Next; n1; n1 = n1->nxt)
  56. { dump(n1); printf(", "); }
  57. printf("\n\tother:\t");
  58. for (n1 = g->Other; n1; n1 = n1->nxt)
  59. { dump(n1); printf(", "); }
  60. printf("\n");
  61. }
  62. static void
  63. push_stack(Graph *g)
  64. {
  65. if (!g) return;
  66. g->nxt = Nodes_Stack;
  67. Nodes_Stack = g;
  68. if (tl_verbose)
  69. { Symbol *z;
  70. printf("\nPush %s, from ", g->name->name);
  71. for (z = g->incoming; z; z = z->next)
  72. printf("%s, ", z->name);
  73. dump_graph(g);
  74. }
  75. Stack_sz++;
  76. if (Stack_sz > Stack_mx) Stack_mx = Stack_sz;
  77. }
  78. static Graph *
  79. pop_stack(void)
  80. { Graph *g = Nodes_Stack;
  81. if (g) Nodes_Stack = g->nxt;
  82. Stack_sz--;
  83. return g;
  84. }
  85. static char *
  86. newname(void)
  87. { static int cnt = 0;
  88. static char buf[32];
  89. sprintf(buf, "S%d", cnt++);
  90. return buf;
  91. }
  92. static int
  93. has_clause(int tok, Graph *p, Node *n)
  94. { Node *q, *qq;
  95. switch (n->ntyp) {
  96. case AND:
  97. return has_clause(tok, p, n->lft) &&
  98. has_clause(tok, p, n->rgt);
  99. case OR:
  100. return has_clause(tok, p, n->lft) ||
  101. has_clause(tok, p, n->rgt);
  102. }
  103. for (q = p->Other; q; q = q->nxt)
  104. { qq = right_linked(q);
  105. if (anywhere(tok, n, qq))
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. static void
  111. mk_grn(Node *n)
  112. { Graph *p;
  113. n = right_linked(n);
  114. more:
  115. for (p = Nodes_Set; p; p = p->nxt)
  116. if (p->outgoing
  117. && has_clause(AND, p, n))
  118. { p->isgrn[p->grncnt++] =
  119. (unsigned char) Red_cnt;
  120. Lab_cnt++;
  121. }
  122. if (n->ntyp == U_OPER) /* 3.4.0 */
  123. { n = n->rgt;
  124. goto more;
  125. }
  126. }
  127. static void
  128. mk_red(Node *n)
  129. { Graph *p;
  130. n = right_linked(n);
  131. for (p = Nodes_Set; p; p = p->nxt)
  132. { if (p->outgoing
  133. && has_clause(0, p, n))
  134. { if (p->redcnt >= 63)
  135. Fatal("too many Untils", (char *)0);
  136. p->isred[p->redcnt++] =
  137. (unsigned char) Red_cnt;
  138. Lab_cnt++; Max_Red = Red_cnt;
  139. } }
  140. }
  141. static void
  142. liveness(Node *n)
  143. {
  144. if (n)
  145. switch (n->ntyp) {
  146. #ifdef NXT
  147. case NEXT:
  148. liveness(n->lft);
  149. break;
  150. #endif
  151. case U_OPER:
  152. Red_cnt++;
  153. mk_red(n);
  154. mk_grn(n->rgt);
  155. /* fall through */
  156. case V_OPER:
  157. case OR: case AND:
  158. liveness(n->lft);
  159. liveness(n->rgt);
  160. break;
  161. }
  162. }
  163. static Graph *
  164. findgraph(char *nm)
  165. { Graph *p;
  166. Mapping *m;
  167. for (p = Nodes_Set; p; p = p->nxt)
  168. if (!strcmp(p->name->name, nm))
  169. return p;
  170. for (m = Mapped; m; m = m->nxt)
  171. if (strcmp(m->from, nm) == 0)
  172. return m->to;
  173. printf("warning: node %s not found\n", nm);
  174. return (Graph *) 0;
  175. }
  176. static void
  177. Addout(char *to, char *from)
  178. { Graph *p = findgraph(from);
  179. Symbol *s;
  180. if (!p) return;
  181. s = getsym(tl_lookup(to));
  182. s->next = p->outgoing;
  183. p->outgoing = s;
  184. }
  185. #ifdef NXT
  186. int
  187. only_nxt(Node *n)
  188. {
  189. switch (n->ntyp) {
  190. case NEXT:
  191. return 1;
  192. case OR:
  193. case AND:
  194. return only_nxt(n->rgt) && only_nxt(n->lft);
  195. default:
  196. return 0;
  197. }
  198. }
  199. #endif
  200. int
  201. dump_cond(Node *pp, Node *r, int first)
  202. { Node *q;
  203. int frst = first;
  204. if (!pp) return frst;
  205. q = dupnode(pp);
  206. q = rewrite(q);
  207. if (q->ntyp == PREDICATE
  208. || q->ntyp == NOT
  209. #ifndef NXT
  210. || q->ntyp == OR
  211. #endif
  212. || q->ntyp == FALSE)
  213. { if (!frst) fprintf(tl_out, " && ");
  214. dump(q);
  215. frst = 0;
  216. #ifdef NXT
  217. } else if (q->ntyp == OR)
  218. { if (!frst) fprintf(tl_out, " && ");
  219. fprintf(tl_out, "((");
  220. frst = dump_cond(q->lft, r, 1);
  221. if (!frst)
  222. fprintf(tl_out, ") || (");
  223. else
  224. { if (only_nxt(q->lft))
  225. { fprintf(tl_out, "1))");
  226. return 0;
  227. }
  228. }
  229. frst = dump_cond(q->rgt, r, 1);
  230. if (frst)
  231. { if (only_nxt(q->rgt))
  232. fprintf(tl_out, "1");
  233. else
  234. fprintf(tl_out, "0");
  235. frst = 0;
  236. }
  237. fprintf(tl_out, "))");
  238. #endif
  239. } else if (q->ntyp == V_OPER
  240. && !anywhere(AND, q->rgt, r))
  241. { frst = dump_cond(q->rgt, r, frst);
  242. } else if (q->ntyp == AND)
  243. {
  244. frst = dump_cond(q->lft, r, frst);
  245. frst = dump_cond(q->rgt, r, frst);
  246. }
  247. return frst;
  248. }
  249. static int
  250. choueka(Graph *p, int count)
  251. { int j, k, incr_cnt = 0;
  252. for (j = count; j <= Max_Red; j++) /* for each acceptance class */
  253. { int delta = 0;
  254. /* is state p labeled Grn-j OR not Red-j ? */
  255. for (k = 0; k < (int) p->grncnt; k++)
  256. if (p->isgrn[k] == j)
  257. { delta = 1;
  258. break;
  259. }
  260. if (delta)
  261. { incr_cnt++;
  262. continue;
  263. }
  264. for (k = 0; k < (int) p->redcnt; k++)
  265. if (p->isred[k] == j)
  266. { delta = 1;
  267. break;
  268. }
  269. if (delta) break;
  270. incr_cnt++;
  271. }
  272. return incr_cnt;
  273. }
  274. static int
  275. set_prefix(char *pref, int count, Graph *r2)
  276. { int incr_cnt = 0; /* acceptance class 'count' */
  277. if (Lab_cnt == 0
  278. || Max_Red == 0)
  279. sprintf(pref, "accept"); /* new */
  280. else if (count >= Max_Red)
  281. sprintf(pref, "T0"); /* cycle */
  282. else
  283. { incr_cnt = choueka(r2, count+1);
  284. if (incr_cnt + count >= Max_Red)
  285. sprintf(pref, "accept"); /* last hop */
  286. else
  287. sprintf(pref, "T%d", count+incr_cnt);
  288. }
  289. return incr_cnt;
  290. }
  291. static void
  292. fsm_trans(Graph *p, int count, char *curnm)
  293. { Graph *r;
  294. Symbol *s;
  295. char prefix[128], nwnm[128];
  296. if (!p->outgoing)
  297. addtrans(p, curnm, False, "accept_all");
  298. for (s = p->outgoing; s; s = s->next)
  299. { r = findgraph(s->name);
  300. if (!r) continue;
  301. if (r->outgoing)
  302. { (void) set_prefix(prefix, count, r);
  303. sprintf(nwnm, "%s_%s", prefix, s->name);
  304. } else
  305. strcpy(nwnm, "accept_all");
  306. if (tl_verbose)
  307. { printf("maxred=%d, count=%d, curnm=%s, nwnm=%s ",
  308. Max_Red, count, curnm, nwnm);
  309. printf("(greencnt=%d,%d, redcnt=%d,%d)\n",
  310. r->grncnt, r->isgrn[0],
  311. r->redcnt, r->isred[0]);
  312. }
  313. addtrans(p, curnm, r->Old, nwnm);
  314. }
  315. }
  316. static void
  317. mkbuchi(void)
  318. { Graph *p;
  319. int k;
  320. char curnm[64];
  321. for (k = 0; k <= Max_Red; k++)
  322. for (p = Nodes_Set; p; p = p->nxt)
  323. { if (!p->outgoing)
  324. continue;
  325. if (k != 0
  326. && !strcmp(p->name->name, "init")
  327. && Max_Red != 0)
  328. continue;
  329. if (k == Max_Red
  330. && strcmp(p->name->name, "init") != 0)
  331. strcpy(curnm, "accept_");
  332. else
  333. sprintf(curnm, "T%d_", k);
  334. strcat(curnm, p->name->name);
  335. fsm_trans(p, k, curnm);
  336. }
  337. fsm_print();
  338. }
  339. static Symbol *
  340. dupSlist(Symbol *s)
  341. { Symbol *p1, *p2, *p3, *d = ZS;
  342. for (p1 = s; p1; p1 = p1->next)
  343. { for (p3 = d; p3; p3 = p3->next)
  344. { if (!strcmp(p3->name, p1->name))
  345. break;
  346. }
  347. if (p3) continue; /* a duplicate */
  348. p2 = getsym(p1);
  349. p2->next = d;
  350. d = p2;
  351. }
  352. return d;
  353. }
  354. static Symbol *
  355. catSlist(Symbol *a, Symbol *b)
  356. { Symbol *p1, *p2, *p3, *tmp;
  357. /* remove duplicates from b */
  358. for (p1 = a; p1; p1 = p1->next)
  359. { p3 = ZS;
  360. for (p2 = b; p2; p2 = p2->next)
  361. { if (strcmp(p1->name, p2->name))
  362. { p3 = p2;
  363. continue;
  364. }
  365. tmp = p2->next;
  366. tfree((void *) p2);
  367. if (p3)
  368. p3->next = tmp;
  369. else
  370. b = tmp;
  371. } }
  372. if (!a) return b;
  373. if (!b) return a;
  374. if (!b->next)
  375. { b->next = a;
  376. return b;
  377. }
  378. /* find end of list */
  379. for (p1 = a; p1->next; p1 = p1->next)
  380. ;
  381. p1->next = b;
  382. return a;
  383. }
  384. static void
  385. fixinit(Node *orig)
  386. { Graph *p1, *g;
  387. Symbol *q1, *q2 = ZS;
  388. ng(tl_lookup("init"), ZS, ZN, ZN, ZN);
  389. p1 = pop_stack();
  390. p1->nxt = Nodes_Set;
  391. p1->Other = p1->Old = orig;
  392. Nodes_Set = p1;
  393. for (g = Nodes_Set; g; g = g->nxt)
  394. { for (q1 = g->incoming; q1; q1 = q2)
  395. { q2 = q1->next;
  396. Addout(g->name->name, q1->name);
  397. tfree((void *) q1);
  398. }
  399. g->incoming = ZS;
  400. }
  401. }
  402. static Node *
  403. flatten(Node *p)
  404. { Node *q, *r, *z = ZN;
  405. for (q = p; q; q = q->nxt)
  406. { r = dupnode(q);
  407. if (z)
  408. z = tl_nn(AND, r, z);
  409. else
  410. z = r;
  411. }
  412. if (!z) return z;
  413. z = rewrite(z);
  414. return z;
  415. }
  416. static Node *
  417. Duplicate(Node *n)
  418. { Node *n1, *n2, *lst = ZN, *d = ZN;
  419. for (n1 = n; n1; n1 = n1->nxt)
  420. { n2 = dupnode(n1);
  421. if (lst)
  422. { lst->nxt = n2;
  423. lst = n2;
  424. } else
  425. d = lst = n2;
  426. }
  427. return d;
  428. }
  429. static void
  430. ng(Symbol *s, Symbol *in, Node *isnew, Node *isold, Node *next)
  431. { Graph *g = (Graph *) tl_emalloc(sizeof(Graph));
  432. if (s) g->name = s;
  433. else g->name = tl_lookup(newname());
  434. if (in) g->incoming = dupSlist(in);
  435. if (isnew) g->New = flatten(isnew);
  436. if (isold) g->Old = Duplicate(isold);
  437. if (next) g->Next = flatten(next);
  438. push_stack(g);
  439. }
  440. static void
  441. sdump(Node *n)
  442. {
  443. switch (n->ntyp) {
  444. case PREDICATE: strcat(dumpbuf, n->sym->name);
  445. break;
  446. case U_OPER: strcat(dumpbuf, "U");
  447. goto common2;
  448. case V_OPER: strcat(dumpbuf, "V");
  449. goto common2;
  450. case OR: strcat(dumpbuf, "|");
  451. goto common2;
  452. case AND: strcat(dumpbuf, "&");
  453. common2: sdump(n->rgt);
  454. common1: sdump(n->lft);
  455. break;
  456. #ifdef NXT
  457. case NEXT: strcat(dumpbuf, "X");
  458. goto common1;
  459. #endif
  460. case NOT: strcat(dumpbuf, "!");
  461. goto common1;
  462. case TRUE: strcat(dumpbuf, "T");
  463. break;
  464. case FALSE: strcat(dumpbuf, "F");
  465. break;
  466. default: strcat(dumpbuf, "?");
  467. break;
  468. }
  469. }
  470. Symbol *
  471. DoDump(Node *n)
  472. {
  473. if (!n) return ZS;
  474. if (n->ntyp == PREDICATE)
  475. return n->sym;
  476. dumpbuf[0] = '\0';
  477. sdump(n);
  478. return tl_lookup(dumpbuf);
  479. }
  480. static int
  481. not_new(Graph *g)
  482. { Graph *q1; Node *tmp, *n1, *n2;
  483. Mapping *map;
  484. tmp = flatten(g->Old); /* duplicate, collapse, normalize */
  485. g->Other = g->Old; /* non normalized full version */
  486. g->Old = tmp;
  487. g->oldstring = DoDump(g->Old);
  488. tmp = flatten(g->Next);
  489. g->nxtstring = DoDump(tmp);
  490. if (tl_verbose) dump_graph(g);
  491. Debug2("\tformula-old: [%s]\n", g->oldstring?g->oldstring->name:"true");
  492. Debug2("\tformula-nxt: [%s]\n", g->nxtstring?g->nxtstring->name:"true");
  493. for (q1 = Nodes_Set; q1; q1 = q1->nxt)
  494. { Debug2(" compare old to: %s", q1->name->name);
  495. Debug2(" [%s]", q1->oldstring?q1->oldstring->name:"true");
  496. Debug2(" compare nxt to: %s", q1->name->name);
  497. Debug2(" [%s]", q1->nxtstring?q1->nxtstring->name:"true");
  498. if (q1->oldstring != g->oldstring
  499. || q1->nxtstring != g->nxtstring)
  500. { Debug(" => different\n");
  501. continue;
  502. }
  503. Debug(" => match\n");
  504. if (g->incoming)
  505. q1->incoming = catSlist(g->incoming, q1->incoming);
  506. /* check if there's anything in g->Other that needs
  507. adding to q1->Other
  508. */
  509. for (n2 = g->Other; n2; n2 = n2->nxt)
  510. { for (n1 = q1->Other; n1; n1 = n1->nxt)
  511. if (isequal(n1, n2))
  512. break;
  513. if (!n1)
  514. { Node *n3 = dupnode(n2);
  515. /* don't mess up n2->nxt */
  516. n3->nxt = q1->Other;
  517. q1->Other = n3;
  518. } }
  519. map = (Mapping *) tl_emalloc(sizeof(Mapping));
  520. map->from = g->name->name;
  521. map->to = q1;
  522. map->nxt = Mapped;
  523. Mapped = map;
  524. for (n1 = g->Other; n1; n1 = n2)
  525. { n2 = n1->nxt;
  526. releasenode(1, n1);
  527. }
  528. for (n1 = g->Old; n1; n1 = n2)
  529. { n2 = n1->nxt;
  530. releasenode(1, n1);
  531. }
  532. for (n1 = g->Next; n1; n1 = n2)
  533. { n2 = n1->nxt;
  534. releasenode(1, n1);
  535. }
  536. return 1;
  537. }
  538. if (newstates) tl_verbose=1;
  539. Debug2(" New Node %s [", g->name->name);
  540. for (n1 = g->Old; n1; n1 = n1->nxt)
  541. { Dump(n1); Debug(", "); }
  542. Debug2("] nr %d\n", Base);
  543. if (newstates) tl_verbose=0;
  544. Base++;
  545. g->nxt = Nodes_Set;
  546. Nodes_Set = g;
  547. return 0;
  548. }
  549. static void
  550. expand_g(Graph *g)
  551. { Node *now, *n1, *n2, *nx;
  552. int can_release;
  553. if (!g->New)
  554. { Debug2("\nDone with %s", g->name->name);
  555. if (tl_verbose) dump_graph(g);
  556. if (not_new(g))
  557. { if (tl_verbose) printf("\tIs Not New\n");
  558. return;
  559. }
  560. if (g->Next)
  561. { Debug(" Has Next [");
  562. for (n1 = g->Next; n1; n1 = n1->nxt)
  563. { Dump(n1); Debug(", "); }
  564. Debug("]\n");
  565. ng(ZS, getsym(g->name), g->Next, ZN, ZN);
  566. }
  567. return;
  568. }
  569. if (tl_verbose)
  570. { Symbol *z;
  571. printf("\nExpand %s, from ", g->name->name);
  572. for (z = g->incoming; z; z = z->next)
  573. printf("%s, ", z->name);
  574. printf("\n\thandle:\t"); Explain(g->New->ntyp);
  575. dump_graph(g);
  576. }
  577. if (g->New->ntyp == AND)
  578. { if (g->New->nxt)
  579. { n2 = g->New->rgt;
  580. while (n2->nxt)
  581. n2 = n2->nxt;
  582. n2->nxt = g->New->nxt;
  583. }
  584. n1 = n2 = g->New->lft;
  585. while (n2->nxt)
  586. n2 = n2->nxt;
  587. n2->nxt = g->New->rgt;
  588. releasenode(0, g->New);
  589. g->New = n1;
  590. push_stack(g);
  591. return;
  592. }
  593. can_release = 0; /* unless it need not go into Old */
  594. now = g->New;
  595. g->New = g->New->nxt;
  596. now->nxt = ZN;
  597. if (now->ntyp != TRUE)
  598. { if (g->Old)
  599. { for (n1 = g->Old; n1->nxt; n1 = n1->nxt)
  600. if (isequal(now, n1))
  601. { can_release = 1;
  602. goto out;
  603. }
  604. n1->nxt = now;
  605. } else
  606. g->Old = now;
  607. }
  608. out:
  609. switch (now->ntyp) {
  610. case FALSE:
  611. push_stack(g);
  612. break;
  613. case TRUE:
  614. releasenode(1, now);
  615. push_stack(g);
  616. break;
  617. case PREDICATE:
  618. case NOT:
  619. if (can_release) releasenode(1, now);
  620. push_stack(g);
  621. break;
  622. case V_OPER:
  623. Assert(now->rgt->nxt == ZN, now->ntyp);
  624. Assert(now->lft->nxt == ZN, now->ntyp);
  625. n1 = now->rgt;
  626. n1->nxt = g->New;
  627. if (can_release)
  628. nx = now;
  629. else
  630. nx = getnode(now); /* now also appears in Old */
  631. nx->nxt = g->Next;
  632. n2 = now->lft;
  633. n2->nxt = getnode(now->rgt);
  634. n2->nxt->nxt = g->New;
  635. g->New = flatten(n2);
  636. push_stack(g);
  637. ng(ZS, g->incoming, n1, g->Old, nx);
  638. break;
  639. case U_OPER:
  640. Assert(now->rgt->nxt == ZN, now->ntyp);
  641. Assert(now->lft->nxt == ZN, now->ntyp);
  642. n1 = now->lft;
  643. if (can_release)
  644. nx = now;
  645. else
  646. nx = getnode(now); /* now also appears in Old */
  647. nx->nxt = g->Next;
  648. n2 = now->rgt;
  649. n2->nxt = g->New;
  650. goto common;
  651. #ifdef NXT
  652. case NEXT:
  653. nx = dupnode(now->lft);
  654. nx->nxt = g->Next;
  655. g->Next = nx;
  656. if (can_release) releasenode(0, now);
  657. push_stack(g);
  658. break;
  659. #endif
  660. case OR:
  661. Assert(now->rgt->nxt == ZN, now->ntyp);
  662. Assert(now->lft->nxt == ZN, now->ntyp);
  663. n1 = now->lft;
  664. nx = g->Next;
  665. n2 = now->rgt;
  666. n2->nxt = g->New;
  667. common:
  668. n1->nxt = g->New;
  669. ng(ZS, g->incoming, n1, g->Old, nx);
  670. g->New = flatten(n2);
  671. if (can_release) releasenode(1, now);
  672. push_stack(g);
  673. break;
  674. }
  675. }
  676. Node *
  677. twocases(Node *p)
  678. { Node *q;
  679. /* 1: ([]p1 && []p2) == [](p1 && p2) */
  680. /* 2: (<>p1 || <>p2) == <>(p1 || p2) */
  681. if (!p) return p;
  682. switch(p->ntyp) {
  683. case AND:
  684. case OR:
  685. case U_OPER:
  686. case V_OPER:
  687. p->lft = twocases(p->lft);
  688. p->rgt = twocases(p->rgt);
  689. break;
  690. #ifdef NXT
  691. case NEXT:
  692. #endif
  693. case NOT:
  694. p->lft = twocases(p->lft);
  695. break;
  696. default:
  697. break;
  698. }
  699. if (p->ntyp == AND /* 1 */
  700. && p->lft->ntyp == V_OPER
  701. && p->lft->lft->ntyp == FALSE
  702. && p->rgt->ntyp == V_OPER
  703. && p->rgt->lft->ntyp == FALSE)
  704. { q = tl_nn(V_OPER, False,
  705. tl_nn(AND, p->lft->rgt, p->rgt->rgt));
  706. } else
  707. if (p->ntyp == OR /* 2 */
  708. && p->lft->ntyp == U_OPER
  709. && p->lft->lft->ntyp == TRUE
  710. && p->rgt->ntyp == U_OPER
  711. && p->rgt->lft->ntyp == TRUE)
  712. { q = tl_nn(U_OPER, True,
  713. tl_nn(OR, p->lft->rgt, p->rgt->rgt));
  714. } else
  715. q = p;
  716. return q;
  717. }
  718. void
  719. trans(Node *p)
  720. { Node *op;
  721. Graph *g;
  722. if (!p || tl_errs) return;
  723. p = twocases(p);
  724. if (tl_verbose || tl_terse)
  725. { fprintf(tl_out, "\t/* Normlzd: ");
  726. dump(p);
  727. fprintf(tl_out, " */\n");
  728. }
  729. if (tl_terse)
  730. return;
  731. op = dupnode(p);
  732. ng(ZS, getsym(tl_lookup("init")), p, ZN, ZN);
  733. while ((g = Nodes_Stack) != (Graph *) 0)
  734. { Nodes_Stack = g->nxt;
  735. expand_g(g);
  736. }
  737. if (newstates)
  738. return;
  739. fixinit(p);
  740. liveness(flatten(op)); /* was: liveness(op); */
  741. mkbuchi();
  742. if (tl_verbose)
  743. { printf("/*\n");
  744. printf(" * %d states in Streett automaton\n", Base);
  745. printf(" * %d Streett acceptance conditions\n", Max_Red);
  746. printf(" * %d Buchi states\n", Total);
  747. printf(" */\n");
  748. }
  749. }