]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/ppc/tree.c
sim: unify -Werror build settings
[thirdparty/binutils-gdb.git] / sim / ppc / tree.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
17
18 */
19
20
21 #ifndef _PARSE_C_
22 #define _PARSE_C_
23
24 #include <stdio.h>
25 #include <stdarg.h>
26
27 #include "basics.h"
28
29 #include "device.h"
30 #include "tree.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include "libiberty.h"
37
38 /* manipulate/lookup device names */
39
40 typedef struct _name_specifier {
41 /* components in the full length name */
42 char *path;
43 char *property;
44 char *value;
45 /* current device */
46 char *name;
47 char *base;
48 char *unit;
49 char *args;
50 /* previous device */
51 char *last_name;
52 char *last_base;
53 char *last_unit;
54 char *last_args;
55 /* work area */
56 char buf[1024];
57 } name_specifier;
58
59
60
61 /* Given a device specifier, break it up into its main components:
62 path (and if present) property name and property value. */
63
64 STATIC_INLINE_TREE\
65 (int)
66 split_device_specifier(device *current,
67 const char *device_specifier,
68 name_specifier *spec)
69 {
70 char *chp = NULL;
71
72 /* expand any leading alias if present */
73 if (current != NULL
74 && *device_specifier != '\0'
75 && *device_specifier != '.'
76 && *device_specifier != '/') {
77 device *aliases = tree_find_device(current, "/aliases");
78 char alias[32];
79 int len = 0;
80 while (device_specifier[len] != '\0'
81 && device_specifier[len] != '/'
82 && device_specifier[len] != ':'
83 && !isspace(device_specifier[len])) {
84 alias[len] = device_specifier[len];
85 len++;
86 if (len >= sizeof(alias))
87 error("split_device_specifier: buffer overflow");
88 }
89 alias[len] = '\0';
90 if (aliases != NULL
91 && device_find_property(aliases, alias)) {
92 strcpy(spec->buf, device_find_string_property(aliases, alias));
93 strcat(spec->buf, device_specifier + len);
94 }
95 else {
96 strcpy(spec->buf, device_specifier);
97 }
98 }
99 else {
100 strcpy(spec->buf, device_specifier);
101 }
102
103 /* check no overflow */
104 if (strlen(spec->buf) >= sizeof(spec->buf))
105 error("split_device_specifier: buffer overflow\n");
106
107 /* strip leading spaces */
108 chp = spec->buf;
109 while (*chp != '\0' && isspace(*chp))
110 chp++;
111 if (*chp == '\0')
112 return 0;
113
114 /* find the path and terminate it with null */
115 spec->path = chp;
116 while (*chp != '\0' && !isspace(*chp))
117 chp++;
118 if (*chp != '\0') {
119 *chp = '\0';
120 chp++;
121 }
122
123 /* and any value */
124 while (*chp != '\0' && isspace(*chp))
125 chp++;
126 spec->value = chp;
127
128 /* now go back and chop the property off of the path */
129 if (spec->value[0] == '\0') {
130 spec->property = NULL; /*not a property*/
131 spec->value = NULL;
132 }
133 else if (spec->value[0] == '>'
134 || spec->value[0] == '<') {
135 /* an interrupt spec */
136 spec->property = NULL;
137 }
138 else {
139 chp = strrchr(spec->path, '/');
140 if (chp == NULL) {
141 spec->property = spec->path;
142 spec->path = strchr(spec->property, '\0');
143 }
144 else {
145 *chp = '\0';
146 spec->property = chp+1;
147 }
148 }
149
150 /* and mark the rest as invalid */
151 spec->name = NULL;
152 spec->base = NULL;
153 spec->unit = NULL;
154 spec->args = NULL;
155 spec->last_name = NULL;
156 spec->last_base = NULL;
157 spec->last_unit = NULL;
158 spec->last_args = NULL;
159
160 return 1;
161 }
162
163
164 /* given a device specifier break it up into its main components -
165 path and property name - assuming that the last `device' is a
166 property name. */
167
168 STATIC_INLINE_DEVICE\
169 (int)
170 split_property_specifier(device *current,
171 const char *property_specifier,
172 name_specifier *spec)
173 {
174 if (split_device_specifier(current, property_specifier, spec)) {
175 if (spec->property == NULL) {
176 /* force the last name to be a property name */
177 char *chp = strrchr(spec->path, '/');
178 if (chp == NULL) {
179 spec->property = spec->path;
180 spec->path = strrchr(spec->property, '\0');;
181 }
182 else {
183 *chp = '\0';
184 spec->property = chp+1;
185 }
186 }
187 return 1;
188 }
189 else
190 return 0;
191 }
192
193
194 /* device the next device name and split it up, return 0 when no more
195 names to device */
196
197 STATIC_INLINE_TREE\
198 (int)
199 split_device_name(name_specifier *spec)
200 {
201 char *chp;
202 /* remember what came before */
203 spec->last_name = spec->name;
204 spec->last_base = spec->base;
205 spec->last_unit = spec->unit;
206 spec->last_args = spec->args;
207 /* finished? */
208 if (spec->path[0] == '\0') {
209 spec->name = NULL;
210 spec->base = NULL;
211 spec->unit = NULL;
212 spec->args = NULL;
213 return 0;
214 }
215 /* break the current device spec from the path */
216 spec->name = spec->path;
217 chp = strchr(spec->name, '/');
218 if (chp == NULL)
219 spec->path = strchr(spec->name, '\0');
220 else {
221 spec->path = chp+1;
222 *chp = '\0';
223 }
224 /* break out the base */
225 if (spec->name[0] == '(') {
226 chp = strchr(spec->name, ')');
227 if (chp == NULL) {
228 spec->base = spec->name;
229 }
230 else {
231 *chp = '\0';
232 spec->base = spec->name + 1;
233 spec->name = chp + 1;
234 }
235 }
236 else {
237 spec->base = spec->name;
238 }
239 /* now break out the unit */
240 chp = strchr(spec->name, '@');
241 if (chp == NULL) {
242 spec->unit = NULL;
243 chp = spec->name;
244 }
245 else {
246 *chp = '\0';
247 chp += 1;
248 spec->unit = chp;
249 }
250 /* finally any args */
251 chp = strchr(chp, ':');
252 if (chp == NULL)
253 spec->args = NULL;
254 else {
255 *chp = '\0';
256 spec->args = chp+1;
257 }
258 return 1;
259 }
260
261
262 /* device the value, returning the next non-space token */
263
264 STATIC_INLINE_TREE\
265 (char *)
266 split_value(name_specifier *spec)
267 {
268 char *token;
269 if (spec->value == NULL)
270 return NULL;
271 /* skip leading white space */
272 while (isspace(spec->value[0]))
273 spec->value++;
274 if (spec->value[0] == '\0') {
275 spec->value = NULL;
276 return NULL;
277 }
278 token = spec->value;
279 /* find trailing space */
280 while (spec->value[0] != '\0' && !isspace(spec->value[0]))
281 spec->value++;
282 /* chop this value out */
283 if (spec->value[0] != '\0') {
284 spec->value[0] = '\0';
285 spec->value++;
286 }
287 return token;
288 }
289
290
291
292 /* traverse the path specified by spec starting at current */
293
294 STATIC_INLINE_TREE\
295 (device *)
296 split_find_device(device *current,
297 name_specifier *spec)
298 {
299 /* strip off (and process) any leading ., .., ./ and / */
300 while (1) {
301 if (strncmp(spec->path, "/", strlen("/")) == 0) {
302 /* cd /... */
303 while (current != NULL && device_parent(current) != NULL)
304 current = device_parent(current);
305 spec->path += strlen("/");
306 }
307 else if (strncmp(spec->path, "./", strlen("./")) == 0) {
308 /* cd ./... */
309 current = current;
310 spec->path += strlen("./");
311 }
312 else if (strncmp(spec->path, "../", strlen("../")) == 0) {
313 /* cd ../... */
314 if (current != NULL && device_parent(current) != NULL)
315 current = device_parent(current);
316 spec->path += strlen("../");
317 }
318 else if (strcmp(spec->path, ".") == 0) {
319 /* cd . */
320 current = current;
321 spec->path += strlen(".");
322 }
323 else if (strcmp(spec->path, "..") == 0) {
324 /* cd . */
325 if (current != NULL && device_parent(current) != NULL)
326 current = device_parent(current);
327 spec->path += strlen("..");
328 }
329 else
330 break;
331 }
332
333 /* now go through the path proper */
334
335 if (current == NULL) {
336 split_device_name(spec);
337 return NULL;
338 }
339
340 while (split_device_name(spec)) {
341 device *child;
342 for (child = device_child(current);
343 child != NULL; child = device_sibling(child)) {
344 if (strcmp(spec->name, device_name(child)) == 0) {
345 if (spec->unit == NULL)
346 break;
347 else {
348 device_unit phys;
349 device_decode_unit(current, spec->unit, &phys);
350 if (memcmp(&phys, device_unit_address(child),
351 sizeof(device_unit)) == 0)
352 break;
353 }
354 }
355 }
356 if (child == NULL)
357 return current; /* search failed */
358 current = child;
359 }
360
361 return current;
362 }
363
364
365 STATIC_INLINE_TREE\
366 (device *)
367 split_fill_path(device *current,
368 const char *device_specifier,
369 name_specifier *spec)
370 {
371 /* break it up */
372 if (!split_device_specifier(current, device_specifier, spec))
373 device_error(current, "error parsing %s\n", device_specifier);
374
375 /* fill our tree with its contents */
376 current = split_find_device(current, spec);
377
378 /* add any additional devices as needed */
379 if (spec->name != NULL) {
380 do {
381 current = device_create(current, spec->base, spec->name,
382 spec->unit, spec->args);
383 } while (split_device_name(spec));
384 }
385
386 return current;
387 }
388
389
390 INLINE_TREE\
391 (void)
392 tree_init(device *root,
393 psim *system)
394 {
395 TRACE(trace_device_tree, ("tree_init(root=0x%lx, system=0x%lx)\n",
396 (long)root,
397 (long)system));
398 /* remove the old, rebuild the new */
399 tree_traverse(root, device_clean, NULL, system);
400 tree_traverse(root, device_init_static_properties, NULL, system);
401 tree_traverse(root, device_init_address, NULL, system);
402 tree_traverse(root, device_init_runtime_properties, NULL, system);
403 tree_traverse(root, device_init_data, NULL, system);
404 }
405
406
407 \f
408 /* <non-white-space> */
409
410 STATIC_INLINE_TREE\
411 (const char *)
412 skip_token(const char *chp)
413 {
414 while (!isspace(*chp) && *chp != '\0')
415 chp++;
416 while (isspace(*chp) && *chp != '\0')
417 chp++;
418 return chp;
419 }
420
421
422 /* count the number of entries */
423
424 STATIC_INLINE_TREE\
425 (int)
426 count_entries(device *current,
427 const char *property_name,
428 const char *property_value,
429 int modulo)
430 {
431 const char *chp = property_value;
432 int nr_entries = 0;
433 while (*chp != '\0') {
434 nr_entries += 1;
435 chp = skip_token(chp);
436 }
437 if ((nr_entries % modulo) != 0) {
438 device_error(current, "incorrect number of entries for %s property %s, should be multiple of %d",
439 property_name, property_value, modulo);
440 }
441 return nr_entries / modulo;
442 }
443
444
445
446 /* parse: <address> ::= <token> ; device dependant */
447
448 STATIC_INLINE_TREE\
449 (const char *)
450 parse_address(device *current,
451 device *bus,
452 const char *chp,
453 device_unit *address)
454 {
455 ASSERT(device_nr_address_cells(bus) > 0);
456 if (device_decode_unit(bus, chp, address) < 0)
457 device_error(current, "invalid unit address in %s", chp);
458 return skip_token(chp);
459 }
460
461
462 /* parse: <size> ::= <number> { "," <number> } ; */
463
464 STATIC_INLINE_TREE\
465 (const char *)
466 parse_size(device *current,
467 device *bus,
468 const char *chp,
469 device_unit *size)
470 {
471 int i;
472 int nr;
473 const char *curr = chp;
474 memset(size, 0, sizeof(*size));
475 /* parse the numeric list */
476 size->nr_cells = device_nr_size_cells(bus);
477 nr = 0;
478 ASSERT(size->nr_cells > 0);
479 while (1) {
480 char *next;
481 size->cells[nr] = strtoul(curr, &next, 0);
482 if (curr == next)
483 device_error(current, "Problem parsing <size> %s", chp);
484 nr += 1;
485 if (next[0] != ',')
486 break;
487 if (nr == size->nr_cells)
488 device_error(current, "Too many values in <size> %s", chp);
489 curr = next + 1;
490 }
491 ASSERT(nr > 0 && nr <= size->nr_cells);
492 /* right align the numbers */
493 for (i = 1; i <= size->nr_cells; i++) {
494 if (i <= nr)
495 size->cells[size->nr_cells - i] = size->cells[nr - i];
496 else
497 size->cells[size->nr_cells - i] = 0;
498 }
499 return skip_token(chp);
500 }
501
502
503 /* parse: <reg> ::= { <address> <size> } ; */
504
505 STATIC_INLINE_TREE\
506 (void)
507 parse_reg_property(device *current,
508 const char *property_name,
509 const char *property_value)
510 {
511 int nr_regs;
512 int reg_nr;
513 reg_property_spec *regs;
514 const char *chp;
515 device *bus = device_parent(current);
516
517 /* determine the number of reg entries by counting tokens */
518 nr_regs = count_entries(current, property_name, property_value,
519 1 + (device_nr_size_cells(bus) > 0));
520
521 /* create working space */
522 regs = zalloc(nr_regs * sizeof(*regs));
523
524 /* fill it in */
525 chp = property_value;
526 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) {
527 chp = parse_address(current, bus, chp, &regs[reg_nr].address);
528 if (device_nr_size_cells(bus) > 0)
529 chp = parse_size(current, bus, chp, &regs[reg_nr].size);
530 else
531 memset(&regs[reg_nr].size, 0, sizeof (regs[reg_nr].size));
532 }
533
534 /* create it */
535 device_add_reg_array_property(current, property_name,
536 regs, nr_regs);
537
538 free(regs);
539 }
540
541
542 /* { <child-address> <parent-address> <child-size> }* */
543
544 STATIC_INLINE_TREE\
545 (void)
546 parse_ranges_property(device *current,
547 const char *property_name,
548 const char *property_value)
549 {
550 int nr_ranges;
551 int range_nr;
552 range_property_spec *ranges;
553 const char *chp;
554
555 /* determine the number of ranges specified */
556 nr_ranges = count_entries(current, property_name, property_value, 3);
557
558 /* create a property of that size */
559 ranges = zalloc(nr_ranges * sizeof(*ranges));
560
561 /* fill it in */
562 chp = property_value;
563 for (range_nr = 0; range_nr < nr_ranges; range_nr++) {
564 chp = parse_address(current, current,
565 chp, &ranges[range_nr].child_address);
566 chp = parse_address(current, device_parent(current),
567 chp, &ranges[range_nr].parent_address);
568 chp = parse_size(current, current,
569 chp, &ranges[range_nr].size);
570 }
571
572 /* create it */
573 device_add_range_array_property(current, property_name, ranges, nr_ranges);
574
575 free(ranges);
576 }
577
578
579 /* <integer> ... */
580
581 STATIC_INLINE_TREE\
582 (void)
583 parse_integer_property(device *current,
584 const char *property_name,
585 const char *property_value)
586 {
587 int nr_entries;
588 unsigned_cell words[1024];
589 /* integer or integer array? */
590 nr_entries = 0;
591 while (1) {
592 char *end;
593 words[nr_entries] = strtoul(property_value, &end, 0);
594 if (property_value == end)
595 break;
596 nr_entries += 1;
597 if (nr_entries * sizeof(words[0]) >= sizeof(words))
598 device_error(current, "buffer overflow");
599 property_value = end;
600 }
601 if (nr_entries == 0)
602 device_error(current, "error parsing integer property %s (%s)",
603 property_name, property_value);
604 else if (nr_entries == 1)
605 device_add_integer_property(current, property_name, words[0]);
606 else {
607 int i;
608 for (i = 0; i < nr_entries; i++) {
609 H2BE(words[i]);
610 }
611 /* perhaps integer array property is better */
612 device_add_array_property(current, property_name, words,
613 sizeof(words[0]) * nr_entries);
614 }
615 }
616
617 /* PROPERTY_VALUE is a raw property value. Quote it as required by
618 parse_string_property. It is the caller's responsibility to free
619 the memory returned. */
620
621 EXTERN_TREE\
622 (char *)
623 tree_quote_property(const char *property_value)
624 {
625 char *p;
626 char *ret;
627 const char *chp;
628 int quotees;
629
630 /* Count characters needing quotes in PROPERTY_VALUE. */
631 quotees = 0;
632 for (chp = property_value; *chp; ++chp)
633 if (*chp == '\\' || *chp == '"')
634 ++quotees;
635
636 ret = (char *) xmalloc (strlen (property_value)
637 + 2 /* quotes */
638 + quotees
639 + 1 /* terminator */);
640
641 p = ret;
642 /* Add the opening quote. */
643 *p++ = '"';
644 /* Copy the value. */
645 for (chp = property_value; *chp; ++chp)
646 if (*chp == '\\' || *chp == '"')
647 {
648 /* Quote this character. */
649 *p++ = '\\';
650 *p++ = *chp;
651 }
652 else
653 *p++ = *chp;
654 /* Add the closing quote. */
655 *p++ = '"';
656 /* Terminate the string. */
657 *p++ = '\0';
658
659 return ret;
660 }
661
662 /* <string> ... */
663
664 STATIC_INLINE_TREE\
665 (void)
666 parse_string_property(device *current,
667 const char *property_name,
668 const char *property_value)
669 {
670 char **strings;
671 const char *chp;
672 int nr_strings;
673 int approx_nr_strings;
674
675 /* get an estimate as to the number of strings by counting double
676 quotes */
677 approx_nr_strings = 2;
678 for (chp = property_value; *chp; chp++) {
679 if (*chp == '"')
680 approx_nr_strings++;
681 }
682 approx_nr_strings = (approx_nr_strings) / 2;
683
684 /* create a string buffer for that many (plus a null) */
685 strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
686
687 /* now find all the strings */
688 chp = property_value;
689 nr_strings = 0;
690 while (1) {
691
692 /* skip leading space */
693 while (*chp != '\0' && isspace(*chp))
694 chp += 1;
695 if (*chp == '\0')
696 break;
697
698 /* copy it in */
699 if (*chp == '"') {
700 /* a quoted string - watch for '\' et.al. */
701 /* estimate the size and allocate space for it */
702 int pos;
703 chp++;
704 pos = 0;
705 while (chp[pos] != '\0' && chp[pos] != '"') {
706 if (chp[pos] == '\\' && chp[pos+1] != '\0')
707 pos += 2;
708 else
709 pos += 1;
710 }
711 strings[nr_strings] = zalloc(pos + 1);
712 /* copy the string over */
713 pos = 0;
714 while (*chp != '\0' && *chp != '"') {
715 if (*chp == '\\' && *(chp+1) != '\0') {
716 strings[nr_strings][pos] = *(chp+1);
717 chp += 2;
718 pos++;
719 }
720 else {
721 strings[nr_strings][pos] = *chp;
722 chp += 1;
723 pos++;
724 }
725 }
726 if (*chp != '\0')
727 chp++;
728 strings[nr_strings][pos] = '\0';
729 }
730 else {
731 /* copy over a single unquoted token */
732 int len = 0;
733 while (chp[len] != '\0' && !isspace(chp[len]))
734 len++;
735 strings[nr_strings] = zalloc(len + 1);
736 strncpy(strings[nr_strings], chp, len);
737 strings[nr_strings][len] = '\0';
738 chp += len;
739 }
740 nr_strings++;
741 if (nr_strings > approx_nr_strings)
742 device_error(current, "String property %s badly formatted",
743 property_name);
744 }
745 ASSERT(strings[nr_strings] == NULL); /* from zalloc */
746
747 /* install it */
748 if (nr_strings == 0)
749 device_add_string_property(current, property_name, "");
750 else if (nr_strings == 1)
751 device_add_string_property(current, property_name, strings[0]);
752 else {
753 const char **specs = (const char**)strings; /* stop a bogus error */
754 device_add_string_array_property(current, property_name,
755 specs, nr_strings);
756 }
757
758 /* flush the created string */
759 while (nr_strings > 0) {
760 nr_strings--;
761 free(strings[nr_strings]);
762 }
763 free(strings);
764 }
765
766
767 /* <path-to-ihandle-device> */
768
769 STATIC_INLINE_TREE\
770 (void)
771 parse_ihandle_property(device *current,
772 const char *property,
773 const char *value)
774 {
775 ihandle_runtime_property_spec ihandle;
776
777 /* pass the full path */
778 ihandle.full_path = value;
779
780 /* save this ready for the ihandle create */
781 device_add_ihandle_runtime_property(current, property,
782 &ihandle);
783 }
784
785
786
787 EXTERN_TREE\
788 (device *)
789 tree_parse(device *current,
790 const char *fmt,
791 ...)
792 {
793 char device_specifier[1024];
794 name_specifier spec;
795
796 /* format the path */
797 {
798 va_list ap;
799 va_start(ap, fmt);
800 vsprintf(device_specifier, fmt, ap);
801 va_end(ap);
802 if (strlen(device_specifier) >= sizeof(device_specifier))
803 error("device_tree_add_deviced: buffer overflow\n");
804 }
805
806 /* construct the tree down to the final device */
807 current = split_fill_path(current, device_specifier, &spec);
808
809 /* is there an interrupt spec */
810 if (spec.property == NULL
811 && spec.value != NULL) {
812 char *op = split_value(&spec);
813 switch (op[0]) {
814 case '>':
815 {
816 char *my_port_name = split_value(&spec);
817 int my_port;
818 char *dest_port_name = split_value(&spec);
819 int dest_port;
820 name_specifier dest_spec;
821 char *dest_device_name = split_value(&spec);
822 device *dest;
823 /* find my name */
824 my_port = device_interrupt_decode(current, my_port_name,
825 output_port);
826 /* find the dest device and port */
827 dest = split_fill_path(current, dest_device_name, &dest_spec);
828 dest_port = device_interrupt_decode(dest, dest_port_name,
829 input_port);
830 /* connect the two */
831 device_interrupt_attach(current,
832 my_port,
833 dest,
834 dest_port,
835 permenant_object);
836 }
837 break;
838 default:
839 device_error(current, "unreconised interrupt spec %s\n", spec.value);
840 break;
841 }
842 }
843
844 /* is there a property */
845 if (spec.property != NULL) {
846 if (strcmp(spec.value, "true") == 0)
847 device_add_boolean_property(current, spec.property, 1);
848 else if (strcmp(spec.value, "false") == 0)
849 device_add_boolean_property(current, spec.property, 0);
850 else {
851 const device_property *property;
852 switch (spec.value[0]) {
853 case '*':
854 parse_ihandle_property(current, spec.property, spec.value + 1);
855 break;
856 case '[':
857 {
858 unsigned8 words[1024];
859 char *curr = spec.value + 1;
860 int nr_words = 0;
861 while (1) {
862 char *next;
863 words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
864 if (curr == next)
865 break;
866 curr = next;
867 nr_words += 1;
868 }
869 device_add_array_property(current, spec.property,
870 words, sizeof(words[0]) * nr_words);
871 }
872 break;
873 case '"':
874 parse_string_property(current, spec.property, spec.value);
875 break;
876 case '!':
877 spec.value++;
878 property = tree_find_property(current, spec.value);
879 if (property == NULL)
880 device_error(current, "property %s not found\n", spec.value);
881 device_add_duplicate_property(current,
882 spec.property,
883 property);
884 break;
885 default:
886 if (strcmp(spec.property, "reg") == 0
887 || strcmp(spec.property, "assigned-addresses") == 0
888 || strcmp(spec.property, "alternate-reg") == 0){
889 parse_reg_property(current, spec.property, spec.value);
890 }
891 else if (strcmp(spec.property, "ranges") == 0) {
892 parse_ranges_property(current, spec.property, spec.value);
893 }
894 else if (isdigit(spec.value[0])
895 || (spec.value[0] == '-' && isdigit(spec.value[1]))
896 || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
897 parse_integer_property(current, spec.property, spec.value);
898 }
899 else
900 parse_string_property(current, spec.property, spec.value);
901 break;
902 }
903 }
904 }
905 return current;
906 }
907
908
909 INLINE_TREE\
910 (void)
911 tree_traverse(device *root,
912 tree_traverse_function *prefix,
913 tree_traverse_function *postfix,
914 void *data)
915 {
916 device *child;
917 if (prefix != NULL)
918 prefix(root, data);
919 for (child = device_child(root);
920 child != NULL;
921 child = device_sibling(child)) {
922 tree_traverse(child, prefix, postfix, data);
923 }
924 if (postfix != NULL)
925 postfix(root, data);
926 }
927
928
929 STATIC_INLINE_TREE\
930 (void)
931 print_address(device *bus,
932 const device_unit *phys)
933 {
934 char unit[32];
935 device_encode_unit(bus, phys, unit, sizeof(unit));
936 printf_filtered(" %s", unit);
937 }
938
939 STATIC_INLINE_TREE\
940 (void)
941 print_size(device *bus,
942 const device_unit *size)
943 {
944 int i;
945 for (i = 0; i < size->nr_cells; i++)
946 if (size->cells[i] != 0)
947 break;
948 if (i < size->nr_cells) {
949 printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
950 i++;
951 for (; i < size->nr_cells; i++)
952 printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
953 }
954 else
955 printf_filtered(" 0");
956 }
957
958 STATIC_INLINE_TREE\
959 (void)
960 print_reg_property(device *me,
961 const device_property *property)
962 {
963 int reg_nr;
964 reg_property_spec reg;
965 for (reg_nr = 0;
966 device_find_reg_array_property(me, property->name, reg_nr, &reg);
967 reg_nr++) {
968 print_address(device_parent(me), &reg.address);
969 print_size(me, &reg.size);
970 }
971 }
972
973 STATIC_INLINE_TREE\
974 (void)
975 print_ranges_property(device *me,
976 const device_property *property)
977 {
978 int range_nr;
979 range_property_spec range;
980 for (range_nr = 0;
981 device_find_range_array_property(me, property->name, range_nr, &range);
982 range_nr++) {
983 print_address(me, &range.child_address);
984 print_address(device_parent(me), &range.parent_address);
985 print_size(me, &range.size);
986 }
987 }
988
989 STATIC_INLINE_TREE\
990 (void)
991 print_string(const char *string)
992 {
993 printf_filtered(" \"");
994 while (*string != '\0') {
995 switch (*string) {
996 case '"':
997 printf_filtered("\\\"");
998 break;
999 case '\\':
1000 printf_filtered("\\\\");
1001 break;
1002 default:
1003 printf_filtered("%c", *string);
1004 break;
1005 }
1006 string++;
1007 }
1008 printf_filtered("\"");
1009 }
1010
1011 STATIC_INLINE_TREE\
1012 (void)
1013 print_string_array_property(device *me,
1014 const device_property *property)
1015 {
1016 int nr;
1017 string_property_spec string;
1018 for (nr = 0;
1019 device_find_string_array_property(me, property->name, nr, &string);
1020 nr++) {
1021 print_string(string);
1022 }
1023 }
1024
1025 STATIC_INLINE_TREE\
1026 (void)
1027 print_properties(device *me)
1028 {
1029 const device_property *property;
1030 for (property = device_find_property(me, NULL);
1031 property != NULL;
1032 property = device_next_property(property)) {
1033 printf_filtered("%s/%s", device_path(me), property->name);
1034 if (property->original != NULL) {
1035 printf_filtered(" !");
1036 printf_filtered("%s/%s",
1037 device_path(property->original->owner),
1038 property->original->name);
1039 }
1040 else {
1041 switch (property->type) {
1042 case array_property:
1043 if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
1044 unsigned_cell *w = (unsigned_cell*)property->array;
1045 int cell_nr;
1046 for (cell_nr = 0;
1047 cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
1048 cell_nr++) {
1049 printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
1050 }
1051 }
1052 else {
1053 unsigned8 *w = (unsigned8*)property->array;
1054 printf_filtered(" [");
1055 while ((char*)w - (char*)property->array < property->sizeof_array) {
1056 printf_filtered(" 0x%2x", BE2H_1(*w));
1057 w++;
1058 }
1059 }
1060 break;
1061 case boolean_property:
1062 {
1063 int b = device_find_boolean_property(me, property->name);
1064 printf_filtered(" %s", b ? "true" : "false");
1065 }
1066 break;
1067 case ihandle_property:
1068 {
1069 if (property->array != NULL) {
1070 device_instance *instance = device_find_ihandle_property(me, property->name);
1071 printf_filtered(" *%s", device_instance_path(instance));
1072 }
1073 else {
1074 /* not yet initialized, ask the device for the path */
1075 ihandle_runtime_property_spec spec;
1076 device_find_ihandle_runtime_property(me, property->name, &spec);
1077 printf_filtered(" *%s", spec.full_path);
1078 }
1079 }
1080 break;
1081 case integer_property:
1082 {
1083 unsigned_word w = device_find_integer_property(me, property->name);
1084 printf_filtered(" 0x%lx", (unsigned long)w);
1085 }
1086 break;
1087 case range_array_property:
1088 print_ranges_property(me, property);
1089 break;
1090 case reg_array_property:
1091 print_reg_property(me, property);
1092 break;
1093 case string_property:
1094 {
1095 const char *s = device_find_string_property(me, property->name);
1096 print_string(s);
1097 }
1098 break;
1099 case string_array_property:
1100 print_string_array_property(me, property);
1101 break;
1102 }
1103 }
1104 printf_filtered("\n");
1105 }
1106 }
1107
1108 STATIC_INLINE_TREE\
1109 (void)
1110 print_interrupts(device *me,
1111 int my_port,
1112 device *dest,
1113 int dest_port,
1114 void *ignore_or_null)
1115 {
1116 char src[32];
1117 char dst[32];
1118 device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
1119 device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
1120 printf_filtered("%s > %s %s %s\n",
1121 device_path(me),
1122 src, dst,
1123 device_path(dest));
1124 }
1125
1126 STATIC_INLINE_TREE\
1127 (void)
1128 print_device(device *me,
1129 void *ignore_or_null)
1130 {
1131 printf_filtered("%s\n", device_path(me));
1132 print_properties(me);
1133 device_interrupt_traverse(me, print_interrupts, NULL);
1134 }
1135
1136 INLINE_TREE\
1137 (void)
1138 tree_print(device *root)
1139 {
1140 tree_traverse(root,
1141 print_device, NULL,
1142 NULL);
1143 }
1144
1145
1146 INLINE_TREE\
1147 (void)
1148 tree_usage(int verbose)
1149 {
1150 if (verbose == 1) {
1151 printf_filtered("\n");
1152 printf_filtered("A device/property specifier has the form:\n");
1153 printf_filtered("\n");
1154 printf_filtered(" /path/to/a/device [ property-value ]\n");
1155 printf_filtered("\n");
1156 printf_filtered("and a possible device is\n");
1157 printf_filtered("\n");
1158 }
1159 if (verbose > 1) {
1160 printf_filtered("\n");
1161 printf_filtered("A device/property specifier (<spec>) has the format:\n");
1162 printf_filtered("\n");
1163 printf_filtered(" <spec> ::= <path> [ <value> ] ;\n");
1164 printf_filtered(" <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
1165 printf_filtered(" <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
1166 printf_filtered(" <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
1167 printf_filtered(" <unit> ::= <number> { \",\" <number> } ;\n");
1168 printf_filtered("\n");
1169 printf_filtered("Where:\n");
1170 printf_filtered("\n");
1171 printf_filtered(" <name> is the name of a device (list below)\n");
1172 printf_filtered(" <unit> is the unit-address relative to the parent bus\n");
1173 printf_filtered(" <args> additional arguments used when creating the device\n");
1174 printf_filtered(" <value> ::= ( <number> # integer property\n");
1175 printf_filtered(" | \"[\" { <number> } # array property (byte)\n");
1176 printf_filtered(" | \"{\" { <number> } # array property (cell)\n");
1177 printf_filtered(" | [ \"true\" | \"false\" ] # boolean property\n");
1178 printf_filtered(" | \"*\" <path> # ihandle property\n");
1179 printf_filtered(" | \"!\" <path> # copy property\n");
1180 printf_filtered(" | \">\" [ <number> ] <path> # attach interrupt\n");
1181 printf_filtered(" | \"<\" <path> # attach child interrupt\n");
1182 printf_filtered(" | \"\\\"\" <text> # string property\n");
1183 printf_filtered(" | <text> # string property\n");
1184 printf_filtered(" ) ;\n");
1185 printf_filtered("\n");
1186 printf_filtered("And the following are valid device names:\n");
1187 printf_filtered("\n");
1188 }
1189 }
1190
1191
1192
1193 INLINE_TREE\
1194 (device_instance *)
1195 tree_instance(device *root,
1196 const char *device_specifier)
1197 {
1198 /* find the device node */
1199 device *me;
1200 name_specifier spec;
1201 if (!split_device_specifier(root, device_specifier, &spec))
1202 return NULL;
1203 me = split_find_device(root, &spec);
1204 if (spec.name != NULL)
1205 return NULL;
1206 /* create the instance */
1207 return device_create_instance(me, device_specifier, spec.last_args);
1208 }
1209
1210
1211 INLINE_TREE\
1212 (device *)
1213 tree_find_device(device *root,
1214 const char *path_to_device)
1215 {
1216 device *node;
1217 name_specifier spec;
1218
1219 /* parse the path */
1220 split_device_specifier(root, path_to_device, &spec);
1221 if (spec.value != NULL)
1222 return NULL; /* something wierd */
1223
1224 /* now find it */
1225 node = split_find_device(root, &spec);
1226 if (spec.name != NULL)
1227 return NULL; /* not a leaf */
1228
1229 return node;
1230 }
1231
1232
1233 INLINE_TREE\
1234 (const device_property *)
1235 tree_find_property(device *root,
1236 const char *path_to_property)
1237 {
1238 name_specifier spec;
1239 if (!split_property_specifier(root, path_to_property, &spec))
1240 device_error(root, "Invalid property path %s", path_to_property);
1241 root = split_find_device(root, &spec);
1242 return device_find_property(root, spec.property);
1243 }
1244
1245 INLINE_TREE\
1246 (int)
1247 tree_find_boolean_property(device *root,
1248 const char *path_to_property)
1249 {
1250 name_specifier spec;
1251 if (!split_property_specifier(root, path_to_property, &spec))
1252 device_error(root, "Invalid property path %s", path_to_property);
1253 root = split_find_device(root, &spec);
1254 return device_find_boolean_property(root, spec.property);
1255 }
1256
1257 INLINE_TREE\
1258 (signed_cell)
1259 tree_find_integer_property(device *root,
1260 const char *path_to_property)
1261 {
1262 name_specifier spec;
1263 if (!split_property_specifier(root, path_to_property, &spec))
1264 device_error(root, "Invalid property path %s", path_to_property);
1265 root = split_find_device(root, &spec);
1266 return device_find_integer_property(root, spec.property);
1267 }
1268
1269 INLINE_TREE\
1270 (device_instance *)
1271 tree_find_ihandle_property(device *root,
1272 const char *path_to_property)
1273 {
1274 name_specifier spec;
1275 if (!split_property_specifier(root, path_to_property, &spec))
1276 device_error(root, "Invalid property path %s", path_to_property);
1277 root = split_find_device(root, &spec);
1278 return device_find_ihandle_property(root, spec.property);
1279 }
1280
1281 INLINE_TREE\
1282 (const char *)
1283 tree_find_string_property(device *root,
1284 const char *path_to_property)
1285 {
1286 name_specifier spec;
1287 if (!split_property_specifier(root, path_to_property, &spec))
1288 device_error(root, "Invalid property path %s", path_to_property);
1289 root = split_find_device(root, &spec);
1290 return device_find_string_property(root, spec.property);
1291 }
1292
1293
1294 #endif /* _PARSE_C_ */