pangen2.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653
  1. /***** spin: pangen2.c *****/
  2. /* Copyright (c) 1991-2000 by Lucent Technologies - Bell Laboratories */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* Permission is given to distribute this code provided that this intro- */
  5. /* ductory message is not removed and no monies are exchanged. */
  6. /* No guarantee is expressed or implied by the distribution of this code. */
  7. /* Software written by Gerard J. Holzmann as part of the book: */
  8. /* `Design and Validation of Computer Protocols,' ISBN 0-13-539925-4, */
  9. /* Prentice Hall, Englewood Cliffs, NJ, 07632. */
  10. /* Send bug-reports and/or questions to: gerard@research.bell-labs.com */
  11. #include "spin.h"
  12. #include "version.h"
  13. #ifdef PC
  14. #include "y_tab.h"
  15. #else
  16. #include "y.tab.h"
  17. #endif
  18. #include "pangen2.h"
  19. #include "pangen4.h"
  20. #include "pangen5.h"
  21. #define DELTA 500 /* sets an upperbound on nr of chan names */
  22. #define blurb(fd, e) { fprintf(fd, "\n"); if (!merger) fprintf(fd, "\t\t/* %s:%d */\n", \
  23. e->n->fn->name, e->n->ln); }
  24. #define tr_map(m, e) { if (!merger) fprintf(tt, "\t\ttr_2_src(%d, %s, %d);\n", \
  25. m, e->n->fn->name, e->n->ln); }
  26. extern ProcList *rdy;
  27. extern RunList *run;
  28. extern Symbol *Fname, *oFname, *owner, *context;
  29. extern char *claimproc, *eventmap;
  30. extern int lineno, verbose, Npars, Mpars;
  31. extern int m_loss, Nid, has_remote, merger, rvopt, separate;
  32. extern int Ntimeouts, Etimeouts;
  33. extern int u_sync, u_async, nrRdy, nqs;
  34. extern int GenCode, IsGuard, Level, TestOnly;
  35. extern char *NextLab[];
  36. FILE *tc, *th, *tt, *tm, *tb;
  37. ProcList *cur_proc;
  38. int OkBreak = -1;
  39. short nocast=0; /* to turn off casts in lvalues */
  40. short terse=0; /* terse printing of varnames */
  41. short no_arrays=0;
  42. short has_last=0; /* spec refers to _last */
  43. short has_badelse=0; /* spec contains else combined with chan refs */
  44. short has_enabled=0; /* spec contains enabled() */
  45. short has_pcvalue=0; /* spec contains pc_value() */
  46. short has_np=0; /* spec contains np_ */
  47. short has_sorted=0; /* spec contains `!!' (sorted-send) operator */
  48. short has_random=0; /* spec contains `??' (random-recv) operator */
  49. short has_xu=0; /* spec contains xr or xs assertions */
  50. short has_unless=0; /* spec contains unless statements */
  51. short has_provided=0; /* spec contains PROVIDED clauses on procs */
  52. short _isok=0; /* checks usage of predefined varble _ */
  53. short evalindex=0; /* evaluate index of var names */
  54. short withprocname=0; /* prefix local varnames with procname */
  55. int mst=0; /* max nr of state/process */
  56. int claimnr = -1; /* claim process, if any */
  57. int eventmapnr = -1; /* event trace, if any */
  58. int Pid; /* proc currently processed */
  59. int multi_oval; /* set in merges, used also in pangen4.c */
  60. #define MAXMERGE 256 /* max nr of bups per merge sequence */
  61. static short CnT[MAXMERGE];
  62. static Lextok XZ, YZ[MAXMERGE];
  63. static int didcase, YZmax, YZcnt;
  64. static Lextok *Nn[2];
  65. static int Det; /* set if deterministic */
  66. static int T_sum, T_mus, t_cyc;
  67. static int TPE[2], EPT[2];
  68. static int uniq=1;
  69. static int multi_needed, multi_undo;
  70. static short AllGlobal=0; /* set if process has provided clause */
  71. int has_global(Lextok *);
  72. static int getweight(Lextok *);
  73. static int scan_seq(Sequence *);
  74. static void genconditionals(void);
  75. static void mark_seq(Sequence *);
  76. static void patch_atomic(Sequence *);
  77. static void put_seq(Sequence *, int, int);
  78. static void putproc(ProcList *);
  79. static void Tpe(Lextok *);
  80. extern void spit_recvs(FILE *, FILE*);
  81. static int
  82. fproc(char *s)
  83. { ProcList *p;
  84. for (p = rdy; p; p = p->nxt)
  85. if (strcmp(p->n->name, s) == 0)
  86. return p->tn;
  87. fatal("proctype %s not found", s);
  88. return -1;
  89. }
  90. static void
  91. reverse_procs(RunList *q)
  92. {
  93. if (!q) return;
  94. reverse_procs(q->nxt);
  95. fprintf(tc, " Addproc(%d);\n", q->tn);
  96. }
  97. static void
  98. tm_predef_np(void)
  99. {
  100. fprintf(th, "#define _T5 %d\n", uniq++);
  101. fprintf(th, "#define _T2 %d\n", uniq++);
  102. fprintf(tm, "\tcase _T5:\t/* np_ */\n");
  103. if (separate == 2)
  104. fprintf(tm, "\t\tif (!((!(o_pm&4) && !(tau&128))))\n");
  105. else
  106. fprintf(tm, "\t\tif (!((!(trpt->o_pm&4) && !(trpt->tau&128))))\n");
  107. fprintf(tm, "\t\t\tcontinue;\n");
  108. fprintf(tm, "\t\t/* else fall through */\n");
  109. fprintf(tm, "\tcase _T2:\t/* true */\n");
  110. fprintf(tm, "\t\tm = 3; goto P999;\n");
  111. }
  112. static void
  113. tt_predef_np(void)
  114. {
  115. fprintf(tt, "\t/* np_ demon: */\n");
  116. fprintf(tt, "\ttrans[_NP_] = ");
  117. fprintf(tt, "(Trans **) emalloc(2*sizeof(Trans *));\n");
  118. fprintf(tt, "\tT = trans[_NP_][0] = ");
  119. fprintf(tt, "settr(9997,0,0,_T2,0,\"(1)\", 0,2,0);\n");
  120. fprintf(tt, "\t T->nxt = ");
  121. fprintf(tt, "settr(9998,0,1,_T5,0,\"(np_)\", 1,2,0);\n");
  122. fprintf(tt, "\tT = trans[_NP_][1] = ");
  123. fprintf(tt, "settr(9999,0,1,_T5,0,\"(np_)\", 1,2,0);\n");
  124. }
  125. static struct {
  126. char *nm[3];
  127. } Cfile[] = {
  128. { "pan.c", "pan_s.c", "pan_t.c" },
  129. { "pan.h", "pan_s.h", "pan_t.h" },
  130. { "pan.t", "pan_s.t", "pan_t.t" },
  131. { "pan.m", "pan_s.m", "pan_t.m" },
  132. { "pan.b", "pan_s.b", "pan_t.b" },
  133. };
  134. void
  135. gensrc(void)
  136. { ProcList *p;
  137. if (!(tc = fopen(Cfile[0].nm[separate], "w")) /* main routines */
  138. || !(th = fopen(Cfile[1].nm[separate], "w")) /* header file */
  139. || !(tt = fopen(Cfile[2].nm[separate], "w")) /* transition matrix */
  140. || !(tm = fopen(Cfile[3].nm[separate], "w")) /* forward moves */
  141. || !(tb = fopen(Cfile[4].nm[separate], "w"))) /* backward moves */
  142. { printf("spin: cannot create pan.[chtmfb]\n");
  143. alldone(1);
  144. }
  145. fprintf(th, "#define Version \"%s\"\n", Version);
  146. fprintf(th, "#define Source \"%s\"\n\n", oFname->name);
  147. if (separate != 2)
  148. fprintf(th, "char *TrailFile = Source; /* default */\n");
  149. fprintf(th, "#define uchar unsigned char\n");
  150. if (sizeof(long)==8)
  151. fprintf(th, "#define long int\n");
  152. if (separate == 1 && !claimproc)
  153. { Symbol *n = (Symbol *) emalloc(sizeof(Symbol));
  154. Sequence *s = (Sequence *) emalloc(sizeof(Sequence));
  155. claimproc = n->name = "_:never_template:_";
  156. ready(n, ZN, s, 0, ZN);
  157. }
  158. if (separate == 2)
  159. { if (has_remote)
  160. { printf("spin: warning, make sure that the S1 model\n");
  161. printf(" includes the same remote references\n");
  162. }
  163. fprintf(th, "#ifndef NFAIR\n");
  164. fprintf(th, "#define NFAIR 2 /* must be >= 2 */\n");
  165. fprintf(th, "#endif\n");
  166. if (has_last)
  167. fprintf(th, "#define HAS_LAST %d\n", has_last);
  168. goto doless;
  169. }
  170. fprintf(th, "#define DELTA %d\n", DELTA);
  171. fprintf(th, "#ifdef MA\n");
  172. fprintf(th, "#if MA==1\n"); /* user typed -DMA without size */
  173. fprintf(th, "#undef MA\n#define MA 100\n");
  174. fprintf(th, "#endif\n#endif\n");
  175. fprintf(th, "#ifdef W_XPT\n");
  176. fprintf(th, "#if W_XPT==1\n"); /* user typed -DW_XPT without size */
  177. fprintf(th, "#undef W_XPT\n#define W_XPT 1000000\n");
  178. fprintf(th, "#endif\n#endif\n");
  179. fprintf(th, "#ifndef NFAIR\n");
  180. fprintf(th, "#define NFAIR 2 /* must be >= 2 */\n");
  181. fprintf(th, "#endif\n");
  182. if (Ntimeouts)
  183. fprintf(th, "#define NTIM %d\n", Ntimeouts);
  184. if (Etimeouts)
  185. fprintf(th, "#define ETIM %d\n", Etimeouts);
  186. if (has_remote)
  187. fprintf(th, "#define REM_REFS %d\n", has_remote); /* not yet used */
  188. if (has_last)
  189. fprintf(th, "#define HAS_LAST %d\n", has_last);
  190. if (has_sorted)
  191. fprintf(th, "#define HAS_SORTED %d\n", has_sorted);
  192. if (has_random)
  193. fprintf(th, "#define HAS_RANDOM %d\n", has_random);
  194. if (!has_enabled && !eventmap)
  195. fprintf(th, "#define INLINE 1\n");
  196. if (has_enabled)
  197. fprintf(th, "#define HAS_ENABLED 1\n");
  198. if (has_unless)
  199. fprintf(th, "#define HAS_UNLESS %d\n", has_unless);
  200. if (has_provided)
  201. fprintf(th, "#define HAS_PROVIDED %d\n", has_provided);
  202. if (has_pcvalue)
  203. fprintf(th, "#define HAS_PCVALUE %d\n", has_pcvalue);
  204. if (has_badelse)
  205. fprintf(th, "#define HAS_BADELSE %d\n", has_badelse);
  206. if (has_np)
  207. fprintf(th, "#define HAS_NP %d\n", has_np);
  208. if (merger)
  209. fprintf(th, "#define MERGED 1\n");
  210. doless:
  211. fprintf(th, "#ifdef NP /* includes np_ demon */\n");
  212. if (!has_np)
  213. fprintf(th, "#define HAS_NP 2\n");
  214. fprintf(th, "#define VERI %d\n", nrRdy);
  215. fprintf(th, "#define endclaim 3 /* none */\n");
  216. fprintf(th, "#endif\n");
  217. if (claimproc)
  218. { claimnr = fproc(claimproc);
  219. /* NP overrides claimproc */
  220. fprintf(th, "#if !defined(NOCLAIM) && !defined NP\n");
  221. fprintf(th, "#define VERI %d\n", claimnr);
  222. fprintf(th, "#define endclaim endstate%d\n", claimnr);
  223. fprintf(th, "#endif\n");
  224. }
  225. if (eventmap)
  226. { eventmapnr = fproc(eventmap);
  227. fprintf(th, "#define EVENT_TRACE %d\n", eventmapnr);
  228. fprintf(th, "#define endevent endstate%d\n", eventmapnr);
  229. if (eventmap[2] == 'o') /* ":no_trace:" */
  230. fprintf(th, "#define NEGATED_TRACE 1\n");
  231. }
  232. fprintf(th, "typedef struct S_F_MAP {\n");
  233. fprintf(th, " char *fnm; int from; int upto;\n");
  234. fprintf(th, "} S_F_MAP;\n");
  235. fprintf(tc, "/*** Generated by %s ***/\n", Version);
  236. fprintf(tc, "/*** From source: %s ***/\n\n", oFname->name);
  237. ntimes(tc, 0, 1, Pre0);
  238. switch (separate) {
  239. case 0: fprintf(tc, "#include \"pan.h\"\n"); break;
  240. case 1: fprintf(tc, "#include \"pan_s.h\"\n"); break;
  241. case 2: fprintf(tc, "#include \"pan_t.h\"\n"); break;
  242. }
  243. if (separate != 2)
  244. ntimes(tc, 0, 1, Preamble);
  245. fprintf(tc, "#ifndef NOBOUNDCHECK\n");
  246. fprintf(tc, "#define Index(x, y)\tBoundcheck(x, y, II, tt, t)\n");
  247. fprintf(tc, "#else\n");
  248. fprintf(tc, "#define Index(x, y)\tx\n");
  249. fprintf(tc, "#endif\n");
  250. for (p = rdy; p; p = p->nxt)
  251. mst = max(p->s->maxel, mst);
  252. if (separate != 2)
  253. { fprintf(tt, "#ifdef PEG\n");
  254. fprintf(tt, "struct T_SRC {\n");
  255. fprintf(tt, " char *fl; int ln;\n");
  256. fprintf(tt, "} T_SRC[NTRANS];\n\n");
  257. fprintf(tt, "void\ntr_2_src(int m, char *file, int ln)\n");
  258. fprintf(tt, "{ T_SRC[m].fl = file;\n");
  259. fprintf(tt, " T_SRC[m].ln = ln;\n");
  260. fprintf(tt, "}\n\n");
  261. fprintf(tt, "void\nputpeg(int n, int m)\n");
  262. fprintf(tt, "{ printf(\"%%5d\ttrans %%4d \", m, n);\n");
  263. fprintf(tt, " printf(\"file %%s line %%3d\\n\",\n");
  264. fprintf(tt, " T_SRC[n].fl, T_SRC[n].ln);\n");
  265. fprintf(tt, "}\n");
  266. if (!merger)
  267. { fprintf(tt, "#else\n");
  268. fprintf(tt, "#define tr_2_src(m,f,l)\n");
  269. }
  270. fprintf(tt, "#endif\n\n");
  271. fprintf(tt, "void\nsettable(void)\n{\tTrans *T;\n");
  272. fprintf(tt, "\tTrans *settr(int, int, int, int, int,");
  273. fprintf(tt, " char *, int, int, int);\n\n");
  274. fprintf(tt, "\ttrans = (Trans ***) ");
  275. fprintf(tt, "emalloc(%d*sizeof(Trans **));\n", nrRdy+1);
  276. /* +1 for np_ automaton */
  277. if (separate == 1)
  278. {
  279. fprintf(tm, " if (II == 0)\n");
  280. fprintf(tm, " { m = step_claim(trpt->o_pm, trpt->tau, tt, ot, t);\n");
  281. fprintf(tm, " if (m) goto P999; else continue;\n");
  282. fprintf(tm, " } else\n");
  283. }
  284. fprintf(tm, " switch (t->forw) {\n");
  285. } else
  286. { fprintf(tt, "#ifndef PEG\n");
  287. fprintf(tt, "#define tr_2_src(m,f,l)\n");
  288. fprintf(tt, "#endif\n");
  289. fprintf(tt, "void\nset_claim(void)\n{\tTrans *T;\n");
  290. fprintf(tt, "\textern Trans ***trans;\n");
  291. fprintf(tt, "\textern Trans *settr(int, int, int, int, int,");
  292. fprintf(tt, " char *, int, int, int);\n\n");
  293. fprintf(tm, " switch (forw) {\n");
  294. }
  295. fprintf(tm, " default: Uerror(\"bad forward move\");\n");
  296. fprintf(tm, " case 0: /* if without executable clauses */\n");
  297. fprintf(tm, " continue;\n");
  298. fprintf(tm, " case 1: /* generic 'goto' or 'skip' */\n");
  299. if (separate != 2)
  300. fprintf(tm, " IfNotBlocked\n");
  301. fprintf(tm, " m = 3; goto P999;\n");
  302. fprintf(tm, " case 2: /* generic 'else' */\n");
  303. if (separate == 2)
  304. fprintf(tm, " if (o_pm&1) continue;\n");
  305. else
  306. { fprintf(tm, " IfNotBlocked\n");
  307. fprintf(tm, " if (trpt->o_pm&1) continue;\n");
  308. }
  309. fprintf(tm, " m = 3; goto P999;\n");
  310. uniq = 3;
  311. if (separate == 1)
  312. fprintf(tb, " if (II == 0) goto R999;\n");
  313. fprintf(tb, " switch (t->back) {\n");
  314. fprintf(tb, " default: Uerror(\"bad return move\");\n");
  315. fprintf(tb, " case 0: goto R999; /* nothing to undo */\n");
  316. for (p = rdy; p; p = p->nxt)
  317. { cur_proc = p;
  318. putproc(p);
  319. }
  320. if (separate != 1)
  321. { tm_predef_np();
  322. tt_predef_np();
  323. }
  324. fprintf(tt, "}\n\n"); /* end of settable() */
  325. fprintf(tm, " }\n\n");
  326. fprintf(tb, " }\n\n");
  327. if (separate != 2)
  328. { ntimes(tt, 0, 1, Tail);
  329. genheader();
  330. if (separate == 1)
  331. { fprintf(th, "#define FORWARD_MOVES\t\"pan_s.m\"\n");
  332. fprintf(th, "#define REVERSE_MOVES\t\"pan_s.b\"\n");
  333. fprintf(th, "#define TRANSITIONS\t\"pan_s.t\"\n");
  334. fprintf(th, "extern void ini_claim(int, int);\n");
  335. } else
  336. { fprintf(th, "#define FORWARD_MOVES\t\"pan.m\"\n");
  337. fprintf(th, "#define REVERSE_MOVES\t\"pan.b\"\n");
  338. fprintf(th, "#define TRANSITIONS\t\"pan.t\"\n");
  339. }
  340. genaddproc();
  341. genother();
  342. genaddqueue();
  343. genunio();
  344. genconditionals();
  345. gensvmap();
  346. if (!run) fatal("no runable process", (char *)0);
  347. fprintf(tc, "void\n");
  348. fprintf(tc, "active_procs(void)\n{\n");
  349. reverse_procs(run);
  350. fprintf(tc, "}\n");
  351. ntimes(tc, 0, 1, Dfa);
  352. ntimes(tc, 0, 1, Xpt);
  353. fprintf(th, "#define NTRANS %d\n", uniq);
  354. fprintf(th, "#ifdef PEG\n");
  355. fprintf(th, "long peg[NTRANS];\n");
  356. fprintf(th, "#endif\n");
  357. if (u_sync && !u_async)
  358. spit_recvs(th, tc);
  359. } else
  360. { genheader();
  361. fprintf(th, "#define FORWARD_MOVES\t\"pan_t.m\"\n");
  362. fprintf(th, "#define REVERSE_MOVES\t\"pan_t.b\"\n");
  363. fprintf(th, "#define TRANSITIONS\t\"pan_t.t\"\n");
  364. fprintf(tc, "extern int Maxbody;\n");
  365. fprintf(tc, "extern int proc_offset[];\n");
  366. fprintf(tc, "extern int proc_skip[];\n");
  367. fprintf(tc, "extern uchar *reached[];\n");
  368. fprintf(tc, "extern uchar *accpstate[];\n");
  369. fprintf(tc, "extern uchar *progstate[];\n");
  370. fprintf(tc, "extern uchar *stopstate[];\n");
  371. fprintf(tc, "extern uchar *visstate[];\n\n");
  372. fprintf(tc, "void\nini_claim(int n, int h)\n{");
  373. fprintf(tc, "\textern State now;\n");
  374. fprintf(tc, "\textern void set_claim(void);\n\n");
  375. fprintf(tc, "#ifdef PROV\n");
  376. fprintf(tc, "#include PROV\n");
  377. fprintf(tc, "#endif\n");
  378. fprintf(tc, "\tset_claim();\n");
  379. genother();
  380. fprintf(tc, "\n\tswitch (n) {\n");
  381. genaddproc();
  382. fprintf(tc, "\t}\n");
  383. fprintf(tc, "\n}\n");
  384. fprintf(tc, "int\nstep_claim(int o_pm, int tau, int tt, int ot, Trans *t)\n");
  385. fprintf(tc, "{ int forw = t->forw; int m = 0; extern char *noptr; int II=0;\n");
  386. fprintf(tc, " extern State now;\n");
  387. fprintf(tc, "#define continue return 0\n");
  388. fprintf(tc, "#include \"pan_t.m\"\n");
  389. fprintf(tc, "P999:\n\treturn m;\n}\n");
  390. fprintf(tc, "#undef continue\n");
  391. fprintf(tc, "int\nrev_claim(int backw)\n{ return 0; }\n");
  392. fprintf(tc, "#include TRANSITIONS\n");
  393. }
  394. if (separate != 1)
  395. ntimes(tc, 0, 1, Nvr1);
  396. }
  397. static int
  398. find_id(Symbol *s)
  399. { ProcList *p;
  400. if (s)
  401. for (p = rdy; p; p = p->nxt)
  402. if (s == p->n)
  403. return p->tn;
  404. return 0;
  405. }
  406. static void
  407. dolen(Symbol *s, char *pre, int pid, int ai, int qln)
  408. {
  409. if (ai > 0)
  410. fprintf(tc, "\n\t\t\t || ");
  411. fprintf(tc, "%s(", pre);
  412. if (!(s->hidden&1))
  413. { if (s->context)
  414. fprintf(tc, "((P%d *)this)->", pid);
  415. else
  416. fprintf(tc, "now.");
  417. }
  418. fprintf(tc, "%s", s->name);
  419. if (qln > 1) fprintf(tc, "[%d]", ai);
  420. fprintf(tc, ")");
  421. }
  422. struct AA { char TT[9]; char CC[8]; };
  423. static struct AA BB[4] = {
  424. { "Q_FULL_F", " q_full" },
  425. { "Q_FULL_T", "!q_full" },
  426. { "Q_EMPT_F", " !q_len" },
  427. { "Q_EMPT_T", " q_len" }
  428. };
  429. static struct AA DD[4] = {
  430. { "Q_FULL_F", " q_e_f" }, /* empty or full */
  431. { "Q_FULL_T", "!q_full" },
  432. { "Q_EMPT_F", " q_e_f" },
  433. { "Q_EMPT_T", " q_len" }
  434. };
  435. /* this reduces the number of cases where 's' and 'r'
  436. are considered conditionally safe under the
  437. partial order reduction rules; as a price for
  438. this simple implementation, it also affects the
  439. cases where nfull and nempty can be considered
  440. safe -- since these are labeled the same way as
  441. 's' and 'r' respectively
  442. it only affects reduction, not functionality
  443. */
  444. void
  445. bb_or_dd(int j, int which)
  446. {
  447. if (which)
  448. { if (has_unless)
  449. fprintf(tc, "%s", DD[j].CC);
  450. else
  451. fprintf(tc, "%s", BB[j].CC);
  452. } else
  453. { if (has_unless)
  454. fprintf(tc, "%s", DD[j].TT);
  455. else
  456. fprintf(tc, "%s", BB[j].TT);
  457. }
  458. }
  459. void
  460. Done_case(char *nm, Symbol *z)
  461. { int j, k;
  462. int nid = z->Nid;
  463. int qln = z->nel;
  464. fprintf(tc, "\t\tcase %d: if (", nid);
  465. for (j = 0; j < 4; j++)
  466. { fprintf(tc, "\t(t->ty[i] == ");
  467. bb_or_dd(j, 0);
  468. fprintf(tc, " && (");
  469. for (k = 0; k < qln; k++)
  470. { if (k > 0)
  471. fprintf(tc, "\n\t\t\t || ");
  472. bb_or_dd(j, 1);
  473. fprintf(tc, "(%s%s", nm, z->name);
  474. if (qln > 1)
  475. fprintf(tc, "[%d]", k);
  476. fprintf(tc, ")");
  477. }
  478. fprintf(tc, "))\n\t\t\t ");
  479. if (j < 3)
  480. fprintf(tc, "|| ");
  481. else
  482. fprintf(tc, " ");
  483. }
  484. fprintf(tc, ") return 0; break;\n");
  485. }
  486. static void
  487. Docase(Symbol *s, int pid, int nid)
  488. { int i, j;
  489. fprintf(tc, "\t\tcase %d: if (", nid);
  490. for (j = 0; j < 4; j++)
  491. { fprintf(tc, "\t(t->ty[i] == ");
  492. bb_or_dd(j, 0);
  493. fprintf(tc, " && (");
  494. if (has_unless)
  495. { for (i = 0; i < s->nel; i++)
  496. dolen(s, DD[j].CC, pid, i, s->nel);
  497. } else
  498. { for (i = 0; i < s->nel; i++)
  499. dolen(s, BB[j].CC, pid, i, s->nel);
  500. }
  501. fprintf(tc, "))\n\t\t\t ");
  502. if (j < 3)
  503. fprintf(tc, "|| ");
  504. else
  505. fprintf(tc, " ");
  506. }
  507. fprintf(tc, ") return 0; break;\n");
  508. }
  509. static void
  510. genconditionals(void)
  511. { Symbol *s;
  512. int last=0, j;
  513. extern Ordered *all_names;
  514. Ordered *walk;
  515. fprintf(th, "#define LOCAL 1\n");
  516. fprintf(th, "#define Q_FULL_F 2\n");
  517. fprintf(th, "#define Q_EMPT_F 3\n");
  518. fprintf(th, "#define Q_EMPT_T 4\n");
  519. fprintf(th, "#define Q_FULL_T 5\n");
  520. fprintf(th, "#define TIMEOUT_F 6\n");
  521. fprintf(th, "#define GLOBAL 7\n");
  522. fprintf(th, "#define BAD 8\n");
  523. fprintf(th, "#define ALPHA_F 9\n");
  524. fprintf(tc, "int\n");
  525. fprintf(tc, "q_cond(short II, Trans *t)\n");
  526. fprintf(tc, "{ int i = 0;\n");
  527. fprintf(tc, " for (i = 0; i < 6; i++)\n");
  528. fprintf(tc, " { if (t->ty[i] == TIMEOUT_F) return %s;\n",
  529. (Etimeouts)?"(!(trpt->tau&1))":"1");
  530. fprintf(tc, " if (t->ty[i] == ALPHA_F)\n");
  531. fprintf(tc, "#ifdef GLOB_ALPHA\n");
  532. fprintf(tc, " return 0;\n");
  533. fprintf(tc, "#else\n\t\t\treturn ");
  534. fprintf(tc, "(II+1 == (short) now._nr_pr && II+1 < MAXPROC);\n");
  535. fprintf(tc, "#endif\n");
  536. /* we switch on the chan name from the spec (as identified by
  537. * the corresponding Nid number) rather than the actual qid
  538. * because we cannot predict at compile time which specific qid
  539. * will be accessed by the statement at runtime. that is:
  540. * we do not know which qid to pass to q_cond at runtime
  541. * but we do know which name is used. if it's a chan array, we
  542. * must check all elements of the array for compliance (bummer)
  543. */
  544. fprintf(tc, " switch (t->qu[i]) {\n");
  545. fprintf(tc, " case 0: break;\n");
  546. for (walk = all_names; walk; walk = walk->next)
  547. { s = walk->entry;
  548. if (s->owner) continue;
  549. j = find_id(s->context);
  550. if (s->type == CHAN)
  551. { if (last == s->Nid) continue; /* chan array */
  552. last = s->Nid;
  553. Docase(s, j, last);
  554. } else if (s->type == STRUCT)
  555. { /* struct may contain a chan */
  556. char pregat[128];
  557. extern void walk2_struct(char *, Symbol *);
  558. strcpy(pregat, "");
  559. if (!(s->hidden&1))
  560. { if (s->context)
  561. sprintf(pregat, "((P%d *)this)->",j);
  562. else
  563. sprintf(pregat, "now.");
  564. }
  565. walk2_struct(pregat, s);
  566. }
  567. }
  568. fprintf(tc, " \tdefault: Uerror(\"unknown qid - q_cond\");\n");
  569. fprintf(tc, " \t\t\treturn 0;\n");
  570. fprintf(tc, " \t}\n");
  571. fprintf(tc, " }\n");
  572. fprintf(tc, " return 1;\n");
  573. fprintf(tc, "}\n");
  574. }
  575. static void
  576. putproc(ProcList *p)
  577. { Pid = p->tn;
  578. Det = p->det;
  579. if (Pid == claimnr
  580. && separate == 1)
  581. { fprintf(th, "extern uchar reached%d[];\n", Pid);
  582. fprintf(th, "extern short nstates%d;\n", Pid);
  583. fprintf(th, "extern short src_ln%d[];\n", Pid);
  584. fprintf(th, "extern S_F_MAP src_file%d[];\n", Pid);
  585. fprintf(th, "#define endstate%d %d\n",
  586. Pid, p->s->last?p->s->last->seqno:0);
  587. fprintf(th, "#define src_claim src_ln%d\n", claimnr);
  588. return;
  589. }
  590. if (Pid != claimnr
  591. && separate == 2)
  592. return;
  593. AllGlobal = (p->prov)?1:0; /* process has provided clause */
  594. fprintf(th, "\nshort nstates%d=%d;\t/* %s */\n",
  595. Pid, p->s->maxel, p->n->name);
  596. if (Pid == claimnr)
  597. fprintf(th, "#define nstates_claim nstates%d\n", Pid);
  598. if (Pid == eventmapnr)
  599. fprintf(th, "#define nstates_event nstates%d\n", Pid);
  600. fprintf(th, "#define endstate%d %d\n",
  601. Pid, p->s->last->seqno);
  602. fprintf(tm, "\n /* PROC %s */\n", p->n->name);
  603. fprintf(tb, "\n /* PROC %s */\n", p->n->name);
  604. fprintf(tt, "\n /* proctype %d: %s */\n", Pid, p->n->name);
  605. fprintf(tt, "\n trans[%d] = (Trans **)", Pid);
  606. fprintf(tt, " emalloc(%d*sizeof(Trans *));\n\n", p->s->maxel);
  607. if (Pid == eventmapnr)
  608. { fprintf(th, "\n#define in_s_scope(x_y3_) 0");
  609. fprintf(tc, "\n#define in_r_scope(x_y3_) 0");
  610. }
  611. put_seq(p->s, 2, 0);
  612. if (Pid == eventmapnr)
  613. { fprintf(th, "\n\n");
  614. fprintf(tc, "\n\n");
  615. }
  616. dumpsrc(p->s->maxel, Pid);
  617. }
  618. static void
  619. addTpe(int x)
  620. { int i;
  621. if (x <= 2) return;
  622. for (i = 0; i < T_sum; i++)
  623. if (TPE[i] == x)
  624. return;
  625. TPE[(T_sum++)%2] = x;
  626. }
  627. static void
  628. cnt_seq(Sequence *s)
  629. { Element *f;
  630. SeqList *h;
  631. if (s)
  632. for (f = s->frst; ; f = f->nxt)
  633. { Tpe(f->n); /* sets EPT */
  634. addTpe(EPT[0]);
  635. addTpe(EPT[1]);
  636. for (h = f->sub; h; h = h->nxt)
  637. cnt_seq(h->this);
  638. if (f == s->last)
  639. break;
  640. }
  641. }
  642. static void
  643. typ_seq(Sequence *s)
  644. {
  645. T_sum = 0;
  646. TPE[0] = 2; TPE[1] = 0;
  647. cnt_seq(s);
  648. if (T_sum > 2) /* more than one type */
  649. { TPE[0] = 5*DELTA; /* non-mixing */
  650. TPE[1] = 0;
  651. }
  652. }
  653. static int
  654. hidden(Lextok *n)
  655. {
  656. if (n)
  657. switch (n->ntyp) {
  658. case FULL: case EMPTY:
  659. case NFULL: case NEMPTY: case TIMEOUT:
  660. Nn[(T_mus++)%2] = n;
  661. break;
  662. case '!': case UMIN: case '~': case ASSERT: case 'c':
  663. (void) hidden(n->lft);
  664. break;
  665. case '/': case '*': case '-': case '+':
  666. case '%': case LT: case GT: case '&': case '^':
  667. case '|': case LE: case GE: case NE: case '?':
  668. case EQ: case OR: case AND: case LSHIFT: case RSHIFT:
  669. (void) hidden(n->lft);
  670. (void) hidden(n->rgt);
  671. break;
  672. }
  673. return T_mus;
  674. }
  675. static int
  676. getNid(Lextok *n)
  677. {
  678. if (n->sym->type == STRUCT
  679. && n->rgt && n->rgt->lft)
  680. return getNid(n->rgt->lft);
  681. if (n->sym->Nid == 0)
  682. { fatal("bad channel name '%s'",
  683. (n->sym)?n->sym->name:"no name");
  684. }
  685. return n->sym->Nid;
  686. }
  687. static int
  688. valTpe(Lextok *n)
  689. { int res = 2;
  690. /*
  691. 2 = local
  692. 2+1 .. 2+1*DELTA = nfull, 's' - require q_full==false
  693. 2+1+1*DELTA .. 2+2*DELTA = nempty, 'r' - require q_len!=0
  694. 2+1+2*DELTA .. 2+3*DELTA = empty - require q_len==0
  695. 2+1+3*DELTA .. 2+4*DELTA = full - require q_full==true
  696. 5*DELTA = non-mixing (i.e., always makes the selection global)
  697. 6*DELTA = timeout (conditionally safe)
  698. 7*DELTA = @, process deletion (conditionally safe)
  699. */
  700. switch (n->ntyp) { /* a series of fall-thru cases: */
  701. case FULL: res += DELTA; /* add 3*DELTA + chan nr */
  702. case EMPTY: res += DELTA; /* add 2*DELTA + chan nr */
  703. case 'r':
  704. case NEMPTY: res += DELTA; /* add 1*DELTA + chan nr */
  705. case 's':
  706. case NFULL: res += getNid(n->lft); /* add channel nr */
  707. break;
  708. case TIMEOUT: res = 6*DELTA; break;
  709. case '@': res = 7*DELTA; break;
  710. default: break;
  711. }
  712. return res;
  713. }
  714. static void
  715. Tpe(Lextok *n) /* mixing in selections */
  716. {
  717. EPT[0] = 2; EPT[1] = 0;
  718. if (!n) return;
  719. T_mus = 0;
  720. Nn[0] = Nn[1] = ZN;
  721. if (n->ntyp == 'c')
  722. { if (hidden(n->lft) > 2)
  723. { EPT[0] = 5*DELTA; /* non-mixing */
  724. EPT[1] = 0;
  725. return;
  726. }
  727. } else
  728. Nn[0] = n;
  729. if (Nn[0]) EPT[0] = valTpe(Nn[0]);
  730. if (Nn[1]) EPT[1] = valTpe(Nn[1]);
  731. }
  732. static void
  733. put_escp(Element *e)
  734. { int n;
  735. SeqList *x;
  736. if (e->esc && e->n->ntyp != GOTO && e->n->ntyp != '.')
  737. { for (x = e->esc, n = 0; x; x = x->nxt, n++)
  738. { int i = huntele(x->this->frst, e->status)->seqno;
  739. fprintf(tt, "\ttrans[%d][%d]->escp[%d] = %d;\n",
  740. Pid, e->seqno, n, i);
  741. fprintf(tt, "\treached%d[%d] = 1;\n",
  742. Pid, i);
  743. }
  744. for (x = e->esc, n=0; x; x = x->nxt, n++)
  745. { fprintf(tt, " /* escape #%d: %d */\n", n,
  746. huntele(x->this->frst, e->status)->seqno);
  747. put_seq(x->this, 2, 0); /* args?? */
  748. }
  749. fprintf(tt, " /* end-escapes */\n");
  750. }
  751. }
  752. static void
  753. put_sub(Element *e, int Tt0, int Tt1)
  754. { Sequence *s = e->n->sl->this;
  755. Element *g = ZE;
  756. int a;
  757. patch_atomic(s);
  758. putskip(s->frst->seqno);
  759. g = huntstart(s->frst);
  760. a = g->seqno;
  761. if ((e->n->ntyp == ATOMIC
  762. || e->n->ntyp == D_STEP)
  763. && scan_seq(s))
  764. mark_seq(s);
  765. s->last->nxt = e->nxt;
  766. typ_seq(s); /* sets TPE */
  767. if (e->n->ntyp == D_STEP)
  768. { int inherit = (e->status&(ATOM|L_ATOM));
  769. fprintf(tm, "\tcase %d: ", uniq++);
  770. fprintf(tm, "/* STATE %d - line %d %s - [",
  771. e->seqno, e->n->ln, e->n->fn->name);
  772. comment(tm, e->n, 0);
  773. fprintf(tm, "] */\n\t\t");
  774. if (s->last->n->ntyp == BREAK)
  775. OkBreak = target(huntele(s->last->nxt,
  776. s->last->status))->Seqno;
  777. else
  778. OkBreak = -1;
  779. if (!putcode(tm, s, e->nxt, 0, e->n->ln))
  780. fprintf(tm, "\t\tm = %d; goto P999;\n\n",
  781. getweight(s->frst->n));
  782. fprintf(tb, "\tcase %d: ", uniq-1);
  783. fprintf(tb, "/* STATE %d */\n", e->seqno);
  784. fprintf(tb, "\t\tsv_restor();\n");
  785. fprintf(tb, "\t\tgoto R999;\n");
  786. if (e->nxt)
  787. a = huntele(e->nxt, e->status)->seqno;
  788. else
  789. a = 0;
  790. tr_map(uniq-1, e);
  791. fprintf(tt, "/*->*/\ttrans[%d][%d]\t= ",
  792. Pid, e->seqno);
  793. fprintf(tt, "settr(%d,%d,%d,%d,%d,\"",
  794. e->Seqno, D_ATOM|inherit, a, uniq-1, uniq-1);
  795. comment(tt, e->n, e->seqno);
  796. fprintf(tt, "\", %d, ", (s->frst->status&I_GLOB)?1:0);
  797. fprintf(tt, "%d, %d);\n", TPE[0], TPE[1]);
  798. put_escp(e);
  799. } else
  800. { /* ATOMIC or NON_ATOMIC */
  801. fprintf(tt, "\tT = trans[ %d][%d] = ", Pid, e->seqno);
  802. fprintf(tt, "settr(%d,%d,0,0,0,\"",
  803. e->Seqno, (e->n->ntyp == ATOMIC)?ATOM:0);
  804. comment(tt, e->n, e->seqno);
  805. if ((e->status&CHECK2)
  806. || (g->status&CHECK2))
  807. s->frst->status |= I_GLOB;
  808. fprintf(tt, "\", %d, %d, %d);",
  809. (s->frst->status&I_GLOB)?1:0, Tt0, Tt1);
  810. blurb(tt, e);
  811. fprintf(tt, "\tT->nxt\t= ");
  812. fprintf(tt, "settr(%d,%d,%d,0,0,\"",
  813. e->Seqno, (e->n->ntyp == ATOMIC)?ATOM:0, a);
  814. comment(tt, e->n, e->seqno);
  815. fprintf(tt, "\", %d, ", (s->frst->status&I_GLOB)?1:0);
  816. if (e->n->ntyp == NON_ATOMIC)
  817. { fprintf(tt, "%d, %d);", Tt0, Tt1);
  818. blurb(tt, e);
  819. put_seq(s, Tt0, Tt1);
  820. } else
  821. { fprintf(tt, "%d, %d);", TPE[0], TPE[1]);
  822. blurb(tt, e);
  823. put_seq(s, TPE[0], TPE[1]);
  824. }
  825. }
  826. }
  827. typedef struct CaseCache {
  828. int m, b;
  829. Element *e;
  830. Lextok *n;
  831. FSM_use *u;
  832. struct CaseCache *nxt;
  833. } CaseCache;
  834. CaseCache *casing[6];
  835. static int
  836. identical(Lextok *p, Lextok *q)
  837. {
  838. if ((!p && q) || (p && !q))
  839. return 0;
  840. if (!p)
  841. return 1;
  842. if (p->ntyp != q->ntyp
  843. || p->ismtyp != q->ismtyp
  844. || p->val != q->val
  845. || p->indstep != q->indstep
  846. || p->sym != q->sym
  847. || p->sq != q->sq
  848. || p->sl != q->sl)
  849. return 0;
  850. return identical(p->lft, q->lft)
  851. && identical(p->rgt, q->rgt);
  852. }
  853. static int
  854. samedeads(FSM_use *a, FSM_use *b)
  855. { FSM_use *p, *q;
  856. for (p = a, q = b; p && q; p = p->nxt, q = q->nxt)
  857. if (p->var != q->var
  858. || p->special != q->special)
  859. return 0;
  860. return (!p && !q);
  861. }
  862. static Element *
  863. findnext(Element *f)
  864. { Element *g;
  865. if (f->n->ntyp == GOTO)
  866. { g = get_lab(f->n, 1);
  867. return huntele(g, f->status);
  868. }
  869. return f->nxt;
  870. }
  871. static Element *
  872. advance(Element *e, int stopat)
  873. { Element *f = e;
  874. if (stopat)
  875. while (f && f->seqno != stopat)
  876. { f = findnext(f);
  877. switch (f->n->ntyp) {
  878. case GOTO:
  879. case '.':
  880. case PRINT:
  881. break;
  882. default:
  883. return f;
  884. }
  885. }
  886. return (Element *) 0;
  887. }
  888. static int
  889. equiv_merges(Element *a, Element *b)
  890. { Element *f, *g;
  891. int stopat_a, stopat_b;
  892. if (a->merge_start)
  893. stopat_a = a->merge_start;
  894. else
  895. stopat_a = a->merge;
  896. if (b->merge_start)
  897. stopat_b = b->merge_start;
  898. else
  899. stopat_b = b->merge;
  900. if (!stopat_a && !stopat_b)
  901. return 1;
  902. for (;;)
  903. {
  904. f = advance(a, stopat_a);
  905. g = advance(b, stopat_b);
  906. if (!f && !g)
  907. return 1;
  908. if (f && g)
  909. return identical(f->n, g->n);
  910. else
  911. return 0;
  912. }
  913. return 1;
  914. }
  915. static CaseCache *
  916. prev_case(Element *e)
  917. { int j; CaseCache *nc;
  918. switch (e->n->ntyp) {
  919. case 'r': j = 0; break;
  920. case 's': j = 1; break;
  921. case 'c': j = 2; break;
  922. case ASGN: j = 3; break;
  923. case ASSERT: j = 4; break;
  924. default: j = 5; break;
  925. }
  926. for (nc = casing[j]; nc; nc = nc->nxt)
  927. if (identical(nc->n, e->n)
  928. && samedeads(nc->u, e->dead)
  929. && equiv_merges(nc->e, e))
  930. return nc;
  931. return (CaseCache *) 0;
  932. }
  933. static void
  934. new_case(Element *e, int m, int b)
  935. { int j; CaseCache *nc;
  936. switch (e->n->ntyp) {
  937. case 'r': j = 0; break;
  938. case 's': j = 1; break;
  939. case 'c': j = 2; break;
  940. case ASGN: j = 3; break;
  941. case ASSERT: j = 4; break;
  942. default: j = 5; break;
  943. }
  944. nc = (CaseCache *) emalloc(sizeof(CaseCache));
  945. nc->m = m;
  946. nc->b = b;
  947. nc->e = e;
  948. nc->n = e->n;
  949. nc->u = e->dead;
  950. nc->nxt = casing[j];
  951. casing[j] = nc;
  952. }
  953. static int
  954. nr_bup(Element *e)
  955. { FSM_use *u;
  956. Lextok *v;
  957. int nr = 0;
  958. switch (e->n->ntyp) {
  959. case ASGN:
  960. nr++;
  961. break;
  962. case 'r':
  963. if (e->n->val == 1)
  964. nr++; /* random recv */
  965. for (v = e->n->rgt; v; v = v->rgt)
  966. { if ((v->lft->ntyp == CONST
  967. || v->lft->ntyp == EVAL))
  968. continue;
  969. nr++;
  970. }
  971. break;
  972. default:
  973. break;
  974. }
  975. for (u = e->dead; u; u = u->nxt)
  976. { switch (u->special) {
  977. case 2: /* dead after write */
  978. if (e->n->ntyp == ASGN
  979. && e->n->rgt->ntyp == CONST
  980. && e->n->rgt->val == 0)
  981. break;
  982. nr++;
  983. break;
  984. case 1: /* dead after read */
  985. nr++;
  986. break;
  987. } }
  988. return nr;
  989. }
  990. static int
  991. nrhops(Element *e)
  992. { Element *f = e, *g;
  993. int cnt = 0;
  994. int stopat;
  995. if (e->merge_start)
  996. stopat = e->merge_start;
  997. else
  998. stopat = e->merge;
  999. do {
  1000. cnt += nr_bup(f);
  1001. if (f->n->ntyp == GOTO)
  1002. { g = get_lab(f->n, 1);
  1003. f = huntele(g, f->status);
  1004. } else
  1005. f = f->nxt;
  1006. #if 1
  1007. if (f && !f->merge && !f->merge_single && f->seqno != stopat)
  1008. {
  1009. fprintf(tm, "\n\t\tbad hop %s:%d -- at %d, <",
  1010. f->n->fn->name,f->n->ln, f->seqno);
  1011. comment(tm, f->n, 0);
  1012. fprintf(tm, "> looking for %d -- merge %d:%d:%d\n\t\t",
  1013. stopat, f->merge, f->merge_start, f->merge_single);
  1014. break;
  1015. }
  1016. #endif
  1017. } while (f && f->seqno != stopat);
  1018. return cnt;
  1019. }
  1020. static void
  1021. check_needed(void)
  1022. {
  1023. if (multi_needed)
  1024. { fprintf(tm, "(trpt+1)->bup.ovals = grab_ints(%d);\n\t\t",
  1025. multi_needed);
  1026. multi_undo = multi_needed;
  1027. multi_needed = 0;
  1028. }
  1029. }
  1030. static void
  1031. doforward(FILE *tm, Element *e)
  1032. { FSM_use *u;
  1033. putstmnt(tm, e->n, e->seqno);
  1034. if (e->n->ntyp != ELSE && Det)
  1035. { fprintf(tm, ";\n\t\tif (trpt->o_pm&1)\n\t\t");
  1036. fprintf(tm, "\tuerror(\"non-determinism in D_proctype\")");
  1037. }
  1038. for (u = e->dead; u; u = u->nxt)
  1039. { fprintf(tm, ";\n\t\t/* dead %d: %s */ ",
  1040. u->special, u->var->name);
  1041. switch (u->special) {
  1042. case 2: /* dead after write -- lval already bupped */
  1043. if (e->n->ntyp == ASGN) /* could be recv or asgn */
  1044. { if (e->n->rgt->ntyp == CONST
  1045. && e->n->rgt->val == 0)
  1046. continue; /* already set to 0 */
  1047. }
  1048. if (e->n->ntyp != 'r')
  1049. { XZ.sym = u->var;
  1050. putname(tm, "", &XZ, 0, " = 0");
  1051. break;
  1052. } /* else fall through */
  1053. case 1: /* dead after read -- add asgn of rval -- needs bup */
  1054. YZ[YZmax].sym = u->var; /* store for pan.b */
  1055. CnT[YZcnt]++; /* this step added bups */
  1056. if (multi_oval)
  1057. { check_needed();
  1058. fprintf(tm, "(trpt+1)->bup.ovals[%d] = ",
  1059. multi_oval-1);
  1060. multi_oval++;
  1061. } else
  1062. fprintf(tm, "(trpt+1)->bup.oval = ");
  1063. putname(tm, "", &YZ[YZmax], 0, "");
  1064. putname(tm, ";\n\t\t", &YZ[YZmax], 0, " = 0");
  1065. YZmax++;
  1066. break;
  1067. } }
  1068. fprintf(tm, ";\n\t\t");
  1069. }
  1070. static int
  1071. dobackward(Element *e, int casenr)
  1072. {
  1073. if (!any_undo(e->n) && CnT[YZcnt] == 0)
  1074. { YZcnt--;
  1075. return 0;
  1076. }
  1077. if (!didcase)
  1078. { fprintf(tb, "\n\tcase %d: ", casenr);
  1079. fprintf(tb, "/* STATE %d */\n\t\t", e->seqno);
  1080. didcase++;
  1081. }
  1082. _isok++;
  1083. while (CnT[YZcnt] > 0) /* undo dead variable resets */
  1084. { CnT[YZcnt]--;
  1085. YZmax--;
  1086. if (YZmax < 0)
  1087. fatal("cannot happen, dobackward", (char *)0);
  1088. fprintf(tb, ";\n\t/* %d */\t", YZmax);
  1089. putname(tb, "", &YZ[YZmax], 0, " = trpt->bup.oval");
  1090. if (multi_oval > 0)
  1091. { multi_oval--;
  1092. fprintf(tb, "s[%d]", multi_oval-1);
  1093. }
  1094. }
  1095. if (e->n->ntyp != '.')
  1096. { fprintf(tb, ";\n\t\t");
  1097. undostmnt(e->n, e->seqno);
  1098. }
  1099. _isok--;
  1100. YZcnt--;
  1101. return 1;
  1102. }
  1103. static void
  1104. lastfirst(int stopat, Element *fin, int casenr)
  1105. { Element *f = fin, *g;
  1106. if (f->n->ntyp == GOTO)
  1107. { g = get_lab(f->n, 1);
  1108. f = huntele(g, f->status);
  1109. } else
  1110. f = f->nxt;
  1111. if (!f || f->seqno == stopat)
  1112. return;
  1113. lastfirst(stopat, f, casenr);
  1114. #if 0
  1115. fprintf(tb, "\n\t/* merge %d -- %d:%d %d:%d:%d (casenr %d) ",
  1116. YZcnt,
  1117. f->merge_start, f->merge,
  1118. f->seqno, f?f->seqno:-1, stopat,
  1119. casenr);
  1120. comment(tb, f->n, 0);
  1121. fprintf(tb, " */\n");
  1122. fflush(tb);
  1123. #endif
  1124. dobackward(f, casenr);
  1125. }
  1126. static int modifier;
  1127. static void
  1128. lab_transfer(Element *to, Element *from)
  1129. { Symbol *ns, *s = has_lab(from, (1|2|4));
  1130. Symbol *oc;
  1131. int ltp, usedit=0;
  1132. if (!s) return;
  1133. /* from could have all three labels -- rename
  1134. * to prevent jumps to the transfered copies
  1135. */
  1136. oc = context; /* remember */
  1137. for (ltp = 1; ltp < 8; ltp *= 2) /* 1, 2, and 4 */
  1138. if (s = has_lab(from, ltp))
  1139. { ns = (Symbol *) emalloc(sizeof(Symbol));
  1140. ns->name = (char *) emalloc(strlen(s->name) + 4);
  1141. sprintf(ns->name, "%s%d", s->name, modifier);
  1142. context = s->context;
  1143. set_lab(ns, to);
  1144. usedit++;
  1145. }
  1146. context = oc; /* restore */
  1147. if (usedit)
  1148. { if (modifier++ > 990)
  1149. fatal("modifier overflow error", (char *) 0);
  1150. }
  1151. }
  1152. static int
  1153. case_cache(Element *e, int a)
  1154. { int bupcase = 0, casenr = uniq, fromcache = 0;
  1155. CaseCache *Cached = (CaseCache *) 0;
  1156. Element *f, *g;
  1157. int j, nrbups, mark, target;
  1158. extern int ccache;
  1159. mark = (e->status&ATOM); /* could lose atomicity in a merge chain */
  1160. if (e->merge_mark > 0
  1161. || (merger && e->merge_in == 0))
  1162. { /* state is unreachable */
  1163. #if 0
  1164. casenr = bupcase = 0;
  1165. fromcache = -1;
  1166. goto gotit;
  1167. #else
  1168. return -1;
  1169. #endif
  1170. }
  1171. fprintf(tt, "\ttrans[%d][%d]\t= ", Pid, e->seqno);
  1172. if (ccache
  1173. #if 0
  1174. && !e->merge
  1175. && !e->merge_start
  1176. #endif
  1177. && Pid != claimnr
  1178. && (Cached = prev_case(e)))
  1179. { bupcase = Cached->b;
  1180. casenr = Cached->m;
  1181. fromcache = 1;
  1182. fprintf(tm, "/* STATE %d - line %d %s - [",
  1183. e->seqno, e->n->ln, e->n->fn->name);
  1184. comment(tm, e->n, 0);
  1185. fprintf(tm, "] (%d:%d - %d) same as %d (%d:%d - %d) */\n",
  1186. e->merge_start, e->merge, e->merge_in,
  1187. casenr,
  1188. Cached->e->merge_start, Cached->e->merge, Cached->e->merge_in);
  1189. goto gotit;
  1190. }
  1191. fprintf(tm, "\tcase %d: /* STATE %d - line %d %s - [",
  1192. uniq++, e->seqno, e->n->ln, e->n->fn->name);
  1193. comment(tm, e->n, 0);
  1194. nrbups = (e->merge || e->merge_start) ? nrhops(e) : nr_bup(e);
  1195. fprintf(tm, "] (%d:%d:%d - %d) */\n\t\t",
  1196. e->merge_start, e->merge, nrbups, e->merge_in);
  1197. if (nrbups > MAXMERGE-1)
  1198. fatal("merge requires more than 256 bups", (char *)0);
  1199. if (e->n->ntyp != 'r' && Pid != claimnr)
  1200. fprintf(tm, "IfNotBlocked\n\t\t");
  1201. if (multi_needed != 0 || multi_undo != 0)
  1202. fatal("cannot happen, case_cache", (char *) 0);
  1203. if (nrbups > 1)
  1204. { multi_oval = 1;
  1205. multi_needed = nrbups; /* allocated after edge condition */
  1206. } else
  1207. multi_oval = 0;
  1208. memset(CnT, 0, sizeof(CnT));
  1209. YZmax = YZcnt = 0;
  1210. doforward(tm, e);
  1211. if (e->merge_start)
  1212. target = e->merge_start;
  1213. else
  1214. target = e->merge;
  1215. if (target)
  1216. { f = e;
  1217. more: if (f->n->ntyp == GOTO)
  1218. { g = get_lab(f->n, 1);
  1219. f = huntele(g, f->status);
  1220. } else
  1221. f = f->nxt;
  1222. if (f && f->seqno != target)
  1223. {
  1224. fprintf(tm, "/* merge: ");
  1225. comment(tm, f->n, 0);
  1226. fprintf(tm, "(%d, %d, %d) */\n\t\t", f->merge, f->seqno, target);
  1227. fprintf(tm, "reached[ot][%d] = 1;\n\t\t", f->seqno);
  1228. YZcnt++;
  1229. lab_transfer(e, f);
  1230. mark = f->status&(ATOM|L_ATOM); /* last step wins */
  1231. doforward(tm, f);
  1232. if (f->merge_in == 1) f->merge_mark++;
  1233. goto more;
  1234. } }
  1235. fprintf(tm, "m = %d; goto P999; /* %d */\n", getweight(e->n), YZcnt);
  1236. multi_needed = 0;
  1237. didcase = 0;
  1238. if (target)
  1239. lastfirst(target, e, casenr); /* mergesteps only */
  1240. dobackward(e, casenr); /* the original step */
  1241. fprintf(tb, ";\n\t\t");
  1242. if (e->merge || e->merge_start)
  1243. { if (!didcase)
  1244. { fprintf(tb, "\n\tcase %d: ", casenr);
  1245. fprintf(tb, "/* STATE %d */", e->seqno);
  1246. didcase++;
  1247. } else
  1248. fprintf(tb, ";");
  1249. } else
  1250. fprintf(tb, ";");
  1251. fprintf(tb, "\n\t\t");
  1252. if (multi_undo)
  1253. { fprintf(tb, "ungrab_ints(trpt->bup.ovals, %d);\n\t\t",
  1254. multi_undo);
  1255. multi_undo = 0;
  1256. }
  1257. if (didcase)
  1258. { fprintf(tb, "goto R999;\n");
  1259. bupcase = casenr;
  1260. }
  1261. if (!e->merge && !e->merge_start)
  1262. new_case(e, casenr, bupcase);
  1263. gotit:
  1264. j = a;
  1265. if (e->merge_start)
  1266. j = e->merge_start;
  1267. else if (e->merge)
  1268. j = e->merge;
  1269. fprintf(tt, "%ssettr(%d,%d,%d,%d,%d,\"", fromcache?"/* c */ ":"",
  1270. e->Seqno, mark, j, casenr, bupcase);
  1271. return (fromcache)?0:casenr;
  1272. }
  1273. static void
  1274. put_el(Element *e, int Tt0, int Tt1)
  1275. { int a, casenr, Global_ref;
  1276. Element *g = ZE;
  1277. if (e->n->ntyp == GOTO)
  1278. { g = get_lab(e->n, 1);
  1279. g = huntele(g, e->status);
  1280. cross_dsteps(e->n, g->n);
  1281. a = g->seqno;
  1282. } else if (e->nxt)
  1283. { g = huntele(e->nxt, e->status);
  1284. a = g->seqno;
  1285. } else
  1286. a = 0;
  1287. if (g
  1288. && (g->status&CHECK2 /* entering remotely ref'd state */
  1289. || e->status&CHECK2)) /* leaving remotely ref'd state */
  1290. e->status |= I_GLOB;
  1291. /* don't remove dead edges in here, to preserve structure of fsm */
  1292. if (e->merge_start || e->merge)
  1293. goto non_generic;
  1294. /*** avoid duplicate or redundant cases in pan.m ***/
  1295. switch (e->n->ntyp) {
  1296. case ELSE:
  1297. casenr = 2; /* standard else */
  1298. goto generic_case;
  1299. break;
  1300. case '.':
  1301. case GOTO:
  1302. case BREAK:
  1303. putskip(e->seqno);
  1304. casenr = 1; /* standard goto */
  1305. generic_case: fprintf(tt, "\ttrans[%d][%d]\t= ", Pid, e->seqno);
  1306. fprintf(tt, "settr(%d,%d,%d,%d,0,\"",
  1307. e->Seqno, e->status&ATOM, a, casenr);
  1308. break;
  1309. #ifndef PRINTF
  1310. case PRINT:
  1311. if (e->n->lft == ZN)
  1312. { casenr = 1;
  1313. goto generic_case;
  1314. }
  1315. goto non_generic;
  1316. #endif
  1317. case 'c':
  1318. if (e->n->lft->ntyp == CONST
  1319. && e->n->lft->val == 1) /* skip or true */
  1320. { casenr = 1;
  1321. goto generic_case;
  1322. }
  1323. goto non_generic;
  1324. default:
  1325. non_generic:
  1326. casenr = case_cache(e, a);
  1327. if (casenr < 0) return; /* unreachable state */
  1328. break;
  1329. }
  1330. /* tailend of settr(...); */
  1331. Global_ref = (e->status&I_GLOB)?1:has_global(e->n);
  1332. comment(tt, e->n, e->seqno);
  1333. fprintf(tt, "\", %d, ", Global_ref);
  1334. if (Tt0 != 2)
  1335. { fprintf(tt, "%d, %d);", Tt0, Tt1);
  1336. } else
  1337. { Tpe(e->n); /* sets EPT */
  1338. fprintf(tt, "%d, %d);", EPT[0], EPT[1]);
  1339. }
  1340. if ((e->merge_start && e->merge_start != a)
  1341. || (e->merge && e->merge != a))
  1342. fprintf(tt, " /* m: %d -> %d,%d */",
  1343. a, e->merge_start, e->merge);
  1344. fprintf(tt, "\n");
  1345. if (casenr > 2)
  1346. tr_map(casenr, e);
  1347. put_escp(e);
  1348. }
  1349. static void
  1350. nested_unless(Element *e, Element *g)
  1351. { struct SeqList *y = e->esc, *z = g->esc;
  1352. for ( ; y && z; y = y->nxt, z = z->nxt)
  1353. if (z->this != y->this)
  1354. break;
  1355. if (!y && !z)
  1356. return;
  1357. if (g->n->ntyp != GOTO
  1358. && g->n->ntyp != '.'
  1359. && e->sub->nxt)
  1360. { printf("error: (%s:%d) saw 'unless' on a guard:\n",
  1361. (e->n)?e->n->fn->name:"-",
  1362. (e->n)?e->n->ln:0);
  1363. printf("=====>instead of\n");
  1364. printf(" do (or if)\n");
  1365. printf(" :: ...\n");
  1366. printf(" :: stmnt1 unless stmnt2\n");
  1367. printf(" od (of fi)\n");
  1368. printf("=====>use\n");
  1369. printf(" do (or if)\n");
  1370. printf(" :: ...\n");
  1371. printf(" :: stmnt1\n");
  1372. printf(" od (or fi) unless stmnt2\n");
  1373. printf("=====>or rewrite\n");
  1374. }
  1375. }
  1376. static void
  1377. put_seq(Sequence *s, int Tt0, int Tt1)
  1378. { SeqList *h;
  1379. Element *e, *g;
  1380. int a, deadlink;
  1381. for (e = s->frst; e; e = e->nxt)
  1382. { if (e->status & DONE)
  1383. goto checklast;
  1384. e->status |= DONE;
  1385. if (e->n->ln)
  1386. putsrc(e);
  1387. if (e->n->ntyp == UNLESS)
  1388. { put_seq(e->sub->this, Tt0, Tt1);
  1389. } else if (e->sub)
  1390. { fprintf(tt, "\tT = trans[%d][%d] = ",
  1391. Pid, e->seqno);
  1392. fprintf(tt, "settr(%d,%d,0,0,0,\"",
  1393. e->Seqno, e->status&ATOM);
  1394. comment(tt, e->n, e->seqno);
  1395. if (e->status&CHECK2)
  1396. e->status |= I_GLOB;
  1397. fprintf(tt, "\", %d, %d, %d);",
  1398. (e->status&I_GLOB)?1:0, Tt0, Tt1);
  1399. blurb(tt, e);
  1400. for (h = e->sub; h; h = h->nxt)
  1401. { putskip(h->this->frst->seqno);
  1402. g = huntstart(h->this->frst);
  1403. if (g->esc)
  1404. nested_unless(e, g);
  1405. a = g->seqno;
  1406. if (g->n->ntyp == 'c'
  1407. && g->n->lft->ntyp == CONST
  1408. && g->n->lft->val == 0 /* 0 or false */
  1409. && !g->esc)
  1410. { fprintf(tt, "#if 0\n\t/* dead link: */\n");
  1411. deadlink = 1;
  1412. if (verbose&32)
  1413. printf("spin: line %3d %s, Warning: condition is always false\n",
  1414. g->n->ln, g->n->fn?g->n->fn->name:"");
  1415. } else
  1416. deadlink = 0;
  1417. if (h->nxt)
  1418. fprintf(tt, "\tT = T->nxt\t= ");
  1419. else
  1420. fprintf(tt, "\t T->nxt\t= ");
  1421. fprintf(tt, "settr(%d,%d,%d,0,0,\"",
  1422. e->Seqno, e->status&ATOM, a);
  1423. comment(tt, e->n, e->seqno);
  1424. if (g->status&CHECK2)
  1425. h->this->frst->status |= I_GLOB;
  1426. fprintf(tt, "\", %d, %d, %d);",
  1427. (h->this->frst->status&I_GLOB)?1:0,
  1428. Tt0, Tt1);
  1429. blurb(tt, e);
  1430. if (deadlink)
  1431. fprintf(tt, "#endif\n");
  1432. }
  1433. for (h = e->sub; h; h = h->nxt)
  1434. put_seq(h->this, Tt0, Tt1);
  1435. } else
  1436. { if (e->n->ntyp == ATOMIC
  1437. || e->n->ntyp == D_STEP
  1438. || e->n->ntyp == NON_ATOMIC)
  1439. put_sub(e, Tt0, Tt1);
  1440. else
  1441. put_el(e, Tt0, Tt1);
  1442. }
  1443. checklast: if (e == s->last)
  1444. break;
  1445. }
  1446. }
  1447. static void
  1448. patch_atomic(Sequence *s) /* catch goto's that break the chain */
  1449. { Element *f, *g;
  1450. SeqList *h;
  1451. for (f = s->frst; ; f = f->nxt)
  1452. { if (f->n && f->n->ntyp == GOTO)
  1453. { g = get_lab(f->n,1);
  1454. cross_dsteps(f->n, g->n);
  1455. if ((f->status & (ATOM|L_ATOM))
  1456. && !(g->status & (ATOM|L_ATOM)))
  1457. { f->status &= ~ATOM;
  1458. f->status |= L_ATOM;
  1459. }
  1460. /* new - bridges atomics */
  1461. if ((f->status & L_ATOM)
  1462. && (g->status & (ATOM|L_ATOM)))
  1463. { f->status &= ~L_ATOM;
  1464. f->status |= ATOM;
  1465. }
  1466. /* end */
  1467. } else
  1468. for (h = f->sub; h; h = h->nxt)
  1469. patch_atomic(h->this);
  1470. if (f == s->extent)
  1471. break;
  1472. }
  1473. }
  1474. static void
  1475. mark_seq(Sequence *s)
  1476. { Element *f;
  1477. SeqList *h;
  1478. for (f = s->frst; ; f = f->nxt)
  1479. { f->status |= I_GLOB;
  1480. for (h = f->sub; h; h = h->nxt)
  1481. mark_seq(h->this);
  1482. if (f == s->last)
  1483. return;
  1484. }
  1485. }
  1486. static Element *
  1487. find_target(Element *e)
  1488. { Element *f;
  1489. if (!e) return e;
  1490. if (t_cyc++ > 32)
  1491. { fatal("cycle of goto jumps", (char *) 0);
  1492. }
  1493. switch (e->n->ntyp) {
  1494. case GOTO:
  1495. f = get_lab(e->n,1);
  1496. cross_dsteps(e->n, f->n);
  1497. f = find_target(f);
  1498. break;
  1499. case BREAK:
  1500. if (e->nxt)
  1501. f = find_target(huntele(e->nxt, e->status));
  1502. /* else fall through */
  1503. default:
  1504. f = e;
  1505. break;
  1506. }
  1507. return f;
  1508. }
  1509. Element *
  1510. target(Element *e)
  1511. {
  1512. if (!e) return e;
  1513. lineno = e->n->ln;
  1514. Fname = e->n->fn;
  1515. t_cyc = 0;
  1516. return find_target(e);
  1517. }
  1518. static int
  1519. scan_seq(Sequence *s)
  1520. { Element *f, *g;
  1521. SeqList *h;
  1522. for (f = s->frst; ; f = f->nxt)
  1523. { if ((f->status&CHECK2)
  1524. || has_global(f->n))
  1525. return 1;
  1526. if (f->n->ntyp == GOTO) /* may reach other atomic */
  1527. { g = target(f);
  1528. if (g
  1529. && !(f->status & L_ATOM)
  1530. && !(g->status & (ATOM|L_ATOM)))
  1531. { fprintf(tt, " /* goto mark-down, ");
  1532. fprintf(tt, "line %d - %d */\n",
  1533. f->n->ln, (g->n)?g->n->ln:0);
  1534. return 1; /* assume worst case */
  1535. } }
  1536. for (h = f->sub; h; h = h->nxt)
  1537. if (scan_seq(h->this))
  1538. return 1;
  1539. if (f == s->last)
  1540. break;
  1541. }
  1542. return 0;
  1543. }
  1544. static int
  1545. glob_args(Lextok *n)
  1546. { int result = 0;
  1547. Lextok *v;
  1548. for (v = n->rgt; v; v = v->rgt)
  1549. { if (v->lft->ntyp == CONST)
  1550. continue;
  1551. if (v->lft->ntyp == EVAL)
  1552. result += has_global(v->lft->lft);
  1553. else
  1554. result += has_global(v->lft);
  1555. }
  1556. return result;
  1557. }
  1558. int
  1559. has_global(Lextok *n)
  1560. { Lextok *v; extern int runsafe;
  1561. if (!n) return 0;
  1562. if (AllGlobal) return 1; /* global provided clause */
  1563. switch (n->ntyp) {
  1564. case '.': case BREAK: case GOTO: case CONST:
  1565. return 0;
  1566. case ELSE: return n->val; /* true if combined with chan refs */
  1567. case NAME: return (n->sym->context || (n->sym->hidden&64))?0:1;
  1568. case 's': return glob_args(n)!=0 || ((n->sym->xu&(XS|XX)) != XS);
  1569. case 'r': return glob_args(n)!=0 || ((n->sym->xu&(XR|XX)) != XR);
  1570. case 'R': return glob_args(n)!=0 || (((n->sym->xu)&(XR|XS|XX)) != (XR|XS));
  1571. case NEMPTY: return ((n->sym->xu&(XR|XX)) != XR);
  1572. case NFULL: return ((n->sym->xu&(XS|XX)) != XS);
  1573. case FULL: return ((n->sym->xu&(XR|XX)) != XR);
  1574. case EMPTY: return ((n->sym->xu&(XS|XX)) != XS);
  1575. case LEN: return (((n->sym->xu)&(XR|XS|XX)) != (XR|XS));
  1576. case RUN: return 1-runsafe;
  1577. case ENABLED: case PC_VAL: case NONPROGRESS:
  1578. case 'p': case 'q':
  1579. case TIMEOUT:
  1580. return 1;
  1581. /* @ was 1 (global) since 2.8.5
  1582. in 3.0 it is considered local and
  1583. conditionally safe, provided:
  1584. II is the youngest process
  1585. and nrprocs < MAXPROCS
  1586. */
  1587. case '@': return 0;
  1588. case '!': case UMIN: case '~': case ASSERT:
  1589. return has_global(n->lft);
  1590. case '/': case '*': case '-': case '+':
  1591. case '%': case LT: case GT: case '&': case '^':
  1592. case '|': case LE: case GE: case NE: case '?':
  1593. case EQ: case OR: case AND: case LSHIFT:
  1594. case RSHIFT: case 'c': case ASGN:
  1595. return has_global(n->lft) || has_global(n->rgt);
  1596. case PRINT:
  1597. for (v = n->lft; v; v = v->rgt)
  1598. if (has_global(v->lft)) return 1;
  1599. return 0;
  1600. }
  1601. return 0;
  1602. }
  1603. static void
  1604. Bailout(FILE *fd, char *str)
  1605. {
  1606. if (!GenCode)
  1607. fprintf(fd, "continue%s", str);
  1608. else if (IsGuard)
  1609. fprintf(fd, "%s%s", NextLab[Level], str);
  1610. else
  1611. fprintf(fd, "Uerror(\"block in step seq\")%s", str);
  1612. }
  1613. #define cat0(x) putstmnt(fd,now->lft,m); fprintf(fd, x); \
  1614. putstmnt(fd,now->rgt,m)
  1615. #define cat1(x) fprintf(fd,"("); cat0(x); fprintf(fd,")")
  1616. #define cat2(x,y) fprintf(fd,x); putstmnt(fd,y,m)
  1617. #define cat3(x,y,z) fprintf(fd,x); putstmnt(fd,y,m); fprintf(fd,z)
  1618. void
  1619. putstmnt(FILE *fd, Lextok *now, int m)
  1620. { Lextok *v;
  1621. int i, j;
  1622. if (!now) { fprintf(fd, "0"); return; }
  1623. lineno = now->ln;
  1624. Fname = now->fn;
  1625. switch (now->ntyp) {
  1626. case CONST: fprintf(fd, "%d", now->val); break;
  1627. case '!': cat3(" !(", now->lft, ")"); break;
  1628. case UMIN: cat3(" -(", now->lft, ")"); break;
  1629. case '~': cat3(" ~(", now->lft, ")"); break;
  1630. case '/': cat1("/"); break;
  1631. case '*': cat1("*"); break;
  1632. case '-': cat1("-"); break;
  1633. case '+': cat1("+"); break;
  1634. case '%': cat1("%%"); break;
  1635. case '&': cat1("&"); break;
  1636. case '^': cat1("^"); break;
  1637. case '|': cat1("|"); break;
  1638. case LT: cat1("<"); break;
  1639. case GT: cat1(">"); break;
  1640. case LE: cat1("<="); break;
  1641. case GE: cat1(">="); break;
  1642. case NE: cat1("!="); break;
  1643. case EQ: cat1("=="); break;
  1644. case OR: cat1("||"); break;
  1645. case AND: cat1("&&"); break;
  1646. case LSHIFT: cat1("<<"); break;
  1647. case RSHIFT: cat1(">>"); break;
  1648. case TIMEOUT:
  1649. if (separate == 2)
  1650. fprintf(fd, "((tau)&1)");
  1651. else
  1652. fprintf(fd, "((trpt->tau)&1)");
  1653. if (GenCode)
  1654. printf("spin: line %3d, warning: 'timeout' in d_step sequence\n",
  1655. lineno);
  1656. /* is okay as a guard */
  1657. break;
  1658. case RUN:
  1659. if (claimproc
  1660. && strcmp(now->sym->name, claimproc) == 0)
  1661. fatal("claim %s, (not runnable)", claimproc);
  1662. if (eventmap
  1663. && strcmp(now->sym->name, eventmap) == 0)
  1664. fatal("eventmap %s, (not runnable)", eventmap);
  1665. if (GenCode)
  1666. fatal("'run' in d_step sequence (use atomic)",
  1667. (char *)0);
  1668. fprintf(fd,"addproc(%d", fproc(now->sym->name));
  1669. for (v = now->lft, i = 0; v; v = v->rgt, i++)
  1670. { cat2(", ", v->lft);
  1671. }
  1672. if (i > Npars)
  1673. fatal("too many parameters in run %s(...)",
  1674. now->sym->name);
  1675. for ( ; i < Npars; i++)
  1676. fprintf(fd, ", 0");
  1677. fprintf(fd, ")");
  1678. break;
  1679. case ENABLED:
  1680. cat3("enabled(II, ", now->lft, ")");
  1681. break;
  1682. case NONPROGRESS:
  1683. /* o_pm&4=progress, tau&128=claim stutter */
  1684. if (separate == 2)
  1685. fprintf(fd, "(!(o_pm&4) && !(tau&128))");
  1686. else
  1687. fprintf(fd, "(!(trpt->o_pm&4) && !(trpt->tau&128))");
  1688. break;
  1689. case PC_VAL:
  1690. cat3("((P0 *) Pptr(", now->lft, "+BASE))->_p");
  1691. break;
  1692. case LEN:
  1693. if (!terse && !TestOnly && has_xu)
  1694. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1695. putname(fd, "(!(q_claim[", now->lft, m, "]&1) || ");
  1696. putname(fd, "q_R_check(", now->lft, m, "");
  1697. fprintf(fd, ", II)) &&\n\t\t");
  1698. putname(fd, "(!(q_claim[", now->lft, m, "]&2) || ");
  1699. putname(fd, "q_S_check(", now->lft, m, ", II)) &&");
  1700. fprintf(fd, "\n#endif\n\t\t");
  1701. }
  1702. putname(fd, "q_len(", now->lft, m, ")");
  1703. break;
  1704. case FULL:
  1705. if (!terse && !TestOnly && has_xu)
  1706. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1707. putname(fd, "(!(q_claim[", now->lft, m, "]&1) || ");
  1708. putname(fd, "q_R_check(", now->lft, m, "");
  1709. fprintf(fd, ", II)) &&\n\t\t");
  1710. putname(fd, "(!(q_claim[", now->lft, m, "]&2) || ");
  1711. putname(fd, "q_S_check(", now->lft, m, ", II)) &&");
  1712. fprintf(fd, "\n#endif\n\t\t");
  1713. }
  1714. putname(fd, "q_full(", now->lft, m, ")");
  1715. break;
  1716. case EMPTY:
  1717. if (!terse && !TestOnly && has_xu)
  1718. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1719. putname(fd, "(!(q_claim[", now->lft, m, "]&1) || ");
  1720. putname(fd, "q_R_check(", now->lft, m, "");
  1721. fprintf(fd, ", II)) &&\n\t\t");
  1722. putname(fd, "(!(q_claim[", now->lft, m, "]&2) || ");
  1723. putname(fd, "q_S_check(", now->lft, m, ", II)) &&");
  1724. fprintf(fd, "\n#endif\n\t\t");
  1725. }
  1726. putname(fd, "(q_len(", now->lft, m, ")==0)");
  1727. break;
  1728. case NFULL:
  1729. if (!terse && !TestOnly && has_xu)
  1730. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1731. putname(fd, "(!(q_claim[", now->lft, m, "]&2) || ");
  1732. putname(fd, "q_S_check(", now->lft, m, ", II)) &&");
  1733. fprintf(fd, "\n#endif\n\t\t");
  1734. }
  1735. putname(fd, "(!q_full(", now->lft, m, "))");
  1736. break;
  1737. case NEMPTY:
  1738. if (!terse && !TestOnly && has_xu)
  1739. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1740. putname(fd, "(!(q_claim[", now->lft, m, "]&1) || ");
  1741. putname(fd, "q_R_check(", now->lft, m, ", II)) &&");
  1742. fprintf(fd, "\n#endif\n\t\t");
  1743. }
  1744. putname(fd, "(q_len(", now->lft, m, ")>0)");
  1745. break;
  1746. case 's':
  1747. if (Pid == eventmapnr)
  1748. { fprintf(fd, "if (_tp != 's' ");
  1749. putname(fd, "|| _qid+1 != ", now->lft, m, "");
  1750. for (v = now->rgt, i=0; v; v = v->rgt, i++)
  1751. { if (v->lft->ntyp != CONST
  1752. && v->lft->ntyp != EVAL)
  1753. continue;
  1754. fprintf(fd, " \\\n\t\t|| qrecv(");
  1755. putname(fd, "", now->lft, m, ", ");
  1756. putname(fd, "q_len(", now->lft, m, ")-1, ");
  1757. fprintf(fd, "%d, 0) != ", i);
  1758. if (v->lft->ntyp == CONST)
  1759. putstmnt(fd, v->lft, m);
  1760. else /* EVAL */
  1761. putstmnt(fd, v->lft->lft, m);
  1762. }
  1763. fprintf(fd, ")\n");
  1764. fprintf(fd, "\t\t continue");
  1765. putname(th, " || (x_y3_ == ", now->lft, m, ")");
  1766. break;
  1767. }
  1768. if (TestOnly)
  1769. { if (m_loss)
  1770. fprintf(fd, "1");
  1771. else
  1772. putname(fd, "!q_full(", now->lft, m, ")");
  1773. break;
  1774. }
  1775. if (has_xu)
  1776. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1777. putname(fd, "if (q_claim[", now->lft, m, "]&2) ");
  1778. putname(fd, "q_S_check(", now->lft, m, ", II);");
  1779. fprintf(fd, "\n#endif\n\t\t");
  1780. }
  1781. fprintf(fd, "if (q_%s",
  1782. (u_sync > 0 && u_async == 0)?"len":"full");
  1783. putname(fd, "(", now->lft, m, "))\n");
  1784. if (m_loss)
  1785. { if (!GenCode)
  1786. fprintf(fd, "\t\t{ nlost++; m=3; goto P999; }\n");
  1787. else
  1788. fprintf(fd, "\t\t\tnlost++;\n\t\telse {");
  1789. } else
  1790. { fprintf(fd, "\t\t\t");
  1791. Bailout(fd, ";");
  1792. }
  1793. if (has_enabled)
  1794. fprintf(fd, "\n\t\tif (TstOnly) return 1;");
  1795. #if 1
  1796. if (u_sync && !u_async && rvopt)
  1797. fprintf(fd, "\n\n\t\tif (no_recvs(II)) continue;\n");
  1798. #endif
  1799. putname(fd, "\n\t\tqsend(", now->lft, m, "");
  1800. fprintf(fd, ", %d", now->val);
  1801. for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  1802. { cat2(", ", v->lft);
  1803. }
  1804. if (i > Mpars)
  1805. { putname(stdout, "channel name: ", now->lft, m, "\n");
  1806. printf(" %d msg parameters\n", i);
  1807. fatal("too many pars in send", "");
  1808. }
  1809. for ( ; i < Mpars; i++)
  1810. fprintf(fd, ", 0");
  1811. fprintf(fd, ")");
  1812. if (u_sync)
  1813. { fprintf(fd, ";\n\t\t");
  1814. if (u_async)
  1815. putname(fd, "if (q_zero(", now->lft, m, ")) ");
  1816. putname(fd, "{ boq = ", now->lft, m, "");
  1817. if (GenCode)
  1818. fprintf(fd, "; Uerror(\"rv-attempt in d_step\")");
  1819. fprintf(fd, "; }");
  1820. }
  1821. if (m_loss && GenCode)
  1822. fprintf(fd, ";\n\t\t}\n\t\t");
  1823. break;
  1824. case 'r':
  1825. if (Pid == eventmapnr)
  1826. { fprintf(fd, "if (_tp != 'r' ");
  1827. putname(fd, "|| _qid+1 != ", now->lft, m, "");
  1828. for (v = now->rgt, i=0; v; v = v->rgt, i++)
  1829. { if (v->lft->ntyp != CONST
  1830. && v->lft->ntyp != EVAL)
  1831. continue;
  1832. fprintf(fd, " \\\n\t\t|| qrecv(");
  1833. putname(fd, "", now->lft, m, ", ");
  1834. fprintf(fd, "0, %d, 0) != ", i);
  1835. if (v->lft->ntyp == CONST)
  1836. putstmnt(fd, v->lft, m);
  1837. else /* EVAL */
  1838. putstmnt(fd, v->lft->lft, m);
  1839. }
  1840. fprintf(fd, ")\n");
  1841. fprintf(fd, "\t\t continue");
  1842. putname(tc, " || (x_y3_ == ", now->lft, m, ")");
  1843. break;
  1844. }
  1845. if (TestOnly)
  1846. { fprintf(fd, "((");
  1847. if (u_sync)
  1848. fprintf(fd, "(boq == -1 && ");
  1849. putname(fd, "q_len(", now->lft, m, ")");
  1850. if (u_sync && now->val <= 1)
  1851. { putname(fd, ") || (boq == ", now->lft,m," && ");
  1852. putname(fd, "q_zero(", now->lft,m,"))");
  1853. }
  1854. fprintf(fd, ")");
  1855. if (now->val == 0 || now->val == 2)
  1856. { for (v = now->rgt, i=j=0; v; v = v->rgt, i++)
  1857. { if (v->lft->ntyp == CONST)
  1858. { cat3("\n\t\t&& (", v->lft, " == ");
  1859. putname(fd, "qrecv(", now->lft, m, ", ");
  1860. fprintf(fd, "0, %d, 0))", i);
  1861. } else if (v->lft->ntyp == EVAL)
  1862. { cat3("\n\t\t&& (", v->lft->lft, " == ");
  1863. putname(fd, "qrecv(", now->lft, m, ", ");
  1864. fprintf(fd, "0, %d, 0))", i);
  1865. } else
  1866. { j++; continue;
  1867. }
  1868. }
  1869. fprintf(fd, ")");
  1870. } else
  1871. { fprintf(fd, "\n\t\t&& Q_has(");
  1872. putname(fd, "", now->lft, m, "");
  1873. for (v = now->rgt, i=0; v; v = v->rgt, i++)
  1874. { if (v->lft->ntyp == CONST)
  1875. { fprintf(fd, ", 1, ");
  1876. putstmnt(fd, v->lft, m);
  1877. } else if (v->lft->ntyp == EVAL)
  1878. { fprintf(fd, ", 1, ");
  1879. putstmnt(fd, v->lft->lft, m);
  1880. } else
  1881. { fprintf(fd, ", 0, 0");
  1882. } }
  1883. for ( ; i < Mpars; i++)
  1884. fprintf(fd, ", 0, 0");
  1885. fprintf(fd, "))");
  1886. }
  1887. break;
  1888. }
  1889. if (has_xu)
  1890. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  1891. putname(fd, "if (q_claim[", now->lft, m, "]&1) ");
  1892. putname(fd, "q_R_check(", now->lft, m, ", II);");
  1893. fprintf(fd, "\n#endif\n\t\t");
  1894. }
  1895. if (u_sync)
  1896. { if (now->val >= 2)
  1897. { if (u_async)
  1898. { fprintf(fd, "if (");
  1899. putname(fd, "q_zero(", now->lft,m,"))");
  1900. fprintf(fd, "\n\t\t{\t");
  1901. }
  1902. fprintf(fd, "uerror(\"polling ");
  1903. fprintf(fd, "rv chan\");\n\t\t");
  1904. if (u_async)
  1905. fprintf(fd, " continue;\n\t\t}\n\t\t");
  1906. fprintf(fd, "IfNotBlocked\n\t\t");
  1907. } else
  1908. { fprintf(fd, "if (");
  1909. if (u_async == 0)
  1910. putname(fd, "boq != ", now->lft,m,") ");
  1911. else
  1912. { putname(fd, "q_zero(", now->lft,m,"))");
  1913. fprintf(fd, "\n\t\t{\tif (boq != ");
  1914. putname(fd, "", now->lft,m,") ");
  1915. Bailout(fd, ";\n\t\t} else\n\t\t");
  1916. fprintf(fd, "{\tif (boq != -1) ");
  1917. }
  1918. Bailout(fd, ";\n\t\t");
  1919. if (u_async)
  1920. fprintf(fd, "}\n\t\t");
  1921. } }
  1922. putname(fd, "if (q_len(", now->lft, m, ") == 0) ");
  1923. Bailout(fd, "");
  1924. for (v = now->rgt, j=0; v; v = v->rgt)
  1925. { if (v->lft->ntyp != CONST
  1926. && v->lft->ntyp != EVAL)
  1927. j++; /* count settables */
  1928. }
  1929. fprintf(fd, ";\n\n\t\tXX=1");
  1930. /* test */ if (now->val == 0 || now->val == 2)
  1931. { for (v = now->rgt, i=0; v; v = v->rgt, i++)
  1932. { if (v->lft->ntyp == CONST)
  1933. { fprintf(fd, ";\n\t\t");
  1934. cat3("if (", v->lft, " != ");
  1935. putname(fd, "qrecv(", now->lft, m, ", ");
  1936. fprintf(fd, "0, %d, 0)) ", i);
  1937. Bailout(fd, "");
  1938. } else if (v->lft->ntyp == EVAL)
  1939. { fprintf(fd, ";\n\t\t");
  1940. cat3("if (", v->lft->lft, " != ");
  1941. putname(fd, "qrecv(", now->lft, m, ", ");
  1942. fprintf(fd, "0, %d, 0)) ", i);
  1943. Bailout(fd, "");
  1944. } }
  1945. } else /* random receive: val 1 or 3 */
  1946. { fprintf(fd, ";\n\t\tif (!(XX = Q_has(");
  1947. putname(fd, "", now->lft, m, "");
  1948. for (v = now->rgt, i=0; v; v = v->rgt, i++)
  1949. { if (v->lft->ntyp == CONST)
  1950. { fprintf(fd, ", 1, ");
  1951. putstmnt(fd, v->lft, m);
  1952. } else if (v->lft->ntyp == EVAL)
  1953. { fprintf(fd, ", 1, ");
  1954. putstmnt(fd, v->lft->lft, m);
  1955. } else
  1956. { fprintf(fd, ", 0, 0");
  1957. } }
  1958. for ( ; i < Mpars; i++)
  1959. fprintf(fd, ", 0, 0");
  1960. fprintf(fd, "))) ");
  1961. Bailout(fd, "");
  1962. /* new */
  1963. fprintf(fd, ";\n\t\t");
  1964. if (multi_oval)
  1965. { check_needed();
  1966. fprintf(fd, "(trpt+1)->bup.ovals[%d] = ",
  1967. multi_oval-1);
  1968. multi_oval++;
  1969. } else
  1970. fprintf(fd, "(trpt+1)->bup.oval = ");
  1971. fprintf(fd, "XX");
  1972. /* end */
  1973. }
  1974. if (has_enabled)
  1975. fprintf(fd, ";\n\t\tif (TstOnly) return 1");
  1976. if (j == 0 && now->val >= 2)
  1977. { fprintf(fd, ";\n\t\t");
  1978. break; /* poll without side-effect */
  1979. }
  1980. if (!GenCode)
  1981. { int jj = 0;
  1982. fprintf(fd, ";\n\t\t");
  1983. /* no variables modified */
  1984. if (j == 0 && now->val == 0)
  1985. { fprintf(fd, "if (q_flds[((Q0 *)qptr(");
  1986. putname(fd, "", now->lft, m, "-1))->_t]");
  1987. fprintf(fd, " != %d)\n\t", i);
  1988. fprintf(fd, "\t\tUerror(\"wrong nr of msg fields in rcv\");\n\t\t");
  1989. }
  1990. #if 0
  1991. putname(fd, "bup_q(II, ", now->lft, m, ");\n\t\t");
  1992. #else
  1993. for (v = now->rgt; v; v = v->rgt)
  1994. if ((v->lft->ntyp != CONST
  1995. && v->lft->ntyp != EVAL))
  1996. jj++; /* nr of vars needing bup */
  1997. if (jj)
  1998. for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  1999. { char tempbuf[64];
  2000. if ((v->lft->ntyp == CONST
  2001. || v->lft->ntyp == EVAL))
  2002. continue;
  2003. if (multi_oval)
  2004. { check_needed();
  2005. sprintf(tempbuf, "(trpt+1)->bup.ovals[%d] = ",
  2006. multi_oval-1);
  2007. multi_oval++;
  2008. } else
  2009. sprintf(tempbuf, "(trpt+1)->bup.oval = ");
  2010. if (v->lft->sym && !strcmp(v->lft->sym->name, "_"))
  2011. { fprintf(fd, tempbuf);
  2012. putname(fd, "qrecv(", now->lft, m, "");
  2013. fprintf(fd, ", XX-1, %d, 0);\n\t\t", i);
  2014. } else
  2015. { _isok++;
  2016. cat3(tempbuf, v->lft, ";\n\t\t");
  2017. _isok--;
  2018. }
  2019. }
  2020. #endif
  2021. }
  2022. /* set */ for (v = now->rgt, i = 0; v; v = v->rgt, i++)
  2023. { if ((v->lft->ntyp == CONST
  2024. || v->lft->ntyp == EVAL) && v->rgt)
  2025. continue;
  2026. fprintf(fd, ";\n\t\t");
  2027. if (v->lft->ntyp != CONST
  2028. && v->lft->ntyp != EVAL
  2029. && strcmp(v->lft->sym->name, "_") != 0)
  2030. { nocast=1;
  2031. _isok++;
  2032. putstmnt(fd, v->lft, m);
  2033. _isok--;
  2034. nocast=0;
  2035. fprintf(fd, " = ");
  2036. }
  2037. putname(fd, "qrecv(", now->lft, m, ", ");
  2038. fprintf(fd, "XX-1, %d, ", i);
  2039. fprintf(fd, "%d)", (v->rgt || now->val >= 2)?0:1);
  2040. if (v->lft->ntyp != CONST
  2041. && v->lft->ntyp != EVAL
  2042. && strcmp(v->lft->sym->name, "_") != 0
  2043. && (v->lft->ntyp != NAME
  2044. || v->lft->sym->type != CHAN))
  2045. { fprintf(fd, ";\n#ifdef VAR_RANGES");
  2046. fprintf(fd, "\n\t\tlogval(\"");
  2047. withprocname = terse = nocast = 1;
  2048. _isok++;
  2049. putstmnt(fd,v->lft,m);
  2050. withprocname = terse = nocast = 0;
  2051. fprintf(fd, "\", ");
  2052. putstmnt(fd,v->lft,m);
  2053. _isok--;
  2054. fprintf(fd, ");\n#endif\n");
  2055. fprintf(fd, "\t\t");
  2056. }
  2057. }
  2058. fprintf(fd, ";\n\t\t");
  2059. if (u_sync)
  2060. { putname(fd, "if (q_zero(", now->lft, m, "))");
  2061. fprintf(fd, "\n\t\t{ boq = -1;\n");
  2062. fprintf(fd, "#ifndef NOFAIR\n"); /* NEW 3.0.8 */
  2063. fprintf(fd, "\t\t\tif (fairness\n");
  2064. fprintf(fd, "\t\t\t&& !(trpt->o_pm&32)\n");
  2065. fprintf(fd, "\t\t\t&& (now._a_t&2)\n");
  2066. fprintf(fd, "\t\t\t&& now._cnt[now._a_t&1] == II+2)\n");
  2067. fprintf(fd, "\t\t\t{ now._cnt[now._a_t&1] -= 1;\n");
  2068. fprintf(fd, "#ifdef VERI\n");
  2069. fprintf(fd, "\t\t\t if (II == 1)\n");
  2070. fprintf(fd, "\t\t\t now._cnt[now._a_t&1] = 1;\n");
  2071. fprintf(fd, "#endif\n");
  2072. fprintf(fd, "#ifdef DEBUG\n");
  2073. fprintf(fd, "\t\t\tprintf(\"%%3d: proc %%d fairness \", depth, II);\n");
  2074. fprintf(fd, "\t\t\tprintf(\"Rule 2: --cnt to %%d (%%d)\\n\",\n");
  2075. fprintf(fd, "\t\t\t now._cnt[now._a_t&1], now._a_t);\n");
  2076. fprintf(fd, "#endif\n");
  2077. fprintf(fd, "\t\t\t trpt->o_pm |= (32|64);\n");
  2078. fprintf(fd, "\t\t\t}\n");
  2079. fprintf(fd, "#endif\n");
  2080. fprintf(fd, "\n\t\t}");
  2081. }
  2082. break;
  2083. case 'R':
  2084. if (!terse && !TestOnly && has_xu)
  2085. { fprintf(fd, "\n#ifndef XUSAFE\n\t\t");
  2086. putname(fd, "(!(q_claim[", now->lft, m, "]&1) || ");
  2087. fprintf(fd, "q_R_check(");
  2088. putname(fd, "", now->lft, m, ", II)) &&\n\t\t");
  2089. putname(fd, "(!(q_claim[", now->lft, m, "]&2) || ");
  2090. putname(fd, "q_S_check(", now->lft, m, ", II)) &&");
  2091. fprintf(fd, "\n#endif\n\t\t");
  2092. }
  2093. if (u_sync>0)
  2094. putname(fd, "not_RV(", now->lft, m, ") && \\\n\t\t");
  2095. for (v = now->rgt, i=j=0; v; v = v->rgt, i++)
  2096. if (v->lft->ntyp != CONST
  2097. && v->lft->ntyp != EVAL)
  2098. { j++; continue;
  2099. }
  2100. if (now->val == 0 || i == j)
  2101. { putname(fd, "(q_len(", now->lft, m, ") > 0");
  2102. for (v = now->rgt, i=0; v; v = v->rgt, i++)
  2103. { if (v->lft->ntyp != CONST
  2104. && v->lft->ntyp != EVAL)
  2105. continue;
  2106. fprintf(fd, " \\\n\t\t&& qrecv(");
  2107. putname(fd, "", now->lft, m, ", ");
  2108. fprintf(fd, "0, %d, 0) == ", i);
  2109. if (v->lft->ntyp == CONST)
  2110. putstmnt(fd, v->lft, m);
  2111. else /* EVAL */
  2112. putstmnt(fd, v->lft->lft, m);
  2113. }
  2114. fprintf(fd, ")");
  2115. } else
  2116. { putname(fd, "Q_has(", now->lft, m, "");
  2117. for (v = now->rgt, i=0; v; v = v->rgt, i++)
  2118. { if (v->lft->ntyp == CONST)
  2119. { fprintf(fd, ", 1, ");
  2120. putstmnt(fd, v->lft, m);
  2121. } else if (v->lft->ntyp == EVAL)
  2122. { fprintf(fd, ", 1, ");
  2123. putstmnt(fd, v->lft->lft, m);
  2124. } else
  2125. fprintf(fd, ", 0, 0");
  2126. }
  2127. for ( ; i < Mpars; i++)
  2128. fprintf(fd, ", 0, 0");
  2129. fprintf(fd, ")");
  2130. }
  2131. break;
  2132. case 'c':
  2133. cat3("if (!(", now->lft, "))\n\t\t\t");
  2134. Bailout(fd, "");
  2135. break;
  2136. case ELSE:
  2137. if (!GenCode)
  2138. { if (separate == 2)
  2139. fprintf(fd, "if (o_pm&1)\n\t\t\t");
  2140. else
  2141. fprintf(fd, "if (trpt->o_pm&1)\n\t\t\t");
  2142. Bailout(fd, "");
  2143. } else
  2144. { fprintf(fd, "/* else */");
  2145. }
  2146. break;
  2147. case '?':
  2148. cat3("( (", now->lft, ") ? ");
  2149. cat3("(", now->rgt->lft, ") : ");
  2150. cat3("(", now->rgt->rgt, ") )");
  2151. break;
  2152. case ASGN:
  2153. if (has_enabled)
  2154. fprintf(fd, "if (TstOnly) return 1;\n\t\t");
  2155. _isok++;
  2156. if (!GenCode)
  2157. { if (multi_oval)
  2158. { char tempbuf[64];
  2159. check_needed();
  2160. sprintf(tempbuf, "(trpt+1)->bup.ovals[%d] = ",
  2161. multi_oval-1);
  2162. multi_oval++;
  2163. cat3(tempbuf, now->lft, ";\n\t\t");
  2164. } else
  2165. { cat3("(trpt+1)->bup.oval = ", now->lft, ";\n\t\t");
  2166. } }
  2167. nocast = 1; putstmnt(fd,now->lft,m); nocast = 0;
  2168. fprintf(fd," = ");
  2169. _isok--;
  2170. putstmnt(fd,now->rgt,m);
  2171. if (now->sym->type != CHAN
  2172. || verbose > 0)
  2173. { fprintf(fd, ";\n#ifdef VAR_RANGES");
  2174. fprintf(fd, "\n\t\tlogval(\"");
  2175. withprocname = terse = nocast = 1;
  2176. _isok++;
  2177. putstmnt(fd,now->lft,m);
  2178. withprocname = terse = nocast = 0;
  2179. fprintf(fd, "\", ");
  2180. putstmnt(fd,now->lft,m);
  2181. _isok--;
  2182. fprintf(fd, ");\n#endif\n");
  2183. fprintf(fd, "\t\t");
  2184. }
  2185. break;
  2186. case PRINT:
  2187. if (has_enabled)
  2188. fprintf(fd, "if (TstOnly) return 1;\n\t\t");
  2189. #ifdef PRINTF
  2190. fprintf(fd, "printf(%s", now->sym->name);
  2191. #else
  2192. fprintf(fd, "Printf(%s", now->sym->name);
  2193. #endif
  2194. for (v = now->lft; v; v = v->rgt)
  2195. { cat2(", ", v->lft);
  2196. }
  2197. fprintf(fd, ")");
  2198. break;
  2199. case NAME:
  2200. if (!nocast && now->sym && Sym_typ(now) < SHORT)
  2201. putname(fd, "((int)", now, m, ")");
  2202. else
  2203. putname(fd, "", now, m, "");
  2204. break;
  2205. case 'p':
  2206. putremote(fd, now, m);
  2207. break;
  2208. case 'q':
  2209. if (terse)
  2210. fprintf(fd, "%s", now->sym->name);
  2211. else
  2212. fprintf(fd, "%d", remotelab(now));
  2213. break;
  2214. case ASSERT:
  2215. if (has_enabled)
  2216. fprintf(fd, "if (TstOnly) return 1;\n\t\t");
  2217. cat3("assert(", now->lft, ", ");
  2218. terse = nocast = 1;
  2219. cat3("\"", now->lft, "\", II, tt, t)");
  2220. terse = nocast = 0;
  2221. break;
  2222. case '.':
  2223. case BREAK:
  2224. case GOTO:
  2225. if (Pid == eventmapnr)
  2226. fprintf(fd, "Uerror(\"cannot get here\")");
  2227. putskip(m);
  2228. break;
  2229. case '@':
  2230. if (Pid == eventmapnr)
  2231. { fprintf(fd, "return 0");
  2232. break;
  2233. }
  2234. if (has_enabled)
  2235. { fprintf(fd, "if (TstOnly)\n\t\t\t");
  2236. fprintf(fd, "return (II+1 == now._nr_pr);\n\t\t");
  2237. }
  2238. fprintf(fd, "if (!delproc(1, II)) ");
  2239. Bailout(fd, "");
  2240. break;
  2241. default:
  2242. printf("spin: bad node type %d (.m) - line %d\n",
  2243. now->ntyp, now->ln);
  2244. alldone(1);
  2245. }
  2246. }
  2247. void
  2248. putname(FILE *fd, char *pre, Lextok *n, int m, char *suff) /* varref */
  2249. { Symbol *s = n->sym;
  2250. lineno = n->ln; Fname = n->fn;
  2251. if (!s)
  2252. fatal("no name - putname", (char *) 0);
  2253. if (s->context && context && s->type)
  2254. s = findloc(s); /* it's a local var */
  2255. if (!s)
  2256. { fprintf(fd, "%s%s%s", pre, n->sym->name, suff);
  2257. return;
  2258. }
  2259. if (!s->type) /* not a local name */
  2260. s = lookup(s->name); /* must be a global */
  2261. if (!s->type)
  2262. { if (strcmp(pre, ".") != 0)
  2263. non_fatal("undeclared variable '%s'", s->name);
  2264. s->type = INT;
  2265. }
  2266. if (s->type == PROCTYPE)
  2267. fatal("proctype-name '%s' used as array-name", s->name);
  2268. fprintf(fd, pre);
  2269. if (!terse && !s->owner && evalindex != 1)
  2270. { if (s->context
  2271. || strcmp(s->name, "_p") == 0
  2272. || strcmp(s->name, "_pid") == 0)
  2273. { fprintf(fd, "((P%d *)this)->", Pid);
  2274. } else
  2275. { int x = strcmp(s->name, "_");
  2276. if (!(s->hidden&1) && x != 0)
  2277. fprintf(fd, "now.");
  2278. if (x == 0 && _isok == 0)
  2279. fatal("attempt to read value of '_'", 0);
  2280. } }
  2281. if (withprocname
  2282. && s->context
  2283. && strcmp(pre, "."))
  2284. fprintf(fd, "%s:", s->context->name);
  2285. if (evalindex != 1)
  2286. fprintf(fd, "%s", s->name);
  2287. if (s->nel != 1)
  2288. { if (no_arrays)
  2289. {
  2290. non_fatal("ref to array element invalid in this context",
  2291. (char *)0);
  2292. printf("\thint: instead of, e.g., x[rs] qu[3], use\n");
  2293. printf("\tchan nm_3 = qu[3]; x[rs] nm_3;\n");
  2294. printf("\tand use nm_3 in sends/recvs instead of qu[3]\n");
  2295. }
  2296. /* an xr or xs reference to an array element
  2297. * becomes an exclusion tag on the array itself -
  2298. * which could result in invalidly labeling
  2299. * operations on other elements of this array to
  2300. * be also safe under the partial order reduction
  2301. * (see procedure has_global())
  2302. */
  2303. if (evalindex == 2)
  2304. { fprintf(fd, "[%%d]");
  2305. } else if (evalindex == 1)
  2306. { evalindex = 0; /* no good if index is indexed array */
  2307. cat3(", ", n->lft, "");
  2308. evalindex = 1;
  2309. } else
  2310. { if (terse
  2311. || (n->lft
  2312. && n->lft->ntyp == CONST
  2313. && n->lft->val < s->nel)
  2314. || (!n->lft && s->nel > 0))
  2315. { cat3("[", n->lft, "]");
  2316. } else
  2317. { cat3("[ Index(", n->lft, ", ");
  2318. fprintf(fd, "%d) ]", s->nel);
  2319. }
  2320. }
  2321. }
  2322. if (s->type == STRUCT && n->rgt && n->rgt->lft)
  2323. { putname(fd, ".", n->rgt->lft, m, "");
  2324. }
  2325. fprintf(fd, suff);
  2326. }
  2327. void
  2328. putremote(FILE *fd, Lextok *n, int m) /* remote reference */
  2329. { int promoted = 0;
  2330. if (terse)
  2331. { fprintf(fd, "%s[", n->lft->sym->name);
  2332. putstmnt(fd, n->lft->lft, m);
  2333. fprintf(fd, "].%s", n->sym->name);
  2334. } else
  2335. { if (Sym_typ(n) < SHORT)
  2336. { promoted = 1;
  2337. fprintf(fd, "((int)");
  2338. }
  2339. fprintf(fd, "((P%d *)Pptr(",
  2340. fproc(n->lft->sym->name));
  2341. fprintf(fd, "BASE+");
  2342. putstmnt(fd, n->lft->lft, m);
  2343. fprintf(fd, "))->%s", n->sym->name);
  2344. }
  2345. if (n->rgt)
  2346. { fprintf(fd, "[");
  2347. putstmnt(fd, n->rgt, m);
  2348. fprintf(fd, "]");
  2349. }
  2350. if (promoted) fprintf(fd, ")");
  2351. }
  2352. static int
  2353. getweight(Lextok *n)
  2354. { /* this piece of code is a remnant of early versions
  2355. * of the verifier -- in the current version of Spin
  2356. * only non-zero values matter - so this could probably
  2357. * simply return 1 in all cases.
  2358. */
  2359. switch (n->ntyp) {
  2360. case 'r': return 4;
  2361. case 's': return 2;
  2362. case TIMEOUT: return 1;
  2363. case 'c': if (has_typ(n->lft, TIMEOUT)) return 1;
  2364. }
  2365. return 3;
  2366. }
  2367. int
  2368. has_typ(Lextok *n, int m)
  2369. {
  2370. if (!n) return 0;
  2371. if (n->ntyp == m) return 1;
  2372. return (has_typ(n->lft, m) || has_typ(n->rgt, m));
  2373. }