lib/CPU/Z80/Assembler/Macro.pm

filter: Statementstmt 90.3% 112 / 124 Branchbran 79.7% 59 / 74 Conditioncond 71.4% 15 / 21 MC/DCmcdc 53.8% 7 / 13 Subroutinesub 100% 16 / 16 Podpod 100% 8 / 8 totaltotal 84.7% 217 / 256 SCAR 39.7
CC
46
Cov
85%
parse_body
19.2
_parse_argument
11.6
parse_macro_arguments
9.0
CRAP 53.2
« lib/CPU/Z80/Assembler/Program.pm ↑ Summary lib/CPU/Z80/Assembler/Expr.pm »
1 # $Id$
2  
3 package CPU::Z80::Assembler::Macro;
4  
5 #------------------------------------------------------------------------------
6  
7 =head1 NAME
8  
9 CPU::Z80::Assembler::Macro - Macro pre-processor for the Z80 assembler
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 ▶use CPU::Z80::Assembler::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use Iterator::Simple::Lookahead;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 36 ▶use Asm::Preproc::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 36 our $VERSION = '2.23';
23  
24 #------------------------------------------------------------------------------
25 # Class::Struct cannot be used with Exporter
26 #use Class::Struct (
27 # name => '$', # macro name
28 # params => '@', # list of macro parameter names
29 # locals => '%', # list of macro local labels
30 # tokens => '@', # list of macro tokens
31 #);
32 22 ▶sub new { my($class, %args) = @_;
Subroutine new: called CC=4 SCAR=15.8
Pod documented
33 return bless [
34 $args{name},
35 $args{params} || [],
36 $args{locals} || {},
37 22 ▶ $args{tokens} || []
Condition50%3/6
$args{'params'} || []
l!l
022
$args{'locals'} || {}
l!l
022
$args{'tokens'} || []
l!l
022
MC/DC0%0/1
$args{'params'} || []
$args{'params'}
MC/DC0%0/1
$args{'locals'} || {}
$args{'locals'}
MC/DC0%0/1
$args{'tokens'} || []
$args{'tokens'}
Truth table50%1/2
A
A= $args{'params'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'locals'}
Aresult
11
00
Truth table50%1/2
A
A= $args{'tokens'}
Aresult
11
00
38 ], $class;
39 }
40 19 ▶sub name { defined($_[1]) ? $_[0][0] = $_[1] : $_[0][0] }
Subroutine name: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 19 0 19
41 44 ▶sub params { defined($_[1]) ? $_[0][1] = $_[1] : $_[0][1] }
Subroutine params: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 19 25 44
42 39 ▶sub locals { defined($_[1]) ? $_[0][2] = $_[1] : $_[0][2] }
Subroutine locals: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 20 19 39
43 38 ▶sub tokens { defined($_[1]) ? $_[0][3] = $_[1] : $_[0][3] }
Subroutine tokens: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 20 18 38
44  
45 #------------------------------------------------------------------------------
46  
47 =head1 SYNOPSIS
48  
49 use CPU::Z80::Assembler::Macro;
50  
51 my $macro = CPU::Z80::Assembler::Macro->new(
52 name => $name,
53 params => \@params_names,
54 locals => \%local_labels,
55 tokens => \@token_list);
56 $macro->parse_body($input);
57 $macro->expand_macro($input);
58  
59 =head1 DESCRIPTION
60  
61 This module provides a macro pre-processor to parse macro definition statements,
62 and expand macro calls in the token stream. Both the input and output streams
63 are L<Iterator::Simple::Lookahead|Iterator::Simple::Lookahead> objects returning sequences
64 of tokens.
65  
66 The object created by new() describes one macro. It is used during the parse phase
67 to define the macro object while reading the input token stream.
68  
69 =head1 EXPORTS
70  
71 None.
72  
73 =head1 FUNCTIONS
74  
75 =head2 new
76  
77 Creates a new macro definition object, see L<Class::Struct|Class::Struct>.
78  
79 =head2 name
80  
81 Get/set the macro name.
82  
83 =head2 params
84  
85 Get/set the formal parameter names list.
86  
87 =head2 locals
88  
89 Get/set the list of local macro labels, stored as a hash.
90  
91 =head2 tokens
92  
93 Get/set the list of tokens in the macro definition.
94  
95 =cut
96  
97 #------------------------------------------------------------------------------
98  
99 =head2 parse_body
100  
101 This method is called with the token input stream pointing at the first token
102 after the macro parameter list, i.e. the '{' or ':' or "\n" character.
103  
104 It parses the macro body, leaving the input stream after the last token of the
105 macro definition ('endm' or closing '}'), with all the "\n" characters of the
106 macro defintion pre-pended, and filling in locals() and tokens().
107  
108 =cut
109  
110 #------------------------------------------------------------------------------
111  
112 sub parse_body {
113 22 ▶ my($self, $input) = @_;
Subroutine parse_body: called CC=16 SCAR=29.5
Pod documented
114 22 my $token;
115
116 # skip {
117 my $opened_brace;
118 22 ▶ defined($token = $input->peek)
Branch: 1 branch
BranchTrueFalseTotal
defined($token = $input->peek) or "Asm::Preproc::Token"->error_at($token, "macro body not found") 0 22 22
119 or Asm::Preproc::Token->error_at($token, "macro body not found");
120 22 ▶ if ($token->type eq '{') {
Branch: 2 branches
BranchTrueFalseTotal
if ($token->type eq '{') { } 21 1 22
elsif ($token->type =~ /^[:\n]$/) { } 1 0 1
121 21 $input->next;
122 21 $opened_brace++;
123 }
124 elsif ($token->type =~ /^[:\n]$/) {
125 # OK, macro body follows on next line
126 }
127 else {
128 0 $token->error("unexpected '". $token->type ."'");
129 }
130
131 # retrieve tokens
132 22 my @macro_tokens;
133 my @line_tokens;
134 22 my %locals;
135  
136 # need to note all the labels in the macro,
137 # i.e. NAME after statement end
138 22 my $last_stmt_end = 1;
139  
140 22 my $parens = 0;
141 22 ▶ while (defined($token = $input->peek)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($token = $input->peek)) 234 1 235
142 234 my $type = $token->type;
143 234 ▶ if ($type eq "{") {
Branch: 4 branches
BranchTrueFalseTotal
if ($type eq '{') { } 0 234 234
elsif ($type eq 'endm') { } 1 233 234
elsif ($type eq '}') { } 20 213 233
elsif ($type eq 'NAME' and $last_stmt_end) { } 8 205 213
144 0 $parens++;
145 0 push @macro_tokens, $token;
146 0 $input->next;
147 }
148 elsif ($type eq "endm") {
149 1 ▶ $opened_brace
Branch: 1 branch
BranchTrueFalseTotal
$opened_brace and $token->error("expected \"}\"") 1 0 1
150 and $token->error("expected \"}\"");
151 0 $input->next; # skip delimiter
152 0 last;
153 }
154 elsif ($type eq "}") {
155 20 ▶ if ($parens > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parens > 0) { } 0 20 20
156 0 $parens--;
157 0 push @macro_tokens, $token;
158 0 $input->next;
159 }
160 else {
161 20 ▶ $input->next if $opened_brace; # skip delimiter
Branch: 1 branch
BranchTrueFalseTotal
if $opened_brace 20 0 20
162 20 last;
163 }
164 }
165 ▶ elsif ($type eq "NAME" && $last_stmt_end) { # local label
Condition100%3/3
$type eq "NAME" and $last_stmt_end
!ll&&!rl&&r
174318
MC/DC100%2/2
$type eq "NAME" and $last_stmt_end
$type eq "NAME" $last_stmt_end
Truth table100%3/3
A and B
A= $type eq "NAME"B= $last_stmt_end
ABresult
0X0
100
111
166 8 $locals{$token->value}++;
167 8 push @macro_tokens, $token;
168 8 $input->next;
169 }
170 else {
171 205 push @macro_tokens, $token;
172 205 ▶ push @line_tokens, $token if $type eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "\n" 70 135 205
173 # save new-lines for listing
174 205 $input->next;
175 }
176 213 $last_stmt_end = ($type =~ /^[:\n]$/);
177 }
178 21 ▶ defined($token)
Branch: 1 branch
BranchTrueFalseTotal
defined $token or "Asm::Preproc::Token"->error_at($token, "macro body not finished") 1 20 21
179 or Asm::Preproc::Token->error_at($token, "macro body not finished");
180 20 ▶ ($parens == 0)
Branch: 1 branch
BranchTrueFalseTotal
$parens == 0 or $token->error("Unmatched braces") 0 20 20
181 or $token->error("Unmatched braces");
182
183 # prepend all seen LINE tokens in input
184 20 $input->unget(@line_tokens);
185
186 20 $self->tokens(\@macro_tokens);
187 20 $self->locals(\%locals);
188 }
189  
190 #------------------------------------------------------------------------------
191  
192 =head2 expand_macro
193  
194 This method is called with the input stream pointing at the first token
195 after the macro name in a macro call. It parses the macro arguments, if any
196 and expands the macro call, inserting the expanded tokens in the input stream.
197  
198 =cut
199  
200 #------------------------------------------------------------------------------
201  
202 sub expand_macro {
203 25 ▶ my($self, $input) = @_;
Subroutine expand_macro: called CC=2 SCAR=6.9
Pod documented
204 25 our $instance++; # unique ID for local labels
205
206 25 my $start_token = $input->peek; # for error messages
207 25 ▶ defined($start_token) or die; # must have at least a "\n"
Branch: 1 branch
BranchTrueFalseTotal
defined $start_token or die 0 25 25
208
209 25 my $args = $self->parse_macro_arguments($input);
210
211 # compute token expansion
212 18 my $macro_stream = Iterator::Simple::Lookahead->new(@{$self->tokens});
213 my $expand_stream = Iterator::Simple::Lookahead->new(
214 sub {
215 234 ▶ for(;;) {
Subroutine __ANON__: called CC=5 SCAR=16.1
216 234 my $token = $macro_stream->next;
217 234 ▶ defined($token) or return undef; # end of expansion
Branch: 1 branch
BranchTrueFalseTotal
defined $token or return undef 18 216 234
218
219 216 $token = $token->clone; # make a copy
220 216 $token->line($start_token->line); # set the line of invocation
221
222 216 ▶ if ($token->type eq 'NAME') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token->type eq 'NAME') { } 34 182 216
223 34 my $name = $token->value;
224 34 ▶ if (exists $args->{$name}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{$name}) { } 15 19 34
elsif (exists $self->locals->{$name}) { } 19 0 19
225 15 my @tokens = @{$args->{$name}}; # expansion of the name
226 15 ▶ return sub {shift @tokens}; # insert a new iterator to return
Subroutine __ANON__: called CC=1 SCAR=0.0
227 # these - $macro_stream->unget();
228 # would allow recursive expansion
229 # of arg names - not intended
230 }
231 elsif (exists $self->locals->{$name}) {
232 19 $token->value("_macro_".$instance."_".$name);
233 19 return $token;
234 }
235 else {
236 0 return $token;
237 }
238 }
239 else {
240 182 return $token;
241 }
242 }
243 18 });
244
245 # prepend the expanded stream in the input
246 18 $input->unget($expand_stream);
247 }
248  
249 #------------------------------------------------------------------------------
250  
251 =head2 parse_macro_arguments
252  
253 This method is called with the input stream pointing at the first token
254 after the macro name in a macro call. It parses the macro arguments, leaves
255 the input stream after the macro call, and returns an hash reference mapping
256 formal argument names to list of tokens in the actual parameters.
257  
258 The arguments are list of tokens separated by ','. An argument can be enclosed
259 in braces '{' '}' to allow ',' to be passed - the braces are not part of the argument
260 value.
261  
262 =cut
263  
264 #------------------------------------------------------------------------------
265  
266 sub parse_macro_arguments {
267 25 ▶ my($self, $input) = @_;
Subroutine parse_macro_arguments: called CC=9 SCAR=22.0
Pod documented
268 25 my %args;
269 my $token;
270
271 25 my @params = @{$self->params}; # formal parameters
272 25 ▶ for (my $i = 0; $i < @params; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @params) 22 21 43
273 22 my $param = $params[$i];
274 22 $token = $input->peek;
275 22 ▶ defined($token) && $token->type !~ /^[:\n,]$/
Branch: 1 branch
BranchTrueFalseTotal
defined $token and not $token->type =~ /^[:\n,]$/ or "Asm::Preproc::Token"->error_at($token, "expected value for macro parameter $param") 2 20 22
Condition66%2/3
defined $token and not $token->type =~ /^[:\n,]$/
!ll&&!rl&&r
0220
MC/DC50%1/2
defined $token and not $token->type =~ /^[:\n,]$/
defined $token not $token->type =~ /^[:\n,]$/
Truth table66%2/3
A and not(B)
A= defined $tokenB= not $token->type =~ /^[:\n,]$/
ABresult
0X0
100
111
276 or Asm::Preproc::Token->error_at($token,
277 "expected value for macro parameter $param");
278 20 my @arg = $self->_parse_argument($input);
279 19 $args{$param} = \@arg;
280
281 19 ▶ if ($i != $#params) { # expect a comma
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $#params) 10 9 19
282 10 $token = $input->peek;
283 10 ▶ defined($token) && $token->type eq ','
Branch: 1 branch
BranchTrueFalseTotal
defined $token and $token->type eq "," or "Asm::Preproc::Token"->error_at($token, qq[expected "," after macro parameter $param]) 1 9 10
Condition66%2/3
defined $token and $token->type eq ","
!ll&&!rl&&r
019
MC/DC50%1/2
defined $token and $token->type eq ","
defined $token $token->type eq ","
Truth table66%2/3
A and B
A= defined $tokenB= $token->type eq ","
ABresult
0X0
100
111
284 or Asm::Preproc::Token->error_at($token,
285 "expected \",\" after macro parameter $param");
286 9 $input->next;
287 }
288 }
289
290 # expect end of statement, keep input at end of statement marker
291 21 $token = $input->peek;
292 21 ▶ (!defined($token) || $token->type =~ /^[:\n]$/)
Branch: 1 branch
BranchTrueFalseTotal
not defined $token or $token->type =~ /^[:\n]$/ or "Asm::Preproc::Token"->error_at($token, "too many macro arguments") 3 18 21
Condition66%2/3
not defined $token or $token->type =~ /^[:\n]$/
l!l&&r!l&&!r
0183
MC/DC50%1/2
not defined $token or $token->type =~ /^[:\n]$/
not defined $token $token->type =~ /^[:\n]$/
Truth table66%2/3
not(A) or B
A= not defined $tokenB= $token->type =~ /^[:\n]$/
ABresult
1X1
011
000
293 or Asm::Preproc::Token->error_at($token, "too many macro arguments");
294
295 18 return \%args;
296 }
297  
298 #------------------------------------------------------------------------------
299 # @tokens = _parse_argument($input)
300 # Extract the sequence of input tokens from $input into @tokens up to and
301 # not including the delimiter token
302 sub _parse_argument {
303 20 ▶ my($class, $input) = @_;
Subroutine _parse_argument: called CC=11 SCAR=24.5
304 20 my $token;
305  
306 # retrieve tokens
307 my @tokens;
308 20 my $parens = 0;
309 20 my $opened_brace;
310 20 ▶ while (defined($token = $input->peek)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($token = $input->peek)) 47 1 48
311 47 my $type = $token->type;
312 47 ▶ if ($type =~ /^[:\n,]$/ && $parens == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /^[:\n,]$/ and $parens == 0) { } 19 28 47
elsif ($type eq '{') { } 2 26 28
elsif ($type eq '}') { } 1 25 26
Condition100%3/3
$type =~ /^[:\n,]$/ and $parens == 0
!ll&&!rl&&r
25319
MC/DC100%2/2
$type =~ /^[:\n,]$/ and $parens == 0
$type =~ /^[:\n,]$/ $parens == 0
Truth table100%3/3
A and B
A= $type =~ /^[:\n,]$/B= $parens == 0
ABresult
0X0
100
111
313 19 last;
314 }
315 elsif ($type eq '{') {
316 2 $parens++;
317 2 ▶ push(@tokens, $token) if $opened_brace++;
Branch: 1 branch
BranchTrueFalseTotal
if $opened_brace++ 0 2 2
318 2 $input->next;
319 }
320 elsif ($type eq '}') {
321 1 ▶ if ($parens > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parens > 0) { } 1 0 1
322 1 $parens--;
323 1 ▶ push(@tokens, $token) if --$opened_brace;
Branch: 1 branch
BranchTrueFalseTotal
if --$opened_brace 0 1 1
324 1 $input->next;
325 }
326 else {
327 0 ▶ $input->next if $opened_brace; # skip delimiter
Branch: 1 branch
BranchTrueFalseTotal
if $opened_brace 0 0 0
328 0 last;
329 }
330 }
331 else {
332 25 push(@tokens, $token);
333 25 $input->next;
334 }
335 }
336 20 ▶ Asm::Preproc::Token->error_at($token, "unmatched braces")
Branch: 1 branch
BranchTrueFalseTotal
if $parens != 0 1 19 20
337 if $parens != 0;
338  
339 19 return @tokens;
340 }
341  
342 #------------------------------------------------------------------------------
343  
344 =head1 SYNTAX
345  
346 =head2 Macros
347  
348 Macros are created thus. This example creates an "instruction" called MAGIC
349 that takes two parameters:
350  
351 MACRO MAGIC param1, param2 {
352 LD param1, 0
353 BIT param2, L
354 label = 0x1234
355 ... more real instructions go here.
356 }
357  
358 Within the macro, param1, param2 etc will be replaced with whatever
359 parameters you pass to the macro. So, for example, this:
360  
361 MAGIC HL, 2
362  
363 Is the same as:
364  
365 LD HL, 0
366 BIT 2, L
367 ...
368  
369 Any labels that you define inside a macro are local to that macro. Actually
370 they're not but they get renamed to _macro_NN_... so that they
371 effectively *are* local.
372  
373 There is an alternative syntax, for compatibility with other assemblers, with exactly the
374 same effect.
375  
376 MACRO MAGIC param1, param2
377 LD param1, 0
378 BIT param2, L
379 label = 0x1234
380 ... more real instructions go here.
381 ENDM
382  
383 A ',' can be passed as part of a macro argument, by enclosing the arguments between {braces}.
384  
385 MACRO PAIR x {
386 LD x
387 }
388 PAIR {A,B}
389  
390 expands to:
391  
392 LD A,B
393  
394 =head1 BUGS and FEEDBACK
395  
396 See L<CPU::Z80::Assembler|CPU::Z80::Assembler>.
397  
398 =head1 SEE ALSO
399  
400 L<CPU::Z80::Assembler|CPU::Z80::Assembler>
401 L<Iterator::Simple::Lookahead|Iterator::Simple::Lookahead>
402  
403 =head1 AUTHORS, COPYRIGHT and LICENCE
404  
405 See L<CPU::Z80::Assembler|CPU::Z80::Assembler>.
406  
407 =cut
408  
409 #------------------------------------------------------------------------------
410  
411 36 1;
« lib/CPU/Z80/Assembler/Program.pm ↑ Summary lib/CPU/Z80/Assembler/Expr.pm »