Chris Lattner | 633a5b1 | 2002-09-17 23:03:30 +0000 | [diff] [blame] | 1 | char rcsid_item[] = "$Id$"; |
| 2 | |
| 3 | #include "b.h" |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include "fe.h" |
| 7 | |
| 8 | static Item_Set fptr; |
| 9 | |
| 10 | ItemArray |
| 11 | newItemArray() |
| 12 | { |
| 13 | ItemArray ia; |
| 14 | ia = (ItemArray) zalloc(max_nonterminal *sizeof(*ia)); |
| 15 | return ia; |
| 16 | } |
| 17 | |
| 18 | ItemArray |
| 19 | itemArrayCopy(src) ItemArray src; |
| 20 | { |
| 21 | ItemArray dst; |
| 22 | |
| 23 | dst = newItemArray(); |
| 24 | memcpy(dst, src, max_nonterminal * sizeof(*dst)); |
| 25 | return dst; |
| 26 | } |
| 27 | |
| 28 | Item_Set |
| 29 | newItem_Set(relevant) Relevant relevant; |
| 30 | { |
| 31 | Item_Set ts; |
| 32 | |
| 33 | if (fptr) { |
| 34 | ts = fptr; |
| 35 | fptr = 0; |
| 36 | memset(ts->virgin, 0, max_nonterminal * sizeof(struct item)); |
| 37 | if (ts->closed) { |
| 38 | zfree(ts->closed); |
| 39 | ts->closed = 0; |
| 40 | } |
| 41 | ts->num = 0; |
| 42 | ts->op = 0; |
| 43 | } else { |
| 44 | ts = (Item_Set) zalloc(sizeof(struct item_set)); |
| 45 | ts->virgin = newItemArray(); |
| 46 | } |
| 47 | ts->relevant = relevant; |
| 48 | return ts; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | freeItem_Set(ts) Item_Set ts; |
| 53 | { |
| 54 | assert(!fptr); |
| 55 | fptr = ts; |
| 56 | } |
| 57 | |
| 58 | int |
| 59 | equivSet(a, b) Item_Set a; Item_Set b; |
| 60 | { |
| 61 | register Relevant r; |
| 62 | register int nt; |
| 63 | register Item *aa = a->virgin; |
| 64 | register Item *ba = b->virgin; |
| 65 | |
| 66 | /* |
| 67 | return !bcmp(a->virgin, b->virgin, max_nonterminal * sizeof(Item)); |
| 68 | */ |
| 69 | |
| 70 | r = a->relevant ? a->relevant : b->relevant; |
| 71 | assert(r); |
| 72 | |
| 73 | if (a->op && b->op && a->op != b->op) { |
| 74 | return 0; |
| 75 | } |
| 76 | for (; (nt = *r) != 0; r++) { |
| 77 | if (aa[nt].rule != ba[nt].rule || !EQUALCOST(aa[nt].delta, ba[nt].delta)) { |
| 78 | return 0; |
| 79 | } |
| 80 | } |
| 81 | return 1; |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | printRepresentative(f, s) FILE *f; Item_Set s; |
| 86 | { |
| 87 | if (!s) { |
| 88 | return; |
| 89 | } |
| 90 | fprintf(f, "%s", s->op->name); |
| 91 | switch (s->op->arity) { |
| 92 | case 1: |
| 93 | fprintf(f, "("); |
| 94 | printRepresentative(f, s->kids[0]); |
| 95 | fprintf(f, ")"); |
| 96 | break; |
| 97 | case 2: |
| 98 | fprintf(f, "("); |
| 99 | printRepresentative(f, s->kids[0]); |
| 100 | fprintf(f, ", "); |
| 101 | printRepresentative(f, s->kids[1]); |
| 102 | fprintf(f, ")"); |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | dumpItem(t) Item *t; |
| 109 | { |
| 110 | printf("[%s #%d]", t->rule->lhs->name, t->rule->num); |
| 111 | dumpCost(t->delta); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | dumpItem_Set(ts) Item_Set ts; |
| 116 | { |
| 117 | int i; |
| 118 | |
| 119 | printf("Item_Set #%d: [", ts->num); |
| 120 | for (i = 1; i < max_nonterminal; i++) { |
| 121 | if (ts->virgin[i].rule) { |
| 122 | printf(" %d", i); |
| 123 | dumpCost(ts->virgin[i].delta); |
| 124 | } |
| 125 | } |
| 126 | printf(" ]\n"); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | dumpCost(dc) DeltaCost dc; |
| 131 | { |
| 132 | printf("(%ld)", (long) dc); |
| 133 | } |