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"
38 /* Following is dubious stuff that had been in the xcoff reader. */
42 long line_offset; /* Line offset for function. */
43 struct saved_fcn *next;
47 struct saved_bf_symnum
49 long symnum_fcn; /* Symnum of function (i.e. .function
51 long symnum_bf; /* Symnum of .bf for this function. */
52 struct saved_bf_symnum *next;
55 typedef struct saved_fcn SAVED_FUNCTION, *SAVED_FUNCTION_PTR;
56 typedef struct saved_bf_symnum SAVED_BF, *SAVED_BF_PTR;
60 extern void _initialize_f_language (void);
62 static void clear_function_list (void);
63 static long get_bf_for_fcn (long);
64 static void clear_bf_list (void);
65 static void patch_all_commons_by_name (char *, CORE_ADDR, int);
66 static SAVED_F77_COMMON_PTR find_first_common_named (char *);
67 static void add_common_entry (struct symbol *);
68 static void add_common_block (char *, CORE_ADDR, int, char *);
69 static SAVED_FUNCTION *allocate_saved_function_node (void);
70 static SAVED_BF_PTR allocate_saved_bf_node (void);
71 static COMMON_ENTRY_PTR allocate_common_entry_node (void);
72 static SAVED_F77_COMMON_PTR allocate_saved_f77_common_node (void);
73 static void patch_common_entries (SAVED_F77_COMMON_PTR, CORE_ADDR, int);
76 static void f_printchar (int c, struct type *type, struct ui_file * stream);
77 static void f_emit_char (int c, struct type *type,
78 struct ui_file * stream, int quoter);
80 /* Return the encoding that should be used for the character type
84 f_get_encoding (struct type *type)
88 switch (TYPE_LENGTH (type))
91 encoding = target_charset (get_type_arch (type));
94 if (gdbarch_byte_order (get_type_arch (type)) == BFD_ENDIAN_BIG)
95 encoding = "UTF-32BE";
97 encoding = "UTF-32LE";
101 error (_("unrecognized character type"));
107 /* Print the character C on STREAM as part of the contents of a literal
108 string whose delimiter is QUOTER. Note that that format for printing
109 characters and strings is language specific.
110 FIXME: This is a copy of the same function from c-exp.y. It should
111 be replaced with a true F77 version. */
114 f_emit_char (int c, struct type *type, struct ui_file *stream, int quoter)
116 const char *encoding = f_get_encoding (type);
118 generic_emit_char (c, type, stream, quoter, encoding);
121 /* Implementation of la_printchar. */
124 f_printchar (int c, struct type *type, struct ui_file *stream)
126 fputs_filtered ("'", stream);
127 LA_EMIT_CHAR (c, type, stream, '\'');
128 fputs_filtered ("'", stream);
131 /* Print the character string STRING, printing at most LENGTH characters.
132 Printing stops early if the number hits print_max; repeat counts
133 are printed as appropriate. Print ellipses at the end if we
134 had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
135 FIXME: This is a copy of the same function from c-exp.y. It should
136 be replaced with a true F77 version. */
139 f_printstr (struct ui_file *stream, struct type *type, const gdb_byte *string,
140 unsigned int length, const char *encoding, int force_ellipses,
141 const struct value_print_options *options)
143 const char *type_encoding = f_get_encoding (type);
145 if (TYPE_LENGTH (type) == 4)
146 fputs_filtered ("4_", stream);
148 if (!encoding || !*encoding)
149 encoding = type_encoding;
151 generic_printstr (stream, type, string, length, encoding,
152 force_ellipses, '\'', 0, options);
156 /* Table of operators and their precedences for printing expressions. */
158 static const struct op_print f_op_print_tab[] =
160 {"+", BINOP_ADD, PREC_ADD, 0},
161 {"+", UNOP_PLUS, PREC_PREFIX, 0},
162 {"-", BINOP_SUB, PREC_ADD, 0},
163 {"-", UNOP_NEG, PREC_PREFIX, 0},
164 {"*", BINOP_MUL, PREC_MUL, 0},
165 {"/", BINOP_DIV, PREC_MUL, 0},
166 {"DIV", BINOP_INTDIV, PREC_MUL, 0},
167 {"MOD", BINOP_REM, PREC_MUL, 0},
168 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
169 {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
170 {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
171 {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
172 {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
173 {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
174 {".LE.", BINOP_LEQ, PREC_ORDER, 0},
175 {".GE.", BINOP_GEQ, PREC_ORDER, 0},
176 {".GT.", BINOP_GTR, PREC_ORDER, 0},
177 {".LT.", BINOP_LESS, PREC_ORDER, 0},
178 {"**", UNOP_IND, PREC_PREFIX, 0},
179 {"@", BINOP_REPEAT, PREC_REPEAT, 0},
183 enum f_primitive_types {
184 f_primitive_type_character,
185 f_primitive_type_logical,
186 f_primitive_type_logical_s1,
187 f_primitive_type_logical_s2,
188 f_primitive_type_logical_s8,
189 f_primitive_type_integer,
190 f_primitive_type_integer_s2,
191 f_primitive_type_real,
192 f_primitive_type_real_s8,
193 f_primitive_type_real_s16,
194 f_primitive_type_complex_s8,
195 f_primitive_type_complex_s16,
196 f_primitive_type_void,
201 f_language_arch_info (struct gdbarch *gdbarch,
202 struct language_arch_info *lai)
204 const struct builtin_f_type *builtin = builtin_f_type (gdbarch);
206 lai->string_char_type = builtin->builtin_character;
207 lai->primitive_type_vector
208 = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_f_primitive_types + 1,
211 lai->primitive_type_vector [f_primitive_type_character]
212 = builtin->builtin_character;
213 lai->primitive_type_vector [f_primitive_type_logical]
214 = builtin->builtin_logical;
215 lai->primitive_type_vector [f_primitive_type_logical_s1]
216 = builtin->builtin_logical_s1;
217 lai->primitive_type_vector [f_primitive_type_logical_s2]
218 = builtin->builtin_logical_s2;
219 lai->primitive_type_vector [f_primitive_type_logical_s8]
220 = builtin->builtin_logical_s8;
221 lai->primitive_type_vector [f_primitive_type_real]
222 = builtin->builtin_real;
223 lai->primitive_type_vector [f_primitive_type_real_s8]
224 = builtin->builtin_real_s8;
225 lai->primitive_type_vector [f_primitive_type_real_s16]
226 = builtin->builtin_real_s16;
227 lai->primitive_type_vector [f_primitive_type_complex_s8]
228 = builtin->builtin_complex_s8;
229 lai->primitive_type_vector [f_primitive_type_complex_s16]
230 = builtin->builtin_complex_s16;
231 lai->primitive_type_vector [f_primitive_type_void]
232 = builtin->builtin_void;
234 lai->bool_type_symbol = "logical";
235 lai->bool_type_default = builtin->builtin_logical_s2;
238 /* Remove the modules separator :: from the default break list. */
241 f_word_break_characters (void)
249 retval = xstrdup (default_word_break_characters ());
250 s = strchr (retval, ':');
253 char *last_char = &s[strlen (s) - 1];
262 /* Consider the modules separator :: as a valid symbol name character
266 f_make_symbol_completion_list (char *text, char *word)
268 return default_make_symbol_completion_list_break_on (text, word, ":");
271 /* This is declared in c-lang.h but it is silly to import that file for what
272 is already just a hack. */
273 extern int c_value_print (struct value *, struct ui_file *,
274 const struct value_print_options *);
276 const struct language_defn f_language_defn =
285 &exp_descriptor_standard,
286 f_parse, /* parser */
287 f_error, /* parser error function */
289 f_printchar, /* Print character constant */
290 f_printstr, /* function to print string constant */
291 f_emit_char, /* Function to print a single character */
292 f_print_type, /* Print a type using appropriate syntax */
293 default_print_typedef, /* Print a typedef using appropriate syntax */
294 f_val_print, /* Print a value using appropriate syntax */
295 c_value_print, /* FIXME */
296 NULL, /* Language specific skip_trampoline */
297 NULL, /* name_of_this */
298 cp_lookup_symbol_nonlocal, /* lookup_symbol_nonlocal */
299 basic_lookup_transparent_type,/* lookup_transparent_type */
300 NULL, /* Language specific symbol demangler */
301 NULL, /* Language specific
302 class_name_from_physname */
303 f_op_print_tab, /* expression operators for printing */
304 0, /* arrays are first-class (not c-style) */
305 1, /* String lower bound */
306 f_word_break_characters,
307 f_make_symbol_completion_list,
308 f_language_arch_info,
309 default_print_array_index,
310 default_pass_by_reference,
312 NULL, /* la_get_symbol_name_match_p */
313 iterate_over_symbols,
318 build_fortran_types (struct gdbarch *gdbarch)
320 struct builtin_f_type *builtin_f_type
321 = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_f_type);
323 builtin_f_type->builtin_void
324 = arch_type (gdbarch, TYPE_CODE_VOID, 1, "VOID");
326 builtin_f_type->builtin_character
327 = arch_integer_type (gdbarch, TARGET_CHAR_BIT, 0, "character");
329 builtin_f_type->builtin_logical_s1
330 = arch_boolean_type (gdbarch, TARGET_CHAR_BIT, 1, "logical*1");
332 builtin_f_type->builtin_integer_s2
333 = arch_integer_type (gdbarch, gdbarch_short_bit (gdbarch), 0,
336 builtin_f_type->builtin_logical_s2
337 = arch_boolean_type (gdbarch, gdbarch_short_bit (gdbarch), 1,
340 builtin_f_type->builtin_logical_s8
341 = arch_boolean_type (gdbarch, gdbarch_long_long_bit (gdbarch), 1,
344 builtin_f_type->builtin_integer
345 = arch_integer_type (gdbarch, gdbarch_int_bit (gdbarch), 0,
348 builtin_f_type->builtin_logical
349 = arch_boolean_type (gdbarch, gdbarch_int_bit (gdbarch), 1,
352 builtin_f_type->builtin_real
353 = arch_float_type (gdbarch, gdbarch_float_bit (gdbarch),
355 builtin_f_type->builtin_real_s8
356 = arch_float_type (gdbarch, gdbarch_double_bit (gdbarch),
358 builtin_f_type->builtin_real_s16
359 = arch_float_type (gdbarch, gdbarch_long_double_bit (gdbarch),
362 builtin_f_type->builtin_complex_s8
363 = arch_complex_type (gdbarch, "complex*8",
364 builtin_f_type->builtin_real);
365 builtin_f_type->builtin_complex_s16
366 = arch_complex_type (gdbarch, "complex*16",
367 builtin_f_type->builtin_real_s8);
368 builtin_f_type->builtin_complex_s32
369 = arch_complex_type (gdbarch, "complex*32",
370 builtin_f_type->builtin_real_s16);
372 return builtin_f_type;
375 static struct gdbarch_data *f_type_data;
377 const struct builtin_f_type *
378 builtin_f_type (struct gdbarch *gdbarch)
380 return gdbarch_data (gdbarch, f_type_data);
384 _initialize_f_language (void)
386 f_type_data = gdbarch_data_register_post_init (build_fortran_types);
388 add_language (&f_language_defn);
393 allocate_saved_bf_node (void)
397 new = (SAVED_BF_PTR) xmalloc (sizeof (SAVED_BF));
401 static SAVED_FUNCTION *
402 allocate_saved_function_node (void)
406 new = (SAVED_FUNCTION *) xmalloc (sizeof (SAVED_FUNCTION));
410 static SAVED_F77_COMMON_PTR
411 allocate_saved_f77_common_node (void)
413 SAVED_F77_COMMON_PTR new;
415 new = (SAVED_F77_COMMON_PTR) xmalloc (sizeof (SAVED_F77_COMMON));
419 static COMMON_ENTRY_PTR
420 allocate_common_entry_node (void)
422 COMMON_ENTRY_PTR new;
424 new = (COMMON_ENTRY_PTR) xmalloc (sizeof (COMMON_ENTRY));
429 SAVED_F77_COMMON_PTR head_common_list = NULL; /* Ptr to 1st saved COMMON */
430 SAVED_F77_COMMON_PTR tail_common_list = NULL; /* Ptr to last saved COMMON */
431 SAVED_F77_COMMON_PTR current_common = NULL; /* Ptr to current COMMON */
434 static SAVED_BF_PTR saved_bf_list = NULL; /* Ptr to (.bf,function)
436 static SAVED_BF_PTR saved_bf_list_end = NULL; /* Ptr to above list's end */
437 static SAVED_BF_PTR current_head_bf_list = NULL; /* Current head of
440 static SAVED_BF_PTR tmp_bf_ptr; /* Generic temporary for use
443 /* The following function simply enters a given common block onto
444 the global common block chain. */
447 add_common_block (char *name, CORE_ADDR offset, int secnum, char *func_stab)
449 SAVED_F77_COMMON_PTR tmp;
450 char *c, *local_copy_func_stab;
452 /* If the COMMON block we are trying to add has a blank
453 name (i.e. "#BLNK_COM") then we set it to __BLANK
454 because the darn "#" character makes GDB's input
458 if (strcmp (name, BLANK_COMMON_NAME_ORIGINAL) == 0
459 || strcmp (name, BLANK_COMMON_NAME_MF77) == 0)
463 name = alloca (strlen (BLANK_COMMON_NAME_LOCAL) + 1);
464 strcpy (name, BLANK_COMMON_NAME_LOCAL);
467 tmp = allocate_saved_f77_common_node ();
469 local_copy_func_stab = xmalloc (strlen (func_stab) + 1);
470 strcpy (local_copy_func_stab, func_stab);
472 tmp->name = xmalloc (strlen (name) + 1);
474 /* local_copy_func_stab is a stabstring, let us first extract the
475 function name from the stab by NULLing out the ':' character. */
479 c = strchr (local_copy_func_stab, ':');
484 error (_("Malformed function STAB found in add_common_block()"));
487 tmp->owning_function = xmalloc (strlen (local_copy_func_stab) + 1);
489 strcpy (tmp->owning_function, local_copy_func_stab);
491 strcpy (tmp->name, name);
492 tmp->offset = offset;
495 tmp->secnum = secnum;
497 current_common = tmp;
499 if (head_common_list == NULL)
501 head_common_list = tail_common_list = tmp;
505 tail_common_list->next = tmp;
506 tail_common_list = tmp;
511 /* The following function simply enters a given common entry onto
512 the "current_common" block that has been saved away. */
516 add_common_entry (struct symbol *entry_sym_ptr)
518 COMMON_ENTRY_PTR tmp;
522 /* The order of this list is important, since
523 we expect the entries to appear in decl.
524 order when we later issue "info common" calls. */
526 tmp = allocate_common_entry_node ();
529 tmp->symbol = entry_sym_ptr;
531 if (current_common == NULL)
532 error (_("Attempt to add COMMON entry with no block open!"));
535 if (current_common->entries == NULL)
537 current_common->entries = tmp;
538 current_common->end_of_entries = tmp;
542 current_common->end_of_entries->next = tmp;
543 current_common->end_of_entries = tmp;
549 /* This routine finds the first encountred COMMON block named "name". */
552 static SAVED_F77_COMMON_PTR
553 find_first_common_named (char *name)
556 SAVED_F77_COMMON_PTR tmp;
558 tmp = head_common_list;
562 if (strcmp (tmp->name, name) == 0)
571 /* This routine finds the first encountred COMMON block named "name"
572 that belongs to function funcname. */
575 find_common_for_function (char *name, char *funcname)
578 SAVED_F77_COMMON_PTR tmp;
580 tmp = head_common_list;
584 if (strcmp (tmp->name, name) == 0
585 && strcmp (tmp->owning_function, funcname) == 0)
596 /* The following function is called to patch up the offsets
597 for the statics contained in the COMMON block named
601 patch_common_entries (SAVED_F77_COMMON_PTR blk, CORE_ADDR offset, int secnum)
603 COMMON_ENTRY_PTR entry;
605 blk->offset = offset; /* Keep this around for future use. */
607 entry = blk->entries;
609 while (entry != NULL)
611 SYMBOL_VALUE (entry->symbol) += offset;
612 SYMBOL_SECTION (entry->symbol) = secnum;
616 blk->secnum = secnum;
619 /* Patch all commons named "name" that need patching.Since COMMON
620 blocks occur with relative infrequency, we simply do a linear scan on
621 the name. Eventually, the best way to do this will be a
622 hashed-lookup. Secnum is the section number for the .bss section
623 (which is where common data lives). */
626 patch_all_commons_by_name (char *name, CORE_ADDR offset, int secnum)
629 SAVED_F77_COMMON_PTR tmp;
631 /* For blank common blocks, change the canonical reprsentation
634 if (strcmp (name, BLANK_COMMON_NAME_ORIGINAL) == 0
635 || strcmp (name, BLANK_COMMON_NAME_MF77) == 0)
638 name = alloca (strlen (BLANK_COMMON_NAME_LOCAL) + 1);
639 strcpy (name, BLANK_COMMON_NAME_LOCAL);
642 tmp = head_common_list;
646 if (COMMON_NEEDS_PATCHING (tmp))
647 if (strcmp (tmp->name, name) == 0)
648 patch_common_entries (tmp, offset, secnum);
655 /* This macro adds the symbol-number for the start of the function
656 (the symbol number of the .bf) referenced by symnum_fcn to a
657 list. This list, in reality should be a FIFO queue but since
658 #line pragmas sometimes cause line ranges to get messed up
659 we simply create a linear list. This list can then be searched
660 first by a queueing algorithm and upon failure fall back to
664 #define ADD_BF_SYMNUM(bf_sym,fcn_sym) \
666 if (saved_bf_list == NULL) \
668 tmp_bf_ptr = allocate_saved_bf_node(); \
670 tmp_bf_ptr->symnum_bf = (bf_sym); \
671 tmp_bf_ptr->symnum_fcn = (fcn_sym); \
672 tmp_bf_ptr->next = NULL; \
674 current_head_bf_list = saved_bf_list = tmp_bf_ptr; \
675 saved_bf_list_end = tmp_bf_ptr; \
679 tmp_bf_ptr = allocate_saved_bf_node(); \
681 tmp_bf_ptr->symnum_bf = (bf_sym); \
682 tmp_bf_ptr->symnum_fcn = (fcn_sym); \
683 tmp_bf_ptr->next = NULL; \
685 saved_bf_list_end->next = tmp_bf_ptr; \
686 saved_bf_list_end = tmp_bf_ptr; \
690 /* This function frees the entire (.bf,function) list. */
697 SAVED_BF_PTR tmp = saved_bf_list;
698 SAVED_BF_PTR next = NULL;
706 saved_bf_list = NULL;
710 int global_remote_debug;
715 get_bf_for_fcn (long the_function)
720 /* First use a simple queuing algorithm (i.e. look and see if the
721 item at the head of the queue is the one you want). */
723 if (saved_bf_list == NULL)
724 internal_error (__FILE__, __LINE__,
725 _("cannot get .bf node off empty list"));
727 if (current_head_bf_list != NULL)
728 if (current_head_bf_list->symnum_fcn == the_function)
730 if (global_remote_debug)
731 fprintf_unfiltered (gdb_stderr, "*");
733 tmp = current_head_bf_list;
734 current_head_bf_list = current_head_bf_list->next;
735 return (tmp->symnum_bf);
738 /* If the above did not work (probably because #line directives were
739 used in the sourcefile and they messed up our internal tables) we now do
740 the ugly linear scan. */
742 if (global_remote_debug)
743 fprintf_unfiltered (gdb_stderr, "\ndefaulting to linear scan\n");
750 if (tmp->symnum_fcn == the_function)
752 if (global_remote_debug)
753 fprintf_unfiltered (gdb_stderr, "Found in %d probes\n", nprobes);
754 current_head_bf_list = tmp->next;
755 return (tmp->symnum_bf);
763 static SAVED_FUNCTION_PTR saved_function_list = NULL;
764 static SAVED_FUNCTION_PTR saved_function_list_end = NULL;
767 clear_function_list (void)
769 SAVED_FUNCTION_PTR tmp = saved_function_list;
770 SAVED_FUNCTION_PTR next = NULL;
779 saved_function_list = NULL;