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., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "expression.h"
30 #include "c-lang.h" /* For c_val_print */
31 #include "typeprint.h"
35 chill_print_value_fields PARAMS ((struct type *, char *, GDB_FILE *, int, int,
36 enum val_prettyprint, struct type **));
39 /* Print the elements of an array.
40 Similar to val_print_array_elements, but prints
41 element indexes (in Chill syntax). */
44 chill_val_print_array_elements (type, valaddr, address, stream,
45 format, deref_ref, recurse, pretty)
53 enum val_prettyprint pretty;
56 unsigned int things_printed = 0;
59 struct type *range_type = TYPE_FIELD_TYPE (type, 0);
60 struct type *index_type = TYPE_TARGET_TYPE (range_type);
62 /* Position of the array element we are examining to see
63 whether it is repeated. */
65 /* Number of repetitions we have detected so far. */
67 LONGEST low_bound = TYPE_FIELD_BITPOS (range_type, 0);
68 LONGEST high_bound = TYPE_FIELD_BITPOS (range_type, 1);
70 elttype = TYPE_TARGET_TYPE (type);
71 eltlen = TYPE_LENGTH (elttype);
72 len = TYPE_LENGTH (type) / eltlen;
74 annotate_array_section_begin (i, elttype);
76 for (; i < len && things_printed < print_max; i++)
80 if (prettyprint_arrays)
82 fprintf_filtered (stream, ",\n");
83 print_spaces_filtered (2 + 2 * recurse, stream);
87 fprintf_filtered (stream, ", ");
90 wrap_here (n_spaces (2 + 2 * recurse));
94 while ((rep1 < len) &&
95 !memcmp (valaddr + i * eltlen, valaddr + rep1 * eltlen, eltlen))
101 fputs_filtered ("(", stream);
102 print_type_scalar (index_type, low_bound + i, stream);
105 fputs_filtered (":", stream);
106 print_type_scalar (index_type, low_bound + i + reps - 1, stream);
107 fputs_filtered ("): ", stream);
108 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
109 deref_ref, recurse + 1, pretty);
116 fputs_filtered ("): ", stream);
117 val_print (elttype, valaddr + i * eltlen, 0, stream, format,
118 deref_ref, recurse + 1, pretty);
123 annotate_array_section_end ();
126 fprintf_filtered (stream, "...");
130 /* Print data of type TYPE located at VALADDR (within GDB), which came from
131 the inferior at address ADDRESS, onto stdio stream STREAM according to
132 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
135 If the data are a string pointer, returns the number of string characters
138 If DEREF_REF is nonzero, then dereference references, otherwise just print
141 The PRETTY parameter controls prettyprinting. */
144 chill_val_print (type, valaddr, address, stream, format, deref_ref, recurse,
153 enum val_prettyprint pretty;
156 unsigned int i = 0; /* Number of characters printed. */
157 struct type *elttype;
160 switch (TYPE_CODE (type))
162 case TYPE_CODE_ARRAY:
163 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
165 if (prettyprint_arrays)
167 print_spaces_filtered (2 + 2 * recurse, stream);
169 fprintf_filtered (stream, "[");
170 chill_val_print_array_elements (type, valaddr, address, stream,
171 format, deref_ref, recurse, pretty);
172 fprintf_filtered (stream, "]");
176 error ("unimplemented in chill_val_print; unspecified array length");
181 format = format ? format : output_format;
184 print_scalar_formatted (valaddr, type, format, 0, stream);
188 val_print_type_code_int (type, valaddr, stream);
193 format = format ? format : output_format;
196 print_scalar_formatted (valaddr, type, format, 0, stream);
200 LA_PRINT_CHAR ((unsigned char) unpack_long (type, valaddr),
208 print_scalar_formatted (valaddr, type, format, 0, stream);
212 print_floating (valaddr, type, stream);
217 format = format ? format : output_format;
220 print_scalar_formatted (valaddr, type, format, 0, stream);
224 /* FIXME: Why is this using builtin_type_chill_bool not type? */
225 val = unpack_long (builtin_type_chill_bool, valaddr);
226 fprintf_filtered (stream, val ? "TRUE" : "FALSE");
230 case TYPE_CODE_UNDEF:
231 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
232 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
233 and no complete type for struct foo in that file. */
234 fprintf_filtered (stream, "<incomplete type>");
238 if (format && format != 's')
240 print_scalar_formatted (valaddr, type, format, 0, stream);
243 addr = unpack_pointer (type, valaddr);
244 elttype = TYPE_TARGET_TYPE (type);
246 /* We assume a NULL pointer is all zeros ... */
249 fputs_filtered ("NULL", stream);
253 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
255 /* Try to print what function it points to. */
256 print_address_demangle (addr, stream, demangle);
257 /* Return value is irrelevant except for string pointers. */
260 if (addressprint && format != 's')
262 print_address_numeric (addr, 1, stream);
265 /* For a pointer to char or unsigned char, also print the string
266 pointed to, unless pointer is null. */
267 if (TYPE_LENGTH (elttype) == 1
268 && TYPE_CODE (elttype) == TYPE_CODE_CHAR
269 && (format == 0 || format == 's')
271 && /* If print_max is UINT_MAX, the alloca below will fail.
272 In that case don't try to print the string. */
273 print_max < UINT_MAX)
275 i = val_print_string (addr, 0, stream);
277 /* Return number of characters printed, plus one for the
278 terminating null if we have "reached the end". */
279 return (i + (print_max && i != print_max));
282 case TYPE_CODE_STRING:
283 if (format && format != 's')
285 print_scalar_formatted (valaddr, type, format, 0, stream);
288 i = TYPE_LENGTH (type);
289 LA_PRINT_STRING (stream, valaddr, i, 0);
290 /* Return number of characters printed, plus one for the terminating
291 null if we have "reached the end". */
292 return (i + (print_max && i != print_max));
295 case TYPE_CODE_BITSTRING:
297 elttype = TYPE_FIELD_TYPE (type, 0);
298 check_stub_type (elttype);
299 if (TYPE_FLAGS (elttype) & TYPE_FLAG_STUB)
301 fprintf_filtered (stream, "<incomplete type>");
306 struct type *range = elttype;
307 int low_bound = TYPE_LOW_BOUND (range);
308 int high_bound = TYPE_HIGH_BOUND (range);
310 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
314 fputs_filtered ("B'", stream);
316 fputs_filtered ("[", stream);
317 for (i = low_bound; i <= high_bound; i++)
319 int element = value_bit_index (type, valaddr, i);
321 fprintf_filtered (stream, "%d", element);
325 fputs_filtered (", ", stream);
326 print_type_scalar (TYPE_TARGET_TYPE (range), i, stream);
329 /* Look for a continuous range of true elements. */
330 if (i+1 <= high_bound && value_bit_index (type, valaddr, ++i))
332 int j = i; /* j is the upper bound so far of the range */
333 fputs_filtered (":", stream);
334 while (i+1 <= high_bound
335 && value_bit_index (type, valaddr, ++i))
337 print_type_scalar (TYPE_TARGET_TYPE (range), j, stream);
342 fputs_filtered ("'", stream);
344 fputs_filtered ("]", stream);
348 case TYPE_CODE_STRUCT:
349 if (chill_is_varying_struct (type))
351 struct type *inner = TYPE_FIELD_TYPE (type, 1);
352 long length = unpack_long (TYPE_FIELD_TYPE (type, 0), valaddr);
353 char *data_addr = valaddr + TYPE_FIELD_BITPOS (type, 1) / 8;
355 switch (TYPE_CODE (inner))
357 case TYPE_CODE_STRING:
358 if (length > TYPE_LENGTH (type))
360 fprintf_filtered (stream,
361 "<dynamic length %d > static length %d>",
362 length, TYPE_LENGTH (type));
364 LA_PRINT_STRING (stream, data_addr, length, 0);
370 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
377 fprintf_filtered (stream, "LOC(");
378 print_address_numeric
379 (extract_address (valaddr, TARGET_PTR_BIT / HOST_CHAR_BIT),
382 fprintf_filtered (stream, ")");
384 fputs_filtered (": ", stream);
386 /* De-reference the reference. */
389 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) != TYPE_CODE_UNDEF)
391 value_ptr deref_val =
393 (TYPE_TARGET_TYPE (type),
394 unpack_pointer (lookup_pointer_type (builtin_type_void),
396 val_print (VALUE_TYPE (deref_val),
397 VALUE_CONTENTS (deref_val),
398 VALUE_ADDRESS (deref_val), stream, format,
399 deref_ref, recurse + 1, pretty);
402 fputs_filtered ("???", stream);
407 c_val_print (type, valaddr, address, stream, format,
408 deref_ref, recurse, pretty);
411 case TYPE_CODE_RANGE:
412 if (TYPE_TARGET_TYPE (type))
413 chill_val_print (TYPE_TARGET_TYPE (type), valaddr, address, stream,
414 format, deref_ref, recurse, pretty);
417 case TYPE_CODE_MEMBER:
418 case TYPE_CODE_UNION:
421 case TYPE_CODE_ERROR:
423 /* Let's defer printing to the C printer, rather than
424 print an error message. FIXME! */
425 c_val_print (type, valaddr, address, stream, format,
426 deref_ref, recurse, pretty);
432 /* Mutually recursive subroutines of cplus_print_value and c_val_print to
433 print out a structure's fields: cp_print_value_fields and cplus_print_value.
435 TYPE, VALADDR, STREAM, RECURSE, and PRETTY have the
436 same meanings as in cplus_print_value and c_val_print.
438 DONT_PRINT is an array of baseclass types that we
439 should not print, or zero if called from top level. */
442 chill_print_value_fields (type, valaddr, stream, format, recurse, pretty,
449 enum val_prettyprint pretty;
450 struct type **dont_print;
455 check_stub_type (type);
457 fprintf_filtered (stream, "[");
458 len = TYPE_NFIELDS (type);
461 fprintf_filtered (stream, "<No data fields>");
465 for (i = 0; i < len; i++)
469 fprintf_filtered (stream, ", ");
474 fprintf_filtered (stream, "\n");
475 print_spaces_filtered (2 + 2 * recurse, stream);
479 wrap_here (n_spaces (2 + 2 * recurse));
481 fputs_filtered (".", stream);
482 fprintf_symbol_filtered (stream, TYPE_FIELD_NAME (type, i),
483 language_chill, DMGL_NO_OPTS);
484 fputs_filtered (": ", stream);
485 if (TYPE_FIELD_PACKED (type, i))
489 /* Bitfields require special handling, especially due to byte
491 v = value_from_longest (TYPE_FIELD_TYPE (type, i),
492 unpack_field_as_long (type, valaddr, i));
494 chill_val_print (TYPE_FIELD_TYPE (type, i), VALUE_CONTENTS (v), 0,
495 stream, format, 0, recurse + 1, pretty);
499 chill_val_print (TYPE_FIELD_TYPE (type, i),
500 valaddr + TYPE_FIELD_BITPOS (type, i) / 8,
501 0, stream, format, 0, recurse + 1, pretty);
506 fprintf_filtered (stream, "\n");
507 print_spaces_filtered (2 * recurse, stream);
510 fprintf_filtered (stream, "]");
514 chill_value_print (val, stream, format, pretty)
518 enum val_prettyprint pretty;
520 /* A "repeated" value really contains several values in a row.
521 They are made by the @ operator.
522 Print such values as if they were arrays. */
524 if (VALUE_REPEATED (val))
526 register unsigned int n = VALUE_REPETITIONS (val);
527 register unsigned int typelen = TYPE_LENGTH (VALUE_TYPE (val));
528 fprintf_filtered (stream, "[");
529 /* Print arrays of characters using string syntax. */
530 if (typelen == 1 && TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_INT
532 LA_PRINT_STRING (stream, VALUE_CONTENTS (val), n, 0);
535 value_print_array_elements (val, stream, format, pretty);
537 fprintf_filtered (stream, "]");
538 return (n * typelen);
542 struct type *type = VALUE_TYPE (val);
544 /* If it is a pointer, indicate what it points to.
546 Print type also if it is a reference.
548 C++: if it is a member pointer, we will take care
549 of that when we print it. */
550 if (TYPE_CODE (type) == TYPE_CODE_PTR ||
551 TYPE_CODE (type) == TYPE_CODE_REF)
553 char *valaddr = VALUE_CONTENTS (val);
554 CORE_ADDR addr = unpack_pointer (type, valaddr);
555 if (TYPE_CODE (type) != TYPE_CODE_PTR || addr != 0)
558 char *name = TYPE_NAME (type);
560 fputs_filtered (name, stream);
561 else if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_VOID)
562 fputs_filtered ("PTR", stream);
565 fprintf_filtered (stream, "(");
566 type_print (type, "", stream, -1);
567 fprintf_filtered (stream, ")");
569 fprintf_filtered (stream, "(");
570 i = val_print (type, valaddr, VALUE_ADDRESS (val),
571 stream, format, 1, 0, pretty);
572 fprintf_filtered (stream, ")");
576 return (val_print (type, VALUE_CONTENTS (val),
577 VALUE_ADDRESS (val), stream, format, 1, 0, pretty));