pangen2.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /***** spin: pangen2.h *****/
  2. /* Copyright (c) 1991-2000 by Lucent Technologies - Bell Laboratories */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* Permission is given to distribute this code provided that this intro- */
  5. /* ductory message is not removed and no monies are exchanged. */
  6. /* No guarantee is expressed or implied by the distribution of this code. */
  7. /* Software written by Gerard J. Holzmann as part of the book: */
  8. /* `Design and Validation of Computer Protocols,' ISBN 0-13-539925-4, */
  9. /* Prentice Hall, Englewood Cliffs, NJ, 07632. */
  10. /* Send bug-reports and/or questions to: gerard@research.bell-labs.com */
  11. static char *Nvr1[] = { /* allow separate compilation */
  12. "#ifdef VERI",
  13. "void",
  14. "check_claim(int st)",
  15. "{",
  16. " if (st == endclaim)",
  17. " uerror(\"claim violated!\");",
  18. " if (stopstate[VERI][st])",
  19. " uerror(\"endstate in claim reached\");",
  20. "}",
  21. "#endif",
  22. 0,
  23. };
  24. static char *Pre0[] = {
  25. "#include <stdio.h>",
  26. "#include <signal.h>",
  27. "#include <stdlib.h>",
  28. "#include <string.h>", /* was strings.h -- string.h is ANSI */
  29. /* new */
  30. "#include <errno.h>",
  31. /* end */
  32. "#define Offsetof(X, Y) ((unsigned long)(&(((X *)0)->Y)))",
  33. "#ifndef max",
  34. "#define max(a,b) (((a)<(b)) ? (b) : (a))",
  35. "#endif",
  36. 0,
  37. };
  38. static char *Preamble[] = {
  39. "#ifdef CNTRSTACK",
  40. "#define onstack_now() (LL[trpt->j6] && LL[trpt->j7])",
  41. "#define onstack_put() LL[trpt->j6]++; LL[trpt->j7]++",
  42. "#define onstack_zap() LL[trpt->j6]--; LL[trpt->j7]--",
  43. "#endif",
  44. "#if !defined(SAFETY) && !defined(NOCOMP)",
  45. /*
  46. * V_A identifies states in the current statespace
  47. * A_V identifies states in the 'other' statespace
  48. * S_A remembers how many leading bytes in the sv
  49. * are used for these markers + fairness bits
  50. */
  51. "#define V_A (((now._a_t&1)?2:1) << (now._a_t&2))",
  52. "#define A_V (((now._a_t&1)?1:2) << (now._a_t&2))",
  53. "int S_A = 0;",
  54. "#else",
  55. "#define V_A 0",
  56. "#define A_V 0",
  57. "#define S_A 0",
  58. "#endif",
  59. "#ifdef MA",
  60. "#define onstack_put() ;",
  61. "#define onstack_zap() gstore((char *) &now, vsize, 4)",
  62. "#else",
  63. "#if defined(FULLSTACK) && !defined(BITSTATE)",
  64. "#define onstack_put() trpt->ostate = Lstate",
  65. "#define onstack_zap() { \\",
  66. " if (trpt->ostate) \\",
  67. " trpt->ostate->tagged = \\",
  68. " (S_A)? (trpt->ostate->tagged&~V_A) : 0; \\",
  69. " }",
  70. "#endif",
  71. "#endif",
  72. "struct H_el {",
  73. " struct H_el *nxt;",
  74. "#ifdef FULLSTACK",
  75. " unsigned tagged;",
  76. "#if defined(BITSTATE) && !defined(NOREDUCE) && !defined(SAFETY)",
  77. " unsigned proviso;", /* uses just 1 bit 0/1 */
  78. "#endif",
  79. "#endif",
  80. "#if defined(CHECK) || (defined(COLLAPSE) && !defined(FULLSTACK))",
  81. " unsigned long st_id;",
  82. "#endif",
  83. "#ifdef COLLAPSE",
  84. "#if VECTORSZ<65536",
  85. " unsigned short ln;", /* length of vector */
  86. "#else",
  87. " unsigned long ln;", /* length of vector */
  88. "#endif",
  89. "#endif",
  90. "#ifdef REACH",
  91. " unsigned D;",
  92. "#endif",
  93. " unsigned state;",
  94. "} **H_tab, **S_Tab;\n",
  95. "typedef struct Trail {",
  96. " short st; /* current state */",
  97. " uchar pr; /* process id */",
  98. " uchar tau; /* 8 bit-flags */",
  99. " uchar o_pm; /* 8 more bit-flags */",
  100. "#if 0",
  101. " Meaning of bit-flags:",
  102. " tau&1 -> timeout enabled",
  103. " tau&2 -> request to enable timeout 1 level up (in claim)",
  104. " tau&4 -> current transition is a claim move",
  105. " tau&8 -> current transition is an atomic move",
  106. " tau&16 -> last move was truncated on stack",
  107. " tau&32 -> current transition is a preselected move",
  108. " tau&64 -> at least one next state is not on the stack",
  109. " tau&128 -> current transition is a stutter move",
  110. " o_pm&1 -> the current pid moved -- implements else",
  111. " o_pm&2 -> this is an acceptance state",
  112. " o_pm&4 -> this is a progress state",
  113. " o_pm&8 -> fairness alg rule 1 undo mark",
  114. " o_pm&16 -> fairness alg rule 3 undo mark",
  115. " o_pm&32 -> fairness alg rule 2 undo mark",
  116. " o_pm&64 -> the current proc applied rule2",
  117. " o_pm&128 -> a fairness, dummy move - all procs blocked",
  118. "#endif",
  119. "#if defined(FULLSTACK) && defined(MA)",
  120. " uchar proviso;",
  121. "#endif",
  122. " char o_n, o_ot, o_m; /* to save locals */",
  123. "#ifdef EVENT_TRACE",
  124. "#if nstates_event<256",
  125. " uchar o_event;",
  126. "#else",
  127. " unsigned short o_event;",
  128. "#endif",
  129. "#endif",
  130. " short o_tt, o_To;", /* used in new_state() */
  131. "#ifdef HAS_UNLESS",
  132. " short e_state; /* if escape trans - state of origin */",
  133. "#endif",
  134. "#if defined(FULLSTACK) && !defined(MA)",
  135. " struct H_el *ostate; /* pointer to stored state */",
  136. "#endif",
  137. "#ifdef CNTRSTACK",
  138. " long j6, j7;",
  139. "#endif",
  140. " Trans *o_t;", /* transition fct, next state */
  141. " union {",
  142. " int oval;", /* single backup value of variable */
  143. " int *ovals;", /* ptr to multiple values */
  144. " } bup;",
  145. "} Trail;",
  146. "Trail *trail, *trpt;",
  147. "FILE *efd;",
  148. "uchar *this;",
  149. "long maxdepth=10000;",
  150. "#ifdef SC", /* stack cycling */
  151. "long omaxdepth;",
  152. "char *stackfile;",
  153. "#endif",
  154. "uchar *SS, *LL;",
  155. "uchar HASH_NR = 0;",
  156. "",
  157. "#ifdef MEMCNT",
  158. "double memcnt = (double) 0;",
  159. "double overhead = (double) 0;",
  160. "double memlim = (double) (1<<30);",
  161. "#endif",
  162. "/* for emalloc: */",
  163. "static char *have;",
  164. "static long left = 0L;",
  165. "static double fragment = (double) 0;",
  166. "static unsigned long grow;",
  167. "",
  168. "unsigned int HASH_CONST[] = {",
  169. " /* asuming 4 bytes per int */",
  170. " 0x88888EEF, 0x00400007,",
  171. " 0x04c11db7, 0x100d4e63,",
  172. " 0x0fc22f87, 0x3ff0c3ff,",
  173. " 0x38e84cd7, 0x02b148e9,",
  174. " 0x98b2e49d, 0xb616d379,",
  175. " 0xa5247fd9, 0xbae92a15,",
  176. " 0xb91c8bc5, 0x8e5880f3,",
  177. " 0xacd7c069, 0xb4c44bb3,",
  178. " 0x2ead1fb7, 0x8e428171,",
  179. " 0xdbebd459, 0x828ae611,",
  180. " 0x6cb25933, 0x86cdd651,",
  181. " 0x9e8f5f21, 0xd5f8d8e7,",
  182. " 0x9c4e956f, 0xb5cf2c71,",
  183. " 0x2e805a6d, 0x33fc3a55,",
  184. " 0xaf203ed1, 0xe31f5909,",
  185. " 0x5276db35, 0x0c565ef7,",
  186. " 0x273d1aa5, 0x8923b1dd,",
  187. " 0",
  188. "};",
  189. "int mreached=0, done=0, errors=0, Nrun=1, single=0;",
  190. "double nstates=0, nlinks=0, truncs=0, truncs2=0;",
  191. "double nlost=0, nShadow=0, hcmp=0, ngrabs=0;",
  192. "unsigned long nr_states=0; /* nodes in DFA */",
  193. "long Fa=0, Fh=0, Zh=0, Zn=0;",
  194. "long PUT=0, PROBE=0, ZAPS=0;",
  195. "long Ccheck=0, Cholds=0;",
  196. "unsigned long mask;",
  197. "int a_cycles=0, upto=1, strict=0, verbose = 0;",
  198. "int state_tables=0, fairness=0, no_rck=0, Nr_Trails=0;",
  199. "#ifndef INLINE",
  200. "int TstOnly=0;",
  201. "#endif",
  202. "#ifdef BITSTATE",
  203. "int ssize=22;",
  204. "#else",
  205. "int ssize=18;",
  206. "#endif",
  207. "int hmax=0, svmax=0, smax=0;",
  208. "int Maxbody=0, XX;",
  209. "uchar *noptr; /* used by macro Pptr(x) */",
  210. "State A_Root; /* root of acceptance cycles */",
  211. "State now; /* the full state vector */",
  212. "#ifdef VAR_RANGES",
  213. "void logval(char *, int);",
  214. "void dumpranges(void);",
  215. "#endif",
  216. "#ifdef MA",
  217. "#define INLINE_REV",
  218. "extern void dfa_init(unsigned short);",
  219. "extern int dfa_member(unsigned short);",
  220. "extern int dfa_store(unsigned char *);",
  221. "unsigned int maxgs = 0;",
  222. "#endif",
  223. "#if !defined(NOCOMP) || defined(BITSTATE)",
  224. "State comp_now; /* compressed state vector */",
  225. "State comp_msk;",
  226. "uchar *Mask = (uchar *) &comp_msk;",
  227. "#endif",
  228. "#ifdef COLLAPSE",
  229. "State comp_tmp;",
  230. "char *scratch = (char *) &comp_tmp;",
  231. "#endif",
  232. "Stack *stack; /* for queues, processes */",
  233. "Svtack *svtack; /* for old state vectors */",
  234. "long J1, J2, J3, J4, j1, j2, j3, j4;",
  235. "long A_depth = 0, depth = 0;",
  236. "uchar warned = 0, iterative = 0, like_java = 0, every_error = 0;",
  237. "uchar noasserts = 0, noends = 0;",
  238. "#if SYNC>0 && ASYNC==0",
  239. "void set_recvs(void);",
  240. "int no_recvs(int);",
  241. "#endif",
  242. "#if SYNC",
  243. "#define IfNotBlocked if (boq != -1) continue;",
  244. "#define UnBlock boq = -1",
  245. "#else",
  246. "#define IfNotBlocked /* cannot block */",
  247. "#define UnBlock /* don't bother */",
  248. "#endif\n",
  249. "void active_procs(void);",
  250. "void cleanup(void);",
  251. "void do_the_search(void);",
  252. "void find_shorter(int);",
  253. "void iniglobals(void);",
  254. "void stopped(int);",
  255. "void wrapup(void);",
  256. #if 0
  257. "void bup_q(int, int);",
  258. "void unbup_q(int, int);",
  259. #endif
  260. "int *grab_ints(int);",
  261. "void ungrab_ints(int *, int);",
  262. 0,
  263. };
  264. static char *Tail[] = {
  265. "Trans *",
  266. "settr( int t_id, int a, int b, int c, int d,",
  267. " char *t, int g, int tpe0, int tpe1)",
  268. "{ Trans *tmp = (Trans *) emalloc(sizeof(Trans));\n",
  269. " tmp->atom = a&(6|32); /* only (2|8|32) have meaning */",
  270. " if (!g) tmp->atom |= 8; /* no global references */",
  271. " tmp->st = b;",
  272. " tmp->tpe[0] = tpe0;",
  273. " tmp->tpe[1] = tpe1;",
  274. " tmp->tp = t;",
  275. " tmp->t_id = t_id;",
  276. " tmp->forw = c;",
  277. " tmp->back = d;",
  278. " return tmp;",
  279. "}\n",
  280. "Trans *",
  281. "cpytr(Trans *a)",
  282. "{ Trans *tmp = (Trans *) emalloc(sizeof(Trans));\n",
  283. " int i;",
  284. " tmp->atom = a->atom;",
  285. " tmp->st = a->st;",
  286. "#ifdef HAS_UNLESS",
  287. " tmp->e_trans = a->e_trans;",
  288. " for (i = 0; i < HAS_UNLESS; i++)",
  289. " tmp->escp[i] = a->escp[i];",
  290. "#endif",
  291. " tmp->tpe[0] = a->tpe[0];",
  292. " tmp->tpe[1] = a->tpe[1];",
  293. " for (i = 0; i < 6; i++)",
  294. " { tmp->qu[i] = a->qu[i];",
  295. " tmp->ty[i] = a->ty[i];",
  296. " }",
  297. " tmp->tp = (char *) emalloc(strlen(a->tp)+1);",
  298. " strcpy(tmp->tp, a->tp);",
  299. " tmp->t_id = a->t_id;",
  300. " tmp->forw = a->forw;",
  301. " tmp->back = a->back;",
  302. " return tmp;",
  303. "}\n",
  304. "#ifndef NOREDUCE",
  305. "int",
  306. "srinc_set(int n)",
  307. "{ if (n <= 2) return LOCAL;",
  308. " if (n <= 2+ DELTA) return Q_FULL_F; /* 's' or nfull */",
  309. " if (n <= 2+2*DELTA) return Q_EMPT_F; /* 'r' or nempty */",
  310. " if (n <= 2+3*DELTA) return Q_EMPT_T; /* empty */",
  311. " if (n <= 2+4*DELTA) return Q_FULL_T; /* full */",
  312. " if (n == 5*DELTA) return GLOBAL;",
  313. " if (n == 6*DELTA) return TIMEOUT_F;",
  314. " if (n == 7*DELTA) return ALPHA_F;",
  315. " Uerror(\"cannot happen srinc_class\");",
  316. " return BAD;",
  317. "}",
  318. "int",
  319. "srunc(int n, int m)",
  320. "{ switch(m) {",
  321. " case Q_FULL_F: return n-2;",
  322. " case Q_EMPT_F: return n-2-DELTA;",
  323. " case Q_EMPT_T: return n-2-2*DELTA;",
  324. " case Q_FULL_T: return n-2-3*DELTA;",
  325. " case ALPHA_F:",
  326. " case TIMEOUT_F: return 257; /* non-zero, and > MAXQ */",
  327. " }",
  328. " Uerror(\"cannot happen srunc\");",
  329. " return 0;",
  330. "}",
  331. "#endif",
  332. /* new */
  333. "uchar *visited;",
  334. "void",
  335. "dovisit(int n, int m, int is, uchar reach[])",
  336. "{ Trans *z;",
  337. " if (is >= m || !trans[n][is]",
  338. " || is <= 0 || visited[is])",
  339. " return;",
  340. " visited[is] = 1;",
  341. " for (z = trans[n][is]; z; z = z->nxt)",
  342. " { int i, j;",
  343. " dovisit(n, m, z->st, reach);",
  344. "#ifdef HAS_UNLESS",
  345. " for (i = 0; i < HAS_UNLESS; i++)",
  346. " { j = trans[n][is]->escp[i];",
  347. " if (!j) break;",
  348. " dovisit(n, m, j, reach);",
  349. " }",
  350. "#endif",
  351. " }",
  352. "}",
  353. "void",
  354. "check_unreachable(int n, int m, int is, short srcln[], uchar reach[])",
  355. "{ int i; static int oldm=0;",
  356. " if (m > oldm) /* not much memory */",
  357. " { visited = (uchar *) emalloc(m * sizeof(uchar));",
  358. " oldm = m;",
  359. " } else",
  360. " memset(visited, 0, m);",
  361. " dovisit(n, m, is, reach);",
  362. " for (i = 1; i < m; i++)",
  363. " if (!visited[i] && reach[i] == 0)",
  364. " { if (0) printf(\"%%s: merged state %%d, line %%d\\n\",",
  365. " procname[n], i, srcln[i]);",
  366. " reach[i] = 1;",
  367. " }",
  368. "}",
  369. "int cnt;",
  370. "int",
  371. "isthere(Trans *a, int b)", /* is b already in a's list? */
  372. "{ Trans *t;",
  373. " for (t = a; t; t = t->nxt)",
  374. " if (t->t_id == b)",
  375. " return 1;",
  376. " return 0;",
  377. "}",
  378. "#ifndef NOREDUCE",
  379. "int",
  380. "mark_safety(Trans *t) /* for conditional safety */",
  381. "{ int g = 0, h, i, j, k;",
  382. "",
  383. " if (!t) return 0;",
  384. " if (t->qu[0])",
  385. " return (t->qu[1])?2:1; /* marked */",
  386. "",
  387. " for (i = 0; i < 2; i++)",
  388. " { j = srinc_set(t->tpe[i]);",
  389. " if (j >= GLOBAL)",
  390. " return -1;",
  391. " if (j != LOCAL)",
  392. " { k = srunc(t->tpe[i], j);",
  393. " if (g == 0",
  394. " || t->qu[0] != k",
  395. " || t->ty[0] != j)",
  396. " { t->qu[g] = k;",
  397. " t->ty[g] = j;",
  398. " g++;",
  399. " } } }",
  400. " return g;",
  401. "}",
  402. "#endif",
  403. "void",
  404. "retrans(int n, int m, int is, short srcln[], uchar reach[])",
  405. " /* process n, with m states, is=initial state */",
  406. "{ Trans *T0, *T1, *T2, *T3;",
  407. " int i, j, k, p, h, g, aa;",
  408. " if (state_tables >= 4)",
  409. " { printf(\"STEP 1 proctype %%s\\n\", ",
  410. " procname[n]);",
  411. " for (i = 1; i < m; i++)",
  412. " for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  413. " crack(n, i, T0, srcln);",
  414. " return;",
  415. " }",
  416. " do {",
  417. " for (i = 1, cnt = 0; i < m; i++)",
  418. " { T2 = trans[n][i];",
  419. " T1 = T2?T2->nxt:(Trans *)0;",
  420. "/* prescan: */ for (T0 = T1; T0; T0 = T0->nxt)",
  421. "/* choice in choice */ { if (T0->st && trans[n][T0->st]",
  422. " && trans[n][T0->st]->nxt)",
  423. " break;",
  424. " }",
  425. "#if 0",
  426. " if (T0)",
  427. " printf(\"\\tstate %%d / %%d: choice in choice\\n\",",
  428. " i, T0->st);",
  429. "#endif",
  430. " if (T0)",
  431. " for (T0 = T1; T0; T0 = T0->nxt)",
  432. " { T3 = trans[n][T0->st];",
  433. " if (!T3->nxt)",
  434. " { T2->nxt = cpytr(T0);",
  435. " T2 = T2->nxt;",
  436. " imed(T2, T0->st, n);",
  437. " continue;",
  438. " }",
  439. " do { T3 = T3->nxt;",
  440. " T2->nxt = cpytr(T3);",
  441. " T2 = T2->nxt;",
  442. " imed(T2, T0->st, n);",
  443. " } while (T3->nxt);",
  444. " cnt++;",
  445. " }",
  446. " }",
  447. " } while (cnt);",
  448. " if (state_tables >= 3)",
  449. " { printf(\"STEP 2 proctype %%s\\n\", ",
  450. " procname[n]);",
  451. " for (i = 1; i < m; i++)",
  452. " for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  453. " crack(n, i, T0, srcln);",
  454. " return;",
  455. " }",
  456. " for (i = 1; i < m; i++)",
  457. " { if (trans[n][i] && trans[n][i]->nxt) /* optimize */",
  458. " { T1 = trans[n][i]->nxt;",
  459. "#if 0",
  460. " printf(\"\\t\\tpull %%d (%%d) to %%d\\n\",",
  461. " T1->st, T1->forw, i);",
  462. "#endif",
  463. " if (!trans[n][T1->st]) continue;",
  464. " T0 = cpytr(trans[n][T1->st]);",
  465. " trans[n][i] = T0;",
  466. " reach[T1->st] = 1;",
  467. " imed(T0, T1->st, n);",
  468. " for (T1 = T1->nxt; T1; T1 = T1->nxt)",
  469. " {",
  470. "#if 0",
  471. " printf(\"\\t\\tpull %%d (%%d) to %%d\\n\",",
  472. " T1->st, T1->forw, i);",
  473. "#endif",
  474. " if (!trans[n][T1->st]) continue;",
  475. " T0->nxt = cpytr(trans[n][T1->st]);",
  476. " T0 = T0->nxt;",
  477. " reach[T1->st] = 1;",
  478. " imed(T0, T1->st, n);",
  479. " } } }",
  480. " if (state_tables >= 2)",
  481. " { printf(\"STEP 3 proctype %%s\\n\", ",
  482. " procname[n]);",
  483. " for (i = 1; i < m; i++)",
  484. " for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  485. " crack(n, i, T0, srcln);",
  486. " return;",
  487. " }",
  488. "#ifdef HAS_UNLESS",
  489. " for (i = 1; i < m; i++)",
  490. " { if (!trans[n][i]) continue;",
  491. " /* check for each state i if an",
  492. " * escape to some state p is defined",
  493. " * if so, copy and mark p's transitions",
  494. " * and prepend them to the transition-",
  495. " * list of state i",
  496. " */",
  497. " if (!like_java) /* the default */",
  498. " { for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  499. " for (k = HAS_UNLESS-1; k >= 0; k--)",
  500. " { if (p = T0->escp[k])",
  501. " for (T1 = trans[n][p]; T1; T1 = T1->nxt)",
  502. " { if (isthere(trans[n][i], T1->t_id))",
  503. " continue;",
  504. " T2 = cpytr(T1);",
  505. " T2->e_trans = p;",
  506. " T2->nxt = trans[n][i];",
  507. " trans[n][i] = T2;",
  508. " } }",
  509. " } else /* outermost unless checked first */",
  510. " { Trans *T4;",
  511. " T4 = T3 = (Trans *) 0;",
  512. " for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  513. " for (k = HAS_UNLESS-1; k >= 0; k--)",
  514. " { if (p = T0->escp[k])",
  515. " for (T1 = trans[n][p]; T1; T1 = T1->nxt)",
  516. " { if (isthere(trans[n][i], T1->t_id))",
  517. " continue;",
  518. " T2 = cpytr(T1);",
  519. " T2->nxt = (Trans *) 0;",
  520. " T2->e_trans = p;",
  521. " if (T3) T3->nxt = T2;",
  522. " else T4 = T2;",
  523. " T3 = T2;",
  524. " } }",
  525. " if (T4)",
  526. " { T3->nxt = trans[n][i];",
  527. " trans[n][i] = T4;",
  528. " }",
  529. " }",
  530. " }",
  531. "#endif",
  532. "#ifndef NOREDUCE",
  533. " for (i = 1; i < m; i++)",
  534. " {",
  535. " if (a_cycles)",
  536. " { /* moves through these states are visible */",
  537. "#if PROG_LAB>0 && defined(HAS_NP)",
  538. " if (progstate[n][i])",
  539. " goto degrade;",
  540. " for (T1 = trans[n][i]; T1; T1 = T1->nxt)",
  541. " if (progstate[n][T1->st])",
  542. " goto degrade;",
  543. "#endif",
  544. " if (accpstate[n][i] || visstate[n][i])",
  545. " goto degrade;",
  546. " for (T1 = trans[n][i]; T1; T1 = T1->nxt)",
  547. " if (accpstate[n][T1->st])",
  548. " goto degrade;",
  549. " }",
  550. " T1 = trans[n][i];",
  551. " if (!T1) continue;",
  552. " g = mark_safety(T1); /* V3.3.1 */",
  553. " if (g < 0) goto degrade; /* global */",
  554. " /* check if mixing of guards preserves reduction */",
  555. " if (T1->nxt)",
  556. " { k = 0;",
  557. " for (T0 = T1; T0; T0 = T0->nxt)",
  558. " { if (!(T0->atom&8))",
  559. " goto degrade;",
  560. " for (aa = 0; aa < 2; aa++)",
  561. " { if (srinc_set(T0->tpe[aa])",
  562. " >= GLOBAL)",
  563. " goto degrade;",
  564. " if (T0->tpe[aa]",
  565. " && T0->tpe[aa]",
  566. " != T1->tpe[0])",
  567. " k = 1;",
  568. " } }",
  569. " /* g = 0; V3.3.1 */",
  570. " if (k) /* non-uniform selection */",
  571. " for (T0 = T1; T0; T0 = T0->nxt)",
  572. " for (aa = 0; aa < 2; aa++)",
  573. " { j = srinc_set(T0->tpe[aa]);",
  574. " if (j != LOCAL)",
  575. " { k = srunc(T0->tpe[aa], j);",
  576. " for (h = 0; h < 6; h++)",
  577. " if (T1->qu[h] == k",
  578. " && T1->ty[h] == j)",
  579. " break;",
  580. " if (h >= 6)",
  581. " { T1->qu[g%%6] = k;",
  582. " T1->ty[g%%6] = j;",
  583. " g++;",
  584. " } } }",
  585. " if (g > 6)",
  586. " { T1->qu[0] = 0; /* turn it off */",
  587. "#if 1",
  588. " printf(\"pan: warning, line %%d, \",",
  589. " srcln[i]);",
  590. " printf(\"too many stmnt types (%%d)\",",
  591. " g);",
  592. " printf(\" in selection\\n\");",
  593. "#endif",
  594. " goto degrade;",
  595. " }",
  596. " }",
  597. " /* mark all options global if >=1 is global */",
  598. " for (T1 = trans[n][i]; T1; T1 = T1->nxt)",
  599. " if (!(T1->atom&8)) break;",
  600. " if (T1)",
  601. "degrade: for (T1 = trans[n][i]; T1; T1 = T1->nxt)",
  602. " T1->atom &= ~8; /* mark as unsafe */",
  603. " /* can only mix 'r's or 's's if on same chan */",
  604. " /* and not mixed with other local operations */",
  605. " T1 = trans[n][i];",
  606. " if (!T1 || T1->qu[0]) continue;",
  607. " j = T1->tpe[0];",
  608. " if (T1->nxt && T1->atom&8)",
  609. " { if (j == 5*DELTA)",
  610. " {",
  611. "#if 1",
  612. " printf(\"warning: line %%d \", srcln[i]);",
  613. " printf(\"mixed condition \");",
  614. " printf(\"(defeats reduction)\\n\");",
  615. "#endif",
  616. " goto degrade;",
  617. " }",
  618. " for (T0 = T1; T0; T0 = T0->nxt)",
  619. " for (aa = 0; aa < 2; aa++)",
  620. " if (T0->tpe[aa] && T0->tpe[aa] != j)",
  621. " {",
  622. "#if 1",
  623. " printf(\"warning: line %%d \", srcln[i]);",
  624. " printf(\"[%%d-%%d] mixed %%stion \",",
  625. " T0->tpe[aa], j, ",
  626. " (j==5*DELTA)?\"condi\":\"selec\");",
  627. " printf(\"(defeats reduction)\\n\");",
  628. " printf(\" '%%s' <-> '%%s'\\n\",",
  629. " T1->tp, T0->tp);",
  630. "#endif",
  631. " goto degrade;",
  632. " } }",
  633. " }",
  634. "#endif",
  635. " for (i = 1; i < m; i++)", /* R */
  636. " { T2 = trans[n][i];",
  637. " if (!T2",
  638. " || T2->nxt",
  639. " || strncmp(T2->tp, \".(goto)\", 7)",
  640. " || !stopstate[n][i])",
  641. " continue;",
  642. " stopstate[n][T2->st] = 1;",
  643. " }",
  644. " if (state_tables)",
  645. " { printf(\"proctype \");",
  646. " if (!strcmp(procname[n], \":init:\"))",
  647. " printf(\"init\\n\");",
  648. " else",
  649. " printf(\"%%s\\n\", procname[n]);",
  650. " for (i = 1; i < m; i++)",
  651. " reach[i] = 1;",
  652. " tagtable(n, m, is, srcln, reach);",
  653. " } else",
  654. " for (i = 1; i < m; i++)",
  655. " { int nrelse;",
  656. " if (strcmp(procname[n], \":never:\") != 0)",
  657. " for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  658. " { if (T0->st == i",
  659. " && strcmp(T0->tp, \"(1)\") == 0)",
  660. " { printf(\"error: proctype '%%s' \",",
  661. " procname[n]);",
  662. " printf(\"line %%d, state %%d: has un\",",
  663. " srcln[i], i);",
  664. " printf(\"conditional self-loop\\n\");",
  665. " exit(1);",
  666. " } }",
  667. " nrelse = 0;",
  668. " for (T0 = trans[n][i]; T0; T0 = T0->nxt)",
  669. " { if (strcmp(T0->tp, \"else\") == 0)",
  670. " nrelse++;",
  671. " }",
  672. " if (nrelse > 1)",
  673. " { printf(\"error: proctype '%%s' state\",",
  674. " procname[n]);",
  675. " printf(\" %%d, inherits %%d\", i, nrelse);",
  676. " printf(\" 'else' stmnts\\n\");",
  677. " exit(1);",
  678. " } }",
  679. " check_unreachable(n, m, is, srcln, reach);",
  680. "}\n",
  681. "void",
  682. "imed(Trans *T, int v, int n) /* set intermediate state */",
  683. "{ progstate[n][T->st] |= progstate[n][v];",
  684. " accpstate[n][T->st] |= accpstate[n][v];",
  685. " stopstate[n][T->st] |= stopstate[n][v];",
  686. "}\n",
  687. "void",
  688. "tagtable(int n, int m, int is, short srcln[], uchar reach[])",
  689. "{ Trans *z;\n",
  690. " if (is >= m || !trans[n][is]",
  691. " || is <= 0 || reach[is] == 0)",
  692. " return;",
  693. " reach[is] = 0;",
  694. " if (state_tables)",
  695. " for (z = trans[n][is]; z; z = z->nxt)",
  696. " crack(n, is, z, srcln);",
  697. " for (z = trans[n][is]; z; z = z->nxt)",
  698. " { int i, j;",
  699. " tagtable(n, m, z->st, srcln, reach);",
  700. "#ifdef HAS_UNLESS",
  701. " for (i = 0; i < HAS_UNLESS; i++)",
  702. " { j = trans[n][is]->escp[i];",
  703. " if (!j) break;",
  704. " tagtable(n, m, j, srcln, reach);",
  705. " }",
  706. "#endif",
  707. " }",
  708. "}\n",
  709. "void",
  710. "crack(int n, int j, Trans *z, short srcln[])",
  711. "{ int i;\n",
  712. " if (!z) return;",
  713. " printf(\"\tstate %%3d -(tr %%3d)-> state %%3d \",",
  714. " j, z->forw, z->st);",
  715. " printf(\"[id %%3d tp %%3d\", z->t_id, z->tpe[0]);",
  716. " if (z->tpe[1]) printf(\",%%d\", z->tpe[1]);",
  717. "#ifdef HAS_UNLESS",
  718. " if (z->e_trans)",
  719. " printf(\" org %%3d\", z->e_trans);",
  720. " else if (state_tables >= 2)",
  721. " for (i = 0; i < HAS_UNLESS; i++)",
  722. " { if (!z->escp[i]) break;",
  723. " printf(\" esc %%d\", z->escp[i]);",
  724. " }",
  725. "#endif",
  726. " printf(\"]\");",
  727. " printf(\" [%%s%%s%%s%%s%%s] line %%d => \",",
  728. " z->atom&6?\"A\":z->atom&32?\"D\":\"-\",",
  729. " accpstate[n][j]?\"a\" :\"-\",",
  730. " stopstate[n][j]?\"e\" : \"-\",",
  731. " progstate[n][j]?\"p\" : \"-\",",
  732. " z->atom & 8 ?\"L\":\"G\",",
  733. " srcln[j]);",
  734. " for (i = 0; z->tp[i]; i++)",
  735. " if (z->tp[i] == \'\\n\')",
  736. " printf(\"\\\\n\");",
  737. " else",
  738. " putchar(z->tp[i]);",
  739. "#if 0",
  740. " printf(\"\\n\");",
  741. "#else",
  742. " if (z->qu[0])",
  743. " { printf(\"\\t[\");",
  744. " for (i = 0; i < 6; i++)",
  745. " if (z->qu[i])",
  746. " printf(\"(%%d,%%d)\",",
  747. " z->qu[i], z->ty[i]);",
  748. " printf(\"]\");",
  749. " }",
  750. " printf(\"\\n\");",
  751. "#endif",
  752. " fflush(stdout);",
  753. "}",
  754. "",
  755. "#ifdef VAR_RANGES",
  756. "#define BYTESIZE 32 /* 2^8 : 2^3 = 256:8 = 32 */",
  757. "",
  758. "typedef struct Vr_Ptr {",
  759. " char *nm;",
  760. " unsigned char vals[BYTESIZE];",
  761. " struct Vr_Ptr *nxt;",
  762. "} Vr_Ptr;",
  763. "Vr_Ptr *ranges = (Vr_Ptr *) 0;",
  764. "",
  765. "void",
  766. "logval(char *s, int v)",
  767. "{ Vr_Ptr *tmp;",
  768. "",
  769. " if (v<0 || v > 255) return;",
  770. " for (tmp = ranges; tmp; tmp = tmp->nxt)",
  771. " if (!strcmp(tmp->nm, s))",
  772. " goto found;",
  773. " tmp = (Vr_Ptr *) emalloc(sizeof(Vr_Ptr));",
  774. " tmp->nxt = ranges;",
  775. " ranges = tmp;",
  776. " tmp->nm = s;",
  777. "found:",
  778. " tmp->vals[(v)/8] |= 1<<((v)%%8);",
  779. "}",
  780. "",
  781. "void",
  782. "dumpval(unsigned char X[], int range)",
  783. "{ int w, x, i, j = -1;",
  784. "",
  785. " for (w = i = 0; w < range; w++)",
  786. " for (x = 0; x < 8; x++, i++)",
  787. " {",
  788. "from: if ((X[w] & (1<<x)))",
  789. " { printf(\"%%d\", i);",
  790. " j = i;",
  791. " goto upto;",
  792. " } }",
  793. " return;",
  794. " for (w = 0; w < range; w++)",
  795. " for (x = 0; x < 8; x++, i++)",
  796. " {",
  797. "upto: if (!(X[w] & (1<<x)))",
  798. " { if (i-1 == j)",
  799. " printf(\", \");",
  800. " else",
  801. " printf(\"-%%d, \", i-1);",
  802. " goto from;",
  803. " } }",
  804. " if (j >= 0 && j != 255)",
  805. " printf(\"-255\");",
  806. "}",
  807. "",
  808. "void",
  809. "dumpranges(void)",
  810. "{ Vr_Ptr *tmp;",
  811. " printf(\"\\nValues assigned within \");",
  812. " printf(\"interval [0..255]:\\n\");",
  813. " for (tmp = ranges; tmp; tmp = tmp->nxt)",
  814. " { printf(\"\\t%%s\\t: \", tmp->nm);",
  815. " dumpval(tmp->vals, BYTESIZE);",
  816. " printf(\"\\n\");",
  817. " }",
  818. "}",
  819. "#endif",
  820. 0,
  821. };