simplify 8*x as well as x*8
[qbe.git] / tools / lexh.c
blobefc30fe2c49792fe24ba193c052eb7fa569cbcde
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", "neg", "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", "stoui", "dtoui", "uwtof",
18 "ultof", "swtof", "sltof", "cast", "copy",
19 "alloc4", "alloc8", "alloc16", "culew", "cultw",
20 "cslew", "csltw", "csgtw", "csgew", "cugtw",
21 "cugew", "ceqw", "cnew", "culel", "cultl", "cslel",
22 "csltl", "csgtl", "csgel", "cugtl", "cugel",
23 "ceql", "cnel", "cles", "clts", "cgts", "cges",
24 "cnes", "ceqs", "cos", "cuos", "cled", "cltd",
25 "cgtd", "cged", "cned", "ceqd", "cod", "cuod",
26 "vaarg", "vastart", "...", "env", "dbgloc",
28 "call", "phi", "jmp", "jnz", "ret", "hlt", "export",
29 "function", "type", "data", "section", "align", "dbgfile",
30 "blit", "l", "w", "sh", "uh", "h", "sb", "ub", "b",
31 "d", "s", "z", "loadw", "loadl", "loads", "loadd",
32 "alloc1", "alloc2", "thread", "common",
35 enum {
36 Ntok = sizeof tok / sizeof tok[0]
39 uint32_t th[Ntok];
41 uint32_t
42 hash(char *s)
44 uint32_t h;
46 h = 0;
47 for (; *s; ++s)
48 h = *s + 17*h;
49 return h;
52 int
53 main()
55 char *bmap;
56 uint32_t h, M, K;
57 int i, j;
59 bmap = malloc(1u << 31);
61 for (i=0; i<Ntok; ++i) {
62 h = hash(tok[i]);
63 for (j=0; j<i; ++j)
64 if (th[j] == h) {
65 printf("error: hash()\n");
66 printf("\t%s\n", tok[i]);
67 printf("\t%s\n", tok[j]);
68 exit(1);
70 th[i] = h;
73 for (i=9; 1<<i < Ntok; ++i);
74 M = 32 - i;
76 for (;; --M) {
77 printf("trying M=%d...\n", M);
78 K = 1;
79 do {
80 memset(bmap, 0, 1 << (32 - M));
81 for (i=0; i<Ntok; ++i) {
82 h = (th[i]*K) >> M;
83 if (bmap[h])
84 break;
85 bmap[h] = 1;
87 if (i==Ntok) {
88 printf("found K=%d for M=%d\n", K, M);
89 exit(0);
91 K += 2;
92 } while (K != 1);