macros.c: compress by tokenizing macro directives
[nasm/autotest.git] / macros.pl
blob3474f6825cac5302a281937f348c776297177d9a
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 $nmodule = 0;
35 my @module_list = ();
36 my %module_number = ();
37 my %module_index = ();
38 my $module;
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 $module = $1;
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;
61 $s1 = $1;
62 $s1 =~ s/(\s)\s+/$1/g;
63 $s1 =~ s/\\/\\\\/g;
64 $s1 =~ s/"/\\"/g;
65 $s2 = '';
66 print $s1, ":";
67 while ($s1 =~ /^(.*)(\%[a-zA-Z_][a-zA-Z0-9_]*)(\s*)(.*)$/) {
68 $s2 .= $1;
69 $pd = $2;
70 $ws = $3;
71 $s1 = $4;
72 print " ", $pd;
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 print "\n";
81 $s2 .= $s1;
82 if (length($s2) > 0) {
83 printf OUT " /* %4d */ \"%s\",\n", $index++, $s2;
85 } else {
86 die "$fname:$line: error unterminated quote";
89 close(INPUT);
91 printf OUT " /* %4d */ NULL\n};\n\n", $index++;
92 print OUT "const char * const * const nasm_stdmac_after_tasm = ",
93 "&nasm_stdmac[$tasm_count];\n\n";
95 my @hashinfo = gen_perfect_hash(\%module_number);
96 if (!@hashinfo) {
97 die "$0: no hash found\n";
99 # Paranoia...
100 verify_hash_table(\%module_number, \@hashinfo);
101 my ($n, $sv, $g) = @hashinfo;
102 die if ($n & ($n-1));
104 print OUT "const char * const *nasm_stdmac_find_module(const char *module)\n";
105 print OUT "{\n";
106 print OUT " static const struct {\n";
107 print OUT " const char *module;\n";
108 print OUT " const char * const *macros;\n";
109 print OUT " } modules[$nmodule] = {\n";
110 foreach $module (@module_list) {
111 printf OUT " { \"%s\", nasm_stdmac+%d },\n",
112 $module, $module_index{$module};
114 print OUT " };\n";
116 # Put a large value in unused slots. This makes it extremely unlikely
117 # that any combination that involves unused slot will pass the range test.
118 # This speeds up rejection of unrecognized tokens, i.e. identifiers.
119 print OUT "#define UNUSED 16383\n";
121 print OUT " static const int16_t hash1[$n] = {\n";
122 for ($i = 0; $i < $n; $i++) {
123 my $h = ${$g}[$i*2+0];
124 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
126 print OUT " };\n";
128 print OUT " static const int16_t hash2[$n] = {\n";
129 for ($i = 0; $i < $n; $i++) {
130 my $h = ${$g}[$i*2+1];
131 print OUT " ", defined($h) ? $h : 'UNUSED', ",\n";
133 print OUT " };\n";
135 print OUT " uint32_t k1, k2;\n";
136 print OUT " uint64_t crc;\n";
137 # For correct overflow behavior, "ix" should be unsigned of the same
138 # width as the hash arrays.
139 print OUT " uint16_t ix;\n";
140 print OUT "\n";
142 printf OUT " crc = crc64i(UINT64_C(0x%08x%08x), module);\n",
143 $$sv[0], $$sv[1];
144 print OUT " k1 = (uint32_t)crc;\n";
145 print OUT " k2 = (uint32_t)(crc >> 32);\n";
146 print OUT "\n";
147 printf OUT " ix = hash1[k1 & 0x%x] + hash2[k2 & 0x%x];\n", $n-1, $n-1;
148 printf OUT " if (ix >= %d)\n", scalar(@module_list);
149 print OUT " return NULL;\n";
150 print OUT "\n";
151 print OUT " if (nasm_stricmp(modules[ix].module, module))\n";
152 print OUT " return NULL;\n";
153 print OUT "\n";
154 print OUT " return modules[ix].macros;\n";
155 print OUT "}\n";
157 close(OUT);