1 /* Pascal language support routines for GDB, the GNU debugger.
3 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007
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 /* This file is derived from c-lang.c */
24 #include "gdb_string.h"
27 #include "expression.h"
28 #include "parser-defs.h"
35 extern void _initialize_pascal_language (void);
38 /* Determines if type TYPE is a pascal string type.
39 Returns 1 if the type is a known pascal type
40 This function is used by p-valprint.c code to allow better string display.
41 If it is a pascal string type, then it also sets info needed
42 to get the length and the data of the string
43 length_pos, length_size and string_pos are given in bytes.
44 char_size gives the element size in bytes.
45 FIXME: if the position or the size of these fields
46 are not multiple of TARGET_CHAR_BIT then the results are wrong
47 but this does not happen for Free Pascal nor for GPC. */
49 is_pascal_string_type (struct type
*type
,int *length_pos
,
50 int *length_size
, int *string_pos
, int *char_size
,
53 if (TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
55 /* Old Borland type pascal strings from Free Pascal Compiler. */
56 /* Two fields: length and st. */
57 if (TYPE_NFIELDS (type
) == 2
58 && strcmp (TYPE_FIELDS (type
)[0].name
, "length") == 0
59 && strcmp (TYPE_FIELDS (type
)[1].name
, "st") == 0)
62 *length_pos
= TYPE_FIELD_BITPOS (type
, 0) / TARGET_CHAR_BIT
;
64 *length_size
= TYPE_LENGTH (TYPE_FIELD_TYPE (type
, 0));
66 *string_pos
= TYPE_FIELD_BITPOS (type
, 1) / TARGET_CHAR_BIT
;
70 *arrayname
= TYPE_FIELDS (type
)[1].name
;
73 /* GNU pascal strings. */
74 /* Three fields: Capacity, length and schema$ or _p_schema. */
75 if (TYPE_NFIELDS (type
) == 3
76 && strcmp (TYPE_FIELDS (type
)[0].name
, "Capacity") == 0
77 && strcmp (TYPE_FIELDS (type
)[1].name
, "length") == 0)
80 *length_pos
= TYPE_FIELD_BITPOS (type
, 1) / TARGET_CHAR_BIT
;
82 *length_size
= TYPE_LENGTH (TYPE_FIELD_TYPE (type
, 1));
84 *string_pos
= TYPE_FIELD_BITPOS (type
, 2) / TARGET_CHAR_BIT
;
85 /* FIXME: how can I detect wide chars in GPC ?? */
89 *arrayname
= TYPE_FIELDS (type
)[2].name
;
96 static void pascal_one_char (int, struct ui_file
*, int *);
98 /* Print the character C on STREAM as part of the contents of a literal
100 In_quotes is reset to 0 if a char is written with #4 notation */
103 pascal_one_char (int c
, struct ui_file
*stream
, int *in_quotes
)
106 c
&= 0xFF; /* Avoid sign bit follies */
108 if ((c
== '\'') || (PRINT_LITERAL_FORM (c
)))
111 fputs_filtered ("'", stream
);
115 fputs_filtered ("''", stream
);
118 fprintf_filtered (stream
, "%c", c
);
123 fputs_filtered ("'", stream
);
125 fprintf_filtered (stream
, "#%d", (unsigned int) c
);
129 static void pascal_emit_char (int c
, struct ui_file
*stream
, int quoter
);
131 /* Print the character C on STREAM as part of the contents of a literal
132 string whose delimiter is QUOTER. Note that that format for printing
133 characters and strings is language specific. */
136 pascal_emit_char (int c
, struct ui_file
*stream
, int quoter
)
139 pascal_one_char (c
, stream
, &in_quotes
);
141 fputs_filtered ("'", stream
);
145 pascal_printchar (int c
, struct ui_file
*stream
)
148 pascal_one_char (c
, stream
, &in_quotes
);
150 fputs_filtered ("'", stream
);
153 /* Print the character string STRING, printing at most LENGTH characters.
154 Printing stops early if the number hits print_max; repeat counts
155 are printed as appropriate. Print ellipses at the end if we
156 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES. */
159 pascal_printstr (struct ui_file
*stream
, const gdb_byte
*string
,
160 unsigned int length
, int width
, int force_ellipses
)
163 unsigned int things_printed
= 0;
167 /* If the string was not truncated due to `set print elements', and
168 the last byte of it is a null, we don't print that, in traditional C
170 if ((!force_ellipses
) && length
> 0 && string
[length
- 1] == '\0')
175 fputs_filtered ("''", stream
);
179 for (i
= 0; i
< length
&& things_printed
< print_max
; ++i
)
181 /* Position of the character we are examining
182 to see whether it is repeated. */
184 /* Number of repetitions we have detected so far. */
191 fputs_filtered (", ", stream
);
197 while (rep1
< length
&& string
[rep1
] == string
[i
])
203 if (reps
> repeat_count_threshold
)
208 fputs_filtered ("\\', ", stream
);
210 fputs_filtered ("', ", stream
);
213 pascal_printchar (string
[i
], stream
);
214 fprintf_filtered (stream
, " <repeats %u times>", reps
);
216 things_printed
+= repeat_count_threshold
;
222 if ((!in_quotes
) && (PRINT_LITERAL_FORM (c
)))
225 fputs_filtered ("\\'", stream
);
227 fputs_filtered ("'", stream
);
230 pascal_one_char (c
, stream
, &in_quotes
);
235 /* Terminate the quotes if necessary. */
239 fputs_filtered ("\\'", stream
);
241 fputs_filtered ("'", stream
);
244 if (force_ellipses
|| i
< length
)
245 fputs_filtered ("...", stream
);
248 /* Create a fundamental Pascal type using default reasonable for the current
251 Some object/debugging file formats (DWARF version 1, COFF, etc) do not
252 define fundamental types such as "int" or "double". Others (stabs or
253 DWARF version 2, etc) do define fundamental types. For the formats which
254 don't provide fundamental types, gdb can create such types using this
257 FIXME: Some compilers distinguish explicitly signed integral types
258 (signed short, signed int, signed long) from "regular" integral types
259 (short, int, long) in the debugging information. There is some dis-
260 agreement as to how useful this feature is. In particular, gcc does
261 not support this. Also, only some debugging formats allow the
262 distinction to be passed on to a debugger. For now, we always just
263 use "short", "int", or "long" as the type name, for both the implicit
264 and explicitly signed types. This also makes life easier for the
265 gdb test suite since we don't have to account for the differences
266 in output depending upon what the compiler and debugging format
267 support. We will probably have to re-examine the issue when gdb
268 starts taking it's fundamental type information directly from the
269 debugging information supplied by the compiler. fnf@cygnus.com */
271 /* Note there might be some discussion about the choosen correspondance
272 because it mainly reflects Free Pascal Compiler setup for now PM */
276 pascal_create_fundamental_type (struct objfile
*objfile
, int typeid)
278 struct type
*type
= NULL
;
283 /* FIXME: For now, if we are asked to produce a type not in this
284 language, create the equivalent of a C integer type with the
285 name "<?type?>". When all the dust settles from the type
286 reconstruction work, this should probably become an error. */
287 type
= init_type (TYPE_CODE_INT
,
288 gdbarch_int_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
289 0, "<?type?>", objfile
);
290 warning (_("internal error: no Pascal fundamental type %d"), typeid);
293 type
= init_type (TYPE_CODE_VOID
,
294 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
298 type
= init_type (TYPE_CODE_CHAR
,
299 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
303 type
= init_type (TYPE_CODE_INT
,
304 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
305 0, "shortint", objfile
);
307 case FT_UNSIGNED_CHAR
:
308 type
= init_type (TYPE_CODE_INT
,
309 TARGET_CHAR_BIT
/ TARGET_CHAR_BIT
,
310 TYPE_FLAG_UNSIGNED
, "byte", objfile
);
313 type
= init_type (TYPE_CODE_INT
,
314 gdbarch_short_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
315 0, "integer", objfile
);
317 case FT_SIGNED_SHORT
:
318 type
= init_type (TYPE_CODE_INT
,
319 gdbarch_short_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
320 0, "integer", objfile
); /* FIXME-fnf */
322 case FT_UNSIGNED_SHORT
:
323 type
= init_type (TYPE_CODE_INT
,
324 gdbarch_short_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
325 TYPE_FLAG_UNSIGNED
, "word", objfile
);
328 type
= init_type (TYPE_CODE_INT
,
329 gdbarch_int_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
330 0, "longint", objfile
);
332 case FT_SIGNED_INTEGER
:
333 type
= init_type (TYPE_CODE_INT
,
334 gdbarch_int_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
335 0, "longint", objfile
); /* FIXME -fnf */
337 case FT_UNSIGNED_INTEGER
:
338 type
= init_type (TYPE_CODE_INT
,
339 gdbarch_int_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
340 TYPE_FLAG_UNSIGNED
, "cardinal", objfile
);
343 type
= init_type (TYPE_CODE_INT
,
344 gdbarch_long_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
348 type
= init_type (TYPE_CODE_INT
,
349 gdbarch_long_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
350 0, "long", objfile
); /* FIXME -fnf */
352 case FT_UNSIGNED_LONG
:
353 type
= init_type (TYPE_CODE_INT
,
354 gdbarch_long_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
355 TYPE_FLAG_UNSIGNED
, "unsigned long", objfile
);
358 type
= init_type (TYPE_CODE_INT
,
359 gdbarch_long_long_bit
360 (current_gdbarch
) / TARGET_CHAR_BIT
,
361 0, "long long", objfile
);
363 case FT_SIGNED_LONG_LONG
:
364 type
= init_type (TYPE_CODE_INT
,
365 gdbarch_long_long_bit
366 (current_gdbarch
) / TARGET_CHAR_BIT
,
367 0, "signed long long", objfile
);
369 case FT_UNSIGNED_LONG_LONG
:
370 type
= init_type (TYPE_CODE_INT
,
371 gdbarch_long_long_bit
372 (current_gdbarch
) / TARGET_CHAR_BIT
,
373 TYPE_FLAG_UNSIGNED
, "unsigned long long", objfile
);
376 type
= init_type (TYPE_CODE_FLT
,
377 gdbarch_float_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
378 0, "float", objfile
);
380 case FT_DBL_PREC_FLOAT
:
381 type
= init_type (TYPE_CODE_FLT
,
382 gdbarch_double_bit (current_gdbarch
) / TARGET_CHAR_BIT
,
383 0, "double", objfile
);
385 case FT_EXT_PREC_FLOAT
:
386 type
= init_type (TYPE_CODE_FLT
,
387 gdbarch_long_double_bit (current_gdbarch
)
389 0, "extended", objfile
);
396 /* Table mapping opcodes into strings for printing operators
397 and precedences of the operators. */
399 const struct op_print pascal_op_print_tab
[] =
401 {",", BINOP_COMMA
, PREC_COMMA
, 0},
402 {":=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
403 {"or", BINOP_BITWISE_IOR
, PREC_BITWISE_IOR
, 0},
404 {"xor", BINOP_BITWISE_XOR
, PREC_BITWISE_XOR
, 0},
405 {"and", BINOP_BITWISE_AND
, PREC_BITWISE_AND
, 0},
406 {"=", BINOP_EQUAL
, PREC_EQUAL
, 0},
407 {"<>", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
408 {"<=", BINOP_LEQ
, PREC_ORDER
, 0},
409 {">=", BINOP_GEQ
, PREC_ORDER
, 0},
410 {">", BINOP_GTR
, PREC_ORDER
, 0},
411 {"<", BINOP_LESS
, PREC_ORDER
, 0},
412 {"shr", BINOP_RSH
, PREC_SHIFT
, 0},
413 {"shl", BINOP_LSH
, PREC_SHIFT
, 0},
414 {"+", BINOP_ADD
, PREC_ADD
, 0},
415 {"-", BINOP_SUB
, PREC_ADD
, 0},
416 {"*", BINOP_MUL
, PREC_MUL
, 0},
417 {"/", BINOP_DIV
, PREC_MUL
, 0},
418 {"div", BINOP_INTDIV
, PREC_MUL
, 0},
419 {"mod", BINOP_REM
, PREC_MUL
, 0},
420 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
421 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
422 {"not", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
423 {"^", UNOP_IND
, PREC_SUFFIX
, 1},
424 {"@", UNOP_ADDR
, PREC_PREFIX
, 0},
425 {"sizeof", UNOP_SIZEOF
, PREC_PREFIX
, 0},
429 enum pascal_primitive_types
{
430 pascal_primitive_type_int
,
431 pascal_primitive_type_long
,
432 pascal_primitive_type_short
,
433 pascal_primitive_type_char
,
434 pascal_primitive_type_float
,
435 pascal_primitive_type_double
,
436 pascal_primitive_type_void
,
437 pascal_primitive_type_long_long
,
438 pascal_primitive_type_signed_char
,
439 pascal_primitive_type_unsigned_char
,
440 pascal_primitive_type_unsigned_short
,
441 pascal_primitive_type_unsigned_int
,
442 pascal_primitive_type_unsigned_long
,
443 pascal_primitive_type_unsigned_long_long
,
444 pascal_primitive_type_long_double
,
445 pascal_primitive_type_complex
,
446 pascal_primitive_type_double_complex
,
447 nr_pascal_primitive_types
451 pascal_language_arch_info (struct gdbarch
*gdbarch
,
452 struct language_arch_info
*lai
)
454 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
455 lai
->string_char_type
= builtin
->builtin_char
;
456 lai
->primitive_type_vector
457 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_pascal_primitive_types
+ 1,
459 lai
->primitive_type_vector
[pascal_primitive_type_int
]
460 = builtin
->builtin_int
;
461 lai
->primitive_type_vector
[pascal_primitive_type_long
]
462 = builtin
->builtin_long
;
463 lai
->primitive_type_vector
[pascal_primitive_type_short
]
464 = builtin
->builtin_short
;
465 lai
->primitive_type_vector
[pascal_primitive_type_char
]
466 = builtin
->builtin_char
;
467 lai
->primitive_type_vector
[pascal_primitive_type_float
]
468 = builtin
->builtin_float
;
469 lai
->primitive_type_vector
[pascal_primitive_type_double
]
470 = builtin
->builtin_double
;
471 lai
->primitive_type_vector
[pascal_primitive_type_void
]
472 = builtin
->builtin_void
;
473 lai
->primitive_type_vector
[pascal_primitive_type_long_long
]
474 = builtin
->builtin_long_long
;
475 lai
->primitive_type_vector
[pascal_primitive_type_signed_char
]
476 = builtin
->builtin_signed_char
;
477 lai
->primitive_type_vector
[pascal_primitive_type_unsigned_char
]
478 = builtin
->builtin_unsigned_char
;
479 lai
->primitive_type_vector
[pascal_primitive_type_unsigned_short
]
480 = builtin
->builtin_unsigned_short
;
481 lai
->primitive_type_vector
[pascal_primitive_type_unsigned_int
]
482 = builtin
->builtin_unsigned_int
;
483 lai
->primitive_type_vector
[pascal_primitive_type_unsigned_long
]
484 = builtin
->builtin_unsigned_long
;
485 lai
->primitive_type_vector
[pascal_primitive_type_unsigned_long_long
]
486 = builtin
->builtin_unsigned_long_long
;
487 lai
->primitive_type_vector
[pascal_primitive_type_long_double
]
488 = builtin
->builtin_long_double
;
489 lai
->primitive_type_vector
[pascal_primitive_type_complex
]
490 = builtin
->builtin_complex
;
491 lai
->primitive_type_vector
[pascal_primitive_type_double_complex
]
492 = builtin
->builtin_double_complex
;
495 const struct language_defn pascal_language_defn
=
497 "pascal", /* Language name */
504 &exp_descriptor_standard
,
508 pascal_printchar
, /* Print a character constant */
509 pascal_printstr
, /* Function to print string constant */
510 pascal_emit_char
, /* Print a single char */
511 pascal_create_fundamental_type
, /* Create fundamental type in this language */
512 pascal_print_type
, /* Print a type using appropriate syntax */
513 pascal_val_print
, /* Print a value using appropriate syntax */
514 pascal_value_print
, /* Print a top-level value */
515 NULL
, /* Language specific skip_trampoline */
516 value_of_this
, /* value_of_this */
517 basic_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
518 basic_lookup_transparent_type
,/* lookup_transparent_type */
519 NULL
, /* Language specific symbol demangler */
520 NULL
, /* Language specific class_name_from_physname */
521 pascal_op_print_tab
, /* expression operators for printing */
522 1, /* c-style arrays */
523 0, /* String lower bound */
525 default_word_break_characters
,
526 pascal_language_arch_info
,
527 default_print_array_index
,
532 _initialize_pascal_language (void)
534 add_language (&pascal_language_defn
);