pattern.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* $Id$ */
  2. /*
  3. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  4. * See the copyright notice in the ACK home directory, in the file "Copyright".
  5. */
  6. /* p a t t e r n . c
  7. *
  8. * Deals with the pattern stuff.
  9. * it maintains a table of information about the patterns
  10. * Functions : addpattern() and printpatterns()
  11. */
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <ctype.h>
  16. #include "misc.h"
  17. #include "symtab.h"
  18. struct pattern {
  19. char *p_constraint; /* constraint of this pattern */
  20. int p_lineno, /* line number of constraint */
  21. p_npat, /* # of instructions in pattern */
  22. p_nrepl; /* # of instructions in replacement */
  23. };
  24. static struct pattern *pattable, /* ptr to pattern array */
  25. *current, /* ptr to first unoccupied el of
  26. * pattern array
  27. */
  28. *maxpat; /* if beyond this, new space must
  29. * be allocated
  30. */
  31. void addpattern(char *str, int l, int np, int nr)
  32. {
  33. /*
  34. * Just add a pattern to the list.
  35. * "str" is the constraint, "l" is the line number,
  36. * "np" is the number of instructions in the pattern,
  37. * "nr" is the number of instructions in the replacement
  38. * Space is allocated in chunks of 50
  39. */
  40. struct pattern *p;
  41. if (!pattable) { /* No space allocated yet */
  42. pattable = (struct pattern *) malloc(50 * sizeof *pattable);
  43. current = pattable;
  44. maxpat = pattable + 50;
  45. }
  46. if (current >= maxpat) { /* Allocate some new space */
  47. p = pattable;
  48. pattable = (struct pattern *) realloc(
  49. (char *) pattable,
  50. (unsigned) (sizeof *pattable * (50 + (maxpat - pattable))));
  51. current = pattable + (current - p);
  52. maxpat = pattable + (maxpat - p) + 50;
  53. }
  54. p = current++;
  55. p->p_constraint = str;
  56. p->p_lineno = l;
  57. p->p_npat = np;
  58. p->p_nrepl = nr;
  59. }
  60. static void prconstraint(char *str)
  61. {
  62. /*
  63. * prints a constraint, with variable names replaced
  64. */
  65. char c;
  66. char *p, *q;
  67. struct symtab *name;
  68. p = str;
  69. while (*p) {
  70. if (isupper(*p) || islower(*p) || *p == '_') {
  71. /*
  72. * Start of identifier
  73. */
  74. q = p + 1;
  75. while (*q && (
  76. isupper(*q) || islower(*q) || isdigit(*q) || *q == '_')) {
  77. q++;
  78. }
  79. c = *q;
  80. *q = '\0';
  81. /* Temporarily let it end with null byte */
  82. name = findident(p,LOOKING,&idtable);
  83. if (name) { /* yeah, it was a variable */
  84. fprintf(genc,"var[%d].value", name->s_num);
  85. }
  86. else if (!strcmp(p, "ANY")) {
  87. fputs("ANY.value", genc);
  88. }
  89. else fputs(p,genc);
  90. /* Now replace null byte with whatever used to be there */
  91. *q = c;
  92. p = q;
  93. }
  94. else {
  95. putc(*p,genc);
  96. p++;
  97. }
  98. }
  99. }
  100. void printpatterns()
  101. {
  102. /*
  103. * Prints the pattern_descr table and generates the routine
  104. * "check_constraint"
  105. */
  106. struct pattern *p;
  107. int i;
  108. p = pattable;
  109. i = 1;
  110. fputs("struct pattern_descr patterns[] = {\n", genc);
  111. while (p != current) {
  112. fprintf(genc," {%d,pat%d,%d,rep%d,},\n",
  113. p->p_npat, i, p->p_nrepl, i);
  114. p++;
  115. i++;
  116. }
  117. fputs("};\n", genc);
  118. fputs("int\ncheck_constraint(patno){\n\tint r;\n\tswitch(patno){\n",genc);
  119. p = pattable;
  120. while (p < current) {
  121. if (p->p_constraint) {
  122. /* The pattern has a constraint */
  123. fprintf(genc,"\tcase %ld :\n",(long)(p - pattable));
  124. fprintf(genc,linedir,p->p_lineno,inpfile); /* linedirective */
  125. fputs("\tr = (",genc);
  126. prconstraint(p->p_constraint);
  127. fputs("); break;\n",genc);
  128. }
  129. p++;
  130. }
  131. fputs("\tdefault :\n\t\tr = 1;\n\t}\n\treturn r;\n}\n\n",genc);
  132. }