1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
10 extern int yyparse(void);
11 extern YYLTYPE yylloc
;
13 struct dt_info
*parser_output
;
14 bool treesource_error
;
16 struct dt_info
*dt_from_source(const char *fname
)
19 treesource_error
= false;
22 yyin
= current_srcfile
->f
;
23 yylloc
.file
= current_srcfile
;
26 die("Unable to parse input tree\n");
29 die("Syntax error parsing input tree\n");
34 static void write_prefix(FILE *f
, int level
)
38 for (i
= 0; i
< level
; i
++)
42 static bool isstring(char c
)
44 return (isprint((unsigned char)c
)
46 || strchr("\a\b\t\n\v\f\r", c
));
49 static void write_propval_string(FILE *f
, const char *s
, size_t len
)
51 const char *end
= s
+ len
- 1;
93 if (isprint((unsigned char)c
))
96 fprintf(f
, "\\x%02"PRIx8
, c
);
102 static void write_propval_int(FILE *f
, const char *p
, size_t len
, size_t width
)
104 const char *end
= p
+ len
;
105 assert(len
% width
== 0);
107 for (; p
< end
; p
+= width
) {
110 fprintf(f
, "%02"PRIx8
, *(const uint8_t*)p
);
113 fprintf(f
, "0x%02"PRIx16
, fdt16_to_cpu(*(const fdt16_t
*)p
));
116 fprintf(f
, "0x%02"PRIx32
, fdt32_to_cpu(*(const fdt32_t
*)p
));
119 fprintf(f
, "0x%02"PRIx64
, fdt64_to_cpu(*(const fdt64_t
*)p
));
127 static bool has_data_type_information(struct marker
*m
)
129 return m
->type
>= TYPE_UINT8
;
132 static struct marker
*next_type_marker(struct marker
*m
)
134 while (m
&& !has_data_type_information(m
))
139 size_t type_marker_length(struct marker
*m
)
141 struct marker
*next
= next_type_marker(m
->next
);
144 return next
->offset
- m
->offset
;
148 static const char *delim_start
[] = {
150 [TYPE_UINT16
] = "/bits/ 16 <",
152 [TYPE_UINT64
] = "/bits/ 64 <",
155 static const char *delim_end
[] = {
163 static enum markertype
guess_value_type(struct property
*prop
)
165 int len
= prop
->val
.len
;
166 const char *p
= prop
->val
.val
;
167 struct marker
*m
= prop
->val
.markers
;
168 int nnotstring
= 0, nnul
= 0;
169 int nnotstringlbl
= 0, nnotcelllbl
= 0;
172 for (i
= 0; i
< len
; i
++) {
173 if (! isstring(p
[i
]))
179 for_each_marker_of_type(m
, LABEL
) {
180 if ((m
->offset
> 0) && (prop
->val
.val
[m
->offset
- 1] != '\0'))
182 if ((m
->offset
% sizeof(cell_t
)) != 0)
186 if ((p
[len
-1] == '\0') && (nnotstring
== 0) && (nnul
< (len
-nnul
))
187 && (nnotstringlbl
== 0)) {
189 } else if (((len
% sizeof(cell_t
)) == 0) && (nnotcelllbl
== 0)) {
196 static void write_propval(FILE *f
, struct property
*prop
)
198 size_t len
= prop
->val
.len
;
199 struct marker
*m
= prop
->val
.markers
;
200 struct marker dummy_marker
;
201 enum markertype emit_type
= TYPE_NONE
;
207 srcstr
= srcpos_string_first(prop
->srcpos
, annotate
);
209 fprintf(f
, " /* %s */", srcstr
);
219 if (!next_type_marker(m
)) {
220 /* data type information missing, need to guess */
221 dummy_marker
.type
= guess_value_type(prop
);
222 dummy_marker
.next
= prop
->val
.markers
;
223 dummy_marker
.offset
= 0;
224 dummy_marker
.ref
= NULL
;
229 size_t chunk_len
= (m
->next
? m
->next
->offset
: len
) - m
->offset
;
230 size_t data_len
= type_marker_length(m
) ? : len
- m
->offset
;
231 const char *p
= &prop
->val
.val
[m
->offset
];
233 if (has_data_type_information(m
)) {
235 fprintf(f
, " %s", delim_start
[emit_type
]);
236 } else if (m
->type
== LABEL
)
237 fprintf(f
, " %s:", m
->ref
);
241 if (emit_type
== TYPE_NONE
) {
242 assert(chunk_len
== 0);
248 write_propval_int(f
, p
, chunk_len
, 2);
251 write_propval_int(f
, p
, chunk_len
, 4);
254 write_propval_int(f
, p
, chunk_len
, 8);
257 write_propval_string(f
, p
, chunk_len
);
260 write_propval_int(f
, p
, chunk_len
, 1);
263 if (chunk_len
== data_len
) {
264 size_t pos
= m
->offset
+ chunk_len
;
265 fprintf(f
, pos
== len
? "%s" : "%s,",
266 delim_end
[emit_type
] ? : "");
267 emit_type
= TYPE_NONE
;
272 srcstr
= srcpos_string_first(prop
->srcpos
, annotate
);
274 fprintf(f
, " /* %s */", srcstr
);
281 static void write_tree_source_node(FILE *f
, struct node
*tree
, int level
)
283 struct property
*prop
;
288 write_prefix(f
, level
);
289 for_each_label(tree
->labels
, l
)
290 fprintf(f
, "%s: ", l
->label
);
291 if (tree
->name
&& (*tree
->name
))
292 fprintf(f
, "%s {", tree
->name
);
297 srcstr
= srcpos_string_first(tree
->srcpos
, annotate
);
299 fprintf(f
, " /* %s */", srcstr
);
305 for_each_property(tree
, prop
) {
306 write_prefix(f
, level
+1);
307 for_each_label(prop
->labels
, l
)
308 fprintf(f
, "%s: ", l
->label
);
309 fprintf(f
, "%s", prop
->name
);
310 write_propval(f
, prop
);
312 for_each_child(tree
, child
) {
314 write_tree_source_node(f
, child
, level
+1);
316 write_prefix(f
, level
);
319 srcstr
= srcpos_string_last(tree
->srcpos
, annotate
);
321 fprintf(f
, " /* %s */", srcstr
);
328 void dt_to_source(FILE *f
, struct dt_info
*dti
)
330 struct reserve_info
*re
;
332 fprintf(f
, "/dts-v1/;\n\n");
334 for (re
= dti
->reservelist
; re
; re
= re
->next
) {
337 for_each_label(re
->labels
, l
)
338 fprintf(f
, "%s: ", l
->label
);
339 fprintf(f
, "/memreserve/\t0x%016llx 0x%016llx;\n",
340 (unsigned long long)re
->address
,
341 (unsigned long long)re
->size
);
344 write_tree_source_node(f
, dti
->dt
, 0);