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.
5 This file is part of GDB.
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.
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.
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. */
26 #include "expression.h"
30 #include "c-lang.h" /* For c_val_print */
31 #include "typeprint.h"
36 chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
37 enum val_prettyprint, struct type **));
40 chill_print_type_scalar PARAMS ((struct type *, LONGEST, GDB_FILE *));
43 chill_val_print_array_elements PARAMS ((struct type *, char *, CORE_ADDR, GDB_FILE *,
44 int, int, int, enum val_prettyprint));
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. */
54 chill_print_type_scalar (type, val, stream)
59 switch (TYPE_CODE (type))
62 if (TYPE_TARGET_TYPE (type))
64 chill_print_type_scalar (TYPE_TARGET_TYPE (type), val, stream);
71 case TYPE_CODE_STRUCT:
79 case TYPE_CODE_STRING:
80 case TYPE_CODE_BITSTRING:
82 case TYPE_CODE_MEMBER:
83 case TYPE_CODE_METHOD:
87 case TYPE_CODE_COMPLEX:
88 case TYPE_CODE_TYPEDEF:
92 print_type_scalar (type, val, stream);
95 /* Print the elements of an array.
96 Similar to val_print_array_elements, but prints
97 element indexes (in Chill syntax). */
100 chill_val_print_array_elements (type, valaddr, address, stream,
101 format, deref_ref, recurse, pretty)
109 enum val_prettyprint pretty;
112 unsigned int things_printed = 0;
114 struct type *elttype;
115 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
116 struct type *index_type = TYPE_TARGET_TYPE (range_type);
118 /* Position of the array element we are examining to see
119 whether it is repeated. */
121 /* Number of repetitions we have detected so far. */
123 LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
125 elttype = check_typedef (TYPE_TARGET_TYPE (type));
126 eltlen = TYPE_LENGTH (elttype);
127 len = TYPE_LENGTH (type) / eltlen;
129 annotate_array_section_begin (i, elttype);
131 for (; i < len && things_printed < print_max; i++)
135 if (prettyprint_arrays)
137 fprintf_filtered (stream, ",\n");
138 print_spaces_filtered (2 + 2 * recurse, stream);
142 fprintf_filtered (stream, ", ");
145 wrap_here (n_spaces (2 + 2 * recurse));
149 while ((rep1 < len) &&
150 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
156 fputs_filtered ("(", stream);
157 chill_print_type_scalar (index_type, low_bound + i, stream);
160 fputs_filtered (":", stream);
161 chill_print_type_scalar (index_type, low_bound + i + reps - 1,
163 fputs_filtered ("): ", stream);
164 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
165 deref_ref, recurse + 1, pretty);
172 fputs_filtered ("): ", stream);
173 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
174 deref_ref, recurse + 1, pretty);
179 annotate_array_section_end ();
182 fprintf_filtered (stream, "...");
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
191 If the data are a string pointer, returns the number of string characters
194 If DEREF_REF is nonzero, then dereference references, otherwise just print
197 The PRETTY parameter controls prettyprinting. */
200 chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
209 enum val_prettyprint pretty;
212 unsigned int i = 0; /* Number of characters printed. */
213 struct type *elttype;
216 CHECK_TYPEDEF (type);
218 switch (TYPE_CODE (type))
220 case TYPE_CODE_ARRAY:
221 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
223 if (prettyprint_arrays)
225 print_spaces_filtered (2 + 2 * recurse, stream);
227 fprintf_filtered (stream, "[");
228 chill_val_print_array_elements (type, valaddr, address, stream,
229 format, deref_ref, recurse, pretty);
230 fprintf_filtered (stream, "]");
234 error ("unimplemented in chill_val_print; unspecified array length");
239 format = format ? format : output_format;
242 print_scalar_formatted (valaddr, type, format, 0, stream);
246 val_print_type_code_int (type, valaddr, stream);
251 format = format ? format : output_format;
254 print_scalar_formatted (valaddr, type, format, 0, stream);
258 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
266 print_scalar_formatted (valaddr, type, format, 0, stream);
270 print_floating (valaddr, type, stream);
275 format = format ? format : output_format;
278 print_scalar_formatted (valaddr, type, format, 0, stream);
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");
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>");
296 if (format && format != 's')
298 print_scalar_formatted (valaddr, type, format, 0, stream);
301 addr = unpack_pointer (type, valaddr);
302 elttype = check_typedef (TYPE_TARGET_TYPE (type));
304 /* We assume a NULL pointer is all zeros ... */
307 fputs_filtered ("NULL", stream);
311 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
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. */
318 if (addressprint && format != 's')
320 print_address_numeric (addr, 1, stream);
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')
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 i = val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
334 /* Return number of characters printed, plus one for the
335 terminating null if we have "reached the end". */
336 return (i + (print_max && i != print_max));
339 case TYPE_CODE_STRING:
340 i = TYPE_LENGTH (type);
341 LA_PRINT_STRING (stream, valaddr, i, 1, 0);
342 /* Return number of characters printed, plus one for the terminating
343 null if we have "reached the end". */
344 return (i + (print_max && i != print_max));
347 case TYPE_CODE_BITSTRING:
349 elttype = TYPE_INDEX_TYPE (type);
350 CHECK_TYPEDEF (elttype);
351 if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
353 fprintf_filtered (stream, "<incomplete type>");
358 struct type *range = elttype;
359 LONGEST low_bound, high_bound;
361 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
365 fputs_filtered ("B'", stream);
367 fputs_filtered ("[", stream);
369 i = get_discrete_bounds (range, &low_bound, &high_bound);
373 fputs_filtered ("<error value>", stream);
377 for (i = low_bound; i <= high_bound; i++)
379 int element = value_bit_index (type, valaddr, i);
383 goto maybe_bad_bstring;
386 fprintf_filtered (stream, "%d", element);
390 fputs_filtered (", ", stream);
391 chill_print_type_scalar (range, (LONGEST) i, stream);
394 /* Look for a continuous range of true elements. */
395 if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
397 int j = i; /* j is the upper bound so far of the range */
398 fputs_filtered (":", stream);
399 while (i+1 <= high_bound
400 && value_bit_index (type, valaddr, ++i))
402 chill_print_type_scalar (range, (LONGEST) j, stream);
408 fputs_filtered ("'", stream);
410 fputs_filtered ("]", stream);
414 case TYPE_CODE_STRUCT:
415 if (chill_varying_type (type))
417 struct type *inner = check_typedef (TYPE_FIELD_TYPE (type, 1));
418 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
419 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
421 switch (TYPE_CODE (inner))
423 case TYPE_CODE_STRING:
424 if (length > TYPE_LENGTH (type) - 2)
426 fprintf_filtered (stream,
427 "<dynamic length %ld > static length %d> *invalid*",
428 length, TYPE_LENGTH (type));
430 /* Don't print the string; doing so might produce a
434 LA_PRINT_STRING (stream, data_addr, length, 1, 0);
440 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
447 fprintf_filtered (stream, "LOC(");
448 print_address_numeric
449 (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
452 fprintf_filtered (stream, ")");
454 fputs_filtered (": ", stream);
456 /* De-reference the reference. */
459 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
461 value_ptr deref_val =
463 (TYPE_TARGET_TYPE (type),
464 unpack_pointer (lookup_pointer_type (builtin_type_void),
467 val_print (VALUE_TYPE (deref_val),
468 VALUE_CONTENTS (deref_val),
469 VALUE_ADDRESS (deref_val), stream, format,
470 deref_ref, recurse + 1, pretty);
473 fputs_filtered ("???", stream);
478 c_val_print (type, valaddr, address, stream, format,
479 deref_ref, recurse, pretty);
482 case TYPE_CODE_RANGE:
483 if (TYPE_TARGET_TYPE (type))
484 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
485 format, deref_ref, recurse, pretty);
488 case TYPE_CODE_MEMBER:
489 case TYPE_CODE_UNION:
492 case TYPE_CODE_ERROR:
494 /* Let's defer printing to the C printer, rather than
495 print an error message. FIXME! */
496 c_val_print (type, valaddr, address, stream, format,
497 deref_ref, recurse, pretty);
503 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
504 print out a structure's fields: cp_print_value_fields and cplus_print_value.
506 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
507 same meanings as in cplus_print_value and c_val_print.
509 DONT_PRINT is an array of baseclass types that we
510 should not print, or zero if called from top level. */
513 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
520 enum val_prettyprint pretty;
521 struct type **dont_print;
526 CHECK_TYPEDEF (type);
528 fprintf_filtered (stream, "[");
529 len = TYPE_NFIELDS (type);
532 fprintf_filtered (stream, "<No data fields>");
536 for (i = 0; i < len; i++)
540 fprintf_filtered (stream, ", ");
545 fprintf_filtered (stream, "\n");
546 print_spaces_filtered (2 + 2 * recurse, stream);
550 wrap_here (n_spaces (2 + 2 * recurse));
552 fputs_filtered (".", stream);
553 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
554 language_chill, DMGL_NO_OPTS);
555 fputs_filtered (": ", stream);
556 if (TYPE_FIELD_PACKED (type, i))
560 /* Bitfields require special handling, especially due to byte
562 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
563 unpack_field_as_long (type, valaddr, i));
565 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
566 stream, format, 0, recurse + 1, pretty);
570 chill_val_print (TYPE_FIELD_TYPE (type, i),
571 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
572 0, stream, format, 0, recurse + 1, pretty);
577 fprintf_filtered (stream, "\n");
578 print_spaces_filtered (2 * recurse, stream);
581 fprintf_filtered (stream, "]");
585 chill_value_print (val, stream, format, pretty)
589 enum val_prettyprint pretty;
591 struct type *type = VALUE_TYPE (val);
592 struct type *real_type = check_typedef (type);
594 /* If it is a pointer, indicate what it points to.
596 Print type also if it is a reference. */
598 if (TYPE_CODE (real_type) == TYPE_CODE_PTR ||
599 TYPE_CODE (real_type) == TYPE_CODE_REF)
601 char *valaddr = VALUE_CONTENTS (val);
602 CORE_ADDR addr = unpack_pointer (type, valaddr);
603 if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
606 char *name = TYPE_NAME (type);
608 fputs_filtered (name, stream);
609 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
610 fputs_filtered ("PTR", stream);
613 fprintf_filtered (stream, "(");
614 type_print (type, "", stream, -1);
615 fprintf_filtered (stream, ")");
617 fprintf_filtered (stream, "(");
618 i = val_print (type, valaddr, VALUE_ADDRESS (val),
619 stream, format, 1, 0, pretty);
620 fprintf_filtered (stream, ")");
624 return (val_print (type, VALUE_CONTENTS (val),
625 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));