2000-03-21 J.T. Conklin <jtc@redback.com>
[platform/upstream/binutils.git] / gdb / f-lang.c
1 /* Fortran language support routines for GDB, the GNU debugger.
2    Copyright 1993, 1994, 1996, 2000 Free Software Foundation, Inc.
3    Contributed by Motorola.  Adapted from the C parser by Farooq Butt
4    (fmbutt@engage.sps.mot.com).
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "gdb_string.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "parser-defs.h"
29 #include "language.h"
30 #include "f-lang.h"
31 #include "valprint.h"
32
33 /* The built-in types of F77.  FIXME: integer*4 is missing, plain
34    logical is missing (builtin_type_logical is logical*4).  */
35
36 struct type *builtin_type_f_character;
37 struct type *builtin_type_f_logical;
38 struct type *builtin_type_f_logical_s1;
39 struct type *builtin_type_f_logical_s2;
40 struct type *builtin_type_f_integer;
41 struct type *builtin_type_f_integer_s2;
42 struct type *builtin_type_f_real;
43 struct type *builtin_type_f_real_s8;
44 struct type *builtin_type_f_real_s16;
45 struct type *builtin_type_f_complex_s8;
46 struct type *builtin_type_f_complex_s16;
47 struct type *builtin_type_f_complex_s32;
48 struct type *builtin_type_f_void;
49
50 /* Following is dubious stuff that had been in the xcoff reader. */
51
52 struct saved_fcn
53   {
54     long line_offset;           /* Line offset for function */
55     struct saved_fcn *next;
56   };
57
58
59 struct saved_bf_symnum
60   {
61     long symnum_fcn;            /* Symnum of function (i.e. .function directive) */
62     long symnum_bf;             /* Symnum of .bf for this function */
63     struct saved_bf_symnum *next;
64   };
65
66 typedef struct saved_fcn SAVED_FUNCTION, *SAVED_FUNCTION_PTR;
67 typedef struct saved_bf_symnum SAVED_BF, *SAVED_BF_PTR;
68
69 /* Local functions */
70
71 extern void _initialize_f_language PARAMS ((void));
72 #if 0
73 static void clear_function_list PARAMS ((void));
74 static long get_bf_for_fcn PARAMS ((long));
75 static void clear_bf_list PARAMS ((void));
76 static void patch_all_commons_by_name PARAMS ((char *, CORE_ADDR, int));
77 static SAVED_F77_COMMON_PTR find_first_common_named PARAMS ((char *));
78 static void add_common_entry PARAMS ((struct symbol *));
79 static void add_common_block PARAMS ((char *, CORE_ADDR, int, char *));
80 static SAVED_FUNCTION *allocate_saved_function_node PARAMS ((void));
81 static SAVED_BF_PTR allocate_saved_bf_node PARAMS ((void));
82 static COMMON_ENTRY_PTR allocate_common_entry_node PARAMS ((void));
83 static SAVED_F77_COMMON_PTR allocate_saved_f77_common_node PARAMS ((void));
84 static void patch_common_entries PARAMS ((SAVED_F77_COMMON_PTR, CORE_ADDR, int));
85 #endif
86
87 static struct type *f_create_fundamental_type PARAMS ((struct objfile *, int));
88 static void f_printstr (struct ui_file * stream, char *string,
89                         unsigned int length, int width,
90                         int force_ellipses);
91 static void f_printchar (int c, struct ui_file * stream);
92 static void f_emit_char (int c, struct ui_file * stream, int quoter);
93
94 /* Print the character C on STREAM as part of the contents of a literal
95    string whose delimiter is QUOTER.  Note that that format for printing
96    characters and strings is language specific.
97    FIXME:  This is a copy of the same function from c-exp.y.  It should
98    be replaced with a true F77 version.  */
99
100 static void
101 f_emit_char (c, stream, quoter)
102      register int c;
103      struct ui_file *stream;
104      int quoter;
105 {
106   c &= 0xFF;                    /* Avoid sign bit follies */
107
108   if (PRINT_LITERAL_FORM (c))
109     {
110       if (c == '\\' || c == quoter)
111         fputs_filtered ("\\", stream);
112       fprintf_filtered (stream, "%c", c);
113     }
114   else
115     {
116       switch (c)
117         {
118         case '\n':
119           fputs_filtered ("\\n", stream);
120           break;
121         case '\b':
122           fputs_filtered ("\\b", stream);
123           break;
124         case '\t':
125           fputs_filtered ("\\t", stream);
126           break;
127         case '\f':
128           fputs_filtered ("\\f", stream);
129           break;
130         case '\r':
131           fputs_filtered ("\\r", stream);
132           break;
133         case '\033':
134           fputs_filtered ("\\e", stream);
135           break;
136         case '\007':
137           fputs_filtered ("\\a", stream);
138           break;
139         default:
140           fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
141           break;
142         }
143     }
144 }
145
146 /* FIXME:  This is a copy of the same function from c-exp.y.  It should
147    be replaced with a true F77version. */
148
149 static void
150 f_printchar (c, stream)
151      int c;
152      struct ui_file *stream;
153 {
154   fputs_filtered ("'", stream);
155   LA_EMIT_CHAR (c, stream, '\'');
156   fputs_filtered ("'", stream);
157 }
158
159 /* Print the character string STRING, printing at most LENGTH characters.
160    Printing stops early if the number hits print_max; repeat counts
161    are printed as appropriate.  Print ellipses at the end if we
162    had to stop before printing LENGTH characters, or if FORCE_ELLIPSES.
163    FIXME:  This is a copy of the same function from c-exp.y.  It should
164    be replaced with a true F77 version. */
165
166 static void
167 f_printstr (stream, string, length, width, force_ellipses)
168      struct ui_file *stream;
169      char *string;
170      unsigned int length;
171      int width;
172      int force_ellipses;
173 {
174   register unsigned int i;
175   unsigned int things_printed = 0;
176   int in_quotes = 0;
177   int need_comma = 0;
178   extern int inspect_it;
179
180   if (length == 0)
181     {
182       fputs_filtered ("''", gdb_stdout);
183       return;
184     }
185
186   for (i = 0; i < length && things_printed < print_max; ++i)
187     {
188       /* Position of the character we are examining
189          to see whether it is repeated.  */
190       unsigned int rep1;
191       /* Number of repetitions we have detected so far.  */
192       unsigned int reps;
193
194       QUIT;
195
196       if (need_comma)
197         {
198           fputs_filtered (", ", stream);
199           need_comma = 0;
200         }
201
202       rep1 = i + 1;
203       reps = 1;
204       while (rep1 < length && string[rep1] == string[i])
205         {
206           ++rep1;
207           ++reps;
208         }
209
210       if (reps > repeat_count_threshold)
211         {
212           if (in_quotes)
213             {
214               if (inspect_it)
215                 fputs_filtered ("\\', ", stream);
216               else
217                 fputs_filtered ("', ", stream);
218               in_quotes = 0;
219             }
220           f_printchar (string[i], stream);
221           fprintf_filtered (stream, " <repeats %u times>", reps);
222           i = rep1 - 1;
223           things_printed += repeat_count_threshold;
224           need_comma = 1;
225         }
226       else
227         {
228           if (!in_quotes)
229             {
230               if (inspect_it)
231                 fputs_filtered ("\\'", stream);
232               else
233                 fputs_filtered ("'", stream);
234               in_quotes = 1;
235             }
236           LA_EMIT_CHAR (string[i], stream, '"');
237           ++things_printed;
238         }
239     }
240
241   /* Terminate the quotes if necessary.  */
242   if (in_quotes)
243     {
244       if (inspect_it)
245         fputs_filtered ("\\'", stream);
246       else
247         fputs_filtered ("'", stream);
248     }
249
250   if (force_ellipses || i < length)
251     fputs_filtered ("...", stream);
252 }
253
254 /* FIXME:  This is a copy of c_create_fundamental_type(), before
255    all the non-C types were stripped from it.  Needs to be fixed
256    by an experienced F77 programmer. */
257
258 static struct type *
259 f_create_fundamental_type (objfile, typeid)
260      struct objfile *objfile;
261      int typeid;
262 {
263   register struct type *type = NULL;
264
265   switch (typeid)
266     {
267     case FT_VOID:
268       type = init_type (TYPE_CODE_VOID,
269                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
270                         0, "VOID", objfile);
271       break;
272     case FT_BOOLEAN:
273       type = init_type (TYPE_CODE_BOOL,
274                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
275                         TYPE_FLAG_UNSIGNED, "boolean", objfile);
276       break;
277     case FT_STRING:
278       type = init_type (TYPE_CODE_STRING,
279                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
280                         0, "string", objfile);
281       break;
282     case FT_CHAR:
283       type = init_type (TYPE_CODE_INT,
284                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
285                         0, "character", objfile);
286       break;
287     case FT_SIGNED_CHAR:
288       type = init_type (TYPE_CODE_INT,
289                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
290                         0, "integer*1", objfile);
291       break;
292     case FT_UNSIGNED_CHAR:
293       type = init_type (TYPE_CODE_BOOL,
294                         TARGET_CHAR_BIT / TARGET_CHAR_BIT,
295                         TYPE_FLAG_UNSIGNED, "logical*1", objfile);
296       break;
297     case FT_SHORT:
298       type = init_type (TYPE_CODE_INT,
299                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
300                         0, "integer*2", objfile);
301       break;
302     case FT_SIGNED_SHORT:
303       type = init_type (TYPE_CODE_INT,
304                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
305                         0, "short", objfile);   /* FIXME-fnf */
306       break;
307     case FT_UNSIGNED_SHORT:
308       type = init_type (TYPE_CODE_BOOL,
309                         TARGET_SHORT_BIT / TARGET_CHAR_BIT,
310                         TYPE_FLAG_UNSIGNED, "logical*2", objfile);
311       break;
312     case FT_INTEGER:
313       type = init_type (TYPE_CODE_INT,
314                         TARGET_INT_BIT / TARGET_CHAR_BIT,
315                         0, "integer*4", objfile);
316       break;
317     case FT_SIGNED_INTEGER:
318       type = init_type (TYPE_CODE_INT,
319                         TARGET_INT_BIT / TARGET_CHAR_BIT,
320                         0, "integer", objfile);         /* FIXME -fnf */
321       break;
322     case FT_UNSIGNED_INTEGER:
323       type = init_type (TYPE_CODE_BOOL,
324                         TARGET_INT_BIT / TARGET_CHAR_BIT,
325                         TYPE_FLAG_UNSIGNED, "logical*4", objfile);
326       break;
327     case FT_FIXED_DECIMAL:
328       type = init_type (TYPE_CODE_INT,
329                         TARGET_INT_BIT / TARGET_CHAR_BIT,
330                         0, "fixed decimal", objfile);
331       break;
332     case FT_LONG:
333       type = init_type (TYPE_CODE_INT,
334                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
335                         0, "long", objfile);
336       break;
337     case FT_SIGNED_LONG:
338       type = init_type (TYPE_CODE_INT,
339                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
340                         0, "long", objfile);    /* FIXME -fnf */
341       break;
342     case FT_UNSIGNED_LONG:
343       type = init_type (TYPE_CODE_INT,
344                         TARGET_LONG_BIT / TARGET_CHAR_BIT,
345                         TYPE_FLAG_UNSIGNED, "unsigned long", objfile);
346       break;
347     case FT_LONG_LONG:
348       type = init_type (TYPE_CODE_INT,
349                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
350                         0, "long long", objfile);
351       break;
352     case FT_SIGNED_LONG_LONG:
353       type = init_type (TYPE_CODE_INT,
354                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
355                         0, "signed long long", objfile);
356       break;
357     case FT_UNSIGNED_LONG_LONG:
358       type = init_type (TYPE_CODE_INT,
359                         TARGET_LONG_LONG_BIT / TARGET_CHAR_BIT,
360                         TYPE_FLAG_UNSIGNED, "unsigned long long", objfile);
361       break;
362     case FT_FLOAT:
363       type = init_type (TYPE_CODE_FLT,
364                         TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
365                         0, "real", objfile);
366       break;
367     case FT_DBL_PREC_FLOAT:
368       type = init_type (TYPE_CODE_FLT,
369                         TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
370                         0, "real*8", objfile);
371       break;
372     case FT_FLOAT_DECIMAL:
373       type = init_type (TYPE_CODE_FLT,
374                         TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
375                         0, "floating decimal", objfile);
376       break;
377     case FT_EXT_PREC_FLOAT:
378       type = init_type (TYPE_CODE_FLT,
379                         TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
380                         0, "real*16", objfile);
381       break;
382     case FT_COMPLEX:
383       type = init_type (TYPE_CODE_COMPLEX,
384                         2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
385                         0, "complex*8", objfile);
386       TYPE_TARGET_TYPE (type) = builtin_type_f_real;
387       break;
388     case FT_DBL_PREC_COMPLEX:
389       type = init_type (TYPE_CODE_COMPLEX,
390                         2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
391                         0, "complex*16", objfile);
392       TYPE_TARGET_TYPE (type) = builtin_type_f_real_s8;
393       break;
394     case FT_EXT_PREC_COMPLEX:
395       type = init_type (TYPE_CODE_COMPLEX,
396                         2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
397                         0, "complex*32", objfile);
398       TYPE_TARGET_TYPE (type) = builtin_type_f_real_s16;
399       break;
400     default:
401       /* FIXME:  For now, if we are asked to produce a type not in this
402          language, create the equivalent of a C integer type with the
403          name "<?type?>".  When all the dust settles from the type
404          reconstruction work, this should probably become an error. */
405       type = init_type (TYPE_CODE_INT,
406                         TARGET_INT_BIT / TARGET_CHAR_BIT,
407                         0, "<?type?>", objfile);
408       warning ("internal error: no F77 fundamental type %d", typeid);
409       break;
410     }
411   return (type);
412 }
413 \f
414
415 /* Table of operators and their precedences for printing expressions.  */
416
417 static const struct op_print f_op_print_tab[] =
418 {
419   {"+", BINOP_ADD, PREC_ADD, 0},
420   {"+", UNOP_PLUS, PREC_PREFIX, 0},
421   {"-", BINOP_SUB, PREC_ADD, 0},
422   {"-", UNOP_NEG, PREC_PREFIX, 0},
423   {"*", BINOP_MUL, PREC_MUL, 0},
424   {"/", BINOP_DIV, PREC_MUL, 0},
425   {"DIV", BINOP_INTDIV, PREC_MUL, 0},
426   {"MOD", BINOP_REM, PREC_MUL, 0},
427   {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
428   {".OR.", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
429   {".AND.", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
430   {".NOT.", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
431   {".EQ.", BINOP_EQUAL, PREC_EQUAL, 0},
432   {".NE.", BINOP_NOTEQUAL, PREC_EQUAL, 0},
433   {".LE.", BINOP_LEQ, PREC_ORDER, 0},
434   {".GE.", BINOP_GEQ, PREC_ORDER, 0},
435   {".GT.", BINOP_GTR, PREC_ORDER, 0},
436   {".LT.", BINOP_LESS, PREC_ORDER, 0},
437   {"**", UNOP_IND, PREC_PREFIX, 0},
438   {"@", BINOP_REPEAT, PREC_REPEAT, 0},
439   {NULL, 0, 0, 0}
440 };
441 \f
442 struct type **CONST_PTR (f_builtin_types[]) =
443 {
444   &builtin_type_f_character,
445     &builtin_type_f_logical,
446     &builtin_type_f_logical_s1,
447     &builtin_type_f_logical_s2,
448     &builtin_type_f_integer,
449     &builtin_type_f_integer_s2,
450     &builtin_type_f_real,
451     &builtin_type_f_real_s8,
452     &builtin_type_f_real_s16,
453     &builtin_type_f_complex_s8,
454     &builtin_type_f_complex_s16,
455 #if 0
456     &builtin_type_f_complex_s32,
457 #endif
458     &builtin_type_f_void,
459     0
460 };
461
462 /* This is declared in c-lang.h but it is silly to import that file for what
463    is already just a hack. */
464 extern int c_value_print (struct value *, struct ui_file *, int,
465                           enum val_prettyprint);
466
467 const struct language_defn f_language_defn =
468 {
469   "fortran",
470   language_fortran,
471   f_builtin_types,
472   range_check_on,
473   type_check_on,
474   f_parse,                      /* parser */
475   f_error,                      /* parser error function */
476   evaluate_subexp_standard,
477   f_printchar,                  /* Print character constant */
478   f_printstr,                   /* function to print string constant */
479   f_emit_char,                  /* Function to print a single character */
480   f_create_fundamental_type,    /* Create fundamental type in this language */
481   f_print_type,                 /* Print a type using appropriate syntax */
482   f_val_print,                  /* Print a value using appropriate syntax */
483   c_value_print,                /* FIXME */
484   {"", "", "", ""},             /* Binary format info */
485   {"0%o", "0", "o", ""},        /* Octal format info */
486   {"%d", "", "d", ""},          /* Decimal format info */
487   {"0x%x", "0x", "x", ""},      /* Hex format info */
488   f_op_print_tab,               /* expression operators for printing */
489   0,                            /* arrays are first-class (not c-style) */
490   1,                            /* String lower bound */
491   &builtin_type_f_character,    /* Type of string elements */
492   LANG_MAGIC
493 };
494
495 void
496 _initialize_f_language ()
497 {
498   builtin_type_f_void =
499     init_type (TYPE_CODE_VOID, 1,
500                0,
501                "VOID", (struct objfile *) NULL);
502
503   builtin_type_f_character =
504     init_type (TYPE_CODE_INT, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
505                0,
506                "character", (struct objfile *) NULL);
507
508   builtin_type_f_logical_s1 =
509     init_type (TYPE_CODE_BOOL, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
510                TYPE_FLAG_UNSIGNED,
511                "logical*1", (struct objfile *) NULL);
512
513   builtin_type_f_integer_s2 =
514     init_type (TYPE_CODE_INT, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
515                0,
516                "integer*2", (struct objfile *) NULL);
517
518   builtin_type_f_logical_s2 =
519     init_type (TYPE_CODE_BOOL, TARGET_SHORT_BIT / TARGET_CHAR_BIT,
520                TYPE_FLAG_UNSIGNED,
521                "logical*2", (struct objfile *) NULL);
522
523   builtin_type_f_integer =
524     init_type (TYPE_CODE_INT, TARGET_INT_BIT / TARGET_CHAR_BIT,
525                0,
526                "integer", (struct objfile *) NULL);
527
528   builtin_type_f_logical =
529     init_type (TYPE_CODE_BOOL, TARGET_INT_BIT / TARGET_CHAR_BIT,
530                TYPE_FLAG_UNSIGNED,
531                "logical*4", (struct objfile *) NULL);
532
533   builtin_type_f_real =
534     init_type (TYPE_CODE_FLT, TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
535                0,
536                "real", (struct objfile *) NULL);
537
538   builtin_type_f_real_s8 =
539     init_type (TYPE_CODE_FLT, TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
540                0,
541                "real*8", (struct objfile *) NULL);
542
543   builtin_type_f_real_s16 =
544     init_type (TYPE_CODE_FLT, TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
545                0,
546                "real*16", (struct objfile *) NULL);
547
548   builtin_type_f_complex_s8 =
549     init_type (TYPE_CODE_COMPLEX, 2 * TARGET_FLOAT_BIT / TARGET_CHAR_BIT,
550                0,
551                "complex*8", (struct objfile *) NULL);
552   TYPE_TARGET_TYPE (builtin_type_f_complex_s8) = builtin_type_f_real;
553
554   builtin_type_f_complex_s16 =
555     init_type (TYPE_CODE_COMPLEX, 2 * TARGET_DOUBLE_BIT / TARGET_CHAR_BIT,
556                0,
557                "complex*16", (struct objfile *) NULL);
558   TYPE_TARGET_TYPE (builtin_type_f_complex_s16) = builtin_type_f_real_s8;
559
560   /* We have a new size == 4 double floats for the
561      complex*32 data type */
562
563   builtin_type_f_complex_s32 =
564     init_type (TYPE_CODE_COMPLEX, 2 * TARGET_LONG_DOUBLE_BIT / TARGET_CHAR_BIT,
565                0,
566                "complex*32", (struct objfile *) NULL);
567   TYPE_TARGET_TYPE (builtin_type_f_complex_s32) = builtin_type_f_real_s16;
568
569   builtin_type_string =
570     init_type (TYPE_CODE_STRING, TARGET_CHAR_BIT / TARGET_CHAR_BIT,
571                0,
572                "character string", (struct objfile *) NULL);
573
574   add_language (&f_language_defn);
575 }
576
577 #if 0
578 static SAVED_BF_PTR
579 allocate_saved_bf_node ()
580 {
581   SAVED_BF_PTR new;
582
583   new = (SAVED_BF_PTR) xmalloc (sizeof (SAVED_BF));
584   return (new);
585 }
586
587 static SAVED_FUNCTION *
588 allocate_saved_function_node ()
589 {
590   SAVED_FUNCTION *new;
591
592   new = (SAVED_FUNCTION *) xmalloc (sizeof (SAVED_FUNCTION));
593   return (new);
594 }
595
596 static SAVED_F77_COMMON_PTR
597 allocate_saved_f77_common_node ()
598 {
599   SAVED_F77_COMMON_PTR new;
600
601   new = (SAVED_F77_COMMON_PTR) xmalloc (sizeof (SAVED_F77_COMMON));
602   return (new);
603 }
604
605 static COMMON_ENTRY_PTR
606 allocate_common_entry_node ()
607 {
608   COMMON_ENTRY_PTR new;
609
610   new = (COMMON_ENTRY_PTR) xmalloc (sizeof (COMMON_ENTRY));
611   return (new);
612 }
613 #endif
614
615 SAVED_F77_COMMON_PTR head_common_list = NULL;   /* Ptr to 1st saved COMMON  */
616 SAVED_F77_COMMON_PTR tail_common_list = NULL;   /* Ptr to last saved COMMON  */
617 SAVED_F77_COMMON_PTR current_common = NULL;     /* Ptr to current COMMON */
618
619 #if 0
620 static SAVED_BF_PTR saved_bf_list = NULL;       /* Ptr to (.bf,function) 
621                                                    list */
622 static SAVED_BF_PTR saved_bf_list_end = NULL;   /* Ptr to above list's end */
623 static SAVED_BF_PTR current_head_bf_list = NULL;        /* Current head of above list
624                                                          */
625
626 static SAVED_BF_PTR tmp_bf_ptr; /* Generic temporary for use 
627                                    in macros */
628
629 /* The following function simply enters a given common block onto 
630    the global common block chain */
631
632 static void
633 add_common_block (name, offset, secnum, func_stab)
634      char *name;
635      CORE_ADDR offset;
636      int secnum;
637      char *func_stab;
638 {
639   SAVED_F77_COMMON_PTR tmp;
640   char *c, *local_copy_func_stab;
641
642   /* If the COMMON block we are trying to add has a blank 
643      name (i.e. "#BLNK_COM") then we set it to __BLANK
644      because the darn "#" character makes GDB's input 
645      parser have fits. */
646
647
648   if (STREQ (name, BLANK_COMMON_NAME_ORIGINAL) ||
649       STREQ (name, BLANK_COMMON_NAME_MF77))
650     {
651
652       free (name);
653       name = alloca (strlen (BLANK_COMMON_NAME_LOCAL) + 1);
654       strcpy (name, BLANK_COMMON_NAME_LOCAL);
655     }
656
657   tmp = allocate_saved_f77_common_node ();
658
659   local_copy_func_stab = xmalloc (strlen (func_stab) + 1);
660   strcpy (local_copy_func_stab, func_stab);
661
662   tmp->name = xmalloc (strlen (name) + 1);
663
664   /* local_copy_func_stab is a stabstring, let us first extract the 
665      function name from the stab by NULLing out the ':' character. */
666
667
668   c = NULL;
669   c = strchr (local_copy_func_stab, ':');
670
671   if (c)
672     *c = '\0';
673   else
674     error ("Malformed function STAB found in add_common_block()");
675
676
677   tmp->owning_function = xmalloc (strlen (local_copy_func_stab) + 1);
678
679   strcpy (tmp->owning_function, local_copy_func_stab);
680
681   strcpy (tmp->name, name);
682   tmp->offset = offset;
683   tmp->next = NULL;
684   tmp->entries = NULL;
685   tmp->secnum = secnum;
686
687   current_common = tmp;
688
689   if (head_common_list == NULL)
690     {
691       head_common_list = tail_common_list = tmp;
692     }
693   else
694     {
695       tail_common_list->next = tmp;
696       tail_common_list = tmp;
697     }
698 }
699 #endif
700
701 /* The following function simply enters a given common entry onto 
702    the "current_common" block that has been saved away. */
703
704 #if 0
705 static void
706 add_common_entry (entry_sym_ptr)
707      struct symbol *entry_sym_ptr;
708 {
709   COMMON_ENTRY_PTR tmp;
710
711
712
713   /* The order of this list is important, since 
714      we expect the entries to appear in decl.
715      order when we later issue "info common" calls */
716
717   tmp = allocate_common_entry_node ();
718
719   tmp->next = NULL;
720   tmp->symbol = entry_sym_ptr;
721
722   if (current_common == NULL)
723     error ("Attempt to add COMMON entry with no block open!");
724   else
725     {
726       if (current_common->entries == NULL)
727         {
728           current_common->entries = tmp;
729           current_common->end_of_entries = tmp;
730         }
731       else
732         {
733           current_common->end_of_entries->next = tmp;
734           current_common->end_of_entries = tmp;
735         }
736     }
737 }
738 #endif
739
740 /* This routine finds the first encountred COMMON block named "name" */
741
742 #if 0
743 static SAVED_F77_COMMON_PTR
744 find_first_common_named (name)
745      char *name;
746 {
747
748   SAVED_F77_COMMON_PTR tmp;
749
750   tmp = head_common_list;
751
752   while (tmp != NULL)
753     {
754       if (STREQ (tmp->name, name))
755         return (tmp);
756       else
757         tmp = tmp->next;
758     }
759   return (NULL);
760 }
761 #endif
762
763 /* This routine finds the first encountred COMMON block named "name" 
764    that belongs to function funcname */
765
766 SAVED_F77_COMMON_PTR
767 find_common_for_function (name, funcname)
768      char *name;
769      char *funcname;
770 {
771
772   SAVED_F77_COMMON_PTR tmp;
773
774   tmp = head_common_list;
775
776   while (tmp != NULL)
777     {
778       if (STREQ (tmp->name, name) && STREQ (tmp->owning_function, funcname))
779         return (tmp);
780       else
781         tmp = tmp->next;
782     }
783   return (NULL);
784 }
785
786
787 #if 0
788
789 /* The following function is called to patch up the offsets 
790    for the statics contained in the COMMON block named
791    "name."  */
792
793 static void
794 patch_common_entries (blk, offset, secnum)
795      SAVED_F77_COMMON_PTR blk;
796      CORE_ADDR offset;
797      int secnum;
798 {
799   COMMON_ENTRY_PTR entry;
800
801   blk->offset = offset;         /* Keep this around for future use. */
802
803   entry = blk->entries;
804
805   while (entry != NULL)
806     {
807       SYMBOL_VALUE (entry->symbol) += offset;
808       SYMBOL_SECTION (entry->symbol) = secnum;
809
810       entry = entry->next;
811     }
812   blk->secnum = secnum;
813 }
814
815 /* Patch all commons named "name" that need patching.Since COMMON
816    blocks occur with relative infrequency, we simply do a linear scan on
817    the name.  Eventually, the best way to do this will be a
818    hashed-lookup.  Secnum is the section number for the .bss section
819    (which is where common data lives). */
820
821 static void
822 patch_all_commons_by_name (name, offset, secnum)
823      char *name;
824      CORE_ADDR offset;
825      int secnum;
826 {
827
828   SAVED_F77_COMMON_PTR tmp;
829
830   /* For blank common blocks, change the canonical reprsentation 
831      of a blank name */
832
833   if ((STREQ (name, BLANK_COMMON_NAME_ORIGINAL)) ||
834       (STREQ (name, BLANK_COMMON_NAME_MF77)))
835     {
836       free (name);
837       name = alloca (strlen (BLANK_COMMON_NAME_LOCAL) + 1);
838       strcpy (name, BLANK_COMMON_NAME_LOCAL);
839     }
840
841   tmp = head_common_list;
842
843   while (tmp != NULL)
844     {
845       if (COMMON_NEEDS_PATCHING (tmp))
846         if (STREQ (tmp->name, name))
847           patch_common_entries (tmp, offset, secnum);
848
849       tmp = tmp->next;
850     }
851 }
852 #endif
853
854 /* This macro adds the symbol-number for the start of the function 
855    (the symbol number of the .bf) referenced by symnum_fcn to a 
856    list.  This list, in reality should be a FIFO queue but since 
857    #line pragmas sometimes cause line ranges to get messed up 
858    we simply create a linear list.  This list can then be searched 
859    first by a queueing algorithm and upon failure fall back to 
860    a linear scan. */
861
862 #if 0
863 #define ADD_BF_SYMNUM(bf_sym,fcn_sym) \
864   \
865   if (saved_bf_list == NULL) \
866 { \
867     tmp_bf_ptr = allocate_saved_bf_node(); \
868       \
869         tmp_bf_ptr->symnum_bf = (bf_sym); \
870           tmp_bf_ptr->symnum_fcn = (fcn_sym);  \
871             tmp_bf_ptr->next = NULL; \
872               \
873                 current_head_bf_list = saved_bf_list = tmp_bf_ptr; \
874                   saved_bf_list_end = tmp_bf_ptr; \
875                   } \
876 else \
877 {  \
878      tmp_bf_ptr = allocate_saved_bf_node(); \
879        \
880          tmp_bf_ptr->symnum_bf = (bf_sym);  \
881            tmp_bf_ptr->symnum_fcn = (fcn_sym);  \
882              tmp_bf_ptr->next = NULL;  \
883                \
884                  saved_bf_list_end->next = tmp_bf_ptr;  \
885                    saved_bf_list_end = tmp_bf_ptr; \
886                    }
887 #endif
888
889 /* This function frees the entire (.bf,function) list */
890
891 #if 0
892 static void
893 clear_bf_list ()
894 {
895
896   SAVED_BF_PTR tmp = saved_bf_list;
897   SAVED_BF_PTR next = NULL;
898
899   while (tmp != NULL)
900     {
901       next = tmp->next;
902       free (tmp);
903       tmp = next;
904     }
905   saved_bf_list = NULL;
906 }
907 #endif
908
909 int global_remote_debug;
910
911 #if 0
912
913 static long
914 get_bf_for_fcn (the_function)
915      long the_function;
916 {
917   SAVED_BF_PTR tmp;
918   int nprobes = 0;
919
920   /* First use a simple queuing algorithm (i.e. look and see if the 
921      item at the head of the queue is the one you want)  */
922
923   if (saved_bf_list == NULL)
924     internal_error ("cannot get .bf node off empty list");
925
926   if (current_head_bf_list != NULL)
927     if (current_head_bf_list->symnum_fcn == the_function)
928       {
929         if (global_remote_debug)
930           fprintf (stderr, "*");
931
932         tmp = current_head_bf_list;
933         current_head_bf_list = current_head_bf_list->next;
934         return (tmp->symnum_bf);
935       }
936
937   /* If the above did not work (probably because #line directives were 
938      used in the sourcefile and they messed up our internal tables) we now do
939      the ugly linear scan */
940
941   if (global_remote_debug)
942     fprintf (stderr, "\ndefaulting to linear scan\n");
943
944   nprobes = 0;
945   tmp = saved_bf_list;
946   while (tmp != NULL)
947     {
948       nprobes++;
949       if (tmp->symnum_fcn == the_function)
950         {
951           if (global_remote_debug)
952             fprintf (stderr, "Found in %d probes\n", nprobes);
953           current_head_bf_list = tmp->next;
954           return (tmp->symnum_bf);
955         }
956       tmp = tmp->next;
957     }
958
959   return (-1);
960 }
961
962 static SAVED_FUNCTION_PTR saved_function_list = NULL;
963 static SAVED_FUNCTION_PTR saved_function_list_end = NULL;
964
965 static void
966 clear_function_list ()
967 {
968   SAVED_FUNCTION_PTR tmp = saved_function_list;
969   SAVED_FUNCTION_PTR next = NULL;
970
971   while (tmp != NULL)
972     {
973       next = tmp->next;
974       free (tmp);
975       tmp = next;
976     }
977
978   saved_function_list = NULL;
979 }
980 #endif