insns.pl: allow relaxed forms to be created without duplication
[platform/upstream/nasm.git] / insns.pl
1 #!/usr/bin/perl
2 #
3 # insns.pl   produce insnsa.c, insnsd.c, insnsi.h, insnsn.c from insns.dat
4 #
5 # The Netwide Assembler is copyright (C) 1996 Simon Tatham and
6 # Julian Hall. All rights reserved. The software is
7 # redistributable under the license given in the file "LICENSE"
8 # distributed in the NASM archive.
9
10 # Opcode prefixes which need their own opcode tables
11 # LONGER PREFIXES FIRST!
12 @disasm_prefixes = qw(0F24 0F25 0F38 0F3A 0F7A 0FA6 0FA7 0F);
13
14 # This should match MAX_OPERANDS from nasm.h
15 $MAX_OPERANDS = 5;
16
17 # Add VEX/XOP prefixes
18 @vex_class = ( 'vex', 'xop' );
19 $vex_classes = scalar(@vex_class);
20 @vexlist = ();
21 %vexmap = ();
22 for ($c = 0; $c < $vex_classes; $c++) {
23     $vexmap{$vex_class[$c]} = $c;
24     for ($m = 0; $m < 32; $m++) {
25         for ($lp = 0; $lp < 8; $lp++) {
26             push(@vexlist, sprintf("%s%02X%01X", $vex_class[$c], $m, $lp));
27         }
28     }
29 }
30 @disasm_prefixes = (@vexlist, @disasm_prefixes);
31
32 @bytecode_count = (0) x 256;
33
34 print STDERR "Reading insns.dat...\n";
35
36 @args   = ();
37 undef $output;
38 foreach $arg ( @ARGV ) {
39     if ( $arg =~ /^\-/ ) {
40         if  ( $arg =~ /^\-([abdin])$/ ) {
41             $output = $1;
42         } else {
43             die "$0: Unknown option: ${arg}\n";
44         }
45     } else {
46         push (@args, $arg);
47     }
48 }
49
50 $fname = "insns.dat" unless $fname = $args[0];
51 open (F, $fname) || die "unable to open $fname";
52
53 %dinstables = ();
54 @bytecode_list = ();
55
56 $line = 0;
57 $insns = 0;
58 while (<F>) {
59   $line++;
60   chomp;
61   next if ( /^\s*(\;.*|)$/ );   # comments or blank lines
62
63   unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
64       warn "line $line does not contain four fields\n";
65       next;
66   }
67   @fields = ($1, $2, $3, $4);
68   @field_list = ([@fields, 0]);
69
70   if ($fields[1] =~ /\*/) {
71       # This instruction has relaxed form(s)
72       if ($fields[2] !~ /^\[/) {
73           warn "line $line has an * operand but uses raw bytecodes\n";
74           next;
75       }
76       
77       $opmask = 0;
78       @ops = split(/,/, $fields[1]);
79       for ($oi = 0; $oi < scalar @ops; $oi++) {
80           if ($ops[$oi] =~ /\*$/) {
81               if ($oi == 0) {
82                   warn "line $line has a first operand with a *\n";
83                   next;
84               }
85               $opmask |= 1 << $oi;
86           }
87       }
88
89       for ($oi = 1; $oi < (1 << scalar @ops); $oi++) {
90           if (($oi & ~$opmask) == 0) {
91               my @xops = ();
92               my $omask = ~$oi;
93               for ($oj = 0; $oj < scalar(@ops); $oj++) {
94                   if ($omask & 1) {
95                       push(@xops, $ops[$oj]);
96                   }
97                   $omask >>= 1;
98               }
99               push(@field_list, [$fields[0], join(',', @xops),
100                                  $fields[2], $fields[3], $oi]);
101           }
102       }
103   }
104
105   foreach $fptr (@field_list) {
106       @fields = @$fptr;
107       ($formatted, $nd) = format_insn(@fields);
108       if ($formatted) {
109           $insns++;
110           $aname = "aa_$fields[0]";
111           push @$aname, $formatted;
112       }
113       if ( $fields[0] =~ /cc$/ ) {
114           # Conditional instruction
115           $k_opcodes_cc{$fields[0]}++;
116       } else {
117           # Unconditional instruction
118           $k_opcodes{$fields[0]}++;
119       }
120       if ($formatted && !$nd) {
121           push @big, $formatted;
122           my @sseq = startseq($fields[2], $fields[4]);
123           foreach $i (@sseq) {
124               if (!defined($dinstables{$i})) {
125                   $dinstables{$i} = [];
126               }
127               push(@{$dinstables{$i}}, $#big);
128           }
129       }
130   }
131 }
132
133 close F;
134
135 #
136 # Generate the bytecode array.  At this point, @bytecode_list contains
137 # the full set of bytecodes.
138 #
139
140 # Sort by descending length
141 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
142 @bytecode_array = ();
143 %bytecode_pos = ();
144 $bytecode_next = 0;
145 foreach $bl (@bytecode_list) {
146     my $h = hexstr(@$bl);
147     next if (defined($bytecode_pos{$h}));
148
149     push(@bytecode_array, $bl);
150     while ($h ne '') {
151         $bytecode_pos{$h} = $bytecode_next;
152         $h = substr($h, 2);
153         $bytecode_next++;
154     }
155 }
156 undef @bytecode_list;
157
158 @opcodes    = sort keys(%k_opcodes);
159 @opcodes_cc = sort keys(%k_opcodes_cc);
160
161 if ( !defined($output) || $output eq 'b') {
162     print STDERR "Writing insnsb.c...\n";
163
164     open B, ">insnsb.c";
165
166     print B "/* This file auto-generated from insns.dat by insns.pl" .
167         " - don't edit it */\n\n";
168
169     print B "#include \"nasm.h\"\n";
170     print B "#include \"insns.h\"\n\n";
171
172     print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
173
174     $p = 0;
175     foreach $bl (@bytecode_array) {
176         printf B "    /* %5d */ ", $p;
177         foreach $d (@$bl) {
178             printf B "%#o,", $d;
179             $p++;
180         }
181         printf B "\n";
182     }
183     print B "};\n";
184
185     print B "\n";
186     print B "/*\n";
187     print B " * Bytecode frequencies (including reuse):\n";
188     print B " *\n";
189     for ($i = 0; $i < 32; $i++) {
190         print B " *";
191         for ($j = 0; $j < 256; $j += 32) {
192             print B " |" if ($j);
193             printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
194         }
195         print B "\n";
196     }
197     print B " */\n";
198
199     close B;
200 }
201
202 if ( !defined($output) || $output eq 'a' ) {
203     print STDERR "Writing insnsa.c...\n";
204
205     open A, ">insnsa.c";
206
207     print A "/* This file auto-generated from insns.dat by insns.pl" .
208         " - don't edit it */\n\n";
209
210     print A "#include \"nasm.h\"\n";
211     print A "#include \"insns.h\"\n\n";
212
213     foreach $i (@opcodes, @opcodes_cc) {
214         print A "static const struct itemplate instrux_${i}[] = {\n";
215         $aname = "aa_$i";
216         foreach $j (@$aname) {
217             print A "    ", codesubst($j), "\n";
218         }
219         print A "    ITEMPLATE_END\n};\n\n";
220     }
221     print A "const struct itemplate * const nasm_instructions[] = {\n";
222     foreach $i (@opcodes, @opcodes_cc) {
223         print A "    instrux_${i},\n";
224     }
225     print A "};\n";
226
227     close A;
228 }
229
230 if ( !defined($output) || $output eq 'd' ) {
231     print STDERR "Writing insnsd.c...\n";
232
233     open D, ">insnsd.c";
234
235     print D "/* This file auto-generated from insns.dat by insns.pl" .
236         " - don't edit it */\n\n";
237
238     print D "#include \"nasm.h\"\n";
239     print D "#include \"insns.h\"\n\n";
240
241     print D "static const struct itemplate instrux[] = {\n";
242     $n = 0;
243     foreach $j (@big) {
244         printf D "    /* %4d */ %s\n", $n++, codesubst($j);
245     }
246     print D "};\n";
247
248     foreach $h (sort(keys(%dinstables))) {
249         next if ($h eq '');     # Skip pseudo-instructions
250         print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
251         foreach $j (@{$dinstables{$h}}) {
252             print D "    instrux + $j,\n";
253         }
254         print D "};\n";
255     }
256
257     @prefix_list = ();
258     foreach $h (@disasm_prefixes, '') {
259         for ($c = 0; $c < 256; $c++) {
260             $nn = sprintf("%s%02X", $h, $c);
261             if ($is_prefix{$nn} || defined($dinstables{$nn})) {
262                 # At least one entry in this prefix table
263                 push(@prefix_list, $h);
264                 $is_prefix{$h} = 1;
265                 last;
266             }
267         }
268     }
269
270     foreach $h (@prefix_list) {
271         print D "\n";
272         print D "static " unless ($h eq '');
273         print D "const struct disasm_index ";
274         print D ($h eq '') ? 'itable' : "itable_$h";
275         print D "[256] = {\n";
276         for ($c = 0; $c < 256; $c++) {
277             $nn = sprintf("%s%02X", $h, $c);
278             if ($is_prefix{$nn}) {
279                 die "$fname: ambiguous decoding of $nn\n"
280                     if (defined($dinstables{$nn}));
281                 printf D "    { itable_%s, -1 },\n", $nn;
282             } elsif (defined($dinstables{$nn})) {
283                 printf D "    { itable_%s, %u },\n",
284                 $nn, scalar(@{$dinstables{$nn}});
285             } else {
286                 printf D "    { NULL, 0 },\n";
287             }
288         }
289         print D "};\n";
290     }
291
292     printf D "\nconst struct disasm_index * const itable_vex[%d][32][8] =\n",
293         $vex_classes;
294     print D "{\n";
295     for ($c = 0; $c < $vex_classes; $c++) {
296         print D "    {\n";
297         for ($m = 0; $m < 32; $m++) {
298             print D "        {\n";
299             for ($lp = 0; $lp < 8; $lp++) {
300                 $vp = sprintf("%s%02X%01X", $vex_class[$c], $m, $lp);
301                 if ($is_prefix{$vp}) {
302                     printf D "            itable_%s,\n", $vp;
303                 } else {
304                     print  D "            NULL,\n";
305                 }
306             }
307             print D "        },\n";
308         }
309         print D "    },\n";
310     }
311     print D "};\n";
312
313     close D;
314 }
315
316 if ( !defined($output) || $output eq 'i' ) {
317     print STDERR "Writing insnsi.h...\n";
318
319     open I, ">insnsi.h";
320
321     print I "/* This file is auto-generated from insns.dat by insns.pl" .
322         " - don't edit it */\n\n";
323     print I "/* This file in included by nasm.h */\n\n";
324
325     print I "/* Instruction names */\n\n";
326     print I "#ifndef NASM_INSNSI_H\n";
327     print I "#define NASM_INSNSI_H 1\n\n";
328     print I "enum opcode {\n";
329     $maxlen = 0;
330     foreach $i (@opcodes, @opcodes_cc) {
331         print I "\tI_${i},\n";
332         $len = length($i);
333         $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
334         $maxlen = $len if ( $len > $maxlen );
335     }
336     print I "\tI_none = -1\n";
337     print I "\n};\n\n";
338     print I "#define MAX_INSLEN ", $maxlen, "\n";
339     print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
340     print I "#endif /* NASM_INSNSI_H */\n";
341
342     close I;
343 }
344
345 if ( !defined($output) || $output eq 'n' ) {
346     print STDERR "Writing insnsn.c...\n";
347
348     open N, ">insnsn.c";
349
350     print N "/* This file is auto-generated from insns.dat by insns.pl" .
351         " - don't edit it */\n\n";
352     print N "#include \"tables.h\"\n\n";
353
354     print N "const char * const nasm_insn_names[] = {";
355     $first = 1;
356     foreach $i (@opcodes, @opcodes_cc) {
357         print N "," if ( !$first );
358         $first = 0;
359         $ilower = $i;
360         $ilower =~ s/cc$//;     # Remove conditional cc suffix
361         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
362         print N "\n\t\"${ilower}\"";
363     }
364     print N "\n};\n";
365     close N;
366 }
367
368 printf STDERR "Done: %d instructions\n", $insns;
369
370 # Count primary bytecodes, for statistics
371 sub count_bytecodes(@) {
372     my $skip = 0;
373     foreach my $bc (@_) {
374         if ($skip) {
375             $skip--;
376             next;
377         }
378         $bytecode_count[$bc]++;
379         if ($bc >= 01 && $bc <= 04) {
380             $skip = $bc;
381         } elsif (($bc & ~03) == 010) {
382             $skip = 1;
383         } elsif (($bc & ~013) == 0144) {
384             $skip = 1;
385         } elsif ($bc == 0172) {
386             $skip = 1;
387         } elsif ($bc >= 0260 && $bc <= 0270) {
388             $skip = 2;
389         } elsif ($bc == 0330) {
390             $skip = 1;
391         }
392     }
393 }
394
395 sub format_insn($$$$$) {
396     my ($opcode, $operands, $codes, $flags, $relax) = @_;
397     my $num, $nd = 0;
398     my @bytecode;
399
400     return (undef, undef) if $operands eq "ignore";
401
402     # format the operands
403     $operands =~ s/\*//g;
404     $operands =~ s/:/|colon,/g;
405     $operands =~ s/mem(\d+)/mem|bits$1/g;
406     $operands =~ s/mem/memory/g;
407     $operands =~ s/memory_offs/mem_offs/g;
408     $operands =~ s/imm(\d+)/imm|bits$1/g;
409     $operands =~ s/imm/immediate/g;
410     $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
411     $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
412     $operands =~ s/\=([0-9]+)/same_as|$1/g;
413     if ($operands eq 'void') {
414         @ops = ();
415     } else {
416         @ops = split(/\,/, $operands);
417     }
418     $num = scalar(@ops);
419     while (scalar(@ops) < $MAX_OPERANDS) {
420         push(@ops, '0');
421     }
422     $operands = join(',', @ops);
423     $operands =~ tr/a-z/A-Z/;
424
425     # format the flags
426     $flags =~ s/,/|IF_/g;
427     $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
428     $flags = "IF_" . $flags;
429
430     @bytecode = (decodify($codes, $relax), 0);
431     push(@bytecode_list, [@bytecode]);
432     $codes = hexstr(@bytecode);
433     count_bytecodes(@bytecode);
434
435     ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
436 }
437
438 #
439 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
440 # offset into nasm_bytecodes
441 #
442 sub codesubst($) {
443     my($s) = @_;
444     my $n;
445
446     while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
447         my $pos = $bytecode_pos{$1};
448         if (!defined($pos)) {
449             die "$fname: no position assigned to byte code $1\n";
450         }
451         $s = $` . "nasm_bytecodes+${pos}" . "$'";
452     }
453     return $s;
454 }
455
456 sub addprefix ($@) {
457     my ($prefix, @list) = @_;
458     my $x;
459     my @l = ();
460
461     foreach $x (@list) {
462         push(@l, sprintf("%s%02X", $prefix, $x));
463     }
464
465     return @l;
466 }
467
468 #
469 # Turn a code string into a sequence of bytes
470 #
471 sub decodify($$) {
472   # Although these are C-syntax strings, by convention they should have
473   # only octal escapes (for directives) and hexadecimal escapes
474   # (for verbatim bytes)
475     my($codestr, $relax) = @_;
476
477     if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
478         return byte_code_compile($1, $relax);
479     }
480
481     my $c = $codestr;
482     my @codes = ();
483
484     while ($c ne '') {
485         if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
486             push(@codes, hex $1);
487             $c = $2;
488             next;
489         } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
490             push(@codes, oct $1);
491             $c = $2;
492             next;
493         } else {
494             die "$fname: unknown code format in \"$codestr\"\n";
495         }
496     }
497
498     return @codes;
499 }
500
501 # Turn a numeric list into a hex string
502 sub hexstr(@) {
503     my $s = '';
504     my $c;
505
506     foreach $c (@_) {
507         $s .= sprintf("%02X", $c);
508     }
509     return $s;
510 }
511
512 # Here we determine the range of possible starting bytes for a given
513 # instruction. We need only consider the codes:
514 # \[1234]      mean literal bytes, of course
515 # \1[0123]     mean byte plus register value
516 # \330         means byte plus condition code
517 # \0 or \340   mean give up and return empty set
518 # \34[4567]    mean PUSH/POP of segment registers: special case
519 # \17[234]     skip is4 control byte
520 # \26x \270    skip VEX control bytes
521 sub startseq($$) {
522   my ($codestr, $relax) = @_;
523   my $word, @range;
524   my @codes = ();
525   my $c = $codestr;
526   my $c0, $c1, $i;
527   my $prefix = '';
528
529   @codes = decodify($codestr, $relax);
530
531   while ($c0 = shift(@codes)) {
532       $c1 = $codes[0];
533       if ($c0 >= 01 && $c0 <= 04) {
534           # Fixed byte string
535           my $fbs = $prefix;
536           while (1) {
537               if ($c0 >= 01 && $c0 <= 04) {
538                   while ($c0--) {
539                       $fbs .= sprintf("%02X", shift(@codes));
540                   }
541               } else {
542                   last;
543               }
544               $c0 = shift(@codes);
545           }
546
547           foreach $pfx (@disasm_prefixes) {
548               if (substr($fbs, 0, length($pfx)) eq $pfx) {
549                   $prefix = $pfx;
550                   $fbs = substr($fbs, length($pfx));
551                   last;
552               }
553           }
554
555           if ($fbs ne '') {
556               return ($prefix.substr($fbs,0,2));
557           }
558
559           unshift(@codes, $c0);
560       } elsif ($c0 >= 010 && $c0 <= 013) {
561           return addprefix($prefix, $c1..($c1+7));
562       } elsif (($c0 & ~013) == 0144) {
563           return addprefix($prefix, $c1, $c1|2);
564       } elsif ($c0 == 0330) {
565           return addprefix($prefix, $c1..($c1+15));
566       } elsif ($c0 == 0 || $c0 == 0340) {
567           return $prefix;
568       } elsif ($c0 == 0344) {
569           return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
570       } elsif ($c0 == 0345) {
571           return addprefix($prefix, 0x07, 0x17, 0x1F);
572       } elsif ($c0 == 0346) {
573           return addprefix($prefix, 0xA0, 0xA8);
574       } elsif ($c0 == 0347) {
575           return addprefix($prefix, 0xA1, 0xA9);
576       } elsif (($c0 & ~3) == 0260 || $c0 == 0270) {
577           my $c,$m,$wlp;
578           $m   = shift(@codes);
579           $wlp = shift(@codes);
580           $c = ($m >> 6);
581           $m = $m & 31;
582           $prefix .= sprintf('%s%02X%01X', $vex_class[$c], $m, $wlp & 7);
583       } elsif ($c0 >= 0172 && $c0 <= 174) {
584           shift(@codes);        # Skip is4 control byte
585       } else {
586           # We really need to be able to distinguish "forbidden"
587           # and "ignorable" codes here
588       }
589   }
590   return $prefix;
591 }
592
593 #
594 # This function takes a series of byte codes in a format which is more
595 # typical of the Intel documentation, and encode it.
596 #
597 # The format looks like:
598 #
599 # [operands: opcodes]
600 #
601 # The operands word lists the order of the operands:
602 #
603 # r = register field in the modr/m
604 # m = modr/m
605 # v = VEX "v" field
606 # d = DREX "dst" field
607 # i = immediate
608 # s = register field of is4/imz2 field
609 # - = implicit (unencoded) operand
610 #
611 # For an operand that should be filled into more than one field,
612 # enter it as e.g. "r+v".
613 #
614 sub byte_code_compile($$) {
615     my($str, $relax) = @_;
616     my $opr;
617     my $opc;
618     my @codes = ();
619     my $litix = undef;
620     my %oppos = ();
621     my $i;
622     my $op, $oq;
623     my $opex;
624
625     unless ($str =~ /^(([^\s:]*)\:|)\s*(.*\S)\s*$/) {
626         die "$fname: $line: cannot parse: [$str]\n";
627     }
628     $opr = "\L$2";
629     $opc = "\L$3";
630
631     my $op = 0;
632     for ($i = 0; $i < length($opr); $i++) {
633         my $c = substr($opr,$i,1);
634         if ($c eq '+') {
635             $op--;
636         } else {
637             if ($relax & 1) {
638                 $op--;
639             }
640             $relax >>= 1;
641             $oppos{$c} = $op++;
642         }
643     }
644
645     $prefix_ok = 1;
646     foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
647         if ($op eq 'o16') {
648             push(@codes, 0320);
649         } elsif ($op eq 'o32') {
650             push(@codes, 0321);
651         } elsif ($op eq 'o64') {  # 64-bit operand size requiring REX.W
652             push(@codes, 0324);
653         } elsif ($op eq 'o64nw') { # Implied 64-bit operand size (no REX.W)
654             push(@codes, 0323);
655         } elsif ($op eq 'a16') {
656             push(@codes, 0310);
657         } elsif ($op eq 'a32') {
658             push(@codes, 0311);
659         } elsif ($op eq 'a64') {
660             push(@codes, 0313);
661         } elsif ($op eq '!osp') {
662             push(@codes, 0364);
663         } elsif ($op eq '!asp') {
664             push(@codes, 0365);
665         } elsif ($op eq 'rex.l') {
666             push(@codes, 0334);
667         } elsif ($op eq 'repe') {
668             push(@codes, 0335);
669         } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
670             # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
671             if ($op eq '66') {
672                 push(@codes, 0361);
673             } elsif ($op eq 'f2') {
674                 push(@codes, 0362);
675             } elsif ($op eq 'f3') {
676                 push(@codes, 0363);
677             } else {
678                 push(@codes, 0360);
679             }
680         } elsif ($op =~ /^[0-9a-f]{2}$/) {
681             if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
682                 $codes[$litix] < 4) {
683                 $codes[$litix]++;
684                 push(@codes, hex $op);
685             } else {
686                 $litix = scalar(@codes);
687                 push(@codes, 01, hex $op);
688             }
689             $prefix_ok = 0;
690         } elsif ($op eq '/r') {
691             if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
692                 die "$fname: $line: $op requires r and m operands\n";
693             }
694             $opex = (($oppos{'m'} & 4) ? 06 : 0) |
695                     (($oppos{'r'} & 4) ? 05 : 0);
696             push(@codes, $opex) if ($opex);
697             push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
698             $prefix_ok = 0;
699         } elsif ($op =~ m:^/([0-7])$:) {
700             if (!defined($oppos{'m'})) {
701                 die "$fname: $line: $op requires m operand\n";
702             }
703             push(@codes, 06) if ($oppos{'m'} & 4);
704             push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
705             $prefix_ok = 0;
706         } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
707             my $c = $vexmap{$1};
708             my ($m,$w,$l,$p) = (undef,2,undef,0);
709             my $has_nds = 0;
710             my @subops = split(/\./, $op);
711             shift @subops;      # Drop prefix
712             foreach $oq (@subops) {
713                 if ($oq eq '128' || $oq eq 'l0') {
714                     $l = 0;
715                 } elsif ($oq eq '256' || $oq eq 'l1') {
716                     $l = 1;
717                 } elsif ($oq eq 'w0') {
718                     $w = 0;
719                 } elsif ($oq eq 'w1') {
720                     $w = 1;
721                 } elsif ($oq eq 'wx') {
722                     $w = 2;
723                 } elsif ($oq eq 'ww') {
724                     $w = 3;
725                 } elsif ($oq eq 'p0') {
726                     $p = 0;
727                 } elsif ($oq eq '66' || $oq eq 'p1') {
728                     $p = 1;
729                 } elsif ($oq eq 'f3' || $oq eq 'p2') {
730                     $p = 2;
731                 } elsif ($oq eq 'f2' || $oq eq 'p3') {
732                     $p = 3;
733                 } elsif ($oq eq '0f') {
734                     $m = 1;
735                 } elsif ($oq eq '0f38') {
736                     $m = 2;
737                 } elsif ($oq eq '0f3a') {
738                     $m = 3;
739                 } elsif ($oq =~ /^m([0-9]+)$/) {
740                     $m = $1+0;
741                 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
742                     if (!defined($oppos{'v'})) {
743                         die "$fname: $line: vex.$oq without 'v' operand\n";
744                     }
745                     $has_nds = 1;
746                 } else {
747                     die "$fname: $line: undefined VEX subcode: $oq\n";
748                 }
749             }
750             if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
751                 die "$fname: $line: missing fields in VEX specification\n";
752             }
753             if (defined($oppos{'v'}) && !$has_nds) {
754                 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
755             }
756             push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
757                  ($c << 6)+$m, ($w << 3)+($l << 2)+$p);
758             $prefix_ok = 0;
759         } elsif ($op =~ /^\/drex([01])$/) {
760             my $oc0 = $1;
761             if (!defined($oppos{'d'})) {
762                 die "$fname: $line: DREX without a 'd' operand\n";
763             }
764             # Note the use of *unshift* here, as opposed to *push*.
765             # This is because NASM want this byte code at the start of
766             # the instruction sequence, but the AMD documentation puts
767             # this at (roughly) the position of the drex byte itself.
768             # This allows us to match the AMD documentation and still
769             # do the right thing.
770             unshift(@codes, 0160+($oppos{'d'} & 3)+($oc0 ? 4 : 0));
771             unshift(@codes, 05) if ($oppos{'d'} & 4);
772         } elsif ($op =~ /^(ib\,s|ib|ibx|ib\,w|iw|iwd|id|idx|iwdq|rel|rel8|rel16|rel32|iq|seg|ibw|ibd|ibd,s)$/) {
773             if (!defined($oppos{'i'})) {
774                 die "$fname: $line: $op without 'i' operand\n";
775             }
776             if ($op eq 'ib,s') { # Signed imm8
777                 push(@codes, 05) if ($oppos{'i'} & 4);
778                 push(@codes, 014+($oppos{'i'} & 3));
779             } elsif ($op eq 'ib') { # imm8
780                 push(@codes, 05) if ($oppos{'i'} & 4);
781                 push(@codes, 020+($oppos{'i'} & 3));
782             } elsif ($op eq 'ib,u') { # Unsigned imm8
783                 push(@codes, 05) if ($oppos{'i'} & 4);
784                 push(@codes, 024+($oppos{'i'} & 3));
785             } elsif ($op eq 'iw') { # imm16
786                 push(@codes, 05) if ($oppos{'i'} & 4);
787                 push(@codes, 030+($oppos{'i'} & 3));
788             } elsif ($op eq 'ibx') { # imm8 sign-extended to opsize
789                 push(@codes, 05) if ($oppos{'i'} & 4);
790                 push(@codes, 0274+($oppos{'i'} & 3));
791             } elsif ($op eq 'iwd') { # imm16 or imm32, depending on opsize
792                 push(@codes, 05) if ($oppos{'i'} & 4);
793                 push(@codes, 034+($oppos{'i'} & 3));
794             } elsif ($op eq 'id') { # imm32
795                 push(@codes, 05) if ($oppos{'i'} & 4);
796                 push(@codes, 040+($oppos{'i'} & 3));
797             } elsif ($op eq 'idx') { # imm32 extended to 64 bits
798                 push(@codes, 05) if ($oppos{'i'} & 4);
799                 push(@codes, 0254+($oppos{'i'} & 3));
800             } elsif ($op eq 'iwdq') { # imm16/32/64, depending on opsize
801                 push(@codes, 05) if ($oppos{'i'} & 4);
802                 push(@codes, 044+($oppos{'i'} & 3));
803             } elsif ($op eq 'rel8') {
804                 push(@codes, 05) if ($oppos{'i'} & 4);
805                 push(@codes, 050+($oppos{'i'} & 3));
806             } elsif ($op eq 'iq') {
807                 push(@codes, 05) if ($oppos{'i'} & 4);
808                 push(@codes, 054+($oppos{'i'} & 3));
809             } elsif ($op eq 'rel16') {
810                 push(@codes, 05) if ($oppos{'i'} & 4);
811                 push(@codes, 060+($oppos{'i'} & 3));
812             } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
813                 push(@codes, 05) if ($oppos{'i'} & 4);
814                 push(@codes, 064+($oppos{'i'} & 3));
815             } elsif ($op eq 'rel32') {
816                 push(@codes, 05) if ($oppos{'i'} & 4);
817                 push(@codes, 070+($oppos{'i'} & 3));
818             } elsif ($op eq 'seg') {
819                 push(@codes, 05) if ($oppos{'i'} & 4);
820                 push(@codes, 074+($oppos{'i'} & 3));
821             } elsif ($op eq 'ibw') { # imm16 that can be bytified
822                 if (!defined($s_pos)) {
823                     die "$fname: $line: $op without a +s byte\n";
824                 }
825                 $codes[$s_pos] += 0144;
826                 push(@codes, 05) if ($oppos{'i'} & 4);
827                 push(@codes, 0140+($oppos{'i'} & 3));
828             } elsif ($op eq 'ibd') { # imm32 that can be bytified
829                 if (!defined($s_pos)) {
830                     die "$fname: $line: $op without a +s byte\n";
831                 }
832                 $codes[$s_pos] += 0154;
833                 push(@codes, 05) if ($oppos{'i'} & 4);
834                 push(@codes, 0150+($oppos{'i'} & 3));
835             } elsif ($op eq 'ibd,s') {
836                 # imm32 that can be bytified, sign extended to 64 bits
837                 if (!defined($s_pos)) {
838                     die "$fname: $line: $op without a +s byte\n";
839                 }
840                 $codes[$s_pos] += 0154;
841                 push(@codes, 05) if ($oppos{'i'} & 4);
842                 push(@codes, 0250+($oppos{'i'} & 3));
843             }
844             $prefix_ok = 0;
845         } elsif ($op eq '/is4') {
846             if (!defined($oppos{'s'})) {
847                 die "$fname: $line: $op without 's' operand\n";
848             }
849             if (defined($oppos{'i'})) {
850                 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
851             } else {
852                 push(@codes, 0174, $oppos{'s'});
853             }
854             $prefix_ok = 0;
855         } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
856             my $imm = $1;
857             if (!defined($oppos{'s'})) {
858                 die "$fname: $line: $op without 's' operand\n";
859             }
860             if ($imm < 0 || $imm > 15) {
861                 die "$fname: $line: invalid imm4 value for $op: $imm\n";
862             }
863             push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
864             $prefix_ok = 0;
865         } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
866             if (!defined($oppos{'i'})) {
867                 die "$fname: $line: $op without 'i' operand\n";
868             }
869             $s_pos = scalar @codes;
870             push(@codes, 05) if ($oppos{'i'} & 4);
871             push(@codes, $oppos{'i'} & 3, hex $1);
872             $prefix_ok = 0;
873         } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
874             push(@codes, 0330, hex $1);
875             $prefix_ok = 0;
876         } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
877             # Escape to enter literal bytecodes
878             push(@codes, oct $1);
879         } else {
880             die "$fname: $line: unknown operation: $op\n";
881         }
882     }
883
884     return @codes;
885 }