pangen4.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /***** spin: pangen4.c *****/
  2. /* Copyright (c) 1991-2000 by Lucent Technologies - Bell Laboratories */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* Permission is given to distribute this code provided that this intro- */
  5. /* ductory message is not removed and no monies are exchanged. */
  6. /* No guarantee is expressed or implied by the distribution of this code. */
  7. /* Software written by Gerard J. Holzmann as part of the book: */
  8. /* `Design and Validation of Computer Protocols,' ISBN 0-13-539925-4, */
  9. /* Prentice Hall, Englewood Cliffs, NJ, 07632. */
  10. /* Send bug-reports and/or questions to: gerard@research.bell-labs.com */
  11. #include "spin.h"
  12. #ifdef PC
  13. #include "y_tab.h"
  14. #else
  15. #include "y.tab.h"
  16. #endif
  17. extern FILE *tc, *tb;
  18. extern ProcList *cur_proc;
  19. extern Queue *qtab;
  20. extern Symbol *Fname;
  21. extern int lineno, m_loss, Pid, eventmapnr, verbose, multi_oval;
  22. extern short terse, nocast, has_provided, has_sorted;
  23. extern short evalindex, withprocname, _isok;
  24. extern char *R13[], *R14[], *R15[];
  25. static void check_proc(Lextok *, int);
  26. extern int dolocal(FILE *, char *, int, int, char *, char *);
  27. void
  28. undostmnt(Lextok *now, int m)
  29. { Lextok *v;
  30. int i, j;
  31. if (!now)
  32. { fprintf(tb, "0");
  33. return;
  34. }
  35. lineno = now->ln;
  36. Fname = now->fn;
  37. switch (now->ntyp) {
  38. case CONST: case '!': case UMIN:
  39. case '~': case '/': case '*':
  40. case '-': case '+': case '%':
  41. case LT: case GT: case '&':
  42. case '|': case LE: case GE:
  43. case NE: case EQ: case OR:
  44. case AND: case LSHIFT: case RSHIFT:
  45. case TIMEOUT: case LEN: case NAME:
  46. case FULL: case EMPTY: case 'R':
  47. case NFULL: case NEMPTY: case ENABLED:
  48. case '?': case PC_VAL: case '^':
  49. case NONPROGRESS:
  50. putstmnt(tb, now, m);
  51. break;
  52. case RUN:
  53. fprintf(tb, "delproc(0, now._nr_pr-1)");
  54. break;
  55. case 's':
  56. if (Pid == eventmapnr) break;
  57. if (m_loss)
  58. { fprintf(tb, "if (m == 2) m = unsend");
  59. putname(tb, "(", now->lft, m, ")");
  60. } else
  61. { fprintf(tb, "m = unsend");
  62. putname(tb, "(", now->lft, m, ")");
  63. }
  64. break;
  65. case 'r':
  66. if (Pid == eventmapnr) break;
  67. for (v = now->rgt, i=j=0; v; v = v->rgt, i++)
  68. if (v->lft->ntyp != CONST
  69. && v->lft->ntyp != EVAL)
  70. j++;
  71. if (j == 0 && now->val >= 2)
  72. break; /* poll without side-effect */
  73. { int ii = 0, jj;
  74. for (v = now->rgt; v; v = v->rgt)
  75. if ((v->lft->ntyp != CONST
  76. && v->lft->ntyp != EVAL))
  77. ii++; /* nr of things bupped */
  78. if (now->val == 1)
  79. { ii++;
  80. jj = multi_oval - ii - 1;
  81. fprintf(tb, "XX = trpt->bup.oval");
  82. if (multi_oval > 0)
  83. { fprintf(tb, "s[%d]", jj);
  84. jj++;
  85. }
  86. fprintf(tb, ";\n\t\t");
  87. } else
  88. { fprintf(tb, "XX = 1;\n\t\t");
  89. jj = multi_oval - ii - 1;
  90. }
  91. if (now->val < 2) /* not for channel poll */
  92. for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  93. { switch(v->lft->ntyp) {
  94. case CONST:
  95. case EVAL:
  96. fprintf(tb, "unrecv");
  97. putname(tb, "(", now->lft, m, ", XX-1, ");
  98. fprintf(tb, "%d, ", i);
  99. if (v->lft->ntyp == EVAL)
  100. undostmnt(v->lft->lft, m);
  101. else
  102. undostmnt(v->lft, m);
  103. fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
  104. break;
  105. default:
  106. fprintf(tb, "unrecv");
  107. putname(tb, "(", now->lft, m, ", XX-1, ");
  108. fprintf(tb, "%d, ", i);
  109. if (v->lft->sym
  110. && !strcmp(v->lft->sym->name, "_"))
  111. { fprintf(tb, "trpt->bup.oval");
  112. if (multi_oval > 0)
  113. fprintf(tb, "s[%d]", jj);
  114. } else
  115. putstmnt(tb, v->lft, m);
  116. fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
  117. if (multi_oval > 0)
  118. jj++;
  119. break;
  120. } }
  121. jj = multi_oval - ii - 1;
  122. for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  123. { switch(v->lft->ntyp) {
  124. case CONST:
  125. case EVAL:
  126. break;
  127. default:
  128. if (!v->lft->sym
  129. || strcmp(v->lft->sym->name, "_") != 0)
  130. { nocast=1; putstmnt(tb,v->lft,m);
  131. nocast=0; fprintf(tb, " = trpt->bup.oval");
  132. if (multi_oval > 0)
  133. fprintf(tb, "s[%d]", jj);
  134. fprintf(tb, ";\n\t\t");
  135. }
  136. if (multi_oval > 0)
  137. jj++;
  138. break;
  139. } }
  140. multi_oval -= ii;
  141. }
  142. break;
  143. case '@':
  144. fprintf(tb, "p_restor(II);\n\t\t");
  145. break;
  146. case ASGN:
  147. nocast=1; putstmnt(tb,now->lft,m);
  148. nocast=0; fprintf(tb, " = trpt->bup.oval");
  149. if (multi_oval > 0)
  150. { multi_oval--;
  151. fprintf(tb, "s[%d]", multi_oval-1);
  152. }
  153. check_proc(now->rgt, m);
  154. break;
  155. case 'c':
  156. check_proc(now->lft, m);
  157. break;
  158. case '.':
  159. case GOTO:
  160. case ELSE:
  161. case BREAK:
  162. break;
  163. case ASSERT:
  164. case PRINT:
  165. check_proc(now, m);
  166. break;
  167. default:
  168. printf("spin: bad node type %d (.b)\n", now->ntyp);
  169. alldone(1);
  170. }
  171. }
  172. int
  173. any_undo(Lextok *now)
  174. { /* is there anything to undo on a return move? */
  175. if (!now) return 1;
  176. switch (now->ntyp) {
  177. case 'c': return any_oper(now->lft, RUN);
  178. case ASSERT:
  179. case PRINT: return any_oper(now, RUN);
  180. case '.':
  181. case GOTO:
  182. case ELSE:
  183. case BREAK: return 0;
  184. default: return 1;
  185. }
  186. }
  187. int
  188. any_oper(Lextok *now, int oper)
  189. { /* check if an expression contains oper operator */
  190. if (!now) return 0;
  191. if (now->ntyp == oper)
  192. return 1;
  193. return (any_oper(now->lft, oper) || any_oper(now->rgt, oper));
  194. }
  195. static void
  196. check_proc(Lextok *now, int m)
  197. {
  198. if (!now)
  199. return;
  200. if (now->ntyp == '@' || now->ntyp == RUN)
  201. { fprintf(tb, ";\n\t\t");
  202. undostmnt(now, m);
  203. }
  204. check_proc(now->lft, m);
  205. check_proc(now->rgt, m);
  206. }
  207. void
  208. genunio(void)
  209. { char buf1[256];
  210. Queue *q; int i;
  211. ntimes(tc, 0, 1, R13);
  212. for (q = qtab; q; q = q->nxt)
  213. { fprintf(tc, "\tcase %d:\n", q->qid);
  214. if (has_sorted)
  215. { sprintf(buf1, "((Q%d *)z)->contents", q->qid);
  216. fprintf(tc, "\t\tj = trpt->bup.oval;\n");
  217. fprintf(tc, "\t\tfor (k = j; k < ((Q%d *)z)->Qlen; k++)\n",
  218. q->qid);
  219. fprintf(tc, "\t\t{\n");
  220. for (i = 0; i < q->nflds; i++)
  221. fprintf(tc, "\t\t\t%s[k].fld%d = %s[k+1].fld%d;\n",
  222. buf1, i, buf1, i);
  223. fprintf(tc, "\t\t}\n");
  224. fprintf(tc, "\t\tj = ((Q0 *)z)->Qlen;\n");
  225. }
  226. sprintf(buf1, "((Q%d *)z)->contents[j].fld", q->qid);
  227. for (i = 0; i < q->nflds; i++)
  228. fprintf(tc, "\t\t%s%d = 0;\n", buf1, i);
  229. if (q->nslots==0)
  230. { /* check if rendezvous succeeded, 1 level down */
  231. fprintf(tc, "\t\tm = (trpt+1)->o_m;\n");
  232. fprintf(tc, "\t\tif (m) (trpt-1)->o_pm |= 1;\n");
  233. fprintf(tc, "\t\tUnBlock;\n");
  234. } else
  235. fprintf(tc, "\t\tm = trpt->o_m;\n");
  236. fprintf(tc, "\t\tbreak;\n");
  237. }
  238. ntimes(tc, 0, 1, R14);
  239. for (q = qtab; q; q = q->nxt)
  240. { sprintf(buf1, "((Q%d *)z)->contents", q->qid);
  241. fprintf(tc, " case %d:\n", q->qid);
  242. if (q->nslots == 0)
  243. fprintf(tc, "\t\tif (strt) boq = from+1;\n");
  244. else if (q->nslots > 1) /* shift */
  245. { fprintf(tc, "\t\tif (strt && slot<%d)\n",
  246. q->nslots-1);
  247. fprintf(tc, "\t\t{\tfor (j--; j>=slot; j--)\n");
  248. fprintf(tc, "\t\t\t{");
  249. for (i = 0; i < q->nflds; i++)
  250. { fprintf(tc, "\t%s[j+1].fld%d =\n\t\t\t",
  251. buf1, i);
  252. fprintf(tc, "\t%s[j].fld%d;\n\t\t\t",
  253. buf1, i);
  254. }
  255. fprintf(tc, "}\n\t\t}\n");
  256. }
  257. strcat(buf1, "[slot].fld");
  258. fprintf(tc, "\t\tif (strt) {\n");
  259. for (i = 0; i < q->nflds; i++)
  260. fprintf(tc, "\t\t\t%s%d = 0;\n", buf1, i);
  261. fprintf(tc, "\t\t}\n");
  262. if (q->nflds == 1) /* set */
  263. fprintf(tc, "\t\tif (fld == 0) %s0 = fldvar;\n",
  264. buf1);
  265. else
  266. { fprintf(tc, "\t\tswitch (fld) {\n");
  267. for (i = 0; i < q->nflds; i++)
  268. { fprintf(tc, "\t\tcase %d:\t%s", i, buf1);
  269. fprintf(tc, "%d = fldvar; break;\n", i);
  270. }
  271. fprintf(tc, "\t\t}\n");
  272. }
  273. fprintf(tc, "\t\tbreak;\n");
  274. }
  275. ntimes(tc, 0, 1, R15);
  276. }
  277. int
  278. proper_enabler(Lextok *n)
  279. {
  280. if (!n) return 1;
  281. switch (n->ntyp) {
  282. case NEMPTY: case FULL:
  283. case NFULL: case EMPTY:
  284. case LEN: case 'R':
  285. case NAME:
  286. has_provided = 1;
  287. if (strcmp(n->sym->name, "_pid") == 0)
  288. return 1;
  289. return (!(n->sym->context));
  290. case CONST: case TIMEOUT:
  291. has_provided = 1;
  292. return 1;
  293. case ENABLED: case PC_VAL:
  294. return proper_enabler(n->lft);
  295. case '!': case UMIN: case '~':
  296. return proper_enabler(n->lft);
  297. case '/': case '*': case '-': case '+':
  298. case '%': case LT: case GT: case '&': case '^':
  299. case '|': case LE: case GE: case NE: case '?':
  300. case EQ: case OR: case AND: case LSHIFT:
  301. case RSHIFT: case 'c':
  302. return proper_enabler(n->lft) && proper_enabler(n->rgt);
  303. default:
  304. break;
  305. }
  306. return 0;
  307. }