Factor out string decoder in insns.pl
[nasm.git] / insns.pl
blob03c840aa92d223c72c3c47129fb250d5be0fea73
1 #!/usr/bin/perl
3 # insns.pl produce insnsa.c, insnsd.c, insnsi.h, insnsn.c from insns.dat
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 # Opcode prefixes which need their own opcode tables
11 # LONGER PREFIXES FIRST!
12 @disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
14 print STDERR "Reading insns.dat...\n";
16 @args = ();
17 undef $output;
18 foreach $arg ( @ARGV ) {
19 if ( $arg =~ /^\-/ ) {
20 if ( $arg =~ /^\-([adin])$/ ) {
21 $output = $1;
22 } else {
23 die "$0: Unknown option: ${arg}\n";
25 } else {
26 push (@args, $arg);
30 $fname = "insns.dat" unless $fname = $args[0];
31 open (F, $fname) || die "unable to open $fname";
33 %dinstables = ();
35 $line = 0;
36 $insns = 0;
37 while (<F>) {
38 $line++;
39 next if /^\s*;/; # comments
40 chomp;
41 split;
42 next if $#_ == -1; # blank lines
43 (warn "line $line does not contain four fields\n"), next if $#_ != 3;
44 ($formatted, $nd) = &format(@_);
45 if ($formatted) {
46 $insns++;
47 $aname = "aa_$_[0]";
48 push @$aname, $formatted;
50 if ( $_[0] =~ /cc$/ ) {
51 # Conditional instruction
52 $k_opcodes_cc{$_[0]}++;
53 } else {
54 # Unconditional instruction
55 $k_opcodes{$_[0]}++;
57 if ($formatted && !$nd) {
58 push @big, $formatted;
59 my @sseq = startseq($_[2]);
60 foreach $i (@sseq) {
61 if (!defined($dinstables{$i})) {
62 $dinstables{$i} = [];
64 push(@{$dinstables{$i}}, $#big);
69 close F;
71 @opcodes = sort keys(%k_opcodes);
72 @opcodes_cc = sort keys(%k_opcodes_cc);
74 if ( !defined($output) || $output eq 'a' ) {
75 print STDERR "Writing insnsa.c...\n";
77 open A, ">insnsa.c";
79 print A "/* This file auto-generated from insns.dat by insns.pl" .
80 " - don't edit it */\n\n";
81 print A "#include \"nasm.h\"\n";
82 print A "#include \"insns.h\"\n";
83 print A "\n";
85 foreach $i (@opcodes, @opcodes_cc) {
86 print A "static const struct itemplate instrux_${i}[] = {\n";
87 $aname = "aa_$i";
88 foreach $j (@$aname) {
89 print A " $j\n";
91 print A " ITEMPLATE_END\n};\n\n";
93 print A "const struct itemplate * const nasm_instructions[] = {\n";
94 foreach $i (@opcodes, @opcodes_cc) {
95 print A " instrux_${i},\n";
97 print A "};\n";
99 close A;
102 if ( !defined($output) || $output eq 'd' ) {
103 print STDERR "Writing insnsd.c...\n";
105 open D, ">insnsd.c";
107 print D "/* This file auto-generated from insns.dat by insns.pl" .
108 " - don't edit it */\n\n";
109 print D "#include \"nasm.h\"\n";
110 print D "#include \"insns.h\"\n";
111 print D "\n";
113 print D "static const struct itemplate instrux[] = {\n";
114 $n = 0;
115 foreach $j (@big) {
116 printf D " /* %4d */ %s\n", $n++, $j;
118 print D "};\n";
120 foreach $h (sort(keys(%dinstables))) {
121 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
122 foreach $j (@{$dinstables{$h}}) {
123 print D " instrux + $j,\n";
125 print D "};\n";
128 foreach $h (@disasm_prefixes, '') {
129 $is_prefix{$h} = 1;
130 print D "\n";
131 print D "static " unless ($h eq '');
132 print D "const struct disasm_index ";
133 print D ($h eq '') ? 'itable' : "itable_$h";
134 print D "[256] = {\n";
135 for ($c = 0; $c < 256; $c++) {
136 $nn = sprintf("%s%02X", $h, $c);
137 if ($is_prefix{$nn}) {
138 die "$0: ambiguous decoding of $nn\n"
139 if (defined($dinstables{$nn}));
140 printf D " { itable_%s, -1 },\n", $nn;
141 } elsif (defined($dinstables{$nn})) {
142 printf D " { itable_%s, %u },\n",
143 $nn, scalar(@{$dinstables{$nn}});
144 } else {
145 printf D " { NULL, 0 },\n";
148 print D "};\n";
151 close D;
154 if ( !defined($output) || $output eq 'i' ) {
155 print STDERR "Writing insnsi.h...\n";
157 open I, ">insnsi.h";
159 print I "/* This file is auto-generated from insns.dat by insns.pl" .
160 " - don't edit it */\n\n";
161 print I "/* This file in included by nasm.h */\n\n";
163 print I "/* Instruction names */\n\n";
164 print I "#ifndef NASM_INSNSI_H\n";
165 print I "#define NASM_INSNSI_H 1\n\n";
166 print I "enum opcode {\n";
167 $maxlen = 0;
168 foreach $i (@opcodes, @opcodes_cc) {
169 print I "\tI_${i},\n";
170 $len = length($i);
171 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
172 $maxlen = $len if ( $len > $maxlen );
174 print I "\tI_none = -1\n";
175 print I "\n};\n\n";
176 print I "#define MAX_INSLEN ", $maxlen, "\n\n";
177 print I "#endif /* NASM_INSNSI_H */\n";
179 close I;
182 if ( !defined($output) || $output eq 'n' ) {
183 print STDERR "Writing insnsn.c...\n";
185 open N, ">insnsn.c";
187 print N "/* This file is auto-generated from insns.dat by insns.pl" .
188 " - don't edit it */\n\n";
189 print N "/* This file in included by names.c */\n\n";
191 print N "static const char * const insn_names[] = {";
192 $first = 1;
193 foreach $i (@opcodes) {
194 print N "," if ( !$first );
195 $first = 0;
196 $ilower = $i;
197 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
198 print N "\n\t\"${ilower}\"";
200 print N "\n};\n\n";
201 print N "/* Conditional instructions */\n";
202 print N "static const char *icn[] = {";
203 $first = 1;
204 foreach $i (@opcodes_cc) {
205 print N "," if ( !$first );
206 $first = 0;
207 $ilower = $i;
208 $ilower =~ s/cc$//; # Skip cc suffix
209 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
210 print N "\n\t\"${ilower}\"";
213 print N "\n};\n\n";
214 print N "/* and the corresponding opcodes */\n";
215 print N "static const enum opcode ico[] = {";
216 $first = 1;
217 foreach $i (@opcodes_cc) {
218 print N "," if ( !$first );
219 $first = 0;
220 print N "\n\tI_$i";
223 print N "\n};\n";
225 close N;
228 printf STDERR "Done: %d instructions\n", $insns;
230 sub format {
231 my ($opcode, $operands, $codes, $flags) = @_;
232 my $num, $nd = 0;
234 return (undef, undef) if $operands eq "ignore";
236 # format the operands
237 $operands =~ s/:/|colon,/g;
238 $operands =~ s/mem(\d+)/mem|bits$1/g;
239 $operands =~ s/mem/memory/g;
240 $operands =~ s/memory_offs/mem_offs/g;
241 $operands =~ s/imm(\d+)/imm|bits$1/g;
242 $operands =~ s/imm/immediate/g;
243 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
244 $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
245 $operands =~ s/\=([0-9]+)/same_as|$1/g;
246 if ($operands eq 'void') {
247 @ops = ();
248 } else {
249 @ops = split(/\,/, $operands);
251 $num = scalar(@ops);
252 while (scalar(@ops) < 4) {
253 push(@ops, '0');
255 $operands = join(',', @ops);
256 $operands =~ tr/a-z/A-Z/;
258 # format the flags
259 $flags =~ s/,/|IF_/g;
260 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
261 $flags = "IF_" . $flags;
263 ("{I_$opcode, $num, {$operands}, \"$codes\", $flags},", $nd);
266 sub addprefix ($@) {
267 my ($prefix, @list) = @_;
268 my $x;
269 my @l = ();
271 foreach $x (@list) {
272 push(@l, sprintf("%s%02X", $prefix, $x));
275 return @l;
279 # Turn a code string into a sequence of bytes
281 sub decodify($) {
282 # Although these are C-syntax strings, by convention they should have
283 # only octal escapes (for directives) and hexadecimal escapes
284 # (for verbatim bytes)
285 my($codestr) = @_;
286 my $c = $codestr;
287 my @codes = ();
289 while ($c ne '') {
290 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
291 push(@codes, hex $1);
292 $c = $2;
293 next;
294 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
295 push(@codes, oct $1);
296 $c = $2;
297 next;
298 } else {
299 die "$0: unknown code format in \"$codestr\"\n";
303 return @codes;
306 # Here we determine the range of possible starting bytes for a given
307 # instruction. We need only consider the codes:
308 # \1 \2 \3 mean literal bytes, of course
309 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
310 # \1[0123] mean byte plus register value
311 # \170 means byte zero
312 # \330 means byte plus condition code
313 # \0 or \340 mean give up and return empty set
314 sub startseq($) {
315 my ($codestr) = @_;
316 my $word, @range;
317 my @codes = ();
318 my $c = $codestr;
319 my $c0, $c1, $i;
320 my $prefix = '';
322 @codes = decodify($codestr);
324 while ($c0 = shift(@codes)) {
325 $c1 = $codes[0];
326 if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
327 # Fixed byte string
328 my $fbs = $prefix;
329 while (1) {
330 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
331 while ($c0--) {
332 $fbs .= sprintf("%02X", shift(@codes));
334 } elsif ($c0 == 0170) {
335 $fbs .= '00';
336 } else {
337 last;
339 $c0 = shift(@codes);
342 foreach $pfx (@disasm_prefixes) {
343 if (substr($fbs, 0, length($pfx)) eq $pfx) {
344 $prefix = $pfx;
345 $fbs = substr($fbs, length($pfx));
346 last;
350 if ($fbs ne '') {
351 return ($prefix.substr($fbs,0,2));
354 unshift(@codes, $c0);
355 } elsif ($c0 == 04) {
356 return addprefix($prefix, 0x07, 0x17, 0x1F);
357 } elsif ($c0 == 05) {
358 return addprefix($prefix, 0xA1, 0xA9);
359 } elsif ($c0 == 06) {
360 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
361 } elsif ($c0 == 07) {
362 return addprefix($prefix, 0xA0, 0xA8);
363 } elsif ($c0 >= 010 && $c0 <= 013) {
364 return addprefix($prefix, $c1..($c1+7));
365 } elsif (($c0 & ~013) == 0144) {
366 return addprefix($prefix, $c1, $c1|2);
367 } elsif ($c0 == 0330) {
368 return addprefix($prefix, $c1..($c1+15));
369 } elsif ($c0 == 0 || $c0 == 0340) {
370 return $prefix;
371 } elsif (($c0 & ~3) == 0260 || $c0 == 270) {
372 shift(@codes);
373 shift(@codes);
374 } elsif ($c0 == 0172) {
375 shift(@codes);
376 } else {
377 # We really need to be able to distinguish "forbidden"
378 # and "ignorable" codes here
381 return $prefix;