rbtree: add rb_search_exact()
[nasm.git] / x86 / insns.pl
blobcd9aaf4ff2b0e28c3dd10499d1b3b8c09bff1849
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:$line: 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 $nd = 0;
445 my ($num, $flagsindex);
446 my @bytecode;
447 my ($op, @ops, $opp, @opx, @oppx, @decos, @opevex);
449 return (undef, undef) if $operands eq "ignore";
451 # format the operands
452 $operands =~ s/\*//g;
453 $operands =~ s/:/|colon,/g;
454 @ops = ();
455 @decos = ();
456 if ($operands ne 'void') {
457 foreach $op (split(/,/, $operands)) {
458 @opx = ();
459 @opevex = ();
460 foreach $opp (split(/\|/, $op)) {
461 @oppx = ();
462 if ($opp =~ s/^(b(32|64)|mask|z|er|sae)$//) {
463 push(@opevex, $1);
466 if ($opp =~ s/(?<!\d)(8|16|32|64|80|128|256|512)$//) {
467 push(@oppx, "bits$1");
469 $opp =~ s/^mem$/memory/;
470 $opp =~ s/^memory_offs$/mem_offs/;
471 $opp =~ s/^imm$/immediate/;
472 $opp =~ s/^([a-z]+)rm$/rm_$1/;
473 $opp =~ s/^rm$/rm_gpr/;
474 $opp =~ s/^reg$/reg_gpr/;
475 # only for evex insns, high-16 regs are allowed
476 if ($codes !~ /(^|\s)evex\./) {
477 $opp =~ s/^(rm_[xyz]mm)$/$1_l16/;
478 $opp =~ s/^([xyz]mm)reg$/$1_l16/;
480 push(@opx, $opp, @oppx) if $opp;
482 $op = join('|', @opx);
483 push(@ops, $op);
484 push(@decos, (@opevex ? join('|', @opevex) : '0'));
488 $num = scalar(@ops);
489 while (scalar(@ops) < $MAX_OPERANDS) {
490 push(@ops, '0');
491 push(@decos, '0');
493 $operands = join(',', @ops);
494 $operands =~ tr/a-z/A-Z/;
496 $decorators = "{" . join(',', @decos) . "}";
497 if ($decorators =~ /^{(0,)+0}$/) {
498 $decorators = "NO_DECORATOR";
500 $decorators =~ tr/a-z/A-Z/;
502 # expand and uniqify the flags
503 my %flags;
504 foreach my $flag (split(',', $flags)) {
505 if ($flag eq 'ND') {
506 $nd = 1;
507 } elsif ($flag eq 'X64') {
508 # X64 is shorthand for "LONG,X86_64"
509 $flags{'LONG'}++;
510 $flags{'X86_64'}++;
511 } elsif ($flag ne '') {
512 $flags{$flag}++;
515 if ($flag eq 'NEVER' || $flag eq 'NOP') {
516 # These flags imply OBSOLETE
517 $flags{'OBSOLETE'}++;
521 if ($codes =~ /evex\./) {
522 $flags{'EVEX'}++;
523 } elsif ($codes =~ /(vex|xop)\./) {
524 $flags{'VEX'}++;
527 $flagsindex = insns_flag_index(keys %flags);
528 die "$fname:$line: error in flags $flags" unless (defined($flagsindex));
530 @bytecode = (decodify($codes, $relax), 0);
531 push(@bytecode_list, [@bytecode]);
532 $codes = hexstr(@bytecode);
533 count_bytecodes(@bytecode);
535 ("{I_$opcode, $num, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flagsindex},", $nd);
539 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
540 # offset into nasm_bytecodes
542 sub codesubst($) {
543 my($s) = @_;
544 my $n;
546 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
547 my $pos = $bytecode_pos{$1};
548 if (!defined($pos)) {
549 die "$fname:$line: no position assigned to byte code $1\n";
551 $s = $` . "nasm_bytecodes+${pos}" . "$'";
553 return $s;
556 sub addprefix ($@) {
557 my ($prefix, @list) = @_;
558 my $x;
559 my @l = ();
561 foreach $x (@list) {
562 push(@l, sprintf("%s%02X", $prefix, $x));
565 return @l;
569 # Turn a code string into a sequence of bytes
571 sub decodify($$) {
572 # Although these are C-syntax strings, by convention they should have
573 # only octal escapes (for directives) and hexadecimal escapes
574 # (for verbatim bytes)
575 my($codestr, $relax) = @_;
577 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
578 return byte_code_compile($1, $relax);
581 my $c = $codestr;
582 my @codes = ();
584 unless ($codestr eq 'ignore') {
585 while ($c ne '') {
586 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
587 push(@codes, hex $1);
588 $c = $2;
589 next;
590 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
591 push(@codes, oct $1);
592 $c = $2;
593 next;
594 } else {
595 die "$fname:$line: unknown code format in \"$codestr\"\n";
600 return @codes;
603 # Turn a numeric list into a hex string
604 sub hexstr(@) {
605 my $s = '';
606 my $c;
608 foreach $c (@_) {
609 $s .= sprintf("%02X", $c);
611 return $s;
614 # Here we determine the range of possible starting bytes for a given
615 # instruction. We need only consider the codes:
616 # \[1234] mean literal bytes, of course
617 # \1[0123] mean byte plus register value
618 # \330 means byte plus condition code
619 # \0 or \340 mean give up and return empty set
620 # \34[4567] mean PUSH/POP of segment registers: special case
621 # \17[234] skip is4 control byte
622 # \26x \270 skip VEX control bytes
623 # \24x \250 skip EVEX control bytes
624 sub startseq($$) {
625 my ($codestr, $relax) = @_;
626 my $word;
627 my @codes = ();
628 my $c = $codestr;
629 my($c0, $c1, $i);
630 my $prefix = '';
632 @codes = decodify($codestr, $relax);
634 while (defined($c0 = shift(@codes))) {
635 $c1 = $codes[0];
636 if ($c0 >= 01 && $c0 <= 04) {
637 # Fixed byte string
638 my $fbs = $prefix;
639 while (defined($c0)) {
640 if ($c0 >= 01 && $c0 <= 04) {
641 while ($c0--) {
642 $fbs .= sprintf("%02X", shift(@codes));
644 } else {
645 last;
647 $c0 = shift(@codes);
650 foreach $pfx (@disasm_prefixes) {
651 if (substr($fbs, 0, length($pfx)) eq $pfx) {
652 $prefix = $pfx;
653 $fbs = substr($fbs, length($pfx));
654 last;
658 if ($fbs ne '') {
659 return ($prefix.substr($fbs,0,2));
662 unshift(@codes, $c0);
663 } elsif ($c0 >= 010 && $c0 <= 013) {
664 return addprefix($prefix, $c1..($c1+7));
665 } elsif (($c0 & ~013) == 0144) {
666 return addprefix($prefix, $c1, $c1|2);
667 } elsif ($c0 == 0330) {
668 return addprefix($prefix, $c1..($c1+15));
669 } elsif ($c0 == 0 || $c0 == 0340) {
670 return $prefix;
671 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
672 ($c0 & ~3) == 0240 || $c0 == 0250) {
673 my($c,$m,$wlp);
674 $m = shift(@codes);
675 $wlp = shift(@codes);
676 $c = ($m >> 6);
677 $m = $m & 31;
678 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
679 if ($c0 < 0260) {
680 my $tuple = shift(@codes);
682 } elsif ($c0 >= 0172 && $c0 <= 173) {
683 shift(@codes); # Skip is4 control byte
684 } else {
685 # We really need to be able to distinguish "forbidden"
686 # and "ignorable" codes here
689 return $prefix;
692 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
693 sub tupletype($) {
694 my ($tuplestr) = @_;
695 my %tuple_codes = (
696 '' => 000,
697 'fv' => 001,
698 'hv' => 002,
699 'fvm' => 003,
700 't1s8' => 004,
701 't1s16' => 005,
702 't1s' => 006,
703 't1f32' => 007,
704 't1f64' => 010,
705 't2' => 011,
706 't4' => 012,
707 't8' => 013,
708 'hvm' => 014,
709 'qvm' => 015,
710 'ovm' => 016,
711 'm128' => 017,
712 'dup' => 020,
715 if (defined $tuple_codes{$tuplestr}) {
716 return 0300 + $tuple_codes{$tuplestr};
717 } else {
718 die "$fname:$line: undefined tuple type : $tuplestr\n";
723 # This function takes a series of byte codes in a format which is more
724 # typical of the Intel documentation, and encode it.
726 # The format looks like:
728 # [operands: opcodes]
730 # The operands word lists the order of the operands:
732 # r = register field in the modr/m
733 # m = modr/m
734 # v = VEX "v" field
735 # i = immediate
736 # s = register field of is4/imz2 field
737 # - = implicit (unencoded) operand
738 # x = indeX register of mib. 014..017 bytecodes are used.
740 # For an operand that should be filled into more than one field,
741 # enter it as e.g. "r+v".
743 sub byte_code_compile($$) {
744 my($str, $relax) = @_;
745 my $opr;
746 my $opc;
747 my @codes = ();
748 my $litix = undef;
749 my %oppos = ();
750 my $i;
751 my ($op, $oq);
752 my $opex;
754 my %imm_codes = (
755 'ib' => 020, # imm8
756 'ib,u' => 024, # Unsigned imm8
757 'iw' => 030, # imm16
758 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
759 'iwd' => 034, # imm16 or imm32, depending on opsize
760 'id' => 040, # imm32
761 'id,s' => 0254, # imm32 sign-extended to 64 bits
762 'iwdq' => 044, # imm16/32/64, depending on addrsize
763 'rel8' => 050,
764 'iq' => 054,
765 'rel16' => 060,
766 'rel' => 064, # 16 or 32 bit relative operand
767 'rel32' => 070,
768 'seg' => 074,
770 my %plain_codes = (
771 'o16' => 0320, # 16-bit operand size
772 'o32' => 0321, # 32-bit operand size
773 'odf' => 0322, # Operand size is default
774 'o64' => 0324, # 64-bit operand size requiring REX.W
775 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
776 'a16' => 0310,
777 'a32' => 0311,
778 'adf' => 0312, # Address size is default
779 'a64' => 0313,
780 '!osp' => 0364,
781 '!asp' => 0365,
782 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
783 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
784 'mustrep' => 0336,
785 'mustrepne' => 0337,
786 'rex.l' => 0334,
787 'norexb' => 0314,
788 'norexx' => 0315,
789 'norexr' => 0316,
790 'norexw' => 0317,
791 'repe' => 0335,
792 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
793 'nof3' => 0326, # No REP 0xF3 prefix permitted
794 'norep' => 0331, # No REP prefix permitted
795 'wait' => 0341, # Needs a wait prefix
796 'resb' => 0340,
797 'np' => 0360, # No prefix
798 'jcc8' => 0370, # Match only if Jcc possible with single byte
799 'jmp8' => 0371, # Match only if JMP possible with single byte
800 'jlen' => 0373, # Length of jump
801 'hlexr' => 0271,
802 'hlenl' => 0272,
803 'hle' => 0273,
805 # This instruction takes XMM VSIB
806 'vsibx' => 0374,
807 'vm32x' => 0374,
808 'vm64x' => 0374,
810 # This instruction takes YMM VSIB
811 'vsiby' => 0375,
812 'vm32y' => 0375,
813 'vm64y' => 0375,
815 # This instruction takes ZMM VSIB
816 'vsibz' => 0376,
817 'vm32z' => 0376,
818 'vm64z' => 0376,
821 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
822 die "$fname:$line: cannot parse: [$str]\n";
824 $opr = lc($2);
825 $tuple = lc($3); # Tuple type for AVX512
826 $opc = lc($4);
828 $op = 0;
829 for ($i = 0; $i < length($opr); $i++) {
830 my $c = substr($opr,$i,1);
831 if ($c eq '+') {
832 $op--;
833 } else {
834 if ($relax & 1) {
835 $op--;
837 $relax >>= 1;
838 $oppos{$c} = $op++;
841 $tup = tupletype($tuple);
843 my $last_imm = 'h';
844 my $prefix_ok = 1;
845 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
846 my $pc = $plain_codes{$op};
848 if (defined $pc) {
849 # Plain code
850 push(@codes, $pc);
851 } elsif ($prefix_ok && $op =~ /^(66|f2|f3)$/) {
852 # 66/F2/F3 prefix used as an opcode extension
853 if ($op eq '66') {
854 push(@codes, 0361);
855 } elsif ($op eq 'f2') {
856 push(@codes, 0332);
857 } else {
858 push(@codes, 0333);
860 } elsif ($op =~ /^[0-9a-f]{2}$/) {
861 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
862 $codes[$litix] < 4) {
863 $codes[$litix]++;
864 push(@codes, hex $op);
865 } else {
866 $litix = scalar(@codes);
867 push(@codes, 01, hex $op);
869 $prefix_ok = 0;
870 } elsif ($op eq '/r') {
871 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
872 die "$fname:$line: $op requires r and m operands\n";
874 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
875 (($oppos{'r'} & 4) ? 05 : 0);
876 push(@codes, $opex) if ($opex);
877 # if mib is composed with two separate operands - ICC style
878 push(@codes, 014 + ($oppos{'x'} & 3)) if (defined($oppos{'x'}));
879 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
880 $prefix_ok = 0;
881 } elsif ($op =~ m:^/([0-7])$:) {
882 if (!defined($oppos{'m'})) {
883 die "$fname:$line: $op requires m operand\n";
885 push(@codes, 06) if ($oppos{'m'} & 4);
886 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
887 $prefix_ok = 0;
888 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
889 my $vexname = $1;
890 my $c = $vexmap{$vexname};
891 my ($m,$w,$l,$p) = (undef,2,undef,0);
892 my $has_nds = 0;
893 my @subops = split(/\./, $op);
894 shift @subops; # Drop prefix
895 foreach $oq (@subops) {
896 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
897 $l = 0;
898 } elsif ($oq eq '256' || $oq eq 'l1') {
899 $l = 1;
900 } elsif ($oq eq 'lig') {
901 $l = 2;
902 } elsif ($oq eq 'w0') {
903 $w = 0;
904 } elsif ($oq eq 'w1') {
905 $w = 1;
906 } elsif ($oq eq 'wig') {
907 $w = 2;
908 } elsif ($oq eq 'ww') {
909 $w = 3;
910 } elsif ($oq eq 'p0') {
911 $p = 0;
912 } elsif ($oq eq '66' || $oq eq 'p1') {
913 $p = 1;
914 } elsif ($oq eq 'f3' || $oq eq 'p2') {
915 $p = 2;
916 } elsif ($oq eq 'f2' || $oq eq 'p3') {
917 $p = 3;
918 } elsif ($oq eq '0f') {
919 $m = 1;
920 } elsif ($oq eq '0f38') {
921 $m = 2;
922 } elsif ($oq eq '0f3a') {
923 $m = 3;
924 } elsif ($oq =~ /^m([0-9]+)$/) {
925 $m = $1+0;
926 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
927 if (!defined($oppos{'v'})) {
928 die "$fname:$line: $vexname.$oq without 'v' operand\n";
930 $has_nds = 1;
931 } else {
932 die "$fname:$line: undefined \U$vexname\E subcode: $oq\n";
935 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
936 die "$fname:$line: missing fields in \U$vexname\E specification\n";
938 if (defined($oppos{'v'}) && !$has_nds) {
939 die "$fname:$line: 'v' operand without ${vexname}.nds or ${vexname}.ndd\n";
941 my $minmap = ($c == 1) ? 8 : 0; # 0-31 for VEX, 8-31 for XOP
942 if ($m < $minmap || $m > 31) {
943 die "$fname:$line: Only maps ${minmap}-31 are valid for \U${vexname}\n";
945 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
946 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
947 $prefix_ok = 0;
948 } elsif ($op =~ /^(evex)(|\..*)$/) {
949 my $c = $vexmap{$1};
950 my ($m,$w,$l,$p) = (undef,2,undef,0);
951 my $has_nds = 0;
952 my @subops = split(/\./, $op);
953 shift @subops; # Drop prefix
954 foreach $oq (@subops) {
955 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz' || $oq eq 'lig') {
956 $l = 0;
957 } elsif ($oq eq '256' || $oq eq 'l1') {
958 $l = 1;
959 } elsif ($oq eq '512' || $oq eq 'l2') {
960 $l = 2;
961 } elsif ($oq eq 'w0') {
962 $w = 0;
963 } elsif ($oq eq 'w1') {
964 $w = 1;
965 } elsif ($oq eq 'wig') {
966 $w = 2;
967 } elsif ($oq eq 'ww') {
968 $w = 3;
969 } elsif ($oq eq 'p0') {
970 $p = 0;
971 } elsif ($oq eq '66' || $oq eq 'p1') {
972 $p = 1;
973 } elsif ($oq eq 'f3' || $oq eq 'p2') {
974 $p = 2;
975 } elsif ($oq eq 'f2' || $oq eq 'p3') {
976 $p = 3;
977 } elsif ($oq eq '0f') {
978 $m = 1;
979 } elsif ($oq eq '0f38') {
980 $m = 2;
981 } elsif ($oq eq '0f3a') {
982 $m = 3;
983 } elsif ($oq =~ /^m([0-9]+)$/) {
984 $m = $1+0;
985 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
986 if (!defined($oppos{'v'})) {
987 die "$fname:$line: evex.$oq without 'v' operand\n";
989 $has_nds = 1;
990 } else {
991 die "$fname:$line: undefined EVEX subcode: $oq\n";
994 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
995 die "$fname:$line: missing fields in EVEX specification\n";
997 if (defined($oppos{'v'}) && !$has_nds) {
998 die "$fname:$line: 'v' operand without evex.nds or evex.ndd\n";
1000 if ($m > 15) {
1001 die "$fname:$line: Only maps 0-15 are valid for EVEX\n";
1003 push(@codes, defined($oppos{'v'}) ? 0240+($oppos{'v'} & 3) : 0250,
1004 ($c << 6)+$m, ($w << 4)+($l << 2)+$p, $tup);
1005 $prefix_ok = 0;
1006 } elsif (defined $imm_codes{$op}) {
1007 if ($op eq 'seg') {
1008 if ($last_imm lt 'i') {
1009 die "$fname:$line: seg without an immediate operand\n";
1011 } else {
1012 $last_imm++;
1013 if ($last_imm gt 'j') {
1014 die "$fname:$line: too many immediate operands\n";
1017 if (!defined($oppos{$last_imm})) {
1018 die "$fname:$line: $op without '$last_imm' operand\n";
1020 push(@codes, 05) if ($oppos{$last_imm} & 4);
1021 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
1022 $prefix_ok = 0;
1023 } elsif ($op eq '/is4') {
1024 if (!defined($oppos{'s'})) {
1025 die "$fname:$line: $op without 's' operand\n";
1027 if (defined($oppos{'i'})) {
1028 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
1029 } else {
1030 push(@codes, 05) if ($oppos{'s'} & 4);
1031 push(@codes, 0174+($oppos{'s'} & 3));
1033 $prefix_ok = 0;
1034 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
1035 my $imm = $1;
1036 if (!defined($oppos{'s'})) {
1037 die "$fname:$line: $op without 's' operand\n";
1039 if ($imm < 0 || $imm > 15) {
1040 die "$fname:$line: invalid imm4 value for $op: $imm\n";
1042 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
1043 $prefix_ok = 0;
1044 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
1045 push(@codes, 0330, hex $1);
1046 $prefix_ok = 0;
1047 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
1048 if (!defined($oppos{'r'})) {
1049 die "$fname:$line: $op without 'r' operand\n";
1051 push(@codes, 05) if ($oppos{'r'} & 4);
1052 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
1053 $prefix_ok = 0;
1054 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1055 # Escape to enter literal bytecodes
1056 push(@codes, oct $1);
1057 } else {
1058 die "$fname:$line: unknown operation: $op\n";
1062 return @codes;