-
Notifications
You must be signed in to change notification settings - Fork 0
/
pangen4.c
358 lines (316 loc) · 7.93 KB
/
pangen4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/***** spin: pangen4.c *****/
/*
* This file is part of the public release of Spin. It is subject to the
* terms in the LICENSE file that is included in this source directory.
* Tool documentation is available at http://spinroot.com
*/
#include "spin.h"
#include "y.tab.h"
extern FILE *tc, *tb;
extern Queue *qtab;
extern Symbol *Fname;
extern int lineno, m_loss, Pid, eventmapnr, multi_oval;
extern short nocast, has_provided, has_sorted;
extern const char *R13[], *R14[], *R15[];
static void check_proc(Lextok *, int);
void
undostmnt(Lextok *now, int m)
{ Lextok *v;
int i, j;
if (!now)
{ fprintf(tb, "0");
return;
}
lineno = now->ln;
Fname = now->fn;
switch (now->ntyp) {
case CONST: case '!': case UMIN:
case '~': case '/': case '*':
case '-': case '+': case '%':
case LT: case GT: case '&':
case '|': case LE: case GE:
case NE: case EQ: case OR:
case AND: case LSHIFT: case RSHIFT:
case TIMEOUT: case LEN: case NAME:
case FULL: case EMPTY: case 'R':
case NFULL: case NEMPTY: case ENABLED:
case '?': case PC_VAL: case '^':
case C_EXPR: case GET_P:
case NONPROGRESS:
putstmnt(tb, now, m);
break;
case RUN:
fprintf(tb, "delproc(0, now._nr_pr-1)");
break;
case 's':
if (Pid == eventmapnr) break;
if (m_loss)
fprintf(tb, "if (_m == 2) ");
putname(tb, "_m = unsend(", now->lft, m, ")");
break;
case 'r':
if (Pid == eventmapnr) break;
for (v = now->rgt, i=j=0; v; v = v->rgt, i++)
if (v->lft->ntyp != CONST
&& v->lft->ntyp != EVAL)
j++;
if (j == 0 && now->val >= 2)
break; /* poll without side-effect */
{ int ii = 0, jj;
for (v = now->rgt; v; v = v->rgt)
if ((v->lft->ntyp != CONST
&& v->lft->ntyp != EVAL))
ii++; /* nr of things bupped */
if (now->val == 1)
{ ii++;
jj = multi_oval - ii - 1;
fprintf(tb, "XX = trpt->bup.oval");
if (multi_oval > 0)
{ fprintf(tb, "s[%d]", jj);
jj++;
}
fprintf(tb, ";\n\t\t");
} else
{ fprintf(tb, "XX = 1;\n\t\t");
jj = multi_oval - ii - 1;
}
if (now->val < 2) /* not for channel poll */
for (v = now->rgt, i = 0; v; v = v->rgt, i++)
{ switch(v->lft->ntyp) {
case CONST:
case EVAL:
fprintf(tb, "unrecv");
putname(tb, "(", now->lft, m, ", XX-1, ");
fprintf(tb, "%d, ", i);
if (v->lft->ntyp == EVAL)
undostmnt(v->lft->lft, m);
else
undostmnt(v->lft, m);
fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
break;
default:
fprintf(tb, "unrecv");
putname(tb, "(", now->lft, m, ", XX-1, ");
fprintf(tb, "%d, ", i);
if (v->lft->sym
&& !strcmp(v->lft->sym->name, "_"))
{ fprintf(tb, "trpt->bup.oval");
if (multi_oval > 0)
fprintf(tb, "s[%d]", jj);
} else
putstmnt(tb, v->lft, m);
fprintf(tb, ", %d);\n\t\t", (i==0)?1:0);
if (multi_oval > 0)
jj++;
break;
} }
jj = multi_oval - ii - 1;
if (now->val == 1 && multi_oval > 0)
jj++; /* new 3.4.0 */
for (v = now->rgt, i = 0; v; v = v->rgt, i++)
{ switch(v->lft->ntyp) {
case CONST:
case EVAL:
break;
default:
if (!v->lft->sym
|| strcmp(v->lft->sym->name, "_") != 0)
{ nocast=1; putstmnt(tb,v->lft,m);
nocast=0; fprintf(tb, " = trpt->bup.oval");
if (multi_oval > 0)
fprintf(tb, "s[%d]", jj);
fprintf(tb, ";\n\t\t");
}
if (multi_oval > 0)
jj++;
break;
} }
multi_oval -= ii;
}
break;
case '@':
fprintf(tb, "p_restor(II);\n\t\t");
break;
case SET_P:
fprintf(tb, "((P0 *)pptr((trpt->o_priority >> 8)))");
fprintf(tb, "->_priority = trpt->o_priority & 255");
break;
case ASGN:
if (check_track(now) == STRUCT) { break; }
nocast=1; putstmnt(tb,now->lft,m);
nocast=0; fprintf(tb, " = trpt->bup.oval");
if (multi_oval > 0)
{ multi_oval--;
fprintf(tb, "s[%d]", multi_oval-1);
}
check_proc(now->rgt, m);
break;
case 'c':
check_proc(now->lft, m);
break;
case '.':
case GOTO:
case ELSE:
case BREAK:
break;
case C_CODE:
fprintf(tb, "sv_restor();\n");
break;
case ASSERT:
case PRINT:
check_proc(now, m);
break;
case PRINTM:
break;
default:
printf("spin: bad node type %d (.b)\n", now->ntyp);
alldone(1);
}
}
int
any_undo(Lextok *now)
{ /* is there anything to undo on a return move? */
if (!now) return 1;
switch (now->ntyp) {
case 'c': return any_oper(now->lft, RUN);
case ASSERT:
case PRINT: return any_oper(now, RUN);
case PRINTM:
case '.':
case GOTO:
case ELSE:
case BREAK: return 0;
default: return 1;
}
}
int
any_oper(Lextok *now, int oper)
{ /* check if an expression contains oper operator */
if (!now) return 0;
if (now->ntyp == oper)
return 1;
return (any_oper(now->lft, oper) || any_oper(now->rgt, oper));
}
static void
check_proc(Lextok *now, int m)
{
if (!now)
return;
if (now->ntyp == '@' || now->ntyp == RUN)
{ fprintf(tb, ";\n\t\t");
undostmnt(now, m);
}
check_proc(now->lft, m);
check_proc(now->rgt, m);
}
void
genunio(void)
{ char buf1[256];
Queue *q; int i;
ntimes(tc, 0, 1, R13);
for (q = qtab; q; q = q->nxt)
{ fprintf(tc, "\tcase %d:\n", q->qid);
if (has_sorted)
{ sprintf(buf1, "((Q%d *)z)->contents", q->qid);
fprintf(tc, "#ifdef HAS_SORTED\n");
fprintf(tc, "\t\tj = trpt->ipt;\n"); /* ipt was bup.oval */
fprintf(tc, "#endif\n");
fprintf(tc, "\t\tfor (k = j; k < ((Q%d *)z)->Qlen; k++)\n",
q->qid);
fprintf(tc, "\t\t{\n");
for (i = 0; i < q->nflds; i++)
fprintf(tc, "\t\t\t%s[k].fld%d = %s[k+1].fld%d;\n",
buf1, i, buf1, i);
fprintf(tc, "\t\t}\n");
fprintf(tc, "\t\tj = ((Q0 *)z)->Qlen;\n");
}
sprintf(buf1, "((Q%d *)z)->contents[j].fld", q->qid);
for (i = 0; i < q->nflds; i++)
fprintf(tc, "\t\t%s%d = 0;\n", buf1, i);
if (q->nslots==0)
{ /* check if rendezvous succeeded, 1 level down */
fprintf(tc, "\t\t_m = (trpt+1)->o_m;\n");
fprintf(tc, "\t\tif (_m) (trpt-1)->o_pm |= 1;\n");
fprintf(tc, "\t\tUnBlock;\n");
} else
fprintf(tc, "\t\t_m = trpt->o_m;\n");
fprintf(tc, "\t\tbreak;\n");
}
ntimes(tc, 0, 1, R14);
for (q = qtab; q; q = q->nxt)
{ sprintf(buf1, "((Q%d *)z)->contents", q->qid);
fprintf(tc, " case %d:\n", q->qid);
if (q->nslots == 0)
fprintf(tc, "\t\tif (strt) boq = from+1;\n");
else if (q->nslots > 1) /* shift */
{ fprintf(tc, "\t\tif (strt && slot<%d)\n",
q->nslots-1);
fprintf(tc, "\t\t{\tfor (j--; j>=slot; j--)\n");
fprintf(tc, "\t\t\t{");
for (i = 0; i < q->nflds; i++)
{ fprintf(tc, "\t%s[j+1].fld%d =\n\t\t\t",
buf1, i);
fprintf(tc, "\t%s[j].fld%d;\n\t\t\t",
buf1, i);
}
fprintf(tc, "}\n\t\t}\n");
}
strcat(buf1, "[slot].fld");
fprintf(tc, "\t\tif (strt) {\n");
for (i = 0; i < q->nflds; i++)
fprintf(tc, "\t\t\t%s%d = 0;\n", buf1, i);
fprintf(tc, "\t\t}\n");
if (q->nflds == 1) /* set */
fprintf(tc, "\t\tif (fld == 0) %s0 = fldvar;\n",
buf1);
else
{ fprintf(tc, "\t\tswitch (fld) {\n");
for (i = 0; i < q->nflds; i++)
{ fprintf(tc, "\t\tcase %d:\t%s", i, buf1);
fprintf(tc, "%d = fldvar; break;\n", i);
}
fprintf(tc, "\t\t}\n");
}
fprintf(tc, "\t\tbreak;\n");
}
ntimes(tc, 0, 1, R15);
}
extern void explain(int);
int
proper_enabler(Lextok *n)
{
if (!n) return 1;
switch (n->ntyp) {
case NEMPTY: case FULL:
case NFULL: case EMPTY:
case LEN: case 'R':
case NAME:
has_provided = 1;
if (strcmp(n->sym->name, "_pid") == 0
|| strcmp(n->sym->name, "_priority") == 0)
return 1;
return (!(n->sym->context));
case C_EXPR:
case CONST:
case TIMEOUT:
has_provided = 1;
return 1;
case ENABLED: case PC_VAL:
case GET_P: /* not SET_P */
return proper_enabler(n->lft);
case '!': case UMIN: case '~':
return proper_enabler(n->lft);
case '/': case '*': case '-': case '+':
case '%': case LT: case GT: case '&': case '^':
case '|': case LE: case GE: case NE: case '?':
case EQ: case OR: case AND: case LSHIFT:
case RSHIFT: case 'c': /* case ',': */
return proper_enabler(n->lft) && proper_enabler(n->rgt);
default:
break;
}
printf("spin: saw ");
explain(n->ntyp);
printf("\n");
return 0;
}