| 1 | # $Id$ | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | package CPU::Z80::Assembler::Program; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | CPU::Z80::Assembler::Program - Represents one assembly program | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 36 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 36 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 36 | our $VERSION = '2.24'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 36 | ▶ | use CPU::Z80::Assembler::Parser; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 36 | ▶ | use CPU::Z80::Assembler::Segment; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 36 | ▶ | use CPU::Z80::Assembler::Expr; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 36 | ▶ | use CPU::Z80::Assembler::Opcode; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 36 | ▶ | use Data::Dump 'dump'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 2796 | ▶ | my($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | bless [ | ||||||||||||||||||||||||||||||||||||||||||
| 30 | $args{_segment_id}, # index of the current segment | ||||||||||||||||||||||||||||||||||||||||||
| 31 | $args{_segment_map} || {}, # map segment name => index in child | ||||||||||||||||||||||||||||||||||||||||||
| 32 | $args{child} || [], # list of segments | ||||||||||||||||||||||||||||||||||||||||||
| 33 | $args{symbols} || {}, # map name => Node with evaluate() method | ||||||||||||||||||||||||||||||||||||||||||
| 34 | $args{macros} || {}, # list of defined macros | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 2796 | ▶ | ], $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%4/8
$args{'_segment_map'} || {}
$args{'child'} || []
$args{'symbols'} || {}
$args{'macros'} || {}
MC/DC0%0/1
$args{'_segment_map'} || {}
$args{'_segment_map'}
MC/DC0%0/1
$args{'child'} || []
$args{'child'}
MC/DC0%0/1
$args{'symbols'} || {}
$args{'symbols'}
MC/DC0%0/1
$args{'macros'} || {}
$args{'macros'}
Truth table50%1/2
A
A= $args{'_segment_map'}
Truth table50%1/2
A
A= $args{'child'}
Truth table50%1/2
A
A= $args{'symbols'}
Truth table50%1/2
A
A= $args{'macros'}
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 27395 | ▶ | sub _segment_id { defined($_[1]) ? $_[0][0] = $_[1] : $_[0][0] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_segment_id: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 5632 | ▶ | sub _segment_map { defined($_[1]) ? $_[0][1] = $_[1] : $_[0][1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_segment_map: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 98628 | ▶ | sub child { defined($_[1]) ? $_[0][2] = $_[1] : $_[0][2] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
child: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 5898 | ▶ | sub symbols { defined($_[1]) ? $_[0][3] = $_[1] : $_[0][3] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
symbols: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 151 | ▶ | sub macros { defined($_[1]) ? $_[0][4] = $_[1] : $_[0][4] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
macros: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | use CPU::Z80::Assembler::Program; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | my $program = CPU::Z80::Assembler::Program->new( | ||||||||||||||||||||||||||||||||||||||||||
| 49 | symbols => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 50 | macros => {}); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | $program->parse($input); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | $segment = $program->segment; | ||||||||||||||||||||||||||||||||||||||||||
| 53 | $segment = $program->segment("CODE"); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | $segment = $program->split_segment; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | $program->add_opcodes(@opcodes); | ||||||||||||||||||||||||||||||||||||||||||
| 56 | $program->add_label($name, $line); | ||||||||||||||||||||||||||||||||||||||||||
| 57 | $program->org($address); | ||||||||||||||||||||||||||||||||||||||||||
| 58 | $bytes = $program->bytes; | ||||||||||||||||||||||||||||||||||||||||||
| 59 | $list_output = CPU::Z80::Assembler::List->new(input => \@input, output => \*STDOUT); | ||||||||||||||||||||||||||||||||||||||||||
| 60 | $bytes = $program->bytes($list_output); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | This module defines the class that represents one assembly program composed of | ||||||||||||||||||||||||||||||||||||||||||
| 65 | L<CPU::Z80::Assembler::Segment|CPU::Z80::Assembler::Segment>. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | =head1 EXPORTS | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Nothing. | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | Creates a new object, see L<Class::Struct|Class::Struct>. | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =head2 child | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Each child is one L<CPU::Z80::Assembler::Segment|CPU::Z80::Assembler::Segment> object, in the order found in the | ||||||||||||||||||||||||||||||||||||||||||
| 80 | program. | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =head2 symbols | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | Hash of all symbols defined in the program. The key is the symbol name, and | ||||||||||||||||||||||||||||||||||||||||||
| 85 | the value is either a scalar for a constant, a L<CPU::Z80::Assembler::Expr|CPU::Z80::Assembler::Expr> for | ||||||||||||||||||||||||||||||||||||||||||
| 86 | an expression, or a L<CPU::Z80::Assembler::Opcode|CPU::Z80::Assembler::Opcode> for a label. | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | =head2 macros | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | Hash of macro names to L<CPU::Z80::Assembler::Macro|CPU::Z80::Assembler::Macro> objects for all defined macros. | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =head2 parse | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | $program->parse($input); | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | Parse the assembly program and collect the opcodes into the object. $input is | ||||||||||||||||||||||||||||||||||||||||||
| 101 | a stream of tokens as retrieved by L<CPU::Z80::Assembler|CPU::Z80::Assembler> | ||||||||||||||||||||||||||||||||||||||||||
| 102 | C<z80lexer>. | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 2742 | ▶ | sub parse { my($self, $input) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2742 | z80parser($input, $self); | |||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =head2 segment | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | Get/Set the current segment. The current segment is the one where new opcodes | ||||||||||||||||||||||||||||||||||||||||||
| 117 | are added. | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | When called without arguments returns a L<CPU::Z80::Assembler::Segment|CPU::Z80::Assembler::Segment> object | ||||||||||||||||||||||||||||||||||||||||||
| 120 | of the current segment. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | When called with a $name, it sets the segment with the given name as current. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | If no such segment exists, a new segment with that name is appended to the list | ||||||||||||||||||||||||||||||||||||||||||
| 124 | and set current. | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | sub segment { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 27373 | ▶ | my($self, $name) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
segment: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | 27373 | ▶ | if (defined($name) || @{$self->child} == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $name or @{$self->child;} == 0
MC/DC100%2/2
defined $name or @{$self->child;} == 0
defined $name
@{$self->child;} == 0
Truth table100%3/3
A or B
A= defined $nameB= @{$self->child;} == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | # set or get but still no segments -> create | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 2812 | ▶ | $name = "_" unless defined($name); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 2812 | my $id = $self->_segment_map->{$name}; | |||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 2812 | ▶ | if (! defined $id) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | # new segment | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 2787 | $id = @{$self->child}; # index of new segment | |||||||||||||||||||||||||||||||||||||||||
| 142 | 2787 | my $segment = CPU::Z80::Assembler::Segment->new(name => $name); | |||||||||||||||||||||||||||||||||||||||||
| 143 | 2787 | push(@{$self->child}, $segment); | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 2787 | $self->_segment_map->{$name} = $id; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # segment exists | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 2812 | $self->_segment_id( $id ); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 2812 | return $self->child->[$id]; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 152 | # get | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 24561 | return $self->child->[ $self->_segment_id ]; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 159 | # creates a new name based on the given name, with a suffix number to make it | ||||||||||||||||||||||||||||||||||||||||||
| 160 | # unique | ||||||||||||||||||||||||||||||||||||||||||
| 161 | sub _build_name { | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 11 | ▶ | my($self, $name) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_name: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | 11 | ▶ | while (exists $self->_segment_map->{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 11 | ▶ | $name =~ s/(\d*)$/ ($1 || 0) + 1/e; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$1 || 0
MC/DC0%0/1
$1 || 0
$1
Truth table50%1/2
A
A= $1
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||||||||||
| 167 | 11 | return $name; | |||||||||||||||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =head2 split_segment | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | Splits the current segment at the current position, creating a new segment, | ||||||||||||||||||||||||||||||||||||||||||
| 175 | inserting it just after the current one and setting it as current. | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | Returns the new current segment. | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | As a special case, if the current is empty, then nothing is done. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | This is used to split one segment in two after a second ORG statement. | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | sub split_segment { | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 2634 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
split_segment: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | return $self->segment | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 2634 | ▶ | unless @{$self->segment->child}; # if empty, already split | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | # segment id | ||||||||||||||||||||||||||||||||||||||||||
| 194 | 11 | my $old_id = $self->_segment_id; | |||||||||||||||||||||||||||||||||||||||||
| 195 | 11 | my $new_id = $old_id + 1; | |||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | # build a new name | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 11 | my $old_name = $self->segment->name; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 11 | my $new_name = $self->_build_name( $old_name ); | |||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||
| 201 | # make space in the index map for a new item | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 11 | my $segment_map = $self->_segment_map; | |||||||||||||||||||||||||||||||||||||||||
| 203 | 11 | for (keys %$segment_map) { | |||||||||||||||||||||||||||||||||||||||||
| 204 | 12 | ▶ | $segment_map->{$_}++ if $segment_map->{$_} >= $new_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 11 | $segment_map->{$new_name} = $new_id; | |||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | # create the segment and insert it in the child list | ||||||||||||||||||||||||||||||||||||||||||
| 209 | 11 | my $new_segment = CPU::Z80::Assembler::Segment->new(name => $new_name); | |||||||||||||||||||||||||||||||||||||||||
| 210 | 11 | splice( @{$self->child}, $new_id, 0, $new_segment ); | |||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 11 | $self->_segment_id( $new_id ); | |||||||||||||||||||||||||||||||||||||||||
| 213 | 11 | return $self->child->[ $new_id ]; | |||||||||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =head2 add_opcodes | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | Adds the opcodes to the current segment. | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | sub add_opcodes { | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 22052 | ▶ | my($self, @opcodes) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_opcodes: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | 22052 | ▶ | $self->segment->add(@opcodes) if @opcodes; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | =head2 add_label | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | Add a new label at the current position with given name and line. The line | ||||||||||||||||||||||||||||||||||||||||||
| 237 | is used for error messages and assembly listing. | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | It is an error to add a label twice with the same name. | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | sub add_label { | ||||||||||||||||||||||||||||||||||||||||||
| 246 | 101 | ▶ | my($self, $name, $line) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_label: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||||||||||||||||
| 248 | 101 | my $opcode = CPU::Z80::Assembler::Opcode->new( | |||||||||||||||||||||||||||||||||||||||||
| 249 | child => [], | ||||||||||||||||||||||||||||||||||||||||||
| 250 | line => $line); | ||||||||||||||||||||||||||||||||||||||||||
| 251 | 101 | $self->add_opcodes($opcode); | |||||||||||||||||||||||||||||||||||||||||
| 252 | 101 | ▶ | if (exists $self->symbols->{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | $line->error("duplicate label definition"); | |||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | die "not reached"; | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | 100 | $self->symbols->{$name} = $opcode; | |||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||
| 259 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | =head2 org | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | Splits the current segment with split_segment() and sets the start address | ||||||||||||||||||||||||||||||||||||||||||
| 264 | of the new current segment. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | sub org { | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 2631 | ▶ | my($self, $address) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
org: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | 2631 | $self->split_segment->address($address); | |||||||||||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||
| 276 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # Allocate addresses for all child segments, starting at | ||||||||||||||||||||||||||||||||||||||||||
| 278 | # the first segment's C<address> (defined by a "org" instruction), or at 0. | ||||||||||||||||||||||||||||||||||||||||||
| 279 | # Returns the first free address after the end of the last segment. | ||||||||||||||||||||||||||||||||||||||||||
| 280 | sub _locate { | ||||||||||||||||||||||||||||||||||||||||||
| 281 | 2821 | ▶ | my($self) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_locate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 2821 | my @jump_opcodes; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 2821 | $self->_locate_opcodes(0, \@jump_opcodes); # preliminary addresses, get list of jumps | |||||||||||||||||||||||||||||||||||||||||
| 285 | 2819 | $self->_check_short_jumps(\@jump_opcodes); # change short to long junps, as needed | |||||||||||||||||||||||||||||||||||||||||
| 286 | 2819 | $self->_locate_opcodes(1); # final addresses | |||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | sub _locate_opcodes { | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 5640 | ▶ | my($self, $final, $jump_opcodes) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_locate_opcodes: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | 5640 | ▶ | return unless @{$self->child}; # if empty, nothing to do | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | # define start address; only define segment address on final pass | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 5640 | my $first = $self->child->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 5640 | ▶ | my $address = defined($first->address) ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | $first->address : | ||||||||||||||||||||||||||||||||||||||||||
| 298 | $final ? | ||||||||||||||||||||||||||||||||||||||||||
| 299 | $first->address( 0 ) : | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 0; | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | 5640 | for my $segment_id (0 .. $#{$self->child}) { | |||||||||||||||||||||||||||||||||||||||||
| 303 | 5668 | my $segment = $self->child->[$segment_id]; | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | # define start | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 5668 | ▶ | if (defined($segment->address)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | # check for overlapping segments | ||||||||||||||||||||||||||||||||||||||||||
| 308 | 5505 | ▶ | if ($segment->address < $address) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 309 | 2 | $segment->line->error(sprintf("segments overlap, previous ends at ". | |||||||||||||||||||||||||||||||||||||||||
| 310 | "0x%04X, next starts at 0x%04X", | ||||||||||||||||||||||||||||||||||||||||||
| 311 | $address, $segment->address)); | ||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | die; # NOT REACHED | |||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||||
| 314 | # check for new address | ||||||||||||||||||||||||||||||||||||||||||
| 315 | elsif ($segment->address > $address) { | ||||||||||||||||||||||||||||||||||||||||||
| 316 | 14 | $address = $segment->address; | |||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 163 | ▶ | $segment->address( $address ) if $final; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | # locate the segment | ||||||||||||||||||||||||||||||||||||||||||
| 324 | 5666 | for my $opcode_id (0 .. $#{$segment->child}) { | |||||||||||||||||||||||||||||||||||||||||
| 325 | 47478 | my $opcode = $segment->child->[$opcode_id]; | |||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | 47478 | $opcode->address($address); # define opcode address | |||||||||||||||||||||||||||||||||||||||||
| 328 | 47478 | ▶ | if ($jump_opcodes && $opcode->can('short_jump_dist')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$jump_opcodes and $opcode->can("short_jump_dist")
MC/DC100%2/2
$jump_opcodes and $opcode->can("short_jump_dist")
$jump_opcodes
$opcode->can("short_jump_dist")
Truth table100%3/3
A and B
A= $jump_opcodesB= $opcode->can("short_jump_dist")
| |||||||||||||||||||||||||||||||||||||||||||
| 329 | 4012 | push(@$jump_opcodes, [$address, $segment_id, $opcode_id]); | |||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||
| 332 | 47478 | $address += $opcode->size; | |||||||||||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | 5638 | return $address; | |||||||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | # Jump opcodes -> list of [opcode_address, opcode], computed on the first call to _locate() | ||||||||||||||||||||||||||||||||||||||||||
| 340 | sub _check_short_jumps { | ||||||||||||||||||||||||||||||||||||||||||
| 341 | 2819 | ▶ | my($self, $jump_opcodes) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_check_short_jumps: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | 2819 | my $jumps = $self->_compute_slack($jump_opcodes); | |||||||||||||||||||||||||||||||||||||||||
| 344 | 2819 | $self->_change_to_long_jump($jumps); | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | # compute slack and impacted jumps for each jump | ||||||||||||||||||||||||||||||||||||||||||
| 348 | sub _compute_slack { | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 2819 | ▶ | my($self, $jump_opcodes) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_compute_slack: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | 2819 | my $jumps = {}; | |||||||||||||||||||||||||||||||||||||||||
| 352 | 2819 | my $symbols = $self->symbols; | |||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||
| 354 | 2819 | ▶ | for (my $i = 0; $i < @$jump_opcodes; $i++) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 355 | 4012 | my($address, $segment_id, $opcode_id) = @{$jump_opcodes->[$i]}; | |||||||||||||||||||||||||||||||||||||||||
| 356 | 4012 | my $opcode = $self->child->[$segment_id]->child->[$opcode_id]; | |||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 4012 | my $dist = $opcode->short_jump_dist($address, $symbols); | |||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | 4012 | $jumps->{$address}{segment_id} = $segment_id; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 4012 | $jumps->{$address}{opcode_id} = $opcode_id; | |||||||||||||||||||||||||||||||||||||||||
| 362 | 4012 | $jumps->{$address}{depends} = []; # list of address of other jumps that reduce | |||||||||||||||||||||||||||||||||||||||||
| 363 | # their slack if we grow | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 4012 | my $target = $address + 2 + $dist; | |||||||||||||||||||||||||||||||||||||||||
| 366 | 4012 | ▶ | if ($dist >= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 367 | 2036 | my $min_target = $address + 2 + 127; | |||||||||||||||||||||||||||||||||||||||||
| 368 | 2036 | ▶ | $min_target = $target if $target < $min_target; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | 2036 | $jumps->{$address}{slack} = 127 - $dist; | |||||||||||||||||||||||||||||||||||||||||
| 371 | 2036 | ▶ | for ( my $j = $i + 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$j < @$jump_opcodes and (my $depend_address = $jump_opcodes->[$j][0]) < $min_target
MC/DC100%2/2
$j < @$jump_opcodes and (my $depend_address = $jump_opcodes->[$j][0]) < $min_target
$j < @$jump_opcodes
(my $depend_address = $jump_opcodes->[$j][0]) < $min_target
Truth table100%3/3
A and B
A= $j < @$jump_opcodesB= (my $depend_address = $jump_opcodes->[$j][0]) < $min_target
| |||||||||||||||||||||||||||||||||||||||||||
| 372 | $j < @$jump_opcodes && | ||||||||||||||||||||||||||||||||||||||||||
| 373 | (my $depend_address = $jump_opcodes->[$j][0]) < $min_target; | ||||||||||||||||||||||||||||||||||||||||||
| 374 | $j++ ) { | ||||||||||||||||||||||||||||||||||||||||||
| 375 | 85675 | push(@{$jumps->{$depend_address}{depends}}, $address); | |||||||||||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||
| 378 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 379 | 1976 | my $max_target = $address + 2 - 128; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 1976 | ▶ | $max_target = $target if $target > $max_target; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 1976 | $jumps->{$address}{slack} = 128 + $dist; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 1976 | ▶ | for ( my $j = $i - 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$j >= 0 and (my $depend_address = $jump_opcodes->[$j][0]) >= $max_target
MC/DC100%2/2
$j >= 0 and (my $depend_address = $jump_opcodes->[$j][0]) >= $max_target
$j >= 0
(my $depend_address = $jump_opcodes->[$j][0]) >= $max_target
Truth table100%3/3
A and B
A= $j >= 0B= (my $depend_address = $jump_opcodes->[$j][0]) >= $max_target
| |||||||||||||||||||||||||||||||||||||||||||
| 384 | $j >= 0 && | ||||||||||||||||||||||||||||||||||||||||||
| 385 | (my $depend_address = $jump_opcodes->[$j][0]) >= $max_target; | ||||||||||||||||||||||||||||||||||||||||||
| 386 | $j-- ) { | ||||||||||||||||||||||||||||||||||||||||||
| 387 | 84687 | push(@{$jumps->{$depend_address}{depends}}, $address); | |||||||||||||||||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||
| 391 | 2819 | $jumps; | |||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | # go through the list of jumps and change all with negative slack to long jumps | ||||||||||||||||||||||||||||||||||||||||||
| 395 | # on each change reduce the slack of the dependent jumps accordingly | ||||||||||||||||||||||||||||||||||||||||||
| 396 | sub _change_to_long_jump { | ||||||||||||||||||||||||||||||||||||||||||
| 397 | 2819 | ▶ | my($self, $jumps) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_change_to_long_jump: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | 2819 | my $changed; | |||||||||||||||||||||||||||||||||||||||||
| 400 | 2819 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 2876 | $changed = 0; | |||||||||||||||||||||||||||||||||||||||||
| 402 | 2876 | for my $address (keys %$jumps) { | |||||||||||||||||||||||||||||||||||||||||
| 403 | 5618 | my $jump = $jumps->{$address}; | |||||||||||||||||||||||||||||||||||||||||
| 404 | 5618 | ▶ | if ($jump->{slack} < 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | # need to change this | ||||||||||||||||||||||||||||||||||||||||||
| 406 | 1584 | my $segment_id = $jump->{segment_id}; | |||||||||||||||||||||||||||||||||||||||||
| 407 | 1584 | my $opcode_id = $jump->{opcode_id}; | |||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | 1584 | my $opcode = $self->child->[$segment_id]->child->[$opcode_id]; | |||||||||||||||||||||||||||||||||||||||||
| 410 | 1584 | my $inc_size = $opcode->long_jump->size - $opcode->short_jump->size; | |||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | # discard the short jump | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 1584 | $self->child->[$segment_id]->child->[$opcode_id] = $opcode->long_jump; | |||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | # impact all dependents | ||||||||||||||||||||||||||||||||||||||||||
| 416 | 1584 | for my $depend_address (@{$jump->{depends}}) { | |||||||||||||||||||||||||||||||||||||||||
| 417 | exists $jumps->{$depend_address} | ||||||||||||||||||||||||||||||||||||||||||
| 418 | 24192 | ▶ | and $jumps->{$depend_address}{slack} -= $inc_size; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | # delete this from the list | ||||||||||||||||||||||||||||||||||||||||||
| 422 | 1584 | delete $jumps->{$address}; | |||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | 1584 | $changed++; | |||||||||||||||||||||||||||||||||||||||||
| 425 | } | ||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | } while ($changed); | ||||||||||||||||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | =head2 bytes | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | Allocate addresses for all child segments, starting at | ||||||||||||||||||||||||||||||||||||||||||
| 435 | the first segment's C<address> (defined by a "org" instruction), or at 0. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | Computes the bytes of each segment, and concatenates them together. Returns the | ||||||||||||||||||||||||||||||||||||||||||
| 438 | complete object code. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | Gaps between segments are filled with $CPU::Z80::Assembler::fill_byte. | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | $list_output is an optional L<CPU::Z80::Assembler::List|CPU::Z80::Assembler::List> object to dump the assembly | ||||||||||||||||||||||||||||||||||||||||||
| 443 | listing to. | ||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||
| 449 | sub bytes { | ||||||||||||||||||||||||||||||||||||||||||
| 450 | 2820 | ▶ | my($self, $list_output) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | 2820 | ▶ | return "" unless @{$self->child}; # if empty, nothing to do | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 2818 | my $symbols = $self->symbols; | |||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | # locate the code | ||||||||||||||||||||||||||||||||||||||||||
| 457 | 2818 | $self->_locate; | |||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | # get start address | ||||||||||||||||||||||||||||||||||||||||||
| 460 | 2816 | my $address = $self->child->[0]->address; | |||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | # char used to fill gaps between segments | ||||||||||||||||||||||||||||||||||||||||||
| 463 | 2816 | ▶ | my $fill_byte = defined($CPU::Z80::Assembler::fill_byte) ? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | chr($CPU::Z80::Assembler::fill_byte) : | ||||||||||||||||||||||||||||||||||||||||||
| 465 | chr(0xFF); | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | 2816 | my $bytes = ""; | |||||||||||||||||||||||||||||||||||||||||
| 468 | 2816 | for my $segment (@{$self->child}) { | |||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | # fill in the gap, if any | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 2829 | my $segment_address = $segment->address; | |||||||||||||||||||||||||||||||||||||||||
| 472 | 2829 | ▶ | if (length($bytes) && $address != $segment_address) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $bytes and $address != $segment_address
MC/DC100%2/2
length $bytes and $address != $segment_address
length $bytes
$address != $segment_address
Truth table100%3/3
A and B
A= length $bytesB= $address != $segment_address
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 7 | my $fill = $segment_address - $address; | |||||||||||||||||||||||||||||||||||||||||
| 474 | 7 | ▶ | die if $fill < 0; # ASSERT | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | 7 | $bytes .= $fill_byte x $fill; | |||||||||||||||||||||||||||||||||||||||||
| 477 | 7 | $address = $segment_address; | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | # fill segment bytes | ||||||||||||||||||||||||||||||||||||||||||
| 481 | 2829 | for my $opcode (@{$segment->child}) { | |||||||||||||||||||||||||||||||||||||||||
| 482 | 23728 | $opcode->address($address); | |||||||||||||||||||||||||||||||||||||||||
| 483 | 23728 | my $opcode_bytes = $opcode->bytes($address, $symbols); | |||||||||||||||||||||||||||||||||||||||||
| 484 | 23728 | $bytes .= $opcode_bytes; | |||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | 23728 | ▶ | $list_output->add($opcode->line, $address, $opcode_bytes) if ($list_output); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | 23728 | $address += $opcode->size; | |||||||||||||||||||||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||||||||||||||||||||||
| 490 | } | ||||||||||||||||||||||||||||||||||||||||||
| 491 | 2816 | return $bytes; | |||||||||||||||||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||||||||||||||||
| 494 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | =head1 BUGS and FEEDBACK | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | See L<CPU::Z80::Assembler|CPU::Z80::Assembler>. | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | L<CPU::Z80::Assembler|CPU::Z80::Assembler> | ||||||||||||||||||||||||||||||||||||||||||
| 503 | L<CPU::Z80::Assembler::Segment|CPU::Z80::Assembler::Segment> | ||||||||||||||||||||||||||||||||||||||||||
| 504 | L<CPU::Z80::Assembler::Parser|CPU::Z80::Assembler::Parser> | ||||||||||||||||||||||||||||||||||||||||||
| 505 | L<Class::Struct|Class::Struct> | ||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||
| 507 | =head1 AUTHORS, COPYRIGHT and LICENCE | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | See L<CPU::Z80::Assembler|CPU::Z80::Assembler>. | ||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||
| 513 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | 36 | 1; | |||||||||||||||||||||||||||||||||||||||||