pangen6.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352
  1. /***** spin: pangen6.c *****/
  2. /* Copyright (c) 2000-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. /* Abstract syntax tree analysis / slicing (spin option -A) */
  11. /* AST_store stores the fsms's for each proctype */
  12. /* AST_track keeps track of variables used in properties */
  13. /* AST_slice starts the slicing algorithm */
  14. /* it first collects more info and then calls */
  15. /* AST_criteria to process the slice criteria */
  16. #include "spin.h"
  17. #include "y.tab.h"
  18. extern Ordered *all_names;
  19. extern FSM_use *use_free;
  20. extern FSM_state **fsm_tbl;
  21. extern FSM_state *fsm;
  22. extern int verbose, o_max;
  23. static FSM_trans *cur_t;
  24. static FSM_trans *expl_par;
  25. static FSM_trans *expl_var;
  26. static FSM_trans *explicit;
  27. extern void rel_use(FSM_use *);
  28. #define ulong unsigned long
  29. typedef struct Pair {
  30. FSM_state *h;
  31. int b;
  32. struct Pair *nxt;
  33. } Pair;
  34. typedef struct AST {
  35. ProcList *p; /* proctype decl */
  36. int i_st; /* start state */
  37. int nstates, nwords;
  38. int relevant;
  39. Pair *pairs; /* entry and exit nodes of proper subgraphs */
  40. FSM_state *fsm; /* proctype body */
  41. struct AST *nxt; /* linked list */
  42. } AST;
  43. typedef struct RPN { /* relevant proctype names */
  44. Symbol *rn;
  45. struct RPN *nxt;
  46. } RPN;
  47. typedef struct ALIAS { /* channel aliasing info */
  48. Lextok *cnm; /* this chan */
  49. int origin; /* debugging - origin of the alias */
  50. struct ALIAS *alias; /* can be an alias for these other chans */
  51. struct ALIAS *nxt; /* linked list */
  52. } ALIAS;
  53. typedef struct ChanList {
  54. Lextok *s; /* containing stmnt */
  55. Lextok *n; /* point of reference - could be struct */
  56. struct ChanList *nxt; /* linked list */
  57. } ChanList;
  58. /* a chan alias can be created in one of three ways:
  59. assignement to chan name
  60. a = b -- a is now an alias for b
  61. passing chan name as parameter in run
  62. run x(b) -- proctype x(chan a)
  63. passing chan name through channel
  64. x!b -- x?a
  65. */
  66. #define USE 1
  67. #define DEF 2
  68. #define DEREF_DEF 4
  69. #define DEREF_USE 8
  70. static AST *ast;
  71. static ALIAS *chalcur;
  72. static ALIAS *chalias;
  73. static ChanList *chanlist;
  74. static Slicer *slicer;
  75. static Slicer *rel_vars; /* all relevant variables */
  76. static int AST_Changes;
  77. static int AST_Round;
  78. static FSM_state no_state;
  79. static RPN *rpn;
  80. static int in_recv = 0;
  81. static int AST_mutual(Lextok *, Lextok *, int);
  82. static void AST_dominant(void);
  83. static void AST_hidden(void);
  84. static void AST_setcur(Lextok *);
  85. static void check_slice(Lextok *, int);
  86. static void curtail(AST *);
  87. static void def_use(Lextok *, int);
  88. static void name_AST_track(Lextok *, int);
  89. static void show_expl(void);
  90. static int
  91. AST_isini(Lextok *n) /* is this an initialized channel */
  92. { Symbol *s;
  93. if (!n || !n->sym) return 0;
  94. s = n->sym;
  95. if (s->type == CHAN)
  96. return (s->ini->ntyp == CHAN); /* freshly instantiated */
  97. if (s->type == STRUCT && n->rgt)
  98. return AST_isini(n->rgt->lft);
  99. return 0;
  100. }
  101. static void
  102. AST_var(Lextok *n, Symbol *s, int toplevel)
  103. {
  104. if (!s) return;
  105. if (toplevel)
  106. { if (s->context && s->type)
  107. printf(":%s:L:", s->context->name);
  108. else
  109. printf("G:");
  110. }
  111. printf("%s", s->name); /* array indices ignored */
  112. if (s->type == STRUCT && n && n->rgt && n->rgt->lft)
  113. { printf(":");
  114. AST_var(n->rgt->lft, n->rgt->lft->sym, 0);
  115. }
  116. }
  117. static void
  118. name_def_indices(Lextok *n, int code)
  119. {
  120. if (!n || !n->sym) return;
  121. if (n->sym->nel != 1)
  122. def_use(n->lft, code); /* process the index */
  123. if (n->sym->type == STRUCT /* and possible deeper ones */
  124. && n->rgt)
  125. name_def_indices(n->rgt->lft, code);
  126. }
  127. static void
  128. name_def_use(Lextok *n, int code)
  129. { FSM_use *u;
  130. if (!n) return;
  131. if ((code&USE)
  132. && cur_t->step
  133. && cur_t->step->n)
  134. { switch (cur_t->step->n->ntyp) {
  135. case 'c': /* possible predicate abstraction? */
  136. n->sym->colnr |= 2; /* yes */
  137. break;
  138. default:
  139. n->sym->colnr |= 1; /* no */
  140. break;
  141. }
  142. }
  143. for (u = cur_t->Val[0]; u; u = u->nxt)
  144. if (AST_mutual(n, u->n, 1)
  145. && u->special == code)
  146. return;
  147. if (use_free)
  148. { u = use_free;
  149. use_free = use_free->nxt;
  150. } else
  151. u = (FSM_use *) emalloc(sizeof(FSM_use));
  152. u->n = n;
  153. u->special = code;
  154. u->nxt = cur_t->Val[0];
  155. cur_t->Val[0] = u;
  156. name_def_indices(n, USE|(code&(~DEF))); /* not def, but perhaps deref */
  157. }
  158. static void
  159. def_use(Lextok *now, int code)
  160. { Lextok *v;
  161. if (now)
  162. switch (now->ntyp) {
  163. case '!':
  164. case UMIN:
  165. case '~':
  166. case 'c':
  167. case ENABLED:
  168. case ASSERT:
  169. case EVAL:
  170. def_use(now->lft, USE|code);
  171. break;
  172. case LEN:
  173. case FULL:
  174. case EMPTY:
  175. case NFULL:
  176. case NEMPTY:
  177. def_use(now->lft, DEREF_USE|USE|code);
  178. break;
  179. case '/':
  180. case '*':
  181. case '-':
  182. case '+':
  183. case '%':
  184. case '&':
  185. case '^':
  186. case '|':
  187. case LE:
  188. case GE:
  189. case GT:
  190. case LT:
  191. case NE:
  192. case EQ:
  193. case OR:
  194. case AND:
  195. case LSHIFT:
  196. case RSHIFT:
  197. def_use(now->lft, USE|code);
  198. def_use(now->rgt, USE|code);
  199. break;
  200. case ASGN:
  201. def_use(now->lft, DEF|code);
  202. def_use(now->rgt, USE|code);
  203. break;
  204. case TYPE: /* name in parameter list */
  205. name_def_use(now, code);
  206. break;
  207. case NAME:
  208. name_def_use(now, code);
  209. break;
  210. case RUN:
  211. name_def_use(now, USE); /* procname - not really needed */
  212. for (v = now->lft; v; v = v->rgt)
  213. def_use(v->lft, USE); /* params */
  214. break;
  215. case 's':
  216. def_use(now->lft, DEREF_DEF|DEREF_USE|USE|code);
  217. for (v = now->rgt; v; v = v->rgt)
  218. def_use(v->lft, USE|code);
  219. break;
  220. case 'r':
  221. def_use(now->lft, DEREF_DEF|DEREF_USE|USE|code);
  222. for (v = now->rgt; v; v = v->rgt)
  223. { if (v->lft->ntyp == EVAL)
  224. def_use(v->lft, code); /* will add USE */
  225. else if (v->lft->ntyp != CONST)
  226. def_use(v->lft, DEF|code);
  227. }
  228. break;
  229. case 'R':
  230. def_use(now->lft, DEREF_USE|USE|code);
  231. for (v = now->rgt; v; v = v->rgt)
  232. { if (v->lft->ntyp == EVAL)
  233. def_use(v->lft, code); /* will add USE */
  234. }
  235. break;
  236. case '?':
  237. def_use(now->lft, USE|code);
  238. if (now->rgt)
  239. { def_use(now->rgt->lft, code);
  240. def_use(now->rgt->rgt, code);
  241. }
  242. break;
  243. case PRINT:
  244. for (v = now->lft; v; v = v->rgt)
  245. def_use(v->lft, USE|code);
  246. break;
  247. case PRINTM:
  248. def_use(now->lft, USE);
  249. break;
  250. case CONST:
  251. case ELSE: /* ? */
  252. case NONPROGRESS:
  253. case PC_VAL:
  254. case 'p':
  255. case 'q':
  256. break;
  257. case '.':
  258. case GOTO:
  259. case BREAK:
  260. case '@':
  261. case D_STEP:
  262. case ATOMIC:
  263. case NON_ATOMIC:
  264. case IF:
  265. case DO:
  266. case UNLESS:
  267. case TIMEOUT:
  268. case C_CODE:
  269. case C_EXPR:
  270. default:
  271. break;
  272. }
  273. }
  274. static int
  275. AST_add_alias(Lextok *n, int nr)
  276. { ALIAS *ca;
  277. int res;
  278. for (ca = chalcur->alias; ca; ca = ca->nxt)
  279. if (AST_mutual(ca->cnm, n, 1))
  280. { res = (ca->origin&nr);
  281. ca->origin |= nr; /* 1, 2, or 4 - run, asgn, or rcv */
  282. return (res == 0); /* 0 if already there with same origin */
  283. }
  284. ca = (ALIAS *) emalloc(sizeof(ALIAS));
  285. ca->cnm = n;
  286. ca->origin = nr;
  287. ca->nxt = chalcur->alias;
  288. chalcur->alias = ca;
  289. return 1;
  290. }
  291. static void
  292. AST_run_alias(char *pn, char *s, Lextok *t, int parno)
  293. { Lextok *v;
  294. int cnt;
  295. if (!t) return;
  296. if (t->ntyp == RUN)
  297. { if (strcmp(t->sym->name, s) == 0)
  298. for (v = t->lft, cnt = 1; v; v = v->rgt, cnt++)
  299. if (cnt == parno)
  300. { AST_add_alias(v->lft, 1); /* RUN */
  301. break;
  302. }
  303. } else
  304. { AST_run_alias(pn, s, t->lft, parno);
  305. AST_run_alias(pn, s, t->rgt, parno);
  306. }
  307. }
  308. static void
  309. AST_findrun(char *s, int parno)
  310. { FSM_state *f;
  311. FSM_trans *t;
  312. AST *a;
  313. for (a = ast; a; a = a->nxt) /* automata */
  314. for (f = a->fsm; f; f = f->nxt) /* control states */
  315. for (t = f->t; t; t = t->nxt) /* transitions */
  316. { if (t->step)
  317. AST_run_alias(a->p->n->name, s, t->step->n, parno);
  318. }
  319. }
  320. static void
  321. AST_par_chans(ProcList *p) /* find local chan's init'd to chan passed as param */
  322. { Ordered *walk;
  323. Symbol *sp;
  324. for (walk = all_names; walk; walk = walk->next)
  325. { sp = walk->entry;
  326. if (sp
  327. && sp->context
  328. && strcmp(sp->context->name, p->n->name) == 0
  329. && sp->Nid >= 0 /* not itself a param */
  330. && sp->type == CHAN
  331. && sp->ini->ntyp == NAME) /* != CONST and != CHAN */
  332. { Lextok *x = nn(ZN, 0, ZN, ZN);
  333. x->sym = sp;
  334. AST_setcur(x);
  335. AST_add_alias(sp->ini, 2); /* ASGN */
  336. } }
  337. }
  338. static void
  339. AST_para(ProcList *p)
  340. { Lextok *f, *t, *c;
  341. int cnt = 0;
  342. AST_par_chans(p);
  343. for (f = p->p; f; f = f->rgt) /* list of types */
  344. for (t = f->lft; t; t = t->rgt)
  345. { if (t->ntyp != ',')
  346. c = t;
  347. else
  348. c = t->lft; /* expanded struct */
  349. cnt++;
  350. if (Sym_typ(c) == CHAN)
  351. { ALIAS *na = (ALIAS *) emalloc(sizeof(ALIAS));
  352. na->cnm = c;
  353. na->nxt = chalias;
  354. chalcur = chalias = na;
  355. #if 0
  356. printf("%s -- (par) -- ", p->n->name);
  357. AST_var(c, c->sym, 1);
  358. printf(" => <<");
  359. #endif
  360. AST_findrun(p->n->name, cnt);
  361. #if 0
  362. printf(">>\n");
  363. #endif
  364. }
  365. }
  366. }
  367. static void
  368. AST_haschan(Lextok *c)
  369. {
  370. if (!c) return;
  371. if (Sym_typ(c) == CHAN)
  372. { AST_add_alias(c, 2); /* ASGN */
  373. #if 0
  374. printf("<<");
  375. AST_var(c, c->sym, 1);
  376. printf(">>\n");
  377. #endif
  378. } else
  379. { AST_haschan(c->rgt);
  380. AST_haschan(c->lft);
  381. }
  382. }
  383. static int
  384. AST_nrpar(Lextok *n) /* 's' or 'r' */
  385. { Lextok *m;
  386. int j = 0;
  387. for (m = n->rgt; m; m = m->rgt)
  388. j++;
  389. return j;
  390. }
  391. static int
  392. AST_ord(Lextok *n, Lextok *s)
  393. { Lextok *m;
  394. int j = 0;
  395. for (m = n->rgt; m; m = m->rgt)
  396. { j++;
  397. if (s->sym == m->lft->sym)
  398. return j;
  399. }
  400. return 0;
  401. }
  402. #if 0
  403. static void
  404. AST_ownership(Symbol *s)
  405. {
  406. if (!s) return;
  407. printf("%s:", s->name);
  408. AST_ownership(s->owner);
  409. }
  410. #endif
  411. static int
  412. AST_mutual(Lextok *a, Lextok *b, int toplevel)
  413. { Symbol *as, *bs;
  414. if (!a && !b) return 1;
  415. if (!a || !b) return 0;
  416. as = a->sym;
  417. bs = b->sym;
  418. if (!as || !bs) return 0;
  419. if (toplevel && as->context != bs->context)
  420. return 0;
  421. if (as->type != bs->type)
  422. return 0;
  423. if (strcmp(as->name, bs->name) != 0)
  424. return 0;
  425. if (as->type == STRUCT && a->rgt && b->rgt)
  426. return AST_mutual(a->rgt->lft, b->rgt->lft, 0);
  427. return 1;
  428. }
  429. static void
  430. AST_setcur(Lextok *n) /* set chalcur */
  431. { ALIAS *ca;
  432. for (ca = chalias; ca; ca = ca->nxt)
  433. if (AST_mutual(ca->cnm, n, 1)) /* if same chan */
  434. { chalcur = ca;
  435. return;
  436. }
  437. ca = (ALIAS *) emalloc(sizeof(ALIAS));
  438. ca->cnm = n;
  439. ca->nxt = chalias;
  440. chalcur = chalias = ca;
  441. }
  442. static void
  443. AST_other(AST *a) /* check chan params in asgns and recvs */
  444. { FSM_state *f;
  445. FSM_trans *t;
  446. FSM_use *u;
  447. ChanList *cl;
  448. for (f = a->fsm; f; f = f->nxt) /* control states */
  449. for (t = f->t; t; t = t->nxt) /* transitions */
  450. for (u = t->Val[0]; u; u = u->nxt) /* def/use info */
  451. if (Sym_typ(u->n) == CHAN
  452. && (u->special&DEF)) /* def of chan-name */
  453. { AST_setcur(u->n);
  454. switch (t->step->n->ntyp) {
  455. case ASGN:
  456. AST_haschan(t->step->n->rgt);
  457. break;
  458. case 'r':
  459. /* guess sends where name may originate */
  460. for (cl = chanlist; cl; cl = cl->nxt) /* all sends */
  461. { int a = AST_nrpar(cl->s);
  462. int b = AST_nrpar(t->step->n);
  463. if (a != b) /* matching nrs of params */
  464. continue;
  465. a = AST_ord(cl->s, cl->n);
  466. b = AST_ord(t->step->n, u->n);
  467. if (a != b) /* same position in parlist */
  468. continue;
  469. AST_add_alias(cl->n, 4); /* RCV assume possible match */
  470. }
  471. break;
  472. default:
  473. printf("type = %d\n", t->step->n->ntyp);
  474. non_fatal("unexpected chan def type", (char *) 0);
  475. break;
  476. } }
  477. }
  478. static void
  479. AST_aliases(void)
  480. { ALIAS *na, *ca;
  481. for (na = chalias; na; na = na->nxt)
  482. { printf("\npossible aliases of ");
  483. AST_var(na->cnm, na->cnm->sym, 1);
  484. printf("\n\t");
  485. for (ca = na->alias; ca; ca = ca->nxt)
  486. { if (!ca->cnm->sym)
  487. printf("no valid name ");
  488. else
  489. AST_var(ca->cnm, ca->cnm->sym, 1);
  490. printf("<");
  491. if (ca->origin & 1) printf("RUN ");
  492. if (ca->origin & 2) printf("ASGN ");
  493. if (ca->origin & 4) printf("RCV ");
  494. printf("[%s]", AST_isini(ca->cnm)?"Initzd":"Name");
  495. printf(">");
  496. if (ca->nxt) printf(", ");
  497. }
  498. printf("\n");
  499. }
  500. printf("\n");
  501. }
  502. static void
  503. AST_indirect(FSM_use *uin, FSM_trans *t, char *cause, char *pn)
  504. { FSM_use *u;
  505. /* this is a newly discovered relevant statement */
  506. /* all vars it uses to contribute to its DEF are new criteria */
  507. if (!(t->relevant&1)) AST_Changes++;
  508. t->round = AST_Round;
  509. t->relevant = 1;
  510. if ((verbose&32) && t->step)
  511. { printf("\tDR %s [[ ", pn);
  512. comment(stdout, t->step->n, 0);
  513. printf("]]\n\t\tfully relevant %s", cause);
  514. if (uin) { printf(" due to "); AST_var(uin->n, uin->n->sym, 1); }
  515. printf("\n");
  516. }
  517. for (u = t->Val[0]; u; u = u->nxt)
  518. if (u != uin
  519. && (u->special&(USE|DEREF_USE)))
  520. { if (verbose&32)
  521. { printf("\t\t\tuses(%d): ", u->special);
  522. AST_var(u->n, u->n->sym, 1);
  523. printf("\n");
  524. }
  525. name_AST_track(u->n, u->special); /* add to slice criteria */
  526. }
  527. }
  528. static void
  529. def_relevant(char *pn, FSM_trans *t, Lextok *n, int ischan)
  530. { FSM_use *u;
  531. ALIAS *na, *ca;
  532. int chanref;
  533. /* look for all DEF's of n
  534. * mark those stmnts relevant
  535. * mark all var USEs in those stmnts as criteria
  536. */
  537. if (n->ntyp != ELSE)
  538. for (u = t->Val[0]; u; u = u->nxt)
  539. { chanref = (Sym_typ(u->n) == CHAN);
  540. if (ischan != chanref /* no possible match */
  541. || !(u->special&(DEF|DEREF_DEF))) /* not a def */
  542. continue;
  543. if (AST_mutual(u->n, n, 1))
  544. { AST_indirect(u, t, "(exact match)", pn);
  545. continue;
  546. }
  547. if (chanref)
  548. for (na = chalias; na; na = na->nxt)
  549. { if (!AST_mutual(u->n, na->cnm, 1))
  550. continue;
  551. for (ca = na->alias; ca; ca = ca->nxt)
  552. if (AST_mutual(ca->cnm, n, 1)
  553. && AST_isini(ca->cnm))
  554. { AST_indirect(u, t, "(alias match)", pn);
  555. break;
  556. }
  557. if (ca) break;
  558. } }
  559. }
  560. static void
  561. AST_relevant(Lextok *n)
  562. { AST *a;
  563. FSM_state *f;
  564. FSM_trans *t;
  565. int ischan;
  566. /* look for all DEF's of n
  567. * mark those stmnts relevant
  568. * mark all var USEs in those stmnts as criteria
  569. */
  570. if (!n) return;
  571. ischan = (Sym_typ(n) == CHAN);
  572. if (verbose&32)
  573. { printf("<<ast_relevant (ntyp=%d) ", n->ntyp);
  574. AST_var(n, n->sym, 1);
  575. printf(">>\n");
  576. }
  577. for (t = expl_par; t; t = t->nxt) /* param assignments */
  578. { if (!(t->relevant&1))
  579. def_relevant(":params:", t, n, ischan);
  580. }
  581. for (t = expl_var; t; t = t->nxt)
  582. { if (!(t->relevant&1)) /* var inits */
  583. def_relevant(":vars:", t, n, ischan);
  584. }
  585. for (a = ast; a; a = a->nxt) /* all other stmnts */
  586. { if (strcmp(a->p->n->name, ":never:") != 0
  587. && strcmp(a->p->n->name, ":trace:") != 0
  588. && strcmp(a->p->n->name, ":notrace:") != 0)
  589. for (f = a->fsm; f; f = f->nxt)
  590. for (t = f->t; t; t = t->nxt)
  591. { if (!(t->relevant&1))
  592. def_relevant(a->p->n->name, t, n, ischan);
  593. } }
  594. }
  595. static int
  596. AST_relpar(char *s)
  597. { FSM_trans *t, *T;
  598. FSM_use *u;
  599. for (T = expl_par; T; T = (T == expl_par)?expl_var: (FSM_trans *) 0)
  600. for (t = T; t; t = t->nxt)
  601. { if (t->relevant&1)
  602. for (u = t->Val[0]; u; u = u->nxt)
  603. { if (u->n->sym->type
  604. && u->n->sym->context
  605. && strcmp(u->n->sym->context->name, s) == 0)
  606. {
  607. if (verbose&32)
  608. { printf("proctype %s relevant, due to symbol ", s);
  609. AST_var(u->n, u->n->sym, 1);
  610. printf("\n");
  611. }
  612. return 1;
  613. } } }
  614. return 0;
  615. }
  616. static void
  617. AST_dorelevant(void)
  618. { AST *a;
  619. RPN *r;
  620. for (r = rpn; r; r = r->nxt)
  621. { for (a = ast; a; a = a->nxt)
  622. if (strcmp(a->p->n->name, r->rn->name) == 0)
  623. { a->relevant |= 1;
  624. break;
  625. }
  626. if (!a)
  627. fatal("cannot find proctype %s", r->rn->name);
  628. }
  629. }
  630. static void
  631. AST_procisrelevant(Symbol *s)
  632. { RPN *r;
  633. for (r = rpn; r; r = r->nxt)
  634. if (strcmp(r->rn->name, s->name) == 0)
  635. return;
  636. r = (RPN *) emalloc(sizeof(RPN));
  637. r->rn = s;
  638. r->nxt = rpn;
  639. rpn = r;
  640. }
  641. static int
  642. AST_proc_isrel(char *s)
  643. { AST *a;
  644. for (a = ast; a; a = a->nxt)
  645. if (strcmp(a->p->n->name, s) == 0)
  646. return (a->relevant&1);
  647. non_fatal("cannot happen, missing proc in ast", (char *) 0);
  648. return 0;
  649. }
  650. static int
  651. AST_scoutrun(Lextok *t)
  652. {
  653. if (!t) return 0;
  654. if (t->ntyp == RUN)
  655. return AST_proc_isrel(t->sym->name);
  656. return (AST_scoutrun(t->lft) || AST_scoutrun(t->rgt));
  657. }
  658. static void
  659. AST_tagruns(void)
  660. { AST *a;
  661. FSM_state *f;
  662. FSM_trans *t;
  663. /* if any stmnt inside a proctype is relevant
  664. * or any parameter passed in a run
  665. * then so are all the run statements on that proctype
  666. */
  667. for (a = ast; a; a = a->nxt)
  668. { if (strcmp(a->p->n->name, ":never:") == 0
  669. || strcmp(a->p->n->name, ":trace:") == 0
  670. || strcmp(a->p->n->name, ":notrace:") == 0
  671. || strcmp(a->p->n->name, ":init:") == 0)
  672. { a->relevant |= 1; /* the proctype is relevant */
  673. continue;
  674. }
  675. if (AST_relpar(a->p->n->name))
  676. a->relevant |= 1;
  677. else
  678. { for (f = a->fsm; f; f = f->nxt)
  679. for (t = f->t; t; t = t->nxt)
  680. if (t->relevant)
  681. goto yes;
  682. yes: if (f)
  683. a->relevant |= 1;
  684. }
  685. }
  686. for (a = ast; a; a = a->nxt)
  687. for (f = a->fsm; f; f = f->nxt)
  688. for (t = f->t; t; t = t->nxt)
  689. if (t->step
  690. && AST_scoutrun(t->step->n))
  691. { AST_indirect((FSM_use *)0, t, ":run:", a->p->n->name);
  692. /* BUT, not all actual params are relevant */
  693. }
  694. }
  695. static void
  696. AST_report(AST *a, Element *e) /* ALSO deduce irrelevant vars */
  697. {
  698. if (!(a->relevant&2))
  699. { a->relevant |= 2;
  700. printf("spin: redundant in proctype %s (for given property):\n",
  701. a->p->n->name);
  702. }
  703. printf(" line %3d %s (state %d)",
  704. e->n?e->n->ln:-1,
  705. e->n?e->n->fn->name:"-",
  706. e->seqno);
  707. printf(" [");
  708. comment(stdout, e->n, 0);
  709. printf("]\n");
  710. }
  711. static int
  712. AST_always(Lextok *n)
  713. {
  714. if (!n) return 0;
  715. if (n->ntyp == '@' /* -end */
  716. || n->ntyp == 'p') /* remote reference */
  717. return 1;
  718. return AST_always(n->lft) || AST_always(n->rgt);
  719. }
  720. static void
  721. AST_edge_dump(AST *a, FSM_state *f)
  722. { FSM_trans *t;
  723. FSM_use *u;
  724. for (t = f->t; t; t = t->nxt) /* edges */
  725. {
  726. if (t->step && AST_always(t->step->n))
  727. t->relevant |= 1; /* always relevant */
  728. if (verbose&32)
  729. { switch (t->relevant) {
  730. case 0: printf(" "); break;
  731. case 1: printf("*%3d ", t->round); break;
  732. case 2: printf("+%3d ", t->round); break;
  733. case 3: printf("#%3d ", t->round); break;
  734. default: printf("? "); break;
  735. }
  736. printf("%d\t->\t%d\t", f->from, t->to);
  737. if (t->step)
  738. comment(stdout, t->step->n, 0);
  739. else
  740. printf("Unless");
  741. for (u = t->Val[0]; u; u = u->nxt)
  742. { printf(" <");
  743. AST_var(u->n, u->n->sym, 1);
  744. printf(":%d>", u->special);
  745. }
  746. printf("\n");
  747. } else
  748. { if (t->relevant)
  749. continue;
  750. if (t->step)
  751. switch(t->step->n->ntyp) {
  752. case ASGN:
  753. case 's':
  754. case 'r':
  755. case 'c':
  756. if (t->step->n->lft->ntyp != CONST)
  757. AST_report(a, t->step);
  758. break;
  759. case PRINT: /* don't report */
  760. case PRINTM:
  761. case ASSERT:
  762. case C_CODE:
  763. case C_EXPR:
  764. default:
  765. break;
  766. } } }
  767. }
  768. static void
  769. AST_dfs(AST *a, int s, int vis)
  770. { FSM_state *f;
  771. FSM_trans *t;
  772. f = fsm_tbl[s];
  773. if (f->seen) return;
  774. f->seen = 1;
  775. if (vis) AST_edge_dump(a, f);
  776. for (t = f->t; t; t = t->nxt)
  777. AST_dfs(a, t->to, vis);
  778. }
  779. static void
  780. AST_dump(AST *a)
  781. { FSM_state *f;
  782. for (f = a->fsm; f; f = f->nxt)
  783. { f->seen = 0;
  784. fsm_tbl[f->from] = f;
  785. }
  786. if (verbose&32)
  787. printf("AST_START %s from %d\n", a->p->n->name, a->i_st);
  788. AST_dfs(a, a->i_st, 1);
  789. }
  790. static void
  791. AST_sends(AST *a)
  792. { FSM_state *f;
  793. FSM_trans *t;
  794. FSM_use *u;
  795. ChanList *cl;
  796. for (f = a->fsm; f; f = f->nxt) /* control states */
  797. for (t = f->t; t; t = t->nxt) /* transitions */
  798. { if (t->step
  799. && t->step->n
  800. && t->step->n->ntyp == 's')
  801. for (u = t->Val[0]; u; u = u->nxt)
  802. { if (Sym_typ(u->n) == CHAN
  803. && ((u->special&USE) && !(u->special&DEREF_USE)))
  804. {
  805. #if 0
  806. printf("%s -- (%d->%d) -- ",
  807. a->p->n->name, f->from, t->to);
  808. AST_var(u->n, u->n->sym, 1);
  809. printf(" -> chanlist\n");
  810. #endif
  811. cl = (ChanList *) emalloc(sizeof(ChanList));
  812. cl->s = t->step->n;
  813. cl->n = u->n;
  814. cl->nxt = chanlist;
  815. chanlist = cl;
  816. } } } }
  817. static ALIAS *
  818. AST_alfind(Lextok *n)
  819. { ALIAS *na;
  820. for (na = chalias; na; na = na->nxt)
  821. if (AST_mutual(na->cnm, n, 1))
  822. return na;
  823. return (ALIAS *) 0;
  824. }
  825. static void
  826. AST_trans(void)
  827. { ALIAS *na, *ca, *da, *ea;
  828. int nchanges;
  829. do {
  830. nchanges = 0;
  831. for (na = chalias; na; na = na->nxt)
  832. { chalcur = na;
  833. for (ca = na->alias; ca; ca = ca->nxt)
  834. { da = AST_alfind(ca->cnm);
  835. if (da)
  836. for (ea = da->alias; ea; ea = ea->nxt)
  837. { nchanges += AST_add_alias(ea->cnm,
  838. ea->origin|ca->origin);
  839. } } }
  840. } while (nchanges > 0);
  841. chalcur = (ALIAS *) 0;
  842. }
  843. static void
  844. AST_def_use(AST *a)
  845. { FSM_state *f;
  846. FSM_trans *t;
  847. for (f = a->fsm; f; f = f->nxt) /* control states */
  848. for (t = f->t; t; t = t->nxt) /* all edges */
  849. { cur_t = t;
  850. rel_use(t->Val[0]); /* redo Val; doesn't cover structs */
  851. rel_use(t->Val[1]);
  852. t->Val[0] = t->Val[1] = (FSM_use *) 0;
  853. if (!t->step) continue;
  854. def_use(t->step->n, 0); /* def/use info, including structs */
  855. }
  856. cur_t = (FSM_trans *) 0;
  857. }
  858. static void
  859. name_AST_track(Lextok *n, int code)
  860. { extern int nr_errs;
  861. #if 0
  862. printf("AST_name: ");
  863. AST_var(n, n->sym, 1);
  864. printf(" -- %d\n", code);
  865. #endif
  866. if (in_recv && (code&DEF) && (code&USE))
  867. { printf("spin: error: DEF and USE of same var in rcv stmnt: ");
  868. AST_var(n, n->sym, 1);
  869. printf(" -- %d\n", code);
  870. nr_errs++;
  871. }
  872. check_slice(n, code);
  873. }
  874. void
  875. AST_track(Lextok *now, int code) /* called from main.c */
  876. { Lextok *v; extern int export_ast;
  877. if (!export_ast) return;
  878. if (now)
  879. switch (now->ntyp) {
  880. case LEN:
  881. case FULL:
  882. case EMPTY:
  883. case NFULL:
  884. case NEMPTY:
  885. AST_track(now->lft, DEREF_USE|USE|code);
  886. break;
  887. case '/':
  888. case '*':
  889. case '-':
  890. case '+':
  891. case '%':
  892. case '&':
  893. case '^':
  894. case '|':
  895. case LE:
  896. case GE:
  897. case GT:
  898. case LT:
  899. case NE:
  900. case EQ:
  901. case OR:
  902. case AND:
  903. case LSHIFT:
  904. case RSHIFT:
  905. AST_track(now->rgt, USE|code);
  906. /* fall through */
  907. case '!':
  908. case UMIN:
  909. case '~':
  910. case 'c':
  911. case ENABLED:
  912. case ASSERT:
  913. AST_track(now->lft, USE|code);
  914. break;
  915. case EVAL:
  916. AST_track(now->lft, USE|(code&(~DEF)));
  917. break;
  918. case NAME:
  919. name_AST_track(now, code);
  920. if (now->sym->nel != 1)
  921. AST_track(now->lft, USE|code); /* index */
  922. break;
  923. case 'R':
  924. AST_track(now->lft, DEREF_USE|USE|code);
  925. for (v = now->rgt; v; v = v->rgt)
  926. AST_track(v->lft, code); /* a deeper eval can add USE */
  927. break;
  928. case '?':
  929. AST_track(now->lft, USE|code);
  930. if (now->rgt)
  931. { AST_track(now->rgt->lft, code);
  932. AST_track(now->rgt->rgt, code);
  933. }
  934. break;
  935. /* added for control deps: */
  936. case TYPE:
  937. name_AST_track(now, code);
  938. break;
  939. case ASGN:
  940. AST_track(now->lft, DEF|code);
  941. AST_track(now->rgt, USE|code);
  942. break;
  943. case RUN:
  944. name_AST_track(now, USE);
  945. for (v = now->lft; v; v = v->rgt)
  946. AST_track(v->lft, USE|code);
  947. break;
  948. case 's':
  949. AST_track(now->lft, DEREF_DEF|DEREF_USE|USE|code);
  950. for (v = now->rgt; v; v = v->rgt)
  951. AST_track(v->lft, USE|code);
  952. break;
  953. case 'r':
  954. AST_track(now->lft, DEREF_DEF|DEREF_USE|USE|code);
  955. for (v = now->rgt; v; v = v->rgt)
  956. { in_recv++;
  957. AST_track(v->lft, DEF|code);
  958. in_recv--;
  959. }
  960. break;
  961. case PRINT:
  962. for (v = now->lft; v; v = v->rgt)
  963. AST_track(v->lft, USE|code);
  964. break;
  965. case PRINTM:
  966. AST_track(now->lft, USE);
  967. break;
  968. /* end add */
  969. case 'p':
  970. #if 0
  971. 'p' -sym-> _p
  972. /
  973. '?' -sym-> a (proctype)
  974. /
  975. b (pid expr)
  976. #endif
  977. AST_track(now->lft->lft, USE|code);
  978. AST_procisrelevant(now->lft->sym);
  979. break;
  980. case CONST:
  981. case ELSE:
  982. case NONPROGRESS:
  983. case PC_VAL:
  984. case 'q':
  985. break;
  986. case '.':
  987. case GOTO:
  988. case BREAK:
  989. case '@':
  990. case D_STEP:
  991. case ATOMIC:
  992. case NON_ATOMIC:
  993. case IF:
  994. case DO:
  995. case UNLESS:
  996. case TIMEOUT:
  997. case C_CODE:
  998. case C_EXPR:
  999. break;
  1000. default:
  1001. printf("AST_track, NOT EXPECTED ntyp: %d\n", now->ntyp);
  1002. break;
  1003. }
  1004. }
  1005. static int
  1006. AST_dump_rel(void)
  1007. { Slicer *rv;
  1008. Ordered *walk;
  1009. char buf[64];
  1010. int banner=0;
  1011. if (verbose&32)
  1012. { printf("Relevant variables:\n");
  1013. for (rv = rel_vars; rv; rv = rv->nxt)
  1014. { printf("\t");
  1015. AST_var(rv->n, rv->n->sym, 1);
  1016. printf("\n");
  1017. }
  1018. return 1;
  1019. }
  1020. for (rv = rel_vars; rv; rv = rv->nxt)
  1021. rv->n->sym->setat = 1; /* mark it */
  1022. for (walk = all_names; walk; walk = walk->next)
  1023. { Symbol *s;
  1024. s = walk->entry;
  1025. if (!s->setat
  1026. && (s->type != MTYPE || s->ini->ntyp != CONST)
  1027. && s->type != STRUCT /* report only fields */
  1028. && s->type != PROCTYPE
  1029. && !s->owner
  1030. && sputtype(buf, s->type))
  1031. { if (!banner)
  1032. { banner = 1;
  1033. printf("spin: redundant vars (for given property):\n");
  1034. }
  1035. printf("\t");
  1036. symvar(s);
  1037. } }
  1038. return banner;
  1039. }
  1040. static void
  1041. AST_suggestions(void)
  1042. { Symbol *s;
  1043. Ordered *walk;
  1044. FSM_state *f;
  1045. FSM_trans *t;
  1046. AST *a;
  1047. int banner=0;
  1048. int talked=0;
  1049. for (walk = all_names; walk; walk = walk->next)
  1050. { s = walk->entry;
  1051. if (s->colnr == 2 /* only used in conditionals */
  1052. && (s->type == BYTE
  1053. || s->type == SHORT
  1054. || s->type == INT
  1055. || s->type == MTYPE))
  1056. { if (!banner)
  1057. { banner = 1;
  1058. printf("spin: consider using predicate");
  1059. printf(" abstraction to replace:\n");
  1060. }
  1061. printf("\t");
  1062. symvar(s);
  1063. } }
  1064. /* look for source and sink processes */
  1065. for (a = ast; a; a = a->nxt) /* automata */
  1066. { banner = 0;
  1067. for (f = a->fsm; f; f = f->nxt) /* control states */
  1068. for (t = f->t; t; t = t->nxt) /* transitions */
  1069. { if (t->step)
  1070. switch (t->step->n->ntyp) {
  1071. case 's':
  1072. banner |= 1;
  1073. break;
  1074. case 'r':
  1075. banner |= 2;
  1076. break;
  1077. case '.':
  1078. case D_STEP:
  1079. case ATOMIC:
  1080. case NON_ATOMIC:
  1081. case IF:
  1082. case DO:
  1083. case UNLESS:
  1084. case '@':
  1085. case GOTO:
  1086. case BREAK:
  1087. case PRINT:
  1088. case PRINTM:
  1089. case ASSERT:
  1090. case C_CODE:
  1091. case C_EXPR:
  1092. break;
  1093. default:
  1094. banner |= 4;
  1095. goto no_good;
  1096. }
  1097. }
  1098. no_good: if (banner == 1 || banner == 2)
  1099. { printf("spin: proctype %s defines a %s process\n",
  1100. a->p->n->name,
  1101. banner==1?"source":"sink");
  1102. talked |= banner;
  1103. } else if (banner == 3)
  1104. { printf("spin: proctype %s mimics a buffer\n",
  1105. a->p->n->name);
  1106. talked |= 4;
  1107. }
  1108. }
  1109. if (talked&1)
  1110. { printf("\tto reduce complexity, consider merging the code of\n");
  1111. printf("\teach source process into the code of its target\n");
  1112. }
  1113. if (talked&2)
  1114. { printf("\tto reduce complexity, consider merging the code of\n");
  1115. printf("\teach sink process into the code of its source\n");
  1116. }
  1117. if (talked&4)
  1118. printf("\tto reduce complexity, avoid buffer processes\n");
  1119. }
  1120. static void
  1121. AST_preserve(void)
  1122. { Slicer *sc, *nx, *rv;
  1123. for (sc = slicer; sc; sc = nx)
  1124. { if (!sc->used)
  1125. break; /* done */
  1126. nx = sc->nxt;
  1127. for (rv = rel_vars; rv; rv = rv->nxt)
  1128. if (AST_mutual(sc->n, rv->n, 1))
  1129. break;
  1130. if (!rv) /* not already there */
  1131. { sc->nxt = rel_vars;
  1132. rel_vars = sc;
  1133. } }
  1134. slicer = sc;
  1135. }
  1136. static void
  1137. check_slice(Lextok *n, int code)
  1138. { Slicer *sc;
  1139. for (sc = slicer; sc; sc = sc->nxt)
  1140. if (AST_mutual(sc->n, n, 1)
  1141. && sc->code == code)
  1142. return; /* already there */
  1143. sc = (Slicer *) emalloc(sizeof(Slicer));
  1144. sc->n = n;
  1145. sc->code = code;
  1146. sc->used = 0;
  1147. sc->nxt = slicer;
  1148. slicer = sc;
  1149. }
  1150. static void
  1151. AST_data_dep(void)
  1152. { Slicer *sc;
  1153. /* mark all def-relevant transitions */
  1154. for (sc = slicer; sc; sc = sc->nxt)
  1155. { sc->used = 1;
  1156. if (verbose&32)
  1157. { printf("spin: slice criterion ");
  1158. AST_var(sc->n, sc->n->sym, 1);
  1159. printf(" type=%d\n", Sym_typ(sc->n));
  1160. }
  1161. AST_relevant(sc->n);
  1162. }
  1163. AST_tagruns(); /* mark 'run's relevant if target proctype is relevant */
  1164. }
  1165. static int
  1166. AST_blockable(AST *a, int s)
  1167. { FSM_state *f;
  1168. FSM_trans *t;
  1169. f = fsm_tbl[s];
  1170. for (t = f->t; t; t = t->nxt)
  1171. { if (t->relevant&2)
  1172. return 1;
  1173. if (t->step && t->step->n)
  1174. switch (t->step->n->ntyp) {
  1175. case IF:
  1176. case DO:
  1177. case ATOMIC:
  1178. case NON_ATOMIC:
  1179. case D_STEP:
  1180. if (AST_blockable(a, t->to))
  1181. { t->round = AST_Round;
  1182. t->relevant |= 2;
  1183. return 1;
  1184. }
  1185. /* else fall through */
  1186. default:
  1187. break;
  1188. }
  1189. else if (AST_blockable(a, t->to)) /* Unless */
  1190. { t->round = AST_Round;
  1191. t->relevant |= 2;
  1192. return 1;
  1193. }
  1194. }
  1195. return 0;
  1196. }
  1197. static void
  1198. AST_spread(AST *a, int s)
  1199. { FSM_state *f;
  1200. FSM_trans *t;
  1201. f = fsm_tbl[s];
  1202. for (t = f->t; t; t = t->nxt)
  1203. { if (t->relevant&2)
  1204. continue;
  1205. if (t->step && t->step->n)
  1206. switch (t->step->n->ntyp) {
  1207. case IF:
  1208. case DO:
  1209. case ATOMIC:
  1210. case NON_ATOMIC:
  1211. case D_STEP:
  1212. AST_spread(a, t->to);
  1213. /* fall thru */
  1214. default:
  1215. t->round = AST_Round;
  1216. t->relevant |= 2;
  1217. break;
  1218. }
  1219. else /* Unless */
  1220. { AST_spread(a, t->to);
  1221. t->round = AST_Round;
  1222. t->relevant |= 2;
  1223. }
  1224. }
  1225. }
  1226. static int
  1227. AST_notrelevant(Lextok *n)
  1228. { Slicer *s;
  1229. for (s = rel_vars; s; s = s->nxt)
  1230. if (AST_mutual(s->n, n, 1))
  1231. return 0;
  1232. for (s = slicer; s; s = s->nxt)
  1233. if (AST_mutual(s->n, n, 1))
  1234. return 0;
  1235. return 1;
  1236. }
  1237. static int
  1238. AST_withchan(Lextok *n)
  1239. {
  1240. if (!n) return 0;
  1241. if (Sym_typ(n) == CHAN)
  1242. return 1;
  1243. return AST_withchan(n->lft) || AST_withchan(n->rgt);
  1244. }
  1245. static int
  1246. AST_suspect(FSM_trans *t)
  1247. { FSM_use *u;
  1248. /* check for possible overkill */
  1249. if (!t || !t->step || !AST_withchan(t->step->n))
  1250. return 0;
  1251. for (u = t->Val[0]; u; u = u->nxt)
  1252. if (AST_notrelevant(u->n))
  1253. return 1;
  1254. return 0;
  1255. }
  1256. static void
  1257. AST_shouldconsider(AST *a, int s)
  1258. { FSM_state *f;
  1259. FSM_trans *t;
  1260. f = fsm_tbl[s];
  1261. for (t = f->t; t; t = t->nxt)
  1262. { if (t->step && t->step->n)
  1263. switch (t->step->n->ntyp) {
  1264. case IF:
  1265. case DO:
  1266. case ATOMIC:
  1267. case NON_ATOMIC:
  1268. case D_STEP:
  1269. AST_shouldconsider(a, t->to);
  1270. break;
  1271. default:
  1272. AST_track(t->step->n, 0);
  1273. /*
  1274. AST_track is called here for a blockable stmnt from which
  1275. a relevant stmnmt was shown to be reachable
  1276. for a condition this makes all USEs relevant
  1277. but for a channel operation it only makes the executability
  1278. relevant -- in those cases, parameters that aren't already
  1279. relevant may be replaceable with arbitrary tokens
  1280. */
  1281. if (AST_suspect(t))
  1282. { printf("spin: possibly redundant parameters in: ");
  1283. comment(stdout, t->step->n, 0);
  1284. printf("\n");
  1285. }
  1286. break;
  1287. }
  1288. else /* an Unless */
  1289. AST_shouldconsider(a, t->to);
  1290. }
  1291. }
  1292. static int
  1293. FSM_critical(AST *a, int s)
  1294. { FSM_state *f;
  1295. FSM_trans *t;
  1296. /* is a 1-relevant stmnt reachable from this state? */
  1297. f = fsm_tbl[s];
  1298. if (f->seen)
  1299. goto done;
  1300. f->seen = 1;
  1301. f->cr = 0;
  1302. for (t = f->t; t; t = t->nxt)
  1303. if ((t->relevant&1)
  1304. || FSM_critical(a, t->to))
  1305. { f->cr = 1;
  1306. if (verbose&32)
  1307. { printf("\t\t\t\tcritical(%d) ", t->relevant);
  1308. comment(stdout, t->step->n, 0);
  1309. printf("\n");
  1310. }
  1311. break;
  1312. }
  1313. #if 0
  1314. else {
  1315. if (verbose&32)
  1316. { printf("\t\t\t\tnot-crit ");
  1317. comment(stdout, t->step->n, 0);
  1318. printf("\n");
  1319. }
  1320. }
  1321. #endif
  1322. done:
  1323. return f->cr;
  1324. }
  1325. static void
  1326. AST_ctrl(AST *a)
  1327. { FSM_state *f;
  1328. FSM_trans *t;
  1329. int hit;
  1330. /* add all blockable transitions
  1331. * from which relevant transitions can be reached
  1332. */
  1333. if (verbose&32)
  1334. printf("CTL -- %s\n", a->p->n->name);
  1335. /* 1 : mark all blockable edges */
  1336. for (f = a->fsm; f; f = f->nxt)
  1337. { if (!(f->scratch&2)) /* not part of irrelevant subgraph */
  1338. for (t = f->t; t; t = t->nxt)
  1339. { if (t->step && t->step->n)
  1340. switch (t->step->n->ntyp) {
  1341. case 'r':
  1342. case 's':
  1343. case 'c':
  1344. case ELSE:
  1345. t->round = AST_Round;
  1346. t->relevant |= 2; /* mark for next phases */
  1347. if (verbose&32)
  1348. { printf("\tpremark ");
  1349. comment(stdout, t->step->n, 0);
  1350. printf("\n");
  1351. }
  1352. break;
  1353. default:
  1354. break;
  1355. } } }
  1356. /* 2: keep only 2-marked stmnts from which 1-marked stmnts can be reached */
  1357. for (f = a->fsm; f; f = f->nxt)
  1358. { fsm_tbl[f->from] = f;
  1359. f->seen = 0; /* used in dfs from FSM_critical */
  1360. }
  1361. for (f = a->fsm; f; f = f->nxt)
  1362. { if (!FSM_critical(a, f->from))
  1363. for (t = f->t; t; t = t->nxt)
  1364. if (t->relevant&2)
  1365. { t->relevant &= ~2; /* clear mark */
  1366. if (verbose&32)
  1367. { printf("\t\tnomark ");
  1368. comment(stdout, t->step->n, 0);
  1369. printf("\n");
  1370. } } }
  1371. /* 3 : lift marks across IF/DO etc. */
  1372. for (f = a->fsm; f; f = f->nxt)
  1373. { hit = 0;
  1374. for (t = f->t; t; t = t->nxt)
  1375. { if (t->step && t->step->n)
  1376. switch (t->step->n->ntyp) {
  1377. case IF:
  1378. case DO:
  1379. case ATOMIC:
  1380. case NON_ATOMIC:
  1381. case D_STEP:
  1382. if (AST_blockable(a, t->to))
  1383. hit = 1;
  1384. break;
  1385. default:
  1386. break;
  1387. }
  1388. else if (AST_blockable(a, t->to)) /* Unless */
  1389. hit = 1;
  1390. if (hit) break;
  1391. }
  1392. if (hit) /* at least one outgoing trans can block */
  1393. for (t = f->t; t; t = t->nxt)
  1394. { t->round = AST_Round;
  1395. t->relevant |= 2; /* lift */
  1396. if (verbose&32)
  1397. { printf("\t\t\tliftmark ");
  1398. comment(stdout, t->step->n, 0);
  1399. printf("\n");
  1400. }
  1401. AST_spread(a, t->to); /* and spread to all guards */
  1402. } }
  1403. /* 4: nodes with 2-marked out-edges contribute new slice criteria */
  1404. for (f = a->fsm; f; f = f->nxt)
  1405. for (t = f->t; t; t = t->nxt)
  1406. if (t->relevant&2)
  1407. { AST_shouldconsider(a, f->from);
  1408. break; /* inner loop */
  1409. }
  1410. }
  1411. static void
  1412. AST_control_dep(void)
  1413. { AST *a;
  1414. for (a = ast; a; a = a->nxt)
  1415. if (strcmp(a->p->n->name, ":never:") != 0
  1416. && strcmp(a->p->n->name, ":trace:") != 0
  1417. && strcmp(a->p->n->name, ":notrace:") != 0)
  1418. AST_ctrl(a);
  1419. }
  1420. static void
  1421. AST_prelabel(void)
  1422. { AST *a;
  1423. FSM_state *f;
  1424. FSM_trans *t;
  1425. for (a = ast; a; a = a->nxt)
  1426. { if (strcmp(a->p->n->name, ":never:") != 0
  1427. && strcmp(a->p->n->name, ":trace:") != 0
  1428. && strcmp(a->p->n->name, ":notrace:") != 0)
  1429. for (f = a->fsm; f; f = f->nxt)
  1430. for (t = f->t; t; t = t->nxt)
  1431. { if (t->step
  1432. && t->step->n
  1433. && t->step->n->ntyp == ASSERT
  1434. )
  1435. { t->relevant |= 1;
  1436. } } }
  1437. }
  1438. static void
  1439. AST_criteria(void)
  1440. { /*
  1441. * remote labels are handled separately -- by making
  1442. * sure they are not pruned away during optimization
  1443. */
  1444. AST_Changes = 1; /* to get started */
  1445. for (AST_Round = 1; slicer && AST_Changes; AST_Round++)
  1446. { AST_Changes = 0;
  1447. AST_data_dep();
  1448. AST_preserve(); /* moves processed vars from slicer to rel_vars */
  1449. AST_dominant(); /* mark data-irrelevant subgraphs */
  1450. AST_control_dep(); /* can add data deps, which add control deps */
  1451. if (verbose&32)
  1452. printf("\n\nROUND %d -- changes %d\n",
  1453. AST_Round, AST_Changes);
  1454. }
  1455. }
  1456. static void
  1457. AST_alias_analysis(void) /* aliasing of promela channels */
  1458. { AST *a;
  1459. for (a = ast; a; a = a->nxt)
  1460. AST_sends(a); /* collect chan-names that are send across chans */
  1461. for (a = ast; a; a = a->nxt)
  1462. AST_para(a->p); /* aliasing of chans thru proctype parameters */
  1463. for (a = ast; a; a = a->nxt)
  1464. AST_other(a); /* chan params in asgns and recvs */
  1465. AST_trans(); /* transitive closure of alias table */
  1466. if (verbose&32)
  1467. AST_aliases(); /* show channel aliasing info */
  1468. }
  1469. void
  1470. AST_slice(void)
  1471. { AST *a;
  1472. int spurious = 0;
  1473. if (!slicer)
  1474. { non_fatal("no slice criteria (or no claim) specified",
  1475. (char *) 0);
  1476. spurious = 1;
  1477. }
  1478. AST_dorelevant(); /* mark procs refered to in remote refs */
  1479. for (a = ast; a; a = a->nxt)
  1480. AST_def_use(a); /* compute standard def/use information */
  1481. AST_hidden(); /* parameter passing and local var inits */
  1482. AST_alias_analysis(); /* channel alias analysis */
  1483. AST_prelabel(); /* mark all 'assert(...)' stmnts as relevant */
  1484. AST_criteria(); /* process the slice criteria from
  1485. * asserts and from the never claim
  1486. */
  1487. if (!spurious || (verbose&32))
  1488. { spurious = 1;
  1489. for (a = ast; a; a = a->nxt)
  1490. { AST_dump(a); /* marked up result */
  1491. if (a->relevant&2) /* it printed something */
  1492. spurious = 0;
  1493. }
  1494. if (!AST_dump_rel() /* relevant variables */
  1495. && spurious)
  1496. printf("spin: no redundancies found (for given property)\n");
  1497. }
  1498. AST_suggestions();
  1499. if (verbose&32)
  1500. show_expl();
  1501. }
  1502. void
  1503. AST_store(ProcList *p, int start_state)
  1504. { AST *n_ast;
  1505. if (strcmp(p->n->name, ":never:") != 0
  1506. && strcmp(p->n->name, ":trace:") != 0
  1507. && strcmp(p->n->name, ":notrace:") != 0)
  1508. { n_ast = (AST *) emalloc(sizeof(AST));
  1509. n_ast->p = p;
  1510. n_ast->i_st = start_state;
  1511. n_ast->relevant = 0;
  1512. n_ast->fsm = fsm;
  1513. n_ast->nxt = ast;
  1514. ast = n_ast;
  1515. }
  1516. fsm = (FSM_state *) 0; /* hide it from FSM_DEL */
  1517. }
  1518. static void
  1519. AST_add_explicit(Lextok *d, Lextok *u)
  1520. { FSM_trans *e = (FSM_trans *) emalloc(sizeof(FSM_trans));
  1521. e->to = 0; /* or start_state ? */
  1522. e->relevant = 0; /* to be determined */
  1523. e->step = (Element *) 0; /* left blank */
  1524. e->Val[0] = e->Val[1] = (FSM_use *) 0;
  1525. cur_t = e;
  1526. def_use(u, USE);
  1527. def_use(d, DEF);
  1528. cur_t = (FSM_trans *) 0;
  1529. e->nxt = explicit;
  1530. explicit = e;
  1531. }
  1532. static void
  1533. AST_fp1(char *s, Lextok *t, Lextok *f, int parno)
  1534. { Lextok *v;
  1535. int cnt;
  1536. if (!t) return;
  1537. if (t->ntyp == RUN)
  1538. { if (strcmp(t->sym->name, s) == 0)
  1539. for (v = t->lft, cnt = 1; v; v = v->rgt, cnt++)
  1540. if (cnt == parno)
  1541. { AST_add_explicit(f, v->lft);
  1542. break;
  1543. }
  1544. } else
  1545. { AST_fp1(s, t->lft, f, parno);
  1546. AST_fp1(s, t->rgt, f, parno);
  1547. }
  1548. }
  1549. static void
  1550. AST_mk1(char *s, Lextok *c, int parno)
  1551. { AST *a;
  1552. FSM_state *f;
  1553. FSM_trans *t;
  1554. /* concoct an extra FSM_trans *t with the asgn of
  1555. * formal par c to matching actual pars made explicit
  1556. */
  1557. for (a = ast; a; a = a->nxt) /* automata */
  1558. for (f = a->fsm; f; f = f->nxt) /* control states */
  1559. for (t = f->t; t; t = t->nxt) /* transitions */
  1560. { if (t->step)
  1561. AST_fp1(s, t->step->n, c, parno);
  1562. }
  1563. }
  1564. static void
  1565. AST_par_init(void) /* parameter passing -- hidden assignments */
  1566. { AST *a;
  1567. Lextok *f, *t, *c;
  1568. int cnt;
  1569. for (a = ast; a; a = a->nxt)
  1570. { if (strcmp(a->p->n->name, ":never:") == 0
  1571. || strcmp(a->p->n->name, ":trace:") == 0
  1572. || strcmp(a->p->n->name, ":notrace:") == 0
  1573. || strcmp(a->p->n->name, ":init:") == 0)
  1574. continue; /* have no params */
  1575. cnt = 0;
  1576. for (f = a->p->p; f; f = f->rgt) /* types */
  1577. for (t = f->lft; t; t = t->rgt) /* formals */
  1578. { cnt++; /* formal par count */
  1579. c = (t->ntyp != ',')? t : t->lft; /* the formal parameter */
  1580. AST_mk1(a->p->n->name, c, cnt); /* all matching run statements */
  1581. } }
  1582. }
  1583. static void
  1584. AST_var_init(void) /* initialized vars (not chans) - hidden assignments */
  1585. { Ordered *walk;
  1586. Lextok *x;
  1587. Symbol *sp;
  1588. AST *a;
  1589. for (walk = all_names; walk; walk = walk->next)
  1590. { sp = walk->entry;
  1591. if (sp
  1592. && !sp->context /* globals */
  1593. && sp->type != PROCTYPE
  1594. && sp->ini
  1595. && (sp->type != MTYPE || sp->ini->ntyp != CONST) /* not mtype defs */
  1596. && sp->ini->ntyp != CHAN)
  1597. { x = nn(ZN, TYPE, ZN, ZN);
  1598. x->sym = sp;
  1599. AST_add_explicit(x, sp->ini);
  1600. } }
  1601. for (a = ast; a; a = a->nxt)
  1602. { if (strcmp(a->p->n->name, ":never:") != 0
  1603. && strcmp(a->p->n->name, ":trace:") != 0
  1604. && strcmp(a->p->n->name, ":notrace:") != 0) /* claim has no locals */
  1605. for (walk = all_names; walk; walk = walk->next)
  1606. { sp = walk->entry;
  1607. if (sp
  1608. && sp->context
  1609. && strcmp(sp->context->name, a->p->n->name) == 0
  1610. && sp->Nid >= 0 /* not a param */
  1611. && sp->type != LABEL
  1612. && sp->ini
  1613. && sp->ini->ntyp != CHAN)
  1614. { x = nn(ZN, TYPE, ZN, ZN);
  1615. x->sym = sp;
  1616. AST_add_explicit(x, sp->ini);
  1617. } } }
  1618. }
  1619. static void
  1620. show_expl(void)
  1621. { FSM_trans *t, *T;
  1622. FSM_use *u;
  1623. printf("\nExplicit List:\n");
  1624. for (T = expl_par; T; T = (T == expl_par)?expl_var: (FSM_trans *) 0)
  1625. { for (t = T; t; t = t->nxt)
  1626. { if (!t->Val[0]) continue;
  1627. printf("%s", t->relevant?"*":" ");
  1628. printf("%3d", t->round);
  1629. for (u = t->Val[0]; u; u = u->nxt)
  1630. { printf("\t<");
  1631. AST_var(u->n, u->n->sym, 1);
  1632. printf(":%d>, ", u->special);
  1633. }
  1634. printf("\n");
  1635. }
  1636. printf("==\n");
  1637. }
  1638. printf("End\n");
  1639. }
  1640. static void
  1641. AST_hidden(void) /* reveal all hidden assignments */
  1642. {
  1643. AST_par_init();
  1644. expl_par = explicit;
  1645. explicit = (FSM_trans *) 0;
  1646. AST_var_init();
  1647. expl_var = explicit;
  1648. explicit = (FSM_trans *) 0;
  1649. }
  1650. #define BPW (8*sizeof(ulong)) /* bits per word */
  1651. static int
  1652. bad_scratch(FSM_state *f, int upto)
  1653. { FSM_trans *t;
  1654. #if 0
  1655. 1. all internal branch-points have else-s
  1656. 2. all non-branchpoints have non-blocking out-edge
  1657. 3. all internal edges are non-relevant
  1658. subgraphs like this need NOT contribute control-dependencies
  1659. #endif
  1660. if (!f->seen
  1661. || (f->scratch&4))
  1662. return 0;
  1663. if (f->scratch&8)
  1664. return 1;
  1665. f->scratch |= 4;
  1666. if (verbose&32) printf("X[%d:%d:%d] ", f->from, upto, f->scratch);
  1667. if (f->scratch&1)
  1668. { if (verbose&32)
  1669. printf("\tbad scratch: %d\n", f->from);
  1670. bad: f->scratch &= ~4;
  1671. /* f->scratch |= 8; wrong */
  1672. return 1;
  1673. }
  1674. if (f->from != upto)
  1675. for (t = f->t; t; t = t->nxt)
  1676. if (bad_scratch(fsm_tbl[t->to], upto))
  1677. goto bad;
  1678. return 0;
  1679. }
  1680. static void
  1681. mark_subgraph(FSM_state *f, int upto)
  1682. { FSM_trans *t;
  1683. if (f->from == upto
  1684. || !f->seen
  1685. || (f->scratch&2))
  1686. return;
  1687. f->scratch |= 2;
  1688. for (t = f->t; t; t = t->nxt)
  1689. mark_subgraph(fsm_tbl[t->to], upto);
  1690. }
  1691. static void
  1692. AST_pair(AST *a, FSM_state *h, int y)
  1693. { Pair *p;
  1694. for (p = a->pairs; p; p = p->nxt)
  1695. if (p->h == h
  1696. && p->b == y)
  1697. return;
  1698. p = (Pair *) emalloc(sizeof(Pair));
  1699. p->h = h;
  1700. p->b = y;
  1701. p->nxt = a->pairs;
  1702. a->pairs = p;
  1703. }
  1704. static void
  1705. AST_checkpairs(AST *a)
  1706. { Pair *p;
  1707. for (p = a->pairs; p; p = p->nxt)
  1708. { if (verbose&32)
  1709. printf(" inspect pair %d %d\n", p->b, p->h->from);
  1710. if (!bad_scratch(p->h, p->b)) /* subgraph is clean */
  1711. { if (verbose&32)
  1712. printf("subgraph: %d .. %d\n", p->b, p->h->from);
  1713. mark_subgraph(p->h, p->b);
  1714. }
  1715. }
  1716. }
  1717. static void
  1718. subgraph(AST *a, FSM_state *f, int out)
  1719. { FSM_state *h;
  1720. int i, j;
  1721. ulong *g;
  1722. #if 0
  1723. reverse dominance suggests that this is a possible
  1724. entry and exit node for a proper subgraph
  1725. #endif
  1726. h = fsm_tbl[out];
  1727. i = f->from / BPW;
  1728. j = f->from % BPW;
  1729. g = h->mod;
  1730. if (verbose&32)
  1731. printf("possible pair %d %d -- %d\n",
  1732. f->from, h->from, (g[i]&(1<<j))?1:0);
  1733. if (g[i]&(1<<j)) /* also a forward dominance pair */
  1734. AST_pair(a, h, f->from); /* record this pair */
  1735. }
  1736. static void
  1737. act_dom(AST *a)
  1738. { FSM_state *f;
  1739. FSM_trans *t;
  1740. int i, j, cnt;
  1741. for (f = a->fsm; f; f = f->nxt)
  1742. { if (!f->seen) continue;
  1743. #if 0
  1744. f->from is the exit-node of a proper subgraph, with
  1745. the dominator its entry-node, if:
  1746. a. this node has more than 1 reachable predecessor
  1747. b. the dominator has more than 1 reachable successor
  1748. (need reachability - in case of reverse dominance)
  1749. d. the dominator is reachable, and not equal to this node
  1750. #endif
  1751. for (t = f->p, i = 0; t; t = t->nxt)
  1752. i += fsm_tbl[t->to]->seen;
  1753. if (i <= 1) continue; /* a. */
  1754. for (cnt = 1; cnt < a->nstates; cnt++) /* 0 is endstate */
  1755. { if (cnt == f->from
  1756. || !fsm_tbl[cnt]->seen)
  1757. continue; /* c. */
  1758. i = cnt / BPW;
  1759. j = cnt % BPW;
  1760. if (!(f->dom[i]&(1<<j)))
  1761. continue;
  1762. for (t = fsm_tbl[cnt]->t, i = 0; t; t = t->nxt)
  1763. i += fsm_tbl[t->to]->seen;
  1764. if (i <= 1)
  1765. continue; /* b. */
  1766. if (f->mod) /* final check in 2nd phase */
  1767. subgraph(a, f, cnt); /* possible entry-exit pair */
  1768. }
  1769. }
  1770. }
  1771. static void
  1772. reachability(AST *a)
  1773. { FSM_state *f;
  1774. for (f = a->fsm; f; f = f->nxt)
  1775. f->seen = 0; /* clear */
  1776. AST_dfs(a, a->i_st, 0); /* mark 'seen' */
  1777. }
  1778. static int
  1779. see_else(FSM_state *f)
  1780. { FSM_trans *t;
  1781. for (t = f->t; t; t = t->nxt)
  1782. { if (t->step
  1783. && t->step->n)
  1784. switch (t->step->n->ntyp) {
  1785. case ELSE:
  1786. return 1;
  1787. case IF:
  1788. case DO:
  1789. case ATOMIC:
  1790. case NON_ATOMIC:
  1791. case D_STEP:
  1792. if (see_else(fsm_tbl[t->to]))
  1793. return 1;
  1794. default:
  1795. break;
  1796. }
  1797. }
  1798. return 0;
  1799. }
  1800. static int
  1801. is_guard(FSM_state *f)
  1802. { FSM_state *g;
  1803. FSM_trans *t;
  1804. for (t = f->p; t; t = t->nxt)
  1805. { g = fsm_tbl[t->to];
  1806. if (!g->seen)
  1807. continue;
  1808. if (t->step
  1809. && t->step->n)
  1810. switch(t->step->n->ntyp) {
  1811. case IF:
  1812. case DO:
  1813. return 1;
  1814. case ATOMIC:
  1815. case NON_ATOMIC:
  1816. case D_STEP:
  1817. if (is_guard(g))
  1818. return 1;
  1819. default:
  1820. break;
  1821. }
  1822. }
  1823. return 0;
  1824. }
  1825. static void
  1826. curtail(AST *a)
  1827. { FSM_state *f, *g;
  1828. FSM_trans *t;
  1829. int i, haselse, isrel, blocking;
  1830. #if 0
  1831. mark nodes that do not satisfy these requirements:
  1832. 1. all internal branch-points have else-s
  1833. 2. all non-branchpoints have non-blocking out-edge
  1834. 3. all internal edges are non-data-relevant
  1835. #endif
  1836. if (verbose&32)
  1837. printf("Curtail %s:\n", a->p->n->name);
  1838. for (f = a->fsm; f; f = f->nxt)
  1839. { if (!f->seen
  1840. || (f->scratch&(1|2)))
  1841. continue;
  1842. isrel = haselse = i = blocking = 0;
  1843. for (t = f->t; t; t = t->nxt)
  1844. { g = fsm_tbl[t->to];
  1845. isrel |= (t->relevant&1); /* data relevant */
  1846. i += g->seen;
  1847. if (t->step
  1848. && t->step->n)
  1849. { switch (t->step->n->ntyp) {
  1850. case IF:
  1851. case DO:
  1852. haselse |= see_else(g);
  1853. break;
  1854. case 'c':
  1855. case 's':
  1856. case 'r':
  1857. blocking = 1;
  1858. break;
  1859. } } }
  1860. #if 0
  1861. if (verbose&32)
  1862. printf("prescratch %d -- %d %d %d %d -- %d\n",
  1863. f->from, i, isrel, blocking, haselse, is_guard(f));
  1864. #endif
  1865. if (isrel /* 3. */
  1866. || (i == 1 && blocking) /* 2. */
  1867. || (i > 1 && !haselse)) /* 1. */
  1868. { if (!is_guard(f))
  1869. { f->scratch |= 1;
  1870. if (verbose&32)
  1871. printf("scratch %d -- %d %d %d %d\n",
  1872. f->from, i, isrel, blocking, haselse);
  1873. }
  1874. }
  1875. }
  1876. }
  1877. static void
  1878. init_dom(AST *a)
  1879. { FSM_state *f;
  1880. int i, j, cnt;
  1881. #if 0
  1882. (1) D(s0) = {s0}
  1883. (2) for s in S - {s0} do D(s) = S
  1884. #endif
  1885. for (f = a->fsm; f; f = f->nxt)
  1886. { if (!f->seen) continue;
  1887. f->dom = (ulong *)
  1888. emalloc(a->nwords * sizeof(ulong));
  1889. if (f->from == a->i_st)
  1890. { i = a->i_st / BPW;
  1891. j = a->i_st % BPW;
  1892. f->dom[i] = (1<<j); /* (1) */
  1893. } else /* (2) */
  1894. { for (i = 0; i < a->nwords; i++)
  1895. f->dom[i] = (ulong) ~0; /* all 1's */
  1896. if (a->nstates % BPW)
  1897. for (i = (a->nstates % BPW); i < (int) BPW; i++)
  1898. f->dom[a->nwords-1] &= ~(1<<i); /* clear tail */
  1899. for (cnt = 0; cnt < a->nstates; cnt++)
  1900. if (!fsm_tbl[cnt]->seen)
  1901. { i = cnt / BPW;
  1902. j = cnt % BPW;
  1903. f->dom[i] &= ~(1<<j);
  1904. } } }
  1905. }
  1906. static int
  1907. dom_perculate(AST *a, FSM_state *f)
  1908. { static ulong *ndom = (ulong *) 0;
  1909. static int on = 0;
  1910. int i, j, cnt = 0;
  1911. FSM_state *g;
  1912. FSM_trans *t;
  1913. if (on < a->nwords)
  1914. { on = a->nwords;
  1915. ndom = (ulong *)
  1916. emalloc(on * sizeof(ulong));
  1917. }
  1918. for (i = 0; i < a->nwords; i++)
  1919. ndom[i] = (ulong) ~0;
  1920. for (t = f->p; t; t = t->nxt) /* all reachable predecessors */
  1921. { g = fsm_tbl[t->to];
  1922. if (g->seen)
  1923. for (i = 0; i < a->nwords; i++)
  1924. ndom[i] &= g->dom[i]; /* (5b) */
  1925. }
  1926. i = f->from / BPW;
  1927. j = f->from % BPW;
  1928. ndom[i] |= (1<<j); /* (5a) */
  1929. for (i = 0; i < a->nwords; i++)
  1930. if (f->dom[i] != ndom[i])
  1931. { cnt++;
  1932. f->dom[i] = ndom[i];
  1933. }
  1934. return cnt;
  1935. }
  1936. static void
  1937. dom_forward(AST *a)
  1938. { FSM_state *f;
  1939. int cnt;
  1940. init_dom(a); /* (1,2) */
  1941. do {
  1942. cnt = 0;
  1943. for (f = a->fsm; f; f = f->nxt)
  1944. { if (f->seen
  1945. && f->from != a->i_st) /* (4) */
  1946. cnt += dom_perculate(a, f); /* (5) */
  1947. }
  1948. } while (cnt); /* (3) */
  1949. dom_perculate(a, fsm_tbl[a->i_st]);
  1950. }
  1951. static void
  1952. AST_dominant(void)
  1953. { FSM_state *f;
  1954. FSM_trans *t;
  1955. AST *a;
  1956. int oi;
  1957. #if 0
  1958. find dominators
  1959. Aho, Sethi, & Ullman, Compilers - principles, techniques, and tools
  1960. Addison-Wesley, 1986, p.671.
  1961. (1) D(s0) = {s0}
  1962. (2) for s in S - {s0} do D(s) = S
  1963. (3) while any D(s) changes do
  1964. (4) for s in S - {s0} do
  1965. (5) D(s) = {s} union with intersection of all D(p)
  1966. where p are the immediate predecessors of s
  1967. the purpose is to find proper subgraphs
  1968. (one entry node, one exit node)
  1969. #endif
  1970. if (AST_Round == 1) /* computed once, reused in every round */
  1971. for (a = ast; a; a = a->nxt)
  1972. { a->nstates = 0;
  1973. for (f = a->fsm; f; f = f->nxt)
  1974. { a->nstates++; /* count */
  1975. fsm_tbl[f->from] = f; /* fast lookup */
  1976. f->scratch = 0; /* clear scratch marks */
  1977. }
  1978. for (oi = 0; oi < a->nstates; oi++)
  1979. if (!fsm_tbl[oi])
  1980. fsm_tbl[oi] = &no_state;
  1981. a->nwords = (a->nstates + BPW - 1) / BPW; /* round up */
  1982. if (verbose&32)
  1983. { printf("%s (%d): ", a->p->n->name, a->i_st);
  1984. printf("states=%d (max %d), words = %d, bpw %d, overflow %d\n",
  1985. a->nstates, o_max, a->nwords,
  1986. (int) BPW, (int) (a->nstates % BPW));
  1987. }
  1988. reachability(a);
  1989. dom_forward(a); /* forward dominance relation */
  1990. curtail(a); /* mark ineligible edges */
  1991. for (f = a->fsm; f; f = f->nxt)
  1992. { t = f->p;
  1993. f->p = f->t;
  1994. f->t = t; /* invert edges */
  1995. f->mod = f->dom;
  1996. f->dom = (ulong *) 0;
  1997. }
  1998. oi = a->i_st;
  1999. if (fsm_tbl[0]->seen) /* end-state reachable - else leave it */
  2000. a->i_st = 0; /* becomes initial state */
  2001. dom_forward(a); /* reverse dominance -- don't redo reachability! */
  2002. act_dom(a); /* mark proper subgraphs, if any */
  2003. AST_checkpairs(a); /* selectively place 2 scratch-marks */
  2004. for (f = a->fsm; f; f = f->nxt)
  2005. { t = f->p;
  2006. f->p = f->t;
  2007. f->t = t; /* restore */
  2008. }
  2009. a->i_st = oi; /* restore */
  2010. } else
  2011. for (a = ast; a; a = a->nxt)
  2012. { for (f = a->fsm; f; f = f->nxt)
  2013. { fsm_tbl[f->from] = f;
  2014. f->scratch &= 1; /* preserve 1-marks */
  2015. }
  2016. for (oi = 0; oi < a->nstates; oi++)
  2017. if (!fsm_tbl[oi])
  2018. fsm_tbl[oi] = &no_state;
  2019. curtail(a); /* mark ineligible edges */
  2020. for (f = a->fsm; f; f = f->nxt)
  2021. { t = f->p;
  2022. f->p = f->t;
  2023. f->t = t; /* invert edges */
  2024. }
  2025. AST_checkpairs(a); /* recompute 2-marks */
  2026. for (f = a->fsm; f; f = f->nxt)
  2027. { t = f->p;
  2028. f->p = f->t;
  2029. f->t = t; /* restore */
  2030. } }
  2031. }