Linux 5.2.12
[linux/fpc-iii.git] / scripts / dtc / dtc-parser.y
blob2ec981e861110d4984bd4f11ee5230a934902ab1
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>
22 #include <inttypes.h>
24 #include "dtc.h"
25 #include "srcpos.h"
27 extern int yylex(void);
28 extern void yyerror(char const *s);
29 #define ERROR(loc, ...) \
30 do { \
31 srcpos_error((loc), "Error", __VA_ARGS__); \
32 treesource_error = true; \
33 } while (0)
35 extern struct dt_info *parser_output;
36 extern bool treesource_error;
39 %union {
40 char *propnodename;
41 char *labelref;
42 uint8_t byte;
43 struct data data;
45 struct {
46 struct data data;
47 int bits;
48 } array;
50 struct property *prop;
51 struct property *proplist;
52 struct node *node;
53 struct node *nodelist;
54 struct reserve_info *re;
55 uint64_t integer;
56 unsigned int flags;
59 %token DT_V1
60 %token DT_PLUGIN
61 %token DT_MEMRESERVE
62 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
63 %token DT_BITS
64 %token DT_DEL_PROP
65 %token DT_DEL_NODE
66 %token DT_OMIT_NO_REF
67 %token <propnodename> DT_PROPNODENAME
68 %token <integer> DT_LITERAL
69 %token <integer> DT_CHAR_LITERAL
70 %token <byte> DT_BYTE
71 %token <data> DT_STRING
72 %token <labelref> DT_LABEL
73 %token <labelref> DT_LABEL_REF
74 %token <labelref> DT_PATH_REF
75 %token DT_INCBIN
77 %type <data> propdata
78 %type <data> propdataprefix
79 %type <flags> header
80 %type <flags> headers
81 %type <re> memreserve
82 %type <re> memreserves
83 %type <array> arrayprefix
84 %type <data> bytestring
85 %type <prop> propdef
86 %type <proplist> proplist
87 %type <labelref> dt_ref
89 %type <node> devicetree
90 %type <node> nodedef
91 %type <node> subnode
92 %type <nodelist> subnodes
94 %type <integer> integer_prim
95 %type <integer> integer_unary
96 %type <integer> integer_mul
97 %type <integer> integer_add
98 %type <integer> integer_shift
99 %type <integer> integer_rela
100 %type <integer> integer_eq
101 %type <integer> integer_bitand
102 %type <integer> integer_bitxor
103 %type <integer> integer_bitor
104 %type <integer> integer_and
105 %type <integer> integer_or
106 %type <integer> integer_trinary
107 %type <integer> integer_expr
111 sourcefile:
112 headers memreserves devicetree
114 parser_output = build_dt_info($1, $2, $3,
115 guess_boot_cpuid($3));
119 header:
120 DT_V1 ';'
122 $$ = DTSF_V1;
124 | DT_V1 ';' DT_PLUGIN ';'
126 $$ = DTSF_V1 | DTSF_PLUGIN;
130 headers:
131 header
132 | header headers
134 if ($2 != $1)
135 ERROR(&@2, "Header flags don't match earlier ones");
136 $$ = $1;
140 memreserves:
141 /* empty */
143 $$ = NULL;
145 | memreserve memreserves
147 $$ = chain_reserve_entry($1, $2);
151 memreserve:
152 DT_MEMRESERVE integer_prim integer_prim ';'
154 $$ = build_reserve_entry($2, $3);
156 | DT_LABEL memreserve
158 add_label(&$2->labels, $1);
159 $$ = $2;
163 dt_ref: DT_LABEL_REF | DT_PATH_REF;
165 devicetree:
166 '/' nodedef
168 $$ = name_node($2, "");
170 | devicetree '/' nodedef
172 $$ = merge_nodes($1, $3);
174 | dt_ref nodedef
177 * We rely on the rule being always:
178 * versioninfo plugindecl memreserves devicetree
179 * so $-1 is what we want (plugindecl)
181 if (!($<flags>-1 & DTSF_PLUGIN))
182 ERROR(&@2, "Label or path %s not found", $1);
183 $$ = add_orphan_node(
184 name_node(build_node(NULL, NULL, NULL),
185 ""),
186 $2, $1);
188 | devicetree DT_LABEL dt_ref nodedef
190 struct node *target = get_node_by_ref($1, $3);
192 if (target) {
193 add_label(&target->labels, $2);
194 merge_nodes(target, $4);
195 } else
196 ERROR(&@3, "Label or path %s not found", $3);
197 $$ = $1;
199 | devicetree DT_PATH_REF nodedef
202 * We rely on the rule being always:
203 * versioninfo plugindecl memreserves devicetree
204 * so $-1 is what we want (plugindecl)
206 if ($<flags>-1 & DTSF_PLUGIN) {
207 add_orphan_node($1, $3, $2);
208 } else {
209 struct node *target = get_node_by_ref($1, $2);
211 if (target)
212 merge_nodes(target, $3);
213 else
214 ERROR(&@2, "Label or path %s not found", $2);
216 $$ = $1;
218 | devicetree DT_LABEL_REF nodedef
220 struct node *target = get_node_by_ref($1, $2);
222 if (target) {
223 merge_nodes(target, $3);
224 } else {
226 * We rely on the rule being always:
227 * versioninfo plugindecl memreserves devicetree
228 * so $-1 is what we want (plugindecl)
230 if ($<flags>-1 & DTSF_PLUGIN)
231 add_orphan_node($1, $3, $2);
232 else
233 ERROR(&@2, "Label or path %s not found", $2);
235 $$ = $1;
237 | devicetree DT_DEL_NODE dt_ref ';'
239 struct node *target = get_node_by_ref($1, $3);
241 if (target)
242 delete_node(target);
243 else
244 ERROR(&@3, "Label or path %s not found", $3);
247 $$ = $1;
249 | devicetree DT_OMIT_NO_REF dt_ref ';'
251 struct node *target = get_node_by_ref($1, $3);
253 if (target)
254 omit_node_if_unused(target);
255 else
256 ERROR(&@3, "Label or path %s not found", $3);
259 $$ = $1;
263 nodedef:
264 '{' proplist subnodes '}' ';'
266 $$ = build_node($2, $3, &@$);
270 proplist:
271 /* empty */
273 $$ = NULL;
275 | proplist propdef
277 $$ = chain_property($2, $1);
281 propdef:
282 DT_PROPNODENAME '=' propdata ';'
284 $$ = build_property($1, $3, &@$);
286 | DT_PROPNODENAME ';'
288 $$ = build_property($1, empty_data, &@$);
290 | DT_DEL_PROP DT_PROPNODENAME ';'
292 $$ = build_property_delete($2);
294 | DT_LABEL propdef
296 add_label(&$2->labels, $1);
297 $$ = $2;
301 propdata:
302 propdataprefix DT_STRING
304 $$ = data_merge($1, $2);
306 | propdataprefix arrayprefix '>'
308 $$ = data_merge($1, $2.data);
310 | propdataprefix '[' bytestring ']'
312 $$ = data_merge($1, $3);
314 | propdataprefix dt_ref
316 $1 = data_add_marker($1, TYPE_STRING, $2);
317 $$ = data_add_marker($1, REF_PATH, $2);
319 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
321 FILE *f = srcfile_relative_open($4.val, NULL);
322 struct data d;
324 if ($6 != 0)
325 if (fseek(f, $6, SEEK_SET) != 0)
326 die("Couldn't seek to offset %llu in \"%s\": %s",
327 (unsigned long long)$6, $4.val,
328 strerror(errno));
330 d = data_copy_file(f, $8);
332 $$ = data_merge($1, d);
333 fclose(f);
335 | propdataprefix DT_INCBIN '(' DT_STRING ')'
337 FILE *f = srcfile_relative_open($4.val, NULL);
338 struct data d = empty_data;
340 d = data_copy_file(f, -1);
342 $$ = data_merge($1, d);
343 fclose(f);
345 | propdata DT_LABEL
347 $$ = data_add_marker($1, LABEL, $2);
351 propdataprefix:
352 /* empty */
354 $$ = empty_data;
356 | propdata ','
358 $$ = $1;
360 | propdataprefix DT_LABEL
362 $$ = data_add_marker($1, LABEL, $2);
366 arrayprefix:
367 DT_BITS DT_LITERAL '<'
369 unsigned long long bits;
370 enum markertype type = TYPE_UINT32;
372 bits = $2;
374 switch (bits) {
375 case 8: type = TYPE_UINT8; break;
376 case 16: type = TYPE_UINT16; break;
377 case 32: type = TYPE_UINT32; break;
378 case 64: type = TYPE_UINT64; break;
379 default:
380 ERROR(&@2, "Array elements must be"
381 " 8, 16, 32 or 64-bits");
382 bits = 32;
385 $$.data = data_add_marker(empty_data, type, NULL);
386 $$.bits = bits;
388 | '<'
390 $$.data = data_add_marker(empty_data, TYPE_UINT32, NULL);
391 $$.bits = 32;
393 | arrayprefix integer_prim
395 if ($1.bits < 64) {
396 uint64_t mask = (1ULL << $1.bits) - 1;
398 * Bits above mask must either be all zero
399 * (positive within range of mask) or all one
400 * (negative and sign-extended). The second
401 * condition is true if when we set all bits
402 * within the mask to one (i.e. | in the
403 * mask), all bits are one.
405 if (($2 > mask) && (($2 | mask) != -1ULL))
406 ERROR(&@2, "Value out of range for"
407 " %d-bit array element", $1.bits);
410 $$.data = data_append_integer($1.data, $2, $1.bits);
412 | arrayprefix dt_ref
414 uint64_t val = ~0ULL >> (64 - $1.bits);
416 if ($1.bits == 32)
417 $1.data = data_add_marker($1.data,
418 REF_PHANDLE,
419 $2);
420 else
421 ERROR(&@2, "References are only allowed in "
422 "arrays with 32-bit elements.");
424 $$.data = data_append_integer($1.data, val, $1.bits);
426 | arrayprefix DT_LABEL
428 $$.data = data_add_marker($1.data, LABEL, $2);
432 integer_prim:
433 DT_LITERAL
434 | DT_CHAR_LITERAL
435 | '(' integer_expr ')'
437 $$ = $2;
441 integer_expr:
442 integer_trinary
445 integer_trinary:
446 integer_or
447 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
450 integer_or:
451 integer_and
452 | integer_or DT_OR integer_and { $$ = $1 || $3; }
455 integer_and:
456 integer_bitor
457 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
460 integer_bitor:
461 integer_bitxor
462 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
465 integer_bitxor:
466 integer_bitand
467 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
470 integer_bitand:
471 integer_eq
472 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
475 integer_eq:
476 integer_rela
477 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
478 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
481 integer_rela:
482 integer_shift
483 | integer_rela '<' integer_shift { $$ = $1 < $3; }
484 | integer_rela '>' integer_shift { $$ = $1 > $3; }
485 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
486 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
489 integer_shift:
490 integer_shift DT_LSHIFT integer_add { $$ = $1 << $3; }
491 | integer_shift DT_RSHIFT integer_add { $$ = $1 >> $3; }
492 | integer_add
495 integer_add:
496 integer_add '+' integer_mul { $$ = $1 + $3; }
497 | integer_add '-' integer_mul { $$ = $1 - $3; }
498 | integer_mul
501 integer_mul:
502 integer_mul '*' integer_unary { $$ = $1 * $3; }
503 | integer_mul '/' integer_unary
505 if ($3 != 0) {
506 $$ = $1 / $3;
507 } else {
508 ERROR(&@$, "Division by zero");
509 $$ = 0;
512 | integer_mul '%' integer_unary
514 if ($3 != 0) {
515 $$ = $1 % $3;
516 } else {
517 ERROR(&@$, "Division by zero");
518 $$ = 0;
521 | integer_unary
524 integer_unary:
525 integer_prim
526 | '-' integer_unary { $$ = -$2; }
527 | '~' integer_unary { $$ = ~$2; }
528 | '!' integer_unary { $$ = !$2; }
531 bytestring:
532 /* empty */
534 $$ = data_add_marker(empty_data, TYPE_UINT8, NULL);
536 | bytestring DT_BYTE
538 $$ = data_append_byte($1, $2);
540 | bytestring DT_LABEL
542 $$ = data_add_marker($1, LABEL, $2);
546 subnodes:
547 /* empty */
549 $$ = NULL;
551 | subnode subnodes
553 $$ = chain_node($1, $2);
555 | subnode propdef
557 ERROR(&@2, "Properties must precede subnodes");
558 YYERROR;
562 subnode:
563 DT_PROPNODENAME nodedef
565 $$ = name_node($2, $1);
567 | DT_DEL_NODE DT_PROPNODENAME ';'
569 $$ = name_node(build_node_delete(&@$), $2);
571 | DT_OMIT_NO_REF subnode
573 $$ = omit_node_if_unused($2);
575 | DT_LABEL subnode
577 add_label(&$2->labels, $1);
578 $$ = $2;
584 void yyerror(char const *s)
586 ERROR(&yylloc, "%s", s);