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 4

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 4 Quanticles
 
171 3 Quanticles
    # run perl on block
172
    $generated_text = `perl $temp_file`;
173
 
174 4 Quanticles
    # Stop if there's an error
175
    if ($? != 0) {
176 3 Quanticles
        die;
177
    }
178
 
179 4 Quanticles
    `rm $temp_file`;
180
 
181 3 Quanticles
    return $generated_text;
182
}
183
 
184
 
185
################################################################################################
186
# This function takes a file name and runs the program on that file
187
 
188
sub convert_file {
189
 
190
    my @params = @_;
191
 
192
    my $file_name        = $params[0];
193
    my $output_file_name = $file_name;
194
 
195
    $output_file_name =~ s/\.perl\./\./;
196
 
197
    # determine warning based on file type (determines type of comments used)
198
    my $warning;
199
    if ($file_name =~ /\.io/) {
200
        $warning = $warning_io;
201
    } else {
202
        $warning = $warning_verilog;
203
    }
204
 
205
    # Read the entire file
206
    my $file_contents;
207
 
208
    {
209
        local( $/, *FH ) ;
210
        open(FH, "< " . $file_name) or die "Failed to open \"". $file_name . "\" correctly";
211
        $file_contents = <FH>;
212
        close(FH)
213
    }
214
 
215
    # Do some operation
216
    $file_contents =~ s/[\t ]*PERL\s+begin\s+\/\*(.*?)\*\/\s+end\s*?\n/execute_block($1)/gse;
217
 
218
    $file_contents = $warning . $file_contents;
219
 
220
    # Write the entire file
221
    {
222
        local( *FH ) ;
223
        open(FH, "> " . $output_file_name) or die "Failed to write \"". $output_file_name . "\" correctly";
224
        print FH $file_contents;
225
        close(FH)
226
    }
227
 
228
}
229
 
230
 
231
################################################################################################
232
# Main code
233
 
234
foreach my $argnum (0 .. $#ARGV) {
235
 
236
    grab_defines($ARGV[$argnum]);
237
 
238
    if ($ARGV[$argnum] =~ /(\.perl\.v)|(\.perl\.io)/) {
239
        convert_file($ARGV[$argnum]);
240
    }
241
}
242
 
243
 
244
################################################################################################
245
 
246
 

powered by: WebSVN 2.1.0

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