]>
git.saurik.com Git - apple/javascriptcore.git/blob - create_hash_table
3 # Static Hashtable Generator
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.
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.
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.
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
29 my $includelookup = 0;
31 # Use -i as second argument to make it include "Lookup.h"
32 $includelookup = 1 if (defined($ARGV[0]) && $ARGV[0] eq "-i");
34 # Use -n as second argument to make it use the third argument as namespace parameter ie. -n KDOM
35 my $useNameSpace = $ARGV[1] if (defined($ARGV[0]) && $ARGV[0] eq "-n");
37 print STDERR
"Creating hashtable for $file\n";
38 open(IN
, $file) or die "No such file $file";
47 my $hasSetter = "false";
53 my $compactHashSizeMask;
55 sub calcPerfectHashSize
();
56 sub calcCompactHashSize
();
64 next if /^\#|^$/; # Comment or blank line. Do nothing.
65 if (/^\@begin/ && !$inside) {
66 if (/^\@begin\s*([:_\w]+)\s*\d*\s*$/) {
70 print STDERR
"WARNING: \@begin without table name, skipping $_\n";
72 } elsif (/^\@end\s*$/ && $inside) {
73 calcPerfectHashSize
();
74 calcCompactHashSize
();
85 } elsif (/^(\S+)\s*(\S+)\s*([\w\|]*)\s*(\w*)\s*$/ && $inside) {
92 push(@attrs, length($att) > 0 ? $att : "0");
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 ($att =~ m/Accessor/) {
101 push(@values, { "type" => "Accessor", "get" => $get, "put" => $put });
102 } elsif (length($att)) {
105 if (!($att =~ m/ReadOnly/)) {
106 $put = "set" . jsc_ucfirst
($val);
109 push(@values, { "type" => "Property", "get" => $get, "put" => $put });
111 push(@values, { "type" => "Lexer", "value" => $val });
113 push(@hashes, hashValue
($key));
115 die "invalid data {" . $_ . "}";
119 die "missing closing \@end" if ($inside);
125 if ($value =~ /js/) {
130 return ucfirst($value);
134 sub ceilingToPowerOf2
136 my ($pefectHashSize) = @_;
139 while ($pefectHashSize > $powerOf2) {
146 sub calcPerfectHashSize
()
149 for ($pefectHashSize = ceilingToPowerOf2
(scalar @keys); ; $pefectHashSize += $pefectHashSize) {
151 foreach my $key (@keys) {
152 my $h = hashValue
($key) % $pefectHashSize;
153 next tableSizeLoop
if $table[$h];
161 my ($value, $distance) = @_;
162 return (($value << $distance) & 0xFFFFFFFF);
165 sub calcCompactHashSize
()
167 my $compactHashSize = ceilingToPowerOf2
(2 * @keys);
168 $compactHashSizeMask = $compactHashSize - 1;
169 $compactSize = $compactHashSize;
173 foreach my $key (@keys) {
175 my $h = hashValue
($key) % $compactHashSize;
176 while (defined($table[$h])) {
177 if (defined($links[$h])) {
182 $links[$h] = $compactSize;
189 $maxdepth = $depth if ( $depth > $maxdepth);
193 # Paul Hsieh's SuperFastHash
194 # http://www.azillionmonkeys.com/qed/hash.html
196 my @chars = split(/ */, $_[0]);
198 # This hash is designed to work on 16-bit chunks at a time. But since the normal case
199 # (above) is to hash UTF-16 characters, we just treat the 8-bit chars as if they
200 # were 16-bit chunks, which should give matching results
202 my $EXP2_32 = 4294967296;
204 my $hash = 0x9e3779b9;
205 my $l = scalar @chars; #I wish this was in Ruby --- Maks
212 for (; $l > 0; $l--) {
213 $hash += ord($chars[$s]);
214 my $tmp = leftShift
(ord($chars[$s+1]), 11) ^ $hash;
215 $hash = (leftShift
($hash, 16)% $EXP2_32) ^ $tmp;
217 $hash += $hash >> 11;
223 $hash += ord($chars[$s]);
224 $hash ^= (leftShift
($hash, 11)% $EXP2_32);
225 $hash += $hash >> 17;
228 # Force "avalanching" of final 127 bits
229 $hash ^= leftShift
($hash, 3);
230 $hash += ($hash >> 5);
231 $hash = ($hash% $EXP2_32);
232 $hash ^= (leftShift
($hash, 2)% $EXP2_32);
233 $hash += ($hash >> 15);
234 $hash = $hash% $EXP2_32;
235 $hash ^= (leftShift
($hash, 10)% $EXP2_32);
237 # Save 8 bits for StringImpl to use as flags.
240 # This avoids ever returning a hash code of 0, since that is used to
241 # signal "hash not computed yet". Setting the high bit maintains
242 # reasonable fidelity to a hash code of 0 because it is likely to yield
243 # exactly 0 when hash lookup masks out the high bits.
244 $hash = (0x80000000 >> 8) if ($hash == 0);
252 print "// Automatically generated from $file using $0. DO NOT EDIT!\n";
255 my $nameEntries = "${name}Values";
256 $nameEntries =~ s/:/_/g;
257 my $nameIndex = "${name}Index";
258 $nameIndex =~ s/:/_/g;
260 print "\n#include \"JSCBuiltins.h\"\n";
261 print "\n#include \"Lookup.h\"\n" if ($includelookup);
264 print "\nnamespace ${useNameSpace} {\n";
265 print "\nusing namespace JSC;\n";
267 print "\nnamespace JSC {\n";
270 print "\nstatic const struct CompactHashIndex ${nameIndex}\[$compactSize\] = {\n";
271 for (my $i = 0; $i < $compactSize; $i++) {
273 if (defined($table[$i])) { $T = $table[$i]; }
275 if (defined($links[$i])) { $L = $links[$i]; }
276 print " { $T, $L },\n";
280 my $packedSize = scalar @keys;
281 print "\nstatic const struct HashTableValue ${nameEntries}\[$packedSize\] = {\n";
283 foreach my $key (@keys) {
285 my $secondValue = "";
286 my $firstCastStr = "";
287 my $secondCastStr = "";
289 if ($values[$i]{"type"} eq "Function") {
290 $firstCastStr = "static_cast<NativeFunction>";
291 $firstValue = $values[$i]{"function"};
292 $secondValue = $values[$i]{"params"};
293 } elsif ($values[$i]{"type"} eq "Accessor") {
294 $firstCastStr = "static_cast<NativeFunction>";
295 $secondCastStr = "static_cast<NativeFunction>";
296 $firstValue = $values[$i]{"get"};
297 $secondValue = $values[$i]{"put"};
298 } elsif ($values[$i]{"type"} eq "Property") {
299 $firstCastStr = "static_cast<PropertySlot::GetValueFunc>";
300 $secondCastStr = "static_cast<PutPropertySlot::PutValueFunc>";
301 $firstValue = $values[$i]{"get"};
302 $secondValue = $values[$i]{"put"};
303 } elsif ($values[$i]{"type"} eq "Lexer") {
304 $firstValue = $values[$i]{"value"};
308 my $intrinsic = "NoIntrinsic";
309 $intrinsic = "FromCharCodeIntrinsic" if ($key eq "fromCharCode");
310 if ($name eq "arrayPrototypeTable") {
311 $intrinsic = "ArrayPushIntrinsic" if ($key eq "push");
312 $intrinsic = "ArrayPopIntrinsic" if ($key eq "pop");
314 if ($name eq "regExpPrototypeTable") {
315 $intrinsic = "RegExpExecIntrinsic" if ($key eq "exec");
316 $intrinsic = "RegExpTestIntrinsic" if ($key eq "test");
319 if ($values[$i]{"type"} eq "Function") {
320 my $tableHead = $name;
321 $tableHead =~ s/Table$//;
322 print " #if JSC_BUILTIN_EXISTS(" . uc($tableHead . $key) .")\n";
323 print " { \"$key\", (($attrs[$i]) & ~Function) | Builtin, $intrinsic, (intptr_t)static_cast<BuiltinGenerator>(" . $tableHead . ucfirst($key) . "CodeGenerator), (intptr_t)$secondValue },\n";
326 print " { \"$key\", $attrs[$i], $intrinsic, (intptr_t)" . $firstCastStr . "($firstValue), (intptr_t)" . $secondCastStr . "($secondValue) },\n";
327 if ($values[$i]{"type"} eq "Function") {
333 print "JS_EXPORT_PRIVATE extern const struct HashTable $name =\n";
334 print " \{ $packedSize, $compactHashSizeMask, $hasSetter, $nameEntries, 0, $nameIndex \};\n";
335 print "} // namespace\n";