coff: Better handling of section redefinition
[nasm.git] / insns.pl
bloba63ee7127c31a2507e1fa1fae9f499543c238d31
1 #!/usr/bin/perl
2 ## --------------------------------------------------------------------------
3 ##
4 ## Copyright 1996-2012 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 # Opcode prefixes which need their own opcode tables
41 # LONGER PREFIXES FIRST!
42 @disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
44 # This should match MAX_OPERANDS from nasm.h
45 $MAX_OPERANDS = 5;
47 # Add VEX/XOP prefixes
48 @vex_class = ( 'vex', 'xop' );
49 $vex_classes = scalar(@vex_class);
50 @vexlist = ();
51 %vexmap = ();
52 for ($c = 0; $c < $vex_classes; $c++) {
53 $vexmap{$vex_class[$c]} = $c;
54 for ($m = 0; $m < 32; $m++) {
55 for ($p = 0; $p < 4; $p++) {
56 push(@vexlist, sprintf("%s%02X%01X", $vex_class[$c], $m, $p));
60 @disasm_prefixes = (@vexlist, @disasm_prefixes);
62 @bytecode_count = (0) x 256;
64 print STDERR "Reading insns.dat...\n";
66 @args = ();
67 undef $output;
68 foreach $arg ( @ARGV ) {
69 if ( $arg =~ /^\-/ ) {
70 if ( $arg =~ /^\-([abdin])$/ ) {
71 $output = $1;
72 } else {
73 die "$0: Unknown option: ${arg}\n";
75 } else {
76 push (@args, $arg);
80 $fname = "insns.dat" unless $fname = $args[0];
81 open (F, $fname) || die "unable to open $fname";
83 %dinstables = ();
84 @bytecode_list = ();
86 $line = 0;
87 $insns = 0;
88 while (<F>) {
89 $line++;
90 chomp;
91 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
93 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
94 warn "line $line does not contain four fields\n";
95 next;
97 @fields = ($1, $2, $3, $4);
98 @field_list = ([@fields, 0]);
100 if ($fields[1] =~ /\*/) {
101 # This instruction has relaxed form(s)
102 if ($fields[2] !~ /^\[/) {
103 warn "line $line has an * operand but uses raw bytecodes\n";
104 next;
107 $opmask = 0;
108 @ops = split(/,/, $fields[1]);
109 for ($oi = 0; $oi < scalar @ops; $oi++) {
110 if ($ops[$oi] =~ /\*$/) {
111 if ($oi == 0) {
112 warn "line $line has a first operand with a *\n";
113 next;
115 $opmask |= 1 << $oi;
119 for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
120 if (($oi & ~$opmask) == 0) {
121 my @xops = ();
122 my $omask = ~$oi;
123 for ($oj = 0; $oj < scalar(@ops); $oj++) {
124 if ($omask & 1) {
125 push(@xops, $ops[$oj]);
127 $omask >>= 1;
129 push(@field_list, [$fields[0], join(',', @xops),
130 $fields[2], $fields[3], $oi]);
135 foreach $fptr (@field_list) {
136 @fields = @$fptr;
137 ($formatted, $nd) = format_insn(@fields);
138 if ($formatted) {
139 $insns++;
140 $aname = "aa_$fields[0]";
141 push @$aname, $formatted;
143 if ( $fields[0] =~ /cc$/ ) {
144 # Conditional instruction
145 $k_opcodes_cc{$fields[0]}++;
146 } else {
147 # Unconditional instruction
148 $k_opcodes{$fields[0]}++;
150 if ($formatted && !$nd) {
151 push @big, $formatted;
152 my @sseq = startseq($fields[2], $fields[4]);
153 foreach $i (@sseq) {
154 if (!defined($dinstables{$i})) {
155 $dinstables{$i} = [];
157 push(@{$dinstables{$i}}, $#big);
163 close F;
166 # Generate the bytecode array. At this point, @bytecode_list contains
167 # the full set of bytecodes.
170 # Sort by descending length
171 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
172 @bytecode_array = ();
173 %bytecode_pos = ();
174 $bytecode_next = 0;
175 foreach $bl (@bytecode_list) {
176 my $h = hexstr(@$bl);
177 next if (defined($bytecode_pos{$h}));
179 push(@bytecode_array, $bl);
180 while ($h ne '') {
181 $bytecode_pos{$h} = $bytecode_next;
182 $h = substr($h, 2);
183 $bytecode_next++;
186 undef @bytecode_list;
188 @opcodes = sort keys(%k_opcodes);
189 @opcodes_cc = sort keys(%k_opcodes_cc);
191 if ( !defined($output) || $output eq 'b') {
192 print STDERR "Writing insnsb.c...\n";
194 open B, ">insnsb.c";
196 print B "/* This file auto-generated from insns.dat by insns.pl" .
197 " - don't edit it */\n\n";
199 print B "#include \"nasm.h\"\n";
200 print B "#include \"insns.h\"\n\n";
202 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
204 $p = 0;
205 foreach $bl (@bytecode_array) {
206 printf B " /* %5d */ ", $p;
207 foreach $d (@$bl) {
208 printf B "%#o,", $d;
209 $p++;
211 printf B "\n";
213 print B "};\n";
215 print B "\n";
216 print B "/*\n";
217 print B " * Bytecode frequencies (including reuse):\n";
218 print B " *\n";
219 for ($i = 0; $i < 32; $i++) {
220 print B " *";
221 for ($j = 0; $j < 256; $j += 32) {
222 print B " |" if ($j);
223 printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
225 print B "\n";
227 print B " */\n";
229 close B;
232 if ( !defined($output) || $output eq 'a' ) {
233 print STDERR "Writing insnsa.c...\n";
235 open A, ">insnsa.c";
237 print A "/* This file auto-generated from insns.dat by insns.pl" .
238 " - don't edit it */\n\n";
240 print A "#include \"nasm.h\"\n";
241 print A "#include \"insns.h\"\n\n";
243 foreach $i (@opcodes, @opcodes_cc) {
244 print A "static const struct itemplate instrux_${i}[] = {\n";
245 $aname = "aa_$i";
246 foreach $j (@$aname) {
247 print A " ", codesubst($j), "\n";
249 print A " ITEMPLATE_END\n};\n\n";
251 print A "const struct itemplate * const nasm_instructions[] = {\n";
252 foreach $i (@opcodes, @opcodes_cc) {
253 print A " instrux_${i},\n";
255 print A "};\n";
257 close A;
260 if ( !defined($output) || $output eq 'd' ) {
261 print STDERR "Writing insnsd.c...\n";
263 open D, ">insnsd.c";
265 print D "/* This file auto-generated from insns.dat by insns.pl" .
266 " - don't edit it */\n\n";
268 print D "#include \"nasm.h\"\n";
269 print D "#include \"insns.h\"\n\n";
271 print D "static const struct itemplate instrux[] = {\n";
272 $n = 0;
273 foreach $j (@big) {
274 printf D " /* %4d */ %s\n", $n++, codesubst($j);
276 print D "};\n";
278 foreach $h (sort(keys(%dinstables))) {
279 next if ($h eq ''); # Skip pseudo-instructions
280 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
281 foreach $j (@{$dinstables{$h}}) {
282 print D " instrux + $j,\n";
284 print D "};\n";
287 @prefix_list = ();
288 foreach $h (@disasm_prefixes, '') {
289 for ($c = 0; $c < 256; $c++) {
290 $nn = sprintf("%s%02X", $h, $c);
291 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
292 # At least one entry in this prefix table
293 push(@prefix_list, $h);
294 $is_prefix{$h} = 1;
295 last;
300 foreach $h (@prefix_list) {
301 print D "\n";
302 print D "static " unless ($h eq '');
303 print D "const struct disasm_index ";
304 print D ($h eq '') ? 'itable' : "itable_$h";
305 print D "[256] = {\n";
306 for ($c = 0; $c < 256; $c++) {
307 $nn = sprintf("%s%02X", $h, $c);
308 if ($is_prefix{$nn}) {
309 die "$fname: ambiguous decoding of $nn\n"
310 if (defined($dinstables{$nn}));
311 printf D " /* 0x%02x */ { itable_%s, -1 },\n", $c, $nn;
312 } elsif (defined($dinstables{$nn})) {
313 printf D " /* 0x%02x */ { itable_%s, %u },\n", $c,
314 $nn, scalar(@{$dinstables{$nn}});
315 } else {
316 printf D " /* 0x%02x */ { NULL, 0 },\n", $c;
319 print D "};\n";
322 printf D "\nconst struct disasm_index * const itable_vex[%d][32][4] =\n",
323 $vex_classes;
324 print D "{\n";
325 for ($c = 0; $c < $vex_classes; $c++) {
326 print D " {\n";
327 for ($m = 0; $m < 32; $m++) {
328 print D " { ";
329 for ($p = 0; $p < 4; $p++) {
330 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $p);
331 printf D "%-15s",
332 ($is_prefix{$vp} ? sprintf("itable_%s,", $vp) : 'NULL,');
334 print D "},\n";
336 print D " },\n";
338 print D "};\n";
340 close D;
343 if ( !defined($output) || $output eq 'i' ) {
344 print STDERR "Writing insnsi.h...\n";
346 open I, ">insnsi.h";
348 print I "/* This file is auto-generated from insns.dat by insns.pl" .
349 " - don't edit it */\n\n";
350 print I "/* This file in included by nasm.h */\n\n";
352 print I "/* Instruction names */\n\n";
353 print I "#ifndef NASM_INSNSI_H\n";
354 print I "#define NASM_INSNSI_H 1\n\n";
355 print I "enum opcode {\n";
356 $maxlen = 0;
357 foreach $i (@opcodes, @opcodes_cc) {
358 print I "\tI_${i},\n";
359 $len = length($i);
360 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
361 $maxlen = $len if ( $len > $maxlen );
363 print I "\tI_none = -1\n";
364 print I "};\n\n";
365 print I "#define MAX_INSLEN ", $maxlen, "\n";
366 print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
367 print I "#endif /* NASM_INSNSI_H */\n";
369 close I;
372 if ( !defined($output) || $output eq 'n' ) {
373 print STDERR "Writing insnsn.c...\n";
375 open N, ">insnsn.c";
377 print N "/* This file is auto-generated from insns.dat by insns.pl" .
378 " - don't edit it */\n\n";
379 print N "#include \"tables.h\"\n\n";
381 print N "const char * const nasm_insn_names[] = {";
382 $first = 1;
383 foreach $i (@opcodes, @opcodes_cc) {
384 print N "," if ( !$first );
385 $first = 0;
386 $ilower = $i;
387 $ilower =~ s/cc$//; # Remove conditional cc suffix
388 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
389 print N "\n\t\"${ilower}\"";
391 print N "\n};\n";
392 close N;
395 printf STDERR "Done: %d instructions\n", $insns;
397 # Count primary bytecodes, for statistics
398 sub count_bytecodes(@) {
399 my $skip = 0;
400 foreach my $bc (@_) {
401 if ($skip) {
402 $skip--;
403 next;
405 $bytecode_count[$bc]++;
406 if ($bc >= 01 && $bc <= 04) {
407 $skip = $bc;
408 } elsif (($bc & ~03) == 010) {
409 $skip = 1;
410 } elsif (($bc & ~013) == 0144) {
411 $skip = 1;
412 } elsif ($bc == 0172 || $bc == 0173) {
413 $skip = 1;
414 } elsif ($bc >= 0260 && $bc <= 0270) {
415 $skip = 2;
416 } elsif ($bc == 0330) {
417 $skip = 1;
422 sub format_insn($$$$$) {
423 my ($opcode, $operands, $codes, $flags, $relax) = @_;
424 my $num, $nd = 0;
425 my @bytecode;
426 my $op, @ops, $opp, @opx, @oppx;
428 return (undef, undef) if $operands eq "ignore";
430 # format the operands
431 $operands =~ s/\*//g;
432 $operands =~ s/:/|colon,/g;
433 @ops = ();
434 if ($operands ne 'void') {
435 foreach $op (split(/,/, $operands)) {
436 @opx = ();
437 foreach $opp (split(/\|/, $op)) {
438 @oppx = ();
439 if ($opp =~ s/(?<=\D)(8|16|32|64|80|128|256)$//) {
440 push(@oppx, "bits$1");
442 $opp =~ s/^mem$/memory/;
443 $opp =~ s/^memory_offs$/mem_offs/;
444 $opp =~ s/^imm$/immediate/;
445 $opp =~ s/^([a-z]+)rm$/rm_$1/;
446 $opp =~ s/^rm$/rm_gpr/;
447 $opp =~ s/^reg$/reg_gpr/;
448 push(@opx, $opp, @oppx);
450 $op = join('|', @opx);
451 push(@ops, $op);
455 $num = scalar(@ops);
456 while (scalar(@ops) < $MAX_OPERANDS) {
457 push(@ops, '0');
459 $operands = join(',', @ops);
460 $operands =~ tr/a-z/A-Z/;
462 # format the flags
463 $flags =~ s/,/|IF_/g;
464 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
465 $flags = "IF_" . $flags;
467 @bytecode = (decodify($codes, $relax), 0);
468 push(@bytecode_list, [@bytecode]);
469 $codes = hexstr(@bytecode);
470 count_bytecodes(@bytecode);
472 ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
476 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
477 # offset into nasm_bytecodes
479 sub codesubst($) {
480 my($s) = @_;
481 my $n;
483 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
484 my $pos = $bytecode_pos{$1};
485 if (!defined($pos)) {
486 die "$fname: no position assigned to byte code $1\n";
488 $s = $` . "nasm_bytecodes+${pos}" . "$'";
490 return $s;
493 sub addprefix ($@) {
494 my ($prefix, @list) = @_;
495 my $x;
496 my @l = ();
498 foreach $x (@list) {
499 push(@l, sprintf("%s%02X", $prefix, $x));
502 return @l;
506 # Turn a code string into a sequence of bytes
508 sub decodify($$) {
509 # Although these are C-syntax strings, by convention they should have
510 # only octal escapes (for directives) and hexadecimal escapes
511 # (for verbatim bytes)
512 my($codestr, $relax) = @_;
514 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
515 return byte_code_compile($1, $relax);
518 my $c = $codestr;
519 my @codes = ();
521 unless ($codestr eq 'ignore') {
522 while ($c ne '') {
523 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
524 push(@codes, hex $1);
525 $c = $2;
526 next;
527 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
528 push(@codes, oct $1);
529 $c = $2;
530 next;
531 } else {
532 die "$fname: unknown code format in \"$codestr\"\n";
537 return @codes;
540 # Turn a numeric list into a hex string
541 sub hexstr(@) {
542 my $s = '';
543 my $c;
545 foreach $c (@_) {
546 $s .= sprintf("%02X", $c);
548 return $s;
551 # Here we determine the range of possible starting bytes for a given
552 # instruction. We need only consider the codes:
553 # \[1234] mean literal bytes, of course
554 # \1[0123] mean byte plus register value
555 # \330 means byte plus condition code
556 # \0 or \340 mean give up and return empty set
557 # \34[4567] mean PUSH/POP of segment registers: special case
558 # \17[234] skip is4 control byte
559 # \26x \270 skip VEX control bytes
560 sub startseq($$) {
561 my ($codestr, $relax) = @_;
562 my $word, @range;
563 my @codes = ();
564 my $c = $codestr;
565 my $c0, $c1, $i;
566 my $prefix = '';
568 @codes = decodify($codestr, $relax);
570 while ($c0 = shift(@codes)) {
571 $c1 = $codes[0];
572 if ($c0 >= 01 && $c0 <= 04) {
573 # Fixed byte string
574 my $fbs = $prefix;
575 while (1) {
576 if ($c0 >= 01 && $c0 <= 04) {
577 while ($c0--) {
578 $fbs .= sprintf("%02X", shift(@codes));
580 } else {
581 last;
583 $c0 = shift(@codes);
586 foreach $pfx (@disasm_prefixes) {
587 if (substr($fbs, 0, length($pfx)) eq $pfx) {
588 $prefix = $pfx;
589 $fbs = substr($fbs, length($pfx));
590 last;
594 if ($fbs ne '') {
595 return ($prefix.substr($fbs,0,2));
598 unshift(@codes, $c0);
599 } elsif ($c0 >= 010 && $c0 <= 013) {
600 return addprefix($prefix, $c1..($c1+7));
601 } elsif (($c0 & ~013) == 0144) {
602 return addprefix($prefix, $c1, $c1|2);
603 } elsif ($c0 == 0330) {
604 return addprefix($prefix, $c1..($c1+15));
605 } elsif ($c0 == 0 || $c0 == 0340) {
606 return $prefix;
607 } elsif (($c0 & ~3) == 0260 || $c0 == 0270) {
608 my $c,$m,$wlp;
609 $m = shift(@codes);
610 $wlp = shift(@codes);
611 $c = ($m >> 6);
612 $m = $m & 31;
613 $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 3);
614 } elsif ($c0 >= 0172 && $c0 <= 173) {
615 shift(@codes); # Skip is4 control byte
616 } else {
617 # We really need to be able to distinguish "forbidden"
618 # and "ignorable" codes here
621 return $prefix;
625 # This function takes a series of byte codes in a format which is more
626 # typical of the Intel documentation, and encode it.
628 # The format looks like:
630 # [operands: opcodes]
632 # The operands word lists the order of the operands:
634 # r = register field in the modr/m
635 # m = modr/m
636 # v = VEX "v" field
637 # i = immediate
638 # s = register field of is4/imz2 field
639 # - = implicit (unencoded) operand
641 # For an operand that should be filled into more than one field,
642 # enter it as e.g. "r+v".
644 sub byte_code_compile($$) {
645 my($str, $relax) = @_;
646 my $opr;
647 my $opc;
648 my @codes = ();
649 my $litix = undef;
650 my %oppos = ();
651 my $i;
652 my $op, $oq;
653 my $opex;
655 my %imm_codes = (
656 'ib' => 020, # imm8
657 'ib,u' => 024, # Unsigned imm8
658 'iw' => 030, # imm16
659 'ib,s' => 0274, # imm8 sign-extended to opsize or bits
660 'iwd' => 034, # imm16 or imm32, depending on opsize
661 'id' => 040, # imm32
662 'id,s' => 0254, # imm32 sign-extended to 64 bits
663 'iwdq' => 044, # imm16/32/64, depending on addrsize
664 'rel8' => 050,
665 'iq' => 054,
666 'rel16' => 060,
667 'rel' => 064, # 16 or 32 bit relative operand
668 'rel32' => 070,
669 'seg' => 074,
671 my %plain_codes = (
672 'o16' => 0320, # 16-bit operand size
673 'o32' => 0321, # 32-bit operand size
674 'odf' => 0322, # Operand size is default
675 'o64' => 0324, # 64-bit operand size requiring REX.W
676 'o64nw' => 0323, # Implied 64-bit operand size (no REX.W)
677 'a16' => 0310,
678 'a32' => 0311,
679 'adf' => 0312, # Address size is default
680 'a64' => 0313,
681 '!osp' => 0364,
682 '!asp' => 0365,
683 'f2i' => 0332, # F2 prefix, but 66 for operand size is OK
684 'f3i' => 0333, # F3 prefix, but 66 for operand size is OK
685 'mustrep' => 0336,
686 'mustrepne' => 0337,
687 'rex.l' => 0334,
688 'norexb' => 0314,
689 'norexx' => 0315,
690 'norexr' => 0316,
691 'norexw' => 0317,
692 'repe' => 0335,
693 'nohi' => 0325, # Use spl/bpl/sil/dil even without REX
694 'nof3' => 0326, # No REP 0xF3 prefix permitted
695 'norep' => 0331, # No REP prefix permitted
696 'wait' => 0341, # Needs a wait prefix
697 'resb' => 0340,
698 'jcc8' => 0370, # Match only if Jcc possible with single byte
699 'jmp8' => 0371, # Match only if JMP possible with single byte
700 'jlen' => 0373, # Length of jump
701 'hlexr' => 0271,
702 'hlenl' => 0272,
703 'hle' => 0273,
705 # This instruction takes XMM VSIB
706 'vsibx' => 0374,
707 'vm32x' => 0374,
708 'vm64x' => 0374,
710 # This instruction takes YMM VSIB
711 'vsiby' => 0375,
712 'vm32y' => 0375,
713 'vm64y' => 0375
716 unless ($str =~ /^(([^\s:]*)\:|)\s*(.*\S)\s*$/) {
717 die "$fname: $line: cannot parse: [$str]\n";
719 $opr = "\L$2";
720 $opc = "\L$3";
722 my $op = 0;
723 for ($i = 0; $i < length($opr); $i++) {
724 my $c = substr($opr,$i,1);
725 if ($c eq '+') {
726 $op--;
727 } else {
728 if ($relax & 1) {
729 $op--;
731 $relax >>= 1;
732 $oppos{$c} = $op++;
736 my $last_imm = 'h';
737 my $prefix_ok = 1;
738 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
739 my $pc = $plain_codes{$op};
741 if (defined $pc) {
742 # Plain code
743 push(@codes, $pc);
744 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
745 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
746 if ($op eq '66') {
747 push(@codes, 0361);
748 } elsif ($op eq 'f2') {
749 push(@codes, 0332);
750 } elsif ($op eq 'f3') {
751 push(@codes, 0333);
752 } else {
753 push(@codes, 0360);
755 } elsif ($op =~ /^[0-9a-f]{2}$/) {
756 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
757 $codes[$litix] < 4) {
758 $codes[$litix]++;
759 push(@codes, hex $op);
760 } else {
761 $litix = scalar(@codes);
762 push(@codes, 01, hex $op);
764 $prefix_ok = 0;
765 } elsif ($op eq '/r') {
766 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
767 die "$fname: $line: $op requires r and m operands\n";
769 $opex = (($oppos{'m'} & 4) ? 06 : 0) |
770 (($oppos{'r'} & 4) ? 05 : 0);
771 push(@codes, $opex) if ($opex);
772 push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
773 $prefix_ok = 0;
774 } elsif ($op =~ m:^/([0-7])$:) {
775 if (!defined($oppos{'m'})) {
776 die "$fname: $line: $op requires m operand\n";
778 push(@codes, 06) if ($oppos{'m'} & 4);
779 push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
780 $prefix_ok = 0;
781 } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
782 my $c = $vexmap{$1};
783 my ($m,$w,$l,$p) = (undef,2,undef,0);
784 my $has_nds = 0;
785 my @subops = split(/\./, $op);
786 shift @subops; # Drop prefix
787 foreach $oq (@subops) {
788 if ($oq eq '128' || $oq eq 'l0' || $oq eq 'lz') {
789 $l = 0;
790 } elsif ($oq eq '256' || $oq eq 'l1') {
791 $l = 1;
792 } elsif ($oq eq 'lig') {
793 $l = 2;
794 } elsif ($oq eq 'w0') {
795 $w = 0;
796 } elsif ($oq eq 'w1') {
797 $w = 1;
798 } elsif ($oq eq 'wig') {
799 $w = 2;
800 } elsif ($oq eq 'ww') {
801 $w = 3;
802 } elsif ($oq eq 'p0') {
803 $p = 0;
804 } elsif ($oq eq '66' || $oq eq 'p1') {
805 $p = 1;
806 } elsif ($oq eq 'f3' || $oq eq 'p2') {
807 $p = 2;
808 } elsif ($oq eq 'f2' || $oq eq 'p3') {
809 $p = 3;
810 } elsif ($oq eq '0f') {
811 $m = 1;
812 } elsif ($oq eq '0f38') {
813 $m = 2;
814 } elsif ($oq eq '0f3a') {
815 $m = 3;
816 } elsif ($oq =~ /^m([0-9]+)$/) {
817 $m = $1+0;
818 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
819 if (!defined($oppos{'v'})) {
820 die "$fname: $line: vex.$oq without 'v' operand\n";
822 $has_nds = 1;
823 } else {
824 die "$fname: $line: undefined VEX subcode: $oq\n";
827 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
828 die "$fname: $line: missing fields in VEX specification\n";
830 if (defined($oppos{'v'}) && !$has_nds) {
831 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
833 push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
834 ($c << 6)+$m, ($w << 4)+($l << 2)+$p);
835 $prefix_ok = 0;
836 } elsif (defined $imm_codes{$op}) {
837 if ($op eq 'seg') {
838 if ($last_imm lt 'i') {
839 die "$fname: $line: seg without an immediate operand\n";
841 } else {
842 $last_imm++;
843 if ($last_imm gt 'j') {
844 die "$fname: $line: too many immediate operands\n";
847 if (!defined($oppos{$last_imm})) {
848 die "$fname: $line: $op without '$last_imm' operand\n";
850 push(@codes, 05) if ($oppos{$last_imm} & 4);
851 push(@codes, $imm_codes{$op} + ($oppos{$last_imm} & 3));
852 $prefix_ok = 0;
853 } elsif ($op eq '/is4') {
854 if (!defined($oppos{'s'})) {
855 die "$fname: $line: $op without 's' operand\n";
857 if (defined($oppos{'i'})) {
858 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
859 } else {
860 push(@codes, 05) if ($oppos{'s'} & 4);
861 push(@codes, 0174+($oppos{'s'} & 3));
863 $prefix_ok = 0;
864 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
865 my $imm = $1;
866 if (!defined($oppos{'s'})) {
867 die "$fname: $line: $op without 's' operand\n";
869 if ($imm < 0 || $imm > 15) {
870 die "$fname: $line: invalid imm4 value for $op: $imm\n";
872 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
873 $prefix_ok = 0;
874 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
875 push(@codes, 0330, hex $1);
876 $prefix_ok = 0;
877 } elsif ($op =~ /^([0-9a-f]{2})\+r$/) {
878 if (!defined($oppos{'r'})) {
879 die "$fname: $line: $op without 'r' operand\n";
881 push(@codes, 05) if ($oppos{'r'} & 4);
882 push(@codes, 010 + ($oppos{'r'} & 3), hex $1);
883 $prefix_ok = 0;
884 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
885 # Escape to enter literal bytecodes
886 push(@codes, oct $1);
887 } else {
888 die "$fname: $line: unknown operation: $op\n";
892 return @codes;