5b45d7e5ca140a015627f8e6c8fc7787e93fb716
[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 print STDERR "Reading insns.dat...\n";
18
19 @args   = ();
20 undef $output;
21 foreach $arg ( @ARGV ) {
22     if ( $arg =~ /^\-/ ) {
23         if  ( $arg =~ /^\-([abdin])$/ ) {
24             $output = $1;
25         } else {
26             die "$0: Unknown option: ${arg}\n";
27         }
28     } else {
29         push (@args, $arg);
30     }
31 }
32
33 $fname = "insns.dat" unless $fname = $args[0];
34 open (F, $fname) || die "unable to open $fname";
35
36 %dinstables = ();
37 @bytecode_list = ();
38
39 $line = 0;
40 $insns = 0;
41 while (<F>) {
42   $line++;
43   chomp;
44   next if ( /^\s*(\;.*|)$/ );   # comments or blank lines
45
46   unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
47       warn "line $line does not contain four fields\n";
48       next;
49   }
50   @fields = ($1, $2, $3, $4);
51   ($formatted, $nd) = format_insn(@fields);
52   if ($formatted) {
53     $insns++;
54     $aname = "aa_$fields[0]";
55     push @$aname, $formatted;
56   }
57   if ( $fields[0] =~ /cc$/ ) {
58       # Conditional instruction
59       $k_opcodes_cc{$fields[0]}++;
60   } else {
61       # Unconditional instruction
62       $k_opcodes{$fields[0]}++;
63   }
64   if ($formatted && !$nd) {
65     push @big, $formatted;
66     my @sseq = startseq($fields[2]);
67     foreach $i (@sseq) {
68         if (!defined($dinstables{$i})) {
69             $dinstables{$i} = [];
70         }
71         push(@{$dinstables{$i}}, $#big);
72     }
73   }
74 }
75
76 close F;
77
78 #
79 # Generate the bytecode array.  At this point, @bytecode_list contains
80 # the full set of bytecodes.
81 #
82
83 # Sort by descending length
84 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
85 @bytecode_array = ();
86 %bytecode_pos = ();
87 $bytecode_next = 0;
88 foreach $bl (@bytecode_list) {
89     my $h = hexstr(@$bl);
90     next if (defined($bytecode_pos{$h}));
91
92     push(@bytecode_array, $bl);
93     while ($h ne '') {
94         $bytecode_pos{$h} = $bytecode_next;
95         $h = substr($h, 2);
96         $bytecode_next++;
97     }
98 }
99 undef @bytecode_list;
100
101 @opcodes    = sort keys(%k_opcodes);
102 @opcodes_cc = sort keys(%k_opcodes_cc);
103
104 if ( !defined($output) || $output eq 'b') {
105     print STDERR "Writing insnsb.c...\n";
106
107     open B, ">insnsb.c";
108
109     print B "/* This file auto-generated from insns.dat by insns.pl" .
110         " - don't edit it */\n\n";
111
112     print B "#include \"nasm.h\"\n";
113     print B "#include \"insns.h\"\n\n";
114
115     print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
116
117     $p = 0;
118     foreach $bl (@bytecode_array) {
119         printf B "    /* %5d */ ", $p;
120         foreach $d (@$bl) {
121             printf B "%#o,", $d;
122             $p++;
123         }
124         printf B "\n";
125     }
126     print B "};\n";
127
128     close B;
129 }
130
131 if ( !defined($output) || $output eq 'a' ) {
132     print STDERR "Writing insnsa.c...\n";
133
134     open A, ">insnsa.c";
135
136     print A "/* This file auto-generated from insns.dat by insns.pl" .
137         " - don't edit it */\n\n";
138
139     print A "#include \"nasm.h\"\n";
140     print A "#include \"insns.h\"\n\n";
141
142     foreach $i (@opcodes, @opcodes_cc) {
143         print A "static const struct itemplate instrux_${i}[] = {\n";
144         $aname = "aa_$i";
145         foreach $j (@$aname) {
146             print A "    ", codesubst($j), "\n";
147         }
148         print A "    ITEMPLATE_END\n};\n\n";
149     }
150     print A "const struct itemplate * const nasm_instructions[] = {\n";
151     foreach $i (@opcodes, @opcodes_cc) {
152         print A "    instrux_${i},\n";
153     }
154     print A "};\n";
155
156     close A;
157 }
158
159 if ( !defined($output) || $output eq 'd' ) {
160     print STDERR "Writing insnsd.c...\n";
161
162     open D, ">insnsd.c";
163
164     print D "/* This file auto-generated from insns.dat by insns.pl" .
165         " - don't edit it */\n\n";
166
167     print D "#include \"nasm.h\"\n";
168     print D "#include \"insns.h\"\n\n";
169
170     print D "static const struct itemplate instrux[] = {\n";
171     $n = 0;
172     foreach $j (@big) {
173         printf D "    /* %4d */ %s\n", $n++, codesubst($j);
174     }
175     print D "};\n";
176
177     foreach $h (sort(keys(%dinstables))) {
178         print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
179         foreach $j (@{$dinstables{$h}}) {
180             print D "    instrux + $j,\n";
181         }
182         print D "};\n";
183     }
184
185     foreach $h (@disasm_prefixes, '') {
186         $is_prefix{$h} = 1;
187         print D "\n";
188         print D "static " unless ($h eq '');
189         print D "const struct disasm_index ";
190         print D ($h eq '') ? 'itable' : "itable_$h";
191         print D "[256] = {\n";
192         for ($c = 0; $c < 256; $c++) {
193             $nn = sprintf("%s%02X", $h, $c);
194             if ($is_prefix{$nn}) {
195                 die "$0: ambiguous decoding of $nn\n"
196                     if (defined($dinstables{$nn}));
197                 printf D "    { itable_%s, -1 },\n", $nn;
198             } elsif (defined($dinstables{$nn})) {
199                 printf D "    { itable_%s, %u },\n",
200                 $nn, scalar(@{$dinstables{$nn}});
201             } else {
202                 printf D "    { NULL, 0 },\n";
203             }
204         }
205     print D "};\n";
206     }
207
208     close D;
209 }
210
211 if ( !defined($output) || $output eq 'i' ) {
212     print STDERR "Writing insnsi.h...\n";
213
214     open I, ">insnsi.h";
215
216     print I "/* This file is auto-generated from insns.dat by insns.pl" .
217         " - don't edit it */\n\n";
218     print I "/* This file in included by nasm.h */\n\n";
219
220     print I "/* Instruction names */\n\n";
221     print I "#ifndef NASM_INSNSI_H\n";
222     print I "#define NASM_INSNSI_H 1\n\n";
223     print I "enum opcode {\n";
224     $maxlen = 0;
225     foreach $i (@opcodes, @opcodes_cc) {
226         print I "\tI_${i},\n";
227         $len = length($i);
228         $len++ if ( $i =~ /cc$/ );      # Condition codes can be 3 characters long
229         $maxlen = $len if ( $len > $maxlen );
230     }
231     print I "\tI_none = -1\n";
232     print I "\n};\n\n";
233     print I "#define MAX_INSLEN ", $maxlen, "\n";
234     print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
235     print I "#endif /* NASM_INSNSI_H */\n";
236
237     close I;
238 }
239
240 if ( !defined($output) || $output eq 'n' ) {
241     print STDERR "Writing insnsn.c...\n";
242
243     open N, ">insnsn.c";
244
245     print N "/* This file is auto-generated from insns.dat by insns.pl" .
246         " - don't edit it */\n\n";
247     print N "#include \"tables.h\"\n\n";
248
249     print N "const char * const nasm_insn_names[] = {";
250     $first = 1;
251     foreach $i (@opcodes) {
252         print N "," if ( !$first );
253         $first = 0;
254         $ilower = $i;
255         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
256         print N "\n\t\"${ilower}\"";
257     }
258     print N "\n};\n\n";
259     print N "/* Conditional instructions */\n";
260     print N "const char * const nasm_cond_insn_names[] = {";
261     $first = 1;
262     foreach $i (@opcodes_cc) {
263         print N "," if ( !$first );
264         $first = 0;
265         $ilower = $i;
266         $ilower =~ s/cc$//;             # Skip cc suffix
267         $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
268         print N "\n\t\"${ilower}\"";
269     }
270
271     print N "\n};\n\n";
272     print N "/* and the corresponding opcodes */\n";
273     print N "const enum opcode nasm_cond_insn_opcodes[] = {";
274     $first = 1;
275     foreach $i (@opcodes_cc) {
276         print N "," if ( !$first );
277         $first = 0;
278         print N "\n\tI_$i";
279     }
280
281     print N "\n};\n";
282     close N;
283 }
284
285 printf STDERR "Done: %d instructions\n", $insns;
286
287 sub format_insn(@) {
288     my ($opcode, $operands, $codes, $flags) = @_;
289     my $num, $nd = 0;
290     my @bytecode;
291
292     return (undef, undef) if $operands eq "ignore";
293
294     # format the operands
295     $operands =~ s/:/|colon,/g;
296     $operands =~ s/mem(\d+)/mem|bits$1/g;
297     $operands =~ s/mem/memory/g;
298     $operands =~ s/memory_offs/mem_offs/g;
299     $operands =~ s/imm(\d+)/imm|bits$1/g;
300     $operands =~ s/imm/immediate/g;
301     $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
302     $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
303     $operands =~ s/\=([0-9]+)/same_as|$1/g;
304     if ($operands eq 'void') {
305         @ops = ();
306     } else {
307         @ops = split(/\,/, $operands);
308     }
309     $num = scalar(@ops);
310     while (scalar(@ops) < $MAX_OPERANDS) {
311         push(@ops, '0');
312     }
313     $operands = join(',', @ops);
314     $operands =~ tr/a-z/A-Z/;
315
316     # format the flags
317     $flags =~ s/,/|IF_/g;
318     $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
319     $flags = "IF_" . $flags;
320
321     @bytecode = (decodify($codes), 0);
322     push(@bytecode_list, [@bytecode]);
323     $codes = hexstr(@bytecode);
324
325     ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
326 }
327
328 #
329 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
330 # offset into nasm_bytecodes
331 #
332 sub codesubst($) {
333     my($s) = @_;
334     my $n;
335
336     while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
337         my $pos = $bytecode_pos{$1};
338         if (!defined($pos)) {
339             die "$0: no position assigned to byte code $1\n";
340         }
341         $s = $` . "nasm_bytecodes+${pos}" . "$'";
342     }
343     return $s;
344 }
345
346 sub addprefix ($@) {
347     my ($prefix, @list) = @_;
348     my $x;
349     my @l = ();
350
351     foreach $x (@list) {
352         push(@l, sprintf("%s%02X", $prefix, $x));
353     }
354
355     return @l;
356 }
357
358 #
359 # Turn a code string into a sequence of bytes
360 #
361 sub decodify($) {
362   # Although these are C-syntax strings, by convention they should have
363   # only octal escapes (for directives) and hexadecimal escapes
364   # (for verbatim bytes)
365     my($codestr) = @_;
366
367     if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
368         return byte_code_compile($1);
369     }
370
371     my $c = $codestr;
372     my @codes = ();
373
374     while ($c ne '') {
375         if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
376             push(@codes, hex $1);
377             $c = $2;
378             next;
379         } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
380             push(@codes, oct $1);
381             $c = $2;
382             next;
383         } else {
384             die "$0: unknown code format in \"$codestr\"\n";
385         }
386     }
387
388     return @codes;
389 }
390
391 # Turn a numeric list into a hex string
392 sub hexstr(@) {
393     my $s = '';
394     my $c;
395
396     foreach $c (@_) {
397         $s .= sprintf("%02X", $c);
398     }
399     return $s;
400 }
401
402 # Here we determine the range of possible starting bytes for a given
403 # instruction. We need only consider the codes:
404 # \1 \2 \3     mean literal bytes, of course
405 # \4 \5 \6 \7  mean PUSH/POP of segment registers: special case
406 # \1[0123]     mean byte plus register value
407 # \330         means byte plus condition code
408 # \0 or \340   mean give up and return empty set
409 sub startseq($) {
410   my ($codestr) = @_;
411   my $word, @range;
412   my @codes = ();
413   my $c = $codestr;
414   my $c0, $c1, $i;
415   my $prefix = '';
416
417   @codes = decodify($codestr);
418
419   while ($c0 = shift(@codes)) {
420       $c1 = $codes[0];
421       if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
422           # Fixed byte string
423           my $fbs = $prefix;
424           while (1) {
425               if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
426                   while ($c0--) {
427                       $fbs .= sprintf("%02X", shift(@codes));
428                   }
429               } else {
430                   last;
431               }
432               $c0 = shift(@codes);
433           }
434
435           foreach $pfx (@disasm_prefixes) {
436               if (substr($fbs, 0, length($pfx)) eq $pfx) {
437                   $prefix = $pfx;
438                   $fbs = substr($fbs, length($pfx));
439                   last;
440               }
441           }
442
443           if ($fbs ne '') {
444               return ($prefix.substr($fbs,0,2));
445           }
446
447           unshift(@codes, $c0);
448       } elsif ($c0 == 04) {
449           return addprefix($prefix, 0x07, 0x17, 0x1F);
450       } elsif ($c0 == 05) {
451           return addprefix($prefix, 0xA1, 0xA9);
452       } elsif ($c0 == 06) {
453           return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
454       } elsif ($c0 == 07) {
455           return addprefix($prefix, 0xA0, 0xA8);
456       } elsif ($c0 >= 010 && $c0 <= 013) {
457           return addprefix($prefix, $c1..($c1+7));
458       } elsif (($c0 & ~013) == 0144) {
459           return addprefix($prefix, $c1, $c1|2);
460       } elsif ($c0 == 0330) {
461           return addprefix($prefix, $c1..($c1+15));
462       } elsif ($c0 == 0 || $c0 == 0340) {
463           return $prefix;
464       } elsif (($c0 & ~3) == 0260 || $c0 == 270) {
465           shift(@codes);
466           shift(@codes);
467       } elsif ($c0 == 0172) {
468           shift(@codes);
469       } else {
470           # We really need to be able to distinguish "forbidden"
471           # and "ignorable" codes here
472       }
473   }
474   return $prefix;
475 }
476
477 #
478 # This function takes a series of byte codes in a format which is more
479 # typical of the Intel documentation, and encode it.
480 #
481 # The format looks like:
482 #
483 # [operands: opcodes]
484 #
485 # The operands word lists the order of the operands:
486 #
487 # r = register field in the modr/m
488 # m = modr/m
489 # v = VEX "v" field
490 # d = DREX "dst" field
491 # i = immediate
492 # s = register field of is4/imz2 field
493 # - = implicit (unencoded) operand
494 #
495 # For an operand that should be filled into more than one field,
496 # enter it as e.g. "r+v".
497 #
498 sub byte_code_compile($) {
499     my($str) = @_;
500     my $opr;
501     my $opc;
502     my @codes = ();
503     my $litix = undef;
504     my %oppos = ();
505     my $i;
506     my $op, $oq;
507
508     if ($str =~ /^(\S*)\:\s*(.*\S)\s*$/) {
509         $opr = "\L$1";
510         $opc = "\L$2";
511     } else {
512         $opr = '';
513         $opc = "\L$str";
514     }
515
516     my $op = 0;
517     for ($i = 0; $i < length($opr); $i++) {
518         my $c = substr($opr,$i,1);
519         if ($c eq '+') {
520             $op--;
521         } else {
522             $oppos{$c} = $op++;
523         }
524     }
525
526     $prefix_ok = 1;
527     foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
528         if ($op eq 'o16') {
529             push(@codes, 0320);
530         } elsif ($op eq 'o32') {
531             push(@codes, 0321);
532         } elsif ($op eq 'o64') {  # 64-bit operand size requiring REX.W
533             push(@codes, 0324);
534         } elsif ($op eq 'o64i') { # Implied 64-bit operand size (no REX.W)
535             push(@codes, 0323);
536         } elsif ($op eq 'a16') {
537             push(@codes, 0310);
538         } elsif ($op eq 'a32') {
539             push(@codes, 0311);
540         } elsif ($op eq 'a64') {
541             push(@codes, 0313);
542         } elsif ($op eq '!osp') {
543             push(@codes, 0364);
544         } elsif ($op eq '!asp') {
545             push(@codes, 0365);
546         } elsif ($op eq 'rex.l') {
547             push(@codes, 0334);
548         } elsif ($op eq 'repe') {
549             push(@codes, 0335);
550         } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
551             # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
552             if ($op eq '66') {
553                 push(@codes, 0361);
554             } elsif ($op eq 'f2') {
555                 push(@codes, 0362);
556             } elsif ($op eq 'f3') {
557                 push(@codes, 0363);
558             } else {
559                 push(@codes, 0360);
560             }
561         } elsif ($op =~ /^[0-9a-f]{2}$/) {
562             if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes) {
563                 $codes[$litix]++;
564                 push(@codes, hex $op);
565             } else {
566                 $litix = scalar(@codes);
567                 push(@codes, 01, hex $op);
568             }
569             $prefix_ok = 0;
570         } elsif ($op eq '/r') {
571             if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
572                 die "$0: $line: $op requires r and m operands\n";
573             }
574             push(@codes, 0100 + ($oppos{'m'} << 3) + $oppos{'r'});
575             $prefix_ok = 0;
576         } elsif ($op =~ m:^/([0-7])$:) {
577             if (!defined($oppos{'m'})) {
578                 die "$0: $line: $op requires m operand\n";
579             }
580             push(@codes, 0200 + ($oppos{'m'} << 3) + $1);
581             $prefix_ok = 0;
582         } elsif ($op =~ /^vex(|\..*)$/) {
583             my ($m,$w,$l,$p) = (undef,2,undef,0);
584             foreach $oq (split(/\./, $op)) {
585                 if ($oq eq 'vex') {
586                     # prefix
587                 } elsif ($oq eq '128' || $oq eq 'l0') {
588                     $l = 0;
589                 } elsif ($oq eq '256' || $oq eq 'l1') {
590                     $l = 1;
591                 } elsif ($oq eq 'w0') {
592                     $w = 0;
593                 } elsif ($oq eq 'w1') {
594                     $w = 1;
595                 } elsif ($oq eq '66') {
596                     $p = 1;
597                 } elsif ($oq eq 'f3') {
598                     $p = 2;
599                 } elsif ($oq eq 'f2') {
600                     $p = 3;
601                 } elsif ($oq eq '0f') {
602                     $m = 1;
603                 } elsif ($oq eq '0f38') {
604                     $m = 2;
605                 } elsif ($oq eq '0f3a') {
606                     $m = 3;
607                 } elsif ($oq =~ /^m([0-9]+)$/) {
608                     $m = $1+0;
609                 } elsif ($oq eq 'nds' || $oq eq 'ndd') {
610                     if (!defined($oppos{'v'})) {
611                         die "$0: $line: vex.$oq without 'v' operand\n";
612                     }
613                 } else {
614                     die "$0: $line: undefined VEX subcode: $oq\n";
615                 }
616             }
617             if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
618                 die "$0: $line: missing fields in VEX specification\n";
619             }
620             push(@codes, defined($oppos{'v'}) ? 0260+$oppos{'v'} : 0270,
621                  $m, ($w << 3)+($l << 2)+$p);
622             $prefix_ok = 0;
623         } elsif ($op =~ /^drex(|..*)$/) {
624             my ($oc0) = (0);
625             foreach $oq (split(/\./, $op)) {
626                 if ($oq eq 'drex') {
627                     #prefix
628                 } elsif ($oq eq 'oc0') {
629                     $oc0 = 1;
630                 } else {
631                     die "$0: $line: undefined DREX subcode: $oq\n";
632                 }
633             }
634             if (!defined($oppos{'d'})) {
635                 die "$0: $line: DREX without a 'd' operand\n";
636             }
637             push(@codes, 0160+$oppos{'d'}+($oc0 ? 4 : 0));
638         } elsif ($op =~ /^(ib\,s|ib|ib\,w|iw|iwd|id|iwdq|rel|rel8|rel16|rel32|iq|seg|ibw|ibd|ibd,s)$/) {
639             if (!defined($oppos{'i'})) {
640                 die "$0: $op without 'i' operand\n";
641             }
642             if ($op eq 'ib,s') { # Signed imm8
643                 push(@codes, 014+$oppos{'i'});
644             } elsif ($op eq 'ib') { # imm8
645                 push(@codes, 020+$oppos{'i'});
646             } elsif ($op eq 'ib,u') { # Unsigned imm8
647                 push(@codes, 024+$oppos{'i'});
648             } elsif ($op eq 'iw') { # imm16
649                 push(@codes, 030+$oppos{'i'});
650             } elsif ($op eq 'iwd') { # imm16 or imm32, depending on opsize
651                 push(@codes, 034+$oppos{'i'});
652             } elsif ($op eq 'id') { # imm32
653                 push(@codes, 040+$oppos{'i'});
654             } elsif ($op eq 'iwdq') { # imm16/32/64, depending on opsize
655                 push(@codes, 044+$oppos{'i'});
656             } elsif ($op eq 'rel8') {
657                 push(@codes, 050+$oppos{'i'});
658             } elsif ($op eq 'iq') {
659                 push(@codes, 054+$oppos{'i'});
660             } elsif ($op eq 'rel16') {
661                 push(@codes, 060+$oppos{'i'});
662             } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
663                 push(@codes, 064+$oppos{'i'});
664             } elsif ($op eq 'rel32') {
665                 push(@codes, 070+$oppos{'i'});
666             } elsif ($op eq 'seg') {
667                 push(@codes, 074+$oppos{'i'});
668             } elsif ($op eq 'ibw') { # imm16 that can be bytified
669                 if (!defined($s_pos)) {
670                     die "$0: $line: $op without a +s byte\n";
671                 }
672                 $codes[$s_pos] += 0144;
673                 push(@codes, 0140+$oppos{'i'});
674             } elsif ($op eq 'ibd') { # imm32 that can be bytified
675                 if (!defined($s_pos)) {
676                     die "$0: $line: $op without a +s byte\n";
677                 }
678                 $codes[$s_pos] += 0154;
679                 push(@codes, 0150+$oppos{'i'});
680             } elsif ($op eq 'ibd,s') {
681                 # imm32 that can be bytified, sign extended to 64 bits
682                 if (!defined($s_pos)) {
683                     die "$0: $line: $op without a +s byte\n";
684                 }
685                 $codes[$s_pos] += 0154;
686                 push(@codes, 0250+$oppos{'i'});
687             }
688             $prefix_ok = 0;
689         } elsif ($op eq '/is4') {
690             if (!defined($oppos{'i'} || !defined($oppos{'s'}))) {
691                 die "$0: $line: $op without 'i' and 's' operands\n";
692             }
693             push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
694             $prefix_ok = 0;
695         } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
696             my $imm = $1;
697             if (!defined($oppos{'s'})) {
698                 die "$0: $line: $op without 's' operand\n";
699             }
700             if ($imm < 0 || $imm > 15) {
701                 die "$0: $line: invalid imm4 value for $op: $imm\n";
702             }
703             push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
704             $prefix_ok = 0;
705         } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
706             if (!defined($oppos{'i'})) {
707                 die "$0: $op without 'i' operand\n";
708             }
709             $s_pos = scalar @codes;
710             push(@codes, $oppos{'i'}, hex $1);
711             $prefix_ok = 0;
712         } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
713             push(@codes, 0330, hex $1);
714             $prefix_ok = 0;
715         } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
716             # Escape to enter literal bytecodes
717             push(@codes, oct $1);
718         } else {
719             die "$0: unknown operation: $op\n";
720         }
721     }
722
723     return @codes;
724 }