2 ## --------------------------------------------------------------------------
4 ## Copyright 1996-2017 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";
92 $n_opcodes = $n_opcodes_cc = 0;
96 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
98 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
99 warn "line $line does not contain four fields\n";
102 @fields = ($1, $2, $3, $4);
103 @field_list = ([@fields, 0]);
105 if ($fields[1] =~ /\*/) {
106 # This instruction has relaxed form(s)
107 if ($fields[2] !~ /^\[/) {
108 warn "line $line has an * operand but uses raw bytecodes\n";
113 @ops = split(/,/, $fields[1]);
114 for ($oi = 0; $oi < scalar @ops; $oi++) {
115 if ($ops[$oi] =~ /\*$/) {
117 warn "line $line has a first operand with a *\n";
124 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
125 if (($oi & ~$opmask) == 0) {
128 for ($oj = 0; $oj < scalar(@ops); $oj++) {
130 push(@xops, $ops[$oj]);
134 push(@field_list, [$fields[0], join(',', @xops),
135 $fields[2], $fields[3], $oi]);
140 foreach $fptr (@field_list) {
142 ($formatted, $nd) = format_insn
(@fields);
145 $aname = "aa_$fields[0]";
146 push @
$aname, $formatted;
148 if ( $fields[0] =~ /cc$/ ) {
149 # Conditional instruction
150 if (!defined($k_opcodes_cc{$fields[0]})) {
151 $k_opcodes_cc{$fields[0]} = $n_opcodes_cc++;
154 # Unconditional instruction
155 if (!defined($k_opcodes{$fields[0]})) {
156 $k_opcodes{$fields[0]} = $n_opcodes++;
159 if ($formatted && !$nd) {
160 push @big, $formatted;
161 my @sseq = startseq
($fields[2], $fields[4]);
163 if (!defined($dinstables{$i})) {
164 $dinstables{$i} = [];
166 push(@
{$dinstables{$i}}, $#big);
175 # Generate the bytecode array. At this point, @bytecode_list contains
176 # the full set of bytecodes.
179 # Sort by descending length
180 @bytecode_list = sort { scalar(@
$b) <=> scalar(@
$a) } @bytecode_list;
181 @bytecode_array = ();
184 foreach $bl (@bytecode_list) {
185 my $h = hexstr
(@
$bl);
186 next if (defined($bytecode_pos{$h}));
188 push(@bytecode_array, $bl);
190 $bytecode_pos{$h} = $bytecode_next;
195 undef @bytecode_list;
197 @opcodes = sort { $k_opcodes{$a} <=> $k_opcodes{$b} } keys(%k_opcodes);
198 @opcodes_cc = sort { $k_opcodes_cc{$a} <=> $k_opcodes_cc{$b} } keys(%k_opcodes_cc);
200 if ( $output eq 'b') {
201 print STDERR
"Writing $oname...\n";
203 open(B
, '>', $oname);
205 print B
"/* This file auto-generated from insns.dat by insns.pl" .
206 " - don't edit it */\n\n";
208 print B
"#include \"nasm.h\"\n";
209 print B
"#include \"insns.h\"\n\n";
211 print B
"const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
214 foreach $bl (@bytecode_array) {
215 printf B
" /* %5d */ ", $p;
226 print B
" * Bytecode frequencies (including reuse):\n";
228 for ($i = 0; $i < 32; $i++) {
230 for ($j = 0; $j < 256; $j += 32) {
231 print B
" |" if ($j);
232 printf B
" %3o:%4d", $i+$j, $bytecode_count[$i+$j];
241 if ( $output eq 'a' ) {
242 print STDERR
"Writing $oname...\n";
244 open(A
, '>', $oname);
246 print A
"/* This file auto-generated from insns.dat by insns.pl" .
247 " - don't edit it */\n\n";
249 print A
"#include \"nasm.h\"\n";
250 print A
"#include \"insns.h\"\n\n";
252 foreach $i (@opcodes, @opcodes_cc) {
253 print A
"static const struct itemplate instrux_${i}[] = {\n";
255 foreach $j (@
$aname) {
256 print A
" ", codesubst
($j), "\n";
258 print A
" ITEMPLATE_END\n};\n\n";
260 print A
"const struct itemplate * const nasm_instructions[] = {\n";
261 foreach $i (@opcodes, @opcodes_cc) {
262 print A
" instrux_${i},\n";
269 if ( $output eq 'd' ) {
270 print STDERR
"Writing $oname...\n";
272 open(D
, '>', $oname);
274 print D
"/* This file auto-generated from insns.dat by insns.pl" .
275 " - don't edit it */\n\n";
277 print D
"#include \"nasm.h\"\n";
278 print D
"#include \"insns.h\"\n\n";
280 print D
"static const struct itemplate instrux[] = {\n";
283 printf D
" /* %4d */ %s\n", $n++, codesubst
($j);
287 foreach $h (sort(keys(%dinstables))) {
288 next if ($h eq ''); # Skip pseudo-instructions
289 print D
"\nstatic const struct itemplate * const itable_${h}[] = {\n";
290 foreach $j (@
{$dinstables{$h}}) {
291 print D
" instrux + $j,\n";
297 foreach $h (@disasm_prefixes, '') {
298 for ($c = 0; $c < 256; $c++) {
299 $nn = sprintf("%s%02X", $h, $c);
300 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
301 # At least one entry in this prefix table
302 push(@prefix_list, $h);
309 foreach $h (@prefix_list) {
311 print D
"static " unless ($h eq '');
312 print D
"const struct disasm_index ";
313 print D
($h eq '') ?
'itable' : "itable_$h";
314 print D
"[256] = {\n";
315 for ($c = 0; $c < 256; $c++) {
316 $nn = sprintf("%s%02X", $h, $c);
317 if ($is_prefix{$nn}) {
318 die "$fname: ambiguous decoding of $nn\n"
319 if (defined($dinstables{$nn}));
320 printf D
" /* 0x%02x */ { itable_%s, -1 },\n", $c, $nn;
321 } elsif (defined($dinstables{$nn})) {
322 printf D
" /* 0x%02x */ { itable_%s, %u },\n", $c,
323 $nn, scalar(@
{$dinstables{$nn}});
325 printf D
" /* 0x%02x */ { NULL, 0 },\n", $c;
331 printf D
"\nconst struct disasm_index * const itable_vex[NASM_VEX_CLASSES][32][4] =\n";
333 for ($c = 0; $c < $vex_classes; $c++) {
335 for ($m = 0; $m < 32; $m++) {
337 for ($p = 0; $p < 4; $p++) {
338 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $p);
340 ($is_prefix{$vp} ?
sprintf("itable_%s,", $vp) : 'NULL,');
351 if ( $output eq 'i' ) {
352 print STDERR
"Writing $oname...\n";
354 open(I
, '>', $oname);
356 print I
"/* This file is auto-generated from insns.dat by insns.pl" .
357 " - don't edit it */\n\n";
358 print I
"/* This file in included by nasm.h */\n\n";
360 print I
"/* Instruction names */\n\n";
361 print I
"#ifndef NASM_INSNSI_H\n";
362 print I
"#define NASM_INSNSI_H 1\n\n";
363 print I
"enum opcode {\n";
365 foreach $i (@opcodes, @opcodes_cc) {
366 print I
"\tI_${i},\n";
368 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
369 $maxlen = $len if ( $len > $maxlen );
371 print I
"\tI_none = -1\n";
373 print I
"#define MAX_INSLEN ", $maxlen, "\n";
374 print I
"#define NASM_VEX_CLASSES ", $vex_classes, "\n";
375 print I
"#define NO_DECORATOR\t{", join(',',(0) x
$MAX_OPERANDS), "}\n";
376 print I
"#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
377 print I
"#endif /* NASM_INSNSI_H */\n";
382 if ( $output eq 'n' ) {
383 print STDERR
"Writing $oname...\n";
385 open(N
, '>', $oname);
387 print N
"/* This file is auto-generated from insns.dat by insns.pl" .
388 " - don't edit it */\n\n";
389 print N
"#include \"tables.h\"\n\n";
391 print N
"const char * const nasm_insn_names[] = {";
393 foreach $i (@opcodes, @opcodes_cc) {
394 print N
"," if ( !$first );
397 $ilower =~ s/cc$//; # Remove conditional cc suffix
398 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
399 print N
"\n\t\"${ilower}\"";
405 if ( $output eq 'fh') {
409 if ( $output eq 'fc') {
413 printf STDERR
"Done: %d instructions\n", $insns;
415 # Count primary bytecodes, for statistics
416 sub count_bytecodes
(@
) {
418 foreach my $bc (@_) {
423 $bytecode_count[$bc]++;
424 if ($bc >= 01 && $bc <= 04) {
426 } elsif (($bc & ~03) == 010) {
428 } elsif (($bc & ~013) == 0144) {
430 } elsif ($bc == 0172 || $bc == 0173) {
432 } elsif (($bc & ~3) == 0260 || $bc == 0270) { # VEX
434 } elsif (($bc & ~3) == 0240 || $bc == 0250) { # EVEX
436 } elsif ($bc == 0330) {
442 sub format_insn
($$$$$) {
443 my ($opcode, $operands, $codes, $flags, $relax) = @_;
444 my $num, $nd = 0, $rawflags, $flagsindex;
446 my $op, @ops, $opp, @opx, @oppx, @decos, @opevex;
448 return (undef, undef) if $operands eq "ignore";
450 # format the operands
451 $operands =~ s/\*//g;
452 $operands =~ s/:/|colon,/g;
455 if ($operands ne 'void') {
456 foreach $op (split(/,/, $operands)) {
459 foreach $opp (split(/\|/, $op)) {
461 if ($opp =~ s/^(b(32|64)|mask|z|er|sae)$//) {
465 if ($opp =~ s/(?<!\d)(8|16|32|64|80|128|256|512)$//) {
466 push(@oppx, "bits$1");
468 $opp =~ s/^mem$/memory/;
469 $opp =~ s/^memory_offs$/mem_offs/;
470 $opp =~ s/^imm$/immediate/;
471 $opp =~ s/^([a-z]+)rm$/rm_$1/;
472 $opp =~ s/^rm$/rm_gpr/;
473 $opp =~ s/^reg$/reg_gpr/;
474 # only for evex insns, high-16 regs are allowed
475 if ($codes !~ /(^|\s)evex\./) {
476 $opp =~ s/^(rm_[xyz]mm)$/$1_l16/;
477 $opp =~ s/^([xyz]mm)reg$/$1_l16/;
479 push(@opx, $opp, @oppx) if $opp;
481 $op = join('|', @opx);
483 push(@decos, (@opevex ?
join('|', @opevex) : '0'));
488 while (scalar(@ops) < $MAX_OPERANDS) {
492 $operands = join(',', @ops);
493 $operands =~ tr/a-z/A-Z/;
495 $decorators = "{" . join(',', @decos) . "}";
496 if ($decorators =~ /^{(0,)+0}$/) {
497 $decorators = "NO_DECORATOR";
499 $decorators =~ tr/a-z/A-Z/;
502 $nd = 1 if $flags =~ /(^|\,)ND($|\,)/;
503 $flags =~ s/(^|\,)ND($|\,)/\1/g;
504 $flags =~ s/(^|\,)X64($|\,)/\1LONG,X86_64\2/g;
505 if ($codes =~ /evex\./) {
507 } elsif ($codes =~ /(vex|xop)\./) {
511 $flagsindex = insns_flag_index
(split(',',$flags));
513 die "Error in flags $rawflags" if not defined($flagsindex);
515 @bytecode = (decodify
($codes, $relax), 0);
516 push(@bytecode_list, [@bytecode]);
517 $codes = hexstr
(@bytecode);
518 count_bytecodes
(@bytecode);
520 ("{I_$opcode, $num, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flagsindex},", $nd);
524 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
525 # offset into nasm_bytecodes
531 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
532 my $pos = $bytecode_pos{$1};
533 if (!defined($pos)) {
534 die "$fname: no position assigned to byte code $1\n";
536 $s = $` . "nasm_bytecodes+${pos}" . "$'";
542 my ($prefix, @list) = @_;
547 push(@l, sprintf("%s%02X", $prefix, $x));
554 # Turn a code string into a sequence of bytes
557 # Although these are C-syntax strings, by convention they should have
558 # only octal escapes (for directives) and hexadecimal escapes
559 # (for verbatim bytes)
560 my($codestr, $relax) = @_;
562 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
563 return byte_code_compile($1, $relax);
569 unless ($codestr eq 'ignore') {
571 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
572 push(@codes, hex $1);
575 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
576 push(@codes, oct $1);
580 die "$fname: unknown code format in \"$codestr\"\n";
588 # Turn a numeric list into a hex string
594 $s .= sprintf("%02X", $c);
599 # Here we determine the range of possible starting bytes for a given
600 # instruction. We need only consider the codes:
601 # \[1234] mean literal bytes, of course
602 # \1[0123] mean byte plus register value
603 # \330 means byte plus condition code
604 # \0 or \340 mean give up and return empty set
605 # \34[4567] mean PUSH/POP of segment registers: special case
606 # \17[234] skip is4 control byte
607 # \26x \270 skip VEX control bytes
608 # \24x \250 skip EVEX control bytes
610 my ($codestr, $relax) = @_;
617 @codes = decodify($codestr, $relax);
619 while ($c0 = shift(@codes)) {
621 if ($c0 >= 01 && $c0 <= 04) {
625 if ($c0 >= 01 && $c0 <= 04) {
627 $fbs .= sprintf("%02X", shift(@codes));
635 foreach $pfx (@disasm_prefixes) {
636 if (substr($fbs, 0, length($pfx)) eq $pfx) {
638 $fbs = substr($fbs, length($pfx));
644 return ($prefix.substr($fbs,0,2));
647 unshift(@codes, $c0);
648 } elsif ($c0 >= 010 && $c0 <= 013) {
649 return addprefix($prefix, $c1..($c1+7));
650 } elsif (($c0 & ~013) == 0144) {
651 return addprefix($prefix, $c1, $c1|2);
652 } elsif ($c0 == 0330) {
653 return addprefix($prefix, $c1..($c1+15));
654 } elsif ($c0 == 0 || $c0 == 0340) {
656 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
657 ($c0 & ~3) == 0240 || $c0 == 0250) {
660 $wlp = shift(@codes);
663 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
665 my $tuple = shift(@codes);
667 } elsif ($c0 >= 0172 && $c0 <= 173) {
668 shift(@codes); # Skip is4 control byte
670 # We really need to be able to distinguish "forbidden"
671 # and "ignorable" codes here
677 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
700 if (defined $tuple_codes{$tuplestr}) {
701 return 0300 + $tuple_codes{$tuplestr};
703 die "Undefined tuple type : $tuplestr\n";
708 # This function takes a series of byte codes in a format which is more
709 # typical of the Intel documentation, and encode it.
711 # The format looks like:
713 # [operands: opcodes]
715 # The operands word lists the order of the operands:
717 # r = register field in the modr/m
721 # s = register field of is4/imz2 field
722 # - = implicit (unencoded) operand
723 # x = indeX register of mib. 014..017 bytecodes are used.
725 # For an operand that should be filled into more than one field,
726 # enter it as e.g. "r+v".
728 sub byte_code_compile($$) {
729 my($str, $relax) = @_;
741 'ib,u' => 024, # Unsigned imm8
743 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
744 'iwd' => 034, # imm16 or imm32, depending on opsize
746 'id,s' => 0254, # imm32 sign-extended to 64 bits
747 'iwdq' => 044, # imm16/32/64, depending on addrsize
751 'rel' => 064, # 16 or 32 bit relative operand
756 'o16' => 0320, # 16-bit operand size
757 'o32' => 0321, # 32-bit operand size
758 'odf' => 0322, # Operand size is default
759 'o64' => 0324, # 64-bit operand size requiring REX.W
760 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
763 'adf' => 0312, # Address size is default
767 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
768 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
777 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
778 'nof3' => 0326, # No REP 0xF3 prefix permitted
779 'norep' => 0331, # No REP prefix permitted
780 'wait' => 0341, # Needs a wait prefix
782 'np' => 0360, # No prefix
783 'jcc8' => 0370, # Match only if Jcc possible with single byte
784 'jmp8' => 0371, # Match only if JMP possible with single byte
785 'jlen' => 0373, # Length of jump
790 # This instruction takes XMM VSIB
795 # This instruction takes YMM VSIB
800 # This instruction takes ZMM VSIB
806 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
807 die "$fname: $line: cannot parse: [$str]\n";
810 $tuple = "\L$3"; # Tuple type for AVX512
814 for ($i = 0; $i < length($opr); $i++) {
815 my $c = substr($opr,$i,1);
826 $tup = tupletype($tuple);
830 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
831 my $pc = $plain_codes{$op};
836 } elsif ($prefix_ok && $op =~ /^(66|f2|f3)$/) {
837 # 66/F2/F3 prefix used as an opcode extension
840 } elsif ($op eq 'f2') {
845 } elsif ($op =~ /^[0-9a-f]{2}$/) {
846 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
847 $codes[$litix] < 4) {
849 push(@codes, hex $op);
851 $litix = scalar(@codes);
852 push(@codes, 01, hex $op);
855 } elsif ($op eq '/r') {
856 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
857 die "$fname: $line: $op requires r and m operands\n";
859 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
860 (($oppos{'r'} & 4) ? 05 : 0);
861 push(@codes, $opex) if ($opex);
862 # if mib is composed with two separate operands - ICC style
863 push(@codes, 014 + ($oppos{'x'} & 3)) if (defined($oppos{'x'}));
864 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
866 } elsif ($op =~ m:^/([0-7])$:) {
867 if (!defined($oppos{'m'})) {
868 die "$fname: $line: $op requires m operand\n";
870 push(@codes, 06) if ($oppos{'m'} & 4);
871 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
873 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
875 my $c = $vexmap{$vexname};
876 my ($m,$w,$l,$p) = (undef,2,undef,0);
878 my @subops = split(/\./, $op);
879 shift @subops; # Drop prefix
880 foreach $oq (@subops) {
881 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
883 } elsif ($oq eq '256' || $oq eq 'l1') {
885 } elsif ($oq eq 'lig') {
887 } elsif ($oq eq 'w0') {
889 } elsif ($oq eq 'w1') {
891 } elsif ($oq eq 'wig') {
893 } elsif ($oq eq 'ww') {
895 } elsif ($oq eq 'p0') {
897 } elsif ($oq eq '66' || $oq eq 'p1') {
899 } elsif ($oq eq 'f3' || $oq eq 'p2') {
901 } elsif ($oq eq 'f2' || $oq eq 'p3') {
903 } elsif ($oq eq '0f') {
905 } elsif ($oq eq '0f38') {
907 } elsif ($oq eq '0f3a') {
909 } elsif ($oq =~ /^m([0-9]+)$/) {
911 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
912 if (!defined($oppos{'v'})) {
913 die "$fname: $line: $vexname.$oq without 'v' operand\n";
917 die "$fname: $line: undefined \U$vexname\E subcode: $oq\n";
920 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
921 die "$fname: $line: missing fields in \U$vexname\E specification\n";
923 if (defined($oppos{'v'}) && !$has_nds) {
924 die "$fname: $line: 'v' operand without ${vexname}.nds or ${vexname}.ndd\n";
926 my $minmap = ($c == 1) ? 8 : 0; # 0-31 for VEX, 8-31 for XOP
927 if ($m < $minmap || $m > 31) {
928 die "$fname: $line: Only maps ${minmap}-31 are valid for \U${vexname}\n";
930 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
931 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
933 } elsif ($op =~ /^(evex)(|\..*)$/) {
935 my ($m,$w,$l,$p) = (undef,2,undef,0);
937 my @subops = split(/\./, $op);
938 shift @subops; # Drop prefix
939 foreach $oq (@subops) {
940 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz' || $oq eq 'lig') {
942 } elsif ($oq eq '256' || $oq eq 'l1') {
944 } elsif ($oq eq '512' || $oq eq 'l2') {
946 } elsif ($oq eq 'w0') {
948 } elsif ($oq eq 'w1') {
950 } elsif ($oq eq 'wig') {
952 } elsif ($oq eq 'ww') {
954 } elsif ($oq eq 'p0') {
956 } elsif ($oq eq '66' || $oq eq 'p1') {
958 } elsif ($oq eq 'f3' || $oq eq 'p2') {
960 } elsif ($oq eq 'f2' || $oq eq 'p3') {
962 } elsif ($oq eq '0f') {
964 } elsif ($oq eq '0f38') {
966 } elsif ($oq eq '0f3a') {
968 } elsif ($oq =~ /^m([0-9]+)$/) {
970 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
971 if (!defined($oppos{'v'})) {
972 die "$fname: $line: evex.$oq without 'v' operand\n";
976 die "$fname: $line: undefined EVEX subcode: $oq\n";
979 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
980 die "$fname: $line: missing fields in EVEX specification\n";
982 if (defined($oppos{'v'}) && !$has_nds) {
983 die "$fname: $line: 'v' operand without evex.nds or evex.ndd\n";
986 die "$fname: $line: Only maps 0-15 are valid for EVEX\n";
988 push(@codes, defined($oppos{'v'}) ? 0240+($oppos{'v'} & 3) : 0250,
989 ($c << 6)+$m, ($w << 4)+($l << 2)+$p, $tup);
991 } elsif (defined $imm_codes{$op}) {
993 if ($last_imm lt 'i') {
994 die "$fname: $line: seg without an immediate operand\n";
998 if ($last_imm gt 'j') {
999 die "$fname: $line: too many immediate operands\n";
1002 if (!defined($oppos{$last_imm})) {
1003 die "$fname: $line: $op without '$last_imm' operand\n";
1005 push(@codes, 05) if ($oppos{$last_imm} & 4);
1006 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
1008 } elsif ($op eq '/is4') {
1009 if (!defined($oppos{'s'})) {
1010 die "$fname: $line: $op without 's' operand\n";
1012 if (defined($oppos{'i'})) {
1013 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
1015 push(@codes, 05) if ($oppos{'s'} & 4);
1016 push(@codes, 0174+($oppos{'s'} & 3));
1019 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
1021 if (!defined($oppos{'s'})) {
1022 die "$fname: $line: $op without 's' operand\n";
1024 if ($imm < 0 || $imm > 15) {
1025 die "$fname: $line: invalid imm4 value for $op: $imm\n";
1027 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
1029 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
1030 push(@codes, 0330, hex $1);
1032 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
1033 if (!defined($oppos{'r'})) {
1034 die "$fname: $line: $op without 'r' operand\n";
1036 push(@codes, 05) if ($oppos{'r'} & 4);
1037 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
1039 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1040 # Escape to enter literal bytecodes
1041 push(@codes, oct $1);
1043 die "$fname: $line: unknown operation: $op\n";