spin.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /***** spin: spin.h *****/
  2. /* Copyright (c) 1991-2000 by Lucent Technologies - Bell Laboratories */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* Permission is given to distribute this code provided that this intro- */
  5. /* ductory message is not removed and no monies are exchanged. */
  6. /* No guarantee is expressed or implied by the distribution of this code. */
  7. /* Software written by Gerard J. Holzmann as part of the book: */
  8. /* `Design and Validation of Computer Protocols,' ISBN 0-13-539925-4, */
  9. /* Prentice Hall, Englewood Cliffs, NJ, 07632. */
  10. /* Send bug-reports and/or questions to: gerard@research.bell-labs.com */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. typedef struct Lextok {
  15. unsigned short ntyp; /* node type */
  16. short ismtyp; /* CONST derived from MTYP */
  17. int val; /* value attribute */
  18. int ln; /* line number */
  19. int indstep; /* part of d_step sequence */
  20. struct Symbol *fn; /* file name */
  21. struct Symbol *sym; /* symbol reference */
  22. struct Sequence *sq; /* sequence */
  23. struct SeqList *sl; /* sequence list */
  24. struct Lextok *lft, *rgt; /* children in parse tree */
  25. } Lextok;
  26. typedef struct Access {
  27. struct Symbol *who; /* proctype name of accessor */
  28. struct Symbol *what; /* proctype name of accessed */
  29. int cnt, typ; /* parameter nr and, e.g., 's' or 'r' */
  30. struct Access *lnk; /* linked list */
  31. } Access;
  32. typedef struct Symbol {
  33. char *name;
  34. int Nid; /* unique number for the name */
  35. unsigned short type; /* bit,short,.., chan,struct */
  36. unsigned char hidden; /* bit-flags:
  37. 1=hide, 2=show,
  38. 4=bit-equiv, 8=byte-equiv,
  39. 16=formal par, 32=inline par,
  40. 64=treat as if local
  41. */
  42. unsigned char colnr; /* for use with xspin */
  43. int nbits; /* an optional width specifier */
  44. int nel; /* 1 if scalar, >1 if array */
  45. int setat; /* last depth value changed */
  46. int *val; /* runtime value(s), initl 0 */
  47. struct Lextok **Sval; /* values for structures */
  48. int xu; /* exclusive r or w by 1 pid */
  49. struct Symbol *xup[2]; /* xr or xs proctype */
  50. struct Access *access;/* e.g., senders and receives of chan */
  51. struct Lextok *ini; /* initial value, or chan-def */
  52. struct Lextok *Slst; /* template for structure if struct */
  53. struct Symbol *Snm; /* name of the defining struct */
  54. struct Symbol *owner; /* set for names of subfields in typedefs */
  55. struct Symbol *context; /* 0 if global, or procname */
  56. struct Symbol *next; /* linked list */
  57. } Symbol;
  58. typedef struct Ordered { /* links all names in Symbol table */
  59. struct Symbol *entry;
  60. struct Ordered *next;
  61. } Ordered;
  62. typedef struct Queue {
  63. short qid; /* runtime q index */
  64. short qlen; /* nr messages stored */
  65. short nslots, nflds; /* capacity, flds/slot */
  66. int setat; /* last depth value changed */
  67. int *fld_width; /* type of each field */
  68. int *contents; /* the values stored */
  69. int *stepnr; /* depth when each msg was sent */
  70. struct Queue *nxt; /* linked list */
  71. } Queue;
  72. typedef struct FSM_use { /* used in pangen5.c - dataflow */
  73. Symbol *var;
  74. int special;
  75. struct FSM_use *nxt;
  76. } FSM_use;
  77. typedef struct FSM_trans { /* used in pangen5.c - dataflow */
  78. int to;
  79. FSM_use *Val[2]; /* 0=reads, 1=writes */
  80. struct Element *step;
  81. struct FSM_trans *nxt;
  82. } FSM_trans;
  83. typedef struct FSM_state { /* used in pangen5.c - dataflow */
  84. int from; /* state number */
  85. int seen; /* used for dfs */
  86. int in; /* nr of incoming edges */
  87. FSM_trans *t; /* outgoing edges list */
  88. struct FSM_state *nxt; /* linked list of all states */
  89. } FSM_state;
  90. typedef struct Element {
  91. Lextok *n; /* defines the type & contents */
  92. int Seqno; /* identifies this el within system */
  93. int seqno; /* identifies this el within a proc */
  94. int merge; /* set by -O if step can be merged */
  95. int merge_start;
  96. int merge_single;
  97. short merge_in; /* nr of incoming edges */
  98. short merge_mark; /* state was generated in merge sequence */
  99. unsigned char status; /* used by analyzer generator */
  100. struct FSM_use *dead; /* optional dead variable list */
  101. struct SeqList *sub; /* subsequences, for compounds */
  102. struct SeqList *esc; /* zero or more escape sequences */
  103. struct Element *Nxt; /* linked list - for global lookup */
  104. struct Element *nxt; /* linked list - program structure */
  105. } Element;
  106. typedef struct Sequence {
  107. Element *frst;
  108. Element *last; /* links onto continuations */
  109. Element *extent; /* last element in original */
  110. int maxel; /* 1+largest id in sequence */
  111. } Sequence;
  112. typedef struct SeqList {
  113. Sequence *this; /* one sequence */
  114. struct SeqList *nxt; /* linked list */
  115. } SeqList;
  116. typedef struct Label {
  117. Symbol *s;
  118. Symbol *c;
  119. Element *e;
  120. int visible; /* label referenced in claim */
  121. struct Label *nxt;
  122. } Label;
  123. typedef struct Lbreak {
  124. Symbol *l;
  125. struct Lbreak *nxt;
  126. } Lbreak;
  127. typedef struct RunList {
  128. Symbol *n; /* name */
  129. int tn; /* ordinal of type */
  130. int pid; /* process id */
  131. int priority; /* for simulations only */
  132. Element *pc; /* current stmnt */
  133. Sequence *ps; /* used by analyzer generator */
  134. Lextok *prov; /* provided clause */
  135. Symbol *symtab; /* local variables */
  136. struct RunList *nxt; /* linked list */
  137. } RunList;
  138. typedef struct ProcList {
  139. Symbol *n; /* name */
  140. Lextok *p; /* parameters */
  141. Sequence *s; /* body */
  142. Lextok *prov; /* provided clause */
  143. short tn; /* ordinal number */
  144. short det; /* deterministic */
  145. struct ProcList *nxt; /* linked list */
  146. } ProcList;
  147. typedef Lextok *Lexptr;
  148. #define YYSTYPE Lexptr
  149. #define ZN (Lextok *)0
  150. #define ZS (Symbol *)0
  151. #define ZE (Element *)0
  152. #define DONE 1 /* status bits of elements */
  153. #define ATOM 2 /* part of an atomic chain */
  154. #define L_ATOM 4 /* last element in a chain */
  155. #define I_GLOB 8 /* inherited global ref */
  156. #define DONE2 16 /* used in putcode and main*/
  157. #define D_ATOM 32 /* deterministic atomic */
  158. #define ENDSTATE 64 /* normal endstate */
  159. #define CHECK2 128
  160. #define Nhash 255 /* slots in symbol hash-table */
  161. #define XR 1 /* non-shared receive-only */
  162. #define XS 2 /* non-shared send-only */
  163. #define XX 4 /* overrides XR or XS tag */
  164. #define PREDEF 3 /* predefined names: _p, _last */
  165. #define UNSIGNED 5 /* val defines width in bits */
  166. #define BIT 1 /* also equal to width in bits */
  167. #define BYTE 8 /* ditto */
  168. #define SHORT 16 /* ditto */
  169. #define INT 32 /* ditto */
  170. #define CHAN 64 /* not */
  171. #define STRUCT 128 /* user defined structure name */
  172. #ifndef max
  173. #define max(a,b) (((a)<(b)) ? (b) : (a))
  174. #endif
  175. enum { INIV, PUTV, LOGV }; /* for pangen[14].c */
  176. /***** prototype definitions *****/
  177. Element *eval_sub(Element *);
  178. Element *get_lab(Lextok *, int);
  179. Element *huntele(Element *, int);
  180. Element *huntstart(Element *);
  181. Element *target(Element *);
  182. Lextok *do_unless(Lextok *, Lextok *);
  183. Lextok *expand(Lextok *, int);
  184. Lextok *getuname(Symbol *);
  185. Lextok *mk_explicit(Lextok *, int, int);
  186. Lextok *nn(Lextok *, int, Lextok *, Lextok *);
  187. Lextok *rem_lab(Symbol *, Lextok *, Symbol *);
  188. Lextok *tail_add(Lextok *, Lextok *);
  189. ProcList *ready(Symbol *, Lextok *, Sequence *, int, Lextok *);
  190. SeqList *seqlist(Sequence *, SeqList *);
  191. Sequence *close_seq(int);
  192. Symbol *break_dest(void);
  193. Symbol *findloc(Symbol *);
  194. Symbol *lookup(char *);
  195. Symbol *has_lab(Element *, int);
  196. char *emalloc(int);
  197. long Rand(void);
  198. int any_oper(Lextok *, int);
  199. int any_undo(Lextok *);
  200. int cast_val(int, int, int);
  201. int checkvar(Symbol *, int);
  202. int Cnt_flds(Lextok *);
  203. int cnt_mpars(Lextok *);
  204. int complete_rendez(void);
  205. int enable(Lextok *);
  206. int Enabled0(Element *);
  207. int eval(Lextok *);
  208. int find_lab(Symbol *, Symbol *, int);
  209. int find_maxel(Symbol *);
  210. int full_name(FILE *, Lextok *, Symbol *, int);
  211. int getlocal(Lextok *);
  212. int getval(Lextok *);
  213. int has_typ(Lextok *, int);
  214. int interprint(FILE *, Lextok *);
  215. int ismtype(char *);
  216. int isproctype(char *);
  217. int isutype(char *);
  218. int Lval_struct(Lextok *, Symbol *, int, int);
  219. int main(int, char **);
  220. int pc_value(Lextok *);
  221. int proper_enabler(Lextok *);
  222. int putcode(FILE *, Sequence *, Element *, int, int);
  223. int q_is_sync(Lextok *);
  224. int qlen(Lextok *);
  225. int qfull(Lextok *);
  226. int qmake(Symbol *);
  227. int qrecv(Lextok *, int);
  228. int qsend(Lextok *);
  229. int remotelab(Lextok *);
  230. int remotevar(Lextok *);
  231. int Rval_struct(Lextok *, Symbol *, int);
  232. int setlocal(Lextok *, int);
  233. int setval(Lextok *, int);
  234. int sputtype(char *, int);
  235. int Sym_typ(Lextok *);
  236. int tl_main(int, char *[]);
  237. int Width_set(int *, int, Lextok *);
  238. int yyparse(void);
  239. int yywrap(void);
  240. int yylex(void);
  241. void add_seq(Lextok *);
  242. void alldone(int);
  243. void announce(char *);
  244. void chanaccess(void);
  245. void checkrun(Symbol *, int);
  246. void comment(FILE *, Lextok *, int);
  247. void cross_dsteps(Lextok *, Lextok *);
  248. void doq(Symbol *, int, RunList *);
  249. void dotag(FILE *, char *);
  250. void do_var(FILE *, int, char *, Symbol *, char *, char *, char *);
  251. void dump_struct(Symbol *, char *, RunList *);
  252. void dumpclaims(FILE *, int, char *);
  253. void dumpglobals(void);
  254. void dumplabels(void);
  255. void dumplocal(RunList *);
  256. void dumpsrc(int, int);
  257. /* void exit(int); in stdlib.h */
  258. void fatal(char *, char *);
  259. void fix_dest(Symbol *, Symbol *);
  260. void genaddproc(void);
  261. void genaddqueue(void);
  262. void genheader(void);
  263. void genother(void);
  264. void gensrc(void);
  265. void gensvmap(void);
  266. void genunio(void);
  267. void ini_struct(Symbol *);
  268. void make_atomic(Sequence *, int);
  269. void match_trail(void);
  270. void nochan_manip(Lextok *, Lextok *, int);
  271. void non_fatal(char *, char *);
  272. void ntimes(FILE *, int, int, char *c[]);
  273. void open_seq(int);
  274. void p_talk(Element *, int);
  275. void pickup_inline(Symbol *, Lextok *);
  276. void prehint(Symbol *);
  277. void prep_inline(Symbol *, Lextok *);
  278. void prune_opts(Lextok *);
  279. void pstext(int, char *);
  280. void pushbreak(void);
  281. void putname(FILE *, char *, Lextok *, int, char *);
  282. void putremote(FILE *, Lextok *, int);
  283. void putskip(int);
  284. void putsrc(Element *);
  285. void putstmnt(FILE *, Lextok *, int);
  286. void putunames(FILE *);
  287. void rem_Seq(void);
  288. void runnable(ProcList *, int, int);
  289. void sched(void);
  290. void setaccess(Symbol *, Symbol *, int, int);
  291. void set_lab(Symbol *, Element *);
  292. void setmtype(Lextok *);
  293. void setpname(Lextok *);
  294. void setptype(Lextok *, int, Lextok *);
  295. void setuname(Lextok *);
  296. void setutype(Lextok *, Symbol *, Lextok *);
  297. void setxus(Lextok *, int);
  298. void Srand(unsigned);
  299. void start_claim(int);
  300. void struct_name(Lextok *, Symbol *, int, char *);
  301. void symdump(void);
  302. void trackchanuse(Lextok *, Lextok *, int);
  303. void trackvar(Lextok *, Lextok *);
  304. void trackrun(Lextok *);
  305. void trapwonly(Lextok *, char *); /* spin.y and main.c */
  306. void typ2c(Symbol *);
  307. void typ_ck(int, int, char *);
  308. void undostmnt(Lextok *, int);
  309. void unrem_Seq(void);
  310. void unskip(int);
  311. void varcheck(Element *, Element *);
  312. void whoruns(int);
  313. void wrapup(int);
  314. void yyerror(char *, ...);