pangen4.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /***** spin: pangen4.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. extern FILE *tc, *tb;
  17. extern Queue *qtab;
  18. extern Symbol *Fname;
  19. extern int lineno, m_loss, Pid, eventmapnr, multi_oval;
  20. extern short nocast, has_provided, has_sorted;
  21. extern char *R13[], *R14[], *R15[];
  22. static void check_proc(Lextok *, int);
  23. void
  24. undostmnt(Lextok *now, int m)
  25. { Lextok *v;
  26. int i, j;
  27. if (!now)
  28. { fprintf(tb, "0");
  29. return;
  30. }
  31. lineno = now->ln;
  32. Fname = now->fn;
  33. switch (now->ntyp) {
  34. case CONST: case '!': case UMIN:
  35. case '~': case '/': case '*':
  36. case '-': case '+': case '%':
  37. case LT: case GT: case '&':
  38. case '|': case LE: case GE:
  39. case NE: case EQ: case OR:
  40. case AND: case LSHIFT: case RSHIFT:
  41. case TIMEOUT: case LEN: case NAME:
  42. case FULL: case EMPTY: case 'R':
  43. case NFULL: case NEMPTY: case ENABLED:
  44. case '?': case PC_VAL: case '^':
  45. case C_EXPR:
  46. case NONPROGRESS:
  47. putstmnt(tb, now, m);
  48. break;
  49. case RUN:
  50. fprintf(tb, "delproc(0, now._nr_pr-1)");
  51. break;
  52. case 's':
  53. if (Pid == eventmapnr) break;
  54. if (m_loss)
  55. fprintf(tb, "if (_m == 2) ");
  56. putname(tb, "_m = unsend(", now->lft, m, ")");
  57. break;
  58. case 'r':
  59. if (Pid == eventmapnr) break;
  60. for (v = now->rgt, i=j=0; v; v = v->rgt, i++)
  61. if (v->lft->ntyp != CONST
  62. && v->lft->ntyp != EVAL)
  63. j++;
  64. if (j == 0 && now->val >= 2)
  65. break; /* poll without side-effect */
  66. { int ii = 0, jj;
  67. for (v = now->rgt; v; v = v->rgt)
  68. if ((v->lft->ntyp != CONST
  69. && v->lft->ntyp != EVAL))
  70. ii++; /* nr of things bupped */
  71. if (now->val == 1)
  72. { ii++;
  73. jj = multi_oval - ii - 1;
  74. fprintf(tb, "XX = trpt->bup.oval");
  75. if (multi_oval > 0)
  76. { fprintf(tb, "s[%d]", jj);
  77. jj++;
  78. }
  79. fprintf(tb, ";\n\t\t");
  80. } else
  81. { fprintf(tb, "XX = 1;\n\t\t");
  82. jj = multi_oval - ii - 1;
  83. }
  84. if (now->val < 2) /* not for channel poll */
  85. for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  86. { switch(v->lft->ntyp) {
  87. case CONST:
  88. case EVAL:
  89. fprintf(tb, "unrecv");
  90. putname(tb, "(", now->lft, m, ", XX-1, ");
  91. fprintf(tb, "%d, ", i);
  92. if (v->lft->ntyp == EVAL)
  93. undostmnt(v->lft->lft, m);
  94. else
  95. undostmnt(v->lft, m);
  96. fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
  97. break;
  98. default:
  99. fprintf(tb, "unrecv");
  100. putname(tb, "(", now->lft, m, ", XX-1, ");
  101. fprintf(tb, "%d, ", i);
  102. if (v->lft->sym
  103. && !strcmp(v->lft->sym->name, "_"))
  104. { fprintf(tb, "trpt->bup.oval");
  105. if (multi_oval > 0)
  106. fprintf(tb, "s[%d]", jj);
  107. } else
  108. putstmnt(tb, v->lft, m);
  109. fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
  110. if (multi_oval > 0)
  111. jj++;
  112. break;
  113. } }
  114. jj = multi_oval - ii - 1;
  115. if (now->val == 1 && multi_oval > 0)
  116. jj++; /* new 3.4.0 */
  117. for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  118. { switch(v->lft->ntyp) {
  119. case CONST:
  120. case EVAL:
  121. break;
  122. default:
  123. if (!v->lft->sym
  124. || strcmp(v->lft->sym->name, "_") != 0)
  125. { nocast=1; putstmnt(tb,v->lft,m);
  126. nocast=0; fprintf(tb, " = trpt->bup.oval");
  127. if (multi_oval > 0)
  128. fprintf(tb, "s[%d]", jj);
  129. fprintf(tb, ";\n\t\t");
  130. }
  131. if (multi_oval > 0)
  132. jj++;
  133. break;
  134. } }
  135. multi_oval -= ii;
  136. }
  137. break;
  138. case '@':
  139. fprintf(tb, "p_restor(II);\n\t\t");
  140. break;
  141. case ASGN:
  142. nocast=1; putstmnt(tb,now->lft,m);
  143. nocast=0; fprintf(tb, " = trpt->bup.oval");
  144. if (multi_oval > 0)
  145. { multi_oval--;
  146. fprintf(tb, "s[%d]", multi_oval-1);
  147. }
  148. check_proc(now->rgt, m);
  149. break;
  150. case 'c':
  151. check_proc(now->lft, m);
  152. break;
  153. case '.':
  154. case GOTO:
  155. case ELSE:
  156. case BREAK:
  157. break;
  158. case C_CODE:
  159. fprintf(tb, "sv_restor();\n");
  160. break;
  161. case ASSERT:
  162. case PRINT:
  163. check_proc(now, m);
  164. break;
  165. case PRINTM:
  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 PRINTM:
  181. case '.':
  182. case GOTO:
  183. case ELSE:
  184. case BREAK: return 0;
  185. default: return 1;
  186. }
  187. }
  188. int
  189. any_oper(Lextok *now, int oper)
  190. { /* check if an expression contains oper operator */
  191. if (!now) return 0;
  192. if (now->ntyp == oper)
  193. return 1;
  194. return (any_oper(now->lft, oper) || any_oper(now->rgt, oper));
  195. }
  196. static void
  197. check_proc(Lextok *now, int m)
  198. {
  199. if (!now)
  200. return;
  201. if (now->ntyp == '@' || now->ntyp == RUN)
  202. { fprintf(tb, ";\n\t\t");
  203. undostmnt(now, m);
  204. }
  205. check_proc(now->lft, m);
  206. check_proc(now->rgt, m);
  207. }
  208. void
  209. genunio(void)
  210. { char buf1[256];
  211. Queue *q; int i;
  212. ntimes(tc, 0, 1, R13);
  213. for (q = qtab; q; q = q->nxt)
  214. { fprintf(tc, "\tcase %d:\n", q->qid);
  215. if (has_sorted)
  216. { sprintf(buf1, "((Q%d *)z)->contents", q->qid);
  217. fprintf(tc, "#ifdef HAS_SORTED\n");
  218. fprintf(tc, "\t\tj = trpt->ipt;\n"); /* ipt was bup.oval */
  219. fprintf(tc, "#endif\n");
  220. fprintf(tc, "\t\tfor (k = j; k < ((Q%d *)z)->Qlen; k++)\n",
  221. q->qid);
  222. fprintf(tc, "\t\t{\n");
  223. for (i = 0; i < q->nflds; i++)
  224. fprintf(tc, "\t\t\t%s[k].fld%d = %s[k+1].fld%d;\n",
  225. buf1, i, buf1, i);
  226. fprintf(tc, "\t\t}\n");
  227. fprintf(tc, "\t\tj = ((Q0 *)z)->Qlen;\n");
  228. }
  229. sprintf(buf1, "((Q%d *)z)->contents[j].fld", q->qid);
  230. for (i = 0; i < q->nflds; i++)
  231. fprintf(tc, "\t\t%s%d = 0;\n", buf1, i);
  232. if (q->nslots==0)
  233. { /* check if rendezvous succeeded, 1 level down */
  234. fprintf(tc, "\t\t_m = (trpt+1)->o_m;\n");
  235. fprintf(tc, "\t\tif (_m) (trpt-1)->o_pm |= 1;\n");
  236. fprintf(tc, "\t\tUnBlock;\n");
  237. } else
  238. fprintf(tc, "\t\t_m = trpt->o_m;\n");
  239. fprintf(tc, "\t\tbreak;\n");
  240. }
  241. ntimes(tc, 0, 1, R14);
  242. for (q = qtab; q; q = q->nxt)
  243. { sprintf(buf1, "((Q%d *)z)->contents", q->qid);
  244. fprintf(tc, " case %d:\n", q->qid);
  245. if (q->nslots == 0)
  246. fprintf(tc, "\t\tif (strt) boq = from+1;\n");
  247. else if (q->nslots > 1) /* shift */
  248. { fprintf(tc, "\t\tif (strt && slot<%d)\n",
  249. q->nslots-1);
  250. fprintf(tc, "\t\t{\tfor (j--; j>=slot; j--)\n");
  251. fprintf(tc, "\t\t\t{");
  252. for (i = 0; i < q->nflds; i++)
  253. { fprintf(tc, "\t%s[j+1].fld%d =\n\t\t\t",
  254. buf1, i);
  255. fprintf(tc, "\t%s[j].fld%d;\n\t\t\t",
  256. buf1, i);
  257. }
  258. fprintf(tc, "}\n\t\t}\n");
  259. }
  260. strcat(buf1, "[slot].fld");
  261. fprintf(tc, "\t\tif (strt) {\n");
  262. for (i = 0; i < q->nflds; i++)
  263. fprintf(tc, "\t\t\t%s%d = 0;\n", buf1, i);
  264. fprintf(tc, "\t\t}\n");
  265. if (q->nflds == 1) /* set */
  266. fprintf(tc, "\t\tif (fld == 0) %s0 = fldvar;\n",
  267. buf1);
  268. else
  269. { fprintf(tc, "\t\tswitch (fld) {\n");
  270. for (i = 0; i < q->nflds; i++)
  271. { fprintf(tc, "\t\tcase %d:\t%s", i, buf1);
  272. fprintf(tc, "%d = fldvar; break;\n", i);
  273. }
  274. fprintf(tc, "\t\t}\n");
  275. }
  276. fprintf(tc, "\t\tbreak;\n");
  277. }
  278. ntimes(tc, 0, 1, R15);
  279. }
  280. int
  281. proper_enabler(Lextok *n)
  282. {
  283. if (!n) return 1;
  284. switch (n->ntyp) {
  285. case NEMPTY: case FULL:
  286. case NFULL: case EMPTY:
  287. case LEN: case 'R':
  288. case NAME:
  289. has_provided = 1;
  290. if (strcmp(n->sym->name, "_pid") == 0)
  291. return 1;
  292. return (!(n->sym->context));
  293. case CONST: case TIMEOUT:
  294. has_provided = 1;
  295. return 1;
  296. case ENABLED: case PC_VAL:
  297. return proper_enabler(n->lft);
  298. case '!': case UMIN: case '~':
  299. return proper_enabler(n->lft);
  300. case '/': case '*': case '-': case '+':
  301. case '%': case LT: case GT: case '&': case '^':
  302. case '|': case LE: case GE: case NE: case '?':
  303. case EQ: case OR: case AND: case LSHIFT:
  304. case RSHIFT: case 'c':
  305. return proper_enabler(n->lft) && proper_enabler(n->rgt);
  306. default:
  307. break;
  308. }
  309. return 0;
  310. }