Initial creation of sourceware repository
[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   if (device_decode_unit(bus, chp, address) < 0)
467     device_error(current, "invalid unit address in %s", chp);
468   return skip_token(chp);
469 }
470
471
472 /* parse: <size> ::= <number> { "," <number> } ; */
473
474 STATIC_INLINE_TREE\
475 (const char *)
476 parse_size(device *current,
477            device *bus,
478            const char *chp,
479            device_unit *size)
480 {
481   int i;
482   int nr;
483   const char *curr = chp;
484   memset(size, 0, sizeof(*size));
485   /* parse the numeric list */
486   size->nr_cells = device_nr_size_cells(bus);
487   nr = 0;
488   while (1) {
489     char *next;
490     size->cells[nr] = strtoul(curr, &next, 0);
491     if (curr == next)
492       device_error(current, "Problem parsing <size> %s", chp);
493     nr += 1;
494     if (next[0] != ',')
495       break;
496     if (nr == size->nr_cells)
497       device_error(current, "Too many values in <size> %s", chp);
498     curr = next + 1;
499   }
500   ASSERT(nr > 0 && nr <= size->nr_cells);
501   /* right align the numbers */
502   for (i = 1; i <= size->nr_cells; i++) {
503     if (i <= nr)
504       size->cells[size->nr_cells - i] = size->cells[nr - i];
505     else
506       size->cells[size->nr_cells - i] = 0;
507   }
508   return skip_token(chp);
509 }
510
511
512 /* parse: <reg> ::= { <address> <size> } ; */
513
514 STATIC_INLINE_TREE\
515 (void)
516 parse_reg_property(device *current,
517                    const char *property_name,
518                    const char *property_value)
519 {
520   int nr_regs;
521   int reg_nr;
522   reg_property_spec *regs;
523   const char *chp;
524
525   /* determine the number of reg entries by counting tokens */
526   nr_regs = count_entries(current, property_name, property_value, 2);
527
528   /* create working space */
529   regs = zalloc(nr_regs * sizeof(*regs));
530
531   /* fill it in */
532   chp = property_value;
533   for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) {
534     chp = parse_address(current, device_parent(current),
535                         chp, &regs[reg_nr].address);
536     chp = parse_size(current, device_parent(current),
537                      chp, &regs[reg_nr].size);
538   }
539
540   /* create it */
541   device_add_reg_array_property(current, property_name,
542                                 regs, nr_regs);
543
544   zfree(regs);
545 }
546
547
548 /* { <child-address> <parent-address> <child-size> }* */
549
550 STATIC_INLINE_TREE\
551 (void)
552 parse_ranges_property(device *current,
553                       const char *property_name,
554                       const char *property_value)
555 {
556   int nr_ranges;
557   int range_nr;
558   range_property_spec *ranges;
559   const char *chp;
560   
561   /* determine the number of ranges specified */
562   nr_ranges = count_entries(current, property_name, property_value, 3);
563
564   /* create a property of that size */
565   ranges = zalloc(nr_ranges * sizeof(*ranges));
566
567   /* fill it in */
568   chp = property_value;
569   for (range_nr = 0; range_nr < nr_ranges; range_nr++) {
570     chp = parse_address(current, current,
571                         chp, &ranges[range_nr].child_address);
572     chp = parse_address(current, device_parent(current),
573                         chp, &ranges[range_nr].parent_address);
574     chp = parse_size(current, current,
575                      chp, &ranges[range_nr].size);
576   }
577
578   /* create it */
579   device_add_range_array_property(current, property_name, ranges, nr_ranges);
580
581   zfree(ranges);
582 }
583
584
585 /* <integer> ... */
586
587 STATIC_INLINE_TREE\
588 (void)
589 parse_integer_property(device *current,
590                        const char *property_name,
591                        const char *property_value)
592 {
593   int nr_entries;
594   unsigned_cell words[1024];
595   /* integer or integer array? */
596   nr_entries = 0;
597   while (1) {
598     char *end;
599     words[nr_entries] = strtoul(property_value, &end, 0);
600     if (property_value == end)
601       break;
602     nr_entries += 1;
603     if (nr_entries * sizeof(words[0]) >= sizeof(words))
604       device_error(current, "buffer overflow");
605     property_value = end;
606   }
607   if (nr_entries == 0)
608     device_error(current, "error parsing integer property %s (%s)",
609                  property_name, property_value);
610   else if (nr_entries == 1)
611     device_add_integer_property(current, property_name, words[0]);
612   else {
613     int i;
614     for (i = 0; i < nr_entries; i++) {
615       H2BE(words[i]);
616     }
617     /* perhaphs integer array property is better */
618     device_add_array_property(current, property_name, words,
619                               sizeof(words[0]) * nr_entries);
620   }
621 }
622
623
624 /* <string> ... */
625
626 STATIC_INLINE_TREE\
627 (void)
628 parse_string_property(device *current,
629                       const char *property_name,
630                       const char *property_value)
631 {
632   char **strings;
633   const char *chp;
634   int nr_strings;
635   int approx_nr_strings;
636
637   /* get an estimate as to the number of strings by counting double
638      quotes */
639   approx_nr_strings = 2;
640   for (chp = property_value; *chp; chp++) {
641     if (*chp == '"')
642       approx_nr_strings++;
643   }
644   approx_nr_strings = (approx_nr_strings) / 2;
645
646   /* create a string buffer for that many (plus a null) */
647   strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
648
649   /* now find all the strings */
650   chp = property_value;
651   nr_strings = 0;
652   while (1) {
653
654     /* skip leading space */
655     while (*chp != '\0' && isspace(*chp))
656       chp += 1;
657     if (*chp == '\0')
658       break;
659
660     /* copy it in */
661     if (*chp == '"') {
662       /* a quoted string - watch for '\' et.al. */
663       /* estimate the size and allocate space for it */
664       int pos;
665       chp++;
666       pos = 0;
667       while (chp[pos] != '\0' && chp[pos] != '"') {
668         if (chp[pos] == '\\' && chp[pos+1] != '\0')
669           pos += 2;
670         else
671           pos += 1;
672       }
673       strings[nr_strings] = zalloc(pos + 1);
674       /* copy the string over */
675       pos = 0;
676       while (*chp != '\0' && *chp != '"') {
677         if (*chp == '\\' && *(chp+1) != '\0') {
678           strings[nr_strings][pos] = *(chp+1);
679           chp += 2;
680           pos++;
681         }
682         else {
683           strings[nr_strings][pos] = *chp;
684           chp += 1;
685           pos++;
686         }
687       }
688       if (*chp != '\0')
689         chp++;
690       strings[nr_strings][pos] = '\0';
691     }
692     else {
693       /* copy over a single unquoted token */
694       int len = 0;
695       while (chp[len] != '\0' && !isspace(chp[len]))
696         len++;
697       strings[nr_strings] = zalloc(len + 1);
698       strncpy(strings[nr_strings], chp, len);
699       strings[nr_strings][len] = '\0';
700       chp += len;
701     }
702     nr_strings++;
703     if (nr_strings > approx_nr_strings)
704       device_error(current, "String property %s badly formatted",
705                    property_name);
706   }
707   ASSERT(strings[nr_strings] == NULL); /* from zalloc */
708
709   /* install it */
710   if (nr_strings == 0)
711     device_add_string_property(current, property_name, "");
712   else if (nr_strings == 1)
713     device_add_string_property(current, property_name, strings[0]);
714   else {
715     const char **specs = (const char**)strings; /* stop a bogus error */
716     device_add_string_array_property(current, property_name,
717                                      specs, nr_strings);
718   }
719
720   /* flush the created string */
721   while (nr_strings > 0) {
722     nr_strings--;
723     zfree(strings[nr_strings]);
724   }
725   zfree(strings);
726 }
727
728
729 /* <path-to-ihandle-device> */
730
731 STATIC_INLINE_TREE\
732 (void)
733 parse_ihandle_property(device *current,
734                        const char *property,
735                        const char *value)
736 {
737   ihandle_runtime_property_spec ihandle;
738
739   /* pass the full path */
740   ihandle.full_path = value;
741
742   /* save this ready for the ihandle create */
743   device_add_ihandle_runtime_property(current, property,
744                                       &ihandle);
745 }
746
747
748
749 EXTERN_TREE\
750 (device *)
751 tree_parse(device *current,
752            const char *fmt,
753            ...)
754 {
755   char device_specifier[1024];
756   name_specifier spec;
757
758   /* format the path */
759   {
760     va_list ap;
761     va_start(ap, fmt);
762     vsprintf(device_specifier, fmt, ap);
763     va_end(ap);
764     if (strlen(device_specifier) >= sizeof(device_specifier))
765       error("device_tree_add_deviced: buffer overflow\n");
766   }
767
768   /* construct the tree down to the final device */
769   current = split_fill_path(current, device_specifier, &spec);
770
771   /* is there an interrupt spec */
772   if (spec.property == NULL
773       && spec.value != NULL) {
774     char *op = split_value(&spec);
775     switch (op[0]) {
776     case '>':
777       {
778         char *my_port_name = split_value(&spec);
779         int my_port;
780         char *dest_port_name = split_value(&spec);
781         int dest_port;
782         name_specifier dest_spec;
783         char *dest_device_name = split_value(&spec);
784         device *dest;
785         /* find my name */
786         my_port = device_interrupt_decode(current, my_port_name,
787                                           output_port);
788         /* find the dest device and port */
789         dest = split_fill_path(current, dest_device_name, &dest_spec);
790         dest_port = device_interrupt_decode(dest, dest_port_name,
791                                             input_port);
792         /* connect the two */
793         device_interrupt_attach(current,
794                                 my_port,
795                                 dest,
796                                 dest_port,
797                                 permenant_object);
798       }
799       break;
800     default:
801       device_error(current, "unreconised interrupt spec %s\n", spec.value);
802       break;
803     }
804   }
805
806   /* is there a property */
807   if (spec.property != NULL) {
808     if (strcmp(spec.value, "true") == 0)
809       device_add_boolean_property(current, spec.property, 1);
810     else if (strcmp(spec.value, "false") == 0)
811       device_add_boolean_property(current, spec.property, 0);
812     else {
813       const device_property *property;
814       switch (spec.value[0]) {
815       case '*':
816         parse_ihandle_property(current, spec.property, spec.value + 1);
817         break;
818       case '[':
819         {
820           unsigned8 words[1024];
821           char *curr = spec.value + 1;
822           int nr_words = 0;
823           while (1) {
824             char *next;
825             words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
826             if (curr == next)
827               break;
828             curr = next;
829             nr_words += 1;
830           }
831           device_add_array_property(current, spec.property,
832                                     words, sizeof(words[0]) * nr_words);
833         }
834         break;
835       case '"':
836         parse_string_property(current, spec.property, spec.value);
837         break;
838       case '!':
839         spec.value++;
840         property = tree_find_property(current, spec.value);
841         if (property == NULL)
842           device_error(current, "property %s not found\n", spec.value);
843         device_add_duplicate_property(current,
844                                       spec.property,
845                                       property);
846         break;
847       default:
848         if (strcmp(spec.property, "reg") == 0
849             || strcmp(spec.property, "assigned-addresses") == 0
850             || strcmp(spec.property, "alternate-reg") == 0){
851           parse_reg_property(current, spec.property, spec.value);
852         }
853         else if (strcmp(spec.property, "ranges") == 0) {
854           parse_ranges_property(current, spec.property, spec.value);
855         }
856         else if (isdigit(spec.value[0])
857                  || (spec.value[0] == '-' && isdigit(spec.value[1]))
858                  || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
859           parse_integer_property(current, spec.property, spec.value);
860         }
861         else
862           parse_string_property(current, spec.property, spec.value);
863         break;
864       }
865     }
866   }
867   return current;
868 }
869
870
871 INLINE_TREE\
872 (void)
873 tree_traverse(device *root,
874               tree_traverse_function *prefix,
875               tree_traverse_function *postfix,
876               void *data)
877 {
878   device *child;
879   if (prefix != NULL)
880     prefix(root, data);
881   for (child = device_child(root);
882        child != NULL;
883        child = device_sibling(child)) {
884     tree_traverse(child, prefix, postfix, data);
885   }
886   if (postfix != NULL)
887     postfix(root, data);
888 }
889
890
891 STATIC_INLINE_TREE\
892 (void)
893 print_address(device *bus,
894               const device_unit *phys)
895 {
896   char unit[32];
897   device_encode_unit(bus, phys, unit, sizeof(unit));
898   printf_filtered(" %s", unit);
899 }
900
901 STATIC_INLINE_TREE\
902 (void)
903 print_size(device *bus,
904            const device_unit *size)
905 {
906   int i;
907   for (i = 0; i < size->nr_cells; i++)
908     if (size->cells[i] != 0)
909       break;
910   if (i < size->nr_cells) {
911     printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
912     i++;
913     for (; i < size->nr_cells; i++)
914       printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
915   }
916   else
917     printf_filtered(" 0");
918 }
919
920 STATIC_INLINE_TREE\
921 (void)
922 print_reg_property(device *me,
923                    const device_property *property)
924 {
925   int reg_nr;
926   reg_property_spec reg;
927   for (reg_nr = 0;
928        device_find_reg_array_property(me, property->name, reg_nr, &reg);
929        reg_nr++) {
930     print_address(device_parent(me), &reg.address);
931     print_size(me, &reg.size);
932   }
933 }
934
935 STATIC_INLINE_TREE\
936 (void)
937 print_ranges_property(device *me,
938                       const device_property *property)
939 {
940   int range_nr;
941   range_property_spec range;
942   for (range_nr = 0;
943        device_find_range_array_property(me, property->name, range_nr, &range);
944        range_nr++) {
945     print_address(me, &range.child_address);
946     print_address(device_parent(me), &range.parent_address);
947     print_size(me, &range.size);
948   }
949 }
950
951 STATIC_INLINE_TREE\
952 (void)
953 print_string(const char *string)
954 {
955   printf_filtered(" \"");
956   while (*string != '\0') {
957     switch (*string) {
958     case '"':
959       printf_filtered("\\\"");
960       break;
961     case '\\':
962       printf_filtered("\\\\");
963       break;
964     default:
965       printf_filtered("%c", *string);
966       break;
967     }
968     string++;
969   }
970   printf_filtered("\"");
971 }
972
973 STATIC_INLINE_TREE\
974 (void)
975 print_string_array_property(device *me,
976                             const device_property *property)
977 {
978   int nr;
979   string_property_spec string;
980   for (nr = 0;
981        device_find_string_array_property(me, property->name, nr, &string);
982        nr++) {
983     print_string(string);
984   }
985 }
986
987 STATIC_INLINE_TREE\
988 (void)
989 print_properties(device *me)
990 {
991   const device_property *property;
992   for (property = device_find_property(me, NULL);
993        property != NULL;
994        property = device_next_property(property)) {
995     printf_filtered("%s/%s", device_path(me), property->name);
996     if (property->original != NULL) {
997       printf_filtered(" !");
998       printf_filtered("%s/%s", 
999                       device_path(property->original->owner),
1000                       property->original->name);
1001     }
1002     else {
1003       switch (property->type) {
1004       case array_property:
1005         if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
1006           unsigned_cell *w = (unsigned_cell*)property->array;
1007           int cell_nr;
1008           for (cell_nr = 0;
1009                cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
1010                cell_nr++) {
1011             printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
1012           }
1013         }
1014         else {
1015           unsigned8 *w = (unsigned8*)property->array;
1016           printf_filtered(" [");
1017           while ((char*)w - (char*)property->array < property->sizeof_array) {
1018             printf_filtered(" 0x%2x", BE2H_1(*w));
1019             w++;
1020           }
1021         }
1022         break;
1023       case boolean_property:
1024         {
1025           int b = device_find_boolean_property(me, property->name);
1026           printf_filtered(" %s", b ? "true"  : "false");
1027         }
1028         break;
1029       case ihandle_property:
1030         {
1031           if (property->array != NULL) {
1032             device_instance *instance = device_find_ihandle_property(me, property->name);
1033             printf_filtered(" *%s", device_instance_path(instance));
1034           }
1035           else {
1036             /* not yet initialized, ask the device for the path */
1037             ihandle_runtime_property_spec spec;
1038             device_find_ihandle_runtime_property(me, property->name, &spec);
1039             printf_filtered(" *%s", spec.full_path);
1040           }
1041         }
1042         break;
1043       case integer_property:
1044         {
1045           unsigned_word w = device_find_integer_property(me, property->name);
1046           printf_filtered(" 0x%lx", (unsigned long)w);
1047         }
1048         break;
1049       case range_array_property:
1050         print_ranges_property(me, property);
1051         break;
1052       case reg_array_property:
1053         print_reg_property(me, property);
1054         break;
1055       case string_property:
1056         {
1057           const char *s = device_find_string_property(me, property->name);
1058           print_string(s);
1059         }
1060         break;
1061       case string_array_property:
1062         print_string_array_property(me, property);
1063         break;
1064       }
1065     }
1066     printf_filtered("\n");
1067   }
1068 }
1069
1070 STATIC_INLINE_TREE\
1071 (void)
1072 print_interrupts(device *me,
1073                  int my_port,
1074                  device *dest,
1075                  int dest_port,
1076                  void *ignore_or_null)
1077 {
1078   char src[32];
1079   char dst[32];
1080   device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
1081   device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
1082   printf_filtered("%s > %s %s %s\n",
1083                   device_path(me),
1084                   src, dst,
1085                   device_path(dest));
1086 }
1087
1088 STATIC_INLINE_TREE\
1089 (void)
1090 print_device(device *me,
1091              void *ignore_or_null)
1092 {
1093   printf_filtered("%s\n", device_path(me));
1094   print_properties(me);
1095   device_interrupt_traverse(me, print_interrupts, NULL);
1096 }
1097
1098 INLINE_TREE\
1099 (void)
1100 tree_print(device *root)
1101 {
1102   tree_traverse(root,
1103                 print_device, NULL,
1104                 NULL);
1105 }
1106
1107
1108 INLINE_TREE\
1109 (void)
1110 tree_usage(int verbose)
1111 {
1112   if (verbose == 1) {
1113     printf_filtered("\n");
1114     printf_filtered("A device/property specifier has the form:\n");
1115     printf_filtered("\n");
1116     printf_filtered("  /path/to/a/device [ property-value ]\n");
1117     printf_filtered("\n");
1118     printf_filtered("and a possible device is\n");
1119     printf_filtered("\n");
1120   }
1121   if (verbose > 1) {
1122     printf_filtered("\n");
1123     printf_filtered("A device/property specifier (<spec>) has the format:\n");
1124     printf_filtered("\n");
1125     printf_filtered("  <spec> ::= <path> [ <value> ] ;\n");
1126     printf_filtered("  <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
1127     printf_filtered("  <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
1128     printf_filtered("  <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
1129     printf_filtered("  <unit> ::= <number> { \",\" <number> } ;\n");
1130     printf_filtered("\n");
1131     printf_filtered("Where:\n");
1132     printf_filtered("\n");
1133     printf_filtered("  <name>  is the name of a device (list below)\n");
1134     printf_filtered("  <unit>  is the unit-address relative to the parent bus\n");
1135     printf_filtered("  <args>  additional arguments used when creating the device\n");
1136     printf_filtered("  <value> ::= ( <number> # integer property\n");
1137     printf_filtered("              | \"[\" { <number> } # array property (byte)\n");
1138     printf_filtered("              | \"{\" { <number> } # array property (cell)\n");
1139     printf_filtered("              | [ \"true\" | \"false\" ] # boolean property\n");
1140     printf_filtered("              | \"*\" <path> # ihandle property\n");
1141     printf_filtered("              | \"!\" <path> # copy property\n");
1142     printf_filtered("              | \">\" [ <number> ] <path> # attach interrupt\n");
1143     printf_filtered("              | \"<\" <path> # attach child interrupt\n");
1144     printf_filtered("              | \"\\\"\" <text> # string property\n");
1145     printf_filtered("              | <text> # string property\n");
1146     printf_filtered("              ) ;\n");
1147     printf_filtered("\n");
1148     printf_filtered("And the following are valid device names:\n");
1149     printf_filtered("\n");
1150   }
1151 }
1152
1153
1154
1155 INLINE_TREE\
1156 (device_instance *)
1157 tree_instance(device *root,
1158               const char *device_specifier)
1159 {
1160   /* find the device node */
1161   device *me;
1162   name_specifier spec;
1163   if (!split_device_specifier(root, device_specifier, &spec))
1164     return NULL;
1165   me = split_find_device(root, &spec);
1166   if (spec.name != NULL)
1167     return NULL;
1168   /* create the instance */
1169   return device_create_instance(me, device_specifier, spec.last_args);
1170 }
1171
1172
1173 INLINE_TREE\
1174 (device *)
1175 tree_find_device(device *root,
1176                  const char *path_to_device)
1177 {
1178   device *node;
1179   name_specifier spec;
1180
1181   /* parse the path */
1182   split_device_specifier(root, path_to_device, &spec);
1183   if (spec.value != NULL)
1184     return NULL; /* something wierd */
1185
1186   /* now find it */
1187   node = split_find_device(root, &spec);
1188   if (spec.name != NULL)
1189     return NULL; /* not a leaf */
1190
1191   return node;
1192 }
1193
1194
1195 INLINE_TREE\
1196 (const device_property *)
1197 tree_find_property(device *root,
1198                    const char *path_to_property)
1199 {
1200   name_specifier spec;
1201   if (!split_property_specifier(root, path_to_property, &spec))
1202     device_error(root, "Invalid property path %s", path_to_property);
1203   root = split_find_device(root, &spec);
1204   return device_find_property(root, spec.property);
1205 }
1206
1207 INLINE_TREE\
1208 (int)
1209 tree_find_boolean_property(device *root,
1210                            const char *path_to_property)
1211 {
1212   name_specifier spec;
1213   if (!split_property_specifier(root, path_to_property, &spec))
1214     device_error(root, "Invalid property path %s", path_to_property);
1215   root = split_find_device(root, &spec);
1216   return device_find_boolean_property(root, spec.property);
1217 }
1218
1219 INLINE_TREE\
1220 (signed_cell)
1221 tree_find_integer_property(device *root,
1222                            const char *path_to_property)
1223 {
1224   name_specifier spec;
1225   if (!split_property_specifier(root, path_to_property, &spec))
1226     device_error(root, "Invalid property path %s", path_to_property);
1227   root = split_find_device(root, &spec);
1228   return device_find_integer_property(root, spec.property);
1229 }
1230
1231 INLINE_TREE\
1232 (device_instance *)
1233 tree_find_ihandle_property(device *root,
1234                            const char *path_to_property)
1235 {
1236   name_specifier spec;
1237   if (!split_property_specifier(root, path_to_property, &spec))
1238     device_error(root, "Invalid property path %s", path_to_property);
1239   root = split_find_device(root, &spec);
1240   return device_find_ihandle_property(root, spec.property);
1241 }
1242
1243 INLINE_TREE\
1244 (const char *)
1245 tree_find_string_property(device *root,
1246                           const char *path_to_property)
1247 {
1248   name_specifier spec;
1249   if (!split_property_specifier(root, path_to_property, &spec))
1250     device_error(root, "Invalid property path %s", path_to_property);
1251   root = split_find_device(root, &spec);
1252   return device_find_string_property(root, spec.property);
1253 }
1254
1255
1256 #endif /* _PARSE_C_ */