2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / xcoffout.c
blob73e3d23827d51bb22a053c59ef895aa1ec472b07
1 /* Output xcoff-format symbol table information from GNU compiler.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* Output xcoff-format symbol table data. The main functionality is contained
21 in dbxout.c. This file implements the sdbout-like parts of the xcoff
22 interface. Many functions are very similar to their counterparts in
23 sdbout.c. */
25 #include "config.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "alias.h"
30 #include "symtab.h"
31 #include "tree.h"
32 #include "varasm.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "diagnostic-core.h"
36 #include "output.h"
37 #include "target.h"
38 #include "debug.h"
40 #ifdef XCOFF_DEBUGGING_INFO
42 /* This defines the C_* storage classes. */
43 #include "xcoff.h"
44 #include "xcoffout.h"
45 #include "dbxout.h"
46 #include "gstab.h"
48 /* Line number of beginning of current function, minus one.
49 Negative means not in a function or not using xcoff. */
51 static int xcoff_begin_function_line = -1;
52 static int xcoff_inlining = 0;
54 /* Name of the current include file. */
56 const char *xcoff_current_include_file;
58 /* Name of the current function file. This is the file the `.bf' is
59 emitted from. In case a line is emitted from a different file,
60 (by including that file of course), then the line number will be
61 absolute. */
63 static const char *xcoff_current_function_file;
65 /* Names of bss and data sections. These should be unique names for each
66 compilation unit. */
68 char *xcoff_bss_section_name;
69 char *xcoff_private_data_section_name;
70 char *xcoff_tls_data_section_name;
71 char *xcoff_tbss_section_name;
72 char *xcoff_read_only_section_name;
74 /* Last source file name mentioned in a NOTE insn. */
76 const char *xcoff_lastfile;
78 /* Macro definitions used below. */
80 #define ABS_OR_RELATIVE_LINENO(LINENO) \
81 ((xcoff_inlining) ? (LINENO) : (LINENO) - xcoff_begin_function_line)
83 /* Output source line numbers via ".line". */
84 #define ASM_OUTPUT_LINE(FILE,LINENUM) \
85 do \
86 { \
87 /* Make sure we're in a function and prevent output of .line 0, as \
88 line # 0 is meant for symbol addresses in xcoff. Additionally, \
89 line numbers are 'unsigned short' in 32-bit mode. */ \
90 if (xcoff_begin_function_line >= 0) \
91 { \
92 int lno = ABS_OR_RELATIVE_LINENO (LINENUM); \
93 if (lno > 0 && (TARGET_64BIT || lno <= (int)USHRT_MAX)) \
94 fprintf (FILE, "\t.line\t%d\n", lno); \
95 } \
96 } \
97 while (0)
99 #define ASM_OUTPUT_LFB(FILE,LINENUM) \
101 if (xcoff_begin_function_line == -1) \
103 xcoff_begin_function_line = (LINENUM) - 1;\
104 fprintf (FILE, "\t.bf\t%d\n", (LINENUM)); \
106 xcoff_current_function_file \
107 = (xcoff_current_include_file \
108 ? xcoff_current_include_file : main_input_filename); \
111 #define ASM_OUTPUT_LFE(FILE,LINENUM) \
112 do \
114 fprintf (FILE, "\t.ef\t%d\n", (LINENUM)); \
115 xcoff_begin_function_line = -1; \
117 while (0)
119 #define ASM_OUTPUT_LBB(FILE,LINENUM,BLOCKNUM) \
120 fprintf (FILE, "\t.bb\t%d\n", ABS_OR_RELATIVE_LINENO (LINENUM))
122 #define ASM_OUTPUT_LBE(FILE,LINENUM,BLOCKNUM) \
123 fprintf (FILE, "\t.eb\t%d\n", ABS_OR_RELATIVE_LINENO (LINENUM))
125 static void xcoffout_block (tree, int, tree);
126 static void xcoffout_source_file (FILE *, const char *, int);
128 /* Support routines for XCOFF debugging info. */
130 struct xcoff_type_number
132 const char *name;
133 int number;
135 static const struct xcoff_type_number xcoff_type_numbers[] = {
136 { "int", -1 },
137 { "char", -2 },
138 { "short int", -3 },
139 { "long int", -4 }, /* fiddled to -31 if 64 bits */
140 { "unsigned char", -5 },
141 { "signed char", -6 },
142 { "short unsigned int", -7 },
143 { "unsigned int", -8 },
144 /* No such type "unsigned". */
145 { "long unsigned int", -10 }, /* fiddled to -32 if 64 bits */
146 { "void", -11 },
147 { "float", -12 },
148 { "double", -13 },
149 { "long double", -14 },
150 /* Pascal and Fortran types run from -15 to -29. */
151 { "wchar", -30 }, /* XXX Should be "wchar_t" ? */
152 { "long long int", -31 },
153 { "long long unsigned int", -32 },
154 /* Additional Fortran types run from -33 to -37. */
156 /* ??? Should also handle built-in C++ and Obj-C types. There perhaps
157 aren't any that C doesn't already have. */
160 /* Returns an XCOFF fundamental type number for DECL (assumed to be a
161 TYPE_DECL), or 0 if dbxout.c should assign a type number normally. */
163 xcoff_assign_fundamental_type_number (tree decl)
165 const char *name;
166 size_t i;
168 /* Do not waste time searching the list for non-intrinsic types. */
169 if (DECL_NAME (decl) == 0 || ! DECL_IS_BUILTIN (decl))
170 return 0;
172 name = IDENTIFIER_POINTER (DECL_NAME (decl));
174 /* Linear search, blech, but the list is too small to bother
175 doing anything else. */
176 for (i = 0; i < ARRAY_SIZE (xcoff_type_numbers); i++)
177 if (!strcmp (xcoff_type_numbers[i].name, name))
178 goto found;
179 return 0;
181 found:
182 /* -4 and -10 should be replaced with -31 and -32, respectively,
183 when used for a 64-bit type. */
184 if (int_size_in_bytes (TREE_TYPE (decl)) == 8)
186 if (xcoff_type_numbers[i].number == -4)
187 return -31;
188 if (xcoff_type_numbers[i].number == -10)
189 return -32;
191 return xcoff_type_numbers[i].number;
194 /* Print an error message for unrecognized stab codes. */
196 #define UNKNOWN_STAB(STR) \
197 internal_error ("no sclass for %s stab (0x%x)", STR, stab)
199 /* Conversion routine from BSD stabs to AIX storage classes. */
202 stab_to_sclass (int stab)
204 switch (stab)
206 case N_GSYM:
207 return C_GSYM;
209 case N_FNAME:
210 UNKNOWN_STAB ("N_FNAME");
212 case N_FUN:
213 return C_FUN;
215 case N_STSYM:
216 case N_LCSYM:
217 return C_STSYM;
219 case N_MAIN:
220 UNKNOWN_STAB ("N_MAIN");
222 case N_RSYM:
223 return C_RSYM;
225 case N_SSYM:
226 UNKNOWN_STAB ("N_SSYM");
228 case N_RPSYM:
229 return C_RPSYM;
231 case N_PSYM:
232 return C_PSYM;
233 case N_LSYM:
234 return C_LSYM;
235 case N_DECL:
236 return C_DECL;
237 case N_ENTRY:
238 return C_ENTRY;
240 case N_SO:
241 UNKNOWN_STAB ("N_SO");
243 case N_SOL:
244 UNKNOWN_STAB ("N_SOL");
246 case N_SLINE:
247 UNKNOWN_STAB ("N_SLINE");
249 case N_DSLINE:
250 UNKNOWN_STAB ("N_DSLINE");
252 case N_BSLINE:
253 UNKNOWN_STAB ("N_BSLINE");
255 case N_BINCL:
256 UNKNOWN_STAB ("N_BINCL");
258 case N_EINCL:
259 UNKNOWN_STAB ("N_EINCL");
261 case N_EXCL:
262 UNKNOWN_STAB ("N_EXCL");
264 case N_LBRAC:
265 UNKNOWN_STAB ("N_LBRAC");
267 case N_RBRAC:
268 UNKNOWN_STAB ("N_RBRAC");
270 case N_BCOMM:
271 return C_BCOMM;
272 case N_ECOMM:
273 return C_ECOMM;
274 case N_ECOML:
275 return C_ECOML;
277 case N_LENG:
278 UNKNOWN_STAB ("N_LENG");
280 case N_PC:
281 UNKNOWN_STAB ("N_PC");
283 case N_M2C:
284 UNKNOWN_STAB ("N_M2C");
286 case N_SCOPE:
287 UNKNOWN_STAB ("N_SCOPE");
289 case N_CATCH:
290 UNKNOWN_STAB ("N_CATCH");
292 case N_OPT:
293 UNKNOWN_STAB ("N_OPT");
295 default:
296 UNKNOWN_STAB ("?");
300 /* Output debugging info to FILE to switch to sourcefile FILENAME.
301 INLINE_P is true if this is from an inlined function. */
303 static void
304 xcoffout_source_file (FILE *file, const char *filename, int inline_p)
306 if (filename
307 && (xcoff_lastfile == 0 || strcmp (filename, xcoff_lastfile)
308 || (inline_p && ! xcoff_inlining)
309 || (! inline_p && xcoff_inlining)))
311 if (xcoff_current_include_file)
313 fprintf (file, "\t.ei\t");
314 output_quoted_string (file,
315 remap_debug_filename (xcoff_current_include_file));
316 fprintf (file, "\n");
317 xcoff_current_include_file = NULL;
319 xcoff_inlining = inline_p;
320 if (strcmp (main_input_filename, filename) || inline_p)
322 fprintf (file, "\t.bi\t");
323 output_quoted_string (file, remap_debug_filename (filename));
324 fprintf (file, "\n");
325 xcoff_current_include_file = filename;
327 xcoff_lastfile = filename;
331 /* Output a line number symbol entry for location (FILENAME, LINE). */
333 void
334 xcoffout_source_line (unsigned int line, const char *filename,
335 int discriminator ATTRIBUTE_UNUSED,
336 bool is_stmt ATTRIBUTE_UNUSED)
338 bool inline_p = (strcmp (xcoff_current_function_file, filename) != 0
339 || (int) line < xcoff_begin_function_line);
341 xcoffout_source_file (asm_out_file, filename, inline_p);
343 ASM_OUTPUT_LINE (asm_out_file, line);
346 /* Output the symbols defined in block number DO_BLOCK.
348 This function works by walking the tree structure of blocks,
349 counting blocks until it finds the desired block. */
351 static int do_block = 0;
353 static void
354 xcoffout_block (tree block, int depth, tree args)
356 while (block)
358 /* Ignore blocks never expanded or otherwise marked as real. */
359 if (TREE_USED (block))
361 /* When we reach the specified block, output its symbols. */
362 if (BLOCK_NUMBER (block) == do_block)
364 /* Output the syms of the block. */
365 if (debug_info_level != DINFO_LEVEL_TERSE || depth == 0)
366 dbxout_syms (BLOCK_VARS (block));
367 if (args)
368 dbxout_reg_parms (args);
370 /* We are now done with the block. Don't go to inner blocks. */
371 return;
373 /* If we are past the specified block, stop the scan. */
374 else if (BLOCK_NUMBER (block) >= do_block)
375 return;
377 /* Output the subblocks. */
378 xcoffout_block (BLOCK_SUBBLOCKS (block), depth + 1, NULL_TREE);
380 block = BLOCK_CHAIN (block);
384 /* Describe the beginning of an internal block within a function.
385 Also output descriptions of variables defined in this block.
387 N is the number of the block, by order of beginning, counting from 1,
388 and not counting the outermost (function top-level) block.
389 The blocks match the BLOCKs in DECL_INITIAL (current_function_decl),
390 if the count starts at 0 for the outermost one. */
392 void
393 xcoffout_begin_block (unsigned int line, unsigned int n)
395 tree decl = current_function_decl;
397 /* The IBM AIX compiler does not emit a .bb for the function level scope,
398 so we avoid it here also. */
399 if (n != 1)
400 ASM_OUTPUT_LBB (asm_out_file, line, n);
402 do_block = n;
403 xcoffout_block (DECL_INITIAL (decl), 0, DECL_ARGUMENTS (decl));
406 /* Describe the end line-number of an internal block within a function. */
408 void
409 xcoffout_end_block (unsigned int line, unsigned int n)
411 if (n != 1)
412 ASM_OUTPUT_LBE (asm_out_file, line, n);
415 /* Called at beginning of function (before prologue).
416 Declare function as needed for debugging. */
418 void
419 xcoffout_declare_function (FILE *file, tree decl, const char *name)
421 size_t len;
423 if (*name == '*')
424 name++;
425 len = strlen (name);
426 if (name[len - 1] == ']')
428 char *n = XALLOCAVEC (char, len - 3);
429 memcpy (n, name, len - 4);
430 n[len - 4] = '\0';
431 name = n;
434 /* Any pending .bi or .ei must occur before the .function pseudo op.
435 Otherwise debuggers will think that the function is in the previous
436 file and/or at the wrong line number. */
437 xcoffout_source_file (file, DECL_SOURCE_FILE (decl), 0);
438 dbxout_symbol (decl, 0);
440 /* .function NAME, TOP, MAPPING, TYPE, SIZE
441 16 and 044 are placeholders for backwards compatibility */
442 fprintf (file, "\t.function .%s,.%s,16,044,FE..%s-.%s\n",
443 name, name, name, name);
446 /* Called at beginning of function body (at start of prologue).
447 Record the function's starting line number, so we can output
448 relative line numbers for the other lines.
449 Record the file name that this function is contained in. */
451 void
452 xcoffout_begin_prologue (unsigned int line,
453 const char *file ATTRIBUTE_UNUSED)
455 ASM_OUTPUT_LFB (asm_out_file, line);
456 dbxout_parms (DECL_ARGUMENTS (current_function_decl));
458 /* Emit the symbols for the outermost BLOCK's variables. sdbout.c does this
459 in sdbout_begin_block, but there is no guarantee that there will be any
460 inner block 1, so we must do it here. This gives a result similar to
461 dbxout, so it does make some sense. */
462 do_block = BLOCK_NUMBER (DECL_INITIAL (current_function_decl));
463 xcoffout_block (DECL_INITIAL (current_function_decl), 0,
464 DECL_ARGUMENTS (current_function_decl));
466 ASM_OUTPUT_LINE (asm_out_file, line);
469 /* Called at end of function (before epilogue).
470 Describe end of outermost block. */
472 void
473 xcoffout_end_function (unsigned int last_linenum)
475 ASM_OUTPUT_LFE (asm_out_file, last_linenum);
478 /* Output xcoff info for the absolute end of a function.
479 Called after the epilogue is output. */
481 void
482 xcoffout_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
483 const char *file ATTRIBUTE_UNUSED)
485 /* We need to pass the correct function size to .function, otherwise,
486 the xas assembler can't figure out the correct size for the function
487 aux entry. So, we emit a label after the last instruction which can
488 be used by the .function pseudo op to calculate the function size. */
490 const char *fname = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
491 if (*fname == '*')
492 ++fname;
493 fprintf (asm_out_file, "FE..");
494 ASM_OUTPUT_LABEL (asm_out_file, fname);
496 #endif /* XCOFF_DEBUGGING_INFO */