Linux 2.6.33-rc8
[linux-2.6/lguest.git] / scripts / dtc / dtc-parser.y
blobb2ab562420ead9815ac83adf4372bece16e10f23
1 /*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
21 %locations
24 #include <stdio.h>
26 #include "dtc.h"
27 #include "srcpos.h"
29 extern int yylex(void);
31 extern struct boot_info *the_boot_info;
32 extern int treesource_error;
34 static unsigned long long eval_literal(const char *s, int base, int bits);
37 %union {
38 char *propnodename;
39 char *literal;
40 char *labelref;
41 unsigned int cbase;
42 uint8_t byte;
43 struct data data;
45 uint64_t addr;
46 cell_t cell;
47 struct property *prop;
48 struct property *proplist;
49 struct node *node;
50 struct node *nodelist;
51 struct reserve_info *re;
54 %token DT_V1
55 %token DT_MEMRESERVE
56 %token <propnodename> DT_PROPNODENAME
57 %token <literal> DT_LITERAL
58 %token <literal> DT_LEGACYLITERAL
59 %token <cbase> DT_BASE
60 %token <byte> DT_BYTE
61 %token <data> DT_STRING
62 %token <labelref> DT_LABEL
63 %token <labelref> DT_REF
64 %token DT_INCBIN
66 %type <data> propdata
67 %type <data> propdataprefix
68 %type <re> memreserve
69 %type <re> memreserves
70 %type <re> v0_memreserve
71 %type <re> v0_memreserves
72 %type <addr> addr
73 %type <data> celllist
74 %type <cbase> cellbase
75 %type <cell> cellval
76 %type <data> bytestring
77 %type <prop> propdef
78 %type <proplist> proplist
80 %type <node> devicetree
81 %type <node> nodedef
82 %type <node> subnode
83 %type <nodelist> subnodes
84 %type <labelref> label
88 sourcefile:
89 DT_V1 ';' memreserves devicetree
91 the_boot_info = build_boot_info($3, $4, 0);
93 | v0_memreserves devicetree
95 the_boot_info = build_boot_info($1, $2, 0);
99 memreserves:
100 /* empty */
102 $$ = NULL;
104 | memreserve memreserves
106 $$ = chain_reserve_entry($1, $2);
110 memreserve:
111 label DT_MEMRESERVE addr addr ';'
113 $$ = build_reserve_entry($3, $4, $1);
117 v0_memreserves:
118 /* empty */
120 $$ = NULL;
122 | v0_memreserve v0_memreserves
124 $$ = chain_reserve_entry($1, $2);
128 v0_memreserve:
129 memreserve
131 $$ = $1;
133 | label DT_MEMRESERVE addr '-' addr ';'
135 $$ = build_reserve_entry($3, $5 - $3 + 1, $1);
139 addr:
140 DT_LITERAL
142 $$ = eval_literal($1, 0, 64);
144 | DT_LEGACYLITERAL
146 $$ = eval_literal($1, 16, 64);
150 devicetree:
151 '/' nodedef
153 $$ = name_node($2, "", NULL);
157 nodedef:
158 '{' proplist subnodes '}' ';'
160 $$ = build_node($2, $3);
164 proplist:
165 /* empty */
167 $$ = NULL;
169 | proplist propdef
171 $$ = chain_property($2, $1);
175 propdef:
176 label DT_PROPNODENAME '=' propdata ';'
178 $$ = build_property($2, $4, $1);
180 | label DT_PROPNODENAME ';'
182 $$ = build_property($2, empty_data, $1);
186 propdata:
187 propdataprefix DT_STRING
189 $$ = data_merge($1, $2);
191 | propdataprefix '<' celllist '>'
193 $$ = data_merge($1, $3);
195 | propdataprefix '[' bytestring ']'
197 $$ = data_merge($1, $3);
199 | propdataprefix DT_REF
201 $$ = data_add_marker($1, REF_PATH, $2);
203 | propdataprefix DT_INCBIN '(' DT_STRING ',' addr ',' addr ')'
205 struct search_path path = { srcpos_file->dir, NULL, NULL };
206 struct dtc_file *file = dtc_open_file($4.val, &path);
207 struct data d = empty_data;
209 if ($6 != 0)
210 if (fseek(file->file, $6, SEEK_SET) != 0)
211 yyerrorf("Couldn't seek to offset %llu in \"%s\": %s",
212 (unsigned long long)$6,
213 $4.val, strerror(errno));
215 d = data_copy_file(file->file, $8);
217 $$ = data_merge($1, d);
218 dtc_close_file(file);
220 | propdataprefix DT_INCBIN '(' DT_STRING ')'
222 struct search_path path = { srcpos_file->dir, NULL, NULL };
223 struct dtc_file *file = dtc_open_file($4.val, &path);
224 struct data d = empty_data;
226 d = data_copy_file(file->file, -1);
228 $$ = data_merge($1, d);
229 dtc_close_file(file);
231 | propdata DT_LABEL
233 $$ = data_add_marker($1, LABEL, $2);
237 propdataprefix:
238 /* empty */
240 $$ = empty_data;
242 | propdata ','
244 $$ = $1;
246 | propdataprefix DT_LABEL
248 $$ = data_add_marker($1, LABEL, $2);
252 celllist:
253 /* empty */
255 $$ = empty_data;
257 | celllist cellval
259 $$ = data_append_cell($1, $2);
261 | celllist DT_REF
263 $$ = data_append_cell(data_add_marker($1, REF_PHANDLE,
264 $2), -1);
266 | celllist DT_LABEL
268 $$ = data_add_marker($1, LABEL, $2);
272 cellbase:
273 /* empty */
275 $$ = 16;
277 | DT_BASE
280 cellval:
281 DT_LITERAL
283 $$ = eval_literal($1, 0, 32);
285 | cellbase DT_LEGACYLITERAL
287 $$ = eval_literal($2, $1, 32);
291 bytestring:
292 /* empty */
294 $$ = empty_data;
296 | bytestring DT_BYTE
298 $$ = data_append_byte($1, $2);
300 | bytestring DT_LABEL
302 $$ = data_add_marker($1, LABEL, $2);
306 subnodes:
307 /* empty */
309 $$ = NULL;
311 | subnode subnodes
313 $$ = chain_node($1, $2);
315 | subnode propdef
317 yyerror("syntax error: properties must precede subnodes");
318 YYERROR;
322 subnode:
323 label DT_PROPNODENAME nodedef
325 $$ = name_node($3, $2, $1);
329 label:
330 /* empty */
332 $$ = NULL;
334 | DT_LABEL
336 $$ = $1;
342 void yyerrorf(char const *s, ...)
344 const char *fname = srcpos_file ? srcpos_file->name : "<no-file>";
345 va_list va;
346 va_start(va, s);
348 if (strcmp(fname, "-") == 0)
349 fname = "stdin";
351 fprintf(stderr, "%s:%d ", fname, yylloc.first_line);
352 vfprintf(stderr, s, va);
353 fprintf(stderr, "\n");
355 treesource_error = 1;
356 va_end(va);
359 void yyerror (char const *s)
361 yyerrorf("%s", s);
364 static unsigned long long eval_literal(const char *s, int base, int bits)
366 unsigned long long val;
367 char *e;
369 errno = 0;
370 val = strtoull(s, &e, base);
371 if (*e)
372 yyerror("bad characters in literal");
373 else if ((errno == ERANGE)
374 || ((bits < 64) && (val >= (1ULL << bits))))
375 yyerror("literal out of range");
376 else if (errno != 0)
377 yyerror("bad literal");
378 return val;