doc: document the %[...] construct.
[nasm.git] / insns.pl
blobb74017b2e51a52a2d23f4b41556954c73edc16a8
1 #!/usr/bin/perl
3 # insns.pl produce insnsa.c, insnsd.c, insnsi.h, insnsn.c from insns.dat
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.
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);
14 # This should match MAX_OPERANDS from nasm.h
15 $MAX_OPERANDS = 5;
17 # Add VEX prefixes
18 @vexlist = ();
19 for ($m = 0; $m < 32; $m++) {
20 for ($lp = 0; $lp < 8; $lp++) {
21 push(@vexlist, sprintf("VEX%02X%01X", $m, $lp));
24 @disasm_prefixes = (@vexlist, @disasm_prefixes);
26 @bytecode_count = (0) x 256;
28 print STDERR "Reading insns.dat...\n";
30 @args = ();
31 undef $output;
32 foreach $arg ( @ARGV ) {
33 if ( $arg =~ /^\-/ ) {
34 if ( $arg =~ /^\-([abdin])$/ ) {
35 $output = $1;
36 } else {
37 die "$0: Unknown option: ${arg}\n";
39 } else {
40 push (@args, $arg);
44 $fname = "insns.dat" unless $fname = $args[0];
45 open (F, $fname) || die "unable to open $fname";
47 %dinstables = ();
48 @bytecode_list = ();
50 $line = 0;
51 $insns = 0;
52 while (<F>) {
53 $line++;
54 chomp;
55 next if ( /^\s*(\;.*|)$/ ); # comments or blank lines
57 unless (/^\s*(\S+)\s+(\S+)\s+(\S+|\[.*\])\s+(\S+)\s*$/) {
58 warn "line $line does not contain four fields\n";
59 next;
61 @fields = ($1, $2, $3, $4);
62 ($formatted, $nd) = format_insn(@fields);
63 if ($formatted) {
64 $insns++;
65 $aname = "aa_$fields[0]";
66 push @$aname, $formatted;
68 if ( $fields[0] =~ /cc$/ ) {
69 # Conditional instruction
70 $k_opcodes_cc{$fields[0]}++;
71 } else {
72 # Unconditional instruction
73 $k_opcodes{$fields[0]}++;
75 if ($formatted && !$nd) {
76 push @big, $formatted;
77 my @sseq = startseq($fields[2]);
78 foreach $i (@sseq) {
79 if (!defined($dinstables{$i})) {
80 $dinstables{$i} = [];
82 push(@{$dinstables{$i}}, $#big);
87 close F;
90 # Generate the bytecode array. At this point, @bytecode_list contains
91 # the full set of bytecodes.
94 # Sort by descending length
95 @bytecode_list = sort { scalar(@$b) <=> scalar(@$a) } @bytecode_list;
96 @bytecode_array = ();
97 %bytecode_pos = ();
98 $bytecode_next = 0;
99 foreach $bl (@bytecode_list) {
100 my $h = hexstr(@$bl);
101 next if (defined($bytecode_pos{$h}));
103 push(@bytecode_array, $bl);
104 while ($h ne '') {
105 $bytecode_pos{$h} = $bytecode_next;
106 $h = substr($h, 2);
107 $bytecode_next++;
110 undef @bytecode_list;
112 @opcodes = sort keys(%k_opcodes);
113 @opcodes_cc = sort keys(%k_opcodes_cc);
115 if ( !defined($output) || $output eq 'b') {
116 print STDERR "Writing insnsb.c...\n";
118 open B, ">insnsb.c";
120 print B "/* This file auto-generated from insns.dat by insns.pl" .
121 " - don't edit it */\n\n";
123 print B "#include \"nasm.h\"\n";
124 print B "#include \"insns.h\"\n\n";
126 print B "const uint8_t nasm_bytecodes[$bytecode_next] = {\n";
128 $p = 0;
129 foreach $bl (@bytecode_array) {
130 printf B " /* %5d */ ", $p;
131 foreach $d (@$bl) {
132 printf B "%#o,", $d;
133 $p++;
135 printf B "\n";
137 print B "};\n";
139 print B "\n";
140 print B "/*\n";
141 print B " * Bytecode frequencies (including reuse):\n";
142 print B " *\n";
143 for ($i = 0; $i < 32; $i++) {
144 print B " *";
145 for ($j = 0; $j < 256; $j += 32) {
146 print B " |" if ($j);
147 printf B " %3o:%4d", $i+$j, $bytecode_count[$i+$j];
149 print B "\n";
151 print B " */\n";
153 close B;
156 if ( !defined($output) || $output eq 'a' ) {
157 print STDERR "Writing insnsa.c...\n";
159 open A, ">insnsa.c";
161 print A "/* This file auto-generated from insns.dat by insns.pl" .
162 " - don't edit it */\n\n";
164 print A "#include \"nasm.h\"\n";
165 print A "#include \"insns.h\"\n\n";
167 foreach $i (@opcodes, @opcodes_cc) {
168 print A "static const struct itemplate instrux_${i}[] = {\n";
169 $aname = "aa_$i";
170 foreach $j (@$aname) {
171 print A " ", codesubst($j), "\n";
173 print A " ITEMPLATE_END\n};\n\n";
175 print A "const struct itemplate * const nasm_instructions[] = {\n";
176 foreach $i (@opcodes, @opcodes_cc) {
177 print A " instrux_${i},\n";
179 print A "};\n";
181 close A;
184 if ( !defined($output) || $output eq 'd' ) {
185 print STDERR "Writing insnsd.c...\n";
187 open D, ">insnsd.c";
189 print D "/* This file auto-generated from insns.dat by insns.pl" .
190 " - don't edit it */\n\n";
192 print D "#include \"nasm.h\"\n";
193 print D "#include \"insns.h\"\n\n";
195 print D "static const struct itemplate instrux[] = {\n";
196 $n = 0;
197 foreach $j (@big) {
198 printf D " /* %4d */ %s\n", $n++, codesubst($j);
200 print D "};\n";
202 foreach $h (sort(keys(%dinstables))) {
203 next if ($h eq ''); # Skip pseudo-instructions
204 print D "\nstatic const struct itemplate * const itable_${h}[] = {\n";
205 foreach $j (@{$dinstables{$h}}) {
206 print D " instrux + $j,\n";
208 print D "};\n";
211 @prefix_list = ();
212 foreach $h (@disasm_prefixes, '') {
213 for ($c = 0; $c < 256; $c++) {
214 $nn = sprintf("%s%02X", $h, $c);
215 if ($is_prefix{$nn} || defined($dinstables{$nn})) {
216 # At least one entry in this prefix table
217 push(@prefix_list, $h);
218 $is_prefix{$h} = 1;
219 last;
224 foreach $h (@prefix_list) {
225 print D "\n";
226 print D "static " unless ($h eq '');
227 print D "const struct disasm_index ";
228 print D ($h eq '') ? 'itable' : "itable_$h";
229 print D "[256] = {\n";
230 for ($c = 0; $c < 256; $c++) {
231 $nn = sprintf("%s%02X", $h, $c);
232 if ($is_prefix{$nn}) {
233 die "$fname: ambiguous decoding of $nn\n"
234 if (defined($dinstables{$nn}));
235 printf D " { itable_%s, -1 },\n", $nn;
236 } elsif (defined($dinstables{$nn})) {
237 printf D " { itable_%s, %u },\n",
238 $nn, scalar(@{$dinstables{$nn}});
239 } else {
240 printf D " { NULL, 0 },\n";
243 print D "};\n";
246 print D "\nconst struct disasm_index * const itable_VEX[32][8] = {\n ";
247 for ($m = 0; $m < 32; $m++) {
248 print D " {\n";
249 for ($lp = 0; $lp < 8; $lp++) {
250 $vp = sprintf("VEX%02X%01X", $m, $lp);
251 if ($is_prefix{$vp}) {
252 printf D " itable_%s,\n", $vp;
253 } else {
254 print D " NULL,\n";
257 print D " },";
259 print D "\n};\n";
261 close D;
264 if ( !defined($output) || $output eq 'i' ) {
265 print STDERR "Writing insnsi.h...\n";
267 open I, ">insnsi.h";
269 print I "/* This file is auto-generated from insns.dat by insns.pl" .
270 " - don't edit it */\n\n";
271 print I "/* This file in included by nasm.h */\n\n";
273 print I "/* Instruction names */\n\n";
274 print I "#ifndef NASM_INSNSI_H\n";
275 print I "#define NASM_INSNSI_H 1\n\n";
276 print I "enum opcode {\n";
277 $maxlen = 0;
278 foreach $i (@opcodes, @opcodes_cc) {
279 print I "\tI_${i},\n";
280 $len = length($i);
281 $len++ if ( $i =~ /cc$/ ); # Condition codes can be 3 characters long
282 $maxlen = $len if ( $len > $maxlen );
284 print I "\tI_none = -1\n";
285 print I "\n};\n\n";
286 print I "#define MAX_INSLEN ", $maxlen, "\n";
287 print I "#define FIRST_COND_OPCODE I_", $opcodes_cc[0], "\n\n";
288 print I "#endif /* NASM_INSNSI_H */\n";
290 close I;
293 if ( !defined($output) || $output eq 'n' ) {
294 print STDERR "Writing insnsn.c...\n";
296 open N, ">insnsn.c";
298 print N "/* This file is auto-generated from insns.dat by insns.pl" .
299 " - don't edit it */\n\n";
300 print N "#include \"tables.h\"\n\n";
302 print N "const char * const nasm_insn_names[] = {";
303 $first = 1;
304 foreach $i (@opcodes, @opcodes_cc) {
305 print N "," if ( !$first );
306 $first = 0;
307 $ilower = $i;
308 $ilower =~ s/cc$//; # Remove conditional cc suffix
309 $ilower =~ tr/A-Z/a-z/; # Change to lower case (Perl 4 compatible)
310 print N "\n\t\"${ilower}\"";
312 print N "\n};\n";
313 close N;
316 printf STDERR "Done: %d instructions\n", $insns;
318 # Count primary bytecodes, for statistics
319 sub count_bytecodes(@) {
320 my $skip = 0;
321 foreach my $bc (@_) {
322 if ($skip) {
323 $skip--;
324 next;
326 $bytecode_count[$bc]++;
327 if ($bc >= 01 && $bc <= 03) {
328 $skip = $bc;
329 } elsif (($bc & ~03) == 010) {
330 $skip = 1;
331 } elsif (($bc & ~013) == 0144) {
332 $skip = 1;
333 } elsif ($bc == 0172) {
334 $skip = 1;
335 } elsif ($bc >= 0260 && $bc <= 0270) {
336 $skip = 2;
337 } elsif ($bc == 0330) {
338 $skip = 1;
343 sub format_insn(@) {
344 my ($opcode, $operands, $codes, $flags) = @_;
345 my $num, $nd = 0;
346 my @bytecode;
348 return (undef, undef) if $operands eq "ignore";
350 # format the operands
351 $operands =~ s/:/|colon,/g;
352 $operands =~ s/mem(\d+)/mem|bits$1/g;
353 $operands =~ s/mem/memory/g;
354 $operands =~ s/memory_offs/mem_offs/g;
355 $operands =~ s/imm(\d+)/imm|bits$1/g;
356 $operands =~ s/imm/immediate/g;
357 $operands =~ s/rm(\d+)/rm_gpr|bits$1/g;
358 $operands =~ s/(mmx|xmm|ymm)rm/rm_$1/g;
359 $operands =~ s/\=([0-9]+)/same_as|$1/g;
360 if ($operands eq 'void') {
361 @ops = ();
362 } else {
363 @ops = split(/\,/, $operands);
365 $num = scalar(@ops);
366 while (scalar(@ops) < $MAX_OPERANDS) {
367 push(@ops, '0');
369 $operands = join(',', @ops);
370 $operands =~ tr/a-z/A-Z/;
372 # format the flags
373 $flags =~ s/,/|IF_/g;
374 $flags =~ s/(\|IF_ND|IF_ND\|)//, $nd = 1 if $flags =~ /IF_ND/;
375 $flags = "IF_" . $flags;
377 @bytecode = (decodify($codes), 0);
378 push(@bytecode_list, [@bytecode]);
379 $codes = hexstr(@bytecode);
380 count_bytecodes(@bytecode);
382 ("{I_$opcode, $num, {$operands}, \@\@CODES-$codes\@\@, $flags},", $nd);
386 # Look for @@CODES-xxx@@ sequences and replace them with the appropriate
387 # offset into nasm_bytecodes
389 sub codesubst($) {
390 my($s) = @_;
391 my $n;
393 while ($s =~ /\@\@CODES-([0-9A-F]+)\@\@/) {
394 my $pos = $bytecode_pos{$1};
395 if (!defined($pos)) {
396 die "$fname: no position assigned to byte code $1\n";
398 $s = $` . "nasm_bytecodes+${pos}" . "$'";
400 return $s;
403 sub addprefix ($@) {
404 my ($prefix, @list) = @_;
405 my $x;
406 my @l = ();
408 foreach $x (@list) {
409 push(@l, sprintf("%s%02X", $prefix, $x));
412 return @l;
416 # Turn a code string into a sequence of bytes
418 sub decodify($) {
419 # Although these are C-syntax strings, by convention they should have
420 # only octal escapes (for directives) and hexadecimal escapes
421 # (for verbatim bytes)
422 my($codestr) = @_;
424 if ($codestr =~ /^\s*\[([^\]]*)\]\s*$/) {
425 return byte_code_compile($1);
428 my $c = $codestr;
429 my @codes = ();
431 while ($c ne '') {
432 if ($c =~ /^\\x([0-9a-f]+)(.*)$/i) {
433 push(@codes, hex $1);
434 $c = $2;
435 next;
436 } elsif ($c =~ /^\\([0-7]{1,3})(.*)$/) {
437 push(@codes, oct $1);
438 $c = $2;
439 next;
440 } else {
441 die "$fname: unknown code format in \"$codestr\"\n";
445 return @codes;
448 # Turn a numeric list into a hex string
449 sub hexstr(@) {
450 my $s = '';
451 my $c;
453 foreach $c (@_) {
454 $s .= sprintf("%02X", $c);
456 return $s;
459 # Here we determine the range of possible starting bytes for a given
460 # instruction. We need only consider the codes:
461 # \1 \2 \3 mean literal bytes, of course
462 # \4 \5 \6 \7 mean PUSH/POP of segment registers: special case
463 # \1[0123] mean byte plus register value
464 # \330 means byte plus condition code
465 # \0 or \340 mean give up and return empty set
466 # \17[234] skip is4 control byte
467 # \26x \270 skip VEX control bytes
468 sub startseq($) {
469 my ($codestr) = @_;
470 my $word, @range;
471 my @codes = ();
472 my $c = $codestr;
473 my $c0, $c1, $i;
474 my $prefix = '';
476 @codes = decodify($codestr);
478 while ($c0 = shift(@codes)) {
479 $c1 = $codes[0];
480 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
481 # Fixed byte string
482 my $fbs = $prefix;
483 while (1) {
484 if ($c0 == 01 || $c0 == 02 || $c0 == 03) {
485 while ($c0--) {
486 $fbs .= sprintf("%02X", shift(@codes));
488 } else {
489 last;
491 $c0 = shift(@codes);
494 foreach $pfx (@disasm_prefixes) {
495 if (substr($fbs, 0, length($pfx)) eq $pfx) {
496 $prefix = $pfx;
497 $fbs = substr($fbs, length($pfx));
498 last;
502 if ($fbs ne '') {
503 return ($prefix.substr($fbs,0,2));
506 unshift(@codes, $c0);
507 } elsif ($c0 >= 010 && $c0 <= 013) {
508 return addprefix($prefix, $c1..($c1+7));
509 } elsif (($c0 & ~013) == 0144) {
510 return addprefix($prefix, $c1, $c1|2);
511 } elsif ($c0 == 0330) {
512 return addprefix($prefix, $c1..($c1+15));
513 } elsif ($c0 == 0 || $c0 == 0340) {
514 return $prefix;
515 } elsif ($c0 == 0344) {
516 return addprefix($prefix, 0x06, 0x0E, 0x16, 0x1E);
517 } elsif ($c0 == 0345) {
518 return addprefix($prefix, 0x07, 0x17, 0x1F);
519 } elsif ($c0 == 0346) {
520 return addprefix($prefix, 0xA0, 0xA8);
521 } elsif ($c0 == 0347) {
522 return addprefix($prefix, 0xA1, 0xA9);
523 } elsif (($c0 & ~3) == 0260 || $c0 == 0270) {
524 my $m,$wlp,$vxp;
525 $m = shift(@codes);
526 $wlp = shift(@codes);
527 $prefix .= sprintf('VEX%02X%01X', $m, $wlp & 7);
528 } elsif ($c0 >= 0172 && $c0 <= 174) {
529 shift(@codes); # Skip is4 control byte
530 } else {
531 # We really need to be able to distinguish "forbidden"
532 # and "ignorable" codes here
535 return $prefix;
539 # This function takes a series of byte codes in a format which is more
540 # typical of the Intel documentation, and encode it.
542 # The format looks like:
544 # [operands: opcodes]
546 # The operands word lists the order of the operands:
548 # r = register field in the modr/m
549 # m = modr/m
550 # v = VEX "v" field
551 # d = DREX "dst" field
552 # i = immediate
553 # s = register field of is4/imz2 field
554 # - = implicit (unencoded) operand
556 # For an operand that should be filled into more than one field,
557 # enter it as e.g. "r+v".
559 sub byte_code_compile($) {
560 my($str) = @_;
561 my $opr;
562 my $opc;
563 my @codes = ();
564 my $litix = undef;
565 my %oppos = ();
566 my $i;
567 my $op, $oq;
569 unless ($str =~ /^(([^\s:]*)\:|)\s*(.*\S)\s*$/) {
570 die "$fname: $line: cannot parse: [$str]\n";
572 $opr = "\L$2";
573 $opc = "\L$3";
575 my $op = 0;
576 for ($i = 0; $i < length($opr); $i++) {
577 my $c = substr($opr,$i,1);
578 if ($c eq '+') {
579 $op--;
580 } else {
581 $oppos{$c} = $op++;
585 $prefix_ok = 1;
586 foreach $op (split(/\s*(?:\s|(?=[\/\\]))/, $opc)) {
587 if ($op eq 'o16') {
588 push(@codes, 0320);
589 } elsif ($op eq 'o32') {
590 push(@codes, 0321);
591 } elsif ($op eq 'o64') { # 64-bit operand size requiring REX.W
592 push(@codes, 0324);
593 } elsif ($op eq 'o64nw') { # Implied 64-bit operand size (no REX.W)
594 push(@codes, 0323);
595 } elsif ($op eq 'a16') {
596 push(@codes, 0310);
597 } elsif ($op eq 'a32') {
598 push(@codes, 0311);
599 } elsif ($op eq 'a64') {
600 push(@codes, 0313);
601 } elsif ($op eq '!osp') {
602 push(@codes, 0364);
603 } elsif ($op eq '!asp') {
604 push(@codes, 0365);
605 } elsif ($op eq 'rex.l') {
606 push(@codes, 0334);
607 } elsif ($op eq 'repe') {
608 push(@codes, 0335);
609 } elsif ($prefix_ok && $op =~ /^(66|f2|f3|np)$/) {
610 # 66/F2/F3 prefix used as an opcode extension, or np = no prefix
611 if ($op eq '66') {
612 push(@codes, 0361);
613 } elsif ($op eq 'f2') {
614 push(@codes, 0362);
615 } elsif ($op eq 'f3') {
616 push(@codes, 0363);
617 } else {
618 push(@codes, 0360);
620 } elsif ($op =~ /^[0-9a-f]{2}$/) {
621 if (defined($litix) && $litix+$codes[$litix]+1 == scalar @codes) {
622 $codes[$litix]++;
623 push(@codes, hex $op);
624 } else {
625 $litix = scalar(@codes);
626 push(@codes, 01, hex $op);
628 $prefix_ok = 0;
629 } elsif ($op eq '/r') {
630 if (!defined($oppos{'r'}) || !defined($oppos{'m'})) {
631 die "$fname: $line: $op requires r and m operands\n";
633 push(@codes, 0100 + ($oppos{'m'} << 3) + $oppos{'r'});
634 $prefix_ok = 0;
635 } elsif ($op =~ m:^/([0-7])$:) {
636 if (!defined($oppos{'m'})) {
637 die "$fname: $line: $op requires m operand\n";
639 push(@codes, 0200 + ($oppos{'m'} << 3) + $1);
640 $prefix_ok = 0;
641 } elsif ($op =~ /^vex(|\..*)$/) {
642 my ($m,$w,$l,$p) = (undef,2,undef,0);
643 my $has_nds = 0;
644 foreach $oq (split(/\./, $op)) {
645 if ($oq eq 'vex') {
646 # prefix
647 } elsif ($oq eq '128' || $oq eq 'l0') {
648 $l = 0;
649 } elsif ($oq eq '256' || $oq eq 'l1') {
650 $l = 1;
651 } elsif ($oq eq 'w0') {
652 $w = 0;
653 } elsif ($oq eq 'w1') {
654 $w = 1;
655 } elsif ($oq eq 'wx') {
656 $w = 2;
657 } elsif ($oq eq 'ww') {
658 $w = 3;
659 } elsif ($oq eq '66') {
660 $p = 1;
661 } elsif ($oq eq 'f3') {
662 $p = 2;
663 } elsif ($oq eq 'f2') {
664 $p = 3;
665 } elsif ($oq eq '0f') {
666 $m = 1;
667 } elsif ($oq eq '0f38') {
668 $m = 2;
669 } elsif ($oq eq '0f3a') {
670 $m = 3;
671 } elsif ($oq =~ /^m([0-9]+)$/) {
672 $m = $1+0;
673 } elsif ($oq eq 'nds' || $oq eq 'ndd') {
674 if (!defined($oppos{'v'})) {
675 die "$fname: $line: vex.$oq without 'v' operand\n";
677 $has_nds = 1;
678 } else {
679 die "$fname: $line: undefined VEX subcode: $oq\n";
682 if (!defined($m) || !defined($w) || !defined($l) || !defined($p)) {
683 die "$fname: $line: missing fields in VEX specification\n";
685 if (defined($oppos{'v'}) && !$has_nds) {
686 die "$fname: $line: 'v' operand without vex.nds or vex.ndd\n";
688 push(@codes, defined($oppos{'v'}) ? 0260+$oppos{'v'} : 0270,
689 $m, ($w << 3)+($l << 2)+$p);
690 $prefix_ok = 0;
691 } elsif ($op =~ /^\/drex([01])$/) {
692 my $oc0 = $1;
693 if (!defined($oppos{'d'})) {
694 die "$fname: $line: DREX without a 'd' operand\n";
696 # Note the use of *unshift* here, as opposed to *push*.
697 # This is because NASM want this byte code at the start of
698 # the instruction sequence, but the AMD documentation puts
699 # this at (roughly) the position of the drex byte itself.
700 # This allows us to match the AMD documentation and still
701 # do the right thing.
702 unshift(@codes, 0160+$oppos{'d'}+($oc0 ? 4 : 0));
703 } elsif ($op =~ /^(ib\,s|ib|ibx|ib\,w|iw|iwd|id|idx|iwdq|rel|rel8|rel16|rel32|iq|seg|ibw|ibd|ibd,s)$/) {
704 if (!defined($oppos{'i'})) {
705 die "$fname: $line: $op without 'i' operand\n";
707 if ($op eq 'ib,s') { # Signed imm8
708 push(@codes, 014+$oppos{'i'});
709 } elsif ($op eq 'ib') { # imm8
710 push(@codes, 020+$oppos{'i'});
711 } elsif ($op eq 'ib,u') { # Unsigned imm8
712 push(@codes, 024+$oppos{'i'});
713 } elsif ($op eq 'iw') { # imm16
714 push(@codes, 030+$oppos{'i'});
715 } elsif ($op eq 'ibx') { # imm8 sign-extended to opsize
716 push(@codes, 0274+$oppos{'i'});
717 } elsif ($op eq 'iwd') { # imm16 or imm32, depending on opsize
718 push(@codes, 034+$oppos{'i'});
719 } elsif ($op eq 'id') { # imm32
720 push(@codes, 040+$oppos{'i'});
721 } elsif ($op eq 'idx') { # imm32 extended to 64 bits
722 push(@codes, 0254+$oppos{'i'});
723 } elsif ($op eq 'iwdq') { # imm16/32/64, depending on opsize
724 push(@codes, 044+$oppos{'i'});
725 } elsif ($op eq 'rel8') {
726 push(@codes, 050+$oppos{'i'});
727 } elsif ($op eq 'iq') {
728 push(@codes, 054+$oppos{'i'});
729 } elsif ($op eq 'rel16') {
730 push(@codes, 060+$oppos{'i'});
731 } elsif ($op eq 'rel') { # 16 or 32 bit relative operand
732 push(@codes, 064+$oppos{'i'});
733 } elsif ($op eq 'rel32') {
734 push(@codes, 070+$oppos{'i'});
735 } elsif ($op eq 'seg') {
736 push(@codes, 074+$oppos{'i'});
737 } elsif ($op eq 'ibw') { # imm16 that can be bytified
738 if (!defined($s_pos)) {
739 die "$fname: $line: $op without a +s byte\n";
741 $codes[$s_pos] += 0144;
742 push(@codes, 0140+$oppos{'i'});
743 } elsif ($op eq 'ibd') { # imm32 that can be bytified
744 if (!defined($s_pos)) {
745 die "$fname: $line: $op without a +s byte\n";
747 $codes[$s_pos] += 0154;
748 push(@codes, 0150+$oppos{'i'});
749 } elsif ($op eq 'ibd,s') {
750 # imm32 that can be bytified, sign extended to 64 bits
751 if (!defined($s_pos)) {
752 die "$fname: $line: $op without a +s byte\n";
754 $codes[$s_pos] += 0154;
755 push(@codes, 0250+$oppos{'i'});
757 $prefix_ok = 0;
758 } elsif ($op eq '/is4') {
759 if (!defined($oppos{'s'})) {
760 die "$fname: $line: $op without 's' operand\n";
762 if (defined($oppos{'i'})) {
763 push(@codes, 0172, ($oppos{'s'} << 3)+$oppos{'i'});
764 } else {
765 push(@codes, 0174, $oppos{'s'});
767 $prefix_ok = 0;
768 } elsif ($op =~ /^\/is4\=([0-9]+)$/) {
769 my $imm = $1;
770 if (!defined($oppos{'s'})) {
771 die "$fname: $line: $op without 's' operand\n";
773 if ($imm < 0 || $imm > 15) {
774 die "$fname: $line: invalid imm4 value for $op: $imm\n";
776 push(@codes, 0173, ($oppos{'s'} << 4) + $imm);
777 $prefix_ok = 0;
778 } elsif ($op =~ /^([0-9a-f]{2})\+s$/) {
779 if (!defined($oppos{'i'})) {
780 die "$fname: $line: $op without 'i' operand\n";
782 $s_pos = scalar @codes;
783 push(@codes, $oppos{'i'}, hex $1);
784 $prefix_ok = 0;
785 } elsif ($op =~ /^([0-9a-f]{2})\+c$/) {
786 push(@codes, 0330, hex $1);
787 $prefix_ok = 0;
788 } elsif ($op =~ /^\\([0-7]+|x[0-9a-f]{2})$/) {
789 # Escape to enter literal bytecodes
790 push(@codes, oct $1);
791 } else {
792 die "$fname: $line: unknown operation: $op\n";
796 return @codes;