2008-06-20 06:04:18 +08:00
|
|
|
#!/usr/bin/perl
|
2007-10-20 05:42:29 +08:00
|
|
|
#
|
2002-05-01 04:52:26 +08:00
|
|
|
# macros.pl produce macros.c from standard.mac
|
|
|
|
#
|
|
|
|
# The Netwide Assembler is copyright (C) 1996 Simon Tatham and
|
|
|
|
# Julian Hall. All rights reserved. The software is
|
2007-12-29 22:44:23 +08:00
|
|
|
# redistributable under the license given in the file "LICENSE"
|
2002-05-01 04:52:26 +08:00
|
|
|
# distributed in the NASM archive.
|
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;
|
|
|
|
foreach $o (unpack("C*", join('',@_))) {
|
|
|
|
$c = pack("C", $o);
|
|
|
|
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 {
|
|
|
|
$l .= "\'".$c."\',";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $l;
|
|
|
|
}
|
|
|
|
|
2008-06-20 07:00:04 +08:00
|
|
|
#
|
|
|
|
# Generate macros.c
|
|
|
|
#
|
2008-06-20 06:04:18 +08:00
|
|
|
open(OUT,">macros.c") 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";
|
|
|
|
print OUT "\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
|
|
|
|
2002-05-04 11:57:52 +08:00
|
|
|
foreach $fname ( @ARGV ) {
|
2002-05-19 09:51:00 +08:00
|
|
|
open(INPUT,$fname) or die "unable to open $fname\n";
|
2002-05-04 11:57:52 +08:00
|
|
|
while (<INPUT>) {
|
2002-05-01 05:01:08 +08:00
|
|
|
$line++;
|
|
|
|
chomp;
|
2002-05-04 11:57:52 +08:00
|
|
|
if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
|
|
|
|
$tasm_count = $index;
|
2008-06-20 06:04:18 +08:00
|
|
|
print OUT " /* End of TASM macros */\n";
|
2008-06-21 06:20:16 +08:00
|
|
|
} elsif (m/^OUT:\s*(.*\S)\s*$/) {
|
|
|
|
undef $pkg;
|
|
|
|
my @out_alias = split(/\s+/, $1);
|
2008-06-22 06:15:40 +08:00
|
|
|
printf OUT " /* %4d */ 0\n", $index++;
|
2008-06-21 06:20:16 +08:00
|
|
|
print OUT "};\n";
|
|
|
|
$index = 0;
|
2008-06-26 05:54:14 +08:00
|
|
|
printf OUT "const unsigned char %s_stdmac[] = {\n", $out_alias[0];
|
2008-06-21 06:20:16 +08:00
|
|
|
print OUT " /* From $fname */\n";
|
|
|
|
$lastname = $fname;
|
|
|
|
push(@out_list, $out_alias[0]);
|
|
|
|
$out_index{$out_alias[0]} = $index;
|
2008-06-20 06:04:18 +08:00
|
|
|
} elsif (m/^USE:\s*(\S+)\s*$/) {
|
2008-06-20 07:26:12 +08:00
|
|
|
$pkg = $1;
|
|
|
|
if (defined($pkg_number{$pkg})) {
|
|
|
|
die "$0: $fname: duplicate package: $pkg\n";
|
2008-06-20 06:04:18 +08:00
|
|
|
}
|
2008-06-22 06:15:40 +08:00
|
|
|
printf OUT " /* %4d */ 0\n", $index++;
|
2008-06-21 06:20:16 +08:00
|
|
|
print OUT "};\n";
|
|
|
|
$index = 0;
|
2008-06-26 05:54:14 +08:00
|
|
|
printf OUT "static const unsigned char nasm_stdmac_%s[] = {\n", $pkg;
|
2008-06-21 06:20:16 +08:00
|
|
|
print OUT " /* From $fname */\n";
|
|
|
|
$lastname = $fname;
|
2008-06-20 07:26:12 +08:00
|
|
|
push(@pkg_list, $pkg);
|
|
|
|
$pkg_number{$pkg} = $npkg++;
|
2008-06-22 06:15:40 +08:00
|
|
|
$z = pack("C", $pptok_hash{'%define'}+128)."__USE_\U$pkg\E__";
|
|
|
|
printf OUT " /* %4d */ %s0,\n", $index, charcify($z);
|
|
|
|
$index += length($z)+1;
|
2002-05-04 11:57:52 +08:00
|
|
|
} elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
|
2008-06-20 07:00:04 +08:00
|
|
|
my $s1, $s2, $pd, $ws;
|
|
|
|
$s1 = $1;
|
|
|
|
$s2 = '';
|
2008-06-20 07:17:41 +08:00
|
|
|
while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
|
|
|
|
$s2 .= "$'";
|
|
|
|
$pd = $1;
|
|
|
|
$ws = $3;
|
|
|
|
$s1 = $4;
|
2008-06-20 07:00:04 +08:00
|
|
|
if (defined($pptok_hash{$pd}) &&
|
|
|
|
$pptok_hash{$pd} <= 127) {
|
2008-06-22 06:15:40 +08:00
|
|
|
$s2 .= pack("C", $pptok_hash{$pd}+128);
|
2008-06-20 07:00:04 +08:00
|
|
|
} else {
|
|
|
|
$s2 .= $pd.$ws;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$s2 .= $s1;
|
|
|
|
if (length($s2) > 0) {
|
2008-06-21 06:20:16 +08:00
|
|
|
if ($lastname ne $fname) {
|
|
|
|
print OUT "\n /* From $fname */\n";
|
|
|
|
$lastname = $fname;
|
|
|
|
}
|
2008-06-22 06:15:40 +08:00
|
|
|
printf OUT " /* %4d */ %s0,\n",
|
|
|
|
$index, charcify($s2);
|
|
|
|
$index += length($s2)+1;
|
2007-10-20 05:42:29 +08:00
|
|
|
}
|
2002-05-04 11:57:52 +08:00
|
|
|
} else {
|
|
|
|
die "$fname:$line: error unterminated quote";
|
2002-05-01 05:01:08 +08:00
|
|
|
}
|
2002-05-04 11:57:52 +08:00
|
|
|
}
|
|
|
|
close(INPUT);
|
2002-05-01 04:52:26 +08:00
|
|
|
}
|
2008-06-22 06:15:40 +08:00
|
|
|
printf OUT " /* %4d */ 0\n};\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.
|
|
|
|
print OUT "#define UNUSED 16383\n";
|
|
|
|
|
|
|
|
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);
|