* ppc-opc.c (powerpc_opcodes) <"lswx">: Use RAX for the second and
[binutils-gdb.git] / gdb / m2-valprint.c
blob3d3127ee62103524f0319973b5a2ea6d538447a3
1 /* Support for printing Modula 2 values for GDB, the GNU debugger.
3 Copyright (C) 1986, 1988-1989, 1991-1992, 1996, 1998, 2000, 2005-2012
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
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"
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 *val,
42 const struct value_print_options *options,
43 int len);
46 /* get_long_set_bounds - assigns the bounds of the long set to low and
47 high. */
49 int
50 get_long_set_bounds (struct type *type, LONGEST *low, LONGEST *high)
52 int len, i;
54 if (TYPE_CODE (type) == TYPE_CODE_STRUCT)
56 len = TYPE_NFIELDS (type);
57 i = TYPE_N_BASECLASSES (type);
58 if (len == 0)
59 return 0;
60 *low = TYPE_LOW_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, i)));
61 *high = TYPE_HIGH_BOUND (TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type,
62 len-1)));
63 return 1;
65 error (_("expecting long_set"));
66 return 0;
69 static void
70 m2_print_long_set (struct type *type, const gdb_byte *valaddr,
71 int embedded_offset, CORE_ADDR address,
72 struct ui_file *stream)
74 int empty_set = 1;
75 int element_seen = 0;
76 LONGEST previous_low = 0;
77 LONGEST previous_high= 0;
78 LONGEST i, low_bound, high_bound;
79 LONGEST field_low, field_high;
80 struct type *range;
81 int len, field;
82 struct type *target;
83 int bitval;
85 CHECK_TYPEDEF (type);
87 fprintf_filtered (stream, "{");
88 len = TYPE_NFIELDS (type);
89 if (get_long_set_bounds (type, &low_bound, &high_bound))
91 field = TYPE_N_BASECLASSES (type);
92 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
94 else
96 fprintf_filtered (stream, " %s }", _("<unknown bounds of set>"));
97 return;
100 target = TYPE_TARGET_TYPE (range);
102 if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
104 for (i = low_bound; i <= high_bound; i++)
106 bitval = value_bit_index (TYPE_FIELD_TYPE (type, field),
107 (TYPE_FIELD_BITPOS (type, field) / 8) +
108 valaddr + embedded_offset, i);
109 if (bitval < 0)
110 error (_("bit test is out of range"));
111 else if (bitval > 0)
113 previous_high = i;
114 if (! element_seen)
116 if (! empty_set)
117 fprintf_filtered (stream, ", ");
118 print_type_scalar (target, i, stream);
119 empty_set = 0;
120 element_seen = 1;
121 previous_low = i;
124 else
126 /* bit is not set */
127 if (element_seen)
129 if (previous_low+1 < previous_high)
130 fprintf_filtered (stream, "..");
131 if (previous_low+1 < previous_high)
132 print_type_scalar (target, previous_high, stream);
133 element_seen = 0;
136 if (i == field_high)
138 field++;
139 if (field == len)
140 break;
141 range = TYPE_INDEX_TYPE (TYPE_FIELD_TYPE (type, field));
142 if (get_discrete_bounds (range, &field_low, &field_high) < 0)
143 break;
144 target = TYPE_TARGET_TYPE (range);
147 if (element_seen)
149 if (previous_low+1 < previous_high)
151 fprintf_filtered (stream, "..");
152 print_type_scalar (target, previous_high, stream);
154 element_seen = 0;
156 fprintf_filtered (stream, "}");
160 static void
161 m2_print_unbounded_array (struct type *type, const gdb_byte *valaddr,
162 int embedded_offset, CORE_ADDR address,
163 struct ui_file *stream, int recurse,
164 const struct value_print_options *options)
166 struct type *content_type;
167 CORE_ADDR addr;
168 LONGEST len;
169 struct value *val;
171 CHECK_TYPEDEF (type);
172 content_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0));
174 addr = unpack_pointer (TYPE_FIELD_TYPE (type, 0),
175 (TYPE_FIELD_BITPOS (type, 0) / 8) +
176 valaddr + embedded_offset);
178 val = value_at_lazy (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0)),
179 addr);
180 len = unpack_field_as_long (type, valaddr + embedded_offset, 1);
182 fprintf_filtered (stream, "{");
183 m2_print_array_contents (value_type (val),
184 value_contents_for_printing (val),
185 value_embedded_offset (val), addr, stream,
186 recurse, val, options, len);
187 fprintf_filtered (stream, ", HIGH = %d}", (int) len);
190 static int
191 print_unpacked_pointer (struct type *type,
192 CORE_ADDR address, CORE_ADDR addr,
193 const struct value_print_options *options,
194 struct ui_file *stream)
196 struct gdbarch *gdbarch = get_type_arch (type);
197 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
198 int want_space = 0;
200 if (TYPE_CODE (elttype) == TYPE_CODE_FUNC)
202 /* Try to print what function it points to. */
203 print_function_pointer_address (options, gdbarch, addr, stream);
204 /* Return value is irrelevant except for string pointers. */
205 return 0;
208 if (options->addressprint && options->format != 's')
210 fputs_filtered (paddress (gdbarch, address), stream);
211 want_space = 1;
214 /* For a pointer to char or unsigned char, also print the string
215 pointed to, unless pointer is null. */
217 if (TYPE_LENGTH (elttype) == 1
218 && TYPE_CODE (elttype) == TYPE_CODE_INT
219 && (options->format == 0 || options->format == 's')
220 && addr != 0)
222 if (want_space)
223 fputs_filtered (" ", stream);
224 return val_print_string (TYPE_TARGET_TYPE (type), NULL, addr, -1,
225 stream, options);
228 return 0;
231 static void
232 print_variable_at_address (struct type *type,
233 const gdb_byte *valaddr,
234 struct ui_file *stream,
235 int recurse,
236 const struct value_print_options *options)
238 struct gdbarch *gdbarch = get_type_arch (type);
239 CORE_ADDR addr = unpack_pointer (type, valaddr);
240 struct type *elttype = check_typedef (TYPE_TARGET_TYPE (type));
242 fprintf_filtered (stream, "[");
243 fputs_filtered (paddress (gdbarch, addr), stream);
244 fprintf_filtered (stream, "] : ");
246 if (TYPE_CODE (elttype) != TYPE_CODE_UNDEF)
248 struct value *deref_val =
249 value_at (TYPE_TARGET_TYPE (type), unpack_pointer (type, valaddr));
251 common_val_print (deref_val, stream, recurse, options, current_language);
253 else
254 fputs_filtered ("???", stream);
258 /* m2_print_array_contents - prints out the contents of an
259 array up to a max_print values.
260 It prints arrays of char as a string
261 and all other data types as comma
262 separated values. */
264 static void
265 m2_print_array_contents (struct type *type, const gdb_byte *valaddr,
266 int embedded_offset, CORE_ADDR address,
267 struct ui_file *stream, int recurse,
268 const struct value *val,
269 const struct value_print_options *options,
270 int len)
272 int eltlen;
273 CHECK_TYPEDEF (type);
275 if (TYPE_LENGTH (type) > 0)
277 eltlen = TYPE_LENGTH (type);
278 if (options->prettyprint_arrays)
279 print_spaces_filtered (2 + 2 * recurse, stream);
280 /* For an array of chars, print with string syntax. */
281 if (eltlen == 1 &&
282 ((TYPE_CODE (type) == TYPE_CODE_INT)
283 || ((current_language->la_language == language_m2)
284 && (TYPE_CODE (type) == TYPE_CODE_CHAR)))
285 && (options->format == 0 || options->format == 's'))
286 val_print_string (type, NULL, address, len+1, stream, options);
287 else
289 fprintf_filtered (stream, "{");
290 val_print_array_elements (type, valaddr, embedded_offset,
291 address, stream, recurse, val,
292 options, 0);
293 fprintf_filtered (stream, "}");
298 /* Decorations for Modula 2. */
300 static const struct generic_val_print_decorations m2_decorations =
303 " + ",
304 " * I",
305 "TRUE",
306 "FALSE",
307 "void"
310 /* See val_print for a description of the various parameters of this
311 function; they are identical. */
313 void
314 m2_val_print (struct type *type, const gdb_byte *valaddr, int embedded_offset,
315 CORE_ADDR address, struct ui_file *stream, int recurse,
316 const struct value *original_value,
317 const struct value_print_options *options)
319 struct gdbarch *gdbarch = get_type_arch (type);
320 unsigned int i = 0; /* Number of characters printed. */
321 unsigned len;
322 struct type *elttype;
323 unsigned eltlen;
324 CORE_ADDR addr;
326 CHECK_TYPEDEF (type);
327 switch (TYPE_CODE (type))
329 case TYPE_CODE_ARRAY:
330 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0)
332 elttype = check_typedef (TYPE_TARGET_TYPE (type));
333 eltlen = TYPE_LENGTH (elttype);
334 len = TYPE_LENGTH (type) / eltlen;
335 if (options->prettyprint_arrays)
336 print_spaces_filtered (2 + 2 * recurse, stream);
337 /* For an array of chars, print with string syntax. */
338 if (eltlen == 1 &&
339 ((TYPE_CODE (elttype) == TYPE_CODE_INT)
340 || ((current_language->la_language == language_m2)
341 && (TYPE_CODE (elttype) == TYPE_CODE_CHAR)))
342 && (options->format == 0 || options->format == 's'))
344 /* If requested, look for the first null char and only print
345 elements up to it. */
346 if (options->stop_print_at_null)
348 unsigned int temp_len;
350 /* Look for a NULL char. */
351 for (temp_len = 0;
352 (valaddr + embedded_offset)[temp_len]
353 && temp_len < len && temp_len < options->print_max;
354 temp_len++);
355 len = temp_len;
358 LA_PRINT_STRING (stream, TYPE_TARGET_TYPE (type),
359 valaddr + embedded_offset, len, NULL,
360 0, options);
361 i = len;
363 else
365 fprintf_filtered (stream, "{");
366 val_print_array_elements (type, valaddr, embedded_offset,
367 address, stream,
368 recurse, original_value,
369 options, 0);
370 fprintf_filtered (stream, "}");
372 break;
374 /* Array of unspecified length: treat like pointer to first elt. */
375 print_unpacked_pointer (type, address, address, options, stream);
376 break;
378 case TYPE_CODE_PTR:
379 if (TYPE_CONST (type))
380 print_variable_at_address (type, valaddr + embedded_offset,
381 stream, recurse, options);
382 else if (options->format && options->format != 's')
383 val_print_scalar_formatted (type, valaddr, embedded_offset,
384 original_value, options, 0, stream);
385 else
387 addr = unpack_pointer (type, valaddr + embedded_offset);
388 print_unpacked_pointer (type, addr, address, options, stream);
390 break;
392 case TYPE_CODE_UNION:
393 if (recurse && !options->unionprint)
395 fprintf_filtered (stream, "{...}");
396 break;
398 /* Fall through. */
399 case TYPE_CODE_STRUCT:
400 if (m2_is_long_set (type))
401 m2_print_long_set (type, valaddr, embedded_offset, address,
402 stream);
403 else if (m2_is_unbounded_array (type))
404 m2_print_unbounded_array (type, valaddr, embedded_offset,
405 address, stream, recurse, options);
406 else
407 cp_print_value_fields (type, type, valaddr, embedded_offset,
408 address, stream, recurse, original_value,
409 options, NULL, 0);
410 break;
412 case TYPE_CODE_SET:
413 elttype = TYPE_INDEX_TYPE (type);
414 CHECK_TYPEDEF (elttype);
415 if (TYPE_STUB (elttype))
417 fprintf_filtered (stream, _("<incomplete type>"));
418 gdb_flush (stream);
419 break;
421 else
423 struct type *range = elttype;
424 LONGEST low_bound, high_bound;
425 int i;
426 int need_comma = 0;
428 fputs_filtered ("{", stream);
430 i = get_discrete_bounds (range, &low_bound, &high_bound);
431 maybe_bad_bstring:
432 if (i < 0)
434 fputs_filtered (_("<error value>"), stream);
435 goto done;
438 for (i = low_bound; i <= high_bound; i++)
440 int element = value_bit_index (type, valaddr + embedded_offset,
443 if (element < 0)
445 i = element;
446 goto maybe_bad_bstring;
448 if (element)
450 if (need_comma)
451 fputs_filtered (", ", stream);
452 print_type_scalar (range, i, stream);
453 need_comma = 1;
455 if (i + 1 <= high_bound
456 && value_bit_index (type, valaddr + embedded_offset,
457 ++i))
459 int j = i;
461 fputs_filtered ("..", stream);
462 while (i + 1 <= high_bound
463 && value_bit_index (type,
464 valaddr + embedded_offset,
465 ++i))
466 j = i;
467 print_type_scalar (range, j, stream);
471 done:
472 fputs_filtered ("}", stream);
474 break;
476 case TYPE_CODE_RANGE:
477 if (TYPE_LENGTH (type) == TYPE_LENGTH (TYPE_TARGET_TYPE (type)))
479 m2_val_print (TYPE_TARGET_TYPE (type), valaddr, embedded_offset,
480 address, stream, recurse, original_value, options);
481 break;
483 /* FIXME: create_range_type does not set the unsigned bit in a
484 range type (I think it probably should copy it from the target
485 type), so we won't print values which are too large to
486 fit in a signed integer correctly. */
487 /* FIXME: Doesn't handle ranges of enums correctly. (Can't just
488 print with the target type, though, because the size of our type
489 and the target type might differ). */
490 /* FALLTHROUGH */
492 case TYPE_CODE_REF:
493 case TYPE_CODE_ENUM:
494 case TYPE_CODE_FUNC:
495 case TYPE_CODE_INT:
496 case TYPE_CODE_FLT:
497 case TYPE_CODE_METHOD:
498 case TYPE_CODE_VOID:
499 case TYPE_CODE_ERROR:
500 case TYPE_CODE_UNDEF:
501 case TYPE_CODE_BOOL:
502 case TYPE_CODE_CHAR:
503 default:
504 generic_val_print (type, valaddr, embedded_offset, address,
505 stream, recurse, original_value, options,
506 &m2_decorations);
507 break;
509 gdb_flush (stream);