4 #@ Parse 'enum okeys' from nail.h and create okeys.h. And see acmava.c.
7 # Acceptable "longest distance" from hash-modulo-index to key
8 my $MAXDISTANCE_PENALTY = 5;
14 use diagnostics
-verbose
;
18 use sigtrap
qw(handler cleanup normal-signals);
20 my (@ENTS, $CTOOL, $CTOOL_EXE);
37 die "$CTOOL_EXE: couldn't unlink: $^E"
38 if $CTOOL_EXE && -f
$CTOOL_EXE && 1 != unlink $CTOOL_EXE;
39 die "$CTOOL: couldn't unlink: $^E"
40 if $CTOOL && -f
$CTOOL && 1 != unlink $CTOOL;
41 die "Terminating due to signal $_[0]" if $_[0]
45 die "nail.h: open: $^E" unless open F
, '<', 'nail.h';
48 # Only want the enum okeys content
49 if (/^enum okeys/) {$init = 1; next}
50 if (/^};/) {if ($init) {$init = 2; last}; next}
53 # Ignore empty and comment lines
57 # An entry may have a comment with special directives
58 /^\s*(\w+),?\s*(?:\/\
*\s
*(?
:{(.*)})\s
*\
*\
/\s*)?$/;
59 my ($k, $x) = ($1, $2);
62 $vals{binary
} = ($k =~ /^ok_b/ ?
1 : 0);
63 $k = $1 if $k =~ /^ok_[bv]_(.+)$/;
67 while ($x && $x =~ /^([^,]+?)(?:,(.*))?$/) {
70 die "Unsupported special directive: $1"
71 if ($1 ne 'name' && $1 ne 'special');
77 if ($init != 2) {die 'nail.h does not have the expected content'}
82 $CTOOL = './tmp-okey-tool-' . $$ . '.c';
83 $CTOOL_EXE = $CTOOL . '.exe';
85 die "$CTOOL: open: $^E" unless open F
, '>', $CTOOL;
86 # xxx optimize: could read lines and write lines in HASH_MODE..
87 print F
'#define MAX_DISTANCE_PENALTY ', $MAXDISTANCE_PENALTY, "\n";
96 # define NELEM(A) (sizeof(A) / sizeof(A[0]))
99 #define ui32_t uint32_t
100 #define ui16_t uint16_t
101 #define ui8_t uint8_t
111 /* NOTE: copied over from auxlily.c */
113 torek_hash
(char const
*name
)
115 /* Chris Torek
's hash.
116 * NOTE: need to change *at least* create-okey-map.pl when changing the
120 while (*name != '\
0') {
128 /* Include what has been written in HASH_MODE */
131 static ui8_t seen_wraparound;
132 static size_t longest_distance;
135 next_prime(size_t no) /* blush (brute force) */
139 for (size_t i = 3; i < no; i += 2)
148 struct var_map const *vmp = _var_map, *vmaxp = vmp + NELEM(_var_map);
149 size_t ldist = 0, *arr;
151 arr = malloc(sizeof *arr * size);
152 for (size_t i = 0; i < size; ++i)
153 arr[i] = NELEM(_var_map);
156 longest_distance = 0;
158 while (vmp < vmaxp) {
159 ui32_t hash = vmp->vm_hash, i = hash % size, l;
161 for (l = 0; arr[i] != NELEM(_var_map); ++l)
166 if (l > longest_distance)
167 longest_distance = l;
168 arr[i] = (size_t)(vmp++ - _var_map);
172 #endif /* !HASH_MODE */
175 main(int argc, char **argv)
178 size_t h = torek_hash(argv[1]);
180 printf("%lu\n", (unsigned long)h);
183 size_t *arr, size = NELEM(_var_map);
185 fprintf(stderr, "Starting reversy, okeys=%zu\n", size);
187 arr = reversy(size = next_prime(size));
188 fprintf(stderr, " - size=%zu longest_distance=%zu seen_wraparound=%d\n",
189 size, longest_distance, seen_wraparound);
190 if (longest_distance <= MAX_DISTANCE_PENALTY)
196 "#define _VAR_REV_ILL %zuu\n"
197 "#define _VAR_REV_PRIME %zuu\n"
198 "#define _VAR_REV_LONGEST %zuu\n"
199 "#define _VAR_REV_WRAPAROUND %d\n"
200 "static %s const _var_revmap[_VAR_REV_PRIME] = {\n ",
201 NELEM(_var_map), size, longest_distance, seen_wraparound, argv[1]);
202 for (size_t i = 0; i < size; ++i)
203 printf("%s%zuu", (i == 0 ? "" : (i % 10 == 0 ? ",\n " : ", ")), arr[i]);
209 # <<<<<<<<<<<<<<<<<<<
214 system("c99 -DHASH_MODE -I. -o $CTOOL_EXE $CTOOL");
216 foreach my $e (@ENTS) {
217 my $h = `$CTOOL_EXE $e->{name}`;
223 sub dump_keydat_varmap {
224 die "$OUT: open: $^E" unless open F, '>', $OUT;
225 print F "/*@ $OUT, generated by $0 on ", scalar gmtime(), ".\n",
226 " *@ See acmava.c for more */\n\n";
228 print F 'static char const _var_keydat
[] = {', "\n";
229 my ($i, $alen) = (0, 0);
230 foreach my $e (@ENTS) {
231 $e->{keyoff} = $alen;
234 my $a = join '\',\'', split(//, $k);
235 print F " /* $i. [$alen]+$l $k, binary=$e->{binary} */\n",
240 print F '};', "\n\n";
242 print F 'static struct var_map const _var_map
[] = {', "\n";
243 foreach my $e (@ENTS) {
244 print F " /* $e->{enum} */ {$e->{hash}u, $e->{keyoff}u, ",
245 ($e->{binary} ? '1' : '0'), ', ', ($e->{special} ? '1' : '0'), "},\n"
247 print F '};', "\n\n";
249 die "$OUT: close: $^E" unless close F
253 system("c99 -I. -o $CTOOL_EXE $CTOOL");
254 my $t = (@ENTS < 0xFF ? 'ui8_t
' : (@ENTS < 0xFFFF ? 'ui16_t
' : 'ui32_t
'));
255 `$CTOOL_EXE $t >> $OUT`
258 {package main; main_fun()}
260 # vim:set fenc=utf-8:s-it-mode