drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / scripts / dtc / dtc-parser.y
blob4d5eece526243460203157464e3cd75f781e50e7
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;
27 static bool is_ref_relative(const char *ref)
29 return ref[0] != '/' && strchr(&ref[1], '/');
34 %union {
35 char *propnodename;
36 char *labelref;
37 uint8_t byte;
38 struct data data;
40 struct {
41 struct data data;
42 int bits;
43 } array;
45 struct property *prop;
46 struct property *proplist;
47 struct node *node;
48 struct node *nodelist;
49 struct reserve_info *re;
50 uint64_t integer;
51 unsigned int flags;
54 %token DT_V1
55 %token DT_PLUGIN
56 %token DT_MEMRESERVE
57 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
58 %token DT_BITS
59 %token DT_DEL_PROP
60 %token DT_DEL_NODE
61 %token DT_OMIT_NO_REF
62 %token <propnodename> DT_PROPNODENAME
63 %token <integer> DT_LITERAL
64 %token <integer> DT_CHAR_LITERAL
65 %token <byte> DT_BYTE
66 %token <data> DT_STRING
67 %token <labelref> DT_LABEL
68 %token <labelref> DT_LABEL_REF
69 %token <labelref> DT_PATH_REF
70 %token DT_INCBIN
72 %type <data> propdata
73 %type <data> propdataprefix
74 %type <flags> header
75 %type <flags> headers
76 %type <re> memreserve
77 %type <re> memreserves
78 %type <array> arrayprefix
79 %type <data> bytestring
80 %type <prop> propdef
81 %type <proplist> proplist
82 %type <labelref> dt_ref
84 %type <node> devicetree
85 %type <node> nodedef
86 %type <node> subnode
87 %type <nodelist> subnodes
89 %type <integer> integer_prim
90 %type <integer> integer_unary
91 %type <integer> integer_mul
92 %type <integer> integer_add
93 %type <integer> integer_shift
94 %type <integer> integer_rela
95 %type <integer> integer_eq
96 %type <integer> integer_bitand
97 %type <integer> integer_bitxor
98 %type <integer> integer_bitor
99 %type <integer> integer_and
100 %type <integer> integer_or
101 %type <integer> integer_trinary
102 %type <integer> integer_expr
106 sourcefile:
107 headers memreserves devicetree
109 parser_output = build_dt_info($1, $2, $3,
110 guess_boot_cpuid($3));
114 header:
115 DT_V1 ';'
117 $$ = DTSF_V1;
119 | DT_V1 ';' DT_PLUGIN ';'
121 $$ = DTSF_V1 | DTSF_PLUGIN;
125 headers:
126 header
127 | header headers
129 if ($2 != $1)
130 ERROR(&@2, "Header flags don't match earlier ones");
131 $$ = $1;
135 memreserves:
136 /* empty */
138 $$ = NULL;
140 | memreserve memreserves
142 $$ = chain_reserve_entry($1, $2);
146 memreserve:
147 DT_MEMRESERVE integer_prim integer_prim ';'
149 $$ = build_reserve_entry($2, $3);
151 | DT_LABEL memreserve
153 add_label(&$2->labels, $1);
154 $$ = $2;
158 dt_ref: DT_LABEL_REF | DT_PATH_REF;
160 devicetree:
161 '/' nodedef
163 $$ = name_node($2, "");
165 | devicetree '/' nodedef
167 $$ = merge_nodes($1, $3);
169 | dt_ref nodedef
172 * We rely on the rule being always:
173 * versioninfo plugindecl memreserves devicetree
174 * so $-1 is what we want (plugindecl)
176 if (!($<flags>-1 & DTSF_PLUGIN))
177 ERROR(&@2, "Label or path %s not found", $1);
178 else if (is_ref_relative($1))
179 ERROR(&@2, "Label-relative reference %s not supported in plugin", $1);
180 $$ = add_orphan_node(
181 name_node(build_node(NULL, NULL, NULL),
182 ""),
183 $2, $1);
185 | devicetree DT_LABEL dt_ref nodedef
187 struct node *target = get_node_by_ref($1, $3);
189 if (($<flags>-1 & DTSF_PLUGIN) && is_ref_relative($3))
190 ERROR(&@2, "Label-relative reference %s not supported in plugin", $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 if (is_ref_relative($2))
208 ERROR(&@2, "Label-relative reference %s not supported in plugin", $2);
209 add_orphan_node($1, $3, $2);
210 } else {
211 struct node *target = get_node_by_ref($1, $2);
213 if (target)
214 merge_nodes(target, $3);
215 else
216 ERROR(&@2, "Label or path %s not found", $2);
218 $$ = $1;
220 | devicetree DT_LABEL_REF nodedef
222 struct node *target = get_node_by_ref($1, $2);
224 if (target) {
225 merge_nodes(target, $3);
226 } else {
228 * We rely on the rule being always:
229 * versioninfo plugindecl memreserves devicetree
230 * so $-1 is what we want (plugindecl)
232 if ($<flags>-1 & DTSF_PLUGIN)
233 add_orphan_node($1, $3, $2);
234 else
235 ERROR(&@2, "Label or path %s not found", $2);
237 $$ = $1;
239 | devicetree DT_DEL_NODE dt_ref ';'
241 struct node *target = get_node_by_ref($1, $3);
243 if (target)
244 delete_node(target);
245 else
246 ERROR(&@3, "Label or path %s not found", $3);
249 $$ = $1;
251 | devicetree DT_OMIT_NO_REF dt_ref ';'
253 struct node *target = get_node_by_ref($1, $3);
255 if (target)
256 omit_node_if_unused(target);
257 else
258 ERROR(&@3, "Label or path %s not found", $3);
261 $$ = $1;
265 nodedef:
266 '{' proplist subnodes '}' ';'
268 $$ = build_node($2, $3, &@$);
272 proplist:
273 /* empty */
275 $$ = NULL;
277 | proplist propdef
279 $$ = chain_property($2, $1);
283 propdef:
284 DT_PROPNODENAME '=' propdata ';'
286 $$ = build_property($1, $3, &@$);
287 free($1);
289 | DT_PROPNODENAME ';'
291 $$ = build_property($1, empty_data, &@$);
292 free($1);
294 | DT_DEL_PROP DT_PROPNODENAME ';'
296 $$ = build_property_delete($2);
297 free($2);
299 | DT_LABEL propdef
301 add_label(&$2->labels, $1);
302 $$ = $2;
306 propdata:
307 propdataprefix DT_STRING
309 $$ = data_merge($1, $2);
311 | propdataprefix arrayprefix '>'
313 $$ = data_merge($1, $2.data);
315 | propdataprefix '[' bytestring ']'
317 $$ = data_merge($1, $3);
319 | propdataprefix dt_ref
321 $1 = data_add_marker($1, TYPE_STRING, $2);
322 $$ = data_add_marker($1, REF_PATH, $2);
324 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
326 FILE *f = srcfile_relative_open($4.val, NULL);
327 struct data d;
329 if ($6 != 0)
330 if (fseek(f, $6, SEEK_SET) != 0)
331 die("Couldn't seek to offset %llu in \"%s\": %s",
332 (unsigned long long)$6, $4.val,
333 strerror(errno));
335 d = data_copy_file(f, $8);
337 $$ = data_merge($1, d);
338 fclose(f);
340 | propdataprefix DT_INCBIN '(' DT_STRING ')'
342 FILE *f = srcfile_relative_open($4.val, NULL);
343 struct data d = empty_data;
345 d = data_copy_file(f, -1);
347 $$ = data_merge($1, d);
348 fclose(f);
350 | propdata DT_LABEL
352 $$ = data_add_marker($1, LABEL, $2);
356 propdataprefix:
357 /* empty */
359 $$ = empty_data;
361 | propdata ','
363 $$ = $1;
365 | propdataprefix DT_LABEL
367 $$ = data_add_marker($1, LABEL, $2);
371 arrayprefix:
372 DT_BITS DT_LITERAL '<'
374 unsigned long long bits;
375 enum markertype type = TYPE_UINT32;
377 bits = $2;
379 switch (bits) {
380 case 8: type = TYPE_UINT8; break;
381 case 16: type = TYPE_UINT16; break;
382 case 32: type = TYPE_UINT32; break;
383 case 64: type = TYPE_UINT64; break;
384 default:
385 ERROR(&@2, "Array elements must be"
386 " 8, 16, 32 or 64-bits");
387 bits = 32;
390 $$.data = data_add_marker(empty_data, type, NULL);
391 $$.bits = bits;
393 | '<'
395 $$.data = data_add_marker(empty_data, TYPE_UINT32, NULL);
396 $$.bits = 32;
398 | arrayprefix integer_prim
400 if ($1.bits < 64) {
401 uint64_t mask = (1ULL << $1.bits) - 1;
403 * Bits above mask must either be all zero
404 * (positive within range of mask) or all one
405 * (negative and sign-extended). The second
406 * condition is true if when we set all bits
407 * within the mask to one (i.e. | in the
408 * mask), all bits are one.
410 if (($2 > mask) && (($2 | mask) != -1ULL)) {
411 char *loc = srcpos_string(&@2);
412 fprintf(stderr,
413 "WARNING: %s: Value 0x%016" PRIx64
414 " truncated to 0x%0*" PRIx64 "\n",
415 loc, $2, $1.bits / 4, ($2 & mask));
416 free(loc);
420 $$.data = data_append_integer($1.data, $2, $1.bits);
422 | arrayprefix dt_ref
424 uint64_t val = ~0ULL >> (64 - $1.bits);
426 if ($1.bits == 32)
427 $1.data = data_add_marker($1.data,
428 REF_PHANDLE,
429 $2);
430 else
431 ERROR(&@2, "References are only allowed in "
432 "arrays with 32-bit elements.");
434 $$.data = data_append_integer($1.data, val, $1.bits);
436 | arrayprefix DT_LABEL
438 $$.data = data_add_marker($1.data, LABEL, $2);
442 integer_prim:
443 DT_LITERAL
444 | DT_CHAR_LITERAL
445 | '(' integer_expr ')'
447 $$ = $2;
451 integer_expr:
452 integer_trinary
455 integer_trinary:
456 integer_or
457 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
460 integer_or:
461 integer_and
462 | integer_or DT_OR integer_and { $$ = $1 || $3; }
465 integer_and:
466 integer_bitor
467 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
470 integer_bitor:
471 integer_bitxor
472 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
475 integer_bitxor:
476 integer_bitand
477 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
480 integer_bitand:
481 integer_eq
482 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
485 integer_eq:
486 integer_rela
487 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
488 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
491 integer_rela:
492 integer_shift
493 | integer_rela '<' integer_shift { $$ = $1 < $3; }
494 | integer_rela '>' integer_shift { $$ = $1 > $3; }
495 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
496 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
499 integer_shift:
500 integer_shift DT_LSHIFT integer_add { $$ = ($3 < 64) ? ($1 << $3) : 0; }
501 | integer_shift DT_RSHIFT integer_add { $$ = ($3 < 64) ? ($1 >> $3) : 0; }
502 | integer_add
505 integer_add:
506 integer_add '+' integer_mul { $$ = $1 + $3; }
507 | integer_add '-' integer_mul { $$ = $1 - $3; }
508 | integer_mul
511 integer_mul:
512 integer_mul '*' integer_unary { $$ = $1 * $3; }
513 | integer_mul '/' integer_unary
515 if ($3 != 0) {
516 $$ = $1 / $3;
517 } else {
518 ERROR(&@$, "Division by zero");
519 $$ = 0;
522 | integer_mul '%' integer_unary
524 if ($3 != 0) {
525 $$ = $1 % $3;
526 } else {
527 ERROR(&@$, "Division by zero");
528 $$ = 0;
531 | integer_unary
534 integer_unary:
535 integer_prim
536 | '-' integer_unary { $$ = -$2; }
537 | '~' integer_unary { $$ = ~$2; }
538 | '!' integer_unary { $$ = !$2; }
541 bytestring:
542 /* empty */
544 $$ = data_add_marker(empty_data, TYPE_UINT8, NULL);
546 | bytestring DT_BYTE
548 $$ = data_append_byte($1, $2);
550 | bytestring DT_LABEL
552 $$ = data_add_marker($1, LABEL, $2);
556 subnodes:
557 /* empty */
559 $$ = NULL;
561 | subnode subnodes
563 $$ = chain_node($1, $2);
565 | subnode propdef
567 ERROR(&@2, "Properties must precede subnodes");
568 YYERROR;
572 subnode:
573 DT_PROPNODENAME nodedef
575 $$ = name_node($2, $1);
576 free($1);
578 | DT_DEL_NODE DT_PROPNODENAME ';'
580 $$ = name_node(build_node_delete(&@$), $2);
581 free($2);
583 | DT_OMIT_NO_REF subnode
585 $$ = omit_node_if_unused($2);
587 | DT_LABEL subnode
589 add_label(&$2->labels, $1);
590 $$ = $2;
596 void yyerror(char const *s)
598 ERROR(&yylloc, "%s", s);