test: nasm-t -- Update riprel
[nasm.git] / x86 / insns.pl
blob5845ed89aeb69abcbc3592c26a664a37cf7329c5
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
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.
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.
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 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
47 $MAX_OPERANDS = 5;
49 # Add VEX/XOP prefixes
50 @vex_class = ( 'vex', 'xop', 'evex' );
51 $vex_classes = scalar(@vex_class);
52 @vexlist = ();
53 %vexmap = ();
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";
68 @args = ();
69 undef $output;
70 foreach $arg ( @ARGV ) {
71 if ( $arg =~ /^\-/ ) {
72 if ( $arg =~ /^\-([abdin]|f[hc])$/ ) {
73 $output = $1;
74 } else {
75 die "$0: Unknown option: ${arg}\n";
77 } else {
78 push (@args, $arg);
82 die if (scalar(@args) != 2); # input output
83 ($fname, $oname) = @args;
85 open(F, '<', $fname) || die "unable to open $fname";
87 %dinstables = ();
88 @bytecode_list = ();
90 $line = 0;
91 $insns = 0;
92 $n_opcodes = $n_opcodes_cc = 0;
93 while (<F>) {
94 $line++;
95 chomp;
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";
100 next;
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";
109 next;
112 $opmask = 0;
113 @ops = split(/,/, $fields[1]);
114 for ($oi = 0; $oi < scalar @ops; $oi++) {
115 if ($ops[$oi] =~ /\*$/) {
116 if ($oi == 0) {
117 warn "line $line has a first operand with a *\n";
118 next;
120 $opmask |= 1 << $oi;
124 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
125 if (($oi & ~$opmask) == 0) {
126 my @xops = ();
127 my $omask = ~$oi;
128 for ($oj = 0; $oj < scalar(@ops); $oj++) {
129 if ($omask & 1) {
130 push(@xops, $ops[$oj]);
132 $omask >>= 1;
134 push(@field_list, [$fields[0], join(',', @xops),
135 $fields[2], $fields[3], $oi]);
140 foreach $fptr (@field_list) {
141 @fields = @$fptr;
142 ($formatted, $nd) = format_insn(@fields);
143 if ($formatted) {
144 $insns++;
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++;
153 } else {
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]);
162 foreach $i (@sseq) {
163 if (!defined($dinstables{$i})) {
164 $dinstables{$i} = [];
166 push(@{$dinstables{$i}}, $#big);
172 close F;
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 = ();
182 %bytecode_pos = ();
183 $bytecode_next = 0;
184 foreach $bl (@bytecode_list) {
185 my $h = hexstr(@$bl);
186 next if (defined($bytecode_pos{$h}));
188 push(@bytecode_array, $bl);
189 while ($h ne '') {
190 $bytecode_pos{$h} = $bytecode_next;
191 $h = substr($h, 2);
192 $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";
213 $p = 0;
214 foreach $bl (@bytecode_array) {
215 printf B " /* %5d */ ", $p;
216 foreach $d (@$bl) {
217 printf B "%#o,", $d;
218 $p++;
220 printf B "\n";
222 print B "};\n";
224 print B "\n";
225 print B "/*\n";
226 print B " * Bytecode frequencies (including reuse):\n";
227 print B " *\n";
228 for ($i = 0; $i < 32; $i++) {
229 print B " *";
230 for ($j = 0; $j < 256; $j += 32) {
231 print B " |" if ($j);
232 printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
234 print B "\n";
236 print B " */\n";
238 close B;
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";
254 $aname = "aa_$i";
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";
264 print A "};\n";
266 close A;
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";
281 $n = 0;
282 foreach $j (@big) {
283 printf D " /* %4d */ %s\n", $n++, codesubst($j);
285 print D "};\n";
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";
293 print D "};\n";
296 @prefix_list = ();
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);
303 $is_prefix{$h} = 1;
304 last;
309 foreach $h (@prefix_list) {
310 print D "\n";
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}});
324 } else {
325 printf D " /* 0x%02x */ { NULL, 0 },\n", $c;
328 print D "};\n";
331 printf D "\nconst struct disasm_index * const itable_vex[NASM_VEX_CLASSES][32][4] =\n";
332 print D "{\n";
333 for ($c = 0; $c < $vex_classes; $c++) {
334 print D " {\n";
335 for ($m = 0; $m < 32; $m++) {
336 print D " { ";
337 for ($p = 0; $p < 4; $p++) {
338 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $p);
339 printf D "%-15s",
340 ($is_prefix{$vp} ? sprintf("itable_%s,", $vp) : 'NULL,');
342 print D "},\n";
344 print D " },\n";
346 print D "};\n";
348 close D;
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";
364 $maxlen = 0;
365 foreach $i (@opcodes, @opcodes_cc) {
366 print I "\tI_${i},\n";
367 $len = length($i);
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";
372 print I "};\n\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";
379 close I;
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[] = {";
392 $first = 1;
393 foreach $i (@opcodes, @opcodes_cc) {
394 print N "," if ( !$first );
395 $first = 0;
396 $ilower = $i;
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}\"";
401 print N "\n};\n";
402 close N;
405 if ( $output eq 'fh') {
406 write_iflaggen_h();
409 if ( $output eq 'fc') {
410 write_iflag_c();
413 printf STDERR "Done: %d instructions\n", $insns;
415 # Count primary bytecodes, for statistics
416 sub count_bytecodes(@) {
417 my $skip = 0;
418 foreach my $bc (@_) {
419 if ($skip) {
420 $skip--;
421 next;
423 $bytecode_count[$bc]++;
424 if ($bc >= 01 && $bc <= 04) {
425 $skip = $bc;
426 } elsif (($bc & ~03) == 010) {
427 $skip = 1;
428 } elsif (($bc & ~013) == 0144) {
429 $skip = 1;
430 } elsif ($bc == 0172 || $bc == 0173) {
431 $skip = 1;
432 } elsif (($bc & ~3) == 0260 || $bc == 0270) { # VEX
433 $skip = 2;
434 } elsif (($bc & ~3) == 0240 || $bc == 0250) { # EVEX
435 $skip = 3;
436 } elsif ($bc == 0330) {
437 $skip = 1;
442 sub format_insn($$$$$) {
443 my ($opcode, $operands, $codes, $flags, $relax) = @_;
444 my $num, $nd = 0, $rawflags, $flagsindex;
445 my @bytecode;
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;
453 @ops = ();
454 @decos = ();
455 if ($operands ne 'void') {
456 foreach $op (split(/,/, $operands)) {
457 @opx = ();
458 @opevex = ();
459 foreach $opp (split(/\|/, $op)) {
460 @oppx = ();
461 if ($opp =~ s/^(b(32|64)|mask|z|er|sae)$//) {
462 push(@opevex, $1);
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);
482 push(@ops, $op);
483 push(@decos, (@opevex ? join('|', @opevex) : '0'));
487 $num = scalar(@ops);
488 while (scalar(@ops) < $MAX_OPERANDS) {
489 push(@ops, '0');
490 push(@decos, '0');
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/;
501 # format the flags
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\./) {
506 $flags .= ",EVEX";
507 } elsif ($codes =~ /(vex|xop)\./) {
508 $flags .= ",VEX";
510 $rawflags = $flags;
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
527 sub codesubst($) {
528 my($s) = @_;
529 my $n;
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}" . "$'";
538 return $s;
541 sub addprefix ($@) {
542 my ($prefix, @list) = @_;
543 my $x;
544 my @l = ();
546 foreach $x (@list) {
547 push(@l, sprintf("%s%02X", $prefix, $x));
550 return @l;
554 # Turn a code string into a sequence of bytes
556 sub decodify($$) {
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);
566 my $c = $codestr;
567 my @codes = ();
569 unless ($codestr eq 'ignore') {
570 while ($c ne '') {
571 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
572 push(@codes, hex $1);
573 $c = $2;
574 next;
575 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
576 push(@codes, oct $1);
577 $c = $2;
578 next;
579 } else {
580 die "$fname: unknown code format in \"$codestr\"\n";
585 return @codes;
588 # Turn a numeric list into a hex string
589 sub hexstr(@) {
590 my $s = '';
591 my $c;
593 foreach $c (@_) {
594 $s .= sprintf("%02X", $c);
596 return $s;
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
609 sub startseq($$) {
610 my ($codestr, $relax) = @_;
611 my $word, @range;
612 my @codes = ();
613 my $c = $codestr;
614 my $c0, $c1, $i;
615 my $prefix = '';
617 @codes = decodify($codestr, $relax);
619 while ($c0 = shift(@codes)) {
620 $c1 = $codes[0];
621 if ($c0 >= 01 && $c0 <= 04) {
622 # Fixed byte string
623 my $fbs = $prefix;
624 while (1) {
625 if ($c0 >= 01 && $c0 <= 04) {
626 while ($c0--) {
627 $fbs .= sprintf("%02X", shift(@codes));
629 } else {
630 last;
632 $c0 = shift(@codes);
635 foreach $pfx (@disasm_prefixes) {
636 if (substr($fbs, 0, length($pfx)) eq $pfx) {
637 $prefix = $pfx;
638 $fbs = substr($fbs, length($pfx));
639 last;
643 if ($fbs ne '') {
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) {
655 return $prefix;
656 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
657 ($c0 & ~3) == 0240 || $c0 == 0250) {
658 my $c,$m,$wlp;
659 $m = shift(@codes);
660 $wlp = shift(@codes);
661 $c = ($m >> 6);
662 $m = $m & 31;
663 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
664 if ($c0 < 0260) {
665 my $tuple = shift(@codes);
667 } elsif ($c0 >= 0172 && $c0 <= 173) {
668 shift(@codes); # Skip is4 control byte
669 } else {
670 # We really need to be able to distinguish "forbidden"
671 # and "ignorable" codes here
674 return $prefix;
677 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
678 sub tupletype($) {
679 my ($tuplestr) = @_;
680 my %tuple_codes = (
681 '' => 000,
682 'fv' => 001,
683 'hv' => 002,
684 'fvm' => 003,
685 't1s8' => 004,
686 't1s16' => 005,
687 't1s' => 006,
688 't1f32' => 007,
689 't1f64' => 010,
690 't2' => 011,
691 't4' => 012,
692 't8' => 013,
693 'hvm' => 014,
694 'qvm' => 015,
695 'ovm' => 016,
696 'm128' => 017,
697 'dup' => 020,
700 if (defined $tuple_codes{$tuplestr}) {
701 return 0300 + $tuple_codes{$tuplestr};
702 } else {
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
718 # m = modr/m
719 # v = VEX "v" field
720 # i = immediate
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) = @_;
730 my $opr;
731 my $opc;
732 my @codes = ();
733 my $litix = undef;
734 my %oppos = ();
735 my $i;
736 my $op, $oq;
737 my $opex;
739 my %imm_codes = (
740 'ib' => 020, # imm8
741 'ib,u' => 024, # Unsigned imm8
742 'iw' => 030, # imm16
743 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
744 'iwd' => 034, # imm16 or imm32, depending on opsize
745 'id' => 040, # imm32
746 'id,s' => 0254, # imm32 sign-extended to 64 bits
747 'iwdq' => 044, # imm16/32/64, depending on addrsize
748 'rel8' => 050,
749 'iq' => 054,
750 'rel16' => 060,
751 'rel' => 064, # 16 or 32 bit relative operand
752 'rel32' => 070,
753 'seg' => 074,
755 my %plain_codes = (
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)
761 'a16' => 0310,
762 'a32' => 0311,
763 'adf' => 0312, # Address size is default
764 'a64' => 0313,
765 '!osp' => 0364,
766 '!asp' => 0365,
767 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
768 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
769 'mustrep' => 0336,
770 'mustrepne' => 0337,
771 'rex.l' => 0334,
772 'norexb' => 0314,
773 'norexx' => 0315,
774 'norexr' => 0316,
775 'norexw' => 0317,
776 'repe' => 0335,
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
781 'resb' => 0340,
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
786 'hlexr' => 0271,
787 'hlenl' => 0272,
788 'hle' => 0273,
790 # This instruction takes XMM VSIB
791 'vsibx' => 0374,
792 'vm32x' => 0374,
793 'vm64x' => 0374,
795 # This instruction takes YMM VSIB
796 'vsiby' => 0375,
797 'vm32y' => 0375,
798 'vm64y' => 0375,
800 # This instruction takes ZMM VSIB
801 'vsibz' => 0376,
802 'vm32z' => 0376,
803 'vm64z' => 0376,
806 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
807 die "$fname: $line: cannot parse: [$str]\n";
809 $opr = "\L$2";
810 $tuple = "\L$3"; # Tuple type for AVX512
811 $opc = "\L$4";
813 my $op = 0;
814 for ($i = 0; $i < length($opr); $i++) {
815 my $c = substr($opr,$i,1);
816 if ($c eq '+') {
817 $op--;
818 } else {
819 if ($relax & 1) {
820 $op--;
822 $relax >>= 1;
823 $oppos{$c} = $op++;
826 $tup = tupletype($tuple);
828 my $last_imm = 'h';
829 my $prefix_ok = 1;
830 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
831 my $pc = $plain_codes{$op};
833 if (defined $pc) {
834 # Plain code
835 push(@codes, $pc);
836 } elsif ($prefix_ok && $op =~ /^(66|f2|f3)$/) {
837 # 66/F2/F3 prefix used as an opcode extension
838 if ($op eq '66') {
839 push(@codes, 0361);
840 } elsif ($op eq 'f2') {
841 push(@codes, 0332);
842 } else {
843 push(@codes, 0333);
845 } elsif ($op =~ /^[0-9a-f]{2}$/) {
846 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
847 $codes[$litix] < 4) {
848 $codes[$litix]++;
849 push(@codes, hex $op);
850 } else {
851 $litix = scalar(@codes);
852 push(@codes, 01, hex $op);
854 $prefix_ok = 0;
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));
865 $prefix_ok = 0;
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);
872 $prefix_ok = 0;
873 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
874 my $vexname = $1;
875 my $c = $vexmap{$vexname};
876 my ($m,$w,$l,$p) = (undef,2,undef,0);
877 my $has_nds = 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') {
882 $l = 0;
883 } elsif ($oq eq '256' || $oq eq 'l1') {
884 $l = 1;
885 } elsif ($oq eq 'lig') {
886 $l = 2;
887 } elsif ($oq eq 'w0') {
888 $w = 0;
889 } elsif ($oq eq 'w1') {
890 $w = 1;
891 } elsif ($oq eq 'wig') {
892 $w = 2;
893 } elsif ($oq eq 'ww') {
894 $w = 3;
895 } elsif ($oq eq 'p0') {
896 $p = 0;
897 } elsif ($oq eq '66' || $oq eq 'p1') {
898 $p = 1;
899 } elsif ($oq eq 'f3' || $oq eq 'p2') {
900 $p = 2;
901 } elsif ($oq eq 'f2' || $oq eq 'p3') {
902 $p = 3;
903 } elsif ($oq eq '0f') {
904 $m = 1;
905 } elsif ($oq eq '0f38') {
906 $m = 2;
907 } elsif ($oq eq '0f3a') {
908 $m = 3;
909 } elsif ($oq =~ /^m([0-9]+)$/) {
910 $m = $1+0;
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";
915 $has_nds = 1;
916 } else {
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);
932 $prefix_ok = 0;
933 } elsif ($op =~ /^(evex)(|\..*)$/) {
934 my $c = $vexmap{$1};
935 my ($m,$w,$l,$p) = (undef,2,undef,0);
936 my $has_nds = 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') {
941 $l = 0;
942 } elsif ($oq eq '256' || $oq eq 'l1') {
943 $l = 1;
944 } elsif ($oq eq '512' || $oq eq 'l2') {
945 $l = 2;
946 } elsif ($oq eq 'w0') {
947 $w = 0;
948 } elsif ($oq eq 'w1') {
949 $w = 1;
950 } elsif ($oq eq 'wig') {
951 $w = 2;
952 } elsif ($oq eq 'ww') {
953 $w = 3;
954 } elsif ($oq eq 'p0') {
955 $p = 0;
956 } elsif ($oq eq '66' || $oq eq 'p1') {
957 $p = 1;
958 } elsif ($oq eq 'f3' || $oq eq 'p2') {
959 $p = 2;
960 } elsif ($oq eq 'f2' || $oq eq 'p3') {
961 $p = 3;
962 } elsif ($oq eq '0f') {
963 $m = 1;
964 } elsif ($oq eq '0f38') {
965 $m = 2;
966 } elsif ($oq eq '0f3a') {
967 $m = 3;
968 } elsif ($oq =~ /^m([0-9]+)$/) {
969 $m = $1+0;
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";
974 $has_nds = 1;
975 } else {
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";
985 if ($m > 15) {
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);
990 $prefix_ok = 0;
991 } elsif (defined $imm_codes{$op}) {
992 if ($op eq 'seg') {
993 if ($last_imm lt 'i') {
994 die "$fname: $line: seg without an immediate operand\n";
996 } else {
997 $last_imm++;
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));
1007 $prefix_ok = 0;
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'});
1014 } else {
1015 push(@codes, 05) if ($oppos{'s'} & 4);
1016 push(@codes, 0174+($oppos{'s'} & 3));
1018 $prefix_ok = 0;
1019 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
1020 my $imm = $1;
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);
1028 $prefix_ok = 0;
1029 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
1030 push(@codes, 0330, hex $1);
1031 $prefix_ok = 0;
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);
1038 $prefix_ok = 0;
1039 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1040 # Escape to enter literal bytecodes
1041 push(@codes, oct $1);
1042 } else {
1043 die "$fname: $line: unknown operation: $op\n";
1047 return @codes;