treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / scripts / dtc / dtc-parser.y
blob40dcf4f149dad647364ad1a3912f4333a79c0a82
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
4 */
5 %locations
7 %{
8 #include <stdio.h>
9 #include <inttypes.h>
11 #include "dtc.h"
12 #include "srcpos.h"
14 extern int yylex(void);
15 extern void yyerror(char const *s);
16 #define ERROR(loc, ...) \
17 do { \
18 srcpos_error((loc), "Error", __VA_ARGS__); \
19 treesource_error = true; \
20 } while (0)
22 #define YYERROR_CALL(msg) yyerror(msg)
24 extern struct dt_info *parser_output;
25 extern bool treesource_error;
28 %union {
29 char *propnodename;
30 char *labelref;
31 uint8_t byte;
32 struct data data;
34 struct {
35 struct data data;
36 int bits;
37 } array;
39 struct property *prop;
40 struct property *proplist;
41 struct node *node;
42 struct node *nodelist;
43 struct reserve_info *re;
44 uint64_t integer;
45 unsigned int flags;
48 %token DT_V1
49 %token DT_PLUGIN
50 %token DT_MEMRESERVE
51 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
52 %token DT_BITS
53 %token DT_DEL_PROP
54 %token DT_DEL_NODE
55 %token DT_OMIT_NO_REF
56 %token <propnodename> DT_PROPNODENAME
57 %token <integer> DT_LITERAL
58 %token <integer> DT_CHAR_LITERAL
59 %token <byte> DT_BYTE
60 %token <data> DT_STRING
61 %token <labelref> DT_LABEL
62 %token <labelref> DT_LABEL_REF
63 %token <labelref> DT_PATH_REF
64 %token DT_INCBIN
66 %type <data> propdata
67 %type <data> propdataprefix
68 %type <flags> header
69 %type <flags> headers
70 %type <re> memreserve
71 %type <re> memreserves
72 %type <array> arrayprefix
73 %type <data> bytestring
74 %type <prop> propdef
75 %type <proplist> proplist
76 %type <labelref> dt_ref
78 %type <node> devicetree
79 %type <node> nodedef
80 %type <node> subnode
81 %type <nodelist> subnodes
83 %type <integer> integer_prim
84 %type <integer> integer_unary
85 %type <integer> integer_mul
86 %type <integer> integer_add
87 %type <integer> integer_shift
88 %type <integer> integer_rela
89 %type <integer> integer_eq
90 %type <integer> integer_bitand
91 %type <integer> integer_bitxor
92 %type <integer> integer_bitor
93 %type <integer> integer_and
94 %type <integer> integer_or
95 %type <integer> integer_trinary
96 %type <integer> integer_expr
100 sourcefile:
101 headers memreserves devicetree
103 parser_output = build_dt_info($1, $2, $3,
104 guess_boot_cpuid($3));
108 header:
109 DT_V1 ';'
111 $$ = DTSF_V1;
113 | DT_V1 ';' DT_PLUGIN ';'
115 $$ = DTSF_V1 | DTSF_PLUGIN;
119 headers:
120 header
121 | header headers
123 if ($2 != $1)
124 ERROR(&@2, "Header flags don't match earlier ones");
125 $$ = $1;
129 memreserves:
130 /* empty */
132 $$ = NULL;
134 | memreserve memreserves
136 $$ = chain_reserve_entry($1, $2);
140 memreserve:
141 DT_MEMRESERVE integer_prim integer_prim ';'
143 $$ = build_reserve_entry($2, $3);
145 | DT_LABEL memreserve
147 add_label(&$2->labels, $1);
148 $$ = $2;
152 dt_ref: DT_LABEL_REF | DT_PATH_REF;
154 devicetree:
155 '/' nodedef
157 $$ = name_node($2, "");
159 | devicetree '/' nodedef
161 $$ = merge_nodes($1, $3);
163 | dt_ref nodedef
166 * We rely on the rule being always:
167 * versioninfo plugindecl memreserves devicetree
168 * so $-1 is what we want (plugindecl)
170 if (!($<flags>-1 & DTSF_PLUGIN))
171 ERROR(&@2, "Label or path %s not found", $1);
172 $$ = add_orphan_node(
173 name_node(build_node(NULL, NULL, NULL),
174 ""),
175 $2, $1);
177 | devicetree DT_LABEL dt_ref nodedef
179 struct node *target = get_node_by_ref($1, $3);
181 if (target) {
182 add_label(&target->labels, $2);
183 merge_nodes(target, $4);
184 } else
185 ERROR(&@3, "Label or path %s not found", $3);
186 $$ = $1;
188 | devicetree DT_PATH_REF nodedef
191 * We rely on the rule being always:
192 * versioninfo plugindecl memreserves devicetree
193 * so $-1 is what we want (plugindecl)
195 if ($<flags>-1 & DTSF_PLUGIN) {
196 add_orphan_node($1, $3, $2);
197 } else {
198 struct node *target = get_node_by_ref($1, $2);
200 if (target)
201 merge_nodes(target, $3);
202 else
203 ERROR(&@2, "Label or path %s not found", $2);
205 $$ = $1;
207 | devicetree DT_LABEL_REF nodedef
209 struct node *target = get_node_by_ref($1, $2);
211 if (target) {
212 merge_nodes(target, $3);
213 } else {
215 * We rely on the rule being always:
216 * versioninfo plugindecl memreserves devicetree
217 * so $-1 is what we want (plugindecl)
219 if ($<flags>-1 & DTSF_PLUGIN)
220 add_orphan_node($1, $3, $2);
221 else
222 ERROR(&@2, "Label or path %s not found", $2);
224 $$ = $1;
226 | devicetree DT_DEL_NODE dt_ref ';'
228 struct node *target = get_node_by_ref($1, $3);
230 if (target)
231 delete_node(target);
232 else
233 ERROR(&@3, "Label or path %s not found", $3);
236 $$ = $1;
238 | devicetree DT_OMIT_NO_REF dt_ref ';'
240 struct node *target = get_node_by_ref($1, $3);
242 if (target)
243 omit_node_if_unused(target);
244 else
245 ERROR(&@3, "Label or path %s not found", $3);
248 $$ = $1;
252 nodedef:
253 '{' proplist subnodes '}' ';'
255 $$ = build_node($2, $3, &@$);
259 proplist:
260 /* empty */
262 $$ = NULL;
264 | proplist propdef
266 $$ = chain_property($2, $1);
270 propdef:
271 DT_PROPNODENAME '=' propdata ';'
273 $$ = build_property($1, $3, &@$);
275 | DT_PROPNODENAME ';'
277 $$ = build_property($1, empty_data, &@$);
279 | DT_DEL_PROP DT_PROPNODENAME ';'
281 $$ = build_property_delete($2);
283 | DT_LABEL propdef
285 add_label(&$2->labels, $1);
286 $$ = $2;
290 propdata:
291 propdataprefix DT_STRING
293 $$ = data_merge($1, $2);
295 | propdataprefix arrayprefix '>'
297 $$ = data_merge($1, $2.data);
299 | propdataprefix '[' bytestring ']'
301 $$ = data_merge($1, $3);
303 | propdataprefix dt_ref
305 $1 = data_add_marker($1, TYPE_STRING, $2);
306 $$ = data_add_marker($1, REF_PATH, $2);
308 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
310 FILE *f = srcfile_relative_open($4.val, NULL);
311 struct data d;
313 if ($6 != 0)
314 if (fseek(f, $6, SEEK_SET) != 0)
315 die("Couldn't seek to offset %llu in \"%s\": %s",
316 (unsigned long long)$6, $4.val,
317 strerror(errno));
319 d = data_copy_file(f, $8);
321 $$ = data_merge($1, d);
322 fclose(f);
324 | propdataprefix DT_INCBIN '(' DT_STRING ')'
326 FILE *f = srcfile_relative_open($4.val, NULL);
327 struct data d = empty_data;
329 d = data_copy_file(f, -1);
331 $$ = data_merge($1, d);
332 fclose(f);
334 | propdata DT_LABEL
336 $$ = data_add_marker($1, LABEL, $2);
340 propdataprefix:
341 /* empty */
343 $$ = empty_data;
345 | propdata ','
347 $$ = $1;
349 | propdataprefix DT_LABEL
351 $$ = data_add_marker($1, LABEL, $2);
355 arrayprefix:
356 DT_BITS DT_LITERAL '<'
358 unsigned long long bits;
359 enum markertype type = TYPE_UINT32;
361 bits = $2;
363 switch (bits) {
364 case 8: type = TYPE_UINT8; break;
365 case 16: type = TYPE_UINT16; break;
366 case 32: type = TYPE_UINT32; break;
367 case 64: type = TYPE_UINT64; break;
368 default:
369 ERROR(&@2, "Array elements must be"
370 " 8, 16, 32 or 64-bits");
371 bits = 32;
374 $$.data = data_add_marker(empty_data, type, NULL);
375 $$.bits = bits;
377 | '<'
379 $$.data = data_add_marker(empty_data, TYPE_UINT32, NULL);
380 $$.bits = 32;
382 | arrayprefix integer_prim
384 if ($1.bits < 64) {
385 uint64_t mask = (1ULL << $1.bits) - 1;
387 * Bits above mask must either be all zero
388 * (positive within range of mask) or all one
389 * (negative and sign-extended). The second
390 * condition is true if when we set all bits
391 * within the mask to one (i.e. | in the
392 * mask), all bits are one.
394 if (($2 > mask) && (($2 | mask) != -1ULL))
395 ERROR(&@2, "Value out of range for"
396 " %d-bit array element", $1.bits);
399 $$.data = data_append_integer($1.data, $2, $1.bits);
401 | arrayprefix dt_ref
403 uint64_t val = ~0ULL >> (64 - $1.bits);
405 if ($1.bits == 32)
406 $1.data = data_add_marker($1.data,
407 REF_PHANDLE,
408 $2);
409 else
410 ERROR(&@2, "References are only allowed in "
411 "arrays with 32-bit elements.");
413 $$.data = data_append_integer($1.data, val, $1.bits);
415 | arrayprefix DT_LABEL
417 $$.data = data_add_marker($1.data, LABEL, $2);
421 integer_prim:
422 DT_LITERAL
423 | DT_CHAR_LITERAL
424 | '(' integer_expr ')'
426 $$ = $2;
430 integer_expr:
431 integer_trinary
434 integer_trinary:
435 integer_or
436 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
439 integer_or:
440 integer_and
441 | integer_or DT_OR integer_and { $$ = $1 || $3; }
444 integer_and:
445 integer_bitor
446 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
449 integer_bitor:
450 integer_bitxor
451 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
454 integer_bitxor:
455 integer_bitand
456 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
459 integer_bitand:
460 integer_eq
461 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
464 integer_eq:
465 integer_rela
466 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
467 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
470 integer_rela:
471 integer_shift
472 | integer_rela '<' integer_shift { $$ = $1 < $3; }
473 | integer_rela '>' integer_shift { $$ = $1 > $3; }
474 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
475 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
478 integer_shift:
479 integer_shift DT_LSHIFT integer_add { $$ = $1 << $3; }
480 | integer_shift DT_RSHIFT integer_add { $$ = $1 >> $3; }
481 | integer_add
484 integer_add:
485 integer_add '+' integer_mul { $$ = $1 + $3; }
486 | integer_add '-' integer_mul { $$ = $1 - $3; }
487 | integer_mul
490 integer_mul:
491 integer_mul '*' integer_unary { $$ = $1 * $3; }
492 | integer_mul '/' integer_unary
494 if ($3 != 0) {
495 $$ = $1 / $3;
496 } else {
497 ERROR(&@$, "Division by zero");
498 $$ = 0;
501 | integer_mul '%' integer_unary
503 if ($3 != 0) {
504 $$ = $1 % $3;
505 } else {
506 ERROR(&@$, "Division by zero");
507 $$ = 0;
510 | integer_unary
513 integer_unary:
514 integer_prim
515 | '-' integer_unary { $$ = -$2; }
516 | '~' integer_unary { $$ = ~$2; }
517 | '!' integer_unary { $$ = !$2; }
520 bytestring:
521 /* empty */
523 $$ = data_add_marker(empty_data, TYPE_UINT8, NULL);
525 | bytestring DT_BYTE
527 $$ = data_append_byte($1, $2);
529 | bytestring DT_LABEL
531 $$ = data_add_marker($1, LABEL, $2);
535 subnodes:
536 /* empty */
538 $$ = NULL;
540 | subnode subnodes
542 $$ = chain_node($1, $2);
544 | subnode propdef
546 ERROR(&@2, "Properties must precede subnodes");
547 YYERROR;
551 subnode:
552 DT_PROPNODENAME nodedef
554 $$ = name_node($2, $1);
556 | DT_DEL_NODE DT_PROPNODENAME ';'
558 $$ = name_node(build_node_delete(&@$), $2);
560 | DT_OMIT_NO_REF subnode
562 $$ = omit_node_if_unused($2);
564 | DT_LABEL subnode
566 add_label(&$2->labels, $1);
567 $$ = $2;
573 void yyerror(char const *s)
575 ERROR(&yylloc, "%s", s);