qbe

Internal scc patchset buffer for QBE
Log | Files | Refs | README | LICENSE

lexh.c (2003B)


      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>
      8 
      9 char *tok[] = {
     10 
     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",
     27 
     28 	"call", "phi", "jmp", "jnz", "ret", "hlt", "export",
     29 	"function", "type", "data", "section", "common", "align", "dbgfile",
     30 	"blit", "l", "w", "sh", "uh", "h", "sb", "ub", "b",
     31 	"d", "s", "z", "loadw", "loadl", "loads", "loadd",
     32 	"alloc1", "alloc2",
     33 
     34 };
     35 enum {
     36 	Ntok = sizeof tok / sizeof tok[0]
     37 };
     38 
     39 uint32_t th[Ntok];
     40 
     41 uint32_t
     42 hash(char *s)
     43 {
     44 	uint32_t h;
     45 
     46 	h = 0;
     47 	for (; *s; ++s)
     48 		h = *s + 17*h;
     49 	return h;
     50 }
     51 
     52 int
     53 main()
     54 {
     55 	char *bmap;
     56 	uint32_t h, M, K;
     57 	int i, j;
     58 
     59 	bmap = malloc(1u << 31);
     60 
     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);
     69 			}
     70 		th[i] = h;
     71 	}
     72 
     73 	for (i=9; 1<<i < Ntok; ++i);
     74 	M = 32 - i;
     75 
     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;
     86 			}
     87 			if (i==Ntok) {
     88 				printf("found K=%d for M=%d\n", K, M);
     89 				exit(0);
     90 			}
     91 			K += 2;
     92 		} while (K != 1);
     93 	}
     94 }