2007-09-12 09:29:43 +08:00
|
|
|
#!/usr/bin/perl
|
2009-06-29 07:52:56 +08:00
|
|
|
## --------------------------------------------------------------------------
|
2019-08-15 16:14:23 +08:00
|
|
|
##
|
|
|
|
## Copyright 1996-2019 The NASM Authors - All Rights Reserved
|
2009-06-29 07:52:56 +08:00
|
|
|
## See the file AUTHORS included with the NASM distribution for
|
|
|
|
## the specific copyright holders.
|
|
|
|
##
|
|
|
|
## Redistribution and use in source and binary forms, with or without
|
|
|
|
## modification, are permitted provided that the following
|
|
|
|
## conditions are met:
|
|
|
|
##
|
|
|
|
## * Redistributions of source code must retain the above copyright
|
|
|
|
## notice, this list of conditions and the following disclaimer.
|
|
|
|
## * Redistributions in binary form must reproduce the above
|
|
|
|
## copyright notice, this list of conditions and the following
|
|
|
|
## disclaimer in the documentation and/or other materials provided
|
|
|
|
## with the distribution.
|
2019-08-15 16:14:23 +08:00
|
|
|
##
|
2009-06-29 07:52:56 +08:00
|
|
|
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
##
|
|
|
|
## --------------------------------------------------------------------------
|
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
#
|
2008-06-20 07:00:04 +08:00
|
|
|
# Produce pptok.c, pptok.h and pptok.ph from pptok.dat
|
2007-09-12 09:29:43 +08:00
|
|
|
#
|
|
|
|
|
|
|
|
require 'phash.ph';
|
|
|
|
|
|
|
|
my($what, $in, $out) = @ARGV;
|
|
|
|
|
|
|
|
#
|
|
|
|
# Read pptok.dat
|
|
|
|
#
|
2017-04-03 10:28:13 +08:00
|
|
|
open(IN, '<', $in) or die "$0: cannot open: $in\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
while (defined($line = <IN>)) {
|
2013-06-30 05:30:44 +08:00
|
|
|
$line =~ s/\r?\n$//; # Remove trailing \r\n or \n
|
2007-09-12 09:29:43 +08:00
|
|
|
$line =~ s/^\s+//; # Remove leading whitespace
|
|
|
|
$line =~ s/\s*\#.*$//; # Remove comments and trailing whitespace
|
|
|
|
next if ($line eq '');
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-12 12:18:37 +08:00
|
|
|
if ($line =~ /^\%(.*)\*$/) {
|
2019-08-15 16:14:23 +08:00
|
|
|
# Condition stem
|
2007-09-12 09:29:43 +08:00
|
|
|
push(@cctok, $1);
|
2019-08-15 16:14:23 +08:00
|
|
|
} elsif ($line =~ /^\%(.*\!.*)$/) {
|
|
|
|
# Directive with case insensitity "i" option
|
|
|
|
# Mnemonic: ! is "upside down i"
|
|
|
|
push(@ppitok, $1);
|
2007-09-12 12:18:37 +08:00
|
|
|
} elsif ($line =~ /^\%(.*)$/) {
|
2019-08-15 16:14:23 +08:00
|
|
|
# Other directive
|
2007-09-12 09:29:43 +08:00
|
|
|
push(@pptok, $1);
|
|
|
|
} elsif ($line =~ /^\*(.*)$/) {
|
2019-08-15 16:14:23 +08:00
|
|
|
# Condition tail
|
2007-09-12 09:29:43 +08:00
|
|
|
push(@cond, $1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(IN);
|
|
|
|
|
2019-08-15 16:14:23 +08:00
|
|
|
# Always sort %if first
|
|
|
|
@cctok = sort { $a eq 'if' ? -1 : $b eq 'if' ? 1 : $a cmp $b } @cctok;
|
2007-09-12 10:12:07 +08:00
|
|
|
@cond = sort @cond;
|
2007-09-12 12:18:37 +08:00
|
|
|
@pptok = sort @pptok;
|
2019-08-15 16:14:23 +08:00
|
|
|
@ppitok = sort @ppitok;
|
2007-09-12 12:18:37 +08:00
|
|
|
|
|
|
|
# Generate the expanded list including conditionals. The conditionals
|
|
|
|
# are at the beginning, padded to a power of 2, with the inverses
|
2019-08-15 16:14:23 +08:00
|
|
|
# following each group; this allows a simple mask to pick out the condition,
|
|
|
|
# polarity, and directive type.
|
2007-09-12 10:12:07 +08:00
|
|
|
|
2007-09-12 12:18:37 +08:00
|
|
|
while ((scalar @cond) & (scalar @cond)-1) {
|
2019-08-15 16:14:23 +08:00
|
|
|
push(@cond, sprintf("_COND_%d", scalar @cond));
|
2007-09-12 12:18:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@cptok = ();
|
2007-09-12 09:29:43 +08:00
|
|
|
foreach $ct (@cctok) {
|
|
|
|
foreach $cc (@cond) {
|
2019-08-15 16:14:23 +08:00
|
|
|
push(@cptok, $ct.$cc);
|
|
|
|
}
|
|
|
|
foreach $cc (@cond) {
|
|
|
|
push(@cptok, $ct.'n'.$cc);
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-15 16:14:23 +08:00
|
|
|
$first_uncond = scalar @cptok;
|
2007-09-12 12:18:37 +08:00
|
|
|
@pptok = (@cptok, @pptok);
|
2007-09-12 09:29:43 +08:00
|
|
|
|
2019-08-15 16:14:23 +08:00
|
|
|
# Generate the list of case-specific tokens; these are in pairs
|
|
|
|
# with the -i- variant following the plain variant
|
|
|
|
if (scalar(@pptok) & 1) {
|
|
|
|
push(@pptok, 'CASE_PAD');
|
|
|
|
}
|
|
|
|
|
|
|
|
$first_itoken = scalar @pptok;
|
|
|
|
foreach $it (@ppitok) {
|
|
|
|
(my $at = $it) =~ s/\!//;
|
|
|
|
(my $bt = $it) =~ s/\!/i/;
|
|
|
|
|
|
|
|
push(@pptok, $at, $bt);
|
|
|
|
}
|
|
|
|
|
2017-04-03 10:28:13 +08:00
|
|
|
open(OUT, '>', $out) or die "$0: cannot open: $out\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Output pptok.h
|
|
|
|
#
|
|
|
|
if ($what eq 'h') {
|
2008-06-20 07:00:04 +08:00
|
|
|
print OUT "/* Automatically generated from $in by $0 */\n";
|
|
|
|
print OUT "/* Do not edit */\n";
|
|
|
|
print OUT "\n";
|
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT "enum preproc_token {\n";
|
2007-09-12 12:18:37 +08:00
|
|
|
$n = 0;
|
2007-09-12 09:29:43 +08:00
|
|
|
foreach $pt (@pptok) {
|
2007-09-12 12:18:37 +08:00
|
|
|
if (defined($pt)) {
|
2019-08-15 16:14:23 +08:00
|
|
|
printf OUT " %-24s = %3d,\n", "PP_\U$pt\E", $n;
|
2007-09-12 12:18:37 +08:00
|
|
|
}
|
|
|
|
$n++;
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
2019-08-15 16:14:23 +08:00
|
|
|
printf OUT " %-24s = %3d\n", 'PP_INVALID', -1;
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT "};\n";
|
2007-09-12 10:12:07 +08:00
|
|
|
print OUT "\n";
|
|
|
|
|
2019-08-15 16:14:23 +08:00
|
|
|
printf OUT "#define PP_COND(x) ((x) & 0x%x)\n",
|
|
|
|
(scalar(@cond)-1);
|
|
|
|
printf OUT "#define PP_IS_COND(x) ((unsigned int)(x) < PP_%s)\n",
|
|
|
|
uc($pptok[$first_uncond]);
|
|
|
|
printf OUT "#define PP_COND_NEGATIVE(x) (!!((x) & 0x%x))\n", scalar(@cond);
|
|
|
|
print OUT "\n";
|
|
|
|
printf OUT "#define PP_HAS_CASE(x) ((x) >= PP_%s)\n",
|
|
|
|
uc($pptok[$first_itoken]);
|
|
|
|
print OUT "#define PP_INSENSITIVE(x) ((x) & 1)\n";
|
2007-09-12 12:18:37 +08:00
|
|
|
print OUT "\n";
|
2007-09-12 10:12:07 +08:00
|
|
|
|
|
|
|
foreach $ct (@cctok) {
|
2007-09-12 12:18:37 +08:00
|
|
|
print OUT "#define CASE_PP_\U$ct\E";
|
|
|
|
$pref = " \\\n";
|
|
|
|
foreach $cc (@cond) {
|
2019-08-15 16:14:23 +08:00
|
|
|
print OUT "$pref\tcase PP_\U${ct}${cc}\E";
|
|
|
|
$pref = ":\\\n";
|
|
|
|
}
|
|
|
|
foreach $cc (@cond) {
|
|
|
|
print OUT "$pref\tcase PP_\U${ct}N${cc}\E";
|
|
|
|
$pref = ":\\\n";
|
2007-09-12 12:18:37 +08:00
|
|
|
}
|
|
|
|
print OUT "\n"; # No colon or newline on the last one
|
2007-09-12 10:12:07 +08:00
|
|
|
}
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Output pptok.c
|
|
|
|
#
|
|
|
|
if ($what eq 'c') {
|
2008-06-20 07:00:04 +08:00
|
|
|
print OUT "/* Automatically generated from $in by $0 */\n";
|
|
|
|
print OUT "/* Do not edit */\n";
|
|
|
|
print OUT "\n";
|
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
my %tokens = ();
|
|
|
|
my @tokendata = ();
|
|
|
|
|
2007-09-12 12:18:37 +08:00
|
|
|
my $n = 0;
|
2007-09-12 09:29:43 +08:00
|
|
|
foreach $pt (@pptok) {
|
2019-08-15 16:14:23 +08:00
|
|
|
# Upper case characters signify internal use tokens only
|
|
|
|
if (defined($pt) && $pt !~ /[A-Z]/) {
|
2007-09-12 12:18:37 +08:00
|
|
|
$tokens{'%'.$pt} = $n;
|
2007-09-13 01:02:55 +08:00
|
|
|
if ($pt =~ /[\@\[\]\\_]/) {
|
|
|
|
# Fail on characters which look like upper-case letters
|
|
|
|
# to the quick-and-dirty downcasing in the prehash
|
|
|
|
# (see below)
|
|
|
|
die "$in: invalid character in token: $pt";
|
|
|
|
}
|
2007-09-12 12:18:37 +08:00
|
|
|
}
|
|
|
|
$n++;
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my @hashinfo = gen_perfect_hash(\%tokens);
|
2010-11-08 00:20:23 +08:00
|
|
|
if (!@hashinfo) {
|
2007-09-12 09:29:43 +08:00
|
|
|
die "$0: no hash found\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
# Paranoia...
|
|
|
|
verify_hash_table(\%tokens, \@hashinfo);
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
($n, $sv, $g) = @hashinfo;
|
|
|
|
die if ($n & ($n-1));
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
print OUT "#include \"compiler.h\"\n";
|
2018-12-28 04:37:25 +08:00
|
|
|
print OUT "#include \"nctype.h\"\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT "#include \"nasmlib.h\"\n";
|
2007-10-03 08:40:00 +08:00
|
|
|
print OUT "#include \"hashtbl.h\"\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT "#include \"preproc.h\"\n";
|
|
|
|
print OUT "\n";
|
|
|
|
|
|
|
|
# Note that this is global.
|
2007-09-12 12:18:37 +08:00
|
|
|
printf OUT "const char * const pp_directives[%d] = {\n", scalar(@pptok);
|
|
|
|
foreach $d (@pptok) {
|
2019-08-15 16:37:48 +08:00
|
|
|
if (defined($d) && $d !~ /[A-Z]/) {
|
2007-09-12 12:18:37 +08:00
|
|
|
print OUT " \"%$d\",\n";
|
|
|
|
} else {
|
|
|
|
print OUT " NULL,\n";
|
|
|
|
}
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
|
|
|
print OUT "};\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2008-06-22 09:18:41 +08:00
|
|
|
printf OUT "const uint8_t pp_directives_len[%d] = {\n", scalar(@pptok);
|
2008-06-20 07:00:04 +08:00
|
|
|
foreach $d (@pptok) {
|
|
|
|
printf OUT " %d,\n", defined($d) ? length($d)+1 : 0;
|
|
|
|
}
|
|
|
|
print OUT "};\n";
|
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT "enum preproc_token pp_token_hash(const char *token)\n";
|
|
|
|
print OUT "{\n";
|
|
|
|
|
|
|
|
# Put a large value in unused slots. This makes it extremely unlikely
|
|
|
|
# that any combination that involves unused slot will pass the range test.
|
|
|
|
# This speeds up rejection of unrecognized tokens, i.e. identifiers.
|
2020-07-01 00:17:41 +08:00
|
|
|
print OUT "#define UNUSED_HASH_ENTRY (65535/3)\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
|
|
|
|
print OUT " static const int16_t hash1[$n] = {\n";
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
my $h = ${$g}[$i*2+0];
|
2020-07-01 00:17:41 +08:00
|
|
|
print OUT " ", defined($h) ? $h : 'UNUSED_HASH_ENTRY', ",\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
|
|
|
print OUT " };\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT " static const int16_t hash2[$n] = {\n";
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
my $h = ${$g}[$i*2+1];
|
2020-07-01 00:17:41 +08:00
|
|
|
print OUT " ", defined($h) ? $h : 'UNUSED_HASH_ENTRY', ",\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
}
|
|
|
|
print OUT " };\n";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2007-10-03 08:40:00 +08:00
|
|
|
print OUT " uint32_t k1, k2;\n";
|
|
|
|
print OUT " uint64_t crc;\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
# For correct overflow behavior, "ix" should be unsigned of the same
|
|
|
|
# width as the hash arrays.
|
|
|
|
print OUT " uint16_t ix;\n";
|
|
|
|
print OUT "\n";
|
|
|
|
|
2007-10-03 08:40:00 +08:00
|
|
|
printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), token);\n",
|
2007-10-20 05:42:29 +08:00
|
|
|
$$sv[0], $$sv[1];
|
2007-10-03 08:40:00 +08:00
|
|
|
print OUT " k1 = (uint32_t)crc;\n";
|
|
|
|
print OUT " k2 = (uint32_t)(crc >> 32);\n";
|
|
|
|
print OUT "\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
|
2007-09-12 12:18:37 +08:00
|
|
|
printf OUT " if (ix >= %d)\n", scalar(@pptok);
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT " return PP_INVALID;\n";
|
|
|
|
print OUT "\n";
|
|
|
|
|
2007-09-12 13:18:20 +08:00
|
|
|
print OUT " if (!pp_directives[ix] || nasm_stricmp(pp_directives[ix], token))\n";
|
2007-09-12 09:29:43 +08:00
|
|
|
print OUT " return PP_INVALID;\n";
|
|
|
|
print OUT "\n";
|
|
|
|
print OUT " return ix;\n";
|
|
|
|
print OUT "}\n";
|
|
|
|
}
|
2008-06-20 07:00:04 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Output pptok.ph
|
|
|
|
#
|
|
|
|
if ($what eq 'ph') {
|
|
|
|
print OUT "# Automatically generated from $in by $0\n";
|
|
|
|
print OUT "# Do not edit\n";
|
|
|
|
print OUT "\n";
|
2019-08-15 16:14:23 +08:00
|
|
|
|
2008-06-20 07:00:04 +08:00
|
|
|
print OUT "%pptok_hash = (\n";
|
|
|
|
$n = 0;
|
|
|
|
foreach $tok (@pptok) {
|
|
|
|
if (defined($tok)) {
|
|
|
|
printf OUT " '%%%s' => %d,\n", $tok, $n;
|
|
|
|
}
|
|
|
|
$n++;
|
|
|
|
}
|
|
|
|
print OUT ");\n";
|
|
|
|
print OUT "1;\n";
|
|
|
|
}
|