treesource.c 7.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation.  2005.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *                                                                   USA
 */

#include "dtc.h"
#include "srcpos.h"

extern FILE *yyin;
extern int yyparse(void);
26
extern YYLTYPE yylloc;
27

28
struct dt_info *parser_output;
29
bool treesource_error;
30

31
struct dt_info *dt_from_source(const char *fname)
32
{
33
	parser_output = NULL;
34
	treesource_error = false;
35

36 37
	srcfile_push(fname);
	yyin = current_srcfile->f;
38
	yylloc.file = current_srcfile;
39 40

	if (yyparse() != 0)
41
		die("Unable to parse input tree\n");
42

43 44
	if (treesource_error)
		die("Syntax error parsing input tree\n");
45

46
	return parser_output;
47 48 49 50 51 52 53 54 55 56
}

static void write_prefix(FILE *f, int level)
{
	int i;

	for (i = 0; i < level; i++)
		fputc('\t', f);
}

57
static bool isstring(char c)
58
{
59
	return (isprint((unsigned char)c)
60 61 62 63
		|| (c == '\0')
		|| strchr("\a\b\t\n\v\f\r", c));
}

64
static void write_propval_string(FILE *f, const char *s, size_t len)
65
{
66
	const char *end = s + len - 1;
67 68 69 70

	if (!len)
		return;

71
	assert(*end == '\0');
72

73
	fprintf(f, "\"");
74 75
	while (s < end) {
		char c = *s++;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
		switch (c) {
		case '\a':
			fprintf(f, "\\a");
			break;
		case '\b':
			fprintf(f, "\\b");
			break;
		case '\t':
			fprintf(f, "\\t");
			break;
		case '\n':
			fprintf(f, "\\n");
			break;
		case '\v':
			fprintf(f, "\\v");
			break;
		case '\f':
			fprintf(f, "\\f");
			break;
		case '\r':
			fprintf(f, "\\r");
			break;
		case '\\':
			fprintf(f, "\\\\");
			break;
		case '\"':
			fprintf(f, "\\\"");
			break;
		case '\0':
105
			fprintf(f, "\\0");
106 107
			break;
		default:
108
			if (isprint((unsigned char)c))
109 110
				fprintf(f, "%c", c);
			else
111
				fprintf(f, "\\x%02"PRIx8, c);
112 113 114 115 116
		}
	}
	fprintf(f, "\"");
}

117
static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
118
{
119 120
	const char *end = p + len;
	assert(len % width == 0);
121

122 123 124
	for (; p < end; p += width) {
		switch (width) {
		case 1:
125
			fprintf(f, "%02"PRIx8, *(const uint8_t*)p);
126 127
			break;
		case 2:
128
			fprintf(f, "0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
129 130
			break;
		case 4:
131
			fprintf(f, "0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
132 133
			break;
		case 8:
134
			fprintf(f, "0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
135
			break;
136
		}
137 138
		if (p + width < end)
			fputc(' ', f);
139
	}
140
}
141

142 143 144
static bool has_data_type_information(struct marker *m)
{
	return m->type >= TYPE_UINT8;
145 146
}

147
static struct marker *next_type_marker(struct marker *m)
148
{
149 150 151 152
	while (m && !has_data_type_information(m))
		m = m->next;
	return m;
}
153

154 155 156
size_t type_marker_length(struct marker *m)
{
	struct marker *next = next_type_marker(m->next);
157

158 159 160
	if (next)
		return next->offset - m->offset;
	return 0;
161 162
}

163 164 165 166 167 168 169 170
static const char *delim_start[] = {
	[TYPE_UINT8] = "[",
	[TYPE_UINT16] = "/bits/ 16 <",
	[TYPE_UINT32] = "<",
	[TYPE_UINT64] = "/bits/ 64 <",
	[TYPE_STRING] = "",
};
static const char *delim_end[] = {
171 172 173 174
	[TYPE_UINT8] = "]",
	[TYPE_UINT16] = ">",
	[TYPE_UINT32] = ">",
	[TYPE_UINT64] = ">",
175 176 177 178
	[TYPE_STRING] = "",
};

static enum markertype guess_value_type(struct property *prop)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
{
	int len = prop->val.len;
	const char *p = prop->val.val;
	struct marker *m = prop->val.markers;
	int nnotstring = 0, nnul = 0;
	int nnotstringlbl = 0, nnotcelllbl = 0;
	int i;

	for (i = 0; i < len; i++) {
		if (! isstring(p[i]))
			nnotstring++;
		if (p[i] == '\0')
			nnul++;
	}

	for_each_marker_of_type(m, LABEL) {
		if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
			nnotstringlbl++;
		if ((m->offset % sizeof(cell_t)) != 0)
			nnotcelllbl++;
	}

	if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
	    && (nnotstringlbl == 0)) {
203
		return TYPE_STRING;
204
	} else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
205
		return TYPE_UINT32;
206 207
	}

208 209 210 211 212 213 214 215 216
	return TYPE_UINT8;
}

static void write_propval(FILE *f, struct property *prop)
{
	size_t len = prop->val.len;
	struct marker *m = prop->val.markers;
	struct marker dummy_marker;
	enum markertype emit_type = TYPE_NONE;
217
	char *srcstr;
218 219

	if (len == 0) {
220 221 222 223 224 225 226 227 228
		fprintf(f, ";");
		if (annotate) {
			srcstr = srcpos_string_first(prop->srcpos, annotate);
			if (srcstr) {
				fprintf(f, " /* %s */", srcstr);
				free(srcstr);
			}
		}
		fprintf(f, "\n");
229 230 231
		return;
	}

232
	fprintf(f, " =");
233 234 235 236 237 238 239 240 241 242 243

	if (!next_type_marker(m)) {
		/* data type information missing, need to guess */
		dummy_marker.type = guess_value_type(prop);
		dummy_marker.next = prop->val.markers;
		dummy_marker.offset = 0;
		dummy_marker.ref = NULL;
		m = &dummy_marker;
	}

	for_each_marker(m) {
244 245
		size_t chunk_len = (m->next ? m->next->offset : len) - m->offset;
		size_t data_len = type_marker_length(m) ? : len - m->offset;
246 247
		const char *p = &prop->val.val[m->offset];

248 249 250 251 252 253 254
		if (has_data_type_information(m)) {
			emit_type = m->type;
			fprintf(f, " %s", delim_start[emit_type]);
		} else if (m->type == LABEL)
			fprintf(f, " %s:", m->ref);
		else if (m->offset)
			fputc(' ', f);
255

256 257
		if (emit_type == TYPE_NONE) {
			assert(chunk_len == 0);
258
			continue;
259
		}
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

		switch(emit_type) {
		case TYPE_UINT16:
			write_propval_int(f, p, chunk_len, 2);
			break;
		case TYPE_UINT32:
			write_propval_int(f, p, chunk_len, 4);
			break;
		case TYPE_UINT64:
			write_propval_int(f, p, chunk_len, 8);
			break;
		case TYPE_STRING:
			write_propval_string(f, p, chunk_len);
			break;
		default:
			write_propval_int(f, p, chunk_len, 1);
		}

278 279 280 281 282 283
		if (chunk_len == data_len) {
			size_t pos = m->offset + chunk_len;
			fprintf(f, pos == len ? "%s" : "%s,",
			        delim_end[emit_type] ? : "");
			emit_type = TYPE_NONE;
		}
284
	}
285 286 287 288 289 290 291 292 293
	fprintf(f, ";");
	if (annotate) {
		srcstr = srcpos_string_first(prop->srcpos, annotate);
		if (srcstr) {
			fprintf(f, " /* %s */", srcstr);
			free(srcstr);
		}
	}
	fprintf(f, "\n");
294 295 296 297 298 299
}

static void write_tree_source_node(FILE *f, struct node *tree, int level)
{
	struct property *prop;
	struct node *child;
300
	struct label *l;
301
	char *srcstr;
302 303

	write_prefix(f, level);
304 305
	for_each_label(tree->labels, l)
		fprintf(f, "%s: ", l->label);
306
	if (tree->name && (*tree->name))
307
		fprintf(f, "%s {", tree->name);
308
	else
309 310 311 312 313 314 315 316 317 318
		fprintf(f, "/ {");

	if (annotate) {
		srcstr = srcpos_string_first(tree->srcpos, annotate);
		if (srcstr) {
			fprintf(f, " /* %s */", srcstr);
			free(srcstr);
		}
	}
	fprintf(f, "\n");
319 320 321

	for_each_property(tree, prop) {
		write_prefix(f, level+1);
322 323
		for_each_label(prop->labels, l)
			fprintf(f, "%s: ", l->label);
324 325 326 327 328 329 330 331
		fprintf(f, "%s", prop->name);
		write_propval(f, prop);
	}
	for_each_child(tree, child) {
		fprintf(f, "\n");
		write_tree_source_node(f, child, level+1);
	}
	write_prefix(f, level);
332 333 334 335 336 337 338 339 340
	fprintf(f, "};");
	if (annotate) {
		srcstr = srcpos_string_last(tree->srcpos, annotate);
		if (srcstr) {
			fprintf(f, " /* %s */", srcstr);
			free(srcstr);
		}
	}
	fprintf(f, "\n");
341 342
}

343
void dt_to_source(FILE *f, struct dt_info *dti)
344 345 346 347 348
{
	struct reserve_info *re;

	fprintf(f, "/dts-v1/;\n\n");

349
	for (re = dti->reservelist; re; re = re->next) {
350 351 352 353
		struct label *l;

		for_each_label(re->labels, l)
			fprintf(f, "%s: ", l->label);
354
		fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
355 356
			(unsigned long long)re->address,
			(unsigned long long)re->size);
357 358
	}

359
	write_tree_source_node(f, dti->dt, 0);
360
}