dstep.c 9.8 KB

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