3 # macros.pl produce macros.c from standard.mac
5 # The Netwide Assembler is copyright (C) 1996 Simon Tatham and
6 # Julian Hall. All rights reserved. The software is
7 # redistributable under the license given in the file "LICENSE"
8 # distributed in the NASM archive.
21 open(OUT
,">macros.c") or die "unable to open macros.c\n";
24 print OUT
" * Do not edit - this file auto-generated by macros.pl from:\n";
25 print OUT
" * ", join(' ', @ARGV), "\n";
28 print OUT
"#include \"tables.h\"\n";
29 print OUT
"#include \"nasmlib.h\"\n";
30 print OUT
"#include \"hashtbl.h\"\n";
32 print OUT
"const char * const nasm_stdmac[] = {";
36 my %module_number = ();
37 my %module_index = ();
40 foreach $fname ( @ARGV ) {
41 open(INPUT
,$fname) or die "unable to open $fname\n";
42 print OUT
"\n /* From $fname */\n";
46 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
48 print OUT
" /* End of TASM macros */\n";
49 } elsif (m/^USE:\s*(\S+)\s*$/) {
51 if (defined($module_number{$module})) {
52 die "$0: $fname: duplicate module: $module\n";
54 printf OUT
" /* %4d */ NULL,\n", $index++;
55 print OUT
" /* %use $module */\n";
56 push(@module_list, $module);
57 $module_number{$module} = $nmodule++;
58 $module_index{$module} = $index;
59 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
60 my $s1, $s2, $pd, $ws;
62 $s1 =~ s/(\s)\s+/$1/g;
66 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
71 if (defined($pptok_hash{$pd}) &&
72 $pptok_hash{$pd} <= 127) {
73 $s2 .= sprintf("\\x%02x\"\"", $pptok_hash{$pd}+128);
79 if (length($s2) > 0) {
80 printf OUT
" /* %4d */ \"%s\",\n", $index++, $s2;
83 die "$fname:$line: error unterminated quote";
88 printf OUT
" /* %4d */ NULL\n};\n\n", $index++;
89 print OUT
"const char * const * const nasm_stdmac_after_tasm = ",
90 "&nasm_stdmac[$tasm_count];\n\n";
92 my @hashinfo = gen_perfect_hash
(\
%module_number);
94 die "$0: no hash found\n";
97 verify_hash_table
(\
%module_number, \
@hashinfo);
98 my ($n, $sv, $g) = @hashinfo;
101 print OUT
"const char * const *nasm_stdmac_find_module(const char *module)\n";
103 print OUT
" static const struct {\n";
104 print OUT
" const char *module;\n";
105 print OUT
" const char * const *macros;\n";
106 print OUT
" } modules[$nmodule] = {\n";
107 foreach $module (@module_list) {
108 printf OUT
" { \"%s\", nasm_stdmac+%d },\n",
109 $module, $module_index{$module};
113 # Put a large value in unused slots. This makes it extremely unlikely
114 # that any combination that involves unused slot will pass the range test.
115 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
116 print OUT
"#define UNUSED 16383\n";
118 print OUT
" static const int16_t hash1[$n] = {\n";
119 for ($i = 0; $i < $n; $i++) {
120 my $h = ${$g}[$i*2+0];
121 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
125 print OUT
" static const int16_t hash2[$n] = {\n";
126 for ($i = 0; $i < $n; $i++) {
127 my $h = ${$g}[$i*2+1];
128 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
132 print OUT
" uint32_t k1, k2;\n";
133 print OUT
" uint64_t crc;\n";
134 # For correct overflow behavior, "ix" should be unsigned of the same
135 # width as the hash arrays.
136 print OUT
" uint16_t ix;\n";
139 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), module);\n",
141 print OUT
" k1 = (uint32_t)crc;\n";
142 print OUT
" k2 = (uint32_t)(crc >> 32);\n";
144 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
145 printf OUT
" if (ix >= %d)\n", scalar(@module_list);
146 print OUT
" return NULL;\n";
148 print OUT
" if (nasm_stricmp(modules[ix].module, module))\n";
149 print OUT
" return NULL;\n";
151 print OUT
" return modules[ix].macros;\n";