Somewhat more clever way to generate the %use guard macros
[nasm.git] / macros.pl
blob5429a5eaea8d9a9a3de7d7b9ab32ffe085097d3b
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';
11 require 'pptok.ph';
13 my $fname;
14 my $line = 0;
15 my $index = 0;
16 my $tasm_count = 0;
19 # Generate macros.c
21 open(OUT,">macros.c") or die "unable to open macros.c\n";
23 print OUT "/*\n";
24 print OUT " * Do not edit - this file auto-generated by macros.pl from:\n";
25 print OUT " * ", join(' ', @ARGV), "\n";
26 print OUT " */\n";
27 print OUT "\n";
28 print OUT "#include \"tables.h\"\n";
29 print OUT "#include \"nasmlib.h\"\n";
30 print OUT "#include \"hashtbl.h\"\n";
31 print OUT "\n";
32 print OUT "const char * const nasm_stdmac[] = {";
34 my $npkg = 0;
35 my @pkg_list = ();
36 my %pkg_number = ();
37 my %pkg_index = ();
38 my $pkg;
40 foreach $fname ( @ARGV ) {
41 open(INPUT,$fname) or die "unable to open $fname\n";
42 print OUT "\n /* From $fname */\n";
43 while (<INPUT>) {
44 $line++;
45 chomp;
46 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
47 $tasm_count = $index;
48 print OUT " /* End of TASM macros */\n";
49 } elsif (m/^USE:\s*(\S+)\s*$/) {
50 $pkg = $1;
51 if (defined($pkg_number{$pkg})) {
52 die "$0: $fname: duplicate package: $pkg\n";
54 printf OUT " /* %4d */ NULL,\n", $index++;
55 print OUT " /* %use $pkg */\n";
56 push(@pkg_list, $pkg);
57 $pkg_number{$pkg} = $npkg++;
58 $pkg_index{$pkg} = $index;
59 printf OUT " /* %4d */ \"\\x%02x\"\"%s\",\n",
60 $index++, $pptok_hash{'%define'}+128, "__USE_\U$pkg\E__";
61 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
62 my $s1, $s2, $pd, $ws;
63 $s1 = $1;
64 $s1 =~ s/(\s)\s+/$1/g;
65 $s1 =~ s/\\/\\\\/g;
66 $s1 =~ s/"/\\"/g;
67 $s2 = '';
68 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
69 $s2 .= "$'";
70 $pd = $1;
71 $ws = $3;
72 $s1 = $4;
73 if (defined($pptok_hash{$pd}) &&
74 $pptok_hash{$pd} <= 127) {
75 $s2 .= sprintf("\\x%02x\"\"", $pptok_hash{$pd}+128);
76 } else {
77 $s2 .= $pd.$ws;
80 $s2 .= $s1;
81 if (length($s2) > 0) {
82 printf OUT " /* %4d */ \"%s\",\n", $index++, $s2;
84 } else {
85 die "$fname:$line: error unterminated quote";
88 close(INPUT);
90 printf OUT " /* %4d */ NULL\n};\n\n", $index++;
91 print OUT "const char * const * const nasm_stdmac_after_tasm = ",
92 "&nasm_stdmac[$tasm_count];\n\n";
94 my @hashinfo = gen_perfect_hash(\%pkg_number);
95 if (!@hashinfo) {
96 die "$0: no hash found\n";
98 # Paranoia...
99 verify_hash_table(\%pkg_number, \@hashinfo);
100 my ($n, $sv, $g) = @hashinfo;
101 die if ($n & ($n-1));
103 print OUT "const char * const *nasm_stdmac_find_package(const char *package)\n";
104 print OUT "{\n";
105 print OUT " static const struct {\n";
106 print OUT " const char *package;\n";
107 print OUT " const char * const *macros;\n";
108 print OUT " } packages[$npkg] = {\n";
109 foreach $pkg (@pkg_list) {
110 printf OUT " { \"%s\", nasm_stdmac+%d },\n",
111 $pkg, $pkg_index{$pkg};
113 print OUT " };\n";
115 # Put a large value in unused slots. This makes it extremely unlikely
116 # that any combination that involves unused slot will pass the range test.
117 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
118 print OUT "#define UNUSED 16383\n";
120 print OUT " static const int16_t hash1[$n] = {\n";
121 for ($i = 0; $i < $n; $i++) {
122 my $h = ${$g}[$i*2+0];
123 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
125 print OUT " };\n";
127 print OUT " static const int16_t hash2[$n] = {\n";
128 for ($i = 0; $i < $n; $i++) {
129 my $h = ${$g}[$i*2+1];
130 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
132 print OUT " };\n";
134 print OUT " uint32_t k1, k2;\n";
135 print OUT " uint64_t crc;\n";
136 # For correct overflow behavior, "ix" should be unsigned of the same
137 # width as the hash arrays.
138 print OUT " uint16_t ix;\n";
139 print OUT "\n";
141 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
142 $$sv[0], $$sv[1];
143 print OUT " k1 = (uint32_t)crc;\n";
144 print OUT " k2 = (uint32_t)(crc >> 32);\n";
145 print OUT "\n";
146 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
147 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
148 print OUT " return NULL;\n";
149 print OUT "\n";
150 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
151 print OUT " return NULL;\n";
152 print OUT "\n";
153 print OUT " return packages[ix].macros;\n";
154 print OUT "}\n";
156 close(OUT);