gdb/p-lang.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Pascal language support routines for GDB, the GNU debugger.

  2.    Copyright (C) 2000-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* This file is derived from c-lang.c */

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "expression.h"
  19. #include "parser-defs.h"
  20. #include "language.h"
  21. #include "varobj.h"
  22. #include "p-lang.h"
  23. #include "valprint.h"
  24. #include "value.h"
  25. #include <ctype.h>

  26. extern void _initialize_pascal_language (void);


  27. /* All GPC versions until now (2007-09-27) also define a symbol called
  28.    '_p_initialize'.  Check for the presence of this symbol first.  */
  29. static const char GPC_P_INITIALIZE[] = "_p_initialize";

  30. /* The name of the symbol that GPC uses as the name of the main
  31.    procedure (since version 20050212).  */
  32. static const char GPC_MAIN_PROGRAM_NAME_1[] = "_p__M0_main_program";

  33. /* Older versions of GPC (versions older than 20050212) were using
  34.    a different name for the main procedure.  */
  35. static const char GPC_MAIN_PROGRAM_NAME_2[] = "pascal_main_program";

  36. /* Function returning the special symbol name used
  37.    by GPC for the main procedure in the main program
  38.    if it is found in minimal symbol list.
  39.    This function tries to find minimal symbols generated by GPC
  40.    so that it finds the even if the program was compiled
  41.    without debugging information.
  42.    According to information supplied by Waldeck Hebisch,
  43.    this should work for all versions posterior to June 2000.  */

  44. const char *
  45. pascal_main_name (void)
  46. {
  47.   struct bound_minimal_symbol msym;

  48.   msym = lookup_minimal_symbol (GPC_P_INITIALIZE, NULL, NULL);

  49.   /*  If '_p_initialize' was not found, the main program is likely not
  50.      written in Pascal.  */
  51.   if (msym.minsym == NULL)
  52.     return NULL;

  53.   msym = lookup_minimal_symbol (GPC_MAIN_PROGRAM_NAME_1, NULL, NULL);
  54.   if (msym.minsym != NULL)
  55.     {
  56.       return GPC_MAIN_PROGRAM_NAME_1;
  57.     }

  58.   msym = lookup_minimal_symbol (GPC_MAIN_PROGRAM_NAME_2, NULL, NULL);
  59.   if (msym.minsym != NULL)
  60.     {
  61.       return GPC_MAIN_PROGRAM_NAME_2;
  62.     }

  63.   /*  No known entry procedure found, the main program is probably
  64.       not compiled with GPC.  */
  65.   return NULL;
  66. }

  67. /* Determines if type TYPE is a pascal string type.
  68.    Returns a positive value if the type is a known pascal string type.
  69.    This function is used by p-valprint.c code to allow better string display.
  70.    If it is a pascal string type, then it also sets info needed
  71.    to get the length and the data of the string
  72.    length_pos, length_size and string_pos are given in bytes.
  73.    char_size gives the element size in bytes.
  74.    FIXME: if the position or the size of these fields
  75.    are not multiple of TARGET_CHAR_BIT then the results are wrong
  76.    but this does not happen for Free Pascal nor for GPC.  */
  77. int
  78. is_pascal_string_type (struct type *type,int *length_pos,
  79.                        int *length_size, int *string_pos,
  80.                        struct type **char_type,
  81.                        const char **arrayname)
  82. {
  83.   if (type != NULL && TYPE_CODE (type) == TYPE_CODE_STRUCT)
  84.     {
  85.       /* Old Borland type pascal strings from Free Pascal Compiler.  */
  86.       /* Two fields: length and st.  */
  87.       if (TYPE_NFIELDS (type) == 2
  88.           && TYPE_FIELD_NAME (type, 0)
  89.           && strcmp (TYPE_FIELD_NAME (type, 0), "length") == 0
  90.           && TYPE_FIELD_NAME (type, 1)
  91.           && strcmp (TYPE_FIELD_NAME (type, 1), "st") == 0)
  92.         {
  93.           if (length_pos)
  94.             *length_pos = TYPE_FIELD_BITPOS (type, 0) / TARGET_CHAR_BIT;
  95.           if (length_size)
  96.             *length_size = TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
  97.           if (string_pos)
  98.             *string_pos = TYPE_FIELD_BITPOS (type, 1) / TARGET_CHAR_BIT;
  99.           if (char_type)
  100.             *char_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 1));
  101.            if (arrayname)
  102.             *arrayname = TYPE_FIELD_NAME (type, 1);
  103.          return 2;
  104.         };
  105.       /* GNU pascal strings.  */
  106.       /* Three fields: Capacity, length and schema$ or _p_schema.  */
  107.       if (TYPE_NFIELDS (type) == 3
  108.           && TYPE_FIELD_NAME (type, 0)
  109.           && strcmp (TYPE_FIELD_NAME (type, 0), "Capacity") == 0
  110.           && TYPE_FIELD_NAME (type, 1)
  111.           && strcmp (TYPE_FIELD_NAME (type, 1), "length") == 0)
  112.         {
  113.           if (length_pos)
  114.             *length_pos = TYPE_FIELD_BITPOS (type, 1) / TARGET_CHAR_BIT;
  115.           if (length_size)
  116.             *length_size = TYPE_LENGTH (TYPE_FIELD_TYPE (type, 1));
  117.           if (string_pos)
  118.             *string_pos = TYPE_FIELD_BITPOS (type, 2) / TARGET_CHAR_BIT;
  119.           /* FIXME: how can I detect wide chars in GPC ??  */
  120.           if (char_type)
  121.             {
  122.               *char_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 2));

  123.               if (TYPE_CODE (*char_type) == TYPE_CODE_ARRAY)
  124.                 *char_type = TYPE_TARGET_TYPE (*char_type);
  125.             }
  126.            if (arrayname)
  127.             *arrayname = TYPE_FIELD_NAME (type, 2);
  128.          return 3;
  129.         };
  130.     }
  131.   return 0;
  132. }

  133. static void pascal_one_char (int, struct ui_file *, int *);

  134. /* Print the character C on STREAM as part of the contents of a literal
  135.    string.
  136.    In_quotes is reset to 0 if a char is written with #4 notation.  */

  137. static void
  138. pascal_one_char (int c, struct ui_file *stream, int *in_quotes)
  139. {
  140.   if (c == '\'' || ((unsigned int) c <= 0xff && (PRINT_LITERAL_FORM (c))))
  141.     {
  142.       if (!(*in_quotes))
  143.         fputs_filtered ("'", stream);
  144.       *in_quotes = 1;
  145.       if (c == '\'')
  146.         {
  147.           fputs_filtered ("''", stream);
  148.         }
  149.       else
  150.         fprintf_filtered (stream, "%c", c);
  151.     }
  152.   else
  153.     {
  154.       if (*in_quotes)
  155.         fputs_filtered ("'", stream);
  156.       *in_quotes = 0;
  157.       fprintf_filtered (stream, "#%d", (unsigned int) c);
  158.     }
  159. }

  160. static void pascal_emit_char (int c, struct type *type,
  161.                               struct ui_file *stream, int quoter);

  162. /* Print the character C on STREAM as part of the contents of a literal
  163.    string whose delimiter is QUOTER.  Note that that format for printing
  164.    characters and strings is language specific.  */

  165. static void
  166. pascal_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
  167. {
  168.   int in_quotes = 0;

  169.   pascal_one_char (c, stream, &in_quotes);
  170.   if (in_quotes)
  171.     fputs_filtered ("'", stream);
  172. }

  173. void
  174. pascal_printchar (int c, struct type *type, struct ui_file *stream)
  175. {
  176.   int in_quotes = 0;

  177.   pascal_one_char (c, stream, &in_quotes);
  178.   if (in_quotes)
  179.     fputs_filtered ("'", stream);
  180. }

  181. /* Print the character string STRING, printing at most LENGTH characters.
  182.    Printing stops early if the number hits print_max; repeat counts
  183.    are printed as appropriate.  Print ellipses at the end if we
  184.    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.  */

  185. void
  186. pascal_printstr (struct ui_file *stream, struct type *type,
  187.                  const gdb_byte *string, unsigned int length,
  188.                  const char *encoding, int force_ellipses,
  189.                  const struct value_print_options *options)
  190. {
  191.   enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  192.   unsigned int i;
  193.   unsigned int things_printed = 0;
  194.   int in_quotes = 0;
  195.   int need_comma = 0;
  196.   int width;

  197.   /* Preserve TYPE's original type, just set its LENGTH.  */
  198.   check_typedef (type);
  199.   width = TYPE_LENGTH (type);

  200.   /* If the string was not truncated due to `set print elements', and
  201.      the last byte of it is a null, we don't print that, in traditional C
  202.      style.  */
  203.   if ((!force_ellipses) && length > 0
  204.         && extract_unsigned_integer (string + (length - 1) * width, width,
  205.                                      byte_order) == 0)
  206.     length--;

  207.   if (length == 0)
  208.     {
  209.       fputs_filtered ("''", stream);
  210.       return;
  211.     }

  212.   for (i = 0; i < length && things_printed < options->print_max; ++i)
  213.     {
  214.       /* Position of the character we are examining
  215.          to see whether it is repeated.  */
  216.       unsigned int rep1;
  217.       /* Number of repetitions we have detected so far.  */
  218.       unsigned int reps;
  219.       unsigned long int current_char;

  220.       QUIT;

  221.       if (need_comma)
  222.         {
  223.           fputs_filtered (", ", stream);
  224.           need_comma = 0;
  225.         }

  226.       current_char = extract_unsigned_integer (string + i * width, width,
  227.                                                byte_order);

  228.       rep1 = i + 1;
  229.       reps = 1;
  230.       while (rep1 < length
  231.              && extract_unsigned_integer (string + rep1 * width, width,
  232.                                           byte_order) == current_char)
  233.         {
  234.           ++rep1;
  235.           ++reps;
  236.         }

  237.       if (reps > options->repeat_count_threshold)
  238.         {
  239.           if (in_quotes)
  240.             {
  241.               fputs_filtered ("', ", stream);
  242.               in_quotes = 0;
  243.             }
  244.           pascal_printchar (current_char, type, stream);
  245.           fprintf_filtered (stream, " <repeats %u times>", reps);
  246.           i = rep1 - 1;
  247.           things_printed += options->repeat_count_threshold;
  248.           need_comma = 1;
  249.         }
  250.       else
  251.         {
  252.           if ((!in_quotes) && (PRINT_LITERAL_FORM (current_char)))
  253.             {
  254.               fputs_filtered ("'", stream);
  255.               in_quotes = 1;
  256.             }
  257.           pascal_one_char (current_char, stream, &in_quotes);
  258.           ++things_printed;
  259.         }
  260.     }

  261.   /* Terminate the quotes if necessary.  */
  262.   if (in_quotes)
  263.     fputs_filtered ("'", stream);

  264.   if (force_ellipses || i < length)
  265.     fputs_filtered ("...", stream);
  266. }


  267. /* Table mapping opcodes into strings for printing operators
  268.    and precedences of the operators.  */

  269. const struct op_print pascal_op_print_tab[] =
  270. {
  271.   {",", BINOP_COMMA, PREC_COMMA, 0},
  272.   {":=", BINOP_ASSIGN, PREC_ASSIGN, 1},
  273.   {"or", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
  274.   {"xor", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
  275.   {"and", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
  276.   {"=", BINOP_EQUAL, PREC_EQUAL, 0},
  277.   {"<>", BINOP_NOTEQUAL, PREC_EQUAL, 0},
  278.   {"<=", BINOP_LEQ, PREC_ORDER, 0},
  279.   {">=", BINOP_GEQ, PREC_ORDER, 0},
  280.   {">", BINOP_GTR, PREC_ORDER, 0},
  281.   {"<", BINOP_LESS, PREC_ORDER, 0},
  282.   {"shr", BINOP_RSH, PREC_SHIFT, 0},
  283.   {"shl", BINOP_LSH, PREC_SHIFT, 0},
  284.   {"+", BINOP_ADD, PREC_ADD, 0},
  285.   {"-", BINOP_SUB, PREC_ADD, 0},
  286.   {"*", BINOP_MUL, PREC_MUL, 0},
  287.   {"/", BINOP_DIV, PREC_MUL, 0},
  288.   {"div", BINOP_INTDIV, PREC_MUL, 0},
  289.   {"mod", BINOP_REM, PREC_MUL, 0},
  290.   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
  291.   {"-", UNOP_NEG, PREC_PREFIX, 0},
  292.   {"not", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
  293.   {"^", UNOP_IND, PREC_SUFFIX, 1},
  294.   {"@", UNOP_ADDR, PREC_PREFIX, 0},
  295.   {"sizeof", UNOP_SIZEOF, PREC_PREFIX, 0},
  296.   {NULL, 0, 0, 0}
  297. };

  298. enum pascal_primitive_types {
  299.   pascal_primitive_type_int,
  300.   pascal_primitive_type_long,
  301.   pascal_primitive_type_short,
  302.   pascal_primitive_type_char,
  303.   pascal_primitive_type_float,
  304.   pascal_primitive_type_double,
  305.   pascal_primitive_type_void,
  306.   pascal_primitive_type_long_long,
  307.   pascal_primitive_type_signed_char,
  308.   pascal_primitive_type_unsigned_char,
  309.   pascal_primitive_type_unsigned_short,
  310.   pascal_primitive_type_unsigned_int,
  311.   pascal_primitive_type_unsigned_long,
  312.   pascal_primitive_type_unsigned_long_long,
  313.   pascal_primitive_type_long_double,
  314.   pascal_primitive_type_complex,
  315.   pascal_primitive_type_double_complex,
  316.   nr_pascal_primitive_types
  317. };

  318. static void
  319. pascal_language_arch_info (struct gdbarch *gdbarch,
  320.                            struct language_arch_info *lai)
  321. {
  322.   const struct builtin_type *builtin = builtin_type (gdbarch);

  323.   lai->string_char_type = builtin->builtin_char;
  324.   lai->primitive_type_vector
  325.     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_pascal_primitive_types + 1,
  326.                               struct type *);
  327.   lai->primitive_type_vector [pascal_primitive_type_int]
  328.     = builtin->builtin_int;
  329.   lai->primitive_type_vector [pascal_primitive_type_long]
  330.     = builtin->builtin_long;
  331.   lai->primitive_type_vector [pascal_primitive_type_short]
  332.     = builtin->builtin_short;
  333.   lai->primitive_type_vector [pascal_primitive_type_char]
  334.     = builtin->builtin_char;
  335.   lai->primitive_type_vector [pascal_primitive_type_float]
  336.     = builtin->builtin_float;
  337.   lai->primitive_type_vector [pascal_primitive_type_double]
  338.     = builtin->builtin_double;
  339.   lai->primitive_type_vector [pascal_primitive_type_void]
  340.     = builtin->builtin_void;
  341.   lai->primitive_type_vector [pascal_primitive_type_long_long]
  342.     = builtin->builtin_long_long;
  343.   lai->primitive_type_vector [pascal_primitive_type_signed_char]
  344.     = builtin->builtin_signed_char;
  345.   lai->primitive_type_vector [pascal_primitive_type_unsigned_char]
  346.     = builtin->builtin_unsigned_char;
  347.   lai->primitive_type_vector [pascal_primitive_type_unsigned_short]
  348.     = builtin->builtin_unsigned_short;
  349.   lai->primitive_type_vector [pascal_primitive_type_unsigned_int]
  350.     = builtin->builtin_unsigned_int;
  351.   lai->primitive_type_vector [pascal_primitive_type_unsigned_long]
  352.     = builtin->builtin_unsigned_long;
  353.   lai->primitive_type_vector [pascal_primitive_type_unsigned_long_long]
  354.     = builtin->builtin_unsigned_long_long;
  355.   lai->primitive_type_vector [pascal_primitive_type_long_double]
  356.     = builtin->builtin_long_double;
  357.   lai->primitive_type_vector [pascal_primitive_type_complex]
  358.     = builtin->builtin_complex;
  359.   lai->primitive_type_vector [pascal_primitive_type_double_complex]
  360.     = builtin->builtin_double_complex;

  361.   lai->bool_type_symbol = "boolean";
  362.   lai->bool_type_default = builtin->builtin_bool;
  363. }

  364. const struct language_defn pascal_language_defn =
  365. {
  366.   "pascal",                        /* Language name */
  367.   "Pascal",
  368.   language_pascal,
  369.   range_check_on,
  370.   case_sensitive_on,
  371.   array_row_major,
  372.   macro_expansion_no,
  373.   &exp_descriptor_standard,
  374.   pascal_parse,
  375.   pascal_error,
  376.   null_post_parser,
  377.   pascal_printchar,                /* Print a character constant */
  378.   pascal_printstr,                /* Function to print string constant */
  379.   pascal_emit_char,                /* Print a single char */
  380.   pascal_print_type,                /* Print a type using appropriate syntax */
  381.   pascal_print_typedef,                /* Print a typedef using appropriate syntax */
  382.   pascal_val_print,                /* Print a value using appropriate syntax */
  383.   pascal_value_print,                /* Print a top-level value */
  384.   default_read_var_value,        /* la_read_var_value */
  385.   NULL,                                /* Language specific skip_trampoline */
  386.   "this",                        /* name_of_this */
  387.   basic_lookup_symbol_nonlocal,        /* lookup_symbol_nonlocal */
  388.   basic_lookup_transparent_type,/* lookup_transparent_type */
  389.   NULL,                                /* Language specific symbol demangler */
  390.   NULL,                                /* Language specific class_name_from_physname */
  391.   pascal_op_print_tab,                /* expression operators for printing */
  392.   1,                                /* c-style arrays */
  393.   0,                                /* String lower bound */
  394.   default_word_break_characters,
  395.   default_make_symbol_completion_list,
  396.   pascal_language_arch_info,
  397.   default_print_array_index,
  398.   default_pass_by_reference,
  399.   default_get_string,
  400.   NULL,                                /* la_get_symbol_name_cmp */
  401.   iterate_over_symbols,
  402.   &default_varobj_ops,
  403.   NULL,
  404.   NULL,
  405.   LANG_MAGIC
  406. };

  407. void
  408. _initialize_pascal_language (void)
  409. {
  410.   add_language (&pascal_language_defn);
  411. }