Debian Codegraph @

x86/insns.pl

raw
1 #!/usr/bin/perl
2 # SPDX-License-Identifier: BSD-2-Clause
3 # Copyright 1996-2025 The NASM Authors - All Rights Reserved
4
5 #
6 # insns.pl
7 #
8 # Parse insns.dat and produce generated source code files
9 #
10 # See x86/bytecode.txt for the defintion of the byte code
11 # output to x86/insnsb.c.
12 #
13
14 require 'x86/insns-iflags.ph';
15
16 # Create disassembly root tables
17 my @vex_class = ( 'novex', 'vex', 'xop', 'evex', 'rex2' );
18 my $vex_classes = scalar(@vex_class);
19 my @map_count = ( 4, 32, 24, 8, 2 );
20 my @map_start = ( 0, 0, 8, 0, 0 );
21 my @map_max;
22 for ($c = 0; $c < $vex_classes; $c++) {
23 push(@distable, [(undef) x $map_start[$c]]);
24 push(@map_max, $map_start[$c] + $map_count[$c] - 1);
25 for ($m = $map_start[$c]; $m <= $map_max[$c]; $m++) {
26 push(@{$distable[$c]}, {});
27 }
28 }
29 @disasm_prefixes = (@vexlist, @disasm_prefixes, '');
30 %disasm_prefixes = map { $_ => 1 } @disasm_prefixes;
31
32 @bytecode_count = (0) x 256;
33
34 # Push to an array reference, creating the array if needed
35 sub xpush($@) {
36 my $ref = shift @_;
37
38 $$ref = [] unless (defined($$ref));
39 return push(@$$ref, @_);
40 }
41
42 #
43 # Here we determine the set of possible [encoding, map, opcode] sets
44 # for a given instruction, used to generate the disassembler tables.
45 #
46 # It is only necessary to take into account the following byte codes:
47 # \[1234] mean literal bytes, of course
48 # \1[0123] mean byte plus register value
49 # \30[0123] 0F 1[8-F]
50 # \35[01] REX2 prefix
51 # \35[567] legacy map number
52 # \26x \270 VEX prefix and map
53 # \24x \250 EVEX prefix and map
54 # prefixes ignored
55 #
56 my @skip_bytecode = ((0) x 256);
57 map { $skip_bytecode[$_] = 1; }
58 (05...07, 014...017, 0271...0273, 0310...0337,
59 0341...0347, 0360...0372, 0374...0376);
60
61 # To assist with debugging...
62 my %skipped_due_to;
63
64 # Create combinatorial sets of possible encodings
65 sub genseqs($$$@) {
66 my($flags, $enc, $map, @opcodes) = @_;
67 my @encs = ($enc);
68
69 if ($enc == 0 && $map < 2 && $flags !~ /\bNO(?:APX|REX)\b/) {
70 push(@encs, 4); # Allow REX2 encoding
71 }
72
73 my @seqs = ();
74 foreach my $enc (@encs) {
75 push(@seqs, map { [$enc, $map, $_] } @opcodes);
76 }
77
78 return @seqs;
79 }
80
81 sub startseq($$) {
82 my ($codestr,$flags) = @_;
83 my @codes = ();
84 my $c = $codestr;
85 my($c0, $c1, $i);
86 my $prefix = '';
87 my $enc = 0; # Legacy
88 my $map = 0; # Map 0
89
90 @codes = decodify(undef, $codestr, {}, undef);
91
92 while (defined($c0 = shift(@codes))) {
93 $c1 = $codes[0]; # The immediate following code
94 if ($c0 >= 01 && $c0 <= 04) {
95 # Fixed byte string, this should be the opcode
96 return genseqs($flags, $enc, $map, $c1);
97 } elsif ($c0 >= 010 && $c0 <= 013) {
98 return genseqs($flags, $enc, $map, $c1...($c1+7));
99 } elsif (($c0 & ~013) == 0144) {
100 return genseqs($flags, $enc, $map, $c1, $c1|2);
101 } elsif (($c0 & ~3) == 0300) {
102 return genseqs($flags, $enc, $map, 0x18...0x1f);
103 } elsif ($c0 >= 0355 && $c0 <= 0357) {
104 $map = $c0 - 0354;
105 } elsif (($c0 & ~1) == 0350) {
106 # REX2
107 $enc = 4; # rex2 required
108 } elsif (($c0 & ~3) == 0260 || $c0 == 0270) {
109 # VEX/XOP
110 my($cm,$wlp);
111 $cm = shift(@codes);
112 $wlp = shift(@codes);
113 $enc = (($cm >> 6) & 1) + 1; # vex or xop
114 $map = $cm & 31;
115 } elsif (($c0 & ~3) == 0240 || $c0 == 0250) {
116 # EVEX
117 my @p;
118 push(@p, shift(@codes));
119 push(@p, shift(@codes));
120 push(@p, shift(@codes));
121 my $tuple = shift(@codes);
122 $map = $p[0] & 7;
123 $enc = 3; # evex
124 } elsif (!$skip_bytecode[$c0]) {
125 # This cannot be an opcode
126 $skipped_due_to{$c0}++;
127 last;
128 }
129 }
130 return ();
131 }
132
133 # Generate relaxed form patterns if applicable
134 # * is used for an optional source operand, duplicating the previous one
135 # in the encoding if it is missing.
136 # ? is used for an optional destination operand which is not encoded if
137 # missing; if combined with evex.ndx set the nd bit if present.
138 sub relaxed_forms(@) {
139 my @field_list = ();
140
141 foreach my $fields (@_) {
142 if ($fields->[1] !~ /([\*\?])/) {
143 push(@field_list, $fields);
144 next;
145 }
146 my $flag = $1;
147
148 # This instruction has relaxed form(s)
149 if ($fields->[2] !~ /^(\[\s*)([^:\s]+)(\s*:.*\])$/) {
150 warn "$fname:$line: has a $flag operand but uses raw bytecodes\n";
151 push(@field_list, $fields);
152 next;
153 }
154 # Subfields of the instruction encoding field;
155 # [1] is the encoding of the operands
156 my @f2o = ($1, $2, $3);
157
158 my $opmask = 0;
159 my $ndmask = 0;
160 my $ndflag = 0;
161 my @ops = split(/,/, $fields->[1]);
162 for (my $oi = 0; $oi < scalar @ops; $oi++) {
163 if ($ops[$oi] =~ /\*$/) {
164 if ($oi == 0) {
165 warn "$fname:$line: has a first operand with a *\n";
166 next;
167 }
168 $opmask |= 1 << $oi;
169 } elsif ($ops[$oi] =~ /\?$/) {
170 $opmask |= 1 << $oi;
171 $ndmask |= 1 << $oi;
172 $ndflag = 1;
173 }
174 }
175
176 # If .ndx is present, then change it to .nd0 or .nd1
177 # Set to .nd0 if no ndmask fields are present, otherwise 1
178 # (This line applies to the full form instruction; see below
179 # for the modified versions.)
180 my $has_ndx = ($f2o[2] =~ /(\.nd)x\b/);
181
182 my %flags = split_flags($fields->[3]);
183 my $has_zu = $flags{'ZU'};
184
185 for (my $oi = 0; $oi < (1 << scalar @ops); $oi++) {
186 if (($oi & ~$opmask) == 0) {
187 # First and ending variants. 0 means a ? operand is to
188 # be omitted entirely, a 1 that is is to be treated
189 # like the *next* operand (emitted with a relax annotation.)
190 # This is used to generate the {zu} forms of instrutions
191 # which support ND - the {zu} form is created as the ND form
192 # with the destination and first source operand the same.
193 my $fvar = !($has_ndx && ($oi & $ndmask));
194 my $evar = $fvar || ($has_ndx && !$has_zu);
195 for (my $var = $fvar; $var <= $evar; $var++) {
196 my @xops = ();
197 my $ndflag = 0;
198 my $ondflag = 0;
199 my $setzu = 0;
200 my $voi = $oi;
201 my $f2 = $f2o[0];
202 my $dropped = 0;
203 for (my $oj = 0; $oj < scalar(@ops); $oj++) {
204 my $ob = 1 << $oj;
205 my $emit = 0;
206 if ($ob & ~$voi) {
207 $emit = 1;
208 } elsif ($ob & $ndmask) {
209 die if (($ob << 1) & $ndmask);
210 if (!$var) {
211 # Make it disappear completely
212 $dropped |= $ob;
213 next;
214 } else {
215 # Treat the *next* operand as an omitted
216 # * operand
217 $emit = 1;
218 $voi |= $ob << 1;
219 }
220 }
221 if ($emit) {
222 push(@xops, $ops[$oj]);
223 if ($ndmask & $ob) {
224 $setzu |= $has_ndx;
225 $ondflag = 1;
226 }
227 } else {
228 $dropped |= $ob;
229 $f2 .= '+';
230 }
231 $f2 .= substr($f2o[1], $oj, 1);
232 }
233 $f2 .= $f2o[2];
234 my @ff = @$fields;
235 $ff[1] = join(',', @xops);
236 $f2 =~ s/(\.nd)x\b/$1$ondflag/ if ($has_ndx);
237 $ff[2] = $f2;
238 my %newfl = %flags;
239 $newfl{'ZU'}++ if ($setzu && !$has_zu);
240 $newfl{'ND'}++ if ($oi && $var);
241 # Change ARx and SMx if needed
242 foreach my $as ('AR', 'SM') {
243 my $ndelta = 0;
244 for (my $i = 0; $i < $MAX_OPERANDS; $i++) {
245 if ($dropped & (1 << $i)) {
246 delete $newfl{"$as$i"};
247 $ndelta++;
248 } elsif ($ndelta) {
249 my $newi = $i - $ndelta;
250 if ($newfl{"$as$i"}) {
251 delete $newfl{"$as$i"};
252 $newfl{"$as$newi"}++ if ($newi >= 0);
253 }
254 }
255 }
256 }
257
258 $ff[3] = merge_flags(\%newfl);
259
260 push(@field_list, [@ff]);
261 }
262 }
263 }
264 }
265
266 return @field_list;
267 }
268
269 # Condition codes.
270 my $c_ccmask = 0x0f;
271 my $c_nd = 0x10; # Not for the disassembler
272 my $c_cc = 0x20; # cc only (not scc)
273 my $c_scc = 0x40; # scc only (not cc)
274 my %conds = (
275 'o' => 0, 'no' => 1, 'c' => 2, 'nc' => 3,
276 'z' => 4, 'nz' => 5, 'na' => 6, 'a' => 7,
277 's' => 8, 'ns' => 9,
278 'pe' => 10|$c_cc, 'po' => 11|$c_cc,
279 't' => 10|$c_scc, 'f' => 11|$c_scc,
280 'l' => 12, 'nl' => 13, 'ng' => 14, 'g' => 15,
281
282 'ae' => 3|$c_nd, 'b' => 2|$c_nd, 'be' => 6|$c_nd, 'e' => 4|$c_nd,
283 'ge' => 13|$c_nd, 'le' => 14|$c_nd, 'nae' => 2|$c_nd, 'nb' => 3|$c_nd,
284 'nbe' => 7|$c_nd, 'ne' => 5|$c_nd, 'nge' => 12|$c_nd, 'nle' => 15|$c_nd,
285 'np' => 11|$c_nd|$c_cc, 'p' => 10|$c_nd|$c_cc,
286 '' => 11|$c_nd|$c_scc);
287
288 my @conds = sort keys(%conds);
289
290 # Generate conditional form patterns if applicable
291 sub conditional_forms(@) {
292 my @field_list = ();
293
294 foreach my $fields (@_) {
295 # This is a case sensitive match!
296 if ($fields->[0] !~ /s?cc/) {
297 # Not a conditional instruction pattern
298 push(@field_list, $fields);
299 next;
300 }
301
302 if ($fields->[2] !~ /^\[/) {
303 warn "$fname:$line: conditional instruction using raw bytecodes\n";
304 next;
305 }
306
307 my $exclude_mask = ($fields->[0] =~ /scc/ ? $c_cc : $c_scc);
308
309 foreach my $cc (@conds) {
310 my $ccval = $conds{$cc};
311 next if ($ccval & $exclude_mask);
312
313 my @ff = @$fields;
314
315 $ff[0] =~ s/s?cc/\U$cc/;
316
317 if ($ff[2] =~ /^(\[.*?)\b([0-9a-f]{2})\+c\b(.*\])$/) {
318 $ff[2] = $1.sprintf('%02x', hex($2)^($ccval & $c_ccmask)).$3;
319 } elsif ($ff[2] =~ /^(\[.*?\.scc\b)(.*\])$/) {
320 $ff[2] = $1.sprintf('%d', $ccval & $c_ccmask).$2;
321 } else {
322 my $eerr = $ff[2];
323 warn "$fname:$line: invalid conditional encoding: $eerr\n";
324 next;
325 }
326
327 if (($ccval & $c_nd) && !($ff[3] =~ /\bND\b/)) {
328 $ff[3] .= ',ND';
329 }
330
331 push(@field_list, [@ff]);
332 }
333 }
334 return @field_list;
335 }
336
337 # APX: EVEX promoted forms of VEX instructions
338 sub apx_evex_forms(@) {
339 my @field_list;
340
341 foreach my $fields (@_) {
342 my $doapx = 1;
343 my @ff = @$fields;
344
345 $doapx = 0 if ($ff[3] =~ /\bNOAPX\b/);
346 $doapx = 0 if ($ff[2] !~ /^(\[.*?)\b(vex\+)(\.[^\s]*)?(.*\])$/);
347
348 if (!$doapx) {
349 push(@field_list, $fields);
350 next;
351 }
352
353 my($head,$vexplus,$vexargs,$tail) = ($1,$2,$3,$4);
354
355 $doapx = $vexargs !~ /\.m([89|[1-9][0-9]+)\b/;
356
357 $ff[2] = $head.'vex'.$vexargs.$tail;
358 push(@field_list, [@ff]);
359 if ($doapx) {
360 $ff[2] = $head.'evex'.$vexargs.$tail;
361 $ff[3] .= ',APX';
362 push(@field_list, [@ff]);
363 }
364 }
365
366 return @field_list;
367 }
368
369 print STDERR "Reading insns.dat...\n";
370
371 @args = ();
372 undef $output;
373 foreach $arg ( @ARGV ) {
374 if ( $arg =~ /^\-/ ) {
375 if ( $arg =~ /^\-([abdin]|f[hc])$/ ) {
376 $output = $1;
377 } else {
378 die "$0: Unknown option: ${arg}\n";
379 }
380 } else {
381 push (@args, $arg);
382 }
383 }
384
385 die if (scalar(@args) != 2); # input output
386 ($fname, $oname) = @args;
387
388 open(F, '<', $fname) || die "unable to open $fname";
389
390 @bytecode_list = ();
391 %aname = ();
392
393 $line = 0;
394 $insns = 0;
395 $n_opcodes = 0;
396 my @allpatterns = ();
397
398 while (<F>) {
399 $line++;
400 chomp;
401 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
402
403 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
404 warn "$fname:$line: line does not contain four fields\n";
405 next;
406 }
407 my @field_list = ([$1, $2, $3, uc($4)]);
408 @field_list = relaxed_forms(@field_list);
409 @field_list = conditional_forms(@field_list);
410 @field_list = apx_evex_forms(@field_list);
411
412 foreach my $fields (@field_list) {
413 ($formatted, $nd) = format_insn(@$fields);
414 if ($formatted) {
415 $insns++;
416 xpush(\$aname{$fields->[0]}, [$formatted, $fields]);
417 }
418 if (!defined($k_opcodes{$fields->[0]})) {
419 $k_opcodes{$fields->[0]} = $n_opcodes++;
420 }
421 if ($formatted && !$nd) {
422 push(@big, [$formatted, $fields]);
423 my @sseq = startseq($fields->[2], $fields->[3]);
424 foreach my $i (@sseq) {
425 xpush(\$distable[$i->[0]][$i->[1]]{$i->[2]}, $#big);
426 }
427 }
428 }
429 }
430
431 close F;
432
433 #
434 # Generate the bytecode array. At this point, @bytecode_list contains
435 # the full set of bytecodes.
436 #
437
438 # Sort by descending length
439 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
440 @bytecode_array = ();
441 %bytecode_pos = ();
442 $bytecode_next = 0;
443 foreach $bl (@bytecode_list) {
444 my $h = hexstr(@$bl);
445 next if (defined($bytecode_pos{$h}));
446
447 push(@bytecode_array, $bl);
448 while ($h ne '') {
449 $bytecode_pos{$h} = $bytecode_next;
450 $h = substr($h, 2);
451 $bytecode_next++;
452 }
453 }
454 undef @bytecode_list;
455
456 @opcodes = sort { $k_opcodes{$a} <=> $k_opcodes{$b} } keys(%k_opcodes);
457
458 if ( $output eq 'b') {
459 print STDERR "Writing $oname...\n";
460
461 open(B, '>', $oname);
462
463 print B "/* This file auto-generated from insns.dat by insns.pl" .
464 " - don't edit it */\n\n";
465
466 print B "#include \"nasm.h\"\n";
467 print B "#include \"insns.h\"\n\n";
468
469 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
470
471 $p = 0;
472 foreach $bl (@bytecode_array) {
473 printf B " /* %5d */ ", $p;
474 foreach $d (@$bl) {
475 printf B "%#o,", $d;
476 $p++;
477 }
478 printf B "\n";
479 }
480 print B "};\n";
481
482 print B "\n";
483 print B "/*\n";
484 print B " * Bytecode frequencies (including reuse):\n";
485 print B " *\n";
486 for ($i = 0; $i < 32; $i++) {
487 print B " *";
488 for ($j = 0; $j < 256; $j += 32) {
489 print B " |" if ($j);
490 printf B " %3o:%5d", $i+$j, $bytecode_count[$i+$j];
491 }
492 print B "\n";
493 }
494 print B " */\n";
495
496 close B;
497 }
498
499 if ( $output eq 'a' ) {
500 print STDERR "Writing $oname...\n";
501
502 open(A, '>', $oname);
503
504 print A "/* This file auto-generated from insns.dat by insns.pl" .
505 " - don't edit it */\n\n";
506
507 print A "#include \"nasm.h\"\n";
508 print A "#include \"insns.h\"\n\n";
509
510 foreach my $i (@opcodes) {
511 my $pat = $aname{$i};
512 if (scalar(@$pat)) {
513 printf A "static const struct itemplate instrux_%s[%d] = {\n",
514 $i, scalar(@$pat);
515 my $e = '}';
516 my $n = 0;
517 foreach $j (@$pat) {
518 printf A " /* %3d : %s */\n", $n++, join(' ', @{$j->[1]});
519 print A ' /* ', show_bytecodes($j->[0]), ' : ', show_iflags($j->[0]), " */\n";
520 print A ' ', codesubst($j->[0]), "\n";
521 }
522 print A "};\n\n";
523 }
524 }
525 printf A "const struct itemplate_list nasm_instructions[%d] = {\n",
526 scalar(@opcodes);
527 foreach my $i (@opcodes) {
528 my $pat = $aname{$i};
529 my $npat = scalar(@$pat);
530 my $tbl = $npat ? "instrux_$i" : "NULL /* $i */";
531 printf A " { %3d, %s },\n", $npat, $tbl;
532 }
533 print A "};\n";
534
535 close A;
536 }
537
538 if ( $output eq 'd' ) {
539 print STDERR "Writing $oname...\n";
540
541 open(D, '>', $oname);
542
543 print D "/* This file auto-generated from insns.dat by insns.pl" .
544 " - don't edit it */\n\n";
545
546 my @skipped = sort { $a <=> $b } keys(%skipped_due_to);
547
548 if (scalar @skipped) {
549 print D "/*\n";
550 print D " * Note: skipped patterns due to byte codes:\n";
551 print D " *", map { sprintf(' 0%o', $_) } @skipped;
552 print D "\n */\n\n";
553 }
554
555 print D "#include \"nasm.h\"\n";
556 print D "#include \"insns.h\"\n\n";
557
558 print D "static const struct itemplate instrux[] = {\n";
559 $n = 0;
560 foreach $j (@big) {
561 printf D " /* %3d : %s */\n", $n++, join(' ', @{$j->[1]});
562 print D ' /* ', show_bytecodes($j->[0]), ' : ', show_iflags($j->[0]), " */\n";
563 print D ' ', codesubst($j->[0]), "\n";
564 }
565 print D "};\n";
566
567 my @dinstname;
568 for (my $c = 0; $c < $vex_classes; $c++) {
569 push(@dinstname, []);
570 for (my $m = $map_base[$c]; $m <= $map_max[$c]; $m++) {
571 my $ninst = scalar(keys %{$distable[$c][$m]});
572 if (!$ninst) {
573 push(@{$dinstname[$c]}, 'NULL');
574 } else {
575 my $tname = sprintf("itbl_%s_map%d", $vex_class[$c], $m);
576 push(@{$dinstname[$c]}, $tname);
577 my @itbls = ();
578 for (my $o = 0; $o < 256; $o++) {
579 my $tbl = $distable[$c][$m]{$o};
580 if (defined($tbl)) {
581 my $name = sprintf("%s_%02x", $tname, $o);
582 push(@itbls, $name);
583 printf D "\nstatic const struct itemplate * const %s[] = {\n", $name;
584 foreach my $j (@$tbl) {
585 print D " instrux + $j,\n";
586 }
587 print D " NULL\n};\n";
588 } else {
589 push(@itbls, 'NULL');
590 }
591 }
592
593 printf D "\nstatic const struct itemplate * const * const %s[256] = {\n", $tname;
594 print D map { " $_,\n" } @itbls;
595 print D "};\n";
596 }
597 }
598 }
599
600 print D "\nconst struct itemplate * const * const * const\n";
601 print D "ndisasm_itable[] = {\n";
602 for (my $c = 0; $c < $vex_classes; $c++) {
603 my $class = $vex_class[$c];
604 printf D " /* ---- %s ---- */\n", $class;
605 for (my $m = $map_start[$c]; $m <= $map_max[$c]; $m++) {
606 printf D " /* %2d */ %s,\n", $m, $dinstname[$c][$m];
607 }
608 }
609 print D "};\n";
610
611 close D;
612 }
613
614 if ( $output eq 'i' ) {
615 print STDERR "Writing $oname...\n";
616
617 open(I, '>', $oname);
618
619 print I "/* This file is auto-generated from insns.dat by insns.pl" .
620 " - don't edit it */\n\n";
621 print I "/* This file in included by nasm.h */\n\n";
622
623 print I "/* Instruction names */\n\n";
624 print I "#ifndef NASM_INSNSI_H\n";
625 print I "#define NASM_INSNSI_H 1\n\n";
626 print I "enum opcode {\n";
627 $maxlen = 0;
628 foreach $i (@opcodes) {
629 print I "\tI_${i},\n";
630 $len = length($i);
631 $maxlen = $len if ( $len > $maxlen );
632 }
633 print I "\tI_none = -1\n";
634 print I "};\n\n";
635 print I "#define MAX_INSLEN ", $maxlen, "\n";
636 print I "#define MAX_OPERANDS ", $MAX_OPERANDS, "\n";
637 print I "#define NASM_VEX_CLASSES ", $vex_classes, "\n";
638 my $mapcnt = 0;
639 for (my $c = 0; $c < $vex_classes; $c++) {
640 printf I "#define MAP_BASE_%s (%d-%d)\n",
641 uc($vex_class[$c]), $mapcnt, $map_start[$c];
642 $mapcnt += $map_count[$c];
643 }
644
645 print I "#define NO_DECORATOR\t{", join(',',(0) x $MAX_OPERANDS), "}\n";
646 print I "\n#endif /* NASM_INSNSI_H */\n";
647
648 close I;
649 }
650
651 if ( $output eq 'n' ) {
652 print STDERR "Writing $oname...\n";
653
654 open(N, '>', $oname);
655
656 print N "/* This file is auto-generated from insns.dat by insns.pl" .
657 " - don't edit it */\n\n";
658 print N "#include \"tables.h\"\n\n";
659
660 print N "const char * const nasm_insn_names[] = {";
661 foreach $i (@opcodes) {
662 print N "\n\t\"\L$i\"";
663 print N ',' if ($i < $#opcodes);
664 }
665 print N "\n};\n";
666 close N;
667 }
668
669 if ( $output eq 'fh') {
670 write_iflaggen_h();
671 }
672
673 if ( $output eq 'fc') {
674 write_iflag_c();
675 }
676
677 printf STDERR "Done: %d instructions\n", $insns;
678
679 # Count primary bytecodes, for statistics
680 sub count_bytecodes(@) {
681 my $skip = 0;
682 foreach my $bc (@_) {
683 if ($skip) {
684 $skip--;
685 next;
686 }
687 $bytecode_count[$bc]++;
688 if ($bc >= 01 && $bc <= 04) {
689 $skip = $bc;
690 } elsif (($bc & ~03) == 010) {
691 $skip = 1;
692 } elsif (($bc & ~013) == 0144) {
693 $skip = 1;
694 } elsif ($bc >= 0171 && $bc <= 0173) {
695 $skip = 1;
696 } elsif (($bc & ~3) == 0260 || $bc == 0270) { # VEX
697 $skip = 2;
698 } elsif (($bc & ~3) == 0240 || $bc == 0250) { # EVEX
699 $skip = 4;
700 } elsif (($bc & ~3) == 0304) {
701 $skip = 2;
702 } elsif ($bc == 0330) {
703 $skip = 1;
704 }
705 }
706 }
707
708 sub format_insn($$$$) {
709 my ($opcode, $operands, $codes, $flags) = @_;
710 my $nd = 0;
711 my ($num, $flagsindex);
712 my @bytecode;
713 my ($op, @ops, @opsize, $opp, @opx, @oppx, @decos, @opevex);
714 my $opinfo;
715
716 return (undef, undef) if $operands eq 'ignore';
717
718 # Remember if we have an ARx flag
719 my $arx = undef;
720
721 my %flags = split_flags($flags);
722 set_implied_flags(\%flags);
723
724 # Generate byte code. This may modify the flags.
725 @bytecode = (decodify($opcode, $codes, \%flags, \$opinfo), 0);
726 my($oppos, $openc) = @$opinfo;
727 push(@bytecode_list, [@bytecode]);
728 $codes = hexstr(@bytecode);
729 count_bytecodes(@bytecode);
730
731 my $nd = !!($flags{'ND'} || $flags{'PSEUDO'});
732 delete $flags{'ND'};
733
734 # format the operands
735 $operands =~ s/[\*\?]//g;
736 $operands =~ s/:/|colon,/g;
737 @ops = ();
738 @opsize = ();
739 @decos = ();
740 if ($operands ne 'void') {
741 foreach $op (split(/,/, $operands)) {
742 my $opnum = scalar(@ops);
743 my $isreg = 0;
744 my $ismem = 0;
745 my $ismoffs = 0;
746 my $isimm = 0;
747 my $isrm = 0;
748 my $iszero = 0;
749 my $opsz = 0;
750 @opx = ();
751 @opevex = ();
752 foreach $opp (split(/\|/, $op)) {
753 @oppx = ();
754 if ($opp =~ s/^(b(16|32|64)|mask|z|er|sae)$//) {
755 push(@opevex, $1);
756 }
757
758 $opp =~ s/^reg([0-9]*)na$/reg_na$1/;
759
760 if ($opp =~ s/([^0-9]0?)(8|16|32|64|80|128|256|512|1024|1k)$/$1/) {
761 push(@oppx, "bits$2");
762 $opsz = $1 + 0;
763 }
764 if ($opp =~ s/0$//) {
765 push(@oppx, 'rn_zero');
766 $iszero = 1;
767 if ($opp !~ /reg/) {
768 $opp .= 'reg';
769 }
770 }
771
772 $opp =~ s/^memory_offs$/mem_offs/;
773 $opp =~ s/^mem$/memory/;
774
775 if ($opp =~ s/^(spec|imm)4$/$1/) {
776 push(@oppx, 'fourbits');
777 $isimm = 1;
778 }
779 $opp =~ s/^spec$/immediate/; # Special or normal immediate
780 $opp =~ s/^imm$/imm_normal/; # Normal immediate only
781 if ($opp =~ /^(unity|sbyted?word|[su]dword)$/) {
782 push(@oppx, 'imm_normal');
783 $isimm = 1;
784 }
785 if ($opp =~ /^imm/) {
786 $isimm = 1;
787 }
788 $opp =~ s/^([a-z]+)rm$/rm_$1/;
789 $opp =~ s/^(rm|reg)$/$1_gpr/;
790 $opp =~ s/^rm_k$/rm_opmask/;
791 $opp =~ s/^kreg$/opmaskreg/;
792 if ($opp =~ /\brm_/) {
793 $isrm = 1;
794 } elsif ($opp =~ /(\breg_|reg\b)/) {
795 $isreg = 1;
796 } elsif ($opp =~ /\b[xyzt]?mem/) {
797 $ismem = 1;
798 }
799 if ($opp =~ /\bmem_offs/) {
800 $ismoffs = 1;
801 }
802 if ($opp =~ /\b[xyzt]mm/) {
803 $isvec = 1;
804 }
805 if (($isrm || ($ismem && !$ismoffs) || $isreg) &&
806 !(($flags{'EVEX'} && $isvec) || !$flags{'NOAPX'})) {
807 # Register numbers >= 16 disallowed
808 push(@oppx, 'rn_l16');
809 }
810 if ($isreg && $isvec && $openc->[$opnum] =~ /b/) {
811 $flags{'MOPVEC'}++;
812 }
813 push(@opx, $opp, @oppx) if $opp;
814 }
815
816 # Sanity-check the encoding of this operand
817 my $opvalid = '-';
818 if ($isreg) {
819 $opvalid .= 'rvmsbx';
820 } elsif ($isimm || $ismoffs) {
821 $opvalid .= 'ijnw';
822 } elsif ($ismem || $isrm) {
823 $opvalid .= 'm';
824 }
825
826 foreach my $c (split(//, $openc->[$opnum])) {
827 if (index($opvalid, $c) < 0) {
828 die "$fname:$line: $opcode: operand $opnum \"$op\": '$c' must be one of '$opvalid'\n";
829 }
830 }
831
832 $op = join('|',@opx);
833 push(@ops, $op);
834 push(@opsize, $opsz);
835 push(@decos, (@opevex ? join('|', @opevex) : '0'));
836 }
837 }
838
839 my $nops = scalar(@ops);
840
841 while (scalar(@ops) < $MAX_OPERANDS) {
842 push(@ops, '0');
843 push(@opsize, 0);
844 push(@decos, '0');
845 }
846 $operands = join(',', @ops);
847 $operands =~ tr/a-z/A-Z/;
848
849 $decorators = "{" . join(',', @decos) . "}";
850 if ($decorators =~ /^{(0,)+0}$/) {
851 $decorators = "NO_DECORATOR";
852 }
853 $decorators =~ tr/a-z/A-Z/;
854
855 # Tidy up the flags now then the operand count is known
856 set_implied_flags(\%flags, $nops);
857
858 # Look for SM flags clearly inconsistent with operand bitsizes
859 my $ssize = 0;
860 for (my $i = 0; $i < $nopr; $i++) {
861 next if (!$flags{"SM$i"} || !$opsize[$i]);
862 if (!$ssize) {
863 $ssize = $opsize[$i];
864 } elsif ($opsize[$i] != $ssize) {
865 die "$fname:$line: $opcode: inconsistent SM flag for argument $i\n";
866 }
867 }
868
869 # Look for ARx flags
870 my $arx = opr_flags(\%flags, 'AR');
871
872 # Look for Sx flags that can never match operand bitsizes. If the
873 # intent is to never match (require explicit sizes), use the SX flag.
874 # This doesn't apply to registers that pre-define specific sizes;
875 # this should really be derived from include/opflags.h...
876 my $fsize = size_flag(\%flags);
877 if ($fsize) {
878 for (my $i = 0; $i < $nops; $i++) {
879 next unless ($arx & (1 << $i));
880 if ($opsize[$i] && $ops[$i] !~ /(\breg_|reg\b)/ &&
881 $opsize[$i] != $fsize) {
882 die "$fname:$line: $opcode: inconsistent Sx flag for argument $i ($ops[$i])\n";
883 }
884 }
885 }
886
887 # Generate the final index into the flags table
888 $flagsindex = insns_flag_index(\%flags);
889 die "$fname:$line: $opcode: error in flags $flags\n" unless (defined($flagsindex));
890
891 return ("{I_$opcode, $nops, {$operands}, $decorators, \@\@CODES-$codes\@\@, $flagsindex},", $nd);
892 }
893
894 #
895 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
896 # offset into nasm_bytecodes
897 #
898 sub codesubst($) {
899 my($s) = @_;
900 my $n;
901
902 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
903 my $pos = $bytecode_pos{$1};
904 if (!defined($pos)) {
905 die "$fname:$line: no position assigned to byte code $1\n";
906 }
907 $s = $` . "nasm_bytecodes+${pos}" . "$'";
908 }
909 return $s;
910 }
911
912 #
913 # Extract byte codes in human-friendly form. Added as a comment
914 # to insnsa.c/insnsd.c to help debugging.
915 #
916 sub show_bytecodes($) {
917 my($s) = @_;
918 my @o = ();
919
920 if ($s =~ /\@\@CODES-([0-9A-F]+)00\@\@/) {
921 my $hstr = $1;
922 my $literals = 0;
923 my $hexlit = 0;
924 for (my $i = 0; $i < length($hstr); $i += 2) {
925 my $c = hex(substr($hstr,$i,2));
926 if ($literals) {
927 # Non-leading bytes
928 $literals--;
929 $o[-1] .= sprintf($hexlit ? '%02x' : '%03o', $c);
930 $o[-1] .= $literals ? ' ' : ')';
931 } else {
932 push(@o, sprintf("%03o", $c));
933 if ($c <= 4) {
934 $literals = $c;
935 $hexlit = 1;
936 } elsif (($c & ~3) == 0240 || $c == 0250) {
937 $literals = 3;
938 $hexlit = 1;
939 } elsif (($c & ~3) == 0260 || $c == 0270) {
940 $literals = 2;
941 $hexlit = 0;
942 } elsif ($c == 0171) {
943 $literals = 1;
944 $hexlit = 0;
945 } elsif ($c == 0172) {
946 $literals = 1;
947 $hexlit = 1;
948 }
949 $o[-1] .= '(' if ($literals);
950 }
951 }
952 return join(' ', @o);
953 } else {
954 return 'no bytecode';
955 }
956 }
957
958 # Get the iflags from an insnsa.c pattern.
959 # Added as a comment to help debugging.
960 sub show_iflags($) {
961 my($s) = @_;
962 return undef unless ($s =~ /([0-9]+)\},$/);
963 return get_iflags($1);
964 }
965
966 #
967 # Turn a code string into a sequence of bytes
968 #
969 sub decodify($$$$) {
970 # Although these are C-syntax strings, by convention they should have
971 # only octal escapes (for directives) and hexadecimal escapes
972 # (for verbatim bytes)
973 my($opcode, $codestr, $flags, $opinfo) = @_;
974 my @codes;
975
976 if ($codestr eq 'ignore') {
977 @codes = ();
978 } elsif ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
979 @codes = byte_code_compile($opcode, $1, $flags, $opinfo);
980 } else {
981 # This really shouldn't happen anymore...
982 warn "$fname:$line: raw bytecodes?!\n";
983
984 my $c = $codestr;
985
986 @codes = ();
987
988 while ($c ne '') {
989 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
990 push(@codes, hex $1);
991 $c = $2;
992 next;
993 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
994 push(@codes, oct $1);
995 $c = $2;
996 next;
997 } else {
998 die "$fname:$line: unknown code format in \"$codestr\"\n";
999 }
1000 }
1001 }
1002
1003 # Flags may have been updated
1004 set_implied_flags($flags);
1005 return @codes;
1006 }
1007
1008 # Turn a numeric list into a hex string
1009 sub hexstr(@) {
1010 my $s = '';
1011 my $c;
1012
1013 foreach $c (@_) {
1014 $s .= sprintf("%02X", $c);
1015 }
1016 return $s;
1017 }
1018
1019 # EVEX tuple types offset is 0300. e.g. 0301 is for full vector(fv).
1020 sub tupletype($) {
1021 my ($tuplestr) = @_;
1022 my %tuple_codes = (
1023 '' => 000,
1024 'fv' => 001,
1025 'hv' => 002,
1026 'fvm' => 003,
1027 't1s8' => 004,
1028 't1s16' => 005,
1029 't1s' => 006,
1030 't1f32' => 007,
1031 't1f64' => 010,
1032 't2' => 011,
1033 't4' => 012,
1034 't8' => 013,
1035 'hvm' => 014,
1036 'qvm' => 015,
1037 'ovm' => 016,
1038 'm128' => 017,
1039 'dup' => 020,
1040 );
1041
1042 if (defined $tuple_codes{$tuplestr}) {
1043 return 0300 + $tuple_codes{$tuplestr};
1044 } else {
1045 die "$fname:$line: undefined tuple type: $tuplestr\n";
1046 }
1047 }
1048
1049 #
1050 # This function takes a series of byte codes in a format which is more
1051 # typical of the Intel documentation, and encode it.
1052 #
1053 # The format looks like:
1054 #
1055 # [operands: opcodes]
1056 #
1057 # The operands word lists the order of the operands:
1058 #
1059 # r = register field in the modr/m
1060 # m = modr/m
1061 # v = VEX "v" field or DFV
1062 # i = immediate
1063 # s = register field of is4/imz2 field
1064 # - = implicit (unencoded) operand
1065 # x = indeX register of mib. 014..017 bytecodes are used.
1066 #
1067 # For an operand that should be filled into more than one field,
1068 # enter it as e.g. "r+v".
1069 #
1070 sub byte_code_compile($$$$) {
1071 my($opcode, $str, $flags, $opinfo) = @_;
1072 my $opr;
1073 my $opc;
1074 my @codes = ();
1075 my $litix = undef;
1076 my $i;
1077 my ($op, $oq);
1078 my $opex;
1079
1080 my %imm_codes = (
1081 'ib' => 020, # imm8
1082 'ib,u' => 024, # Unsigned imm8
1083 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
1084 'iw' => 030, # imm16
1085 'iw,s' => 030, # imm16 sign-extended: NOT IMPLEMENTED
1086 'iw,u' => 030, # imm16 zero-extended: NOT IMPLEMENTED
1087 'id' => 040, # imm32
1088 'id,s' => 0254, # imm32 sign-extended to opsize
1089 'id,u' => 0264, # imm32 zero-extended to opsize
1090 'iq' => 054, # imm64
1091 'iq,s' => 054, # for orthogonality
1092 'iq,u' => 054, # for orthogonality
1093 'iwd' => 034, # imm16 or imm32, depending on opsize
1094 'iwdq' => 044, # imm16/32/64, depending on addrsize
1095 'rel8' => 050,
1096 'rel16' => 060,
1097 'rel32' => 070,
1098 'rel' => 064, # 16 or 32 bit relative operand
1099 'seg' => 074, # 16-bit segment value
1100 'ibn' => 0300, # A valid HINT_NOP opcode
1101 );
1102 my %plain_codes = (
1103 'o8' => undef, # 8-bit operand size (for orthogonality)
1104 'o16' => 0320, # 16-bit operand size
1105 'o32' => 0321, # 32-bit operand size
1106 'odf' => 0322, # Operand size is default
1107 'o64' => 0324, # 64-bit operand size requiring REX.W
1108 'w1' => 0324,
1109 'a16' => 0310,
1110 'a32' => 0311,
1111 'adf' => 0312, # Address size is default (disassembly)
1112 'asz' => 0312, # Alias for adf, for macro convenience
1113 'asm' => 0312, # Alias for adf, for macro convenience
1114 'a64' => 0313,
1115 '!osp' => 0364,
1116 '!asp' => 0365,
1117 'osp' => 0366,
1118 'osz' => 0330, # Operand size depending on default
1119 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
1120 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
1121 'mustrep' => 0336,
1122 'mustrepne' => 0337,
1123 'rex.l' => 0334,
1124 'norexb' => 0314,
1125 'norexx' => 0315,
1126 'norexr' => 0316,
1127 'norexw' => 0317,
1128 'w0' => 0317,
1129 'repe' => 0335,
1130 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
1131 'nof3' => 0326, # No REP 0xF3 prefix permitted
1132 'norep' => 0331, # No REP prefix permitted
1133 'wait' => 0341, # Needs a wait prefix
1134 'osm' => 0342, # Operand size = mode (16/32/64)
1135 'rex.b' => 0344,
1136 'rex.x' => 0345,
1137 'rex.r' => 0346,
1138 'rex.w' => 0347,
1139 'resb' => 0340,
1140 'np' => 0360, # No prefix
1141 'jlen' => 0373, # Length of jump
1142 'hlexr' => 0271,
1143 'hlenl' => 0272,
1144 'hle' => 0273,
1145
1146 'optw' => 0336,
1147 'optd' => 0337,
1148
1149 # This instruction takes XMM VSIB
1150 'vsibx' => 0374,
1151 'vm32x' => 0374,
1152 'vm64x' => 0374,
1153
1154 # This instruction takes YMM VSIB
1155 'vsiby' => 0375,
1156 'vm32y' => 0375,
1157 'vm64y' => 0375,
1158
1159 # This instruction takes ZMM VSIB
1160 'vsibz' => 0376,
1161 'vm32z' => 0376,
1162 'vm64z' => 0376,
1163 );
1164
1165 unless ($str =~ /^(([^\s:]*)\:*([^\s:]*)\:|)\s*(.*\S)\s*$/) {
1166 die "$fname:$line: cannot parse: [$str]\n";
1167 }
1168 $opr = lc($2);
1169 $tuple = lc($3); # Tuple type for AVX512
1170 $opc = lc($4);
1171
1172 $op = 0;
1173 my $oppos = {};
1174 my $openc = [];
1175 if (defined($opinfo)) {
1176 $$opinfo = [$oppos, $openc];
1177 }
1178 for ($i = 0; $i < length($opr); $i++) {
1179 my $c = substr($opr,$i,1);
1180 if ($c eq '+') {
1181 die "$fname:$line: $opcode: invalid use of '+' in '$opr'\n"
1182 if ($op < 1);
1183 $op--;
1184 } elsif ($c =~ /^[rmnvwsijbx-]$/) {
1185 # n means an immediate which is encoded as a memory address,
1186 # but unlike a mem_offs it supports rel encoding on 64 bits.
1187 # w means an immediate to be encoded into the v register
1188 # position.
1189 (my $realc = $c) =~ tr/nw/mv/;
1190 $openc->[$op] = '' unless (defined($openc->[$op]));
1191 $openc->[$op] .= $c;
1192 if (defined($oppos->{$realc})) {
1193 my $what = ($c eq $realc) ? "'$c'" : "[${realc}${c}]";
1194 die "$fname:$line: $opcode: More than one $what operand in '$opr'\n";
1195 }
1196 $oppos->{$realc} = $op unless ($realc eq '-');
1197 $op++;
1198 } else {
1199 die "$fname:$line: $opcode: Unknown operand encoding '$c'\n";
1200 }
1201 }
1202
1203 if (defined($oppos->{'m'})) {
1204 if (defined($oppos->{'b'})) {
1205 die "$fname:$line: $opcode: [mn] operand mutually exclusive with 'b'\n";
1206 } elsif (defined($oppos->{'x'})) {
1207 # memory operand + x register operand requires MIB
1208 $flags->{'MIB'}++;
1209 }
1210 }
1211 if (defined($oppos->{'s'}) && defined($oppos->{'i'})) {
1212 die "$fname:$line: $opcode: 's' operand mutually exclusive with 'i'\n";
1213 }
1214 if (defined($oppos->{'j'}) && !defined($oppos->{'i'})) {
1215 die "$fname:$line: $opcode 'j' without 'i' operand\n";
1216 }
1217 $tup = tupletype($tuple);
1218
1219 # Opcode map. Map 0 is the default, but not explicitly defined yet.
1220 my $opmap = undef;
1221
1222 my $last_imm = 'h';
1223 my $prefix_ok = 1;
1224 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
1225 if (exists($plain_codes{$op})) {
1226 # Plain code
1227 my $pc = $plain_codes{$op};
1228 push(@codes, $pc) if (defined($pc));
1229 } elsif ($op =~ /^(o64)?(nw)$/) {
1230 push(@codes, $1 eq '' ? 0327 : 0323);
1231 $flags->{'NWSIZE'}++;
1232 } elsif ($prefix_ok && $op =~ /^(66|f2|f3)$/) {
1233 # 66/F2/F3 prefix used as an opcode extension
1234 if ($op eq '66') {
1235 push(@codes, 0361);
1236 } elsif ($op eq 'f2') {
1237 push(@codes, 0332);
1238 } else {
1239 push(@codes, 0333);
1240 }
1241 } elsif ($prefix_ok && $op =~ /^(0f|0f38|0f3a|m([0-3]))$/) {
1242 if ($2 ne '') {
1243 $opmap = $2 + 0;
1244 } elsif ($op eq '0f') {
1245 $opmap = 1;
1246 } elsif ($op eq '0f38') {
1247 $opmap = 2;
1248 } elsif ($op eq '0f3a') {
1249 $opmap = 3;
1250 }
1251 push(@codes, 0354 + $opmap) if ($opmap > 0);
1252 $prefix_ok = 0;
1253 } elsif ($op =~ /^(m[0-9]+|0f38|0f3a)$/) {
1254 if ($prefix_ok) {
1255 die "$fname:$line: $opcode: invalid legacy map: $m\n";
1256 } elsif (defined($opmap)) {
1257 die "$fname:$line: $opcode: multiple legacy map specifiers\n";
1258 } else {
1259 die "$fname:$line: $opcode: legacy map must precede opcodes\n";
1260 }
1261 } elsif ($op =~ /^[0-9a-f]{2}$/) {
1262 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
1263 $codes[$litix] < 4) {
1264 $codes[$litix]++;
1265 push(@codes, hex $op);
1266 } else {
1267 $litix = scalar(@codes);
1268 push(@codes, 01, hex $op);
1269 }
1270 $prefix_ok = 0;
1271 } elsif ($op eq '/r') {
1272 if (!defined($oppos->{'r'}) || !defined($oppos->{'m'})) {
1273 die "$fname:$line: $opcode: $op requires 'r' and [mn] operands\n";
1274 }
1275 $opex = (($oppos->{'m'} & 4) ? 06 : 0) |
1276 (($oppos->{'r'} & 4) ? 05 : 0);
1277 push(@codes, $opex) if ($opex);
1278 # if mib is composed with two separate operands - ICC style
1279 push(@codes, 014 + ($oppos->{'x'} & 3)) if (defined($oppos->{'x'}));
1280 push(@codes, 0100 + (($oppos->{'m'} & 3) << 3) + ($oppos->{'r'} & 3));
1281 $prefix_ok = 0;
1282 } elsif ($op =~ m:^/([0-7])$:) {
1283 if (!defined($oppos->{'m'})) {
1284 die "$fname:$line: $opcode: $op requires an [mn] operand\n";
1285 }
1286 push(@codes, 06) if ($oppos->{'m'} & 4);
1287 push(@codes, 0200 + (($oppos->{'m'} & 3) << 3) + $1);
1288 $prefix_ok = 0;
1289 } elsif ($op =~ m:^/([0-3]?)r([0-7])$:) {
1290 if (!defined($oppos->{'r'})) {
1291 die "$fname:$line: $opcode: $op requires an 'r' operand\n";
1292 }
1293 push(@codes, 05) if ($oppos->{'r'} & 4);
1294 push(@codes, 0171);
1295 push(@codes, (($1+0) << 6) + (($oppos->{'r'} & 3) << 3) + $2);
1296 $prefix_ok = 0;
1297 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
1298 my $vexname = $1;
1299 my $c = $vexname eq 'xop' ? 1 : 0;
1300 my ($m,$w,$l,$p) = (undef,undef,undef,undef);
1301 my $has_nds = 0;
1302 my @subops = split(/\./, $2);
1303 my $opsize = undef;
1304
1305 if (defined($opmap)) {
1306 warn "$fname:$line: $opcode: legacy prefix ignored with VEX\n";
1307 }
1308 foreach $oq (@subops) {
1309 if ($oq eq '') {
1310 next;
1311 } elsif ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
1312 $l = 0;
1313 } elsif ($oq eq '256' || $oq eq 'l1') {
1314 $l = 1;
1315 } elsif ($oq eq 'lig') {
1316 $l = 0 unless (defined($l));
1317 $flags->{'LIG'}++;
1318 } elsif ($oq eq 'w0') {
1319 $w = 0;
1320 } elsif ($oq eq 'w1') {
1321 $w = 1;
1322 } elsif ($oq eq 'wig') {
1323 $w = 0;
1324 $flags->{'WIG'}++;
1325 } elsif ($oq eq 'ww') {
1326 $flags->{'WW'}++;
1327 } elsif ($oq eq 'o8') {
1328 $p = 0 unless (defined($p)); # np
1329 if (!defined($w)) { # wig
1330 $w = 0;
1331 $flags->{'WIG'}++;
1332 }
1333 } elsif ($oq eq 'o16') {
1334 $p = 1 unless (defined($p)); # 66
1335 $w = 0 unless (defined($w)); # w0
1336 $opsize = 0320;
1337 } elsif ($oq eq 'o32') {
1338 $p = 0 unless (defined($p)); # np
1339 if (!defined($w)) {
1340 $w = 0 unless (defined($w)); # w0
1341 $flags->{'WW'}++;
1342 }
1343 $opsize = 0321;
1344 } elsif ($oq eq 'o64') {
1345 $p = 0 unless (defined($p)); # np
1346 $w = 1 unless (defined($w)); # w1
1347 $flags->{'WW'}++;
1348 $opsize = 0323 + $w;
1349 } elsif ($oq eq 'ko8') {
1350 $p = 1 unless (defined($p)); # 66
1351 $w = 0 unless (defined($w)); # w0
1352 } elsif ($oq eq 'ko16') {
1353 $p = 0 unless (defined($p)); # np
1354 $w = 0 unless (defined($w)); # w0
1355 # $opsize = 0320;
1356 } elsif ($oq eq 'ko32') {
1357 $p = 1 unless (defined($p)); # 66
1358 $w = 1 unless (defined($w)); # w1
1359 # $opsize = 0321;
1360 } elsif ($oq eq 'ko64') {
1361 $p = 0 unless (defined($p)); # 66
1362 $w = 1 unless (defined($w)); # w1
1363 # $opsize = 0323 + $w;
1364 } elsif ($oq eq 'np' || $oq eq 'p0') {
1365 $p = 0;
1366 } elsif ($oq eq '66' || $oq eq 'p1') {
1367 $p = 1;
1368 } elsif ($oq eq 'f3' || $oq eq 'p2') {
1369 $p = 2;
1370 } elsif ($oq eq 'f2' || $oq eq 'p3') {
1371 $p = 3;
1372 } elsif ($oq eq '0f') {
1373 $m = 1;
1374 } elsif ($oq eq '0f38') {
1375 $m = 2;
1376 } elsif ($oq eq '0f3a') {
1377 $m = 3;
1378 } elsif ($oq =~ /^(m|map)([0-9]+)$/) {
1379 $m = $2+0;
1380 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
1381 if (!defined($oppos->{'v'})) {
1382 die "$fname:$line: $opcode: $vexname.$oq without [vw] operand\n";
1383 }
1384 $has_nds = 1;
1385 } else {
1386 die "$fname:$line: $opcode: undefined modifier: $vexname.$oq\n";
1387 }
1388 }
1389 if (!defined($m) || !defined($l)) {
1390 die "$fname:$line: $opcode: missing fields in \U$vexname\E specification\n";
1391 }
1392
1393 if (!defined($w)) {
1394 $w = 0;
1395 $flags->{'WIG'}++;
1396 }
1397
1398 my $minmap = ($c == 1) ? 8 : 0; # 0-31 for VEX, 8-31 for XOP
1399 if ($m < $minmap || $m > 31) {
1400 die "$fname:$line: $opcode: Only maps ${minmap}-31 are valid for \U${vexname}\n";
1401 }
1402 push(@codes, 05) if ($oppos->{'v'} > 3);
1403 push(@codes, defined($oppos->{'v'}) ? 0260+$oppos->{'v'} : 0270,
1404 ($c << 6)+$m, ($w << 7)+($l << 2)+$p);
1405
1406 push(@codes, $opsize) if (defined($opsize));
1407
1408 $flags->{'VEX'}++;
1409 $flags->{'NOAPX'}++; # VEX doesn't support registers 16+
1410 $prefix_ok = 0;
1411 } elsif ($op =~ /^(evex)(|\..*)$/) {
1412 my $c = $vexmap{$1};
1413 my ($m,$w,$l,$p,$scc,$nf,$u,$ndd) = (undef,undef,undef,undef,undef,undef,undef,0,0);
1414 my ($nds,$nd,$dfv,$v) = (0, 0, 0, 0);
1415 my $opsize = undef;
1416 my @bad_op = ();
1417 my @subops = split(/\./, $2);
1418 if (defined($opmap)) {
1419 warn "$fname:$line: $opcode: legacy prefix ignored with EVEX\n";
1420 }
1421 foreach $oq (@subops) {
1422 if ($oq eq '') {
1423 next;
1424 } elsif ($oq =~ /^(128|ll?[0z])$/) {
1425 $l = 0;
1426 } elsif ($oq =~ /^(256|ll?1)$/) {
1427 $l = 1;
1428 } elsif ($oq =~ /^(512|ll?2)$/) {
1429 $l = 2;
1430 } elsif ($oq =~ /^(1024|1k|ll?3)$/) {
1431 # Not actually defined, but...
1432 $l = 3;
1433 } elsif ($oq eq 'lig') {
1434 $l = 0 unless (defined($l));
1435 $flags->{'LIG'}++;
1436 } elsif ($oq eq 'w0') {
1437 $w = 0;
1438 } elsif ($oq eq 'w1') {
1439 $w = 1;
1440 } elsif ($oq eq 'wig') {
1441 $w = 0;
1442 $flags->{'WIG'}++;
1443 } elsif ($oq eq 'ww') {
1444 $w = 0;
1445 $flags->{'WW'}++;
1446 } elsif ($oq eq 'np' || $oq eq 'p0') {
1447 $p = 0;
1448 } elsif ($oq eq '66' || $oq eq 'p1') {
1449 $p = 1;
1450 } elsif ($oq eq 'f3' || $oq eq 'p2') {
1451 $p = 2;
1452 } elsif ($oq eq 'f2' || $oq eq 'p3') {
1453 $p = 3;
1454 } elsif ($oq eq 'o8') {
1455 $p = 0 unless (defined($p)); # np
1456 if (!defined($w)) { # wig
1457 $w = 0;
1458 $flags->{'WIG'}++;
1459 }
1460 } elsif ($oq eq 'o16') {
1461 $p = 1 unless (defined($p)); # 66
1462 $w = 0 unless (defined($w)); # w0
1463 $opsize = 0320;
1464 } elsif ($oq eq 'o32') {
1465 $p = 0 unless (defined($p)); # np
1466 if (!defined($w)) {
1467 $w = 0 unless (defined($w)); # w0
1468 $flags->{'WW'}++;
1469 }
1470 $opsize = 0321;
1471 } elsif ($oq eq 'o64') {
1472 $p = 0 unless (defined($p)); # np
1473 $w = 1 unless (defined($w)); # w1
1474 $flags->{'WW'}++;
1475 $opsize = 0323 + $w;
1476 } elsif ($oq eq 'ko8') {
1477 $p = 1 unless (defined($p)); # 66
1478 $w = 0 unless (defined($w)); # w0
1479 } elsif ($oq eq 'ko16') {
1480 $p = 0 unless (defined($p)); # np
1481 $w = 0 unless (defined($w)); # w0
1482 # $opsize = 0320;
1483 } elsif ($oq eq 'ko32') {
1484 $p = 1 unless (defined($p)); # 66
1485 $w = 1 unless (defined($w)); # w1
1486 # $opsize = 0321;
1487 } elsif ($oq eq 'ko64') {
1488 $p = 0 unless (defined($p)); # 66
1489 $w = 1 unless (defined($w)); # w1
1490 # $opsize = 0323 + $w;
1491 } elsif ($oq eq 'ww') {
1492 $flags->{'WW'}++;
1493 } elsif ($oq eq '0f') {
1494 $m = 1;
1495 } elsif ($oq eq '0f38') {
1496 $m = 2;
1497 } elsif ($oq eq '0f3a') {
1498 $m = 3;
1499 } elsif ($oq =~ /^(m|map)([0-7])$/) {
1500 $m = $2+0;
1501 } elsif ($oq =~ /^scc([0-9]+)$/) {
1502 $scc = $1+0;
1503 $flags->{'SCC'}++;
1504 push(@bad_op, ['v', $oq]);
1505 } elsif ($oq eq 'u') {
1506 $u = 1;
1507 } elsif ($oq eq 'nf') {
1508 $flags->{'NF_E'}++;
1509 $nf = 0;
1510 } elsif ($oq =~ /^nf([01])$/) {
1511 $nf = $1 + 0;
1512 } elsif ($oq =~ /^v([0-9]+)$/) {
1513 $v = $1 + 0;
1514 push(@bad_op, ['v', $oq]);
1515 } elsif ($oq eq 'dfv') {
1516 $flags->{'DFV'}++;
1517 $dfv = 1;
1518 push(@bad_op, ['v', $oq]);
1519 } elsif ($oq =~ /^nd([01])$/) {
1520 $nd = $1 + 0;
1521 } elsif ($oq eq 'zu') {
1522 # Set .ND if {zu} prefix is present
1523 $flags->{'ZU_E'}++;
1524 } elsif ($oq =~ /^(nds|ndd|nd|dds)$/) {
1525 if (!defined($oppos->{'v'})) {
1526 die "$fname:$line: $opcode: evex.$oq without [vw] operand\n";
1527 }
1528 $nds = 1;
1529 $nd = $oq eq 'nd';
1530 } else {
1531 die "$fname:$line: $opcode: undefined modifier: evex.$oq\n";
1532 }
1533 }
1534 # Currently too many patterns miss .w or .p; it would be good
1535 # to figure out if they should be .[wp]0 or .[wp]ig, but
1536 # don't warn yet to keep the noise down
1537 if (!defined($m) || !defined($l)) {
1538 warn "$fname:$line: $opcode: missing fields in EVEX specification\n";
1539 }
1540 if ($m > 7) {
1541 die "$fname:$line: $opcode: Only maps 0-7 are valid for EVEX\n";
1542 }
1543 foreach my $bad (@bad_op) {
1544 my($what, $because) = @$inv;
1545 if (defined($oppos->{$what})) {
1546 die "$fname:$line: $opcode: $what and evex.$because are mutually incompatible\n";
1547 }
1548 }
1549 if ($scc && $nf) {
1550 die "$fname:$line: $opcode: evex.scc and evex.nf are mutually incompatible\n";
1551 }
1552
1553 my @p = ($m | 0xf0, $p | 0x7c, ($l << 5) | 0x08);
1554 $v ^= 0x0f if ($dfv);
1555 $v ^= 0x10 if (defined($scc));
1556 $p[1] ^= ($v & 15) << 3;
1557 $p[1] ^= $w << 7;
1558 $p[2] ^= ($v & 16) >> 1;
1559 $p[2] ^= $scc & 15;
1560 $p[2] |= 0x04 if ($nf);
1561 $p[2] |= 0x10 if ($nd);
1562
1563 push(@codes, 05) if ($oppos->{'v'} > 3);
1564 push(@codes, defined($oppos->{'v'}) ? 0240+$oppos->{'v'} : 0250, @p);
1565 push(@codes, $tup);
1566
1567 push(@codes, $opsize) if (defined($opsize));
1568
1569 $flags->{'EVEX'}++;
1570 $prefix_ok = 0;
1571 } elsif ($op =~ /^(rex2)(\..*)?$/) {
1572 my $name = $1;
1573 my @subops = split(/\./, $2);
1574 my $c = 0350;
1575 my $m = undef;
1576 foreach $oq (@subops) {
1577 if ($oq eq '') {
1578 next;
1579 } elsif ($oq eq 'w') {
1580 $c |= 01;
1581 } else {
1582 die "$fname:$line: $opcode: unknown modifier: $name.$oq\n";
1583 }
1584 }
1585
1586 push(@codes, $c);
1587 $flags->{'APX'}++;
1588 $flags->{'REX2'}++;
1589 $flags->{'LONG'}++;
1590 $prefix_ok = 0;
1591 } elsif (defined $imm_codes{$op}) {
1592 if ($op eq 'seg') {
1593 if ($last_imm lt 'i') {
1594 die "$fname:$line: $opcode: seg without an [ij] operand\n";
1595 }
1596 } else {
1597 $last_imm++;
1598 if ($last_imm gt 'j') {
1599 die "$fname:$line: $opcode: too many immediate operands\n";
1600 }
1601 }
1602 if (!defined($oppos->{$last_imm})) {
1603 die "$fname:$line: $opcode: $op without '$last_imm' operand\n";
1604 }
1605 push(@codes, 05) if ($oppos->{$last_imm} & 4);
1606 push(@codes, $imm_codes{$op} + ($oppos->{$last_imm} & 3));
1607 $prefix_ok = 0;
1608 } elsif ($op =~ /^ib(,[us])?[\^]([0-9a-f]+)$/) {
1609 my $type = $1 eq ',u' ? 2 : $1 eq ',s' ? 1 : 0;
1610 my $mod = hex $2;
1611 $last_imm++;
1612 if ($last_imm gt 'j') {
1613 die "$fname:$line: $opcode: too many immediate operands\n";
1614 }
1615 if (!defined($oppos->{$last_imm})) {
1616 die "$fname:$line: $opcode: $op without '$last_imm' operand\n";
1617 }
1618 push(@codes, 05) if ($oppos->{$last_imm} & 4);
1619 push(@codes, 0304 + ($oppos->{$last_imm} & 3));
1620 push(@codes, $type, $mod);
1621 $flags->{'ND'}++;
1622 } elsif ($op eq '/is4') {
1623 if (!defined($oppos->{'s'})) {
1624 die "$fname:$line: $opcode: $op without 's' operand\n";
1625 }
1626 if (defined($oppos->{'i'})) {
1627 push(@codes, 0172, ($oppos->{'s'} << 3)+$oppos->{'i'});
1628 } else {
1629 push(@codes, 05) if ($oppos->{'s'} & 4);
1630 push(@codes, 0174+($oppos->{'s'} & 3));
1631 }
1632 $prefix_ok = 0;
1633 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
1634 my $imm = $1;
1635 if (!defined($oppos->{'s'})) {
1636 die "$fname:$line: $opcode: $op without 's' operand\n";
1637 }
1638 if ($imm < 0 || $imm > 15) {
1639 die "$fname:$line: $opcode: invalid imm4 value for $op: $imm\n";
1640 }
1641 push(@codes, 0173, ($oppos->{'s'} << 4) + $imm);
1642 $prefix_ok = 0;
1643 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
1644 if (!defined($oppos->{'r'})) {
1645 die "$fname:$line: $opcode: $op without 'r' operand\n";
1646 }
1647 push(@codes, 05) if ($oppos->{'r'} & 4);
1648 push(@codes, 010 + ($oppos->{'r'} & 3), hex $1);
1649 $prefix_ok = 0;
1650 } elsif ($op =~ /^(jcc|jmp)8$/) {
1651 # A relaxable jump instruction
1652 push(@codes, $op eq 'jcc8' ? 0370 : 0371);
1653 $flags->{'JMP_RELAX'}++;
1654 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
1655 # Escape to enter literal bytecodes
1656 push(@codes, oct $1);
1657 } else {
1658 die "$fname:$line: $opcode: unknown operation: $op\n";
1659 }
1660 }
1661
1662 # Legacy maps 2+ do not support REX2 encodings
1663 if ($opmap > 1 && !$flags{'EVEX'}) {
1664 $flags->{'NOAPX'}++;
1665 }
1666
1667 return @codes;
1668 }