1 /* Fortran language support routines for GDB, the GNU debugger.
3 Copyright (C) 1993-1996, 1998-2005, 2007-2012 Free Software
6 Contributed by Motorola. Adapted from the C parser by Farooq Butt
7 (fmbutt@engage.sps.mot.com).
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_string.h"
28 #include "expression.h"
29 #include "parser-defs.h"
34 #include "cp-support.h"
39 /* Following is dubious stuff that had been in the xcoff reader. */
43 long line_offset
; /* Line offset for function. */
44 struct saved_fcn
*next
;
48 struct saved_bf_symnum
50 long symnum_fcn
; /* Symnum of function (i.e. .function
52 long symnum_bf
; /* Symnum of .bf for this function. */
53 struct saved_bf_symnum
*next
;
56 typedef struct saved_fcn SAVED_FUNCTION
, *SAVED_FUNCTION_PTR
;
57 typedef struct saved_bf_symnum SAVED_BF
, *SAVED_BF_PTR
;
61 extern void _initialize_f_language (void);
63 static void clear_function_list (void);
64 static long get_bf_for_fcn (long);
65 static void clear_bf_list (void);
66 static void patch_all_commons_by_name (char *, CORE_ADDR
, int);
67 static SAVED_F77_COMMON_PTR
find_first_common_named (char *);
68 static void add_common_entry (struct symbol
*);
69 static void add_common_block (char *, CORE_ADDR
, int, char *);
70 static SAVED_FUNCTION
*allocate_saved_function_node (void);
71 static SAVED_BF_PTR
allocate_saved_bf_node (void);
72 static COMMON_ENTRY_PTR
allocate_common_entry_node (void);
73 static SAVED_F77_COMMON_PTR
allocate_saved_f77_common_node (void);
74 static void patch_common_entries (SAVED_F77_COMMON_PTR
, CORE_ADDR
, int);
77 static void f_printchar (int c
, struct type
*type
, struct ui_file
* stream
);
78 static void f_emit_char (int c
, struct type
*type
,
79 struct ui_file
* stream
, int quoter
);
81 /* Return the encoding that should be used for the character type
85 f_get_encoding (struct type
*type
)
89 switch (TYPE_LENGTH (type
))
92 encoding
= target_charset (get_type_arch (type
));
95 if (gdbarch_byte_order (get_type_arch (type
)) == BFD_ENDIAN_BIG
)
96 encoding
= "UTF-32BE";
98 encoding
= "UTF-32LE";
102 error (_("unrecognized character type"));
108 /* Print the character C on STREAM as part of the contents of a literal
109 string whose delimiter is QUOTER. Note that that format for printing
110 characters and strings is language specific.
111 FIXME: This is a copy of the same function from c-exp.y. It should
112 be replaced with a true F77 version. */
115 f_emit_char (int c
, struct type
*type
, struct ui_file
*stream
, int quoter
)
117 const char *encoding
= f_get_encoding (type
);
119 generic_emit_char (c
, type
, stream
, quoter
, encoding
);
122 /* Implementation of la_printchar. */
125 f_printchar (int c
, struct type
*type
, struct ui_file
*stream
)
127 fputs_filtered ("'", stream
);
128 LA_EMIT_CHAR (c
, type
, stream
, '\'');
129 fputs_filtered ("'", stream
);
132 /* Print the character string STRING, printing at most LENGTH characters.
133 Printing stops early if the number hits print_max; repeat counts
134 are printed as appropriate. Print ellipses at the end if we
135 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
136 FIXME: This is a copy of the same function from c-exp.y. It should
137 be replaced with a true F77 version. */
140 f_printstr (struct ui_file
*stream
, struct type
*type
, const gdb_byte
*string
,
141 unsigned int length
, const char *encoding
, int force_ellipses
,
142 const struct value_print_options
*options
)
144 const char *type_encoding
= f_get_encoding (type
);
146 if (TYPE_LENGTH (type
) == 4)
147 fputs_filtered ("4_", stream
);
149 if (!encoding
|| !*encoding
)
150 encoding
= type_encoding
;
152 generic_printstr (stream
, type
, string
, length
, encoding
,
153 force_ellipses
, '\'', 0, options
);
157 /* Table of operators and their precedences for printing expressions. */
159 static const struct op_print f_op_print_tab
[] =
161 {"+", BINOP_ADD
, PREC_ADD
, 0},
162 {"+", UNOP_PLUS
, PREC_PREFIX
, 0},
163 {"-", BINOP_SUB
, PREC_ADD
, 0},
164 {"-", UNOP_NEG
, PREC_PREFIX
, 0},
165 {"*", BINOP_MUL
, PREC_MUL
, 0},
166 {"/", BINOP_DIV
, PREC_MUL
, 0},
167 {"DIV", BINOP_INTDIV
, PREC_MUL
, 0},
168 {"MOD", BINOP_REM
, PREC_MUL
, 0},
169 {"=", BINOP_ASSIGN
, PREC_ASSIGN
, 1},
170 {".OR.", BINOP_LOGICAL_OR
, PREC_LOGICAL_OR
, 0},
171 {".AND.", BINOP_LOGICAL_AND
, PREC_LOGICAL_AND
, 0},
172 {".NOT.", UNOP_LOGICAL_NOT
, PREC_PREFIX
, 0},
173 {".EQ.", BINOP_EQUAL
, PREC_EQUAL
, 0},
174 {".NE.", BINOP_NOTEQUAL
, PREC_EQUAL
, 0},
175 {".LE.", BINOP_LEQ
, PREC_ORDER
, 0},
176 {".GE.", BINOP_GEQ
, PREC_ORDER
, 0},
177 {".GT.", BINOP_GTR
, PREC_ORDER
, 0},
178 {".LT.", BINOP_LESS
, PREC_ORDER
, 0},
179 {"**", UNOP_IND
, PREC_PREFIX
, 0},
180 {"@", BINOP_REPEAT
, PREC_REPEAT
, 0},
184 enum f_primitive_types
{
185 f_primitive_type_character
,
186 f_primitive_type_logical
,
187 f_primitive_type_logical_s1
,
188 f_primitive_type_logical_s2
,
189 f_primitive_type_logical_s8
,
190 f_primitive_type_integer
,
191 f_primitive_type_integer_s2
,
192 f_primitive_type_real
,
193 f_primitive_type_real_s8
,
194 f_primitive_type_real_s16
,
195 f_primitive_type_complex_s8
,
196 f_primitive_type_complex_s16
,
197 f_primitive_type_void
,
202 f_language_arch_info (struct gdbarch
*gdbarch
,
203 struct language_arch_info
*lai
)
205 const struct builtin_f_type
*builtin
= builtin_f_type (gdbarch
);
207 lai
->string_char_type
= builtin
->builtin_character
;
208 lai
->primitive_type_vector
209 = GDBARCH_OBSTACK_CALLOC (gdbarch
, nr_f_primitive_types
+ 1,
212 lai
->primitive_type_vector
[f_primitive_type_character
]
213 = builtin
->builtin_character
;
214 lai
->primitive_type_vector
[f_primitive_type_logical
]
215 = builtin
->builtin_logical
;
216 lai
->primitive_type_vector
[f_primitive_type_logical_s1
]
217 = builtin
->builtin_logical_s1
;
218 lai
->primitive_type_vector
[f_primitive_type_logical_s2
]
219 = builtin
->builtin_logical_s2
;
220 lai
->primitive_type_vector
[f_primitive_type_logical_s8
]
221 = builtin
->builtin_logical_s8
;
222 lai
->primitive_type_vector
[f_primitive_type_real
]
223 = builtin
->builtin_real
;
224 lai
->primitive_type_vector
[f_primitive_type_real_s8
]
225 = builtin
->builtin_real_s8
;
226 lai
->primitive_type_vector
[f_primitive_type_real_s16
]
227 = builtin
->builtin_real_s16
;
228 lai
->primitive_type_vector
[f_primitive_type_complex_s8
]
229 = builtin
->builtin_complex_s8
;
230 lai
->primitive_type_vector
[f_primitive_type_complex_s16
]
231 = builtin
->builtin_complex_s16
;
232 lai
->primitive_type_vector
[f_primitive_type_void
]
233 = builtin
->builtin_void
;
235 lai
->bool_type_symbol
= "logical";
236 lai
->bool_type_default
= builtin
->builtin_logical_s2
;
239 /* Remove the modules separator :: from the default break list. */
242 f_word_break_characters (void)
250 retval
= xstrdup (default_word_break_characters ());
251 s
= strchr (retval
, ':');
254 char *last_char
= &s
[strlen (s
) - 1];
263 /* Consider the modules separator :: as a valid symbol name character
266 static VEC (char_ptr
) *
267 f_make_symbol_completion_list (char *text
, char *word
)
269 return default_make_symbol_completion_list_break_on (text
, word
, ":");
272 const struct language_defn f_language_defn
=
280 &exp_descriptor_standard
,
281 f_parse
, /* parser */
282 f_error
, /* parser error function */
284 f_printchar
, /* Print character constant */
285 f_printstr
, /* function to print string constant */
286 f_emit_char
, /* Function to print a single character */
287 f_print_type
, /* Print a type using appropriate syntax */
288 default_print_typedef
, /* Print a typedef using appropriate syntax */
289 f_val_print
, /* Print a value using appropriate syntax */
290 c_value_print
, /* FIXME */
291 default_read_var_value
, /* la_read_var_value */
292 NULL
, /* Language specific skip_trampoline */
293 NULL
, /* name_of_this */
294 cp_lookup_symbol_nonlocal
, /* lookup_symbol_nonlocal */
295 basic_lookup_transparent_type
,/* lookup_transparent_type */
296 NULL
, /* Language specific symbol demangler */
297 NULL
, /* Language specific
298 class_name_from_physname */
299 f_op_print_tab
, /* expression operators for printing */
300 0, /* arrays are first-class (not c-style) */
301 1, /* String lower bound */
302 f_word_break_characters
,
303 f_make_symbol_completion_list
,
304 f_language_arch_info
,
305 default_print_array_index
,
306 default_pass_by_reference
,
308 NULL
, /* la_get_symbol_name_cmp */
309 iterate_over_symbols
,
314 build_fortran_types (struct gdbarch
*gdbarch
)
316 struct builtin_f_type
*builtin_f_type
317 = GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct builtin_f_type
);
319 builtin_f_type
->builtin_void
320 = arch_type (gdbarch
, TYPE_CODE_VOID
, 1, "VOID");
322 builtin_f_type
->builtin_character
323 = arch_integer_type (gdbarch
, TARGET_CHAR_BIT
, 0, "character");
325 builtin_f_type
->builtin_logical_s1
326 = arch_boolean_type (gdbarch
, TARGET_CHAR_BIT
, 1, "logical*1");
328 builtin_f_type
->builtin_integer_s2
329 = arch_integer_type (gdbarch
, gdbarch_short_bit (gdbarch
), 0,
332 builtin_f_type
->builtin_logical_s2
333 = arch_boolean_type (gdbarch
, gdbarch_short_bit (gdbarch
), 1,
336 builtin_f_type
->builtin_logical_s8
337 = arch_boolean_type (gdbarch
, gdbarch_long_long_bit (gdbarch
), 1,
340 builtin_f_type
->builtin_integer
341 = arch_integer_type (gdbarch
, gdbarch_int_bit (gdbarch
), 0,
344 builtin_f_type
->builtin_logical
345 = arch_boolean_type (gdbarch
, gdbarch_int_bit (gdbarch
), 1,
348 builtin_f_type
->builtin_real
349 = arch_float_type (gdbarch
, gdbarch_float_bit (gdbarch
),
351 builtin_f_type
->builtin_real_s8
352 = arch_float_type (gdbarch
, gdbarch_double_bit (gdbarch
),
354 builtin_f_type
->builtin_real_s16
355 = arch_float_type (gdbarch
, gdbarch_long_double_bit (gdbarch
),
358 builtin_f_type
->builtin_complex_s8
359 = arch_complex_type (gdbarch
, "complex*8",
360 builtin_f_type
->builtin_real
);
361 builtin_f_type
->builtin_complex_s16
362 = arch_complex_type (gdbarch
, "complex*16",
363 builtin_f_type
->builtin_real_s8
);
364 builtin_f_type
->builtin_complex_s32
365 = arch_complex_type (gdbarch
, "complex*32",
366 builtin_f_type
->builtin_real_s16
);
368 return builtin_f_type
;
371 static struct gdbarch_data
*f_type_data
;
373 const struct builtin_f_type
*
374 builtin_f_type (struct gdbarch
*gdbarch
)
376 return gdbarch_data (gdbarch
, f_type_data
);
380 _initialize_f_language (void)
382 f_type_data
= gdbarch_data_register_post_init (build_fortran_types
);
384 add_language (&f_language_defn
);
389 allocate_saved_bf_node (void)
393 new = (SAVED_BF_PTR
) xmalloc (sizeof (SAVED_BF
));
397 static SAVED_FUNCTION
*
398 allocate_saved_function_node (void)
402 new = (SAVED_FUNCTION
*) xmalloc (sizeof (SAVED_FUNCTION
));
406 static SAVED_F77_COMMON_PTR
407 allocate_saved_f77_common_node (void)
409 SAVED_F77_COMMON_PTR
new;
411 new = (SAVED_F77_COMMON_PTR
) xmalloc (sizeof (SAVED_F77_COMMON
));
415 static COMMON_ENTRY_PTR
416 allocate_common_entry_node (void)
418 COMMON_ENTRY_PTR
new;
420 new = (COMMON_ENTRY_PTR
) xmalloc (sizeof (COMMON_ENTRY
));
425 SAVED_F77_COMMON_PTR head_common_list
= NULL
; /* Ptr to 1st saved COMMON */
426 SAVED_F77_COMMON_PTR tail_common_list
= NULL
; /* Ptr to last saved COMMON */
427 SAVED_F77_COMMON_PTR current_common
= NULL
; /* Ptr to current COMMON */
430 static SAVED_BF_PTR saved_bf_list
= NULL
; /* Ptr to (.bf,function)
432 static SAVED_BF_PTR saved_bf_list_end
= NULL
; /* Ptr to above list's end */
433 static SAVED_BF_PTR current_head_bf_list
= NULL
; /* Current head of
436 static SAVED_BF_PTR tmp_bf_ptr
; /* Generic temporary for use
439 /* The following function simply enters a given common block onto
440 the global common block chain. */
443 add_common_block (char *name
, CORE_ADDR offset
, int secnum
, char *func_stab
)
445 SAVED_F77_COMMON_PTR tmp
;
446 char *c
, *local_copy_func_stab
;
448 /* If the COMMON block we are trying to add has a blank
449 name (i.e. "#BLNK_COM") then we set it to __BLANK
450 because the darn "#" character makes GDB's input
454 if (strcmp (name
, BLANK_COMMON_NAME_ORIGINAL
) == 0
455 || strcmp (name
, BLANK_COMMON_NAME_MF77
) == 0)
459 name
= alloca (strlen (BLANK_COMMON_NAME_LOCAL
) + 1);
460 strcpy (name
, BLANK_COMMON_NAME_LOCAL
);
463 tmp
= allocate_saved_f77_common_node ();
465 local_copy_func_stab
= xmalloc (strlen (func_stab
) + 1);
466 strcpy (local_copy_func_stab
, func_stab
);
468 tmp
->name
= xmalloc (strlen (name
) + 1);
470 /* local_copy_func_stab is a stabstring, let us first extract the
471 function name from the stab by NULLing out the ':' character. */
475 c
= strchr (local_copy_func_stab
, ':');
480 error (_("Malformed function STAB found in add_common_block()"));
483 tmp
->owning_function
= xmalloc (strlen (local_copy_func_stab
) + 1);
485 strcpy (tmp
->owning_function
, local_copy_func_stab
);
487 strcpy (tmp
->name
, name
);
488 tmp
->offset
= offset
;
491 tmp
->secnum
= secnum
;
493 current_common
= tmp
;
495 if (head_common_list
== NULL
)
497 head_common_list
= tail_common_list
= tmp
;
501 tail_common_list
->next
= tmp
;
502 tail_common_list
= tmp
;
507 /* The following function simply enters a given common entry onto
508 the "current_common" block that has been saved away. */
512 add_common_entry (struct symbol
*entry_sym_ptr
)
514 COMMON_ENTRY_PTR tmp
;
518 /* The order of this list is important, since
519 we expect the entries to appear in decl.
520 order when we later issue "info common" calls. */
522 tmp
= allocate_common_entry_node ();
525 tmp
->symbol
= entry_sym_ptr
;
527 if (current_common
== NULL
)
528 error (_("Attempt to add COMMON entry with no block open!"));
531 if (current_common
->entries
== NULL
)
533 current_common
->entries
= tmp
;
534 current_common
->end_of_entries
= tmp
;
538 current_common
->end_of_entries
->next
= tmp
;
539 current_common
->end_of_entries
= tmp
;
545 /* This routine finds the first encountred COMMON block named "name". */
548 static SAVED_F77_COMMON_PTR
549 find_first_common_named (char *name
)
552 SAVED_F77_COMMON_PTR tmp
;
554 tmp
= head_common_list
;
558 if (strcmp (tmp
->name
, name
) == 0)
567 /* This routine finds the first encountred COMMON block named "name"
568 that belongs to function funcname. */
571 find_common_for_function (const char *name
, const char *funcname
)
574 SAVED_F77_COMMON_PTR tmp
;
576 tmp
= head_common_list
;
580 if (strcmp (tmp
->name
, name
) == 0
581 && strcmp (tmp
->owning_function
, funcname
) == 0)
592 /* The following function is called to patch up the offsets
593 for the statics contained in the COMMON block named
597 patch_common_entries (SAVED_F77_COMMON_PTR blk
, CORE_ADDR offset
, int secnum
)
599 COMMON_ENTRY_PTR entry
;
601 blk
->offset
= offset
; /* Keep this around for future use. */
603 entry
= blk
->entries
;
605 while (entry
!= NULL
)
607 SYMBOL_VALUE (entry
->symbol
) += offset
;
608 SYMBOL_SECTION (entry
->symbol
) = secnum
;
612 blk
->secnum
= secnum
;
615 /* Patch all commons named "name" that need patching.Since COMMON
616 blocks occur with relative infrequency, we simply do a linear scan on
617 the name. Eventually, the best way to do this will be a
618 hashed-lookup. Secnum is the section number for the .bss section
619 (which is where common data lives). */
622 patch_all_commons_by_name (char *name
, CORE_ADDR offset
, int secnum
)
625 SAVED_F77_COMMON_PTR tmp
;
627 /* For blank common blocks, change the canonical reprsentation
630 if (strcmp (name
, BLANK_COMMON_NAME_ORIGINAL
) == 0
631 || strcmp (name
, BLANK_COMMON_NAME_MF77
) == 0)
634 name
= alloca (strlen (BLANK_COMMON_NAME_LOCAL
) + 1);
635 strcpy (name
, BLANK_COMMON_NAME_LOCAL
);
638 tmp
= head_common_list
;
642 if (COMMON_NEEDS_PATCHING (tmp
))
643 if (strcmp (tmp
->name
, name
) == 0)
644 patch_common_entries (tmp
, offset
, secnum
);
651 /* This macro adds the symbol-number for the start of the function
652 (the symbol number of the .bf) referenced by symnum_fcn to a
653 list. This list, in reality should be a FIFO queue but since
654 #line pragmas sometimes cause line ranges to get messed up
655 we simply create a linear list. This list can then be searched
656 first by a queueing algorithm and upon failure fall back to
660 #define ADD_BF_SYMNUM(bf_sym,fcn_sym) \
662 if (saved_bf_list == NULL) \
664 tmp_bf_ptr = allocate_saved_bf_node(); \
666 tmp_bf_ptr->symnum_bf = (bf_sym); \
667 tmp_bf_ptr->symnum_fcn = (fcn_sym); \
668 tmp_bf_ptr->next = NULL; \
670 current_head_bf_list = saved_bf_list = tmp_bf_ptr; \
671 saved_bf_list_end = tmp_bf_ptr; \
675 tmp_bf_ptr = allocate_saved_bf_node(); \
677 tmp_bf_ptr->symnum_bf = (bf_sym); \
678 tmp_bf_ptr->symnum_fcn = (fcn_sym); \
679 tmp_bf_ptr->next = NULL; \
681 saved_bf_list_end->next = tmp_bf_ptr; \
682 saved_bf_list_end = tmp_bf_ptr; \
686 /* This function frees the entire (.bf,function) list. */
693 SAVED_BF_PTR tmp
= saved_bf_list
;
694 SAVED_BF_PTR next
= NULL
;
702 saved_bf_list
= NULL
;
706 int global_remote_debug
;
711 get_bf_for_fcn (long the_function
)
716 /* First use a simple queuing algorithm (i.e. look and see if the
717 item at the head of the queue is the one you want). */
719 if (saved_bf_list
== NULL
)
720 internal_error (__FILE__
, __LINE__
,
721 _("cannot get .bf node off empty list"));
723 if (current_head_bf_list
!= NULL
)
724 if (current_head_bf_list
->symnum_fcn
== the_function
)
726 if (global_remote_debug
)
727 fprintf_unfiltered (gdb_stderr
, "*");
729 tmp
= current_head_bf_list
;
730 current_head_bf_list
= current_head_bf_list
->next
;
731 return (tmp
->symnum_bf
);
734 /* If the above did not work (probably because #line directives were
735 used in the sourcefile and they messed up our internal tables) we now do
736 the ugly linear scan. */
738 if (global_remote_debug
)
739 fprintf_unfiltered (gdb_stderr
, "\ndefaulting to linear scan\n");
746 if (tmp
->symnum_fcn
== the_function
)
748 if (global_remote_debug
)
749 fprintf_unfiltered (gdb_stderr
, "Found in %d probes\n", nprobes
);
750 current_head_bf_list
= tmp
->next
;
751 return (tmp
->symnum_bf
);
759 static SAVED_FUNCTION_PTR saved_function_list
= NULL
;
760 static SAVED_FUNCTION_PTR saved_function_list_end
= NULL
;
763 clear_function_list (void)
765 SAVED_FUNCTION_PTR tmp
= saved_function_list
;
766 SAVED_FUNCTION_PTR next
= NULL
;
775 saved_function_list
= NULL
;