Generate a byte array instead of using strings for the byte codes
[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 license given in the file "LICENSE"
8 # distributed in the NASM archive.
9
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);
13
14 print STDERR "Reading insns.dat...\n";
15
16 @args   = ();
17 undef $output;
18 foreach $arg ( @ARGV ) {
19     if ( $arg =~ /^\-/ ) {
20         if  ( $arg =~ /^\-([abdin])$/ ) {
21             $output = $1;
22         } else {
23             die "$0: Unknown option: ${arg}\n";
24         }
25     } else {
26         push (@args, $arg);
27     }
28 }
29
30 $fname = "insns.dat" unless $fname = $args[0];
31 open (F, $fname) || die "unable to open $fname";
32
33 %dinstables = ();
34 @bytecode_list = ();
35
36 $line = 0;
37 $insns = 0;
38 while (<F>) {
39   $line++;
40   next if /^\s*;/;   # comments
41   chomp;
42   split;
43   next if $#_ == -1; # blank lines
44   (warn "line $line does not contain four fields\n"), next if $#_ != 3;
45   ($formatted, $nd) = &format(@_);
46   if ($formatted) {
47     $insns++;
48     $aname = "aa_$_[0]";
49     push @$aname, $formatted;
50   }
51   if ( $_[0] =~ /cc$/ ) {
52       # Conditional instruction
53       $k_opcodes_cc{$_[0]}++;
54   } else {
55       # Unconditional instruction
56       $k_opcodes{$_[0]}++;
57   }
58   if ($formatted && !$nd) {
59     push @big, $formatted;
60     my @sseq = startseq($_[2]);
61     foreach $i (@sseq) {
62         if (!defined($dinstables{$i})) {
63             $dinstables{$i} = [];
64         }
65         push(@{$dinstables{$i}}, $#big);
66     }
67   }
68 }
69
70 close F;
71
72 #
73 # Generate the bytecode array.  At this point, @bytecode_list contains
74 # the full set of bytecodes.
75 #
76
77 # Sort by descending length
78 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
79 @bytecode_array = ();
80 %bytecode_pos = ();
81 $bytecode_next = 0;
82 foreach $bl (@bytecode_list) {
83     my $h = hexstr(@$bl);
84     next if (defined($bytecode_pos{$h}));
85
86     push(@bytecode_array, $bl);
87     while ($h ne '') {
88         $bytecode_pos{$h} = $bytecode_next;
89         $h = substr($h, 2);
90         $bytecode_next++;
91     }
92 }
93 undef @bytecode_list;
94
95 @opcodes    = sort keys(%k_opcodes);
96 @opcodes_cc = sort keys(%k_opcodes_cc);
97
98 if ( !defined($output) || $output eq 'b') {
99     print STDERR "Writing insnsb.c...\n";
100
101     open B, ">insnsb.c";
102     
103     print B "/* This file auto-generated from insns.dat by insns.pl" .
104         " - don't edit it */\n\n";
105
106     print B "#include \"nasm.h\"\n";
107     print B "#include \"insns.h\"\n\n";
108
109     print B "static const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
110
111     $p = 0;
112     foreach $bl (@bytecode_array) {
113         printf B "    /* %4d */ ", $p;
114         foreach $d (@$bl) {
115             printf B "%#o,", $d;
116             $p++;
117         }
118         printf B "\n";
119     }
120     print B "};\n";
121
122     close B;
123 }
124     
125 if ( !defined($output) || $output eq 'a' ) {
126     print STDERR "Writing insnsa.c...\n";
127
128     open A, ">insnsa.c";
129
130     print A "/* This file auto-generated from insns.dat by insns.pl" .
131         " - don't edit it */\n\n";
132
133     print A "#include \"insnsb.c\"\n\n";
134
135     foreach $i (@opcodes, @opcodes_cc) {
136         print A "static const struct itemplate instrux_${i}[] = {\n";
137         $aname = "aa_$i";
138         foreach $j (@$aname) {
139             print A "    ", codesubst($j), "\n";
140         }
141         print A "    ITEMPLATE_END\n};\n\n";
142     }
143     print A "const struct itemplate * const nasm_instructions[] = {\n";
144     foreach $i (@opcodes, @opcodes_cc) {
145         print A "    instrux_${i},\n";
146     }
147     print A "};\n";
148
149     close A;
150 }
151
152 if ( !defined($output) || $output eq 'd' ) {
153     print STDERR "Writing insnsd.c...\n";
154
155     open D, ">insnsd.c";
156
157     print D "/* This file auto-generated from insns.dat by insns.pl" .
158         " - don't edit it */\n\n";
159
160     print D "#include \"insnsb.c\"\n\n";
161
162     print D "static const struct itemplate instrux[] = {\n";
163     $n = 0;
164     foreach $j (@big) {
165         printf D "    /* %4d */ %s\n", $n++, codesubst($j);
166     }
167     print D "};\n";
168
169     foreach $h (sort(keys(%dinstables))) {
170         print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
171         foreach $j (@{$dinstables{$h}}) {
172             print D "    instrux + $j,\n";
173         }
174         print D "};\n";
175     }
176
177     foreach $h (@disasm_prefixes, '') {
178         $is_prefix{$h} = 1;
179         print D "\n";
180         print D "static " unless ($h eq '');
181         print D "const struct disasm_index ";
182         print D ($h eq '') ? 'itable' : "itable_$h";
183         print D "[256] = {\n";
184         for ($c = 0; $c < 256; $c++) {
185             $nn = sprintf("%s%02X", $h, $c);
186             if ($is_prefix{$nn}) {
187                 die "$0: ambiguous decoding of $nn\n"
188                     if (defined($dinstables{$nn}));
189                 printf D "    { itable_%s, -1 },\n", $nn;
190             } elsif (defined($dinstables{$nn})) {
191                 printf D "    { itable_%s, %u },\n",
192                 $nn, scalar(@{$dinstables{$nn}});
193             } else {
194                 printf D "    { NULL, 0 },\n";
195             }
196         }
197     print D "};\n";
198     }
199
200     close D;
201 }
202
203 if ( !defined($output) || $output eq 'i' ) {
204     print STDERR "Writing insnsi.h...\n";
205
206     open I, ">insnsi.h";
207
208     print I "/* This file is auto-generated from insns.dat by insns.pl" .
209         " - don't edit it */\n\n";
210     print I "/* This file in included by nasm.h */\n\n";
211
212     print I "/* Instruction names */\n\n";
213     print I "#ifndef NASM_INSNSI_H\n";
214     print I "#define NASM_INSNSI_H 1\n\n";
215     print I "enum opcode {\n";
216     $maxlen = 0;
217     foreach $i (@opcodes, @opcodes_cc) {
218         print I "\tI_${i},\n";
219         $len = length($i);
220         $len++ if ( $i =~ /cc$/ );      # Condition codes can be 3 characters long
221         $maxlen = $len if ( $len > $maxlen );
222     }
223     print I "\tI_none = -1\n";
224     print I "\n};\n\n";
225     print I "#define MAX_INSLEN ", $maxlen, "\n\n";
226     print I "#endif /* NASM_INSNSI_H */\n";
227
228     close I;
229 }
230
231 if ( !defined($output) || $output eq 'n' ) {
232     print STDERR "Writing insnsn.c...\n";
233
234     open N, ">insnsn.c";
235
236     print N "/* This file is auto-generated from insns.dat by insns.pl" .
237         " - don't edit it */\n\n";
238     print N "/* This file in included by names.c */\n\n";
239
240     print N "static const char * const insn_names[] = {";
241     $first = 1;
242     foreach $i (@opcodes) {
243         print N "," if ( !$first );
244         $first = 0;
245         $ilower = $i;
246         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
247         print N "\n\t\"${ilower}\"";
248     }
249     print N "\n};\n\n";
250     print N "/* Conditional instructions */\n";
251     print N "static const char *icn[] = {";
252     $first = 1;
253     foreach $i (@opcodes_cc) {
254         print N "," if ( !$first );
255         $first = 0;
256         $ilower = $i;
257         $ilower =~ s/cc$//;             # Skip cc suffix
258         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
259         print N "\n\t\"${ilower}\"";
260     }
261
262     print N "\n};\n\n";
263     print N "/* and the corresponding opcodes */\n";
264     print N "static const enum opcode ico[] = {";
265     $first = 1;
266     foreach $i (@opcodes_cc) {
267         print N "," if ( !$first );
268         $first = 0;
269         print N "\n\tI_$i";
270     }
271
272     print N "\n};\n";
273
274     close N;
275 }
276
277 printf STDERR "Done: %d instructions\n", $insns;
278
279 sub format {
280     my ($opcode, $operands, $codes, $flags) = @_;
281     my $num, $nd = 0;
282     my @bytecode;
283
284     return (undef, undef) if $operands eq "ignore";
285
286     # format the operands
287     $operands =~ s/:/|colon,/g;
288     $operands =~ s/mem(\d+)/mem|bits$1/g;
289     $operands =~ s/mem/memory/g;
290     $operands =~ s/memory_offs/mem_offs/g;
291     $operands =~ s/imm(\d+)/imm|bits$1/g;
292     $operands =~ s/imm/immediate/g;
293     $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
294     $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
295     $operands =~ s/\=([0-9]+)/same_as|$1/g;
296     if ($operands eq 'void') {
297         @ops = ();
298     } else {
299         @ops = split(/\,/, $operands);
300     }
301     $num = scalar(@ops);
302     while (scalar(@ops) < 4) {
303         push(@ops, '0');
304     }
305     $operands = join(',', @ops);
306     $operands =~ tr/a-z/A-Z/;
307
308     # format the flags
309     $flags =~ s/,/|IF_/g;
310     $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
311     $flags = "IF_" . $flags;
312
313     @bytecode = (decodify($codes), 0);
314     push(@bytecode_list, [@bytecode]);
315     $codes = hexstr(@bytecode);
316
317     ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
318 }
319
320 #
321 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
322 # offset into nasm_bytecodes
323 #
324 sub codesubst($) {
325     my($s) = @_;
326     my $n;
327
328     while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
329         my $pos = $bytecode_pos{$1};
330         if (!defined($pos)) {
331             die "$0: no position assigned to byte code $1\n";
332         }
333         $s = $` . "nasm_bytecodes+${pos}" . "$'";
334     }
335     return $s;
336 }
337
338 sub addprefix ($@) {
339     my ($prefix, @list) = @_;
340     my $x;
341     my @l = ();
342
343     foreach $x (@list) {
344         push(@l, sprintf("%s%02X", $prefix, $x));
345     }
346
347     return @l;
348 }
349
350 #
351 # Turn a code string into a sequence of bytes
352 #
353 sub decodify($) {
354   # Although these are C-syntax strings, by convention they should have
355   # only octal escapes (for directives) and hexadecimal escapes
356   # (for verbatim bytes)
357     my($codestr) = @_;
358     my $c = $codestr;
359     my @codes = ();
360
361     while ($c ne '') {
362         if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
363             push(@codes, hex $1);
364             $c = $2;
365             next;
366         } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
367             push(@codes, oct $1);
368             $c = $2;
369             next;
370         } else {
371             die "$0: unknown code format in \"$codestr\"\n";
372         }
373     }
374
375     return @codes;
376 }
377
378 # Turn a numeric list into a hex string
379 sub hexstr(@) {
380     my $s = '';
381     my $c;
382
383     foreach $c (@_) {
384         $s .= sprintf("%02X", $c);
385     }
386     return $s;
387 }
388
389 # Here we determine the range of possible starting bytes for a given
390 # instruction. We need only consider the codes:
391 # \1 \2 \3     mean literal bytes, of course
392 # \4 \5 \6 \7  mean PUSH/POP of segment registers: special case
393 # \1[0123]     mean byte plus register value
394 # \170         means byte zero
395 # \330         means byte plus condition code
396 # \0 or \340   mean give up and return empty set
397 sub startseq($) {
398   my ($codestr) = @_;
399   my $word, @range;
400   my @codes = ();
401   my $c = $codestr;
402   my $c0, $c1, $i;
403   my $prefix = '';
404
405   @codes = decodify($codestr);
406
407   while ($c0 = shift(@codes)) {
408       $c1 = $codes[0];
409       if ($c0 == 01 || $c0 == 02 || $c0 == 03 || $c0 == 0170) {
410           # Fixed byte string
411           my $fbs = $prefix;
412           while (1) {
413               if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
414                   while ($c0--) {
415                       $fbs .= sprintf("%02X", shift(@codes));
416                   }
417               } elsif ($c0 == 0170) {
418                   $fbs .= '00';
419               } else {
420                   last;
421               }
422               $c0 = shift(@codes);
423           }
424
425           foreach $pfx (@disasm_prefixes) {
426               if (substr($fbs, 0, length($pfx)) eq $pfx) {
427                   $prefix = $pfx;
428                   $fbs = substr($fbs, length($pfx));
429                   last;
430               }
431           }
432
433           if ($fbs ne '') {
434               return ($prefix.substr($fbs,0,2));
435           }
436
437           unshift(@codes, $c0);
438       } elsif ($c0 == 04) {
439           return addprefix($prefix, 0x07, 0x17, 0x1F);
440       } elsif ($c0 == 05) {
441           return addprefix($prefix, 0xA1, 0xA9);
442       } elsif ($c0 == 06) {
443           return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
444       } elsif ($c0 == 07) {
445           return addprefix($prefix, 0xA0, 0xA8);
446       } elsif ($c0 >= 010 && $c0 <= 013) {
447           return addprefix($prefix, $c1..($c1+7));
448       } elsif (($c0 & ~013) == 0144) {
449           return addprefix($prefix, $c1, $c1|2);
450       } elsif ($c0 == 0330) {
451           return addprefix($prefix, $c1..($c1+15));
452       } elsif ($c0 == 0 || $c0 == 0340) {
453           return $prefix;
454       } elsif (($c0 & ~3) == 0260 || $c0 == 270) {
455           shift(@codes);
456           shift(@codes);
457       } elsif ($c0 == 0172) {
458           shift(@codes);
459       } else {
460           # We really need to be able to distinguish "forbidden"
461           # and "ignorable" codes here
462       }
463   }
464   return $prefix;
465 }