| 1 | package CPU::Z80::Disassembler::Labels; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | CPU::Z80::Disassembler::Labels - All labels used in the disassembled program | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use Bit::Vector; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | use CPU::Z80::Disassembler::Label; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | ▶ | use CPU::Z80::Disassembler::Format; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 11 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 SYNOPSYS | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | $labels = CPU::Z80::Disassembler::Labels->new(); | ||||||||||||||||||||||||||||||||||||||||||
| 29 | $labels->add($addr, $name, $from_addr); | ||||||||||||||||||||||||||||||||||||||||||
| 30 | $found = $labels->search_addr($addr); | ||||||||||||||||||||||||||||||||||||||||||
| 31 | $found = $labels->search_name($name); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | @labels = $labels->search_all; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | print $labels->max_length; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | $label = $labels->next_label($addr); | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | Contains an indexed list of all L<CPU::Z80::Diassembler::Label> labels | ||||||||||||||||||||||||||||||||||||||||||
| 39 | in the disassembled program. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | Each label is created by the add() | ||||||||||||||||||||||||||||||||||||||||||
| 42 | method, that simultaneously prepares the indexes for a fast search. There are also | ||||||||||||||||||||||||||||||||||||||||||
| 43 | methods to search for labels at a given address of with a given name. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | This module assumes that the address of a label does not change after being defined, | ||||||||||||||||||||||||||||||||||||||||||
| 46 | i.e. there is never the need to reindex all labels. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | Creates a new empty object. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 11 | ▶ | use base 'Class::Accessor'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 11 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||||||||||||||||
| 59 | '_by_addr', # array of labels by address | ||||||||||||||||||||||||||||||||||||||||||
| 60 | '_by_name', # hash of labels by name | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 'max_length', # max length of all defined labels | ||||||||||||||||||||||||||||||||||||||||||
| 62 | '_has_label', # Bit::Vector, one bit per address, 1 if label | ||||||||||||||||||||||||||||||||||||||||||
| 63 | # exists at that address | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 22 | ▶ | my($class) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 22 | my $has_label = Bit::Vector->new(0x10000); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 22 | return bless { _by_addr => [], | |||||||||||||||||||||||||||||||||||||||||
| 70 | _by_name => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 71 | max_length => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 72 | _has_label => $has_label, | ||||||||||||||||||||||||||||||||||||||||||
| 73 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head2 add | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Creates and adds a new label to the indexes. If the same name and address as an | ||||||||||||||||||||||||||||||||||||||||||
| 80 | existing label is given then the $from_addr is updated. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | If the name is not given, creates a temporary label of the form L_HHHH. | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | It is an error to add a label already added with a different address. | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =head2 max_length | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | Length of the longest label name of all defined labels. This is updated when | ||||||||||||||||||||||||||||||||||||||||||
| 89 | a label is added to the index, and can be used for formating label lists in columns. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 94 | sub add { | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 7145 | ▶ | my($self, $addr, $name, $from_addr) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 7145 | my $temp_name = sprintf("L_%04X", $addr); | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # check for dupplicate names | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 7145 | my $label; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 7145 | ▶ | if ( defined($name) && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $name and defined($label = $self->_by_name->{$name}) and $label->addr != $addr
defined $name and defined($label = $self->_by_name->{$name})
MC/DC100%3/3
defined $name and defined($label = $self->_by_name->{$name}) and $label->addr != $addr
defined $name
defined($label = $self->_by_name->{$name})
$label->addr != $addr
Truth table100%4/4
A and B and C
A= defined $nameB= defined($label = $self->_by_name->{$name})C= $label->addr != $addr
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | defined($label = $self->_by_name->{$name}) && | ||||||||||||||||||||||||||||||||||||||||||
| 103 | $label->addr != $addr | ||||||||||||||||||||||||||||||||||||||||||
| 104 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | croak("Label '$name' with addresses ".format_hex4($label->addr). | |||||||||||||||||||||||||||||||||||||||||
| 106 | " and ".format_hex4($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # check for dupplicate address | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 7144 | ▶ | if (! defined($label = $self->_by_addr->[$addr])) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 2809 | ▶ | $label = CPU::Z80::Disassembler::Label | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$name || $temp_name
MC/DC0%0/2
$name || $temp_name
$name
$temp_name
Truth table66%2/3
A || B
A= $nameB= $temp_name
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | ->new($addr, $name || $temp_name); | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # create index | ||||||||||||||||||||||||||||||||||||||||||
| 116 | $self->_by_addr->[$addr] = | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 2809 | $self->_by_name->{$label->name} = | |||||||||||||||||||||||||||||||||||||||||
| 118 | $label; | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 2809 | $self->_has_label->Bit_On($addr); | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # label at that address exists | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 4335 | ▶ | if ( defined($name) && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
defined $name and $label->name eq $temp_name
MC/DC100%2/2
defined $name and $label->name eq $temp_name
defined $name
$label->name eq $temp_name
Truth table100%3/3
A and B
A= defined $nameB= $label->name eq $temp_name
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | $label->name eq $temp_name) { | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | # temp label was given a name | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 1785 | $label->name($name); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 1785 | delete $self->_by_name->{$temp_name}; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 1785 | $self->_by_name->{$name} = $label; | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | ▶ | elsif ( defined($name) && | |||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
defined $name and $label->name ne $name
MC/DC100%2/2
defined $name and $label->name ne $name
defined $name
$label->name ne $name
Truth table100%3/3
A and B
A= defined $nameB= $label->name ne $name
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | $label->name ne $name) { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | # label renamed | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | croak("Labels '".$label->name."' and '$name' at the same address ". | |||||||||||||||||||||||||||||||||||||||||
| 137 | format_hex4($addr)); | ||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # OK, same address and name | ||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | # define max length | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 7143 | my $length = length($label->name); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 7143 | ▶ | $self->max_length($length) if $length > $self->max_length; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | # add references | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 7143 | ▶ | $label->add_refer($from_addr) if defined $from_addr; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | 7143 | return $label; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =head2 search_addr | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Return the label object defined at the given address, undef if none. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 162 | sub search_addr { | ||||||||||||||||||||||||||||||||||||||||||
| 163 | 25111 | ▶ | my($self, $addr) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_addr: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 25111 | return $self->_by_addr->[$addr]; | |||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =head2 search_name | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | Return the label object with the given name, undef if none. | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 176 | sub search_name { | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 30 | ▶ | my($self, $name) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 30 | return $self->_by_name->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =head2 search_all | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | Return all the defined label objects. | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 190 | sub search_all { | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 29 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
search_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | 29 | return sort {$a->name cmp $b->name} values %{$self->_by_name}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | =head2 next_label | ||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | Return the first label defined on the given address or after. If no address | ||||||||||||||||||||||||||||||||||||||||||
| 200 | is given, returns the first defined label. | ||||||||||||||||||||||||||||||||||||||||||
| 201 | Returns undef if there is no label on the address or after. | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | This can be used to find the next label after the current instruction. | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 208 | sub next_label { | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 1066 | ▶ | my($self, $addr) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
next_label: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 210 | 1066 | ▶ | $addr ||= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$addr ||= 0
MC/DC100%1/1
$addr ||= 0
$addr
Truth table100%2/2
A
A= $addr
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 1066 | ▶ | if (my($min,$max) = $self->_has_label->Interval_Scan_inc($addr)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 1059 | return $self->search_addr($min); | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 7 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||||||||
| 219 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | =head1 BUGS, FEEDBACK, AUTHORS, COPYRIGHT and LICENCE | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | See L<CPU::Z80::Disassembler|CPU::Z80::Disassembler>. | ||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||
| 225 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||