Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Fortran language support routines for GDB, the GNU debugger. |
ce27fb25 | 2 | |
b811d2c2 | 3 | Copyright (C) 1993-2020 Free Software Foundation, Inc. |
ce27fb25 | 4 | |
c906108c SS |
5 | Contributed by Motorola. Adapted from the C parser by Farooq Butt |
6 | (fmbutt@engage.sps.mot.com). | |
7 | ||
c5aa993b | 8 | This file is part of GDB. |
c906108c | 9 | |
c5aa993b JM |
10 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 12 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 13 | (at your option) any later version. |
c906108c | 14 | |
c5aa993b JM |
15 | This program is distributed in the hope that it will be useful, |
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | GNU General Public License for more details. | |
c906108c | 19 | |
c5aa993b | 20 | You should have received a copy of the GNU General Public License |
a9762ec7 | 21 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
22 | |
23 | #include "defs.h" | |
4de283e4 | 24 | #include "symtab.h" |
d55e5aa6 | 25 | #include "gdbtypes.h" |
4de283e4 | 26 | #include "expression.h" |
d55e5aa6 | 27 | #include "parser-defs.h" |
4de283e4 TT |
28 | #include "language.h" |
29 | #include "varobj.h" | |
30 | #include "gdbcore.h" | |
31 | #include "f-lang.h" | |
745b8ca0 | 32 | #include "valprint.h" |
5f9a71c3 | 33 | #include "value.h" |
4de283e4 TT |
34 | #include "cp-support.h" |
35 | #include "charset.h" | |
36 | #include "c-lang.h" | |
37 | #include "target-float.h" | |
0d12e84c | 38 | #include "gdbarch.h" |
a5c641b5 AB |
39 | #include "gdbcmd.h" |
40 | #include "f-array-walker.h" | |
4de283e4 TT |
41 | |
42 | #include <math.h> | |
c906108c | 43 | |
a5c641b5 AB |
44 | /* Whether GDB should repack array slices created by the user. */ |
45 | static bool repack_array_slices = false; | |
46 | ||
47 | /* Implement 'show fortran repack-array-slices'. */ | |
48 | static void | |
49 | show_repack_array_slices (struct ui_file *file, int from_tty, | |
50 | struct cmd_list_element *c, const char *value) | |
51 | { | |
52 | fprintf_filtered (file, _("Repacking of Fortran array slices is %s.\n"), | |
53 | value); | |
54 | } | |
55 | ||
56 | /* Debugging of Fortran's array slicing. */ | |
57 | static bool fortran_array_slicing_debug = false; | |
58 | ||
59 | /* Implement 'show debug fortran-array-slicing'. */ | |
60 | static void | |
61 | show_fortran_array_slicing_debug (struct ui_file *file, int from_tty, | |
62 | struct cmd_list_element *c, | |
63 | const char *value) | |
64 | { | |
65 | fprintf_filtered (file, _("Debugging of Fortran array slicing is %s.\n"), | |
66 | value); | |
67 | } | |
68 | ||
c906108c SS |
69 | /* Local functions */ |
70 | ||
5a7cf527 AB |
71 | static struct value *fortran_argument_convert (struct value *value, |
72 | bool is_artificial); | |
73 | ||
3b2b8fea TT |
74 | /* Return the encoding that should be used for the character type |
75 | TYPE. */ | |
76 | ||
1a0ea399 AB |
77 | const char * |
78 | f_language::get_encoding (struct type *type) | |
3b2b8fea TT |
79 | { |
80 | const char *encoding; | |
81 | ||
82 | switch (TYPE_LENGTH (type)) | |
83 | { | |
84 | case 1: | |
85 | encoding = target_charset (get_type_arch (type)); | |
86 | break; | |
87 | case 4: | |
34877895 | 88 | if (type_byte_order (type) == BFD_ENDIAN_BIG) |
3b2b8fea TT |
89 | encoding = "UTF-32BE"; |
90 | else | |
91 | encoding = "UTF-32LE"; | |
92 | break; | |
93 | ||
94 | default: | |
95 | error (_("unrecognized character type")); | |
96 | } | |
97 | ||
98 | return encoding; | |
99 | } | |
100 | ||
c906108c | 101 | \f |
c5aa993b | 102 | |
c906108c SS |
103 | /* Table of operators and their precedences for printing expressions. */ |
104 | ||
1a0ea399 | 105 | const struct op_print f_language::op_print_tab[] = |
c5aa993b JM |
106 | { |
107 | {"+", BINOP_ADD, PREC_ADD, 0}, | |
108 | {"+", UNOP_PLUS, PREC_PREFIX, 0}, | |
109 | {"-", BINOP_SUB, PREC_ADD, 0}, | |
110 | {"-", UNOP_NEG, PREC_PREFIX, 0}, | |
111 | {"*", BINOP_MUL, PREC_MUL, 0}, | |
112 | {"/", BINOP_DIV, PREC_MUL, 0}, | |
113 | {"DIV", BINOP_INTDIV, PREC_MUL, 0}, | |
114 | {"MOD", BINOP_REM, PREC_MUL, 0}, | |
115 | {"=", BINOP_ASSIGN, PREC_ASSIGN, 1}, | |
116 | {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0}, | |
117 | {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0}, | |
118 | {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0}, | |
119 | {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0}, | |
120 | {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0}, | |
121 | {".LE.", BINOP_LEQ, PREC_ORDER, 0}, | |
122 | {".GE.", BINOP_GEQ, PREC_ORDER, 0}, | |
123 | {".GT.", BINOP_GTR, PREC_ORDER, 0}, | |
124 | {".LT.", BINOP_LESS, PREC_ORDER, 0}, | |
125 | {"**", UNOP_IND, PREC_PREFIX, 0}, | |
126 | {"@", BINOP_REPEAT, PREC_REPEAT, 0}, | |
f486487f | 127 | {NULL, OP_NULL, PREC_REPEAT, 0} |
c906108c SS |
128 | }; |
129 | \f | |
c906108c | 130 | |
6d816919 AB |
131 | /* Return the number of dimensions for a Fortran array or string. */ |
132 | ||
133 | int | |
134 | calc_f77_array_dims (struct type *array_type) | |
135 | { | |
136 | int ndimen = 1; | |
137 | struct type *tmp_type; | |
138 | ||
139 | if ((array_type->code () == TYPE_CODE_STRING)) | |
140 | return 1; | |
141 | ||
142 | if ((array_type->code () != TYPE_CODE_ARRAY)) | |
143 | error (_("Can't get dimensions for a non-array type")); | |
144 | ||
145 | tmp_type = array_type; | |
146 | ||
147 | while ((tmp_type = TYPE_TARGET_TYPE (tmp_type))) | |
148 | { | |
149 | if (tmp_type->code () == TYPE_CODE_ARRAY) | |
150 | ++ndimen; | |
151 | } | |
152 | return ndimen; | |
153 | } | |
154 | ||
a5c641b5 AB |
155 | /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array |
156 | slices. This is a base class for two alternative repacking mechanisms, | |
157 | one for when repacking from a lazy value, and one for repacking from a | |
158 | non-lazy (already loaded) value. */ | |
159 | class fortran_array_repacker_base_impl | |
160 | : public fortran_array_walker_base_impl | |
161 | { | |
162 | public: | |
163 | /* Constructor, DEST is the value we are repacking into. */ | |
164 | fortran_array_repacker_base_impl (struct value *dest) | |
165 | : m_dest (dest), | |
166 | m_dest_offset (0) | |
167 | { /* Nothing. */ } | |
168 | ||
169 | /* When we start processing the inner most dimension, this is where we | |
170 | will be creating values for each element as we load them and then copy | |
171 | them into the M_DEST value. Set a value mark so we can free these | |
172 | temporary values. */ | |
173 | void start_dimension (bool inner_p) | |
174 | { | |
175 | if (inner_p) | |
176 | { | |
177 | gdb_assert (m_mark == nullptr); | |
178 | m_mark = value_mark (); | |
179 | } | |
180 | } | |
181 | ||
182 | /* When we finish processing the inner most dimension free all temporary | |
183 | value that were created. */ | |
184 | void finish_dimension (bool inner_p, bool last_p) | |
185 | { | |
186 | if (inner_p) | |
187 | { | |
188 | gdb_assert (m_mark != nullptr); | |
189 | value_free_to_mark (m_mark); | |
190 | m_mark = nullptr; | |
191 | } | |
192 | } | |
193 | ||
194 | protected: | |
195 | /* Copy the contents of array element ELT into M_DEST at the next | |
196 | available offset. */ | |
197 | void copy_element_to_dest (struct value *elt) | |
198 | { | |
199 | value_contents_copy (m_dest, m_dest_offset, elt, 0, | |
200 | TYPE_LENGTH (value_type (elt))); | |
201 | m_dest_offset += TYPE_LENGTH (value_type (elt)); | |
202 | } | |
203 | ||
204 | /* The value being written to. */ | |
205 | struct value *m_dest; | |
206 | ||
207 | /* The byte offset in M_DEST at which the next element should be | |
208 | written. */ | |
209 | LONGEST m_dest_offset; | |
210 | ||
211 | /* Set with a call to VALUE_MARK, and then reset after calling | |
212 | VALUE_FREE_TO_MARK. */ | |
213 | struct value *m_mark = nullptr; | |
214 | }; | |
215 | ||
216 | /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array | |
217 | slices. This class is specialised for repacking an array slice from a | |
218 | lazy array value, as such it does not require the parent array value to | |
219 | be loaded into GDB's memory; the parent value could be huge, while the | |
220 | slice could be tiny. */ | |
221 | class fortran_lazy_array_repacker_impl | |
222 | : public fortran_array_repacker_base_impl | |
223 | { | |
224 | public: | |
225 | /* Constructor. TYPE is the type of the slice being loaded from the | |
226 | parent value, so this type will correctly reflect the strides required | |
227 | to find all of the elements from the parent value. ADDRESS is the | |
228 | address in target memory of value matching TYPE, and DEST is the value | |
229 | we are repacking into. */ | |
230 | explicit fortran_lazy_array_repacker_impl (struct type *type, | |
231 | CORE_ADDR address, | |
232 | struct value *dest) | |
233 | : fortran_array_repacker_base_impl (dest), | |
234 | m_addr (address) | |
235 | { /* Nothing. */ } | |
236 | ||
237 | /* Create a lazy value in target memory representing a single element, | |
238 | then load the element into GDB's memory and copy the contents into the | |
239 | destination value. */ | |
240 | void process_element (struct type *elt_type, LONGEST elt_off, bool last_p) | |
241 | { | |
242 | copy_element_to_dest (value_at_lazy (elt_type, m_addr + elt_off)); | |
243 | } | |
244 | ||
245 | private: | |
246 | /* The address in target memory where the parent value starts. */ | |
247 | CORE_ADDR m_addr; | |
248 | }; | |
249 | ||
250 | /* A class used by FORTRAN_VALUE_SUBARRAY when repacking Fortran array | |
251 | slices. This class is specialised for repacking an array slice from a | |
252 | previously loaded (non-lazy) array value, as such it fetches the | |
253 | element values from the contents of the parent value. */ | |
254 | class fortran_array_repacker_impl | |
255 | : public fortran_array_repacker_base_impl | |
256 | { | |
257 | public: | |
258 | /* Constructor. TYPE is the type for the array slice within the parent | |
259 | value, as such it has stride values as required to find the elements | |
260 | within the original parent value. ADDRESS is the address in target | |
261 | memory of the value matching TYPE. BASE_OFFSET is the offset from | |
262 | the start of VAL's content buffer to the start of the object of TYPE, | |
263 | VAL is the parent object from which we are loading the value, and | |
264 | DEST is the value into which we are repacking. */ | |
265 | explicit fortran_array_repacker_impl (struct type *type, CORE_ADDR address, | |
266 | LONGEST base_offset, | |
267 | struct value *val, struct value *dest) | |
268 | : fortran_array_repacker_base_impl (dest), | |
269 | m_base_offset (base_offset), | |
270 | m_val (val) | |
271 | { | |
272 | gdb_assert (!value_lazy (val)); | |
273 | } | |
274 | ||
275 | /* Extract an element of ELT_TYPE at offset (M_BASE_OFFSET + ELT_OFF) | |
276 | from the content buffer of M_VAL then copy this extracted value into | |
277 | the repacked destination value. */ | |
278 | void process_element (struct type *elt_type, LONGEST elt_off, bool last_p) | |
279 | { | |
280 | struct value *elt | |
281 | = value_from_component (m_val, elt_type, (elt_off + m_base_offset)); | |
282 | copy_element_to_dest (elt); | |
283 | } | |
284 | ||
285 | private: | |
286 | /* The offset into the content buffer of M_VAL to the start of the slice | |
287 | being extracted. */ | |
288 | LONGEST m_base_offset; | |
289 | ||
290 | /* The parent value from which we are extracting a slice. */ | |
291 | struct value *m_val; | |
292 | }; | |
293 | ||
6d816919 AB |
294 | /* Called from evaluate_subexp_standard to perform array indexing, and |
295 | sub-range extraction, for Fortran. As well as arrays this function | |
296 | also handles strings as they can be treated like arrays of characters. | |
297 | ARRAY is the array or string being accessed. EXP, POS, and NOSIDE are | |
298 | as for evaluate_subexp_standard, and NARGS is the number of arguments | |
299 | in this access (e.g. 'array (1,2,3)' would be NARGS 3). */ | |
300 | ||
301 | static struct value * | |
302 | fortran_value_subarray (struct value *array, struct expression *exp, | |
303 | int *pos, int nargs, enum noside noside) | |
304 | { | |
a5c641b5 AB |
305 | type *original_array_type = check_typedef (value_type (array)); |
306 | bool is_string_p = original_array_type->code () == TYPE_CODE_STRING; | |
307 | ||
308 | /* Perform checks for ARRAY not being available. The somewhat overly | |
309 | complex logic here is just to keep backward compatibility with the | |
310 | errors that we used to get before FORTRAN_VALUE_SUBARRAY was | |
311 | rewritten. Maybe a future task would streamline the error messages we | |
312 | get here, and update all the expected test results. */ | |
313 | if (exp->elts[*pos].opcode != OP_RANGE) | |
314 | { | |
315 | if (type_not_associated (original_array_type)) | |
316 | error (_("no such vector element (vector not associated)")); | |
317 | else if (type_not_allocated (original_array_type)) | |
318 | error (_("no such vector element (vector not allocated)")); | |
319 | } | |
320 | else | |
6d816919 | 321 | { |
a5c641b5 AB |
322 | if (type_not_associated (original_array_type)) |
323 | error (_("array not associated")); | |
324 | else if (type_not_allocated (original_array_type)) | |
325 | error (_("array not allocated")); | |
6d816919 AB |
326 | } |
327 | ||
a5c641b5 AB |
328 | /* First check that the number of dimensions in the type we are slicing |
329 | matches the number of arguments we were passed. */ | |
330 | int ndimensions = calc_f77_array_dims (original_array_type); | |
331 | if (nargs != ndimensions) | |
332 | error (_("Wrong number of subscripts")); | |
333 | ||
334 | /* This will be initialised below with the type of the elements held in | |
335 | ARRAY. */ | |
336 | struct type *inner_element_type; | |
337 | ||
338 | /* Extract the types of each array dimension from the original array | |
339 | type. We need these available so we can fill in the default upper and | |
340 | lower bounds if the user requested slice doesn't provide that | |
341 | information. Additionally unpacking the dimensions like this gives us | |
342 | the inner element type. */ | |
343 | std::vector<struct type *> dim_types; | |
344 | { | |
345 | dim_types.reserve (ndimensions); | |
346 | struct type *type = original_array_type; | |
347 | for (int i = 0; i < ndimensions; ++i) | |
348 | { | |
349 | dim_types.push_back (type); | |
350 | type = TYPE_TARGET_TYPE (type); | |
351 | } | |
352 | /* TYPE is now the inner element type of the array, we start the new | |
353 | array slice off as this type, then as we process the requested slice | |
354 | (from the user) we wrap new types around this to build up the final | |
355 | slice type. */ | |
356 | inner_element_type = type; | |
357 | } | |
358 | ||
359 | /* As we analyse the new slice type we need to understand if the data | |
360 | being referenced is contiguous. Do decide this we must track the size | |
361 | of an element at each dimension of the new slice array. Initially the | |
362 | elements of the inner most dimension of the array are the same inner | |
363 | most elements as the original ARRAY. */ | |
364 | LONGEST slice_element_size = TYPE_LENGTH (inner_element_type); | |
365 | ||
366 | /* Start off assuming all data is contiguous, this will be set to false | |
367 | if access to any dimension results in non-contiguous data. */ | |
368 | bool is_all_contiguous = true; | |
369 | ||
370 | /* The TOTAL_OFFSET is the distance in bytes from the start of the | |
371 | original ARRAY to the start of the new slice. This is calculated as | |
372 | we process the information from the user. */ | |
373 | LONGEST total_offset = 0; | |
374 | ||
375 | /* A structure representing information about each dimension of the | |
376 | resulting slice. */ | |
377 | struct slice_dim | |
378 | { | |
379 | /* Constructor. */ | |
380 | slice_dim (LONGEST l, LONGEST h, LONGEST s, struct type *idx) | |
381 | : low (l), | |
382 | high (h), | |
383 | stride (s), | |
384 | index (idx) | |
385 | { /* Nothing. */ } | |
386 | ||
387 | /* The low bound for this dimension of the slice. */ | |
388 | LONGEST low; | |
6d816919 | 389 | |
a5c641b5 AB |
390 | /* The high bound for this dimension of the slice. */ |
391 | LONGEST high; | |
6d816919 | 392 | |
a5c641b5 AB |
393 | /* The byte stride for this dimension of the slice. */ |
394 | LONGEST stride; | |
6d816919 | 395 | |
a5c641b5 AB |
396 | struct type *index; |
397 | }; | |
398 | ||
399 | /* The dimensions of the resulting slice. */ | |
400 | std::vector<slice_dim> slice_dims; | |
401 | ||
402 | /* Process the incoming arguments. These arguments are in the reverse | |
403 | order to the array dimensions, that is the first argument refers to | |
404 | the last array dimension. */ | |
405 | if (fortran_array_slicing_debug) | |
406 | debug_printf ("Processing array access:\n"); | |
407 | for (int i = 0; i < nargs; ++i) | |
408 | { | |
409 | /* For each dimension of the array the user will have either provided | |
410 | a ranged access with optional lower bound, upper bound, and | |
411 | stride, or the user will have supplied a single index. */ | |
412 | struct type *dim_type = dim_types[ndimensions - (i + 1)]; | |
413 | if (exp->elts[*pos].opcode == OP_RANGE) | |
414 | { | |
415 | int pc = (*pos) + 1; | |
416 | enum range_flag range_flag = (enum range_flag) exp->elts[pc].longconst; | |
417 | *pos += 3; | |
418 | ||
419 | LONGEST low, high, stride; | |
420 | low = high = stride = 0; | |
421 | ||
422 | if ((range_flag & RANGE_LOW_BOUND_DEFAULT) == 0) | |
423 | low = value_as_long (evaluate_subexp (nullptr, exp, pos, noside)); | |
424 | else | |
425 | low = f77_get_lowerbound (dim_type); | |
426 | if ((range_flag & RANGE_HIGH_BOUND_DEFAULT) == 0) | |
427 | high = value_as_long (evaluate_subexp (nullptr, exp, pos, noside)); | |
428 | else | |
429 | high = f77_get_upperbound (dim_type); | |
430 | if ((range_flag & RANGE_HAS_STRIDE) == RANGE_HAS_STRIDE) | |
431 | stride = value_as_long (evaluate_subexp (nullptr, exp, pos, noside)); | |
432 | else | |
433 | stride = 1; | |
434 | ||
435 | if (stride == 0) | |
436 | error (_("stride must not be 0")); | |
437 | ||
438 | /* Get information about this dimension in the original ARRAY. */ | |
439 | struct type *target_type = TYPE_TARGET_TYPE (dim_type); | |
440 | struct type *index_type = dim_type->index_type (); | |
441 | LONGEST lb = f77_get_lowerbound (dim_type); | |
442 | LONGEST ub = f77_get_upperbound (dim_type); | |
443 | LONGEST sd = index_type->bit_stride (); | |
444 | if (sd == 0) | |
445 | sd = TYPE_LENGTH (target_type) * 8; | |
446 | ||
447 | if (fortran_array_slicing_debug) | |
448 | { | |
449 | debug_printf ("|-> Range access\n"); | |
450 | std::string str = type_to_string (dim_type); | |
451 | debug_printf ("| |-> Type: %s\n", str.c_str ()); | |
452 | debug_printf ("| |-> Array:\n"); | |
a5adb8f3 SM |
453 | debug_printf ("| | |-> Low bound: %s\n", plongest (lb)); |
454 | debug_printf ("| | |-> High bound: %s\n", plongest (ub)); | |
455 | debug_printf ("| | |-> Bit stride: %s\n", plongest (sd)); | |
456 | debug_printf ("| | |-> Byte stride: %s\n", plongest (sd / 8)); | |
457 | debug_printf ("| | |-> Type size: %s\n", | |
458 | pulongest (TYPE_LENGTH (dim_type))); | |
459 | debug_printf ("| | '-> Target type size: %s\n", | |
460 | pulongest (TYPE_LENGTH (target_type))); | |
a5c641b5 | 461 | debug_printf ("| |-> Accessing:\n"); |
a5adb8f3 SM |
462 | debug_printf ("| | |-> Low bound: %s\n", |
463 | plongest (low)); | |
464 | debug_printf ("| | |-> High bound: %s\n", | |
465 | plongest (high)); | |
466 | debug_printf ("| | '-> Element stride: %s\n", | |
467 | plongest (stride)); | |
a5c641b5 AB |
468 | } |
469 | ||
470 | /* Check the user hasn't asked for something invalid. */ | |
471 | if (high > ub || low < lb) | |
472 | error (_("array subscript out of bounds")); | |
473 | ||
474 | /* Calculate what this dimension of the new slice array will look | |
475 | like. OFFSET is the byte offset from the start of the | |
476 | previous (more outer) dimension to the start of this | |
477 | dimension. E_COUNT is the number of elements in this | |
478 | dimension. REMAINDER is the number of elements remaining | |
479 | between the last included element and the upper bound. For | |
480 | example an access '1:6:2' will include elements 1, 3, 5 and | |
481 | have a remainder of 1 (element #6). */ | |
482 | LONGEST lowest = std::min (low, high); | |
483 | LONGEST offset = (sd / 8) * (lowest - lb); | |
484 | LONGEST e_count = std::abs (high - low) + 1; | |
485 | e_count = (e_count + (std::abs (stride) - 1)) / std::abs (stride); | |
486 | LONGEST new_low = 1; | |
487 | LONGEST new_high = new_low + e_count - 1; | |
488 | LONGEST new_stride = (sd * stride) / 8; | |
489 | LONGEST last_elem = low + ((e_count - 1) * stride); | |
490 | LONGEST remainder = high - last_elem; | |
491 | if (low > high) | |
492 | { | |
493 | offset += std::abs (remainder) * TYPE_LENGTH (target_type); | |
494 | if (stride > 0) | |
495 | error (_("incorrect stride and boundary combination")); | |
496 | } | |
497 | else if (stride < 0) | |
498 | error (_("incorrect stride and boundary combination")); | |
499 | ||
500 | /* Is the data within this dimension contiguous? It is if the | |
501 | newly computed stride is the same size as a single element of | |
502 | this dimension. */ | |
503 | bool is_dim_contiguous = (new_stride == slice_element_size); | |
504 | is_all_contiguous &= is_dim_contiguous; | |
6d816919 | 505 | |
a5c641b5 AB |
506 | if (fortran_array_slicing_debug) |
507 | { | |
508 | debug_printf ("| '-> Results:\n"); | |
a5adb8f3 SM |
509 | debug_printf ("| |-> Offset = %s\n", plongest (offset)); |
510 | debug_printf ("| |-> Elements = %s\n", plongest (e_count)); | |
511 | debug_printf ("| |-> Low bound = %s\n", plongest (new_low)); | |
512 | debug_printf ("| |-> High bound = %s\n", | |
513 | plongest (new_high)); | |
514 | debug_printf ("| |-> Byte stride = %s\n", | |
515 | plongest (new_stride)); | |
516 | debug_printf ("| |-> Last element = %s\n", | |
517 | plongest (last_elem)); | |
518 | debug_printf ("| |-> Remainder = %s\n", | |
519 | plongest (remainder)); | |
a5c641b5 AB |
520 | debug_printf ("| '-> Contiguous = %s\n", |
521 | (is_dim_contiguous ? "Yes" : "No")); | |
522 | } | |
523 | ||
524 | /* Figure out how big (in bytes) an element of this dimension of | |
525 | the new array slice will be. */ | |
526 | slice_element_size = std::abs (new_stride * e_count); | |
6d816919 | 527 | |
a5c641b5 AB |
528 | slice_dims.emplace_back (new_low, new_high, new_stride, |
529 | index_type); | |
530 | ||
531 | /* Update the total offset. */ | |
532 | total_offset += offset; | |
533 | } | |
534 | else | |
535 | { | |
536 | /* There is a single index for this dimension. */ | |
537 | LONGEST index | |
538 | = value_as_long (evaluate_subexp_with_coercion (exp, pos, noside)); | |
539 | ||
540 | /* Get information about this dimension in the original ARRAY. */ | |
541 | struct type *target_type = TYPE_TARGET_TYPE (dim_type); | |
542 | struct type *index_type = dim_type->index_type (); | |
543 | LONGEST lb = f77_get_lowerbound (dim_type); | |
544 | LONGEST ub = f77_get_upperbound (dim_type); | |
545 | LONGEST sd = index_type->bit_stride () / 8; | |
546 | if (sd == 0) | |
547 | sd = TYPE_LENGTH (target_type); | |
548 | ||
549 | if (fortran_array_slicing_debug) | |
550 | { | |
551 | debug_printf ("|-> Index access\n"); | |
552 | std::string str = type_to_string (dim_type); | |
553 | debug_printf ("| |-> Type: %s\n", str.c_str ()); | |
554 | debug_printf ("| |-> Array:\n"); | |
a5adb8f3 SM |
555 | debug_printf ("| | |-> Low bound: %s\n", plongest (lb)); |
556 | debug_printf ("| | |-> High bound: %s\n", plongest (ub)); | |
557 | debug_printf ("| | |-> Byte stride: %s\n", plongest (sd)); | |
558 | debug_printf ("| | |-> Type size: %s\n", | |
559 | pulongest (TYPE_LENGTH (dim_type))); | |
560 | debug_printf ("| | '-> Target type size: %s\n", | |
561 | pulongest (TYPE_LENGTH (target_type))); | |
a5c641b5 | 562 | debug_printf ("| '-> Accessing:\n"); |
a5adb8f3 SM |
563 | debug_printf ("| '-> Index: %s\n", |
564 | plongest (index)); | |
a5c641b5 AB |
565 | } |
566 | ||
567 | /* If the array has actual content then check the index is in | |
568 | bounds. An array without content (an unbound array) doesn't | |
569 | have a known upper bound, so don't error check in that | |
570 | situation. */ | |
571 | if (index < lb | |
572 | || (dim_type->index_type ()->bounds ()->high.kind () != PROP_UNDEFINED | |
573 | && index > ub) | |
574 | || (VALUE_LVAL (array) != lval_memory | |
575 | && dim_type->index_type ()->bounds ()->high.kind () == PROP_UNDEFINED)) | |
576 | { | |
577 | if (type_not_associated (dim_type)) | |
578 | error (_("no such vector element (vector not associated)")); | |
579 | else if (type_not_allocated (dim_type)) | |
580 | error (_("no such vector element (vector not allocated)")); | |
581 | else | |
582 | error (_("no such vector element")); | |
583 | } | |
584 | ||
585 | /* Calculate using the type stride, not the target type size. */ | |
586 | LONGEST offset = sd * (index - lb); | |
587 | total_offset += offset; | |
588 | } | |
589 | } | |
590 | ||
591 | if (noside == EVAL_SKIP) | |
592 | return array; | |
6d816919 | 593 | |
a5c641b5 AB |
594 | /* Build a type that represents the new array slice in the target memory |
595 | of the original ARRAY, this type makes use of strides to correctly | |
596 | find only those elements that are part of the new slice. */ | |
597 | struct type *array_slice_type = inner_element_type; | |
598 | for (const auto &d : slice_dims) | |
6d816919 | 599 | { |
a5c641b5 AB |
600 | /* Create the range. */ |
601 | dynamic_prop p_low, p_high, p_stride; | |
602 | ||
603 | p_low.set_const_val (d.low); | |
604 | p_high.set_const_val (d.high); | |
605 | p_stride.set_const_val (d.stride); | |
606 | ||
607 | struct type *new_range | |
608 | = create_range_type_with_stride ((struct type *) NULL, | |
609 | TYPE_TARGET_TYPE (d.index), | |
610 | &p_low, &p_high, 0, &p_stride, | |
611 | true); | |
612 | array_slice_type | |
613 | = create_array_type (nullptr, array_slice_type, new_range); | |
614 | } | |
6d816919 | 615 | |
a5c641b5 AB |
616 | if (fortran_array_slicing_debug) |
617 | { | |
618 | debug_printf ("'-> Final result:\n"); | |
619 | debug_printf (" |-> Type: %s\n", | |
620 | type_to_string (array_slice_type).c_str ()); | |
a5adb8f3 SM |
621 | debug_printf (" |-> Total offset: %s\n", |
622 | plongest (total_offset)); | |
a5c641b5 AB |
623 | debug_printf (" |-> Base address: %s\n", |
624 | core_addr_to_string (value_address (array))); | |
625 | debug_printf (" '-> Contiguous = %s\n", | |
626 | (is_all_contiguous ? "Yes" : "No")); | |
6d816919 AB |
627 | } |
628 | ||
a5c641b5 AB |
629 | /* Should we repack this array slice? */ |
630 | if (!is_all_contiguous && (repack_array_slices || is_string_p)) | |
6d816919 | 631 | { |
a5c641b5 AB |
632 | /* Build a type for the repacked slice. */ |
633 | struct type *repacked_array_type = inner_element_type; | |
634 | for (const auto &d : slice_dims) | |
635 | { | |
636 | /* Create the range. */ | |
637 | dynamic_prop p_low, p_high, p_stride; | |
638 | ||
639 | p_low.set_const_val (d.low); | |
640 | p_high.set_const_val (d.high); | |
641 | p_stride.set_const_val (TYPE_LENGTH (repacked_array_type)); | |
642 | ||
643 | struct type *new_range | |
644 | = create_range_type_with_stride ((struct type *) NULL, | |
645 | TYPE_TARGET_TYPE (d.index), | |
646 | &p_low, &p_high, 0, &p_stride, | |
647 | true); | |
648 | repacked_array_type | |
649 | = create_array_type (nullptr, repacked_array_type, new_range); | |
650 | } | |
6d816919 | 651 | |
a5c641b5 AB |
652 | /* Now copy the elements from the original ARRAY into the packed |
653 | array value DEST. */ | |
654 | struct value *dest = allocate_value (repacked_array_type); | |
655 | if (value_lazy (array) | |
656 | || (total_offset + TYPE_LENGTH (array_slice_type) | |
657 | > TYPE_LENGTH (check_typedef (value_type (array))))) | |
658 | { | |
659 | fortran_array_walker<fortran_lazy_array_repacker_impl> p | |
660 | (array_slice_type, value_address (array) + total_offset, dest); | |
661 | p.walk (); | |
662 | } | |
663 | else | |
664 | { | |
665 | fortran_array_walker<fortran_array_repacker_impl> p | |
666 | (array_slice_type, value_address (array) + total_offset, | |
667 | total_offset, array, dest); | |
668 | p.walk (); | |
669 | } | |
670 | array = dest; | |
671 | } | |
672 | else | |
673 | { | |
674 | if (VALUE_LVAL (array) == lval_memory) | |
675 | { | |
676 | /* If the value we're taking a slice from is not yet loaded, or | |
677 | the requested slice is outside the values content range then | |
678 | just create a new lazy value pointing at the memory where the | |
679 | contents we're looking for exist. */ | |
680 | if (value_lazy (array) | |
681 | || (total_offset + TYPE_LENGTH (array_slice_type) | |
682 | > TYPE_LENGTH (check_typedef (value_type (array))))) | |
683 | array = value_at_lazy (array_slice_type, | |
684 | value_address (array) + total_offset); | |
685 | else | |
686 | array = value_from_contents_and_address (array_slice_type, | |
687 | (value_contents (array) | |
688 | + total_offset), | |
689 | (value_address (array) | |
690 | + total_offset)); | |
691 | } | |
692 | else if (!value_lazy (array)) | |
693 | { | |
694 | const void *valaddr = value_contents (array) + total_offset; | |
695 | array = allocate_value (array_slice_type); | |
696 | memcpy (value_contents_raw (array), valaddr, TYPE_LENGTH (array_slice_type)); | |
697 | } | |
698 | else | |
699 | error (_("cannot subscript arrays that are not in memory")); | |
6d816919 AB |
700 | } |
701 | ||
702 | return array; | |
703 | } | |
704 | ||
9dad4a58 | 705 | /* Special expression evaluation cases for Fortran. */ |
cb8c24b6 SM |
706 | |
707 | static struct value * | |
9dad4a58 AB |
708 | evaluate_subexp_f (struct type *expect_type, struct expression *exp, |
709 | int *pos, enum noside noside) | |
710 | { | |
b6d03bb2 | 711 | struct value *arg1 = NULL, *arg2 = NULL; |
4d00f5d8 AB |
712 | enum exp_opcode op; |
713 | int pc; | |
714 | struct type *type; | |
715 | ||
716 | pc = *pos; | |
717 | *pos += 1; | |
718 | op = exp->elts[pc].opcode; | |
719 | ||
720 | switch (op) | |
721 | { | |
722 | default: | |
723 | *pos -= 1; | |
724 | return evaluate_subexp_standard (expect_type, exp, pos, noside); | |
725 | ||
0841c79a | 726 | case UNOP_ABS: |
fe1fe7ea | 727 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
0841c79a AB |
728 | if (noside == EVAL_SKIP) |
729 | return eval_skip_value (exp); | |
730 | type = value_type (arg1); | |
78134374 | 731 | switch (type->code ()) |
0841c79a AB |
732 | { |
733 | case TYPE_CODE_FLT: | |
734 | { | |
735 | double d | |
736 | = fabs (target_float_to_host_double (value_contents (arg1), | |
737 | value_type (arg1))); | |
738 | return value_from_host_double (type, d); | |
739 | } | |
740 | case TYPE_CODE_INT: | |
741 | { | |
742 | LONGEST l = value_as_long (arg1); | |
743 | l = llabs (l); | |
744 | return value_from_longest (type, l); | |
745 | } | |
746 | } | |
747 | error (_("ABS of type %s not supported"), TYPE_SAFE_NAME (type)); | |
748 | ||
b6d03bb2 | 749 | case BINOP_MOD: |
fe1fe7ea | 750 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
b6d03bb2 AB |
751 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
752 | if (noside == EVAL_SKIP) | |
753 | return eval_skip_value (exp); | |
754 | type = value_type (arg1); | |
78134374 | 755 | if (type->code () != value_type (arg2)->code ()) |
b6d03bb2 | 756 | error (_("non-matching types for parameters to MOD ()")); |
78134374 | 757 | switch (type->code ()) |
b6d03bb2 AB |
758 | { |
759 | case TYPE_CODE_FLT: | |
760 | { | |
761 | double d1 | |
762 | = target_float_to_host_double (value_contents (arg1), | |
763 | value_type (arg1)); | |
764 | double d2 | |
765 | = target_float_to_host_double (value_contents (arg2), | |
766 | value_type (arg2)); | |
767 | double d3 = fmod (d1, d2); | |
768 | return value_from_host_double (type, d3); | |
769 | } | |
770 | case TYPE_CODE_INT: | |
771 | { | |
772 | LONGEST v1 = value_as_long (arg1); | |
773 | LONGEST v2 = value_as_long (arg2); | |
774 | if (v2 == 0) | |
775 | error (_("calling MOD (N, 0) is undefined")); | |
776 | LONGEST v3 = v1 - (v1 / v2) * v2; | |
777 | return value_from_longest (value_type (arg1), v3); | |
778 | } | |
779 | } | |
780 | error (_("MOD of type %s not supported"), TYPE_SAFE_NAME (type)); | |
781 | ||
782 | case UNOP_FORTRAN_CEILING: | |
783 | { | |
fe1fe7ea | 784 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
b6d03bb2 AB |
785 | if (noside == EVAL_SKIP) |
786 | return eval_skip_value (exp); | |
787 | type = value_type (arg1); | |
78134374 | 788 | if (type->code () != TYPE_CODE_FLT) |
b6d03bb2 AB |
789 | error (_("argument to CEILING must be of type float")); |
790 | double val | |
791 | = target_float_to_host_double (value_contents (arg1), | |
792 | value_type (arg1)); | |
793 | val = ceil (val); | |
794 | return value_from_host_double (type, val); | |
795 | } | |
796 | ||
797 | case UNOP_FORTRAN_FLOOR: | |
798 | { | |
fe1fe7ea | 799 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
b6d03bb2 AB |
800 | if (noside == EVAL_SKIP) |
801 | return eval_skip_value (exp); | |
802 | type = value_type (arg1); | |
78134374 | 803 | if (type->code () != TYPE_CODE_FLT) |
b6d03bb2 AB |
804 | error (_("argument to FLOOR must be of type float")); |
805 | double val | |
806 | = target_float_to_host_double (value_contents (arg1), | |
807 | value_type (arg1)); | |
808 | val = floor (val); | |
809 | return value_from_host_double (type, val); | |
810 | } | |
811 | ||
812 | case BINOP_FORTRAN_MODULO: | |
813 | { | |
fe1fe7ea | 814 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
b6d03bb2 AB |
815 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
816 | if (noside == EVAL_SKIP) | |
817 | return eval_skip_value (exp); | |
818 | type = value_type (arg1); | |
78134374 | 819 | if (type->code () != value_type (arg2)->code ()) |
b6d03bb2 | 820 | error (_("non-matching types for parameters to MODULO ()")); |
dda83cd7 | 821 | /* MODULO(A, P) = A - FLOOR (A / P) * P */ |
78134374 | 822 | switch (type->code ()) |
b6d03bb2 AB |
823 | { |
824 | case TYPE_CODE_INT: | |
825 | { | |
826 | LONGEST a = value_as_long (arg1); | |
827 | LONGEST p = value_as_long (arg2); | |
828 | LONGEST result = a - (a / p) * p; | |
829 | if (result != 0 && (a < 0) != (p < 0)) | |
830 | result += p; | |
831 | return value_from_longest (value_type (arg1), result); | |
832 | } | |
833 | case TYPE_CODE_FLT: | |
834 | { | |
835 | double a | |
836 | = target_float_to_host_double (value_contents (arg1), | |
837 | value_type (arg1)); | |
838 | double p | |
839 | = target_float_to_host_double (value_contents (arg2), | |
840 | value_type (arg2)); | |
841 | double result = fmod (a, p); | |
842 | if (result != 0 && (a < 0.0) != (p < 0.0)) | |
843 | result += p; | |
844 | return value_from_host_double (type, result); | |
845 | } | |
846 | } | |
847 | error (_("MODULO of type %s not supported"), TYPE_SAFE_NAME (type)); | |
848 | } | |
849 | ||
850 | case BINOP_FORTRAN_CMPLX: | |
fe1fe7ea | 851 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
b6d03bb2 AB |
852 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
853 | if (noside == EVAL_SKIP) | |
854 | return eval_skip_value (exp); | |
855 | type = builtin_f_type(exp->gdbarch)->builtin_complex_s16; | |
856 | return value_literal_complex (arg1, arg2, type); | |
857 | ||
83228e93 | 858 | case UNOP_FORTRAN_KIND: |
4d00f5d8 AB |
859 | arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
860 | type = value_type (arg1); | |
861 | ||
78134374 | 862 | switch (type->code ()) |
dda83cd7 SM |
863 | { |
864 | case TYPE_CODE_STRUCT: | |
865 | case TYPE_CODE_UNION: | |
866 | case TYPE_CODE_MODULE: | |
867 | case TYPE_CODE_FUNC: | |
868 | error (_("argument to kind must be an intrinsic type")); | |
869 | } | |
4d00f5d8 AB |
870 | |
871 | if (!TYPE_TARGET_TYPE (type)) | |
dda83cd7 | 872 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, |
4d00f5d8 AB |
873 | TYPE_LENGTH (type)); |
874 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, | |
78134374 | 875 | TYPE_LENGTH (TYPE_TARGET_TYPE (type))); |
6d816919 AB |
876 | |
877 | ||
878 | case OP_F77_UNDETERMINED_ARGLIST: | |
879 | /* Remember that in F77, functions, substring ops and array subscript | |
dda83cd7 SM |
880 | operations cannot be disambiguated at parse time. We have made |
881 | all array subscript operations, substring operations as well as | |
882 | function calls come here and we now have to discover what the heck | |
883 | this thing actually was. If it is a function, we process just as | |
884 | if we got an OP_FUNCALL. */ | |
6d816919 AB |
885 | int nargs = longest_to_int (exp->elts[pc + 1].longconst); |
886 | (*pos) += 2; | |
887 | ||
888 | /* First determine the type code we are dealing with. */ | |
889 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); | |
890 | type = check_typedef (value_type (arg1)); | |
891 | enum type_code code = type->code (); | |
892 | ||
893 | if (code == TYPE_CODE_PTR) | |
894 | { | |
895 | /* Fortran always passes variable to subroutines as pointer. | |
896 | So we need to look into its target type to see if it is | |
897 | array, string or function. If it is, we need to switch | |
898 | to the target value the original one points to. */ | |
899 | struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type)); | |
900 | ||
901 | if (target_type->code () == TYPE_CODE_ARRAY | |
902 | || target_type->code () == TYPE_CODE_STRING | |
903 | || target_type->code () == TYPE_CODE_FUNC) | |
904 | { | |
905 | arg1 = value_ind (arg1); | |
906 | type = check_typedef (value_type (arg1)); | |
907 | code = type->code (); | |
908 | } | |
909 | } | |
910 | ||
911 | switch (code) | |
912 | { | |
913 | case TYPE_CODE_ARRAY: | |
914 | case TYPE_CODE_STRING: | |
915 | return fortran_value_subarray (arg1, exp, pos, nargs, noside); | |
916 | ||
917 | case TYPE_CODE_PTR: | |
918 | case TYPE_CODE_FUNC: | |
919 | case TYPE_CODE_INTERNAL_FUNCTION: | |
920 | { | |
921 | /* It's a function call. Allocate arg vector, including | |
922 | space for the function to be called in argvec[0] and a | |
923 | termination NULL. */ | |
924 | struct value **argvec = (struct value **) | |
925 | alloca (sizeof (struct value *) * (nargs + 2)); | |
926 | argvec[0] = arg1; | |
927 | int tem = 1; | |
928 | for (; tem <= nargs; tem++) | |
929 | { | |
930 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
931 | /* Arguments in Fortran are passed by address. Coerce the | |
932 | arguments here rather than in value_arg_coerce as | |
933 | otherwise the call to malloc to place the non-lvalue | |
934 | parameters in target memory is hit by this Fortran | |
935 | specific logic. This results in malloc being called | |
936 | with a pointer to an integer followed by an attempt to | |
937 | malloc the arguments to malloc in target memory. | |
938 | Infinite recursion ensues. */ | |
939 | if (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC) | |
940 | { | |
941 | bool is_artificial | |
942 | = TYPE_FIELD_ARTIFICIAL (value_type (arg1), tem - 1); | |
943 | argvec[tem] = fortran_argument_convert (argvec[tem], | |
944 | is_artificial); | |
945 | } | |
946 | } | |
947 | argvec[tem] = 0; /* signal end of arglist */ | |
948 | if (noside == EVAL_SKIP) | |
949 | return eval_skip_value (exp); | |
950 | return evaluate_subexp_do_call (exp, noside, nargs, argvec, NULL, | |
951 | expect_type); | |
952 | } | |
953 | ||
954 | default: | |
955 | error (_("Cannot perform substring on this type")); | |
956 | } | |
4d00f5d8 AB |
957 | } |
958 | ||
959 | /* Should be unreachable. */ | |
960 | return nullptr; | |
9dad4a58 AB |
961 | } |
962 | ||
83228e93 AB |
963 | /* Special expression lengths for Fortran. */ |
964 | ||
965 | static void | |
966 | operator_length_f (const struct expression *exp, int pc, int *oplenp, | |
967 | int *argsp) | |
968 | { | |
969 | int oplen = 1; | |
970 | int args = 0; | |
971 | ||
972 | switch (exp->elts[pc - 1].opcode) | |
973 | { | |
974 | default: | |
975 | operator_length_standard (exp, pc, oplenp, argsp); | |
976 | return; | |
977 | ||
978 | case UNOP_FORTRAN_KIND: | |
b6d03bb2 AB |
979 | case UNOP_FORTRAN_FLOOR: |
980 | case UNOP_FORTRAN_CEILING: | |
83228e93 AB |
981 | oplen = 1; |
982 | args = 1; | |
983 | break; | |
b6d03bb2 AB |
984 | |
985 | case BINOP_FORTRAN_CMPLX: | |
986 | case BINOP_FORTRAN_MODULO: | |
987 | oplen = 1; | |
988 | args = 2; | |
989 | break; | |
6d816919 AB |
990 | |
991 | case OP_F77_UNDETERMINED_ARGLIST: | |
992 | oplen = 3; | |
993 | args = 1 + longest_to_int (exp->elts[pc - 2].longconst); | |
994 | break; | |
83228e93 AB |
995 | } |
996 | ||
997 | *oplenp = oplen; | |
998 | *argsp = args; | |
999 | } | |
1000 | ||
b6d03bb2 AB |
1001 | /* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except |
1002 | the extra argument NAME which is the text that should be printed as the | |
1003 | name of this operation. */ | |
1004 | ||
1005 | static void | |
1006 | print_unop_subexp_f (struct expression *exp, int *pos, | |
1007 | struct ui_file *stream, enum precedence prec, | |
1008 | const char *name) | |
1009 | { | |
1010 | (*pos)++; | |
1011 | fprintf_filtered (stream, "%s(", name); | |
1012 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
1013 | fputs_filtered (")", stream); | |
1014 | } | |
1015 | ||
1016 | /* Helper for PRINT_SUBEXP_F. Arguments are as for PRINT_SUBEXP_F, except | |
1017 | the extra argument NAME which is the text that should be printed as the | |
1018 | name of this operation. */ | |
1019 | ||
1020 | static void | |
1021 | print_binop_subexp_f (struct expression *exp, int *pos, | |
1022 | struct ui_file *stream, enum precedence prec, | |
1023 | const char *name) | |
1024 | { | |
1025 | (*pos)++; | |
1026 | fprintf_filtered (stream, "%s(", name); | |
1027 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
1028 | fputs_filtered (",", stream); | |
1029 | print_subexp (exp, pos, stream, PREC_SUFFIX); | |
1030 | fputs_filtered (")", stream); | |
1031 | } | |
1032 | ||
83228e93 AB |
1033 | /* Special expression printing for Fortran. */ |
1034 | ||
1035 | static void | |
1036 | print_subexp_f (struct expression *exp, int *pos, | |
1037 | struct ui_file *stream, enum precedence prec) | |
1038 | { | |
1039 | int pc = *pos; | |
1040 | enum exp_opcode op = exp->elts[pc].opcode; | |
1041 | ||
1042 | switch (op) | |
1043 | { | |
1044 | default: | |
1045 | print_subexp_standard (exp, pos, stream, prec); | |
1046 | return; | |
1047 | ||
1048 | case UNOP_FORTRAN_KIND: | |
b6d03bb2 AB |
1049 | print_unop_subexp_f (exp, pos, stream, prec, "KIND"); |
1050 | return; | |
1051 | ||
1052 | case UNOP_FORTRAN_FLOOR: | |
1053 | print_unop_subexp_f (exp, pos, stream, prec, "FLOOR"); | |
1054 | return; | |
1055 | ||
1056 | case UNOP_FORTRAN_CEILING: | |
1057 | print_unop_subexp_f (exp, pos, stream, prec, "CEILING"); | |
1058 | return; | |
1059 | ||
1060 | case BINOP_FORTRAN_CMPLX: | |
1061 | print_binop_subexp_f (exp, pos, stream, prec, "CMPLX"); | |
1062 | return; | |
1063 | ||
1064 | case BINOP_FORTRAN_MODULO: | |
1065 | print_binop_subexp_f (exp, pos, stream, prec, "MODULO"); | |
83228e93 | 1066 | return; |
6d816919 AB |
1067 | |
1068 | case OP_F77_UNDETERMINED_ARGLIST: | |
86775fab | 1069 | (*pos)++; |
6d816919 AB |
1070 | print_subexp_funcall (exp, pos, stream); |
1071 | return; | |
83228e93 AB |
1072 | } |
1073 | } | |
1074 | ||
83228e93 AB |
1075 | /* Special expression dumping for Fortran. */ |
1076 | ||
1077 | static int | |
1078 | dump_subexp_body_f (struct expression *exp, | |
1079 | struct ui_file *stream, int elt) | |
1080 | { | |
1081 | int opcode = exp->elts[elt].opcode; | |
1082 | int oplen, nargs, i; | |
1083 | ||
1084 | switch (opcode) | |
1085 | { | |
1086 | default: | |
1087 | return dump_subexp_body_standard (exp, stream, elt); | |
1088 | ||
1089 | case UNOP_FORTRAN_KIND: | |
b6d03bb2 AB |
1090 | case UNOP_FORTRAN_FLOOR: |
1091 | case UNOP_FORTRAN_CEILING: | |
1092 | case BINOP_FORTRAN_CMPLX: | |
1093 | case BINOP_FORTRAN_MODULO: | |
83228e93 AB |
1094 | operator_length_f (exp, (elt + 1), &oplen, &nargs); |
1095 | break; | |
6d816919 AB |
1096 | |
1097 | case OP_F77_UNDETERMINED_ARGLIST: | |
86775fab | 1098 | return dump_subexp_body_funcall (exp, stream, elt + 1); |
83228e93 AB |
1099 | } |
1100 | ||
1101 | elt += oplen; | |
1102 | for (i = 0; i < nargs; i += 1) | |
1103 | elt = dump_subexp (exp, stream, elt); | |
1104 | ||
1105 | return elt; | |
1106 | } | |
1107 | ||
1108 | /* Special expression checking for Fortran. */ | |
1109 | ||
1110 | static int | |
1111 | operator_check_f (struct expression *exp, int pos, | |
1112 | int (*objfile_func) (struct objfile *objfile, | |
1113 | void *data), | |
1114 | void *data) | |
1115 | { | |
1116 | const union exp_element *const elts = exp->elts; | |
1117 | ||
1118 | switch (elts[pos].opcode) | |
1119 | { | |
1120 | case UNOP_FORTRAN_KIND: | |
b6d03bb2 AB |
1121 | case UNOP_FORTRAN_FLOOR: |
1122 | case UNOP_FORTRAN_CEILING: | |
1123 | case BINOP_FORTRAN_CMPLX: | |
1124 | case BINOP_FORTRAN_MODULO: | |
83228e93 AB |
1125 | /* Any references to objfiles are held in the arguments to this |
1126 | expression, not within the expression itself, so no additional | |
1127 | checking is required here, the outer expression iteration code | |
1128 | will take care of checking each argument. */ | |
1129 | break; | |
1130 | ||
1131 | default: | |
1132 | return operator_check_standard (exp, pos, objfile_func, data); | |
1133 | } | |
1134 | ||
1135 | return 0; | |
1136 | } | |
1137 | ||
9dad4a58 | 1138 | /* Expression processing for Fortran. */ |
1a0ea399 | 1139 | const struct exp_descriptor f_language::exp_descriptor_tab = |
9dad4a58 | 1140 | { |
83228e93 AB |
1141 | print_subexp_f, |
1142 | operator_length_f, | |
1143 | operator_check_f, | |
83228e93 | 1144 | dump_subexp_body_f, |
9dad4a58 AB |
1145 | evaluate_subexp_f |
1146 | }; | |
1147 | ||
1a0ea399 | 1148 | /* See language.h. */ |
0874fd07 | 1149 | |
1a0ea399 AB |
1150 | void |
1151 | f_language::language_arch_info (struct gdbarch *gdbarch, | |
1152 | struct language_arch_info *lai) const | |
0874fd07 | 1153 | { |
1a0ea399 AB |
1154 | const struct builtin_f_type *builtin = builtin_f_type (gdbarch); |
1155 | ||
7bea47f0 AB |
1156 | /* Helper function to allow shorter lines below. */ |
1157 | auto add = [&] (struct type * t) | |
1158 | { | |
1159 | lai->add_primitive_type (t); | |
1160 | }; | |
1161 | ||
1162 | add (builtin->builtin_character); | |
1163 | add (builtin->builtin_logical); | |
1164 | add (builtin->builtin_logical_s1); | |
1165 | add (builtin->builtin_logical_s2); | |
1166 | add (builtin->builtin_logical_s8); | |
1167 | add (builtin->builtin_real); | |
1168 | add (builtin->builtin_real_s8); | |
1169 | add (builtin->builtin_real_s16); | |
1170 | add (builtin->builtin_complex_s8); | |
1171 | add (builtin->builtin_complex_s16); | |
1172 | add (builtin->builtin_void); | |
1173 | ||
1174 | lai->set_string_char_type (builtin->builtin_character); | |
1175 | lai->set_bool_type (builtin->builtin_logical_s2, "logical"); | |
1a0ea399 | 1176 | } |
5aba6ebe | 1177 | |
1a0ea399 | 1178 | /* See language.h. */ |
5aba6ebe | 1179 | |
1a0ea399 AB |
1180 | unsigned int |
1181 | f_language::search_name_hash (const char *name) const | |
1182 | { | |
1183 | return cp_search_name_hash (name); | |
1184 | } | |
b7c6e27d | 1185 | |
1a0ea399 | 1186 | /* See language.h. */ |
b7c6e27d | 1187 | |
1a0ea399 AB |
1188 | struct block_symbol |
1189 | f_language::lookup_symbol_nonlocal (const char *name, | |
1190 | const struct block *block, | |
1191 | const domain_enum domain) const | |
1192 | { | |
1193 | return cp_lookup_symbol_nonlocal (this, name, block, domain); | |
1194 | } | |
c9debfb9 | 1195 | |
1a0ea399 | 1196 | /* See language.h. */ |
c9debfb9 | 1197 | |
1a0ea399 AB |
1198 | symbol_name_matcher_ftype * |
1199 | f_language::get_symbol_name_matcher_inner | |
1200 | (const lookup_name_info &lookup_name) const | |
1201 | { | |
1202 | return cp_get_symbol_name_matcher (lookup_name); | |
1203 | } | |
0874fd07 AB |
1204 | |
1205 | /* Single instance of the Fortran language class. */ | |
1206 | ||
1207 | static f_language f_language_defn; | |
1208 | ||
54ef06c7 UW |
1209 | static void * |
1210 | build_fortran_types (struct gdbarch *gdbarch) | |
c906108c | 1211 | { |
54ef06c7 UW |
1212 | struct builtin_f_type *builtin_f_type |
1213 | = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type); | |
1214 | ||
e9bb382b | 1215 | builtin_f_type->builtin_void |
bbe75b9d | 1216 | = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void"); |
e9bb382b UW |
1217 | |
1218 | builtin_f_type->builtin_character | |
4a270568 | 1219 | = arch_type (gdbarch, TYPE_CODE_CHAR, TARGET_CHAR_BIT, "character"); |
e9bb382b UW |
1220 | |
1221 | builtin_f_type->builtin_logical_s1 | |
1222 | = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1"); | |
1223 | ||
1224 | builtin_f_type->builtin_integer_s2 | |
1225 | = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0, | |
1226 | "integer*2"); | |
1227 | ||
067630bd AB |
1228 | builtin_f_type->builtin_integer_s8 |
1229 | = arch_integer_type (gdbarch, gdbarch_long_long_bit (gdbarch), 0, | |
1230 | "integer*8"); | |
1231 | ||
e9bb382b UW |
1232 | builtin_f_type->builtin_logical_s2 |
1233 | = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1, | |
1234 | "logical*2"); | |
1235 | ||
ce4b0682 SDJ |
1236 | builtin_f_type->builtin_logical_s8 |
1237 | = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1, | |
1238 | "logical*8"); | |
1239 | ||
e9bb382b UW |
1240 | builtin_f_type->builtin_integer |
1241 | = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0, | |
1242 | "integer"); | |
1243 | ||
1244 | builtin_f_type->builtin_logical | |
1245 | = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1, | |
1246 | "logical*4"); | |
1247 | ||
1248 | builtin_f_type->builtin_real | |
1249 | = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch), | |
49f190bc | 1250 | "real", gdbarch_float_format (gdbarch)); |
e9bb382b UW |
1251 | builtin_f_type->builtin_real_s8 |
1252 | = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch), | |
49f190bc | 1253 | "real*8", gdbarch_double_format (gdbarch)); |
34d11c68 | 1254 | auto fmt = gdbarch_floatformat_for_type (gdbarch, "real(kind=16)", 128); |
dc42e902 AB |
1255 | if (fmt != nullptr) |
1256 | builtin_f_type->builtin_real_s16 | |
1257 | = arch_float_type (gdbarch, 128, "real*16", fmt); | |
1258 | else if (gdbarch_long_double_bit (gdbarch) == 128) | |
1259 | builtin_f_type->builtin_real_s16 | |
1260 | = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch), | |
1261 | "real*16", gdbarch_long_double_format (gdbarch)); | |
1262 | else | |
1263 | builtin_f_type->builtin_real_s16 | |
1264 | = arch_type (gdbarch, TYPE_CODE_ERROR, 128, "real*16"); | |
e9bb382b UW |
1265 | |
1266 | builtin_f_type->builtin_complex_s8 | |
5b930b45 | 1267 | = init_complex_type ("complex*8", builtin_f_type->builtin_real); |
e9bb382b | 1268 | builtin_f_type->builtin_complex_s16 |
5b930b45 | 1269 | = init_complex_type ("complex*16", builtin_f_type->builtin_real_s8); |
0830d301 | 1270 | |
78134374 | 1271 | if (builtin_f_type->builtin_real_s16->code () == TYPE_CODE_ERROR) |
0830d301 TT |
1272 | builtin_f_type->builtin_complex_s32 |
1273 | = arch_type (gdbarch, TYPE_CODE_ERROR, 256, "complex*32"); | |
1274 | else | |
1275 | builtin_f_type->builtin_complex_s32 | |
1276 | = init_complex_type ("complex*32", builtin_f_type->builtin_real_s16); | |
54ef06c7 UW |
1277 | |
1278 | return builtin_f_type; | |
1279 | } | |
1280 | ||
1281 | static struct gdbarch_data *f_type_data; | |
1282 | ||
1283 | const struct builtin_f_type * | |
1284 | builtin_f_type (struct gdbarch *gdbarch) | |
1285 | { | |
9a3c8263 | 1286 | return (const struct builtin_f_type *) gdbarch_data (gdbarch, f_type_data); |
4e845cd3 MS |
1287 | } |
1288 | ||
a5c641b5 AB |
1289 | /* Command-list for the "set/show fortran" prefix command. */ |
1290 | static struct cmd_list_element *set_fortran_list; | |
1291 | static struct cmd_list_element *show_fortran_list; | |
1292 | ||
6c265988 | 1293 | void _initialize_f_language (); |
4e845cd3 | 1294 | void |
6c265988 | 1295 | _initialize_f_language () |
4e845cd3 | 1296 | { |
54ef06c7 | 1297 | f_type_data = gdbarch_data_register_post_init (build_fortran_types); |
a5c641b5 AB |
1298 | |
1299 | add_basic_prefix_cmd ("fortran", no_class, | |
1300 | _("Prefix command for changing Fortran-specific settings."), | |
1301 | &set_fortran_list, "set fortran ", 0, &setlist); | |
1302 | ||
1303 | add_show_prefix_cmd ("fortran", no_class, | |
1304 | _("Generic command for showing Fortran-specific settings."), | |
1305 | &show_fortran_list, "show fortran ", 0, &showlist); | |
1306 | ||
1307 | add_setshow_boolean_cmd ("repack-array-slices", class_vars, | |
1308 | &repack_array_slices, _("\ | |
1309 | Enable or disable repacking of non-contiguous array slices."), _("\ | |
1310 | Show whether non-contiguous array slices are repacked."), _("\ | |
1311 | When the user requests a slice of a Fortran array then we can either return\n\ | |
1312 | a descriptor that describes the array in place (using the original array data\n\ | |
1313 | in its existing location) or the original data can be repacked (copied) to a\n\ | |
1314 | new location.\n\ | |
1315 | \n\ | |
1316 | When the content of the array slice is contiguous within the original array\n\ | |
1317 | then the result will never be repacked, but when the data for the new array\n\ | |
1318 | is non-contiguous within the original array repacking will only be performed\n\ | |
1319 | when this setting is on."), | |
1320 | NULL, | |
1321 | show_repack_array_slices, | |
1322 | &set_fortran_list, &show_fortran_list); | |
1323 | ||
1324 | /* Debug Fortran's array slicing logic. */ | |
1325 | add_setshow_boolean_cmd ("fortran-array-slicing", class_maintenance, | |
1326 | &fortran_array_slicing_debug, _("\ | |
1327 | Set debugging of Fortran array slicing."), _("\ | |
1328 | Show debugging of Fortran array slicing."), _("\ | |
1329 | When on, debugging of Fortran array slicing is enabled."), | |
1330 | NULL, | |
1331 | show_fortran_array_slicing_debug, | |
1332 | &setdebuglist, &showdebuglist); | |
c906108c | 1333 | } |
aa3cfbda | 1334 | |
5a7cf527 AB |
1335 | /* Ensures that function argument VALUE is in the appropriate form to |
1336 | pass to a Fortran function. Returns a possibly new value that should | |
1337 | be used instead of VALUE. | |
1338 | ||
1339 | When IS_ARTIFICIAL is true this indicates an artificial argument, | |
1340 | e.g. hidden string lengths which the GNU Fortran argument passing | |
1341 | convention specifies as being passed by value. | |
aa3cfbda | 1342 | |
5a7cf527 AB |
1343 | When IS_ARTIFICIAL is false, the argument is passed by pointer. If the |
1344 | value is already in target memory then return a value that is a pointer | |
1345 | to VALUE. If VALUE is not in memory (e.g. an integer literal), allocate | |
1346 | space in the target, copy VALUE in, and return a pointer to the in | |
1347 | memory copy. */ | |
1348 | ||
1349 | static struct value * | |
aa3cfbda RB |
1350 | fortran_argument_convert (struct value *value, bool is_artificial) |
1351 | { | |
1352 | if (!is_artificial) | |
1353 | { | |
1354 | /* If the value is not in the inferior e.g. registers values, | |
1355 | convenience variables and user input. */ | |
1356 | if (VALUE_LVAL (value) != lval_memory) | |
1357 | { | |
1358 | struct type *type = value_type (value); | |
1359 | const int length = TYPE_LENGTH (type); | |
1360 | const CORE_ADDR addr | |
1361 | = value_as_long (value_allocate_space_in_inferior (length)); | |
1362 | write_memory (addr, value_contents (value), length); | |
1363 | struct value *val | |
1364 | = value_from_contents_and_address (type, value_contents (value), | |
1365 | addr); | |
1366 | return value_addr (val); | |
1367 | } | |
1368 | else | |
1369 | return value_addr (value); /* Program variables, e.g. arrays. */ | |
1370 | } | |
1371 | return value; | |
1372 | } | |
1373 | ||
1374 | /* See f-lang.h. */ | |
1375 | ||
1376 | struct type * | |
1377 | fortran_preserve_arg_pointer (struct value *arg, struct type *type) | |
1378 | { | |
78134374 | 1379 | if (value_type (arg)->code () == TYPE_CODE_PTR) |
aa3cfbda RB |
1380 | return value_type (arg); |
1381 | return type; | |
1382 | } | |
a5c641b5 AB |
1383 | |
1384 | /* See f-lang.h. */ | |
1385 | ||
1386 | CORE_ADDR | |
1387 | fortran_adjust_dynamic_array_base_address_hack (struct type *type, | |
1388 | CORE_ADDR address) | |
1389 | { | |
1390 | gdb_assert (type->code () == TYPE_CODE_ARRAY); | |
1391 | ||
1392 | int ndimensions = calc_f77_array_dims (type); | |
1393 | LONGEST total_offset = 0; | |
1394 | ||
1395 | /* Walk through each of the dimensions of this array type and figure out | |
1396 | if any of the dimensions are "backwards", that is the base address | |
1397 | for this dimension points to the element at the highest memory | |
1398 | address and the stride is negative. */ | |
1399 | struct type *tmp_type = type; | |
1400 | for (int i = 0 ; i < ndimensions; ++i) | |
1401 | { | |
1402 | /* Grab the range for this dimension and extract the lower and upper | |
1403 | bounds. */ | |
1404 | tmp_type = check_typedef (tmp_type); | |
1405 | struct type *range_type = tmp_type->index_type (); | |
1406 | LONGEST lowerbound, upperbound, stride; | |
1f8d2881 | 1407 | if (!get_discrete_bounds (range_type, &lowerbound, &upperbound)) |
a5c641b5 AB |
1408 | error ("failed to get range bounds"); |
1409 | ||
1410 | /* Figure out the stride for this dimension. */ | |
1411 | struct type *elt_type = check_typedef (TYPE_TARGET_TYPE (tmp_type)); | |
1412 | stride = tmp_type->index_type ()->bounds ()->bit_stride (); | |
1413 | if (stride == 0) | |
1414 | stride = type_length_units (elt_type); | |
1415 | else | |
1416 | { | |
1417 | struct gdbarch *arch = get_type_arch (elt_type); | |
1418 | int unit_size = gdbarch_addressable_memory_unit_size (arch); | |
1419 | stride /= (unit_size * 8); | |
1420 | } | |
1421 | ||
1422 | /* If this dimension is "backward" then figure out the offset | |
1423 | adjustment required to point to the element at the lowest memory | |
1424 | address, and add this to the total offset. */ | |
1425 | LONGEST offset = 0; | |
1426 | if (stride < 0 && lowerbound < upperbound) | |
1427 | offset = (upperbound - lowerbound) * stride; | |
1428 | total_offset += offset; | |
1429 | tmp_type = TYPE_TARGET_TYPE (tmp_type); | |
1430 | } | |
1431 | ||
1432 | /* Adjust the address of this object and return it. */ | |
1433 | address += total_offset; | |
1434 | return address; | |
1435 | } |