| 1 | package CPU::Z80::Disassembler::Label; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | CPU::Z80::Disassembler::Label - Label used in the disassembled program | ||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 12 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 12 | ▶ | use CPU::Z80::Disassembler::Format; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 12 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | $label = CPU::Z80::Disassembler::Label->new($addr, $name, @from_addr); | ||||||||||||||||||||||||||||||||
| 27 | $label->add_refer(@from_addr); | ||||||||||||||||||||||||||||||||
| 28 | my @refer = $label->refer_from; | ||||||||||||||||||||||||||||||||
| 29 | print $label->label_string; | ||||||||||||||||||||||||||||||||
| 30 | print $label->equ_string; | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | Represents one label in the disassembled program. The label contains a name, an | ||||||||||||||||||||||||||||||||
| 35 | address and a list of addresses of opcodes that refer to it. | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||
| 39 | =head2 new | ||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | Creates a new object. | ||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||
| 43 | =head2 name | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | Gets/sets the label name. | ||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | =head2 comment | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | Gets/sets the comment to add to the definition of the label. | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | =head2 addr | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | Gets the label address. The address cannot be modified. | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 58 | 12 | ▶ | use base 'Class::Accessor'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 59 | 12 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||||||
| 60 | 'name', # name | ||||||||||||||||||||||||||||||||
| 61 | 'comment', # comment to add to label when defining | ||||||||||||||||||||||||||||||||
| 62 | 'addr', # address | ||||||||||||||||||||||||||||||||
| 63 | '_refer', # hash of reference address | ||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | sub new { | ||||||||||||||||||||||||||||||||
| 67 | 2816 | ▶ | my($class, $addr, $name, @from_addr) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 68 | 2816 | ▶ | croak("invalid name".(defined($name) ? " '$name'" : "")) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $name and $name =~ /^[a-z_]\w*$/i
MC/DC100%2/2
defined $name and $name =~ /^[a-z_]\w*$/i
defined $name
$name =~ /^[a-z_]\w*$/i
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /^[a-z_]\w*$/i
| |||||||||||||||||||||||||||||||||
| 69 | unless defined($name) && $name =~ /^[a-z_]\w*$/i; | ||||||||||||||||||||||||||||||||
| 70 | 2814 | ▶ | croak("invalid address".(defined($addr) ? " '$addr'" : "")) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $addr and $addr =~ /^\d+$/
MC/DC100%2/2
defined $addr and $addr =~ /^\d+$/
defined $addr
$addr =~ /^\d+$/
Truth table100%3/3
A and B
A= defined $addrB= $addr =~ /^\d+$/
| |||||||||||||||||||||||||||||||||
| 71 | unless defined($addr) && $addr =~ /^\d+$/; | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 2812 | my $self = bless { name => $name, addr => $addr, | |||||||||||||||||||||||||||||||
| 74 | _refer => {}, | ||||||||||||||||||||||||||||||||
| 75 | }, $class; | ||||||||||||||||||||||||||||||||
| 76 | 2812 | ▶ | $self->add_refer(@from_addr) if @from_addr; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 2812 | return $self; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | =head2 add_refer | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | Add the given addresses as references to this label, i.e. places from where | ||||||||||||||||||||||||||||||||
| 84 | this label is used. | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 89 | sub add_refer { | ||||||||||||||||||||||||||||||||
| 90 | 4452 | ▶ | my($self, @from_addr) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
add_refer: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 91 | 4452 | $self->_refer->{$_}++ for (@from_addr); | |||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||
| 94 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | =head2 refer_from | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | Return the list of all addresses from which this label is used. | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 103 | sub refer_from { | ||||||||||||||||||||||||||||||||
| 104 | 11 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
refer_from: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 105 | 11 | return sort {$a <=> $b} keys %{$self->_refer}; | |||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||
| 110 | =head2 label_string | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | Returns the string to be used in an assembly file to define this label | ||||||||||||||||||||||||||||||||
| 113 | at the current location counter: | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | LABEL: ; COMMENT | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 120 | sub label_string { | ||||||||||||||||||||||||||||||||
| 121 | 2592 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
label_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 122 | 2592 | my $opcode = $self->name.":"; | |||||||||||||||||||||||||||||||
| 123 | 2592 | return $self->_format_comment($opcode)."\n"; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | =head2 equ_string | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | Returns the string to be used in an assembly file to define this label | ||||||||||||||||||||||||||||||||
| 130 | as a constant: | ||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | LABEL equ ADDR ; COMMENT | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | =cut | ||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||
| 136 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 137 | sub equ_string { | ||||||||||||||||||||||||||||||||
| 138 | 173 | ▶ | my($self, $field_width) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
equ_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 139 | 173 | ▶ | $field_width ||= 12; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$field_width ||= 12
MC/DC100%1/1
$field_width ||= 12
$field_width
Truth table100%2/2
A
A= $field_width
| |||||||||||||||||||||||||||||||||
| 140 | 173 | my $opcode = sprintf("%-*s equ %s", $field_width-1, $self->name, | |||||||||||||||||||||||||||||||
| 141 | format_hex4($self->addr)); | ||||||||||||||||||||||||||||||||
| 142 | 173 | return $self->_format_comment($opcode)."\n"; | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | sub _format_comment { | ||||||||||||||||||||||||||||||||
| 146 | 2765 | ▶ | my($self, $opcode) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_format_comment: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | 2765 | my $comment = $self->comment; | |||||||||||||||||||||||||||||||
| 149 | 2765 | ▶ | if (defined $comment) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 150 | 176 | $comment =~ s/\n/ "\n" . " " x 32 . "; " /ge; # multi-line comment | |||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 2765 | ▶ | return !defined($comment) ? | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 154 | $opcode : | ||||||||||||||||||||||||||||||||
| 155 | length($opcode) >= 32 ? | ||||||||||||||||||||||||||||||||
| 156 | $opcode . "\n" . " " x 32 . "; " . $comment : | ||||||||||||||||||||||||||||||||
| 157 | sprintf("%-32s; %s", $opcode, $comment); | ||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||
| 162 | =head1 BUGS, FEEDBACK, AUTHORS, COPYRIGHT and LICENCE | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | See L<CPU::Z80::Disassembler|CPU::Z80::Disassembler>. | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | =cut | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | 12 | 1; | |||||||||||||||||||||||||||||||