pangen6.c 47 KB

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