* dache.c (struct dcache_block): Change data member from unsigned char
[external/binutils.git] / gdb / ch-valprint.c
1 /* Support for printing Chill values for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1989, 1991, 1992, 1993, 1994
3    Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "defs.h"
22 #include "obstack.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "valprint.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "language.h"
29 #include "demangle.h"
30 #include "c-lang.h" /* For c_val_print */
31 #include "typeprint.h"
32 #include "ch-lang.h"
33 #include "annotate.h"
34
35 static void
36 chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
37                                   enum val_prettyprint, struct type **));
38
39 \f
40 /* Print integral scalar data VAL, of type TYPE, onto stdio stream STREAM.
41    Used to print data from type structures in a specified type.  For example,
42    array bounds may be characters or booleans in some languages, and this
43    allows the ranges to be printed in their "natural" form rather than as
44    decimal integer values. */
45
46 void
47 chill_print_type_scalar (type, val, stream)
48      struct type *type;
49      LONGEST val;
50      GDB_FILE *stream;
51 {
52   switch (TYPE_CODE (type))
53     {
54     case TYPE_CODE_RANGE:
55       if (TYPE_TARGET_TYPE (type))
56         {
57           chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
58           return;
59         }
60       break;
61     case TYPE_CODE_UNDEF:
62     case TYPE_CODE_PTR:
63     case TYPE_CODE_ARRAY:
64     case TYPE_CODE_STRUCT:
65     case TYPE_CODE_UNION:
66     case TYPE_CODE_ENUM:
67     case TYPE_CODE_FUNC:
68     case TYPE_CODE_INT:
69     case TYPE_CODE_FLT:
70     case TYPE_CODE_VOID:
71     case TYPE_CODE_SET:
72     case TYPE_CODE_STRING:
73     case TYPE_CODE_BITSTRING:
74     case TYPE_CODE_ERROR:
75     case TYPE_CODE_MEMBER:
76     case TYPE_CODE_METHOD:
77     case TYPE_CODE_REF:
78     case TYPE_CODE_CHAR:
79     case TYPE_CODE_BOOL:
80     case TYPE_CODE_COMPLEX:
81     default:
82       break;
83     }
84   print_type_scalar (type, val, stream);
85 }
86 \f
87 /* Print the elements of an array.
88    Similar to val_print_array_elements, but prints
89    element indexes (in Chill syntax). */
90
91 static void
92 chill_val_print_array_elements (type, valaddr, address, stream,
93                                 format, deref_ref, recurse, pretty)
94      struct type *type;
95      char *valaddr;
96      CORE_ADDR address;
97      GDB_FILE *stream;
98      int format;
99      int deref_ref;
100      int recurse;
101      enum val_prettyprint pretty;
102 {
103   unsigned int i = 0;
104   unsigned int things_printed = 0;
105   unsigned len;
106   struct type *elttype;
107   struct type *range_type = TYPE_FIELD_TYPE (type, 0);
108   struct type *index_type = TYPE_TARGET_TYPE (range_type);
109   unsigned eltlen;
110   /* Position of the array element we are examining to see
111      whether it is repeated.  */
112   unsigned int rep1;
113   /* Number of repetitions we have detected so far.  */
114   unsigned int reps;
115   LONGEST low_bound =  TYPE_FIELD_BITPOS (range_type, 0);
116       
117   elttype = TYPE_TARGET_TYPE (type);
118   eltlen = TYPE_LENGTH (elttype);
119   len = TYPE_LENGTH (type) / eltlen;
120
121   annotate_array_section_begin (i, elttype);
122
123   for (; i < len && things_printed < print_max; i++)
124     {
125       if (i != 0)
126         {
127           if (prettyprint_arrays)
128             {
129               fprintf_filtered (stream, ",\n");
130               print_spaces_filtered (2 + 2 * recurse, stream);
131             }
132           else
133             {
134               fprintf_filtered (stream, ", ");
135             }
136         }
137       wrap_here (n_spaces (2 + 2 * recurse));
138
139       rep1 = i + 1;
140       reps = 1;
141       while ((rep1 < len) && 
142              !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
143         {
144           ++reps;
145           ++rep1;
146         }
147
148       fputs_filtered ("(", stream);
149       chill_print_type_scalar (index_type, low_bound + i, stream);
150       if (reps > 1)
151         {
152           fputs_filtered (":", stream);
153           chill_print_type_scalar (index_type, low_bound + i + reps - 1,
154                                    stream);
155           fputs_filtered ("): ", stream);
156           val_print (elttype, valaddr + i * eltlen, 0, stream, format,
157                      deref_ref, recurse + 1, pretty);
158
159           i = rep1 - 1;
160           things_printed += 1;
161         }
162       else
163         {
164           fputs_filtered ("): ", stream);
165           val_print (elttype, valaddr + i * eltlen, 0, stream, format,
166                      deref_ref, recurse + 1, pretty);
167           annotate_elt ();
168           things_printed++;
169         }
170     }
171   annotate_array_section_end ();
172   if (i < len)
173     {
174       fprintf_filtered (stream, "...");
175     }
176 }
177
178 /* Print data of type TYPE located at VALADDR (within GDB), which came from
179    the inferior at address ADDRESS, onto stdio stream STREAM according to
180    FORMAT (a letter or 0 for natural format).  The data at VALADDR is in
181    target byte order.
182
183    If the data are a string pointer, returns the number of string characters
184    printed.
185
186    If DEREF_REF is nonzero, then dereference references, otherwise just print
187    them like pointers.
188
189    The PRETTY parameter controls prettyprinting.  */
190
191 int
192 chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
193                  pretty)
194      struct type *type;
195      char *valaddr;
196      CORE_ADDR address;
197      GDB_FILE *stream;
198      int format;
199      int deref_ref;
200      int recurse;
201      enum val_prettyprint pretty;
202 {
203   LONGEST val;
204   unsigned int i = 0;           /* Number of characters printed.  */
205   struct type *elttype;
206   CORE_ADDR addr;
207
208   switch (TYPE_CODE (type))
209     {
210     case TYPE_CODE_ARRAY:
211       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
212         {
213           if (prettyprint_arrays)
214             {
215               print_spaces_filtered (2 + 2 * recurse, stream);
216             }
217           fprintf_filtered (stream, "[");
218           chill_val_print_array_elements (type, valaddr, address, stream,
219                                           format, deref_ref, recurse, pretty);
220           fprintf_filtered (stream, "]");
221         }
222       else
223         {
224           error ("unimplemented in chill_val_print; unspecified array length");
225         }
226       break;
227
228     case TYPE_CODE_INT:
229       format = format ? format : output_format;
230       if (format)
231         {
232           print_scalar_formatted (valaddr, type, format, 0, stream);
233         }
234       else
235         {
236           val_print_type_code_int (type, valaddr, stream);
237         }
238       break;
239
240     case TYPE_CODE_CHAR:
241       format = format ? format : output_format;
242       if (format)
243         {
244           print_scalar_formatted (valaddr, type, format, 0, stream);
245         }
246       else
247         {
248           LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
249                          stream);
250         }
251       break;
252
253     case TYPE_CODE_FLT:
254       if (format)
255         {
256           print_scalar_formatted (valaddr, type, format, 0, stream);
257         }
258       else
259         {
260           print_floating (valaddr, type, stream);
261         }
262       break;
263
264     case TYPE_CODE_BOOL:
265       format = format ? format : output_format;
266       if (format)
267         {
268           print_scalar_formatted (valaddr, type, format, 0, stream);
269         }
270       else
271         {
272           /* FIXME: Why is this using builtin_type_chill_bool not type?  */
273           val = unpack_long (builtin_type_chill_bool, valaddr);
274           fprintf_filtered (stream, val ? "TRUE" : "FALSE");
275         }
276       break;
277
278     case TYPE_CODE_UNDEF:
279       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
280          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
281          and no complete type for struct foo in that file.  */
282       fprintf_filtered (stream, "<incomplete type>");
283       break;
284
285     case TYPE_CODE_PTR:
286       if (format && format != 's')
287         {
288           print_scalar_formatted (valaddr, type, format, 0, stream);
289           break;
290         }
291       addr = unpack_pointer (type, valaddr);
292       elttype = TYPE_TARGET_TYPE (type);
293
294       /* We assume a NULL pointer is all zeros ... */
295       if (addr == 0)
296         {
297           fputs_filtered ("NULL", stream);
298           return 0;
299         }
300       
301       if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
302         {
303           /* Try to print what function it points to.  */
304           print_address_demangle (addr, stream, demangle);
305           /* Return value is irrelevant except for string pointers.  */
306           return (0);
307         }
308       if (addressprint && format != 's')
309         {
310           print_address_numeric (addr, 1, stream);
311         }
312       
313       /* For a pointer to char or unsigned char, also print the string
314          pointed to, unless pointer is null.  */
315       if (TYPE_LENGTH (elttype) == 1
316           && TYPE_CODE (elttype) == TYPE_CODE_CHAR
317           && (format == 0 || format == 's')
318           && addr != 0
319           && /* If print_max is UINT_MAX, the alloca below will fail.
320                 In that case don't try to print the string.  */
321           print_max < UINT_MAX)
322           {
323             i = val_print_string (addr, 0, stream);
324           }
325       /* Return number of characters printed, plus one for the
326          terminating null if we have "reached the end".  */
327       return (i + (print_max && i != print_max));
328       break;
329
330     case TYPE_CODE_STRING:
331       i = TYPE_LENGTH (type);
332       LA_PRINT_STRING (stream, valaddr, i, 0);
333       /* Return number of characters printed, plus one for the terminating
334          null if we have "reached the end".  */
335       return (i + (print_max && i != print_max));
336       break;
337
338     case TYPE_CODE_BITSTRING:
339     case TYPE_CODE_SET:
340       elttype = TYPE_INDEX_TYPE (type);
341       check_stub_type (elttype);
342       if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
343         {
344           fprintf_filtered (stream, "<incomplete type>");
345           gdb_flush (stream);
346           break;
347         }
348       {
349         struct type *range = elttype;
350         int low_bound = TYPE_LOW_BOUND (range);
351         int high_bound = TYPE_HIGH_BOUND (range);
352         int i;
353         int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
354         int need_comma = 0;
355
356         if (is_bitstring)
357           fputs_filtered ("B'", stream);
358         else
359           fputs_filtered ("[", stream);
360         for (i = low_bound; i <= high_bound; i++)
361           {
362             int element = value_bit_index (type, valaddr, i);
363             if (is_bitstring)
364               fprintf_filtered (stream, "%d", element);
365             else if (element)
366               {
367                 if (need_comma)
368                   fputs_filtered (", ", stream);
369                 chill_print_type_scalar (range, i, stream);
370                 need_comma = 1;
371
372                 /* Look for a continuous range of true elements. */
373                 if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
374                   {
375                     int j = i; /* j is the upper bound so far of the range */
376                     fputs_filtered (":", stream);
377                     while (i+1 <= high_bound
378                            && value_bit_index (type, valaddr, ++i))
379                       j = i;
380                     chill_print_type_scalar (range, j, stream);
381                   }
382               }
383           }
384         if (is_bitstring)
385           fputs_filtered ("'", stream);
386         else
387           fputs_filtered ("]", stream);
388       }
389       break;
390
391     case TYPE_CODE_STRUCT:
392       if (chill_varying_type (type))
393         {
394           struct type *inner = TYPE_FIELD_TYPE (type, 1);
395           long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
396           char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
397           
398           switch (TYPE_CODE (inner))
399             {
400             case TYPE_CODE_STRING:
401               if (length > TYPE_LENGTH (type))
402                 {
403                   fprintf_filtered (stream,
404                                     "<dynamic length %ld > static length %d>",
405                                     length, TYPE_LENGTH (type));
406                 }
407               LA_PRINT_STRING (stream, data_addr, length, 0);
408               return length;
409             default:
410               break;
411             }
412         }
413       chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
414                                 0);
415       break;
416
417     case TYPE_CODE_REF:
418       if (addressprint)
419         {
420           fprintf_filtered (stream, "LOC(");
421           print_address_numeric
422             (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
423              1,
424              stream);
425           fprintf_filtered (stream, ")");
426           if (deref_ref)
427             fputs_filtered (": ", stream);
428         }
429       /* De-reference the reference.  */
430       if (deref_ref)
431         {
432           if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
433             {
434               value_ptr deref_val =
435                 value_at
436                   (TYPE_TARGET_TYPE (type),
437                    unpack_pointer (lookup_pointer_type (builtin_type_void),
438                                    valaddr));
439               val_print (VALUE_TYPE (deref_val),
440                          VALUE_CONTENTS (deref_val),
441                          VALUE_ADDRESS (deref_val), stream, format,
442                          deref_ref, recurse + 1, pretty);
443             }
444           else
445             fputs_filtered ("???", stream);
446         }
447       break;
448
449     case TYPE_CODE_ENUM:
450       c_val_print (type, valaddr, address, stream, format,
451                    deref_ref, recurse, pretty);
452       break;
453
454     case TYPE_CODE_RANGE:
455       if (TYPE_TARGET_TYPE (type))
456         chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
457                          format, deref_ref, recurse, pretty);
458       break;
459
460     case TYPE_CODE_MEMBER:
461     case TYPE_CODE_UNION:
462     case TYPE_CODE_FUNC:
463     case TYPE_CODE_VOID:
464     case TYPE_CODE_ERROR:
465     default:
466       /* Let's defer printing to the C printer, rather than
467          print an error message.  FIXME! */
468       c_val_print (type, valaddr, address, stream, format,
469                    deref_ref, recurse, pretty);
470     }
471   gdb_flush (stream);
472   return (0);
473 }
474
475 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
476    print out a structure's fields: cp_print_value_fields and cplus_print_value.
477
478    TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
479    same meanings as in cplus_print_value and c_val_print.
480
481    DONT_PRINT is an array of baseclass types that we
482    should not print, or zero if called from top level.  */
483
484 static void
485 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
486                           dont_print)
487      struct type *type;
488      char *valaddr;
489      GDB_FILE *stream;
490      int format;
491      int recurse;
492      enum val_prettyprint pretty;
493      struct type **dont_print;
494 {
495   int i, len;
496   int fields_seen = 0;
497
498   check_stub_type (type);
499
500   fprintf_filtered (stream, "[");
501   len = TYPE_NFIELDS (type);
502   if (len == 0)
503     {
504       fprintf_filtered (stream, "<No data fields>");
505     }
506   else
507     {
508       for (i = 0; i < len; i++)
509         {
510           if (fields_seen)
511             {
512               fprintf_filtered (stream, ", ");
513             }
514           fields_seen = 1;
515           if (pretty)
516             {
517               fprintf_filtered (stream, "\n");
518               print_spaces_filtered (2 + 2 * recurse, stream);
519             }
520           else 
521             {
522               wrap_here (n_spaces (2 + 2 * recurse));
523             }
524           fputs_filtered (".", stream);
525           fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
526                                    language_chill, DMGL_NO_OPTS);
527           fputs_filtered (": ", stream);
528           if (TYPE_FIELD_PACKED (type, i))
529             {
530               value_ptr v;
531
532               /* Bitfields require special handling, especially due to byte
533                  order problems.  */
534               v = value_from_longest (TYPE_FIELD_TYPE (type, i),
535                                       unpack_field_as_long (type, valaddr, i));
536
537               chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
538                                stream, format, 0, recurse + 1, pretty);
539             }
540           else
541             {
542               chill_val_print (TYPE_FIELD_TYPE (type, i), 
543                                valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
544                                0, stream, format, 0, recurse + 1, pretty);
545             }
546         }
547       if (pretty)
548         {
549           fprintf_filtered (stream, "\n");
550           print_spaces_filtered (2 * recurse, stream);
551         }
552     }
553   fprintf_filtered (stream, "]");
554 }
555 \f
556 int
557 chill_value_print (val, stream, format, pretty)
558      value_ptr val;
559      GDB_FILE *stream;
560      int format;
561      enum val_prettyprint pretty;
562 {
563   /* A "repeated" value really contains several values in a row.
564      They are made by the @ operator.
565      Print such values as if they were arrays.  */
566
567   if (VALUE_REPEATED (val))
568     {
569       register unsigned int n = VALUE_REPETITIONS (val);
570       register unsigned int typelen = TYPE_LENGTH (VALUE_TYPE (val));
571       fprintf_filtered (stream, "[");
572       /* Print arrays of characters using string syntax.  */
573       if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
574           && format == 0)
575         LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
576       else
577         {
578           value_print_array_elements (val, stream, format, pretty);
579         }
580       fprintf_filtered (stream, "]");
581       return (n * typelen);
582     }
583   else
584     {
585       struct type *type = VALUE_TYPE (val);
586
587       /* If it is a pointer, indicate what it points to.
588
589          Print type also if it is a reference.
590
591          C++: if it is a member pointer, we will take care
592          of that when we print it.  */
593       if (TYPE_CODE (type) == TYPE_CODE_PTR ||
594           TYPE_CODE (type) == TYPE_CODE_REF)
595         {
596           char *valaddr = VALUE_CONTENTS (val);
597           CORE_ADDR addr = unpack_pointer (type, valaddr);
598           if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
599             {
600               int i;
601               char *name = TYPE_NAME (type);
602               if (name)
603                 fputs_filtered (name, stream);
604               else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
605                 fputs_filtered ("PTR", stream);
606               else
607                 {
608                   fprintf_filtered (stream, "(");
609                   type_print (type, "", stream, -1);
610                   fprintf_filtered (stream, ")");
611                 }
612               fprintf_filtered (stream, "(");
613               i = val_print (type, valaddr, VALUE_ADDRESS (val),
614                              stream, format, 1, 0, pretty);
615               fprintf_filtered (stream, ")");
616               return i;
617             }
618         }
619       return (val_print (type, VALUE_CONTENTS (val),
620                          VALUE_ADDRESS (val), stream, format, 1, 0, pretty));
621     }
622 }
623
624