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