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