iflag: Fix dependencies, factor out static components of iflag.h
[nasm.git] / insns.pl
blob8515c02e7dbad99850fb9a8d096dd58ec09d6e6d
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2013 The NASM Authors - All Rights Reserved
5 ## See the file AUTHORS included with the NASM distribution for
6 ## the specific copyright holders.
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 'insns-iflags.pl';
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 $fname = "insns.dat" unless $fname = $args[0];
83 open (F, $fname) || die "unable to open $fname";
85 %dinstables = ();
86 @bytecode_list = ();
88 $line = 0;
89 $insns = 0;
90 while (<F>) {
91 $line++;
92 chomp;
93 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
95 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
96 warn "line $line does not contain four fields\n";
97 next;
99 @fields = ($1, $2, $3, $4);
100 @field_list = ([@fields, 0]);
102 if ($fields[1] =~ /\*/) {
103 # This instruction has relaxed form(s)
104 if ($fields[2] !~ /^\[/) {
105 warn "line $line has an * operand but uses raw bytecodes\n";
106 next;
109 $opmask = 0;
110 @ops = split(/,/, $fields[1]);
111 for ($oi = 0; $oi < scalar @ops; $oi++) {
112 if ($ops[$oi] =~ /\*$/) {
113 if ($oi == 0) {
114 warn "line $line has a first operand with a *\n";
115 next;
117 $opmask |= 1 << $oi;
121 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
122 if (($oi & ~$opmask) == 0) {
123 my @xops = ();
124 my $omask = ~$oi;
125 for ($oj = 0; $oj < scalar(@ops); $oj++) {
126 if ($omask & 1) {
127 push(@xops, $ops[$oj]);
129 $omask >>= 1;
131 push(@field_list, [$fields[0], join(',', @xops),
132 $fields[2], $fields[3], $oi]);
137 foreach $fptr (@field_list) {
138 @fields = @$fptr;
139 ($formatted, $nd) = format_insn(@fields);
140 if ($formatted) {
141 $insns++;
142 $aname = "aa_$fields[0]";
143 push @$aname, $formatted;
145 if ( $fields[0] =~ /cc$/ ) {
146 # Conditional instruction
147 $k_opcodes_cc{$fields[0]}++;
148 } else {
149 # Unconditional instruction
150 $k_opcodes{$fields[0]}++;
152 if ($formatted && !$nd) {
153 push @big, $formatted;
154 my @sseq = startseq($fields[2], $fields[4]);
155 foreach $i (@sseq) {
156 if (!defined($dinstables{$i})) {
157 $dinstables{$i} = [];
159 push(@{$dinstables{$i}}, $#big);
165 close F;
168 # Generate the bytecode array. At this point, @bytecode_list contains
169 # the full set of bytecodes.
172 # Sort by descending length
173 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
174 @bytecode_array = ();
175 %bytecode_pos = ();
176 $bytecode_next = 0;
177 foreach $bl (@bytecode_list) {
178 my $h = hexstr(@$bl);
179 next if (defined($bytecode_pos{$h}));
181 push(@bytecode_array, $bl);
182 while ($h ne '') {
183 $bytecode_pos{$h} = $bytecode_next;
184 $h = substr($h, 2);
185 $bytecode_next++;
188 undef @bytecode_list;
190 @opcodes = sort keys(%k_opcodes);
191 @opcodes_cc = sort keys(%k_opcodes_cc);
193 if ( !defined($output) || $output eq 'b') {
194 print STDERR "Writing insnsb.c...\n";
196 open B, ">insnsb.c";
198 print B "/* This file auto-generated from insns.dat by insns.pl" .
199 " - don't edit it */\n\n";
201 print B "#include \"nasm.h\"\n";
202 print B "#include \"insns.h\"\n\n";
204 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
206 $p = 0;
207 foreach $bl (@bytecode_array) {
208 printf B " /* %5d */ ", $p;
209 foreach $d (@$bl) {
210 printf B "%#o,", $d;
211 $p++;
213 printf B "\n";
215 print B "};\n";
217 print B "\n";
218 print B "/*\n";
219 print B " * Bytecode frequencies (including reuse):\n";
220 print B " *\n";
221 for ($i = 0; $i < 32; $i++) {
222 print B " *";
223 for ($j = 0; $j < 256; $j += 32) {
224 print B " |" if ($j);
225 printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
227 print B "\n";
229 print B " */\n";
231 close B;
234 if ( !defined($output) || $output eq 'a' ) {
235 print STDERR "Writing insnsa.c...\n";
237 open A, ">insnsa.c";
239 print A "/* This file auto-generated from insns.dat by insns.pl" .
240 " - don't edit it */\n\n";
242 print A "#include \"nasm.h\"\n";
243 print A "#include \"insns.h\"\n\n";
245 foreach $i (@opcodes, @opcodes_cc) {
246 print A "static const struct itemplate instrux_${i}[] = {\n";
247 $aname = "aa_$i";
248 foreach $j (@$aname) {
249 print A " ", codesubst($j), "\n";
251 print A " ITEMPLATE_END\n};\n\n";
253 print A "const struct itemplate * const nasm_instructions[] = {\n";
254 foreach $i (@opcodes, @opcodes_cc) {
255 print A " instrux_${i},\n";
257 print A "};\n";
259 close A;
262 if ( !defined($output) || $output eq 'd' ) {
263 print STDERR "Writing insnsd.c...\n";
265 open D, ">insnsd.c";
267 print D "/* This file auto-generated from insns.dat by insns.pl" .
268 " - don't edit it */\n\n";
270 print D "#include \"nasm.h\"\n";
271 print D "#include \"insns.h\"\n\n";
273 print D "static const struct itemplate instrux[] = {\n";
274 $n = 0;
275 foreach $j (@big) {
276 printf D " /* %4d */ %s\n", $n++, codesubst($j);
278 print D "};\n";
280 foreach $h (sort(keys(%dinstables))) {
281 next if ($h eq ''); # Skip pseudo-instructions
282 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
283 foreach $j (@{$dinstables{$h}}) {
284 print D " instrux + $j,\n";
286 print D "};\n";
289 @prefix_list = ();
290 foreach $h (@disasm_prefixes, '') {
291 for ($c = 0; $c < 256; $c++) {
292 $nn = sprintf("%s%02X", $h, $c);
293 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
294 # At least one entry in this prefix table
295 push(@prefix_list, $h);
296 $is_prefix{$h} = 1;
297 last;
302 foreach $h (@prefix_list) {
303 print D "\n";
304 print D "static " unless ($h eq '');
305 print D "const struct disasm_index ";
306 print D ($h eq '') ? 'itable' : "itable_$h";
307 print D "[256] = {\n";
308 for ($c = 0; $c < 256; $c++) {
309 $nn = sprintf("%s%02X", $h, $c);
310 if ($is_prefix{$nn}) {
311 die "$fname: ambiguous decoding of $nn\n"
312 if (defined($dinstables{$nn}));
313 printf D " /* 0x%02x */ { itable_%s, -1 },\n", $c, $nn;
314 } elsif (defined($dinstables{$nn})) {
315 printf D " /* 0x%02x */ { itable_%s, %u },\n", $c,
316 $nn, scalar(@{$dinstables{$nn}});
317 } else {
318 printf D " /* 0x%02x */ { NULL, 0 },\n", $c;
321 print D "};\n";
324 printf D "\nconst struct disasm_index * const itable_vex[NASM_VEX_CLASSES][32][4] =\n";
325 print D "{\n";
326 for ($c = 0; $c < $vex_classes; $c++) {
327 print D " {\n";
328 for ($m = 0; $m < 32; $m++) {
329 print D " { ";
330 for ($p = 0; $p < 4; $p++) {
331 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $p);
332 printf D "%-15s",
333 ($is_prefix{$vp} ? sprintf("itable_%s,", $vp) : 'NULL,');
335 print D "},\n";
337 print D " },\n";
339 print D "};\n";
341 close D;
344 if ( !defined($output) || $output eq 'i' ) {
345 print STDERR "Writing insnsi.h...\n";
347 open I, ">insnsi.h";
349 print I "/* This file is auto-generated from insns.dat by insns.pl" .
350 " - don't edit it */\n\n";
351 print I "/* This file in included by nasm.h */\n\n";
353 print I "/* Instruction names */\n\n";
354 print I "#ifndef NASM_INSNSI_H\n";
355 print I "#define NASM_INSNSI_H 1\n\n";
356 print I "enum opcode {\n";
357 $maxlen = 0;
358 foreach $i (@opcodes, @opcodes_cc) {
359 print I "\tI_${i},\n";
360 $len = length($i);
361 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
362 $maxlen = $len if ( $len > $maxlen );
364 print I "\tI_none = -1\n";
365 print I "};\n\n";
366 print I "#define MAX_INSLEN ", $maxlen, "\n";
367 print I "#define NASM_VEX_CLASSES ", $vex_classes, "\n";
368 print I "#define NO_DECORATOR\t{", join(',',(0) x $MAX_OPERANDS), "}\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 if ( !defined($output) || $output eq 'fh') {
399 write_iflaggen_h();
402 if ( !defined($output) || $output eq 'fc') {
403 write_iflag_c();
406 printf STDERR "Done: %d instructions\n", $insns;
408 # Count primary bytecodes, for statistics
409 sub count_bytecodes(@) {
410 my $skip = 0;
411 foreach my $bc (@_) {
412 if ($skip) {
413 $skip--;
414 next;
416 $bytecode_count[$bc]++;
417 if ($bc >= 01 && $bc <= 04) {
418 $skip = $bc;
419 } elsif (($bc & ~03) == 010) {
420 $skip = 1;
421 } elsif (($bc & ~013) == 0144) {
422 $skip = 1;
423 } elsif ($bc == 0172 || $bc == 0173) {
424 $skip = 1;
425 } elsif (($bc & ~3) == 0260 || $bc == 0270) { # VEX
426 $skip = 2;
427 } elsif (($bc & ~3) == 0240 || $bc == 0250) { # EVEX
428 $skip = 3;
429 } elsif ($bc == 0330) {
430 $skip = 1;
435 sub format_insn($$$$$) {
436 my ($opcode, $operands, $codes, $flags, $relax) = @_;
437 my $num, $nd = 0, $rawflags, $flagsindex;
438 my @bytecode;
439 my $op, @ops, $opp, @opx, @oppx, @decos, @opevex;
441 return (undef, undef) if $operands eq "ignore";
443 # format the operands
444 $operands =~ s/\*//g;
445 $operands =~ s/:/|colon,/g;
446 @ops = ();
447 @decos = ();
448 if ($operands ne 'void') {
449 foreach $op (split(/,/, $operands)) {
450 @opx = ();
451 @opevex = ();
452 foreach $opp (split(/\|/, $op)) {
453 @oppx = ();
454 if ($opp =~ s/^(b(32|64)|mask|z|er|sae)$//) {
455 push(@opevex, $1);
458 if ($opp =~ s/(?<!\d)(8|16|32|64|80|128|256|512)$//) {
459 push(@oppx, "bits$1");
461 $opp =~ s/^mem$/memory/;
462 $opp =~ s/^memory_offs$/mem_offs/;
463 $opp =~ s/^imm$/immediate/;
464 $opp =~ s/^([a-z]+)rm$/rm_$1/;
465 $opp =~ s/^rm$/rm_gpr/;
466 $opp =~ s/^reg$/reg_gpr/;
467 push(@opx, $opp, @oppx) if $opp;
469 $op = join('|', @opx);
470 push(@ops, $op);
471 push(@decos, (@opevex ? join('|', @opevex) : '0'));
475 $num = scalar(@ops);
476 while (scalar(@ops) < $MAX_OPERANDS) {
477 push(@ops, '0');
478 push(@decos, '0');
480 $operands = join(',', @ops);
481 $operands =~ tr/a-z/A-Z/;
483 $decorators = "{" . join(',', @decos) . "}";
484 if ($decorators =~ /^{(0,)+0}$/) {
485 $decorators = "NO_DECORATOR";
487 $decorators =~ tr/a-z/A-Z/;
489 # format the flags
490 $nd = 1 if $flags =~ /(^|\,)ND($|\,)/;
491 $flags =~ s/(^|\,)ND($|\,)/\1/g;
492 $flags =~ s/(^|\,)X64($|\,)/\1LONG,X86_64\2/g;
493 $flags =~ s/(^|\,)AVX512CD($|\,)/\1AVX512CD,AVX512\2/g;
494 $flags =~ s/(^|\,)AVX512ER($|\,)/\1AVX512ER,AVX512\2/g;
495 $flags =~ s/(^|\,)AVX512PF($|\,)/\1AVX512PF,AVX512\2/g;
496 $rawflags = $flags;
497 $flagsindex = insns_flag_index(split(',',$flags));
499 die "Error in flags $rawflags" if not defined($flagsindex);
501 @bytecode = (decodify($codes, $relax), 0);
502 push(@bytecode_list, [@bytecode]);
503 $codes = hexstr(@bytecode);
504 count_bytecodes(@bytecode);
506 ("{I_$opcode, $num, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flagsindex},", $nd);
510 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
511 # offset into nasm_bytecodes
513 sub codesubst($) {
514 my($s) = @_;
515 my $n;
517 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
518 my $pos = $bytecode_pos{$1};
519 if (!defined($pos)) {
520 die "$fname: no position assigned to byte code $1\n";
522 $s = $` . "nasm_bytecodes+${pos}" . "$'";
524 return $s;
527 sub addprefix ($@) {
528 my ($prefix, @list) = @_;
529 my $x;
530 my @l = ();
532 foreach $x (@list) {
533 push(@l, sprintf("%s%02X", $prefix, $x));
536 return @l;
540 # Turn a code string into a sequence of bytes
542 sub decodify($$) {
543 # Although these are C-syntax strings, by convention they should have
544 # only octal escapes (for directives) and hexadecimal escapes
545 # (for verbatim bytes)
546 my($codestr, $relax) = @_;
548 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
549 return byte_code_compile($1, $relax);
552 my $c = $codestr;
553 my @codes = ();
555 unless ($codestr eq 'ignore') {
556 while ($c ne '') {
557 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
558 push(@codes, hex $1);
559 $c = $2;
560 next;
561 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
562 push(@codes, oct $1);
563 $c = $2;
564 next;
565 } else {
566 die "$fname: unknown code format in \"$codestr\"\n";
571 return @codes;
574 # Turn a numeric list into a hex string
575 sub hexstr(@) {
576 my $s = '';
577 my $c;
579 foreach $c (@_) {
580 $s .= sprintf("%02X", $c);
582 return $s;
585 # Here we determine the range of possible starting bytes for a given
586 # instruction. We need only consider the codes:
587 # \[1234] mean literal bytes, of course
588 # \1[0123] mean byte plus register value
589 # \330 means byte plus condition code
590 # \0 or \340 mean give up and return empty set
591 # \34[4567] mean PUSH/POP of segment registers: special case
592 # \17[234] skip is4 control byte
593 # \26x \270 skip VEX control bytes
594 # \24x \250 skip EVEX control bytes
595 sub startseq($$) {
596 my ($codestr, $relax) = @_;
597 my $word, @range;
598 my @codes = ();
599 my $c = $codestr;
600 my $c0, $c1, $i;
601 my $prefix = '';
603 @codes = decodify($codestr, $relax);
605 while ($c0 = shift(@codes)) {
606 $c1 = $codes[0];
607 if ($c0 >= 01 && $c0 <= 04) {
608 # Fixed byte string
609 my $fbs = $prefix;
610 while (1) {
611 if ($c0 >= 01 && $c0 <= 04) {
612 while ($c0--) {
613 $fbs .= sprintf("%02X", shift(@codes));
615 } else {
616 last;
618 $c0 = shift(@codes);
621 foreach $pfx (@disasm_prefixes) {
622 if (substr($fbs, 0, length($pfx)) eq $pfx) {
623 $prefix = $pfx;
624 $fbs = substr($fbs, length($pfx));
625 last;
629 if ($fbs ne '') {
630 return ($prefix.substr($fbs,0,2));
633 unshift(@codes, $c0);
634 } elsif ($c0 >= 010 && $c0 <= 013) {
635 return addprefix($prefix, $c1..($c1+7));
636 } elsif (($c0 & ~013) == 0144) {
637 return addprefix($prefix, $c1, $c1|2);
638 } elsif ($c0 == 0330) {
639 return addprefix($prefix, $c1..($c1+15));
640 } elsif ($c0 == 0 || $c0 == 0340) {
641 return $prefix;
642 } elsif (($c0 & ~3) == 0260 || $c0 == 0270 ||
643 ($c0 & ~3) == 0240 || $c0 == 0250) {
644 my $c,$m,$wlp;
645 $m = shift(@codes);
646 $wlp = shift(@codes);
647 $c = ($m >> 6);
648 $m = $m & 31;
649 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
650 if ($c0 < 0260) {
651 my $tuple = shift(@codes);
653 } elsif ($c0 >= 0172 && $c0 <= 173) {
654 shift(@codes); # Skip is4 control byte
655 } else {
656 # We really need to be able to distinguish "forbidden"
657 # and "ignorable" codes here
660 return $prefix;
663 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
664 sub tupletype($) {
665 my ($tuplestr) = @_;
666 my %tuple_codes = (
667 '' => 000,
668 'fv' => 001,
669 'hv' => 002,
670 'fvm' => 003,
671 't1s8' => 004,
672 't1s16' => 005,
673 't1s' => 006,
674 't1f32' => 007,
675 't1f64' => 010,
676 't2' => 011,
677 't4' => 012,
678 't8' => 013,
679 'hvm' => 014,
680 'qvm' => 015,
681 'ovm' => 016,
682 'm128' => 017,
683 'dup' => 020,
686 if (defined $tuple_codes{$tuplestr}) {
687 return 0300 + $tuple_codes{$tuplestr};
688 } else {
689 die "Undefined tuple type : $tuplestr\n";
694 # This function takes a series of byte codes in a format which is more
695 # typical of the Intel documentation, and encode it.
697 # The format looks like:
699 # [operands: opcodes]
701 # The operands word lists the order of the operands:
703 # r = register field in the modr/m
704 # m = modr/m
705 # v = VEX "v" field
706 # i = immediate
707 # s = register field of is4/imz2 field
708 # - = implicit (unencoded) operand
709 # x = indeX register of mib. 014..017 bytecodes are used.
711 # For an operand that should be filled into more than one field,
712 # enter it as e.g. "r+v".
714 sub byte_code_compile($$) {
715 my($str, $relax) = @_;
716 my $opr;
717 my $opc;
718 my @codes = ();
719 my $litix = undef;
720 my %oppos = ();
721 my $i;
722 my $op, $oq;
723 my $opex;
725 my %imm_codes = (
726 'ib' => 020, # imm8
727 'ib,u' => 024, # Unsigned imm8
728 'iw' => 030, # imm16
729 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
730 'iwd' => 034, # imm16 or imm32, depending on opsize
731 'id' => 040, # imm32
732 'id,s' => 0254, # imm32 sign-extended to 64 bits
733 'iwdq' => 044, # imm16/32/64, depending on addrsize
734 'rel8' => 050,
735 'iq' => 054,
736 'rel16' => 060,
737 'rel' => 064, # 16 or 32 bit relative operand
738 'rel32' => 070,
739 'seg' => 074,
741 my %plain_codes = (
742 'o16' => 0320, # 16-bit operand size
743 'o32' => 0321, # 32-bit operand size
744 'odf' => 0322, # Operand size is default
745 'o64' => 0324, # 64-bit operand size requiring REX.W
746 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
747 'a16' => 0310,
748 'a32' => 0311,
749 'adf' => 0312, # Address size is default
750 'a64' => 0313,
751 '!osp' => 0364,
752 '!asp' => 0365,
753 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
754 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
755 'mustrep' => 0336,
756 'mustrepne' => 0337,
757 'rex.l' => 0334,
758 'norexb' => 0314,
759 'norexx' => 0315,
760 'norexr' => 0316,
761 'norexw' => 0317,
762 'repe' => 0335,
763 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
764 'nof3' => 0326, # No REP 0xF3 prefix permitted
765 'norep' => 0331, # No REP prefix permitted
766 'wait' => 0341, # Needs a wait prefix
767 'resb' => 0340,
768 'jcc8' => 0370, # Match only if Jcc possible with single byte
769 'jmp8' => 0371, # Match only if JMP possible with single byte
770 'jlen' => 0373, # Length of jump
771 'hlexr' => 0271,
772 'hlenl' => 0272,
773 'hle' => 0273,
775 # This instruction takes XMM VSIB
776 'vsibx' => 0374,
777 'vm32x' => 0374,
778 'vm64x' => 0374,
780 # This instruction takes YMM VSIB
781 'vsiby' => 0375,
782 'vm32y' => 0375,
783 'vm64y' => 0375,
785 # This instruction takes ZMM VSIB
786 'vsibz' => 0376,
787 'vm32z' => 0376,
788 'vm64z' => 0376,
791 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
792 die "$fname: $line: cannot parse: [$str]\n";
794 $opr = "\L$2";
795 $tuple = "\L$3"; # Tuple type for AVX512
796 $opc = "\L$4";
798 my $op = 0;
799 for ($i = 0; $i < length($opr); $i++) {
800 my $c = substr($opr,$i,1);
801 if ($c eq '+') {
802 $op--;
803 } else {
804 if ($relax & 1) {
805 $op--;
807 $relax >>= 1;
808 $oppos{$c} = $op++;
811 $tup = tupletype($tuple);
813 my $last_imm = 'h';
814 my $prefix_ok = 1;
815 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
816 my $pc = $plain_codes{$op};
818 if (defined $pc) {
819 # Plain code
820 push(@codes, $pc);
821 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
822 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
823 if ($op eq '66') {
824 push(@codes, 0361);
825 } elsif ($op eq 'f2') {
826 push(@codes, 0332);
827 } elsif ($op eq 'f3') {
828 push(@codes, 0333);
829 } else {
830 push(@codes, 0360);
832 } elsif ($op =~ /^[0-9a-f]{2}$/) {
833 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
834 $codes[$litix] < 4) {
835 $codes[$litix]++;
836 push(@codes, hex $op);
837 } else {
838 $litix = scalar(@codes);
839 push(@codes, 01, hex $op);
841 $prefix_ok = 0;
842 } elsif ($op eq '/r') {
843 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
844 die "$fname: $line: $op requires r and m operands\n";
846 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
847 (($oppos{'r'} & 4) ? 05 : 0);
848 push(@codes, $opex) if ($opex);
849 # if mib is composed with two separate operands - ICC style
850 push(@codes, 014 + ($oppos{'x'} & 3)) if (defined($oppos{'x'}));
851 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
852 $prefix_ok = 0;
853 } elsif ($op =~ m:^/([0-7])$:) {
854 if (!defined($oppos{'m'})) {
855 die "$fname: $line: $op requires m operand\n";
857 push(@codes, 06) if ($oppos{'m'} & 4);
858 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
859 $prefix_ok = 0;
860 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
861 my $c = $vexmap{$1};
862 my ($m,$w,$l,$p) = (undef,2,undef,0);
863 my $has_nds = 0;
864 my @subops = split(/\./, $op);
865 shift @subops; # Drop prefix
866 foreach $oq (@subops) {
867 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
868 $l = 0;
869 } elsif ($oq eq '256' || $oq eq 'l1') {
870 $l = 1;
871 } elsif ($oq eq 'lig') {
872 $l = 2;
873 } elsif ($oq eq 'w0') {
874 $w = 0;
875 } elsif ($oq eq 'w1') {
876 $w = 1;
877 } elsif ($oq eq 'wig') {
878 $w = 2;
879 } elsif ($oq eq 'ww') {
880 $w = 3;
881 } elsif ($oq eq 'p0') {
882 $p = 0;
883 } elsif ($oq eq '66' || $oq eq 'p1') {
884 $p = 1;
885 } elsif ($oq eq 'f3' || $oq eq 'p2') {
886 $p = 2;
887 } elsif ($oq eq 'f2' || $oq eq 'p3') {
888 $p = 3;
889 } elsif ($oq eq '0f') {
890 $m = 1;
891 } elsif ($oq eq '0f38') {
892 $m = 2;
893 } elsif ($oq eq '0f3a') {
894 $m = 3;
895 } elsif ($oq =~ /^m([0-9]+)$/) {
896 $m = $1+0;
897 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
898 if (!defined($oppos{'v'})) {
899 die "$fname: $line: vex.$oq without 'v' operand\n";
901 $has_nds = 1;
902 } else {
903 die "$fname: $line: undefined VEX subcode: $oq\n";
906 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
907 die "$fname: $line: missing fields in VEX specification\n";
909 if (defined($oppos{'v'}) && !$has_nds) {
910 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
912 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
913 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
914 $prefix_ok = 0;
915 } elsif ($op =~ /^(evex)(|\..*)$/) {
916 my $c = $vexmap{$1};
917 my ($m,$w,$l,$p) = (undef,2,undef,0);
918 my $has_nds = 0;
919 my @subops = split(/\./, $op);
920 shift @subops; # Drop prefix
921 foreach $oq (@subops) {
922 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz' || $oq eq 'lig') {
923 $l = 0;
924 } elsif ($oq eq '256' || $oq eq 'l1') {
925 $l = 1;
926 } elsif ($oq eq '512' || $oq eq 'l2') {
927 $l = 2;
928 } elsif ($oq eq 'w0') {
929 $w = 0;
930 } elsif ($oq eq 'w1') {
931 $w = 1;
932 } elsif ($oq eq 'wig') {
933 $w = 2;
934 } elsif ($oq eq 'ww') {
935 $w = 3;
936 } elsif ($oq eq 'p0') {
937 $p = 0;
938 } elsif ($oq eq '66' || $oq eq 'p1') {
939 $p = 1;
940 } elsif ($oq eq 'f3' || $oq eq 'p2') {
941 $p = 2;
942 } elsif ($oq eq 'f2' || $oq eq 'p3') {
943 $p = 3;
944 } elsif ($oq eq '0f') {
945 $m = 1;
946 } elsif ($oq eq '0f38') {
947 $m = 2;
948 } elsif ($oq eq '0f3a') {
949 $m = 3;
950 } elsif ($oq =~ /^m([0-9]+)$/) {
951 $m = $1+0;
952 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
953 if (!defined($oppos{'v'})) {
954 die "$fname: $line: evex.$oq without 'v' operand\n";
956 $has_nds = 1;
957 } else {
958 die "$fname: $line: undefined EVEX subcode: $oq\n";
961 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
962 die "$fname: $line: missing fields in EVEX specification\n";
964 if (defined($oppos{'v'}) && !$has_nds) {
965 die "$fname: $line: 'v' operand without evex.nds or evex.ndd\n";
967 push(@codes, defined($oppos{'v'}) ? 0240+($oppos{'v'} & 3) : 0250,
968 ($c << 6)+$m, ($w << 4)+($l << 2)+$p, $tup);
969 $prefix_ok = 0;
970 } elsif (defined $imm_codes{$op}) {
971 if ($op eq 'seg') {
972 if ($last_imm lt 'i') {
973 die "$fname: $line: seg without an immediate operand\n";
975 } else {
976 $last_imm++;
977 if ($last_imm gt 'j') {
978 die "$fname: $line: too many immediate operands\n";
981 if (!defined($oppos{$last_imm})) {
982 die "$fname: $line: $op without '$last_imm' operand\n";
984 push(@codes, 05) if ($oppos{$last_imm} & 4);
985 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
986 $prefix_ok = 0;
987 } elsif ($op eq '/is4') {
988 if (!defined($oppos{'s'})) {
989 die "$fname: $line: $op without 's' operand\n";
991 if (defined($oppos{'i'})) {
992 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
993 } else {
994 push(@codes, 05) if ($oppos{'s'} & 4);
995 push(@codes, 0174+($oppos{'s'} & 3));
997 $prefix_ok = 0;
998 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
999 my $imm = $1;
1000 if (!defined($oppos{'s'})) {
1001 die "$fname: $line: $op without 's' operand\n";
1003 if ($imm < 0 || $imm > 15) {
1004 die "$fname: $line: invalid imm4 value for $op: $imm\n";
1006 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
1007 $prefix_ok = 0;
1008 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
1009 push(@codes, 0330, hex $1);
1010 $prefix_ok = 0;
1011 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
1012 if (!defined($oppos{'r'})) {
1013 die "$fname: $line: $op without 'r' operand\n";
1015 push(@codes, 05) if ($oppos{'r'} & 4);
1016 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
1017 $prefix_ok = 0;
1018 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1019 # Escape to enter literal bytecodes
1020 push(@codes, oct $1);
1021 } else {
1022 die "$fname: $line: unknown operation: $op\n";
1026 return @codes;