treesource.c (a976c2951d8f376112361830aa7762beff83a205) treesource.c (f858927fd6ce394a7f431153d44ad0a09e8f49a1)
1/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
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.

--- 47 unchanged lines hidden (view full) ---

56
57static bool isstring(char c)
58{
59 return (isprint((unsigned char)c)
60 || (c == '\0')
61 || strchr("\a\b\t\n\v\f\r", c));
62}
63
1/*
2 * (C) Copyright David Gibson <dwg@au1.ibm.com>, IBM Corporation. 2005.
3 *
4 *
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.

--- 47 unchanged lines hidden (view full) ---

56
57static bool isstring(char c)
58{
59 return (isprint((unsigned char)c)
60 || (c == '\0')
61 || strchr("\a\b\t\n\v\f\r", c));
62}
63
64static void write_propval_string(FILE *f, struct data val)
64static void write_propval_string(FILE *f, const char *s, size_t len)
65{
65{
66 const char *str = val.val;
67 int i;
68 struct marker *m = val.markers;
66 const char *end = s + len - 1;
67 assert(*end == '\0');
69
68
70 assert(str[val.len-1] == '\0');
71
72 while (m && (m->offset == 0)) {
73 if (m->type == LABEL)
74 fprintf(f, "%s: ", m->ref);
75 m = m->next;
76 }
77 fprintf(f, "\"");
69 fprintf(f, "\"");
78
79 for (i = 0; i < (val.len-1); i++) {
80 char c = str[i];
81
70 while (s < end) {
71 char c = *s++;
82 switch (c) {
83 case '\a':
84 fprintf(f, "\\a");
85 break;
86 case '\b':
87 fprintf(f, "\\b");
88 break;
89 case '\t':

--- 13 unchanged lines hidden (view full) ---

103 break;
104 case '\\':
105 fprintf(f, "\\\\");
106 break;
107 case '\"':
108 fprintf(f, "\\\"");
109 break;
110 case '\0':
72 switch (c) {
73 case '\a':
74 fprintf(f, "\\a");
75 break;
76 case '\b':
77 fprintf(f, "\\b");
78 break;
79 case '\t':

--- 13 unchanged lines hidden (view full) ---

93 break;
94 case '\\':
95 fprintf(f, "\\\\");
96 break;
97 case '\"':
98 fprintf(f, "\\\"");
99 break;
100 case '\0':
111 fprintf(f, "\", ");
112 while (m && (m->offset <= (i + 1))) {
113 if (m->type == LABEL) {
114 assert(m->offset == (i+1));
115 fprintf(f, "%s: ", m->ref);
116 }
117 m = m->next;
118 }
119 fprintf(f, "\"");
101 fprintf(f, "\\0");
120 break;
121 default:
122 if (isprint((unsigned char)c))
123 fprintf(f, "%c", c);
124 else
102 break;
103 default:
104 if (isprint((unsigned char)c))
105 fprintf(f, "%c", c);
106 else
125 fprintf(f, "\\x%02hhx", c);
107 fprintf(f, "\\x%02"PRIx8, c);
126 }
127 }
128 fprintf(f, "\"");
108 }
109 }
110 fprintf(f, "\"");
129
130 /* Wrap up any labels at the end of the value */
131 for_each_marker_of_type(m, LABEL) {
132 assert (m->offset == val.len);
133 fprintf(f, " %s:", m->ref);
134 }
135}
136
111}
112
137static void write_propval_cells(FILE *f, struct data val)
113static void write_propval_int(FILE *f, const char *p, size_t len, size_t width)
138{
114{
139 void *propend = val.val + val.len;
140 fdt32_t *cp = (fdt32_t *)val.val;
141 struct marker *m = val.markers;
115 const char *end = p + len;
116 assert(len % width == 0);
142
117
143 fprintf(f, "<");
144 for (;;) {
145 while (m && (m->offset <= ((char *)cp - val.val))) {
146 if (m->type == LABEL) {
147 assert(m->offset == ((char *)cp - val.val));
148 fprintf(f, "%s: ", m->ref);
149 }
150 m = m->next;
151 }
152
153 fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
154 if ((void *)cp >= propend)
118 for (; p < end; p += width) {
119 switch (width) {
120 case 1:
121 fprintf(f, " %02"PRIx8, *(const uint8_t*)p);
155 break;
122 break;
156 fprintf(f, " ");
123 case 2:
124 fprintf(f, " 0x%02"PRIx16, fdt16_to_cpu(*(const fdt16_t*)p));
125 break;
126 case 4:
127 fprintf(f, " 0x%02"PRIx32, fdt32_to_cpu(*(const fdt32_t*)p));
128 break;
129 case 8:
130 fprintf(f, " 0x%02"PRIx64, fdt64_to_cpu(*(const fdt64_t*)p));
131 break;
132 }
157 }
133 }
134}
158
135
159 /* Wrap up any labels at the end of the value */
160 for_each_marker_of_type(m, LABEL) {
161 assert (m->offset == val.len);
162 fprintf(f, " %s:", m->ref);
163 }
164 fprintf(f, ">");
136static bool has_data_type_information(struct marker *m)
137{
138 return m->type >= TYPE_UINT8;
165}
166
139}
140
167static void write_propval_bytes(FILE *f, struct data val)
141static struct marker *next_type_marker(struct marker *m)
168{
142{
169 void *propend = val.val + val.len;
170 const char *bp = val.val;
171 struct marker *m = val.markers;
143 while (m && !has_data_type_information(m))
144 m = m->next;
145 return m;
146}
172
147
173 fprintf(f, "[");
174 for (;;) {
175 while (m && (m->offset == (bp-val.val))) {
176 if (m->type == LABEL)
177 fprintf(f, "%s: ", m->ref);
178 m = m->next;
179 }
148size_t type_marker_length(struct marker *m)
149{
150 struct marker *next = next_type_marker(m->next);
180
151
181 fprintf(f, "%02hhx", (unsigned char)(*bp++));
182 if ((const void *)bp >= propend)
183 break;
184 fprintf(f, " ");
185 }
186
187 /* Wrap up any labels at the end of the value */
188 for_each_marker_of_type(m, LABEL) {
189 assert (m->offset == val.len);
190 fprintf(f, " %s:", m->ref);
191 }
192 fprintf(f, "]");
152 if (next)
153 return next->offset - m->offset;
154 return 0;
193}
194
155}
156
195static void write_propval(FILE *f, struct property *prop)
157static const char *delim_start[] = {
158 [TYPE_UINT8] = "[",
159 [TYPE_UINT16] = "/bits/ 16 <",
160 [TYPE_UINT32] = "<",
161 [TYPE_UINT64] = "/bits/ 64 <",
162 [TYPE_STRING] = "",
163};
164static const char *delim_end[] = {
165 [TYPE_UINT8] = " ]",
166 [TYPE_UINT16] = " >",
167 [TYPE_UINT32] = " >",
168 [TYPE_UINT64] = " >",
169 [TYPE_STRING] = "",
170};
171
172static enum markertype guess_value_type(struct property *prop)
196{
197 int len = prop->val.len;
198 const char *p = prop->val.val;
199 struct marker *m = prop->val.markers;
200 int nnotstring = 0, nnul = 0;
201 int nnotstringlbl = 0, nnotcelllbl = 0;
202 int i;
203
173{
174 int len = prop->val.len;
175 const char *p = prop->val.val;
176 struct marker *m = prop->val.markers;
177 int nnotstring = 0, nnul = 0;
178 int nnotstringlbl = 0, nnotcelllbl = 0;
179 int i;
180
204 if (len == 0) {
205 fprintf(f, ";\n");
206 return;
207 }
208
209 for (i = 0; i < len; i++) {
210 if (! isstring(p[i]))
211 nnotstring++;
212 if (p[i] == '\0')
213 nnul++;
214 }
215
216 for_each_marker_of_type(m, LABEL) {
217 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
218 nnotstringlbl++;
219 if ((m->offset % sizeof(cell_t)) != 0)
220 nnotcelllbl++;
221 }
222
181 for (i = 0; i < len; i++) {
182 if (! isstring(p[i]))
183 nnotstring++;
184 if (p[i] == '\0')
185 nnul++;
186 }
187
188 for_each_marker_of_type(m, LABEL) {
189 if ((m->offset > 0) && (prop->val.val[m->offset - 1] != '\0'))
190 nnotstringlbl++;
191 if ((m->offset % sizeof(cell_t)) != 0)
192 nnotcelllbl++;
193 }
194
223 fprintf(f, " = ");
224 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
225 && (nnotstringlbl == 0)) {
195 if ((p[len-1] == '\0') && (nnotstring == 0) && (nnul < (len-nnul))
196 && (nnotstringlbl == 0)) {
226 write_propval_string(f, prop->val);
197 return TYPE_STRING;
227 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
198 } else if (((len % sizeof(cell_t)) == 0) && (nnotcelllbl == 0)) {
228 write_propval_cells(f, prop->val);
229 } else {
230 write_propval_bytes(f, prop->val);
199 return TYPE_UINT32;
231 }
232
200 }
201
233 fprintf(f, ";\n");
202 return TYPE_UINT8;
234}
235
203}
204
205static void write_propval(FILE *f, struct property *prop)
206{
207 size_t len = prop->val.len;
208 struct marker *m = prop->val.markers;
209 struct marker dummy_marker;
210 enum markertype emit_type = TYPE_NONE;
211
212 if (len == 0) {
213 fprintf(f, ";\n");
214 return;
215 }
216
217 fprintf(f, " = ");
218
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;
225 m = &dummy_marker;
226 }
227
228 struct marker *m_label = prop->val.markers;
229 for_each_marker(m) {
230 size_t chunk_len;
231 const char *p = &prop->val.val[m->offset];
232
233 if (!has_data_type_information(m))
234 continue;
235
236 chunk_len = type_marker_length(m);
237 if (!chunk_len)
238 chunk_len = len - m->offset;
239
240 if (emit_type != TYPE_NONE)
241 fprintf(f, "%s, ", delim_end[emit_type]);
242 emit_type = m->type;
243
244 for_each_marker_of_type(m_label, LABEL) {
245 if (m_label->offset > m->offset)
246 break;
247 fprintf(f, "%s: ", m_label->ref);
248 }
249
250 fprintf(f, "%s", delim_start[emit_type]);
251
252 if (chunk_len <= 0)
253 continue;
254
255 switch(emit_type) {
256 case TYPE_UINT16:
257 write_propval_int(f, p, chunk_len, 2);
258 break;
259 case TYPE_UINT32:
260 write_propval_int(f, p, chunk_len, 4);
261 break;
262 case TYPE_UINT64:
263 write_propval_int(f, p, chunk_len, 8);
264 break;
265 case TYPE_STRING:
266 write_propval_string(f, p, chunk_len);
267 break;
268 default:
269 write_propval_int(f, p, chunk_len, 1);
270 }
271 }
272
273 /* Wrap up any labels at the end of the value */
274 for_each_marker_of_type(m_label, LABEL) {
275 assert (m_label->offset == len);
276 fprintf(f, " %s:", m_label->ref);
277 }
278
279 fprintf(f, "%s;\n", delim_end[emit_type] ? : "");
280}
281
236static void write_tree_source_node(FILE *f, struct node *tree, int level)
237{
238 struct property *prop;
239 struct node *child;
240 struct label *l;
241
242 write_prefix(f, level);
243 for_each_label(tree->labels, l)

--- 32 unchanged lines hidden (view full) ---

276 fprintf(f, "%s: ", l->label);
277 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
278 (unsigned long long)re->address,
279 (unsigned long long)re->size);
280 }
281
282 write_tree_source_node(f, dti->dt, 0);
283}
282static void write_tree_source_node(FILE *f, struct node *tree, int level)
283{
284 struct property *prop;
285 struct node *child;
286 struct label *l;
287
288 write_prefix(f, level);
289 for_each_label(tree->labels, l)

--- 32 unchanged lines hidden (view full) ---

322 fprintf(f, "%s: ", l->label);
323 fprintf(f, "/memreserve/\t0x%016llx 0x%016llx;\n",
324 (unsigned long long)re->address,
325 (unsigned long long)re->size);
326 }
327
328 write_tree_source_node(f, dti->dt, 0);
329}
284