]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/m2-valprint.c
Copyright updates for 2007.
[thirdparty/binutils-gdb.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 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23 #include "defs.h"
24 #include "symtab.h"
25 #include "gdbtypes.h"
26 #include "expression.h"
27 #include "value.h"
28 #include "valprint.h"
29 #include "language.h"
30 #include "typeprint.h"
31 #include "c-lang.h"
32 #include "m2-lang.h"
33 #include "target.h"
34
35 int print_unpacked_pointer (struct type *type,
36 CORE_ADDR address, CORE_ADDR addr,
37 int format, struct ui_file *stream);
38
39
40 /* Print function pointer with inferior address ADDRESS onto stdio
41 stream STREAM. */
42
43 static void
44 print_function_pointer_address (CORE_ADDR address, struct ui_file *stream)
45 {
46 CORE_ADDR func_addr = gdbarch_convert_from_func_ptr_addr (current_gdbarch,
47 address,
48 &current_target);
49
50 /* If the function pointer is represented by a description, print the
51 address of the description. */
52 if (addressprint && func_addr != address)
53 {
54 fputs_filtered ("@", stream);
55 fputs_filtered (paddress (address), stream);
56 fputs_filtered (": ", stream);
57 }
58 print_address_demangle (func_addr, stream, demangle);
59 }
60
61 /*
62 * get_long_set_bounds - assigns the bounds of the long set to low and high.
63 */
64
65 int
66 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
67 {
68 int len, i;
69
70 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
71 {
72 len = TYPE_NFIELDS (type);
73 i = TYPE_N_BASECLASSES (type);
74 if (len == 0)
75 return 0;
76 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
77 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
78 len-1)));
79 return 1;
80 }
81 error (_("expecting long_set"));
82 return 0;
83 }
84
85 static void
86 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
87 int embedded_offset, CORE_ADDR address,
88 struct ui_file *stream, int format,
89 enum val_prettyprint pretty)
90 {
91 int empty_set = 1;
92 int element_seen = 0;
93 LONGEST previous_low = 0;
94 LONGEST previous_high= 0;
95 LONGEST i, low_bound, high_bound;
96 LONGEST field_low, field_high;
97 struct type *range;
98 int len, field;
99 struct type *target;
100 int bitval;
101
102 CHECK_TYPEDEF (type);
103
104 fprintf_filtered (stream, "{");
105 len = TYPE_NFIELDS (type);
106 if (get_long_set_bounds (type, &low_bound, &high_bound))
107 {
108 field = TYPE_N_BASECLASSES (type);
109 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
110 }
111 else
112 {
113 fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
114 return;
115 }
116
117 target = TYPE_TARGET_TYPE (range);
118 if (target == NULL)
119 target = builtin_type_int;
120
121 if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
122 {
123 for (i = low_bound; i <= high_bound; i++)
124 {
125 bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
126 (TYPE_FIELD_BITPOS (type, field) / 8) +
127 valaddr + embedded_offset, i);
128 if (bitval < 0)
129 error (_("bit test is out of range"));
130 else if (bitval > 0)
131 {
132 previous_high = i;
133 if (! element_seen)
134 {
135 if (! empty_set)
136 fprintf_filtered (stream, ", ");
137 print_type_scalar (target, i, stream);
138 empty_set = 0;
139 element_seen = 1;
140 previous_low = i;
141 }
142 }
143 else
144 {
145 /* bit is not set */
146 if (element_seen)
147 {
148 if (previous_low+1 < previous_high)
149 fprintf_filtered (stream, "..");
150 if (previous_low+1 < previous_high)
151 print_type_scalar (target, previous_high, stream);
152 element_seen = 0;
153 }
154 }
155 if (i == field_high)
156 {
157 field++;
158 if (field == len)
159 break;
160 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
161 if (get_discrete_bounds (range, &field_low, &field_high) < 0)
162 break;
163 target = TYPE_TARGET_TYPE (range);
164 if (target == NULL)
165 target = builtin_type_int;
166 }
167 }
168 if (element_seen)
169 {
170 if (previous_low+1 < previous_high)
171 {
172 fprintf_filtered (stream, "..");
173 print_type_scalar (target, previous_high, stream);
174 }
175 element_seen = 0;
176 }
177 fprintf_filtered (stream, "}");
178 }
179 }
180
181 int
182 print_unpacked_pointer (struct type *type,
183 CORE_ADDR address, CORE_ADDR addr,
184 int format, struct ui_file *stream)
185 {
186 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
187
188 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
189 {
190 /* Try to print what function it points to. */
191 print_function_pointer_address (addr, stream);
192 /* Return value is irrelevant except for string pointers. */
193 return 0;
194 }
195
196 if (addressprint && format != 's')
197 fputs_filtered (paddress (address), stream);
198
199 /* For a pointer to char or unsigned char, also print the string
200 pointed to, unless pointer is null. */
201
202 if (TYPE_LENGTH (elttype) == 1
203 && TYPE_CODE (elttype) == TYPE_CODE_INT
204 && (format == 0 || format == 's')
205 && addr != 0)
206 return val_print_string (addr, -1, TYPE_LENGTH (elttype), stream);
207
208 return 0;
209 }
210
211 static void
212 print_variable_at_address (struct type *type, const gdb_byte *valaddr,
213 struct ui_file *stream, int format,
214 int deref_ref, int recurse,
215 enum val_prettyprint pretty)
216 {
217 CORE_ADDR addr = unpack_pointer (type, valaddr);
218 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
219
220 fprintf_filtered (stream, "[");
221 fputs_filtered (paddress (addr), stream);
222 fprintf_filtered (stream, "] : ");
223
224 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
225 {
226 struct value *deref_val =
227 value_at
228 (TYPE_TARGET_TYPE (type),
229 unpack_pointer (lookup_pointer_type (builtin_type_void),
230 valaddr));
231 common_val_print (deref_val, stream, format, deref_ref,
232 recurse, pretty);
233 }
234 else
235 fputs_filtered ("???", stream);
236 }
237
238 /* Print data of type TYPE located at VALADDR (within GDB), which came from
239 the inferior at address ADDRESS, onto stdio stream STREAM according to
240 FORMAT (a letter or 0 for natural format). The data at VALADDR is in
241 target byte order.
242
243 If the data are a string pointer, returns the number of string characters
244 printed.
245
246 If DEREF_REF is nonzero, then dereference references, otherwise just print
247 them like pointers.
248
249 The PRETTY parameter controls prettyprinting. */
250
251 int
252 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
253 CORE_ADDR address, struct ui_file *stream, int format,
254 int deref_ref, int recurse, enum val_prettyprint pretty)
255 {
256 unsigned int i = 0; /* Number of characters printed */
257 unsigned len;
258 struct type *elttype;
259 unsigned eltlen;
260 int length_pos, length_size, string_pos;
261 int char_size;
262 LONGEST val;
263 CORE_ADDR addr;
264
265 CHECK_TYPEDEF (type);
266 switch (TYPE_CODE (type))
267 {
268 case TYPE_CODE_ARRAY:
269 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
270 {
271 elttype = check_typedef (TYPE_TARGET_TYPE (type));
272 eltlen = TYPE_LENGTH (elttype);
273 len = TYPE_LENGTH (type) / eltlen;
274 if (prettyprint_arrays)
275 print_spaces_filtered (2 + 2 * recurse, stream);
276 /* For an array of chars, print with string syntax. */
277 if (eltlen == 1 &&
278 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
279 || ((current_language->la_language == language_m2)
280 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
281 && (format == 0 || format == 's'))
282 {
283 /* If requested, look for the first null char and only print
284 elements up to it. */
285 if (stop_print_at_null)
286 {
287 unsigned int temp_len;
288
289 /* Look for a NULL char. */
290 for (temp_len = 0;
291 (valaddr + embedded_offset)[temp_len]
292 && temp_len < len && temp_len < print_max;
293 temp_len++);
294 len = temp_len;
295 }
296
297 LA_PRINT_STRING (stream, valaddr + embedded_offset, len, 1, 0);
298 i = len;
299 }
300 else
301 {
302 fprintf_filtered (stream, "{");
303 val_print_array_elements (type, valaddr + embedded_offset,
304 address, stream, format, deref_ref,
305 recurse, pretty, 0);
306 fprintf_filtered (stream, "}");
307 }
308 break;
309 }
310 /* Array of unspecified length: treat like pointer to first elt. */
311 print_unpacked_pointer (type, address, address, format, stream);
312 break;
313
314 case TYPE_CODE_PTR:
315 if (TYPE_CONST (type))
316 print_variable_at_address (type, valaddr + embedded_offset,
317 stream, format, deref_ref, recurse,
318 pretty);
319 else if (format && format != 's')
320 print_scalar_formatted (valaddr + embedded_offset, type, format,
321 0, stream);
322 else
323 {
324 addr = unpack_pointer (type, valaddr + embedded_offset);
325 print_unpacked_pointer (type, addr, address, format, stream);
326 }
327 break;
328
329 case TYPE_CODE_REF:
330 elttype = check_typedef (TYPE_TARGET_TYPE (type));
331 if (addressprint)
332 {
333 CORE_ADDR addr
334 = extract_typed_address (valaddr + embedded_offset, type);
335 fprintf_filtered (stream, "@");
336 fputs_filtered (paddress (addr), stream);
337 if (deref_ref)
338 fputs_filtered (": ", stream);
339 }
340 /* De-reference the reference. */
341 if (deref_ref)
342 {
343 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
344 {
345 struct value *deref_val =
346 value_at
347 (TYPE_TARGET_TYPE (type),
348 unpack_pointer (lookup_pointer_type (builtin_type_void),
349 valaddr + embedded_offset));
350 common_val_print (deref_val, stream, format, deref_ref,
351 recurse, pretty);
352 }
353 else
354 fputs_filtered ("???", stream);
355 }
356 break;
357
358 case TYPE_CODE_UNION:
359 if (recurse && !unionprint)
360 {
361 fprintf_filtered (stream, "{...}");
362 break;
363 }
364 /* Fall through. */
365 case TYPE_CODE_STRUCT:
366 if (m2_is_long_set (type))
367 m2_print_long_set (type, valaddr, embedded_offset, address,
368 stream, format, pretty);
369 else
370 cp_print_value_fields (type, type, valaddr, embedded_offset,
371 address, stream, format,
372 recurse, pretty, NULL, 0);
373 break;
374
375 case TYPE_CODE_ENUM:
376 if (format)
377 {
378 print_scalar_formatted (valaddr + embedded_offset, type,
379 format, 0, stream);
380 break;
381 }
382 len = TYPE_NFIELDS (type);
383 val = unpack_long (type, valaddr + embedded_offset);
384 for (i = 0; i < len; i++)
385 {
386 QUIT;
387 if (val == TYPE_FIELD_BITPOS (type, i))
388 {
389 break;
390 }
391 }
392 if (i < len)
393 {
394 fputs_filtered (TYPE_FIELD_NAME (type, i), stream);
395 }
396 else
397 {
398 print_longest (stream, 'd', 0, val);
399 }
400 break;
401
402 case TYPE_CODE_FUNC:
403 if (format)
404 {
405 print_scalar_formatted (valaddr + embedded_offset, type,
406 format, 0, stream);
407 break;
408 }
409 /* FIXME, we should consider, at least for ANSI C language, eliminating
410 the distinction made between FUNCs and POINTERs to FUNCs. */
411 fprintf_filtered (stream, "{");
412 type_print (type, "", stream, -1);
413 fprintf_filtered (stream, "} ");
414 /* Try to print what function it points to, and its address. */
415 print_address_demangle (address, stream, demangle);
416 break;
417
418 case TYPE_CODE_BOOL:
419 format = format ? format : output_format;
420 if (format)
421 print_scalar_formatted (valaddr + embedded_offset, type,
422 format, 0, stream);
423 else
424 {
425 val = unpack_long (type, valaddr + embedded_offset);
426 if (val == 0)
427 fputs_filtered ("FALSE", stream);
428 else if (val == 1)
429 fputs_filtered ("TRUE", stream);
430 else
431 fprintf_filtered (stream, "%ld)", (long int) val);
432 }
433 break;
434
435 case TYPE_CODE_RANGE:
436 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
437 {
438 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
439 address, stream, format, deref_ref, recurse, pretty);
440 break;
441 }
442 /* FIXME: create_range_type does not set the unsigned bit in a
443 range type (I think it probably should copy it from the target
444 type), so we won't print values which are too large to
445 fit in a signed integer correctly. */
446 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
447 print with the target type, though, because the size of our type
448 and the target type might differ). */
449 /* FALLTHROUGH */
450
451 case TYPE_CODE_INT:
452 format = format ? format : output_format;
453 if (format)
454 print_scalar_formatted (valaddr + embedded_offset, type, format,
455 0, stream);
456 else
457 val_print_type_code_int (type, valaddr + embedded_offset, stream);
458 break;
459
460 case TYPE_CODE_CHAR:
461 format = format ? format : output_format;
462 if (format)
463 print_scalar_formatted (valaddr + embedded_offset, type,
464 format, 0, stream);
465 else
466 {
467 val = unpack_long (type, valaddr + embedded_offset);
468 if (TYPE_UNSIGNED (type))
469 fprintf_filtered (stream, "%u", (unsigned int) val);
470 else
471 fprintf_filtered (stream, "%d", (int) val);
472 fputs_filtered (" ", stream);
473 LA_PRINT_CHAR ((unsigned char) val, stream);
474 }
475 break;
476
477 case TYPE_CODE_FLT:
478 if (format)
479 print_scalar_formatted (valaddr + embedded_offset, type,
480 format, 0, stream);
481 else
482 print_floating (valaddr + embedded_offset, type, stream);
483 break;
484
485 case TYPE_CODE_METHOD:
486 break;
487
488 case TYPE_CODE_BITSTRING:
489 case TYPE_CODE_SET:
490 elttype = TYPE_INDEX_TYPE (type);
491 CHECK_TYPEDEF (elttype);
492 if (TYPE_STUB (elttype))
493 {
494 fprintf_filtered (stream, _("<incomplete type>"));
495 gdb_flush (stream);
496 break;
497 }
498 else
499 {
500 struct type *range = elttype;
501 LONGEST low_bound, high_bound;
502 int i;
503 int is_bitstring = TYPE_CODE (type) == TYPE_CODE_BITSTRING;
504 int need_comma = 0;
505
506 if (is_bitstring)
507 fputs_filtered ("B'", stream);
508 else
509 fputs_filtered ("{", stream);
510
511 i = get_discrete_bounds (range, &low_bound, &high_bound);
512 maybe_bad_bstring:
513 if (i < 0)
514 {
515 fputs_filtered (_("<error value>"), stream);
516 goto done;
517 }
518
519 for (i = low_bound; i <= high_bound; i++)
520 {
521 int element = value_bit_index (type, valaddr + embedded_offset,
522 i);
523 if (element < 0)
524 {
525 i = element;
526 goto maybe_bad_bstring;
527 }
528 if (is_bitstring)
529 fprintf_filtered (stream, "%d", element);
530 else if (element)
531 {
532 if (need_comma)
533 fputs_filtered (", ", stream);
534 print_type_scalar (range, i, stream);
535 need_comma = 1;
536
537 if (i + 1 <= high_bound
538 && value_bit_index (type, valaddr + embedded_offset,
539 ++i))
540 {
541 int j = i;
542 fputs_filtered ("..", stream);
543 while (i + 1 <= high_bound
544 && value_bit_index (type,
545 valaddr + embedded_offset,
546 ++i))
547 j = i;
548 print_type_scalar (range, j, stream);
549 }
550 }
551 }
552 done:
553 if (is_bitstring)
554 fputs_filtered ("'", stream);
555 else
556 fputs_filtered ("}", stream);
557 }
558 break;
559
560 case TYPE_CODE_VOID:
561 fprintf_filtered (stream, "void");
562 break;
563
564 case TYPE_CODE_ERROR:
565 fprintf_filtered (stream, _("<error type>"));
566 break;
567
568 case TYPE_CODE_UNDEF:
569 /* This happens (without TYPE_FLAG_STUB set) on systems which don't use
570 dbx xrefs (NO_DBX_XREFS in gcc) if a file has a "struct foo *bar"
571 and no complete type for struct foo in that file. */
572 fprintf_filtered (stream, _("<incomplete type>"));
573 break;
574
575 default:
576 error (_("Invalid m2 type code %d in symbol table."), TYPE_CODE (type));
577 }
578 gdb_flush (stream);
579 return (0);
580 }