reprosrc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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: reprosrc.c *****/
  10. /* Copyright (c) 2002-2003 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. #include <stdio.h>
  19. #include "spin.h"
  20. #include "y.tab.h"
  21. static int indent = 1;
  22. extern ProcList *rdy;
  23. void repro_seq(Sequence *);
  24. void
  25. doindent(void)
  26. { int i;
  27. for (i = 0; i < indent; i++)
  28. printf(" ");
  29. }
  30. void
  31. repro_sub(Element *e)
  32. {
  33. doindent();
  34. switch (e->n->ntyp) {
  35. case D_STEP:
  36. printf("d_step {\n");
  37. break;
  38. case ATOMIC:
  39. printf("atomic {\n");
  40. break;
  41. case NON_ATOMIC:
  42. printf(" {\n");
  43. break;
  44. }
  45. indent++;
  46. repro_seq(e->n->sl->this);
  47. indent--;
  48. doindent();
  49. printf(" };\n");
  50. }
  51. void
  52. repro_seq(Sequence *s)
  53. { Element *e;
  54. Symbol *v;
  55. SeqList *h;
  56. for (e = s->frst; e; e = e->nxt)
  57. {
  58. v = has_lab(e, 0);
  59. if (v) printf("%s:\n", v->name);
  60. if (e->n->ntyp == UNLESS)
  61. { printf("/* normal */ {\n");
  62. repro_seq(e->n->sl->this);
  63. doindent();
  64. printf("} unless {\n");
  65. repro_seq(e->n->sl->nxt->this);
  66. doindent();
  67. printf("}; /* end unless */\n");
  68. } else if (e->sub)
  69. {
  70. switch (e->n->ntyp) {
  71. case DO: doindent(); printf("do\n"); indent++; break;
  72. case IF: doindent(); printf("if\n"); indent++; break;
  73. }
  74. for (h = e->sub; h; h = h->nxt)
  75. { indent--; doindent(); indent++; printf("::\n");
  76. repro_seq(h->this);
  77. printf("\n");
  78. }
  79. switch (e->n->ntyp) {
  80. case DO: indent--; doindent(); printf("od;\n"); break;
  81. case IF: indent--; doindent(); printf("fi;\n"); break;
  82. }
  83. } else
  84. { if (e->n->ntyp == ATOMIC
  85. || e->n->ntyp == D_STEP
  86. || e->n->ntyp == NON_ATOMIC)
  87. repro_sub(e);
  88. else if (e->n->ntyp != '.'
  89. && e->n->ntyp != '@'
  90. && e->n->ntyp != BREAK)
  91. {
  92. doindent();
  93. if (e->n->ntyp == C_CODE)
  94. { printf("c_code ");
  95. plunk_inline(stdout, e->n->sym->name, 1, 1);
  96. } else if (e->n->ntyp == 'c'
  97. && e->n->lft->ntyp == C_EXPR)
  98. { printf("c_expr { ");
  99. plunk_expr(stdout, e->n->lft->sym->name);
  100. printf("} ->\n");
  101. } else
  102. { comment(stdout, e->n, 0);
  103. printf(";\n");
  104. } }
  105. }
  106. if (e == s->last)
  107. break;
  108. }
  109. }
  110. void
  111. repro_proc(ProcList *p)
  112. {
  113. if (!p) return;
  114. if (p->nxt) repro_proc(p->nxt);
  115. if (p->det) printf("D"); /* deterministic */
  116. printf("proctype %s()", p->n->name);
  117. if (p->prov)
  118. { printf(" provided ");
  119. comment(stdout, p->prov, 0);
  120. }
  121. printf("\n{\n");
  122. repro_seq(p->s);
  123. printf("}\n");
  124. }
  125. void
  126. repro_src(void)
  127. {
  128. repro_proc(rdy);
  129. }