insns.pl: remove stray whitespace
[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 ($op eq 'nohi') { # Use spl/bpl/sil/dil even without REX
670             push(@codes, 0325);
671         } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
672             # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
673             if ($op eq '66') {
674                 push(@codes, 0361);
675             } elsif ($op eq 'f2') {
676                 push(@codes, 0362);
677             } elsif ($op eq 'f3') {
678                 push(@codes, 0363);
679             } else {
680                 push(@codes, 0360);
681             }
682         } elsif ($op =~ /^[0-9a-f]{2}$/) {
683             if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes &&
684                 $codes[$litix] < 4) {
685                 $codes[$litix]++;
686                 push(@codes, hex $op);
687             } else {
688                 $litix = scalar(@codes);
689                 push(@codes, 01, hex $op);
690             }
691             $prefix_ok = 0;
692         } elsif ($op eq '/r') {
693             if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
694                 die "$fname: $line: $op requires r and m operands\n";
695             }
696             $opex = (($oppos{'m'} & 4) ? 06 : 0) |
697                     (($oppos{'r'} & 4) ? 05 : 0);
698             push(@codes, $opex) if ($opex);
699             push(@codes, 0100 + (($oppos{'m'} & 3) << 3) + ($oppos{'r'} & 3));
700             $prefix_ok = 0;
701         } elsif ($op =~ m:^/([0-7])$:) {
702             if (!defined($oppos{'m'})) {
703                 die "$fname: $line: $op requires m operand\n";
704             }
705             push(@codes, 06) if ($oppos{'m'} & 4);
706             push(@codes, 0200 + (($oppos{'m'} & 3) << 3) + $1);
707             $prefix_ok = 0;
708         } elsif ($op =~ /^(vex|xop)(|\..*)$/) {
709             my $c = $vexmap{$1};
710             my ($m,$w,$l,$p) = (undef,2,undef,0);
711             my $has_nds = 0;
712             my @subops = split(/\./, $op);
713             shift @subops;      # Drop prefix
714             foreach $oq (@subops) {
715                 if ($oq eq '128' || $oq eq 'l0') {
716                     $l = 0;
717                 } elsif ($oq eq '256' || $oq eq 'l1') {
718                     $l = 1;
719                 } elsif ($oq eq 'w0') {
720                     $w = 0;
721                 } elsif ($oq eq 'w1') {
722                     $w = 1;
723                 } elsif ($oq eq 'wx') {
724                     $w = 2;
725                 } elsif ($oq eq 'ww') {
726                     $w = 3;
727                 } elsif ($oq eq 'p0') {
728                     $p = 0;
729                 } elsif ($oq eq '66' || $oq eq 'p1') {
730                     $p = 1;
731                 } elsif ($oq eq 'f3' || $oq eq 'p2') {
732                     $p = 2;
733                 } elsif ($oq eq 'f2' || $oq eq 'p3') {
734                     $p = 3;
735                 } elsif ($oq eq '0f') {
736                     $m = 1;
737                 } elsif ($oq eq '0f38') {
738                     $m = 2;
739                 } elsif ($oq eq '0f3a') {
740                     $m = 3;
741                 } elsif ($oq =~ /^m([0-9]+)$/) {
742                     $m = $1+0;
743                 } elsif ($oq eq 'nds' || $oq eq 'ndd' || $oq eq 'dds') {
744                     if (!defined($oppos{'v'})) {
745                         die "$fname: $line: vex.$oq without 'v' operand\n";
746                     }
747                     $has_nds = 1;
748                 } else {
749                     die "$fname: $line: undefined VEX subcode: $oq\n";
750                 }
751             }
752             if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
753                 die "$fname: $line: missing fields in VEX specification\n";
754             }
755             if (defined($oppos{'v'}) && !$has_nds) {
756                 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
757             }
758             push(@codes, defined($oppos{'v'}) ? 0260+($oppos{'v'} & 3) : 0270,
759                  ($c << 6)+$m, ($w << 3)+($l << 2)+$p);
760             $prefix_ok = 0;
761         } elsif ($op =~ /^\/drex([01])$/) {
762             my $oc0 = $1;
763             if (!defined($oppos{'d'})) {
764                 die "$fname: $line: DREX without a 'd' operand\n";
765             }
766             # Note the use of *unshift* here, as opposed to *push*.
767             # This is because NASM want this byte code at the start of
768             # the instruction sequence, but the AMD documentation puts
769             # this at (roughly) the position of the drex byte itself.
770             # This allows us to match the AMD documentation and still
771             # do the right thing.
772             unshift(@codes, 0160+($oppos{'d'} & 3)+($oc0 ? 4 : 0));
773             unshift(@codes, 05) if ($oppos{'d'} & 4);
774         } elsif ($op =~ /^(ib\,s|ib|ibx|ib\,w|iw|iwd|id|idx|iwdq|rel|rel8|rel16|rel32|iq|seg|ibw|ibd|ibd,s)$/) {
775             if (!defined($oppos{'i'})) {
776                 die "$fname: $line: $op without 'i' operand\n";
777             }
778             if ($op eq 'ib,s') { # Signed imm8
779                 push(@codes, 05) if ($oppos{'i'} & 4);
780                 push(@codes, 014+($oppos{'i'} & 3));
781             } elsif ($op eq 'ib') { # imm8
782                 push(@codes, 05) if ($oppos{'i'} & 4);
783                 push(@codes, 020+($oppos{'i'} & 3));
784             } elsif ($op eq 'ib,u') { # Unsigned imm8
785                 push(@codes, 05) if ($oppos{'i'} & 4);
786                 push(@codes, 024+($oppos{'i'} & 3));
787             } elsif ($op eq 'iw') { # imm16
788                 push(@codes, 05) if ($oppos{'i'} & 4);
789                 push(@codes, 030+($oppos{'i'} & 3));
790             } elsif ($op eq 'ibx') { # imm8 sign-extended to opsize
791                 push(@codes, 05) if ($oppos{'i'} & 4);
792                 push(@codes, 0274+($oppos{'i'} & 3));
793             } elsif ($op eq 'iwd') { # imm16 or imm32, depending on opsize
794                 push(@codes, 05) if ($oppos{'i'} & 4);
795                 push(@codes, 034+($oppos{'i'} & 3));
796             } elsif ($op eq 'id') { # imm32
797                 push(@codes, 05) if ($oppos{'i'} & 4);
798                 push(@codes, 040+($oppos{'i'} & 3));
799             } elsif ($op eq 'idx') { # imm32 extended to 64 bits
800                 push(@codes, 05) if ($oppos{'i'} & 4);
801                 push(@codes, 0254+($oppos{'i'} & 3));
802             } elsif ($op eq 'iwdq') { # imm16/32/64, depending on opsize
803                 push(@codes, 05) if ($oppos{'i'} & 4);
804                 push(@codes, 044+($oppos{'i'} & 3));
805             } elsif ($op eq 'rel8') {
806                 push(@codes, 05) if ($oppos{'i'} & 4);
807                 push(@codes, 050+($oppos{'i'} & 3));
808             } elsif ($op eq 'iq') {
809                 push(@codes, 05) if ($oppos{'i'} & 4);
810                 push(@codes, 054+($oppos{'i'} & 3));
811             } elsif ($op eq 'rel16') {
812                 push(@codes, 05) if ($oppos{'i'} & 4);
813                 push(@codes, 060+($oppos{'i'} & 3));
814             } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
815                 push(@codes, 05) if ($oppos{'i'} & 4);
816                 push(@codes, 064+($oppos{'i'} & 3));
817             } elsif ($op eq 'rel32') {
818                 push(@codes, 05) if ($oppos{'i'} & 4);
819                 push(@codes, 070+($oppos{'i'} & 3));
820             } elsif ($op eq 'seg') {
821                 push(@codes, 05) if ($oppos{'i'} & 4);
822                 push(@codes, 074+($oppos{'i'} & 3));
823             } elsif ($op eq 'ibw') { # imm16 that can be bytified
824                 if (!defined($s_pos)) {
825                     die "$fname: $line: $op without a +s byte\n";
826                 }
827                 $codes[$s_pos] += 0144;
828                 push(@codes, 05) if ($oppos{'i'} & 4);
829                 push(@codes, 0140+($oppos{'i'} & 3));
830             } elsif ($op eq 'ibd') { # imm32 that can be bytified
831                 if (!defined($s_pos)) {
832                     die "$fname: $line: $op without a +s byte\n";
833                 }
834                 $codes[$s_pos] += 0154;
835                 push(@codes, 05) if ($oppos{'i'} & 4);
836                 push(@codes, 0150+($oppos{'i'} & 3));
837             } elsif ($op eq 'ibd,s') {
838                 # imm32 that can be bytified, sign extended to 64 bits
839                 if (!defined($s_pos)) {
840                     die "$fname: $line: $op without a +s byte\n";
841                 }
842                 $codes[$s_pos] += 0154;
843                 push(@codes, 05) if ($oppos{'i'} & 4);
844                 push(@codes, 0250+($oppos{'i'} & 3));
845             }
846             $prefix_ok = 0;
847         } elsif ($op eq '/is4') {
848             if (!defined($oppos{'s'})) {
849                 die "$fname: $line: $op without 's' operand\n";
850             }
851             if (defined($oppos{'i'})) {
852                 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
853             } else {
854                 push(@codes, 0174, $oppos{'s'});
855             }
856             $prefix_ok = 0;
857         } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
858             my $imm = $1;
859             if (!defined($oppos{'s'})) {
860                 die "$fname: $line: $op without 's' operand\n";
861             }
862             if ($imm < 0 || $imm > 15) {
863                 die "$fname: $line: invalid imm4 value for $op: $imm\n";
864             }
865             push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
866             $prefix_ok = 0;
867         } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
868             if (!defined($oppos{'i'})) {
869                 die "$fname: $line: $op without 'i' operand\n";
870             }
871             $s_pos = scalar @codes;
872             push(@codes, 05) if ($oppos{'i'} & 4);
873             push(@codes, $oppos{'i'} & 3, hex $1);
874             $prefix_ok = 0;
875         } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
876             push(@codes, 0330, hex $1);
877             $prefix_ok = 0;
878         } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
879             # Escape to enter literal bytecodes
880             push(@codes, oct $1);
881         } else {
882             die "$fname: $line: unknown operation: $op\n";
883         }
884     }
885
886     return @codes;
887 }