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
29 extern
int yylex(void);
30 extern
void print_error
(char const *fmt
, ...
);
31 extern
void yyerror(char const *s
);
33 extern
struct boot_info
*the_boot_info
;
34 extern
int treesource_error
;
36 static unsigned long long eval_literal
(const char *s
, int base
, int bits
);
37 static unsigned char eval_char_literal
(const char *s
);
53 struct property
*prop
;
54 struct property
*proplist
;
56 struct node
*nodelist
;
57 struct reserve_info
*re
;
63 %token DT_LSHIFT DT_RSHIFT DT_LE DT_GE DT_EQ DT_NE DT_AND DT_OR
67 %token
<propnodename
> DT_PROPNODENAME
68 %token
<literal
> DT_LITERAL
69 %token
<literal
> DT_CHAR_LITERAL
70 %token
<cbase
> DT_BASE
72 %token
<data
> DT_STRING
73 %token
<labelref
> DT_LABEL
74 %token
<labelref
> DT_REF
78 %type
<data
> propdataprefix
80 %type
<re
> memreserves
81 %type
<array
> arrayprefix
82 %type
<data
> bytestring
84 %type
<proplist
> proplist
86 %type
<node
> devicetree
89 %type
<nodelist
> subnodes
91 %type
<integer
> integer_prim
92 %type
<integer
> integer_unary
93 %type
<integer
> integer_mul
94 %type
<integer
> integer_add
95 %type
<integer
> integer_shift
96 %type
<integer
> integer_rela
97 %type
<integer
> integer_eq
98 %type
<integer
> integer_bitand
99 %type
<integer
> integer_bitxor
100 %type
<integer
> integer_bitor
101 %type
<integer
> integer_and
102 %type
<integer
> integer_or
103 %type
<integer
> integer_trinary
104 %type
<integer
> integer_expr
109 DT_V1
';' memreserves devicetree
111 the_boot_info
= build_boot_info
($3, $4,
112 guess_boot_cpuid
($4));
121 | memreserve memreserves
123 $$
= chain_reserve_entry
($1, $2);
128 DT_MEMRESERVE integer_prim integer_prim
';'
130 $$
= build_reserve_entry
($2, $3);
132 | DT_LABEL memreserve
134 add_label
(&$2->labels
, $1);
142 $$
= name_node
($2, "");
144 | devicetree
'/' nodedef
146 $$
= merge_nodes
($1, $3);
148 | devicetree DT_REF nodedef
150 struct node
*target
= get_node_by_ref
($1, $2);
153 merge_nodes
(target
, $3);
155 print_error
("label or path, '%s', not found", $2);
158 | devicetree DT_DEL_NODE DT_REF
';'
160 struct node
*target
= get_node_by_ref
($1, $3);
163 print_error
("label or path, '%s', not found", $3);
172 '{' proplist subnodes
'}' ';'
174 $$
= build_node
($2, $3);
185 $$
= chain_property
($2, $1);
190 DT_PROPNODENAME
'=' propdata
';'
192 $$
= build_property
($1, $3);
194 | DT_PROPNODENAME
';'
196 $$
= build_property
($1, empty_data
);
198 | DT_DEL_PROP DT_PROPNODENAME
';'
200 $$
= build_property_delete
($2);
204 add_label
(&$2->labels
, $1);
210 propdataprefix DT_STRING
212 $$
= data_merge
($1, $2);
214 | propdataprefix arrayprefix
'>'
216 $$
= data_merge
($1, $2.data
);
218 | propdataprefix
'[' bytestring
']'
220 $$
= data_merge
($1, $3);
222 | propdataprefix DT_REF
224 $$
= data_add_marker
($1, REF_PATH
, $2);
226 | propdataprefix DT_INCBIN
'(' DT_STRING
',' integer_prim
',' integer_prim
')'
228 FILE *f
= srcfile_relative_open
($4.val
, NULL
);
232 if
(fseek
(f
, $6, SEEK_SET
) != 0)
233 print_error
("Couldn't seek to offset %llu in \"%s\": %s",
234 (unsigned long long)$6,
238 d
= data_copy_file
(f
, $8);
240 $$
= data_merge
($1, d
);
243 | propdataprefix DT_INCBIN
'(' DT_STRING
')'
245 FILE *f
= srcfile_relative_open
($4.val
, NULL
);
246 struct data d
= empty_data
;
248 d
= data_copy_file
(f
, -1);
250 $$
= data_merge
($1, d
);
255 $$
= data_add_marker
($1, LABEL
, $2);
268 | propdataprefix DT_LABEL
270 $$
= data_add_marker
($1, LABEL
, $2);
275 DT_BITS DT_LITERAL
'<'
277 $$.data
= empty_data
;
278 $$.bits
= eval_literal
($2, 0, 7);
280 if
(($$.bits
!= 8) &&
285 print_error
("Only 8, 16, 32 and 64-bit elements"
286 " are currently supported");
292 $$.data
= empty_data
;
295 | arrayprefix integer_prim
298 uint64_t mask
= (1ULL << $1.bits
) - 1;
300 * Bits above mask must either be all zero
301 * (positive within range of mask) or all one
302 * (negative and sign-extended). The second
303 * condition is true if when we set all bits
304 * within the mask to one (i.e. | in the
305 * mask), all bits are one.
307 if
(($2 > mask
) && (($2 | mask
) != -1ULL))
309 "integer value out of range "
310 "%016lx (%d bits)", $1.bits
);
313 $$.data
= data_append_integer
($1.data
, $2, $1.bits
);
317 uint64_t val
= ~
0ULL >> (64 - $1.bits
);
320 $1.data
= data_add_marker
($1.data
,
324 print_error
("References are only allowed in "
325 "arrays with 32-bit elements.");
327 $$.data
= data_append_integer
($1.data
, val
, $1.bits
);
329 | arrayprefix DT_LABEL
331 $$.data
= data_add_marker
($1.data
, LABEL
, $2);
338 $$
= eval_literal
($1, 0, 64);
342 $$
= eval_char_literal
($1);
344 |
'(' integer_expr
')'
356 | integer_or
'?' integer_expr
':' integer_trinary
{ $$
= $1 ?
$3 : $5; }
361 | integer_or DT_OR integer_and
{ $$
= $1 ||
$3; }
366 | integer_and DT_AND integer_bitor
{ $$
= $1 && $3; }
371 | integer_bitor
'|' integer_bitxor
{ $$
= $1 |
$3; }
376 | integer_bitxor
'^' integer_bitand
{ $$
= $1 ^
$3; }
381 | integer_bitand
'&' integer_eq
{ $$
= $1 & $3; }
386 | integer_eq DT_EQ integer_rela
{ $$
= $1 == $3; }
387 | integer_eq DT_NE integer_rela
{ $$
= $1 != $3; }
392 | integer_rela
'<' integer_shift
{ $$
= $1 < $3; }
393 | integer_rela
'>' integer_shift
{ $$
= $1 > $3; }
394 | integer_rela DT_LE integer_shift
{ $$
= $1 <= $3; }
395 | integer_rela DT_GE integer_shift
{ $$
= $1 >= $3; }
399 integer_shift DT_LSHIFT integer_add
{ $$
= $1 << $3; }
400 | integer_shift DT_RSHIFT integer_add
{ $$
= $1 >> $3; }
405 integer_add
'+' integer_mul
{ $$
= $1 + $3; }
406 | integer_add
'-' integer_mul
{ $$
= $1 - $3; }
411 integer_mul
'*' integer_unary
{ $$
= $1 * $3; }
412 | integer_mul
'/' integer_unary
{ $$
= $1 / $3; }
413 | integer_mul
'%' integer_unary
{ $$
= $1 %
$3; }
419 |
'-' integer_unary
{ $$
= -$2; }
420 |
'~' integer_unary
{ $$
= ~
$2; }
421 |
'!' integer_unary
{ $$
= !$2; }
431 $$
= data_append_byte
($1, $2);
433 | bytestring DT_LABEL
435 $$
= data_add_marker
($1, LABEL
, $2);
446 $$
= chain_node
($1, $2);
450 print_error
("syntax error: properties must precede subnodes");
456 DT_PROPNODENAME nodedef
458 $$
= name_node
($2, $1);
460 | DT_DEL_NODE DT_PROPNODENAME
';'
462 $$
= name_node
(build_node_delete
(), $2);
466 add_label
(&$2->labels
, $1);
473 void print_error
(char const *fmt
, ...
)
478 srcpos_verror
(&yylloc, fmt
, va
);
481 treesource_error
= 1;
484 void yyerror(char const *s
) {
485 print_error
("%s", s
);
488 static unsigned long long eval_literal
(const char *s
, int base
, int bits
)
490 unsigned long long val
;
494 val
= strtoull
(s
, &e
, base
);
496 size_t uls
= strspn
(e
, "UL");
498 print_error
("bad characters in literal");
500 if
((errno
== ERANGE
)
501 ||
((bits
< 64) && (val
>= (1ULL << bits
))))
502 print_error
("literal out of range");
504 print_error
("bad literal");
508 static unsigned char eval_char_literal
(const char *s
)
515 print_error
("empty character literal");
520 * If the first character in the character literal is a \ then process
521 * the remaining characters as an escape encoding. If the first
522 * character is neither an escape or a terminator it should be the only
523 * character in the literal and will be returned.
526 c
= get_escape_char
(s
, &i
);
529 print_error
("malformed character literal");