m2-lang.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Modula 2 language support routines for GDB, the GNU debugger.
  2. Copyright (C) 1992-2022 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. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "expression.h"
  18. #include "parser-defs.h"
  19. #include "language.h"
  20. #include "varobj.h"
  21. #include "m2-lang.h"
  22. #include "c-lang.h"
  23. #include "valprint.h"
  24. #include "gdbarch.h"
  25. #include "m2-exp.h"
  26. /* A helper function for UNOP_HIGH. */
  27. struct value *
  28. eval_op_m2_high (struct type *expect_type, struct expression *exp,
  29. enum noside noside,
  30. struct value *arg1)
  31. {
  32. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  33. return arg1;
  34. else
  35. {
  36. arg1 = coerce_ref (arg1);
  37. struct type *type = check_typedef (value_type (arg1));
  38. if (m2_is_unbounded_array (type))
  39. {
  40. struct value *temp = arg1;
  41. type = type->field (1).type ();
  42. /* i18n: Do not translate the "_m2_high" part! */
  43. arg1 = value_struct_elt (&temp, {}, "_m2_high", NULL,
  44. _("unbounded structure "
  45. "missing _m2_high field"));
  46. if (value_type (arg1) != type)
  47. arg1 = value_cast (type, arg1);
  48. }
  49. }
  50. return arg1;
  51. }
  52. /* A helper function for BINOP_SUBSCRIPT. */
  53. struct value *
  54. eval_op_m2_subscript (struct type *expect_type, struct expression *exp,
  55. enum noside noside,
  56. struct value *arg1, struct value *arg2)
  57. {
  58. /* If the user attempts to subscript something that is not an
  59. array or pointer type (like a plain int variable for example),
  60. then report this as an error. */
  61. arg1 = coerce_ref (arg1);
  62. struct type *type = check_typedef (value_type (arg1));
  63. if (m2_is_unbounded_array (type))
  64. {
  65. struct value *temp = arg1;
  66. type = type->field (0).type ();
  67. if (type == NULL || (type->code () != TYPE_CODE_PTR))
  68. error (_("internal error: unbounded "
  69. "array structure is unknown"));
  70. /* i18n: Do not translate the "_m2_contents" part! */
  71. arg1 = value_struct_elt (&temp, {}, "_m2_contents", NULL,
  72. _("unbounded structure "
  73. "missing _m2_contents field"));
  74. if (value_type (arg1) != type)
  75. arg1 = value_cast (type, arg1);
  76. check_typedef (value_type (arg1));
  77. return value_ind (value_ptradd (arg1, value_as_long (arg2)));
  78. }
  79. else
  80. if (type->code () != TYPE_CODE_ARRAY)
  81. {
  82. if (type->name ())
  83. error (_("cannot subscript something of type `%s'"),
  84. type->name ());
  85. else
  86. error (_("cannot subscript requested type"));
  87. }
  88. if (noside == EVAL_AVOID_SIDE_EFFECTS)
  89. return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
  90. else
  91. return value_subscript (arg1, value_as_long (arg2));
  92. }
  93. /* Single instance of the M2 language. */
  94. static m2_language m2_language_defn;
  95. /* See language.h. */
  96. void
  97. m2_language::language_arch_info (struct gdbarch *gdbarch,
  98. struct language_arch_info *lai) const
  99. {
  100. const struct builtin_m2_type *builtin = builtin_m2_type (gdbarch);
  101. /* Helper function to allow shorter lines below. */
  102. auto add = [&] (struct type * t)
  103. {
  104. lai->add_primitive_type (t);
  105. };
  106. add (builtin->builtin_char);
  107. add (builtin->builtin_int);
  108. add (builtin->builtin_card);
  109. add (builtin->builtin_real);
  110. add (builtin->builtin_bool);
  111. lai->set_string_char_type (builtin->builtin_char);
  112. lai->set_bool_type (builtin->builtin_bool, "BOOLEAN");
  113. }
  114. /* See languge.h. */
  115. void
  116. m2_language::printchar (int c, struct type *type,
  117. struct ui_file *stream) const
  118. {
  119. gdb_puts ("'", stream);
  120. emitchar (c, type, stream, '\'');
  121. gdb_puts ("'", stream);
  122. }
  123. /* See language.h. */
  124. void
  125. m2_language::printstr (struct ui_file *stream, struct type *elttype,
  126. const gdb_byte *string, unsigned int length,
  127. const char *encoding, int force_ellipses,
  128. const struct value_print_options *options) const
  129. {
  130. unsigned int i;
  131. unsigned int things_printed = 0;
  132. int in_quotes = 0;
  133. int need_comma = 0;
  134. if (length == 0)
  135. {
  136. gdb_puts ("\"\"");
  137. return;
  138. }
  139. for (i = 0; i < length && things_printed < options->print_max; ++i)
  140. {
  141. /* Position of the character we are examining
  142. to see whether it is repeated. */
  143. unsigned int rep1;
  144. /* Number of repetitions we have detected so far. */
  145. unsigned int reps;
  146. QUIT;
  147. if (need_comma)
  148. {
  149. gdb_puts (", ", stream);
  150. need_comma = 0;
  151. }
  152. rep1 = i + 1;
  153. reps = 1;
  154. while (rep1 < length && string[rep1] == string[i])
  155. {
  156. ++rep1;
  157. ++reps;
  158. }
  159. if (reps > options->repeat_count_threshold)
  160. {
  161. if (in_quotes)
  162. {
  163. gdb_puts ("\", ", stream);
  164. in_quotes = 0;
  165. }
  166. printchar (string[i], elttype, stream);
  167. gdb_printf (stream, " <repeats %u times>", reps);
  168. i = rep1 - 1;
  169. things_printed += options->repeat_count_threshold;
  170. need_comma = 1;
  171. }
  172. else
  173. {
  174. if (!in_quotes)
  175. {
  176. gdb_puts ("\"", stream);
  177. in_quotes = 1;
  178. }
  179. emitchar (string[i], elttype, stream, '"');
  180. ++things_printed;
  181. }
  182. }
  183. /* Terminate the quotes if necessary. */
  184. if (in_quotes)
  185. gdb_puts ("\"", stream);
  186. if (force_ellipses || i < length)
  187. gdb_puts ("...", stream);
  188. }
  189. /* See language.h. */
  190. void
  191. m2_language::emitchar (int ch, struct type *chtype,
  192. struct ui_file *stream, int quoter) const
  193. {
  194. ch &= 0xFF; /* Avoid sign bit follies. */
  195. if (PRINT_LITERAL_FORM (ch))
  196. {
  197. if (ch == '\\' || ch == quoter)
  198. gdb_puts ("\\", stream);
  199. gdb_printf (stream, "%c", ch);
  200. }
  201. else
  202. {
  203. switch (ch)
  204. {
  205. case '\n':
  206. gdb_puts ("\\n", stream);
  207. break;
  208. case '\b':
  209. gdb_puts ("\\b", stream);
  210. break;
  211. case '\t':
  212. gdb_puts ("\\t", stream);
  213. break;
  214. case '\f':
  215. gdb_puts ("\\f", stream);
  216. break;
  217. case '\r':
  218. gdb_puts ("\\r", stream);
  219. break;
  220. case '\033':
  221. gdb_puts ("\\e", stream);
  222. break;
  223. case '\007':
  224. gdb_puts ("\\a", stream);
  225. break;
  226. default:
  227. gdb_printf (stream, "\\%.3o", (unsigned int) ch);
  228. break;
  229. }
  230. }
  231. }
  232. /* Called during architecture gdbarch initialisation to create language
  233. specific types. */
  234. static void *
  235. build_m2_types (struct gdbarch *gdbarch)
  236. {
  237. struct builtin_m2_type *builtin_m2_type
  238. = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_m2_type);
  239. /* Modula-2 "pervasive" types. NOTE: these can be redefined!!! */
  240. builtin_m2_type->builtin_int
  241. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, "INTEGER");
  242. builtin_m2_type->builtin_card
  243. = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "CARDINAL");
  244. builtin_m2_type->builtin_real
  245. = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), "REAL",
  246. gdbarch_float_format (gdbarch));
  247. builtin_m2_type->builtin_char
  248. = arch_character_type (gdbarch, TARGET_CHAR_BIT, 1, "CHAR");
  249. builtin_m2_type->builtin_bool
  250. = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, "BOOLEAN");
  251. return builtin_m2_type;
  252. }
  253. static struct gdbarch_data *m2_type_data;
  254. const struct builtin_m2_type *
  255. builtin_m2_type (struct gdbarch *gdbarch)
  256. {
  257. return (const struct builtin_m2_type *) gdbarch_data (gdbarch, m2_type_data);
  258. }
  259. /* Initialization for Modula-2 */
  260. void _initialize_m2_language ();
  261. void
  262. _initialize_m2_language ()
  263. {
  264. m2_type_data = gdbarch_data_register_post_init (build_m2_types);
  265. }