preproc: add support for builtin include modules (%use)
[nasm.git] / macros.pl
blobc22185b9760ff67e1ce49af78894d5f7ddf374d3
1 #!/usr/bin/perl
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.
10 require 'phash.ph';
12 my $fname;
13 my $line = 0;
14 my $index = 0;
15 my $tasm_count = 0;
17 open(OUT,">macros.c") or die "unable to open macros.c\n";
19 print OUT "/*\n";
20 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
21 print OUT " * ", join(' ', @ARGV), "\n";
22 print OUT " */\n";
23 print OUT "\n";
24 print OUT "#include \"tables.h\"\n";
25 print OUT "#include \"nasmlib.h\"\n";
26 print OUT "#include \"hashtbl.h\"\n";
27 print OUT "\n";
28 print OUT "const char * const nasm_stdmac[] = {";
30 my $nmodule = 0;
31 my @module_list = ();
32 my %module_number = ();
33 my %module_index = ();
34 my $module;
36 foreach $fname ( @ARGV ) {
37 open(INPUT,$fname) or die "unable to open $fname\n";
38 print OUT "\n /* From $fname */\n";
39 while (<INPUT>) {
40 $line++;
41 chomp;
42 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
43 $tasm_count = $index;
44 print OUT " /* End of TASM macros */\n";
45 } elsif (m/^USE:\s*(\S+)\s*$/) {
46 $module = $1;
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*(;.*)?$/) {
56 $_ = $1;
57 s/(\s)\s+/$1/g;
58 s/\\/\\\\/g;
59 s/"/\\"/g;
60 if (length > 0) {
61 printf OUT " /* %4d */ \"%s\",\n", $index++, $_;
63 } else {
64 die "$fname:$line: error unterminated quote";
67 close(INPUT);
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);
74 if (!@hashinfo) {
75 die "$0: no hash found\n";
77 # Paranoia...
78 verify_hash_table(\%module_number, \@hashinfo);
79 my ($n, $sv, $g) = @hashinfo;
80 die if ($n & ($n-1));
82 print OUT "const char * const *nasm_stdmac_find_module(const char *module)\n";
83 print OUT "{\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};
92 print OUT " };\n";
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";
104 print OUT " };\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";
111 print OUT " };\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";
118 print OUT "\n";
120 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), module);\n",
121 $$sv[0], $$sv[1];
122 print OUT " k1 = (uint32_t)crc;\n";
123 print OUT " k2 = (uint32_t)(crc >> 32);\n";
124 print OUT "\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";
128 print OUT "\n";
129 print OUT " if (nasm_stricmp(modules[ix].module, module))\n";
130 print OUT " return NULL;\n";
131 print OUT "\n";
132 print OUT " return modules[ix].macros;\n";
133 print OUT "}\n";
135 close(OUT);