dstep.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /***** spin: dstep.c *****/
  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. #include "spin.h"
  11. #ifdef PC
  12. #include "y_tab.h"
  13. #else
  14. #include "y.tab.h"
  15. #endif
  16. #define MAXDSTEP 1024 /* was 512 */
  17. char *NextLab[64];
  18. int Level=0, GenCode=0, IsGuard=0, TestOnly=0;
  19. static int Tj=0, Jt=0, LastGoto=0;
  20. static int Tojump[MAXDSTEP], Jumpto[MAXDSTEP], Special[MAXDSTEP];
  21. static void putCode(FILE *, Element *, Element *, Element *, int);
  22. extern int Pid, claimnr, separate, OkBreak;
  23. static void
  24. Sourced(int n, int special)
  25. { int i;
  26. for (i = 0; i < Tj; i++)
  27. if (Tojump[i] == n)
  28. return;
  29. if (Tj >= MAXDSTEP)
  30. fatal("d_step sequence too long", (char *)0);
  31. Special[Tj] = special;
  32. Tojump[Tj++] = n;
  33. }
  34. static void
  35. Dested(int n)
  36. { int i;
  37. for (i = 0; i < Tj; i++)
  38. if (Tojump[i] == n)
  39. return;
  40. for (i = 0; i < Jt; i++)
  41. if (Jumpto[i] == n)
  42. return;
  43. if (Jt >= MAXDSTEP)
  44. fatal("d_step sequence too long", (char *)0);
  45. Jumpto[Jt++] = n;
  46. LastGoto = 1;
  47. }
  48. static void
  49. Mopup(FILE *fd)
  50. { int i, j;
  51. for (i = 0; i < Jt; i++)
  52. { for (j = 0; j < Tj; j++)
  53. if (Tojump[j] == Jumpto[i])
  54. break;
  55. if (j == Tj)
  56. { char buf[12];
  57. if (Jumpto[i] == OkBreak)
  58. { if (!LastGoto)
  59. fprintf(fd, "S_%.3d_0: /* break-dest */\n",
  60. OkBreak);
  61. } else {
  62. sprintf(buf, "S_%.3d_0", Jumpto[i]);
  63. non_fatal("goto %s breaks from d_step seq", buf);
  64. } } }
  65. for (j = 0; j < Tj; j++)
  66. { for (i = 0; i < Jt; i++)
  67. if (Tojump[j] == Jumpto[i])
  68. break;
  69. #ifdef DEBUG
  70. if (i == Jt && !Special[i])
  71. fprintf(fd, "\t\t/* no goto's to S_%.3d_0 */\n",
  72. Tojump[j]);
  73. #endif
  74. }
  75. for (j = i = 0; j < Tj; j++)
  76. if (Special[j])
  77. { Tojump[i] = Tojump[j];
  78. Special[i] = 2;
  79. if (i >= MAXDSTEP)
  80. fatal("cannot happen (dstep.c)", (char *)0);
  81. i++;
  82. }
  83. Tj = i; /* keep only the global exit-labels */
  84. Jt = 0;
  85. }
  86. static int
  87. FirstTime(int n)
  88. { int i;
  89. for (i = 0; i < Tj; i++)
  90. if (Tojump[i] == n)
  91. return (Special[i] <= 1);
  92. return 1;
  93. }
  94. static void
  95. illegal(Element *e, char *str)
  96. {
  97. printf("illegal operator in 'd_step:' '");
  98. comment(stdout, e->n, 0);
  99. printf("'\n");
  100. fatal("'%s'", str);
  101. }
  102. static void
  103. filterbad(Element *e)
  104. {
  105. switch (e->n->ntyp) {
  106. case ASSERT:
  107. case PRINT:
  108. case 'c':
  109. /* run cannot be completely undone
  110. * with sv_save-sv_restor
  111. */
  112. if (any_oper(e->n->lft, RUN))
  113. illegal(e, "run operator in d_step");
  114. /* remote refs inside d_step sequences
  115. * would be okay, but they cannot always
  116. * be interpreted by the simulator the
  117. * same as by the verifier (e.g., for an
  118. * error trail)
  119. */
  120. if (any_oper(e->n->lft, 'p'))
  121. illegal(e, "remote reference in d_step");
  122. break;
  123. case '@':
  124. illegal(e, "process termination");
  125. break;
  126. case D_STEP:
  127. illegal(e, "nested d_step sequence");
  128. break;
  129. case ATOMIC:
  130. illegal(e, "nested atomic sequence");
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. static int
  137. CollectGuards(FILE *fd, Element *e, int inh)
  138. { SeqList *h; Element *ee;
  139. for (h = e->sub; h; h = h->nxt)
  140. { ee = huntstart(h->this->frst);
  141. filterbad(ee);
  142. switch (ee->n->ntyp) {
  143. case NON_ATOMIC:
  144. inh += CollectGuards(fd, ee->n->sl->this->frst, inh);
  145. break;
  146. case IF:
  147. inh += CollectGuards(fd, ee, inh);
  148. break;
  149. case '.':
  150. if (ee->nxt->n->ntyp == DO)
  151. inh += CollectGuards(fd, ee->nxt, inh);
  152. break;
  153. case ELSE:
  154. if (inh++ > 0) fprintf(fd, " || ");
  155. fprintf(fd, "(1 /* else */)");
  156. break;
  157. case 'R':
  158. if (inh++ > 0) fprintf(fd, " || ");
  159. fprintf(fd, "("); TestOnly=1;
  160. putstmnt(fd, ee->n, ee->seqno);
  161. fprintf(fd, ")"); TestOnly=0;
  162. break;
  163. case 'r':
  164. if (inh++ > 0) fprintf(fd, " || ");
  165. fprintf(fd, "("); TestOnly=1;
  166. putstmnt(fd, ee->n, ee->seqno);
  167. fprintf(fd, ")"); TestOnly=0;
  168. break;
  169. case 's':
  170. if (inh++ > 0) fprintf(fd, " || ");
  171. fprintf(fd, "("); TestOnly=1;
  172. /* 4.2.1 */ if (Pid != claimnr) fprintf(fd, "(boq == -1) && ");
  173. putstmnt(fd, ee->n, ee->seqno);
  174. fprintf(fd, ")"); TestOnly=0;
  175. break;
  176. case 'c':
  177. if (inh++ > 0) fprintf(fd, " || ");
  178. fprintf(fd, "("); TestOnly=1;
  179. if (Pid != claimnr)
  180. fprintf(fd, "(boq == -1 && ");
  181. putstmnt(fd, ee->n->lft, e->seqno);
  182. if (Pid != claimnr)
  183. fprintf(fd, ")");
  184. fprintf(fd, ")"); TestOnly=0;
  185. break;
  186. } }
  187. return inh;
  188. }
  189. int
  190. putcode(FILE *fd, Sequence *s, Element *nxt, int justguards, int ln, int seqno)
  191. { int isg=0; char buf[64];
  192. NextLab[0] = "continue";
  193. filterbad(s->frst);
  194. switch (s->frst->n->ntyp) {
  195. case UNLESS:
  196. non_fatal("'unless' inside d_step - ignored", (char *) 0);
  197. return putcode(fd, s->frst->n->sl->this, nxt, 0, ln, seqno);
  198. case NON_ATOMIC:
  199. (void) putcode(fd, s->frst->n->sl->this, ZE, 1, ln, seqno);
  200. break;
  201. case IF:
  202. fprintf(fd, "if (!(");
  203. if (!CollectGuards(fd, s->frst, 0)) /* what about boq? */
  204. fprintf(fd, "1");
  205. fprintf(fd, "))\n\t\t\tcontinue;");
  206. isg = 1;
  207. break;
  208. case '.':
  209. if (s->frst->nxt->n->ntyp == DO)
  210. { fprintf(fd, "if (!(");
  211. if (!CollectGuards(fd, s->frst->nxt, 0))
  212. fprintf(fd, "1");
  213. fprintf(fd, "))\n\t\t\tcontinue;");
  214. isg = 1;
  215. }
  216. break;
  217. case 'R': /* <- can't really happen (it's part of a 'c') */
  218. fprintf(fd, "if (!("); TestOnly=1;
  219. putstmnt(fd, s->frst->n, s->frst->seqno);
  220. fprintf(fd, "))\n\t\t\tcontinue;"); TestOnly=0;
  221. break;
  222. case 'r':
  223. fprintf(fd, "if (!("); TestOnly=1;
  224. putstmnt(fd, s->frst->n, s->frst->seqno);
  225. fprintf(fd, "))\n\t\t\tcontinue;"); TestOnly=0;
  226. break;
  227. case 's':
  228. fprintf(fd, "if (");
  229. #if 1
  230. /* 4.2.1 */ if (Pid != claimnr) fprintf(fd, "(boq != -1) || ");
  231. #endif
  232. fprintf(fd, "!("); TestOnly=1;
  233. putstmnt(fd, s->frst->n, s->frst->seqno);
  234. fprintf(fd, "))\n\t\t\tcontinue;"); TestOnly=0;
  235. break;
  236. case 'c':
  237. fprintf(fd, "if (!(");
  238. if (Pid != claimnr) fprintf(fd, "boq == -1 && ");
  239. TestOnly=1;
  240. putstmnt(fd, s->frst->n->lft, s->frst->seqno);
  241. fprintf(fd, "))\n\t\t\tcontinue;"); TestOnly=0;
  242. break;
  243. case ELSE:
  244. fprintf(fd, "if (boq != -1 || (");
  245. if (separate != 2) fprintf(fd, "trpt->");
  246. fprintf(fd, "o_pm&1))\n\t\t\tcontinue;");
  247. break;
  248. case ASGN: /* new 3.0.8 */
  249. fprintf(fd, "IfNotBlocked");
  250. break;
  251. }
  252. if (justguards) return 0;
  253. fprintf(fd, "\n\t\tsv_save();\n\t\t");
  254. #if 1
  255. fprintf(fd, "reached[%d][%d] = 1;\n\t\t", Pid, seqno);
  256. fprintf(fd, "reached[%d][t->st] = 1;\n\t\t", Pid); /* true next state */
  257. fprintf(fd, "reached[%d][tt] = 1;\n", Pid); /* true current state */
  258. #endif
  259. sprintf(buf, "Uerror(\"block in d_step seq, line %d\")", ln);
  260. NextLab[0] = buf;
  261. putCode(fd, s->frst, s->extent, nxt, isg);
  262. if (nxt)
  263. { extern Symbol *Fname;
  264. extern int lineno;
  265. if (FirstTime(nxt->Seqno)
  266. && (!(nxt->status & DONE2) || !(nxt->status & D_ATOM)))
  267. { fprintf(fd, "S_%.3d_0: /* 1 */\n", nxt->Seqno);
  268. nxt->status |= DONE2;
  269. LastGoto = 0;
  270. }
  271. Sourced(nxt->Seqno, 1);
  272. lineno = ln;
  273. Fname = nxt->n->fn;
  274. Mopup(fd);
  275. }
  276. unskip(s->frst->seqno);
  277. return LastGoto;
  278. }
  279. static void
  280. putCode(FILE *fd, Element *f, Element *last, Element *next, int isguard)
  281. { Element *e, *N;
  282. SeqList *h; int i;
  283. char NextOpt[64];
  284. static int bno = 0;
  285. for (e = f; e; e = e->nxt)
  286. { if (e->status & DONE2)
  287. continue;
  288. e->status |= DONE2;
  289. if (!(e->status & D_ATOM))
  290. { if (!LastGoto)
  291. { fprintf(fd, "\t\tgoto S_%.3d_0;\n",
  292. e->Seqno);
  293. Dested(e->Seqno);
  294. }
  295. break;
  296. }
  297. fprintf(fd, "S_%.3d_0: /* 2 */\n", e->Seqno);
  298. LastGoto = 0;
  299. Sourced(e->Seqno, 0);
  300. if (!e->sub)
  301. { filterbad(e);
  302. switch (e->n->ntyp) {
  303. case NON_ATOMIC:
  304. h = e->n->sl;
  305. putCode(fd, h->this->frst,
  306. h->this->extent, e->nxt, 0);
  307. break;
  308. case BREAK:
  309. if (LastGoto) break;
  310. if (e->nxt)
  311. { i = target( huntele(e->nxt,
  312. e->status, -1))->Seqno;
  313. fprintf(fd, "\t\tgoto S_%.3d_0; ", i);
  314. fprintf(fd, "/* 'break' */\n");
  315. Dested(i);
  316. } else
  317. { if (next)
  318. { fprintf(fd, "\t\tgoto S_%.3d_0;",
  319. next->Seqno);
  320. fprintf(fd, " /* NEXT */\n");
  321. Dested(next->Seqno);
  322. } else
  323. fatal("cannot interpret d_step", 0);
  324. }
  325. break;
  326. case GOTO:
  327. if (LastGoto) break;
  328. i = huntele( get_lab(e->n,1),
  329. e->status, -1)->Seqno;
  330. fprintf(fd, "\t\tgoto S_%.3d_0; ", i);
  331. fprintf(fd, "/* 'goto' */\n");
  332. Dested(i);
  333. break;
  334. case '.':
  335. if (LastGoto) break;
  336. if (e->nxt && (e->nxt->status & DONE2))
  337. { i = e->nxt?e->nxt->Seqno:0;
  338. fprintf(fd, "\t\tgoto S_%.3d_0;", i);
  339. fprintf(fd, " /* '.' */\n");
  340. Dested(i);
  341. }
  342. break;
  343. default:
  344. putskip(e->seqno);
  345. GenCode = 1; IsGuard = isguard;
  346. fprintf(fd, "\t\t");
  347. putstmnt(fd, e->n, e->seqno);
  348. fprintf(fd, ";\n");
  349. GenCode = IsGuard = isguard = LastGoto = 0;
  350. break;
  351. }
  352. i = e->nxt?e->nxt->Seqno:0;
  353. if (e->nxt && e->nxt->status & DONE2 && !LastGoto)
  354. { fprintf(fd, "\t\tgoto S_%.3d_0; ", i);
  355. fprintf(fd, "/* ';' */\n");
  356. Dested(i);
  357. break;
  358. }
  359. } else
  360. { for (h = e->sub, i=1; h; h = h->nxt, i++)
  361. { sprintf(NextOpt, "goto S_%.3d_%d",
  362. e->Seqno, i);
  363. NextLab[++Level] = NextOpt;
  364. N = (e->n->ntyp == DO) ? e : e->nxt;
  365. putCode(fd, h->this->frst,
  366. h->this->extent, N, 1);
  367. Level--;
  368. fprintf(fd, "%s: /* 3 */\n", &NextOpt[5]);
  369. LastGoto = 0;
  370. }
  371. if (!LastGoto)
  372. { fprintf(fd, "\t\tUerror(\"blocking sel ");
  373. fprintf(fd, "in d_step (nr.%d, near line %d)\");\n",
  374. bno++, (e->n)?e->n->ln:0);
  375. LastGoto = 0;
  376. }
  377. }
  378. if (e == last)
  379. { if (!LastGoto && next)
  380. { fprintf(fd, "\t\tgoto S_%.3d_0;\n",
  381. next->Seqno);
  382. Dested(next->Seqno);
  383. }
  384. break;
  385. } }
  386. }