2 ## --------------------------------------------------------------------------
4 ## Copyright 1996-2013 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 # Opcode prefixes which need their own opcode tables
41 # LONGER PREFIXES FIRST!
42 @disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
44 # This should match MAX_OPERANDS from nasm.h
47 # Add VEX/XOP prefixes
48 @vex_class = ( 'vex', 'xop', 'evex' );
49 $vex_classes = scalar(@vex_class);
52 for ($c = 0; $c < $vex_classes; $c++) {
53 $vexmap{$vex_class[$c]} = $c;
54 for ($m = 0; $m < 32; $m++) {
55 for ($p = 0; $p < 4; $p++) {
56 push(@vexlist, sprintf("%s%02X%01X", $vex_class[$c], $m, $p));
60 @disasm_prefixes = (@vexlist, @disasm_prefixes);
62 @bytecode_count = (0) x 256;
64 print STDERR "Reading insns.dat...\n";
68 foreach $arg ( @ARGV ) {
69 if ( $arg =~ /^\-/ ) {
70 if ( $arg =~ /^\-([abdin])$/ ) {
73 die "$0: Unknown option: ${arg}\n";
80 $fname = "insns.dat" unless $fname = $args[0];
81 open (F, $fname) || die "unable to open $fname";
91 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
93 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
94 warn "line $line does not contain four fields\n";
97 @fields = ($1, $2, $3, $4);
98 @field_list = ([@fields, 0]);
100 if ($fields[1] =~ /\*/) {
101 # This instruction has relaxed form(s)
102 if ($fields[2] !~ /^\[/) {
103 warn "line $line has an * operand but uses raw bytecodes\n";
108 @ops = split(/,/, $fields[1]);
109 for ($oi = 0; $oi < scalar @ops; $oi++) {
110 if ($ops[$oi] =~ /\*$/) {
112 warn "line $line has a first operand with a *\n";
119 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
120 if (($oi & ~$opmask) == 0) {
123 for ($oj = 0; $oj < scalar(@ops); $oj++) {
125 push(@xops, $ops[$oj]);
129 push(@field_list, [$fields[0], join(',', @xops),
130 $fields[2], $fields[3], $oi]);
135 foreach $fptr (@field_list) {
137 ($formatted, $nd) = format_insn(@fields);
140 $aname = "aa_$fields[0]";
141 push @$aname, $formatted;
143 if ( $fields[0] =~ /cc$/ ) {
144 # Conditional instruction
145 $k_opcodes_cc{$fields[0]}++;
147 # Unconditional instruction
148 $k_opcodes{$fields[0]}++;
150 if ($formatted && !$nd) {
151 push @big, $formatted;
152 my @sseq = startseq($fields[2], $fields[4]);
154 if (!defined($dinstables{$i})) {
155 $dinstables{$i} = [];
157 push(@{$dinstables{$i}}, $#big);
166 # Generate the bytecode array. At this point, @bytecode_list contains
167 # the full set of bytecodes.
170 # Sort by descending length
171 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
172 @bytecode_array = ();
175 foreach $bl (@bytecode_list) {
176 my $h = hexstr(@$bl);
177 next if (defined($bytecode_pos{$h}));
179 push(@bytecode_array, $bl);
181 $bytecode_pos{$h} = $bytecode_next;
186 undef @bytecode_list;
188 @opcodes = sort keys(%k_opcodes);
189 @opcodes_cc = sort keys(%k_opcodes_cc);
191 if ( !defined($output) || $output eq 'b') {
192 print STDERR "Writing insnsb.c...\n";
196 print B "/* This file auto-generated from insns.dat by insns.pl" .
197 " - don't edit it */\n\n";
199 print B "#include \"nasm.h\"\n";
200 print B "#include \"insns.h\"\n\n";
202 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
205 foreach $bl (@bytecode_array) {
206 printf B " /* %5d */ ", $p;
217 print B " * Bytecode frequencies (including reuse):\n";
219 for ($i = 0; $i < 32; $i++) {
221 for ($j = 0; $j < 256; $j += 32) {
222 print B " |" if ($j);
223 printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
232 if ( !defined($output) || $output eq 'a' ) {
233 print STDERR "Writing insnsa.c...\n";
237 print A "/* This file auto-generated from insns.dat by insns.pl" .
238 " - don't edit it */\n\n";
240 print A "#include \"nasm.h\"\n";
241 print A "#include \"insns.h\"\n\n";
243 foreach $i (@opcodes, @opcodes_cc) {
244 print A "static const struct itemplate instrux_${i}[] = {\n";
246 foreach $j (@$aname) {
247 print A " ", codesubst($j), "\n";
249 print A " ITEMPLATE_END\n};\n\n";
251 print A "const struct itemplate * const nasm_instructions[] = {\n";
252 foreach $i (@opcodes, @opcodes_cc) {
253 print A " instrux_${i},\n";
260 if ( !defined($output) || $output eq 'd' ) {
261 print STDERR "Writing insnsd.c...\n";
265 print D "/* This file auto-generated from insns.dat by insns.pl" .
266 " - don't edit it */\n\n";
268 print D "#include \"nasm.h\"\n";
269 print D "#include \"insns.h\"\n\n";
271 print D "static const struct itemplate instrux[] = {\n";
274 printf D " /* %4d */ %s\n", $n++, codesubst($j);
278 foreach $h (sort(keys(%dinstables))) {
279 next if ($h eq ''); # Skip pseudo-instructions
280 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
281 foreach $j (@{$dinstables{$h}}) {
282 print D " instrux + $j,\n";
288 foreach $h (@disasm_prefixes, '') {
289 for ($c = 0; $c < 256; $c++) {
290 $nn = sprintf("%s%02X", $h, $c);
291 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
292 # At least one entry in this prefix table
293 push(@prefix_list, $h);
300 foreach $h (@prefix_list) {
302 print D "static " unless ($h eq '');
303 print D "const struct disasm_index ";
304 print D ($h eq '') ? 'itable' : "itable_$h";
305 print D "[256] = {\n";
306 for ($c = 0; $c < 256; $c++) {
307 $nn = sprintf("%s%02X", $h, $c);
308 if ($is_prefix{$nn}) {
309 die "$fname: ambiguous decoding of $nn\n"
310 if (defined($dinstables{$nn}));
311 printf D " /* 0x%02x */ { itable_%s, -1 },\n", $c, $nn;
312 } elsif (defined($dinstables{$nn})) {
313 printf D " /* 0x%02x */ { itable_%s, %u },\n", $c,
314 $nn, scalar(@{$dinstables{$nn}});
316 printf D " /* 0x%02x */ { NULL, 0 },\n", $c;
322 printf D "\nconst struct disasm_index * const itable_vex[NASM_VEX_CLASSES][32][4] =\n";
324 for ($c = 0; $c < $vex_classes; $c++) {
326 for ($m = 0; $m < 32; $m++) {
328 for ($p = 0; $p < 4; $p++) {
329 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $p);
331 ($is_prefix{$vp} ? sprintf("itable_%s,", $vp) : 'NULL,');
342 if ( !defined($output) || $output eq 'i' ) {
343 print STDERR "Writing insnsi.h...\n";
347 print I "/* This file is auto-generated from insns.dat by insns.pl" .
348 " - don't edit it */\n\n";
349 print I "/* This file in included by nasm.h */\n\n";
351 print I "/* Instruction names */\n\n";
352 print I "#ifndef NASM_INSNSI_H\n";
353 print I "#define NASM_INSNSI_H 1\n\n";
354 print I "enum opcode {\n";
356 foreach $i (@opcodes, @opcodes_cc) {
357 print I "\tI_${i},\n";
359 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
360 $maxlen = $len if ( $len > $maxlen );
362 print I "\tI_none = -1\n";
364 print I "#define MAX_INSLEN ", $maxlen, "\n";
365 print I "#define NASM_VEX_CLASSES ", $vex_classes, "\n";
366 print I "#define NO_DECORATOR\t{", join(',',(0) x $MAX_OPERANDS), "}\n";
367 print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
368 print I "#endif /* NASM_INSNSI_H */\n";
373 if ( !defined($output) || $output eq 'n' ) {
374 print STDERR "Writing insnsn.c...\n";
378 print N "/* This file is auto-generated from insns.dat by insns.pl" .
379 " - don't edit it */\n\n";
380 print N "#include \"tables.h\"\n\n";
382 print N "const char * const nasm_insn_names[] = {";
384 foreach $i (@opcodes, @opcodes_cc) {
385 print N "," if ( !$first );
388 $ilower =~ s/cc$//; # Remove conditional cc suffix
389 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
390 print N "\n\t\"${ilower}\"";
396 printf STDERR "Done: %d instructions\n", $insns;
398 # Count primary bytecodes, for statistics
399 sub count_bytecodes(@) {
401 foreach my $bc (@_) {
406 $bytecode_count[$bc]++;
407 if ($bc >= 01 && $bc <= 04) {
409 } elsif (($bc & ~03) == 010) {
411 } elsif (($bc & ~013) == 0144) {
413 } elsif ($bc == 0172 || $bc == 0173) {
415 } elsif (($bc & ~3) == 0260 || $bc == 0270) { # VEX
417 } elsif (($bc & ~3) == 0240 || $bc == 0250) { # EVEX
419 } elsif ($bc == 0330) {
425 sub format_insn($$$$$) {
426 my ($opcode, $operands, $codes, $flags, $relax) = @_;
429 my $op, @ops, $opp, @opx, @oppx, @decos, @opevex;
431 return (undef, undef) if $operands eq "ignore";
433 # format the operands
434 $operands =~ s/\*//g;
435 $operands =~ s/:/|colon,/g;
438 if ($operands ne 'void') {
439 foreach $op (split(/,/, $operands)) {
442 foreach $opp (split(/\|/, $op)) {
444 if ($opp =~ s/^(b(32|64)|mask|z|er|sae)$//) {
448 if ($opp =~ s/(?<!\d)(8|16|32|64|80|128|256|512)$//) {
449 push(@oppx, "bits$1");
451 $opp =~ s/^mem$/memory/;
452 $opp =~ s/^memory_offs$/mem_offs/;
453 $opp =~ s/^imm$/immediate/;
454 $opp =~ s/^([a-z]+)rm$/rm_$1/;
455 $opp =~ s/^rm$/rm_gpr/;
456 $opp =~ s/^reg$/reg_gpr/;
457 push(@opx, $opp, @oppx) if $opp;
459 $op = join('|', @opx);
461 push(@decos, (@opevex ? join('|', @opevex) : '0'));
466 while (scalar(@ops) < $MAX_OPERANDS) {
470 $operands = join(',', @ops);
471 $operands =~ tr/a-z/A-Z/;
473 $decorators = "{" . join(',', @decos) . "}";
474 if ($decorators =~ /^{(0,)+0}$/) {
475 $decorators = "NO_DECORATOR";
477 $decorators =~ tr/a-z/A-Z/;
480 $flags =~ s/,/|IF_/g;
481 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
482 $flags = "IF_" . $flags;
484 @bytecode = (decodify($codes, $relax), 0);
485 push(@bytecode_list, [@bytecode]);
486 $codes = hexstr(@bytecode);
487 count_bytecodes(@bytecode);
489 ("{I_$opcode, $num, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flags},", $nd);
493 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
494 # offset into nasm_bytecodes
500 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
501 my $pos = $bytecode_pos{$1};
502 if (!defined($pos)) {
503 die "$fname: no position assigned to byte code $1\n";
505 $s = $` . "nasm_bytecodes+${pos}" . "$'";
511 my ($prefix, @list) = @_;
516 push(@l, sprintf("%s%02X", $prefix, $x));
523 # Turn a code string into a sequence of bytes
526 # Although these are C-syntax strings, by convention they should have
527 # only octal escapes (for directives) and hexadecimal escapes
528 # (for verbatim bytes)
529 my($codestr, $relax) = @_;
531 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
532 return byte_code_compile($1, $relax);
538 unless ($codestr eq 'ignore') {
540 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
541 push(@codes, hex $1);
544 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
545 push(@codes, oct $1);
549 die "$fname: unknown code format in \"$codestr\"\n";
557 # Turn a numeric list into a hex string
563 $s .= sprintf("%02X", $c);
568 # Here we determine the range of possible starting bytes for a given
569 # instruction. We need only consider the codes:
570 # \[1234] mean literal bytes, of course
571 # \1[0123] mean byte plus register value
572 # \330 means byte plus condition code
573 # \0 or \340 mean give up and return empty set
574 # \34[4567] mean PUSH/POP of segment registers: special case
575 # \17[234] skip is4 control byte
576 # \26x \270 skip VEX control bytes
577 # \24x \250 skip EVEX control bytes
579 my ($codestr, $relax) = @_;
586 @codes = decodify($codestr, $relax);
588 while ($c0 = shift(@codes)) {
590 if ($c0 >= 01 && $c0 <= 04) {
594 if ($c0 >= 01 && $c0 <= 04) {
596 $fbs .= sprintf("%02X", shift(@codes));
604 foreach $pfx (@disasm_prefixes) {
605 if (substr($fbs, 0, length($pfx)) eq $pfx) {
607 $fbs = substr($fbs, length($pfx));
613 return ($prefix.substr($fbs,0,2));
616 unshift(@codes, $c0);
617 } elsif ($c0 >= 010 && $c0 <= 013) {
618 return addprefix($prefix, $c1..($c1+7));
619 } elsif (($c0 & ~013) == 0144) {
620 return addprefix($prefix, $c1, $c1|2);
621 } elsif ($c0 == 0330) {
622 return addprefix($prefix, $c1..($c1+15));
623 } elsif ($c0 == 0 || $c0 == 0340) {
625 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
626 ($c0 & ~3) == 0240 || $c0 == 0250) {
629 $wlp = shift(@codes);
632 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
634 my $tuple = shift(@codes);
636 } elsif ($c0 >= 0172 && $c0 <= 173) {
637 shift(@codes); # Skip is4 control byte
639 # We really need to be able to distinguish "forbidden"
640 # and "ignorable" codes here
646 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
669 if (defined $tuple_codes{$tuplestr}) {
670 return 0300 + $tuple_codes{$tuplestr};
672 die "Undefined tuple type : $tuplestr\n";
677 # This function takes a series of byte codes in a format which is more
678 # typical of the Intel documentation, and encode it.
680 # The format looks like:
682 # [operands: opcodes]
684 # The operands word lists the order of the operands:
686 # r = register field in the modr/m
690 # s = register field of is4/imz2 field
691 # - = implicit (unencoded) operand
693 # For an operand that should be filled into more than one field,
694 # enter it as e.g. "r+v".
696 sub byte_code_compile($$) {
697 my($str, $relax) = @_;
709 'ib,u' => 024, # Unsigned imm8
711 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
712 'iwd' => 034, # imm16 or imm32, depending on opsize
714 'id,s' => 0254, # imm32 sign-extended to 64 bits
715 'iwdq' => 044, # imm16/32/64, depending on addrsize
719 'rel' => 064, # 16 or 32 bit relative operand
724 'o16' => 0320, # 16-bit operand size
725 'o32' => 0321, # 32-bit operand size
726 'odf' => 0322, # Operand size is default
727 'o64' => 0324, # 64-bit operand size requiring REX.W
728 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
731 'adf' => 0312, # Address size is default
735 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
736 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
745 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
746 'nof3' => 0326, # No REP 0xF3 prefix permitted
747 'norep' => 0331, # No REP prefix permitted
748 'wait' => 0341, # Needs a wait prefix
750 'jcc8' => 0370, # Match only if Jcc possible with single byte
751 'jmp8' => 0371, # Match only if JMP possible with single byte
752 'jlen' => 0373, # Length of jump
757 # This instruction takes XMM VSIB
762 # This instruction takes YMM VSIB
767 # This instruction takes ZMM VSIB
773 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
774 die "$fname: $line: cannot parse: [$str]\n";
777 $tuple = "\L$3"; # Tuple type for AVX512
781 for ($i = 0; $i < length($opr); $i++) {
782 my $c = substr($opr,$i,1);
793 $tup = tupletype($tuple);
797 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
798 my $pc = $plain_codes{$op};
803 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
804 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
807 } elsif ($op eq 'f2') {
809 } elsif ($op eq 'f3') {
814 } elsif ($op =~ /^[0-9a-f]{2}$/) {
815 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
816 $codes[$litix] < 4) {
818 push(@codes, hex $op);
820 $litix = scalar(@codes);
821 push(@codes, 01, hex $op);
824 } elsif ($op eq '/r') {
825 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
826 die "$fname: $line: $op requires r and m operands\n";
828 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
829 (($oppos{'r'} & 4) ? 05 : 0);
830 push(@codes, $opex) if ($opex);
831 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
833 } elsif ($op =~ m:^/([0-7])$:) {
834 if (!defined($oppos{'m'})) {
835 die "$fname: $line: $op requires m operand\n";
837 push(@codes, 06) if ($oppos{'m'} & 4);
838 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
840 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
842 my ($m,$w,$l,$p) = (undef,2,undef,0);
844 my @subops = split(/\./, $op);
845 shift @subops; # Drop prefix
846 foreach $oq (@subops) {
847 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
849 } elsif ($oq eq '256' || $oq eq 'l1') {
851 } elsif ($oq eq 'lig') {
853 } elsif ($oq eq 'w0') {
855 } elsif ($oq eq 'w1') {
857 } elsif ($oq eq 'wig') {
859 } elsif ($oq eq 'ww') {
861 } elsif ($oq eq 'p0') {
863 } elsif ($oq eq '66' || $oq eq 'p1') {
865 } elsif ($oq eq 'f3' || $oq eq 'p2') {
867 } elsif ($oq eq 'f2' || $oq eq 'p3') {
869 } elsif ($oq eq '0f') {
871 } elsif ($oq eq '0f38') {
873 } elsif ($oq eq '0f3a') {
875 } elsif ($oq =~ /^m([0-9]+)$/) {
877 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
878 if (!defined($oppos{'v'})) {
879 die "$fname: $line: vex.$oq without 'v' operand\n";
883 die "$fname: $line: undefined VEX subcode: $oq\n";
886 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
887 die "$fname: $line: missing fields in VEX specification\n";
889 if (defined($oppos{'v'}) && !$has_nds) {
890 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
892 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
893 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
895 } elsif ($op =~ /^(evex)(|\..*)$/) {
897 my ($m,$w,$l,$p) = (undef,2,undef,0);
899 my @subops = split(/\./, $op);
900 shift @subops; # Drop prefix
901 foreach $oq (@subops) {
902 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz' || $oq eq 'lig') {
904 } elsif ($oq eq '256' || $oq eq 'l1') {
906 } elsif ($oq eq '512' || $oq eq 'l2') {
908 } elsif ($oq eq 'w0') {
910 } elsif ($oq eq 'w1') {
912 } elsif ($oq eq 'wig') {
914 } elsif ($oq eq 'ww') {
916 } elsif ($oq eq 'p0') {
918 } elsif ($oq eq '66' || $oq eq 'p1') {
920 } elsif ($oq eq 'f3' || $oq eq 'p2') {
922 } elsif ($oq eq 'f2' || $oq eq 'p3') {
924 } elsif ($oq eq '0f') {
926 } elsif ($oq eq '0f38') {
928 } elsif ($oq eq '0f3a') {
930 } elsif ($oq =~ /^m([0-9]+)$/) {
932 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
933 if (!defined($oppos{'v'})) {
934 die "$fname: $line: evex.$oq without 'v' operand\n";
938 die "$fname: $line: undefined EVEX subcode: $oq\n";
941 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
942 die "$fname: $line: missing fields in EVEX specification\n";
944 if (defined($oppos{'v'}) && !$has_nds) {
945 die "$fname: $line: 'v' operand without evex.nds or evex.ndd\n";
947 push(@codes, defined($oppos{'v'}) ? 0240+($oppos{'v'} & 3) : 0250,
948 ($c << 6)+$m, ($w << 4)+($l << 2)+$p, $tup);
950 } elsif (defined $imm_codes{$op}) {
952 if ($last_imm lt 'i') {
953 die "$fname: $line: seg without an immediate operand\n";
957 if ($last_imm gt 'j') {
958 die "$fname: $line: too many immediate operands\n";
961 if (!defined($oppos{$last_imm})) {
962 die "$fname: $line: $op without '$last_imm' operand\n";
964 push(@codes, 05) if ($oppos{$last_imm} & 4);
965 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
967 } elsif ($op eq '/is4') {
968 if (!defined($oppos{'s'})) {
969 die "$fname: $line: $op without 's' operand\n";
971 if (defined($oppos{'i'})) {
972 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
974 push(@codes, 05) if ($oppos{'s'} & 4);
975 push(@codes, 0174+($oppos{'s'} & 3));
978 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
980 if (!defined($oppos{'s'})) {
981 die "$fname: $line: $op without 's' operand\n";
983 if ($imm < 0 || $imm > 15) {
984 die "$fname: $line: invalid imm4 value for $op: $imm\n";
986 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
988 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
989 push(@codes, 0330, hex $1);
991 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
992 if (!defined($oppos{'r'})) {
993 die "$fname: $line: $op without 'r' operand\n";
995 push(@codes, 05) if ($oppos{'r'} & 4);
996 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
998 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
999 # Escape to enter literal bytecodes
1000 push(@codes, oct $1);
1002 die "$fname: $line: unknown operation: $op\n";