| 1 |
#!/usr/bin/perl |
| 2 |
# SPDX-License-Identifier: BSD-2-Clause |
| 3 |
# Copyright 1996-2025 The NASM Authors - All Rights Reserved |
| 4 |
|
| 5 |
# |
| 6 |
# macros.pl produce macros.c from standard.mac |
| 7 |
# |
| 8 |
|
| 9 |
use strict; |
| 10 |
use integer; |
| 11 |
use bytes; |
| 12 |
use Compress::Zlib; |
| 13 |
|
| 14 |
require 'phash.ph'; |
| 15 |
|
| 16 |
my $fname; |
| 17 |
my $line = 0; |
| 18 |
my @pname; |
| 19 |
|
| 20 |
my $dump_text = 1; |
| 21 |
|
| 22 |
# |
| 23 |
# Print out a string as a byte array |
| 24 |
# |
| 25 |
sub print_data($$) { |
| 26 |
my($o, $s) = @_; |
| 27 |
my $perline = 8; |
| 28 |
|
| 29 |
for (my $ix = 0; $ix < length($s); $ix += $perline) { |
| 30 |
my $ss = substr($s, $ix, $perline); |
| 31 |
print $o ' '; |
| 32 |
foreach my $b (unpack('C*', $ss)) { |
| 33 |
printf $o '0x%02x,', $b; |
| 34 |
} |
| 35 |
print $o "\n"; |
| 36 |
} |
| 37 |
print $o "};\n"; |
| 38 |
} |
| 39 |
|
| 40 |
# |
| 41 |
# Prefix a string with its length in uleb128 encoding |
| 42 |
# |
| 43 |
sub uleb128($) |
| 44 |
{ |
| 45 |
my($n) = @_; |
| 46 |
my $o = ''; |
| 47 |
|
| 48 |
do { |
| 49 |
my $nn = $n >> 7; |
| 50 |
$o .= pack('C', ($n & 127) | ($nn ? 128 : 0)); |
| 51 |
$n = $nn; |
| 52 |
} while ($n); |
| 53 |
|
| 54 |
return $o; |
| 55 |
} |
| 56 |
|
| 57 |
sub addstringlen($) |
| 58 |
{ |
| 59 |
my($s) = @_; |
| 60 |
|
| 61 |
my $l = length($s); |
| 62 |
return $l ? uleb128($l).$s : ''; |
| 63 |
} |
| 64 |
|
| 65 |
sub init_mac() { |
| 66 |
return { |
| 67 |
'name' => undef, |
| 68 |
'lines' => [], |
| 69 |
'ifdefs' => [], |
| 70 |
'static' => 0 |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
# |
| 75 |
# Output a data blob and a data structure |
| 76 |
# |
| 77 |
sub flush_mac($$) |
| 78 |
{ |
| 79 |
my($out, $mac) = @_; |
| 80 |
|
| 81 |
my $init = init_mac(); |
| 82 |
|
| 83 |
return $init if (!defined($mac)); |
| 84 |
|
| 85 |
my $name = $mac->{'name'}; |
| 86 |
return $init if (!$name); |
| 87 |
|
| 88 |
printf $out "\n\n/* --- from %s --- */\n\n", $mac->{'fname'}; |
| 89 |
|
| 90 |
my $ifdefs = $mac->{'ifdefs'}; |
| 91 |
if (scalar(@$ifdefs)) { |
| 92 |
print $out '#if', join(' ||', map { " defined($_)" } @$ifdefs), "\n"; |
| 93 |
} |
| 94 |
|
| 95 |
if ($dump_text) { |
| 96 |
print $out "/\*\n"; |
| 97 |
print $out map { " * $_\n" } @{$mac->{'lines'}}; |
| 98 |
print $out " \*/\n\n"; |
| 99 |
} |
| 100 |
|
| 101 |
my $data; |
| 102 |
foreach my $l (@{$mac->{'lines'}}) { |
| 103 |
$data .= addstringlen($l); |
| 104 |
} |
| 105 |
$data .= pack('C', 0); # End of blob marker |
| 106 |
|
| 107 |
my $dlen = length($data); |
| 108 |
my $zblob = Compress::Zlib::compress($data, 9); |
| 109 |
my $zlen = length($zblob); |
| 110 |
|
| 111 |
if ($zlen >= $dlen) { |
| 112 |
$zblob = $data; |
| 113 |
$zlen = $dlen; |
| 114 |
} |
| 115 |
|
| 116 |
printf $out "static const unsigned char %s_blob[%d] = {\n", $name, $zlen; |
| 117 |
print_data($out, $zblob); |
| 118 |
|
| 119 |
printf $out "\n%smacros_t %s = {\n %d, %d, %s_blob\n};\n", |
| 120 |
$mac->{'static'} ? 'static ' : '', |
| 121 |
$name, $dlen, $zlen, $name; |
| 122 |
|
| 123 |
print $out "#endif\n" if (scalar(@$ifdefs)); |
| 124 |
return $init; |
| 125 |
} |
| 126 |
|
| 127 |
# |
| 128 |
# Generate macros.c |
| 129 |
# |
| 130 |
my $out; |
| 131 |
|
| 132 |
open($out, '>', 'macros/macros.c') or die "unable to open macros.c\n"; |
| 133 |
|
| 134 |
print $out "/*\n"; |
| 135 |
print $out " * Do not edit - this file auto-generated by macros.pl from:\n"; |
| 136 |
print $out " * ", join("\n * ", @ARGV), "\n"; |
| 137 |
print $out " */\n"; |
| 138 |
print $out "\n"; |
| 139 |
print $out "#include \"macros.h\"\n"; |
| 140 |
print $out "#include \"nasmlib.h\"\n"; |
| 141 |
print $out "#include \"hashtbl.h\"\n"; |
| 142 |
print $out "#include \"outform.h\"\n"; |
| 143 |
|
| 144 |
my $mac = undef; |
| 145 |
my $npkg = 0; |
| 146 |
my @pkg_list = (); |
| 147 |
my %pkg_number = (); |
| 148 |
my $pkg; |
| 149 |
|
| 150 |
foreach my $args ( @ARGV ) { |
| 151 |
my @file_list = glob ( $args ); |
| 152 |
foreach $fname ( @file_list ) { |
| 153 |
open(INPUT,'<', $fname) or die "$0: $fname: $!\n"; |
| 154 |
while (<INPUT>) { |
| 155 |
$line++; |
| 156 |
chomp; |
| 157 |
while (/^(.*)\\$/) { |
| 158 |
$_ = $1; |
| 159 |
$_ .= <INPUT>; |
| 160 |
chomp; |
| 161 |
$line++; |
| 162 |
} |
| 163 |
|
| 164 |
s/^\s*(([^\'\"\;]|\"[^\"]*\"|\'[^\']*\')*?)\s*(\;.*)?$/$1/; |
| 165 |
s/\s+/ /g; # XXX: wrong if strings have whitespace |
| 166 |
next if ($_ eq ''); |
| 167 |
|
| 168 |
if (m/^OUT:\s*(\S.*)$/) { |
| 169 |
my @out_alias = split(/\s+/, $1); |
| 170 |
undef $pkg; |
| 171 |
$mac = flush_mac($out, $mac); |
| 172 |
push(@{$mac->{'ifdefs'}}, map { "OF_\U$_\E" } @out_alias); |
| 173 |
$mac->{'name'} = $out_alias[0].'_stdmac'; |
| 174 |
$mac->{'fname'} = $fname; |
| 175 |
} elsif (m/^STD:\s*(\S+)$/) { |
| 176 |
undef $pkg; |
| 177 |
my $std = $1; |
| 178 |
$mac = flush_mac($out, $mac); |
| 179 |
$mac->{'name'} = 'nasm_stdmac_' . $std; |
| 180 |
$mac->{'fname'} = $fname; |
| 181 |
} elsif (m/^USE:\s*(\S+)$/) { |
| 182 |
$pkg = $1; |
| 183 |
if (defined($pkg_number{$pkg})) { |
| 184 |
die "$0: $fname: duplicate package: $pkg\n"; |
| 185 |
} |
| 186 |
$mac = flush_mac($out, $mac); |
| 187 |
$mac->{'name'} = 'nasm_usemac_' . $pkg; |
| 188 |
$mac->{'static'} = 1; |
| 189 |
$mac->{'fname'} = $fname; |
| 190 |
push(@pkg_list, $pkg); |
| 191 |
$pkg_number{$pkg} = $npkg++; |
| 192 |
push(@{$mac->{'lines'}}, |
| 193 |
"\%define __?USE_\U$pkg\E?__", |
| 194 |
"\%defalias __USE_\U$pkg\E__ __?USE\U$pkg\E?__"); |
| 195 |
} else { |
| 196 |
if (!defined($mac)) { |
| 197 |
die "$0: $fname: macro declarations outside a known block\n"; |
| 198 |
} |
| 199 |
|
| 200 |
push(@{$mac->{'lines'}}, $_); |
| 201 |
} |
| 202 |
} |
| 203 |
close(INPUT); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
$mac = flush_mac($out, $mac); |
| 209 |
|
| 210 |
my @hashinfo = gen_perfect_hash(\%pkg_number); |
| 211 |
if (!@hashinfo) { |
| 212 |
die "$0: no hash found\n"; |
| 213 |
} |
| 214 |
# Paranoia... |
| 215 |
verify_hash_table(\%pkg_number, \@hashinfo); |
| 216 |
my ($n, $sv, $g) = @hashinfo; |
| 217 |
die if ($n & ($n-1)); |
| 218 |
$n <<= 1; |
| 219 |
|
| 220 |
printf $out "\n\nconst unsigned int use_package_count = %d;\n\n", $npkg; |
| 221 |
|
| 222 |
print $out "const struct use_package *nasm_find_use_package(const char *name)\n"; |
| 223 |
print $out "{\n"; |
| 224 |
print $out " static const struct use_package packages[$npkg] = {\n"; |
| 225 |
my $ix = 0; |
| 226 |
foreach $pkg (@pkg_list) { |
| 227 |
printf $out " { \"%s\", \&nasm_usemac_%s, %d },\n", |
| 228 |
$pkg, $pkg, $ix++; |
| 229 |
} |
| 230 |
print $out " };\n"; |
| 231 |
|
| 232 |
# Put a large value in unused slots. This makes it extremely unlikely |
| 233 |
# that any combination that involves unused slot will pass the range test. |
| 234 |
# This speeds up rejection of unrecognized tokens, i.e. identifiers. |
| 235 |
print $out "#define INVALID_HASH_ENTRY (65535/3)\n"; |
| 236 |
|
| 237 |
print $out " static const int16_t hashdata[$n] = {\n"; |
| 238 |
for (my $i = 0; $i < $n; $i++) { |
| 239 |
my $h = ${$g}[$i]; |
| 240 |
print $out " ", defined($h) ? $h : 'INVALID_HASH_ENTRY', ",\n"; |
| 241 |
} |
| 242 |
print $out " };\n"; |
| 243 |
|
| 244 |
print $out " uint32_t k1, k2;\n"; |
| 245 |
print $out " uint64_t crc;\n"; |
| 246 |
# For correct overflow behavior, "ix" should be unsigned of the same |
| 247 |
# width as the hash arrays. |
| 248 |
print $out " uint16_t ix;\n"; |
| 249 |
print $out "\n"; |
| 250 |
|
| 251 |
printf $out " crc = crc64i(UINT64_C(0x%08x%08x), name);\n", |
| 252 |
$$sv[0], $$sv[1]; |
| 253 |
printf $out " k1 = ((uint32_t)crc & 0x%x) + 0;\n", $n-2; |
| 254 |
printf $out " k2 = ((uint32_t)(crc >> 32) & 0x%x) + 1;\n", $n-2; |
| 255 |
print $out "\n"; |
| 256 |
printf $out " ix = hashdata[k1] + hashdata[k2];\n"; |
| 257 |
printf $out " if (ix >= %d)\n", scalar(@pkg_list); |
| 258 |
print $out " return NULL;\n"; |
| 259 |
print $out "\n"; |
| 260 |
print $out " if (nasm_stricmp(packages[ix].package, name))\n"; |
| 261 |
print $out " return NULL;\n"; |
| 262 |
print $out "\n"; |
| 263 |
print $out " return &packages[ix];\n"; |
| 264 |
print $out "}\n"; |
| 265 |
|
| 266 |
close($out); |