lucy.pl
6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File: lucy.pl
# This perl script is to calcualte memory requirement and generate the header file.
# Author: Pibben Tung
use integer; # To make division as C code
my $via_file;
my $dep_file;
my $c_command;
if (scalar(@ARGV) != 2)
{
my @usage = ();
push @usage, "\nUsage lucy.pl <via_file> <dep_file>\n";
die join('', @usage);
}
else
{
($via_file, $dep_file) = @ARGV;
}
# Create lucy.c subject to preprocessing and linking stage
@interface_header = ("./interface/hal/video/video_memory_usage_v2.h", "./interface/hal/video/media_provider_mem_usage.h", "./interface/hal/video/media_session_mem_usage.h");
$declaration = "unsigned int VarVe";
open(VIR_FILE, ">./hal/video/common/src/lucy.c") || die "file open fail!\n";
print VIR_FILE ("// This file is auto generated for memory requirement calculation !!!\n\n");
foreach $headerfile (@interface_header)
{
my @purefilename;
@purefilename = split(/\//, $headerfile);
print VIR_FILE ("#include \"", $purefilename[-1], "\"\n"); # -1 indicates the last element of array
}
print VIR_FILE ("\nvoid main(void) {\n");
foreach $headerfile (@interface_header)
{
my $matching = 0;
my $line;
my $ifdef = "#ifdef";
my $sdefined = "defined";
my $endif = "#endif";
my $define = "#define";
my $declare;
open(INTERFACE_HEADER_FILE, $headerfile) || die "file open fail!\n";
while( $line = <INTERFACE_HEADER_FILE> )
{
if(($line =~ /^\s*\#if\s+$sdefined/) || ($line =~ /^\s*$ifdef/) || ($line =~ /^\s*\#if\s+\(*$sdefined/))
{
print VIR_FILE ($line);
$matching++;
}
if($line =~ /^\s*$define\s+\S+\s+\S+/)
{
my $assign = " = ";
$declare = $line;
$declare =~ s/$define\s+/$declaration/;
$declare =~ /$declaration\S*\s+/;
# $& is the string matched
$declare =~ s/$&/$&$assign/;
chomp($declare); # chomp is to remove next line character
print VIR_FILE ($declare, ";\n");
}
if($line =~ /^\s*$endif/)
{
if($matching > 0)
{
print VIR_FILE ($line);
$matching--;
}
}
}
close(INTERFACE_HEADER_FILE);
}
print VIR_FILE ("}\n");
close(VIR_FILE);
# lucy.c is generated.
# Preprocess lucy.c
$c_command = "armcc --via ".$via_file." -E .\\hal\\video\\common\\src\\lucy.c -o .\\hal\\video\\common\\src\\bunny.c";
system($c_command) && die "Preprocess lucy.c fail !!!\n";
$c_command = "armcc --via ".$via_file." -M .\\hal\\video\\common\\src\\lucy.c > ".$dep_file;
system($c_command) && die "Make dependency of lucy.c fail !!!\n";
# bunny.c is preprocessed lucy.c
open(bunny, "./hal/video/common/src/bunny.c") || die "file bunny.c open fail!\n";
open(VIR_FILE, ">./hal/video/common/src/lucy2.c") || die "file open fail!\n"; # lucy2.c is to put sizeof information
print VIR_FILE ("// This file is auto generated for memory requirement calculation !!!\n\n");
foreach $headerfile (@interface_header)
{
my @purefilename;
@purefilename = split(/\//, $headerfile);
print VIR_FILE ("#include \"", $purefilename[-1], "\"\n"); # -1 indicates the last element of array
}
$filecontent = "\nvoid main(void) {\n";
#print VIR_FILE ("\nvoid main(void) {\n");
%StructToVar;
my $counter = 0;
while( $line = <bunny> )
{
if($line =~ /\s*$declaration/)
{
$filecontent .= $line;
#print VIR_FILE ($line);
# find every sizeof(structure)
while($line =~ /(sizeof\([a-zA-Z0-9_]+\))/g)
{
$StructToVar{$&} = "var".$counter;
$counter++;
}
}
}
print VIR_FILE ("\n// Variables to calculate size of structures\n");
foreach $key (keys(%StructToVar))
{
$key =~ /sizeof\(([a-zA-Z0-9_]+)\)/;
print VIR_FILE ($1, " ", $StructToVar{$key}, ";\n");
}
print VIR_FILE ($filecontent);
print VIR_FILE ("\n// To make variables appear in symbol file\n");
foreach $key (keys(%StructToVar))
{
$key =~ /sizeof\(([a-zA-Z0-9_]+)\)/;
print VIR_FILE ("memset(&", $StructToVar{$key}, ", 1, ", $key, ");\n");
}
print VIR_FILE ("}\n");
close(VIR_FILE);
close(bunny);
# lucy2.c is generated. (including sizeof variables)
$c_command = "armcc --via ".$via_file." .\\hal\\video\\common\\src\\lucy2.c";
system($c_command) && die "Compile lucy2.c fail !!!\n";
system("armlink --symbols .\\lucy2.o > .\\lucy2.sym") && die "Link lucy2.o fail !!!\n";;
open(symbol, "./lucy2.sym") || die "file lucy.sym open fail!\n";
%VarToSize;
while( $line = <symbol> )
{
my $data = "Data";
my $lucydoto = "lucy2.o";
if($line =~ /^\s*([a-zA-Z0-9_]+).*$data\s+(\d+)\s+$lucydoto/)
{
#print ($1, "\t", $2, "\n");
$VarToSize{$1} = $2;
}
}
close(symbol);
open(header, "./hal/video/common/src/lucy2.c") || die "file open fail!\n";
open(AUTO_GEN_HEADER, ">./interface/hal/video/video_mem_gen.h") || die "file open fail!\n";
my $header_file_begin_text = "";
$header_file_begin_text .= "/* This video memory requirements header is Auto Generated.\nAccording to:\n";
foreach $headerfile (@interface_header)
{
my @purefilename;
$header_file_begin_text .= $headerfile."\n";
}
$header_file_begin_text .= "*/\n";
print AUTO_GEN_HEADER ($header_file_begin_text);
while( $line = <header> )
{
if($line =~ /\s*$declaration/)
{
my $expression;
my $null = "";
my $structsize;
($define, $expansion) = split(/=/, $line);
$define =~ s/$declaration/$null/; # s/old-regex/new-string/
# replace sizeof(.) to number in expansion
$expression = $expansion;
while($expression =~ /sizeof\([a-zA-Z0-9_]+\)/g)
{
$structsize = $VarToSize{$StructToVar{$&}};
$expression =~ s/sizeof\([a-zA-Z0-9_]+\)/$structsize/; # why $& is not working?
}
#print("\n", $expression);
$value = eval($expression);
print AUTO_GEN_HEADER ("#define ", $define, " ", $value, " //", $expression);
}
}
close(AUTO_GEN_HEADER);
close(header);