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