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