use B::Disassembler qw(disassemble_fh);
use FileHandle;

my $fh;
if (@ARGV == 0) {
    $fh = \*STDIN;
} elsif (@ARGV == 1) {
    $fh = new FileHandle "<$ARGV[0]";
} else {
    die "Usage: disassemble [filename]\n";
}

sub print_insn {
    my ($insn, $arg, $comment) = @_;
    if (defined($arg)) {
	if ($insn eq 'newopx') {
	  my $type = $arg >> 7;
	  my $size = $arg - ($type << 7);
	  $arg = sprintf("%s \t# size:%d, type:%d %s", $arg, $size, $type, $comment);
	} else {
 	  $arg .= "\t# ".$comment if $comment;
	}
	printf "%s %s\n", $insn, $arg;
    } else {
	$insn .= "\t# ".$comment if $comment;
	print $insn, "\n";
    }
}

disassemble_fh($fh, \&print_insn);
