1 /***** spin: structs.c *****/
2
3 /*
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
7 */
8
9 #include "spin.h"
10 #include "y.tab.h"
11
12 typedef struct UType {
13 Symbol *nm; /* name of the type */
14 Lextok *cn; /* contents */
15 struct UType *nxt; /* linked list */
16 } UType;
17
18 extern Symbol *Fname;
19 extern int lineno, depth, Expand_Ok, has_hidden, in_for;
20
21 Symbol *owner;
22
23 static UType *Unames = 0;
24 static UType *Pnames = 0;
25
26 static Lextok *cpnn(Lextok *, int, int, int);
27 extern void sr_mesg(FILE *, int, int, const char *);
28 extern void Done_case(char *, Symbol *);
29
30 void
setuname(Lextok * n)31 setuname(Lextok *n)
32 { UType *tmp;
33
34 if (!owner)
35 fatal("illegal reference inside typedef", (char *) 0);
36
37 for (tmp = Unames; tmp; tmp = tmp->nxt)
38 if (!strcmp(owner->name, tmp->nm->name))
39 { non_fatal("typename %s was defined before",
40 tmp->nm->name);
41 return;
42 }
43
44 tmp = (UType *) emalloc(sizeof(UType));
45 tmp->nm = owner;
46 tmp->cn = n;
47 tmp->nxt = Unames;
48 Unames = tmp;
49 }
50
51 static void
putUname(FILE * fd,UType * tmp)52 putUname(FILE *fd, UType *tmp)
53 { Lextok *fp, *tl;
54
55 if (!tmp) return;
56 putUname(fd, tmp->nxt); /* postorder */
57 fprintf(fd, "struct %s { /* user defined type */\n",
58 tmp->nm->name);
59 for (fp = tmp->cn; fp; fp = fp->rgt)
60 for (tl = fp->lft; tl; tl = tl->rgt)
61 typ2c(tl->sym);
62 fprintf(fd, "};\n");
63 }
64
65 void
putunames(FILE * fd)66 putunames(FILE *fd)
67 {
68 putUname(fd, Unames);
69 }
70
71 int
isutype(char * t)72 isutype(char *t)
73 { UType *tmp;
74
75 for (tmp = Unames; tmp; tmp = tmp->nxt)
76 { if (!strcmp(t, tmp->nm->name))
77 return 1;
78 }
79 return 0;
80 }
81
82 Lextok *
getuname(Symbol * t)83 getuname(Symbol *t)
84 { UType *tmp;
85
86 for (tmp = Unames; tmp; tmp = tmp->nxt)
87 { if (!strcmp(t->name, tmp->nm->name))
88 return tmp->cn;
89 }
90 fatal("%s is not a typename", t->name);
91 return (Lextok *)0;
92 }
93
94 void
setutype(Lextok * p,Symbol * t,Lextok * vis)95 setutype(Lextok *p, Symbol *t, Lextok *vis) /* user-defined types */
96 { int oln = lineno;
97 Symbol *ofn = Fname;
98 Lextok *m, *n;
99
100 m = getuname(t);
101 for (n = p; n; n = n->rgt)
102 { lineno = n->ln;
103 Fname = n->fn;
104 if (n->sym->type)
105 { fatal("redeclaration of '%s'", n->sym->name);
106 }
107
108 if (n->sym->nbits > 0)
109 non_fatal("(%s) only an unsigned can have width-field",
110 n->sym->name);
111
112 if (Expand_Ok)
113 n->sym->hidden |= (4|8|16); /* formal par */
114
115 if (vis)
116 { if (strncmp(vis->sym->name, ":hide:", (size_t) 6) == 0)
117 { n->sym->hidden |= 1;
118 has_hidden++;
119 } else if (strncmp(vis->sym->name, ":show:", (size_t) 6) == 0)
120 n->sym->hidden |= 2;
121 else if (strncmp(vis->sym->name, ":local:", (size_t) 7) == 0)
122 n->sym->hidden |= 64;
123 }
124 n->sym->type = STRUCT; /* classification */
125 n->sym->Slst = m; /* structure itself */
126 n->sym->Snm = t; /* name of typedef */
127 n->sym->Nid = 0; /* this is no chan */
128 n->sym->hidden |= 4;
129 if (n->sym->nel <= 0)
130 non_fatal("bad array size for '%s'", n->sym->name);
131 }
132 lineno = oln;
133 Fname = ofn;
134 }
135
136 static Symbol *
do_same(Lextok * n,Symbol * v,int xinit)137 do_same(Lextok *n, Symbol *v, int xinit)
138 { Lextok *tmp, *fp, *tl;
139 int ix = eval(n->lft);
140 int oln = lineno;
141 Symbol *ofn = Fname;
142
143 lineno = n->ln;
144 Fname = n->fn;
145
146 /* n->sym->type == STRUCT
147 * index: n->lft
148 * subfields: n->rgt
149 * structure template: n->sym->Slst
150 * runtime values: n->sym->Sval
151 */
152 if (xinit) ini_struct(v); /* once, at top level */
153
154 if (ix >= v->nel || ix < 0)
155 { printf("spin: indexing %s[%d] - size is %d\n",
156 v->name, ix, v->nel);
157 fatal("indexing error \'%s\'", v->name);
158 }
159 if (!n->rgt || !n->rgt->lft)
160 { non_fatal("no subfields %s", v->name); /* i.e., wants all */
161 lineno = oln; Fname = ofn;
162 return ZS;
163 }
164
165 if (n->rgt->ntyp != '.')
166 { printf("bad subfield type %d\n", n->rgt->ntyp);
167 alldone(1);
168 }
169
170 tmp = n->rgt->lft;
171 if (tmp->ntyp != NAME && tmp->ntyp != TYPE)
172 { printf("bad subfield entry %d\n", tmp->ntyp);
173 alldone(1);
174 }
175 for (fp = v->Sval[ix]; fp; fp = fp->rgt)
176 for (tl = fp->lft; tl; tl = tl->rgt)
177 if (!strcmp(tl->sym->name, tmp->sym->name))
178 { lineno = oln; Fname = ofn;
179 return tl->sym;
180 }
181 fatal("cannot locate subfield %s", tmp->sym->name);
182 return ZS;
183 }
184
185 int
Rval_struct(Lextok * n,Symbol * v,int xinit)186 Rval_struct(Lextok *n, Symbol *v, int xinit) /* n varref, v valref */
187 { Symbol *tl;
188 Lextok *tmp;
189 int ix;
190
191 if (!n || !(tl = do_same(n, v, xinit)))
192 return 0;
193
194 tmp = n->rgt->lft;
195 if (tmp->sym->type == STRUCT)
196 { return Rval_struct(tmp, tl, 0);
197 } else if (tmp->rgt)
198 fatal("non-zero 'rgt' on non-structure", 0);
199
200 ix = eval(tmp->lft);
201 /* printf("%d: ix: %d (%d) %d\n", depth, ix, tl->nel, tl->val[ix]); */
202 if (ix >= tl->nel || ix < 0)
203 fatal("indexing error \'%s\'", tl->name);
204
205 return cast_val(tl->type, tl->val[ix], tl->nbits);
206 }
207
208 int
Lval_struct(Lextok * n,Symbol * v,int xinit,int a)209 Lval_struct(Lextok *n, Symbol *v, int xinit, int a) /* a = assigned value */
210 { Symbol *tl;
211 Lextok *tmp;
212 int ix;
213
214 if (!(tl = do_same(n, v, xinit)))
215 return 1;
216
217 tmp = n->rgt->lft;
218 if (tmp->sym->type == STRUCT)
219 return Lval_struct(tmp, tl, 0, a);
220 else if (tmp->rgt)
221 fatal("non-zero 'rgt' on non-structure", 0);
222
223 ix = eval(tmp->lft);
224 if (ix >= tl->nel || ix < 0)
225 fatal("indexing error \'%s\'", tl->name);
226
227 if (tl->nbits > 0)
228 a = (a & ((1<<tl->nbits)-1));
229
230 if (a != tl->val[ix])
231 { tl->val[ix] = a;
232 tl->setat = depth;
233 }
234 return 1;
235 }
236
237 int
Cnt_flds(Lextok * m)238 Cnt_flds(Lextok *m)
239 { Lextok *fp, *tl, *n;
240 int cnt = 0;
241
242 if (m->ntyp == ',')
243 { n = m;
244 goto is_lst;
245 }
246 if (!m->sym || m->ntyp != STRUCT)
247 return 1;
248
249 n = getuname(m->sym);
250 is_lst:
251 for (fp = n; fp; fp = fp->rgt)
252 for (tl = fp->lft; tl; tl = tl->rgt)
253 { if (tl->sym->type == STRUCT)
254 { if (tl->sym->nel > 1 || tl->sym->isarray)
255 fatal("array of structures in param list, %s",
256 tl->sym->name);
257 cnt += Cnt_flds(tl->sym->Slst);
258 } else
259 cnt += tl->sym->nel;
260 }
261 return cnt;
262 }
263
264 int
Sym_typ(Lextok * t)265 Sym_typ(Lextok *t)
266 { Symbol *s = t->sym;
267
268 if (!s) return 0;
269
270 if (s->type != STRUCT)
271 return s->type;
272
273 if (!t->rgt
274 || t->rgt->ntyp != '.' /* gh: had ! in wrong place */
275 || !t->rgt->lft)
276 return STRUCT; /* not a field reference */
277
278 return Sym_typ(t->rgt->lft);
279 }
280
281 int
Width_set(int * wdth,int i,Lextok * n)282 Width_set(int *wdth, int i, Lextok *n)
283 { Lextok *fp, *tl;
284 int j = i, k;
285
286 for (fp = n; fp; fp = fp->rgt)
287 for (tl = fp->lft; tl; tl = tl->rgt)
288 { if (tl->sym->type == STRUCT)
289 j = Width_set(wdth, j, tl->sym->Slst);
290 else
291 { for (k = 0; k < tl->sym->nel; k++, j++)
292 wdth[j] = tl->sym->type;
293 } }
294 return j;
295 }
296
297 void
ini_struct(Symbol * s)298 ini_struct(Symbol *s)
299 { int i; Lextok *fp, *tl;
300
301 if (s->type != STRUCT) /* last step */
302 { (void) checkvar(s, 0);
303 return;
304 }
305 if (s->Sval == (Lextok **) 0)
306 { s->Sval = (Lextok **) emalloc(s->nel * sizeof(Lextok *));
307 for (i = 0; i < s->nel; i++)
308 { s->Sval[i] = cpnn(s->Slst, 1, 1, 1);
309
310 for (fp = s->Sval[i]; fp; fp = fp->rgt)
311 for (tl = fp->lft; tl; tl = tl->rgt)
312 ini_struct(tl->sym);
313 } }
314 }
315
316 static Lextok *
cpnn(Lextok * s,int L,int R,int S)317 cpnn(Lextok *s, int L, int R, int S)
318 { Lextok *d; extern int Nid_nr;
319
320 if (!s) return ZN;
321
322 d = (Lextok *) emalloc(sizeof(Lextok));
323 d->uiid = s->uiid;
324 d->ntyp = s->ntyp;
325 d->val = s->val;
326 d->ln = s->ln;
327 d->fn = s->fn;
328 d->sym = s->sym;
329 if (L) d->lft = cpnn(s->lft, 1, 1, S);
330 if (R) d->rgt = cpnn(s->rgt, 1, 1, S);
331
332 if (S && s->sym)
333 { d->sym = (Symbol *) emalloc(sizeof(Symbol));
334 memcpy(d->sym, s->sym, sizeof(Symbol));
335 if (d->sym->type == CHAN)
336 d->sym->Nid = ++Nid_nr;
337 }
338 if (s->sq || s->sl)
339 fatal("cannot happen cpnn", (char *) 0);
340
341 return d;
342 }
343
344 int
full_name(FILE * fd,Lextok * n,Symbol * v,int xinit)345 full_name(FILE *fd, Lextok *n, Symbol *v, int xinit)
346 { Symbol *tl;
347 Lextok *tmp;
348 int hiddenarrays = 0;
349
350 fprintf(fd, "%s", v->name);
351
352 if (!n || !(tl = do_same(n, v, xinit)))
353 return 0;
354 tmp = n->rgt->lft;
355
356 if (tmp->sym->type == STRUCT)
357 { fprintf(fd, ".");
358 hiddenarrays = full_name(fd, tmp, tl, 0);
359 goto out;
360 }
361 fprintf(fd, ".%s", tl->name);
362 out: if (tmp->sym->nel > 1 || tmp->sym->isarray == 1)
363 { fprintf(fd, "[%d]", eval(tmp->lft));
364 hiddenarrays = 1;
365 }
366 return hiddenarrays;
367 }
368
369 void
validref(Lextok * p,Lextok * c)370 validref(Lextok *p, Lextok *c)
371 { Lextok *fp, *tl;
372 char lbuf[512];
373
374 for (fp = p->sym->Slst; fp; fp = fp->rgt)
375 for (tl = fp->lft; tl; tl = tl->rgt)
376 if (strcmp(tl->sym->name, c->sym->name) == 0)
377 return;
378
379 sprintf(lbuf, "no field '%s' defined in structure '%s'\n",
380 c->sym->name, p->sym->name);
381 non_fatal(lbuf, (char *) 0);
382 }
383
384 void
struct_name(Lextok * n,Symbol * v,int xinit,char * buf)385 struct_name(Lextok *n, Symbol *v, int xinit, char *buf)
386 { Symbol *tl;
387 Lextok *tmp;
388 char lbuf[512];
389
390 if (!n || !(tl = do_same(n, v, xinit)))
391 return;
392 tmp = n->rgt->lft;
393 if (tmp->sym->type == STRUCT)
394 { strcat(buf, ".");
395 struct_name(tmp, tl, 0, buf);
396 return;
397 }
398 sprintf(lbuf, ".%s", tl->name);
399 strcat(buf, lbuf);
400 if (tmp->sym->nel > 1 || tmp->sym->isarray == 1)
401 { sprintf(lbuf, "[%d]", eval(tmp->lft));
402 strcat(buf, lbuf);
403 }
404 }
405
406 void
walk2_struct(char * s,Symbol * z)407 walk2_struct(char *s, Symbol *z)
408 { Lextok *fp, *tl;
409 char eprefix[128];
410 int ix;
411
412 ini_struct(z);
413 if (z->nel == 1 && z->isarray == 0)
414 sprintf(eprefix, "%s%s.", s, z->name);
415 for (ix = 0; ix < z->nel; ix++)
416 { if (z->nel > 1 || z->isarray == 1)
417 sprintf(eprefix, "%s%s[%d].", s, z->name, ix);
418 for (fp = z->Sval[ix]; fp; fp = fp->rgt)
419 for (tl = fp->lft; tl; tl = tl->rgt)
420 { if (tl->sym->type == STRUCT)
421 walk2_struct(eprefix, tl->sym);
422 else if (tl->sym->type == CHAN)
423 Done_case(eprefix, tl->sym);
424 } }
425 }
426
427 void
walk_struct(FILE * ofd,int dowhat,char * s,Symbol * z,char * a,char * b,char * c)428 walk_struct(FILE *ofd, int dowhat, char *s, Symbol *z, char *a, char *b, char *c)
429 { Lextok *fp, *tl;
430 char eprefix[128];
431 int ix;
432
433 ini_struct(z);
434 if (z->nel == 1 && z->isarray == 0)
435 sprintf(eprefix, "%s%s.", s, z->name);
436 for (ix = 0; ix < z->nel; ix++)
437 { if (z->nel > 1 || z->isarray == 1)
438 sprintf(eprefix, "%s%s[%d].", s, z->name, ix);
439 for (fp = z->Sval[ix]; fp; fp = fp->rgt)
440 for (tl = fp->lft; tl; tl = tl->rgt)
441 { if (tl->sym->type == STRUCT)
442 walk_struct(ofd, dowhat, eprefix, tl->sym, a,b,c);
443 else
444 do_var(ofd, dowhat, eprefix, tl->sym, a,b,c);
445 } }
446 }
447
448 void
c_struct(FILE * fd,char * ipref,Symbol * z)449 c_struct(FILE *fd, char *ipref, Symbol *z)
450 { Lextok *fp, *tl;
451 char pref[256], eprefix[300];
452 int ix;
453
454 ini_struct(z);
455
456 for (ix = 0; ix < z->nel; ix++)
457 for (fp = z->Sval[ix]; fp; fp = fp->rgt)
458 for (tl = fp->lft; tl; tl = tl->rgt)
459 { strcpy(eprefix, ipref);
460 if (z->nel > 1 || z->isarray == 1)
461 { /* insert index before last '.' */
462 eprefix[strlen(eprefix)-1] = '\0';
463 sprintf(pref, "[ %d ].", ix);
464 strcat(eprefix, pref);
465 }
466 if (tl->sym->type == STRUCT)
467 { strcat(eprefix, tl->sym->name);
468 strcat(eprefix, ".");
469 c_struct(fd, eprefix, tl->sym);
470 } else
471 c_var(fd, eprefix, tl->sym);
472 }
473 }
474
475 void
dump_struct(Symbol * z,char * prefix,RunList * r)476 dump_struct(Symbol *z, char *prefix, RunList *r)
477 { Lextok *fp, *tl;
478 char eprefix[256];
479 int ix, jx;
480
481 ini_struct(z);
482
483 for (ix = 0; ix < z->nel; ix++)
484 { if (z->nel > 1 || z->isarray == 1)
485 sprintf(eprefix, "%s[%d]", prefix, ix);
486 else
487 strcpy(eprefix, prefix);
488
489 for (fp = z->Sval[ix]; fp; fp = fp->rgt)
490 for (tl = fp->lft; tl; tl = tl->rgt)
491 { if (tl->sym->type == STRUCT)
492 { char pref[300];
493 strcpy(pref, eprefix);
494 strcat(pref, ".");
495 strcat(pref, tl->sym->name);
496 dump_struct(tl->sym, pref, r);
497 } else
498 for (jx = 0; jx < tl->sym->nel; jx++)
499 { if (tl->sym->type == CHAN)
500 doq(tl->sym, jx, r);
501 else
502 { char *s = 0;
503 printf("\t\t");
504 if (r)
505 printf("%s(%d):", r->n->name, r->pid);
506 printf("%s.%s", eprefix, tl->sym->name);
507 if (tl->sym->nel > 1 || tl->sym->isarray == 1)
508 printf("[%d]", jx);
509 printf(" = ");
510
511 if (tl->sym->type == MTYPE
512 && tl->sym->mtype_name)
513 { s = tl->sym->mtype_name->name;
514 }
515
516 sr_mesg(stdout, tl->sym->val[jx],
517 tl->sym->type == MTYPE, s);
518 printf("\n");
519 } } }
520 }
521 }
522
523 static int
retrieve(Lextok ** targ,int i,int want,Lextok * n,int Ntyp)524 retrieve(Lextok **targ, int i, int want, Lextok *n, int Ntyp)
525 { Lextok *fp, *tl;
526 int j = i, k;
527
528 for (fp = n; fp; fp = fp->rgt)
529 for (tl = fp->lft; tl; tl = tl->rgt)
530 { if (tl->sym->type == STRUCT)
531 { j = retrieve(targ, j, want, tl->sym->Slst, Ntyp);
532 if (j < 0)
533 { Lextok *x = cpnn(tl, 1, 0, 0);
534 x->rgt = nn(ZN, '.', (*targ), ZN);
535 (*targ) = x;
536 return -1;
537 }
538 } else
539 { for (k = 0; k < tl->sym->nel; k++, j++)
540 { if (j == want)
541 { *targ = cpnn(tl, 1, 0, 0);
542 (*targ)->lft = nn(ZN, CONST, ZN, ZN);
543 (*targ)->lft->val = k;
544 if (Ntyp)
545 (*targ)->ntyp = (short) Ntyp;
546 return -1;
547 }
548 } } }
549 return j;
550 }
551
552 static int
is_explicit(Lextok * n)553 is_explicit(Lextok *n)
554 {
555 if (!n) return 0;
556 if (!n->sym) fatal("unexpected - no symbol", 0);
557 if (n->sym->type != STRUCT) return 1;
558 if (!n->rgt) return 0;
559 if (n->rgt->ntyp != '.')
560 { lineno = n->ln;
561 Fname = n->fn;
562 printf("ntyp %d\n", n->rgt->ntyp);
563 fatal("unexpected %s, no '.'", n->sym->name);
564 }
565 return is_explicit(n->rgt->lft);
566 }
567
568 Lextok *
expand(Lextok * n,int Ok)569 expand(Lextok *n, int Ok)
570 /* turn rgt-lnked list of struct nms, into ',' list of flds */
571 { Lextok *x = ZN, *y;
572
573 if (!Ok) return n;
574
575 while (n)
576 { y = mk_explicit(n, 1, 0);
577 if (x)
578 (void) tail_add(x, y);
579 else
580 x = y;
581
582 n = n->rgt;
583 }
584 return x;
585 }
586
587 Lextok *
mk_explicit(Lextok * n,int Ok,int Ntyp)588 mk_explicit(Lextok *n, int Ok, int Ntyp)
589 /* produce a single ',' list of fields */
590 { Lextok *bld = ZN, *x;
591 int i, cnt; extern int IArgs;
592
593 if (n->sym->type != STRUCT
594 || in_for
595 || is_explicit(n))
596 return n;
597
598
599 if (n->rgt
600 && n->rgt->ntyp == '.'
601 && n->rgt->lft
602 && n->rgt->lft->sym
603 && n->rgt->lft->sym->type == STRUCT)
604 { Lextok *y;
605 bld = mk_explicit(n->rgt->lft, Ok, Ntyp);
606 for (x = bld; x; x = x->rgt)
607 { y = cpnn(n, 1, 0, 0);
608 y->rgt = nn(ZN, '.', x->lft, ZN);
609 x->lft = y;
610 }
611
612 return bld;
613 }
614
615 if (!Ok || !n->sym->Slst)
616 { if (IArgs) return n;
617 printf("spin: saw '");
618 comment(stdout, n, 0);
619 printf("'\n");
620 fatal("incomplete structure ref '%s'", n->sym->name);
621 }
622
623 cnt = Cnt_flds(n->sym->Slst);
624 for (i = cnt-1; i >= 0; i--)
625 { bld = nn(ZN, ',', ZN, bld);
626 if (retrieve(&(bld->lft), 0, i, n->sym->Slst, Ntyp) >= 0)
627 { printf("cannot retrieve field %d\n", i);
628 fatal("bad structure %s", n->sym->name);
629 }
630 x = cpnn(n, 1, 0, 0);
631 x->rgt = nn(ZN, '.', bld->lft, ZN);
632 bld->lft = x;
633 }
634 return bld;
635 }
636
637 Lextok *
tail_add(Lextok * a,Lextok * b)638 tail_add(Lextok *a, Lextok *b)
639 { Lextok *t;
640
641 for (t = a; t->rgt; t = t->rgt)
642 if (t->ntyp != ',')
643 fatal("unexpected type - tail_add", 0);
644 t->rgt = b;
645 return a;
646 }
647
648 void
setpname(Lextok * n)649 setpname(Lextok *n)
650 { UType *tmp;
651
652 for (tmp = Pnames; tmp; tmp = tmp->nxt)
653 if (!strcmp(n->sym->name, tmp->nm->name))
654 { non_fatal("proctype %s redefined",
655 n->sym->name);
656 return;
657 }
658 tmp = (UType *) emalloc(sizeof(UType));
659 tmp->nm = n->sym;
660 tmp->nxt = Pnames;
661 Pnames = tmp;
662 }
663
664 int
isproctype(char * t)665 isproctype(char *t)
666 { UType *tmp;
667
668 for (tmp = Pnames; tmp; tmp = tmp->nxt)
669 { if (!strcmp(t, tmp->nm->name))
670 return 1;
671 }
672 return 0;
673 }
674