pangen2.c 80 KB

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