]> git.saurik.com Git - apple/javascriptcore.git/blame - create_hash_table
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / create_hash_table
CommitLineData
9dae56ea
A
1#! /usr/bin/perl -w
2#
3# Static Hashtable Generator
4#
5# (c) 2000-2002 by Harri Porten <porten@kde.org> and
6# David Faure <faure@kde.org>
7# Modified (c) 2004 by Nikolas Zimmermann <wildfox@kde.org>
8# Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
9#
10# This library is free software; you can redistribute it and/or
11# modify it under the terms of the GNU Lesser General Public
12# License as published by the Free Software Foundation; either
13# version 2 of the License, or (at your option) any later version.
14#
15# This library is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Lesser General Public License for more details.
19#
20# You should have received a copy of the GNU Lesser General Public
21# License along with this library; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23#
24
25use strict;
26
27my $file = $ARGV[0];
28shift;
29my $includelookup = 0;
30
31# Use -i as second argument to make it include "Lookup.h"
32$includelookup = 1 if (defined($ARGV[0]) && $ARGV[0] eq "-i");
33
34# Use -n as second argument to make it use the third argument as namespace parameter ie. -n KDOM
35my $useNameSpace = $ARGV[1] if (defined($ARGV[0]) && $ARGV[0] eq "-n");
36
37print STDERR "Creating hashtable for $file\n";
38open(IN, $file) or die "No such file $file";
39
40my @keys = ();
41my @attrs = ();
42my @values = ();
43my @hashes = ();
81345200
A
44my @table = ();
45my @links = ();
46
47my $hasSetter = "false";
9dae56ea
A
48
49my $inside = 0;
50my $name;
51my $pefectHashSize;
52my $compactSize;
53my $compactHashSizeMask;
54my $banner = 0;
55sub calcPerfectHashSize();
56sub calcCompactHashSize();
57sub output();
58sub jsc_ucfirst($);
59sub hashValue($);
60
61while (<IN>) {
62 chomp;
63 s/^\s+//;
64 next if /^\#|^$/; # Comment or blank line. Do nothing.
65 if (/^\@begin/ && !$inside) {
66 if (/^\@begin\s*([:_\w]+)\s*\d*\s*$/) {
67 $inside = 1;
68 $name = $1;
69 } else {
70 print STDERR "WARNING: \@begin without table name, skipping $_\n";
71 }
72 } elsif (/^\@end\s*$/ && $inside) {
73 calcPerfectHashSize();
74 calcCompactHashSize();
75 output();
76
77 @keys = ();
78 @attrs = ();
79 @values = ();
80 @hashes = ();
81345200
A
81 @table = ();
82 @links = ();
9dae56ea
A
83
84 $inside = 0;
85 } elsif (/^(\S+)\s*(\S+)\s*([\w\|]*)\s*(\w*)\s*$/ && $inside) {
86 my $key = $1;
87 my $val = $2;
88 my $att = $3;
89 my $param = $4;
90
91 push(@keys, $key);
92 push(@attrs, length($att) > 0 ? $att : "0");
93
94 if ($att =~ m/Function/) {
95 push(@values, { "type" => "Function", "function" => $val, "params" => (length($param) ? $param : "") });
96 #printf STDERR "WARNING: Number of arguments missing for $key/$val\n" if (length($param) == 0);
97 } elsif (length($att)) {
98 my $get = $val;
81345200
A
99 my $put = "0";
100 if (!($att =~ m/ReadOnly/)) {
101 $put = "set" . jsc_ucfirst($val);
102 }
103 $hasSetter = "true";
9dae56ea
A
104 push(@values, { "type" => "Property", "get" => $get, "put" => $put });
105 } else {
106 push(@values, { "type" => "Lexer", "value" => $val });
107 }
108 push(@hashes, hashValue($key));
109 } elsif ($inside) {
110 die "invalid data {" . $_ . "}";
111 }
112}
113
114die "missing closing \@end" if ($inside);
115
116sub jsc_ucfirst($)
117{
118 my ($value) = @_;
119
120 if ($value =~ /js/) {
121 $value =~ s/js/JS/;
122 return $value;
123 }
124
125 return ucfirst($value);
126}
127
128
129sub ceilingToPowerOf2
130{
131 my ($pefectHashSize) = @_;
132
133 my $powerOf2 = 1;
134 while ($pefectHashSize > $powerOf2) {
135 $powerOf2 <<= 1;
136 }
137
138 return $powerOf2;
139}
140
141sub calcPerfectHashSize()
142{
143tableSizeLoop:
144 for ($pefectHashSize = ceilingToPowerOf2(scalar @keys); ; $pefectHashSize += $pefectHashSize) {
145 my @table = ();
146 foreach my $key (@keys) {
147 my $h = hashValue($key) % $pefectHashSize;
148 next tableSizeLoop if $table[$h];
149 $table[$h] = 1;
150 }
151 last;
152 }
153}
154
155sub leftShift($$) {
156 my ($value, $distance) = @_;
157 return (($value << $distance) & 0xFFFFFFFF);
158}
159
160sub calcCompactHashSize()
161{
9dae56ea
A
162 my $compactHashSize = ceilingToPowerOf2(2 * @keys);
163 $compactHashSizeMask = $compactHashSize - 1;
164 $compactSize = $compactHashSize;
165 my $collisions = 0;
166 my $maxdepth = 0;
167 my $i = 0;
168 foreach my $key (@keys) {
169 my $depth = 0;
170 my $h = hashValue($key) % $compactHashSize;
171 while (defined($table[$h])) {
172 if (defined($links[$h])) {
173 $h = $links[$h];
174 $depth++;
175 } else {
176 $collisions++;
177 $links[$h] = $compactSize;
178 $h = $compactSize;
179 $compactSize++;
180 }
181 }
182 $table[$h] = $i;
183 $i++;
184 $maxdepth = $depth if ( $depth > $maxdepth);
185 }
186}
187
188# Paul Hsieh's SuperFastHash
189# http://www.azillionmonkeys.com/qed/hash.html
9dae56ea
A
190sub hashValue($) {
191 my @chars = split(/ */, $_[0]);
192
193 # This hash is designed to work on 16-bit chunks at a time. But since the normal case
194 # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
195 # were 16-bit chunks, which should give matching results
196
197 my $EXP2_32 = 4294967296;
198
199 my $hash = 0x9e3779b9;
200 my $l = scalar @chars; #I wish this was in Ruby --- Maks
201 my $rem = $l & 1;
202 $l = $l >> 1;
203
204 my $s = 0;
205
206 # Main loop
207 for (; $l > 0; $l--) {
208 $hash += ord($chars[$s]);
209 my $tmp = leftShift(ord($chars[$s+1]), 11) ^ $hash;
210 $hash = (leftShift($hash, 16)% $EXP2_32) ^ $tmp;
211 $s += 2;
212 $hash += $hash >> 11;
213 $hash %= $EXP2_32;
214 }
215
216 # Handle end case
6fe7ccc8 217 if ($rem != 0) {
9dae56ea
A
218 $hash += ord($chars[$s]);
219 $hash ^= (leftShift($hash, 11)% $EXP2_32);
220 $hash += $hash >> 17;
221 }
222
223 # Force "avalanching" of final 127 bits
224 $hash ^= leftShift($hash, 3);
225 $hash += ($hash >> 5);
226 $hash = ($hash% $EXP2_32);
227 $hash ^= (leftShift($hash, 2)% $EXP2_32);
228 $hash += ($hash >> 15);
229 $hash = $hash% $EXP2_32;
230 $hash ^= (leftShift($hash, 10)% $EXP2_32);
6fe7ccc8
A
231
232 # Save 8 bits for StringImpl to use as flags.
233 $hash &= 0xffffff;
234
235 # This avoids ever returning a hash code of 0, since that is used to
236 # signal "hash not computed yet". Setting the high bit maintains
237 # reasonable fidelity to a hash code of 0 because it is likely to yield
238 # exactly 0 when hash lookup masks out the high bits.
239 $hash = (0x80000000 >> 8) if ($hash == 0);
9dae56ea
A
240
241 return $hash;
242}
243
244sub output() {
245 if (!$banner) {
246 $banner = 1;
247 print "// Automatically generated from $file using $0. DO NOT EDIT!\n";
248 }
249
250 my $nameEntries = "${name}Values";
251 $nameEntries =~ s/:/_/g;
81345200
A
252 my $nameIndex = "${name}Index";
253 $nameIndex =~ s/:/_/g;
9dae56ea 254
81345200 255 print "\n#include \"JSCBuiltins.h\"\n";
9dae56ea 256 print "\n#include \"Lookup.h\"\n" if ($includelookup);
81345200 257
9dae56ea
A
258 if ($useNameSpace) {
259 print "\nnamespace ${useNameSpace} {\n";
260 print "\nusing namespace JSC;\n";
261 } else {
262 print "\nnamespace JSC {\n";
263 }
81345200
A
264
265 print "\nstatic const struct CompactHashIndex ${nameIndex}\[$compactSize\] = {\n";
266 for (my $i = 0; $i < $compactSize; $i++) {
267 my $T = -1;
268 if (defined($table[$i])) { $T = $table[$i]; }
269 my $L = -1;
270 if (defined($links[$i])) { $L = $links[$i]; }
271 print " { $T, $L },\n";
272 }
273 print "};\n\n";
274
275 my $packedSize = scalar @keys;
276 print "\nstatic const struct HashTableValue ${nameEntries}\[$packedSize\] = {\n";
9dae56ea
A
277 my $i = 0;
278 foreach my $key (@keys) {
279 my $firstValue = "";
280 my $secondValue = "";
81345200
A
281 my $firstCastStr = "";
282 my $secondCastStr = "";
9dae56ea
A
283
284 if ($values[$i]{"type"} eq "Function") {
81345200 285 $firstCastStr = "static_cast<NativeFunction>";
9dae56ea
A
286 $firstValue = $values[$i]{"function"};
287 $secondValue = $values[$i]{"params"};
288 } elsif ($values[$i]{"type"} eq "Property") {
81345200
A
289 $firstCastStr = "static_cast<PropertySlot::GetValueFunc>";
290 $secondCastStr = "static_cast<PutPropertySlot::PutValueFunc>";
9dae56ea
A
291 $firstValue = $values[$i]{"get"};
292 $secondValue = $values[$i]{"put"};
293 } elsif ($values[$i]{"type"} eq "Lexer") {
294 $firstValue = $values[$i]{"value"};
295 $secondValue = "0";
296 }
6fe7ccc8
A
297
298 my $intrinsic = "NoIntrinsic";
6fe7ccc8 299 $intrinsic = "FromCharCodeIntrinsic" if ($key eq "fromCharCode");
6fe7ccc8
A
300 if ($name eq "arrayPrototypeTable") {
301 $intrinsic = "ArrayPushIntrinsic" if ($key eq "push");
302 $intrinsic = "ArrayPopIntrinsic" if ($key eq "pop");
4e4e5a6f 303 }
6fe7ccc8
A
304 if ($name eq "regExpPrototypeTable") {
305 $intrinsic = "RegExpExecIntrinsic" if ($key eq "exec");
306 $intrinsic = "RegExpTestIntrinsic" if ($key eq "test");
14957cd0 307 }
6fe7ccc8 308
81345200
A
309 if ($values[$i]{"type"} eq "Function") {
310 my $tableHead = $name;
311 $tableHead =~ s/Table$//;
312 print " #if JSC_BUILTIN_EXISTS(" . uc($tableHead . $key) .")\n";
313 print " { \"$key\", (($attrs[$i]) & ~Function) | Builtin, $intrinsic, (intptr_t)static_cast<BuiltinGenerator>(" . $tableHead . ucfirst($key) . "CodeGenerator), (intptr_t)$secondValue },\n";
314 print " #else\n"
315 }
316 print " { \"$key\", $attrs[$i], $intrinsic, (intptr_t)" . $firstCastStr . "($firstValue), (intptr_t)" . $secondCastStr . "($secondValue) },\n";
317 if ($values[$i]{"type"} eq "Function") {
318 print " #endif\n"
319 }
9dae56ea
A
320 $i++;
321 }
9dae56ea 322 print "};\n\n";
6fe7ccc8 323 print "extern const struct HashTable $name =\n";
81345200 324 print " \{ $packedSize, $compactHashSizeMask, $hasSetter, $nameEntries, 0, $nameIndex \};\n";
9dae56ea
A
325 print "} // namespace\n";
326}