2008-06-20 06:04:18 +08:00
|
|
|
#!/usr/bin/perl
|
2009-06-29 07:52:56 +08:00
|
|
|
## --------------------------------------------------------------------------
|
|
|
|
##
|
|
|
|
## Copyright 1996-2009 The NASM Authors - All Rights Reserved
|
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
## 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-10-20 05:42:29 +08:00
|
|
|
#
|
2002-05-01 04:52:26 +08:00
|
|
|
# macros.pl produce macros.c from standard.mac
|
|
|
|
#
|
2002-05-01 05:01:38 +08:00
|
|
|
|
2008-06-20 06:04:18 +08:00
|
|
|
require 'phash.ph';
|
2008-06-20 07:00:04 +08:00
|
|
|
require 'pptok.ph';
|
2002-05-01 05:01:08 +08:00
|
|
|
|
2008-06-22 06:15:40 +08:00
|
|
|
use bytes;
|
|
|
|
|
2002-05-01 05:01:08 +08:00
|
|
|
my $fname;
|
|
|
|
my $line = 0;
|
2002-05-04 11:57:52 +08:00
|
|
|
my $index = 0;
|
2008-06-20 06:04:18 +08:00
|
|
|
my $tasm_count = 0;
|
2002-05-01 04:52:26 +08:00
|
|
|
|
2008-06-22 06:15:40 +08:00
|
|
|
#
|
|
|
|
# Print out a string as a character array
|
|
|
|
#
|
|
|
|
sub charcify(@) {
|
|
|
|
my $l = '';
|
|
|
|
my $c, $o;
|
2013-10-20 03:20:28 +08:00
|
|
|
my $space = 1;
|
|
|
|
my $quote = 0;
|
|
|
|
|
2008-06-22 06:15:40 +08:00
|
|
|
foreach $o (unpack("C*", join('',@_))) {
|
|
|
|
$c = pack("C", $o);
|
2013-10-20 03:20:28 +08:00
|
|
|
if ($quote) {
|
|
|
|
if ($o == $quote) {
|
|
|
|
$quote = 0;
|
|
|
|
}
|
|
|
|
} elsif ($c =~ /^[\'\"\`]$/) {
|
|
|
|
$quote = $o;
|
|
|
|
} else {
|
|
|
|
if ($c =~ /\s/) {
|
|
|
|
next if ($space);
|
|
|
|
$o = 32;
|
|
|
|
$c = ' ';
|
|
|
|
$space = 1;
|
|
|
|
} elsif ($o > 126) {
|
|
|
|
$space = 1; # Implicit space after compacted directive
|
|
|
|
} else {
|
|
|
|
$space = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-22 06:15:40 +08:00
|
|
|
if ($o < 32 || $o > 126 || $c eq '"' || $c eq "\\") {
|
2008-06-26 05:54:14 +08:00
|
|
|
$l .= sprintf("%3d,", $o);
|
2008-06-22 06:15:40 +08:00
|
|
|
} else {
|
2010-11-07 06:41:51 +08:00
|
|
|
$c =~ s/\'/\\'/; # << sanitize single quote.
|
2008-06-22 06:15:40 +08:00
|
|
|
$l .= "\'".$c."\',";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $l;
|
|
|
|
}
|
|
|
|
|
2010-11-07 06:41:51 +08:00
|
|
|
|
2008-06-20 07:00:04 +08:00
|
|
|
#
|
|
|
|
# Generate macros.c
|
|
|
|
#
|
2008-09-10 00:43:16 +08:00
|
|
|
open(OUT,"> macros.c\0") or die "unable to open macros.c\n";
|
2002-05-01 04:52:26 +08:00
|
|
|
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT "/*\n";
|
|
|
|
print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
|
2008-06-22 06:15:40 +08:00
|
|
|
print OUT " * ", join("\n * ", @ARGV), "\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT " */\n";
|
|
|
|
print OUT "\n";
|
|
|
|
print OUT "#include \"tables.h\"\n";
|
|
|
|
print OUT "#include \"nasmlib.h\"\n";
|
|
|
|
print OUT "#include \"hashtbl.h\"\n";
|
2009-06-28 13:07:33 +08:00
|
|
|
print OUT "#include \"output/outform.h\"\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT "\n";
|
2008-09-10 00:54:03 +08:00
|
|
|
print OUT "#if 1\n";
|
2008-06-26 05:54:14 +08:00
|
|
|
print OUT "const unsigned char nasm_stdmac[] = {";
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2008-06-20 07:26:12 +08:00
|
|
|
my $npkg = 0;
|
|
|
|
my @pkg_list = ();
|
|
|
|
my %pkg_number = ();
|
|
|
|
my $pkg;
|
2008-06-21 06:20:16 +08:00
|
|
|
my @out_list = ();
|
|
|
|
my $outfmt;
|
|
|
|
my $lastname;
|
2008-06-22 06:15:40 +08:00
|
|
|
my $z;
|
2007-10-20 05:42:29 +08:00
|
|
|
|
2014-12-24 03:38:43 +08:00
|
|
|
foreach $args ( @ARGV ) {
|
|
|
|
my @file_list = glob ( $args );
|
|
|
|
foreach $fname ( @file_list ) {
|
|
|
|
open(INPUT,"< $fname\0") or die "$0: $fname: $!\n";
|
|
|
|
while (<INPUT>) {
|
2008-10-20 07:44:02 +08:00
|
|
|
$line++;
|
2014-12-24 03:38:43 +08:00
|
|
|
chomp;
|
|
|
|
while (/^(.*)\\$/) {
|
|
|
|
$_ = $1;
|
|
|
|
$_ .= <INPUT>;
|
|
|
|
chomp;
|
|
|
|
$line++;
|
2008-06-20 06:04:18 +08:00
|
|
|
}
|
2014-12-24 03:38:43 +08:00
|
|
|
if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
|
|
|
|
$tasm_count = $index;
|
|
|
|
print OUT " /* End of TASM macros */\n";
|
|
|
|
} elsif (m/^OUT:\s*(.*\S)\s*$/) {
|
|
|
|
undef $pkg;
|
|
|
|
my @out_alias = split(/\s+/, $1);
|
|
|
|
printf OUT " /* %4d */ 0\n", $index++;
|
|
|
|
print OUT "};\n#endif\n";
|
|
|
|
$index = 0;
|
|
|
|
print OUT "\n";
|
|
|
|
my $pfx = '#if';
|
|
|
|
foreach my $al (@out_alias) {
|
|
|
|
print OUT $pfx, " defined(OF_\U${al}\E)";
|
|
|
|
$pfx = ' ||';
|
2008-06-20 07:00:04 +08:00
|
|
|
}
|
2014-12-24 03:38:43 +08:00
|
|
|
printf OUT "\nconst unsigned char %s_stdmac[] = {\n", $out_alias[0];
|
|
|
|
print OUT " /* From $fname */\n";
|
|
|
|
$lastname = $fname;
|
|
|
|
push(@out_list, $out_alias[0]);
|
|
|
|
$out_index{$out_alias[0]} = $index;
|
|
|
|
} elsif (m/^USE:\s*(\S+)\s*$/) {
|
|
|
|
$pkg = $1;
|
|
|
|
if (defined($pkg_number{$pkg})) {
|
|
|
|
die "$0: $fname: duplicate package: $pkg\n";
|
|
|
|
}
|
|
|
|
printf OUT " /* %4d */ 0\n", $index++;
|
|
|
|
print OUT "};\n#endif\n";
|
|
|
|
$index = 0;
|
|
|
|
print OUT "\n#if 1\n";
|
|
|
|
printf OUT "static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
|
|
|
|
print OUT " /* From $fname */\n";
|
|
|
|
$lastname = $fname;
|
|
|
|
push(@pkg_list, $pkg);
|
|
|
|
$pkg_number{$pkg} = $npkg++;
|
|
|
|
$z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
|
|
|
|
printf OUT " /* %4d */ %s0,\n", $index, charcify($z);
|
|
|
|
$index += length($z)+1;
|
|
|
|
} elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
|
|
|
|
my $s1, $s2, $pd, $ws;
|
|
|
|
$s1 = $1;
|
|
|
|
$s2 = '';
|
|
|
|
while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
|
|
|
|
$s2 .= "$'";
|
|
|
|
$pd = $1;
|
|
|
|
$ws = $3;
|
|
|
|
$s1 = $4;
|
|
|
|
if (defined($pptok_hash{$pd}) &&
|
|
|
|
$pptok_hash{$pd} <= 127) {
|
|
|
|
$s2 .= pack("C", $pptok_hash{$pd}+128);
|
|
|
|
} else {
|
|
|
|
$s2 .= $pd.$ws;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$s2 .= $s1;
|
|
|
|
if (length($s2) > 0) {
|
|
|
|
if ($lastname ne $fname) {
|
|
|
|
print OUT "\n /* From $fname */\n";
|
|
|
|
$lastname = $fname;
|
|
|
|
}
|
|
|
|
printf OUT " /* %4d */ %s0,\n",
|
|
|
|
$index, charcify($s2);
|
|
|
|
$index += length($s2)+1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
die "$fname:$line: error unterminated quote";
|
2008-06-20 07:00:04 +08:00
|
|
|
}
|
2002-05-01 05:01:08 +08:00
|
|
|
}
|
2014-12-24 03:38:43 +08:00
|
|
|
close(INPUT);
|
2002-05-04 11:57:52 +08:00
|
|
|
}
|
2002-05-01 04:52:26 +08:00
|
|
|
}
|
2008-09-10 00:54:03 +08:00
|
|
|
printf OUT " /* %4d */ 0\n};\n#endif\n\n", $index++;
|
2008-06-26 05:54:14 +08:00
|
|
|
print OUT "const unsigned char * const nasm_stdmac_after_tasm = ",
|
2008-06-20 06:04:18 +08:00
|
|
|
"&nasm_stdmac[$tasm_count];\n\n";
|
|
|
|
|
2008-06-20 07:26:12 +08:00
|
|
|
my @hashinfo = gen_perfect_hash(\%pkg_number);
|
2008-06-20 06:04:18 +08:00
|
|
|
if (!@hashinfo) {
|
|
|
|
die "$0: no hash found\n";
|
|
|
|
}
|
|
|
|
# Paranoia...
|
2008-06-20 07:26:12 +08:00
|
|
|
verify_hash_table(\%pkg_number, \@hashinfo);
|
2008-06-20 06:04:18 +08:00
|
|
|
my ($n, $sv, $g) = @hashinfo;
|
|
|
|
die if ($n & ($n-1));
|
|
|
|
|
2008-06-26 05:54:14 +08:00
|
|
|
print OUT "const unsigned char *nasm_stdmac_find_package(const char *package)\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT "{\n";
|
|
|
|
print OUT " static const struct {\n";
|
2008-06-20 07:26:12 +08:00
|
|
|
print OUT " const char *package;\n";
|
2008-06-26 05:54:14 +08:00
|
|
|
print OUT " const unsigned char *macros;\n";
|
2008-06-20 07:26:12 +08:00
|
|
|
print OUT " } packages[$npkg] = {\n";
|
|
|
|
foreach $pkg (@pkg_list) {
|
2008-06-21 06:20:16 +08:00
|
|
|
printf OUT " { \"%s\", nasm_stdmac_%s },\n",
|
|
|
|
$pkg, $pkg;
|
2008-06-20 06:04:18 +08:00
|
|
|
}
|
|
|
|
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.
|
2016-01-30 04:05:27 +08:00
|
|
|
print OUT "#define UNUSED (65535/3)\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
|
|
|
|
print OUT " static const int16_t hash1[$n] = {\n";
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
my $h = ${$g}[$i*2+0];
|
|
|
|
print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
|
|
|
|
}
|
|
|
|
print OUT " };\n";
|
|
|
|
|
|
|
|
print OUT " static const int16_t hash2[$n] = {\n";
|
|
|
|
for ($i = 0; $i < $n; $i++) {
|
|
|
|
my $h = ${$g}[$i*2+1];
|
|
|
|
print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
|
|
|
|
}
|
|
|
|
print OUT " };\n";
|
|
|
|
|
|
|
|
print OUT " uint32_t k1, k2;\n";
|
|
|
|
print OUT " uint64_t crc;\n";
|
|
|
|
# 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";
|
|
|
|
|
2008-06-20 07:26:12 +08:00
|
|
|
printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
|
2008-06-20 06:04:18 +08:00
|
|
|
$$sv[0], $$sv[1];
|
|
|
|
print OUT " k1 = (uint32_t)crc;\n";
|
|
|
|
print OUT " k2 = (uint32_t)(crc >> 32);\n";
|
|
|
|
print OUT "\n";
|
|
|
|
printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
|
2008-06-20 07:26:12 +08:00
|
|
|
printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT " return NULL;\n";
|
|
|
|
print OUT "\n";
|
2008-06-20 07:26:12 +08:00
|
|
|
print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT " return NULL;\n";
|
|
|
|
print OUT "\n";
|
2008-06-20 07:26:12 +08:00
|
|
|
print OUT " return packages[ix].macros;\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT "}\n";
|
|
|
|
|
|
|
|
close(OUT);
|