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