|
|
1.1 root 1: /* C compiler: string & list management */
2:
3: #include "c.h"
4:
5: #define TABLESIZE 1024
6: static struct string { /* strings: */
7: char *str; /* pointer to the string */
8: int len; /* its length */
9: struct string *link; /* next one on this chain */
10: } *buckets[TABLESIZE]; /* the string table */
11:
12: static char *next; /* slot for next allocated string */
13: static char *strlimit; /* 1 past end of current string region */
14: static int scatter[] = { /* map characters to random values */
15: 2078917053, 143302914, 1027100827, 1953210302, 755253631,
16: 2002600785, 1405390230, 45248011, 1099951567, 433832350,
17: 2018585307, 438263339, 813528929, 1703199216, 618906479,
18: 573714703, 766270699, 275680090, 1510320440, 1583583926,
19: 1723401032, 1965443329, 1098183682, 1636505764, 980071615,
20: 1011597961, 643279273, 1315461275, 157584038, 1069844923,
21: 471560540, 89017443, 1213147837, 1498661368, 2042227746,
22: 1968401469, 1353778505, 1300134328, 2013649480, 306246424,
23: 1733966678, 1884751139, 744509763, 400011959, 1440466707,
24: 1363416242, 973726663, 59253759, 1639096332, 336563455,
25: 1642837685, 1215013716, 154523136, 593537720, 704035832,
26: 1134594751, 1605135681, 1347315106, 302572379, 1762719719,
27: 269676381, 774132919, 1851737163, 1482824219, 125310639,
28: 1746481261, 1303742040, 1479089144, 899131941, 1169907872,
29: 1785335569, 485614972, 907175364, 382361684, 885626931,
30: 200158423, 1745777927, 1859353594, 259412182, 1237390611,
31: 48433401, 1902249868, 304920680, 202956538, 348303940,
32: 1008956512, 1337551289, 1953439621, 208787970, 1640123668,
33: 1568675693, 478464352, 266772940, 1272929208, 1961288571,
34: 392083579, 871926821, 1117546963, 1871172724, 1771058762,
35: 139971187, 1509024645, 109190086, 1047146551, 1891386329,
36: 994817018, 1247304975, 1489680608, 706686964, 1506717157,
37: 579587572, 755120366, 1261483377, 884508252, 958076904,
38: 1609787317, 1893464764, 148144545, 1415743291, 2102252735,
39: 1788268214, 836935336, 433233439, 2055041154, 2109864544,
40: 247038362, 299641085, 834307717
41: };
42: static List freenodes; /* free list nodes */
43:
44: char *stringn(str, n) char *str; {
45: int i;
46: unsigned int h;
47: char *end;
48: struct string *p;
49:
50: assert(str);
51: for (h = 0, i = n, end = str; i > 0; i--)
52: h = (h<<1) + scatter[*(unsigned char *)end++];
53: h &= TABLESIZE-1;
54: for (p = buckets[h]; p; p = p->link)
55: if (n == p->len) {
56: char *s1 = str, *s2 = p->str;
57: do {
58: if (s1 == end)
59: return p->str;
60: } while (*s1++ == *s2++);
61: }
62: if (next + n + 1 >= strlimit) {
63: int m = roundup(n, 8) + BUFSIZE;
64: next = alloc(m);
65: strlimit = next + m;
66: }
67: p = (struct string *) alloc(sizeof *p);
68: p->len = n;
69: for (p->str = next; str < end; )
70: *next++ = *str++;
71: *next++ = 0;
72: p->link = buckets[h];
73: buckets[h] = p;
74: return p->str;
75: }
76: char *string(str) char *str; {
77: char *s;
78:
79: for (s = str; *s; s++)
80: ;
81: return stringn(str, s - str);
82: }
83: char *stringd(n) {
84: char str[30], *s = &str[30];
85: unsigned m;
86:
87: if (n == INT_MIN)
88: m = (unsigned)INT_MAX + 1;
89: else if (n < 0)
90: m = -n;
91: else
92: m = n;
93: do
94: *--s = m%10 + '0';
95: while (m /= 10);
96: if (n < 0)
97: *--s = '-';
98: return stringn(s, &str[30] - s);
99: }
100: /* append - append x to list, return new list */
101: List append(x, list) Generic x; List list; {
102: List new;
103:
104: if (new = freenodes)
105: freenodes = freenodes->link;
106: else
107: new = (List)alloc(sizeof *new);
108: if (list) {
109: new->link = list->link;
110: list->link = new;
111: } else
112: new->link = new;
113: new->x = x;
114: return new;
115: }
116:
117: /* length - # elements in list */
118: int length(list) List list; {
119: int n = 0;
120:
121: if (list) {
122: List lp = list;
123: do n++;
124: while ((lp = lp->link) != list);
125: }
126: return n;
127: }
128:
129: /* ltoa - convert list to an 0-terminated array in a[0..length(list)] */
130: Generic *ltoa(list, a) List list; Generic a[]; {
131: int i = 0;
132:
133: if (a == 0)
134: a = (Generic *)talloc((length(list) + 1)*sizeof a[0]);
135: if (list) {
136: List lp = list;
137: do {
138: lp = lp->link;
139: a[i++] = lp->x;
140: } while (lp != list);
141: lp = list->link;
142: list->link = freenodes;
143: freenodes = lp;
144: }
145: a[i] = 0;
146: return a;
147: }
148:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.