spinlex.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. /***** spin: spinlex.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 <stdlib.h>
  11. #include "spin.h"
  12. #ifdef PC
  13. #include "y_tab.h"
  14. #else
  15. #include "y.tab.h"
  16. #endif
  17. #define MAXINL 16 /* max recursion depth inline fcts */
  18. #define MAXPAR 32 /* max params to an inline call */
  19. #define MAXLEN 512 /* max len of an actual parameter text */
  20. typedef struct IType {
  21. Symbol *nm; /* name of the type */
  22. Lextok *cn; /* contents */
  23. Lextok *params; /* formal pars if any */
  24. char **anms; /* literal text for actual pars */
  25. char *prec; /* precondition for c_code or c_expr */
  26. int dln, cln; /* def and call linenr */
  27. Symbol *dfn, *cfn; /* def and call filename */
  28. struct IType *nxt; /* linked list */
  29. } IType;
  30. typedef struct C_Added {
  31. Symbol *s;
  32. Symbol *t;
  33. Symbol *ival;
  34. struct C_Added *nxt;
  35. } C_Added;
  36. extern RunList *X;
  37. extern ProcList *rdy;
  38. extern Symbol *Fname;
  39. extern Symbol *context, *owner;
  40. extern YYSTYPE yylval;
  41. extern short has_last, has_code;
  42. extern int verbose, IArgs, hastrack, separate;
  43. short has_stack = 0;
  44. int lineno = 1;
  45. char yytext[2048];
  46. FILE *yyin, *yyout;
  47. static C_Added *c_added, *c_tracked;
  48. static IType *Inline_stub[MAXINL];
  49. static char *ReDiRect;
  50. static char *Inliner[MAXINL], IArg_cont[MAXPAR][MAXLEN];
  51. static unsigned char in_comment=0;
  52. static int IArgno = 0, Inlining = -1;
  53. static int check_name(char *);
  54. #if 1
  55. #define Token(y) { if (in_comment) goto again; \
  56. yylval = nn(ZN,0,ZN,ZN); return y; }
  57. #define ValToken(x, y) { if (in_comment) goto again; \
  58. yylval = nn(ZN,0,ZN,ZN); yylval->val = x; return y; }
  59. #define SymToken(x, y) { if (in_comment) goto again; \
  60. yylval = nn(ZN,0,ZN,ZN); yylval->sym = x; return y; }
  61. #else
  62. #define Token(y) { yylval = nn(ZN,0,ZN,ZN); \
  63. if (!in_comment) return y; else goto again; }
  64. #define ValToken(x, y) { yylval = nn(ZN,0,ZN,ZN); yylval->val = x; \
  65. if (!in_comment) return y; else goto again; }
  66. #define SymToken(x, y) { yylval = nn(ZN,0,ZN,ZN); yylval->sym = x; \
  67. if (!in_comment) return y; else goto again; }
  68. #endif
  69. static int getinline(void);
  70. static void uninline(void);
  71. #if 1
  72. #define Getchar() ((Inlining<0)?getc(yyin):getinline())
  73. #define Ungetch(c) {if (Inlining<0) ungetc(c,yyin); else uninline(); }
  74. #else
  75. static int
  76. Getchar(void)
  77. { int c;
  78. if (Inlining<0)
  79. c = getc(yyin);
  80. else
  81. c = getinline();
  82. #if 1
  83. printf("<%c>", c);
  84. #endif
  85. return c;
  86. }
  87. static void
  88. Ungetch(int c)
  89. {
  90. if (Inlining<0)
  91. ungetc(c,yyin);
  92. else
  93. uninline();
  94. #if 1
  95. printf("<bs>");
  96. #endif
  97. }
  98. #endif
  99. static int
  100. notquote(int c)
  101. { return (c != '\"' && c != '\n');
  102. }
  103. int
  104. isalnum_(int c)
  105. { return (isalnum(c) || c == '_');
  106. }
  107. static int
  108. isalpha_(int c)
  109. { return isalpha(c); /* could be macro */
  110. }
  111. static int
  112. isdigit_(int c)
  113. { return isdigit(c); /* could be macro */
  114. }
  115. static void
  116. getword(int first, int (*tst)(int))
  117. { int i=0; char c;
  118. yytext[i++]= (char) first;
  119. while (tst(c = Getchar()))
  120. { yytext[i++] = c;
  121. if (c == '\\')
  122. yytext[i++] = Getchar(); /* no tst */
  123. }
  124. yytext[i] = '\0';
  125. Ungetch(c);
  126. }
  127. static int
  128. follow(int tok, int ifyes, int ifno)
  129. { int c;
  130. if ((c = Getchar()) == tok)
  131. return ifyes;
  132. Ungetch(c);
  133. return ifno;
  134. }
  135. static IType *seqnames;
  136. static void
  137. def_inline(Symbol *s, int ln, char *ptr, char *prc, Lextok *nms)
  138. { IType *tmp;
  139. char *nw = (char *) emalloc((int) strlen(ptr)+1);
  140. strcpy(nw, ptr);
  141. for (tmp = seqnames; tmp; tmp = tmp->nxt)
  142. if (!strcmp(s->name, tmp->nm->name))
  143. { non_fatal("procedure name %s redefined",
  144. tmp->nm->name);
  145. tmp->cn = (Lextok *) nw;
  146. tmp->params = nms;
  147. tmp->dln = ln;
  148. tmp->dfn = Fname;
  149. return;
  150. }
  151. tmp = (IType *) emalloc(sizeof(IType));
  152. tmp->nm = s;
  153. tmp->cn = (Lextok *) nw;
  154. tmp->params = nms;
  155. if (strlen(prc) > 0)
  156. { tmp->prec = (char *) emalloc((int) strlen(prc)+1);
  157. strcpy(tmp->prec, prc);
  158. }
  159. tmp->dln = ln;
  160. tmp->dfn = Fname;
  161. tmp->nxt = seqnames;
  162. seqnames = tmp;
  163. }
  164. void
  165. gencodetable(FILE *fd)
  166. { IType *tmp;
  167. char *q;
  168. int cnt;
  169. if (separate == 2) return;
  170. fprintf(fd, "struct {\n");
  171. fprintf(fd, " char *c; char *t;\n");
  172. fprintf(fd, "} code_lookup[] = {\n");
  173. if (has_code)
  174. for (tmp = seqnames; tmp; tmp = tmp->nxt)
  175. if (tmp->nm->type == CODE_FRAG
  176. || tmp->nm->type == CODE_DECL)
  177. { fprintf(fd, "\t{ \"%s\", ",
  178. tmp->nm->name);
  179. q = (char *) tmp->cn;
  180. while (*q == '\n' || *q == '\r' || *q == '\\')
  181. q++;
  182. fprintf(fd, "\"");
  183. cnt = 0;
  184. while (*q && cnt < 1024) /* pangen1.h allows 2048 */
  185. { switch (*q) {
  186. case '"':
  187. fprintf(fd, "\\\"");
  188. break;
  189. case '%':
  190. fprintf(fd, "%%");
  191. break;
  192. case '\n':
  193. fprintf(fd, "\\n");
  194. break;
  195. default:
  196. putc(*q, fd);
  197. break;
  198. }
  199. q++; cnt++;
  200. }
  201. if (*q) fprintf(fd, "...");
  202. fprintf(fd, "\"");
  203. fprintf(fd, " },\n");
  204. }
  205. fprintf(fd, " { (char *) 0, \"\" }\n");
  206. fprintf(fd, "};\n");
  207. }
  208. static int
  209. iseqname(char *t)
  210. { IType *tmp;
  211. for (tmp = seqnames; tmp; tmp = tmp->nxt)
  212. { if (!strcmp(t, tmp->nm->name))
  213. return 1;
  214. }
  215. return 0;
  216. }
  217. static int
  218. getinline(void)
  219. { int c;
  220. if (ReDiRect)
  221. { c = *ReDiRect++;
  222. if (c == '\0')
  223. { ReDiRect = (char *) 0;
  224. c = *Inliner[Inlining]++;
  225. }
  226. } else
  227. c = *Inliner[Inlining]++;
  228. if (c == '\0')
  229. { lineno = Inline_stub[Inlining]->cln;
  230. Fname = Inline_stub[Inlining]->cfn;
  231. Inlining--;
  232. #if 0
  233. if (verbose&32)
  234. printf("spin: line %d, done inlining %s\n",
  235. lineno, Inline_stub[Inlining+1]->nm->name);
  236. #endif
  237. return Getchar();
  238. }
  239. return c;
  240. }
  241. static void
  242. uninline(void)
  243. {
  244. if (ReDiRect)
  245. ReDiRect--;
  246. else
  247. Inliner[Inlining]--;
  248. }
  249. IType *
  250. find_inline(char *s)
  251. { IType *tmp;
  252. for (tmp = seqnames; tmp; tmp = tmp->nxt)
  253. if (!strcmp(s, tmp->nm->name))
  254. break;
  255. if (!tmp)
  256. fatal("cannot happen, missing inline def %s", s);
  257. return tmp;
  258. }
  259. void
  260. c_state(Symbol *s, Symbol *t, Symbol *ival) /* name, scope, ival */
  261. { C_Added *r;
  262. r = (C_Added *) emalloc(sizeof(C_Added));
  263. r->s = s; /* pointer to a data object */
  264. r->t = t; /* size of object, or "global", or "local proctype_name" */
  265. r->ival = ival;
  266. r->nxt = c_added;
  267. c_added = r;
  268. }
  269. void
  270. c_track(Symbol *s, Symbol *t, Symbol *stackonly) /* name, size */
  271. { C_Added *r;
  272. r = (C_Added *) emalloc(sizeof(C_Added));
  273. r->s = s;
  274. r->t = t;
  275. r->ival = stackonly; /* abuse of name */
  276. r->nxt = c_tracked;
  277. c_tracked = r;
  278. if (stackonly != ZS)
  279. { if (strcmp(stackonly->name, "\"Matched\"") == 0)
  280. r->ival = ZS; /* the default */
  281. else if (strcmp(stackonly->name, "\"UnMatched\"") != 0
  282. && strcmp(stackonly->name, "\"unMatched\"") != 0
  283. && strcmp(stackonly->name, "\"StackOnly\"") != 0)
  284. non_fatal("expecting '[Un]Matched', saw %s", stackonly->name);
  285. else
  286. has_stack = 1;
  287. }
  288. }
  289. char *
  290. jump_etc(char *op)
  291. { char *p = op;
  292. /* kludgy - try to get the type separated from the name */
  293. while (*p == ' ' || *p == '\t')
  294. p++; /* initial white space */
  295. while (*p != ' ' && *p != '\t')
  296. p++; /* type name */
  297. while (*p == ' ' || *p == '\t')
  298. p++; /* white space */
  299. while (*p == '*')
  300. p++; /* decorations */
  301. while (*p == ' ' || *p == '\t')
  302. p++; /* white space */
  303. if (*p == '\0')
  304. fatal("c_state format (%s)", op);
  305. if (strchr(p, '[')
  306. && !strchr(p, '{'))
  307. { non_fatal("array initialization error, c_state (%s)", p);
  308. return (char *) 0;
  309. }
  310. return p;
  311. }
  312. void
  313. c_add_globinit(FILE *fd)
  314. { C_Added *r;
  315. char *p, *q;
  316. fprintf(fd, "void\nglobinit(void)\n{\n");
  317. for (r = c_added; r; r = r->nxt)
  318. { if (r->ival == ZS)
  319. continue;
  320. if (strncmp(r->t->name, " Global ", strlen(" Global ")) == 0)
  321. { for (q = r->ival->name; *q; q++)
  322. { if (*q == '\"')
  323. *q = ' ';
  324. if (*q == '\\')
  325. *q++ = ' '; /* skip over the next */
  326. }
  327. p = jump_etc(r->s->name); /* e.g., "int **q" */
  328. if (p)
  329. fprintf(fd, " now.%s = %s;\n", p, r->ival->name);
  330. } else
  331. if (strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) == 0)
  332. { for (q = r->ival->name; *q; q++)
  333. { if (*q == '\"')
  334. *q = ' ';
  335. if (*q == '\\')
  336. *q++ = ' '; /* skip over the next */
  337. }
  338. p = jump_etc(r->s->name); /* e.g., "int **q" */
  339. if (p)
  340. fprintf(fd, " %s = %s;\n", p, r->ival->name); /* no now. prefix */
  341. } }
  342. fprintf(fd, "}\n");
  343. }
  344. void
  345. c_add_locinit(FILE *fd, int tpnr, char *pnm)
  346. { C_Added *r;
  347. char *p, *q, *s;
  348. int frst = 1;
  349. fprintf(fd, "void\nlocinit%d(int h)\n{\n", tpnr);
  350. for (r = c_added; r; r = r->nxt)
  351. if (r->ival != ZS
  352. && strncmp(r->t->name, " Local", strlen(" Local")) == 0)
  353. { for (q = r->ival->name; *q; q++)
  354. if (*q == '\"')
  355. *q = ' ';
  356. p = jump_etc(r->s->name); /* e.g., "int **q" */
  357. q = r->t->name + strlen(" Local");
  358. while (*q == ' ' || *q == '\t')
  359. q++; /* process name */
  360. s = (char *) emalloc(strlen(q)+1);
  361. strcpy(s, q);
  362. q = &s[strlen(s)-1];
  363. while (*q == ' ' || *q == '\t')
  364. *q-- = '\0';
  365. if (strcmp(pnm, s) != 0)
  366. continue;
  367. if (frst)
  368. { fprintf(fd, "\tuchar *this = pptr(h);\n");
  369. frst = 0;
  370. }
  371. if (p)
  372. fprintf(fd, " ((P%d *)this)->%s = %s;\n",
  373. tpnr, p, r->ival->name);
  374. }
  375. fprintf(fd, "}\n");
  376. }
  377. /* tracking:
  378. 1. for non-global and non-local c_state decls: add up all the sizes in c_added
  379. 2. add a global char array of that size into now
  380. 3. generate a routine that memcpy's the required values into that array
  381. 4. generate a call to that routine
  382. */
  383. void
  384. c_preview(void)
  385. { C_Added *r;
  386. hastrack = 0;
  387. if (c_tracked)
  388. hastrack = 1;
  389. else
  390. for (r = c_added; r; r = r->nxt)
  391. if (strncmp(r->t->name, " Global ", strlen(" Global ")) != 0
  392. && strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) != 0
  393. && strncmp(r->t->name, " Local", strlen(" Local")) != 0)
  394. { hastrack = 1; /* c_state variant now obsolete */
  395. break;
  396. }
  397. }
  398. int
  399. c_add_sv(FILE *fd) /* 1+2 -- called in pangen1.c */
  400. { C_Added *r;
  401. int cnt = 0;
  402. if (!c_added && !c_tracked) return 0;
  403. for (r = c_added; r; r = r->nxt) /* pickup global decls */
  404. if (strncmp(r->t->name, " Global ", strlen(" Global ")) == 0)
  405. fprintf(fd, " %s;\n", r->s->name);
  406. for (r = c_added; r; r = r->nxt)
  407. if (strncmp(r->t->name, " Global ", strlen(" Global ")) != 0
  408. && strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) != 0
  409. && strncmp(r->t->name, " Local", strlen(" Local")) != 0)
  410. { cnt++; /* obsolete use */
  411. }
  412. for (r = c_tracked; r; r = r->nxt)
  413. cnt++; /* preferred use */
  414. if (cnt == 0) return 0;
  415. cnt = 0;
  416. fprintf(fd, " uchar c_state[");
  417. for (r = c_added; r; r = r->nxt)
  418. if (strncmp(r->t->name, " Global ", strlen(" Global ")) != 0
  419. && strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) != 0
  420. && strncmp(r->t->name, " Local", strlen(" Local")) != 0)
  421. { fprintf(fd, "%ssizeof(%s)",
  422. (cnt==0)?"":"+", r->t->name);
  423. cnt++;
  424. }
  425. for (r = c_tracked; r; r = r->nxt)
  426. { if (r->ival != ZS) continue;
  427. fprintf(fd, "%s%s",
  428. (cnt==0)?"":"+", r->t->name);
  429. cnt++;
  430. }
  431. if (cnt == 0) fprintf(fd, "4"); /* now redundant */
  432. fprintf(fd, "];\n");
  433. return 1;
  434. }
  435. void
  436. c_add_stack(FILE *fd)
  437. { C_Added *r;
  438. int cnt = 0;
  439. if ((!c_added && !c_tracked) || !has_stack) return;
  440. for (r = c_tracked; r; r = r->nxt)
  441. if (r->ival != ZS)
  442. cnt++;
  443. if (cnt == 0) return;
  444. fprintf(fd, " uchar c_stack[");
  445. cnt = 0;
  446. for (r = c_tracked; r; r = r->nxt)
  447. { if (r->ival == ZS) continue;
  448. fprintf(fd, "%s%s",
  449. (cnt==0)?"":"+", r->t->name);
  450. cnt++;
  451. }
  452. if (cnt == 0) fprintf(fd, "4"); /* can't happen */
  453. fprintf(fd, "];\n");
  454. }
  455. void
  456. c_add_hidden(FILE *fd)
  457. { C_Added *r;
  458. for (r = c_added; r; r = r->nxt) /* pickup hidden decls */
  459. if (strncmp(r->t->name, "\"Hidden\"", strlen("\"Hidden\"")) == 0)
  460. { r->s->name[strlen(r->s->name)-1] = ' ';
  461. fprintf(fd, "%s; /* Hidden */\n", &r->s->name[1]);
  462. r->s->name[strlen(r->s->name)-1] = '"';
  463. }
  464. /* called before c_add_def - quotes are still there */
  465. }
  466. void
  467. c_add_loc(FILE *fd, char *s) /* state vector entries for proctype s */
  468. { C_Added *r;
  469. static char buf[1024];
  470. char *p;
  471. if (!c_added) return;
  472. strcpy(buf, s);
  473. strcat(buf, " ");
  474. for (r = c_added; r; r = r->nxt) /* pickup local decls */
  475. if (strncmp(r->t->name, " Local", strlen(" Local")) == 0)
  476. { p = r->t->name + strlen(" Local");
  477. while (*p == ' ' || *p == '\t')
  478. p++;
  479. if (strcmp(p, buf) == 0)
  480. fprintf(fd, " %s;\n", r->s->name);
  481. }
  482. }
  483. void
  484. c_add_def(FILE *fd) /* 3 - called in plunk_c_fcts() */
  485. { C_Added *r;
  486. fprintf(fd, "#if defined(C_States) && defined(HAS_TRACK)\n");
  487. for (r = c_added; r; r = r->nxt)
  488. { r->s->name[strlen(r->s->name)-1] = ' ';
  489. r->s->name[0] = ' '; /* remove the "s */
  490. r->t->name[strlen(r->t->name)-1] = ' ';
  491. r->t->name[0] = ' ';
  492. if (strncmp(r->t->name, " Global ", strlen(" Global ")) == 0
  493. || strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) == 0
  494. || strncmp(r->t->name, " Local", strlen(" Local")) == 0)
  495. continue;
  496. if (strchr(r->s->name, '&'))
  497. fatal("dereferencing state object: %s", r->s->name);
  498. fprintf(fd, "extern %s %s;\n", r->t->name, r->s->name);
  499. }
  500. for (r = c_tracked; r; r = r->nxt)
  501. { r->s->name[strlen(r->s->name)-1] = ' ';
  502. r->s->name[0] = ' '; /* remove " */
  503. r->t->name[strlen(r->t->name)-1] = ' ';
  504. r->t->name[0] = ' ';
  505. }
  506. if (separate == 2)
  507. { fprintf(fd, "#endif\n");
  508. return;
  509. }
  510. if (has_stack)
  511. { fprintf(fd, "void\nc_stack(char *p_t_r)\n{\n");
  512. fprintf(fd, "#ifdef VERBOSE\n");
  513. fprintf(fd, " printf(\"c_stack %%u\\n\", p_t_r);\n");
  514. fprintf(fd, "#endif\n");
  515. for (r = c_tracked; r; r = r->nxt)
  516. { if (r->ival == ZS) continue;
  517. fprintf(fd, "\tif(%s)\n", r->s->name);
  518. fprintf(fd, "\t\tmemcpy(p_t_r, %s, %s);\n",
  519. r->s->name, r->t->name);
  520. fprintf(fd, "\telse\n");
  521. fprintf(fd, "\t\tmemset(p_t_r, 0, %s);\n",
  522. r->t->name);
  523. fprintf(fd, "\tp_t_r += %s;\n", r->t->name);
  524. }
  525. fprintf(fd, "}\n\n");
  526. }
  527. fprintf(fd, "void\nc_update(char *p_t_r)\n{\n");
  528. fprintf(fd, "#ifdef VERBOSE\n");
  529. fprintf(fd, " printf(\"c_update %%u\\n\", p_t_r);\n");
  530. fprintf(fd, "#endif\n");
  531. for (r = c_added; r; r = r->nxt)
  532. { if (strncmp(r->t->name, " Global ", strlen(" Global ")) == 0
  533. || strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) == 0
  534. || strncmp(r->t->name, " Local", strlen(" Local")) == 0)
  535. continue;
  536. fprintf(fd, "\tmemcpy(p_t_r, &%s, sizeof(%s));\n",
  537. r->s->name, r->t->name);
  538. fprintf(fd, "\tp_t_r += sizeof(%s);\n", r->t->name);
  539. }
  540. for (r = c_tracked; r; r = r->nxt)
  541. { if (r->ival) continue;
  542. fprintf(fd, "\tif(%s)\n", r->s->name);
  543. fprintf(fd, "\t\tmemcpy(p_t_r, %s, %s);\n",
  544. r->s->name, r->t->name);
  545. fprintf(fd, "\telse\n");
  546. fprintf(fd, "\t\tmemset(p_t_r, 0, %s);\n",
  547. r->t->name);
  548. fprintf(fd, "\tp_t_r += %s;\n", r->t->name);
  549. }
  550. fprintf(fd, "}\n");
  551. if (has_stack)
  552. { fprintf(fd, "void\nc_unstack(char *p_t_r)\n{\n");
  553. fprintf(fd, "#ifdef VERBOSE\n");
  554. fprintf(fd, " printf(\"c_unstack %%u\\n\", p_t_r);\n");
  555. fprintf(fd, "#endif\n");
  556. for (r = c_tracked; r; r = r->nxt)
  557. { if (r->ival == ZS) continue;
  558. fprintf(fd, "\tif(%s)\n", r->s->name);
  559. fprintf(fd, "\t\tmemcpy(%s, p_t_r, %s);\n",
  560. r->s->name, r->t->name);
  561. fprintf(fd, "\tp_t_r += %s;\n", r->t->name);
  562. }
  563. fprintf(fd, "}\n");
  564. }
  565. fprintf(fd, "void\nc_revert(char *p_t_r)\n{\n");
  566. fprintf(fd, "#ifdef VERBOSE\n");
  567. fprintf(fd, " printf(\"c_revert %%u\\n\", p_t_r);\n");
  568. fprintf(fd, "#endif\n");
  569. for (r = c_added; r; r = r->nxt)
  570. { if (strncmp(r->t->name, " Global ", strlen(" Global ")) == 0
  571. || strncmp(r->t->name, " Hidden ", strlen(" Hidden ")) == 0
  572. || strncmp(r->t->name, " Local", strlen(" Local")) == 0)
  573. continue;
  574. fprintf(fd, "\tmemcpy(&%s, p_t_r, sizeof(%s));\n",
  575. r->s->name, r->t->name);
  576. fprintf(fd, "\tp_t_r += sizeof(%s);\n", r->t->name);
  577. }
  578. for (r = c_tracked; r; r = r->nxt)
  579. { if (r->ival != ZS) continue;
  580. fprintf(fd, "\tif(%s)\n", r->s->name);
  581. fprintf(fd, "\t\tmemcpy(%s, p_t_r, %s);\n",
  582. r->s->name, r->t->name);
  583. fprintf(fd, "\tp_t_r += %s;\n", r->t->name);
  584. }
  585. fprintf(fd, "}\n");
  586. fprintf(fd, "#endif\n");
  587. }
  588. void
  589. plunk_reverse(FILE *fd, IType *p, int matchthis)
  590. { char *y, *z;
  591. if (!p) return;
  592. plunk_reverse(fd, p->nxt, matchthis);
  593. if (!p->nm->context
  594. && p->nm->type == matchthis)
  595. { fprintf(fd, "\n/* start of %s */\n", p->nm->name);
  596. z = (char *) p->cn;
  597. while (*z == '\n' || *z == '\r' || *z == '\\')
  598. z++;
  599. /* e.g.: \#include "..." */
  600. y = z;
  601. while ((y = strstr(y, "\\#")) != NULL)
  602. { *y = '\n'; y++;
  603. }
  604. fprintf(fd, "%s\n", z);
  605. fprintf(fd, "\n/* end of %s */\n", p->nm->name);
  606. }
  607. }
  608. void
  609. plunk_c_decls(FILE *fd)
  610. {
  611. plunk_reverse(fd, seqnames, CODE_DECL);
  612. }
  613. void
  614. plunk_c_fcts(FILE *fd)
  615. {
  616. if (separate == 2 && hastrack)
  617. { c_add_def(fd);
  618. return;
  619. }
  620. c_add_hidden(fd);
  621. plunk_reverse(fd, seqnames, CODE_FRAG);
  622. if (c_added || c_tracked) /* enables calls to c_revert and c_update */
  623. fprintf(fd, "#define C_States 1\n");
  624. else
  625. fprintf(fd, "#undef C_States\n");
  626. if (hastrack)
  627. c_add_def(fd);
  628. c_add_globinit(fd);
  629. do_locinits(fd);
  630. }
  631. static void
  632. check_inline(IType *tmp)
  633. { char buf[128];
  634. ProcList *p;
  635. if (!X) return;
  636. for (p = rdy; p; p = p->nxt)
  637. { if (strcmp(p->n->name, X->n->name) == 0)
  638. continue;
  639. sprintf(buf, "P%s->", p->n->name);
  640. if (strstr((char *)tmp->cn, buf))
  641. { printf("spin: in proctype %s, ref to object in proctype %s\n",
  642. X->n->name, p->n->name);
  643. fatal("invalid variable ref in '%s'", tmp->nm->name);
  644. } }
  645. }
  646. void
  647. plunk_expr(FILE *fd, char *s)
  648. { IType *tmp;
  649. tmp = find_inline(s);
  650. check_inline(tmp);
  651. fprintf(fd, "%s", (char *) tmp->cn);
  652. }
  653. void
  654. preruse(FILE *fd, Lextok *n) /* check a condition for c_expr with preconditions */
  655. { IType *tmp;
  656. if (!n) return;
  657. if (n->ntyp == C_EXPR)
  658. { tmp = find_inline(n->sym->name);
  659. if (tmp->prec)
  660. { fprintf(fd, "if (!(%s)) { if (!readtrail) { depth++; ", tmp->prec);
  661. fprintf(fd, "trpt++; trpt->pr = II; trpt->o_t = t;");
  662. fprintf(fd, "trpt->st = tt; Uerror(\"%s\"); } ", tmp->prec);
  663. fprintf(fd, "else { printf(\"pan: precondition false: %s\\n\"); ", tmp->prec);
  664. fprintf(fd, "_m = 3; goto P999; } } \n\t\t");
  665. }
  666. } else
  667. { preruse(fd, n->rgt);
  668. preruse(fd, n->lft);
  669. }
  670. }
  671. int
  672. glob_inline(char *s)
  673. { IType *tmp;
  674. char *bdy;
  675. tmp = find_inline(s);
  676. bdy = (char *) tmp->cn;
  677. return (strstr(bdy, "now.") /* global ref or */
  678. || strchr(bdy, '(') > bdy); /* possible C-function call */
  679. }
  680. void
  681. plunk_inline(FILE *fd, char *s, int how) /* c_code with precondition */
  682. { IType *tmp;
  683. tmp = find_inline(s);
  684. check_inline(tmp);
  685. fprintf(fd, "{ ");
  686. if (how && tmp->prec)
  687. { fprintf(fd, "if (!(%s)) { if (!readtrail) { depth++; ", tmp->prec);
  688. fprintf(fd, "trpt++; trpt->pr = II; trpt->o_t = t;");
  689. fprintf(fd, "trpt->st = tt; Uerror(\"%s\"); } ", tmp->prec);
  690. fprintf(fd, "else { printf(\"pan: precondition false: %s\\n\"); ", tmp->prec);
  691. fprintf(fd, "_m = 3; goto P999; } } ");
  692. }
  693. fprintf(fd, "%s", (char *) tmp->cn);
  694. fprintf(fd, " }\n");
  695. }
  696. void
  697. no_side_effects(char *s)
  698. { IType *tmp;
  699. char *t;
  700. /* could still defeat this check via hidden
  701. * side effects in function calls,
  702. * but this will catch at least some cases
  703. */
  704. tmp = find_inline(s);
  705. t = (char *) tmp->cn;
  706. if (strchr(t, ';')
  707. || strstr(t, "++")
  708. || strstr(t, "--"))
  709. {
  710. bad: lineno = tmp->dln;
  711. Fname = tmp->dfn;
  712. non_fatal("c_expr %s has side-effects", s);
  713. return;
  714. }
  715. while ((t = strchr(t, '=')) != NULL)
  716. { if (*(t-1) == '!'
  717. || *(t-1) == '>'
  718. || *(t-1) == '<')
  719. { t++;
  720. continue;
  721. }
  722. t++;
  723. if (*t != '=')
  724. goto bad;
  725. t++;
  726. }
  727. }
  728. void
  729. pickup_inline(Symbol *t, Lextok *apars)
  730. { IType *tmp; Lextok *p, *q; int j;
  731. tmp = find_inline(t->name);
  732. if (++Inlining >= MAXINL)
  733. fatal("inline fcts too deeply nested", 0);
  734. tmp->cln = lineno; /* remember calling point */
  735. tmp->cfn = Fname; /* and filename */
  736. for (p = apars, q = tmp->params, j = 0; p && q; p = p->rgt, q = q->rgt)
  737. j++; /* count them */
  738. if (p || q)
  739. fatal("wrong nr of params on call of '%s'", t->name);
  740. tmp->anms = (char **) emalloc(j * sizeof(char *));
  741. for (p = apars, j = 0; p; p = p->rgt, j++)
  742. { tmp->anms[j] = (char *) emalloc((int) strlen(IArg_cont[j])+1);
  743. strcpy(tmp->anms[j], IArg_cont[j]);
  744. }
  745. lineno = tmp->dln; /* linenr of def */
  746. Fname = tmp->dfn; /* filename of same */
  747. Inliner[Inlining] = (char *)tmp->cn;
  748. Inline_stub[Inlining] = tmp;
  749. #if 0
  750. if (verbose&32)
  751. printf("spin: line %d, file %s, inlining '%s' (from line %d, file %s)\n",
  752. tmp->cln, tmp->cfn->name, t->name, tmp->dln, tmp->dfn->name);
  753. #endif
  754. for (j = 0; j < Inlining; j++)
  755. if (Inline_stub[j] == Inline_stub[Inlining])
  756. fatal("cyclic inline attempt on: %s", t->name);
  757. }
  758. static void
  759. do_directive(int first)
  760. { int c = first; /* handles lines starting with pound */
  761. getword(c, isalpha_);
  762. if (strcmp(yytext, "#ident") == 0)
  763. goto done;
  764. if ((c = Getchar()) != ' ')
  765. fatal("malformed preprocessor directive - # .", 0);
  766. if (!isdigit_(c = Getchar()))
  767. fatal("malformed preprocessor directive - # .lineno", 0);
  768. getword(c, isdigit_);
  769. lineno = atoi(yytext); /* pickup the line number */
  770. if ((c = Getchar()) == '\n')
  771. return; /* no filename */
  772. if (c != ' ')
  773. fatal("malformed preprocessor directive - .fname", 0);
  774. if ((c = Getchar()) != '\"')
  775. fatal("malformed preprocessor directive - .fname", 0);
  776. getword(c, notquote);
  777. if (Getchar() != '\"')
  778. fatal("malformed preprocessor directive - fname.", 0);
  779. strcat(yytext, "\"");
  780. Fname = lookup(yytext);
  781. done:
  782. while (Getchar() != '\n')
  783. ;
  784. }
  785. void
  786. precondition(char *q)
  787. { int c, nest = 1;
  788. for (;;)
  789. { c = Getchar();
  790. *q++ = c;
  791. switch (c) {
  792. case '\n':
  793. lineno++;
  794. break;
  795. case '[':
  796. nest++;
  797. break;
  798. case ']':
  799. if (--nest <= 0)
  800. { *--q = '\0';
  801. return;
  802. }
  803. break;
  804. }
  805. }
  806. fatal("cannot happen", (char *) 0);
  807. }
  808. Symbol *
  809. prep_inline(Symbol *s, Lextok *nms)
  810. { int c, nest = 1, dln, firstchar, cnr;
  811. char *p, buf[SOMETHINGBIG], buf2[RATHERSMALL];
  812. Lextok *t;
  813. static int c_code = 1;
  814. for (t = nms; t; t = t->rgt)
  815. if (t->lft)
  816. { if (t->lft->ntyp != NAME)
  817. fatal("bad param to inline %s", s->name);
  818. t->lft->sym->hidden |= 32;
  819. }
  820. if (!s) /* C_Code fragment */
  821. { s = (Symbol *) emalloc(sizeof(Symbol));
  822. s->name = (char *) emalloc((int) strlen("c_code")+26);
  823. sprintf(s->name, "c_code%d", c_code++);
  824. s->context = context;
  825. s->type = CODE_FRAG;
  826. } else
  827. s->type = PREDEF;
  828. p = &buf[0];
  829. buf2[0] = '\0';
  830. for (;;)
  831. { c = Getchar();
  832. switch (c) {
  833. case '[':
  834. if (s->type != CODE_FRAG)
  835. goto bad;
  836. precondition(&buf2[0]); /* e.g., c_code [p] { r = p-r; } */
  837. continue;
  838. case '{':
  839. break;
  840. case '\n':
  841. lineno++;
  842. /* fall through */
  843. case ' ': case '\t': case '\f': case '\r':
  844. continue;
  845. default :
  846. printf("spin: saw char '%c'\n", c);
  847. bad: fatal("bad inline: %s", s->name);
  848. }
  849. break;
  850. }
  851. dln = lineno;
  852. if (s->type == CODE_FRAG)
  853. { if (verbose&32)
  854. sprintf(buf, "\t/* line %d %s */\n\t\t",
  855. lineno, Fname->name);
  856. else
  857. strcpy(buf, "");
  858. } else
  859. sprintf(buf, "\n#line %d %s\n{", lineno, Fname->name);
  860. p += strlen(buf);
  861. firstchar = 1;
  862. cnr = 1; /* not zero */
  863. more:
  864. *p++ = c = Getchar();
  865. if (p - buf >= SOMETHINGBIG)
  866. fatal("inline text too long", 0);
  867. switch (c) {
  868. case '\n':
  869. lineno++;
  870. cnr = 0;
  871. break;
  872. case '{':
  873. cnr++;
  874. nest++;
  875. break;
  876. case '}':
  877. cnr++;
  878. if (--nest <= 0)
  879. { *p = '\0';
  880. if (s->type == CODE_FRAG)
  881. *--p = '\0'; /* remove trailing '}' */
  882. def_inline(s, dln, &buf[0], &buf2[0], nms);
  883. if (firstchar && s)
  884. printf("%3d: %s, warning: empty inline definition (%s)\n",
  885. dln, Fname->name, s->name);
  886. return s; /* normal return */
  887. }
  888. break;
  889. case '#':
  890. if (cnr == 0)
  891. { p--;
  892. do_directive(c); /* reads to newline */
  893. break;
  894. } /* else fall through */
  895. default:
  896. firstchar = 0;
  897. case '\t':
  898. case ' ':
  899. case '\f':
  900. cnr++;
  901. break;
  902. }
  903. goto more;
  904. }
  905. static int
  906. lex(void)
  907. { int c;
  908. again:
  909. c = Getchar();
  910. yytext[0] = (char) c;
  911. yytext[1] = '\0';
  912. switch (c) {
  913. case '\n': /* newline */
  914. lineno++;
  915. case '\r': /* carriage return */
  916. goto again;
  917. case ' ': case '\t': case '\f': /* white space */
  918. goto again;
  919. case '#': /* preprocessor directive */
  920. if (in_comment) goto again;
  921. do_directive(c);
  922. goto again;
  923. case '\"':
  924. getword(c, notquote);
  925. if (Getchar() != '\"')
  926. fatal("string not terminated", yytext);
  927. strcat(yytext, "\"");
  928. SymToken(lookup(yytext), STRING)
  929. case '\'': /* new 3.0.9 */
  930. c = Getchar();
  931. if (c == '\\')
  932. { c = Getchar();
  933. if (c == 'n') c = '\n';
  934. else if (c == 'r') c = '\r';
  935. else if (c == 't') c = '\t';
  936. else if (c == 'f') c = '\f';
  937. }
  938. if (Getchar() != '\'' && !in_comment)
  939. fatal("character quote missing: %s", yytext);
  940. ValToken(c, CONST)
  941. default:
  942. break;
  943. }
  944. if (isdigit_(c))
  945. { getword(c, isdigit_);
  946. ValToken(atoi(yytext), CONST)
  947. }
  948. if (isalpha_(c) || c == '_')
  949. { getword(c, isalnum_);
  950. if (!in_comment)
  951. { c = check_name(yytext);
  952. if (c) return c;
  953. /* else fall through */
  954. }
  955. goto again;
  956. }
  957. switch (c) {
  958. case '/': c = follow('*', 0, '/');
  959. if (!c) { in_comment = 1; goto again; }
  960. break;
  961. case '*': c = follow('/', 0, '*');
  962. if (!c) { in_comment = 0; goto again; }
  963. break;
  964. case ':': c = follow(':', SEP, ':'); break;
  965. case '-': c = follow('>', SEMI, follow('-', DECR, '-')); break;
  966. case '+': c = follow('+', INCR, '+'); break;
  967. case '<': c = follow('<', LSHIFT, follow('=', LE, LT)); break;
  968. case '>': c = follow('>', RSHIFT, follow('=', GE, GT)); break;
  969. case '=': c = follow('=', EQ, ASGN); break;
  970. case '!': c = follow('=', NE, follow('!', O_SND, SND)); break;
  971. case '?': c = follow('?', R_RCV, RCV); break;
  972. case '&': c = follow('&', AND, '&'); break;
  973. case '|': c = follow('|', OR, '|'); break;
  974. case ';': c = SEMI; break;
  975. default : break;
  976. }
  977. Token(c)
  978. }
  979. static struct {
  980. char *s; int tok; int val; char *sym;
  981. } Names[] = {
  982. {"active", ACTIVE, 0, 0},
  983. {"assert", ASSERT, 0, 0},
  984. {"atomic", ATOMIC, 0, 0},
  985. {"bit", TYPE, BIT, 0},
  986. {"bool", TYPE, BIT, 0},
  987. {"break", BREAK, 0, 0},
  988. {"byte", TYPE, BYTE, 0},
  989. {"c_code", C_CODE, 0, 0},
  990. {"c_decl", C_DECL, 0, 0},
  991. {"c_expr", C_EXPR, 0, 0},
  992. {"c_state", C_STATE, 0, 0},
  993. {"c_track", C_TRACK, 0, 0},
  994. {"D_proctype", D_PROCTYPE, 0, 0},
  995. {"do", DO, 0, 0},
  996. {"chan", TYPE, CHAN, 0},
  997. {"else", ELSE, 0, 0},
  998. {"empty", EMPTY, 0, 0},
  999. {"enabled", ENABLED, 0, 0},
  1000. {"eval", EVAL, 0, 0},
  1001. {"false", CONST, 0, 0},
  1002. {"fi", FI, 0, 0},
  1003. {"full", FULL, 0, 0},
  1004. {"goto", GOTO, 0, 0},
  1005. {"hidden", HIDDEN, 0, ":hide:"},
  1006. {"if", IF, 0, 0},
  1007. {"init", INIT, 0, ":init:"},
  1008. {"int", TYPE, INT, 0},
  1009. {"len", LEN, 0, 0},
  1010. {"local", ISLOCAL, 0, ":local:"},
  1011. {"mtype", TYPE, MTYPE, 0},
  1012. {"nempty", NEMPTY, 0, 0},
  1013. {"never", CLAIM, 0, ":never:"},
  1014. {"nfull", NFULL, 0, 0},
  1015. {"notrace", TRACE, 0, ":notrace:"},
  1016. {"np_", NONPROGRESS, 0, 0},
  1017. {"od", OD, 0, 0},
  1018. {"of", OF, 0, 0},
  1019. {"pc_value", PC_VAL, 0, 0},
  1020. {"pid", TYPE, BYTE, 0},
  1021. {"printf", PRINT, 0, 0},
  1022. {"printm", PRINTM, 0, 0},
  1023. {"priority", PRIORITY, 0, 0},
  1024. {"proctype", PROCTYPE, 0, 0},
  1025. {"provided", PROVIDED, 0, 0},
  1026. {"run", RUN, 0, 0},
  1027. {"d_step", D_STEP, 0, 0},
  1028. {"inline", INLINE, 0, 0},
  1029. {"short", TYPE, SHORT, 0},
  1030. {"skip", CONST, 1, 0},
  1031. {"timeout", TIMEOUT, 0, 0},
  1032. {"trace", TRACE, 0, ":trace:"},
  1033. {"true", CONST, 1, 0},
  1034. {"show", SHOW, 0, ":show:"},
  1035. {"typedef", TYPEDEF, 0, 0},
  1036. {"unless", UNLESS, 0, 0},
  1037. {"unsigned", TYPE, UNSIGNED, 0},
  1038. {"xr", XU, XR, 0},
  1039. {"xs", XU, XS, 0},
  1040. {0, 0, 0, 0},
  1041. };
  1042. static int
  1043. check_name(char *s)
  1044. { int i;
  1045. yylval = nn(ZN, 0, ZN, ZN);
  1046. for (i = 0; Names[i].s; i++)
  1047. if (strcmp(s, Names[i].s) == 0)
  1048. { yylval->val = Names[i].val;
  1049. if (Names[i].sym)
  1050. yylval->sym = lookup(Names[i].sym);
  1051. return Names[i].tok;
  1052. }
  1053. if ((yylval->val = ismtype(s)) != 0)
  1054. { yylval->ismtyp = 1;
  1055. return CONST;
  1056. }
  1057. if (strcmp(s, "_last") == 0)
  1058. has_last++;
  1059. if (Inlining >= 0 && !ReDiRect)
  1060. { Lextok *tt, *t = Inline_stub[Inlining]->params;
  1061. for (i = 0; t; t = t->rgt, i++) /* formal pars */
  1062. if (!strcmp(s, t->lft->sym->name) /* varname matches formal */
  1063. && strcmp(s, Inline_stub[Inlining]->anms[i]) != 0) /* actual pars */
  1064. {
  1065. #if 0
  1066. if (verbose&32)
  1067. printf("\tline %d, replace %s in call of '%s' with %s\n",
  1068. lineno, s,
  1069. Inline_stub[Inlining]->nm->name,
  1070. Inline_stub[Inlining]->anms[i]);
  1071. #endif
  1072. for (tt = Inline_stub[Inlining]->params; tt; tt = tt->rgt)
  1073. if (!strcmp(Inline_stub[Inlining]->anms[i],
  1074. tt->lft->sym->name))
  1075. { /* would be cyclic if not caught */
  1076. printf("spin: line %d replacement value: %s\n",
  1077. lineno, tt->lft->sym->name);
  1078. fatal("formal par of %s matches replacement value",
  1079. Inline_stub[Inlining]->nm->name);
  1080. yylval->ntyp = tt->lft->ntyp;
  1081. yylval->sym = lookup(tt->lft->sym->name);
  1082. return NAME;
  1083. }
  1084. ReDiRect = Inline_stub[Inlining]->anms[i];
  1085. return 0;
  1086. } }
  1087. yylval->sym = lookup(s); /* symbol table */
  1088. if (isutype(s))
  1089. return UNAME;
  1090. if (isproctype(s))
  1091. return PNAME;
  1092. if (iseqname(s))
  1093. return INAME;
  1094. return NAME;
  1095. }
  1096. int
  1097. yylex(void)
  1098. { static int last = 0;
  1099. static int hold = 0;
  1100. int c;
  1101. /*
  1102. * repair two common syntax mistakes with
  1103. * semi-colons before or after a '}'
  1104. */
  1105. if (hold)
  1106. { c = hold;
  1107. hold = 0;
  1108. } else
  1109. { c = lex();
  1110. if (last == ELSE
  1111. && c != SEMI
  1112. && c != FI)
  1113. { hold = c;
  1114. last = 0;
  1115. return SEMI;
  1116. }
  1117. if (last == '}'
  1118. && c != PROCTYPE
  1119. && c != INIT
  1120. && c != CLAIM
  1121. && c != SEP
  1122. && c != FI
  1123. && c != OD
  1124. && c != '}'
  1125. && c != UNLESS
  1126. && c != SEMI
  1127. && c != EOF)
  1128. { hold = c;
  1129. last = 0;
  1130. return SEMI; /* insert ';' */
  1131. }
  1132. if (c == SEMI)
  1133. { /* if context, we're not in a typedef
  1134. * because they're global.
  1135. * if owner, we're at the end of a ref
  1136. * to a struct field -- prevent that the
  1137. * lookahead is interpreted as a field of
  1138. * the same struct...
  1139. */
  1140. if (context) owner = ZS;
  1141. hold = lex(); /* look ahead */
  1142. if (hold == '}'
  1143. || hold == SEMI)
  1144. { c = hold; /* omit ';' */
  1145. hold = 0;
  1146. }
  1147. }
  1148. }
  1149. last = c;
  1150. if (IArgs)
  1151. { static int IArg_nst = 0;
  1152. if (strcmp(yytext, ",") == 0)
  1153. { IArg_cont[++IArgno][0] = '\0';
  1154. } else if (strcmp(yytext, "(") == 0)
  1155. { if (IArg_nst++ == 0)
  1156. { IArgno = 0;
  1157. IArg_cont[0][0] = '\0';
  1158. } else
  1159. strcat(IArg_cont[IArgno], yytext);
  1160. } else if (strcmp(yytext, ")") == 0)
  1161. { if (--IArg_nst > 0)
  1162. strcat(IArg_cont[IArgno], yytext);
  1163. } else if (c == CONST && yytext[0] == '\'')
  1164. { sprintf(yytext, "'%c'", yylval->val);
  1165. strcat(IArg_cont[IArgno], yytext);
  1166. } else
  1167. { strcat(IArg_cont[IArgno], yytext);
  1168. }
  1169. }
  1170. return c;
  1171. }