1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
14 extern int yylex(void);
15 extern void yyerror(char const *s);
16 #define ERROR(loc, ...) \
18 srcpos_error((loc), "Error", __VA_ARGS__); \
19 treesource_error = true; \
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], '/');
45 struct property *prop;
46 struct property *proplist;
48 struct node *nodelist;
49 struct reserve_info *re;
57 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
62 %token <propnodename> DT_PROPNODENAME
63 %token <integer> DT_LITERAL
64 %token <integer> DT_CHAR_LITERAL
66 %token <data> DT_STRING
67 %token <labelref> DT_LABEL
68 %token <labelref> DT_LABEL_REF
69 %token <labelref> DT_PATH_REF
73 %type <data> propdataprefix
77 %type <re> memreserves
78 %type <array> arrayprefix
79 %type <data> bytestring
81 %type <proplist> proplist
82 %type <labelref> dt_ref
84 %type <node> devicetree
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
107 headers memreserves devicetree
109 parser_output = build_dt_info($1, $2, $3,
110 guess_boot_cpuid($3));
119 | DT_V1 ';' DT_PLUGIN ';'
121 $$ = DTSF_V1 | DTSF_PLUGIN;
130 ERROR(&@2, "Header flags don't match earlier ones");
140 | memreserve memreserves
142 $$ = chain_reserve_entry($1, $2);
147 DT_MEMRESERVE integer_prim integer_prim ';'
149 $$ = build_reserve_entry($2, $3);
151 | DT_LABEL memreserve
153 add_label(&$2->labels, $1);
158 dt_ref: DT_LABEL_REF | DT_PATH_REF;
163 $$ = name_node($2, "");
165 | devicetree '/' nodedef
167 $$ = merge_nodes($1, $3);
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),
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);
193 add_label(&target->labels, $2);
194 merge_nodes(target, $4);
196 ERROR(&@3, "Label or path %s not found", $3);
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);
211 struct node *target = get_node_by_ref($1, $2);
214 merge_nodes(target, $3);
216 ERROR(&@2, "Label or path %s not found", $2);
220 | devicetree DT_LABEL_REF nodedef
222 struct node *target = get_node_by_ref($1, $2);
225 merge_nodes(target, $3);
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);
235 ERROR(&@2, "Label or path %s not found", $2);
239 | devicetree DT_DEL_NODE dt_ref ';'
241 struct node *target = get_node_by_ref($1, $3);
246 ERROR(&@3, "Label or path %s not found", $3);
251 | devicetree DT_OMIT_NO_REF dt_ref ';'
253 struct node *target = get_node_by_ref($1, $3);
256 omit_node_if_unused(target);
258 ERROR(&@3, "Label or path %s not found", $3);
266 '{' proplist subnodes '}' ';'
268 $$ = build_node($2, $3, &@$);
279 $$ = chain_property($2, $1);
284 DT_PROPNODENAME '=' propdata ';'
286 $$ = build_property($1, $3, &@$);
288 | DT_PROPNODENAME ';'
290 $$ = build_property($1, empty_data, &@$);
292 | DT_DEL_PROP DT_PROPNODENAME ';'
294 $$ = build_property_delete($2);
298 add_label(&$2->labels, $1);
304 propdataprefix DT_STRING
306 $$ = data_merge($1, $2);
308 | propdataprefix arrayprefix '>'
310 $$ = data_merge($1, $2.data);
312 | propdataprefix '[' bytestring ']'
314 $$ = data_merge($1, $3);
316 | propdataprefix dt_ref
318 $1 = data_add_marker($1, TYPE_STRING, $2);
319 $$ = data_add_marker($1, REF_PATH, $2);
321 | propdataprefix DT_INCBIN '(' DT_STRING ',' integer_prim ',' integer_prim ')'
323 FILE *f = srcfile_relative_open($4.val, NULL);
327 if (fseek(f, $6, SEEK_SET) != 0)
328 die("Couldn't seek to offset %llu in \"%s\": %s",
329 (unsigned long long)$6, $4.val,
332 d = data_copy_file(f, $8);
334 $$ = data_merge($1, d);
337 | propdataprefix DT_INCBIN '(' DT_STRING ')'
339 FILE *f = srcfile_relative_open($4.val, NULL);
340 struct data d = empty_data;
342 d = data_copy_file(f, -1);
344 $$ = data_merge($1, d);
349 $$ = data_add_marker($1, LABEL, $2);
362 | propdataprefix DT_LABEL
364 $$ = data_add_marker($1, LABEL, $2);
369 DT_BITS DT_LITERAL '<'
371 unsigned long long bits;
372 enum markertype type = TYPE_UINT32;
377 case 8: type = TYPE_UINT8; break;
378 case 16: type = TYPE_UINT16; break;
379 case 32: type = TYPE_UINT32; break;
380 case 64: type = TYPE_UINT64; break;
382 ERROR(&@2, "Array elements must be"
383 " 8, 16, 32 or 64-bits");
387 $$.data = data_add_marker(empty_data, type, NULL);
392 $$.data = data_add_marker(empty_data, TYPE_UINT32, NULL);
395 | arrayprefix integer_prim
398 uint64_t mask = (1ULL << $1.bits) - 1;
400 * Bits above mask must either be all zero
401 * (positive within range of mask) or all one
402 * (negative and sign-extended). The second
403 * condition is true if when we set all bits
404 * within the mask to one (i.e. | in the
405 * mask), all bits are one.
407 if (($2 > mask) && (($2 | mask) != -1ULL)) {
408 char *loc = srcpos_string(&@2);
410 "WARNING: %s: Value 0x%016" PRIx64
411 " truncated to 0x%0*" PRIx64 "\n",
412 loc, $2, $1.bits / 4, ($2 & mask));
417 $$.data = data_append_integer($1.data, $2, $1.bits);
421 uint64_t val = ~0ULL >> (64 - $1.bits);
424 $1.data = data_add_marker($1.data,
428 ERROR(&@2, "References are only allowed in "
429 "arrays with 32-bit elements.");
431 $$.data = data_append_integer($1.data, val, $1.bits);
433 | arrayprefix DT_LABEL
435 $$.data = data_add_marker($1.data, LABEL, $2);
442 | '(' integer_expr ')'
454 | integer_or '?' integer_expr ':' integer_trinary { $$ = $1 ? $3 : $5; }
459 | integer_or DT_OR integer_and { $$ = $1 || $3; }
464 | integer_and DT_AND integer_bitor { $$ = $1 && $3; }
469 | integer_bitor '|' integer_bitxor { $$ = $1 | $3; }
474 | integer_bitxor '^' integer_bitand { $$ = $1 ^ $3; }
479 | integer_bitand '&' integer_eq { $$ = $1 & $3; }
484 | integer_eq DT_EQ integer_rela { $$ = $1 == $3; }
485 | integer_eq DT_NE integer_rela { $$ = $1 != $3; }
490 | integer_rela '<' integer_shift { $$ = $1 < $3; }
491 | integer_rela '>' integer_shift { $$ = $1 > $3; }
492 | integer_rela DT_LE integer_shift { $$ = $1 <= $3; }
493 | integer_rela DT_GE integer_shift { $$ = $1 >= $3; }
497 integer_shift DT_LSHIFT integer_add { $$ = ($3 < 64) ? ($1 << $3) : 0; }
498 | integer_shift DT_RSHIFT integer_add { $$ = ($3 < 64) ? ($1 >> $3) : 0; }
503 integer_add '+' integer_mul { $$ = $1 + $3; }
504 | integer_add '-' integer_mul { $$ = $1 - $3; }
509 integer_mul '*' integer_unary { $$ = $1 * $3; }
510 | integer_mul '/' integer_unary
515 ERROR(&@$, "Division by zero");
519 | integer_mul '%' integer_unary
524 ERROR(&@$, "Division by zero");
533 | '-' integer_unary { $$ = -$2; }
534 | '~' integer_unary { $$ = ~$2; }
535 | '!' integer_unary { $$ = !$2; }
541 $$ = data_add_marker(empty_data, TYPE_UINT8, NULL);
545 $$ = data_append_byte($1, $2);
547 | bytestring DT_LABEL
549 $$ = data_add_marker($1, LABEL, $2);
560 $$ = chain_node($1, $2);
564 ERROR(&@2, "Properties must precede subnodes");
570 DT_PROPNODENAME nodedef
572 $$ = name_node($2, $1);
574 | DT_DEL_NODE DT_PROPNODENAME ';'
576 $$ = name_node(build_node_delete(&@$), $2);
578 | DT_OMIT_NO_REF subnode
580 $$ = omit_node_if_unused($2);
584 add_label(&$2->labels, $1);
591 void yyerror(char const *s)
593 ERROR(&yylloc, "%s", s);