OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-7.1/] [sim/] [ppc/] [tree.c] - Blame information for rev 842

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.