Debian Codegraph @

Parser/callbackgen

raw
1 #!/usr/bin/perl -w
2 # See copyright, etc in below POD section.
3 ######################################################################
4
5 require 5.006_001;
6 use Getopt::Long;
7 use IO::File;
8 use Pod::Usage;
9 use strict;
10 use vars qw($Debug $VERSION);
11
12 # We need keywords, but haven't completely built yet, so can't use the blib
13 # Thus we pull in Language.pm directly
14 require "../Language.pm";
15 package main;
16
17 $VERSION = '3.482';
18
19 # xs_manual=>1, -> The .xs file makes the handler itself
20
21 my %Cbs =
22 (attribute => {which=>'Parser', args => [text=>'string']},
23 comment => {which=>'Parser', args => [text=>'string']},
24 endparse => {which=>'Parser', args => [text=>'string']},
25 keyword => {which=>'Parser', args => [text=>'string']},
26 number => {which=>'Parser', args => [text=>'string']},
27 operator => {which=>'Parser', args => [text=>'string']},
28 preproc => {which=>'Parser', args => [text=>'string']},
29 string => {which=>'Parser', args => [text=>'string']},
30 symbol => {which=>'Parser', args => [text=>'string']},
31 sysfunc => {which=>'Parser', args => [text=>'string']},
32 #
33 class => {which=>'SigParser', args => [kwd=>'string', name=>'string', virt=>'string']},
34 contassign => {which=>'SigParser', args => [kwd=>'string', lhs=>'string', rhs=>'string']},
35 covergroup => {which=>'SigParser', args => [kwd=>'string', name=>'string']},
36 defparam => {which=>'SigParser', args => [kwd=>'string', lhs=>'string', rhs=>'string']},
37 endcell => {which=>'SigParser', args => [kwd=>'string']},
38 endclass => {which=>'SigParser', args => [kwd=>'string']},
39 endgroup => {which=>'SigParser', args => [kwd=>'string']},
40 endinterface=>{which=>'SigParser', args => [kwd=>'string']},
41 endmodport => {which=>'SigParser', args => [kwd=>'string']},
42 endmodule => {which=>'SigParser', args => [kwd=>'string']},
43 endpackage => {which=>'SigParser', args => [kwd=>'string']},
44 endprogram => {which=>'SigParser', args => [kwd=>'string']},
45 endtaskfunc=> {which=>'SigParser', args => [kwd=>'string']},
46 function => {which=>'SigParser', args => [kwd=>'string', name=>'string', data_type=>'string']},
47 import => {which=>'SigParser', args => [package=>'string', id=>'string']},
48 instant => {which=>'SigParser', args => [mod=>'string', cell=>'string', range=>'string']},
49 interface => {which=>'SigParser', args => [kwd=>'string', name=>'string']},
50 modport => {which=>'SigParser', args => [kwd=>'string', name=>'string']},
51 module => {which=>'SigParser', args => [kwd=>'string', name=>'string', ignore3=>'undef', celldefine=>'bool'],},
52 package => {which=>'SigParser', args => [kwd=>'string', name=>'string']},
53 parampin => {which=>'SigParser', args => [name=>'string', conn=>'string', index=>'int']},
54 pin => {which=>'SigParser', args => [name=>'string', conn=>'string', index=>'int']},
55 pinselects => {which=>'SigParser', args => [name=>'string', conns=>'hash', index=>'int']},
56 port => {which=>'SigParser', args => [name=>'string', objof=>'string', direction=>'string',
57 data_type=>'string', array=>'string', index=>'int']},
58 program => {which=>'SigParser', args => [kwd=>'string', name=>'string'],},
59 var => {which=>'SigParser', args => [kwd=>'string', name=>'string', objof=>'string', net=>'string',
60 data_type=>'string', array=>'string', value=>'string'],},
61 task => {which=>'SigParser', args => [kwd=>'string', name=>'string']},
62 );
63
64 #======================================================================
65 # main
66
67 our $Opt_Debug;
68
69 autoflush STDOUT 1;
70 autoflush STDERR 1;
71 Getopt::Long::config("no_auto_abbrev");
72 if (! GetOptions (
73 # Local options
74 "help" => \&usage,
75 "version" => sub { print "Version $VERSION\n"; exit(0); },
76 "<>" => sub { die "%Error: Unknown parameter: $_[0]\n"; },
77 )) {
78 die "%Error: Bad usage, try 'callbackgen --help'\n";
79 }
80
81 process();
82
83 #----------------------------------------------------------------------
84
85 sub usage {
86 print "Version $VERSION\n";
87 pod2usage(-verbose=>2, -exitval=>2, -output=>\*STDOUT, -noperldoc=>1);
88 exit(1);
89 }
90
91 #######################################################################
92
93 sub process {
94 filter("Parser.xs",0);
95 filter("VParse.h",0);
96 filter("Parser_callbackgen.cpp",1);
97 }
98
99 sub filter {
100 my $filename = shift;
101 my $make_xs = shift;
102
103 my $fh = IO::File->new("<$filename");
104 my @lines;
105 if (!$fh) {
106 if ($make_xs) {
107 @lines = ("// CALLBACKGEN_XS\n");
108 } else {
109 die "%Error: $! $filename\n";
110 }
111 } else {
112 @lines = $fh->getlines;
113 $fh->close;
114 }
115 my @orig = @lines;
116
117 my $strip;
118 my @out;
119 foreach my $line (@lines) {
120 if ($line =~ /CALLBACKGEN_GENERATED_BEGIN/) {
121 $strip = 1;
122 } else {
123 if (!$strip) {
124 push @out, $line;
125 }
126
127 if ($line =~ /CALLBACKGEN_GENERATED_END/) {
128 $strip = 0;
129 }
130 elsif ($line =~ /CALLBACKGEN_H_MEMBERS/) {
131 push @out, " // CALLBACKGEN_GENERATED_BEGIN - GENERATED AUTOMATICALLY by callbackgen\n";
132 push @out, _h_use_cb();
133 push @out, " // CALLBACKGEN_GENERATED_END - GENERATED AUTOMATICALLY by callbackgen\n";
134 }
135 elsif ($line =~ /CALLBACKGEN_CB_USE/) {
136 push @out, " // CALLBACKGEN_GENERATED_BEGIN - GENERATED AUTOMATICALLY by callbackgen\n";
137 push @out, _c_use_cb();
138 push @out, " // CALLBACKGEN_GENERATED_END - GENERATED AUTOMATICALLY by callbackgen\n";
139 }
140 elsif ($line =~ /CALLBACKGEN_H_VIRTUAL(_0)?/) {
141 my $zero = (($1||"") eq "_0") ? " = 0":"";
142 push @out, " // CALLBACKGEN_GENERATED_BEGIN - GENERATED AUTOMATICALLY by callbackgen\n";
143 my $last_which = "";
144 foreach my $cb (sort {$Cbs{$a}{which} cmp $Cbs{$b}{which} || $a cmp $b} keys %Cbs) {
145 my $which = $Cbs{$cb}{which};
146 if ($last_which ne $which) {
147 push @out, " // Verilog::$which Callback methods\n";
148 $last_which = $which;
149 }
150 push @out, " virtual void "._func($cb)."("._arglist($cb).")".$zero.";\n";
151 }
152 push @out, " // CALLBACKGEN_GENERATED_END - GENERATED AUTOMATICALLY by callbackgen\n";
153 }
154 elsif ($line =~ /CALLBACKGEN_XS/) {
155 push @out, "// CALLBACKGEN_GENERATED_BEGIN - GENERATED AUTOMATICALLY by callbackgen\n";
156 foreach my $cb (sort {$Cbs{$a}{which} cmp $Cbs{$b}{which} || $a cmp $b} keys %Cbs) {
157 next if $Cbs{$cb}{xs_manual};
158 push @out, _xs($cb);
159 }
160 push @out, _xs_use_cb();
161 push @out, "// CALLBACKGEN_GENERATED_END - GENERATED AUTOMATICALLY by callbackgen\n";
162 }
163 elsif ($line =~ /CALLBACKGEN_KEYWORDS/) {
164 push @out, " // CALLBACKGEN_GENERATED_BEGIN - GENERATED AUTOMATICALLY by callbackgen\n";
165 push @out, _h_keywords();
166 push @out, " // CALLBACKGEN_GENERATED_END - GENERATED AUTOMATICALLY by callbackgen\n";
167 }
168 elsif ($line =~ /CALLBACKGEN/) {
169 die "%Error: callbackgen: Unknown pragma: $line";
170 }
171 }
172 }
173 @lines = @out;
174
175 if (join('',@lines) ne join('',@orig)
176 || $make_xs) { # Generated file, so touch to apppease make
177 print "callbackgen edited $filename\n";
178 $fh = IO::File->new(">$filename") or die "%Error: $! writing $filename\n";
179 $fh->write(join('',@lines));
180 $fh->close;
181 }
182 }
183
184 sub _func {
185 my $cb = shift;
186 return $cb."Cb";
187 }
188
189 sub _arglist {
190 my $cb = shift;
191 my $args = "VFileLine* fl";
192 my $n=0;
193 for (my $i=0; $i<=$#{$Cbs{$cb}{args}}; $i+=2) {
194 my ($arg,$type) = ($Cbs{$cb}{args}[$i],$Cbs{$cb}{args}[$i+1]);
195 $args .= "\n\t" if (($n++%5)==4);
196 if ($type eq 'string') {
197 $args .= ", const string\& $arg";
198 } elsif ($type eq 'bool' || $type eq 'int') {
199 $args .= ", $type $arg";
200 } elsif ($type eq 'hash') {
201 $args .= ", unsigned int arraycnt${n}, unsigned int elemcnt${n}, const VParseHashElem* $arg${n}";
202 } elsif ($type eq 'undef') {
203 $args .= ", bool";
204 } else {
205 die "%Error: callbackgen: Unknown type: $arg=>$type\n";
206 }
207 }
208 return $args;
209 }
210
211 sub _xs {
212 my $cb = shift;
213 my @out;
214 push @out, "// GENERATED AUTOMATICALLY by callbackgen\n";
215 push @out, "void VParserXs::"._func($cb)."("._arglist($cb).") {\n";
216 my $enable = "callbackMasterEna()";
217 $enable .= " && m_useCb_${cb}";
218 $enable .= " && $Cbs{$cb}{enable}" if $Cbs{$cb}{enable};
219 push @out, " if ($enable) {\n";
220 push @out, " cbFileline(fl);\n";
221 my $callargs="";
222 my $n=1;
223 for (my $i=0; $i<=$#{$Cbs{$cb}{args}}; $i+=2) {
224 my ($arg,$type) = ($Cbs{$cb}{args}[$i],$Cbs{$cb}{args}[$i+1]);
225 if ($type eq 'string') {
226 push @out, " static string hold${n}; hold${n} = $arg;\n";
227 $callargs .= ", hold${n}.c_str()";
228 } elsif ($type eq 'bool') {
229 push @out, " static string hold${n}; hold${n} = $arg ? \"1\":\"0\";\n";
230 $callargs .= ", hold${n}.c_str()";
231 } elsif ($type eq 'int') {
232 push @out, " static string hold${n}; static char num".$n."[30]; sprintf(num${n},\"%d\",$arg); hold${n}=num${n};\n";
233 $callargs .= ", hold${n}.c_str()";
234 } elsif ($type eq 'hash') {
235 $callargs .= ", hasharray_param, arraycnt${n}, elemcnt${n}, ${arg}${n}";
236 } elsif ($type eq 'undef') {
237 $callargs .= ", NULL";
238 } else {
239 die "%Error: callbackgen: Unknown type: $arg=>$type\n";
240 }
241
242 $n++;
243 }
244 my $narg = $n-1;
245 push @out, " call(NULL, $narg, \"$cb\"$callargs);\n";
246 push @out, " }\n";
247 push @out, "}\n";
248 return @out;
249 }
250
251 #######################################################################
252
253 sub _h_use_cb {
254 my @out;
255 push @out, " struct { // Bit packed to help the cache\n";
256 foreach my $cb (sort {$a cmp $b} keys %Cbs) {
257 push @out, " bool m_useCb_${cb}:1;\n";
258 }
259 push @out, " };\n";
260 return @out;
261 }
262
263 sub _c_use_cb {
264 my @out;
265 push @out, " void set_cb_use() {\n";
266 foreach my $cb (sort {$a cmp $b} keys %Cbs) {
267 push @out, " m_useCb_${cb} = true;\n";
268 }
269 push @out, " }\n";
270 return @out;
271 }
272
273 sub _xs_use_cb {
274 my @out;
275 push @out, "// GENERATED AUTOMATICALLY by callbackgen\n";
276 # Trailing Ena so it doesn't look like it is a callback itself
277 push @out, "void VParserXs::useCbEna(const char* name, bool flag) {\n";
278 push @out, " if (0) ;\n";
279 foreach my $cb (sort {$a cmp $b} keys %Cbs) {
280 push @out, " else if (0==strcmp(name,\"${cb}\")) m_useCb_${cb} = flag;\n";
281 }
282 push @out, "}\n";
283 return @out;
284 }
285
286 sub _h_keywords {
287 my @out;
288
289 (keys %Verilog::Language::Keyword) or die "%Error: Keyword loading failed,";
290
291 push @out, " static bool isKeyword(const char* kwd, int leng) {\n";
292 # If this gets slow, we can use a perfect hashing function and a table to compare
293 push @out, "\tstatic set<string> s_map;\n";
294 push @out, "\tif (s_map.empty()) {\n";
295 my $i=0;
296 push @out, "\t const char* kwds[] = {";
297 foreach my $kwd (sort keys %Verilog::Language::Keyword) {
298 next if $kwd !~ /^[a-zA-Z_]/;
299 push @out, "\n\t\t" if ($i++%7)==0;
300 push @out, "\"$kwd\",";
301 }
302 push @out, "\"\"};\n";
303 push @out, "\t for (const char** k=kwds; **k; k++) s_map.insert(*k);\n";
304 push @out, "\t}\n";
305 push @out, "\tstring str(kwd,leng);\n";
306 push @out, "\treturn s_map.end() != s_map.find(str);\n";
307 push @out, " }\n";
308 return @out;
309 }
310
311 #######################################################################
312 __END__
313
314 =pod
315
316 =head1 NAME
317
318 callbackgen - Create callback functions for Verilog-Perl internals
319
320 =head1 SYNOPSIS
321
322 make
323
324 This will invoke callbackgen
325
326 =head1 DESCRIPTION
327
328 Callbackgen is an internal utility used in building Verilog::Parser.
329
330 =head1 EXTENSIONS
331
332 =over 4
333
334 =item //CALLBACKGEN_H_VIRTUAL
335
336 Creates "virtual callbackCb(...);"
337
338 =item //CALLBACKGEN_H_VIRTUAL_0
339
340 Creates "virtual callbackCb(...) = 0;"
341
342 =item //CALLBACKGEN_XS
343
344 Creates XS code for accepting the callback.
345
346 =back
347
348 =head1 ARGUMENTS
349
350 =over 4
351
352 =item --help
353
354 Displays this message and program version and exits.
355
356 =item --debug
357
358 Enable debug.
359
360 =item --version
361
362 Print the version number and exit.
363
364 =back
365
366 =head1 DISTRIBUTION
367
368 This is part of the L<https://www.veripool.org/> free Verilog EDA software
369 tool suite. The latest version is available from CPAN and from
370 L<https://www.veripool.org/>.
371
372 Copyright 2008-2024 by Wilson Snyder. This package is free software; you
373 can redistribute it and/or modify it under the terms of either the GNU
374 Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.
375
376 This program is distributed in the hope that it will be useful, but WITHOUT
377 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
378 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
379 more details.
380
381 =head1 AUTHORS
382
383 Wilson Snyder <[email protected]>
384
385 =head1 SEE ALSO
386
387 =cut
388
389 ######################################################################
390 ### Local Variables:
391 ### compile-command: "./callbackgen "
392 ### End: