Same some space by introducing shorthand byte codes for SSE prefixes
[nasm/autotest.git] / insns.pl
blobe9dd72f719cd05b3eebf109ac69f1bbdbdedcd67
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 # This should match MAX_OPERANDS from nasm.h
15 $MAX_OPERANDS = 5;
17 print STDERR "Reading insns.dat...\n";
19 @args = ();
20 undef $output;
21 foreach $arg ( @ARGV ) {
22 if ( $arg =~ /^\-/ ) {
23 if ( $arg =~ /^\-([abdin])$/ ) {
24 $output = $1;
25 } else {
26 die "$0: Unknown option: ${arg}\n";
28 } else {
29 push (@args, $arg);
33 $fname = "insns.dat" unless $fname = $args[0];
34 open (F, $fname) || die "unable to open $fname";
36 %dinstables = ();
37 @bytecode_list = ();
39 $line = 0;
40 $insns = 0;
41 while (<F>) {
42 $line++;
43 chomp;
44 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
46 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
47 warn "line $line does not contain four fields\n";
48 next;
50 @fields = ($1, $2, $3, $4);
51 ($formatted, $nd) = format_insn(@fields);
52 if ($formatted) {
53 $insns++;
54 $aname = "aa_$fields[0]";
55 push @$aname, $formatted;
57 if ( $fields[0] =~ /cc$/ ) {
58 # Conditional instruction
59 $k_opcodes_cc{$fields[0]}++;
60 } else {
61 # Unconditional instruction
62 $k_opcodes{$fields[0]}++;
64 if ($formatted && !$nd) {
65 push @big, $formatted;
66 my @sseq = startseq($fields[2]);
67 foreach $i (@sseq) {
68 if (!defined($dinstables{$i})) {
69 $dinstables{$i} = [];
71 push(@{$dinstables{$i}}, $#big);
76 close F;
79 # Generate the bytecode array. At this point, @bytecode_list contains
80 # the full set of bytecodes.
83 # Sort by descending length
84 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
85 @bytecode_array = ();
86 %bytecode_pos = ();
87 $bytecode_next = 0;
88 foreach $bl (@bytecode_list) {
89 my $h = hexstr(@$bl);
90 next if (defined($bytecode_pos{$h}));
92 push(@bytecode_array, $bl);
93 while ($h ne '') {
94 $bytecode_pos{$h} = $bytecode_next;
95 $h = substr($h, 2);
96 $bytecode_next++;
99 undef @bytecode_list;
101 @opcodes = sort keys(%k_opcodes);
102 @opcodes_cc = sort keys(%k_opcodes_cc);
104 if ( !defined($output) || $output eq 'b') {
105 print STDERR "Writing insnsb.c...\n";
107 open B, ">insnsb.c";
109 print B "/* This file auto-generated from insns.dat by insns.pl" .
110 " - don't edit it */\n\n";
112 print B "#include \"nasm.h\"\n";
113 print B "#include \"insns.h\"\n\n";
115 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
117 $p = 0;
118 foreach $bl (@bytecode_array) {
119 printf B " /* %5d */ ", $p;
120 foreach $d (@$bl) {
121 printf B "%#o,", $d;
122 $p++;
124 printf B "\n";
126 print B "};\n";
128 close B;
131 if ( !defined($output) || $output eq 'a' ) {
132 print STDERR "Writing insnsa.c...\n";
134 open A, ">insnsa.c";
136 print A "/* This file auto-generated from insns.dat by insns.pl" .
137 " - don't edit it */\n\n";
139 print A "#include \"nasm.h\"\n";
140 print A "#include \"insns.h\"\n\n";
142 foreach $i (@opcodes, @opcodes_cc) {
143 print A "static const struct itemplate instrux_${i}[] = {\n";
144 $aname = "aa_$i";
145 foreach $j (@$aname) {
146 print A " ", codesubst($j), "\n";
148 print A " ITEMPLATE_END\n};\n\n";
150 print A "const struct itemplate * const nasm_instructions[] = {\n";
151 foreach $i (@opcodes, @opcodes_cc) {
152 print A " instrux_${i},\n";
154 print A "};\n";
156 close A;
159 if ( !defined($output) || $output eq 'd' ) {
160 print STDERR "Writing insnsd.c...\n";
162 open D, ">insnsd.c";
164 print D "/* This file auto-generated from insns.dat by insns.pl" .
165 " - don't edit it */\n\n";
167 print D "#include \"nasm.h\"\n";
168 print D "#include \"insns.h\"\n\n";
170 print D "static const struct itemplate instrux[] = {\n";
171 $n = 0;
172 foreach $j (@big) {
173 printf D " /* %4d */ %s\n", $n++, codesubst($j);
175 print D "};\n";
177 foreach $h (sort(keys(%dinstables))) {
178 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
179 foreach $j (@{$dinstables{$h}}) {
180 print D " instrux + $j,\n";
182 print D "};\n";
185 foreach $h (@disasm_prefixes, '') {
186 $is_prefix{$h} = 1;
187 print D "\n";
188 print D "static " unless ($h eq '');
189 print D "const struct disasm_index ";
190 print D ($h eq '') ? 'itable' : "itable_$h";
191 print D "[256] = {\n";
192 for ($c = 0; $c < 256; $c++) {
193 $nn = sprintf("%s%02X", $h, $c);
194 if ($is_prefix{$nn}) {
195 die "$0: ambiguous decoding of $nn\n"
196 if (defined($dinstables{$nn}));
197 printf D " { itable_%s, -1 },\n", $nn;
198 } elsif (defined($dinstables{$nn})) {
199 printf D " { itable_%s, %u },\n",
200 $nn, scalar(@{$dinstables{$nn}});
201 } else {
202 printf D " { NULL, 0 },\n";
205 print D "};\n";
208 close D;
211 if ( !defined($output) || $output eq 'i' ) {
212 print STDERR "Writing insnsi.h...\n";
214 open I, ">insnsi.h";
216 print I "/* This file is auto-generated from insns.dat by insns.pl" .
217 " - don't edit it */\n\n";
218 print I "/* This file in included by nasm.h */\n\n";
220 print I "/* Instruction names */\n\n";
221 print I "#ifndef NASM_INSNSI_H\n";
222 print I "#define NASM_INSNSI_H 1\n\n";
223 print I "enum opcode {\n";
224 $maxlen = 0;
225 foreach $i (@opcodes, @opcodes_cc) {
226 print I "\tI_${i},\n";
227 $len = length($i);
228 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
229 $maxlen = $len if ( $len > $maxlen );
231 print I "\tI_none = -1\n";
232 print I "\n};\n\n";
233 print I "#define MAX_INSLEN ", $maxlen, "\n\n";
234 print I "#endif /* NASM_INSNSI_H */\n";
236 close I;
239 if ( !defined($output) || $output eq 'n' ) {
240 print STDERR "Writing insnsn.c...\n";
242 open N, ">insnsn.c";
244 print N "/* This file is auto-generated from insns.dat by insns.pl" .
245 " - don't edit it */\n\n";
246 print N "/* This file in included by names.c */\n\n";
248 print N "static const char * const insn_names[] = {";
249 $first = 1;
250 foreach $i (@opcodes) {
251 print N "," if ( !$first );
252 $first = 0;
253 $ilower = $i;
254 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
255 print N "\n\t\"${ilower}\"";
257 print N "\n};\n\n";
258 print N "/* Conditional instructions */\n";
259 print N "static const char *icn[] = {";
260 $first = 1;
261 foreach $i (@opcodes_cc) {
262 print N "," if ( !$first );
263 $first = 0;
264 $ilower = $i;
265 $ilower =~ s/cc$//; # Skip cc suffix
266 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
267 print N "\n\t\"${ilower}\"";
270 print N "\n};\n\n";
271 print N "/* and the corresponding opcodes */\n";
272 print N "static const enum opcode ico[] = {";
273 $first = 1;
274 foreach $i (@opcodes_cc) {
275 print N "," if ( !$first );
276 $first = 0;
277 print N "\n\tI_$i";
280 print N "\n};\n";
282 close N;
285 printf STDERR "Done: %d instructions\n", $insns;
287 sub format_insn(@) {
288 my ($opcode, $operands, $codes, $flags) = @_;
289 my $num, $nd = 0;
290 my @bytecode;
292 return (undef, undef) if $operands eq "ignore";
294 # format the operands
295 $operands =~ s/:/|colon,/g;
296 $operands =~ s/mem(\d+)/mem|bits$1/g;
297 $operands =~ s/mem/memory/g;
298 $operands =~ s/memory_offs/mem_offs/g;
299 $operands =~ s/imm(\d+)/imm|bits$1/g;
300 $operands =~ s/imm/immediate/g;
301 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
302 $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
303 $operands =~ s/\=([0-9]+)/same_as|$1/g;
304 if ($operands eq 'void') {
305 @ops = ();
306 } else {
307 @ops = split(/\,/, $operands);
309 $num = scalar(@ops);
310 while (scalar(@ops) < $MAX_OPERANDS) {
311 push(@ops, '0');
313 $operands = join(',', @ops);
314 $operands =~ tr/a-z/A-Z/;
316 # format the flags
317 $flags =~ s/,/|IF_/g;
318 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
319 $flags = "IF_" . $flags;
321 @bytecode = (decodify($codes), 0);
322 push(@bytecode_list, [@bytecode]);
323 $codes = hexstr(@bytecode);
325 ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
329 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
330 # offset into nasm_bytecodes
332 sub codesubst($) {
333 my($s) = @_;
334 my $n;
336 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
337 my $pos = $bytecode_pos{$1};
338 if (!defined($pos)) {
339 die "$0: no position assigned to byte code $1\n";
341 $s = $` . "nasm_bytecodes+${pos}" . "$'";
343 return $s;
346 sub addprefix ($@) {
347 my ($prefix, @list) = @_;
348 my $x;
349 my @l = ();
351 foreach $x (@list) {
352 push(@l, sprintf("%s%02X", $prefix, $x));
355 return @l;
359 # Turn a code string into a sequence of bytes
361 sub decodify($) {
362 # Although these are C-syntax strings, by convention they should have
363 # only octal escapes (for directives) and hexadecimal escapes
364 # (for verbatim bytes)
365 my($codestr) = @_;
366 my $c = $codestr;
367 my @codes = ();
369 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
370 return byte_code_compile($1);
373 while ($c ne '') {
374 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
375 push(@codes, hex $1);
376 $c = $2;
377 next;
378 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
379 push(@codes, oct $1);
380 $c = $2;
381 next;
382 } else {
383 die "$0: unknown code format in \"$codestr\"\n";
387 return @codes;
390 # Turn a numeric list into a hex string
391 sub hexstr(@) {
392 my $s = '';
393 my $c;
395 foreach $c (@_) {
396 $s .= sprintf("%02X", $c);
398 return $s;
401 # Here we determine the range of possible starting bytes for a given
402 # instruction. We need only consider the codes:
403 # \1 \2 \3 mean literal bytes, of course
404 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
405 # \1[0123] mean byte plus register value
406 # \330 means byte plus condition code
407 # \0 or \340 mean give up and return empty set
408 sub startseq($) {
409 my ($codestr) = @_;
410 my $word, @range;
411 my @codes = ();
412 my $c = $codestr;
413 my $c0, $c1, $i;
414 my $prefix = '';
416 @codes = decodify($codestr);
418 while ($c0 = shift(@codes)) {
419 $c1 = $codes[0];
420 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
421 # Fixed byte string
422 my $fbs = $prefix;
423 while (1) {
424 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
425 while ($c0--) {
426 $fbs .= sprintf("%02X", shift(@codes));
428 } else {
429 last;
431 $c0 = shift(@codes);
434 foreach $pfx (@disasm_prefixes) {
435 if (substr($fbs, 0, length($pfx)) eq $pfx) {
436 $prefix = $pfx;
437 $fbs = substr($fbs, length($pfx));
438 last;
442 if ($fbs ne '') {
443 return ($prefix.substr($fbs,0,2));
446 unshift(@codes, $c0);
447 } elsif ($c0 == 04) {
448 return addprefix($prefix, 0x07, 0x17, 0x1F);
449 } elsif ($c0 == 05) {
450 return addprefix($prefix, 0xA1, 0xA9);
451 } elsif ($c0 == 06) {
452 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
453 } elsif ($c0 == 07) {
454 return addprefix($prefix, 0xA0, 0xA8);
455 } elsif ($c0 >= 010 && $c0 <= 013) {
456 return addprefix($prefix, $c1..($c1+7));
457 } elsif (($c0 & ~013) == 0144) {
458 return addprefix($prefix, $c1, $c1|2);
459 } elsif ($c0 == 0330) {
460 return addprefix($prefix, $c1..($c1+15));
461 } elsif ($c0 == 0 || $c0 == 0340) {
462 return $prefix;
463 } elsif (($c0 & ~3) == 0260 || $c0 == 270) {
464 shift(@codes);
465 shift(@codes);
466 } elsif ($c0 == 0172) {
467 shift(@codes);
468 } else {
469 # We really need to be able to distinguish "forbidden"
470 # and "ignorable" codes here
473 return $prefix;
477 # This function takes a series of byte codes in a format which is more
478 # typical of the Intel documentation, and encode it.
480 # The format looks like:
482 # [operands: opcodes]
484 # The operands word lists the order of the operands:
486 # r = register field in the modr/m
487 # m = modr/m
488 # v = VEX "v" field
489 # d = DREX "dst" field
490 # i = immediate
491 # s = register field of is4 or imz2 field
493 sub byte_code_compile($) {
494 my($str) = @_;
495 my $opr;
496 my $opc;
497 my @codes = ();
498 my $litix = undef;
499 my %oppos = ();
500 my $i;
501 my $op, $oq;
503 if ($str =~ /^(\S*)\:\s*(.*\S)\s*$/) {
504 $opr = "\L$1";
505 $opc = "\L$2";
506 } else {
507 $opr = '';
508 $opc = $str;
511 for ($i = 0; $i < length($opr); $i++) {
512 $oppos{substr($opr,$i,1)} = $i;
515 $prefix_ok = 1;
516 foreach $op (split($opc)) {
517 if ($op eq 'o16') {
518 push(@codes, 0320);
519 } elsif ($op eq 'o32') {
520 push(@codes, 0321);
521 } elsif ($op eq 'o64') { # 64-bit operand size requiring REX.W
522 push(@codes, 0324);
523 } elsif ($op eq 'o64i') { # Implied 64-bit operand size (no REX.W)
524 push(@codes, 0323);
525 } elsif ($op eq 'a16') {
526 push(@codes, 0310);
527 } elsif ($op eq 'a32') {
528 push(@codes, 0311);
529 } elsif ($op eq 'a64') {
530 push(@codes, 0313);
531 } elsif ($op eq '!osp') {
532 push(@codes, 0364);
533 } elsif ($op eq '!asp') {
534 push(@codes, 0365);
535 } elsif ($op eq 'rex.l') {
536 push(@codes, 0334);
537 } elsif ($op eq 'repe') {
538 push(@codes, 0335);
539 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
540 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
541 if ($op eq '66') {
542 push(@codes, 0361);
543 } elsif ($op eq 'f2') {
544 push(@codes, 0362);
545 } elsif ($op eq 'f3') {
546 push(@codes, 0363);
547 } else {
548 push(@codes, 0360);
550 } elsif ($op =~ /^[0-9a-f]{2}$/) {
551 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes) {
552 $codes[$litix]++;
553 push(@codes, hex $op);
554 } else {
555 $litix = scalar(@codes);
556 push(@codes, 01, hex $op);
558 $prefix_ok = 0;
559 } elsif ($op eq '/r') {
560 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
561 die "$0: $line: $op requires r and m operands\n";
563 push(@codes, 0100 + ($oppos{'m'} << 3) + $oppos{'r'});
564 $prefix_ok = 0;
565 } elsif ($op =~ m:^/([0-7])$:) {
566 if (!defined($oppos{'m'})) {
567 die "$0: $line: $op requires m operand\n";
569 push(@codes, 0200 + ($oppos{'m'} << 3) + $1);
570 $prefix_ok = 0;
571 } elsif ($op =~ /^vex(|\..*)$/) {
572 my ($m,$w,$l,$p) = (undef,2,undef,0);
573 foreach $oq (split(/\./, $op)) {
574 if ($oq eq 'vex') {
575 # prefix
576 } elsif ($oq eq '128' || $oq eq 'l0') {
577 $l = 0;
578 } elsif ($oq eq '256' || $oq eq 'l1') {
579 $l = 1;
580 } elsif ($oq eq 'w0') {
581 $w = 0;
582 } elsif ($oq eq 'w1') {
583 $w = 1;
584 } elsif ($oq eq '66') {
585 $p = 1;
586 } elsif ($oq eq 'f3') {
587 $p = 2;
588 } elsif ($oq eq 'f2') {
589 $p = 3;
590 } elsif ($oq eq '0f') {
591 $m = 1;
592 } elsif ($oq eq '0f38') {
593 $m = 2;
594 } elsif ($oq eq '0f3a') {
595 $m = 3;
596 } elsif ($oq =~ /^m([0-9]+)$/) {
597 $m = $1+0;
598 } elsif ($oq eq 'nds' || $oq eq 'ndd') {
599 return undef if (!defined($oppos{'v'}));
600 } else {
601 die "$0: $line: undefined VEX subcode: $oq\n";
604 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
605 die "$0: $line: missing fields in VEX specification\n";
607 push(@codes, defined($oppos{'v'}) ? 0260+$oppos{'v'} : 0270,
608 $m, ($w << 3)+($l << 2)+$p);
609 $prefix_ok = 0;
610 } elsif ($op =~ /^drex(|..*)$/) {
611 my ($oc0) = (0);
612 foreach $oq (split(/\./, $op)) {
613 if ($oq eq 'drex') {
614 #prefix
615 } elsif ($oq eq 'oc0') {
616 $oc0 = 1;
617 } else {
618 die "$0: $line: undefined DREX subcode: $oq\n";
621 if (!defined($oppos{'d'})) {
622 die "$0: $line: DREX without a 'd' operand\n";
624 push(@codes, 0160+$oppos{'d'}+($oc0 ? 4 : 0));
625 } elsif ($op =~ /^(imm8|imm8u|imm8s|imm16|imm32|imm32s|imm64|imm|immx|rel8|rel16|rel32|rel64|rel|seg|simm16|simm32|simm32s)$/) {
626 if (!defined($oppos{'i'})) {
627 die "$0: $op without 'i' operand\n";
629 if ($op eq 'imm8s') {
630 push(@codes, 014+$oppos{'i'});
631 } elsif ($op eq 'imm8') {
632 push(@codes, 020+$oppos{'i'});
633 } elsif ($op eq 'imm8u') {
634 push(@codes, 024+$oppos{'i'});
635 } elsif ($op eq 'imm16') {
636 push(@codes, 030+$oppos{'i'});
637 } elsif ($op eq 'imm') { # 16 or 32 bit operand
638 push(@codes, 034+$oppos{'i'});
639 } elsif ($op eq 'imm32') {
640 push(@codes, 040+$oppos{'i'});
641 } elsif ($op eq 'immx') { # 16, 32 or 64 bit operand
642 push(@codes, 044+$oppos{'i'});
643 } elsif ($op eq 'rel8') {
644 push(@codes, 050+$oppos{'i'});
645 } elsif ($op eq 'rel64') {
646 push(@codes, 054+$oppos{'i'});
647 } elsif ($op eq 'rel16') {
648 push(@codes, 060+$oppos{'i'});
649 } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
650 push(@codes, 064+$oppos{'i'});
651 } elsif ($op eq 'rel32') {
652 push(@codes, 070+$oppos{'i'});
653 } elsif ($op eq 'seg') {
654 push(@codes, 074+$oppos{'i'});
655 } elsif ($op eq 'simm16') { # imm16 that can be bytified
656 if (!defined($s_pos)) {
657 die "$0: $line: $op without a +s byte\n";
659 $codes[$s_pos] += 0144;
660 push(@codes, 0140+$oppos{'i'});
661 } elsif ($op eq 'simm32') { # imm32 that can be bytified
662 if (!defined($s_pos)) {
663 die "$0: $line: $op without a +s byte\n";
665 $codes[$s_pos] += 0154;
666 push(@codes, 0150+$oppos{'i'});
667 } elsif ($op eq 'simm32s') {
668 # imm32 that can be bytified, sign extended
669 if (!defined($s_pos)) {
670 die "$0: $line: $op without a +s byte\n";
672 $codes[$s_pos] += 0154;
673 push(@codes, 0250+$oppos{'i'});
675 $prefix_ok = 0;
676 } elsif ($op eq 'is4' || $op eq 'imz2') {
677 if (!defined($oppos{'i'} || !defined($oppos{'s'}))) {
678 die "$0: $line: $op without 'i' and 's' operands\n";
680 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
681 $prefix_ok = 0;
682 } elsif ($op =~ /^(is4|imz2)\=([0-9]+)$/) {
683 my $imm = $2;
684 if (!defined($oppos{'s'})) {
685 die "$0: $line: $op without 's' operand\n";
687 if ($imm < 0 || $imm > 15) {
688 die "$0: $line: invalid imm4 value for $op: $imm\n";
690 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
691 $prefix_ok = 0;
692 } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
693 if (!defined($oppos{'i'})) {
694 die "$0: $op without 'i' operand\n";
696 $s_pos = scalar @codes;
697 push(@codes, $oppos{'i'}, hex $1);
698 $prefix_ok = 0;
699 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
700 push(@codes, 0330, hex $1);
701 $prefix_ok = 0;
702 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
703 # Escape to enter literal bytecodes
704 push(@codes, oct $1);
705 } else {
706 die "$0: unknown operation: $op\n";
710 return @codes;