blib/lib/Perlilog/sysclasses/PLcodegen.pl

filter: Statementstmt 3.2% 4 / 125 Branchbran 1.8% 1 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 3.8% 1 / 26 Podpod n/a totaltotal 2.9% 6 / 205 CC 33 SCAR 32.1
CC
33
Cov
3%
wheretorec
72.0
labelID
56.0
attach
30.0
CRAP 24.9
« blib/lib/Perlilog.pm ↑ Summary blib/lib/Perlilog/sysclasses/PLport.pl »
1 #
2 # This file is part of the Perlilog project.
3 #
4 # Copyright (C) 2003, Eli Billauer
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #
20 # A copy of the license can be found in a file named "licence.txt", at the
21 # root directory of this project.
22 #
23  
24 ${__PACKAGE__.'::errorcrawl'}='system';
25 0 ▶sub who {
Subroutine who: not called CC=1 SCAR=6.9
26 0 my $self = shift;
27 0 return "CodeGen. Obj. \'".$self->get('name')."\'";
28 }
29  
30 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
31 1 my $this = shift;
32 1 my $self = $this->SUPER::new(@_);
33 1 ▶ $self -> registerobject($self -> get('beginend'))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Perlilog::interface_rec 1 0 1
34 unless (defined $Perlilog::interface_rec);
35
36 1 return $self;
37 }
38  
39 0 ▶sub sustain {
Subroutine sustain: not called CC=1 SCAR=6.9
40 0 my $self = shift;
41 0 $self->SUPER::sustain(@_);
42 0 $self -> registerobject($self -> get('beginend'));
43 }
44  
45 0 ▶sub complete {
Subroutine complete: not called CC=1 SCAR=6.9
46 0 my $self = shift;
47 0 $self->SUPER::complete(@_);
48 0 $self->set('header-comment',
49 "// This is a generated file. Do not edit -- changes will be lost\n".
50 "// Created by Perlilog v".
51 $Perlilog::VERSION." on ".$Perlilog::STARTTIME."\n".
52 "// Originating object: ".$self->who."\n\n");
53 }
54  
55 0 ▶sub IDvar {
Subroutine IDvar: not called CC=4 SCAR=30.0
56 0 my ($junk, $ID)=@_;
57 0 my ($obj, $var)=@{$Perlilog::VARS[$ID]};
58 0 ▶ if (ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj) { } 0 0 0
59 0 ▶ return ($obj, $var) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
60 0 return $var;
61 } else {
62 0 ▶ return () if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
63 0 return undef;
64 }
65 }
66  
67 0 ▶sub varwho {
Subroutine varwho: not called CC=2 SCAR=17.9
68 0 my ($junk, $ID)=@_;
69 0 my ($obj, $var)=@{$Perlilog::VARS[$ID]};
70 0 ▶ return "(unknown var ID $ID)" unless (ref $obj);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 0 0
71 0 my $name=$obj->get('name');
72 0 return "\'$var\' in module \'$name\'";
73 }
74  
75 0 ▶sub attach {
Subroutine attach: not called CC=5 SCAR=34.0
76 #TODO: Save the comment both for immediate use and log it as well.
77  
78 # Get the details of the variables involved...
79 # Note that it doesn't matter which object we are
80 0 my ($junk, $ID1, $ID2, $comment) = @_;
81 0 my ($obj1, $var1) = @{$Perlilog::VARS[$ID1]};
82 0 my ($obj2, $var2) = @{$Perlilog::VARS[$ID2]};
83 0 my $eq1 = $Perlilog::EQVARS[$ID1];
84 0 my $eq2 = $Perlilog::EQVARS[$ID2];
85  
86 0 ▶ puke("attach() run with illegal ID1=$ID1\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj1 0 0 0
87 unless (ref $obj1);
88 0 ▶ puke("attach() run with illegal ID2=$ID2\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj2 0 0 0
89 unless (ref $obj2);
90  
91 0 ▶ return 1 if ($eq1 eq $eq2); # Do nothing if they are already connected
Branch: 1 branch
BranchTrueFalseTotal
if $eq1 eq $eq2 0 0 0
92
93 # Make a new equivalence list, and update all relevant entries.
94 0 my @neweq = (@{$eq1}, @{$eq2});
95 0 my $i;
96 0 foreach $i (@neweq) {
97 0 $Perlilog::EQVARS[$i] = \@neweq;
98 }
99  
100 # Set magic callbacks to update (or check) the 'dim' property mutually.
101 # If you read this and try to imitate, you'd better know a few things
102 # about the scope in which the anonymous subroutine is run.
103 # You've been warned.
104  
105 $obj1->addmagic(['vars', $var1, 'dim'],
106 0 ▶ sub { $obj2->const(['vars', $var2, 'dim'],
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 $obj1->get(['vars', $var1, 'dim'])); });
108 $obj2->addmagic(['vars', $var2, 'dim'],
109 0 ▶ sub { $obj1->const(['vars', $var1, 'dim'],
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 $obj2->get(['vars', $var2, 'dim'])); });
111 0 return 1;
112 }
113  
114 0 ▶sub samedim {
Subroutine samedim: not called CC=4 SCAR=30.0
115 0 my $self = shift;
116 0 my $var1 = shift;
117 0 ▶ puke("samedim called with unknown variable name ".$self->prettyval($var1).
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get(["vars", $var1, "ID"]) 0 0 0
118 " on object ".$self->who."\n")
119 unless (defined $self->get(['vars', $var1, 'ID']));
120 0 my $i;
121 0 foreach $i (@_) {
122 # We get a local copy of $i for this BLOCK ($var2).
123 # We can't use $i, because by the time the callback is executed,
124 # its value may have been altered.
125 0 my $var2 = $i;
126 0 ▶ puke("samedim called with unknown variable name ".$self->prettyval($var2).
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get(["vars", $var2, "ID"]) 0 0 0
127 " on object ".$self->who."\n")
128 unless (defined $self->get(['vars', $var2, 'ID']));
129  
130 $self->addmagic(['vars', $var1, 'dim'],
131 0 ▶ sub { $self->const(['vars', $var2, 'dim'],
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 $self->get(['vars', $var1, 'dim'])); });
133 $self->addmagic(['vars', $var2, 'dim'],
134 0 ▶ sub { $self->const(['vars', $var1, 'dim'],
Subroutine __ANON__: not called CC=1 SCAR=6.9
135 0 $self->get(['vars', $var2, 'dim'])); });
136 }
137 }
138  
139 0 ▶sub getID {
Subroutine getID: not called CC=4 SCAR=30.0
140 0 my $self = shift;
141 0 my @vars = @_;
142 0 my $ID;
143 0 foreach (@vars) {
144 0 $ID = $self->get(['vars', $_, 'ID']);
145 0 ▶ puke("getID called with unknown variable name\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ID 0 0 0
146 unless (defined $ID);
147 0 $_=$ID;
148 }
149 0 ▶ return @vars if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
150 0 return $vars[0];
151 }
152  
153 0 ▶sub getport {
Subroutine getport: not called CC=2 SCAR=17.9
154 0 my $self = shift;
155 0 my $name = shift;
156 0 my $port = $self->get(['user_port_names', $name]);
157  
158 0 ▶ puke("Failed to find port \'$name\' in ".$self->who."\n")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $port 0 0 0
159 unless (ref $port);
160 0 return $port;
161 }
162  
163  
164 0 ▶sub labelID {
Subroutine labelID: not called CC=7 SCAR=40.3
165 0 my ($self, $port) = @_;
166 0 ▶ puke("labelID called with non-object argument\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isobject($port) 0 0 0
167 unless ($self->isobject($port));
168 0 my $obj = $port->get('parent');
169 0 ▶ puke("labelID called with a port with no parent (is it really a port?)\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isobject($obj) 0 0 0
170 unless ($self->isobject($obj));
171 0 my %h=$port->get('labels');
172 0 my ($val, $name);
173 0 foreach (sort keys %h) {
174 0 $name = $h{$_};
175 0 ▶ if ($name =~ /^\d+$/) { # $name is an ID?
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^\d+$/) 0 0 0
176 0 ▶ wrong("Unknown variable ID ".$self->prettyval($name).
Branch: 1 branch
BranchTrueFalseTotal
unless $self->IDvar($name) 0 0 0
177 " given as \'$_\' in 'labels' property of ".
178 $port->who()."\n")
179 unless ($self->IDvar($name));
180 0 next; # It's in ID format. No more hassle.
181 }
182 0 $val = $obj->get(['vars', $name, 'ID']);
183 0 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 0 0
184 0 $h{$_} = $val;
185 } else {
186 0 wrong("Undefined variable ".$self->prettyval($name).
187 " given as \'$_\' in 'labels' property of ".$port->who()."\n");
188 0 delete $h{$_};
189 }
190 }
191 0 return %h;
192 }
193  
194 0 ▶sub interface {
Subroutine interface: not called CC=2 SCAR=17.9
195 0 my $self = shift;
196 0 my $obj = &Perlilog::interface(@_);
197 0 ▶ $obj->setparent($self) if ($self->isobject($obj));
Branch: 1 branch
BranchTrueFalseTotal
if $self->isobject($obj) 0 0 0
198 0 return $obj;
199 }
200  
201 0 ▶sub getreset {
Subroutine getreset: not called CC=2 SCAR=17.9
202 0 my $self = shift;
203 0 my $global = $self->globalobj();
204 0 my $type = $global->get('reset_type');
205 0 my $ID = $global->get('reset_ID');
206 0 ▶ return ($ID, $type) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
207 0 return $ID;
208 }
209  
210 0 ▶sub wheretorec {
Subroutine wheretorec: not called CC=8 SCAR=42.8
211 0 my $self = shift;
212  
213 # First we check up if we've already answered this question.
214  
215 0 my $cached = $self->get('perlilog-whereto-answer');
216 0 ▶ return $cached if defined($cached);
Branch: 1 branch
BranchTrueFalseTotal
if defined $cached 0 0 0
217  
218 # Now we ask ourselves for recommendations. "self" is always
219 # assumed as a last (possibly only) resort, so we add it.
220  
221 0 my @targets = $self->codetargets;
222  
223 0 @targets = ((grep { ref } @targets), $self);
224  
225 # A yes/no lookup hash for those objects that we are not allowed
226 # to return to (avoiding infinite recursion)
227 0 my %blacklisted = map {($_, 1)} @_;
228  
229 0 my $answer = undef;
230  
231 0 foreach my $target (@targets) {
232 0 ▶ next if ($blacklisted{$target});
Branch: 1 branch
BranchTrueFalseTotal
if $blacklisted{$target} 0 0 0
233 0 ▶ next if ($target->get('static')); # Static objects are no targets
Branch: 1 branch
BranchTrueFalseTotal
if $target->get("static") 0 0 0
234 0 ▶ if ($target == $self) { # $self was a last resort, remember?
Branch: 1 branch
BranchTrueFalseTotal
if ($target == $self) 0 0 0
235 0 $answer = $target;
236 0 last; # Perl novices: "last" means that we're skipping the rest...
237 }
238 # We want someone else to hold our code. But maybe this "someone else"
239 # has a better idea? Let's ask. Note that when looking for that better
240 # idea, $self has been added to the black list, so we won't loop around
241 # forever.
242
243 0 my $gossip = $target->wheretorec(@_, $self);
244 0 ▶ if (ref $gossip) { # Did we get a solid answer?
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gossip) 0 0 0
245 0 $answer = $gossip;
246 0 last;
247 }
248 }
249 # Remember our answer if it's worth anything.
250 0 ▶ $self->const('perlilog-whereto-answer', $answer)
Branch: 1 branch
BranchTrueFalseTotal
if ref $answer 0 0 0
251 if (ref $answer);
252 0 return $answer;
253 }
254  
255 0 ▶sub whereto {
Subroutine whereto: not called CC=2 SCAR=17.9
256 0 my $self = shift;
257 0 my $answer = $self->wheretorec();
258  
259 0 ▶ wrong("Failed to find an object to put the Verilog code created by ".
Branch: 1 branch
BranchTrueFalseTotal
unless ref $answer 0 0 0
260 $self->who." (this is strange)\n")
261 unless (ref $answer);
262  
263 0 return $answer;
264 }
265  
266 0 ▶sub codetargets {
Subroutine codetargets: not called CC=1 SCAR=6.9
267 0 return (); # By default, no other objects to divert the code to
268 }
269  
270 # Empty methods (to avoid unknown method error)
271 0 ▶sub sanity {}
Subroutine sanity: not called CC=1 SCAR=0.0
272 0 ▶sub generate {}
Subroutine generate: not called CC=1 SCAR=0.0
273 0 ▶sub instantiate {}
Subroutine instantiate: not called CC=1 SCAR=0.0
274 0 ▶sub headers {}
Subroutine headers: not called CC=1 SCAR=0.0
275 0 ▶sub epilogue {}
Subroutine epilogue: not called CC=1 SCAR=0.0
276 0 ▶sub files {}
Subroutine files: not called CC=1 SCAR=0.0
« blib/lib/Perlilog.pm ↑ Summary blib/lib/Perlilog/sysclasses/PLport.pl »