gen-table.pl 6.64 KB
Newer Older
1 2
#! /usr/bin/perl

3
#    Copyright (C) 2000, 2001, 2003 Free Software Foundation
4 5 6 7 8 9 10 11 12 13 14 15 16

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2, or (at your option)
#    any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
Kelley Cook committed
17 18
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
#    02110-1301, USA.
19 20 21

# gen-table.pl - Generate tables for gcj from Unicode data.
# Usage: perl gen-table.pl DATA-FILE
22
#
23 24 25 26 27 28 29
# You can find the Unicode data file here:
#   ftp://www.unicode.org/Public/3.0-Update1/UnicodeData-3.0.1.txt
# Please update this URL when this program is used with a more
# recent version of the table.  Note that this table cannot be
# distributed with gcc.
# This program should not be re-run indiscriminately.  Care must be
# taken that what it generates is in sync with the Java specification.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

# Names of fields in Unicode data table.
$CODE = 0;
$NAME = 1;
$CATEGORY = 2;
$COMBINING_CLASSES = 3;
$BIDI_CATEGORY = 4;
$DECOMPOSITION = 5;
$DECIMAL_VALUE = 6;
$DIGIT_VALUE = 7;
$NUMERIC_VALUE = 8;
$MIRRORED = 9;
$OLD_NAME = 10;
$COMMENT = 11;
$UPPER = 12;
$LOWER = 13;
$TITLE = 14;

# Start of special-cased gaps in Unicode data table.
%gaps = (
	 0x4e00 => "CJK",
	 0xac00 => "Hangul",
	 0xd800 => "Unassigned High Surrogate",
	 0xdb80 => "Private Use High Surrogate",
	 0xdc00 => "Low Surrogate",
	 0xe000 => "Private Use"
	 );

# This lists control characters which are also considered whitespace.
# This is a somewhat odd list, taken from the JCL definition of
# Character.isIdentifierIgnorable.
%whitespace_controls =
    (
     0x0009 => 1,
     0x000a => 1,
     0x000b => 1,
     0x000c => 1,
     0x000d => 1,
     0x001c => 1,
     0x001d => 1,
     0x001e => 1,
     0x001f => 1
     );

open (INPUT, "< $ARGV[0]") || exit 1;

$last_code = -1;
while (<INPUT>)
{
    chop;
    @fields = split (';', $_, 30);
    if ($#fields != 14)
    {
	print STDERR "Entry for $fields[$CODE] has wrong number of fields\n";
    }

    $code = hex ($fields[$CODE]);
87
    last if $code > 0xffff;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    if ($code > $last_code + 1)
    {
	# Found a gap.
	if (defined $gaps{$code})
	{
	    # Fill the gap with the last character read.
	    @gfields = @fields;
	}
	else
	{
	    # The gap represents undefined characters.  Only the type
	    # matters.
	    @gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
			'', '', '', '');
	}
	for (++$last_code; $last_code < $code; ++$last_code)
	{
	    $gfields{$CODE} = sprintf ("%04x", $last_code);
	    &process_one ($last_code, @gfields);
	}
    }
    &process_one ($code, @fields);
    $last_code = $code;
}

close (INPUT);

@gfields = ('', '', 'Cn', '0', '', '', '', '', '', '', '',
	    '', '', '', '');
for (++$last_code; $last_code < 0x10000; ++$last_code)
{
    $gfields{$CODE} = sprintf ("%04x", $last_code);
    &process_one ($last_code, @gfields);
}
--$last_code;			# Want last to be 0xFFFF.

&print_tables ($last_code);

exit 0;

# Process a single character.
sub process_one
{
    my ($code, @fields) = @_;

133
    my @value = ();
134 135 136 137 138 139 140
    my $type = $fields[$CATEGORY];

    # See if the character is a valid identifier start.
    if ($type =~ /L./		# Letter
	|| $type eq 'Pc'	# Connecting punctuation
	|| $type eq 'Sc')	# Currency symbol
    {
141
	push (@value, 'LETTER_START');
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    }

    # See if the character is a valid identifier member.
    if ($type =~ /L./		# Letter
	|| $type eq 'Pc'	# Connecting punctuation
	|| $type eq 'Sc'	# Currency symbol
	|| $type =~ /N[dl]/	# Number: decimal or letter
	|| $type =~ /M[nc]/	# Mark: non-spacing or combining
	|| ($type eq 'Cc'	# Certain controls
	    && ! defined $whitespace_controls{$code})
	|| ($code >= 0x200c	# Join controls
	    && $code <= 0x200f)
	|| ($code >= 0x202a	# Bidi controls -- note that there
				# is a typo in the JCL where these are
				# concerned.
	    && $code <= 0x202e)
	|| ($code >= 0x206a	# Format controls
	    && $code <= 0x206f)
	|| $code == 0xfeff)	# ZWNBSP
    {
162 163 164 165 166 167 168 169 170 171 172 173 174 175
	push (@value, 'LETTER_PART');
    }

    if (($type =~ /Z./
	 # Java treats some values specially as non-spaces.
	 && $code != 0x00a0
	 && $code != 0x2007
	 && $code != 0x202f)
	# And for our purposes there are some that should be specially
	# treated as spaces.
	|| $code == 0x000b
	|| ($code >= 0x001c && $code <= 0x001f))
    {
	push (@value, 'LETTER_SPACE');
176 177
    }

178
    if (! @value)
179 180 181 182 183
    {
	$value = '0';
    }
    else
    {
184
	$value = '(' . join (' | ', @value) . ')';
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    }

    $map[$code] = $value;
}

sub print_tables
{
    my ($last) = @_;

    local ($bytes_out) = 0;

    open (OUT, "> chartables.h");

    print OUT "/* This file is automatically generated.  DO NOT EDIT!\n";
    print OUT "   Instead, edit gen-table.pl and re-run.  */\n\n";

201 202
    print OUT "#ifndef GCC_CHARTABLES_H\n";
    print OUT "#define GCC_CHARTABLES_H\n\n";
203 204

    print OUT "#define LETTER_START 1\n";
205 206 207
    print OUT "#define LETTER_PART  2\n";
    print OUT "#define LETTER_SPACE 4\n\n";
    print OUT "#define LETTER_MASK  7\n\n";
208 209 210

    for ($count = 0; $count <= $last; $count += 256)
    {
211
	$row[$count / 256] = &print_row ($count, '(char *) ', 'const char', 1,
212 213 214
					 'page');
    }

215
    print OUT "static const char *const type_table[256] = {\n";
216 217 218 219 220 221 222 223
    for ($count = 0; $count <= $last; $count += 256)
    {
	print OUT ",\n" if $count > 0;
	print OUT "  ", $row[$count / 256];
	$bytes_out += 4;
    }
    print OUT "\n};\n\n";

224
    print OUT "#endif /* ! GCC_CHARTABLES_H */\n";
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

    close (OUT);

    printf "Generated %d bytes\n", $bytes_out;
}

# Print a single "row" of a two-level table.
sub print_row
{
    my ($start, $def_pfx, $typname, $typsize, $name) = @_;

    my ($i);
    my (@values);
    my ($flag) = 1;
    my ($off);
    for ($off = 0; $off < 256; ++$off)
    {
	$values[$off] = $map[$off + $start];
	if ($values[$off] ne $values[0])
	{
	    $flag = 0;
	}
    }
    if ($flag)
    {
	return $def_pfx . $values[0];
    }

    printf OUT "static %s %s%d[256] = {\n  ", $typname, $name, $start / 256;
    my ($column) = 2;
    for ($i = $start; $i < $start + 256; ++$i)
    {
	print OUT ", "
	    if $i > $start;
	my ($text) = $values[$i - $start];
	if (length ($text) + $column + 2 > 78)
	{
	    print OUT "\n  ";
	    $column = 2;
	}
	print OUT $text;
	$column += length ($text) + 2;
    }
    print OUT "\n};\n\n";

    $bytes_out += 256 * $typsize;

    return sprintf "%s%d", $name, $start / 256;
}