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