NASM 2.07rc4
[nasm/avx512.git] / insns.pl
blobe0daccad709ffad2fa861e0e49b0864799372209
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2009 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
7 ##
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.
18 ##
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 ## --------------------------------------------------------------------------
36 # insns.pl
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
45 $MAX_OPERANDS = 5;
47 # Add VEX/XOP prefixes
48 @vex_class = ( 'vex', 'xop' );
49 $vex_classes = scalar(@vex_class);
50 @vexlist = ();
51 %vexmap = ();
52 for ($c = 0; $c < $vex_classes; $c++) {
53 $vexmap{$vex_class[$c]} = $c;
54 for ($m = 0; $m < 32; $m++) {
55 for ($lp = 0; $lp < 8; $lp++) {
56 push(@vexlist, sprintf("%s%02X%01X", $vex_class[$c], $m, $lp));
60 @disasm_prefixes = (@vexlist, @disasm_prefixes);
62 @bytecode_count = (0) x 256;
64 print STDERR "Reading insns.dat...\n";
66 @args = ();
67 undef $output;
68 foreach $arg ( @ARGV ) {
69 if ( $arg =~ /^\-/ ) {
70 if ( $arg =~ /^\-([abdin])$/ ) {
71 $output = $1;
72 } else {
73 die "$0: Unknown option: ${arg}\n";
75 } else {
76 push (@args, $arg);
80 $fname = "insns.dat" unless $fname = $args[0];
81 open (F, $fname) || die "unable to open $fname";
83 %dinstables = ();
84 @bytecode_list = ();
86 $line = 0;
87 $insns = 0;
88 while (<F>) {
89 $line++;
90 chomp;
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";
95 next;
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";
104 next;
107 $opmask = 0;
108 @ops = split(/,/, $fields[1]);
109 for ($oi = 0; $oi < scalar @ops; $oi++) {
110 if ($ops[$oi] =~ /\*$/) {
111 if ($oi == 0) {
112 warn "line $line has a first operand with a *\n";
113 next;
115 $opmask |= 1 << $oi;
119 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
120 if (($oi & ~$opmask) == 0) {
121 my @xops = ();
122 my $omask = ~$oi;
123 for ($oj = 0; $oj < scalar(@ops); $oj++) {
124 if ($omask & 1) {
125 push(@xops, $ops[$oj]);
127 $omask >>= 1;
129 push(@field_list, [$fields[0], join(',', @xops),
130 $fields[2], $fields[3], $oi]);
135 foreach $fptr (@field_list) {
136 @fields = @$fptr;
137 ($formatted, $nd) = format_insn(@fields);
138 if ($formatted) {
139 $insns++;
140 $aname = "aa_$fields[0]";
141 push @$aname, $formatted;
143 if ( $fields[0] =~ /cc$/ ) {
144 # Conditional instruction
145 $k_opcodes_cc{$fields[0]}++;
146 } else {
147 # Unconditional instruction
148 $k_opcodes{$fields[0]}++;
150 if ($formatted && !$nd) {
151 push @big, $formatted;
152 my @sseq = startseq($fields[2], $fields[4]);
153 foreach $i (@sseq) {
154 if (!defined($dinstables{$i})) {
155 $dinstables{$i} = [];
157 push(@{$dinstables{$i}}, $#big);
163 close F;
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 = ();
173 %bytecode_pos = ();
174 $bytecode_next = 0;
175 foreach $bl (@bytecode_list) {
176 my $h = hexstr(@$bl);
177 next if (defined($bytecode_pos{$h}));
179 push(@bytecode_array, $bl);
180 while ($h ne '') {
181 $bytecode_pos{$h} = $bytecode_next;
182 $h = substr($h, 2);
183 $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";
194 open B, ">insnsb.c";
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";
204 $p = 0;
205 foreach $bl (@bytecode_array) {
206 printf B " /* %5d */ ", $p;
207 foreach $d (@$bl) {
208 printf B "%#o,", $d;
209 $p++;
211 printf B "\n";
213 print B "};\n";
215 print B "\n";
216 print B "/*\n";
217 print B " * Bytecode frequencies (including reuse):\n";
218 print B " *\n";
219 for ($i = 0; $i < 32; $i++) {
220 print B " *";
221 for ($j = 0; $j < 256; $j += 32) {
222 print B " |" if ($j);
223 printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
225 print B "\n";
227 print B " */\n";
229 close B;
232 if ( !defined($output) || $output eq 'a' ) {
233 print STDERR "Writing insnsa.c...\n";
235 open A, ">insnsa.c";
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";
245 $aname = "aa_$i";
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";
255 print A "};\n";
257 close A;
260 if ( !defined($output) || $output eq 'd' ) {
261 print STDERR "Writing insnsd.c...\n";
263 open D, ">insnsd.c";
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";
272 $n = 0;
273 foreach $j (@big) {
274 printf D " /* %4d */ %s\n", $n++, codesubst($j);
276 print D "};\n";
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";
284 print D "};\n";
287 @prefix_list = ();
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);
294 $is_prefix{$h} = 1;
295 last;
300 foreach $h (@prefix_list) {
301 print D "\n";
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 " { itable_%s, -1 },\n", $nn;
312 } elsif (defined($dinstables{$nn})) {
313 printf D " { itable_%s, %u },\n",
314 $nn, scalar(@{$dinstables{$nn}});
315 } else {
316 printf D " { NULL, 0 },\n";
319 print D "};\n";
322 printf D "\nconst struct disasm_index * const itable_vex[%d][32][8] =\n",
323 $vex_classes;
324 print D "{\n";
325 for ($c = 0; $c < $vex_classes; $c++) {
326 print D " {\n";
327 for ($m = 0; $m < 32; $m++) {
328 print D " {\n";
329 for ($lp = 0; $lp < 8; $lp++) {
330 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $lp);
331 if ($is_prefix{$vp}) {
332 printf D " itable_%s,\n", $vp;
333 } else {
334 print D " NULL,\n";
337 print D " },\n";
339 print D " },\n";
341 print D "};\n";
343 close D;
346 if ( !defined($output) || $output eq 'i' ) {
347 print STDERR "Writing insnsi.h...\n";
349 open I, ">insnsi.h";
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";
359 $maxlen = 0;
360 foreach $i (@opcodes, @opcodes_cc) {
361 print I "\tI_${i},\n";
362 $len = length($i);
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";
367 print I "\n};\n\n";
368 print I "#define MAX_INSLEN ", $maxlen, "\n";
369 print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
370 print I "#endif /* NASM_INSNSI_H */\n";
372 close I;
375 if ( !defined($output) || $output eq 'n' ) {
376 print STDERR "Writing insnsn.c...\n";
378 open N, ">insnsn.c";
380 print N "/* This file is auto-generated from insns.dat by insns.pl" .
381 " - don't edit it */\n\n";
382 print N "#include \"tables.h\"\n\n";
384 print N "const char * const nasm_insn_names[] = {";
385 $first = 1;
386 foreach $i (@opcodes, @opcodes_cc) {
387 print N "," if ( !$first );
388 $first = 0;
389 $ilower = $i;
390 $ilower =~ s/cc$//; # Remove conditional cc suffix
391 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
392 print N "\n\t\"${ilower}\"";
394 print N "\n};\n";
395 close N;
398 printf STDERR "Done: %d instructions\n", $insns;
400 # Count primary bytecodes, for statistics
401 sub count_bytecodes(@) {
402 my $skip = 0;
403 foreach my $bc (@_) {
404 if ($skip) {
405 $skip--;
406 next;
408 $bytecode_count[$bc]++;
409 if ($bc >= 01 && $bc <= 04) {
410 $skip = $bc;
411 } elsif (($bc & ~03) == 010) {
412 $skip = 1;
413 } elsif (($bc & ~013) == 0144) {
414 $skip = 1;
415 } elsif ($bc == 0172) {
416 $skip = 1;
417 } elsif ($bc >= 0260 && $bc <= 0270) {
418 $skip = 2;
419 } elsif ($bc == 0330) {
420 $skip = 1;
425 sub format_insn($$$$$) {
426 my ($opcode, $operands, $codes, $flags, $relax) = @_;
427 my $num, $nd = 0;
428 my @bytecode;
430 return (undef, undef) if $operands eq "ignore";
432 # format the operands
433 $operands =~ s/\*//g;
434 $operands =~ s/:/|colon,/g;
435 $operands =~ s/mem(\d+)/mem|bits$1/g;
436 $operands =~ s/mem/memory/g;
437 $operands =~ s/memory_offs/mem_offs/g;
438 $operands =~ s/imm(\d+)/imm|bits$1/g;
439 $operands =~ s/imm/immediate/g;
440 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
441 $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
442 $operands =~ s/\=([0-9]+)/same_as|$1/g;
443 if ($operands eq 'void') {
444 @ops = ();
445 } else {
446 @ops = split(/\,/, $operands);
448 $num = scalar(@ops);
449 while (scalar(@ops) < $MAX_OPERANDS) {
450 push(@ops, '0');
452 $operands = join(',', @ops);
453 $operands =~ tr/a-z/A-Z/;
455 # format the flags
456 $flags =~ s/,/|IF_/g;
457 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
458 $flags = "IF_" . $flags;
460 @bytecode = (decodify($codes, $relax), 0);
461 push(@bytecode_list, [@bytecode]);
462 $codes = hexstr(@bytecode);
463 count_bytecodes(@bytecode);
465 ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
469 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
470 # offset into nasm_bytecodes
472 sub codesubst($) {
473 my($s) = @_;
474 my $n;
476 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
477 my $pos = $bytecode_pos{$1};
478 if (!defined($pos)) {
479 die "$fname: no position assigned to byte code $1\n";
481 $s = $` . "nasm_bytecodes+${pos}" . "$'";
483 return $s;
486 sub addprefix ($@) {
487 my ($prefix, @list) = @_;
488 my $x;
489 my @l = ();
491 foreach $x (@list) {
492 push(@l, sprintf("%s%02X", $prefix, $x));
495 return @l;
499 # Turn a code string into a sequence of bytes
501 sub decodify($$) {
502 # Although these are C-syntax strings, by convention they should have
503 # only octal escapes (for directives) and hexadecimal escapes
504 # (for verbatim bytes)
505 my($codestr, $relax) = @_;
507 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
508 return byte_code_compile($1, $relax);
511 my $c = $codestr;
512 my @codes = ();
514 while ($c ne '') {
515 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
516 push(@codes, hex $1);
517 $c = $2;
518 next;
519 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
520 push(@codes, oct $1);
521 $c = $2;
522 next;
523 } else {
524 die "$fname: unknown code format in \"$codestr\"\n";
528 return @codes;
531 # Turn a numeric list into a hex string
532 sub hexstr(@) {
533 my $s = '';
534 my $c;
536 foreach $c (@_) {
537 $s .= sprintf("%02X", $c);
539 return $s;
542 # Here we determine the range of possible starting bytes for a given
543 # instruction. We need only consider the codes:
544 # \[1234] mean literal bytes, of course
545 # \1[0123] mean byte plus register value
546 # \330 means byte plus condition code
547 # \0 or \340 mean give up and return empty set
548 # \34[4567] mean PUSH/POP of segment registers: special case
549 # \17[234] skip is4 control byte
550 # \26x \270 skip VEX control bytes
551 sub startseq($$) {
552 my ($codestr, $relax) = @_;
553 my $word, @range;
554 my @codes = ();
555 my $c = $codestr;
556 my $c0, $c1, $i;
557 my $prefix = '';
559 @codes = decodify($codestr, $relax);
561 while ($c0 = shift(@codes)) {
562 $c1 = $codes[0];
563 if ($c0 >= 01 && $c0 <= 04) {
564 # Fixed byte string
565 my $fbs = $prefix;
566 while (1) {
567 if ($c0 >= 01 && $c0 <= 04) {
568 while ($c0--) {
569 $fbs .= sprintf("%02X", shift(@codes));
571 } else {
572 last;
574 $c0 = shift(@codes);
577 foreach $pfx (@disasm_prefixes) {
578 if (substr($fbs, 0, length($pfx)) eq $pfx) {
579 $prefix = $pfx;
580 $fbs = substr($fbs, length($pfx));
581 last;
585 if ($fbs ne '') {
586 return ($prefix.substr($fbs,0,2));
589 unshift(@codes, $c0);
590 } elsif ($c0 >= 010 && $c0 <= 013) {
591 return addprefix($prefix, $c1..($c1+7));
592 } elsif (($c0 & ~013) == 0144) {
593 return addprefix($prefix, $c1, $c1|2);
594 } elsif ($c0 == 0330) {
595 return addprefix($prefix, $c1..($c1+15));
596 } elsif ($c0 == 0 || $c0 == 0340) {
597 return $prefix;
598 } elsif ($c0 == 0344) {
599 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
600 } elsif ($c0 == 0345) {
601 return addprefix($prefix, 0x07, 0x17, 0x1F);
602 } elsif ($c0 == 0346) {
603 return addprefix($prefix, 0xA0, 0xA8);
604 } elsif ($c0 == 0347) {
605 return addprefix($prefix, 0xA1, 0xA9);
606 } elsif (($c0 & ~3) == 0260 || $c0 == 0270) {
607 my $c,$m,$wlp;
608 $m = shift(@codes);
609 $wlp = shift(@codes);
610 $c = ($m >> 6);
611 $m = $m & 31;
612 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 7);
613 } elsif ($c0 >= 0172 && $c0 <= 174) {
614 shift(@codes); # Skip is4 control byte
615 } else {
616 # We really need to be able to distinguish "forbidden"
617 # and "ignorable" codes here
620 return $prefix;
624 # This function takes a series of byte codes in a format which is more
625 # typical of the Intel documentation, and encode it.
627 # The format looks like:
629 # [operands: opcodes]
631 # The operands word lists the order of the operands:
633 # r = register field in the modr/m
634 # m = modr/m
635 # v = VEX "v" field
636 # d = DREX "dst" field
637 # i = immediate
638 # s = register field of is4/imz2 field
639 # - = implicit (unencoded) operand
641 # For an operand that should be filled into more than one field,
642 # enter it as e.g. "r+v".
644 sub byte_code_compile($$) {
645 my($str, $relax) = @_;
646 my $opr;
647 my $opc;
648 my @codes = ();
649 my $litix = undef;
650 my %oppos = ();
651 my $i;
652 my $op, $oq;
653 my $opex;
655 unless ($str =~ /^(([^\s:]*)\:|)\s*(.*\S)\s*$/) {
656 die "$fname: $line: cannot parse: [$str]\n";
658 $opr = "\L$2";
659 $opc = "\L$3";
661 my $op = 0;
662 for ($i = 0; $i < length($opr); $i++) {
663 my $c = substr($opr,$i,1);
664 if ($c eq '+') {
665 $op--;
666 } else {
667 if ($relax & 1) {
668 $op--;
670 $relax >>= 1;
671 $oppos{$c} = $op++;
675 $prefix_ok = 1;
676 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
677 if ($op eq 'o16') {
678 push(@codes, 0320);
679 } elsif ($op eq 'o32') {
680 push(@codes, 0321);
681 } elsif ($op eq 'o64') { # 64-bit operand size requiring REX.W
682 push(@codes, 0324);
683 } elsif ($op eq 'o64nw') { # Implied 64-bit operand size (no REX.W)
684 push(@codes, 0323);
685 } elsif ($op eq 'a16') {
686 push(@codes, 0310);
687 } elsif ($op eq 'a32') {
688 push(@codes, 0311);
689 } elsif ($op eq 'a64') {
690 push(@codes, 0313);
691 } elsif ($op eq '!osp') {
692 push(@codes, 0364);
693 } elsif ($op eq '!asp') {
694 push(@codes, 0365);
695 } elsif ($op eq 'rex.l') {
696 push(@codes, 0334);
697 } elsif ($op eq 'repe') {
698 push(@codes, 0335);
699 } elsif ($op eq 'nohi') { # Use spl/bpl/sil/dil even without REX
700 push(@codes, 0325);
701 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
702 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
703 if ($op eq '66') {
704 push(@codes, 0361);
705 } elsif ($op eq 'f2') {
706 push(@codes, 0362);
707 } elsif ($op eq 'f3') {
708 push(@codes, 0363);
709 } else {
710 push(@codes, 0360);
712 } elsif ($op =~ /^[0-9a-f]{2}$/) {
713 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
714 $codes[$litix] < 4) {
715 $codes[$litix]++;
716 push(@codes, hex $op);
717 } else {
718 $litix = scalar(@codes);
719 push(@codes, 01, hex $op);
721 $prefix_ok = 0;
722 } elsif ($op eq '/r') {
723 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
724 die "$fname: $line: $op requires r and m operands\n";
726 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
727 (($oppos{'r'} & 4) ? 05 : 0);
728 push(@codes, $opex) if ($opex);
729 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
730 $prefix_ok = 0;
731 } elsif ($op =~ m:^/([0-7])$:) {
732 if (!defined($oppos{'m'})) {
733 die "$fname: $line: $op requires m operand\n";
735 push(@codes, 06) if ($oppos{'m'} & 4);
736 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
737 $prefix_ok = 0;
738 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
739 my $c = $vexmap{$1};
740 my ($m,$w,$l,$p) = (undef,2,undef,0);
741 my $has_nds = 0;
742 my @subops = split(/\./, $op);
743 shift @subops; # Drop prefix
744 foreach $oq (@subops) {
745 if ($oq eq '128' || $oq eq 'l0') {
746 $l = 0;
747 } elsif ($oq eq '256' || $oq eq 'l1') {
748 $l = 1;
749 } elsif ($oq eq 'w0') {
750 $w = 0;
751 } elsif ($oq eq 'w1') {
752 $w = 1;
753 } elsif ($oq eq 'wx') {
754 $w = 2;
755 } elsif ($oq eq 'ww') {
756 $w = 3;
757 } elsif ($oq eq 'p0') {
758 $p = 0;
759 } elsif ($oq eq '66' || $oq eq 'p1') {
760 $p = 1;
761 } elsif ($oq eq 'f3' || $oq eq 'p2') {
762 $p = 2;
763 } elsif ($oq eq 'f2' || $oq eq 'p3') {
764 $p = 3;
765 } elsif ($oq eq '0f') {
766 $m = 1;
767 } elsif ($oq eq '0f38') {
768 $m = 2;
769 } elsif ($oq eq '0f3a') {
770 $m = 3;
771 } elsif ($oq =~ /^m([0-9]+)$/) {
772 $m = $1+0;
773 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
774 if (!defined($oppos{'v'})) {
775 die "$fname: $line: vex.$oq without 'v' operand\n";
777 $has_nds = 1;
778 } else {
779 die "$fname: $line: undefined VEX subcode: $oq\n";
782 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
783 die "$fname: $line: missing fields in VEX specification\n";
785 if (defined($oppos{'v'}) && !$has_nds) {
786 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
788 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
789 ($c << 6)+$m, ($w << 3)+($l << 2)+$p);
790 $prefix_ok = 0;
791 } elsif ($op =~ /^\/drex([01])$/) {
792 my $oc0 = $1;
793 if (!defined($oppos{'d'})) {
794 die "$fname: $line: DREX without a 'd' operand\n";
796 # Note the use of *unshift* here, as opposed to *push*.
797 # This is because NASM want this byte code at the start of
798 # the instruction sequence, but the AMD documentation puts
799 # this at (roughly) the position of the drex byte itself.
800 # This allows us to match the AMD documentation and still
801 # do the right thing.
802 unshift(@codes, 0160+($oppos{'d'} & 3)+($oc0 ? 4 : 0));
803 unshift(@codes, 05) if ($oppos{'d'} & 4);
804 } elsif ($op =~ /^(ib\,s|ib|ibx|ib\,w|iw|iwd|id|idx|iwdq|rel|rel8|rel16|rel32|iq|seg|ibw|ibd|ibd,s)$/) {
805 if (!defined($oppos{'i'})) {
806 die "$fname: $line: $op without 'i' operand\n";
808 if ($op eq 'ib,s') { # Signed imm8
809 push(@codes, 05) if ($oppos{'i'} & 4);
810 push(@codes, 014+($oppos{'i'} & 3));
811 } elsif ($op eq 'ib') { # imm8
812 push(@codes, 05) if ($oppos{'i'} & 4);
813 push(@codes, 020+($oppos{'i'} & 3));
814 } elsif ($op eq 'ib,u') { # Unsigned imm8
815 push(@codes, 05) if ($oppos{'i'} & 4);
816 push(@codes, 024+($oppos{'i'} & 3));
817 } elsif ($op eq 'iw') { # imm16
818 push(@codes, 05) if ($oppos{'i'} & 4);
819 push(@codes, 030+($oppos{'i'} & 3));
820 } elsif ($op eq 'ibx') { # imm8 sign-extended to opsize
821 push(@codes, 05) if ($oppos{'i'} & 4);
822 push(@codes, 0274+($oppos{'i'} & 3));
823 } elsif ($op eq 'iwd') { # imm16 or imm32, depending on opsize
824 push(@codes, 05) if ($oppos{'i'} & 4);
825 push(@codes, 034+($oppos{'i'} & 3));
826 } elsif ($op eq 'id') { # imm32
827 push(@codes, 05) if ($oppos{'i'} & 4);
828 push(@codes, 040+($oppos{'i'} & 3));
829 } elsif ($op eq 'idx') { # imm32 extended to 64 bits
830 push(@codes, 05) if ($oppos{'i'} & 4);
831 push(@codes, 0254+($oppos{'i'} & 3));
832 } elsif ($op eq 'iwdq') { # imm16/32/64, depending on opsize
833 push(@codes, 05) if ($oppos{'i'} & 4);
834 push(@codes, 044+($oppos{'i'} & 3));
835 } elsif ($op eq 'rel8') {
836 push(@codes, 05) if ($oppos{'i'} & 4);
837 push(@codes, 050+($oppos{'i'} & 3));
838 } elsif ($op eq 'iq') {
839 push(@codes, 05) if ($oppos{'i'} & 4);
840 push(@codes, 054+($oppos{'i'} & 3));
841 } elsif ($op eq 'rel16') {
842 push(@codes, 05) if ($oppos{'i'} & 4);
843 push(@codes, 060+($oppos{'i'} & 3));
844 } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
845 push(@codes, 05) if ($oppos{'i'} & 4);
846 push(@codes, 064+($oppos{'i'} & 3));
847 } elsif ($op eq 'rel32') {
848 push(@codes, 05) if ($oppos{'i'} & 4);
849 push(@codes, 070+($oppos{'i'} & 3));
850 } elsif ($op eq 'seg') {
851 push(@codes, 05) if ($oppos{'i'} & 4);
852 push(@codes, 074+($oppos{'i'} & 3));
853 } elsif ($op eq 'ibw') { # imm16 that can be bytified
854 if (!defined($s_pos)) {
855 die "$fname: $line: $op without a +s byte\n";
857 $codes[$s_pos] += 0144;
858 push(@codes, 05) if ($oppos{'i'} & 4);
859 push(@codes, 0140+($oppos{'i'} & 3));
860 } elsif ($op eq 'ibd') { # imm32 that can be bytified
861 if (!defined($s_pos)) {
862 die "$fname: $line: $op without a +s byte\n";
864 $codes[$s_pos] += 0154;
865 push(@codes, 05) if ($oppos{'i'} & 4);
866 push(@codes, 0150+($oppos{'i'} & 3));
867 } elsif ($op eq 'ibd,s') {
868 # imm32 that can be bytified, sign extended to 64 bits
869 if (!defined($s_pos)) {
870 die "$fname: $line: $op without a +s byte\n";
872 $codes[$s_pos] += 0154;
873 push(@codes, 05) if ($oppos{'i'} & 4);
874 push(@codes, 0250+($oppos{'i'} & 3));
876 $prefix_ok = 0;
877 } elsif ($op eq '/is4') {
878 if (!defined($oppos{'s'})) {
879 die "$fname: $line: $op without 's' operand\n";
881 if (defined($oppos{'i'})) {
882 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
883 } else {
884 push(@codes, 0174, $oppos{'s'});
886 $prefix_ok = 0;
887 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
888 my $imm = $1;
889 if (!defined($oppos{'s'})) {
890 die "$fname: $line: $op without 's' operand\n";
892 if ($imm < 0 || $imm > 15) {
893 die "$fname: $line: invalid imm4 value for $op: $imm\n";
895 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
896 $prefix_ok = 0;
897 } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
898 if (!defined($oppos{'i'})) {
899 die "$fname: $line: $op without 'i' operand\n";
901 $s_pos = scalar @codes;
902 push(@codes, 05) if ($oppos{'i'} & 4);
903 push(@codes, $oppos{'i'} & 3, hex $1);
904 $prefix_ok = 0;
905 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
906 push(@codes, 0330, hex $1);
907 $prefix_ok = 0;
908 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
909 # Escape to enter literal bytecodes
910 push(@codes, oct $1);
911 } else {
912 die "$fname: $line: unknown operation: $op\n";
916 return @codes;