blib/lib/Perlilog/siteclasses/template.pl untested

filter: Statementstmt 0.0% 0 / 193 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 343 CC 46 SCAR 74.6
CC
46
Cov
0%
new
1980.0
epilogue
12.0
who
2.0
CRAP 1743.2
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 # Template class
25 ${__PACKAGE__.'::errorcrawl'}='system';
26 sub who {
27 my $self = shift;
28 return "Template Verilog Obj. \'".$self->get('name')."\'";
29 }
30  
31 sub new {
32 my $this = shift;
33 my $self = $this->SUPER::new(@_);
34  
35 my $file = $self->get('tfile');
36  
37 wrong("The \'tfile\' property was not set on ".$self->who."\n")
38 unless (defined $file);
39
40 open (FILE, $file) || blow("Failed to open template file $file for ".
41 $self->who."\n");
42  
43 my @keywords = qw(reg wire iwire input output inout parameter integer real time);
44 my $line;
45 my $verilog = '';
46 my $extrahead = '';
47 my $prehead = '';
48 my ($deftype, $dim, $names, $w);
49 my ($portname, $portclass, $binds);
50 my @ports;
51 my @vars;
52 my %vhash = ();
53 my %chash = ();
54 my $modulecount = 0;
55 my $c = 0; # A counter for preserving the variable's order
56  
57 # We choose a namespace name for the inline Perls
58 my $pack = $self->get('name').'_inlineperl';
59
60 while (!eof(FILE)) {
61 $line = <FILE>; chomp $line;
62
63 # If we have "module", flush until semicolon
64 if ($line =~ /^[\t\s]*module/) {
65 $modulecount++;
66 if ($modulecount > 1) {
67 wrong("More than one module in $file.\n");
68 }
69 wrong("Variables were declared before module header of line $. in $file\n")
70 if (keys %vhash);
71
72 $prehead = $verilog;
73 $verilog = '';
74
75 while (not (eof(FILE) || ($line =~ /;/))) {
76 $line = <FILE>;
77 }
78 next;
79 }
80
81 # If we have "task" or "function", flush until the end of it
82 if ($line =~ /^[\t\s]*(task|function)/) {
83 $verilog .= $line."\n";
84 while (!eof(FILE)) {
85 $line = <FILE>; chomp $line;
86 $verilog .= $line."\n";
87 last if ($line =~ /^[\t\s]*(endtask|endfunction)/);
88 }
89 next;
90 }
91  
92 # Now we take care of perl in-lines
93 if ($line =~ /^[\t\s]*perl(\w*)/) {
94 wrong("Strange expression \'perl$1\' in $file, line $.\n")
95 if ((length($1)>0) && ($1 ne 'onload'));
96 my $toset = ($1 eq 'onload') ? 'early' : 'late';
97 my $perl = '';
98 my $startline = $. + 1;
99 while (!eof(FILE)) {
100 $line = <FILE>; chomp $line;
101 last if ($line =~ /^[\t\s]*(endperl)/);
102 $perl .= $line."\n";
103 }
104 $perl = "no strict;\npackage $pack;\n# line $startline \"$file\"\n".$perl;
105 $self->ppush("perl-inline-$toset", $perl);
106 eval("\$".$pack.'::errorcrawl = "halt";'); # We don't want stack dumps.
107 next;
108 }
109
110 next if ($line =~ /^[\t\s]*endmodule/); # Skip "endmodule"
111 next if ($line =~ /^\`timescale/); # Skip timescale
112
113 # We begin with detecting a port declaration
114 ($portname, $portclass, $binds) =
115 ($line =~ /^[\s\t]*port[\s\t]+(\w+)[\s\t]+(\w+)[\s\t]+([^;]*?)(\/\/|[;]|$)/);
116
117 if (defined $portname) {
118 unless (Perlilog::definedclass($portclass)) {
119 wrong("Attempt to use undefined port class \'$portclass\' in $file, line $.\n");
120 }
121  
122 # If the line didn't end with ";", go on searching.
123 while (not (eof(FILE) || ($line =~ /;/))) {
124 $line = <FILE>; chomp $line;
125 $line =~ /^(.*?)(\/\/|[;]|$)/;
126 $binds.=$1;
127 }
128 $binds =~ s/[\s\t]//g;
129 push @ports, [$portname, $portclass, $binds];
130 next;
131 } elsif ($line =~ /^[\s\t]*port/) {
132 wrong("Bad port declaration syntax in $file, line $.\n");
133 }
134
135 # Now detect variable declarations
136 $dim = undef;
137 ($deftype, $dim, $names) =
138 ($line =~ /^[\s\t]*([a-zA-Z]+)[\s\t]*(\[\s*\d+\s*:\s*\d+\s*\]|\[\s*:\s*\])[\s\t]+([,\s\tA-Za-z0-9_]*)(\/\/|[;]|$)/);
139 ($deftype, $names) =
140 ($line =~ /^[\s\t]*([a-zA-Z]+)[\s\t]+([,\s\tA-Za-z0-9_]*)(\/\/|[;]|$)/)
141 unless (defined $deftype);
142
143 # Now we look for special formats, where we want to have the variable name
144 # registered, but we can't handle it further. Namely, when the variable
145 # is a matrix, or when it's initialized.
146
147 unless (defined $deftype) {
148 ($deftype, $names) =
149 ($line =~ /^[\s\t]*([a-zA-Z]+)[\s\t]*\[.*\][\s\t]*([,\s\tA-Za-z0-9_]*?)(\/\/|[;]|$)/);
150 if (defined $deftype) {
151 if (grep {$deftype eq $_} qw(input output inout)) {
152 undef $deftype;
153 } else {
154 $dim = '[]';
155 }
156 }
157 }
158 unless (defined $deftype) {
159 ($deftype, $names) =
160 ($line =~ /^[\s\t]*([a-zA-Z]+)[\s\t]+([,\s\tA-Za-z0-9_]*)[\s\t]*[=].*/);
161 $dim = '[]' if (defined $deftype)
162 }
163
164 unless ((defined $deftype) &&
165 (grep {$deftype eq $_} @keywords)) {
166 $verilog .= $line."\n";
167 ($w) = ($line =~ /^[\s\t]*([a-zA-Z]+)[\s\t]+/);
168 fishy("This line in $file, line $. was probably not handled properly:\n$line\n")
169 if ((defined $w) &&
170 (grep {$w eq $_} @keywords));
171 next; # Next line if we put this one in $verilog...
172 }
173
174 # Note that if we got here, then we have some "legal" variable
175 # declaration to play with.
176  
177 # If the line didn't end with ";", go on searching.
178 while (not (eof(FILE) || ($line =~ /;/))) {
179 $line = <FILE>; chomp $line;
180 $line =~ /^(.*?)(\/\/|[;]|$)/;
181 $names.=$1;
182 }
183
184 $dim = '' unless (defined $dim); # Single-bit variable
185 $dim =~ s/\s//g;
186 undef $dim if ($dim eq '[:]'); # Special "unknown width" format.
187 $names =~ s/[;\s\t]//g;
188
189 foreach my $n (split(',', $names)) {
190 $c++;
191  
192 if ((defined $dim) && ($dim eq '[]')) { # Variables with troublesome dimensions
193 $vhash{$n} = [$n]; # Only register name
194 $chash{$n} = $c unless (defined $chash{$n});
195 $extrahead .= $line."\n";
196 }
197 elsif (($deftype eq 'iwire') || ($deftype eq 'input')) {
198 $vhash{$n} = [$n, 'wire', 'in', $dim];
199 $chash{$n} = $c unless (defined $chash{$n});
200 }
201 elsif (($deftype eq 'wire') || ($deftype eq 'output')) {
202 unless (defined $vhash{$n}) { # Respect if it was defined as a register
203 $vhash{$n} = [$n, 'wire', 'out', $dim];
204 $chash{$n} = $c;
205 }
206 }
207 elsif ($deftype eq 'inout') {
208 $vhash{$n} = [$n, 'wire', 'zout', $dim];
209 $chash{$n} = $c unless (defined $chash{$n});
210  
211 }
212 elsif ($deftype eq 'reg') {
213 $vhash{$n} = [$n, 'reg', 'out', $dim];
214 $chash{$n} = $c unless (defined $chash{$n});
215 }
216 else {
217 $vhash{$n} = [$n]; # Only register name
218 $chash{$n} = $c unless (defined $chash{$n});
219 $extrahead .= $line."\n";
220 }
221 }
222 }
223 close FILE;
224
225 # We now register all the variables. %chash tells us which
226 # came before which, so we sort the variables accordingly
227
228 foreach my $n (sort { $chash{$a} <=> $chash{$b} } keys %vhash) {
229 $self->addvar( @{$vhash{$n}} );
230 }
231  
232 # Now we parse port definitions, and set the properties and labels.
233 foreach (@ports) {
234 ($portname, $portclass, $binds) = @{$_};
235 my $port;
236 my $realname = $self->suggestname($portname);
237 my %labels = ();
238 my ($x, $is, $y);
239  
240 eval {
241 $port = $portclass -> new(name => $realname,
242 parent => $self);
243 };
244 if ($@) {
245 wrong("Failed to create port $portname in $file:\n$@");
246 next;
247 }
248 foreach my $z (split(',', $binds)) {
249 ($x, $is, $y) = ($z=~/^(.*?)([=]|[:])(.*)/);
250
251 unless (defined $is) {
252 wrong("Bad expression \'$z\' in the definition of $portname\n");
253 next;
254 }
255
256 if ($is eq '=') {
257 $port->const($x, $y);
258 } else {
259 wrong("Numeric value \'$y\' is unallowed on label \'$x\' for port \'$portname\' in \'$file\'\n")
260 if ($y =~ /^\d+$/);
261 $labels{$x}=$y;
262 unless (defined $vhash{$y}) {
263 wrong("Undeclared variable \'$y\' bound to label \'$x\' for port \'$portname\' in \'$file\'\n");
264 next;
265 }
266 }
267 }
268 $port->set('labels', %labels);
269 $self->const(['user_port_names', $portname], $port);
270 }
271 $self->set('verilog', $verilog);
272 $self->set('prehead', $prehead);
273 $self->set('extrahead', $extrahead);
274  
275 # We now execute the inline perl pieces, if needed
276 my @perls = $self->get('perl-inline-early');
277  
278 if (@perls) {
279 my $origpack = __PACKAGE__;
280  
281 foreach my $p (@perls) {
282 my $err = $self->myeval($p);
283 blow("Failure in inline script:\n$err") if $err;
284 }
285 eval("package $origpack;"); # back to home (this shouldn't really matter, actually)
286 }
287 return $self;
288 }
289  
290 sub headers {
291 my $self = shift;
292 $self->ontop($self->get('extrahead'));
293 $self->SUPER::headers(@_);
294 $self->ontop($self->get('prehead'));
295 }
296  
297 sub epilogue {
298 my $self = shift;
299 $self->SUPER::epilogue(@_);
300  
301 my @perls = $self->get('perl-inline-late');
302 return unless @perls;
303  
304 my $origpack = __PACKAGE__;
305
306 foreach my $p (@perls) {
307 my $err=$self->myeval($p);
308 blow("Failure in inline script:\n$err") if $err;
309 }
310 eval("package $origpack;"); # back to home (this shouldn't really matter, actually)
311 }
312  
313 sub myeval {
314 my $self = shift;
315 eval(shift);
316 return $@;
317 }