structs.c 14 KB

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