pangen5.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /***** spin: pangen5.h *****/
  2. /* Copyright (c) 1997-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 *Xpt[] = {
  11. "#if defined(MA) && (defined(W_XPT) || defined(R_XPT))",
  12. "static Vertex **temptree;",
  13. "static char wbuf[4096];",
  14. "static int WCNT = 4096, wcnt=0;",
  15. "static uchar stacker[MA+1];",
  16. "static ulong stackcnt = 0;",
  17. "extern double nstates, nlinks, truncs, truncs2;",
  18. "",
  19. "static void",
  20. "xwrite(int fd, char *b, int n)",
  21. "{",
  22. " if (wcnt+n >= 4096)",
  23. " { write(fd, wbuf, wcnt);",
  24. " wcnt = 0;",
  25. " }",
  26. " memcpy(&wbuf[wcnt], b, n);",
  27. " wcnt += n;",
  28. "}",
  29. "",
  30. "static void",
  31. "wclose(fd)",
  32. "{",
  33. " if (wcnt > 0)",
  34. " write(fd, wbuf, wcnt);",
  35. " wcnt = 0;",
  36. " close(fd);",
  37. "}",
  38. "",
  39. "static void",
  40. "w_vertex(int fd, Vertex *v)",
  41. "{ char t[3]; int i; Edge *e;",
  42. "",
  43. " xwrite(fd, (char *) &v, sizeof(Vertex *));",
  44. " t[0] = 0;",
  45. " for (i = 0; i < 2; i++)",
  46. " if (v->dst[i])",
  47. " { t[1] = v->from[i], t[2] = v->to[i];",
  48. " xwrite(fd, t, 3);",
  49. " xwrite(fd, (char *) &(v->dst[i]), sizeof(Vertex *));",
  50. " }",
  51. " for (e = v->Succ; e; e = e->Nxt)",
  52. " { t[1] = e->From, t[2] = e->To;",
  53. " xwrite(fd, t, 3);",
  54. " xwrite(fd, (char *) &(e->Dst), sizeof(Vertex *));",
  55. "",
  56. " if (e->s)",
  57. " { t[1] = t[2] = e->S;",
  58. " xwrite(fd, t, 3);",
  59. " xwrite(fd, (char *) &(e->Dst), sizeof(Vertex *));",
  60. " } }",
  61. "}",
  62. "",
  63. "static void",
  64. "w_layer(int fd, Vertex *v)",
  65. "{ uchar c=1;",
  66. "",
  67. " if (!v) return;",
  68. " xwrite(fd, (char *) &c, 1);",
  69. " w_vertex(fd, v);",
  70. " w_layer(fd, v->lnk);",
  71. " w_layer(fd, v->left);",
  72. " w_layer(fd, v->right);",
  73. "}",
  74. "",
  75. "void",
  76. "w_xpoint(void)",
  77. "{ int fd; char nm[64];",
  78. " int i, j; uchar c;",
  79. " static uchar xwarned = 0;",
  80. "",
  81. " sprintf(nm, \"%%s.xpt\", Source);",
  82. " if ((fd = creat(nm, 0666)) <= 0)",
  83. " if (!xwarned)",
  84. " { xwarned = 1;",
  85. " printf(\"cannot creat checkpoint file\\n\");",
  86. " return;",
  87. " }",
  88. " xwrite(fd, (char *) &nstates, sizeof(double));",
  89. " xwrite(fd, (char *) &truncs, sizeof(double));",
  90. " xwrite(fd, (char *) &truncs2, sizeof(double));",
  91. " xwrite(fd, (char *) &nlinks, sizeof(double));",
  92. " xwrite(fd, (char *) &dfa_depth, sizeof(int));",
  93. " xwrite(fd, (char *) &R, sizeof(Vertex *));",
  94. " xwrite(fd, (char *) &F, sizeof(Vertex *));",
  95. " xwrite(fd, (char *) &NF, sizeof(Vertex *));",
  96. "",
  97. " for (j = 0; j < TWIDTH; j++)",
  98. " for (i = 0; i < dfa_depth+1; i++)",
  99. " { w_layer(fd, layers[i*TWIDTH+j]);",
  100. " c = 2; xwrite(fd, (char *) &c, 1);",
  101. " }",
  102. " wclose(fd);",
  103. "}",
  104. "",
  105. "static void",
  106. "xread(int fd, char *b, int n)",
  107. "{ int m = wcnt; int delta = 0;",
  108. " if (m < n)",
  109. " { if (m > 0) memcpy(b, &wbuf[WCNT-m], m);",
  110. " delta = m;",
  111. " WCNT = wcnt = read(fd, wbuf, 4096);",
  112. " if (wcnt < n-m)",
  113. " Uerror(\"xread failed -- insufficient data\");",
  114. " n -= m;",
  115. " }",
  116. " memcpy(&b[delta], &wbuf[WCNT-wcnt], n);",
  117. " wcnt -= n;",
  118. "}",
  119. "",
  120. "static void",
  121. "x_cleanup(Vertex *c)",
  122. "{ Edge *e; /* remove the tree and edges from c */",
  123. " if (!c) return;",
  124. " for (e = c->Succ; e; e = e->Nxt)",
  125. " x_cleanup(e->Dst);",
  126. " recyc_vertex(c);",
  127. "}",
  128. "",
  129. "static void",
  130. "x_remove(void)",
  131. "{ Vertex *tmp; int i, s;",
  132. " int r, j;",
  133. " /* double-check: */",
  134. " stacker[dfa_depth-1] = 0; r = dfa_store(stacker);",
  135. " stacker[dfa_depth-1] = 4; j = dfa_member(dfa_depth-1);",
  136. " if (r != 1 || j != 0)",
  137. " { printf(\"%%d: \", stackcnt);",
  138. " for (i = 0; i < dfa_depth; i++)",
  139. " printf(\"%%d,\", stacker[i]);",
  140. " printf(\" -- not a stackstate <o:%%d,4:%%d>\\n\", r, j);",
  141. " return;",
  142. " }",
  143. " stacker[dfa_depth-1] = 1;",
  144. " s = dfa_member(dfa_depth-1);",
  145. "",
  146. " { tmp = F; F = NF; NF = tmp; } /* complement */",
  147. " if (s) dfa_store(stacker);",
  148. " stacker[dfa_depth-1] = 0;",
  149. " dfa_store(stacker);",
  150. " stackcnt++;",
  151. " { tmp = F; F = NF; NF = tmp; }",
  152. "}",
  153. "",
  154. "static void",
  155. "x_rm_stack(Vertex *t, int k)",
  156. "{ int j; Edge *e;",
  157. "",
  158. " if (k == 0)",
  159. " { x_remove();",
  160. " return;",
  161. " }",
  162. " if (t)",
  163. " for (e = t->Succ; e; e = e->Nxt)",
  164. " { for (j = e->From; j <= (int) e->To; j++)",
  165. " { stacker[k] = (uchar) j;",
  166. " x_rm_stack(e->Dst, k-1);",
  167. " }",
  168. " if (e->s)",
  169. " { stacker[k] = e->S;",
  170. " x_rm_stack(e->Dst, k-1);",
  171. " } }",
  172. "}",
  173. "",
  174. "static Vertex *",
  175. "insert_withkey(Vertex *v, int L)",
  176. "{ Vertex *new, *t = temptree[L];",
  177. "",
  178. " if (!t) { temptree[L] = v; return v; }",
  179. " t = splay(v->key, t);",
  180. " if (v->key < t->key)",
  181. " { new = v;",
  182. " new->left = t->left;",
  183. " new->right = t;",
  184. " t->left = (Vertex *) 0;",
  185. " } else if (v->key > t->key)",
  186. " { new = v;",
  187. " new->right = t->right;",
  188. " new->left = t;",
  189. " t->right = (Vertex *) 0;",
  190. " } else",
  191. " { if (t != R && t != F && t != NF)",
  192. " Uerror(\"double insert, bad checkpoint data\");",
  193. " else",
  194. " { recyc_vertex(v);",
  195. " new = t;",
  196. " } }",
  197. " temptree[L] = new;",
  198. "",
  199. " return new;",
  200. "}",
  201. "",
  202. "static Vertex *",
  203. "find_withkey(Vertex *v, int L)",
  204. "{ Vertex *t = temptree[L];",
  205. " if (t)",
  206. " { temptree[L] = t = splay((ulong) v, t);",
  207. " if (t->key == (ulong) v)",
  208. " return t;",
  209. " }",
  210. " Uerror(\"not found error, bad checkpoint data\");",
  211. " return (Vertex *) 0;",
  212. "}",
  213. "",
  214. "void",
  215. "r_layer(int fd, int n)",
  216. "{ Vertex *v;",
  217. " Edge *e;",
  218. " char c, t[2];",
  219. "",
  220. " for (;;)",
  221. " { xread(fd, &c, 1);",
  222. " if (c == 2) break;",
  223. " if (c == 1)",
  224. " { v = new_vertex();",
  225. " xread(fd, (char *) &(v->key), sizeof(Vertex *));",
  226. " v = insert_withkey(v, n);",
  227. " } else /* c == 0 */",
  228. " { e = new_edge((Vertex *) 0);",
  229. " xread(fd, t, 2);",
  230. " e->From = t[0];",
  231. " e->To = t[1];",
  232. " xread(fd, (char *) &(e->Dst), sizeof(Vertex *));",
  233. " insert_edge(v, e);",
  234. " } }",
  235. "}",
  236. "",
  237. "static void",
  238. "v_fix(Vertex *t, int nr)",
  239. "{ int i; Edge *e;",
  240. "",
  241. " if (!t) return;",
  242. "",
  243. " for (i = 0; i < 2; i++)",
  244. " if (t->dst[i])",
  245. " t->dst[i] = find_withkey(t->dst[i], nr);",
  246. "",
  247. " for (e = t->Succ; e; e = e->Nxt)",
  248. " e->Dst = find_withkey(e->Dst, nr);",
  249. " ",
  250. " v_fix(t->left, nr);",
  251. " v_fix(t->right, nr);",
  252. "}",
  253. "",
  254. "static void",
  255. "v_insert(Vertex *t, int nr)",
  256. "{ Edge *e; int i;",
  257. "",
  258. " if (!t) return;",
  259. " v_insert(t->left, nr);",
  260. " v_insert(t->right, nr);",
  261. "",
  262. " /* remove only leafs from temptree */",
  263. " t->left = t->right = t->lnk = (Vertex *) 0;",
  264. " insert_it(t, nr); /* into layers */",
  265. " for (i = 0; i < 2; i++)",
  266. " if (t->dst[i])",
  267. " t->dst[i]->num += (t->to[i] - t->from[i] + 1);",
  268. " for (e = t->Succ; e; e = e->Nxt)",
  269. " e->Dst->num += (e->To - e->From + 1 + e->s);",
  270. "}",
  271. "",
  272. "static void",
  273. "x_fixup(void)",
  274. "{ int i;",
  275. "",
  276. " for (i = 0; i < dfa_depth; i++)",
  277. " v_fix(temptree[i], (i+1));",
  278. "",
  279. " for (i = dfa_depth; i >= 0; i--)",
  280. " v_insert(temptree[i], i);",
  281. "}",
  282. "",
  283. "static Vertex *",
  284. "x_tail(Vertex *t, ulong want)",
  285. "{ int i, yes, no; Edge *e; Vertex *v = (Vertex *) 0;",
  286. "",
  287. " if (!t) return v;",
  288. "",
  289. " yes = no = 0;",
  290. " for (i = 0; i < 2; i++)",
  291. " if ((ulong) t->dst[i] == want)",
  292. " { /* was t->from[i] <= 0 && t->to[i] >= 0 */",
  293. " /* but from and to are uchar */",
  294. " if (t->from[i] == 0)",
  295. " yes = 1;",
  296. " else",
  297. " if (t->from[i] <= 4 && t->to[i] >= 4)",
  298. " no = 1;",
  299. " }",
  300. "",
  301. " for (e = t->Succ; e; e = e->Nxt)",
  302. " if ((ulong) e->Dst == want)",
  303. " { /* was INRANGE(e,0) but From and To are uchar */",
  304. " if ((e->From == 0) || (e->s==1 && e->S==0))",
  305. " yes = 1;",
  306. " else if (INRANGE(e, 4))",
  307. " no = 1;",
  308. " }",
  309. " if (yes && !no) return t;",
  310. " v = x_tail(t->left, want); if (v) return v;",
  311. " v = x_tail(t->right, want); if (v) return v;",
  312. " return (Vertex *) 0;",
  313. "}",
  314. "",
  315. "static void",
  316. "x_anytail(Vertex *t, Vertex *c, int nr)",
  317. "{ int i; Edge *e, *f; Vertex *v;",
  318. "",
  319. " if (!t) return;",
  320. "",
  321. " for (i = 0; i < 2; i++)",
  322. " if ((ulong) t->dst[i] == c->key)",
  323. " { v = new_vertex(); v->key = t->key;",
  324. " f = new_edge(v);",
  325. " f->From = t->from[i];",
  326. " f->To = t->to[i];",
  327. " f->Nxt = c->Succ;",
  328. " c->Succ = f;",
  329. " if (nr > 0)",
  330. " x_anytail(temptree[nr-1], v, nr-1);",
  331. " }",
  332. "",
  333. " for (e = t->Succ; e; e = e->Nxt)",
  334. " if ((ulong) e->Dst == c->key)",
  335. " { v = new_vertex(); v->key = t->key;",
  336. " f = new_edge(v);",
  337. " f->From = e->From;",
  338. " f->To = e->To;",
  339. " f->s = e->s;",
  340. " f->S = e->S;",
  341. " f->Nxt = c->Succ;",
  342. " c->Succ = f;",
  343. " x_anytail(temptree[nr-1], v, nr-1);",
  344. " }",
  345. "",
  346. " x_anytail(t->left, c, nr);",
  347. " x_anytail(t->right, c, nr);",
  348. "}",
  349. "",
  350. "static Vertex *",
  351. "x_cpy_rev(void)",
  352. "{ Vertex *c, *v; /* find 0 and !4 predecessor of F */",
  353. "",
  354. " v = x_tail(temptree[dfa_depth-1], F->key);",
  355. " if (!v) return (Vertex *) 0;",
  356. "",
  357. " c = new_vertex(); c->key = v->key;",
  358. "",
  359. " /* every node on dfa_depth-2 that has v->key as succ */",
  360. " /* make copy and let c point to these (reversing ptrs) */",
  361. "",
  362. " x_anytail(temptree[dfa_depth-2], c, dfa_depth-2);",
  363. " ",
  364. " return c;",
  365. "}",
  366. "",
  367. "void",
  368. "r_xpoint(void)",
  369. "{ int fd; char nm[64]; Vertex *d;",
  370. " int i, j;",
  371. "",
  372. " wcnt = 0;",
  373. " sprintf(nm, \"%%s.xpt\", Source);",
  374. " if ((fd = open(nm, 0)) < 0) /* O_RDONLY */",
  375. " Uerror(\"cannot open checkpoint file\");",
  376. "",
  377. " xread(fd, (char *) &nstates, sizeof(double));",
  378. " xread(fd, (char *) &truncs, sizeof(double));",
  379. " xread(fd, (char *) &truncs2, sizeof(double));",
  380. " xread(fd, (char *) &nlinks, sizeof(double));",
  381. " xread(fd, (char *) &dfa_depth, sizeof(int));",
  382. "",
  383. " if (dfa_depth != MA+a_cycles)",
  384. " Uerror(\"bad dfa_depth in checkpoint file\");",
  385. "",
  386. " path = (Vertex **) emalloc((dfa_depth+1)*sizeof(Vertex *));",
  387. " layers = (Vertex **) emalloc(TWIDTH*(dfa_depth+1)*sizeof(Vertex *));",
  388. " temptree = (Vertex **) emalloc((dfa_depth+2)*sizeof(Vertex *));",
  389. " lastword = (uchar *) emalloc((dfa_depth+1)*sizeof(uchar));",
  390. " lastword[dfa_depth] = lastword[0] = 255; ",
  391. "",
  392. " path[0] = R = new_vertex();",
  393. " xread(fd, (char *) &R->key, sizeof(Vertex *));",
  394. " R = insert_withkey(R, 0);",
  395. "",
  396. " F = new_vertex();",
  397. " xread(fd, (char *) &F->key, sizeof(Vertex *));",
  398. " F = insert_withkey(F, dfa_depth);",
  399. "",
  400. " NF = new_vertex();",
  401. " xread(fd, (char *) &NF->key, sizeof(Vertex *));",
  402. " NF = insert_withkey(NF, dfa_depth);",
  403. "",
  404. " for (j = 0; j < TWIDTH; j++)",
  405. " for (i = 0; i < dfa_depth+1; i++)",
  406. " r_layer(fd, i);",
  407. "",
  408. " if (wcnt != 0) Uerror(\"bad count in checkpoint file\");",
  409. "",
  410. " d = x_cpy_rev();",
  411. " x_fixup();",
  412. " stacker[dfa_depth-1] = 0;",
  413. " x_rm_stack(d, dfa_depth-2);",
  414. " x_cleanup(d);",
  415. " close(fd);",
  416. "",
  417. " printf(\"pan: removed %%d stackstates\\n\", stackcnt);",
  418. " nstates -= (double) stackcnt;",
  419. "}",
  420. "#endif",
  421. 0,
  422. };