Linux 4.2.6
[linux/fpc-iii.git] / scripts / dtc / dtc-parser.y
blob5a897e36562d67107dba44fa91274253b34152dc
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 #include <stdio.h>
23 #include "dtc.h"
24 #include "srcpos.h"
26 extern int yylex(void);
27 extern void yyerror(char const *s);
28 #define ERROR(loc, ...) \
29 do { \
30 srcpos_error((loc), "Error", __VA_ARGS__); \
31 treesource_error = true; \
32 } while (0)
34 extern struct boot_info *the_boot_info;
35 extern bool treesource_error;
38 %union {
39 char *propnodename;
40 char *labelref;
41 uint8_t byte;
42 struct data data;
44 struct {
45 struct data data;
46 int bits;
47 } array;
49 struct property *prop;
50 struct property *proplist;
51 struct node *node;
52 struct node *nodelist;
53 struct reserve_info *re;
54 uint64_t integer;
57 %token DT_V1
58 %token DT_MEMRESERVE
59 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
60 %token DT_BITS
61 %token DT_DEL_PROP
62 %token DT_DEL_NODE
63 %token <propnodename> DT_PROPNODENAME
64 %token <integer> DT_LITERAL
65 %token <integer> DT_CHAR_LITERAL
66 %token <byte> DT_BYTE
67 %token <data> DT_STRING
68 %token <labelref> DT_LABEL
69 %token <labelref> DT_REF
70 %token DT_INCBIN
72 %type <data> propdata
73 %type <data> propdataprefix
74 %type <re> memreserve
75 %type <re> memreserves
76 %type <array> arrayprefix
77 %type <data> bytestring
78 %type <prop> propdef
79 %type <proplist> proplist
81 %type <node> devicetree
82 %type <node> nodedef
83 %type <node> subnode
84 %type <nodelist> subnodes
86 %type <integer> integer_prim
87 %type <integer> integer_unary
88 %type <integer> integer_mul
89 %type <integer> integer_add
90 %type <integer> integer_shift
91 %type <integer> integer_rela
92 %type <integer> integer_eq
93 %type <integer> integer_bitand
94 %type <integer> integer_bitxor
95 %type <integer> integer_bitor
96 %type <integer> integer_and
97 %type <integer> integer_or
98 %type <integer> integer_trinary
99 %type <integer> integer_expr
103 sourcefile:
104 DT_V1 ';' memreserves devicetree
106 the_boot_info = build_boot_info($3, $4,
107 guess_boot_cpuid($4));
111 memreserves:
112 /* empty */
114 $$ = NULL;
116 | memreserve memreserves
118 $$ = chain_reserve_entry($1, $2);
122 memreserve:
123 DT_MEMRESERVE integer_prim integer_prim ';'
125 $$ = build_reserve_entry($2, $3);
127 | DT_LABEL memreserve
129 add_label(&$2->labels, $1);
130 $$ = $2;
134 devicetree:
135 '/' nodedef
137 $$ = name_node($2, "");
139 | devicetree '/' nodedef
141 $$ = merge_nodes($1, $3);
144 | devicetree DT_LABEL DT_REF nodedef
146 struct node *target = get_node_by_ref($1, $3);
148 add_label(&target->labels, $2);
149 if (target)
150 merge_nodes(target, $4);
151 else
152 ERROR(&@3, "Label or path %s not found", $3);
153 $$ = $1;
155 | devicetree DT_REF nodedef
157 struct node *target = get_node_by_ref($1, $2);
159 if (target)
160 merge_nodes(target, $3);
161 else
162 ERROR(&@2, "Label or path %s not found", $2);
163 $$ = $1;
165 | devicetree DT_DEL_NODE DT_REF ';'
167 struct node *target = get_node_by_ref($1, $3);
169 if (target)
170 delete_node(target);
171 else
172 ERROR(&@3, "Label or path %s not found", $3);
175 $$ = $1;
179 nodedef:
180 '{' proplist subnodes '}' ';'
182 $$ = build_node($2, $3);
186 proplist:
187 /* empty */
189 $$ = NULL;
191 | proplist propdef
193 $$ = chain_property($2, $1);
197 propdef:
198 DT_PROPNODENAME '=' propdata ';'
200 $$ = build_property($1, $3);
202 | DT_PROPNODENAME ';'
204 $$ = build_property($1, empty_data);
206 | DT_DEL_PROP DT_PROPNODENAME ';'
208 $$ = build_property_delete($2);
210 | DT_LABEL propdef
212 add_label(&$2->labels, $1);
213 $$ = $2;
217 propdata:
218 propdataprefix DT_STRING
220 $$ = data_merge($1, $2);
222 | propdataprefix arrayprefix '>'
224 $$ = data_merge($1, $2.data);
226 | propdataprefix '[' bytestring ']'
228 $$ = data_merge($1, $3);
230 | propdataprefix DT_REF
232 $$ = data_add_marker($1, REF_PATH, $2);
234 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
236 FILE *f = srcfile_relative_open($4.val, NULL);
237 struct data d;
239 if ($6 != 0)
240 if (fseek(f, $6, SEEK_SET) != 0)
241 die("Couldn't seek to offset %llu in \"%s\": %s",
242 (unsigned long long)$6, $4.val,
243 strerror(errno));
245 d = data_copy_file(f, $8);
247 $$ = data_merge($1, d);
248 fclose(f);
250 | propdataprefix DT_INCBIN '(' DT_STRING ')'
252 FILE *f = srcfile_relative_open($4.val, NULL);
253 struct data d = empty_data;
255 d = data_copy_file(f, -1);
257 $$ = data_merge($1, d);
258 fclose(f);
260 | propdata DT_LABEL
262 $$ = data_add_marker($1, LABEL, $2);
266 propdataprefix:
267 /* empty */
269 $$ = empty_data;
271 | propdata ','
273 $$ = $1;
275 | propdataprefix DT_LABEL
277 $$ = data_add_marker($1, LABEL, $2);
281 arrayprefix:
282 DT_BITS DT_LITERAL '<'
284 unsigned long long bits;
286 bits = $2;
288 if ((bits != 8) && (bits != 16) &&
289 (bits != 32) && (bits != 64)) {
290 ERROR(&@2, "Array elements must be"
291 " 8, 16, 32 or 64-bits");
292 bits = 32;
295 $$.data = empty_data;
296 $$.bits = bits;
298 | '<'
300 $$.data = empty_data;
301 $$.bits = 32;
303 | arrayprefix integer_prim
305 if ($1.bits < 64) {
306 uint64_t mask = (1ULL << $1.bits) - 1;
308 * Bits above mask must either be all zero
309 * (positive within range of mask) or all one
310 * (negative and sign-extended). The second
311 * condition is true if when we set all bits
312 * within the mask to one (i.e. | in the
313 * mask), all bits are one.
315 if (($2 > mask) && (($2 | mask) != -1ULL))
316 ERROR(&@2, "Value out of range for"
317 " %d-bit array element", $1.bits);
320 $$.data = data_append_integer($1.data, $2, $1.bits);
322 | arrayprefix DT_REF
324 uint64_t val = ~0ULL >> (64 - $1.bits);
326 if ($1.bits == 32)
327 $1.data = data_add_marker($1.data,
328 REF_PHANDLE,
329 $2);
330 else
331 ERROR(&@2, "References are only allowed in "
332 "arrays with 32-bit elements.");
334 $$.data = data_append_integer($1.data, val, $1.bits);
336 | arrayprefix DT_LABEL
338 $$.data = data_add_marker($1.data, LABEL, $2);
342 integer_prim:
343 DT_LITERAL
344 | DT_CHAR_LITERAL
345 | '(' integer_expr ')'
347 $$ = $2;
351 integer_expr:
352 integer_trinary
355 integer_trinary:
356 integer_or
357 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
360 integer_or:
361 integer_and
362 | integer_or DT_OR integer_and { $$ = $1 || $3; }
365 integer_and:
366 integer_bitor
367 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
370 integer_bitor:
371 integer_bitxor
372 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
375 integer_bitxor:
376 integer_bitand
377 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
380 integer_bitand:
381 integer_eq
382 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
385 integer_eq:
386 integer_rela
387 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
388 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
391 integer_rela:
392 integer_shift
393 | integer_rela '<' integer_shift { $$ = $1 < $3; }
394 | integer_rela '>' integer_shift { $$ = $1 > $3; }
395 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
396 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
399 integer_shift:
400 integer_shift DT_LSHIFT integer_add { $$ = $1 << $3; }
401 | integer_shift DT_RSHIFT integer_add { $$ = $1 >> $3; }
402 | integer_add
405 integer_add:
406 integer_add '+' integer_mul { $$ = $1 + $3; }
407 | integer_add '-' integer_mul { $$ = $1 - $3; }
408 | integer_mul
411 integer_mul:
412 integer_mul '*' integer_unary { $$ = $1 * $3; }
413 | integer_mul '/' integer_unary { $$ = $1 / $3; }
414 | integer_mul '%' integer_unary { $$ = $1 % $3; }
415 | integer_unary
418 integer_unary:
419 integer_prim
420 | '-' integer_unary { $$ = -$2; }
421 | '~' integer_unary { $$ = ~$2; }
422 | '!' integer_unary { $$ = !$2; }
425 bytestring:
426 /* empty */
428 $$ = empty_data;
430 | bytestring DT_BYTE
432 $$ = data_append_byte($1, $2);
434 | bytestring DT_LABEL
436 $$ = data_add_marker($1, LABEL, $2);
440 subnodes:
441 /* empty */
443 $$ = NULL;
445 | subnode subnodes
447 $$ = chain_node($1, $2);
449 | subnode propdef
451 ERROR(&@2, "Properties must precede subnodes");
452 YYERROR;
456 subnode:
457 DT_PROPNODENAME nodedef
459 $$ = name_node($2, $1);
461 | DT_DEL_NODE DT_PROPNODENAME ';'
463 $$ = name_node(build_node_delete(), $2);
465 | DT_LABEL subnode
467 add_label(&$2->labels, $1);
468 $$ = $2;
474 void yyerror(char const *s)
476 ERROR(&yylloc, "%s", s);