Use enumerations where practical to ease debugging
[platform/upstream/nasm.git] / insns.pl
1 #!/usr/bin/perl
2
3 # insns.pl   produce insnsa.c, insnsd.c, insnsi.h, insnsn.c from insns.dat
4 #
5 # The Netwide Assembler is copyright (C) 1996 Simon Tatham and
6 # Julian Hall. All rights reserved. The software is
7 # redistributable under the licence given in the file "Licence"
8 # distributed in the NASM archive.
9
10 print STDERR "Reading insns.dat...\n";
11
12 @args   = ();
13 undef $output;
14 foreach $arg ( @ARGV ) {
15     if ( $arg =~ /^\-/ ) {
16         if  ( $arg =~ /^\-([adin])$/ ) {
17             $output = $1;
18         } else {
19             die "$0: Unknown option: ${arg}\n";
20         }
21     } else {
22         push (@args, $arg);
23     }
24 }
25
26 $fname = "insns.dat" unless $fname = $args[0];
27 open (F, $fname) || die "unable to open $fname";
28
29 $line = 0;
30 $insns = 0;
31 while (<F>) {
32   $line++;
33   next if /^\s*;/;   # comments
34   chomp;
35   split;
36   next if $#_ == -1; # blank lines
37   (warn "line $line does not contain four fields\n"), next if $#_ != 3;
38   ($formatted, $nd) = &format(@_);
39   if ($formatted) {
40     $insns++;
41     $aname = "aa_$_[0]";
42     push @$aname, $formatted;
43   }
44   if ( $_[0] =~ /cc$/ ) {
45       # Conditional instruction
46       $k_opcodes_cc{$_[0]}++;
47   } else {
48       # Unconditional instruction
49       $k_opcodes{$_[0]}++;
50   }
51   if ($formatted && !$nd) {
52     push @big, $formatted;
53     foreach $i (&startbyte($_[2])) {
54       $aname = sprintf "dd_%02X",$i;
55       push @$aname, $#big;
56     }
57   }
58 }
59
60 close F;
61
62 @opcodes    = sort keys(%k_opcodes);
63 @opcodes_cc = sort keys(%k_opcodes_cc);
64
65 if ( !defined($output) || $output eq 'a' ) {
66     print STDERR "Writing insnsa.c...\n";
67     
68     open A, ">insnsa.c";
69     
70     print A "/* This file auto-generated from insns.dat by insns.pl" .
71         " - don't edit it */\n\n";
72     print A "#include \"nasm.h\"\n";
73     print A "#include \"insns.h\"\n";
74     print A "\n";
75     
76     foreach $i (@opcodes, @opcodes_cc) {
77         print A "static const struct itemplate instrux_${i}[] = {\n";
78         $aname = "aa_$i";
79         foreach $j (@$aname) {
80             print A "    $j\n";
81         }
82         print A "    ITEMPLATE_END\n};\n\n";
83     }
84     print A "const struct itemplate * const nasm_instructions[] = {\n";
85     foreach $i (@opcodes, @opcodes_cc) {
86         print A "    instrux_${i},\n";
87     }
88     print A "};\n";
89     
90     close A;
91 }
92
93 if ( !defined($output) || $output eq 'd' ) {
94     print STDERR "Writing insnsd.c...\n";
95     
96     open D, ">insnsd.c";
97     
98     print D "/* This file auto-generated from insns.dat by insns.pl" .
99         " - don't edit it */\n\n";
100     print D "#include \"nasm.h\"\n";
101     print D "#include \"insns.h\"\n";
102     print D "\n";
103     
104     print D "static const struct itemplate instrux[] = {\n";
105     foreach $j (@big) {
106         print D "    $j\n";
107     }
108         print D "    ITEMPLATE_END\n};\n\n";
109     
110     for ($c=0; $c<256; $c++) {
111         $h = sprintf "%02X", $c;
112         print D "static const struct itemplate * const itable_${h}[] = {\n";
113         $aname = "dd_$h";
114         foreach $j (@$aname) {
115             print D "    instrux + $j,\n";
116         }
117         print D "    NULL\n};\n\n";
118     }
119     
120     print D "const struct itemplate * const * const itable[] = {\n";
121     for ($c=0; $c<256; $c++) {
122         printf D "    itable_%02X,\n", $c;
123     }
124     print D "};\n";
125     
126     close D;
127 }
128
129 if ( !defined($output) || $output eq 'i' ) {
130     print STDERR "Writing insnsi.h...\n";
131     
132     open I, ">insnsi.h";
133     
134     print I "/* This file is auto-generated from insns.dat by insns.pl" .
135         " - don't edit it */\n\n";
136     print I "/* This file in included by nasm.h */\n\n";
137     
138     print I "/* Instruction names */\n\n";
139     print I "#ifndef NASM_INSNSI_H\n";
140     print I "#define NASM_INSNSI_H 1\n\n";
141     print I "enum opcode {\n";
142     $maxlen = 0;
143     foreach $i (@opcodes, @opcodes_cc) {
144         print I "\tI_${i},\n";
145         $len = length($i);
146         $len++ if ( $i =~ /cc$/ );      # Condition codes can be 3 characters long
147         $maxlen = $len if ( $len > $maxlen );
148     }
149     print I "\tI_none = -1\n";
150     print I "\n};\n\n";
151     print I "#define MAX_INSLEN ", $maxlen, "\n\n";
152     print I "#endif /* NASM_INSNSI_H */\n";
153     
154     close I;
155 }
156
157 if ( !defined($output) || $output eq 'n' ) {
158     print STDERR "Writing insnsn.c...\n";
159     
160     open N, ">insnsn.c";
161     
162     print N "/* This file is auto-generated from insns.dat by insns.pl" .
163         " - don't edit it */\n\n";
164     print N "/* This file in included by names.c */\n\n";
165     
166     print N "static const char * const insn_names[] = {";
167     $first = 1;
168     foreach $i (@opcodes) {
169         print N "," if ( !$first );
170         $first = 0;
171         $ilower = $i;
172         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
173         print N "\n\t\"${ilower}\"";
174     }
175     print N "\n};\n\n";
176     print N "/* Conditional instructions */\n";
177     print N "static const char *icn[] = {";
178     $first = 1;
179     foreach $i (@opcodes_cc) {
180         print N "," if ( !$first );
181         $first = 0;
182         $ilower = $i;
183         $ilower =~ s/cc$//;             # Skip cc suffix
184         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
185         print N "\n\t\"${ilower}\"";
186     }
187     
188     print N "\n};\n\n";
189     print N "/* and the corresponding opcodes */\n";
190     print N "static const enum opcode ico[] = {";
191     $first = 1;
192     foreach $i (@opcodes_cc) {
193         print N "," if ( !$first );
194         $first = 0;
195         print N "\n\tI_$i";
196     }
197     
198     print N "\n};\n";
199     
200     close N;
201 }
202
203 printf STDERR "Done: %d instructions\n", $insns;
204
205 sub format {
206   local ($opcode, $operands, $codes, $flags) = @_;
207   local $num, $nd = 0;
208
209   return (undef, undef) if $operands eq "ignore";
210
211   # format the operands
212   $operands =~ s/:/|colon,/g;
213   $operands =~ s/mem(\d+)/mem|bits$1/g;
214   $operands =~ s/mem/memory/g;
215   $operands =~ s/memory_offs/mem_offs/g;
216   $operands =~ s/imm(\d+)/imm|bits$1/g;
217   $operands =~ s/imm/immediate/g;
218   $operands =~ s/rm(\d+)/regmem|bits$1/g;
219   $num = 3;
220   $operands = '0,0,0', $num = 0 if $operands eq 'void';
221   $operands .= ',0', $num-- while $operands !~ /,.*,/;
222   $operands =~ tr/a-z/A-Z/;
223
224   # format the flags
225   $flags =~ s/,/|IF_/g;
226   $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
227   $flags = "IF_" . $flags;
228
229   ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
230 }
231
232 # Here we determine the range of possible starting bytes for a given
233 # instruction. We need only consider the codes:
234 # \1 \2 \3     mean literal bytes, of course
235 # \4 \5 \6 \7  mean PUSH/POP of segment registers: special case
236 # \10 \11 \12  mean byte plus register value
237 # \17          means byte zero
238 # \330         means byte plus condition code
239 # \0 or \340   mean give up and return empty set
240 sub startbyte {
241   local ($codes) = @_;
242   local $word, @range;
243
244   while (1) {
245     die "couldn't get code in '$codes'" if $codes !~ /^(\\[^\\]+)(\\.*)?$/;
246     $word = $1, $codes = $2;
247     return (hex $1) if $word =~ /^\\[123]$/ && $codes =~ /^\\x(..)/;
248     return (0x07, 0x17, 0x1F) if $word eq "\\4";
249     return (0xA1, 0xA9) if $word eq "\\5";
250     return (0x06, 0x0E, 0x16, 0x1E) if $word eq "\\6";
251     return (0xA0, 0xA8) if $word eq "\\7";
252     $start=hex $1, $r=8, last if $word =~ /^\\1[012]$/ && $codes =~/^\\x(..)/;
253     return (0) if $word eq "\\17";
254     $start=hex $1, $r=16, last if $word =~ /^\\330$/ && $codes =~ /^\\x(..)/;
255     return () if $word eq "\\0" || $word eq "\\340";
256   }
257   @range = ();
258   push @range, $start++ while ($r-- > 0);
259   @range;
260 }