summaryrefslogtreecommitdiff
path: root/src/buildvm_fold.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/buildvm_fold.c')
-rw-r--r--src/buildvm_fold.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/buildvm_fold.c b/src/buildvm_fold.c
new file mode 100644
index 00000000..5f065643
--- /dev/null
+++ b/src/buildvm_fold.c
@@ -0,0 +1,206 @@
1/*
2** LuaJIT VM builder: IR folding hash table generator.
3** Copyright (C) 2005-2009 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#include "lj_obj.h"
7#include "lj_ir.h"
8
9#include "buildvm.h"
10
11/* Context for the folding hash table generator. */
12static int lineno;
13static int funcidx;
14static uint32_t foldkeys[BUILD_MAX_FOLD];
15static uint32_t nkeys;
16
17/* Try to fill the hash table with keys using the hash parameters. */
18static int tryhash(uint32_t *htab, uint32_t sz, uint32_t r, int dorol)
19{
20 uint32_t i;
21 if (dorol && ((r & 31) == 0 || (r>>5) == 0))
22 return 0; /* Avoid zero rotates. */
23 memset(htab, 0xff, (sz+1)*sizeof(uint32_t));
24 for (i = 0; i < nkeys; i++) {
25 uint32_t key = foldkeys[i];
26 uint32_t k = key & 0xffffff;
27 uint32_t h = (dorol ? lj_rol(lj_rol(k, r>>5) - k, r&31) :
28 (((k << (r>>5)) - k) << (r&31))) % sz;
29 if (htab[h] != 0xffffffff) { /* Collision on primary slot. */
30 if (htab[h+1] != 0xffffffff) { /* Collision on secondary slot. */
31 /* Try to move the colliding key, if possible. */
32 if (h < sz-1 && htab[h+2] == 0xffffffff) {
33 uint32_t k2 = htab[h+1] & 0xffffff;
34 uint32_t h2 = (dorol ? lj_rol(lj_rol(k2, r>>5) - k2, r&31) :
35 (((k2 << (r>>5)) - k2) << (r&31))) % sz;
36 if (h2 != h+1) return 0; /* Cannot resolve collision. */
37 htab[h+2] = htab[h+1]; /* Move colliding key to secondary slot. */
38 } else {
39 return 0; /* Collision. */
40 }
41 }
42 htab[h+1] = key;
43 } else {
44 htab[h] = key;
45 }
46 }
47 return 1; /* Success, all keys could be stored. */
48}
49
50/* Print the generated hash table. */
51static void printhash(BuildCtx *ctx, uint32_t *htab, uint32_t sz)
52{
53 uint32_t i;
54 fprintf(ctx->fp, "static const uint32_t fold_hash[%d] = {\n0x%08x",
55 sz+1, htab[0]);
56 for (i = 1; i < sz+1; i++)
57 fprintf(ctx->fp, ",\n0x%08x", htab[i]);
58 fprintf(ctx->fp, "\n};\n\n");
59}
60
61/* Exhaustive search for the shortest semi-perfect hash table. */
62static void makehash(BuildCtx *ctx)
63{
64 uint32_t htab[BUILD_MAX_FOLD*2+1];
65 uint32_t sz, r;
66 /* Search for the smallest hash table with an odd size. */
67 for (sz = (nkeys|1); sz < BUILD_MAX_FOLD*2; sz += 2) {
68 /* First try all shift hash combinations. */
69 for (r = 0; r < 32*32; r++) {
70 if (tryhash(htab, sz, r, 0)) {
71 printhash(ctx, htab, sz);
72 fprintf(ctx->fp,
73 "#define fold_hashkey(k)\t(((((k)<<%u)-(k))<<%u)%%%u)\n\n",
74 r>>5, r&31, sz);
75 return;
76 }
77 }
78 /* Then try all rotate hash combinations. */
79 for (r = 0; r < 32*32; r++) {
80 if (tryhash(htab, sz, r, 1)) {
81 printhash(ctx, htab, sz);
82 fprintf(ctx->fp,
83 "#define fold_hashkey(k)\t(lj_rol(lj_rol((k),%u)-(k),%u)%%%u)\n\n",
84 r>>5, r&31, sz);
85 return;
86 }
87 }
88 }
89 fprintf(stderr, "Error: search for perfect hash failed\n");
90 exit(1);
91}
92
93/* Parse one token of a fold rule. */
94static uint32_t nexttoken(char **pp, int allowlit, int allowany)
95{
96 char *p = *pp;
97 if (p) {
98 uint32_t i;
99 char *q = strchr(p, ' ');
100 if (q) *q++ = '\0';
101 *pp = q;
102 if (allowlit && !strncmp(p, "IRFPM_", 6)) {
103 for (i = 0; irfpm_names[i]; i++)
104 if (!strcmp(irfpm_names[i], p+6))
105 return i;
106 } else if (allowlit && !strncmp(p, "IRFL_", 5)) {
107 for (i = 0; irfield_names[i]; i++)
108 if (!strcmp(irfield_names[i], p+5))
109 return i;
110 } else if (allowany && !strcmp("any", p)) {
111 return 0xff;
112 } else {
113 for (i = 0; ir_names[i]; i++)
114 if (!strcmp(ir_names[i], p))
115 return i;
116 }
117 fprintf(stderr, "Error: bad fold definition token \"%s\" at line %d\n", p, lineno);
118 exit(1);
119 }
120 return 0;
121}
122
123/* Parse a fold rule. */
124static void foldrule(char *p)
125{
126 uint32_t op = nexttoken(&p, 0, 0);
127 uint32_t left = nexttoken(&p, 0, 1);
128 uint32_t right = nexttoken(&p, 1, 1);
129 uint32_t key = (funcidx << 24) | (op << 16) | (left << 8) | right;
130 uint32_t i;
131 if (nkeys >= BUILD_MAX_FOLD) {
132 fprintf(stderr, "Error: too many fold rules, increase BUILD_MAX_FOLD.\n");
133 exit(1);
134 }
135 /* Simple insertion sort to detect duplicates. */
136 for (i = nkeys; i > 0; i--) {
137 if ((foldkeys[i-1]&0xffffff) < (key & 0xffffff))
138 break;
139 if ((foldkeys[i-1]&0xffffff) == (key & 0xffffff)) {
140 fprintf(stderr, "Error: duplicate fold definition at line %d\n", lineno);
141 exit(1);
142 }
143 foldkeys[i] = foldkeys[i-1];
144 }
145 foldkeys[i] = key;
146 nkeys++;
147}
148
149/* Emit C source code for IR folding hash table. */
150void emit_fold(BuildCtx *ctx)
151{
152 char buf[256]; /* We don't care about analyzing lines longer than that. */
153 const char *fname = ctx->args[0];
154 FILE *fp;
155
156 if (fname == NULL) {
157 fprintf(stderr, "Error: missing input filename\n");
158 exit(1);
159 }
160
161 if (fname[0] == '-' && fname[1] == '\0') {
162 fp = stdin;
163 } else {
164 fp = fopen(fname, "r");
165 if (!fp) {
166 fprintf(stderr, "Error: cannot open input file '%s': %s\n",
167 fname, strerror(errno));
168 exit(1);
169 }
170 }
171
172 fprintf(ctx->fp, "/* This is a generated file. DO NOT EDIT! */\n\n");
173 fprintf(ctx->fp, "static const FoldFunc fold_func[] = {\n");
174
175 lineno = 0;
176 funcidx = 0;
177 nkeys = 0;
178 while (fgets(buf, sizeof(buf), fp) != NULL) {
179 lineno++;
180 /* The prefix must be at the start of a line, otherwise it's ignored. */
181 if (!strncmp(buf, FOLDDEF_PREFIX, sizeof(FOLDDEF_PREFIX)-1)) {
182 char *p = buf+sizeof(FOLDDEF_PREFIX)-1;
183 char *q = strchr(p, ')');
184 if (p[0] == '(' && q) {
185 p++;
186 *q = '\0';
187 foldrule(p);
188 } else if ((p[0] == 'F' || p[0] == 'X') && p[1] == '(' && q) {
189 p += 2;
190 *q = '\0';
191 fprintf(ctx->fp, funcidx ? ",\n %s" : " %s", p);
192 funcidx++;
193 } else {
194 buf[strlen(buf)-1] = '\0';
195 fprintf(stderr, "Error: unknown fold definition tag %s%s at line %d\n",
196 FOLDDEF_PREFIX, p, lineno);
197 exit(1);
198 }
199 }
200 }
201 fclose(fp);
202 fprintf(ctx->fp, "\n};\n\n");
203
204 makehash(ctx);
205}
206