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
25 extern int yyparse(void);
26 extern YYLTYPE yylloc;
28 struct dt_info *parser_output;
29 bool treesource_error;
31 struct dt_info *dt_from_source(const char *fname)
34 treesource_error = false;
37 yyin = current_srcfile->f;
38 yylloc.file = current_srcfile;
41 die("Unable to parse input tree\n");
44 die("Syntax error parsing input tree\n");
49 static void write_prefix(FILE *f, int level)
53 for (i = 0; i < level; i++)
57 static bool isstring(char c)
59 return (isprint((unsigned char)c)
61 || strchr("\a\b\t\n\v\f\r", c));
64 static void write_propval_string(FILE *f, const char *s, size_t len)
66 const char *end = s + len - 1;
108 if (isprint((unsigned char)c))
111 fprintf(f, "\\x%02"PRIx8, c);
117 static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
119 const char *end = p + len;
120 assert(len % width == 0);
122 for (; p < end; p += width) {
125 fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
128 fprintf(f, "0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
131 fprintf(f, "0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
134 fprintf(f, "0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
142 static bool has_data_type_information(struct marker *m)
144 return m->type >= TYPE_UINT8;
147 static struct marker *next_type_marker(struct marker *m)
149 while (m && !has_data_type_information(m))
154 size_t type_marker_length(struct marker *m)
156 struct marker *next = next_type_marker(m->next);
159 return next->offset - m->offset;
163 static const char *delim_start[] = {
165 [TYPE_UINT16] = "/bits/ 16 <",
167 [TYPE_UINT64] = "/bits/ 64 <",
170 static const char *delim_end[] = {
178 static enum markertype guess_value_type(struct property *prop)
180 int len = prop->val.len;
181 const char *p = prop->val.val;
182 struct marker *m = prop->val.markers;
183 int nnotstring = 0, nnul = 0;
184 int nnotstringlbl = 0, nnotcelllbl = 0;
187 for (i = 0; i < len; i++) {
188 if (! isstring(p[i]))
194 for_each_marker_of_type(m, LABEL) {
195 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
197 if ((m->offset % sizeof(cell_t)) != 0)
201 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
202 && (nnotstringlbl == 0)) {
204 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
211 static void write_propval(FILE *f, struct property *prop)
213 size_t len = prop->val.len;
214 struct marker *m = prop->val.markers;
215 struct marker dummy_marker;
216 enum markertype emit_type = TYPE_NONE;
222 srcstr = srcpos_string_first(prop->srcpos, annotate);
224 fprintf(f, " /* %s */", srcstr);
234 if (!next_type_marker(m)) {
235 /* data type information missing, need to guess */
236 dummy_marker.type = guess_value_type(prop);
237 dummy_marker.next = prop->val.markers;
238 dummy_marker.offset = 0;
239 dummy_marker.ref = NULL;
244 size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
245 size_t data_len = type_marker_length(m) ? : len - m->offset;
246 const char *p = &prop->val.val[m->offset];
248 if (has_data_type_information(m)) {
250 fprintf(f, " %s", delim_start[emit_type]);
251 } else if (m->type == LABEL)
252 fprintf(f, " %s:", m->ref);
256 if (emit_type == TYPE_NONE) {
257 assert(chunk_len == 0);
263 write_propval_int(f, p, chunk_len, 2);
266 write_propval_int(f, p, chunk_len, 4);
269 write_propval_int(f, p, chunk_len, 8);
272 write_propval_string(f, p, chunk_len);
275 write_propval_int(f, p, chunk_len, 1);
278 if (chunk_len == data_len) {
279 size_t pos = m->offset + chunk_len;
280 fprintf(f, pos == len ? "%s" : "%s,",
281 delim_end[emit_type] ? : "");
282 emit_type = TYPE_NONE;
287 srcstr = srcpos_string_first(prop->srcpos, annotate);
289 fprintf(f, " /* %s */", srcstr);
296 static void write_tree_source_node(FILE *f, struct node *tree, int level)
298 struct property *prop;
303 write_prefix(f, level);
304 for_each_label(tree->labels, l)
305 fprintf(f, "%s: ", l->label);
306 if (tree->name && (*tree->name))
307 fprintf(f, "%s {", tree->name);
312 srcstr = srcpos_string_first(tree->srcpos, annotate);
314 fprintf(f, " /* %s */", srcstr);
320 for_each_property(tree, prop) {
321 write_prefix(f, level+1);
322 for_each_label(prop->labels, l)
323 fprintf(f, "%s: ", l->label);
324 fprintf(f, "%s", prop->name);
325 write_propval(f, prop);
327 for_each_child(tree, child) {
329 write_tree_source_node(f, child, level+1);
331 write_prefix(f, level);
334 srcstr = srcpos_string_last(tree->srcpos, annotate);
336 fprintf(f, " /* %s */", srcstr);
343 void dt_to_source(FILE *f, struct dt_info *dti)
345 struct reserve_info *re;
347 fprintf(f, "/dts-v1/;\n\n");
349 for (re = dti->reservelist; re; re = re->next) {
352 for_each_label(re->labels, l)
353 fprintf(f, "%s: ", l->label);
354 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
355 (unsigned long long)re->address,
356 (unsigned long long)re->size);
359 write_tree_source_node(f, dti->dt, 0);