insns.pl: use less cantankerous string expansion; better error info
[nasm.git] / x86 / insns.pl
blob9cf6512a363b30af881df609e0900937d98e9908
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 $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 # expand the flags
502 my @flags;
503 foreach my $flag (split(',', $flags)) {
504 if ($flag eq 'ND') {
505 $nd = 1;
506 } elsif ($flag eq 'X64') {
507 push(@flags, 'LONG', 'X86_64');
508 } elsif ($flag ne '') {
509 push(@flags, $flag);
513 if ($codes =~ /evex\./) {
514 push(@flags, 'EVEX');
515 } elsif ($codes =~ /(vex|xop)\./) {
516 push(@flags, 'VEX');
519 $flagsindex = insns_flag_index(@flags);
520 die "$fname:$line: error in flags $flags" unless (defined($flagsindex));
522 @bytecode = (decodify($codes, $relax), 0);
523 push(@bytecode_list, [@bytecode]);
524 $codes = hexstr(@bytecode);
525 count_bytecodes(@bytecode);
527 ("{I_$opcode, $num, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flagsindex},", $nd);
531 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
532 # offset into nasm_bytecodes
534 sub codesubst($) {
535 my($s) = @_;
536 my $n;
538 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
539 my $pos = $bytecode_pos{$1};
540 if (!defined($pos)) {
541 die "$fname:$line: no position assigned to byte code $1\n";
543 $s = $` . "nasm_bytecodes+${pos}" . "$'";
545 return $s;
548 sub addprefix ($@) {
549 my ($prefix, @list) = @_;
550 my $x;
551 my @l = ();
553 foreach $x (@list) {
554 push(@l, sprintf("%s%02X", $prefix, $x));
557 return @l;
561 # Turn a code string into a sequence of bytes
563 sub decodify($$) {
564 # Although these are C-syntax strings, by convention they should have
565 # only octal escapes (for directives) and hexadecimal escapes
566 # (for verbatim bytes)
567 my($codestr, $relax) = @_;
569 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
570 return byte_code_compile($1, $relax);
573 my $c = $codestr;
574 my @codes = ();
576 unless ($codestr eq 'ignore') {
577 while ($c ne '') {
578 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
579 push(@codes, hex $1);
580 $c = $2;
581 next;
582 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
583 push(@codes, oct $1);
584 $c = $2;
585 next;
586 } else {
587 die "$fname:$line: unknown code format in \"$codestr\"\n";
592 return @codes;
595 # Turn a numeric list into a hex string
596 sub hexstr(@) {
597 my $s = '';
598 my $c;
600 foreach $c (@_) {
601 $s .= sprintf("%02X", $c);
603 return $s;
606 # Here we determine the range of possible starting bytes for a given
607 # instruction. We need only consider the codes:
608 # \[1234] mean literal bytes, of course
609 # \1[0123] mean byte plus register value
610 # \330 means byte plus condition code
611 # \0 or \340 mean give up and return empty set
612 # \34[4567] mean PUSH/POP of segment registers: special case
613 # \17[234] skip is4 control byte
614 # \26x \270 skip VEX control bytes
615 # \24x \250 skip EVEX control bytes
616 sub startseq($$) {
617 my ($codestr, $relax) = @_;
618 my $word, @range;
619 my @codes = ();
620 my $c = $codestr;
621 my $c0, $c1, $i;
622 my $prefix = '';
624 @codes = decodify($codestr, $relax);
626 while ($c0 = shift(@codes)) {
627 $c1 = $codes[0];
628 if ($c0 >= 01 && $c0 <= 04) {
629 # Fixed byte string
630 my $fbs = $prefix;
631 while (1) {
632 if ($c0 >= 01 && $c0 <= 04) {
633 while ($c0--) {
634 $fbs .= sprintf("%02X", shift(@codes));
636 } else {
637 last;
639 $c0 = shift(@codes);
642 foreach $pfx (@disasm_prefixes) {
643 if (substr($fbs, 0, length($pfx)) eq $pfx) {
644 $prefix = $pfx;
645 $fbs = substr($fbs, length($pfx));
646 last;
650 if ($fbs ne '') {
651 return ($prefix.substr($fbs,0,2));
654 unshift(@codes, $c0);
655 } elsif ($c0 >= 010 && $c0 <= 013) {
656 return addprefix($prefix, $c1..($c1+7));
657 } elsif (($c0 & ~013) == 0144) {
658 return addprefix($prefix, $c1, $c1|2);
659 } elsif ($c0 == 0330) {
660 return addprefix($prefix, $c1..($c1+15));
661 } elsif ($c0 == 0 || $c0 == 0340) {
662 return $prefix;
663 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
664 ($c0 & ~3) == 0240 || $c0 == 0250) {
665 my $c,$m,$wlp;
666 $m = shift(@codes);
667 $wlp = shift(@codes);
668 $c = ($m >> 6);
669 $m = $m & 31;
670 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
671 if ($c0 < 0260) {
672 my $tuple = shift(@codes);
674 } elsif ($c0 >= 0172 && $c0 <= 173) {
675 shift(@codes); # Skip is4 control byte
676 } else {
677 # We really need to be able to distinguish "forbidden"
678 # and "ignorable" codes here
681 return $prefix;
684 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
685 sub tupletype($) {
686 my ($tuplestr) = @_;
687 my %tuple_codes = (
688 '' => 000,
689 'fv' => 001,
690 'hv' => 002,
691 'fvm' => 003,
692 't1s8' => 004,
693 't1s16' => 005,
694 't1s' => 006,
695 't1f32' => 007,
696 't1f64' => 010,
697 't2' => 011,
698 't4' => 012,
699 't8' => 013,
700 'hvm' => 014,
701 'qvm' => 015,
702 'ovm' => 016,
703 'm128' => 017,
704 'dup' => 020,
707 if (defined $tuple_codes{$tuplestr}) {
708 return 0300 + $tuple_codes{$tuplestr};
709 } else {
710 die "$fname:$line: undefined tuple type : $tuplestr\n";
715 # This function takes a series of byte codes in a format which is more
716 # typical of the Intel documentation, and encode it.
718 # The format looks like:
720 # [operands: opcodes]
722 # The operands word lists the order of the operands:
724 # r = register field in the modr/m
725 # m = modr/m
726 # v = VEX "v" field
727 # i = immediate
728 # s = register field of is4/imz2 field
729 # - = implicit (unencoded) operand
730 # x = indeX register of mib. 014..017 bytecodes are used.
732 # For an operand that should be filled into more than one field,
733 # enter it as e.g. "r+v".
735 sub byte_code_compile($$) {
736 my($str, $relax) = @_;
737 my $opr;
738 my $opc;
739 my @codes = ();
740 my $litix = undef;
741 my %oppos = ();
742 my $i;
743 my $op, $oq;
744 my $opex;
746 my %imm_codes = (
747 'ib' => 020, # imm8
748 'ib,u' => 024, # Unsigned imm8
749 'iw' => 030, # imm16
750 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
751 'iwd' => 034, # imm16 or imm32, depending on opsize
752 'id' => 040, # imm32
753 'id,s' => 0254, # imm32 sign-extended to 64 bits
754 'iwdq' => 044, # imm16/32/64, depending on addrsize
755 'rel8' => 050,
756 'iq' => 054,
757 'rel16' => 060,
758 'rel' => 064, # 16 or 32 bit relative operand
759 'rel32' => 070,
760 'seg' => 074,
762 my %plain_codes = (
763 'o16' => 0320, # 16-bit operand size
764 'o32' => 0321, # 32-bit operand size
765 'odf' => 0322, # Operand size is default
766 'o64' => 0324, # 64-bit operand size requiring REX.W
767 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
768 'a16' => 0310,
769 'a32' => 0311,
770 'adf' => 0312, # Address size is default
771 'a64' => 0313,
772 '!osp' => 0364,
773 '!asp' => 0365,
774 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
775 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
776 'mustrep' => 0336,
777 'mustrepne' => 0337,
778 'rex.l' => 0334,
779 'norexb' => 0314,
780 'norexx' => 0315,
781 'norexr' => 0316,
782 'norexw' => 0317,
783 'repe' => 0335,
784 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
785 'nof3' => 0326, # No REP 0xF3 prefix permitted
786 'norep' => 0331, # No REP prefix permitted
787 'wait' => 0341, # Needs a wait prefix
788 'resb' => 0340,
789 'np' => 0360, # No prefix
790 'jcc8' => 0370, # Match only if Jcc possible with single byte
791 'jmp8' => 0371, # Match only if JMP possible with single byte
792 'jlen' => 0373, # Length of jump
793 'hlexr' => 0271,
794 'hlenl' => 0272,
795 'hle' => 0273,
797 # This instruction takes XMM VSIB
798 'vsibx' => 0374,
799 'vm32x' => 0374,
800 'vm64x' => 0374,
802 # This instruction takes YMM VSIB
803 'vsiby' => 0375,
804 'vm32y' => 0375,
805 'vm64y' => 0375,
807 # This instruction takes ZMM VSIB
808 'vsibz' => 0376,
809 'vm32z' => 0376,
810 'vm64z' => 0376,
813 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
814 die "$fname:$line: cannot parse: [$str]\n";
816 $opr = "\L$2";
817 $tuple = "\L$3"; # Tuple type for AVX512
818 $opc = "\L$4";
820 my $op = 0;
821 for ($i = 0; $i < length($opr); $i++) {
822 my $c = substr($opr,$i,1);
823 if ($c eq '+') {
824 $op--;
825 } else {
826 if ($relax & 1) {
827 $op--;
829 $relax >>= 1;
830 $oppos{$c} = $op++;
833 $tup = tupletype($tuple);
835 my $last_imm = 'h';
836 my $prefix_ok = 1;
837 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
838 my $pc = $plain_codes{$op};
840 if (defined $pc) {
841 # Plain code
842 push(@codes, $pc);
843 } elsif ($prefix_ok && $op =~ /^(66|f2|f3)$/) {
844 # 66/F2/F3 prefix used as an opcode extension
845 if ($op eq '66') {
846 push(@codes, 0361);
847 } elsif ($op eq 'f2') {
848 push(@codes, 0332);
849 } else {
850 push(@codes, 0333);
852 } elsif ($op =~ /^[0-9a-f]{2}$/) {
853 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
854 $codes[$litix] < 4) {
855 $codes[$litix]++;
856 push(@codes, hex $op);
857 } else {
858 $litix = scalar(@codes);
859 push(@codes, 01, hex $op);
861 $prefix_ok = 0;
862 } elsif ($op eq '/r') {
863 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
864 die "$fname:$line: $op requires r and m operands\n";
866 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
867 (($oppos{'r'} & 4) ? 05 : 0);
868 push(@codes, $opex) if ($opex);
869 # if mib is composed with two separate operands - ICC style
870 push(@codes, 014 + ($oppos{'x'} & 3)) if (defined($oppos{'x'}));
871 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
872 $prefix_ok = 0;
873 } elsif ($op =~ m:^/([0-7])$:) {
874 if (!defined($oppos{'m'})) {
875 die "$fname:$line: $op requires m operand\n";
877 push(@codes, 06) if ($oppos{'m'} & 4);
878 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
879 $prefix_ok = 0;
880 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
881 my $vexname = $1;
882 my $c = $vexmap{$vexname};
883 my ($m,$w,$l,$p) = (undef,2,undef,0);
884 my $has_nds = 0;
885 my @subops = split(/\./, $op);
886 shift @subops; # Drop prefix
887 foreach $oq (@subops) {
888 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
889 $l = 0;
890 } elsif ($oq eq '256' || $oq eq 'l1') {
891 $l = 1;
892 } elsif ($oq eq 'lig') {
893 $l = 2;
894 } elsif ($oq eq 'w0') {
895 $w = 0;
896 } elsif ($oq eq 'w1') {
897 $w = 1;
898 } elsif ($oq eq 'wig') {
899 $w = 2;
900 } elsif ($oq eq 'ww') {
901 $w = 3;
902 } elsif ($oq eq 'p0') {
903 $p = 0;
904 } elsif ($oq eq '66' || $oq eq 'p1') {
905 $p = 1;
906 } elsif ($oq eq 'f3' || $oq eq 'p2') {
907 $p = 2;
908 } elsif ($oq eq 'f2' || $oq eq 'p3') {
909 $p = 3;
910 } elsif ($oq eq '0f') {
911 $m = 1;
912 } elsif ($oq eq '0f38') {
913 $m = 2;
914 } elsif ($oq eq '0f3a') {
915 $m = 3;
916 } elsif ($oq =~ /^m([0-9]+)$/) {
917 $m = $1+0;
918 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
919 if (!defined($oppos{'v'})) {
920 die "$fname:$line: $vexname.$oq without 'v' operand\n";
922 $has_nds = 1;
923 } else {
924 die "$fname:$line: undefined \U$vexname\E subcode: $oq\n";
927 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
928 die "$fname:$line: missing fields in \U$vexname\E specification\n";
930 if (defined($oppos{'v'}) && !$has_nds) {
931 die "$fname:$line: 'v' operand without ${vexname}.nds or ${vexname}.ndd\n";
933 my $minmap = ($c == 1) ? 8 : 0; # 0-31 for VEX, 8-31 for XOP
934 if ($m < $minmap || $m > 31) {
935 die "$fname:$line: Only maps ${minmap}-31 are valid for \U${vexname}\n";
937 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
938 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
939 $prefix_ok = 0;
940 } elsif ($op =~ /^(evex)(|\..*)$/) {
941 my $c = $vexmap{$1};
942 my ($m,$w,$l,$p) = (undef,2,undef,0);
943 my $has_nds = 0;
944 my @subops = split(/\./, $op);
945 shift @subops; # Drop prefix
946 foreach $oq (@subops) {
947 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz' || $oq eq 'lig') {
948 $l = 0;
949 } elsif ($oq eq '256' || $oq eq 'l1') {
950 $l = 1;
951 } elsif ($oq eq '512' || $oq eq 'l2') {
952 $l = 2;
953 } elsif ($oq eq 'w0') {
954 $w = 0;
955 } elsif ($oq eq 'w1') {
956 $w = 1;
957 } elsif ($oq eq 'wig') {
958 $w = 2;
959 } elsif ($oq eq 'ww') {
960 $w = 3;
961 } elsif ($oq eq 'p0') {
962 $p = 0;
963 } elsif ($oq eq '66' || $oq eq 'p1') {
964 $p = 1;
965 } elsif ($oq eq 'f3' || $oq eq 'p2') {
966 $p = 2;
967 } elsif ($oq eq 'f2' || $oq eq 'p3') {
968 $p = 3;
969 } elsif ($oq eq '0f') {
970 $m = 1;
971 } elsif ($oq eq '0f38') {
972 $m = 2;
973 } elsif ($oq eq '0f3a') {
974 $m = 3;
975 } elsif ($oq =~ /^m([0-9]+)$/) {
976 $m = $1+0;
977 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
978 if (!defined($oppos{'v'})) {
979 die "$fname:$line: evex.$oq without 'v' operand\n";
981 $has_nds = 1;
982 } else {
983 die "$fname:$line: undefined EVEX subcode: $oq\n";
986 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
987 die "$fname:$line: missing fields in EVEX specification\n";
989 if (defined($oppos{'v'}) && !$has_nds) {
990 die "$fname:$line: 'v' operand without evex.nds or evex.ndd\n";
992 if ($m > 15) {
993 die "$fname:$line: Only maps 0-15 are valid for EVEX\n";
995 push(@codes, defined($oppos{'v'}) ? 0240+($oppos{'v'} & 3) : 0250,
996 ($c << 6)+$m, ($w << 4)+($l << 2)+$p, $tup);
997 $prefix_ok = 0;
998 } elsif (defined $imm_codes{$op}) {
999 if ($op eq 'seg') {
1000 if ($last_imm lt 'i') {
1001 die "$fname:$line: seg without an immediate operand\n";
1003 } else {
1004 $last_imm++;
1005 if ($last_imm gt 'j') {
1006 die "$fname:$line: too many immediate operands\n";
1009 if (!defined($oppos{$last_imm})) {
1010 die "$fname:$line: $op without '$last_imm' operand\n";
1012 push(@codes, 05) if ($oppos{$last_imm} & 4);
1013 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
1014 $prefix_ok = 0;
1015 } elsif ($op eq '/is4') {
1016 if (!defined($oppos{'s'})) {
1017 die "$fname:$line: $op without 's' operand\n";
1019 if (defined($oppos{'i'})) {
1020 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
1021 } else {
1022 push(@codes, 05) if ($oppos{'s'} & 4);
1023 push(@codes, 0174+($oppos{'s'} & 3));
1025 $prefix_ok = 0;
1026 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
1027 my $imm = $1;
1028 if (!defined($oppos{'s'})) {
1029 die "$fname:$line: $op without 's' operand\n";
1031 if ($imm < 0 || $imm > 15) {
1032 die "$fname:$line: invalid imm4 value for $op: $imm\n";
1034 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
1035 $prefix_ok = 0;
1036 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
1037 push(@codes, 0330, hex $1);
1038 $prefix_ok = 0;
1039 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
1040 if (!defined($oppos{'r'})) {
1041 die "$fname:$line: $op without 'r' operand\n";
1043 push(@codes, 05) if ($oppos{'r'} & 4);
1044 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
1045 $prefix_ok = 0;
1046 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1047 # Escape to enter literal bytecodes
1048 push(@codes, oct $1);
1049 } else {
1050 die "$fname:$line: unknown operation: $op\n";
1054 return @codes;