]>
Commit | Line | Data |
---|---|---|
373a8247 | 1 | /* Pascal language support routines for GDB, the GNU debugger. |
ce27fb25 | 2 | |
d01e8234 | 3 | Copyright (C) 2000-2025 Free Software Foundation, Inc. |
373a8247 PM |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
373a8247 PM |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
373a8247 | 19 | |
5598ce11 | 20 | /* This file is derived from c-lang.c */ |
373a8247 | 21 | |
e5dc0d5d | 22 | #include "event-top.h" |
ec452525 | 23 | #include "extract-store-integer.h" |
373a8247 PM |
24 | #include "symtab.h" |
25 | #include "gdbtypes.h" | |
26 | #include "expression.h" | |
27 | #include "parser-defs.h" | |
28 | #include "language.h" | |
a53b64ea | 29 | #include "varobj.h" |
373a8247 PM |
30 | #include "p-lang.h" |
31 | #include "valprint.h" | |
5f9a71c3 | 32 | #include "value.h" |
5598ce11 | 33 | #include <ctype.h> |
43cc5389 | 34 | #include "c-lang.h" |
0d12e84c | 35 | #include "gdbarch.h" |
7f6aba03 | 36 | #include "cli/cli-style.h" |
6ced1581 | 37 | |
cd6c7346 | 38 | /* All GPC versions until now (2007-09-27) also define a symbol called |
0df8b418 | 39 | '_p_initialize'. Check for the presence of this symbol first. */ |
cd6c7346 PM |
40 | static const char GPC_P_INITIALIZE[] = "_p_initialize"; |
41 | ||
42 | /* The name of the symbol that GPC uses as the name of the main | |
43 | procedure (since version 20050212). */ | |
44 | static const char GPC_MAIN_PROGRAM_NAME_1[] = "_p__M0_main_program"; | |
45 | ||
46 | /* Older versions of GPC (versions older than 20050212) were using | |
47 | a different name for the main procedure. */ | |
48 | static const char GPC_MAIN_PROGRAM_NAME_2[] = "pascal_main_program"; | |
49 | ||
50 | /* Function returning the special symbol name used | |
51 | by GPC for the main procedure in the main program | |
52 | if it is found in minimal symbol list. | |
53 | This function tries to find minimal symbols generated by GPC | |
54 | so that it finds the even if the program was compiled | |
55 | without debugging information. | |
56 | According to information supplied by Waldeck Hebisch, | |
0df8b418 | 57 | this should work for all versions posterior to June 2000. */ |
cd6c7346 PM |
58 | |
59 | const char * | |
60 | pascal_main_name (void) | |
61 | { | |
4144d36a SM |
62 | bound_minimal_symbol msym |
63 | = lookup_minimal_symbol (current_program_space, GPC_P_INITIALIZE); | |
cd6c7346 PM |
64 | |
65 | /* If '_p_initialize' was not found, the main program is likely not | |
66 | written in Pascal. */ | |
3b7344d5 | 67 | if (msym.minsym == NULL) |
cd6c7346 PM |
68 | return NULL; |
69 | ||
4144d36a SM |
70 | msym |
71 | = lookup_minimal_symbol (current_program_space, GPC_MAIN_PROGRAM_NAME_1); | |
3b7344d5 | 72 | if (msym.minsym != NULL) |
cd6c7346 PM |
73 | { |
74 | return GPC_MAIN_PROGRAM_NAME_1; | |
75 | } | |
76 | ||
4144d36a SM |
77 | msym |
78 | = lookup_minimal_symbol (current_program_space, GPC_MAIN_PROGRAM_NAME_2); | |
3b7344d5 | 79 | if (msym.minsym != NULL) |
cd6c7346 PM |
80 | { |
81 | return GPC_MAIN_PROGRAM_NAME_2; | |
82 | } | |
83 | ||
84 | /* No known entry procedure found, the main program is probably | |
85 | not compiled with GPC. */ | |
86 | return NULL; | |
87 | } | |
88 | ||
46157d77 AB |
89 | /* See p-lang.h. */ |
90 | ||
5598ce11 | 91 | int |
46157d77 AB |
92 | pascal_is_string_type (struct type *type,int *length_pos, int *length_size, |
93 | int *string_pos, struct type **char_type, | |
0d5cff50 | 94 | const char **arrayname) |
5598ce11 | 95 | { |
78134374 | 96 | if (type != NULL && type->code () == TYPE_CODE_STRUCT) |
5598ce11 PM |
97 | { |
98 | /* Old Borland type pascal strings from Free Pascal Compiler. */ | |
99 | /* Two fields: length and st. */ | |
1f704f76 | 100 | if (type->num_fields () == 2 |
33d16dd9 SM |
101 | && type->field (0).name () |
102 | && strcmp (type->field (0).name (), "length") == 0 | |
103 | && type->field (1).name () | |
104 | && strcmp (type->field (1).name (), "st") == 0) | |
dda83cd7 SM |
105 | { |
106 | if (length_pos) | |
b610c045 | 107 | *length_pos = type->field (0).loc_bitpos () / TARGET_CHAR_BIT; |
dda83cd7 | 108 | if (length_size) |
df86565b | 109 | *length_size = type->field (0).type ()->length (); |
dda83cd7 | 110 | if (string_pos) |
b610c045 | 111 | *string_pos = type->field (1).loc_bitpos () / TARGET_CHAR_BIT; |
dda83cd7 | 112 | if (char_type) |
27710edb | 113 | *char_type = type->field (1).type ()->target_type (); |
24b21115 | 114 | if (arrayname) |
33d16dd9 | 115 | *arrayname = type->field (1).name (); |
dda83cd7 SM |
116 | return 2; |
117 | }; | |
5598ce11 PM |
118 | /* GNU pascal strings. */ |
119 | /* Three fields: Capacity, length and schema$ or _p_schema. */ | |
1f704f76 | 120 | if (type->num_fields () == 3 |
33d16dd9 SM |
121 | && type->field (0).name () |
122 | && strcmp (type->field (0).name (), "Capacity") == 0 | |
123 | && type->field (1).name () | |
124 | && strcmp (type->field (1).name (), "length") == 0) | |
dda83cd7 | 125 | { |
58159490 | 126 | if (length_pos) |
b610c045 | 127 | *length_pos = type->field (1).loc_bitpos () / TARGET_CHAR_BIT; |
58159490 | 128 | if (length_size) |
df86565b | 129 | *length_size = type->field (1).type ()->length (); |
58159490 | 130 | if (string_pos) |
b610c045 | 131 | *string_pos = type->field (2).loc_bitpos () / TARGET_CHAR_BIT; |
dda83cd7 SM |
132 | /* FIXME: how can I detect wide chars in GPC ?? */ |
133 | if (char_type) | |
58159490 | 134 | { |
27710edb | 135 | *char_type = type->field (2).type ()->target_type (); |
ad3bbd48 | 136 | |
78134374 | 137 | if ((*char_type)->code () == TYPE_CODE_ARRAY) |
27710edb | 138 | *char_type = (*char_type)->target_type (); |
58159490 | 139 | } |
24b21115 | 140 | if (arrayname) |
33d16dd9 | 141 | *arrayname = type->field (2).name (); |
dda83cd7 SM |
142 | return 3; |
143 | }; | |
5598ce11 PM |
144 | } |
145 | return 0; | |
146 | } | |
147 | ||
46157d77 | 148 | /* See p-lang.h. */ |
373a8247 | 149 | |
46157d77 AB |
150 | void |
151 | pascal_language::print_one_char (int c, struct ui_file *stream, | |
152 | int *in_quotes) const | |
373a8247 | 153 | { |
9b79b476 | 154 | if (c == '\'' || ((unsigned int) c <= 0xff && (PRINT_LITERAL_FORM (c)))) |
373a8247 PM |
155 | { |
156 | if (!(*in_quotes)) | |
0426ad51 | 157 | gdb_puts ("'", stream); |
373a8247 PM |
158 | *in_quotes = 1; |
159 | if (c == '\'') | |
160 | { | |
0426ad51 | 161 | gdb_puts ("''", stream); |
373a8247 PM |
162 | } |
163 | else | |
6cb06a8c | 164 | gdb_printf (stream, "%c", c); |
373a8247 PM |
165 | } |
166 | else | |
167 | { | |
168 | if (*in_quotes) | |
0426ad51 | 169 | gdb_puts ("'", stream); |
373a8247 | 170 | *in_quotes = 0; |
6cb06a8c | 171 | gdb_printf (stream, "#%d", (unsigned int) c); |
373a8247 PM |
172 | } |
173 | } | |
174 | ||
46157d77 AB |
175 | /* See language.h. */ |
176 | ||
373a8247 | 177 | void |
46157d77 AB |
178 | pascal_language::printchar (int c, struct type *type, |
179 | struct ui_file *stream) const | |
373a8247 PM |
180 | { |
181 | int in_quotes = 0; | |
ad3bbd48 | 182 | |
46157d77 | 183 | print_one_char (c, stream, &in_quotes); |
373a8247 | 184 | if (in_quotes) |
0426ad51 | 185 | gdb_puts ("'", stream); |
373a8247 PM |
186 | } |
187 | ||
373a8247 PM |
188 | \f |
189 | ||
46157d77 | 190 | /* See language.h. */ |
0874fd07 | 191 | |
46157d77 AB |
192 | void pascal_language::language_arch_info |
193 | (struct gdbarch *gdbarch, struct language_arch_info *lai) const | |
0874fd07 | 194 | { |
46157d77 | 195 | const struct builtin_type *builtin = builtin_type (gdbarch); |
52b50f2c | 196 | |
46157d77 AB |
197 | /* Helper function to allow shorter lines below. */ |
198 | auto add = [&] (struct type * t) | |
52b50f2c | 199 | { |
46157d77 AB |
200 | lai->add_primitive_type (t); |
201 | }; | |
202 | ||
203 | add (builtin->builtin_int); | |
204 | add (builtin->builtin_long); | |
205 | add (builtin->builtin_short); | |
206 | add (builtin->builtin_char); | |
207 | add (builtin->builtin_float); | |
208 | add (builtin->builtin_double); | |
209 | add (builtin->builtin_void); | |
210 | add (builtin->builtin_long_long); | |
211 | add (builtin->builtin_signed_char); | |
212 | add (builtin->builtin_unsigned_char); | |
213 | add (builtin->builtin_unsigned_short); | |
214 | add (builtin->builtin_unsigned_int); | |
215 | add (builtin->builtin_unsigned_long); | |
216 | add (builtin->builtin_unsigned_long_long); | |
217 | add (builtin->builtin_long_double); | |
218 | add (builtin->builtin_complex); | |
219 | add (builtin->builtin_double_complex); | |
220 | ||
221 | lai->set_string_char_type (builtin->builtin_char); | |
222 | lai->set_bool_type (builtin->builtin_bool, "boolean"); | |
223 | } | |
52b50f2c | 224 | |
46157d77 | 225 | /* See language.h. */ |
d711ee67 | 226 | |
46157d77 AB |
227 | void |
228 | pascal_language::printstr (struct ui_file *stream, struct type *elttype, | |
229 | const gdb_byte *string, unsigned int length, | |
230 | const char *encoding, int force_ellipses, | |
231 | const struct value_print_options *options) const | |
232 | { | |
233 | enum bfd_endian byte_order = type_byte_order (elttype); | |
234 | unsigned int i; | |
235 | unsigned int things_printed = 0; | |
236 | int in_quotes = 0; | |
237 | int need_comma = 0; | |
238 | int width; | |
239 | ||
240 | /* Preserve ELTTYPE's original type, just set its LENGTH. */ | |
241 | check_typedef (elttype); | |
df86565b | 242 | width = elttype->length (); |
46157d77 AB |
243 | |
244 | /* If the string was not truncated due to `set print elements', and | |
245 | the last byte of it is a null, we don't print that, in traditional C | |
246 | style. */ | |
247 | if ((!force_ellipses) && length > 0 | |
248 | && extract_unsigned_integer (string + (length - 1) * width, width, | |
249 | byte_order) == 0) | |
250 | length--; | |
251 | ||
252 | if (length == 0) | |
253 | { | |
0426ad51 | 254 | gdb_puts ("''", stream); |
46157d77 AB |
255 | return; |
256 | } | |
4ffc13fb | 257 | |
76b58849 AB |
258 | unsigned int print_max_chars = get_print_max_chars (options); |
259 | for (i = 0; i < length && things_printed < print_max_chars; ++i) | |
46157d77 AB |
260 | { |
261 | /* Position of the character we are examining | |
262 | to see whether it is repeated. */ | |
263 | unsigned int rep1; | |
264 | /* Number of repetitions we have detected so far. */ | |
265 | unsigned int reps; | |
266 | unsigned long int current_char; | |
39e7ecca | 267 | |
46157d77 | 268 | QUIT; |
5bae7c4e | 269 | |
46157d77 AB |
270 | if (need_comma) |
271 | { | |
0426ad51 | 272 | gdb_puts (", ", stream); |
46157d77 AB |
273 | need_comma = 0; |
274 | } | |
5bae7c4e | 275 | |
46157d77 AB |
276 | current_char = extract_unsigned_integer (string + i * width, width, |
277 | byte_order); | |
efdf6a73 | 278 | |
46157d77 AB |
279 | rep1 = i + 1; |
280 | reps = 1; | |
281 | while (rep1 < length | |
282 | && extract_unsigned_integer (string + rep1 * width, width, | |
283 | byte_order) == current_char) | |
284 | { | |
285 | ++rep1; | |
286 | ++reps; | |
287 | } | |
efdf6a73 | 288 | |
46157d77 AB |
289 | if (reps > options->repeat_count_threshold) |
290 | { | |
291 | if (in_quotes) | |
292 | { | |
0426ad51 | 293 | gdb_puts ("', ", stream); |
46157d77 AB |
294 | in_quotes = 0; |
295 | } | |
296 | printchar (current_char, elttype, stream); | |
6cb06a8c TT |
297 | gdb_printf (stream, " %p[<repeats %u times>%p]", |
298 | metadata_style.style ().ptr (), | |
299 | reps, nullptr); | |
46157d77 AB |
300 | i = rep1 - 1; |
301 | things_printed += options->repeat_count_threshold; | |
302 | need_comma = 1; | |
303 | } | |
304 | else | |
305 | { | |
306 | if ((!in_quotes) && (PRINT_LITERAL_FORM (current_char))) | |
307 | { | |
0426ad51 | 308 | gdb_puts ("'", stream); |
46157d77 AB |
309 | in_quotes = 1; |
310 | } | |
311 | print_one_char (current_char, stream, &in_quotes); | |
312 | ++things_printed; | |
313 | } | |
314 | } | |
b7c6e27d | 315 | |
46157d77 AB |
316 | /* Terminate the quotes if necessary. */ |
317 | if (in_quotes) | |
0426ad51 | 318 | gdb_puts ("'", stream); |
b7c6e27d | 319 | |
46157d77 | 320 | if (force_ellipses || i < length) |
0426ad51 | 321 | gdb_puts ("...", stream); |
46157d77 | 322 | } |
0874fd07 AB |
323 | |
324 | /* Single instance of the Pascal language class. */ | |
325 | ||
326 | static pascal_language pascal_language_defn; |