reenable and fix a bug in memopt
[qbe.git] / tools / lexh.c
blob9e8016a0ffce175f18e41a0a50307b9ead15441f
1 /*% c99 -O3 -Wall -o # %
2 */
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <limits.h>
7 #include <stdint.h>
9 char *tok[] = {
11 "add", "sub", "div", "rem", "udiv", "urem", "mul",
12 "and", "or", "xor", "sar", "shr", "shl", "stored",
13 "stores", "storel", "storew", "storeh", "storeb",
14 "load", "loadsw", "loaduw", "loadsh", "loaduh",
15 "loadsb", "loadub", "extsw", "extuw", "extsh",
16 "extuh", "extsb", "extub", "exts", "truncd",
17 "stosi", "dtosi", "swtof", "sltof", "cast", "copy",
18 "alloc4", "alloc8", "alloc16", "culew", "cultw",
19 "cslew", "csltw", "csgtw", "csgew", "cugtw",
20 "cugew", "ceqw", "cnew", "culel", "cultl", "cslel",
21 "csltl", "csgtl", "csgel", "cugtl", "cugel",
22 "ceql", "cnel", "cles", "clts", "cgts", "cges",
23 "cnes", "ceqs", "cos", "cuos", "cled", "cltd",
24 "cgtd", "cged", "cned", "ceqd", "cod", "cuod",
25 "vaarg", "vastart", "...", "env",
27 "call", "phi", "jmp", "jnz", "ret", "export",
28 "function", "type", "data", "align", "l", "w",
29 "h", "b", "d", "s", "z", "loadw", "loadl", "loads",
30 "loadd", "alloc1", "alloc2",
33 enum {
34 Ntok = sizeof tok / sizeof tok[0]
37 uint32_t th[Ntok];
39 uint32_t
40 hash(char *s)
42 uint32_t h;
44 h = 0;
45 for (; *s; ++s)
46 h = *s + 17*h;
47 return h;
50 int
51 main()
53 char *bmap;
54 uint32_t h, M, K;
55 int i, j;
57 bmap = malloc(1u << 31);
59 for (i=0; i<Ntok; ++i) {
60 h = hash(tok[i]);
61 for (j=0; j<i; ++j)
62 if (th[j] == h) {
63 printf("error: hash()\n");
64 printf("\t%s\n", tok[i]);
65 printf("\t%s\n", tok[j]);
66 exit(1);
68 th[i] = h;
71 for (i=0; 1<<i < Ntok; ++i);
72 M = 32 - i;
74 for (;; --M) {
75 printf("trying M=%d...\n", M);
76 K = 1;
77 do {
78 memset(bmap, 0, 1 << (32 - M));
79 for (i=0; i<Ntok; ++i) {
80 h = (th[i]*K) >> M;
81 if (bmap[h])
82 break;
83 bmap[h] = 1;
85 if (i==Ntok) {
86 printf("found K=%d for M=%d\n", K, M);
87 exit(0);
89 K += 2;
90 } while (K != 1);