pangen5.h 11 KB

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