gdb:
[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 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 (CORE_ADDR address, struct ui_file *stream,
50                                 int addressprint)
51 {
52   CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
53                                                             address,
54                                                             &current_target);
55
56   /* If the function pointer is represented by a description, print the
57      address of the description.  */
58   if (addressprint && func_addr != address)
59     {
60       fputs_filtered ("@", stream);
61       fputs_filtered (paddress (address), stream);
62       fputs_filtered (": ", stream);
63     }
64   print_address_demangle (func_addr, stream, demangle);
65 }
66
67 /* get_long_set_bounds - assigns the bounds of the long set to low and
68                          high.  */
69
70 int
71 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
72 {
73   int len, i;
74
75   if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
76     {
77       len = TYPE_NFIELDS (type);
78       i = TYPE_N_BASECLASSES (type);
79       if (len == 0)
80         return 0;
81       *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
82       *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
83                                                                  len-1)));
84       return 1;
85     }
86   error (_("expecting long_set"));
87   return 0;
88 }
89
90 static void
91 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
92                    int embedded_offset, CORE_ADDR address,
93                    struct ui_file *stream)
94 {
95   int empty_set        = 1;
96   int element_seen     = 0;
97   LONGEST previous_low = 0;
98   LONGEST previous_high= 0;
99   LONGEST i, low_bound, high_bound;
100   LONGEST field_low, field_high;
101   struct type *range;
102   int len, field;
103   struct type *target;
104   int bitval;
105
106   CHECK_TYPEDEF (type);
107
108   fprintf_filtered (stream, "{");
109   len = TYPE_NFIELDS (type);
110   if (get_long_set_bounds (type, &low_bound, &high_bound))
111     {
112       field = TYPE_N_BASECLASSES (type);
113       range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
114     }
115   else
116     {
117       fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
118       return;
119     }
120
121   target = TYPE_TARGET_TYPE (range);
122   if (target == NULL)
123     target = builtin_type_int32;
124
125   if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
126     {
127       for (i = low_bound; i <= high_bound; i++)
128         {
129           bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
130                                     (TYPE_FIELD_BITPOS (type, field) / 8) +
131                                     valaddr + embedded_offset, i);
132           if (bitval < 0)
133             error (_("bit test is out of range"));
134           else if (bitval > 0)
135             {
136               previous_high = i;
137               if (! element_seen)
138                 {
139                   if (! empty_set)
140                     fprintf_filtered (stream, ", ");
141                   print_type_scalar (target, i, stream);
142                   empty_set    = 0;
143                   element_seen = 1;
144                   previous_low = i;
145                 }
146             }
147           else
148             {
149               /* bit is not set */
150               if (element_seen)
151                 {
152                   if (previous_low+1 < previous_high)
153                     fprintf_filtered (stream, "..");
154                   if (previous_low+1 < previous_high)
155                     print_type_scalar (target, previous_high, stream);
156                   element_seen = 0;
157                 }
158             }
159           if (i == field_high)
160             {
161               field++;
162               if (field == len)
163                 break;
164               range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
165               if (get_discrete_bounds (range, &field_low, &field_high) < 0)
166                 break;
167               target = TYPE_TARGET_TYPE (range);
168               if (target == NULL)
169                 target = builtin_type_int32;
170             }
171         }
172       if (element_seen)
173         {
174           if (previous_low+1 < previous_high)
175             {
176               fprintf_filtered (stream, "..");
177               print_type_scalar (target, previous_high, stream);
178             }
179           element_seen = 0;
180         }
181       fprintf_filtered (stream, "}");
182     }
183 }
184
185 static void
186 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
187                           int embedded_offset, CORE_ADDR address,
188                           struct ui_file *stream, int recurse,
189                           const struct value_print_options *options)
190 {
191   struct type *content_type;
192   CORE_ADDR addr;
193   LONGEST len;
194   struct value *val;
195
196   CHECK_TYPEDEF (type);
197   content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
198
199   addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
200                          (TYPE_FIELD_BITPOS (type, 0) / 8) +
201                          valaddr + embedded_offset);
202
203   val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
204                        addr);
205   len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
206
207   fprintf_filtered (stream, "{");  
208   m2_print_array_contents (value_type (val), value_contents(val),
209                            value_embedded_offset (val), addr, stream,
210                            recurse, options, len);
211   fprintf_filtered (stream, ", HIGH = %d}", (int) len);
212 }
213
214 static int
215 print_unpacked_pointer (struct type *type,
216                         CORE_ADDR address, CORE_ADDR addr,
217                         const struct value_print_options *options,
218                         struct ui_file *stream)
219 {
220   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
221
222   if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
223     {
224       /* Try to print what function it points to.  */
225       print_function_pointer_address (addr, stream, options->addressprint);
226       /* Return value is irrelevant except for string pointers.  */
227       return 0;
228     }
229
230   if (options->addressprint && options->format != 's')
231     fputs_filtered (paddress (address), stream);
232
233   /* For a pointer to char or unsigned char, also print the string
234      pointed to, unless pointer is null.  */
235
236   if (TYPE_LENGTH (elttype) == 1
237       && TYPE_CODE (elttype) == TYPE_CODE_INT
238       && (options->format == 0 || options->format == 's')
239       && addr != 0)
240     return val_print_string (TYPE_TARGET_TYPE (type), addr, -1,
241                              stream, options);
242   
243   return 0;
244 }
245
246 static void
247 print_variable_at_address (struct type *type,
248                            const gdb_byte *valaddr,
249                            struct ui_file *stream,
250                            int recurse,
251                            const struct value_print_options *options)
252 {
253   CORE_ADDR addr = unpack_pointer (type, valaddr);
254   struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
255
256   fprintf_filtered (stream, "[");
257   fputs_filtered (paddress (addr), stream);
258   fprintf_filtered (stream, "] : ");
259   
260   if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
261     {
262       struct value *deref_val =
263         value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
264       common_val_print (deref_val, stream, recurse, options, current_language);
265     }
266   else
267     fputs_filtered ("???", stream);
268 }
269
270
271 /* m2_print_array_contents - prints out the contents of an
272                              array up to a max_print values.
273                              It prints arrays of char as a string
274                              and all other data types as comma
275                              separated values.  */
276
277 static void
278 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
279                          int embedded_offset, CORE_ADDR address,
280                          struct ui_file *stream, int recurse,
281                          const struct value_print_options *options,
282                          int len)
283 {
284   int eltlen;
285   CHECK_TYPEDEF (type);
286
287   if (TYPE_LENGTH (type) > 0)
288     {
289       eltlen = TYPE_LENGTH (type);
290       if (options->prettyprint_arrays)
291         print_spaces_filtered (2 + 2 * recurse, stream);
292       /* For an array of chars, print with string syntax.  */
293       if (eltlen == 1 &&
294           ((TYPE_CODE (type) == TYPE_CODE_INT)
295            || ((current_language->la_language == language_m2)
296                && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
297           && (options->format == 0 || options->format == 's'))
298         val_print_string (type, address, len+1, stream, options);
299       else
300         {
301           fprintf_filtered (stream, "{");
302           val_print_array_elements (type, valaddr + embedded_offset,
303                                     address, stream, recurse, options, 0);
304           fprintf_filtered (stream, "}");
305         }
306     }
307 }
308
309
310 /* Print data of type TYPE located at VALADDR (within GDB), which came from
311    the inferior at address ADDRESS, onto stdio stream STREAM according to
312    OPTIONS.  The data at VALADDR is in target byte order.
313
314    If the data are a string pointer, returns the number of string characters
315    printed.  */
316
317 int
318 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
319               CORE_ADDR address, struct ui_file *stream, int recurse,
320               const struct value_print_options *options)
321 {
322   unsigned int i = 0;   /* Number of characters printed */
323   unsigned len;
324   struct type *elttype;
325   unsigned eltlen;
326   int length_pos, length_size, string_pos;
327   int char_size;
328   LONGEST val;
329   CORE_ADDR addr;
330
331   CHECK_TYPEDEF (type);
332   switch (TYPE_CODE (type))
333     {
334     case TYPE_CODE_ARRAY:
335       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
336         {
337           elttype = check_typedef (TYPE_TARGET_TYPE (type));
338           eltlen = TYPE_LENGTH (elttype);
339           len = TYPE_LENGTH (type) / eltlen;
340           if (options->prettyprint_arrays)
341             print_spaces_filtered (2 + 2 * recurse, stream);
342           /* For an array of chars, print with string syntax.  */
343           if (eltlen == 1 &&
344               ((TYPE_CODE (elttype) == TYPE_CODE_INT)
345                || ((current_language->la_language == language_m2)
346                    && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
347               && (options->format == 0 || options->format == 's'))
348             {
349               /* If requested, look for the first null char and only print
350                  elements up to it.  */
351               if (options->stop_print_at_null)
352                 {
353                   unsigned int temp_len;
354
355                   /* Look for a NULL char. */
356                   for (temp_len = 0;
357                        (valaddr + embedded_offset)[temp_len]
358                          && temp_len < len && temp_len < options->print_max;
359                        temp_len++);
360                   len = temp_len;
361                 }
362
363               LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
364                                valaddr + embedded_offset, len, 0,
365                                options);
366               i = len;
367             }
368           else
369             {
370               fprintf_filtered (stream, "{");
371               val_print_array_elements (type, valaddr + embedded_offset,
372                                         address, stream, recurse, options, 0);
373               fprintf_filtered (stream, "}");
374             }
375           break;
376         }
377       /* Array of unspecified length: treat like pointer to first elt.  */
378       print_unpacked_pointer (type, address, address, options, stream);
379       break;
380
381     case TYPE_CODE_PTR:
382       if (TYPE_CONST (type))
383         print_variable_at_address (type, valaddr + embedded_offset,
384                                    stream, recurse, options);
385       else if (options->format && options->format != 's')
386         print_scalar_formatted (valaddr + embedded_offset, type,
387                                 options, 0, stream);
388       else
389         {
390           addr = unpack_pointer (type, valaddr + embedded_offset);
391           print_unpacked_pointer (type, addr, address, options, stream);
392         }
393       break;
394
395     case TYPE_CODE_REF:
396       elttype = check_typedef (TYPE_TARGET_TYPE (type));
397       if (options->addressprint)
398         {
399           CORE_ADDR addr
400             = extract_typed_address (valaddr + embedded_offset, type);
401           fprintf_filtered (stream, "@");
402           fputs_filtered (paddress (addr), stream);
403           if (options->deref_ref)
404             fputs_filtered (": ", stream);
405         }
406       /* De-reference the reference.  */
407       if (options->deref_ref)
408         {
409           if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
410             {
411               struct value *deref_val =
412                 value_at
413                 (TYPE_TARGET_TYPE (type),
414                  unpack_pointer (type, valaddr + embedded_offset));
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 (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           opts.format = (options->format ? options->format
490                          : options->output_format);
491           print_scalar_formatted (valaddr + embedded_offset, type,
492                                   &opts, 0, stream);
493         }
494       else
495         {
496           val = unpack_long (type, valaddr + embedded_offset);
497           if (val == 0)
498             fputs_filtered ("FALSE", stream);
499           else if (val == 1)
500             fputs_filtered ("TRUE", stream);
501           else
502             fprintf_filtered (stream, "%ld)", (long int) val);
503         }
504       break;
505
506     case TYPE_CODE_RANGE:
507       if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
508         {
509           m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
510                         address, stream, recurse, options);
511           break;
512         }
513       /* FIXME: create_range_type does not set the unsigned bit in a
514          range type (I think it probably should copy it from the target
515          type), so we won't print values which are too large to
516          fit in a signed integer correctly.  */
517       /* FIXME: Doesn't handle ranges of enums correctly.  (Can't just
518          print with the target type, though, because the size of our type
519          and the target type might differ).  */
520       /* FALLTHROUGH */
521
522     case TYPE_CODE_INT:
523       if (options->format || options->output_format)
524         {
525           struct value_print_options opts = *options;
526           opts.format = (options->format ? options->format
527                          : options->output_format);
528           print_scalar_formatted (valaddr + embedded_offset, type,
529                                   &opts, 0, stream);
530         }
531       else
532         val_print_type_code_int (type, valaddr + embedded_offset, stream);
533       break;
534
535     case TYPE_CODE_CHAR:
536       if (options->format || options->output_format)
537         {
538           struct value_print_options opts = *options;
539           opts.format = (options->format ? options->format
540                          : options->output_format);
541           print_scalar_formatted (valaddr + embedded_offset, type,
542                                   &opts, 0, stream);
543         }
544       else
545         {
546           val = unpack_long (type, valaddr + embedded_offset);
547           if (TYPE_UNSIGNED (type))
548             fprintf_filtered (stream, "%u", (unsigned int) val);
549           else
550             fprintf_filtered (stream, "%d", (int) val);
551           fputs_filtered (" ", stream);
552           LA_PRINT_CHAR ((unsigned char) val, type, stream);
553         }
554       break;
555
556     case TYPE_CODE_FLT:
557       if (options->format)
558         print_scalar_formatted (valaddr + embedded_offset, type,
559                                 options, 0, stream);
560       else
561         print_floating (valaddr + embedded_offset, type, stream);
562       break;
563
564     case TYPE_CODE_METHOD:
565       break;
566
567     case TYPE_CODE_BITSTRING:
568     case TYPE_CODE_SET:
569       elttype = TYPE_INDEX_TYPE (type);
570       CHECK_TYPEDEF (elttype);
571       if (TYPE_STUB (elttype))
572         {
573           fprintf_filtered (stream, _("<incomplete type>"));
574           gdb_flush (stream);
575           break;
576         }
577       else
578         {
579           struct type *range = elttype;
580           LONGEST low_bound, high_bound;
581           int i;
582           int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
583           int need_comma = 0;
584
585           if (is_bitstring)
586             fputs_filtered ("B'", stream);
587           else
588             fputs_filtered ("{", stream);
589
590           i = get_discrete_bounds (range, &low_bound, &high_bound);
591         maybe_bad_bstring:
592           if (i < 0)
593             {
594               fputs_filtered (_("<error value>"), stream);
595               goto done;
596             }
597
598           for (i = low_bound; i <= high_bound; i++)
599             {
600               int element = value_bit_index (type, valaddr + embedded_offset,
601                                              i);
602               if (element < 0)
603                 {
604                   i = element;
605                   goto maybe_bad_bstring;
606                 }
607               if (is_bitstring)
608                 fprintf_filtered (stream, "%d", element);
609               else if (element)
610                 {
611                   if (need_comma)
612                     fputs_filtered (", ", stream);
613                   print_type_scalar (range, i, stream);
614                   need_comma = 1;
615
616                   if (i + 1 <= high_bound
617                       && value_bit_index (type, valaddr + embedded_offset,
618                                           ++i))
619                     {
620                       int j = i;
621                       fputs_filtered ("..", stream);
622                       while (i + 1 <= high_bound
623                              && value_bit_index (type,
624                                                  valaddr + embedded_offset,
625                                                  ++i))
626                         j = i;
627                       print_type_scalar (range, j, stream);
628                     }
629                 }
630             }
631         done:
632           if (is_bitstring)
633             fputs_filtered ("'", stream);
634           else
635             fputs_filtered ("}", stream);
636         }
637       break;
638
639     case TYPE_CODE_VOID:
640       fprintf_filtered (stream, "void");
641       break;
642
643     case TYPE_CODE_ERROR:
644       fprintf_filtered (stream, _("<error type>"));
645       break;
646
647     case TYPE_CODE_UNDEF:
648       /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
649          dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
650          and no complete type for struct foo in that file.  */
651       fprintf_filtered (stream, _("<incomplete type>"));
652       break;
653
654     default:
655       error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));
656     }
657   gdb_flush (stream);
658   return (0);
659 }