pangen2.h 23 KB

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