File Coverage

blib/lib/SystemC/Vregs/File.pm
Criterion Covered Total %
statement 79 130 60.7
branch 19 56 33.9
condition 1 9 11.1
subroutine 12 17 70.5
pod 6 10 60.0
total 117 222 52.7


line stmt bran cond sub pod time code
1             # See copyright, etc in below POD section.
2             ######################################################################
3              
4             package SystemC::Vregs::File;
5 1     1   5 use File::Basename;
  1         1  
  1         94  
6 1     1   6 use vars qw($VERSION);
  1         2  
  1         34  
7 1     1   4 use base qw(SystemC::Vregs::Language);
  1         2  
  1         77  
8              
9 1     1   4 use SystemC::Vregs::Number;
  1         2  
  1         31  
10 1     1   5 use SystemC::Vregs::Language;
  1         2  
  1         15  
11 1     1   4 use strict;
  1         2  
  1         25  
12 1     1   4 use Carp;
  1         2  
  1         1436  
13              
14             $VERSION = '1.470';
15              
16             ######################################################################
17             ######################################################################
18             # Files
19              
20             sub open {
21 1     1 1 2 my $class = shift;
22             # General routine for opening output file and starting header
23              
24 1         6 my %params = @_;
25 1 50       4 $params{language} or croak "%Error: No language=> specified,";
26              
27 1         13 my $self = $class->SUPER::new(verbose=>1,
28             # noheader=>0,
29             %params);
30              
31 1         87 my ($name,$path,$suffix) = fileparse($self->{filename},'\..*');
32 1         4 my $template_filename = $path.$name."__template".$suffix;
33 1 50       4 print "Check Template File $template_filename\n" if $SystemC::Vregs::Debug;
34 1 50       18 if (-r $template_filename) {
35             #$self->{template}->read (filename=>$template_filename);
36             }
37              
38 1 50       4 if (!$self->{noheader}) {
39 1 50       6 $self->print("// -*- C++ -*-\n") if ($self->{CPP});
40 1 50       4 $self->print("\n") if ($self->{XML});
41 1         12 $self->comment("DO NOT EDIT -- Generated automatically by vregs\n");
42 1 50 33     7 if ($self->{C} || $self->{CPP}) {
43 1         19 $self->comment_pre("\\file\n");
44 1         5 $self->comment_pre("\\brief Register Information: Generated automatically by vregs\n");
45             } else {
46 0         0 $self->comment("DESC"."RIPTION: Register Information: Generated automatically by vregs\n");
47             }
48 1         5 $self->comment_pre("\n");
49             }
50              
51 1 50       6 if ($self->{rules}) {
52 1         8 $self->{rules}->filehandle($self);
53 1         5 foreach my $rfile ($self->{rules}->filenames()) {
54 0         0 $rfile = basename($rfile,"^");
55 0         0 $self->comment_pre("See SystemC::Vregs::Rules file: $rfile\n");
56             }
57 1         5 $self->comment_pre("\n");
58             }
59              
60 1 50       9 $self->{rules}->execute_rule ('any_file_before', 'any_file', $self) if $self->{rules};
61              
62 1         6 return $self;
63             }
64              
65             sub close {
66 1     1 1 5 my $self = shift;
67             # General routine for closing output file
68              
69 1         12 $self->close_prep();
70              
71 1 50       10 $self->{rules}->execute_rule ('any_file_after', 'any_file', $self) if $self->{rules};
72              
73 1 50       7 if (!$self->{noheader}) {
74 1         5 $self->print("\n");
75 1         8 $self->comment ("DO NOT EDIT -- Generated automatically by vregs\n");
76             }
77              
78 1         13 $self->SUPER::close();
79             }
80              
81             sub private_not_public {
82 0     0 1 0 my $self = shift;
83 0         0 my $private = shift;
84 0         0 my $pack = shift;
85             # Print public: or private: depending on desired state
86              
87 0 0       0 my $enabled = (defined $pack->{rules}{protect_rdwr_only}
88             ? $pack->{rules}{protect_rdwr_only}
89             : $pack->{protect_rdwr_only});
90 0 0       0 $private = 0 if !$enabled;
91 0 0       0 if ($self->{CPP}) {
92 0 0 0     0 if ($private && !$self->{private}) {
93 0         0 $self->print ("protected:\n");
94             }
95 0 0 0     0 if (!$private && $self->{private}) {
96 0         0 $self->print ("public:\n");
97             }
98             }
99 0         0 $self->set_private($private);
100             }
101              
102             sub set_private {
103 0     0 0 0 my $self = shift;
104 0         0 my $private = shift;
105 0         0 $self->{private} = $private;
106             }
107              
108             sub fn {
109 0     0 1 0 my $self = shift;
110 0         0 my $clname = shift;
111 0         0 my $suffix = shift;
112 0         0 my $proto = shift;
113             # Declare a function with C++ semantics, mangle into C if necessary
114 0 0       0 if ($self->{CPP}) {
115 0         0 $self->print (" $proto ",@_);
116             } else {
117 0 0       0 my $const = ($proto =~ s/const\s*$//) ? "const ":"";
118              
119 0         0 $proto =~ m/\s*(\S+)\s*\(/;
120 0         0 my $fname = lcfirst "${clname}_$1";
121 0 0       0 $fname .= "_".$suffix if $suffix;
122 0 0       0 if ($self->{private}) {
123 0         0 $self->{func_private}{$fname} = $self->{private};
124 0         0 $fname .= "_private";
125             }
126 0         0 $proto =~ s/\s+(\S+)\s*\(/ ${fname}(/;
127              
128 0         0 $proto =~ s/\(/(${const}${clname}* thisp,/;
129 0         0 $proto =~ s/,\s*\)/)/;
130 0         0 $self->print ("$proto ",@_);
131             }
132             }
133              
134             sub call_str {
135 0     0 1 0 my $self = shift;
136 0         0 my $clname = shift;
137 0         0 my $suffix = shift;
138 0         0 my $call = shift;
139             # Call a function with C++ semantics, mangle into C if necessary
140             # return as *string*
141 0 0       0 if ($self->{CPP}) {
142 0         0 return join('',"$call",@_);
143             } else {
144 0         0 $call =~ m/\s*(\S+)\s*\(/;
145 0         0 my $fname = lcfirst "${clname}_$1";
146 0 0       0 $fname .= "_".$suffix if $suffix;
147 0 0       0 $fname .= "_private" if $self->{func_private}{$fname};
148 0         0 $call =~ s/(\S+)\s*\(/${fname}(/;
149 0 0       0 $call =~ s/\(/(thisp,/ or croak "%Error: No args in func call '$call',";
150 0         0 $call =~ s/,\s*\)/)/;
151 0         0 return join('',$call,@_);
152             }
153             }
154              
155             ######################################################################
156             # Tabify all output
157              
158             sub print {
159 153     153 1 195 my $self = shift;
160             # Override default SystemC::Vregs::Language::print to tabify all output
161 153         394 $self->push_text($self->tabify(@_));
162             }
163              
164             sub print_at_close {
165 1     1 0 4 my $self = shift;
166             # Override default SystemC::Vregs::Language::print to tabify all output
167 1         6 $self->push_close_text($self->tabify(@_));
168             }
169              
170             sub printf_tabify {
171 0     0 0 0 my $self = shift;
172 0         0 my $line = sprintf(shift,@_);
173 0         0 $self->print($self->tabify($line));
174             }
175              
176             sub tabify {
177 154     154 0 168 my $self = shift;
178 154         255 my $line = join('',@_);
179             # Convert any space-tabs to just tabs
180 154         165 my $out='';
181 154         157 my $col=0;
182 154         166 my $spaces=0;
183 154         202 $line =~ s/\t /\t\t/g;
184 154         363 for (my $i=0; $i
185 2746         3681 my $c = substr($line,$i,1);
186 2746 100       6346 if ($c eq "\n") {
    100          
    100          
187 108         153 $out .= $c;
188 108         130 $col = 0;
189 108         253 $spaces = 0;
190             } elsif ($c eq "\t") {
191 76         152 my $wantcol = int(($col+$spaces+8)/8)*8;
192 76         147 while ($wantcol > $col) {
193 76         91 $col = int(($col+8)/8)*8;
194 76         171 $out .= "\t";
195             }
196 76         207 $spaces = 0;
197             } elsif ($c eq " ") {
198 463         941 $spaces++;
199             } else {
200 2099 100       3593 if ($spaces) { $out .= ' 'x$spaces; $col+=$spaces; $spaces=0; }
  201         315  
  201         203  
  201         218  
201 2099         2327 $out .= $c;
202 2099         4463 $col++;
203             }
204             }
205 154         595 return $out;
206             }
207              
208             ######################################################################
209             #### Package return
210             1;
211             __END__