Imported Upstream version 2.9.0
[debian/cc1111] / support / scripts / keil2sdcc.pl
1 #!/usr/bin/perl -w
2
3 # keil2sdcc.pl
4 # Scott Bronson
5 # 22 June 2003
6
7
8 # usage (UNIX):
9 #    perl keil2sdcc.pl < keil_header.h > sdcc_header.h
10 # or
11 #    perl keil2sdcc.pl keil_header.h > sdcc_header.h
12 #
13 # usage (Windows):
14 #    perl keil2sdcc.pl keil_header.h > sdcc_header.h
15 #
16 #
17 # keil_header.h and sdcc_header.h must not be the same file since
18 # most shells overwrite the output file before opening the input file.
19
20
21 # This script converts Keil-style header files to SDCC.  It tries to
22 # be pedantic so don't be surprised if you need to munge it a bit to
23 # get it to work.  On the other hand, it doesn't fully parse the C
24 # file (for obvious reasons).
25
26 # It takes the Keil header file either as an argument or on
27 # stdin and it produces the output on stdout.
28
29 # This script is inspired by keil2sdcc.pl by Bela Torok but a lot
30 # more pedantic.
31
32 use strict;
33
34 while(<>) 
35 {
36         s/\r//g;        # remove DOS line endings if necessary
37
38         # external register (kind of a weird format)
39         #
40         # in:  EXTERN xdata volatile BYTE GPIF_WAVE_DATA _AT_ 0xE400;
41         # out: EXTERN xdata at 0xE400 volatile BYTE GPIF_WAVE_DATA;
42         # $1: leading whitespace
43         # $2: variable name
44         # $3: variable location
45         # $4: trailing comments, etc.
46
47         if(/^(\s*)EXTERN\s*xdata\s*volatile\s*BYTE\s*(\w+(?:\s*\[\s*\d+\s*\])?)\s+_AT_\s*([^;]+);(.*)$/) {
48                 print "$1EXTERN xdata at $3 volatile BYTE $2;$4\n";
49                 next;
50         }
51
52         # sfr statement
53         #
54         # in:  sfr IOA = 0x80;
55         # out: sfr at 0x80 IOA;
56         # $1: leading whitespace
57         # $2: variable name
58         # $3: variable location
59         # $4: trailing comments, etc.
60
61         if(/^(\s*)sfr\s*(\w+)\s*=\s*([^;]+);(.*)$/) {
62                 print "$1sfr at $3 $2;$4\n";
63                 next;
64         }
65
66         # sbit statement
67         #
68         # in:  sbit SEL = 0x86+0;
69         # out: sbit at 0x86+0 SEL;
70         # $1: leading whitespace
71         # $2: variable name
72         # $3: variable location
73         # $4: trailing comments, etc.
74
75         if(/^(\s*)sbit\s*(\w+)\s*=\s*([^;]+);(.*)$/) {
76                 print "$1sbit at $3 $2;$4\n";
77                 next;
78         }
79
80
81
82         # entire line is a C++ comment, output it unchanged.
83         if(/^(\s*)\/\/(.*)$/) {
84                 print "$1//$2\n";
85                 next;
86         }
87
88         # C comment, slurp lines until the close comment and output it unchanged.
89         if(/^(\s*)\/\*(.*)$/) {
90                 my($ws,$cmt) = ($1,"$2\n");
91                 $cmt .= <> while $cmt !~ /\*\/\s*$/;
92                 $cmt =~ s/\r//g;
93                 print "$ws/*$cmt";
94                 next;
95         }
96
97         # preprocessor statement (whitespace followed by '#'), don't change
98         if(/^(\s*)\#(.*)$/) {
99                 print "$1#$2\n";
100                 next;
101         }
102
103         # blank line, don't change
104         if(/^(\s*)$/) {
105                 print "\n";
106                 next;
107         }
108
109         chomp;
110         die "Unconvertable line: \"$_\"\n";
111 }
112