| 1 | # $Id$ | ||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||
| 3 | package CPU::Z80::Assembler::Expr; | ||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||
| 5 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||
| 9 | CPU::Z80::Assembler::Expr - Represents one assembly expression to be computed at link time | ||||||||||||||||||||||||||||||
| 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; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 21 | 36 | ▶ | use CPU::Z80::Assembler::Parser; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 22 | 36 | ▶ | use Iterator::Simple::Lookahead; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 23 | 36 | ▶ | use Asm::Preproc::Line; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 24 | 36 | ▶ | use Asm::Preproc::Token; | ||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||
| 26 | #use Class::Struct ( | ||||||||||||||||||||||||||||||
| 27 | # child => '@', # list of children of this node | ||||||||||||||||||||||||||||||
| 28 | # line => 'Asm::Preproc::Line', | ||||||||||||||||||||||||||||||
| 29 | # # line where tokens found | ||||||||||||||||||||||||||||||
| 30 | # type => '$', # one of: | ||||||||||||||||||||||||||||||
| 31 | # # "sb" - signed byte | ||||||||||||||||||||||||||||||
| 32 | # # "ub" - unsigned byte | ||||||||||||||||||||||||||||||
| 33 | # # "w" - 2 byte word | ||||||||||||||||||||||||||||||
| 34 | #); | ||||||||||||||||||||||||||||||
| 35 | sub new { | ||||||||||||||||||||||||||||||
| 36 | 22948 | ▶ | my($class, %args) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 37 | bless [ | ||||||||||||||||||||||||||||||
| 38 | $args{type}, | ||||||||||||||||||||||||||||||
| 39 | $args{line} || Asm::Preproc::Line->new(), | ||||||||||||||||||||||||||||||
| 40 | 22948 | ▶ | $args{child} || [], | ||||||||||||||||||||||||||||
|
Condition80%4/5
$args{'line'} || 'Asm::Preproc::Line'->new
$args{'child'} || []
MC/DC0%0/2
$args{'line'} || 'Asm::Preproc::Line'->new
$args{'line'}
'Asm::Preproc::Line'->new
MC/DC100%1/1
$args{'child'} || []
$args{'child'}
Truth table66%2/3
A || B
A= $args{'line'}B= 'Asm::Preproc::Line'->new
Truth table100%2/2
A
A= $args{'child'}
| |||||||||||||||||||||||||||||||
| 41 | ], $class; | ||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||
| 43 | 14446 | ▶ | sub type { defined($_[1]) ? $_[0][0] = $_[1] : $_[0][0] } | ||||||||||||||||||||||||||||
|
Subroutine
type: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 44 | 8122 | ▶ | sub line { defined($_[1]) ? $_[0][1] = $_[1] : $_[0][1] } | ||||||||||||||||||||||||||||
|
Subroutine
line: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 45 | 38887 | ▶ | sub child { defined($_[1]) ? $_[0][2] = $_[1] : $_[0][2] } | ||||||||||||||||||||||||||||
|
Subroutine
child: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||
| 47 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||
| 49 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||
| 51 | use CPU::Z80::Assembler::Expr; | ||||||||||||||||||||||||||||||
| 52 | my $node = CPU::Z80::Assembler::Expr->new( type => "sb" ); | ||||||||||||||||||||||||||||||
| 53 | $expr->parse($input); | ||||||||||||||||||||||||||||||
| 54 | $new_expr = $expr->build($expr_text); | ||||||||||||||||||||||||||||||
| 55 | $value = $expr->evaluate($address, \%symbol_table); | ||||||||||||||||||||||||||||||
| 56 | $bytes = $expr->bytes($address, \%symbol_table); | ||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||
| 58 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||
| 60 | This module defines the class that represents one assembly expression to be | ||||||||||||||||||||||||||||||
| 61 | computed at link time. | ||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||
| 63 | =head1 EXPORTS | ||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||
| 65 | Nothing. | ||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||
| 67 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||
| 69 | =head2 new | ||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||
| 71 | Creates a new object, see L<Class::Struct|Class::Struct>. | ||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||
| 73 | =head2 type | ||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||
| 75 | The type string has to be defined before the C<bytes> method is called, and defines | ||||||||||||||||||||||||||||||
| 76 | how to code the value returned by C<evaluate> into a byte string. | ||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||
| 78 | Type is one of: | ||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||
| 80 | =over 4 | ||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||
| 82 | =item "sb" | ||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||
| 84 | for signed byte - a 8 bit signed value. A larger value is truncated and a warning | ||||||||||||||||||||||||||||||
| 85 | is issued. | ||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||
| 87 | =item "ub" | ||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||
| 89 | for unsigned byte - a 8 bit unsigned value. A larger value is truncated and a warning | ||||||||||||||||||||||||||||||
| 90 | is issued. | ||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||
| 92 | =item "w" | ||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||
| 94 | for word - a 16 bit unsigned value in little endian format. A larger value is truncated, | ||||||||||||||||||||||||||||||
| 95 | but in this case no warning is issued. The address part above 0xFFFF is considered | ||||||||||||||||||||||||||||||
| 96 | a bank selector for memory banked systems. | ||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||
| 98 | A STRING value is computed in little endian format and only the first two characters are used. | ||||||||||||||||||||||||||||||
| 99 | "ab" is encoded as ord("a")+(ord("b")<<8). | ||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||
| 101 | =back | ||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||
| 103 | The text bytes used in defm / deft are a string of bytes in big endian format, not truncated. For example, 0x112233 is stored as the 3-byte sequence 0x11, 0x22 and 0x33. | ||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||
| 105 | A STRING value is encoded with the list of characters in the string. If the string is | ||||||||||||||||||||||||||||||
| 106 | used in an expression, then the expression applies to the last character of the string. This allows expressions like "CALL"+0x80 to invert bit 7 of the last character of the string. | ||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||
| 108 | C-like escape sequences are expanded both in single- and double-quoted strings. | ||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||
| 110 | =head2 child | ||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||
| 112 | List of tokens composing the expression. | ||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||
| 114 | =head2 line | ||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||
| 116 | Get/set the line - text, file name and line number where the token was read. | ||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||
| 118 | =cut | ||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||
| 120 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||
| 122 | =head2 parse | ||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||
| 124 | $expr->parse($input); | ||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||
| 126 | Parses an expression at the given $input stream | ||||||||||||||||||||||||||||||
| 127 | (L<Iterator::Simple::Lookahead|Iterator::Simple::Lookahead>), | ||||||||||||||||||||||||||||||
| 128 | leaves the stream pointer after the expression and updates the expression object. | ||||||||||||||||||||||||||||||
| 129 | Dies if the expression cannot be parsed. | ||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||
| 133 | sub parse { | ||||||||||||||||||||||||||||||
| 134 | 33 | ▶ | my($self, $input) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||
| 135 | 33 | $self->child([]); | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||
| 137 | 33 | my $value = CPU::Z80::Assembler::Parser::parse($input, undef, "expr"); | |||||||||||||||||||||||||||||
| 138 | 27 | $self->child($value); | |||||||||||||||||||||||||||||
| 139 | 27 | $self->line($value->[0]->line); | |||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||
| 142 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||
| 144 | =head2 evaluate | ||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||
| 146 | $value = $expr->evaluate($address, $symbol_table) | ||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||
| 148 | Computes the value of the expression, as found at the given address and looking | ||||||||||||||||||||||||||||||
| 149 | up any referenced symbols from the given symbol table. | ||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||
| 151 | The address is used to evaluate the value of '$'. | ||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||
| 153 | The symbol table is a hash of symbol names to values. The value is either a | ||||||||||||||||||||||||||||||
| 154 | scalar value that is used directly in the expression, or a reference to a | ||||||||||||||||||||||||||||||
| 155 | sub-expression that is computed recursively by calling its C<evaluate> method. | ||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||
| 157 | Exits with a fatal error if the expression cannot be evaluated (circular reference, | ||||||||||||||||||||||||||||||
| 158 | undefined symbol or mathematical error). | ||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||
| 162 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||
| 164 | 28188 | ▶ | sub evaluate { my($self, $address, $symbol_table, $seen) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
evaluate: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 165 | 28188 | ▶ | $seen ||= {}; # to detect circular references | ||||||||||||||||||||||||||||
|
Condition100%2/2
$seen ||= {}
MC/DC100%1/1
$seen ||= {}
$seen
Truth table100%2/2
A
A= $seen
| |||||||||||||||||||||||||||||||
| 166 | 28188 | my @code; | |||||||||||||||||||||||||||||
| 167 | 28188 | for my $token (@{$self->child}) { | |||||||||||||||||||||||||||||
| 168 | 57127 | my($type, $value) = ($token->type, $token->value); | |||||||||||||||||||||||||||||
| 169 | 57127 | ▶ | if ($type eq "NUMBER") { | ||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||
| 170 | 23376 | push(@code, $value); | |||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||
| 172 | elsif ($type eq "NAME") { | ||||||||||||||||||||||||||||||
| 173 | 15143 | ▶ | if ($value eq '$') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 174 | 6477 | push(@code, $address); | |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||
| 176 | else { | ||||||||||||||||||||||||||||||
| 177 | 8666 | my $expr = $symbol_table->{$value}; | |||||||||||||||||||||||||||||
| 178 | 8666 | my $expr_value; | |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||
| 180 | 8666 | ▶ | defined($expr) or | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 181 | $self->line->error("Symbol '$value' not defined"); | ||||||||||||||||||||||||||||||
| 182 | 8665 | ▶ | if (ref($expr)) { # compute sub-expression first | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 183 | 8644 | ▶ | $seen->{$value} and | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 184 | $self->line->error("Circular reference computing '$value'"); | ||||||||||||||||||||||||||||||
| 185 | 8643 | my %local_seen = (%$seen, $value => 1); | |||||||||||||||||||||||||||||
| 186 | 8643 | $expr_value = $expr->evaluate($address, $symbol_table, | |||||||||||||||||||||||||||||
| 187 | \%local_seen); | ||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||
| 190 | 21 | $expr_value = $expr; | |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||
| 192 | 8663 | push(@code, $expr_value); | |||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||
| 195 | elsif ($type eq "STRING") { | ||||||||||||||||||||||||||||||
| 196 | 16 | ▶ | if (length($value) > 2) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 197 | 7 | $self->line->warning("Expression $value: extra bytes ignored"); | |||||||||||||||||||||||||||||
| 198 | 7 | $value = substr($value, 0, 2); | |||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||||
| 200 | 16 | $value .= "\0\0"; | |||||||||||||||||||||||||||||
| 201 | 16 | my @bytes = map {ord($_)} split(//, $value); | |||||||||||||||||||||||||||||
| 202 | 16 | my $value = $bytes[0] + ($bytes[1] << 8); | |||||||||||||||||||||||||||||
| 203 | 16 | push(@code, $value); | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||
| 205 | elsif ($type eq "EXPR") { | ||||||||||||||||||||||||||||||
| 206 | 2709 | my $expr_value = $value->evaluate($address, $symbol_table, $seen); | |||||||||||||||||||||||||||||
| 207 | 2709 | push(@code, $expr_value); | |||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||||||||||
| 210 | 15883 | ▶ | $type =~ /^[a-z_]/ and # reserved word | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 211 | $self->line->error("Expression '$type': syntax error"); | ||||||||||||||||||||||||||||||
| 212 | 15883 | push(@code, $type); | |||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||
| 215 | 28185 | ▶ | return 0 if !@code; | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 216 | 28175 | my $code = join(" ", @code); | |||||||||||||||||||||||||||||
| 217 | 28175 | my $value = eval $code; | |||||||||||||||||||||||||||||
| 218 | 28175 | ▶ | if ($@) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 219 | 1 | $@ =~ s/ at .*//; | |||||||||||||||||||||||||||||
| 220 | 1 | $self->line->error("Expression '$code': $@"); | |||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||
| 223 | 28174 | return $value; | |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||
| 226 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||
| 228 | =head2 build | ||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||
| 230 | $new_expr = $expr->build($expr_text) | ||||||||||||||||||||||||||||||
| 231 | $new_expr = $expr->build($expr_text, @init_args) | ||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||
| 233 | Build and return a new expresion object with an expression based on the current | ||||||||||||||||||||||||||||||
| 234 | object. The expression is passed as a string and is lexed by L<CPU::Z80::Assembler|CPU::Z80::Assembler> C<z80lexer>. | ||||||||||||||||||||||||||||||
| 235 | The special token '{}' is used to refer to this expression. | ||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||
| 237 | For example, to return a new expression object that, when evaluated, gives the double | ||||||||||||||||||||||||||||||
| 238 | of the current expression object: | ||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||
| 240 | my $new_expr = $expr->build("2*{}"); | ||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||
| 242 | C<@init_args> can be used to pass parameters to the constructor of the new expression | ||||||||||||||||||||||||||||||
| 243 | object. | ||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||
| 245 | =cut | ||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||
| 247 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||
| 249 | 1843 | ▶ | sub build { my($self, $expr_text, @init_args) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
build: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 250 | 1843 | my $line = $self->line; | |||||||||||||||||||||||||||||
| 251 | 1843 | my $new_expr = ref($self)->new(line => $line, type => $self->type, @init_args); | |||||||||||||||||||||||||||||
| 252 | 1843 | my $token_stream = CPU::Z80::Assembler::z80lexer($expr_text); | |||||||||||||||||||||||||||||
| 253 | 1843 | ▶ | while (defined(my $token = $token_stream->next)) { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 254 | 10632 | ▶ | if ($token->type eq '{') { | ||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||
| 255 | 1843 | ▶ | (defined($token_stream->peek) && $token_stream->next->type eq '}') | ||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $token_stream->peek and $token_stream->next->type eq "}"
MC/DC50%1/2
defined $token_stream->peek and $token_stream->next->type eq "}"
defined $token_stream->peek
$token_stream->next->type eq "}"
Truth table66%2/3
A and B
A= defined $token_stream->peekB= $token_stream->next->type eq "}"
| |||||||||||||||||||||||||||||||
| 256 | or die "unmatched {}"; | ||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||
| 258 | # refer to this expression | ||||||||||||||||||||||||||||||
| 259 | 1841 | push(@{$new_expr->child}, | |||||||||||||||||||||||||||||
| 260 | Asm::Preproc::Token->new(EXPR => $self, $line)); | ||||||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||||
| 262 | else { | ||||||||||||||||||||||||||||||
| 263 | 8789 | $token->line($line); | |||||||||||||||||||||||||||||
| 264 | 8789 | push(@{$new_expr->child}, $token); | |||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||
| 267 | 1841 | $new_expr; | |||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||
| 270 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||
| 272 | =head2 bytes | ||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||
| 274 | $bytes = $expr->bytes($address, \%symbol_table); | ||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||
| 276 | Calls C<evaluate> to compute the value of the expression, and converts the | ||||||||||||||||||||||||||||||
| 277 | value to a one or two byte string, according to the C<type>. | ||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||
| 279 | =cut | ||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||
| 281 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||
| 283 | 12596 | ▶ | sub bytes { my($self, $address, $symbol_table) = @_; | ||||||||||||||||||||||||||||
|
Subroutine
bytes: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||||
| 284 | 12596 | ▶ | my $type = $self->type || ""; | ||||||||||||||||||||||||||||
|
Condition100%2/2
$self->type || ''
MC/DC100%1/1
$self->type || ''
$self->type
Truth table100%2/2
A
A= $self->type
| |||||||||||||||||||||||||||||||
| 285 | 12596 | my $value = $self->evaluate($address, $symbol_table); | |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||
| 287 | 12596 | my $ret; | |||||||||||||||||||||||||||||
| 288 | 12596 | ▶ | if ($type eq "w") { | ||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||
| 289 | 2799 | ▶ | if ($value > 0xFFFF) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 290 | # silently accept values > 0xFFFF to ignore segment selectors | ||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||
| 292 | elsif ($value < -0x8000) { | ||||||||||||||||||||||||||||||
| 293 | # error if negative value out of range | ||||||||||||||||||||||||||||||
| 294 | 2 | $self->line->error(sprintf("value -0x%04X out of range", (-$value) & 0xFFFF)); | |||||||||||||||||||||||||||||
| 295 | 0 | die; # not reached | |||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||
| 297 | 2797 | $ret = pack("v", $value & 0xFFFF); # 16 bit little endian unsigned | |||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||
| 299 | elsif ($type eq "ub") { | ||||||||||||||||||||||||||||||
| 300 | 402 | ▶ | if ($value > 0xFF) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 301 | # accept values > 0xFF, but issue warning | ||||||||||||||||||||||||||||||
| 302 | 7 | $self->line->warning(sprintf("value 0x%02X truncated to 0x%02X", | |||||||||||||||||||||||||||||
| 303 | $value, $value & 0xFF)); | ||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||
| 305 | elsif ($value < -0x80) { | ||||||||||||||||||||||||||||||
| 306 | # error if negative value out of range | ||||||||||||||||||||||||||||||
| 307 | 1 | $self->line->error(sprintf("value -0x%02X out of range", (-$value) & 0xFF)); | |||||||||||||||||||||||||||||
| 308 | 0 | die; # not reached | |||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||
| 310 | 401 | $ret = pack("C", $value & 0xFF); # 8 bit unsigned | |||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||
| 312 | elsif ($type eq "sb") { | ||||||||||||||||||||||||||||||
| 313 | # error if value outside of signed byte range | ||||||||||||||||||||||||||||||
| 314 | # used by (ix+d) and jr NN; error if out of range | ||||||||||||||||||||||||||||||
| 315 | 9394 | ▶ | if ($value > 0x7F) { | ||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||
| 316 | 2 | $self->line->error(sprintf("value 0x%02X out of range", $value)); | |||||||||||||||||||||||||||||
| 317 | 0 | die; # not reached | |||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||
| 319 | elsif ($value < -0x80) { | ||||||||||||||||||||||||||||||
| 320 | 2 | $self->line->error(sprintf("value -0x%02X out of range", (-$value) & 0xFF)); | |||||||||||||||||||||||||||||
| 321 | 0 | die; # not reached | |||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||
| 323 | 9390 | $ret = pack("C", $value & 0xFF); # 8 bit unsigned | |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||||
| 325 | else { | ||||||||||||||||||||||||||||||
| 326 | 1 | die "Expr::bytes(): unrecognized type '$type'"; # exception | |||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||
| 328 | 12588 | return $ret; | |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||
| 331 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||
| 333 | =head1 BUGS and FEEDBACK | ||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||
| 335 | See L<CPU::Z80::Assembler|CPU::Z80::Assembler>. | ||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||
| 337 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||
| 339 | L<CPU::Z80::Assembler|CPU::Z80::Assembler> | ||||||||||||||||||||||||||||||
| 340 | L<Asm::Preproc::Line|Asm::Preproc::Line> | ||||||||||||||||||||||||||||||
| 341 | L<Class::Struct|Class::Struct> | ||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||
| 343 | =head1 AUTHORS, COPYRIGHT and LICENCE | ||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||
| 345 | See L<CPU::Z80::Assembler|CPU::Z80::Assembler>. | ||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||
| 347 | =cut | ||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||
| 349 | 36 | 1; | |||||||||||||||||||||||||||||