summaryrefslogtreecommitdiff
path: root/sys/src/cmd/spin/pangen3.c
blob: d59b455ff4179716c41de9208306ff57677c63f6 (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/***** spin: pangen3.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"
#include <assert.h>

extern FILE	*th, *tc;
extern int	eventmapnr, old_priority_rules;

typedef struct SRC {
	int ln, st;	/* linenr, statenr */
	Symbol *fn;	/* filename */
	struct SRC *nxt;
} SRC;

static int	col;
static Symbol	*lastfnm;
static Symbol	lastdef;
static int	lastfrom;
static SRC	*frst = (SRC *) 0;
static SRC	*skip = (SRC *) 0;

extern int	ltl_mode;

extern void	sr_mesg(FILE *, int, int);

static void
putnr(int n)
{
	if (col++ == 8)
	{	fprintf(tc, "\n\t");	/* was th */
		col = 1;
	}
	fprintf(tc, "%3d, ", n);	/* was th */
}

static void
putfnm(int j, Symbol *s)
{
	if (lastfnm && lastfnm == s && j != -1)
		return;

	if (lastfnm)
		fprintf(tc, "{ \"%s\", %d, %d },\n\t",	/* was th */
			lastfnm->name,
			lastfrom,
			j-1);
	lastfnm = s;
	lastfrom = j;
}

static void
putfnm_flush(int j)
{
	if (lastfnm)
		fprintf(tc, "{ \"%s\", %d, %d }\n",	/* was th */
			lastfnm->name,
			lastfrom, j);
}

static SRC *
newsrc(int m, SRC *n)
{	SRC *tmp;
	tmp = (SRC *) emalloc(sizeof(SRC));
	tmp->st  = m;
	tmp->nxt = n;
	return tmp;
}

void
putskip(int m)	/* states that need not be reached */
{	SRC *tmp, *lst = (SRC *)0;
	/* 6.4.0: now an ordered list */
	for (tmp = skip; tmp; lst = tmp, tmp = tmp->nxt)
	{	if (tmp->st == m)
		{	return;
		}
		if (tmp->st > m)	/* insert before */
		{	if (tmp == skip)
			{	tmp = newsrc(m, skip);
				skip = tmp;
			} else
			{	assert(lst);
				tmp = newsrc(m, lst->nxt);
				lst->nxt = tmp;
			}
			return;	
	}	}
	/* insert at the end */
	if (lst)
	{	lst->nxt = newsrc(m, 0);
	} else	/* empty list */
	{	skip = newsrc(m, 0);
	}
}

void
unskip(int m)	/* a state that needs to be reached after all */
{	SRC *tmp, *lst = (SRC *)0;

	for (tmp = skip; tmp; lst = tmp, tmp = tmp->nxt)
	{	if (tmp->st == m)
		{	if (tmp == skip)
				skip = skip->nxt;
			else if (lst)	/* always true, but helps coverity */
				lst->nxt = tmp->nxt;
			break;
		}
		if (tmp->st > m)
		{	break;	/* m is not in list */
	}	}
}

void
putsrc(Element *e)	/* match states to source lines */
{	SRC *tmp, *lst = (SRC *)0;
	int n, m;

	if (!e || !e->n) return;

	n = e->n->ln;
	m = e->seqno;
	/* 6.4.0: now an ordered list */
	for (tmp = frst; tmp; lst = tmp, tmp = tmp->nxt)
	{	if (tmp->st == m)
		{	if (tmp->ln != n || tmp->fn != e->n->fn)
			printf("putsrc mismatch seqno %d, line %d - %d, file %s\n", m, n,
				tmp->ln, tmp->fn->name);
			return;
		}
		if (tmp->st > m) /* insert before */
		{	if (tmp == frst)
			{	tmp = newsrc(m, frst);
				frst = tmp;
			} else
			{	assert(lst);
				tmp = newsrc(m, lst->nxt);
				lst->nxt = tmp;
			}
			tmp->ln = n;
			tmp->fn = e->n->fn;
			return;
	}	}
	/* insert at the end */
	tmp = newsrc(m, lst?lst->nxt:0);
	tmp->ln = n;
	tmp->fn = e->n->fn;
	if (lst)
	{	lst->nxt = tmp;
	} else
	{	frst = tmp;
	}
}

static void
dumpskip(int n, int m)
{	SRC *tmp, *lst;
	FILE *tz = tc;	/* was th */
	int j;

	fprintf(tz, "uchar reached%d [] = {\n\t", m);
	tmp = skip;
	lst = (SRC *) 0;
	for (j = 0, col = 0; j <= n; j++)
	{	/* find j in the sorted list */
		for ( ; tmp; lst = tmp, tmp = tmp->nxt)
		{	if (tmp->st == j)
			{	putnr(1);
				if (lst)
					lst->nxt = tmp->nxt;
				else
					skip = tmp->nxt;
				break;
			}
			if (tmp->st > j)
			{	putnr(0);
				break;	/* j is not in the list */
		}	}

		if (!tmp)
		{	putnr(0);
	}	}
	fprintf(tz, "};\n");
	fprintf(tz, "uchar *loopstate%d;\n", m);

	if (m == eventmapnr)
		fprintf(th, "#define reached_event	reached%d\n", m);

	skip = (SRC *) 0;
}

void
dumpsrc(int n, int m)
{	SRC *tmp, *lst;
	int j;
	static int did_claim = 0;
	FILE *tz = tc;	/* was th */

	fprintf(tz, "\nshort src_ln%d [] = {\n\t", m);
	tmp = frst;
	for (j = 0, col = 0; j <= n; j++)
	{	for ( ; tmp; tmp = tmp->nxt)
		{	if (tmp->st == j)
			{	putnr(tmp->ln);
				break;
			}
			if (tmp->st > j)
			{	putnr(0);
				break;
		}	}
		if (!tmp)
		{	putnr(0);
	}	}
	fprintf(tz, "};\n");

	lastfnm = (Symbol *) 0;
	lastdef.name = "-";
	fprintf(tz, "S_F_MAP src_file%d [] = {\n\t", m);
	tmp = frst;
	lst = (SRC *) 0;
	for (j = 0, col = 0; j <= n; j++)
	{	for ( ; tmp; lst = tmp, tmp = tmp->nxt)
		{	if (tmp->st == j)
			{	putfnm(j, tmp->fn);
				if (lst)
					lst->nxt = tmp->nxt;
				else
					frst = tmp->nxt;
				break;
			}
			if (tmp->st > j)
			{	putfnm(j, &lastdef);
				break;
		}	}
		if (!tmp)
		{	putfnm(j, &lastdef);
	}	}
	putfnm_flush(j);
	fprintf(tz, "};\n");

	if (pid_is_claim(m) && !did_claim)
	{	fprintf(tz, "short *src_claim;\n");
		did_claim++;
	}
	if (m == eventmapnr)
		fprintf(th, "#define src_event	src_ln%d\n", m);

	frst = (SRC *) 0;
	dumpskip(n, m);
}

#define Cat0(x)   	comwork(fd,now->lft,m); fprintf(fd, x); \
			comwork(fd,now->rgt,m)
#define Cat1(x)		fprintf(fd,"("); Cat0(x); fprintf(fd,")")
#define Cat2(x,y)  	fprintf(fd,x); comwork(fd,y,m)
#define Cat3(x,y,z)	fprintf(fd,x); comwork(fd,y,m); fprintf(fd,z)

static int
symbolic(FILE *fd, Lextok *tv)
{	Lextok *n; extern Lextok *Mtype;
	int cnt = 1;

	if (tv->ismtyp)
	for (n = Mtype; n; n = n->rgt, cnt++)
		if (cnt == tv->val)
		{	fprintf(fd, "%s", n->lft->sym->name);
			return 1;
		}
	return 0;
}

static void
comwork(FILE *fd, Lextok *now, int m)
{	Lextok *v;
	int i, j;

	if (!now) { fprintf(fd, "0"); return; }
	switch (now->ntyp) {
	case CONST:	sr_mesg(fd, now->val, now->ismtyp); break;
	case '!':	Cat3("!(", now->lft, ")"); break;
	case UMIN:	Cat3("-(", now->lft, ")"); break;
	case '~':	Cat3("~(", now->lft, ")"); break;

	case '/':	Cat1("/");  break;
	case '*':	Cat1("*");  break;
	case '-':	Cat1("-");  break;
	case '+':	Cat1("+");  break;
	case '%':	Cat1("%%"); break;
	case '&':	Cat1("&");  break;
	case '^':	Cat1("^");  break;
	case '|':	Cat1("|");  break;
	case LE:	Cat1("<="); break;
	case GE:	Cat1(">="); break;
	case GT:	Cat1(">"); break;
	case LT:	Cat1("<"); break;
	case NE:	Cat1("!="); break;
	case EQ:
			if (ltl_mode
			&&  now->lft->ntyp == 'p'
			&&  now->rgt->ntyp == 'q')	/* remote ref */
			{	Lextok *p = now->lft->lft;

				fprintf(fd, "(");
				fprintf(fd, "%s", p->sym->name);
				if (p->lft)
				{	fprintf(fd, "[");
					putstmnt(fd, p->lft, 0); /* pid */
					fprintf(fd, "]");
				}
				fprintf(fd, "@");
				fprintf(fd, "%s", now->rgt->sym->name);
				fprintf(fd, ")");
				break;
			}
			Cat1("==");
			break;

	case OR:	Cat1("||"); break;
	case AND:	Cat1("&&"); break;
	case LSHIFT:	Cat1("<<"); break;
	case RSHIFT:	Cat1(">>"); break;

	case RUN:	fprintf(fd, "run %s(", now->sym->name);
			for (v = now->lft; v; v = v->rgt)
				if (v == now->lft)
				{	comwork(fd, v->lft, m);
				} else
				{	Cat2(",", v->lft);
				}
			fprintf(fd, ")");
			break;

	case LEN:	putname(fd, "len(", now->lft, m, ")");
			break;
	case FULL:	putname(fd, "full(", now->lft, m, ")");
			break;
	case EMPTY:	putname(fd, "empty(", now->lft, m, ")");
			break;
	case NFULL:	putname(fd, "nfull(", now->lft, m, ")");
			break;
	case NEMPTY:	putname(fd, "nempty(", now->lft, m, ")");
			break;

	case 's':	putname(fd, "", now->lft, m, now->val?"!!":"!");
			for (v = now->rgt, i=0; v; v = v->rgt, i++)
			{	if (v != now->rgt) fprintf(fd,",");
				if (!symbolic(fd, v->lft))
					comwork(fd,v->lft,m);
			}
			break;
	case 'r':	putname(fd, "", now->lft, m, "?");
			switch (now->val) {
			case 0: break;
			case 1: fprintf(fd, "?");  break;
			case 2: fprintf(fd, "<");  break;
			case 3: fprintf(fd, "?<"); break;
			}
			for (v = now->rgt, i=0; v; v = v->rgt, i++)
			{	if (v != now->rgt) fprintf(fd,",");
				if (!symbolic(fd, v->lft))
					comwork(fd,v->lft,m);
			}
			if (now->val >= 2)
				fprintf(fd, ">");
			break;
	case 'R':	putname(fd, "", now->lft, m,  now->val?"??[":"?[");
			for (v = now->rgt, i=0; v; v = v->rgt, i++)
			{	if (v != now->rgt) fprintf(fd,",");
				if (!symbolic(fd, v->lft))
					comwork(fd,v->lft,m);
			}
			fprintf(fd, "]");
			break;

	case ENABLED:	Cat3("enabled(", now->lft, ")");
			break;

	case GET_P:	if (old_priority_rules)
			{	fprintf(fd, "1");
			} else
			{	Cat3("get_priority(", now->lft, ")");
			}
			break;

	case SET_P:	if (!old_priority_rules)
			{	fprintf(fd, "set_priority(");
				comwork(fd, now->lft->lft, m);
				fprintf(fd, ", ");
				comwork(fd, now->lft->rgt, m);
				fprintf(fd, ")");
			}
			break;

	case EVAL:	Cat3("eval(", now->lft, ")");
			break;

	case NONPROGRESS:
			fprintf(fd, "np_");
			break;

	case PC_VAL:	Cat3("pc_value(", now->lft, ")");
			break;

	case 'c':	Cat3("(", now->lft, ")");
			break;

	case '?':	if (now->lft)
			{	Cat3("( (", now->lft, ") -> ");
			}
			if (now->rgt)
			{	Cat3("(", now->rgt->lft, ") : ");
				Cat3("(", now->rgt->rgt, ") )");
			}
			break;	

	case ASGN:
			if (check_track(now) == STRUCT) { break; }
			comwork(fd,now->lft,m);
			fprintf(fd," = ");
			comwork(fd,now->rgt,m);
			break;

	case PRINT:	{	char c, buf[1024];
				strncpy(buf, now->sym->name, 510);
				for (i = j = 0; i < 510; i++, j++)
				{	c = now->sym->name[i];
					buf[j] = c;
					if (c == '\\') buf[++j] = c;
					if (c == '\"') buf[j] = '\'';
					if (c == '\0') break;
				}
				if (now->ntyp == PRINT)
					fprintf(fd, "printf");
				else
					fprintf(fd, "annotate");
				fprintf(fd, "(%s", buf);
			}
			for (v = now->lft; v; v = v->rgt)
			{	Cat2(",", v->lft);
			}
			fprintf(fd, ")");
			break;
	case PRINTM:	fprintf(fd, "printm(");
			comwork(fd, now->lft, m);
			fprintf(fd, ")");
			break;
	case NAME:
			putname(fd, "", now, m, "");
			break;

	case   'p':
			if (ltl_mode)
			{	fprintf(fd, "%s", now->lft->sym->name); /* proctype */
				if (now->lft->lft)
				{	fprintf(fd, "[");
					putstmnt(fd, now->lft->lft, 0); /* pid */
					fprintf(fd, "]");
				}
				fprintf(fd, ":");	/* remote varref */
				fprintf(fd, "%s", now->sym->name);	/* varname */
				break;
			}
			putremote(fd, now, m);
			break;
	case   'q':	fprintf(fd, "%s", now->sym->name);
			break;
	case C_EXPR:	
	case C_CODE:	fprintf(fd, "{%s}", now->sym->name);
			break;
	case ASSERT:	Cat3("assert(", now->lft, ")");
			break;
	case   '.':	fprintf(fd, ".(goto)"); break;
	case  GOTO:	fprintf(fd, "goto %s", now->sym->name); break;
	case BREAK:	fprintf(fd, "break"); break;
	case  ELSE:	fprintf(fd, "else"); break;
	case   '@':	fprintf(fd, "-end-"); break;

	case D_STEP:	fprintf(fd, "D_STEP%d", now->ln); break;
	case ATOMIC:	fprintf(fd, "ATOMIC"); break;
	case NON_ATOMIC: fprintf(fd, "sub-sequence"); break;
	case IF:	fprintf(fd, "IF"); break;
	case DO:	fprintf(fd, "DO"); break;
	case UNLESS:	fprintf(fd, "unless"); break;
	case TIMEOUT:	fprintf(fd, "timeout"); break;
	default:	if (isprint(now->ntyp))
				fprintf(fd, "'%c'", now->ntyp);
			else
				fprintf(fd, "%d", now->ntyp);
			break;
	}
}

void
comment(FILE *fd, Lextok *now, int m)
{	extern short terse, nocast;

	terse=nocast=1;
	comwork(fd, now, m);
	terse=nocast=0;
}