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
19 for ($m = 0; $m < 32; $m++) {
20 for ($lp = 0; $lp < 8; $lp++) {
21 push(@vexlist, sprintf("VEX%02X%01X", $m, $lp));
24 @disasm_prefixes = (@vexlist, @disasm_prefixes);
26 print STDERR "Reading insns.dat...\n";
30 foreach $arg ( @ARGV ) {
31 if ( $arg =~ /^\-/ ) {
32 if ( $arg =~ /^\-([abdin])$/ ) {
35 die "$0: Unknown option: ${arg}\n";
42 $fname = "insns.dat" unless $fname = $args[0];
43 open (F, $fname) || die "unable to open $fname";
53 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
55 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
56 warn "line $line does not contain four fields\n";
59 @fields = ($1, $2, $3, $4);
60 ($formatted, $nd) = format_insn(@fields);
63 $aname = "aa_$fields[0]";
64 push @$aname, $formatted;
66 if ( $fields[0] =~ /cc$/ ) {
67 # Conditional instruction
68 $k_opcodes_cc{$fields[0]}++;
70 # Unconditional instruction
71 $k_opcodes{$fields[0]}++;
73 if ($formatted && !$nd) {
74 push @big, $formatted;
75 my @sseq = startseq($fields[2]);
77 if (!defined($dinstables{$i})) {
80 push(@{$dinstables{$i}}, $#big);
88 # Generate the bytecode array. At this point, @bytecode_list contains
89 # the full set of bytecodes.
92 # Sort by descending length
93 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
97 foreach $bl (@bytecode_list) {
99 next if (defined($bytecode_pos{$h}));
101 push(@bytecode_array, $bl);
103 $bytecode_pos{$h} = $bytecode_next;
108 undef @bytecode_list;
110 @opcodes = sort keys(%k_opcodes);
111 @opcodes_cc = sort keys(%k_opcodes_cc);
113 if ( !defined($output) || $output eq 'b') {
114 print STDERR "Writing insnsb.c...\n";
118 print B "/* This file auto-generated from insns.dat by insns.pl" .
119 " - don't edit it */\n\n";
121 print B "#include \"nasm.h\"\n";
122 print B "#include \"insns.h\"\n\n";
124 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
127 foreach $bl (@bytecode_array) {
128 printf B " /* %5d */ ", $p;
140 if ( !defined($output) || $output eq 'a' ) {
141 print STDERR "Writing insnsa.c...\n";
145 print A "/* This file auto-generated from insns.dat by insns.pl" .
146 " - don't edit it */\n\n";
148 print A "#include \"nasm.h\"\n";
149 print A "#include \"insns.h\"\n\n";
151 foreach $i (@opcodes, @opcodes_cc) {
152 print A "static const struct itemplate instrux_${i}[] = {\n";
154 foreach $j (@$aname) {
155 print A " ", codesubst($j), "\n";
157 print A " ITEMPLATE_END\n};\n\n";
159 print A "const struct itemplate * const nasm_instructions[] = {\n";
160 foreach $i (@opcodes, @opcodes_cc) {
161 print A " instrux_${i},\n";
168 if ( !defined($output) || $output eq 'd' ) {
169 print STDERR "Writing insnsd.c...\n";
173 print D "/* This file auto-generated from insns.dat by insns.pl" .
174 " - don't edit it */\n\n";
176 print D "#include \"nasm.h\"\n";
177 print D "#include \"insns.h\"\n\n";
179 print D "static const struct itemplate instrux[] = {\n";
182 printf D " /* %4d */ %s\n", $n++, codesubst($j);
186 foreach $h (sort(keys(%dinstables))) {
187 next if ($h eq ''); # Skip pseudo-instructions
188 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
189 foreach $j (@{$dinstables{$h}}) {
190 print D " instrux + $j,\n";
196 foreach $h (@disasm_prefixes, '') {
197 for ($c = 0; $c < 256; $c++) {
198 $nn = sprintf("%s%02X", $h, $c);
199 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
200 # At least one entry in this prefix table
201 push(@prefix_list, $h);
208 foreach $h (@prefix_list) {
210 print D "static " unless ($h eq '');
211 print D "const struct disasm_index ";
212 print D ($h eq '') ? 'itable' : "itable_$h";
213 print D "[256] = {\n";
214 for ($c = 0; $c < 256; $c++) {
215 $nn = sprintf("%s%02X", $h, $c);
216 if ($is_prefix{$nn}) {
217 die "$fname: ambiguous decoding of $nn\n"
218 if (defined($dinstables{$nn}));
219 printf D " { itable_%s, -1 },\n", $nn;
220 } elsif (defined($dinstables{$nn})) {
221 printf D " { itable_%s, %u },\n",
222 $nn, scalar(@{$dinstables{$nn}});
224 printf D " { NULL, 0 },\n";
230 print D "\nconst struct disasm_index * const itable_VEX[32][8] = {\n ";
231 for ($m = 0; $m < 32; $m++) {
233 for ($lp = 0; $lp < 8; $lp++) {
234 $vp = sprintf("VEX%02X%01X", $m, $lp);
235 if ($is_prefix{$vp}) {
236 printf D " itable_%s,\n", $vp;
248 if ( !defined($output) || $output eq 'i' ) {
249 print STDERR "Writing insnsi.h...\n";
253 print I "/* This file is auto-generated from insns.dat by insns.pl" .
254 " - don't edit it */\n\n";
255 print I "/* This file in included by nasm.h */\n\n";
257 print I "/* Instruction names */\n\n";
258 print I "#ifndef NASM_INSNSI_H\n";
259 print I "#define NASM_INSNSI_H 1\n\n";
260 print I "enum opcode {\n";
262 foreach $i (@opcodes, @opcodes_cc) {
263 print I "\tI_${i},\n";
265 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
266 $maxlen = $len if ( $len > $maxlen );
268 print I "\tI_none = -1\n";
270 print I "#define MAX_INSLEN ", $maxlen, "\n";
271 print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
272 print I "#endif /* NASM_INSNSI_H */\n";
277 if ( !defined($output) || $output eq 'n' ) {
278 print STDERR "Writing insnsn.c...\n";
282 print N "/* This file is auto-generated from insns.dat by insns.pl" .
283 " - don't edit it */\n\n";
284 print N "#include \"tables.h\"\n\n";
286 print N "const char * const nasm_insn_names[] = {";
288 foreach $i (@opcodes, @opcodes_cc) {
289 print N "," if ( !$first );
292 $ilower =~ s/cc$//; # Remove conditional cc suffix
293 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
294 print N "\n\t\"${ilower}\"";
300 printf STDERR "Done: %d instructions\n", $insns;
303 my ($opcode, $operands, $codes, $flags) = @_;
307 return (undef, undef) if $operands eq "ignore";
309 # format the operands
310 $operands =~ s/:/|colon,/g;
311 $operands =~ s/mem(\d+)/mem|bits$1/g;
312 $operands =~ s/mem/memory/g;
313 $operands =~ s/memory_offs/mem_offs/g;
314 $operands =~ s/imm(\d+)/imm|bits$1/g;
315 $operands =~ s/imm/immediate/g;
316 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
317 $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
318 $operands =~ s/\=([0-9]+)/same_as|$1/g;
319 if ($operands eq 'void') {
322 @ops = split(/\,/, $operands);
325 while (scalar(@ops) < $MAX_OPERANDS) {
328 $operands = join(',', @ops);
329 $operands =~ tr/a-z/A-Z/;
332 $flags =~ s/,/|IF_/g;
333 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
334 $flags = "IF_" . $flags;
336 @bytecode = (decodify($codes), 0);
337 push(@bytecode_list, [@bytecode]);
338 $codes = hexstr(@bytecode);
340 ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
344 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
345 # offset into nasm_bytecodes
351 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
352 my $pos = $bytecode_pos{$1};
353 if (!defined($pos)) {
354 die "$fname: no position assigned to byte code $1\n";
356 $s = $` . "nasm_bytecodes+${pos}" . "$'";
362 my ($prefix, @list) = @_;
367 push(@l, sprintf("%s%02X", $prefix, $x));
374 # Turn a code string into a sequence of bytes
377 # Although these are C-syntax strings, by convention they should have
378 # only octal escapes (for directives) and hexadecimal escapes
379 # (for verbatim bytes)
382 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
383 return byte_code_compile($1);
390 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
391 push(@codes, hex $1);
394 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
395 push(@codes, oct $1);
399 die "$fname: unknown code format in \"$codestr\"\n";
406 # Turn a numeric list into a hex string
412 $s .= sprintf("%02X", $c);
417 # Here we determine the range of possible starting bytes for a given
418 # instruction. We need only consider the codes:
419 # \1 \2 \3 mean literal bytes, of course
420 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
421 # \1[0123] mean byte plus register value
422 # \330 means byte plus condition code
423 # \0 or \340 mean give up and return empty set
424 # \17[234] skip is4 control byte
425 # \26x \270 skip VEX control bytes
434 @codes = decodify($codestr);
436 while ($c0 = shift(@codes)) {
438 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
442 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
444 $fbs .= sprintf("%02X", shift(@codes));
452 foreach $pfx (@disasm_prefixes) {
453 if (substr($fbs, 0, length($pfx)) eq $pfx) {
455 $fbs = substr($fbs, length($pfx));
461 return ($prefix.substr($fbs,0,2));
464 unshift(@codes, $c0);
465 } elsif ($c0 == 04) {
466 return addprefix($prefix, 0x07, 0x17, 0x1F);
467 } elsif ($c0 == 05) {
468 return addprefix($prefix, 0xA1, 0xA9);
469 } elsif ($c0 == 06) {
470 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
471 } elsif ($c0 == 07) {
472 return addprefix($prefix, 0xA0, 0xA8);
473 } elsif ($c0 >= 010 && $c0 <= 013) {
474 return addprefix($prefix, $c1..($c1+7));
475 } elsif (($c0 & ~013) == 0144) {
476 return addprefix($prefix, $c1, $c1|2);
477 } elsif ($c0 == 0330) {
478 return addprefix($prefix, $c1..($c1+15));
479 } elsif ($c0 == 0 || $c0 == 0340) {
481 } elsif (($c0 & ~3) == 0260 || $c0 == 0270) {
484 $wlp = shift(@codes);
485 $prefix .= sprintf('VEX%02X%01X', $m, $wlp & 7);
486 } elsif ($c0 >= 0172 && $c0 <= 174) {
487 shift(@codes); # Skip is4 control byte
489 # We really need to be able to distinguish "forbidden"
490 # and "ignorable" codes here
497 # This function takes a series of byte codes in a format which is more
498 # typical of the Intel documentation, and encode it.
500 # The format looks like:
502 # [operands: opcodes]
504 # The operands word lists the order of the operands:
506 # r = register field in the modr/m
509 # d = DREX "dst" field
511 # s = register field of is4/imz2 field
512 # - = implicit (unencoded) operand
514 # For an operand that should be filled into more than one field,
515 # enter it as e.g. "r+v".
517 sub byte_code_compile($) {
527 unless ($str =~ /^(([^\s:]*)\:|)\s*(.*\S)\s*$/) {
528 die "$fname: $line: cannot parse: [$str]\n";
534 for ($i = 0; $i < length($opr); $i++) {
535 my $c = substr($opr,$i,1);
544 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
547 } elsif ($op eq 'o32') {
549 } elsif ($op eq 'o64') { # 64-bit operand size requiring REX.W
551 } elsif ($op eq 'o64nw') { # Implied 64-bit operand size (no REX.W)
553 } elsif ($op eq 'a16') {
555 } elsif ($op eq 'a32') {
557 } elsif ($op eq 'a64') {
559 } elsif ($op eq '!osp') {
561 } elsif ($op eq '!asp') {
563 } elsif ($op eq 'rex.l') {
565 } elsif ($op eq 'repe') {
567 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
568 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
571 } elsif ($op eq 'f2') {
573 } elsif ($op eq 'f3') {
578 } elsif ($op =~ /^[0-9a-f]{2}$/) {
579 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes) {
581 push(@codes, hex $op);
583 $litix = scalar(@codes);
584 push(@codes, 01, hex $op);
587 } elsif ($op eq '/r') {
588 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
589 die "$fname: $line: $op requires r and m operands\n";
591 push(@codes, 0100 + ($oppos{'m'} << 3) + $oppos{'r'});
593 } elsif ($op =~ m:^/([0-7])$:) {
594 if (!defined($oppos{'m'})) {
595 die "$fname: $line: $op requires m operand\n";
597 push(@codes, 0200 + ($oppos{'m'} << 3) + $1);
599 } elsif ($op =~ /^vex(|\..*)$/) {
600 my ($m,$w,$l,$p) = (undef,2,undef,0);
602 foreach $oq (split(/\./, $op)) {
605 } elsif ($oq eq '128' || $oq eq 'l0') {
607 } elsif ($oq eq '256' || $oq eq 'l1') {
609 } elsif ($oq eq 'w0') {
611 } elsif ($oq eq 'w1') {
613 } elsif ($oq eq 'wx') {
615 } elsif ($oq eq 'ww') {
617 } elsif ($oq eq '66') {
619 } elsif ($oq eq 'f3') {
621 } elsif ($oq eq 'f2') {
623 } elsif ($oq eq '0f') {
625 } elsif ($oq eq '0f38') {
627 } elsif ($oq eq '0f3a') {
629 } elsif ($oq =~ /^m([0-9]+)$/) {
631 } elsif ($oq eq 'nds' || $oq eq 'ndd') {
632 if (!defined($oppos{'v'})) {
633 die "$fname: $line: vex.$oq without 'v' operand\n";
637 die "$fname: $line: undefined VEX subcode: $oq\n";
640 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
641 die "$fname: $line: missing fields in VEX specification\n";
643 if (defined($oppos{'v'}) && !$has_nds) {
644 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
646 push(@codes, defined($oppos{'v'}) ? 0260+$oppos{'v'} : 0270,
647 $m, ($w << 3)+($l << 2)+$p);
649 } elsif ($op =~ /^\/drex([01])$/) {
651 if (!defined($oppos{'d'})) {
652 die "$fname: $line: DREX without a 'd' operand\n";
654 # Note the use of *unshift* here, as opposed to *push*.
655 # This is because NASM want this byte code at the start of
656 # the instruction sequence, but the AMD documentation puts
657 # this at (roughly) the position of the drex byte itself.
658 # This allows us to match the AMD documentation and still
659 # do the right thing.
660 unshift(@codes, 0160+$oppos{'d'}+($oc0 ? 4 : 0));
661 } elsif ($op =~ /^(ib\,s|ib|ib\,w|iw|iwd|id|iwdq|rel|rel8|rel16|rel32|iq|seg|ibw|ibd|ibd,s)$/) {
662 if (!defined($oppos{'i'})) {
663 die "$fname: $line: $op without 'i' operand\n";
665 if ($op eq 'ib,s') { # Signed imm8
666 push(@codes, 014+$oppos{'i'});
667 } elsif ($op eq 'ib') { # imm8
668 push(@codes, 020+$oppos{'i'});
669 } elsif ($op eq 'ib,u') { # Unsigned imm8
670 push(@codes, 024+$oppos{'i'});
671 } elsif ($op eq 'iw') { # imm16
672 push(@codes, 030+$oppos{'i'});
673 } elsif ($op eq 'ibx') { # imm8 sign-extended to opsize
674 push(@codes, 0274+$oppos{'i'});
675 } elsif ($op eq 'iwd') { # imm16 or imm32, depending on opsize
676 push(@codes, 034+$oppos{'i'});
677 } elsif ($op eq 'id') { # imm32
678 push(@codes, 040+$oppos{'i'});
679 } elsif ($op eq 'iwdq') { # imm16/32/64, depending on opsize
680 push(@codes, 044+$oppos{'i'});
681 } elsif ($op eq 'rel8') {
682 push(@codes, 050+$oppos{'i'});
683 } elsif ($op eq 'iq') {
684 push(@codes, 054+$oppos{'i'});
685 } elsif ($op eq 'rel16') {
686 push(@codes, 060+$oppos{'i'});
687 } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
688 push(@codes, 064+$oppos{'i'});
689 } elsif ($op eq 'rel32') {
690 push(@codes, 070+$oppos{'i'});
691 } elsif ($op eq 'seg') {
692 push(@codes, 074+$oppos{'i'});
693 } elsif ($op eq 'ibw') { # imm16 that can be bytified
694 if (!defined($s_pos)) {
695 die "$fname: $line: $op without a +s byte\n";
697 $codes[$s_pos] += 0144;
698 push(@codes, 0140+$oppos{'i'});
699 } elsif ($op eq 'ibd') { # imm32 that can be bytified
700 if (!defined($s_pos)) {
701 die "$fname: $line: $op without a +s byte\n";
703 $codes[$s_pos] += 0154;
704 push(@codes, 0150+$oppos{'i'});
705 } elsif ($op eq 'ibd,s') {
706 # imm32 that can be bytified, sign extended to 64 bits
707 if (!defined($s_pos)) {
708 die "$fname: $line: $op without a +s byte\n";
710 $codes[$s_pos] += 0154;
711 push(@codes, 0250+$oppos{'i'});
714 } elsif ($op eq '/is4') {
715 if (!defined($oppos{'s'})) {
716 die "$fname: $line: $op without 's' operand\n";
718 if (defined($oppos{'i'})) {
719 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
721 push(@codes, 0174, $oppos{'s'});
724 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
726 if (!defined($oppos{'s'})) {
727 die "$fname: $line: $op without 's' operand\n";
729 if ($imm < 0 || $imm > 15) {
730 die "$fname: $line: invalid imm4 value for $op: $imm\n";
732 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
734 } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
735 if (!defined($oppos{'i'})) {
736 die "$fname: $line: $op without 'i' operand\n";
738 $s_pos = scalar @codes;
739 push(@codes, $oppos{'i'}, hex $1);
741 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
742 push(@codes, 0330, hex $1);
744 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
745 # Escape to enter literal bytecodes
746 push(@codes, oct $1);
748 die "$fname: $line: unknown operation: $op\n";