OpenCores
URL https://opencores.org/ocsvn/scan_based_serial_communication/scan_based_serial_communication/trunk

Subversion Repositories scan_based_serial_communication

[/] [scan_based_serial_communication/] [trunk/] [deperlify.pl] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 Quanticles
 #!/usr/bin/perl
2
 
3
#################################################################################################
4
#
5
#  Copyright 2010 David Fick. All rights reserved.
6
#  
7
#  Redistribution and use in source and binary forms, with or without modification, are
8
#  permitted provided that the following conditions are met:
9
#  
10
#     1. Redistributions of source code must retain the above copyright notice, this list of
11
#        conditions and the following disclaimer.
12
#  
13
#     2. Redistributions in binary form must reproduce the above copyright notice, this list
14
#        of conditions and the following disclaimer in the documentation and/or other materials
15
#        provided with the distribution.
16
#  
17
#  THIS SOFTWARE IS PROVIDED BY DAVID FICK ``AS IS'' AND ANY EXPRESS OR IMPLIED
18
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19
#  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAVID FICK OR
20
#  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
#  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23
#  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
#  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
#  
27
#  The views and conclusions contained in the software and documentation are those of the
28
#  authors and should not be interpreted as representing official policies, either expressed
29
#  or implied, of David Fick.
30
#
31
#################################################################################################
32
 
33
 
34
use strict;
35
use integer;
36
 
37
my $warning_verilog = "\n\n\n
38
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
39
// !!    THIS IS A TEMPORARY FILE GENERATED BY DEPERILFY      !!
40
// !!             DO NOT MODIFY DIRECTLY!                     !!
41
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
42
\n";
43
 
44
my $warning_io = "\n\n\n
45
#  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
46
#  !!    THIS IS A TEMPORARY FILE GENERATED BY DEPERILFY      !!
47
#  !!             DO NOT MODIFY DIRECTLY!                     !!
48
#  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
49
\n";
50
 
51
sub max {
52
    my $a = shift;
53
    my $b = shift;
54
 
55
    return $a > $b ? $a : $b;
56
}
57
 
58
 
59
################################################################################################
60
# Grab Defines
61
 
62
my %defines;
63
 
64
sub grab_defines {
65
 
66
    my @params    = @_;
67
    my $file_name = $params[0];
68
 
69
    # Read the entire file
70
    my $file_contents;
71
 
72
    {
73
        local( $/, *FH ) ;
74
        open(FH, "< " . $file_name) or die "Failed to open \"". $file_name . "\" correctly.";
75
        $file_contents = <FH>;
76
        close(FH)
77
    }
78
 
79
    # Remove all comments
80
    $file_contents =~ s-//.*\n-\n-g;
81
    $file_contents =~ s-/\*.*\*/- -g;
82
 
83
    # Grab all of the defines
84
    while ($file_contents =~ /\`define\s+(\w+)[ \t]+([^\n]*)?\n/g) {
85
 
86
        my $macro      = $1;
87
        my $definition = $2;
88
 
89
        $defines{$macro} = $definition;
90
    }
91
 
92
    return;
93
}
94
 
95
 
96
################################################################################################
97
# Lookup Define
98
 
99
sub lookup {
100
 
101
    my $define     = shift;
102
    my $definition = $defines{$define};
103
 
104
    $definition = deep_replace($definition);
105
 
106
    return $definition ne "" ? $definition : "undef";
107
}
108
 
109
################################################################################################
110
# Deep Replace - replaces ` defines with their values
111
 
112
sub deep_replace {
113
 
114
    no integer;
115
 
116
    my $text   = shift;
117
 
118
    # Find and replace all defines
119
    $text =~ s-\`LG\(([^()]+?)\)-int(0.99999+log(eval(deep_replace($1)))/log(2))-ge;                # Special case for `LG macro
120
    $text =~ s-\`MAX\(([^()]+?),([^()]+?)\)-max(eval(deep_replace($1)),eval(deep_replace($2)))-ge;  # Special case for `MAX macro
121
 
122
    # Check for errors in the eval statement
123
    if ($@) {
124
        print "Error in perl section:\n" . $text . "\n ERRORS: \n" . $@;
125
        die;
126
    }
127
 
128
    # Do additional normal lookups
129
    $text =~ s/\`(\w+)/lookup($1)/ge;
130
 
131
    return $text;
132
}
133
 
134
 
135
################################################################################################
136
# Shallow Replace - replaces $` defines with their values
137
 
138
sub shallow_replace {
139
 
140
    my $text   = shift;
141
 
142
    # Find and replace all defines
143
    $text =~ s/\$\`(\w+)/lookup($1)/ge;
144
 
145
    return $text;
146
}
147
 
148
 
149
################################################################################################
150
# This function takes a string, executes it, and returns everything that was printed
151
 
152
sub execute_block {
153
 
154
    my $text           = shift;
155
    my $generated_text = "";
156
 
157
    # Inject the DEPERLIFY_INCLUDE files
158
    $text =~ s/DEPERLIFY_INCLUDE\(([^\)]+)\)/`cat $1`/gse;
159
 
160
    # Find and replace all defines
161
    $text = shallow_replace($text);
162
 
163
    # Execute the block of text that now has the generate statements
164
    # write perl code to a file
165
    my $temp_file = `mktemp deperlify.XXXXXXXXX`;
166
    chomp $temp_file;
167
 
168
    open (BLOCK_CODE, ">" . $temp_file);
169
    print BLOCK_CODE $text;
170
    # run perl on block
171
    $generated_text = `perl $temp_file`;
172
 
173 7 Quanticles
    # Check for errors in the eval statement
174
    if ($generated_text ne "") {
175
        `rm $temp_file`;
176
    } else {
177
        print "\n";
178
        print "################################################################################################\n";
179
        print "### Error in deperlify section. Check $temp_file for evaluated perl code ###\n";
180
        print "################################################################################################\n";
181
        print "\n";
182
        exit;
183 3 Quanticles
    }
184
 
185
    return $generated_text;
186
}
187
 
188
 
189
################################################################################################
190
# This function takes a file name and runs the program on that file
191
 
192
sub convert_file {
193
 
194
    my @params = @_;
195
 
196
    my $file_name        = $params[0];
197
    my $output_file_name = $file_name;
198
 
199
    $output_file_name =~ s/\.perl\./\./;
200
 
201
    # determine warning based on file type (determines type of comments used)
202
    my $warning;
203
    if ($file_name =~ /\.io/) {
204
        $warning = $warning_io;
205
    } else {
206
        $warning = $warning_verilog;
207
    }
208
 
209
    # Read the entire file
210
    my $file_contents;
211
 
212
    {
213
        local( $/, *FH ) ;
214
        open(FH, "< " . $file_name) or die "Failed to open \"". $file_name . "\" correctly";
215
        $file_contents = <FH>;
216
        close(FH)
217
    }
218
 
219
    # Do some operation
220
    $file_contents =~ s/[\t ]*PERL\s+begin\s+\/\*(.*?)\*\/\s+end\s*?\n/execute_block($1)/gse;
221
 
222
    $file_contents = $warning . $file_contents;
223
 
224
    # Write the entire file
225
    {
226
        local( *FH ) ;
227
        open(FH, "> " . $output_file_name) or die "Failed to write \"". $output_file_name . "\" correctly";
228
        print FH $file_contents;
229
        close(FH)
230
    }
231
 
232
}
233
 
234
 
235
################################################################################################
236
# Main code
237
 
238
foreach my $argnum (0 .. $#ARGV) {
239
 
240
    grab_defines($ARGV[$argnum]);
241
 
242
    if ($ARGV[$argnum] =~ /(\.perl\.v)|(\.perl\.io)/) {
243
        convert_file($ARGV[$argnum]);
244
    }
245
}
246
 
247
 
248
################################################################################################
249
 
250
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.