s390: Add record/replay support for arch13 instructions
[deliverable/binutils-gdb.git] / sim / ppc / tree.c
CommitLineData
c906108c
SS
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
3fd725ef 7 the Free Software Foundation; either version 3 of the License, or
c906108c
SS
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
51b318de 16 along with this program; if not, see <http://www.gnu.org/licenses/>.
c906108c
SS
17
18 */
19
20
21#ifndef _PARSE_C_
22#define _PARSE_C_
23
24#include <stdio.h>
25#include <stdarg.h>
26
27#include "basics.h"
28
29#include "device.h"
30#include "tree.h"
31
32
33#ifdef HAVE_STDLIB_H
34#include <stdlib.h>
35#endif
36
37#ifdef HAVE_STRING_H
38#include <string.h>
39#else
40#ifdef HAVE_STRINGS_H
41#include <strings.h>
42#endif
43#endif
44
45#include <ctype.h>
46
59f6d9d6 47#include "libiberty.h"
c906108c
SS
48
49/* manipulate/lookup device names */
50
51typedef 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
75STATIC_INLINE_TREE\
76(int)
77split_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
179STATIC_INLINE_DEVICE\
180(int)
181split_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
208STATIC_INLINE_TREE\
209(int)
210split_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
275STATIC_INLINE_TREE\
276(char *)
277split_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
305STATIC_INLINE_TREE\
306(device *)
307split_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
376STATIC_INLINE_TREE\
377(device *)
378split_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
401INLINE_TREE\
402(void)
403tree_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
421STATIC_INLINE_TREE\
422(const char *)
423skip_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
435STATIC_INLINE_TREE\
436(int)
437count_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
459STATIC_INLINE_TREE\
460(const char *)
461parse_address(device *current,
462 device *bus,
463 const char *chp,
464 device_unit *address)
465{
bebd2b35 466 ASSERT(device_nr_address_cells(bus) > 0);
c906108c
SS
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
475STATIC_INLINE_TREE\
476(const char *)
477parse_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;
bebd2b35 489 ASSERT(size->nr_cells > 0);
c906108c
SS
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
516STATIC_INLINE_TREE\
517(void)
518parse_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;
bebd2b35 526 device *bus = device_parent(current);
c906108c
SS
527
528 /* determine the number of reg entries by counting tokens */
bebd2b35
AC
529 nr_regs = count_entries(current, property_name, property_value,
530 1 + (device_nr_size_cells(bus) > 0));
c906108c
SS
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++) {
bebd2b35
AC
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
170cd118 542 memset(&regs[reg_nr].size, 0, sizeof (&regs[reg_nr].size));
c906108c
SS
543 }
544
545 /* create it */
546 device_add_reg_array_property(current, property_name,
547 regs, nr_regs);
548
d79fe0d6 549 free(regs);
c906108c
SS
550}
551
552
553/* { <child-address> <parent-address> <child-size> }* */
554
555STATIC_INLINE_TREE\
556(void)
557parse_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
d79fe0d6 586 free(ranges);
c906108c
SS
587}
588
589
590/* <integer> ... */
591
592STATIC_INLINE_TREE\
593(void)
594parse_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 }
ea5c7021 622 /* perhaps integer array property is better */
c906108c
SS
623 device_add_array_property(current, property_name, words,
624 sizeof(words[0]) * nr_entries);
625 }
626}
627
59f6d9d6
MM
628/* PROPERTY_VALUE is a raw property value. Quote it as required by
629 parse_string_property. It is the caller's responsibility to free
630 the memory returned. */
631
632EXTERN_TREE\
633(char *)
634tree_quote_property(const char *property_value)
635{
636 char *p;
637 char *ret;
638 const char *chp;
639 int quotees;
640
641 /* Count characters needing quotes in PROPERTY_VALUE. */
642 quotees = 0;
643 for (chp = property_value; *chp; ++chp)
644 if (*chp == '\\' || *chp == '"')
645 ++quotees;
646
647 ret = (char *) xmalloc (strlen (property_value)
648 + 2 /* quotes */
649 + quotees
650 + 1 /* terminator */);
651
652 p = ret;
653 /* Add the opening quote. */
654 *p++ = '"';
655 /* Copy the value. */
656 for (chp = property_value; *chp; ++chp)
657 if (*chp == '\\' || *chp == '"')
658 {
659 /* Quote this character. */
660 *p++ = '\\';
661 *p++ = *chp;
662 }
663 else
664 *p++ = *chp;
665 /* Add the closing quote. */
666 *p++ = '"';
667 /* Terminate the string. */
668 *p++ = '\0';
669
670 return ret;
671}
c906108c
SS
672
673/* <string> ... */
674
675STATIC_INLINE_TREE\
676(void)
677parse_string_property(device *current,
678 const char *property_name,
679 const char *property_value)
680{
681 char **strings;
682 const char *chp;
683 int nr_strings;
684 int approx_nr_strings;
685
686 /* get an estimate as to the number of strings by counting double
687 quotes */
688 approx_nr_strings = 2;
689 for (chp = property_value; *chp; chp++) {
690 if (*chp == '"')
691 approx_nr_strings++;
692 }
693 approx_nr_strings = (approx_nr_strings) / 2;
694
695 /* create a string buffer for that many (plus a null) */
696 strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
697
698 /* now find all the strings */
699 chp = property_value;
700 nr_strings = 0;
701 while (1) {
702
703 /* skip leading space */
704 while (*chp != '\0' && isspace(*chp))
705 chp += 1;
706 if (*chp == '\0')
707 break;
708
709 /* copy it in */
710 if (*chp == '"') {
711 /* a quoted string - watch for '\' et.al. */
712 /* estimate the size and allocate space for it */
713 int pos;
714 chp++;
715 pos = 0;
716 while (chp[pos] != '\0' && chp[pos] != '"') {
717 if (chp[pos] == '\\' && chp[pos+1] != '\0')
718 pos += 2;
719 else
720 pos += 1;
721 }
722 strings[nr_strings] = zalloc(pos + 1);
723 /* copy the string over */
724 pos = 0;
725 while (*chp != '\0' && *chp != '"') {
726 if (*chp == '\\' && *(chp+1) != '\0') {
727 strings[nr_strings][pos] = *(chp+1);
728 chp += 2;
729 pos++;
730 }
731 else {
732 strings[nr_strings][pos] = *chp;
733 chp += 1;
734 pos++;
735 }
736 }
737 if (*chp != '\0')
738 chp++;
739 strings[nr_strings][pos] = '\0';
740 }
741 else {
742 /* copy over a single unquoted token */
743 int len = 0;
744 while (chp[len] != '\0' && !isspace(chp[len]))
745 len++;
746 strings[nr_strings] = zalloc(len + 1);
747 strncpy(strings[nr_strings], chp, len);
748 strings[nr_strings][len] = '\0';
749 chp += len;
750 }
751 nr_strings++;
752 if (nr_strings > approx_nr_strings)
753 device_error(current, "String property %s badly formatted",
754 property_name);
755 }
756 ASSERT(strings[nr_strings] == NULL); /* from zalloc */
757
758 /* install it */
759 if (nr_strings == 0)
760 device_add_string_property(current, property_name, "");
761 else if (nr_strings == 1)
762 device_add_string_property(current, property_name, strings[0]);
763 else {
764 const char **specs = (const char**)strings; /* stop a bogus error */
765 device_add_string_array_property(current, property_name,
766 specs, nr_strings);
767 }
768
769 /* flush the created string */
770 while (nr_strings > 0) {
771 nr_strings--;
d79fe0d6 772 free(strings[nr_strings]);
c906108c 773 }
d79fe0d6 774 free(strings);
c906108c
SS
775}
776
777
778/* <path-to-ihandle-device> */
779
780STATIC_INLINE_TREE\
781(void)
782parse_ihandle_property(device *current,
783 const char *property,
784 const char *value)
785{
786 ihandle_runtime_property_spec ihandle;
787
788 /* pass the full path */
789 ihandle.full_path = value;
790
791 /* save this ready for the ihandle create */
792 device_add_ihandle_runtime_property(current, property,
793 &ihandle);
794}
795
796
797
798EXTERN_TREE\
799(device *)
800tree_parse(device *current,
801 const char *fmt,
802 ...)
803{
804 char device_specifier[1024];
805 name_specifier spec;
806
807 /* format the path */
808 {
809 va_list ap;
810 va_start(ap, fmt);
811 vsprintf(device_specifier, fmt, ap);
812 va_end(ap);
813 if (strlen(device_specifier) >= sizeof(device_specifier))
814 error("device_tree_add_deviced: buffer overflow\n");
815 }
816
817 /* construct the tree down to the final device */
818 current = split_fill_path(current, device_specifier, &spec);
819
820 /* is there an interrupt spec */
821 if (spec.property == NULL
822 && spec.value != NULL) {
823 char *op = split_value(&spec);
824 switch (op[0]) {
825 case '>':
826 {
827 char *my_port_name = split_value(&spec);
828 int my_port;
829 char *dest_port_name = split_value(&spec);
830 int dest_port;
831 name_specifier dest_spec;
832 char *dest_device_name = split_value(&spec);
833 device *dest;
834 /* find my name */
835 my_port = device_interrupt_decode(current, my_port_name,
836 output_port);
837 /* find the dest device and port */
838 dest = split_fill_path(current, dest_device_name, &dest_spec);
839 dest_port = device_interrupt_decode(dest, dest_port_name,
840 input_port);
841 /* connect the two */
842 device_interrupt_attach(current,
843 my_port,
844 dest,
845 dest_port,
846 permenant_object);
847 }
848 break;
849 default:
850 device_error(current, "unreconised interrupt spec %s\n", spec.value);
851 break;
852 }
853 }
854
855 /* is there a property */
856 if (spec.property != NULL) {
857 if (strcmp(spec.value, "true") == 0)
858 device_add_boolean_property(current, spec.property, 1);
859 else if (strcmp(spec.value, "false") == 0)
860 device_add_boolean_property(current, spec.property, 0);
861 else {
862 const device_property *property;
863 switch (spec.value[0]) {
864 case '*':
865 parse_ihandle_property(current, spec.property, spec.value + 1);
866 break;
867 case '[':
868 {
869 unsigned8 words[1024];
870 char *curr = spec.value + 1;
871 int nr_words = 0;
872 while (1) {
873 char *next;
874 words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
875 if (curr == next)
876 break;
877 curr = next;
878 nr_words += 1;
879 }
880 device_add_array_property(current, spec.property,
881 words, sizeof(words[0]) * nr_words);
882 }
883 break;
884 case '"':
885 parse_string_property(current, spec.property, spec.value);
886 break;
887 case '!':
888 spec.value++;
889 property = tree_find_property(current, spec.value);
890 if (property == NULL)
891 device_error(current, "property %s not found\n", spec.value);
892 device_add_duplicate_property(current,
893 spec.property,
894 property);
895 break;
896 default:
897 if (strcmp(spec.property, "reg") == 0
898 || strcmp(spec.property, "assigned-addresses") == 0
899 || strcmp(spec.property, "alternate-reg") == 0){
900 parse_reg_property(current, spec.property, spec.value);
901 }
902 else if (strcmp(spec.property, "ranges") == 0) {
903 parse_ranges_property(current, spec.property, spec.value);
904 }
905 else if (isdigit(spec.value[0])
906 || (spec.value[0] == '-' && isdigit(spec.value[1]))
907 || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
908 parse_integer_property(current, spec.property, spec.value);
909 }
910 else
911 parse_string_property(current, spec.property, spec.value);
912 break;
913 }
914 }
915 }
916 return current;
917}
918
919
920INLINE_TREE\
921(void)
922tree_traverse(device *root,
923 tree_traverse_function *prefix,
924 tree_traverse_function *postfix,
925 void *data)
926{
927 device *child;
928 if (prefix != NULL)
929 prefix(root, data);
930 for (child = device_child(root);
931 child != NULL;
932 child = device_sibling(child)) {
933 tree_traverse(child, prefix, postfix, data);
934 }
935 if (postfix != NULL)
936 postfix(root, data);
937}
938
939
940STATIC_INLINE_TREE\
941(void)
942print_address(device *bus,
943 const device_unit *phys)
944{
945 char unit[32];
946 device_encode_unit(bus, phys, unit, sizeof(unit));
947 printf_filtered(" %s", unit);
948}
949
950STATIC_INLINE_TREE\
951(void)
952print_size(device *bus,
953 const device_unit *size)
954{
955 int i;
956 for (i = 0; i < size->nr_cells; i++)
957 if (size->cells[i] != 0)
958 break;
959 if (i < size->nr_cells) {
960 printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
961 i++;
962 for (; i < size->nr_cells; i++)
963 printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
964 }
965 else
966 printf_filtered(" 0");
967}
968
969STATIC_INLINE_TREE\
970(void)
971print_reg_property(device *me,
972 const device_property *property)
973{
974 int reg_nr;
975 reg_property_spec reg;
976 for (reg_nr = 0;
977 device_find_reg_array_property(me, property->name, reg_nr, &reg);
978 reg_nr++) {
979 print_address(device_parent(me), &reg.address);
980 print_size(me, &reg.size);
981 }
982}
983
984STATIC_INLINE_TREE\
985(void)
986print_ranges_property(device *me,
987 const device_property *property)
988{
989 int range_nr;
990 range_property_spec range;
991 for (range_nr = 0;
992 device_find_range_array_property(me, property->name, range_nr, &range);
993 range_nr++) {
994 print_address(me, &range.child_address);
995 print_address(device_parent(me), &range.parent_address);
996 print_size(me, &range.size);
997 }
998}
999
1000STATIC_INLINE_TREE\
1001(void)
1002print_string(const char *string)
1003{
1004 printf_filtered(" \"");
1005 while (*string != '\0') {
1006 switch (*string) {
1007 case '"':
1008 printf_filtered("\\\"");
1009 break;
1010 case '\\':
1011 printf_filtered("\\\\");
1012 break;
1013 default:
1014 printf_filtered("%c", *string);
1015 break;
1016 }
1017 string++;
1018 }
1019 printf_filtered("\"");
1020}
1021
1022STATIC_INLINE_TREE\
1023(void)
1024print_string_array_property(device *me,
1025 const device_property *property)
1026{
1027 int nr;
1028 string_property_spec string;
1029 for (nr = 0;
1030 device_find_string_array_property(me, property->name, nr, &string);
1031 nr++) {
1032 print_string(string);
1033 }
1034}
1035
1036STATIC_INLINE_TREE\
1037(void)
1038print_properties(device *me)
1039{
1040 const device_property *property;
1041 for (property = device_find_property(me, NULL);
1042 property != NULL;
1043 property = device_next_property(property)) {
1044 printf_filtered("%s/%s", device_path(me), property->name);
1045 if (property->original != NULL) {
1046 printf_filtered(" !");
1047 printf_filtered("%s/%s",
1048 device_path(property->original->owner),
1049 property->original->name);
1050 }
1051 else {
1052 switch (property->type) {
1053 case array_property:
1054 if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
1055 unsigned_cell *w = (unsigned_cell*)property->array;
1056 int cell_nr;
1057 for (cell_nr = 0;
1058 cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
1059 cell_nr++) {
1060 printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
1061 }
1062 }
1063 else {
1064 unsigned8 *w = (unsigned8*)property->array;
1065 printf_filtered(" [");
1066 while ((char*)w - (char*)property->array < property->sizeof_array) {
1067 printf_filtered(" 0x%2x", BE2H_1(*w));
1068 w++;
1069 }
1070 }
1071 break;
1072 case boolean_property:
1073 {
1074 int b = device_find_boolean_property(me, property->name);
1075 printf_filtered(" %s", b ? "true" : "false");
1076 }
1077 break;
1078 case ihandle_property:
1079 {
1080 if (property->array != NULL) {
1081 device_instance *instance = device_find_ihandle_property(me, property->name);
1082 printf_filtered(" *%s", device_instance_path(instance));
1083 }
1084 else {
1085 /* not yet initialized, ask the device for the path */
1086 ihandle_runtime_property_spec spec;
1087 device_find_ihandle_runtime_property(me, property->name, &spec);
1088 printf_filtered(" *%s", spec.full_path);
1089 }
1090 }
1091 break;
1092 case integer_property:
1093 {
1094 unsigned_word w = device_find_integer_property(me, property->name);
1095 printf_filtered(" 0x%lx", (unsigned long)w);
1096 }
1097 break;
1098 case range_array_property:
1099 print_ranges_property(me, property);
1100 break;
1101 case reg_array_property:
1102 print_reg_property(me, property);
1103 break;
1104 case string_property:
1105 {
1106 const char *s = device_find_string_property(me, property->name);
1107 print_string(s);
1108 }
1109 break;
1110 case string_array_property:
1111 print_string_array_property(me, property);
1112 break;
1113 }
1114 }
1115 printf_filtered("\n");
1116 }
1117}
1118
1119STATIC_INLINE_TREE\
1120(void)
1121print_interrupts(device *me,
1122 int my_port,
1123 device *dest,
1124 int dest_port,
1125 void *ignore_or_null)
1126{
1127 char src[32];
1128 char dst[32];
1129 device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
1130 device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
1131 printf_filtered("%s > %s %s %s\n",
1132 device_path(me),
1133 src, dst,
1134 device_path(dest));
1135}
1136
1137STATIC_INLINE_TREE\
1138(void)
1139print_device(device *me,
1140 void *ignore_or_null)
1141{
1142 printf_filtered("%s\n", device_path(me));
1143 print_properties(me);
1144 device_interrupt_traverse(me, print_interrupts, NULL);
1145}
1146
1147INLINE_TREE\
1148(void)
1149tree_print(device *root)
1150{
1151 tree_traverse(root,
1152 print_device, NULL,
1153 NULL);
1154}
1155
1156
1157INLINE_TREE\
1158(void)
1159tree_usage(int verbose)
1160{
1161 if (verbose == 1) {
1162 printf_filtered("\n");
1163 printf_filtered("A device/property specifier has the form:\n");
1164 printf_filtered("\n");
1165 printf_filtered(" /path/to/a/device [ property-value ]\n");
1166 printf_filtered("\n");
1167 printf_filtered("and a possible device is\n");
1168 printf_filtered("\n");
1169 }
1170 if (verbose > 1) {
1171 printf_filtered("\n");
1172 printf_filtered("A device/property specifier (<spec>) has the format:\n");
1173 printf_filtered("\n");
1174 printf_filtered(" <spec> ::= <path> [ <value> ] ;\n");
1175 printf_filtered(" <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
1176 printf_filtered(" <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
1177 printf_filtered(" <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
1178 printf_filtered(" <unit> ::= <number> { \",\" <number> } ;\n");
1179 printf_filtered("\n");
1180 printf_filtered("Where:\n");
1181 printf_filtered("\n");
1182 printf_filtered(" <name> is the name of a device (list below)\n");
1183 printf_filtered(" <unit> is the unit-address relative to the parent bus\n");
1184 printf_filtered(" <args> additional arguments used when creating the device\n");
1185 printf_filtered(" <value> ::= ( <number> # integer property\n");
1186 printf_filtered(" | \"[\" { <number> } # array property (byte)\n");
1187 printf_filtered(" | \"{\" { <number> } # array property (cell)\n");
1188 printf_filtered(" | [ \"true\" | \"false\" ] # boolean property\n");
1189 printf_filtered(" | \"*\" <path> # ihandle property\n");
1190 printf_filtered(" | \"!\" <path> # copy property\n");
1191 printf_filtered(" | \">\" [ <number> ] <path> # attach interrupt\n");
1192 printf_filtered(" | \"<\" <path> # attach child interrupt\n");
1193 printf_filtered(" | \"\\\"\" <text> # string property\n");
1194 printf_filtered(" | <text> # string property\n");
1195 printf_filtered(" ) ;\n");
1196 printf_filtered("\n");
1197 printf_filtered("And the following are valid device names:\n");
1198 printf_filtered("\n");
1199 }
1200}
1201
1202
1203
1204INLINE_TREE\
1205(device_instance *)
1206tree_instance(device *root,
1207 const char *device_specifier)
1208{
1209 /* find the device node */
1210 device *me;
1211 name_specifier spec;
1212 if (!split_device_specifier(root, device_specifier, &spec))
1213 return NULL;
1214 me = split_find_device(root, &spec);
1215 if (spec.name != NULL)
1216 return NULL;
1217 /* create the instance */
1218 return device_create_instance(me, device_specifier, spec.last_args);
1219}
1220
1221
1222INLINE_TREE\
1223(device *)
1224tree_find_device(device *root,
1225 const char *path_to_device)
1226{
1227 device *node;
1228 name_specifier spec;
1229
1230 /* parse the path */
1231 split_device_specifier(root, path_to_device, &spec);
1232 if (spec.value != NULL)
1233 return NULL; /* something wierd */
1234
1235 /* now find it */
1236 node = split_find_device(root, &spec);
1237 if (spec.name != NULL)
1238 return NULL; /* not a leaf */
1239
1240 return node;
1241}
1242
1243
1244INLINE_TREE\
1245(const device_property *)
1246tree_find_property(device *root,
1247 const char *path_to_property)
1248{
1249 name_specifier spec;
1250 if (!split_property_specifier(root, path_to_property, &spec))
1251 device_error(root, "Invalid property path %s", path_to_property);
1252 root = split_find_device(root, &spec);
1253 return device_find_property(root, spec.property);
1254}
1255
1256INLINE_TREE\
1257(int)
1258tree_find_boolean_property(device *root,
1259 const char *path_to_property)
1260{
1261 name_specifier spec;
1262 if (!split_property_specifier(root, path_to_property, &spec))
1263 device_error(root, "Invalid property path %s", path_to_property);
1264 root = split_find_device(root, &spec);
1265 return device_find_boolean_property(root, spec.property);
1266}
1267
1268INLINE_TREE\
1269(signed_cell)
1270tree_find_integer_property(device *root,
1271 const char *path_to_property)
1272{
1273 name_specifier spec;
1274 if (!split_property_specifier(root, path_to_property, &spec))
1275 device_error(root, "Invalid property path %s", path_to_property);
1276 root = split_find_device(root, &spec);
1277 return device_find_integer_property(root, spec.property);
1278}
1279
1280INLINE_TREE\
1281(device_instance *)
1282tree_find_ihandle_property(device *root,
1283 const char *path_to_property)
1284{
1285 name_specifier spec;
1286 if (!split_property_specifier(root, path_to_property, &spec))
1287 device_error(root, "Invalid property path %s", path_to_property);
1288 root = split_find_device(root, &spec);
1289 return device_find_ihandle_property(root, spec.property);
1290}
1291
1292INLINE_TREE\
1293(const char *)
1294tree_find_string_property(device *root,
1295 const char *path_to_property)
1296{
1297 name_specifier spec;
1298 if (!split_property_specifier(root, path_to_property, &spec))
1299 device_error(root, "Invalid property path %s", path_to_property);
1300 root = split_find_device(root, &spec);
1301 return device_find_string_property(root, spec.property);
1302}
1303
1304
1305#endif /* _PARSE_C_ */
This page took 0.899194 seconds and 4 git commands to generate.