2 ## --------------------------------------------------------------------------
4 ## Copyright 1996-2016 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
8 ## Redistribution and use in source and binary forms, with or without
9 ## modification, are permitted provided that the following
10 ## conditions are met:
12 ## * Redistributions of source code must retain the above copyright
13 ## notice, this list of conditions and the following disclaimer.
14 ## * Redistributions in binary form must reproduce the above
15 ## copyright notice, this list of conditions and the following
16 ## disclaimer in the documentation and/or other materials provided
17 ## with the distribution.
19 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20 ## CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21 ## INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 ## NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ## LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 ## OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 ## EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ## --------------------------------------------------------------------------
38 # Parse insns.dat and produce generated source code files
40 require 'x86/insns-iflags.ph';
42 # Opcode prefixes which need their own opcode tables
43 # LONGER PREFIXES FIRST!
44 @disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
46 # This should match MAX_OPERANDS from nasm.h
49 # Add VEX/XOP prefixes
50 @vex_class = ( 'vex', 'xop', 'evex' );
51 $vex_classes = scalar(@vex_class);
54 for ($c = 0; $c < $vex_classes; $c++) {
55 $vexmap{$vex_class[$c]} = $c;
56 for ($m = 0; $m < 32; $m++) {
57 for ($p = 0; $p < 4; $p++) {
58 push(@vexlist, sprintf("%s%02X%01X", $vex_class[$c], $m, $p));
62 @disasm_prefixes = (@vexlist, @disasm_prefixes);
64 @bytecode_count = (0) x
256;
66 print STDERR
"Reading insns.dat...\n";
70 foreach $arg ( @ARGV ) {
71 if ( $arg =~ /^\-/ ) {
72 if ( $arg =~ /^\-([abdin]|f[hc])$/ ) {
75 die "$0: Unknown option: ${arg}\n";
82 die if (scalar(@args) != 2); # input output
83 ($fname, $oname) = @args;
85 open(F
, '<', $fname) || die "unable to open $fname";
95 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
97 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
98 warn "line $line does not contain four fields\n";
101 @fields = ($1, $2, $3, $4);
102 @field_list = ([@fields, 0]);
104 if ($fields[1] =~ /\*/) {
105 # This instruction has relaxed form(s)
106 if ($fields[2] !~ /^\[/) {
107 warn "line $line has an * operand but uses raw bytecodes\n";
112 @ops = split(/,/, $fields[1]);
113 for ($oi = 0; $oi < scalar @ops; $oi++) {
114 if ($ops[$oi] =~ /\*$/) {
116 warn "line $line has a first operand with a *\n";
123 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
124 if (($oi & ~$opmask) == 0) {
127 for ($oj = 0; $oj < scalar(@ops); $oj++) {
129 push(@xops, $ops[$oj]);
133 push(@field_list, [$fields[0], join(',', @xops),
134 $fields[2], $fields[3], $oi]);
139 foreach $fptr (@field_list) {
141 ($formatted, $nd) = format_insn
(@fields);
144 $aname = "aa_$fields[0]";
145 push @
$aname, $formatted;
147 if ( $fields[0] =~ /cc$/ ) {
148 # Conditional instruction
149 $k_opcodes_cc{$fields[0]}++;
151 # Unconditional instruction
152 $k_opcodes{$fields[0]}++;
154 if ($formatted && !$nd) {
155 push @big, $formatted;
156 my @sseq = startseq
($fields[2], $fields[4]);
158 if (!defined($dinstables{$i})) {
159 $dinstables{$i} = [];
161 push(@
{$dinstables{$i}}, $#big);
170 # Generate the bytecode array. At this point, @bytecode_list contains
171 # the full set of bytecodes.
174 # Sort by descending length
175 @bytecode_list = sort { scalar(@
$b) <=> scalar(@
$a) } @bytecode_list;
176 @bytecode_array = ();
179 foreach $bl (@bytecode_list) {
180 my $h = hexstr
(@
$bl);
181 next if (defined($bytecode_pos{$h}));
183 push(@bytecode_array, $bl);
185 $bytecode_pos{$h} = $bytecode_next;
190 undef @bytecode_list;
192 @opcodes = sort keys(%k_opcodes);
193 @opcodes_cc = sort keys(%k_opcodes_cc);
195 if ( $output eq 'b') {
196 print STDERR
"Writing $oname...\n";
198 open(B
, '>', $oname);
200 print B
"/* This file auto-generated from insns.dat by insns.pl" .
201 " - don't edit it */\n\n";
203 print B
"#include \"nasm.h\"\n";
204 print B
"#include \"insns.h\"\n\n";
206 print B
"const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
209 foreach $bl (@bytecode_array) {
210 printf B
" /* %5d */ ", $p;
221 print B
" * Bytecode frequencies (including reuse):\n";
223 for ($i = 0; $i < 32; $i++) {
225 for ($j = 0; $j < 256; $j += 32) {
226 print B
" |" if ($j);
227 printf B
" %3o:%4d", $i+$j, $bytecode_count[$i+$j];
236 if ( $output eq 'a' ) {
237 print STDERR
"Writing $oname...\n";
239 open(A
, '>', $oname);
241 print A
"/* This file auto-generated from insns.dat by insns.pl" .
242 " - don't edit it */\n\n";
244 print A
"#include \"nasm.h\"\n";
245 print A
"#include \"insns.h\"\n\n";
247 foreach $i (@opcodes, @opcodes_cc) {
248 print A
"static const struct itemplate instrux_${i}[] = {\n";
250 foreach $j (@
$aname) {
251 print A
" ", codesubst
($j), "\n";
253 print A
" ITEMPLATE_END\n};\n\n";
255 print A
"const struct itemplate * const nasm_instructions[] = {\n";
256 foreach $i (@opcodes, @opcodes_cc) {
257 print A
" instrux_${i},\n";
264 if ( $output eq 'd' ) {
265 print STDERR
"Writing $oname...\n";
267 open(D
, '>', $oname);
269 print D
"/* This file auto-generated from insns.dat by insns.pl" .
270 " - don't edit it */\n\n";
272 print D
"#include \"nasm.h\"\n";
273 print D
"#include \"insns.h\"\n\n";
275 print D
"static const struct itemplate instrux[] = {\n";
278 printf D
" /* %4d */ %s\n", $n++, codesubst
($j);
282 foreach $h (sort(keys(%dinstables))) {
283 next if ($h eq ''); # Skip pseudo-instructions
284 print D
"\nstatic const struct itemplate * const itable_${h}[] = {\n";
285 foreach $j (@
{$dinstables{$h}}) {
286 print D
" instrux + $j,\n";
292 foreach $h (@disasm_prefixes, '') {
293 for ($c = 0; $c < 256; $c++) {
294 $nn = sprintf("%s%02X", $h, $c);
295 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
296 # At least one entry in this prefix table
297 push(@prefix_list, $h);
304 foreach $h (@prefix_list) {
306 print D
"static " unless ($h eq '');
307 print D
"const struct disasm_index ";
308 print D
($h eq '') ?
'itable' : "itable_$h";
309 print D
"[256] = {\n";
310 for ($c = 0; $c < 256; $c++) {
311 $nn = sprintf("%s%02X", $h, $c);
312 if ($is_prefix{$nn}) {
313 die "$fname: ambiguous decoding of $nn\n"
314 if (defined($dinstables{$nn}));
315 printf D
" /* 0x%02x */ { itable_%s, -1 },\n", $c, $nn;
316 } elsif (defined($dinstables{$nn})) {
317 printf D
" /* 0x%02x */ { itable_%s, %u },\n", $c,
318 $nn, scalar(@
{$dinstables{$nn}});
320 printf D
" /* 0x%02x */ { NULL, 0 },\n", $c;
326 printf D
"\nconst struct disasm_index * const itable_vex[NASM_VEX_CLASSES][32][4] =\n";
328 for ($c = 0; $c < $vex_classes; $c++) {
330 for ($m = 0; $m < 32; $m++) {
332 for ($p = 0; $p < 4; $p++) {
333 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $p);
335 ($is_prefix{$vp} ?
sprintf("itable_%s,", $vp) : 'NULL,');
346 if ( $output eq 'i' ) {
347 print STDERR
"Writing $oname...\n";
349 open(I
, '>', $oname);
351 print I
"/* This file is auto-generated from insns.dat by insns.pl" .
352 " - don't edit it */\n\n";
353 print I
"/* This file in included by nasm.h */\n\n";
355 print I
"/* Instruction names */\n\n";
356 print I
"#ifndef NASM_INSNSI_H\n";
357 print I
"#define NASM_INSNSI_H 1\n\n";
358 print I
"enum opcode {\n";
360 foreach $i (@opcodes, @opcodes_cc) {
361 print I
"\tI_${i},\n";
363 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
364 $maxlen = $len if ( $len > $maxlen );
366 print I
"\tI_none = -1\n";
368 print I
"#define MAX_INSLEN ", $maxlen, "\n";
369 print I
"#define NASM_VEX_CLASSES ", $vex_classes, "\n";
370 print I
"#define NO_DECORATOR\t{", join(',',(0) x
$MAX_OPERANDS), "}\n";
371 print I
"#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
372 print I
"#endif /* NASM_INSNSI_H */\n";
377 if ( $output eq 'n' ) {
378 print STDERR
"Writing $oname...\n";
380 open(N
, '>', $oname);
382 print N
"/* This file is auto-generated from insns.dat by insns.pl" .
383 " - don't edit it */\n\n";
384 print N
"#include \"tables.h\"\n\n";
386 print N
"const char * const nasm_insn_names[] = {";
388 foreach $i (@opcodes, @opcodes_cc) {
389 print N
"," if ( !$first );
392 $ilower =~ s/cc$//; # Remove conditional cc suffix
393 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
394 print N
"\n\t\"${ilower}\"";
400 if ( $output eq 'fh') {
404 if ( $output eq 'fc') {
408 printf STDERR
"Done: %d instructions\n", $insns;
410 # Count primary bytecodes, for statistics
411 sub count_bytecodes
(@
) {
413 foreach my $bc (@_) {
418 $bytecode_count[$bc]++;
419 if ($bc >= 01 && $bc <= 04) {
421 } elsif (($bc & ~03) == 010) {
423 } elsif (($bc & ~013) == 0144) {
425 } elsif ($bc == 0172 || $bc == 0173) {
427 } elsif (($bc & ~3) == 0260 || $bc == 0270) { # VEX
429 } elsif (($bc & ~3) == 0240 || $bc == 0250) { # EVEX
431 } elsif ($bc == 0330) {
437 sub format_insn
($$$$$) {
438 my ($opcode, $operands, $codes, $flags, $relax) = @_;
439 my $num, $nd = 0, $rawflags, $flagsindex;
441 my $op, @ops, $opp, @opx, @oppx, @decos, @opevex;
443 return (undef, undef) if $operands eq "ignore";
445 # format the operands
446 $operands =~ s/\*//g;
447 $operands =~ s/:/|colon,/g;
450 if ($operands ne 'void') {
451 foreach $op (split(/,/, $operands)) {
454 foreach $opp (split(/\|/, $op)) {
456 if ($opp =~ s/^(b(32|64)|mask|z|er|sae)$//) {
460 if ($opp =~ s/(?<!\d)(8|16|32|64|80|128|256|512)$//) {
461 push(@oppx, "bits$1");
463 $opp =~ s/^mem$/memory/;
464 $opp =~ s/^memory_offs$/mem_offs/;
465 $opp =~ s/^imm$/immediate/;
466 $opp =~ s/^([a-z]+)rm$/rm_$1/;
467 $opp =~ s/^rm$/rm_gpr/;
468 $opp =~ s/^reg$/reg_gpr/;
469 # only for evex insns, high-16 regs are allowed
470 if ($codes !~ /(^|\s)evex\./) {
471 $opp =~ s/^(rm_[xyz]mm)$/$1_l16/;
472 $opp =~ s/^([xyz]mm)reg$/$1_l16/;
474 push(@opx, $opp, @oppx) if $opp;
476 $op = join('|', @opx);
478 push(@decos, (@opevex ?
join('|', @opevex) : '0'));
483 while (scalar(@ops) < $MAX_OPERANDS) {
487 $operands = join(',', @ops);
488 $operands =~ tr/a-z/A-Z/;
490 $decorators = "{" . join(',', @decos) . "}";
491 if ($decorators =~ /^{(0,)+0}$/) {
492 $decorators = "NO_DECORATOR";
494 $decorators =~ tr/a-z/A-Z/;
497 $nd = 1 if $flags =~ /(^|\,)ND($|\,)/;
498 $flags =~ s/(^|\,)ND($|\,)/\1/g;
499 $flags =~ s/(^|\,)X64($|\,)/\1LONG,X86_64\2/g;
500 if ($codes =~ /evex\./) {
502 } elsif ($codes =~ /(vex|xop)\./) {
506 $flagsindex = insns_flag_index
(split(',',$flags));
508 die "Error in flags $rawflags" if not defined($flagsindex);
510 @bytecode = (decodify
($codes, $relax), 0);
511 push(@bytecode_list, [@bytecode]);
512 $codes = hexstr
(@bytecode);
513 count_bytecodes
(@bytecode);
515 ("{I_$opcode, $num, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flagsindex},", $nd);
519 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
520 # offset into nasm_bytecodes
526 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
527 my $pos = $bytecode_pos{$1};
528 if (!defined($pos)) {
529 die "$fname: no position assigned to byte code $1\n";
531 $s = $` . "nasm_bytecodes+${pos}" . "$'";
537 my ($prefix, @list) = @_;
542 push(@l, sprintf("%s%02X", $prefix, $x));
549 # Turn a code string into a sequence of bytes
552 # Although these are C-syntax strings, by convention they should have
553 # only octal escapes (for directives) and hexadecimal escapes
554 # (for verbatim bytes)
555 my($codestr, $relax) = @_;
557 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
558 return byte_code_compile($1, $relax);
564 unless ($codestr eq 'ignore') {
566 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
567 push(@codes, hex $1);
570 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
571 push(@codes, oct $1);
575 die "$fname: unknown code format in \"$codestr\"\n";
583 # Turn a numeric list into a hex string
589 $s .= sprintf("%02X", $c);
594 # Here we determine the range of possible starting bytes for a given
595 # instruction. We need only consider the codes:
596 # \[1234] mean literal bytes, of course
597 # \1[0123] mean byte plus register value
598 # \330 means byte plus condition code
599 # \0 or \340 mean give up and return empty set
600 # \34[4567] mean PUSH/POP of segment registers: special case
601 # \17[234] skip is4 control byte
602 # \26x \270 skip VEX control bytes
603 # \24x \250 skip EVEX control bytes
605 my ($codestr, $relax) = @_;
612 @codes = decodify($codestr, $relax);
614 while ($c0 = shift(@codes)) {
616 if ($c0 >= 01 && $c0 <= 04) {
620 if ($c0 >= 01 && $c0 <= 04) {
622 $fbs .= sprintf("%02X", shift(@codes));
630 foreach $pfx (@disasm_prefixes) {
631 if (substr($fbs, 0, length($pfx)) eq $pfx) {
633 $fbs = substr($fbs, length($pfx));
639 return ($prefix.substr($fbs,0,2));
642 unshift(@codes, $c0);
643 } elsif ($c0 >= 010 && $c0 <= 013) {
644 return addprefix($prefix, $c1..($c1+7));
645 } elsif (($c0 & ~013) == 0144) {
646 return addprefix($prefix, $c1, $c1|2);
647 } elsif ($c0 == 0330) {
648 return addprefix($prefix, $c1..($c1+15));
649 } elsif ($c0 == 0 || $c0 == 0340) {
651 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
652 ($c0 & ~3) == 0240 || $c0 == 0250) {
655 $wlp = shift(@codes);
658 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
660 my $tuple = shift(@codes);
662 } elsif ($c0 >= 0172 && $c0 <= 173) {
663 shift(@codes); # Skip is4 control byte
665 # We really need to be able to distinguish "forbidden"
666 # and "ignorable" codes here
672 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
695 if (defined $tuple_codes{$tuplestr}) {
696 return 0300 + $tuple_codes{$tuplestr};
698 die "Undefined tuple type : $tuplestr\n";
703 # This function takes a series of byte codes in a format which is more
704 # typical of the Intel documentation, and encode it.
706 # The format looks like:
708 # [operands: opcodes]
710 # The operands word lists the order of the operands:
712 # r = register field in the modr/m
716 # s = register field of is4/imz2 field
717 # - = implicit (unencoded) operand
718 # x = indeX register of mib. 014..017 bytecodes are used.
720 # For an operand that should be filled into more than one field,
721 # enter it as e.g. "r+v".
723 sub byte_code_compile($$) {
724 my($str, $relax) = @_;
736 'ib,u' => 024, # Unsigned imm8
738 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
739 'iwd' => 034, # imm16 or imm32, depending on opsize
741 'id,s' => 0254, # imm32 sign-extended to 64 bits
742 'iwdq' => 044, # imm16/32/64, depending on addrsize
746 'rel' => 064, # 16 or 32 bit relative operand
751 'o16' => 0320, # 16-bit operand size
752 'o32' => 0321, # 32-bit operand size
753 'odf' => 0322, # Operand size is default
754 'o64' => 0324, # 64-bit operand size requiring REX.W
755 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
758 'adf' => 0312, # Address size is default
762 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
763 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
772 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
773 'nof3' => 0326, # No REP 0xF3 prefix permitted
774 'norep' => 0331, # No REP prefix permitted
775 'wait' => 0341, # Needs a wait prefix
777 'np' => 0360, # No prefix
778 'jcc8' => 0370, # Match only if Jcc possible with single byte
779 'jmp8' => 0371, # Match only if JMP possible with single byte
780 'jlen' => 0373, # Length of jump
785 # This instruction takes XMM VSIB
790 # This instruction takes YMM VSIB
795 # This instruction takes ZMM VSIB
801 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
802 die "$fname: $line: cannot parse: [$str]\n";
805 $tuple = "\L$3"; # Tuple type for AVX512
809 for ($i = 0; $i < length($opr); $i++) {
810 my $c = substr($opr,$i,1);
821 $tup = tupletype($tuple);
825 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
826 my $pc = $plain_codes{$op};
831 } elsif ($prefix_ok && $op =~ /^(66|f2|f3)$/) {
832 # 66/F2/F3 prefix used as an opcode extension
835 } elsif ($op eq 'f2') {
840 } elsif ($op =~ /^[0-9a-f]{2}$/) {
841 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
842 $codes[$litix] < 4) {
844 push(@codes, hex $op);
846 $litix = scalar(@codes);
847 push(@codes, 01, hex $op);
850 } elsif ($op eq '/r') {
851 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
852 die "$fname: $line: $op requires r and m operands\n";
854 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
855 (($oppos{'r'} & 4) ? 05 : 0);
856 push(@codes, $opex) if ($opex);
857 # if mib is composed with two separate operands - ICC style
858 push(@codes, 014 + ($oppos{'x'} & 3)) if (defined($oppos{'x'}));
859 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
861 } elsif ($op =~ m:^/([0-7])$:) {
862 if (!defined($oppos{'m'})) {
863 die "$fname: $line: $op requires m operand\n";
865 push(@codes, 06) if ($oppos{'m'} & 4);
866 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
868 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
870 my $c = $vexmap{$vexname};
871 my ($m,$w,$l,$p) = (undef,2,undef,0);
873 my @subops = split(/\./, $op);
874 shift @subops; # Drop prefix
875 foreach $oq (@subops) {
876 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
878 } elsif ($oq eq '256' || $oq eq 'l1') {
880 } elsif ($oq eq 'lig') {
882 } elsif ($oq eq 'w0') {
884 } elsif ($oq eq 'w1') {
886 } elsif ($oq eq 'wig') {
888 } elsif ($oq eq 'ww') {
890 } elsif ($oq eq 'p0') {
892 } elsif ($oq eq '66' || $oq eq 'p1') {
894 } elsif ($oq eq 'f3' || $oq eq 'p2') {
896 } elsif ($oq eq 'f2' || $oq eq 'p3') {
898 } elsif ($oq eq '0f') {
900 } elsif ($oq eq '0f38') {
902 } elsif ($oq eq '0f3a') {
904 } elsif ($oq =~ /^m([0-9]+)$/) {
906 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
907 if (!defined($oppos{'v'})) {
908 die "$fname: $line: $vexname.$oq without 'v' operand\n";
912 die "$fname: $line: undefined \U$vexname\E subcode: $oq\n";
915 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
916 die "$fname: $line: missing fields in \U$vexname\E specification\n";
918 if (defined($oppos{'v'}) && !$has_nds) {
919 die "$fname: $line: 'v' operand without ${vexname}.nds or ${vexname}.ndd\n";
921 my $minmap = ($c == 1) ? 8 : 0; # 0-31 for VEX, 8-31 for XOP
922 if ($m < $minmap || $m > 31) {
923 die "$fname: $line: Only maps ${minmap}-31 are valid for \U${vexname}\n";
925 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
926 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
928 } elsif ($op =~ /^(evex)(|\..*)$/) {
930 my ($m,$w,$l,$p) = (undef,2,undef,0);
932 my @subops = split(/\./, $op);
933 shift @subops; # Drop prefix
934 foreach $oq (@subops) {
935 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz' || $oq eq 'lig') {
937 } elsif ($oq eq '256' || $oq eq 'l1') {
939 } elsif ($oq eq '512' || $oq eq 'l2') {
941 } elsif ($oq eq 'w0') {
943 } elsif ($oq eq 'w1') {
945 } elsif ($oq eq 'wig') {
947 } elsif ($oq eq 'ww') {
949 } elsif ($oq eq 'p0') {
951 } elsif ($oq eq '66' || $oq eq 'p1') {
953 } elsif ($oq eq 'f3' || $oq eq 'p2') {
955 } elsif ($oq eq 'f2' || $oq eq 'p3') {
957 } elsif ($oq eq '0f') {
959 } elsif ($oq eq '0f38') {
961 } elsif ($oq eq '0f3a') {
963 } elsif ($oq =~ /^m([0-9]+)$/) {
965 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
966 if (!defined($oppos{'v'})) {
967 die "$fname: $line: evex.$oq without 'v' operand\n";
971 die "$fname: $line: undefined EVEX subcode: $oq\n";
974 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
975 die "$fname: $line: missing fields in EVEX specification\n";
977 if (defined($oppos{'v'}) && !$has_nds) {
978 die "$fname: $line: 'v' operand without evex.nds or evex.ndd\n";
981 die "$fname: $line: Only maps 0-15 are valid for EVEX\n";
983 push(@codes, defined($oppos{'v'}) ? 0240+($oppos{'v'} & 3) : 0250,
984 ($c << 6)+$m, ($w << 4)+($l << 2)+$p, $tup);
986 } elsif (defined $imm_codes{$op}) {
988 if ($last_imm lt 'i') {
989 die "$fname: $line: seg without an immediate operand\n";
993 if ($last_imm gt 'j') {
994 die "$fname: $line: too many immediate operands\n";
997 if (!defined($oppos{$last_imm})) {
998 die "$fname: $line: $op without '$last_imm' operand\n";
1000 push(@codes, 05) if ($oppos{$last_imm} & 4);
1001 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
1003 } elsif ($op eq '/is4') {
1004 if (!defined($oppos{'s'})) {
1005 die "$fname: $line: $op without 's' operand\n";
1007 if (defined($oppos{'i'})) {
1008 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
1010 push(@codes, 05) if ($oppos{'s'} & 4);
1011 push(@codes, 0174+($oppos{'s'} & 3));
1014 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
1016 if (!defined($oppos{'s'})) {
1017 die "$fname: $line: $op without 's' operand\n";
1019 if ($imm < 0 || $imm > 15) {
1020 die "$fname: $line: invalid imm4 value for $op: $imm\n";
1022 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
1024 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
1025 push(@codes, 0330, hex $1);
1027 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
1028 if (!defined($oppos{'r'})) {
1029 die "$fname: $line: $op without 'r' operand\n";
1031 push(@codes, 05) if ($oppos{'r'} & 4);
1032 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
1034 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1035 # Escape to enter literal bytecodes
1036 push(@codes, oct $1);
1038 die "$fname: $line: unknown operation: $op\n";