Move the output format macros into the macros.pl mechanism
[nasm/autotest.git] / macros.pl
blob18c7759692e7af3f69888a763612268b0166551f
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;
38 my @out_list = ();
39 my $outfmt;
40 my $lastname;
42 foreach $fname ( @ARGV ) {
43 open(INPUT,$fname) or die "unable to open $fname\n";
44 while (<INPUT>) {
45 $line++;
46 chomp;
47 if (m/^\s*\*END\*TASM\*MACROS\*\s*$/) {
48 $tasm_count = $index;
49 print OUT " /* End of TASM macros */\n";
50 } elsif (m/^OUT:\s*(.*\S)\s*$/) {
51 undef $pkg;
52 my @out_alias = split(/\s+/, $1);
53 printf OUT " /* %4d */ NULL\n", $index++;
54 print OUT "};\n";
55 $index = 0;
56 printf OUT "const char * const %s_stdmac[] = {\n", $out_alias[0];
57 print OUT " /* From $fname */\n";
58 $lastname = $fname;
59 push(@out_list, $out_alias[0]);
60 $out_index{$out_alias[0]} = $index;
61 } elsif (m/^USE:\s*(\S+)\s*$/) {
62 $pkg = $1;
63 if (defined($pkg_number{$pkg})) {
64 die "$0: $fname: duplicate package: $pkg\n";
66 printf OUT " /* %4d */ NULL,\n", $index++;
67 print OUT "};\n";
68 $index = 0;
69 printf OUT "static const char * const nasm_stdmac_%s[] = {\n", $pkg;
70 print OUT " /* From $fname */\n";
71 $lastname = $fname;
72 push(@pkg_list, $pkg);
73 $pkg_number{$pkg} = $npkg++;
74 $pkg_index{$pkg} = $index;
75 printf OUT " /* %4d */ \"\\x%02x\"\"%s\",\n",
76 $index++, $pptok_hash{'%define'}+128, "__USE_\U$pkg\E__";
77 } elsif (m/^\s*((\s*([^\"\';\s]+|\"[^\"]*\"|\'[^\']*\'))*)\s*(;.*)?$/) {
78 my $s1, $s2, $pd, $ws;
79 $s1 = $1;
80 $s1 =~ s/(\s)\s+/$1/g;
81 $s1 =~ s/\\/\\\\/g;
82 $s1 =~ s/"/\\"/g;
83 $s2 = '';
84 while ($s1 =~ /(\%[a-zA-Z_][a-zA-Z0-9_]*)((\s+)(.*)|)$/) {
85 $s2 .= "$'";
86 $pd = $1;
87 $ws = $3;
88 $s1 = $4;
89 if (defined($pptok_hash{$pd}) &&
90 $pptok_hash{$pd} <= 127) {
91 $s2 .= sprintf("\\x%02x\"\"", $pptok_hash{$pd}+128);
92 } else {
93 $s2 .= $pd.$ws;
96 $s2 .= $s1;
97 if (length($s2) > 0) {
98 if ($lastname ne $fname) {
99 print OUT "\n /* From $fname */\n";
100 $lastname = $fname;
102 printf OUT " /* %4d */ \"%s\",\n", $index++, $s2;
104 } else {
105 die "$fname:$line: error unterminated quote";
108 close(INPUT);
110 printf OUT " /* %4d */ NULL\n};\n\n", $index++;
111 print OUT "const char * const * const nasm_stdmac_after_tasm = ",
112 "&nasm_stdmac[$tasm_count];\n\n";
114 my @hashinfo = gen_perfect_hash(\%pkg_number);
115 if (!@hashinfo) {
116 die "$0: no hash found\n";
118 # Paranoia...
119 verify_hash_table(\%pkg_number, \@hashinfo);
120 my ($n, $sv, $g) = @hashinfo;
121 die if ($n & ($n-1));
123 print OUT "const char * const *nasm_stdmac_find_package(const char *package)\n";
124 print OUT "{\n";
125 print OUT " static const struct {\n";
126 print OUT " const char *package;\n";
127 print OUT " const char * const *macros;\n";
128 print OUT " } packages[$npkg] = {\n";
129 foreach $pkg (@pkg_list) {
130 printf OUT " { \"%s\", nasm_stdmac_%s },\n",
131 $pkg, $pkg;
133 print OUT " };\n";
135 # Put a large value in unused slots. This makes it extremely unlikely
136 # that any combination that involves unused slot will pass the range test.
137 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
138 print OUT "#define UNUSED 16383\n";
140 print OUT " static const int16_t hash1[$n] = {\n";
141 for ($i = 0; $i < $n; $i++) {
142 my $h = ${$g}[$i*2+0];
143 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
145 print OUT " };\n";
147 print OUT " static const int16_t hash2[$n] = {\n";
148 for ($i = 0; $i < $n; $i++) {
149 my $h = ${$g}[$i*2+1];
150 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
152 print OUT " };\n";
154 print OUT " uint32_t k1, k2;\n";
155 print OUT " uint64_t crc;\n";
156 # For correct overflow behavior, "ix" should be unsigned of the same
157 # width as the hash arrays.
158 print OUT " uint16_t ix;\n";
159 print OUT "\n";
161 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), package);\n",
162 $$sv[0], $$sv[1];
163 print OUT " k1 = (uint32_t)crc;\n";
164 print OUT " k2 = (uint32_t)(crc >> 32);\n";
165 print OUT "\n";
166 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
167 printf OUT " if (ix >= %d)\n", scalar(@pkg_list);
168 print OUT " return NULL;\n";
169 print OUT "\n";
170 print OUT " if (nasm_stricmp(packages[ix].package, package))\n";
171 print OUT " return NULL;\n";
172 print OUT "\n";
173 print OUT " return packages[ix].macros;\n";
174 print OUT "}\n";
176 close(OUT);