VIC/Receiver.pm

filter: Statementstmt 88.4% 902 / 1020 Branchbran 67.8% 495 / 730 Conditioncond 50.0% 81 / 162 MC/DCmcdc 20.0% 20 / 100 Subroutinesub 93.1% 81 / 87 Podpod 1.5% 1 / 63 totaltotal 73.0% 1580 / 2162 SCAR 79.2
CC
444
Cov
77%
generate_code_blocks
128.1
generate_code_conditionals
52.1
generate_code_assign_expr
48.8
CRAP 2749.5
« VIC/PIC/Functions/Operators.pm ↑ Summary VIC.pm »
1 package VIC::Receiver;
2 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use bigint;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use List::MoreUtils qw(any firstidx indexes);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 our $VERSION = '0.29';
10 33 $VERSION = eval $VERSION;
11  
12 33 ▶use Pegex::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 33 extends 'Pegex::Tree';
14  
15 33 ▶use VIC::PIC::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 33 has pic_override => undef;
18 33 has pic => undef;
19 33 has simulator => undef;
20 33 has ast => {
21 block_stack => [],
22 block_mapping => {},
23 block_count => 0,
24 funcs => {},
25 variables => {},
26 tmp_variables => {},
27 conditionals => 0,
28 tmp_stack_size => 0,
29 strings => 0,
30 tables => [],
31 asserts => 0,
32 };
33 33 has intermediate_inline => undef;
34 33 has global_collections => {};
35  
36 431 ▶sub stack { reverse @{shift->parser->stack}; }
Subroutine stack: called CC=1 SCAR=0.0
Pod undocumented
37  
38 2 ▶sub supported_chips { return VIC::PIC::Any::supported_chips(); }
Subroutine supported_chips: called CC=1 SCAR=0.0
Pod undocumented
39  
40 1 ▶sub supported_simulators { return VIC::PIC::Any::supported_simulators(); }
Subroutine supported_simulators: called CC=1 SCAR=0.0
Pod undocumented
41  
42 19 ▶sub is_chip_supported { return VIC::PIC::Any::is_chip_supported(@_); }
Subroutine is_chip_supported: called CC=1 SCAR=0.0
Pod undocumented
43  
44 0 ▶sub is_simulator_supported { return VIC::PIC::Any::is_simulator_supported(@_); }
Subroutine is_simulator_supported: not called CC=1 SCAR=6.9
Pod undocumented
45  
46 0 ▶sub list_chip_features { return VIC::PIC::Any::list_chip_features(@_); }
Subroutine list_chip_features: not called CC=1 SCAR=6.9
Pod undocumented
47  
48 0 ▶sub print_pinout { return VIC::PIC::Any::print_pinout(@_); }
Subroutine print_pinout: not called CC=1 SCAR=6.9
Pod undocumented
49  
50 31 ▶sub current_chip { return $_[0]->pic->type; }
Subroutine current_chip: called CC=1 SCAR=0.0
Pod undocumented
51  
52 31 ▶sub current_simulator { return $_[0]->simulator->type; }
Subroutine current_simulator: called CC=1 SCAR=0.0
Pod undocumented
53  
54 sub got_mcu_select {
55 33 ▶ my ($self, $type) = @_;
Subroutine got_mcu_select: called CC=4 SCAR=14.6
Pod undocumented
56 # override the PIC in code if defined
57 33 ▶ $type = $self->pic_override if defined $self->pic_override;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->pic_override 0 33 33
58 33 $type = lc $type;
59 # assume supported type else return
60 33 $self->pic(VIC::PIC::Any->new($type));
61 33 ▶ unless (defined $self->pic and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->pic and defined $self->pic->type) 0 33 33
Condition33%1/3
defined $self->pic and defined $self->pic->type
!ll&&!rl&&r
0033
MC/DC0%0/2
defined $self->pic and defined $self->pic->type
defined $self->pic defined $self->pic->type
Truth table33%1/3
A and B
A= defined $self->picB= defined $self->pic->type
ABresult
0X0
100
111
62 defined $self->pic->type) {
63 0 $self->parser->throw_error("$type is not a supported chip");
64 }
65 33 $self->ast->{include} = $self->pic->include;
66 # set the defaults in case the headers are not provided by the user
67 33 $self->ast->{org} = $self->pic->org;
68 33 $self->ast->{chip_config} = $self->pic->get_chip_config;
69 33 $self->ast->{code_config} = $self->pic->code_config;
70 # create the default simulator
71 33 $self->simulator(VIC::PIC::Any->new_simulator(pic => $self->pic));
72 33 return;
73 }
74  
75 sub got_pragmas {
76 20 ▶ my ($self, $list) = @_;
Subroutine got_pragmas: called CC=9 SCAR=23.2
Pod undocumented
77 20 $self->flatten($list);
78 20 $self->pic->update_code_config(@$list);
79 # get the updated config
80 20 $self->ast->{chip_config} = $self->pic->get_chip_config;
81 20 $self->ast->{code_config} = $self->pic->code_config;
82 20 ▶ my ($sim, $stype) = @$list if scalar @$list;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$list 20 0 20
83 20 ▶ if ($sim eq 'simulator' and $stype !~ /disable/i) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sim eq 'simulator' and not $stype =~ /disable/i) { } 2 18 20
elsif ($sim eq 'simulator' and $stype =~ /disable/i) { } 1 34 35
Condition100%3/3
$sim eq "simulator" and not $stype =~ /disable/i
!ll&&!rl&&r
1712
MC/DC100%2/2
$sim eq "simulator" and not $stype =~ /disable/i
$sim eq "simulator" not $stype =~ /disable/i
Truth table100%3/3
A and not(B)
A= $sim eq "simulator"B= not $stype =~ /disable/i
ABresult
0X0
100
111
84 2 $self->simulator(VIC::PIC::Any->new_simulator(
85 type => $stype, pic => $self->pic));
86 2 ▶ if ($self->simulator) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simulator) { } 2 0 2
87 2 ▶ unless ($self->simulator->type eq $stype) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->simulator->type eq $stype) 0 2 2
88 0 warn "$stype is not a supported chip. Disabling simulator.";
89 0 $self->simulator->disable(1);
90 }
91 } else {
92 0 die "$stype is not a supported simulator.";
93 }
94 ▶ } elsif ($sim eq 'simulator' and $stype =~ /disable/i) {
Condition66%2/3
$sim eq "simulator" and $stype =~ /disable/i
!ll&&!rl&&r
1701
MC/DC50%1/2
$sim eq "simulator" and $stype =~ /disable/i
$sim eq "simulator" $stype =~ /disable/i
Truth table66%2/3
A and B
A= $sim eq "simulator"B= $stype =~ /disable/i
ABresult
0X0
100
111
95 1 ▶ $self->simulator->disable(1) if $self->simulator;
Branch: 1 branch
BranchTrueFalseTotal
if $self->simulator 1 0 1
96 }
97 20 return;
98 }
99  
100 sub handle_named_block {
101 94 ▶ my ($self, $name, $anon_block, $parent) = @_;
Subroutine handle_named_block: called CC=25 SCAR=33.5
Pod undocumented
102 94 ▶ my $id = $1 if $anon_block =~ /_anonblock(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $anon_block =~ /_anonblock(\d+)/ 94 0 94
103 94 ▶ $id = $self->ast->{block_count} unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 94 94
104 94 my ($expected_label, $expected_param) = ('', '');
105 94 ▶ if ($name eq 'Main') {
Branch: 7 branches
BranchTrueFalseTotal
if ($name eq 'Main') { } 32 62 94
elsif ($name =~ /^Loop/) { } 16 46 62
elsif ($name =~ /^Action/) { } 6 40 46
elsif ($name =~ /^True/) { } 12 28 40
elsif ($name =~ /^False/) { } 4 24 28
elsif ($name =~ /^ISR/) { } 5 19 24
elsif ($name eq 'Simulator') { } 19 0 19
106 32 $expected_label = "_start";
107 } elsif ($name =~ /^Loop/) {
108 16 $expected_label = "_loop_${id}";
109 } elsif ($name =~ /^Action/) {
110 6 $expected_label = "_action_${id}";
111 6 $expected_param = "action${id}_param";
112 } elsif ($name =~ /^True/) {
113 12 $expected_label = "_true_${id}";
114 } elsif ($name =~ /^False/) {
115 4 $expected_label = "_false_${id}";
116 } elsif ($name =~ /^ISR/) {
117 5 $expected_label = "_isr_${id}";
118 5 $expected_param = "isr${id}_param";
119 } elsif ($name eq 'Simulator') {
120 19 $expected_label = '_vic_simulator';
121 } else {
122 0 $expected_label = lc "_$name$id";
123 }
124 94 ▶ $name .= $id if $name =~ /^(?:Loop|Action|True|False|ISR)/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(?:Loop|Action|True|False|ISR)/ 43 51 94
125 94 $self->ast->{block_mapping}->{$name} = {
126 label => $expected_label,
127 block => $anon_block,
128 params => [],
129 param_prefix => $expected_param,
130 };
131 94 $self->ast->{block_mapping}->{$anon_block} = {
132 label => $expected_label,
133 block => $name,
134 params => [],
135 param_prefix => $expected_param,
136 };
137 # make sure the anon-block and named-block refer to the same block
138 94 $self->ast->{$name} = $self->ast->{$anon_block};
139  
140 94 ▶ my $stack = $self->ast->{$name} || $self->ast->{$anon_block};
Condition33%1/3
$self->ast->{$name} || $self->ast->{$anon_block}
l!l&&r!l&&!r
9400
MC/DC0%0/2
$self->ast->{$name} || $self->ast->{$anon_block}
$self->ast->{$name} $self->ast->{$anon_block}
Truth table33%1/3
A || B
A= $self->ast->{$name}B= $self->ast->{$anon_block}
ABresult
1X1
011
000
141 94 ▶ if (defined $stack and ref $stack eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stack and ref $stack eq "ARRAY") 94 0 94
Condition33%1/3
defined $stack and ref $stack eq "ARRAY"
!ll&&!rl&&r
0094
MC/DC0%0/2
defined $stack and ref $stack eq "ARRAY"
defined $stack ref $stack eq "ARRAY"
Truth table33%1/3
A and B
A= defined $stackB= ref $stack eq "ARRAY"
ABresult
0X0
100
111
142 94 my $block_label = $stack->[0];
143 ## this expression is dependent on got_start_block()
144 94 my ($tag, $label, @others) = split /::/, $block_label;
145 94 ▶ $label = $expected_label if $label ne $expected_label;
Branch: 1 branch
BranchTrueFalseTotal
if $label ne $expected_label 94 0 94
146 94 ▶ $block_label = "BLOCK::${label}::${name}" if $label;
Branch: 1 branch
BranchTrueFalseTotal
if $label 94 0 94
147 # change the LABEL:: value in the stack for code-generation ease
148 # we want to use the expected label and not the anon one unless it is an
149 # anon-block
150 94 $stack->[0] = join("::", $tag, $label, @others);
151 94 my $elabel = "_end$label"; # end label
152 94 my $slabel = $label; # start label
153 94 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) 43 51 94
154 43 ▶ unless ($parent =~ /BLOCK::/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent =~ /BLOCK::/) 43 0 43
155 43 $block_label .= "::$parent";
156 43 ▶ if (exists $self->ast->{$parent} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{$parent} and ref $self->ast->{$parent} eq "ARRAY" and $parent ne $anon_block) 43 0 43
Condition33%2/6
exists $self->ast->{$parent} and ref $self->ast->{$parent} eq "ARRAY" and $parent ne $anon_block
!ll&&!rl&&r
0043
exists $self->ast->{$parent} and ref $self->ast->{$parent} eq "ARRAY"
!ll&&!rl&&r
0043
MC/DC0%0/3
exists $self->ast->{$parent} and ref $self->ast->{$parent} eq "ARRAY" and $parent ne $anon_block
exists $self->ast->{$parent} ref $self->ast->{$parent} eq "ARRAY" $parent ne $anon_block
Truth table25%1/4
A and B and C
A= exists $self->ast->{$parent}B= ref $self->ast->{$parent} eq "ARRAY"C= $parent ne $anon_block
ABCresult
0XX0
10X0
1100
1111
157 ref $self->ast->{$parent} eq 'ARRAY' and
158 $parent ne $anon_block) {
159 43 my ($ptag, $plabel) = split /::/, $self->ast->{$parent}->[0];
160 43 ▶ $block_label .= "::$plabel" if $plabel;
Branch: 1 branch
BranchTrueFalseTotal
if $plabel 43 0 43
161 }
162 }
163 43 my $ccount = $self->ast->{conditionals};
164 43 ▶ if ($block_label =~ /True|False/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_label =~ /True|False/i) 16 27 43
165 16 $elabel = "_end_conditional_$ccount";
166 16 $slabel = "_start_conditional_$ccount";
167 }
168 43 $block_label .= "::$elabel";
169 43 ▶ $block_label .= "::$expected_param" if length $expected_param;
Branch: 1 branch
BranchTrueFalseTotal
if length $expected_param 11 32 43
170 43 push @{$self->ast->{$parent}}, $block_label;
171 }
172 # save this for referencing when we need to know what the parent of
173 # this block is in case we need to jump out of the block
174 94 $self->ast->{block_mapping}->{$name}->{parent} = $parent;
175 94 $self->ast->{block_mapping}->{$anon_block}->{parent} = $parent;
176 94 $self->ast->{block_mapping}->{$name}->{end_label} = $elabel;
177 94 $self->ast->{block_mapping}->{$anon_block}->{end_label} = $elabel;
178 94 $self->ast->{block_mapping}->{$name}->{start_label} = $slabel;
179 94 $self->ast->{block_mapping}->{$anon_block}->{start_label} = $slabel;
180 94 ▶ $self->ast->{block_mapping}->{$anon_block}->{loop} = '1' if $block_label =~ /Loop/i;
Branch: 1 branch
BranchTrueFalseTotal
if $block_label =~ /Loop/i 16 78 94
181 94 return $block_label;
182 }
183 }
184  
185 sub got_named_block {
186 78 ▶ my ($self, $list) = @_;
Subroutine got_named_block: called CC=2 SCAR=7.0
Pod undocumented
187 78 ▶ $self->flatten($list) if ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list eq "ARRAY" 78 0 78
188 78 my ($name, $anon_block, $parent_block) = @$list;
189 78 return $self->handle_named_block(@$list);
190 }
191  
192 sub got_anonymous_block {
193 94 ▶ my $self = shift;
Subroutine got_anonymous_block: called CC=1 SCAR=0.0
Pod undocumented
194 94 my $list = shift;
195 94 my ($anon_block, $block_stack, $parent) = @$list;
196 # returns anon_block and parent_block
197 94 return [$anon_block, $parent];
198 }
199  
200 sub got_start_block {
201 95 ▶ my ($self, $list) = @_;
Subroutine got_start_block: called CC=1 SCAR=0.0
Pod undocumented
202 95 my $id = $self->ast->{block_count};
203 # we may not know the block name here
204 95 my $block = lc "_anonblock$id";
205 95 push @{$self->ast->{block_stack}}, $block;
206 95 $self->ast->{$block} = [ "LABEL::$block" ];
207 95 $self->ast->{block_count}++;
208 95 return $block;
209 }
210  
211 sub got_end_block {
212 94 ▶ my ($self, $list) = @_;
Subroutine got_end_block: called CC=1 SCAR=0.0
Pod undocumented
213 # we are not capturing anything here
214 94 my $stack = $self->ast->{block_stack};
215 94 my $block = pop @$stack;
216 94 return $stack->[-1];
217 }
218  
219 sub got_name {
220 779 ▶ my ($self, $list) = @_;
Subroutine got_name: called CC=2 SCAR=7.4
Pod undocumented
221 779 ▶ if (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 779 0 779
222 779 $self->flatten($list);
223 779 return shift(@$list);
224 } else {
225 0 return $list;
226 }
227 }
228  
229 sub update_intermediate {
230 381 ▶ my $self = shift;
Subroutine update_intermediate: called CC=2 SCAR=7.0
Pod undocumented
231 381 my $block = $self->ast->{block_stack}->[-1];
232 381 ▶ push @{$self->ast->{$block}}, @_ if $block;
Branch: 1 branch
BranchTrueFalseTotal
if $block 381 0 381
233 381 return;
234 }
235  
236 sub got_instruction {
237 246 ▶ my ($self, $list) = @_;
Subroutine got_instruction: called CC=7 SCAR=19.5
Pod undocumented
238 246 my $method = shift @$list;
239 246 ▶ $self->flatten($list) if $list;
Branch: 1 branch
BranchTrueFalseTotal
if $list 246 0 246
240 246 my $tag = 'INS';
241 # check if it is a simulator method
242 246 ▶ if ($self->simulator and $self->simulator->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simulator and $self->simulator->can($method)) { } 107 139 246
Condition66%2/3
$self->simulator and $self->simulator->can($method)
!ll&&!rl&&r
0139107
MC/DC50%1/2
$self->simulator and $self->simulator->can($method)
$self->simulator $self->simulator->can($method)
Truth table66%2/3
A and B
A= $self->simulatorB= $self->simulator->can($method)
ABresult
0X0
100
111
243 # this is a simulator instruction
244 107 $tag = 'SIM';
245 } else {
246 139 ▶ unless ($self->pic->can($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->pic->can($method)) 1 138 139
247 1 my $err = "Unsupported instruction '$method' for chip " . uc $self->pic->type;
248 1 return $self->parser->throw_error($err);
249 }
250 }
251 245 my @args = ();
252 245 ▶ while (scalar @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$list) 355 245 600
253 355 my $a = shift @$list;
254 355 ▶ if ($a =~ /BLOCK::(\w+)::(Action|ISR)\w+::.*::(_end_\w+)::(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /BLOCK::(\w+)::(Action|ISR)\w+::.*::(_end_\w+)::(\w+)$/) { } 11 344 355
255 11 push @args, uc($2) . "::$1::END::$3::PARAM::$4";
256 } else {
257 344 push @args, $a;
258 }
259 }
260 245 $self->update_intermediate("${tag}::${method}::" . join ("::", @args));
261 245 return;
262 }
263  
264 sub got_unary_rhs {
265 1 ▶ my ($self, $list) = @_;
Subroutine got_unary_rhs: called CC=1 SCAR=0.0
Pod undocumented
266 1 $self->flatten($list);
267 1 return [ reverse @$list ];
268 }
269  
270 sub got_unary_expr {
271 8 ▶ my ($self, $list) = @_;
Subroutine got_unary_expr: called CC=1 SCAR=0.0
Pod undocumented
272 8 $self->flatten($list);
273 8 my $op = shift @$list;
274 8 my $varname = shift @$list;
275 8 $self->update_intermediate("UNARY::${op}::${varname}");
276 8 return;
277 }
278  
279 sub got_assign_expr {
280 85 ▶ my ($self, $list) = @_;
Subroutine got_assign_expr: called CC=3 SCAR=11.0
Pod undocumented
281 85 $self->flatten($list);
282 85 my $varname = shift @$list;
283 85 my $op = shift @$list;
284 85 my $rhsx = $self->got_expr_value($list);
285 85 ▶ my $rhs = ref $rhsx eq 'ARRAY' ? join ("::", @$rhsx) : $rhsx;
Branch: 1 branch
BranchTrueFalseTotal
ref $rhsx eq 'ARRAY' ? : 0 85 85
286 85 ▶ if ($rhs =~ /PARAM::(\w+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs =~ /PARAM::(\w+)/) { } 5 80 85
287 ## ok now we push this as our statement and handle the rest during
288 ## code generation
289 ## this is of the format PARAM::op::block_name::variable
290 5 my $block = $1;
291 5 $self->update_intermediate("PARAM::${op}::${block}::${varname}");
292 } else {
293 80 $self->update_intermediate("SET::${op}::${varname}::${rhs}");
294 }
295 85 return;
296 }
297  
298 sub got_array_element {
299 1 ▶ my ($self, $list) = @_;
Subroutine got_array_element: called CC=10 SCAR=31.9
Pod undocumented
300 1 my $var1 = shift @$list;
301 1 my $rhsx = $self->got_expr_value($list);
302 1 ▶ if (ref $rhsx eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rhsx eq "ARRAY") 0 1 1
303 0 XXX $rhsx; # why would this even happen
304 }
305 1 my $tvref = $self->ast->{tmp_variables};
306 1 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$tvref);
307 1 my $vref = $self->ast->{variables}->{$var1};
308 1 my @ops = ('OP');
309 1 ▶ if (exists $vref->{type} and $vref->{type} eq 'HASH') {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $vref->{'type'} and $vref->{'type'} eq 'HASH') { } 1 0 1
elsif (exists $vref->{'type'} and $vref->{'type'} eq 'ARRAY') { } 0 0 0
elsif (exists $vref->{'type'} and $vref->{'type'} eq 'string') { } 0 0 0
Condition33%1/3
exists $vref->{"type"} and $vref->{"type"} eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
exists $vref->{"type"} and $vref->{"type"} eq "HASH"
exists $vref->{"type"} $vref->{"type"} eq "HASH"
Truth table33%1/3
A and B
A= exists $vref->{"type"}B= $vref->{"type"} eq "HASH"
ABresult
0X0
100
111
310 1 push @ops, $vref->{label}, 'TBLIDX', $rhsx, $vref->{size};
311 ▶ } elsif (exists $vref->{type} and $vref->{type} eq 'ARRAY') {
Condition0%0/3
exists $vref->{"type"} and $vref->{"type"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $vref->{"type"} and $vref->{"type"} eq "ARRAY"
exists $vref->{"type"} $vref->{"type"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $vref->{"type"}B= $vref->{"type"} eq "ARRAY"
ABresult
0X0
100
111
312 0 push @ops, $vref->{label}, 'ARRIDX', $rhsx, $vref->{size};
313 ▶ } elsif (exists $vref->{type} and $vref->{type} eq 'string') {
Condition0%0/3
exists $vref->{"type"} and $vref->{"type"} eq "string"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $vref->{"type"} and $vref->{"type"} eq "string"
exists $vref->{"type"} $vref->{"type"} eq "string"
Truth table0%0/3
A and B
A= exists $vref->{"type"}B= $vref->{"type"} eq "string"
ABresult
0X0
100
111
314 0 push @ops, $vref->{label}, 'STRIDX', $rhsx, $vref->{size};
315 } else {
316 # this must be a byte
317 0 return $self->parser->throw_error(
318 "Variable '$var1' is not an array, table or string");
319 }
320 1 $tvref->{$tvar} = join("::", @ops);
321 # create a new variable here
322 1 my $varname = sprintf "vic_el_%02d", scalar(keys %$tvref);
323 1 $varname = $self->got_variable([$varname]);
324 1 ▶ if ($varname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($varname) 1 0 1
325 1 $self->update_intermediate("SET::ASSIGN::${varname}::${tvar}");
326 1 return $varname;
327 }
328 0 ▶ return $self->parser->throw_error(
Branch: 1 branch
BranchTrueFalseTotal
unless $varname 0 0 0
329 "Unable to create intermediary variable '$varname'") unless $varname;
330 }
331  
332 sub got_parameter {
333 5 ▶ my $self = shift;
Subroutine got_parameter: called CC=1 SCAR=0.0
Pod undocumented
334 ## ok the target variable needs a parameter here
335 ## this works only in block scope so we want to check which block we are in
336 5 my $block = $self->ast->{block_stack}->[-1];
337 5 return "PARAM::$block";
338 }
339  
340 sub got_declaration {
341 3 ▶ my ($self, $list) = @_;
Subroutine got_declaration: called CC=16 SCAR=36.0
Pod undocumented
342 3 my $lhs = shift @$list;
343 3 my $rhs;
344 3 ▶ if (scalar @$list == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$list == {sign => '+', value => ['1']}) { } 3 0 3
345 3 $rhs = shift @$list;
346 } else {
347 0 $rhs = $list;
348 }
349 # TODO: generate intermediate code here
350 3 ▶ if (ref $rhs eq 'HASH' or ref $rhs eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rhs eq 'HASH' or ref $rhs eq 'ARRAY') { } 3 0 3
Condition33%1/3
ref $rhs eq "HASH" or ref $rhs eq "ARRAY"
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $rhs eq "HASH" or ref $rhs eq "ARRAY"
ref $rhs eq "HASH" ref $rhs eq "ARRAY"
Truth table33%1/3
A or B
A= ref $rhs eq "HASH"B= ref $rhs eq "ARRAY"
ABresult
1X1
011
000
351 3 ▶ if (not exists $self->ast->{variables}->{$lhs}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->ast->{"variables"}{$lhs}) 0 3 3
352 0 return $self->parser->throw_error("Variable '$lhs' doesn't exist");
353 }
354 3 ▶ if (exists $rhs->{TABLE} or ref $rhs eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $rhs->{'TABLE'} or ref $rhs eq 'ARRAY') { } 1 2 3
elsif (exists $rhs->{'string'}) { } 2 0 2
Condition66%2/3
exists $rhs->{"TABLE"} or ref $rhs eq "ARRAY"
l!l&&r!l&&!r
102
MC/DC50%1/2
exists $rhs->{"TABLE"} or ref $rhs eq "ARRAY"
exists $rhs->{"TABLE"} ref $rhs eq "ARRAY"
Truth table66%2/3
A or B
A= exists $rhs->{"TABLE"}B= ref $rhs eq "ARRAY"
ABresult
1X1
011
000
355 1 ▶ my $label = lc "_table_$lhs" if ref $rhs eq 'HASH' and exists $rhs->{TABLE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rhs eq "HASH" and exists $rhs->{"TABLE"} 1 0 1
Condition33%1/3
ref $rhs eq "HASH" and exists $rhs->{"TABLE"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $rhs eq "HASH" and exists $rhs->{"TABLE"}
ref $rhs eq "HASH" exists $rhs->{"TABLE"}
Truth table33%1/3
A and B
A= ref $rhs eq "HASH"B= exists $rhs->{"TABLE"}
ABresult
0X0
100
111
356 1 ▶ my $szpref = "VIC_TBLSZ_" if ref $rhs eq 'HASH' and exists $rhs->{TABLE};
Branch: 1 branch
BranchTrueFalseTotal
if ref $rhs eq "HASH" and exists $rhs->{"TABLE"} 1 0 1
Condition33%1/3
ref $rhs eq "HASH" and exists $rhs->{"TABLE"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $rhs eq "HASH" and exists $rhs->{"TABLE"}
ref $rhs eq "HASH" exists $rhs->{"TABLE"}
Truth table33%1/3
A and B
A= ref $rhs eq "HASH"B= exists $rhs->{"TABLE"}
ABresult
0X0
100
111
357 1 ▶ $szpref = "VIC_ARRSZ_" if ref $rhs eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $rhs eq "ARRAY" 0 1 1
358 1 $self->ast->{variables}->{$lhs}->{type} = ref $rhs;
359 1 $self->ast->{variables}->{$lhs}->{data} = $rhs;
360 1 ▶ $self->ast->{variables}->{$lhs}->{label} = $label || $lhs;
Condition33%1/3
$label || $lhs
l!l&&r!l&&!r
100
MC/DC0%0/2
$label || $lhs
$label $lhs
Truth table33%1/3
A || B
A= $labelB= $lhs
ABresult
1X1
011
000
361 1 ▶ if ($szpref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($szpref) 1 0 1
362 $self->ast->{variables}->{$lhs}->{size} = $szpref .
363 1 $self->ast->{variables}->{$lhs}->{name};
364 }
365 } elsif (exists $rhs->{string}) {
366 # handle variable that are strings here
367 2 $self->ast->{variables}->{$lhs}->{data} = $rhs;
368 2 $self->ast->{variables}->{$lhs}->{type} = 'string';
369 $self->ast->{variables}->{$lhs}->{size} = "VIC_STRSZ_" .
370 2 $self->ast->{variables}->{$lhs}->{name};
371 2 $self->update_intermediate("SET::ASSIGN::${lhs}::${rhs}");
372 } else {
373 0 return $self->parser->throw_error("We should not be here");
374 }
375 } else {
376 # var = number | string etc.
377 0 ▶ if ($rhs =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs =~ /^-?\d+$/) { } 0 0 0
378 # we just use the got_assign_expr. this should never be called in
379 # reality but is here in case the grammar rules change
380 0 $self->update_intermediate("SET::ASSIGN::${lhs}::${rhs}");
381 } else {
382 #VIKAS: check this!
383 # handle strings here
384 0 $self->ast->{variables}->{$lhs}->{type} = 'string';
385 0 $self->ast->{variables}->{$lhs}->{data} = $rhs;
386 0 $self->ast->{variables}->{$lhs}->{label} = $lhs;
387 $self->ast->{variables}->{$lhs}->{size} = "VIC_STRSZ_" .
388 0 $self->ast->{variables}->{$lhs}->{name};
389 }
390 }
391 3 return;
392 }
393  
394 sub got_conditional_statement {
395 12 ▶ my ($self, $list) = @_;
Subroutine got_conditional_statement: called CC=29 SCAR=36.2
Pod undocumented
396 12 my ($type, $subject, $predicate) = @$list;
397 12 ▶ return unless scalar @$predicate;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @$predicate 0 12 12
398 12 ▶ my $is_loop = ($type eq 'while') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'while' ? : 4 8 12
399 12 my ($current, $parent) = $self->stack;
400 12 my $subcond = 0;
401 12 ▶ $subcond = 1 if $parent =~ /^conditional/;
Branch: 1 branch
BranchTrueFalseTotal
if $parent =~ /^conditional/ 3 9 12
402 12 ▶ if (ref $predicate ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $predicate ne "ARRAY") 0 12 12
403 0 $predicate = [ $predicate ];
404 }
405 12 my @condblocks = ();
406 12 ▶ if (scalar @$predicate < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$predicate < {sign => '+', value => ['3']}) { } 12 0 12
407 12 ▶ my $tb = $predicate->[0] || undef;
Condition50%1/2
$$predicate[{sign => '+', value => ['0']}] || undef
l!l
120
MC/DC0%0/1
$$predicate[{sign => '+', value => ['0']}] || undef
$$predicate[{sign => '+', value => ['0']}]
Truth table50%1/2
A
A= $$predicate[{sign => '+', value => ['0']}]
Aresult
11
00
408 12 ▶ my $fb = $predicate->[1] || undef;
Condition50%1/2
$$predicate[{sign => '+', value => ['1']}] || undef
l!l
120
MC/DC0%0/1
$$predicate[{sign => '+', value => ['1']}] || undef
$$predicate[{sign => '+', value => ['1']}]
Truth table50%1/2
A
A= $$predicate[{sign => '+', value => ['1']}]
Aresult
11
00
409 12 ▶ $self->flatten($tb) if $tb;
Branch: 1 branch
BranchTrueFalseTotal
if $tb 12 0 12
410 12 ▶ $self->flatten($fb) if $fb;
Branch: 1 branch
BranchTrueFalseTotal
if $fb 12 0 12
411 12 ▶ my $true_block = $self->handle_named_block('True', @$tb) if $tb and scalar @$tb;
Branch: 1 branch
BranchTrueFalseTotal
if $tb and scalar @$tb 12 0 12
Condition50%1/2
$tb and scalar @$tb
!ll
012
MC/DC0%0/1
$tb and scalar @$tb
$tb
Truth table50%1/2
A
A= $tb
Aresult
00
11
412 12 ▶ push @condblocks, $true_block if $true_block;
Branch: 1 branch
BranchTrueFalseTotal
if $true_block 12 0 12
413 12 ▶ my $false_block = $self->handle_named_block('False', @$fb) if $fb and scalar @$fb;
Branch: 1 branch
BranchTrueFalseTotal
if $fb and scalar @$fb 4 8 12
Condition50%1/2
$fb and scalar @$fb
!ll
012
MC/DC0%0/1
$fb and scalar @$fb
$fb
Truth table50%1/2
A
A= $fb
Aresult
00
11
414 12 ▶ push @condblocks, $false_block if $false_block;
Branch: 1 branch
BranchTrueFalseTotal
if $false_block 4 8 12
415 } else {
416 0 return $self->parser->throw_error("Multiple predicate conditionals not implemented");
417 }
418 12 my $inter;
419 12 ▶ if (scalar @condblocks < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @condblocks < {sign => '+', value => ['3']}) { } 12 0 12
420 12 my ($false_label, $true_label, $end_label);
421 12 my ($false_name, $true_name);
422 12 foreach my $p (@condblocks) {
423 16 ▶ ($false_label, $false_name) = ($1, $2) if $p =~ /BLOCK::(\w+)::(False\d+)::/;
Branch: 1 branch
BranchTrueFalseTotal
if $p =~ /BLOCK::(\w+)::(False\d+)::/ 4 12 16
424 16 ▶ ($true_label, $true_name) = ($1, $2) if $p =~ /BLOCK::(\w+)::(True\d+)::/;
Branch: 1 branch
BranchTrueFalseTotal
if $p =~ /BLOCK::(\w+)::(True\d+)::/ 12 4 16
425 16 ▶ $end_label = $1 if $p =~ /BLOCK::.*::(_end_conditional\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $p =~ /BLOCK::.*::(_end_conditional\w+)$/ 16 0 16
426 }
427 12 ▶ $false_label = $end_label unless defined $false_label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $false_label 8 4 12
428 12 ▶ $true_label = $end_label unless defined $true_label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $true_label 0 12 12
429 12 my $subj = $subject;
430 12 ▶ $subj = shift @$subject if ref $subject eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $subject eq "ARRAY" 0 12 12
431 $inter = join("::",
432 COND => $self->ast->{conditionals},
433 12 SUBJ => $subj,
434 FALSE => $false_label,
435 TRUE => $true_label,
436 END => $end_label,
437 LOOP => $is_loop,
438 SUBCOND => $subcond);
439 12 my $mapping = $self->ast->{block_mapping};
440 12 ▶ if ($true_name and exists $mapping->{$true_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($true_name and exists $mapping->{$true_name}) 12 0 12
Condition33%1/3
$true_name and exists $mapping->{$true_name}
!ll&&!rl&&r
0012
MC/DC0%0/2
$true_name and exists $mapping->{$true_name}
$true_name exists $mapping->{$true_name}
Truth table33%1/3
A and B
A= $true_nameB= exists $mapping->{$true_name}
ABresult
0X0
100
111
441 12 $mapping->{$true_name}->{loop} = "$is_loop";
442 12 my $ab = $mapping->{$true_name}->{block};
443 12 $mapping->{$ab}->{loop} = "$is_loop";
444 }
445 12 ▶ if ($false_name and exists $mapping->{$false_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($false_name and exists $mapping->{$false_name}) 4 8 12
Condition66%2/3
$false_name and exists $mapping->{$false_name}
!ll&&!rl&&r
804
MC/DC50%1/2
$false_name and exists $mapping->{$false_name}
$false_name exists $mapping->{$false_name}
Truth table66%2/3
A and B
A= $false_nameB= exists $mapping->{$false_name}
ABresult
0X0
100
111
446 4 $mapping->{$false_name}->{loop} = "$is_loop";
447 4 my $ab = $mapping->{$false_name}->{block};
448 4 $mapping->{$ab}->{loop} = "$is_loop";
449 }
450 } else {
451 0 return $self->parser->throw_error("Multiple predicate conditionals not implemented");
452 }
453 12 $self->update_intermediate($inter);
454 12 ▶ $self->ast->{conditionals}++ unless $subcond;
Branch: 1 branch
BranchTrueFalseTotal
unless $subcond 9 3 12
455 12 return;
456 }
457  
458 ##WARNING: do not change this function without looking at its effect on
459 #got_conditional_statement() above which calls this function explicitly
460 # this function is identical to got_expr_value() and hence redundant
461 # we may need to just use the same one although precedence will be different
462 # so maybe not
463 sub got_conditional_subject {
464 16 ▶ my ($self, $list) = @_;
Subroutine got_conditional_subject: called CC=8 SCAR=21.1
Pod undocumented
465 16 ▶ if (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 16 0 16
466 16 $self->flatten($list);
467 16 ▶ if (scalar @$list == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @$list == {sign => '+', value => ['1']}) { } 4 12 16
elsif (scalar @$list == {sign => '+', value => ['2']}) { } 0 12 12
elsif (scalar @$list == {sign => '+', value => ['3']}) { } 11 1 12
468 4 my $var1 = shift @$list;
469 4 ▶ return $var1 if $var1 =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $var1 =~ /^\d+$/ 1 3 4
470 3 my $vref = $self->ast->{tmp_variables};
471 3 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$vref);
472 3 $vref->{$tvar} = "OP::${var1}::EQ::1";
473 3 return $tvar;
474 } elsif (scalar @$list == 2) {
475 0 my ($op, $var) = @$list;
476 0 my $vref = $self->ast->{tmp_variables};
477 0 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$vref);
478 0 $vref->{$tvar} = "OP::${op}::${var}";
479 0 return $tvar;
480 } elsif (scalar @$list == 3) {
481 11 my ($var1, $op, $var2) = @$list;
482 11 my $vref = $self->ast->{tmp_variables};
483 11 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$vref);
484 11 $vref->{$tvar} = "OP::${var1}::${op}::${var2}";
485 11 return $tvar;
486 } else {
487 # handle precedence with left-to-right association
488 1 my @arr = @$list;
489 1 ▶ my $idx = firstidx { $_ =~ /^GE|GT|LE|LT|EQ|NE$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
490 1 ▶ while ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 2 1 3
491 2 my $res = $self->got_conditional_subject([$arr[$idx - 1], $arr[$idx], $arr[$idx + 1]]);
492 2 $arr[$idx - 1] = $res;
493 2 splice @arr, $idx, 2; # remove the extra elements
494 2 ▶ $idx = firstidx { $_ =~ /^GE|GT|LE|LT|EQ|NE$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
495 }
496 1 ▶ $idx = firstidx { $_ =~ /^AND|OR$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
497 1 ▶ while ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 1 1 2
498 1 my $res = $self->got_conditional_subject([$arr[$idx - 1], $arr[$idx], $arr[$idx + 1]]);
499 1 $arr[$idx - 1] = $res;
500 1 splice @arr, $idx, 2; # remove the extra elements
501 1 ▶ $idx = firstidx { $_ =~ /^AND|OR$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
502 }
503 # YYY $self->ast->{tmp_variables};
504 1 return $self->got_conditional_subject([@arr]);
505 }
506 } else {
507 0 return $list;
508 }
509 }
510  
511 ##WARNING: do not change this function without looking at its effect on
512 #got_assign_expr() above which calls this function explicitly
513 sub got_expr_value {
514 296 ▶ my ($self, $list) = @_;
Subroutine got_expr_value: called CC=11 SCAR=24.9
Pod undocumented
515 296 ▶ if (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 296 0 296
516 296 $self->flatten($list);
517 296 ▶ if (scalar @$list == 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (scalar @$list == {sign => '+', value => ['1']}) { } 236 60 296
elsif (scalar @$list == {sign => '+', value => ['2']}) { } 8 52 60
elsif (scalar @$list == {sign => '+', value => ['3']}) { } 46 6 52
elsif (scalar @$list > {sign => '+', value => ['3']}) { } 6 0 6
518 236 my $val = shift @$list;
519 236 ▶ if ($val =~ /MOP::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /MOP::/) { } 2 234 236
520 2 my $vref = $self->ast->{tmp_variables};
521 2 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$vref);
522 2 $vref->{$tvar} = $val;
523 2 return $tvar;
524 } else {
525 234 return $val;
526 }
527 } elsif (scalar @$list == 2) {
528 8 my ($op, $var) = @$list;
529 8 my $vref = $self->ast->{tmp_variables};
530 8 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$vref);
531 8 $vref->{$tvar} = "OP::${op}::${var}";
532 8 return $tvar;
533 } elsif (scalar @$list == 3) {
534 46 my ($var1, $op, $var2) = @$list;
535 46 my $vref = $self->ast->{tmp_variables};
536 46 my $tvar = sprintf "_vic_tmp_%02d", scalar(keys %$vref);
537 46 $vref->{$tvar} = "OP::${var1}::${op}::${var2}";
538 46 return $tvar;
539 } elsif (scalar @$list > 3) {
540 # handle precedence with left-to-right association
541 6 my @arr = @$list;
542 6 ▶ my $idx = firstidx { $_ =~ /^MUL|DIV|MOD$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
543 6 ▶ while ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 8 6 14
544 8 my $res = $self->got_expr_value([$arr[$idx - 1], $arr[$idx], $arr[$idx + 1]]);
545 8 $arr[$idx - 1] = $res;
546 8 splice @arr, $idx, 2; # remove the extra elements
547 8 ▶ $idx = firstidx { $_ =~ /^MUL|DIV|MOD$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
548 }
549 6 ▶ $idx = firstidx { $_ =~ /^ADD|SUB$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
550 6 ▶ while ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 8 6 14
551 8 my $res = $self->got_expr_value([$arr[$idx - 1], $arr[$idx], $arr[$idx + 1]]);
552 8 $arr[$idx - 1] = $res;
553 8 splice @arr, $idx, 2; # remove the extra elements
554 8 ▶ $idx = firstidx { $_ =~ /^ADD|SUB$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
555 }
556 6 ▶ $idx = firstidx { $_ =~ /^SHL|SHR$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
557 6 ▶ while ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 0 6 6
558 0 my $res = $self->got_expr_value([$arr[$idx - 1], $arr[$idx], $arr[$idx + 1]]);
559 0 $arr[$idx - 1] = $res;
560 0 splice @arr, $idx, 2; # remove the extra elements
561 0 ▶ $idx = firstidx { $_ =~ /^SHL|SHR$/ } @arr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
562 }
563 6 ▶ $idx = firstidx { $_ =~ /^BAND|BXOR|BOR$/ } @arr;
Subroutine __ANON__: called CC=1 SCAR=0.0
564 6 ▶ while ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 0 6 6
565 0 my $res = $self->got_expr_value([$arr[$idx - 1], $arr[$idx], $arr[$idx + 1]]);
566 0 $arr[$idx - 1] = $res;
567 0 splice @arr, $idx, 2; # remove the extra elements
568 0 ▶ $idx = firstidx { $_ =~ /^BAND|BXOR|BOR$/ } @arr;
Subroutine __ANON__: not called CC=1 SCAR=6.9
569 }
570 # YYY $self->ast->{tmp_variables};
571 6 return $self->got_expr_value([@arr]);
572 } else {
573 0 return $list;
574 }
575 } else {
576 0 return $list;
577 }
578 }
579  
580 sub got_math_operator {
581 42 ▶ my ($self, $op) = @_;
Subroutine got_math_operator: called CC=6 SCAR=18.0
Pod undocumented
582 42 ▶ return 'ADD' if $op eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "+" 16 26 42
583 26 ▶ return 'SUB' if $op eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "-" 6 20 26
584 20 ▶ return 'MUL' if $op eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "*" 10 10 20
585 10 ▶ return 'DIV' if $op eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "/" 6 4 10
586 4 ▶ return 'MOD' if $op eq '%';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "%" 4 0 4
587 0 return $self->parser->throw_error("Math operator '$op' is not supported");
588 }
589  
590 sub got_shift_operator {
591 4 ▶ my ($self, $op) = @_;
Subroutine got_shift_operator: called CC=3 SCAR=11.4
Pod undocumented
592 4 ▶ return 'SHL' if $op eq '<<';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<<" 2 2 4
593 2 ▶ return 'SHR' if $op eq '>>';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">>" 2 0 2
594 0 return $self->parser->throw_error("Shift operator '$op' is not supported");
595 }
596  
597 sub got_bit_operator {
598 0 ▶ my ($self, $op) = @_;
Subroutine got_bit_operator: not called CC=4 SCAR=30.0
Pod undocumented
599 0 ▶ return 'BXOR' if $op eq '^';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "^" 0 0 0
600 0 ▶ return 'BOR' if $op eq '|';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "|" 0 0 0
601 0 ▶ return 'BAND' if $op eq '&';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "&" 0 0 0
602 0 return $self->parser->throw_error("Bitwise operator '$op' is not supported");
603 }
604  
605 sub got_logic_operator {
606 2 ▶ my ($self, $op) = @_;
Subroutine got_logic_operator: called CC=3 SCAR=11.4
Pod undocumented
607 2 ▶ return 'AND' if $op eq '&&';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "&&" 1 1 2
608 1 ▶ return 'OR' if $op eq '||';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "||" 1 0 1
609 0 return $self->parser->throw_error("Logic operator '$op' is not supported");
610 }
611  
612 sub got_compare_operator {
613 37 ▶ my ($self, $op) = @_;
Subroutine got_compare_operator: called CC=7 SCAR=20.0
Pod undocumented
614 37 ▶ return 'LE' if $op eq '<=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<=" 1 36 37
615 36 ▶ return 'LT' if $op eq '<';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<" 0 36 36
616 36 ▶ return 'GE' if $op eq '>=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">=" 0 36 36
617 36 ▶ return 'GT' if $op eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">" 1 35 36
618 35 ▶ return 'EQ' if $op eq '==';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "==" 31 4 35
619 4 ▶ return 'NE' if $op eq '!=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "!=" 4 0 4
620 0 return $self->parser->throw_error("Compare operator '$op' is not supported");
621 }
622  
623 sub got_complement_operator {
624 8 ▶ my ($self, $op) = @_;
Subroutine got_complement_operator: called CC=3 SCAR=16.5
Pod undocumented
625 8 ▶ return 'NOT' if $op eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "!" 8 0 8
626 0 ▶ return 'COMP' if $op eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "~" 0 0 0
627 0 return $self->parser->throw_error("Complement operator '$op' is not supported");
628 }
629  
630 sub got_assign_operator {
631 88 ▶ my ($self, $op) = @_;
Subroutine got_assign_operator: called CC=14 SCAR=26.6
Pod undocumented
632 88 ▶ if (ref $op eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $op eq "ARRAY") 0 88 88
633 0 $self->flatten($op);
634 0 $op = shift @$op;
635 }
636 88 ▶ return 'ASSIGN' if $op eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "=" 63 25 88
637 25 ▶ return 'ADD_ASSIGN' if $op eq '+=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "+=" 2 23 25
638 23 ▶ return 'SUB_ASSIGN' if $op eq '-=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "-=" 2 21 23
639 21 ▶ return 'MUL_ASSIGN' if $op eq '*=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "*=" 2 19 21
640 19 ▶ return 'DIV_ASSIGN' if $op eq '/=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "/=" 2 17 19
641 17 ▶ return 'MOD_ASSIGN' if $op eq '%=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "%=" 2 15 17
642 15 ▶ return 'BXOR_ASSIGN' if $op eq '^=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "^=" 2 13 15
643 13 ▶ return 'BOR_ASSIGN' if $op eq '|=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "|=" 2 11 13
644 11 ▶ return 'BAND_ASSIGN' if $op eq '&=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "&=" 3 8 11
645 8 ▶ return 'SHL_ASSIGN' if $op eq '<<=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<<=" 2 6 8
646 6 ▶ return 'SHR_ASSIGN' if $op eq '>>=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">>=" 5 1 6
647 1 ▶ return 'CAT_ASSIGN' if $op eq '.=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ".=" 1 0 1
648 0 return $self->parser->throw_error("Assignment operator '$op' is not supported");
649 }
650  
651 sub got_unary_operator {
652 8 ▶ my ($self, $op) = @_;
Subroutine got_unary_operator: called CC=3 SCAR=11.4
Pod undocumented
653 8 ▶ return 'INC' if $op eq '++';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "++" 6 2 8
654 2 ▶ return 'DEC' if $op eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "--" 2 0 2
655 0 return $self->parser->throw_error("Increment/Decrement operator '$op' is not supported");
656 }
657  
658 sub got_array {
659 10 ▶ my ($self, $arr) = @_;
Subroutine got_array: called CC=2 SCAR=7.0
Pod undocumented
660 10 ▶ $self->flatten($arr) if ref $arr eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $arr eq "ARRAY" 10 0 10
661 10 $self->global_collections->{"$arr"} = $arr;
662 10 return $arr;
663 }
664  
665 sub got_modifier_constant {
666 12 ▶ my ($self, $list) = @_;
Subroutine got_modifier_constant: called CC=13 SCAR=36.1
Pod undocumented
667 # we don't flatten since $value can be an array as well
668 12 my ($modifier, $value) = @$list;
669 12 $modifier = uc $modifier;
670 ## first check if the modifier is an operator
671 12 my $method = $self->pic->validate_modifier_operator($modifier);
672 12 ▶ $self->flatten($value) if ($method and ref $value eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if $method and ref $value eq "ARRAY" 0 12 12
Condition33%1/3
$method and ref $value eq "ARRAY"
!ll&&!rl&&r
1200
MC/DC0%0/2
$method and ref $value eq "ARRAY"
$method ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= $methodB= ref $value eq "ARRAY"
ABresult
0X0
100
111
673 12 ▶ return $self->got_expr_value(["MOP::${modifier}::${value}"]) if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 0 12 12
674 ## if not then check if it is a type modifier for use by the simulator
675 12 ▶ if ($self->simulator and $self->simulator->supports_modifier($modifier)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simulator and $self->simulator->supports_modifier($modifier)) 11 1 12
Condition66%2/3
$self->simulator and $self->simulator->supports_modifier($modifier)
!ll&&!rl&&r
0111
MC/DC50%1/2
$self->simulator and $self->simulator->supports_modifier($modifier)
$self->simulator $self->simulator->supports_modifier($modifier)
Truth table66%2/3
A and B
A= $self->simulatorB= $self->simulator->supports_modifier($modifier)
ABresult
0X0
100
111
676 11 my $hh = { $modifier => $value };
677 11 $self->global_collections->{"$hh"} = $hh;
678 11 return $hh;
679 }
680 ## ok check if the modifier is a type modifier for code generation
681 ## this is reallly a bad hack
682 1 ▶ if ($modifier eq 'TABLE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($modifier eq 'TABLE') { } 1 0 1
elsif ($modifier eq 'ARRAY') { } 0 0 0
elsif ($modifier eq 'STRING') { } 0 0 0
683 1 ▶ return { TABLE => $value } if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 1 0 1
684 0 return { TABLE => [$value] };
685 } elsif ($modifier eq 'ARRAY') {
686 0 ▶ return $value if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 0 0
687 0 return [$value];
688 } elsif ($modifier eq 'STRING') {
689 0 ▶ return { STRING => $value } if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 0 0
690 0 return { STRING => [$value] };
691 }
692 0 ▶ $self->parser->throw_error("Modifying operator '$modifier' not supported") unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 0 0
693 }
694  
695 sub got_modifier_variable {
696 2 ▶ my ($self, $list) = @_;
Subroutine got_modifier_variable: called CC=3 SCAR=11.1
Pod undocumented
697 2 my ($modifier, $varname);
698 2 ▶ $self->flatten($list) if ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list eq "ARRAY" 2 0 2
699 2 $modifier = shift @$list;
700 2 $varname = shift @$list;
701 2 $modifier = uc $modifier;
702 2 my $method = $self->pic->validate_modifier_operator($modifier);
703 2 ▶ $self->parser->throw_error("Modifying operator '$modifier' not supported") unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 2 2
704 2 return $self->got_expr_value(["MOP::${modifier}::${varname}"]);
705 }
706  
707 sub got_validated_variable {
708 207 ▶ my ($self, $list) = @_;
Subroutine got_validated_variable: called CC=4 SCAR=15.0
Pod undocumented
709 207 my $varname;
710 207 ▶ if (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 207 0 207
711 207 $self->flatten($list);
712 207 $varname = shift @$list;
713 207 my $suffix = shift @$list;
714 207 ▶ $varname .= $suffix if defined $suffix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $suffix 207 0 207
715 } else {
716 0 $varname = $list;
717 }
718 207 ▶ return $varname if $self->pic->validate($varname);
Branch: 1 branch
BranchTrueFalseTotal
if $self->pic->validate($varname) 207 0 207
719 0 return $self->parser->throw_error("'$varname' is not a valid part of the " . uc $self->pic->type);
720 }
721  
722 sub got_variable {
723 419 ▶ my ($self, $list) = @_;
Subroutine got_variable: called CC=5 SCAR=16.2
Pod undocumented
724 419 ▶ $self->flatten($list) if ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list eq "ARRAY" 419 0 419
725 419 my $varname = shift @$list;
726 419 my ($current, $parent) = $self->stack;
727 # if the variable is used from the pragma grammar rule
728 # we do not want to store it yet and definitely not store the size yet
729 # we could remove this if we set the size after the code generation or so
730 # but that may lead to more complexity. this is much easier
731 419 ▶ return $varname if $parent eq 'pragmas';
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq "pragmas" 0 419 419
732 $self->ast->{variables}->{$varname} = {
733 name => uc $varname,
734 scope => $self->ast->{block_stack}->[-1],
735 size => POSIX::ceil($self->pic->address_bits($varname) / 8),
736 type => 'byte',
737 data => undef,
738 419 ▶ } unless exists $self->ast->{variables}->{$varname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->ast->{"variables"}{$varname} 42 377 419
739 419 ▶ $self->ast->{variables}->{$varname}->{scope} = 'global' if $parent =~ /assert_/;
Branch: 1 branch
BranchTrueFalseTotal
if $parent =~ /assert_/ 26 393 419
740 419 return $varname;
741 }
742  
743 sub got_boolean {
744 11 ▶ my ($self, $list) = @_;
Subroutine got_boolean: called CC=6 SCAR=19.9
Pod undocumented
745 11 my $b;
746 11 ▶ if (ref $list eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $list eq 'ARRAY') { } 0 11 11
747 0 $self->flatten($list);
748 0 $b = shift @$list;
749 } else {
750 11 $b = $list;
751 }
752 11 ▶ return 0 unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 11 11
753 11 ▶ return 1 if $b =~ /TRUE|true/i;
Branch: 1 branch
BranchTrueFalseTotal
if $b =~ /TRUE|true/i 6 5 11
754 5 ▶ return 1 if $b == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $b == bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 0 5 5
755 5 ▶ return 0 if $b =~ /FALSE|false/i;
Branch: 1 branch
BranchTrueFalseTotal
if $b =~ /FALSE|false/i 5 0 5
756 0 return 0; # default boolean is false
757 }
758  
759 sub got_double_quoted_string {
760 43 ▶ my $self = shift;
Subroutine got_double_quoted_string: called CC=1 SCAR=0.0
Pod undocumented
761 43 my $str = pop;
762 ## Ripped from Ingy's pegex-json-pm Pegex::JSON::Data
763 ## Unicode support not implemented yet but available in Pegex::JSON::Data
764 43 my %escapes = (
765 '"' => '"',
766 '/' => '/',
767 "\\" => "\\",
768 b => "\b",
769 f => "\x12",
770 n => "\n",
771 r => "\r",
772 t => "\t",
773 0 => "\0",
774 );
775 43 $str =~ s/\\(["\/\\bfnrt0])/$escapes{$1}/ge;
776 43 return $str;
777 }
778  
779 sub got_string {
780 48 ▶ my $self = shift;
Subroutine got_string: called CC=4 SCAR=13.9
Pod undocumented
781 48 my $str = shift;
782 ##TODO: handle empty strings as initializers
783 # store only unique strings otherwise re-use them
784 48 foreach (%{$self->global_collections}) {
785 605 my $h = $self->global_collections->{$_};
786 605 ▶ return $h if ($h->{string} eq $str);
Branch: 1 branch
BranchTrueFalseTotal
if $h->{"string"} eq $str 1 604 605
787 }
788 47 ▶ my $is_empty = 1 if $str eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $str eq "" 1 46 47
789 my $stref = {
790 string => $str,
791 block => $self->ast->{block_stack}->[-1],
792 47 name => sprintf("_vic_str_%02d", $self->ast->{strings}),
793 size => length($str) + 1, # trailing null byte
794 empty => $is_empty, # required for variable allocation later
795 };
796 47 $self->global_collections->{"$stref"} = $stref;
797 47 $self->ast->{strings}++;
798 47 return $stref;
799 #return '@' . $str;
800 }
801  
802 sub got_number {
803 400 ▶ my ($self, $list) = @_;
Subroutine got_number: called CC=3 SCAR=12.3
Pod undocumented
804 # if it is a hexadecimal number we can just convert it to number using int()
805 # since hex is returned here as a string
806 400 ▶ return hex($list) if $list =~ /0x|0X/;
Branch: 1 branch
BranchTrueFalseTotal
if $list =~ /0x|0X/ 56 344 400
807 344 my $val = int($list);
808 344 ▶ return $val if $val >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $val >= bless( {"sign" => "+","value" => bless( [0], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 344 0 344
809 ##TODO: check the negative value
810 0 my $bits = (2 ** $self->pic->address_bits) - 1;
811 0 $val = sprintf "0x%02X", $val;
812 0 return hex($val) & $bits;
813 }
814  
815 # convert the number to appropriate units
816 sub got_number_units {
817 51 ▶ my ($self, $list) = @_;
Subroutine got_number_units: called CC=8 SCAR=20.8
Pod undocumented
818 51 $self->flatten($list);
819 51 my $num = shift @$list;
820 51 my $units = shift @$list;
821 51 ▶ return $num unless defined $units;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $units 0 51 51
822 51 ▶ $num *= 1 if $units eq 'us';
Branch: 1 branch
BranchTrueFalseTotal
if $units eq "us" 2 49 51
823 51 ▶ $num *= 1000 if $units eq 'ms';
Branch: 1 branch
BranchTrueFalseTotal
if $units eq "ms" 13 38 51
824 51 ▶ $num *= 1e6 if $units eq 's';
Branch: 1 branch
BranchTrueFalseTotal
if $units eq "s" 19 32 51
825 51 ▶ $num *= 1 if $units eq 'Hz';
Branch: 1 branch
BranchTrueFalseTotal
if $units eq "Hz" 6 45 51
826 51 ▶ $num *= 1000 if $units eq 'kHz';
Branch: 1 branch
BranchTrueFalseTotal
if $units eq "kHz" 5 46 51
827 51 ▶ $num *= 1e6 if $units eq 'MHz';
Branch: 1 branch
BranchTrueFalseTotal
if $units eq "MHz" 0 51 51
828 # ignore the '%' sign for now
829 51 return $num;
830 }
831  
832 sub got_real_number {
833 5 ▶ my ($self, $list) = @_;
Subroutine got_real_number: called CC=4 SCAR=14.6
Pod undocumented
834 5 ▶ $list .= '0' if $list =~ /\d+\.$/;
Branch: 1 branch
BranchTrueFalseTotal
if $list =~ /\d+\.$/ 0 5 5
835 5 ▶ $list = "0.$1" if $list =~ /^\.(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $list =~ /^\.(\d+)$/ 0 5 5
836 5 ▶ $list = "-0.$1" if $list =~ /^-\.(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $list =~ /^-\.(\d+)$/ 0 5 5
837 5 return $list;
838 }
839  
840 # remove the dumb stuff from the tree
841 68 ▶sub got_comment { return; }
Subroutine got_comment: called CC=1 SCAR=0.0
Pod undocumented
842  
843 sub _update_funcs {
844 66 ▶ my ($self, $funcs, $macros) = @_;
Subroutine _update_funcs: called CC=6 SCAR=18.4
845 66 ▶ if (ref $funcs eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $funcs eq "HASH") 66 0 66
846 66 foreach (keys %$funcs) {
847 21 $self->ast->{funcs}->{$_} = $funcs->{$_};
848 }
849 }
850 66 ▶ if (ref $macros eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $macros eq "HASH") 66 0 66
851 66 ▶ return unless ref $macros eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $macros eq "HASH" 0 66 66
852 66 foreach (keys %$macros) {
853 140 $self->ast->{macros}->{$_} = $macros->{$_};
854 }
855 }
856 66 1;
857 }
858  
859 sub _update_tables {
860 18 ▶ my ($self, $tables) = @_;
Subroutine _update_tables: called CC=6 SCAR=19.6
861 18 ▶ if (ref $tables eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tables eq "HASH") 0 18 18
862 0 $tables = [ $tables ];
863 }
864 18 ▶ unless (ref $tables eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $tables eq "ARRAY") 0 18 18
865 0 return $self->parser->throw_error(
866 "Code generation error. PIC methods should return strings as a HASH or ARRAY");
867 }
868 18 foreach my $s (@$tables) {
869 5 ▶ next unless defined $s->{bytes};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->{"bytes"} 0 5 5
870 5 ▶ next unless defined $s->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s->{"name"} 0 5 5
871 5 push @{$self->ast->{tables}}, $s;
872 }
873 18 1;
874 }
875  
876 ## assert handling is special for now
877 sub got_assert_comparison {
878 28 ▶ my ($self, $list) = @_;
Subroutine got_assert_comparison: called CC=4 SCAR=15.2
Pod undocumented
879 28 ▶ return unless $self->simulator;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->simulator 0 28 28
880 28 ▶ $self->flatten($list) if ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list eq "ARRAY" 28 0 28
881 28 ▶ if (scalar @$list < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$list < bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 0 28 28
882 0 return $self->parser->throw_error("Error in assert statement");
883 }
884 28 return join("@@", @$list);
885 }
886  
887 sub got_assert_statement {
888 28 ▶ my ($self, $list) = @_;
Subroutine got_assert_statement: called CC=3 SCAR=11.0
Pod undocumented
889 28 ▶ $self->flatten($list) if ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $list eq "ARRAY" 28 0 28
890 28 my ($method, $cond, $msg) = @$list;
891 28 ▶ $msg = '' unless defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 1 27 28
892 28 $self->ast->{asserts}++;
893 28 $self->update_intermediate("SIM::${method}::${cond}::${msg}");
894 28 return;
895 }
896  
897 sub generate_simulator_instruction {
898 130 ▶ my ($self, $line) = @_;
Subroutine generate_simulator_instruction: called CC=8 SCAR=21.1
Pod undocumented
899 130 my @ins = split /::/, $line;
900 130 my $tag = shift @ins;
901 130 my $method = shift @ins;
902 130 my @code = ();
903 130 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 130 130
904 130 foreach (@ins) {
905 188 ▶ next unless /HASH|ARRAY/;
Branch: 1 branch
BranchTrueFalseTotal
unless /HASH|ARRAY/ 138 50 188
906 50 ▶ next unless exists $self->global_collections->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->global_collections->{$_} 0 50 50
907 50 $_ = $self->global_collections->{$_};
908 }
909 130 ▶ return @code if $self->simulator->disable;
Branch: 1 branch
BranchTrueFalseTotal
if $self->simulator->disable 1 129 130
910 129 my $code = $self->simulator->$method(@ins);
911 129 ▶ return $self->parser->throw_error("Error in simulator intermediate code '$line'") unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 129 129
912 129 ▶ push @code, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 129 0 129
913 129 return @code;
914 }
915  
916 sub generate_code_instruction {
917 136 ▶ my ($self, $line) = @_;
Subroutine generate_code_instruction: called CC=11 SCAR=24.0
Pod undocumented
918 136 my @ins = split /::/, $line;
919 136 my $tag = shift @ins;
920 136 my $method = shift @ins;
921 136 my @code = ();
922 136 foreach (@ins) {
923 269 ▶ if (exists $self->global_collections->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->global_collections->{$_}) 6 263 269
924 6 $_ = $self->global_collections->{$_};
925 6 next;
926 }
927 263 ▶ if (exists $self->ast->{variables}->{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"variables"}{$_}) 29 234 263
928 29 my $vhref = $self->ast->{variables}->{$_};
929 29 ▶ if ($vhref->{type} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vhref->{"type"} eq "string") 1 28 29
930 # send the string variable information to the method
931 # and hope that the method knows how to handle it
932 # this is useful for I/O methods and operator methods
933 # other methods should outright fail to use this and it should
934 # make sense there.#TODO: make better error messages for that.
935 1 $_ = $vhref;
936 }
937 }
938 }
939 136 my ($code, $funcs, $macros, $tables) = $self->pic->$method(@ins);
940 136 ▶ return $self->parser->throw_error("Error in intermediate code '$line'") unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 1 135 136
941 135 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 135 135
942 135 ▶ push @code, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 135 0 135
943 135 ▶ $self->_update_funcs($funcs, $macros) if ($funcs or $macros);
Branch: 1 branch
BranchTrueFalseTotal
if $funcs or $macros 35 100 135
Condition66%2/3
$funcs or $macros
l!l&&r!l&&!r
350100
MC/DC50%1/2
$funcs or $macros
$funcs $macros
Truth table66%2/3
A or B
A= $funcsB= $macros
ABresult
1X1
011
000
944 135 ▶ $self->_update_tables($tables) if $tables;
Branch: 1 branch
BranchTrueFalseTotal
if $tables 16 119 135
945 135 return @code;
946 }
947  
948 sub generate_code_unary_expr {
949 8 ▶ my ($self, $line) = @_;
Subroutine generate_code_unary_expr: called CC=10 SCAR=26.2
Pod undocumented
950 8 my @code = ();
951 8 my $ast = $self->ast;
952 8 my ($tag, $op, $varname) = split /::/, $line;
953 8 my $method = $self->pic->validate_operator($op);
954 8 ▶ $self->parser->throw_error("Invalid operator '$op' in intermediate code") unless $self->pic->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pic->can($method) 0 8 8
955 # check if temporary variable or not
956 8 ▶ if (exists $ast->{variables}->{$varname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ast->{'variables'}{$varname}) { } 8 0 8
957 8 ▶ my $nvar = $ast->{variables}->{$varname}->{name} || $varname;
Condition33%1/3
$ast->{'variables'}{$varname}{'name'} || $varname
l!l&&r!l&&!r
800
MC/DC0%0/2
$ast->{'variables'}{$varname}{'name'} || $varname
$ast->{'variables'}{$varname}{'name'} $varname
Truth table33%1/3
A || B
A= $ast->{'variables'}{$varname}{'name'}B= $varname
ABresult
1X1
011
000
958 8 my ($code, $funcs, $macros, $tables) = $self->pic->$method($nvar);
959 8 ▶ return $self->parser->throw_error("Error in intermediate code '$line'") unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 8 8
960 8 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 8 8
961 8 ▶ push @code, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 8 0 8
962 8 ▶ $self->_update_funcs($funcs, $macros) if ($funcs or $macros);
Branch: 1 branch
BranchTrueFalseTotal
if $funcs or $macros 0 8 8
Condition33%1/3
$funcs or $macros
l!l&&r!l&&!r
008
MC/DC0%0/2
$funcs or $macros
$funcs $macros
Truth table33%1/3
A or B
A= $funcsB= $macros
ABresult
1X1
011
000
963 8 ▶ $self->_update_tables($tables) if $tables;
Branch: 1 branch
BranchTrueFalseTotal
if $tables 0 8 8
964 } else {
965 0 return $self->parser->throw_error("Error in intermediate code '$line'");
966 }
967 8 return @code;
968 }
969  
970 sub generate_code_operations {
971 70 ▶ my ($self, $line, %extra) = @_;
Subroutine generate_code_operations: called CC=19 SCAR=30.3
Pod undocumented
972 70 my @code = ();
973 70 my ($tag, @args) = split /::/, $line;
974 70 my ($op, $var1, $var2);
975 70 ▶ if (scalar @args == 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (scalar @args == {sign => '+', value => ['2']}) { } 9 61 70
elsif (scalar @args == {sign => '+', value => ['3']}) { } 60 1 61
elsif (scalar @args == {sign => '+', value => ['4']}) { } 1 0 1
976 9 $op = shift @args;
977 9 $var1 = shift @args;
978 } elsif (scalar @args == 3) {
979 60 $var1 = shift @args;
980 60 $op = shift @args;
981 60 $var2 = shift @args;
982 } elsif (scalar @args == 4) {
983 1 $var1 = shift @args;
984 1 $op = shift @args;
985 1 $var2 = shift @args;
986 1 my $var3 = shift @args;
987 1 $extra{SIZE} = $var3;
988 } else {
989 0 return $self->parser->throw_error("Error in intermediate code '$line'");
990 }
991 70 ▶ if (exists $extra{STACK}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $extra{"STACK"}) 36 34 70
992 36 ▶ if (defined $var1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var1) 36 0 36
993 36 ▶ $var1 = $extra{STACK}->{$var1} || $var1;
Condition66%2/3
$extra{'STACK'}{$var1} || $var1
l!l&&r!l&&!r
15210
MC/DC0%0/2
$extra{'STACK'}{$var1} || $var1
$extra{'STACK'}{$var1} $var1
Truth table66%2/3
A || B
A= $extra{'STACK'}{$var1}B= $var1
ABresult
1X1
011
000
994 }
995 36 ▶ if (defined $var2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var2) 35 1 36
996 35 ▶ $var2 = $extra{STACK}->{$var2} || $var2;
Condition100%3/3
$extra{'STACK'}{$var2} || $var2
l!l&&r!l&&!r
13202
MC/DC100%2/2
$extra{'STACK'}{$var2} || $var2
$extra{'STACK'}{$var2} $var2
Truth table100%3/3
A || B
A= $extra{'STACK'}{$var2}B= $var2
ABresult
1X1
011
000
997 }
998 }
999 70 ▶ my $method = $self->pic->validate_operator($op) if $tag eq 'OP';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "OP" 68 2 70
1000 70 ▶ $method = $self->pic->validate_modifier_operator($op) if $tag eq 'MOP';
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "MOP" 2 68 70
1001 70 ▶ $self->parser->throw_error("Invalid operator '$op' in intermediate code") unless
Branch: 1 branch
BranchTrueFalseTotal
unless $method and $self->pic->can($method) 0 70 70
Condition33%1/3
$method and $self->pic->can($method)
!ll&&!rl&&r
0070
MC/DC0%0/2
$method and $self->pic->can($method)
$method $self->pic->can($method)
Truth table33%1/3
A and B
A= $methodB= $self->pic->can($method)
ABresult
0X0
100
111
1002 ($method and $self->pic->can($method));
1003 70 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 70 70
1004 70 my ($code, $funcs, $macros, $tables) = $self->pic->$method($var1, $var2, %extra);
1005 70 ▶ return $self->parser->throw_error("Error in intermediate code '$line'") unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 70 70
1006 70 ▶ push @code, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 70 0 70
1007 70 ▶ $self->_update_funcs($funcs, $macros) if ($funcs or $macros);
Branch: 1 branch
BranchTrueFalseTotal
if $funcs or $macros 22 48 70
Condition66%2/3
$funcs or $macros
l!l&&r!l&&!r
22048
MC/DC50%1/2
$funcs or $macros
$funcs $macros
Truth table66%2/3
A or B
A= $funcsB= $macros
ABresult
1X1
011
000
1008 70 ▶ $self->_update_tables($tables) if $tables;
Branch: 1 branch
BranchTrueFalseTotal
if $tables 0 70 70
1009 70 return @code;
1010 }
1011  
1012 sub find_tmpvar_dependencies {
1013 70 ▶ my ($self, $tvar) = @_;
Subroutine find_tmpvar_dependencies: called CC=12 SCAR=26.0
Pod undocumented
1014 70 my $tcode = $self->ast->{tmp_variables}->{$tvar};
1015 70 my ($tag, @args) = split /::/, $tcode;
1016 70 ▶ return unless $tag eq 'OP';
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq "OP" 2 68 70
1017 68 my @deps = ();
1018 68 my $sz = scalar @args;
1019 68 ▶ if ($sz == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sz == {sign => '+', value => ['2']}) { } 7 61 68
elsif ($sz == {sign => '+', value => ['3']} or $sz == {sign => '+', value => ['4']}) { } 61 0 61
1020 7 my ($op, $var) = @args;
1021 7 ▶ if (exists $self->ast->{tmp_variables}->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"tmp_variables"}{$var}) 0 7 7
1022 0 push @deps, $var;
1023 0 my @rdeps = $self->find_tmpvar_dependencies($var);
1024 0 ▶ push @deps, @rdeps if @rdeps;
Branch: 1 branch
BranchTrueFalseTotal
if @rdeps 0 0 0
1025 }
1026 ▶ } elsif ($sz == 3 or $sz == 4) {
Condition66%2/3
$sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
l!l&&r!l&&!r
6010
MC/DC0%0/2
$sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
$sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table66%2/3
A or B
A= $sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )B= $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
ABresult
1X1
011
000
1027 61 my ($var1, $op, $var2) = @args;
1028 61 ▶ if (exists $self->ast->{tmp_variables}->{$var1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"tmp_variables"}{$var1}) 15 46 61
1029 15 push @deps, $var1;
1030 15 my @rdeps = $self->find_tmpvar_dependencies($var1);
1031 15 ▶ push @deps, @rdeps if @rdeps;
Branch: 1 branch
BranchTrueFalseTotal
if @rdeps 7 8 15
1032 }
1033 61 ▶ if (exists $self->ast->{tmp_variables}->{$var2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"tmp_variables"}{$var2}) 13 48 61
1034 13 push @deps, $var2;
1035 13 my @rdeps = $self->find_tmpvar_dependencies($var2);
1036 13 ▶ push @deps, @rdeps if @rdeps;
Branch: 1 branch
BranchTrueFalseTotal
if @rdeps 6 7 13
1037 }
1038 } else {
1039 0 return $self->parser->throw_error("Error in intermediate code '$tcode'");
1040 }
1041 68 ▶ return wantarray ? @deps : \@deps;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 68 0 68
1042 }
1043  
1044 sub find_var_dependencies {
1045 42 ▶ my ($self, $tvar) = @_;
Subroutine find_var_dependencies: called CC=9 SCAR=22.3
Pod undocumented
1046 42 my $tcode = $self->ast->{tmp_variables}->{$tvar};
1047 42 my ($tag, @args) = split /::/, $tcode;
1048 42 ▶ return unless $tag eq 'OP';
Branch: 1 branch
BranchTrueFalseTotal
unless $tag eq "OP" 2 40 42
1049 40 my @deps = ();
1050 40 my $sz = scalar @args;
1051 40 ▶ if ($sz == 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sz == {sign => '+', value => ['2']}) { } 6 34 40
elsif ($sz == {sign => '+', value => ['3']} or $sz == {sign => '+', value => ['4']}) { } 34 0 34
1052 6 my ($op, $var) = @args;
1053 6 ▶ if (exists $self->ast->{variables}->{$var}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"variables"}{$var}) 6 0 6
1054 6 push @deps, $var;
1055 }
1056 ▶ } elsif ($sz == 3 or $sz == 4) {
Condition66%2/3
$sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
l!l&&r!l&&!r
3310
MC/DC0%0/2
$sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) or $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
$sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table66%2/3
A or B
A= $sz == bless( {"sign" => "+","value" => bless( [3], 'Math::BigInt::Calc' )}, 'Math::BigInt' )B= $sz == bless( {"sign" => "+","value" => bless( [4], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
ABresult
1X1
011
000
1057 34 my ($var1, $op, $var2) = @args;
1058 34 ▶ if (exists $self->ast->{variables}->{$var1}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"variables"}{$var1}) 27 7 34
1059 27 push @deps, $var1;
1060 }
1061 34 ▶ if (exists $self->ast->{variables}->{$var2}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"variables"}{$var2}) 12 22 34
1062 12 push @deps, $var2;
1063 }
1064 } else {
1065 0 return $self->parser->throw_error("Error in intermediate code '$tcode'");
1066 }
1067 40 ▶ return wantarray ? @deps : \@deps;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 40 0 40
1068 }
1069  
1070 sub do_i_use_stack {
1071 34 ▶ my ($self, @deps) = @_;
Subroutine do_i_use_stack: called CC=3 SCAR=11.2
Pod undocumented
1072 34 ▶ return 0 unless @deps;
Branch: 1 branch
BranchTrueFalseTotal
unless @deps 2 32 34
1073 32 my @bits = map { $self->pic->address_bits($_) } @deps;
1074 32 ▶ return 0 if max(@bits) == $self->pic->wreg_size;
Branch: 1 branch
BranchTrueFalseTotal
if &max(@bits) == $self->pic->wreg_size 32 0 32
1075 0 return 1;
1076 }
1077  
1078 sub generate_code_assign_expr {
1079 88 ▶ my ($self, $line) = @_;
Subroutine generate_code_assign_expr: called CC=30 SCAR=38.9
Pod undocumented
1080 88 my @code = ();
1081 88 my $ast = $self->ast;
1082 88 my ($tag, $op, $varname, $rhs) = split /::/, $line;
1083 88 ▶ push @code, ";;; $line\n" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 88 88
1084 88 ▶ if (exists $ast->{variables}->{$varname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ast->{'variables'}{$varname}) { } 88 0 88
1085 88 ▶ if (exists $ast->{tmp_variables}->{$rhs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ast->{'tmp_variables'}{$rhs}) { } 31 57 88
1086 31 my $tmp_code = $ast->{tmp_variables}->{$rhs};
1087 31 my @deps = $self->find_tmpvar_dependencies($rhs);
1088 31 my @vdeps = $self->find_var_dependencies($rhs);
1089 31 ▶ push @deps, $rhs if @deps;
Branch: 1 branch
BranchTrueFalseTotal
if @deps 6 25 31
1090 31 ▶ if ($self->intermediate_inline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->intermediate_inline) 0 31 31
1091 0 ▶ push @code, "\t;; TMP_VAR DEPS - $rhs, ". join (',', @deps) if @deps;
Branch: 1 branch
BranchTrueFalseTotal
if @deps 0 0 0
1092 0 ▶ push @code, "\t;; VAR DEPS - ". join (',', @vdeps) if @vdeps;
Branch: 1 branch
BranchTrueFalseTotal
if @vdeps 0 0 0
1093 0 foreach (sort @deps) {
1094 0 my $tcode = $ast->{tmp_variables}->{$_};
1095 0 push @code, "\t;; $_ = $tcode";
1096 }
1097 0 push @code, "\t;; $line";
1098 }
1099 31 ▶ if (scalar @deps) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @deps) { } 6 25 31
1100 6 $ast->{tmp_stack_size} = max(scalar(@deps), $ast->{tmp_stack_size});
1101 ## it is assumed that the dependencies and intermediate code are
1102 #arranged in expected order
1103 # TODO: bits check
1104 6 my $counter = 0;
1105 6 my %tmpstack = map { $_ => 'VIC_STACK + ' . $counter++ } sort(@deps);
1106 6 foreach (sort @deps) {
1107 30 my $tcode = $ast->{tmp_variables}->{$_};
1108 30 my $result = $tmpstack{$_};
1109 30 ▶ $result = uc $varname if $_ eq $rhs;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $rhs 6 24 30
1110 30 ▶ my @newcode = $self->generate_code_operations($tcode,
Branch: 1 branch
BranchTrueFalseTotal
if $tcode 30 0 30
1111 STACK => \%tmpstack, RESULT => $result) if $tcode;
1112 30 ▶ push @code, "\t;; $_ = $tcode" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 30 30
1113 30 ▶ push @code, @newcode if @newcode;
Branch: 1 branch
BranchTrueFalseTotal
if @newcode 30 0 30
1114 }
1115 } else {
1116 # no tmp-var dependencies
1117 25 ▶ my $use_stack = $self->do_i_use_stack(@vdeps) unless scalar @deps;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @deps 25 0 25
1118 25 ▶ unless ($use_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_stack) { } 0 25 25
1119 25 my @newcode = $self->generate_code_operations($tmp_code,
1120 RESULT => uc $varname);
1121 25 ▶ push @code, @newcode if @newcode;
Branch: 1 branch
BranchTrueFalseTotal
if @newcode 25 0 25
1122 } else {
1123 # TODO: stack
1124 0 XXX @vdeps;
1125 }
1126 }
1127 } else {
1128 57 ▶ my $nvar = $ast->{variables}->{$varname}->{name} || $varname;
Condition33%1/3
$ast->{'variables'}{$varname}{'name'} || $varname
l!l&&r!l&&!r
5700
MC/DC0%0/2
$ast->{'variables'}{$varname}{'name'} || $varname
$ast->{'variables'}{$varname}{'name'} $varname
Truth table33%1/3
A || B
A= $ast->{'variables'}{$varname}{'name'}B= $varname
ABresult
1X1
011
000
1129 57 ▶ if ($rhs =~ /HASH|ARRAY/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhs =~ /HASH|ARRAY/) 2 55 57
1130 2 ▶ if (exists $self->global_collections->{$rhs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->global_collections->{$rhs}) 2 0 2
1131 2 $rhs = $self->global_collections->{$rhs};
1132 }
1133 }
1134 57 ▶ if (exists $self->ast->{variables}->{$varname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->ast->{"variables"}{$varname}) 57 0 57
1135 57 my $vhref = $self->ast->{variables}->{$varname};
1136 57 ▶ if ($vhref->{type} eq 'string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($vhref->{"type"} eq "string") 3 54 57
1137 # send the string variable information to the method
1138 # and hope that the method knows how to handle it
1139 # this is useful for I/O methods and operator methods
1140 # other methods should outright fail to use this and it should
1141 # make sense there.#TODO: make better error messages for that.
1142 3 $nvar = $vhref;
1143 }
1144 }
1145 57 my $method = $self->pic->validate_operator($op);
1146 57 ▶ $self->parser->throw_error("Invalid operator '$op' in intermediate code") unless $self->pic->can($method);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pic->can($method) 0 57 57
1147 57 my ($code, $funcs, $macros, $tables) = $self->pic->$method($nvar, $rhs);
1148 57 ▶ return $self->parser->throw_error("Error in intermediate code '$line'") unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 57 57
1149 57 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 57 57
1150 57 ▶ push @code, $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 57 0 57
1151 57 ▶ $self->_update_funcs($funcs, $macros) if ($funcs or $macros);
Branch: 1 branch
BranchTrueFalseTotal
if $funcs or $macros 9 48 57
Condition66%2/3
$funcs or $macros
l!l&&r!l&&!r
9048
MC/DC50%1/2
$funcs or $macros
$funcs $macros
Truth table66%2/3
A or B
A= $funcsB= $macros
ABresult
1X1
011
000
1152 57 ▶ $self->_update_tables($tables) if $tables;
Branch: 1 branch
BranchTrueFalseTotal
if $tables 2 55 57
1153 }
1154 } else {
1155 0 return $self->parser->throw_error(
1156 "Error in intermediate code '$line': $varname doesn't exist");
1157 }
1158 88 return @code;
1159 }
1160  
1161 sub find_nearest_loop {
1162 7 ▶ my ($self, $mapping, $child) = @_;
Subroutine find_nearest_loop: called CC=5 SCAR=16.6
Pod undocumented
1163 7 ▶ return unless exists $mapping->{$child};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $mapping->{$child} 0 7 7
1164 7 ▶ if (exists $mapping->{$child}->{loop}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mapping->{$child}{"loop"}) 7 0 7
1165 7 ▶ return $child if $mapping->{$child}->{loop} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $mapping->{$child}{"loop"} eq 1 4 3 7
1166 }
1167 3 ▶ return unless $mapping->{$child}->{parent};
Branch: 1 branch
BranchTrueFalseTotal
unless $mapping->{$child}{"parent"} 0 3 3
1168 3 return $self->find_nearest_loop($mapping, $mapping->{$child}->{parent});
1169 }
1170  
1171 sub generate_code_blocks {
1172 43 ▶ my ($self, $line, $block) = @_;
Subroutine generate_code_blocks: called CC=42 SCAR=48.5
Pod undocumented
1173 43 my @code = ();
1174 43 my $ast = $self->ast;
1175 43 my $mapping = $ast->{block_mapping};
1176 43 ▶ my $mapped_block = $mapping->{$block}->{block} || $block;
Condition33%1/3
$mapping->{$block}{'block'} || $block
l!l&&r!l&&!r
4300
MC/DC0%0/2
$mapping->{$block}{'block'} || $block
$mapping->{$block}{'block'} $block
Truth table33%1/3
A || B
A= $mapping->{$block}{'block'}B= $block
ABresult
1X1
011
000
1177 43 my ($tag, $label, $child, $parent, $parent_label, $end_label) = split/::/, $line;
1178 43 ▶ return if ($child eq $block or $child eq $mapped_block or $child eq $parent);
Branch: 1 branch
BranchTrueFalseTotal
if $child eq $block or $child eq $mapped_block or $child eq $parent 0 43 43
Condition33%2/6
$child eq $block or $child eq $mapped_block or $child eq $parent
l!l&&r!l&&!r
0043
$child eq $block or $child eq $mapped_block
l!l&&r!l&&!r
0043
MC/DC0%0/3
$child eq $block or $child eq $mapped_block or $child eq $parent
$child eq $block $child eq $mapped_block $child eq $parent
Truth table25%1/4
A or B or C
A= $child eq $blockB= $child eq $mapped_blockC= $child eq $parent
ABCresult
1XX1
01X1
0011
0000
1179 43 ▶ return if exists $ast->{generated_blocks}->{$child};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ast->{"generated_blocks"}{$child} 0 43 43
1180 43 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 43 43
1181 43 my @newcode = $self->generate_code($ast, $child);
1182 43 ▶ my @bindexes = indexes { $_ eq 'BREAK' } @newcode;
Subroutine __ANON__: called CC=1 SCAR=0.0
1183 43 ▶ my @cindexes = indexes { $_ eq 'CONTINUE' } @newcode;
Subroutine __ANON__: called CC=1 SCAR=0.0
1184 43 ▶ if ($child =~ /^(?:True|False)/ and @newcode) {
Branch: 3 branches
BranchTrueFalseTotal
if ($child =~ /^(?:True|False)/ and @newcode) { } 16 27 43
elsif ($child =~ /^(?:Action|ISR)/ and @newcode) { } 11 16 27
elsif ($child =~ /^Loop/ and @newcode) { } 16 0 16
Condition66%2/3
$child =~ /^(?:True|False)/ and @newcode
!ll&&!rl&&r
27016
MC/DC50%1/2
$child =~ /^(?:True|False)/ and @newcode
$child =~ /^(?:True|False)/ @newcode
Truth table66%2/3
A and B
A= $child =~ /^(?:True|False)/B= @newcode
ABresult
0X0
100
111
1185 16 my $cond_end = "\tgoto $end_label;; go back to end of conditional\n";
1186 # handle break
1187 16 ▶ if (@bindexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bindexes) 2 14 16
1188 #find top most parent loop
1189 2 my $el = $self->find_nearest_loop($mapping, $child);
1190 2 ▶ $el = $mapping->{$el}->{end_label} if $el;
Branch: 1 branch
BranchTrueFalseTotal
if $el 2 0 2
1191 2 my $break_end;
1192 2 ▶ unless ($el) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el) { } 2 0 2
1193 0 $break_end = "\t;; break from existing block since $child not part of any loop\n";
1194 0 $break_end .= "\tgoto $end_label;; break from the conditional\n";
1195 } else {
1196 2 $break_end = "\tgoto $el;; break from the conditional\n";
1197 }
1198 2 $newcode[$_] = $break_end foreach @bindexes;
1199 }
1200 # handle continue
1201 16 ▶ if (@cindexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cindexes) 2 14 16
1202 #find top most parent loop
1203 2 my $sl = $self->find_nearest_loop($mapping, $child);
1204 2 ▶ $sl = $mapping->{$sl}->{start_label} if $sl;
Branch: 1 branch
BranchTrueFalseTotal
if $sl 2 0 2
1205 2 ▶ my $cont_start = "\tgoto $sl;; go back to start of conditional\n" if $sl;
Branch: 1 branch
BranchTrueFalseTotal
if $sl 2 0 2
1206 2 ▶ $cont_start = "\tnop ;; $child or $parent have no start_label" unless $sl;
Branch: 1 branch
BranchTrueFalseTotal
unless $sl 0 2 2
1207 2 $newcode[$_] = $cont_start foreach @cindexes;
1208 }
1209 # add the end _label
1210 # if the current block is a loop, the end label is the start label
1211 16 ▶ if (exists $mapping->{$child}->{loop} and $mapping->{$child}->{loop} eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $mapping->{$child}{'loop'} and $mapping->{$child}{'loop'} eq '1') { } 4 12 16
Condition66%2/3
exists $mapping->{$child}{"loop"} and $mapping->{$child}{"loop"} eq 1
!ll&&!rl&&r
0124
MC/DC50%1/2
exists $mapping->{$child}{"loop"} and $mapping->{$child}{"loop"} eq 1
exists $mapping->{$child}{"loop"} $mapping->{$child}{"loop"} eq 1
Truth table66%2/3
A and B
A= exists $mapping->{$child}{"loop"}B= $mapping->{$child}{"loop"} eq 1
ABresult
0X0
100
111
1212 4 ▶ my $slabel = $mapping->{$child}->{start_label} || $end_label;
Condition33%1/3
$mapping->{$child}{'start_label'} || $end_label
l!l&&r!l&&!r
400
MC/DC0%0/2
$mapping->{$child}{'start_label'} || $end_label
$mapping->{$child}{'start_label'} $end_label
Truth table33%1/3
A || B
A= $mapping->{$child}{'start_label'}B= $end_label
ABresult
1X1
011
000
1213 4 ▶ my $start_code = "\tgoto $slabel ;; go back to start of conditional\n" if $slabel;
Branch: 1 branch
BranchTrueFalseTotal
if $slabel 4 0 4
1214 4 ▶ $start_code = $cond_end unless $start_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $start_code 0 4 4
1215 4 push @newcode, $start_code;
1216 } else {
1217 12 push @newcode, $cond_end;
1218 }
1219 16 push @newcode, ";;;; end of $label";
1220 # hack into the function list
1221 16 $ast->{funcs}->{$label} = [@newcode];
1222 ▶ } elsif ($child =~ /^(?:Action|ISR)/ and @newcode) {
Condition66%2/3
$child =~ /^(?:Action|ISR)/ and @newcode
!ll&&!rl&&r
16011
MC/DC50%1/2
$child =~ /^(?:Action|ISR)/ and @newcode
$child =~ /^(?:Action|ISR)/ @newcode
Truth table66%2/3
A and B
A= $child =~ /^(?:Action|ISR)/B= @newcode
ABresult
0X0
100
111
1223 11 my $cond_end = "\tgoto $end_label ;; go back to end of block\n";
1224 11 ▶ if (@bindexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bindexes) 0 11 11
1225 # we just break from the current block since we are not in any
1226 # sub-block
1227 0 my $break_end = "\tgoto $end_label ;; break from the block\n";
1228 0 $newcode[$_] = $break_end foreach @bindexes;
1229 }
1230 11 ▶ if (@cindexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cindexes) 0 11 11
1231 # continue gets ignored
1232 0 my $cont_start = ";; continue is a NOP for $child block";
1233 0 $newcode[$_] = $cont_start foreach @cindexes;
1234 }
1235 11 push @newcode, $cond_end, ";;;; end of $label";
1236 # hack into the function list
1237 11 $ast->{funcs}->{$label} = [@newcode];
1238 ▶ } elsif ($child =~ /^Loop/ and @newcode) {
Condition33%1/3
$child =~ /^Loop/ and @newcode
!ll&&!rl&&r
0016
MC/DC0%0/2
$child =~ /^Loop/ and @newcode
$child =~ /^Loop/ @newcode
Truth table33%1/3
A and B
A= $child =~ /^Loop/B= @newcode
ABresult
0X0
100
111
1239 16 my $cond_end = "\tgoto $end_label;; go back to end of block\n";
1240 16 ▶ if (@bindexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bindexes) 0 16 16
1241 # we just break from the current block since we are not in any
1242 # sub-block and are in a Loop already
1243 0 my $break_end = "\tgoto $end_label ;; break from the block\n";
1244 0 $newcode[$_] = $break_end foreach @bindexes;
1245 }
1246 16 ▶ if (@cindexes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cindexes) 0 16 16
1247 # continue goes to start of the loop
1248 0 my $cont_start = "\tgoto $label ;; go back to start of loop\n";
1249 0 $newcode[$_] = $cont_start foreach @cindexes;
1250 }
1251 16 push @code, @newcode;
1252 16 push @code, "\tgoto $label ;;;; end of $label\n";
1253 16 push @code, "$end_label:\n";
1254 } else {
1255 0 ▶ push @code, @newcode if @newcode;
Branch: 1 branch
BranchTrueFalseTotal
if @newcode 0 0 0
1256 }
1257 43 ▶ $ast->{generated_blocks}->{$child} = 1 if @newcode;
Branch: 1 branch
BranchTrueFalseTotal
if @newcode 43 0 43
1258 # parent equals block if it is the topmost of the stack
1259 # if the child is not a loop construct it will need a goto back to
1260 # the parent construct. if a child is a loop construct it will
1261 # already have a goto back to itself
1262 43 ▶ if (defined $parent and exists $ast->{$parent} and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent and exists $ast->{$parent} and ref $ast->{$parent} eq "ARRAY" and $parent ne $mapped_block) 0 43 43
Condition33%3/9
defined $parent and exists $ast->{$parent} and ref $ast->{$parent} eq "ARRAY" and $parent ne $mapped_block
!ll&&!rl&&r
0430
defined $parent and exists $ast->{$parent} and ref $ast->{$parent} eq "ARRAY"
!ll&&!rl&&r
0043
defined $parent and exists $ast->{$parent}
!ll&&!rl&&r
0043
MC/DC0%0/4
defined $parent and exists $ast->{$parent} and ref $ast->{$parent} eq "ARRAY" and $parent ne $mapped_block
defined $parent exists $ast->{$parent} ref $ast->{$parent} eq "ARRAY" $parent ne $mapped_block
Truth table20%1/5
A and B and C and D
A= defined $parentB= exists $ast->{$parent}C= ref $ast->{$parent} eq "ARRAY"D= $parent ne $mapped_block
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1263 ref $ast->{$parent} eq 'ARRAY' and $parent ne $mapped_block) {
1264 0 my ($ptag, $plabel) = split /::/, $ast->{$parent}->[0];
1265 0 ▶ push @code, "\tgoto $plabel;; $plabel" if $plabel;
Branch: 1 branch
BranchTrueFalseTotal
if $plabel 0 0 0
1266 }
1267 43 return @code;
1268 }
1269  
1270 sub generate_code_conditionals {
1271 9 ▶ my ($self, @condblocks) = @_;
Subroutine generate_code_conditionals: called CC=34 SCAR=39.5
Pod undocumented
1272 9 my @code = ();
1273 9 my $ast = $self->ast;
1274 9 my ($start_label, $end_label, $is_loop);
1275 9 my $blockcount = scalar @condblocks;
1276 9 my $index = 0;
1277 9 foreach my $line (@condblocks) {
1278 12 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 12 12
1279 12 my %hh = split /::/, $line;
1280 12 my $subj = $hh{SUBJ};
1281 12 ▶ $index++ if $hh{SUBCOND};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"SUBCOND"} 3 9 12
1282 # for multiple if-else-if-else we adjust the labels
1283 # for single ones we do not
1284 12 ▶ $start_label = "_start_conditional_$hh{COND}" unless defined $start_label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start_label 9 3 12
1285 12 ▶ $is_loop = $hh{LOOP} unless defined $is_loop;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $is_loop 9 3 12
1286 12 ▶ $end_label = $hh{END} unless defined $end_label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end_label 9 3 12
1287 # we now modify the TRUE/FALSE/END labels
1288 12 ▶ if ($blockcount > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blockcount > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )) 4 8 12
1289 4 my $el = "$hh{END}_$index"; # new label
1290 4 ▶ $hh{FALSE} = $el if $hh{FALSE} eq $hh{END};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"FALSE"} eq $hh{"END"} 3 1 4
1291 4 ▶ $hh{TRUE} = $el if $hh{TRUE} eq $hh{END};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"TRUE"} eq $hh{"END"} 0 4 4
1292 4 $hh{END} = $el;
1293 }
1294 12 ▶ if ($subj =~ /^\d+?$/) { # if subject is a literal
Branch: 3 branches
BranchTrueFalseTotal
if ($subj =~ /^\d+?$/) { } 1 11 12
elsif (exists $ast->{'variables'}{$subj}) { } 0 11 11
elsif (exists $ast->{'tmp_variables'}{$subj}) { } 11 0 11
1295 1 ▶ push @code, "\t;; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 1 1
1296 1 ▶ if ($subj eq 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subj eq {sign => '+', value => ['0']}) { } 0 1 1
1297 # is false
1298 0 ▶ push @code, "\tgoto $hh{FALSE}" if $hh{FALSE};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"FALSE"} 0 0 0
1299 } else {
1300 # is true
1301 1 ▶ push @code, "\tgoto $hh{TRUE}" if $hh{TRUE};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"TRUE"} 1 0 1
1302 }
1303 1 ▶ push @code, "\tgoto $hh{END}" if $hh{END};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"END"} 1 0 1
1304 1 ▶ push @code, "$hh{END}:\n" if $hh{END};
Branch: 1 branch
BranchTrueFalseTotal
if $hh{"END"} 1 0 1
1305 } elsif (exists $ast->{variables}->{$subj}) {
1306 ## we will never get here actually since we have eliminated this
1307 #possibility
1308 0 XXX \%hh;
1309 } elsif (exists $ast->{tmp_variables}->{$subj}) {
1310 11 my $tmp_code = $ast->{tmp_variables}->{$subj};
1311 11 my @deps = $self->find_tmpvar_dependencies($subj);
1312 11 my @vdeps = $self->find_var_dependencies($subj);
1313 11 ▶ push @deps, $subj if @deps;
Branch: 1 branch
BranchTrueFalseTotal
if @deps 2 9 11
1314 11 ▶ if ($self->intermediate_inline) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->intermediate_inline) 0 11 11
1315 0 ▶ push @code, "\t;; TMP_VAR DEPS - $subj, ". join (',', @deps) if @deps;
Branch: 1 branch
BranchTrueFalseTotal
if @deps 0 0 0
1316 0 ▶ push @code, "\t;; VAR DEPS - ". join (',', @vdeps) if @vdeps;
Branch: 1 branch
BranchTrueFalseTotal
if @vdeps 0 0 0
1317 0 push @code, "\t;; $subj = $tmp_code";
1318 }
1319 11 ▶ if (scalar @deps) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @deps) { } 2 9 11
1320 2 $ast->{tmp_stack_size} = max(scalar(@deps), $ast->{tmp_stack_size});
1321 ## it is assumed that the dependencies and intermediate code are
1322 #arranged in expected order
1323 # TODO: bits check
1324 2 my $counter = 0;
1325 2 my %tmpstack = map { $_ => 'VIC_STACK + ' . $counter++ } sort(@deps);
1326 2 $counter = 0; # reset
1327 2 foreach (sort @deps) {
1328 6 my $tcode = $ast->{tmp_variables}->{$_};
1329 6 my %extra = (%hh, COUNTER => $counter++);
1330 6 ▶ $extra{RESULT} = $tmpstack{$_} if $_ ne $subj;
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne $subj 4 2 6
1331 6 ▶ my @newcode = $self->generate_code_operations($tcode,
Branch: 1 branch
BranchTrueFalseTotal
if $tcode 6 0 6
1332 STACK => \%tmpstack, %extra) if $tcode;
1333 6 ▶ push @code, @newcode if @newcode;
Branch: 1 branch
BranchTrueFalseTotal
if @newcode 6 0 6
1334 }
1335 } else {
1336 # no tmp-var dependencies
1337 9 my $use_stack = $self->do_i_use_stack(@vdeps);
1338 9 ▶ unless ($use_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_stack) { } 0 9 9
1339 9 my @newcode = $self->generate_code_operations($tmp_code, %hh);
1340 9 ▶ push @code, @newcode if @newcode;
Branch: 1 branch
BranchTrueFalseTotal
if @newcode 9 0 9
1341 9 ▶ return $self->parser->throw_error("Error in intermediate code '$tmp_code'")
Branch: 1 branch
BranchTrueFalseTotal
unless @newcode 0 9 9
1342 unless @newcode;
1343 } else {
1344 # TODO: stack
1345 0 XXX \%hh;
1346 }
1347 }
1348 } else {
1349 0 return $self->parser->throw_error("Error in intermediate code '$line'");
1350 }
1351 }
1352 9 ▶ unshift @code, "$start_label:" if defined $start_label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $start_label 9 0 9
1353 9 ▶ push @code, "$end_label:" if defined $end_label and $blockcount > 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $end_label and $blockcount > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' ) 1 8 9
Condition66%2/3
defined $end_label and $blockcount > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
!ll&&!rl&&r
081
MC/DC50%1/2
defined $end_label and $blockcount > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
defined $end_label $blockcount > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
Truth table66%2/3
A and B
A= defined $end_labelB= $blockcount > bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )
ABresult
0X0
100
111
1354 9 return @code;
1355 }
1356  
1357 sub generate_code {
1358 94 ▶ my ($self, $ast, $block_name) = @_;
Subroutine generate_code: called CC=26 SCAR=33.7
Pod undocumented
1359 94 my @code = ();
1360 94 ▶ return wantarray ? @code : [] unless defined $ast;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $ast 0 94 94
wantarray ? : 0 0 0
1361 94 ▶ return wantarray ? @code : [] unless exists $ast->{$block_name};
Branch: 2 branches
BranchTrueFalseTotal
unless exists $ast->{$block_name} 1 93 94
wantarray ? : 1 0 1
1362 93 ▶ $ast->{generated_blocks} = {} unless defined $ast->{generated_blocks};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ast->{"generated_blocks"} 32 61 93
1363 93 push @code, ";;;; generated code for $block_name";
1364 93 my $blocks = $ast->{$block_name};
1365 93 ▶ while (@$blocks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$blocks) 510 92 602
1366 510 my $line = shift @$blocks;
1367 510 ▶ next unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 3 507 510
1368 507 ▶ if ($line =~ /^BLOCK::\w+/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($line =~ /^BLOCK::\w+/) { } 43 464 507
elsif ($line =~ /^INS::\w+/) { } 136 328 464
elsif ($line =~ /^UNARY::\w+/) { } 8 320 328
elsif ($line =~ /^SET::\w+/) { } 83 237 320
elsif ($line =~ /^PARAM::(\w+)::(\w+)::(\w+)/) { } 5 232 237
elsif ($line =~ /^LABEL::(\w+)/) { } 93 139 232
elsif ($line =~ /^COND::(\d+)::/) { } 9 130 139
elsif ($line =~ /^SIM::\w+/) { } 130 0 130
1369 43 ▶ my $blockparams = $ast->{block_mapping}->{$block_name}->{params} || [];
Condition50%1/2
$ast->{'block_mapping'}{$block_name}{'params'} || []
l!l
430
MC/DC0%0/1
$ast->{'block_mapping'}{$block_name}{'params'} || []
$ast->{'block_mapping'}{$block_name}{'params'}
Truth table50%1/2
A
A= $ast->{'block_mapping'}{$block_name}{'params'}
Aresult
11
00
1370 43 push @code, $self->generate_code_blocks($line, $block_name, $blockparams);
1371 } elsif ($line =~ /^INS::\w+/) {
1372 136 push @code, $self->generate_code_instruction($line);
1373 } elsif ($line =~ /^UNARY::\w+/) {
1374 8 push @code, $self->generate_code_unary_expr($line);
1375 } elsif ($line =~ /^SET::\w+/) {
1376 83 push @code, $self->generate_code_assign_expr($line);
1377 } elsif ($line =~ /^PARAM::(\w+)::(\w+)::(\w+)/) {
1378 5 ▶ if (exists $ast->{block_mapping}->{$block_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ast->{'block_mapping'}{$block_name}) { } 5 0 5
1379 5 my $op = $1;
1380 5 my $pblock = $2;
1381 5 my $pvar = $3;
1382 5 my $mapping = $ast->{block_mapping}->{$pblock};
1383 5 my $param_idx = scalar @{$mapping->{params}};
1384 5 ▶ my $paramvar = $mapping->{param_prefix} || lc($block_name . '_param');
Condition33%1/3
$mapping->{'param_prefix'} || lc $block_name . '_param'
l!l&&r!l&&!r
500
MC/DC0%0/2
$mapping->{'param_prefix'} || lc $block_name . '_param'
$mapping->{'param_prefix'} lc $block_name . '_param'
Truth table33%1/3
A || B
A= $mapping->{'param_prefix'}B= lc $block_name . '_param'
ABresult
1X1
011
000
1385 5 $paramvar .= $param_idx;
1386 5 push @{$mapping->{params}}, $paramvar;
1387 # map the param index back to the other mapping too
1388 5 ▶ if ($pblock ne $block_name and $mapping->{block} eq $block_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pblock ne $block_name and $mapping->{"block"} eq $block_name) 5 0 5
Condition33%1/3
$pblock ne $block_name and $mapping->{"block"} eq $block_name
!ll&&!rl&&r
005
MC/DC0%0/2
$pblock ne $block_name and $mapping->{"block"} eq $block_name
$pblock ne $block_name $mapping->{"block"} eq $block_name
Truth table33%1/3
A and B
A= $pblock ne $block_nameB= $mapping->{"block"} eq $block_name
ABresult
0X0
100
111
1389 5 my $mapping2 = $ast->{block_mapping}->{$block_name};
1390 5 $mapping2->{params} = $mapping->{params};
1391 }
1392 5 my $pline = "SET::${op}::${pvar}::${paramvar}";
1393 #YYY [$pblock, $pvar, $block_name, $param_idx, $pline, $paramvar];
1394 5 push @code, $self->generate_code_assign_expr($pline);
1395 } else {
1396 0 $self->parser->throw_error("Intermediate code '$line' in block "
1397 . "$block_name cannot be handled");
1398 }
1399 } elsif ($line =~ /^LABEL::(\w+)/) {
1400 93 my $lbl = $1;
1401 93 ▶ push @code, ";; $line" if $self->intermediate_inline;
Branch: 1 branch
BranchTrueFalseTotal
if $self->intermediate_inline 0 93 93
1402 93 ▶ push @code, "$lbl:\n" if $lbl ne '_vic_simulator';
Branch: 1 branch
BranchTrueFalseTotal
if $lbl ne "_vic_simulator" 75 18 93
1403 } elsif ($line =~ /^COND::(\d+)::/) {
1404 9 my $cblock = $1;
1405 9 my @condblocks = ( $line );
1406 9 for my $i (1 .. scalar @$blocks) {
1407 17 ▶ next unless $blocks->[$i - 1] =~ /^COND::${cblock}::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $$blocks[$i - bless( {"sign" => "+","value" => bless( [1], 'Math::BigInt::Calc' )}, 'Math::BigInt' )] =~ /^COND::$cblock::/ 14 3 17
1408 3 push @condblocks, $blocks->[$i - 1];
1409 3 delete $blocks->[$i - 1];
1410 }
1411 9 push @code, $self->generate_code_conditionals(reverse @condblocks);
1412 } elsif ($line =~ /^SIM::\w+/) {
1413 130 push @code, $self->generate_simulator_instruction($line);
1414 } else {
1415 0 $self->parser->throw_error("Intermediate code '$line' cannot be handled");
1416 }
1417 }
1418 92 ▶ return wantarray ? @code : [@code];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 92 0 92
1419 }
1420  
1421 sub final {
1422 32 ▶ my ($self, $got) = @_;
Subroutine final: called CC=40 SCAR=37.6
Pod documented
1423 32 my $ast = $self->ast;
1424 32 ▶ return $self->parser->throw_error("Missing '}'") if scalar @{$ast->{block_stack}};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$ast->{"block_stack"};} 0 32 32
1425 32 ▶ return $self->parser->throw_error("Main not defined") unless defined $ast->{Main};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ast->{"Main"} 0 32 32
1426 # generate main code first so that any addition to functions, macros,
1427 # variables during generation can be handled after
1428 32 my @main_code = $self->generate_code($ast, 'Main');
1429 31 push @main_code, "_end_start:\n", "\tgoto \$\t;;;; end of Main";
1430 31 my $main_code = join("\n", @main_code);
1431 # variables are part of macros and need to go first
1432 31 my $variables = '';
1433 31 my $vhref = $ast->{variables};
1434 31 ▶ $variables .= "GLOBAL_VAR_UDATA udata\n" if keys %$vhref;
Branch: 1 branch
BranchTrueFalseTotal
if keys %$vhref 19 12 31
1435 31 my @global_vars = ();
1436 31 my @tables = ();
1437 31 my @init_vars = ();
1438 31 foreach my $var (sort(keys %$vhref)) {
1439 42 my $name = $vhref->{$var}->{name};
1440 42 ▶ my $typ = $vhref->{$var}->{type} || 'byte';
Condition50%1/2
$vhref->{$var}{'type'} || 'byte'
l!l
420
MC/DC0%0/1
$vhref->{$var}{'type'} || 'byte'
$vhref->{$var}{'type'}
Truth table50%1/2
A
A= $vhref->{$var}{'type'}
Aresult
11
00
1441 42 my $data = $vhref->{$var}->{data};
1442 42 ▶ my $label = $vhref->{$var}->{label} || $name;
Condition66%2/3
$vhref->{$var}{'label'} || $name
l!l&&r!l&&!r
1410
MC/DC0%0/2
$vhref->{$var}{'label'} || $name
$vhref->{$var}{'label'} $name
Truth table66%2/3
A || B
A= $vhref->{$var}{'label'}B= $name
ABresult
1X1
011
000
1443 42 my $szvar = $vhref->{$var}->{size};
1444 42 ▶ if ($typ eq 'string') {
Branch: 3 branches
BranchTrueFalseTotal
if ($typ eq 'string') { } 2 40 42
elsif ($typ eq 'ARRAY') { } 0 40 40
elsif ($typ eq 'HASH') { } 1 39 40
1445 ##this may need to be stored in a different location
1446 2 ▶ $data = '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 2 2
1447 ## different PICs may have different string handling
1448 2 my ($scode, $szdecl)= $self->pic->store_string($data, $label, $szvar);
1449 2 push @tables, $scode;
1450 2 ▶ $variables .= $szdecl if $szdecl;
Branch: 1 branch
BranchTrueFalseTotal
if $szdecl 2 0 2
1451 } elsif ($typ eq 'ARRAY') {
1452 0 ▶ $data = [] unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
1453 0 push @init_vars, $self->pic->store_array($data, $label,
1454 scalar(@$data), $szvar);
1455 } elsif ($typ eq 'HASH') {
1456 1 ▶ $data = {} unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 1 1
1457 1 ▶ next unless defined $data->{TABLE};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data->{"TABLE"} 0 1 1
1458 1 my $table = $data->{TABLE};
1459 1 my ($code, $szdecl) = $self->pic->store_table($table, $label,
1460 scalar(@$table), $szvar);
1461 1 push @tables, $code;
1462 1 ▶ push @init_vars, $szdecl if $szdecl;
Branch: 1 branch
BranchTrueFalseTotal
if $szdecl 1 0 1
1463 } else {# $typ == 'byte' or any other
1464 # should we care about scope ?
1465 39 $variables .= "$name res $vhref->{$var}->{size}\n";
1466 39 ▶ if (($vhref->{$var}->{scope} eq 'global') or
Branch: 1 branch
BranchTrueFalseTotal
if ($vhref->{$var}{"scope"} eq "global" or $ast->{"code_config"}{"variable"}{"export"}) 20 19 39
Condition100%3/3
$vhref->{$var}{"scope"} eq "global" or $ast->{"code_config"}{"variable"}{"export"}
l!l&&r!l&&!r
81219
MC/DC100%2/2
$vhref->{$var}{"scope"} eq "global" or $ast->{"code_config"}{"variable"}{"export"}
$vhref->{$var}{"scope"} eq "global" $ast->{"code_config"}{"variable"}{"export"}
Truth table100%3/3
A or B
A= $vhref->{$var}{"scope"} eq "global"B= $ast->{"code_config"}{"variable"}{"export"}
ABresult
1X1
011
000
1467 ($ast->{code_config}->{variable}->{export})) {
1468 20 push @global_vars, $name;
1469 }
1470 }
1471 }
1472 31 ▶ if ($ast->{tmp_stack_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ast->{"tmp_stack_size"}) 3 28 31
1473 3 $variables .= "VIC_STACK res $ast->{tmp_stack_size}\t;; temporary stack\n";
1474 }
1475 31 ▶ if (scalar @global_vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @global_vars) 5 26 31
1476 # export the variables
1477 5 $variables .= "\tglobal ". join (", ", @global_vars) . "\n";
1478 }
1479 31 ▶ if (scalar @init_vars) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @init_vars) 1 30 31
1480 1 $variables .= "\nGLOBAL_VAR_IDATA idata\n"; # initialized variables
1481 1 $variables .= join("\n", @init_vars);
1482 }
1483 31 my $macros = '';
1484 31 foreach my $mac (sort(keys %{$ast->{macros}})) {
1485 80 ▶ $variables .= "\n" . $ast->{macros}->{$mac} . "\n", next if $mac =~ /_var$/;
Branch: 1 branch
BranchTrueFalseTotal
if $mac =~ /_var$/ 25 55 80
1486 55 $macros .= $ast->{macros}->{$mac};
1487 55 $macros .= "\n";
1488 }
1489 31 my $isr_checks = '';
1490 31 my $isr_code = '';
1491 31 my $funcs = '';
1492 31 foreach my $fn (sort(keys %{$ast->{funcs}})) {
1493 45 my $fn_val = $ast->{funcs}->{$fn};
1494 # the default ISR checks to be done first
1495 45 ▶ if ($fn =~ /^isr_\w+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fn =~ /^isr_\w+$/) { } 5 40 45
elsif ($fn =~ /^_isr_\w+$/) { } 5 35 40
1496 5 ▶ if (ref $fn_val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fn_val eq 'ARRAY') { } 0 5 5
1497 0 $isr_checks .= join("\n", @$fn_val);
1498 } else {
1499 5 $isr_checks .= $fn_val . "\n";
1500 }
1501 # the user ISR code to be handled next
1502 } elsif ($fn =~ /^_isr_\w+$/) {
1503 5 ▶ if (ref $fn_val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fn_val eq 'ARRAY') { } 5 0 5
1504 5 $isr_code .= join("\n", @$fn_val);
1505 } else {
1506 0 $isr_code .= $fn_val . "\n";
1507 }
1508 } else {
1509 35 ▶ if (ref $fn_val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fn_val eq 'ARRAY') { } 22 13 35
1510 22 $funcs .= join("\n", @$fn_val);
1511 } else {
1512 13 $funcs .= "$fn:\n";
1513 13 ▶ $funcs .= $fn_val unless ref $fn_val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fn_val eq "ARRAY" 13 0 13
1514 }
1515 35 $funcs .= "\n";
1516 }
1517 }
1518 31 foreach my $tbl (@{$ast->{tables}}) {
1519 5 my $dt = $tbl->{bytes};
1520 5 my $dn = $tbl->{name};
1521 }
1522 31 ▶ $funcs .= join ("\n", @tables) if scalar @tables;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @tables 3 28 31
1523 31 $funcs .= $self->pic->store_bytes($ast->{tables});
1524 31 ▶ if (length $isr_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $isr_code) 5 26 31
1525 5 my $isr_entry = $self->pic->isr_entry;
1526 5 my $isr_exit = $self->pic->isr_exit;
1527 5 my $isr_var = $self->pic->isr_var;
1528 5 $isr_checks .= "\tgoto _isr_exit\n";
1529 5 $isr_code = "\tgoto _start\n$isr_entry\n$isr_checks\n$isr_code\n$isr_exit\n";
1530 5 $variables .= "\n$isr_var\n";
1531 }
1532 31 my ($sim_include, $sim_setup_code) = ('', '');
1533 # we need to generate simulator code if either the Simulator block is
1534 # present or if any asserts are present
1535 31 ▶ if ($self->simulator and not $self->simulator->disable and
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simulator and not $self->simulator->disable and $ast->{"Simulator"} || $ast->{"asserts"}) 19 12 31
Condition88%8/9
$self->simulator and not $self->simulator->disable and $ast->{"Simulator"} || $ast->{"asserts"}
!ll&&!rl&&r
1291
$self->simulator and not $self->simulator->disable
!ll&&!rl&&r
0130
$ast->{"Simulator"} or $ast->{"asserts"}
l!l&&r!l&&!r
18111
MC/DC50%2/4
$self->simulator and not $self->simulator->disable and $ast->{"Simulator"} || $ast->{"asserts"}
$self->simulator not $self->simulator->disable $ast->{"Simulator"} $ast->{"asserts"}
Truth table60%3/5
A and not(B) and C or D
A= $self->simulatorB= not $self->simulator->disableC= $ast->{"Simulator"}D= $ast->{"asserts"}
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1536 ($ast->{Simulator} or $ast->{asserts})) {
1537 19 my $stype = $self->simulator->type;
1538 19 $sim_include .= ";;;; generated code for $stype header file\n";
1539 19 $sim_include .= '#include <' . $self->simulator->include .">\n";
1540 19 my @setup_code = $self->generate_code($ast, 'Simulator');
1541 19 my $init_code = $self->simulator->init_code;
1542 19 ▶ $sim_setup_code .= $init_code . "\n" if defined $init_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $init_code 19 0 19
1543 19 ▶ $sim_setup_code .= join("\n", @setup_code) if scalar @setup_code;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @setup_code 18 1 19
1544 19 ▶ if ($self->simulator->should_autorun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->simulator->should_autorun) 12 7 19
1545 12 $sim_setup_code .= $self->simulator->get_autorun_code;
1546 }
1547 }
1548 # final get of the chip config in case it has been modified
1549 31 $self->ast->{chip_config} = $self->pic->get_chip_config;
1550 31 my $pic = <<"...";
1551 ;;;; generated code for PIC header file
1552 #include <$ast->{include}>
1553 $sim_include
1554 ;;;; generated code for variables
1555 $variables
1556 ;;;; generated code for macros
1557 $macros
1558  
1559 $ast->{chip_config}
1560  
1561 \torg $ast->{org}
1562  
1563 $sim_setup_code
1564  
1565 $isr_code
1566  
1567 $main_code
1568  
1569 ;;;; generated code for functions
1570 $funcs
1571 ;;;; generated code for end-of-file
1572 \tend
1573 ...
1574 31 return $pic;
1575 }
1576  
1577 33 1;
1578  
1579 =encoding utf8
1580  
1581 =head1 NAME
1582  
1583 VIC::Receiver
1584  
1585 =head1 SYNOPSIS
1586  
1587 The Pegex::Receiver class for handling the grammar.
1588  
1589 =head1 DESCRIPTION
1590  
1591 INTERNAL CLASS.
1592  
1593 =head1 AUTHOR
1594  
1595 Vikas N Kumar <vikas@cpan.org>
1596  
1597 =head1 COPYRIGHT
1598  
1599 Copyright (c) 2014. Vikas N Kumar
1600  
1601 This program is free software; you can redistribute it and/or modify it
1602 under the same terms as Perl itself.
1603  
1604 See http://www.perl.com/perl/misc/Artistic.html
1605  
1606 =cut
« VIC/PIC/Functions/Operators.pm ↑ Summary VIC.pm »