2010-05-15 Michael Snyder <msnyder@vmware.com>
[external/binutils.git] / gdb / m2-valprint.c
1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1988, 1989, 1991, 1992, 1996, 1998, 2000, 2005, 2006,
4                  2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "expression.h"
25 #include "value.h"
26 #include "valprint.h"
27 #include "language.h"
28 #include "typeprint.h"
29 #include "c-lang.h"
30 #include "m2-lang.h"
31 #include "target.h"
32
33 static int print_unpacked_pointer (struct type *type,
34                                    CORE_ADDR address, CORE_ADDR addr,
35                                    const struct value_print_options *options,
36                                    struct ui_file *stream);
37 static void
38 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
39                          int embedded_offset, CORE_ADDR address,
40                          struct ui_file *stream, int recurse,
41                          const struct value_print_options *options,
42                          int len);
43
44
45 /* Print function pointer with inferior address ADDRESS onto stdio
46    stream STREAM.  */
47
48 static void
49 print_function_pointer_address (struct gdbarch *gdbarch, CORE_ADDR address,
50                                 struct ui_file *stream, int addressprint)
51 {
52   CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (gdbarch, address,
53                                                             &current_target);
54
55   /* If the function pointer is represented by a description, print the
56      address of the description.  */
57   if (addressprint && func_addr != address)
58     {
59       fputs_filtered ("@", stream);
60       fputs_filtered (paddress (gdbarch, address), stream);
61       fputs_filtered (": ", stream);
62     }
63   print_address_demangle (gdbarch, func_addr, stream, demangle);
64 }
65
66 /* get_long_set_bounds - assigns the bounds of the long set to low and
67                          high.  */
68
69 int
70 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
71 {
72   int len, i;
73
74   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
75     {
76       len = TYPE_NFIELDS (type);
77       i = TYPE_N_BASECLASSES (type);
78       if (len == 0)
79         return 0;
80       *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
81       *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
82                                                                  len-1)));
83       return 1;
84     }
85   error (_("expecting long_set"));
86   return 0;
87 }
88
89 static void
90 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
91                    int embedded_offset, CORE_ADDR address,
92                    struct ui_file *stream)
93 {
94   int empty_set        = 1;
95   int element_seen     = 0;
96   LONGEST previous_low = 0;
97   LONGEST previous_high= 0;
98   LONGEST i, low_bound, high_bound;
99   LONGEST field_low, field_high;
100   struct type *range;
101   int len, field;
102   struct type *target;
103   int bitval;
104
105   CHECK_TYPEDEF (type);
106
107   fprintf_filtered (stream, "{");
108   len = TYPE_NFIELDS (type);
109   if (get_long_set_bounds (type, &low_bound, &high_bound))
110     {
111       field = TYPE_N_BASECLASSES (type);
112       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
113     }
114   else
115     {
116       fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
117       return;
118     }
119
120   target = TYPE_TARGET_TYPE (range);
121
122   if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
123     {
124       for (i = low_bound; i <= high_bound; i++)
125         {
126           bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
127                                     (TYPE_FIELD_BITPOS (type, field) / 8) +
128                                     valaddr + embedded_offset, i);
129           if (bitval < 0)
130             error (_("bit test is out of range"));
131           else if (bitval > 0)
132             {
133               previous_high = i;
134               if (! element_seen)
135                 {
136                   if (! empty_set)
137                     fprintf_filtered (stream, ", ");
138                   print_type_scalar (target, i, stream);
139                   empty_set    = 0;
140                   element_seen = 1;
141                   previous_low = i;
142                 }
143             }
144           else
145             {
146               /* bit is not set */
147               if (element_seen)
148                 {
149                   if (previous_low+1 < previous_high)
150                     fprintf_filtered (stream, "..");
151                   if (previous_low+1 < previous_high)
152                     print_type_scalar (target, previous_high, stream);
153                   element_seen = 0;
154                 }
155             }
156           if (i == field_high)
157             {
158               field++;
159               if (field == len)
160                 break;
161               range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
162               if (get_discrete_bounds (range, &field_low, &field_high) < 0)
163                 break;
164               target = TYPE_TARGET_TYPE (range);
165             }
166         }
167       if (element_seen)
168         {
169           if (previous_low+1 < previous_high)
170             {
171               fprintf_filtered (stream, "..");
172               print_type_scalar (target, previous_high, stream);
173             }
174           element_seen = 0;
175         }
176       fprintf_filtered (stream, "}");
177     }
178 }
179
180 static void
181 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
182                           int embedded_offset, CORE_ADDR address,
183                           struct ui_file *stream, int recurse,
184                           const struct value_print_options *options)
185 {
186   struct type *content_type;
187   CORE_ADDR addr;
188   LONGEST len;
189   struct value *val;
190
191   CHECK_TYPEDEF (type);
192   content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
193
194   addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
195                          (TYPE_FIELD_BITPOS (type, 0) / 8) +
196                          valaddr + embedded_offset);
197
198   val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
199                        addr);
200   len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
201
202   fprintf_filtered (stream, "{");  
203   m2_print_array_contents (value_type (val), value_contents(val),
204                            value_embedded_offset (val), addr, stream,
205                            recurse, options, len);
206   fprintf_filtered (stream, ", HIGH = %d}", (int) len);
207 }
208
209 static int
210 print_unpacked_pointer (struct type *type,
211                         CORE_ADDR address, CORE_ADDR addr,
212                         const struct value_print_options *options,
213                         struct ui_file *stream)
214 {
215   struct gdbarch *gdbarch = get_type_arch (type);
216   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
217
218   if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
219     {
220       /* Try to print what function it points to.  */
221       print_function_pointer_address (gdbarch, addr, stream,
222                                       options->addressprint);
223       /* Return value is irrelevant except for string pointers.  */
224       return 0;
225     }
226
227   if (options->addressprint && options->format != 's')
228     fputs_filtered (paddress (gdbarch, address), stream);
229
230   /* For a pointer to char or unsigned char, also print the string
231      pointed to, unless pointer is null.  */
232
233   if (TYPE_LENGTH (elttype) == 1
234       && TYPE_CODE (elttype) == TYPE_CODE_INT
235       && (options->format == 0 || options->format == 's')
236       && addr != 0)
237     return val_print_string (TYPE_TARGET_TYPE (type), addr, -1,
238                              stream, options);
239   
240   return 0;
241 }
242
243 static void
244 print_variable_at_address (struct type *type,
245                            const gdb_byte *valaddr,
246                            struct ui_file *stream,
247                            int recurse,
248                            const struct value_print_options *options)
249 {
250   struct gdbarch *gdbarch = get_type_arch (type);
251   CORE_ADDR addr = unpack_pointer (type, valaddr);
252   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
253
254   fprintf_filtered (stream, "[");
255   fputs_filtered (paddress (gdbarch, addr), stream);
256   fprintf_filtered (stream, "] : ");
257   
258   if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
259     {
260       struct value *deref_val =
261         value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
262
263       common_val_print (deref_val, stream, recurse, options, current_language);
264     }
265   else
266     fputs_filtered ("???", stream);
267 }
268
269
270 /* m2_print_array_contents - prints out the contents of an
271                              array up to a max_print values.
272                              It prints arrays of char as a string
273                              and all other data types as comma
274                              separated values.  */
275
276 static void
277 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
278                          int embedded_offset, CORE_ADDR address,
279                          struct ui_file *stream, int recurse,
280                          const struct value_print_options *options,
281                          int len)
282 {
283   int eltlen;
284   CHECK_TYPEDEF (type);
285
286   if (TYPE_LENGTH (type) > 0)
287     {
288       eltlen = TYPE_LENGTH (type);
289       if (options->prettyprint_arrays)
290         print_spaces_filtered (2 + 2 * recurse, stream);
291       /* For an array of chars, print with string syntax.  */
292       if (eltlen == 1 &&
293           ((TYPE_CODE (type) == TYPE_CODE_INT)
294            || ((current_language->la_language == language_m2)
295                && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
296           && (options->format == 0 || options->format == 's'))
297         val_print_string (type, address, len+1, stream, options);
298       else
299         {
300           fprintf_filtered (stream, "{");
301           val_print_array_elements (type, valaddr + embedded_offset,
302                                     address, stream, recurse, options, 0);
303           fprintf_filtered (stream, "}");
304         }
305     }
306 }
307
308
309 /* Print data of type TYPE located at VALADDR (within GDB), which came from
310    the inferior at address ADDRESS, onto stdio stream STREAM according to
311    OPTIONS.  The data at VALADDR is in target byte order.
312
313    If the data are a string pointer, returns the number of string characters
314    printed.  */
315
316 int
317 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
318               CORE_ADDR address, struct ui_file *stream, int recurse,
319               const struct value_print_options *options)
320 {
321   struct gdbarch *gdbarch = get_type_arch (type);
322   unsigned int i = 0;   /* Number of characters printed */
323   unsigned len;
324   struct type *elttype;
325   unsigned eltlen;
326   LONGEST val;
327   CORE_ADDR addr;
328
329   CHECK_TYPEDEF (type);
330   switch (TYPE_CODE (type))
331     {
332     case TYPE_CODE_ARRAY:
333       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
334         {
335           elttype = check_typedef (TYPE_TARGET_TYPE (type));
336           eltlen = TYPE_LENGTH (elttype);
337           len = TYPE_LENGTH (type) / eltlen;
338           if (options->prettyprint_arrays)
339             print_spaces_filtered (2 + 2 * recurse, stream);
340           /* For an array of chars, print with string syntax.  */
341           if (eltlen == 1 &&
342               ((TYPE_CODE (elttype) == TYPE_CODE_INT)
343                || ((current_language->la_language == language_m2)
344                    && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
345               && (options->format == 0 || options->format == 's'))
346             {
347               /* If requested, look for the first null char and only print
348                  elements up to it.  */
349               if (options->stop_print_at_null)
350                 {
351                   unsigned int temp_len;
352
353                   /* Look for a NULL char. */
354                   for (temp_len = 0;
355                        (valaddr + embedded_offset)[temp_len]
356                          && temp_len < len && temp_len < options->print_max;
357                        temp_len++);
358                   len = temp_len;
359                 }
360
361               LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
362                                valaddr + embedded_offset, len, NULL,
363                                0, options);
364               i = len;
365             }
366           else
367             {
368               fprintf_filtered (stream, "{");
369               val_print_array_elements (type, valaddr + embedded_offset,
370                                         address, stream, recurse, options, 0);
371               fprintf_filtered (stream, "}");
372             }
373           break;
374         }
375       /* Array of unspecified length: treat like pointer to first elt.  */
376       print_unpacked_pointer (type, address, address, options, stream);
377       break;
378
379     case TYPE_CODE_PTR:
380       if (TYPE_CONST (type))
381         print_variable_at_address (type, valaddr + embedded_offset,
382                                    stream, recurse, options);
383       else if (options->format && options->format != 's')
384         print_scalar_formatted (valaddr + embedded_offset, type,
385                                 options, 0, stream);
386       else
387         {
388           addr = unpack_pointer (type, valaddr + embedded_offset);
389           print_unpacked_pointer (type, addr, address, options, stream);
390         }
391       break;
392
393     case TYPE_CODE_REF:
394       elttype = check_typedef (TYPE_TARGET_TYPE (type));
395       if (options->addressprint)
396         {
397           CORE_ADDR addr
398             = extract_typed_address (valaddr + embedded_offset, type);
399
400           fprintf_filtered (stream, "@");
401           fputs_filtered (paddress (gdbarch, addr), stream);
402           if (options->deref_ref)
403             fputs_filtered (": ", stream);
404         }
405       /* De-reference the reference.  */
406       if (options->deref_ref)
407         {
408           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
409             {
410               struct value *deref_val =
411                 value_at
412                 (TYPE_TARGET_TYPE (type),
413                  unpack_pointer (type, valaddr + embedded_offset));
414
415               common_val_print (deref_val, stream, recurse, options,
416                                 current_language);
417             }
418           else
419             fputs_filtered ("???", stream);
420         }
421       break;
422
423     case TYPE_CODE_UNION:
424       if (recurse && !options->unionprint)
425         {
426           fprintf_filtered (stream, "{...}");
427           break;
428         }
429       /* Fall through.  */
430     case TYPE_CODE_STRUCT:
431       if (m2_is_long_set (type))
432         m2_print_long_set (type, valaddr, embedded_offset, address,
433                            stream);
434       else if (m2_is_unbounded_array (type))
435         m2_print_unbounded_array (type, valaddr, embedded_offset,
436                                   address, stream, recurse, options);
437       else
438         cp_print_value_fields (type, type, valaddr, embedded_offset,
439                                address, stream, recurse, options, NULL, 0);
440       break;
441
442     case TYPE_CODE_ENUM:
443       if (options->format)
444         {
445           print_scalar_formatted (valaddr + embedded_offset, type,
446                                   options, 0, stream);
447           break;
448         }
449       len = TYPE_NFIELDS (type);
450       val = unpack_long (type, valaddr + embedded_offset);
451       for (i = 0; i < len; i++)
452         {
453           QUIT;
454           if (val == TYPE_FIELD_BITPOS (type, i))
455             {
456               break;
457             }
458         }
459       if (i < len)
460         {
461           fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
462         }
463       else
464         {
465           print_longest (stream, 'd', 0, val);
466         }
467       break;
468
469     case TYPE_CODE_FUNC:
470       if (options->format)
471         {
472           print_scalar_formatted (valaddr + embedded_offset, type,
473                                   options, 0, stream);
474           break;
475         }
476       /* FIXME, we should consider, at least for ANSI C language, eliminating
477          the distinction made between FUNCs and POINTERs to FUNCs.  */
478       fprintf_filtered (stream, "{");
479       type_print (type, "", stream, -1);
480       fprintf_filtered (stream, "} ");
481       /* Try to print what function it points to, and its address.  */
482       print_address_demangle (gdbarch, address, stream, demangle);
483       break;
484
485     case TYPE_CODE_BOOL:
486       if (options->format || options->output_format)
487         {
488           struct value_print_options opts = *options;
489
490           opts.format = (options->format ? options->format
491                          : options->output_format);
492           print_scalar_formatted (valaddr + embedded_offset, type,
493                                   &opts, 0, stream);
494         }
495       else
496         {
497           val = unpack_long (type, valaddr + embedded_offset);
498           if (val == 0)
499             fputs_filtered ("FALSE", stream);
500           else if (val == 1)
501             fputs_filtered ("TRUE", stream);
502           else
503             fprintf_filtered (stream, "%ld)", (long int) val);
504         }
505       break;
506
507     case TYPE_CODE_RANGE:
508       if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
509         {
510           m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
511                         address, stream, recurse, options);
512           break;
513         }
514       /* FIXME: create_range_type does not set the unsigned bit in a
515          range type (I think it probably should copy it from the target
516          type), so we won't print values which are too large to
517          fit in a signed integer correctly.  */
518       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
519          print with the target type, though, because the size of our type
520          and the target type might differ).  */
521       /* FALLTHROUGH */
522
523     case TYPE_CODE_INT:
524       if (options->format || options->output_format)
525         {
526           struct value_print_options opts = *options;
527
528           opts.format = (options->format ? options->format
529                          : options->output_format);
530           print_scalar_formatted (valaddr + embedded_offset, type,
531                                   &opts, 0, stream);
532         }
533       else
534         val_print_type_code_int (type, valaddr + embedded_offset, stream);
535       break;
536
537     case TYPE_CODE_CHAR:
538       if (options->format || options->output_format)
539         {
540           struct value_print_options opts = *options;
541
542           opts.format = (options->format ? options->format
543                          : options->output_format);
544           print_scalar_formatted (valaddr + embedded_offset, type,
545                                   &opts, 0, stream);
546         }
547       else
548         {
549           val = unpack_long (type, valaddr + embedded_offset);
550           if (TYPE_UNSIGNED (type))
551             fprintf_filtered (stream, "%u", (unsigned int) val);
552           else
553             fprintf_filtered (stream, "%d", (int) val);
554           fputs_filtered (" ", stream);
555           LA_PRINT_CHAR ((unsigned char) val, type, stream);
556         }
557       break;
558
559     case TYPE_CODE_FLT:
560       if (options->format)
561         print_scalar_formatted (valaddr + embedded_offset, type,
562                                 options, 0, stream);
563       else
564         print_floating (valaddr + embedded_offset, type, stream);
565       break;
566
567     case TYPE_CODE_METHOD:
568       break;
569
570     case TYPE_CODE_BITSTRING:
571     case TYPE_CODE_SET:
572       elttype = TYPE_INDEX_TYPE (type);
573       CHECK_TYPEDEF (elttype);
574       if (TYPE_STUB (elttype))
575         {
576           fprintf_filtered (stream, _("<incomplete type>"));
577           gdb_flush (stream);
578           break;
579         }
580       else
581         {
582           struct type *range = elttype;
583           LONGEST low_bound, high_bound;
584           int i;
585           int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
586           int need_comma = 0;
587
588           if (is_bitstring)
589             fputs_filtered ("B'", stream);
590           else
591             fputs_filtered ("{", stream);
592
593           i = get_discrete_bounds (range, &low_bound, &high_bound);
594         maybe_bad_bstring:
595           if (i < 0)
596             {
597               fputs_filtered (_("<error value>"), stream);
598               goto done;
599             }
600
601           for (i = low_bound; i <= high_bound; i++)
602             {
603               int element = value_bit_index (type, valaddr + embedded_offset,
604                                              i);
605
606               if (element < 0)
607                 {
608                   i = element;
609                   goto maybe_bad_bstring;
610                 }
611               if (is_bitstring)
612                 fprintf_filtered (stream, "%d", element);
613               else if (element)
614                 {
615                   if (need_comma)
616                     fputs_filtered (", ", stream);
617                   print_type_scalar (range, i, stream);
618                   need_comma = 1;
619
620                   if (i + 1 <= high_bound
621                       && value_bit_index (type, valaddr + embedded_offset,
622                                           ++i))
623                     {
624                       int j = i;
625
626                       fputs_filtered ("..", stream);
627                       while (i + 1 <= high_bound
628                              && value_bit_index (type,
629                                                  valaddr + embedded_offset,
630                                                  ++i))
631                         j = i;
632                       print_type_scalar (range, j, stream);
633                     }
634                 }
635             }
636         done:
637           if (is_bitstring)
638             fputs_filtered ("'", stream);
639           else
640             fputs_filtered ("}", stream);
641         }
642       break;
643
644     case TYPE_CODE_VOID:
645       fprintf_filtered (stream, "void");
646       break;
647
648     case TYPE_CODE_ERROR:
649       fprintf_filtered (stream, _("<error type>"));
650       break;
651
652     case TYPE_CODE_UNDEF:
653       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
654          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
655          and no complete type for struct foo in that file.  */
656       fprintf_filtered (stream, _("<incomplete type>"));
657       break;
658
659     default:
660       error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));
661     }
662   gdb_flush (stream);
663   return (0);
664 }