Merge from vendor branch BINUTILS:
[dragonfly.git] / contrib / gcc / xcoffout.c
1 /* Output xcoff-format symbol table information from GNU compiler.
2    Copyright (C) 1992, 1994, 1995, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* Output xcoff-format symbol table data.  The main functionality is contained
23    in dbxout.c.  This file implements the sdbout-like parts of the xcoff
24    interface.  Many functions are very similar to their counterparts in
25    sdbout.c.  */
26
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "output.h"
34
35 #ifdef XCOFF_DEBUGGING_INFO
36
37 /* This defines the C_* storage classes.  */
38 #include <dbxstclass.h>
39
40 #include "xcoffout.h"
41 #include "dbxout.h"
42
43 #if defined (USG) || !defined (HAVE_STAB_H)
44 #include "gstab.h"
45 #else
46 #include <stab.h>
47
48 /* This is a GNU extension we need to reference in this file.  */
49 #ifndef N_CATCH
50 #define N_CATCH 0x54
51 #endif
52 #endif
53
54 /* Line number of beginning of current function, minus one.
55    Negative means not in a function or not using xcoff.  */
56
57 static int xcoff_begin_function_line = -1;
58 static int xcoff_inlining = 0;
59
60 /* Name of the current include file.  */
61
62 char *xcoff_current_include_file;
63
64 /* Name of the current function file.  This is the file the `.bf' is
65    emitted from.  In case a line is emitted from a different file,
66    (by including that file of course), then the line number will be
67    absolute.  */
68
69 static char *xcoff_current_function_file;
70
71 /* Names of bss and data sections.  These should be unique names for each
72    compilation unit.  */
73
74 char *xcoff_bss_section_name;
75 char *xcoff_private_data_section_name;
76 char *xcoff_read_only_section_name;
77
78 /* Last source file name mentioned in a NOTE insn.  */
79
80 char *xcoff_lastfile;
81 \f
82 /* Macro definitions used below.  */
83
84 #define ABS_OR_RELATIVE_LINENO(LINENO)          \
85 ((xcoff_inlining) ? (LINENO) : (LINENO) - xcoff_begin_function_line)
86
87 /* Output source line numbers via ".line" rather than ".stabd".  */
88 #define ASM_OUTPUT_SOURCE_LINE(FILE,LINENUM) \
89   do {                                          \
90     if (xcoff_begin_function_line >= 0)         \
91       fprintf (FILE, "\t.line\t%d\n", ABS_OR_RELATIVE_LINENO (LINENUM)); \
92   } while (0)
93
94 #define ASM_OUTPUT_LFB(FILE,LINENUM) \
95 {                                               \
96   if (xcoff_begin_function_line == -1)          \
97     {                                           \
98       xcoff_begin_function_line = (LINENUM) - 1;\
99       fprintf (FILE, "\t.bf\t%d\n", (LINENUM)); \
100     }                                           \
101   xcoff_current_function_file                   \
102     = (xcoff_current_include_file               \
103        ? xcoff_current_include_file : main_input_filename); \
104 }
105
106 #define ASM_OUTPUT_LFE(FILE,LINENUM) \
107   do {                                          \
108     fprintf (FILE, "\t.ef\t%d\n", (LINENUM));   \
109     xcoff_begin_function_line = -1;             \
110   } while (0)
111
112 #define ASM_OUTPUT_LBB(FILE,LINENUM,BLOCKNUM) \
113   fprintf (FILE, "\t.bb\t%d\n", ABS_OR_RELATIVE_LINENO (LINENUM))
114
115 #define ASM_OUTPUT_LBE(FILE,LINENUM,BLOCKNUM) \
116   fprintf (FILE, "\t.eb\t%d\n", ABS_OR_RELATIVE_LINENO (LINENUM))
117
118 static void assign_type_number          PROTO((tree, char *, int));
119 static void xcoffout_block              PROTO((tree, int, tree));
120 \f
121 /* Support routines for XCOFF debugging info.  */
122
123 /* Assign NUMBER as the stabx type number for the type described by NAME.
124    Search all decls in the list SYMS to find the type NAME.  */
125
126 static void
127 assign_type_number (syms, name, number)
128      tree syms;
129      char *name;
130      int number;
131 {
132   tree decl;
133
134   for (decl = syms; decl; decl = TREE_CHAIN (decl))
135     if (DECL_NAME (decl)
136         && strcmp (IDENTIFIER_POINTER (DECL_NAME (decl)), name) == 0)
137       {
138         TREE_ASM_WRITTEN (decl) = 1;
139         TYPE_SYMTAB_ADDRESS (TREE_TYPE (decl)) = number;
140       }
141 }
142
143 /* Setup gcc primitive types to use the XCOFF built-in type numbers where
144    possible.  */
145
146 void
147 xcoff_output_standard_types (syms)
148      tree syms;
149 {
150   /* Handle built-in C types here.  */
151
152   assign_type_number (syms, "int", -1);
153   assign_type_number (syms, "char", -2);
154   assign_type_number (syms, "short int", -3);
155   assign_type_number (syms, "long int", (TARGET_64BIT ? -31 : -4));
156   assign_type_number (syms, "unsigned char", -5);
157   assign_type_number (syms, "signed char", -6);
158   assign_type_number (syms, "short unsigned int", -7);
159   assign_type_number (syms, "unsigned int", -8);
160   /* No such type "unsigned".  */
161   assign_type_number (syms, "long unsigned int", (TARGET_64BIT ? -32 : -10));
162   assign_type_number (syms, "void", -11);
163   assign_type_number (syms, "float", -12);
164   assign_type_number (syms, "double", -13);
165   assign_type_number (syms, "long double", -14);
166   /* Pascal and Fortran types run from -15 to -29.  */
167   assign_type_number (syms, "wchar", -30);
168   assign_type_number (syms, "long long int", -31);
169   assign_type_number (syms, "long long unsigned int", -32);
170   /* Additional Fortran types run from -33 to -37.  */
171
172   /* ??? Should also handle built-in C++ and Obj-C types.  There perhaps
173      aren't any that C doesn't already have.  */
174 }
175
176 /* Print an error message for unrecognized stab codes.  */
177
178 #define UNKNOWN_STAB(STR)       \
179    do { \
180      error ("Unknown stab %s: : 0x%x\n", STR, stab);    \
181      fflush (stderr);   \
182    } while (0)
183
184 /* Conversion routine from BSD stabs to AIX storage classes.  */
185
186 int
187 stab_to_sclass (stab)
188      int stab;
189 {
190   switch (stab)
191     {
192     case N_GSYM:
193       return C_GSYM;
194
195     case N_FNAME:
196       UNKNOWN_STAB ("N_FNAME"); 
197       abort();
198
199     case N_FUN:
200       return C_FUN;
201
202     case N_STSYM:
203     case N_LCSYM:
204       return C_STSYM;
205
206 #ifdef N_MAIN
207     case N_MAIN:
208       UNKNOWN_STAB ("N_MAIN"); 
209       abort ();
210 #endif
211
212     case N_RSYM:
213       return C_RSYM;
214
215     case N_SSYM:
216       UNKNOWN_STAB ("N_SSYM"); 
217       abort ();
218
219     case N_RPSYM:
220       return C_RPSYM;
221
222     case N_PSYM:
223       return C_PSYM;
224     case N_LSYM:
225       return C_LSYM;
226     case N_DECL:
227       return C_DECL;
228     case N_ENTRY:
229       return C_ENTRY;
230
231     case N_SO:
232       UNKNOWN_STAB ("N_SO"); 
233       abort ();
234
235     case N_SOL:
236       UNKNOWN_STAB ("N_SOL"); 
237       abort ();
238
239     case N_SLINE:
240       UNKNOWN_STAB ("N_SLINE"); 
241       abort ();
242
243 #ifdef N_DSLINE
244     case N_DSLINE:
245       UNKNOWN_STAB ("N_DSLINE"); 
246       abort ();
247 #endif
248
249 #ifdef N_BSLINE
250     case N_BSLINE:
251       UNKNOWN_STAB ("N_BSLINE"); 
252       abort ();
253 #endif
254 #if 0
255       /* This has the same value as N_BSLINE.  */
256     case N_BROWS:
257       UNKNOWN_STAB ("N_BROWS"); 
258       abort ();
259 #endif
260
261 #ifdef N_BINCL
262     case N_BINCL:
263       UNKNOWN_STAB ("N_BINCL"); 
264       abort ();
265 #endif
266
267 #ifdef N_EINCL
268     case N_EINCL:
269       UNKNOWN_STAB ("N_EINCL"); 
270       abort ();
271 #endif
272
273 #ifdef N_EXCL
274     case N_EXCL:
275       UNKNOWN_STAB ("N_EXCL"); 
276       abort ();
277 #endif
278
279     case N_LBRAC:
280       UNKNOWN_STAB ("N_LBRAC"); 
281       abort ();
282
283     case N_RBRAC:
284       UNKNOWN_STAB ("N_RBRAC"); 
285       abort ();
286
287     case N_BCOMM:
288       return C_BCOMM;
289     case N_ECOMM:
290       return C_ECOMM;
291     case N_ECOML:
292       return C_ECOML;
293
294     case N_LENG:
295       UNKNOWN_STAB ("N_LENG"); 
296       abort ();
297
298     case N_PC:
299       UNKNOWN_STAB ("N_PC"); 
300       abort ();
301
302 #ifdef N_M2C
303     case N_M2C:
304       UNKNOWN_STAB ("N_M2C"); 
305       abort ();
306 #endif
307
308 #ifdef N_SCOPE
309     case N_SCOPE:
310       UNKNOWN_STAB ("N_SCOPE"); 
311       abort ();
312 #endif
313
314     case N_CATCH:
315       UNKNOWN_STAB ("N_CATCH"); 
316       abort ();
317
318     default:
319       UNKNOWN_STAB ("default"); 
320       abort ();
321   }
322 }
323 \f
324 /* Output debugging info to FILE to switch to sourcefile FILENAME.
325    INLINE_P is true if this is from an inlined function.  */
326
327 void
328 xcoffout_source_file (file, filename, inline_p)
329      FILE *file;
330      char *filename;
331      int inline_p;
332 {
333   if (filename
334       && (xcoff_lastfile == 0 || strcmp (filename, xcoff_lastfile)
335           || (inline_p && ! xcoff_inlining)
336           || (! inline_p && xcoff_inlining)))
337     {
338       if (xcoff_current_include_file)
339         {
340           fprintf (file, "\t.ei\t");
341           output_quoted_string (file, xcoff_current_include_file);
342           fprintf (file, "\n");
343           xcoff_current_include_file = NULL;
344         }
345         xcoff_inlining=inline_p;
346       if (strcmp (main_input_filename, filename) || inline_p)
347         {
348           fprintf (file, "\t.bi\t");
349           output_quoted_string (file, filename);
350           fprintf (file, "\n");
351           xcoff_current_include_file = filename;
352         }
353
354       xcoff_lastfile = filename;
355     }
356 }
357
358 /* Output a line number symbol entry into output stream FILE,
359    for source file FILENAME and line number NOTE.  */
360
361 void
362 xcoffout_source_line (file, filename, note)
363      FILE *file;
364      char *filename;
365      rtx note;
366 {
367   xcoffout_source_file (file, filename, RTX_INTEGRATED_P (note));
368
369   ASM_OUTPUT_SOURCE_LINE (file, NOTE_LINE_NUMBER (note));
370 }
371 \f
372 /* Output the symbols defined in block number DO_BLOCK.
373    Set NEXT_BLOCK_NUMBER to 0 before calling.
374
375    This function works by walking the tree structure of blocks,
376    counting blocks until it finds the desired block.  */
377
378 static int do_block = 0;
379
380 static int next_block_number;
381
382 static void
383 xcoffout_block (block, depth, args)
384      register tree block;
385      int depth;
386      tree args;
387 {
388   while (block)
389     {
390       /* Ignore blocks never expanded or otherwise marked as real.  */
391       if (TREE_USED (block))
392         {
393           /* When we reach the specified block, output its symbols.  */
394           if (next_block_number == do_block)
395             {
396               /* Output the syms of the block.  */
397               if (debug_info_level != DINFO_LEVEL_TERSE || depth == 0)
398                 dbxout_syms (BLOCK_VARS (block));
399               if (args)
400                 dbxout_reg_parms (args);
401
402               /* We are now done with the block.  Don't go to inner blocks.  */
403               return;
404             }
405           /* If we are past the specified block, stop the scan.  */
406           else if (next_block_number >= do_block)
407             return;
408
409           next_block_number++;
410
411           /* Output the subblocks.  */
412           xcoffout_block (BLOCK_SUBBLOCKS (block), depth + 1, NULL_TREE);
413         }
414       block = BLOCK_CHAIN (block);
415     }
416 }
417
418 /* Describe the beginning of an internal block within a function.
419    Also output descriptions of variables defined in this block.
420
421    N is the number of the block, by order of beginning, counting from 1,
422    and not counting the outermost (function top-level) block.
423    The blocks match the BLOCKs in DECL_INITIAL (current_function_decl),
424    if the count starts at 0 for the outermost one.  */
425
426 void
427 xcoffout_begin_block (file, line, n)
428      FILE *file;
429      int line;
430      int n;
431 {
432   tree decl = current_function_decl;
433
434   
435   /* The IBM AIX compiler does not emit a .bb for the function level scope,
436      so we avoid it here also.  */
437   if (n != 1)
438     ASM_OUTPUT_LBB (file, line, n);
439
440   do_block = n;
441   next_block_number = 0;
442   xcoffout_block (DECL_INITIAL (decl), 0, DECL_ARGUMENTS (decl));
443 }
444
445 /* Describe the end line-number of an internal block within a function.  */
446
447 void
448 xcoffout_end_block (file, line, n)
449      FILE *file;
450      int line;
451      int n;
452 {
453   if (n != 1)
454     ASM_OUTPUT_LBE (file, line, n);
455 }
456
457 /* Called at beginning of function (before prologue).
458    Declare function as needed for debugging.  */
459
460 void
461 xcoffout_declare_function (file, decl, name)
462      FILE *file;
463      tree decl;
464      char *name;
465 {
466   char *n = name;
467   int i;
468
469   if (*n == '*')
470     n++;
471   else
472     for (i = 0; name[i]; ++i)
473       {
474         if (name[i] == '[')
475           {
476             n = (char *) alloca (i + 1);
477             strncpy (n, name, i);
478             n[i] = '\0';
479             break;
480           }
481       }
482
483   /* Any pending .bi or .ei must occur before the .function pseudo op.
484      Otherwise debuggers will think that the function is in the previous
485      file and/or at the wrong line number.  */
486   xcoffout_source_file (file, DECL_SOURCE_FILE (decl), 0);
487   dbxout_symbol (decl, 0);
488   fprintf (file, "\t.function .%s,.%s,16,044,FE..%s-.%s\n", n, n, n, n);
489 }
490
491 /* Called at beginning of function body (after prologue).
492    Record the function's starting line number, so we can output
493    relative line numbers for the other lines.
494    Record the file name that this function is contained in.  */
495
496 void
497 xcoffout_begin_function (file, last_linenum)
498      FILE *file;
499      int last_linenum;
500 {
501   ASM_OUTPUT_LFB (file, last_linenum);
502   dbxout_parms (DECL_ARGUMENTS (current_function_decl));
503
504   /* Emit the symbols for the outermost BLOCK's variables.  sdbout.c does this
505      in sdbout_begin_block, but there is no guarantee that there will be any
506      inner block 1, so we must do it here.  This gives a result similar to
507      dbxout, so it does make some sense.  */
508   do_block = 0;
509   next_block_number = 0;
510   xcoffout_block (DECL_INITIAL (current_function_decl), 0,
511                   DECL_ARGUMENTS (current_function_decl));
512
513   ASM_OUTPUT_SOURCE_LINE (file, last_linenum);
514 }
515
516 /* Called at end of function (before epilogue).
517    Describe end of outermost block.  */
518
519 void
520 xcoffout_end_function (file, last_linenum)
521      FILE *file;
522      int last_linenum;
523 {
524   ASM_OUTPUT_LFE (file, last_linenum);
525 }
526
527 /* Output xcoff info for the absolute end of a function.
528    Called after the epilogue is output.  */
529
530 void
531 xcoffout_end_epilogue (file)
532      FILE *file;
533 {
534   /* We need to pass the correct function size to .function, otherwise,
535      the xas assembler can't figure out the correct size for the function
536      aux entry.  So, we emit a label after the last instruction which can
537      be used by the .function pseudo op to calculate the function size.  */
538
539   char *fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
540   if (*fname == '*')
541     ++fname;
542   fprintf (file, "FE..");
543   ASM_OUTPUT_LABEL (file, fname);
544 }
545 #endif /* XCOFF_DEBUGGING_INFO */