1 /***** spin: pangen4.c *****/
4 * This file is part of the public release of Spin. It is subject to the
5 * terms in the LICENSE file that is included in this source directory.
6 * Tool documentation is available at http://spinroot.com
15 extern int lineno, m_loss, Pid, eventmapnr, multi_oval;
16 extern short nocast, has_provided, has_sorted;
17 extern const char *R13[], *R14[], *R15[];
19 static void check_proc(Lextok *, int);
22 undostmnt(Lextok *now, int m)
33 case CONST: case '!': case UMIN:
34 case '~': case '/': case '*':
35 case '-': case '+': case '%':
36 case LT: case GT: case '&':
37 case '|': case LE: case GE:
38 case NE: case EQ: case OR:
39 case AND: case LSHIFT: case RSHIFT:
40 case TIMEOUT: case LEN: case NAME:
41 case FULL: case EMPTY: case 'R':
42 case NFULL: case NEMPTY: case ENABLED:
43 case '?': case PC_VAL: case '^':
44 case C_EXPR: case GET_P:
50 fprintf(tb, "delproc(0, now._nr_pr-1)");
54 if (Pid == eventmapnr) break;
57 fprintf(tb, "if (_m == 2) ");
58 putname(tb, "_m = unsend(", now->lft, m, ")");
62 if (Pid == eventmapnr) break;
64 for (v = now->rgt, i=j=0; v; v = v->rgt, i++)
65 if (v->lft->ntyp != CONST
66 && v->lft->ntyp != EVAL)
68 if (j == 0 && now->val >= 2)
69 break; /* poll without side-effect */
73 for (v = now->rgt; v; v = v->rgt)
74 if ((v->lft->ntyp != CONST
75 && v->lft->ntyp != EVAL))
76 ii++; /* nr of things bupped */
79 jj = multi_oval - ii - 1;
80 fprintf(tb, "XX = trpt->bup.oval");
82 { fprintf(tb, "s[%d]", jj);
85 fprintf(tb, ";\n\t\t");
87 { fprintf(tb, "XX = 1;\n\t\t");
88 jj = multi_oval - ii - 1;
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) {
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);
102 undostmnt(v->lft, m);
103 fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
106 fprintf(tb, "unrecv");
107 putname(tb, "(", now->lft, m, ", XX-1, ");
108 fprintf(tb, "%d, ", i);
110 && !strcmp(v->lft->sym->name, "_"))
111 { fprintf(tb, "trpt->bup.oval");
113 fprintf(tb, "s[%d]", jj);
115 putstmnt(tb, v->lft, m);
117 fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
122 jj = multi_oval - ii - 1;
124 if (now->val == 1 && multi_oval > 0)
125 jj++; /* new 3.4.0 */
127 for (v = now->rgt, i = 0; v; v = v->rgt, i++)
128 { switch(v->lft->ntyp) {
134 || strcmp(v->lft->sym->name, "_") != 0)
135 { nocast=1; putstmnt(tb,v->lft,m);
136 nocast=0; fprintf(tb, " = trpt->bup.oval");
138 fprintf(tb, "s[%d]", jj);
139 fprintf(tb, ";\n\t\t");
150 fprintf(tb, "p_restor(II);\n\t\t");
154 fprintf(tb, "((P0 *)pptr((trpt->o_priority >> 8)))");
155 fprintf(tb, "->_priority = trpt->o_priority & 255");
159 if (check_track(now) == STRUCT) { break; }
161 nocast=1; putstmnt(tb,now->lft,m);
162 nocast=0; fprintf(tb, " = trpt->bup.oval");
165 fprintf(tb, "s[%d]", multi_oval-1);
167 check_proc(now->rgt, m);
171 check_proc(now->lft, m);
181 fprintf(tb, "sv_restor();\n");
192 printf("spin: bad node type %d (.b)\n", now->ntyp);
198 any_undo(Lextok *now)
199 { /* is there anything to undo on a return move? */
202 case 'c': return any_oper(now->lft, RUN);
204 case PRINT: return any_oper(now, RUN);
210 case BREAK: return 0;
216 any_oper(Lextok *now, int oper)
217 { /* check if an expression contains oper operator */
219 if (now->ntyp == oper)
221 return (any_oper(now->lft, oper) || any_oper(now->rgt, oper));
225 check_proc(Lextok *now, int m)
229 if (now->ntyp == '@' || now->ntyp == RUN)
230 { fprintf(tb, ";\n\t\t");
233 check_proc(now->lft, m);
234 check_proc(now->rgt, m);
242 ntimes(tc, 0, 1, R13);
243 for (q = qtab; q; q = q->nxt)
244 { fprintf(tc, "\tcase %d:\n", q->qid);
247 { sprintf(buf1, "((Q%d *)z)->contents", q->qid);
248 fprintf(tc, "#ifdef HAS_SORTED\n");
249 fprintf(tc, "\t\tj = trpt->ipt;\n"); /* ipt was bup.oval */
250 fprintf(tc, "#endif\n");
251 fprintf(tc, "\t\tfor (k = j; k < ((Q%d *)z)->Qlen; k++)\n",
253 fprintf(tc, "\t\t{\n");
254 for (i = 0; i < q->nflds; i++)
255 fprintf(tc, "\t\t\t%s[k].fld%d = %s[k+1].fld%d;\n",
257 fprintf(tc, "\t\t}\n");
258 fprintf(tc, "\t\tj = ((Q0 *)z)->Qlen;\n");
261 sprintf(buf1, "((Q%d *)z)->contents[j].fld", q->qid);
262 for (i = 0; i < q->nflds; i++)
263 fprintf(tc, "\t\t%s%d = 0;\n", buf1, i);
265 { /* check if rendezvous succeeded, 1 level down */
266 fprintf(tc, "\t\t_m = (trpt+1)->o_m;\n");
267 fprintf(tc, "\t\tif (_m) (trpt-1)->o_pm |= 1;\n");
268 fprintf(tc, "\t\tUnBlock;\n");
270 fprintf(tc, "\t\t_m = trpt->o_m;\n");
272 fprintf(tc, "\t\tbreak;\n");
274 ntimes(tc, 0, 1, R14);
275 for (q = qtab; q; q = q->nxt)
276 { sprintf(buf1, "((Q%d *)z)->contents", q->qid);
277 fprintf(tc, " case %d:\n", q->qid);
279 fprintf(tc, "\t\tif (strt) boq = from+1;\n");
280 else if (q->nslots > 1) /* shift */
281 { fprintf(tc, "\t\tif (strt && slot<%d)\n",
283 fprintf(tc, "\t\t{\tfor (j--; j>=slot; j--)\n");
284 fprintf(tc, "\t\t\t{");
285 for (i = 0; i < q->nflds; i++)
286 { fprintf(tc, "\t%s[j+1].fld%d =\n\t\t\t",
288 fprintf(tc, "\t%s[j].fld%d;\n\t\t\t",
291 fprintf(tc, "}\n\t\t}\n");
293 strcat(buf1, "[slot].fld");
294 fprintf(tc, "\t\tif (strt) {\n");
295 for (i = 0; i < q->nflds; i++)
296 fprintf(tc, "\t\t\t%s%d = 0;\n", buf1, i);
297 fprintf(tc, "\t\t}\n");
298 if (q->nflds == 1) /* set */
299 fprintf(tc, "\t\tif (fld == 0) %s0 = fldvar;\n",
302 { fprintf(tc, "\t\tswitch (fld) {\n");
303 for (i = 0; i < q->nflds; i++)
304 { fprintf(tc, "\t\tcase %d:\t%s", i, buf1);
305 fprintf(tc, "%d = fldvar; break;\n", i);
307 fprintf(tc, "\t\t}\n");
309 fprintf(tc, "\t\tbreak;\n");
311 ntimes(tc, 0, 1, R15);
314 extern void explain(int);
317 proper_enabler(Lextok *n)
321 case NEMPTY: case FULL:
322 case NFULL: case EMPTY:
326 if (strcmp(n->sym->name, "_pid") == 0
327 || strcmp(n->sym->name, "_priority") == 0)
329 return (!(n->sym->context));
337 case ENABLED: case PC_VAL:
338 case GET_P: /* not SET_P */
339 return proper_enabler(n->lft);
341 case '!': case UMIN: case '~':
342 return proper_enabler(n->lft);
344 case '/': case '*': case '-': case '+':
345 case '%': case LT: case GT: case '&': case '^':
346 case '|': case LE: case GE: case NE: case '?':
347 case EQ: case OR: case AND: case LSHIFT:
348 case RSHIFT: case 'c': /* case ',': */
349 return proper_enabler(n->lft) && proper_enabler(n->rgt);
354 printf("spin: saw ");