yu.dong | c33b307 | 2024-08-21 23:14:49 -0700 | [diff] [blame^] | 1 | #!/usr/bin/perl -w
|
| 2 |
|
| 3 | #-------------------------------------------------------------------
|
| 4 | #Tool version
|
| 5 |
|
| 6 | $tool_version = '20170719';
|
| 7 |
|
| 8 | #-------------------------------------------------------------------
|
| 9 | #Parameters Configuration
|
| 10 |
|
| 11 | $imm_file = $ARGV[0];
|
| 12 | $list_file = $ARGV[1];
|
| 13 | $symbol_file = $ARGV[2];
|
| 14 | $log_file = $ARGV[3];
|
| 15 |
|
| 16 | #-------------------------------------------------------------------
|
| 17 | # Replace '\' with '/'
|
| 18 |
|
| 19 | $imm_file =~ s/\\/\//g;
|
| 20 | $list_file =~ s/\\/\//g;
|
| 21 | $symbol_file =~ s/\\/\//g;
|
| 22 | $log_file =~ s/\\/\//g;
|
| 23 |
|
| 24 | #-------------------------------------------------------------------
|
| 25 |
|
| 26 | $debug_mode = 0; # 0 or 1
|
| 27 | $error_prefix = 'AutoAMMS Error:';
|
| 28 | $nolist_keyword = 'NO_LIST';
|
| 29 | $attribute_line_number = 0;
|
| 30 |
|
| 31 | #-------------------------------------------------------------------
|
| 32 | # Open log file
|
| 33 | open (FILE_LOG, ">>$log_file" ) or die (print "$error_prefix Can't open Auto-AMMS-Attribute log file $log_file!\n");
|
| 34 | print FILE_LOG "Auto AMMS Attribute Tool $tool_version\n";
|
| 35 |
|
| 36 | #-------------------------------------------------------------------
|
| 37 | #Construct C file name
|
| 38 | $c_file = $imm_file;
|
| 39 | $c_file =~ s/.*\///; # remove path part
|
| 40 | print FILE_LOG "Source C File : $c_file\n";
|
| 41 | print FILE_LOG "Immediated File : $imm_file\n";
|
| 42 |
|
| 43 | #-------------------------------------------------------------------
|
| 44 | # Intialize some list as empty
|
| 45 | %special_symbol_list = ();
|
| 46 | %added_code = ();
|
| 47 | %attribute_list = ();
|
| 48 |
|
| 49 | $attribute = '';
|
| 50 | $find_file = 0;
|
| 51 | $special_symbol_list_num = 0;
|
| 52 |
|
| 53 | Extract_DRDI_attribute($list_file,$c_file);
|
| 54 | if($find_file == 1)
|
| 55 | {
|
| 56 | $special_symbol_list_num = Extrace_DRDI_symbol($symbol_file,$c_file);
|
| 57 | print FILE_LOG "special symbol list num : $special_symbol_list_num\n";
|
| 58 | &Find_DRDI_List($imm_file, $attribute);
|
| 59 | }
|
| 60 |
|
| 61 | #Add __attribute__ code in source code
|
| 62 | if( scalar(keys %added_code) > 0 )
|
| 63 | {
|
| 64 | my $backup = &Add_Attribute_Code( $debug_mode, $imm_file );
|
| 65 | print FILE_LOG "Backup Imm File : $backup\n";
|
| 66 |
|
| 67 | }else
|
| 68 | {
|
| 69 | print FILE_LOG "\t$c_file is not in AMMS symbol lists.\n";
|
| 70 | }
|
| 71 |
|
| 72 | #-------------------------------------------------------------------
|
| 73 | #Close log file handle
|
| 74 |
|
| 75 | print FILE_LOG "Done!\n";
|
| 76 | print FILE_LOG "-----------------------------------------------------------\n";
|
| 77 | close FILE_LOG;
|
| 78 |
|
| 79 | #-------------------------------------------------------------------
|
| 80 |
|
| 81 | #Parsing Done.
|
| 82 |
|
| 83 | exit 0; #return 0 if it is successful
|
| 84 |
|
| 85 | #-------------------------------------------------------------------
|
| 86 |
|
| 87 | sub Extract_DRDI_attribute
|
| 88 | {
|
| 89 | my $list = $_[0];
|
| 90 |
|
| 91 | return 0 if( $list eq $nolist_keyword );
|
| 92 |
|
| 93 | my $search_file_1 = $_[1];
|
| 94 | my $search_file_2 = $search_file_1;
|
| 95 | my $section = '';
|
| 96 | $search_file_2 =~ s/\.c$/\.obj/;
|
| 97 |
|
| 98 | # Open TCM fucntions list file
|
| 99 | open (FILE_ITCM, "<$list" ) or &print_error_info_die("$error_prefix Can't open AMMS 3GFDD list file $list!\n");
|
| 100 |
|
| 101 | while (<FILE_ITCM>)
|
| 102 | {
|
| 103 | if( /^__attribute__/)
|
| 104 | {
|
| 105 | $section = $_;
|
| 106 | }elsif( /^\s*($search_file_1|$search_file_2)\b/ )
|
| 107 | {
|
| 108 | $find_file = 1;
|
| 109 | print FILE_LOG "find file: $search_file_2\n";
|
| 110 | last;
|
| 111 | }
|
| 112 | }
|
| 113 | $section =~ s/\r\n//g;
|
| 114 | $attribute = $section;
|
| 115 | close FILE_ITCM;
|
| 116 | }
|
| 117 |
|
| 118 | sub Extrace_DRDI_symbol
|
| 119 | {
|
| 120 | my $list = $_[0];
|
| 121 |
|
| 122 | return 0 if( $list eq $nolist_keyword );
|
| 123 |
|
| 124 | my $search_file_1 = $_[1];
|
| 125 | my $search_file_2 = $search_file_1;
|
| 126 | my $section = '';
|
| 127 | $search_file_2 =~ s/\.c$/\.obj/;
|
| 128 |
|
| 129 | # Open TCM fucntions list file
|
| 130 | open (FILE_ITCM, "<$list" ) or &print_error_info_die("$error_prefix Can't open AMMS 3GFDD list file $list!\n");
|
| 131 |
|
| 132 | while (<FILE_ITCM>)
|
| 133 | {
|
| 134 | if( /^\s*(\S+)\s*,?\s*($search_file_1|$search_file_2)\b/ )
|
| 135 | {
|
| 136 | my $search_symbol = $1;
|
| 137 | my $symbol_name = $search_symbol;
|
| 138 | $symbol_name =~ s/_SetX/_Set/;
|
| 139 | $special_symbol_list{$symbol_name} = 0;
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | close FILE_ITCM;
|
| 144 |
|
| 145 | return scalar(keys %special_symbol_list);
|
| 146 | }
|
| 147 |
|
| 148 | #-------------------------------------------------------------------
|
| 149 |
|
| 150 | #-------------------------------------------------------------------
|
| 151 |
|
| 152 | sub Find_DRDI_List
|
| 153 | {
|
| 154 | my $imm = $_[0]; #Input is immediated
|
| 155 | my $attribute_section = $_[1]; #Input attribute
|
| 156 |
|
| 157 | # Open imm file, which is output of pre-processor
|
| 158 | open (FILE_IMM, "<$imm" ) or &print_error_info_die("$error_prefix Can't open immediated file $imm!\n");
|
| 159 |
|
| 160 | my $line_in = '';
|
| 161 | my $line_number = 0;
|
| 162 | my $brace_l_sum = 0; # left brace { conter
|
| 163 | my $brace_r_sum = 0; # right brace } conter
|
| 164 | my $setx = 'SetX';
|
| 165 |
|
| 166 | foreach my $i (0..63)
|
| 167 | {
|
| 168 | my $attribute = $attribute_section;
|
| 169 | my $setnum = $setx;
|
| 170 | $attribute =~ s/SETX/SET$i/;
|
| 171 | $setnum =~ s/X/$i/;
|
| 172 | $attribute_list{$setnum} = $attribute;
|
| 173 | }
|
| 174 |
|
| 175 | while( <FILE_IMM> )
|
| 176 | {
|
| 177 | $line_in = $_;
|
| 178 | $line_number++;
|
| 179 |
|
| 180 | next if($line_in =~ /^\#line/); # speed up parsing
|
| 181 | next unless ( $line_in =~ /\S/ ); # speed up parsing
|
| 182 |
|
| 183 | # Update {} counters
|
| 184 | my $brace_l_cnt = $line_in =~ tr/{//; #count { number
|
| 185 | my $brace_r_cnt = $line_in =~ tr/}//; #count } number
|
| 186 | my $brace_tick_ori = $brace_l_sum - $brace_r_sum;
|
| 187 | my $brace_tick_new = $brace_tick_ori + ($brace_l_cnt - $brace_r_cnt);
|
| 188 | my $run_special_symbol_list = 1;
|
| 189 | $brace_l_sum += $brace_l_cnt;
|
| 190 | $brace_r_sum += $brace_r_cnt;
|
| 191 |
|
| 192 | if( $brace_tick_ori == 0 && !($line_in =~ /^\s*{/) ) # Only search global variable
|
| 193 | {
|
| 194 | unless( ( $line_in =~ /\bextern\b/ ) || !($line_in =~ /\bconst\b/ ) ) # || ($line_in =~ /\*/)
|
| 195 | {
|
| 196 | if(($line_in =~ /\*+\s*\b(\S+)_Set(\d+)\b/i) || ($line_in =~ /\*+\s*\b(\S+)_Type(\d+)\b/i))
|
| 197 | {
|
| 198 | if(($line_in =~ /\*+\s*\bconst\b\s*\b(\S+)_Set(\d+)\b.*[=;,]/i) || ($line_in =~ /\*+\s*\bconst\b\s*\b(\S+)_TYPE(\d+)\b.*[=;,]/))
|
| 199 | {
|
| 200 | my $num = $2;
|
| 201 | my $setnum = $setx;
|
| 202 | $setnum =~ s/X/$num/;
|
| 203 | $added_code{ $line_number } = $attribute_list{$setnum};
|
| 204 | $run_special_symbol_list = 0;
|
| 205 | }
|
| 206 | }
|
| 207 | elsif(( $line_in =~ /(^|,)[^=]*\b(\S+)_Set(\d+)\b.*[=;,]/i ) || ( $line_in =~ /(^|,)[^=]*\b(\S+)_TYPE(\d+)\b.*[=;,]/ ))
|
| 208 | {
|
| 209 | my $num = $3;
|
| 210 | my $setnum = $setx;
|
| 211 | $setnum =~ s/X/$num/;
|
| 212 | $added_code{ $line_number } = $attribute_list{$setnum};
|
| 213 | $run_special_symbol_list = 0;
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | if( ($special_symbol_list_num > 0) && ($run_special_symbol_list == 1) )
|
| 218 | {
|
| 219 | foreach( keys %special_symbol_list )
|
| 220 | {
|
| 221 | my $target = $_;
|
| 222 |
|
| 223 | if($target =~ /\s*(\S+)\*(\S+)\*(\S+)/)
|
| 224 | {
|
| 225 | my $pre = $1;
|
| 226 | my $middle = $2;
|
| 227 | my $post = $3;
|
| 228 | if( $line_in =~ /(^|,)[^=]*\b$pre\w+$middle\w+$post(\d+)\b.*[=;,]/ )
|
| 229 | {
|
| 230 | my $num = $2;
|
| 231 | my $setnum = $setx;
|
| 232 | $setnum =~ s/X/$num/;
|
| 233 | $special_symbol_list{ $target }++; # hit and increase hit counter
|
| 234 | $added_code{ $line_number } = $attribute_list{$setnum};
|
| 235 | }
|
| 236 | }
|
| 237 | elsif($target =~ /\s*(\S+)\*(\S+)/)
|
| 238 | {
|
| 239 | my $pre = $1;
|
| 240 | my $post = $2;
|
| 241 | if( $line_in =~ /(^|,)[^=]*\b$pre\w+$post(\d+)\b.*[=;,]/ )
|
| 242 | {
|
| 243 | my $num = $2;
|
| 244 | my $setnum = $setx;
|
| 245 | $setnum =~ s/X/$num/;
|
| 246 | $special_symbol_list{ $target }++; # hit and increase hit counter
|
| 247 | $added_code{ $line_number } = $attribute_list{$setnum};
|
| 248 | }
|
| 249 | }
|
| 250 | else
|
| 251 | {
|
| 252 | if( $line_in =~ /(^|,)[^=]*\b$target(\d+)\b.*[=;,]/ ) # variable name is matched but assignment is excluded
|
| 253 | {
|
| 254 | my $num = $2;
|
| 255 | my $setnum = $setx;
|
| 256 | $setnum =~ s/X/$num/;
|
| 257 | $special_symbol_list{ $target }++; # hit and increase hit counter
|
| 258 | $added_code{ $line_number } = $attribute_list{$setnum};
|
| 259 | }
|
| 260 | }#End of if()
|
| 261 | }
|
| 262 | }
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | close FILE_IMM;
|
| 267 |
|
| 268 | }
|
| 269 |
|
| 270 | #-------------------------------------------------------------------
|
| 271 |
|
| 272 | sub Add_Attribute_Code
|
| 273 | {
|
| 274 | my $debug = $_[0];
|
| 275 | my $file_ori = $_[1];
|
| 276 |
|
| 277 | my $file_new = $file_ori;
|
| 278 | my $file_bak = $file_ori;
|
| 279 | $file_bak =~ s/\.c$/\.i/;
|
| 280 |
|
| 281 | rename $file_ori, $file_bak or &print_error_info_die("$error_prefix Can't rename $file_ori to $file_bak\n");
|
| 282 |
|
| 283 | open (FILE_IN, "<$file_bak" ) or &print_error_info_die("$error_prefix Can't open immediated file $file_bak!\n");
|
| 284 | open (FILE_OUT, ">$file_new" ) or &print_error_info_die("$error_prefix Can't open output file $file_new!\n");
|
| 285 |
|
| 286 | my $line_number = 0;
|
| 287 | my $line_in = '';
|
| 288 | my $log_on = 0;
|
| 289 |
|
| 290 | while( <FILE_IN> )
|
| 291 | {
|
| 292 | $line_in = $_;
|
| 293 | $line_number++;
|
| 294 |
|
| 295 | if( exists $added_code{$line_number} )
|
| 296 | {
|
| 297 | my $added = $added_code{$line_number};
|
| 298 | $attribute_line_number++;
|
| 299 | $added =~ s/LINE/$attribute_line_number/;
|
| 300 | print FILE_OUT "$added\n ";
|
| 301 |
|
| 302 | if( $debug == 1 )
|
| 303 | {
|
| 304 | print FILE_LOG "$added ";
|
| 305 | $log_on = 1;
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | print FILE_OUT $line_in;
|
| 310 |
|
| 311 | if($log_on==1)
|
| 312 | {
|
| 313 | print FILE_LOG $line_in;
|
| 314 |
|
| 315 | if( $line_in =~ /[={\;]/ )
|
| 316 | {
|
| 317 | $log_on= 0;
|
| 318 | print FILE_LOG "\n";
|
| 319 | }
|
| 320 | }
|
| 321 |
|
| 322 | }
|
| 323 |
|
| 324 | close FILE_IN;
|
| 325 | close FILE_OUT;
|
| 326 |
|
| 327 | return $file_bak;
|
| 328 | }
|
| 329 |
|
| 330 | #-------------------------------------------------------------------
|
| 331 |
|
| 332 | sub print_error_info_die
|
| 333 | {
|
| 334 | my $error_info = $_[0];
|
| 335 | print FILE_LOG $error_info;
|
| 336 | print STDERR $error_info;
|
| 337 | exit(1);
|
| 338 |
|
| 339 | }
|
| 340 |
|
| 341 | #------------------------------------------------------------------- |