structs.c 14 KB

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