tl_main.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /***** tl_spin: tl_main.c *****/
  2. /* Copyright (c) 1995-2003 by Lucent Technologies, Bell Laboratories. */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* No guarantee whatsoever is expressed or implied by the distribution of */
  5. /* this code. Permission is given to distribute this code provided that */
  6. /* this introductory message is not removed and no monies are exchanged. */
  7. /* Software written by Gerard J. Holzmann. For tool documentation see: */
  8. /* http://spinroot.com/ */
  9. /* Send all bug-reports and/or questions to: bugs@spinroot.com */
  10. /* Based on the translation algorithm by Gerth, Peled, Vardi, and Wolper, */
  11. /* presented at the PSTV Conference, held in 1995, Warsaw, Poland 1995. */
  12. #include "tl.h"
  13. extern FILE *tl_out;
  14. int newstates = 0; /* debugging only */
  15. int tl_errs = 0;
  16. int tl_verbose = 0;
  17. int tl_terse = 0;
  18. int tl_clutter = 0;
  19. unsigned long All_Mem = 0;
  20. static char uform[4096];
  21. static int hasuform=0, cnt=0;
  22. extern void cache_stats(void);
  23. extern void a_stats(void);
  24. int
  25. tl_Getchar(void)
  26. {
  27. if (cnt < hasuform)
  28. return uform[cnt++];
  29. cnt++;
  30. return -1;
  31. }
  32. void
  33. put_uform(void)
  34. {
  35. fprintf(tl_out, "%s", uform);
  36. }
  37. void
  38. tl_UnGetchar(void)
  39. {
  40. if (cnt > 0) cnt--;
  41. }
  42. static void
  43. tl_stats(void)
  44. { extern int Stack_mx;
  45. printf("total memory used: %9ld\n", All_Mem);
  46. printf("largest stack sze: %9d\n", Stack_mx);
  47. cache_stats();
  48. a_stats();
  49. }
  50. int
  51. tl_main(int argc, char *argv[])
  52. { int i;
  53. extern int verbose, xspin;
  54. tl_verbose = verbose;
  55. tl_clutter = 1-xspin; /* use -X -f to turn off uncluttering */
  56. while (argc > 1 && argv[1][0] == '-')
  57. { switch (argv[1][1]) {
  58. case 'd': newstates = 1; /* debugging mode */
  59. break;
  60. case 'f': argc--; argv++;
  61. for (i = 0; i < argv[1][i]; i++)
  62. { if (argv[1][i] == '\t'
  63. || argv[1][i] == '\"'
  64. || argv[1][i] == '\n')
  65. argv[1][i] = ' ';
  66. }
  67. strcpy(uform, argv[1]);
  68. hasuform = (int) strlen(uform);
  69. break;
  70. case 'v': tl_verbose++;
  71. break;
  72. case 'n': tl_terse = 1;
  73. break;
  74. default : printf("spin -f: saw '-%c'\n", argv[1][1]);
  75. goto nogood;
  76. }
  77. argc--; argv++;
  78. }
  79. if (hasuform == 0)
  80. {
  81. nogood: printf("usage:\tspin [-v] [-n] -f formula\n");
  82. printf(" -v verbose translation\n");
  83. printf(" -n normalize tl formula and exit\n");
  84. exit(1);
  85. }
  86. tl_parse();
  87. if (tl_verbose) tl_stats();
  88. return tl_errs;
  89. }
  90. #define Binop(a) \
  91. fprintf(tl_out, "("); \
  92. dump(n->lft); \
  93. fprintf(tl_out, a); \
  94. dump(n->rgt); \
  95. fprintf(tl_out, ")")
  96. void
  97. dump(Node *n)
  98. {
  99. if (!n) return;
  100. switch(n->ntyp) {
  101. case OR: Binop(" || "); break;
  102. case AND: Binop(" && "); break;
  103. case U_OPER: Binop(" U "); break;
  104. case V_OPER: Binop(" V "); break;
  105. #ifdef NXT
  106. case NEXT:
  107. fprintf(tl_out, "X");
  108. fprintf(tl_out, " (");
  109. dump(n->lft);
  110. fprintf(tl_out, ")");
  111. break;
  112. #endif
  113. case NOT:
  114. fprintf(tl_out, "!");
  115. fprintf(tl_out, " (");
  116. dump(n->lft);
  117. fprintf(tl_out, ")");
  118. break;
  119. case FALSE:
  120. fprintf(tl_out, "false");
  121. break;
  122. case TRUE:
  123. fprintf(tl_out, "true");
  124. break;
  125. case PREDICATE:
  126. fprintf(tl_out, "(%s)", n->sym->name);
  127. break;
  128. case -1:
  129. fprintf(tl_out, " D ");
  130. break;
  131. default:
  132. printf("Unknown token: ");
  133. tl_explain(n->ntyp);
  134. break;
  135. }
  136. }
  137. void
  138. tl_explain(int n)
  139. {
  140. switch (n) {
  141. case ALWAYS: printf("[]"); break;
  142. case EVENTUALLY: printf("<>"); break;
  143. case IMPLIES: printf("->"); break;
  144. case EQUIV: printf("<->"); break;
  145. case PREDICATE: printf("predicate"); break;
  146. case OR: printf("||"); break;
  147. case AND: printf("&&"); break;
  148. case NOT: printf("!"); break;
  149. case U_OPER: printf("U"); break;
  150. case V_OPER: printf("V"); break;
  151. #ifdef NXT
  152. case NEXT: printf("X"); break;
  153. #endif
  154. case TRUE: printf("true"); break;
  155. case FALSE: printf("false"); break;
  156. case ';': printf("end of formula"); break;
  157. default: printf("%c", n); break;
  158. }
  159. }
  160. static void
  161. tl_non_fatal(char *s1, char *s2)
  162. { extern int tl_yychar;
  163. int i;
  164. printf("tl_spin: ");
  165. if (s2)
  166. printf(s1, s2);
  167. else
  168. printf(s1);
  169. if (tl_yychar != -1 && tl_yychar != 0)
  170. { printf(", saw '");
  171. tl_explain(tl_yychar);
  172. printf("'");
  173. }
  174. printf("\ntl_spin: %s\n---------", uform);
  175. for (i = 0; i < cnt; i++)
  176. printf("-");
  177. printf("^\n");
  178. fflush(stdout);
  179. tl_errs++;
  180. }
  181. void
  182. tl_yyerror(char *s1)
  183. {
  184. Fatal(s1, (char *) 0);
  185. }
  186. void
  187. Fatal(char *s1, char *s2)
  188. {
  189. tl_non_fatal(s1, s2);
  190. /* tl_stats(); */
  191. exit(1);
  192. }