pangen6.c 47 KB

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