]>
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 (length($att)) { 
 100             if (!($att =~ m/ReadOnly/)) { 
 101                 $put = "set" . jsc_ucfirst
($val); 
 104             push(@values, { "type" => "Property", "get" => $get, "put" => $put }); 
 106             push(@values, { "type" => "Lexer", "value" => $val });         
 108         push(@hashes, hashValue
($key)); 
 110         die "invalid data {" . $_ . "}"; 
 114 die "missing closing \@end" if ($inside); 
 120     if ($value =~ /js/) { 
 125     return ucfirst($value); 
 129 sub ceilingToPowerOf2
 
 131     my ($pefectHashSize) = @_; 
 134     while ($pefectHashSize > $powerOf2) { 
 141 sub calcPerfectHashSize
() 
 144     for ($pefectHashSize = ceilingToPowerOf2
(scalar @keys); ; $pefectHashSize += $pefectHashSize) { 
 146         foreach my $key (@keys) { 
 147             my $h = hashValue
($key) % $pefectHashSize; 
 148             next tableSizeLoop 
if $table[$h]; 
 156     my ($value, $distance) = @_; 
 157     return (($value << $distance) & 0xFFFFFFFF); 
 160 sub calcCompactHashSize
() 
 162     my $compactHashSize = ceilingToPowerOf2
(2 * @keys); 
 163     $compactHashSizeMask = $compactHashSize - 1; 
 164     $compactSize = $compactHashSize; 
 168     foreach my $key (@keys) { 
 170         my $h = hashValue
($key) % $compactHashSize; 
 171         while (defined($table[$h])) { 
 172             if (defined($links[$h])) { 
 177                 $links[$h] = $compactSize; 
 184         $maxdepth = $depth if ( $depth > $maxdepth); 
 188 # Paul Hsieh's SuperFastHash 
 189 # http://www.azillionmonkeys.com/qed/hash.html 
 191   my @chars = split(/ */, $_[0]); 
 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 
 197   my $EXP2_32 = 4294967296; 
 199   my $hash = 0x9e3779b9; 
 200   my $l    = scalar @chars; #I wish this was in Ruby --- Maks 
 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; 
 212     $hash += $hash >> 11; 
 218     $hash += ord($chars[$s]); 
 219     $hash ^= (leftShift
($hash, 11)% $EXP2_32); 
 220     $hash += $hash >> 17; 
 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); 
 232   # Save 8 bits for StringImpl to use as flags. 
 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); 
 247         print "// Automatically generated from $file using $0. DO NOT EDIT!\n"; 
 250     my $nameEntries = "${name}Values"; 
 251     $nameEntries =~ s/:/_/g; 
 252     my $nameIndex = "${name}Index"; 
 253     $nameIndex =~ s/:/_/g; 
 255     print "\n#include \"JSCBuiltins.h\"\n"; 
 256     print "\n#include \"Lookup.h\"\n" if ($includelookup); 
 259         print "\nnamespace ${useNameSpace} {\n"; 
 260         print "\nusing namespace JSC;\n"; 
 262         print "\nnamespace JSC {\n"; 
 265     print "\nstatic const struct CompactHashIndex ${nameIndex}\[$compactSize\] = {\n"; 
 266     for (my $i = 0; $i < $compactSize; $i++) { 
 268         if (defined($table[$i])) { $T = $table[$i]; } 
 270         if (defined($links[$i])) { $L = $links[$i]; } 
 271         print "    { $T, $L },\n"; 
 275     my $packedSize = scalar @keys; 
 276     print "\nstatic const struct HashTableValue ${nameEntries}\[$packedSize\] = {\n"; 
 278     foreach my $key (@keys) { 
 280         my $secondValue = ""; 
 281         my $firstCastStr = ""; 
 282         my $secondCastStr = ""; 
 284         if ($values[$i]{"type"} eq "Function") { 
 285             $firstCastStr = "static_cast<NativeFunction>"; 
 286             $firstValue = $values[$i]{"function"}; 
 287             $secondValue = $values[$i]{"params"}; 
 288         } elsif ($values[$i]{"type"} eq "Property") { 
 289             $firstCastStr = "static_cast<PropertySlot::GetValueFunc>"; 
 290             $secondCastStr = "static_cast<PutPropertySlot::PutValueFunc>"; 
 291             $firstValue = $values[$i]{"get"}; 
 292             $secondValue = $values[$i]{"put"}; 
 293         } elsif ($values[$i]{"type"} eq "Lexer") { 
 294             $firstValue = $values[$i]{"value"}; 
 298         my $intrinsic = "NoIntrinsic"; 
 299         $intrinsic = "FromCharCodeIntrinsic" if ($key eq "fromCharCode"); 
 300         if ($name eq "arrayPrototypeTable") { 
 301             $intrinsic = "ArrayPushIntrinsic" if ($key eq "push"); 
 302             $intrinsic = "ArrayPopIntrinsic" if ($key eq "pop"); 
 304         if ($name eq "regExpPrototypeTable") { 
 305             $intrinsic = "RegExpExecIntrinsic" if ($key eq "exec"); 
 306             $intrinsic = "RegExpTestIntrinsic" if ($key eq "test"); 
 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"; 
 316         print "   { \"$key\", $attrs[$i], $intrinsic, (intptr_t)" . $firstCastStr . "($firstValue), (intptr_t)" . $secondCastStr . "($secondValue) },\n"; 
 317         if ($values[$i]{"type"} eq "Function")  { 
 323     print "extern const struct HashTable $name =\n"; 
 324     print "    \{ $packedSize, $compactHashSizeMask, $hasSetter, $nameEntries, 0, $nameIndex \};\n"; 
 325     print "} // namespace\n";