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