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.
17 open(OUT
,">macros.c") or die "unable to open macros.c\n";
20 print OUT
" * Do not edit - this file auto-generated by macros.pl from:\n";
21 print OUT
" * ", join(' ', @ARGV), "\n";
24 print OUT
"#include \"tables.h\"\n";
25 print OUT
"#include \"nasmlib.h\"\n";
26 print OUT
"#include \"hashtbl.h\"\n";
28 print OUT
"const char * const nasm_stdmac[] = {";
32 my %module_number = ();
33 my %module_index = ();
36 foreach $fname ( @ARGV ) {
37 open(INPUT
,$fname) or die "unable to open $fname\n";
38 print OUT
"\n /* From $fname */\n";
42 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
44 print OUT
" /* End of TASM macros */\n";
45 } elsif (m/^USE:\s*(\S+)\s*$/) {
47 if (defined($module_number{$module})) {
48 die "$0: $fname: duplicate module: $module\n";
50 printf OUT
" /* %4d */ NULL,\n", $index++;
51 print OUT
" /* %use $module */\n";
52 push(@module_list, $module);
53 $module_number{$module} = $nmodule++;
54 $module_index{$module} = $index;
55 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
61 printf OUT
" /* %4d */ \"%s\",\n", $index++, $_;
64 die "$fname:$line: error unterminated quote";
69 printf OUT
" /* %4d */ NULL\n};\n\n", $index++;
70 print OUT
"const char * const * const nasm_stdmac_after_tasm = ",
71 "&nasm_stdmac[$tasm_count];\n\n";
73 my @hashinfo = gen_perfect_hash
(\
%module_number);
75 die "$0: no hash found\n";
78 verify_hash_table
(\
%module_number, \
@hashinfo);
79 my ($n, $sv, $g) = @hashinfo;
82 print OUT
"const char * const *nasm_stdmac_find_module(const char *module)\n";
84 print OUT
" static const struct {\n";
85 print OUT
" const char *module;\n";
86 print OUT
" const char * const *macros;\n";
87 print OUT
" } modules[$nmodule] = {\n";
88 foreach $module (@module_list) {
89 printf OUT
" { \"%s\", nasm_stdmac+%d },\n",
90 $module, $module_index{$module};
94 # Put a large value in unused slots. This makes it extremely unlikely
95 # that any combination that involves unused slot will pass the range test.
96 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
97 print OUT
"#define UNUSED 16383\n";
99 print OUT
" static const int16_t hash1[$n] = {\n";
100 for ($i = 0; $i < $n; $i++) {
101 my $h = ${$g}[$i*2+0];
102 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
106 print OUT
" static const int16_t hash2[$n] = {\n";
107 for ($i = 0; $i < $n; $i++) {
108 my $h = ${$g}[$i*2+1];
109 print OUT
" ", defined($h) ?
$h : 'UNUSED', ",\n";
113 print OUT
" uint32_t k1, k2;\n";
114 print OUT
" uint64_t crc;\n";
115 # For correct overflow behavior, "ix" should be unsigned of the same
116 # width as the hash arrays.
117 print OUT
" uint16_t ix;\n";
120 printf OUT
" crc = crc64i(UINT64_C(0x%08x%08x), module);\n",
122 print OUT
" k1 = (uint32_t)crc;\n";
123 print OUT
" k2 = (uint32_t)(crc >> 32);\n";
125 printf OUT
" ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
126 printf OUT
" if (ix >= %d)\n", scalar(@module_list);
127 print OUT
" return NULL;\n";
129 print OUT
" if (nasm_stricmp(modules[ix].module, module))\n";
130 print OUT
" return NULL;\n";
132 print OUT
" return modules[ix].macros;\n";