Add qemu 2.4.0
[kvmfornfv.git] / qemu / dtc / convert-dtsv0-lexer.l
1 /*
2  * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005, 2008.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
17  *                                                                   USA
18  */
19
20 %option noyywrap nounput noinput never-interactive
21
22 %x INCLUDE
23 %x BYTESTRING
24 %x PROPNODENAME
25
26 PROPNODECHAR    [a-zA-Z0-9,._+*#?@-]
27 PATHCHAR        ({PROPNODECHAR}|[/])
28 LABEL           [a-zA-Z_][a-zA-Z0-9_]*
29 STRING          \"([^\\"]|\\.)*\"
30 WS              [[:space:]]
31 COMMENT         "/*"([^*]|\*+[^*/])*\*+"/"
32 LINECOMMENT     "//".*\n
33 GAP             ({WS}|{COMMENT}|{LINECOMMENT})*
34
35 %{
36 #include <string.h>
37 #include <stdlib.h>
38 #include <stdarg.h>
39
40 #include <errno.h>
41 #include <assert.h>
42 #include <fnmatch.h>
43
44 #include "srcpos.h"
45 #include "util.h"
46
47 static int v1_tagged; /* = 0 */
48 static int cbase = 16;
49 static int saw_hyphen; /* = 0 */
50 static unsigned long long last_val;
51 static char *last_name; /* = NULL */
52
53 const struct {
54         const char *pattern;
55         int obase, width;
56 } guess_table[] = {
57         { "*-frequency", 10, 0 },
58         { "num-*", 10, 0 },
59         { "#*-cells", 10, 0 },
60         { "*cache-line-size", 10, 0 },
61         { "*cache-block-size", 10, 0 },
62         { "*cache-size", 10, 0 },
63         { "*cache-sets", 10, 0 },
64         { "cell-index", 10, 0 },
65         { "bank-width", 10, 0 },
66         { "*-fifo-size", 10, 0 },
67         { "*-frame-size", 10, 0 },
68         { "*-channel", 10, 0 },
69         { "current-speed", 10, 0 },
70         { "phy-map", 16, 8 },
71         { "dcr-reg", 16, 3 },
72         { "reg", 16, 8 },
73         { "ranges", 16, 8},
74 };
75 %}
76
77 %%
78 <*>"/include/"{GAP}{STRING}     ECHO;
79
80 <*>\"([^\\"]|\\.)*\"    ECHO;
81
82 <*>"/dts-v1/"   {
83                         die("Input dts file is already version 1\n");
84                 }
85
86 <*>"/memreserve/"       {
87                         if (!v1_tagged) {
88                                 fprintf(yyout, "/dts-v1/;\n\n");
89                                 v1_tagged = 1;
90                         }
91
92                         ECHO;
93                         BEGIN(INITIAL);
94                 }
95
96 <*>{LABEL}:             ECHO;
97
98 <INITIAL>[bodh]# {
99                         if (*yytext == 'b')
100                                 cbase = 2;
101                         else if (*yytext == 'o')
102                                 cbase = 8;
103                         else if (*yytext == 'd')
104                                 cbase = 10;
105                         else
106                                 cbase = 16;
107                 }
108
109 <INITIAL>[0-9a-fA-F]+   {
110                         unsigned long long val;
111                         int obase = 16, width = 0;
112                         int i;
113
114                         val = strtoull(yytext, NULL, cbase);
115
116                         if (saw_hyphen)
117                                 val = val - last_val + 1;
118
119                         if (last_name) {
120                                 for (i = 0; i < ARRAY_SIZE(guess_table); i++)
121                                         if (fnmatch(guess_table[i].pattern,
122                                             last_name, 0) == 0) {
123                                                 obase = guess_table[i].obase;
124                                                 width = guess_table[i].width;
125                                         }
126                         } else {
127                                 obase = 16;
128                                 width = 16;
129                         }
130
131                         if (cbase != 16)
132                                 obase = cbase;
133
134                         switch (obase) {
135                         case 2:
136                         case 16:
137                                 fprintf(yyout, "0x%0*llx", width, val);
138                                 break;
139                         case 8:
140                                 fprintf(yyout, "0%0*llo", width, val);
141                                 break;
142                         case 10:
143                                 fprintf(yyout, "%*llu", width, val);
144                                 break;
145                         }
146
147                         cbase = 16;
148                         last_val = val;
149                         saw_hyphen = 0;
150                 }
151
152 \&{LABEL}               ECHO;
153
154 "&{/"{PATHCHAR}+\}      ECHO;
155
156 <INITIAL>"&/"{PATHCHAR}+ fprintf(yyout, "&{/%s}", yytext + 2);
157
158 <BYTESTRING>[0-9a-fA-F]{2} ECHO;
159
160 <BYTESTRING>"]" {
161                         ECHO;
162                         BEGIN(INITIAL);
163                 }
164
165 <PROPNODENAME>{PROPNODECHAR}+ {
166                         ECHO;
167                         last_name = xstrdup(yytext);
168                         BEGIN(INITIAL);
169                 }
170
171 <*>{GAP}        ECHO;
172
173 <*>-            {       /* Hack to convert old style memreserves */
174                         saw_hyphen = 1;
175                         fprintf(yyout, " ");
176                 }
177
178 <*>.            {
179                         if (!v1_tagged) {
180                                 fprintf(yyout, "/dts-v1/;\n\n");
181                                 v1_tagged = 1;
182                         }
183
184                         ECHO;
185                         if (yytext[0] == '[') {
186                                 BEGIN(BYTESTRING);
187                         }
188                         if ((yytext[0] == '{')
189                             || (yytext[0] == ';')) {
190                                 BEGIN(PROPNODENAME);
191                         }
192                 }
193
194 %%
195 /* Usage related data. */
196 static const char usage_synopsis[] = "convert-dtsv0 [options] <v0 dts file>...";
197 static const char usage_short_opts[] = "" USAGE_COMMON_SHORT_OPTS;
198 static struct option const usage_long_opts[] = {
199         USAGE_COMMON_LONG_OPTS
200 };
201 static const char * const usage_opts_help[] = {
202         USAGE_COMMON_OPTS_HELP
203 };
204
205 static void convert_file(const char *fname)
206 {
207         const char suffix[] = "v1";
208         int len = strlen(fname);
209         char *newname;
210
211         newname = xmalloc(len + sizeof(suffix));
212         memcpy(newname, fname, len);
213         memcpy(newname + len, suffix, sizeof(suffix));
214
215         yyin = fopen(fname, "r");
216         if (!yyin)
217                 die("Couldn't open input file %s: %s\n",
218                     fname, strerror(errno));
219
220         yyout = fopen(newname, "w");
221         if (!yyout)
222                 die("Couldn't open output file %s: %s\n",
223                     newname, strerror(errno));
224
225         while(yylex())
226                 ;
227 }
228
229 int main(int argc, char *argv[])
230 {
231         int opt;
232         int i;
233
234         while ((opt = util_getopt_long()) != EOF) {
235                 switch (opt) {
236                 case_USAGE_COMMON_FLAGS
237                 }
238         }
239         if (argc < 2)
240                 usage("missing filename");
241
242         for (i = 1; i < argc; i++) {
243                 fprintf(stderr, "Converting %s from dts v0 to dts v1\n", argv[i]);
244                 convert_file(argv[i]);
245         }
246
247         exit(0);
248 }