📄 mangleappc.pl
字号:
## Copyright (c) 2007 University of Copenhagen# All rights reserved.## Redistribution and use in source and binary forms, with or without# modification, are permitted provided that the following conditions# are met:# - Redistributions of source code must retain the above copyright# notice, this list of conditions and the following disclaimer.# - Redistributions in binary form must reproduce the above copyright# notice, this list of conditions and the following disclaimer in the# documentation and/or other materials provided with the# distribution.# - Neither the name of University of Copenhagen nor the names of# its contributors may be used to endorse or promote products derived# from this software without specific prior written permission.## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD# UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED# OF THE POSSIBILITY OF SUCH DAMAGE.## Based on an idea for a similar script by # Cory Sharp <cssharp@eecs.berkeley.edu>## Later addtions by# Mads Bondo Dydensborg <madsdyd@diku.dk>## Adopted for sdcc and mcs51 by# Martin Leopold <leopold@diku.dk># Sidsel Jensen <purps@diku.dk># Anders Egeskov <egeskov@diku.dk>### This script accepts a C-file in GCC dialect with a bunch of wierd hints# and translates them into a C-file in SDCC or Keil dialect#use strict;use Getopt::Long;my $KEIL = '';my $SDCC = '';my $IAR = '';my $file = '';my $comment_level = 0;my $multi_match = 0;my $memory_att_match = 0;my $NUM_TASKS = 0;my $empty_struct=0;my $string_h_match = 0;my $string_h_seen = 0;my %enums = ();my $typedef_struct = "";my $typedef_struct_ident = "";my $typedef_struct_lines = 0;my $typedef_struct_empty = 0;my $typedef_struct_closed = 0;my %typedef_struct_empty_ident = ();my $line_no = 0;GetOptions( 'keil' => \$KEIL, 'sdcc' => \$SDCC, 'iar' => \$IAR, 'file=s' => \$file,);if ( !( $KEIL || $SDCC || $IAR ) || ! $file) { die "Usage sdccMangleAppC.pl <compiler> -file \"<filename>\"\nwhere compiler is either --keil, --sdcc, --iar\n";}open(FILE,"<$file") or die "no such file $file\n";while(<FILE>) { $line_no++; # If on the first line, print some header. It's in the while loop so if # this script is invoked with -i, the correct things still happen. # NOTE: This was broken by SMAC! if( $. == 1 ) { print <<"EOF";#define MANGLED_NESC_APP_CEOF }## Skip lines that start with // and that are within a comment# if ($_ =~ /^\/\//) { print $_; next;} if ($comment_level > 0) { print $_; next; } if ($_ =~ /\/\*/) { $comment_level++; } if ($_ =~ /\*\//) { $comment_level--; }## replace $ in symbols with __ (dollar in identifiers)# s{([\w\$]+)}{ (my $t=$1) =~ s/\$/__/g; $t }ge if /\$/;## Replace sfr related definitions with non ANSI-C dialekts# # Kill the dummy typedef's # REPLACED! # s{^(typedef uint8_t sfr;)}{//$1}; # s{^(typedef uint16_t sfr16;)}{//$1}; # s{^(typedef uint8_t sbit;)}{//$1}; # Should these be replaced to please CIL? s{^(typedef uint8_t uint8_t_xdata;)}{//$1}; s{^(typedef uint16_t uint16_t_xdata;)}{//$1}; s{^(typedef uint8_t uint8_t_code;)}{//$1}; s{^(typedef uint16_t uint16_t_code;)}{//$1}; # Replace # uint8_t volatile P0 __attribute((sfrAT0x86)) # with # sfr P0 at 0x86if(m{uint(\d+)_t\s+volatile\s+(.*)\s+__attribute(?:__)?\(\((?:__)?(sfr|sbit|sfr16)AT(.{4})(?:__)?\)\).*;}) { my $width=$1; my $ident=$2; my $type=$3; my $addr=$4; if($width==16 && $type ne "sfr16") { printf "Error"; exit; } if ( $KEIL ) {$_ = "$type $ident = $addr;\n"; } if ( $SDCC ) {$_ = "__$type __at ($addr) $ident;\n"; } # IAR uses a slightly different way to define the # sbit: __bit __no_init volatile bool name @ (addr+bit) # sfr: __sfr __no_init volatile unsigned char name @ addr # sfr16: __sfr __no_init volatile unsigned int name @ addr # sfr32: __sfr __no_init volatile unsigned long name @ addr if ( $IAR ) { if ($type eq "sfr") { $_ = "__sfr __no_init volatile unsigned char $ident @ $addr;\n"; } if ($type eq "sfr16") { $_ = "__sfr __no_init volatile unsigned int $ident @ $addr;\n"; } if ($type eq "sbit") { $_ = "__bit __no_init bool $ident @ $addr;\n"; } } } if ($IAR && m(typedef uint8_t bool.*;) ) { $_ = "//" . $_; $_ = $_ . "#include \"stdbool.h\"\n"; } # Replace dummy xdata types with type and storrage class specifier if (s{uint(8|16)_t_xdata}{uint$1_t xdata}g) { $memory_att_match = 1; # Don't replace data with _data } # Replace dummy code types with type and storrage class specifier s{uint(8|16)_t_code}{uint$1_t code}g;## Remove string.h definitions (memset, memcpy, strnlen, ..). Get Keil versions from string.h# # Replaced by custom string.h # # if( $string_h_match==1 || # /^.*extern size_t strlen\(/ || # /^.*extern void \*memset\(/ || # /^.*extern void \*memcpy\(/ # ) { # $_ = "//" . $_; # $string_h_match=1; # if ($string_h_seen == 0) { # $string_h_seen = 1; # $_ = $_ . "#include <string.h>\n" ; # } # } # # look for the ")" and ";" of the function # if ($string_h_match==1 && /^.*\).*;.*/) { # $string_h_match=0; # } # ## Replace attribute definition corresponding to memory segments (storrage class)# For Keil the small memory model puts all variables in data# the large model all goes in xdata## Replace uint8_t j __attribute((xdata)); with uint8_t xdata j; $memory_att_match = 0; if(s{\s+(.*)\s+__attribute(?:__)?\(\((?:__)?((?:x|p)?data)(?:__)?\)\)}{ $2 $1}) { $memory_att_match = 1; # Dont replace with _data later on }## Replace interrupt declarations# # gcc interrupt declatation to sdcc declaration # From: __attribute((interrupt)) void __vector_5(void) # To: void __vector_5(void) interrupt 5 # CIL outputs interrupts in a different manner: # void __attribute__((__interrupt__)) __vector_2(void) # http://www.keil.com/support/man/docs/c51/c51_le_interruptfuncs.htm # SDCC: # http://sdcc.sourceforge.net/doc/sdccman.html/node64.html # void timer_isr (void) __interrupt (1) __using (1) if(m{^\s*(?:void\s+)?__attribute(?:__)?\(\((?:__)?interrupt(?:__)?\)\)\s+(?:void\s+)?([_[:alpha:]]+)(\d+)\s*\(void\)}&& $_ !~ m{;}) { my $int_no=$2; my $func_name=$1; if ( $KEIL ) {$_ = "void $func_name$int_no(void) interrupt $int_no\n"; } if ( $SDCC ) {$_ = "void $func_name$int_no(void) __interrupt ($int_no)\n"; } } # Remove function prototypes # (for some reason nesc doesn't produce prototypes and function defs the same way.. s{^(\s*void\s+__vector_\d+\s*\(void\)\s+__attribute(?:__)?\(\((?:__)?interrupt(?:__)?\)\)\s+\;)}{/*$1*/};## Remove any remaining gcc style attributes# # The remaining attributes at this point don't have SDCC/Keil equivalents # This includes: # __attribute__((packed)) # __attribute((__nothrow__)) # __mode__((__QI__)) - from sys/types.h # __mode__((__HI__)) - from sys/types.h # __mode__((__SI__)) - from sys/types.h # __mode__((__word__)) - from sys/types.h # # First check if the line has been uncommmented sinces comments in comments # are a nono... if (! (m{^.*/\*} || # /* style comment m{^.*//})){ # // style comment #s{(__attribute__\s*\(\(.*\)\))}{/*$1*/}; #s{(__attribute\s*\(\(.*\)\))}{/*$1*/}; s{(__attribute(?:__)?\s*\(\(.*\)\))}{/*$1*/}; }## Mangle wierdness of empty array/typedef defitions..# # keil seems to barf if 0-length array is specified with the actual 0 # specifying [] instead of 0 seems to be the solution # # Look for the actual definition of the array length an mangle if 0 # # The following fail if the enums are 0 (in Keil) # volatile uint8_t SchedulerBasicP$m_next[SchedulerBasicP$NUM_TASKS]; # typedef int BlinkNoTimerTaskC$__nesc_sillytask_toggle[BlinkNoTimerTaskC$toggle]; # # Turns out nescc produces a lot of these based on enums. So we need # to look them up and replace the ones that are 0 (sigh)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -