pangen2.c 74 KB

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