OSDN Git Service

* Makefile.in (BUILD_RTL): Replace $(BUILD_PREFIX)insn-modes.o
[pf3gnuchains/gcc-fork.git] / gcc / dwarfout.c
1 /* Output Dwarf format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003 Free Software Foundation, Inc.
4    Contributed by Ron Guilmette (rfg@monkeys.com) of Network Computing Devices.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA.  */
22
23 /*
24
25  Notes on the GNU Implementation of DWARF Debugging Information
26  --------------------------------------------------------------
27  Last Major Update: Sun Jul 17 08:17:42 PDT 1994 by rfg@segfault.us.com
28  ------------------------------------------------------------
29
30  This file describes special and unique aspects of the GNU implementation of
31  the DWARF Version 1 debugging information language, as provided in the GNU
32  version 2.x compiler(s).
33
34  For general information about the DWARF debugging information language,
35  you should obtain the DWARF version 1.1 specification document (and perhaps
36  also the DWARF version 2 draft specification document) developed by the
37  (now defunct) UNIX International Programming Languages Special Interest Group.
38
39  To obtain a copy of the DWARF Version 1 and/or DWARF Version 2
40  specification, visit the web page for the DWARF Version 2 committee, at
41
42    http://www.eagercon.com/dwarf/dwarf2std.htm
43
44  The generation of DWARF debugging information by the GNU version 2.x C
45  compiler has now been tested rather extensively for m88k, i386, i860, and
46  SPARC targets.  The DWARF output of the GNU C compiler appears to inter-
47  operate well with the standard SVR4 SDB debugger on these kinds of target
48  systems (but of course, there are no guarantees).
49
50  DWARF 1 generation for the GNU g++ compiler is implemented, but limited.
51  C++ users should definitely use DWARF 2 instead.
52
53  Future plans for the dwarfout.c module of the GNU compiler(s) includes the
54  addition of full support for GNU FORTRAN.  (This should, in theory, be a
55  lot simpler to add than adding support for g++... but we'll see.)
56
57  Many features of the DWARF version 2 specification have been adapted to
58  (and used in) the GNU implementation of DWARF (version 1).  In most of
59  these cases, a DWARF version 2 approach is used in place of (or in addition
60  to) DWARF version 1 stuff simply because it is apparent that DWARF version
61  1 is not sufficiently expressive to provide the kinds of information which
62  may be necessary to support really robust debugging.  In all of these cases
63  however, the use of DWARF version 2 features should not interfere in any
64  way with the interoperability (of GNU compilers) with generally available
65  "classic" (pre version 1) DWARF consumer tools (e.g. SVR4 SDB).
66
67  The DWARF generation enhancement for the GNU compiler(s) was initially
68  donated to the Free Software Foundation by Network Computing Devices.
69  (Thanks NCD!) Additional development and maintenance of dwarfout.c has
70  been largely supported (i.e. funded) by Intel Corporation.  (Thanks Intel!)
71
72  If you have questions or comments about the DWARF generation feature, please
73  send mail to me <rfg@netcom.com>.  I will be happy to investigate any bugs
74  reported and I may even provide fixes (but of course, I can make no promises).
75
76  The DWARF debugging information produced by GCC may deviate in a few minor
77  (but perhaps significant) respects from the DWARF debugging information
78  currently produced by other C compilers.  A serious attempt has been made
79  however to conform to the published specifications, to existing practice,
80  and to generally accepted norms in the GNU implementation of DWARF.
81
82      ** IMPORTANT NOTE **    ** IMPORTANT NOTE **    ** IMPORTANT NOTE **
83
84  Under normal circumstances, the DWARF information generated by the GNU
85  compilers (in an assembly language file) is essentially impossible for
86  a human being to read.  This fact can make it very difficult to debug
87  certain DWARF-related problems.  In order to overcome this difficulty,
88  a feature has been added to dwarfout.c (enabled by the -dA
89  option) which causes additional comments to be placed into the assembly
90  language output file, out to the right-hand side of most bits of DWARF
91  material.  The comments indicate (far more clearly that the obscure
92  DWARF hex codes do) what is actually being encoded in DWARF.  Thus, the
93  -dA option can be highly useful for those who must study the
94  DWARF output from the GNU compilers in detail.
95
96  ---------
97
98  (Footnote: Within this file, the term `Debugging Information Entry' will
99  be abbreviated as `DIE'.)
100
101
102  Release Notes  (aka known bugs)
103  -------------------------------
104
105  In one very obscure case involving dynamically sized arrays, the DWARF
106  "location information" for such an array may make it appear that the
107  array has been totally optimized out of existence, when in fact it
108  *must* actually exist.  (This only happens when you are using *both* -g
109  *and* -O.)  This is due to aggressive dead store elimination in the
110  compiler, and to the fact that the DECL_RTL expressions associated with
111  variables are not always updated to correctly reflect the effects of
112  GCC's aggressive dead store elimination.
113
114  -------------------------------
115
116  When attempting to set a breakpoint at the "start" of a function compiled
117  with -g1, the debugger currently has no way of knowing exactly where the
118  end of the prologue code for the function is.  Thus, for most targets,
119  all the debugger can do is to set the breakpoint at the AT_low_pc address
120  for the function.  But if you stop there and then try to look at one or
121  more of the formal parameter values, they may not have been "homed" yet,
122  so you may get inaccurate answers (or perhaps even addressing errors).
123
124  Some people may consider this simply a non-feature, but I consider it a
125  bug, and I hope to provide some GNU-specific attributes (on function
126  DIEs) which will specify the address of the end of the prologue and the
127  address of the beginning of the epilogue in a future release.
128
129  -------------------------------
130
131  It is believed at this time that old bugs relating to the AT_bit_offset
132  values for bit-fields have been fixed.
133
134  There may still be some very obscure bugs relating to the DWARF description
135  of type `long long' bit-fields for target machines (e.g. 80x86 machines)
136  where the alignment of type `long long' data objects is different from
137  (and less than) the size of a type `long long' data object.
138
139  Please report any problems with the DWARF description of bit-fields as you
140  would any other GCC bug.  (Procedures for bug reporting are given in the
141  GNU C compiler manual.)
142
143  --------------------------------
144
145  At this time, GCC does not know how to handle the GNU C "nested functions"
146  extension.  (See the GCC manual for more info on this extension to ANSI C.)
147
148  --------------------------------
149
150  The GNU compilers now represent inline functions (and inlined instances
151  thereof) in exactly the manner described by the current DWARF version 2
152  (draft) specification.  The version 1 specification for handling inline
153  functions (and inlined instances) was known to be brain-damaged (by the
154  PLSIG) when the version 1 spec was finalized, but it was simply too late
155  in the cycle to get it removed before the version 1 spec was formally
156  released to the public (by UI).
157
158  --------------------------------
159
160  At this time, GCC does not generate the kind of really precise information
161  about the exact declared types of entities with signed integral types which
162  is required by the current DWARF draft specification.
163
164  Specifically, the current DWARF draft specification seems to require that
165  the type of a non-unsigned integral bit-field member of a struct or union
166  type be represented as either a "signed" type or as a "plain" type,
167  depending upon the exact set of keywords that were used in the
168  type specification for the given bit-field member.  It was felt (by the
169  UI/PLSIG) that this distinction between "plain" and "signed" integral types
170  could have some significance (in the case of bit-fields) because ANSI C
171  does not constrain the signedness of a plain bit-field, whereas it does
172  constrain the signedness of an explicitly "signed" bit-field.  For this
173  reason, the current DWARF specification calls for compilers to produce
174  type information (for *all* integral typed entities... not just bit-fields)
175  which explicitly indicates the signedness of the relevant type to be
176  "signed" or "plain" or "unsigned".
177
178  Unfortunately, the GNU DWARF implementation is currently incapable of making
179  such distinctions.
180
181  --------------------------------
182
183
184  Known Interoperability Problems
185  -------------------------------
186
187  Although the GNU implementation of DWARF conforms (for the most part) with
188  the current UI/PLSIG DWARF version 1 specification (with many compatible
189  version 2 features added in as "vendor specific extensions" just for good
190  measure) there are a few known cases where GCC's DWARF output can cause
191  some confusion for "classic" (pre version 1) DWARF consumers such as the
192  System V Release 4 SDB debugger.  These cases are described in this section.
193
194  --------------------------------
195
196  The DWARF version 1 specification includes the fundamental type codes
197  FT_ext_prec_float, FT_complex, FT_dbl_prec_complex, and FT_ext_prec_complex.
198  Since GNU C is only a C compiler (and since C doesn't provide any "complex"
199  data types) the only one of these fundamental type codes which GCC ever
200  generates is FT_ext_prec_float.  This fundamental type code is generated
201  by GCC for the `long double' data type.  Unfortunately, due to an apparent
202  bug in the SVR4 SDB debugger, SDB can become very confused wherever any
203  attempt is made to print a variable, parameter, or field whose type was
204  given in terms of FT_ext_prec_float.
205
206  (Actually, SVR4 SDB fails to understand *any* of the four fundamental type
207  codes mentioned here.  This will fact will cause additional problems when
208  there is a GNU FORTRAN front-end.)
209
210  --------------------------------
211
212  In general, it appears that SVR4 SDB is not able to effectively ignore
213  fundamental type codes in the "implementation defined" range.  This can
214  cause problems when a program being debugged uses the `long long' data
215  type (or the signed or unsigned varieties thereof) because these types
216  are not defined by ANSI C, and thus, GCC must use its own private fundamental
217  type codes (from the implementation-defined range) to represent these types.
218
219  --------------------------------
220
221
222  General GNU DWARF extensions
223  ----------------------------
224
225  In the current DWARF version 1 specification, no mechanism is specified by
226  which accurate information about executable code from include files can be
227  properly (and fully) described.  (The DWARF version 2 specification *does*
228  specify such a mechanism, but it is about 10 times more complicated than
229  it needs to be so I'm not terribly anxious to try to implement it right
230  away.)
231
232  In the GNU implementation of DWARF version 1, a fully downward-compatible
233  extension has been implemented which permits the GNU compilers to specify
234  which executable lines come from which files.  This extension places
235  additional information (about source file names) in GNU-specific sections
236  (which should be totally ignored by all non-GNU DWARF consumers) so that
237  this extended information can be provided (to GNU DWARF consumers) in a way
238  which is totally transparent (and invisible) to non-GNU DWARF consumers
239  (e.g. the SVR4 SDB debugger).  The additional information is placed *only*
240  in specialized GNU-specific sections, where it should never even be seen
241  by non-GNU DWARF consumers.
242
243  To understand this GNU DWARF extension, imagine that the sequence of entries
244  in the .lines section is broken up into several subsections.  Each contiguous
245  sequence of .line entries which relates to a sequence of lines (or statements)
246  from one particular file (either a `base' file or an `include' file) could
247  be called a `line entries chunk' (LEC).
248
249  For each LEC there is one entry in the .debug_srcinfo section.
250
251  Each normal entry in the .debug_srcinfo section consists of two 4-byte
252  words of data as follows:
253
254          (1)    The starting address (relative to the entire .line section)
255                  of the first .line entry in the relevant LEC.
256
257          (2)    The starting address (relative to the entire .debug_sfnames
258                  section) of a NUL terminated string representing the
259                  relevant filename.  (This filename name be either a
260                  relative or an absolute filename, depending upon how the
261                  given source file was located during compilation.)
262
263  Obviously, each .debug_srcinfo entry allows you to find the relevant filename,
264  and it also points you to the first .line entry that was generated as a result
265  of having compiled a given source line from the given source file.
266
267  Each subsequent .line entry should also be assumed to have been produced
268  as a result of compiling yet more lines from the same file.  The end of
269  any given LEC is easily found by looking at the first 4-byte pointer in
270  the *next* .debug_srcinfo entry.  That next .debug_srcinfo entry points
271  to a new and different LEC, so the preceding LEC (implicitly) must have
272  ended with the last .line section entry which occurs at the 2 1/2 words
273  just before the address given in the first pointer of the new .debug_srcinfo
274  entry.
275
276  The following picture may help to clarify this feature.  Let's assume that
277  `LE' stands for `.line entry'.  Also, assume that `* 'stands for a pointer.
278
279
280          .line section     .debug_srcinfo section     .debug_sfnames section
281          ----------------------------------------------------------------
282
283          LE  <---------------------- *
284          LE                         * -----------------> "foobar.c" <---
285          LE                                                             |
286          LE                                                             |
287          LE  <---------------------- *                                  |
288          LE                         * -----------------> "foobar.h" <|  |
289          LE                                                          |  |
290          LE                                                          |  |
291          LE  <---------------------- *                               |  |
292          LE                         * ----------------->  "inner.h"  |  |
293          LE                                                          |  |
294          LE  <---------------------- *                               |  |
295          LE                         * -------------------------------   |
296          LE                                                             |
297          LE                                                             |
298          LE                                                             |
299          LE                                                             |
300          LE  <---------------------- *                                  |
301          LE                         * -----------------------------------
302          LE
303          LE
304          LE
305
306  In effect, each entry in the .debug_srcinfo section points to *both* a
307  filename (in the .debug_sfnames section) and to the start of a block of
308  consecutive LEs (in the .line section).
309
310  Note that just like in the .line section, there are specialized first and
311  last entries in the .debug_srcinfo section for each object file.  These
312  special first and last entries for the .debug_srcinfo section are very
313  different from the normal .debug_srcinfo section entries.  They provide
314  additional information which may be helpful to a debugger when it is
315  interpreting the data in the .debug_srcinfo, .debug_sfnames, and .line
316  sections.
317
318  The first entry in the .debug_srcinfo section for each compilation unit
319  consists of five 4-byte words of data.  The contents of these five words
320  should be interpreted (by debuggers) as follows:
321
322          (1)    The starting address (relative to the entire .line section)
323                  of the .line section for this compilation unit.
324
325          (2)    The starting address (relative to the entire .debug_sfnames
326                  section) of the .debug_sfnames section for this compilation
327                  unit.
328
329          (3)    The starting address (in the execution virtual address space)
330                  of the .text section for this compilation unit.
331
332          (4)    The ending address plus one (in the execution virtual address
333                  space) of the .text section for this compilation unit.
334
335          (5)    The date/time (in seconds since midnight 1/1/70) at which the
336                  compilation of this compilation unit occurred.  This value
337                  should be interpreted as an unsigned quantity because gcc
338                  might be configured to generate a default value of 0xffffffff
339                  in this field (in cases where it is desired to have object
340                  files created at different times from identical source files
341                  be byte-for-byte identical).  By default, these timestamps
342                  are *not* generated by dwarfout.c (so that object files
343                  compiled at different times will be byte-for-byte identical).
344                  If you wish to enable this "timestamp" feature however, you
345                  can simply place a #define for the symbol `DWARF_TIMESTAMPS'
346                  in your target configuration file and then rebuild the GNU
347                  compiler(s).
348
349  Note that the first string placed into the .debug_sfnames section for each
350  compilation unit is the name of the directory in which compilation occurred.
351  This string ends with a `/' (to help indicate that it is the pathname of a
352  directory).  Thus, the second word of each specialized initial .debug_srcinfo
353  entry for each compilation unit may be used as a pointer to the (string)
354  name of the compilation directory, and that string may in turn be used to
355  "absolutize" any relative pathnames which may appear later on in the
356  .debug_sfnames section entries for the same compilation unit.
357
358  The fifth and last word of each specialized starting entry for a compilation
359  unit in the .debug_srcinfo section may (depending upon your configuration)
360  indicate the date/time of compilation, and this may be used (by a debugger)
361  to determine if any of the source files which contributed code to this
362  compilation unit are newer than the object code for the compilation unit
363  itself.  If so, the debugger may wish to print an "out-of-date" warning
364  about the compilation unit.
365
366  The .debug_srcinfo section associated with each compilation will also have
367  a specialized terminating entry.  This terminating .debug_srcinfo section
368  entry will consist of the following two 4-byte words of data:
369
370          (1)    The offset, measured from the start of the .line section to
371                  the beginning of the terminating entry for the .line section.
372
373          (2)    A word containing the value 0xffffffff.
374
375  --------------------------------
376
377  In the current DWARF version 1 specification, no mechanism is specified by
378  which information about macro definitions and un-definitions may be provided
379  to the DWARF consumer.
380
381  The DWARF version 2 (draft) specification does specify such a mechanism.
382  That specification was based on the GNU ("vendor specific extension")
383  which provided some support for macro definitions and un-definitions,
384  but the "official" DWARF version 2 (draft) specification mechanism for
385  handling macros and the GNU implementation have diverged somewhat.  I
386  plan to update the GNU implementation to conform to the "official"
387  DWARF version 2 (draft) specification as soon as I get time to do that.
388
389  Note that in the GNU implementation, additional information about macro
390  definitions and un-definitions is *only* provided when the -g3 level of
391  debug-info production is selected.  (The default level is -g2 and the
392  plain old -g option is considered to be identical to -g2.)
393
394  GCC records information about macro definitions and undefinitions primarily
395  in a section called the .debug_macinfo section.  Normal entries in the
396  .debug_macinfo section consist of the following three parts:
397
398          (1)    A special "type" byte.
399
400          (2)    A 3-byte line-number/filename-offset field.
401
402          (3)    A NUL terminated string.
403
404  The interpretation of the second and third parts is dependent upon the
405  value of the leading (type) byte.
406
407  The type byte may have one of four values depending upon the type of the
408  .debug_macinfo entry which follows.  The 1-byte MACINFO type codes presently
409  used, and their meanings are as follows:
410
411          MACINFO_start          A base file or an include file starts here.
412          MACINFO_resume         The current base or include file ends here.
413          MACINFO_define          A #define directive occurs here.
414          MACINFO_undef           A #undef directive occur here.
415
416  (Note that the MACINFO_... codes mentioned here are simply symbolic names
417  for constants which are defined in the GNU dwarf.h file.)
418
419  For MACINFO_define and MACINFO_undef entries, the second (3-byte) field
420  contains the number of the source line (relative to the start of the current
421  base source file or the current include files) when the #define or #undef
422  directive appears.  For a MACINFO_define entry, the following string field
423  contains the name of the macro which is defined, followed by its definition.
424  Note that the definition is always separated from the name of the macro
425  by at least one whitespace character.  For a MACINFO_undef entry, the
426  string which follows the 3-byte line number field contains just the name
427  of the macro which is being undef'ed.
428
429  For a MACINFO_start entry, the 3-byte field following the type byte contains
430  the offset, relative to the start of the .debug_sfnames section for the
431  current compilation unit, of a string which names the new source file which
432  is beginning its inclusion at this point.  Following that 3-byte field,
433  each MACINFO_start entry always contains a zero length NUL terminated
434  string.
435
436  For a MACINFO_resume entry, the 3-byte field following the type byte contains
437  the line number WITHIN THE INCLUDING FILE at which the inclusion of the
438  current file (whose inclusion ends here) was initiated.  Following that
439  3-byte field, each MACINFO_resume entry always contains a zero length NUL
440  terminated string.
441
442  Each set of .debug_macinfo entries for each compilation unit is terminated
443  by a special .debug_macinfo entry consisting of a 4-byte zero value followed
444  by a single NUL byte.
445
446  --------------------------------
447
448  In the current DWARF draft specification, no provision is made for providing
449  a separate level of (limited) debugging information necessary to support
450  tracebacks (only) through fully-debugged code (e.g. code in system libraries).
451
452  A proposal to define such a level was submitted (by me) to the UI/PLSIG.
453  This proposal was rejected by the UI/PLSIG for inclusion into the DWARF
454  version 1 specification for two reasons.  First, it was felt (by the PLSIG)
455  that the issues involved in supporting a "traceback only" subset of DWARF
456  were not well understood.  Second, and perhaps more importantly, the PLSIG
457  is already having enough trouble agreeing on what it means to be "conforming"
458  to the DWARF specification, and it was felt that trying to specify multiple
459  different *levels* of conformance would only complicate our discussions of
460  this already divisive issue.  Nonetheless, the GNU implementation of DWARF
461  provides an abbreviated "traceback only" level of debug-info production for
462  use with fully-debugged "system library" code.  This level should only be
463  used for fully debugged system library code, and even then, it should only
464  be used where there is a very strong need to conserve disk space.  This
465  abbreviated level of debug-info production can be used by specifying the
466  -g1 option on the compilation command line.
467
468  --------------------------------
469
470  As mentioned above, the GNU implementation of DWARF currently uses the DWARF
471  version 2 (draft) approach for inline functions (and inlined instances
472  thereof).  This is used in preference to the version 1 approach because
473  (quite simply) the version 1 approach is highly brain-damaged and probably
474  unworkable.
475
476  --------------------------------
477
478
479  GNU DWARF Representation of GNU C Extensions to ANSI C
480  ------------------------------------------------------
481
482  The file dwarfout.c has been designed and implemented so as to provide
483  some reasonable DWARF representation for each and every declarative
484  construct which is accepted by the GNU C compiler.  Since the GNU C
485  compiler accepts a superset of ANSI C, this means that there are some
486  cases in which the DWARF information produced by GCC must take some
487  liberties in improvising DWARF representations for declarations which
488  are only valid in (extended) GNU C.
489
490  In particular, GNU C provides at least three significant extensions to
491  ANSI C when it comes to declarations.  These are (1) inline functions,
492  and (2) dynamic arrays, and (3) incomplete enum types.  (See the GCC
493  manual for more information on these GNU extensions to ANSI C.)  When
494  used, these GNU C extensions are represented (in the generated DWARF
495  output of GCC) in the most natural and intuitively obvious ways.
496
497  In the case of inline functions, the DWARF representation is exactly as
498  called for in the DWARF version 2 (draft) specification for an identical
499  function written in C++; i.e. we "reuse" the representation of inline
500  functions which has been defined for C++ to support this GNU C extension.
501
502  In the case of dynamic arrays, we use the most obvious representational
503  mechanism available; i.e. an array type in which the upper bound of
504  some dimension (usually the first and only dimension) is a variable
505  rather than a constant.  (See the DWARF version 1 specification for more
506  details.)
507
508  In the case of incomplete enum types, such types are represented simply
509  as TAG_enumeration_type DIEs which DO NOT contain either AT_byte_size
510  attributes or AT_element_list attributes.
511
512  --------------------------------
513
514
515  Future Directions
516  -----------------
517
518  The codes, formats, and other paraphernalia necessary to provide proper
519  support for symbolic debugging for the C++ language are still being worked
520  on by the UI/PLSIG.  The vast majority of the additions to DWARF which will
521  be needed to completely support C++ have already been hashed out and agreed
522  upon, but a few small issues (e.g. anonymous unions, access declarations)
523  are still being discussed.  Also, we in the PLSIG are still discussing
524  whether or not we need to do anything special for C++ templates.  (At this
525  time it is not yet clear whether we even need to do anything special for
526  these.)
527
528  With regard to FORTRAN, the UI/PLSIG has defined what is believed to be a
529  complete and sufficient set of codes and rules for adequately representing
530  all of FORTRAN 77, and most of Fortran 90 in DWARF.  While some support for
531  this has been implemented in dwarfout.c, further implementation and testing
532  is needed.
533
534  GNU DWARF support for other languages (i.e. Pascal and Modula) is a moot
535  issue until there are GNU front-ends for these other languages.
536
537  As currently defined, DWARF only describes a (binary) language which can
538  be used to communicate symbolic debugging information from a compiler
539  through an assembler and a linker, to a debugger.  There is no clear
540  specification of what processing should be (or must be) done by the
541  assembler and/or the linker.  Fortunately, the role of the assembler
542  is easily inferred (by anyone knowledgeable about assemblers) just by
543  looking  at examples of assembly-level DWARF code.  Sadly though, the
544  allowable (or required) processing steps performed by a linker are
545  harder to infer and (perhaps) even harder to agree upon.  There are
546  several forms of very useful `post-processing' steps which intelligent
547  linkers *could* (in theory) perform on object files containing DWARF,
548  but any and all such link-time transformations are currently both disallowed
549  and unspecified.
550
551  In particular, possible link-time transformations of DWARF code which could
552  provide significant benefits include (but are not limited to):
553
554          Commonization of duplicate DIEs obtained from multiple input
555          (object) files.
556
557          Cross-compilation type checking based upon DWARF type information
558          for objects and functions.
559
560          Other possible `compacting' transformations designed to save disk
561          space and to reduce linker & debugger I/O activity.
562
563 */
564
565 #include "config.h"
566 #include "system.h"
567 #include "coretypes.h"
568 #include "tm.h"
569
570 #ifdef DWARF_DEBUGGING_INFO
571 #include "dwarf.h"
572 #include "tree.h"
573 #include "flags.h"
574 #include "function.h"
575 #include "rtl.h"
576 #include "hard-reg-set.h"
577 #include "insn-config.h"
578 #include "reload.h"
579 #include "output.h"
580 #include "dwarf2asm.h"
581 #include "toplev.h"
582 #include "tm_p.h"
583 #include "debug.h"
584 #include "target.h"
585 #include "langhooks.h"
586
587 /* NOTE: In the comments in this file, many references are made to
588    so called "Debugging Information Entries".  For the sake of brevity,
589    this term is abbreviated to `DIE' throughout the remainder of this
590    file.  */
591
592 /* Note that the implementation of C++ support herein is (as yet) unfinished.
593    If you want to try to complete it, more power to you.  */
594
595 /* How to start an assembler comment.  */
596 #ifndef ASM_COMMENT_START
597 #define ASM_COMMENT_START ";#"
598 #endif
599
600 /* Define a macro which returns nonzero for any tagged type which is
601    used (directly or indirectly) in the specification of either some
602    function's return type or some formal parameter of some function.
603    We use this macro when we are operating in "terse" mode to help us
604    know what tagged types have to be represented in Dwarf (even in
605    terse mode) and which ones don't.
606
607    A flag bit with this meaning really should be a part of the normal
608    GCC ..._TYPE nodes, but at the moment, there is no such bit defined
609    for these nodes.  For now, we have to just fake it.  It it safe for
610    us to simply return zero for all complete tagged types (which will
611    get forced out anyway if they were used in the specification of some
612    formal or return type) and nonzero for all incomplete tagged types.
613 */
614
615 #define TYPE_USED_FOR_FUNCTION(tagged_type) (TYPE_SIZE (tagged_type) == 0)
616
617 /* Define a macro which returns nonzero for a TYPE_DECL which was
618    implicitly generated for a tagged type.
619
620    Note that unlike the gcc front end (which generates a NULL named
621    TYPE_DECL node for each complete tagged type, each array type, and
622    each function type node created) the g++ front end generates a
623    _named_ TYPE_DECL node for each tagged type node created.
624    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
625    generate a DW_TAG_typedef DIE for them.  */
626 #define TYPE_DECL_IS_STUB(decl)                         \
627   (DECL_NAME (decl) == NULL                             \
628    || (DECL_ARTIFICIAL (decl)                           \
629        && is_tagged_type (TREE_TYPE (decl))             \
630        && decl == TYPE_STUB_DECL (TREE_TYPE (decl))))
631
632 /* Maximum size (in bytes) of an artificially generated label.  */
633
634 #define MAX_ARTIFICIAL_LABEL_BYTES      30
635 \f
636 /* Structure to keep track of source filenames.  */
637
638 struct filename_entry {
639   unsigned      number;
640   const char *  name;
641 };
642
643 typedef struct filename_entry filename_entry;
644
645 /* Pointer to an array of elements, each one having the structure above.  */
646
647 static filename_entry *filename_table;
648
649 /* Total number of entries in the table (i.e. array) pointed to by
650    `filename_table'.  This is the *total* and includes both used and
651    unused slots.  */
652
653 static unsigned ft_entries_allocated;
654
655 /* Number of entries in the filename_table which are actually in use.  */
656
657 static unsigned ft_entries;
658
659 /* Size (in elements) of increments by which we may expand the filename
660    table.  Actually, a single hunk of space of this size should be enough
661    for most typical programs.    */
662
663 #define FT_ENTRIES_INCREMENT 64
664
665 /* Local pointer to the name of the main input file.  Initialized in
666    dwarfout_init.  */
667
668 static const char *primary_filename;
669
670 /* Counter to generate unique names for DIEs.  */
671
672 static unsigned next_unused_dienum = 1;
673
674 /* Number of the DIE which is currently being generated.  */
675
676 static unsigned current_dienum;
677
678 /* Number to use for the special "pubname" label on the next DIE which
679    represents a function or data object defined in this compilation
680    unit which has "extern" linkage.  */
681
682 static int next_pubname_number = 0;
683
684 #define NEXT_DIE_NUM pending_sibling_stack[pending_siblings-1]
685
686 /* Pointer to a dynamically allocated list of pre-reserved and still
687    pending sibling DIE numbers.  Note that this list will grow as needed.  */
688
689 static unsigned *pending_sibling_stack;
690
691 /* Counter to keep track of the number of pre-reserved and still pending
692    sibling DIE numbers.  */
693
694 static unsigned pending_siblings;
695
696 /* The currently allocated size of the above list (expressed in number of
697    list elements).  */
698
699 static unsigned pending_siblings_allocated;
700
701 /* Size (in elements) of increments by which we may expand the pending
702    sibling stack.  Actually, a single hunk of space of this size should
703    be enough for most typical programs.  */
704
705 #define PENDING_SIBLINGS_INCREMENT 64
706
707 /* Nonzero if we are performing our file-scope finalization pass and if
708    we should force out Dwarf descriptions of any and all file-scope
709    tagged types which are still incomplete types.  */
710
711 static int finalizing = 0;
712
713 /* A pointer to the base of a list of pending types which we haven't
714    generated DIEs for yet, but which we will have to come back to
715    later on.  */
716
717 static tree *pending_types_list;
718
719 /* Number of elements currently allocated for the pending_types_list.  */
720
721 static unsigned pending_types_allocated;
722
723 /* Number of elements of pending_types_list currently in use.  */
724
725 static unsigned pending_types;
726
727 /* Size (in elements) of increments by which we may expand the pending
728    types list.  Actually, a single hunk of space of this size should
729    be enough for most typical programs.  */
730
731 #define PENDING_TYPES_INCREMENT 64
732
733 /* A pointer to the base of a list of incomplete types which might be
734    completed at some later time.  */
735
736 static tree *incomplete_types_list;
737
738 /* Number of elements currently allocated for the incomplete_types_list.  */
739 static unsigned incomplete_types_allocated;
740
741 /* Number of elements of incomplete_types_list currently in use.  */
742 static unsigned incomplete_types;
743
744 /* Size (in elements) of increments by which we may expand the incomplete
745    types list.  Actually, a single hunk of space of this size should
746    be enough for most typical programs.  */
747 #define INCOMPLETE_TYPES_INCREMENT 64
748
749 /* Pointer to an artificial RECORD_TYPE which we create in dwarfout_init.
750    This is used in a hack to help us get the DIEs describing types of
751    formal parameters to come *after* all of the DIEs describing the formal
752    parameters themselves.  That's necessary in order to be compatible
753    with what the brain-damaged svr4 SDB debugger requires.  */
754
755 static tree fake_containing_scope;
756
757 /* A pointer to the ..._DECL node which we have most recently been working
758    on.  We keep this around just in case something about it looks screwy
759    and we want to tell the user what the source coordinates for the actual
760    declaration are.  */
761
762 static tree dwarf_last_decl;
763
764 /* A flag indicating that we are emitting the member declarations of a
765    class, so member functions and variables should not be entirely emitted.
766    This is a kludge to avoid passing a second argument to output_*_die.  */
767
768 static int in_class;
769
770 /* Forward declarations for functions defined in this file.  */
771
772 static void dwarfout_init (const char *);
773 static void dwarfout_finish (const char *);
774 static void dwarfout_define (unsigned int, const char *);
775 static void dwarfout_undef (unsigned int, const char *);
776 static void dwarfout_start_source_file (unsigned, const char *);
777 static void dwarfout_start_source_file_check (unsigned, const char *);
778 static void dwarfout_end_source_file (unsigned);
779 static void dwarfout_end_source_file_check (unsigned);
780 static void dwarfout_begin_block (unsigned, unsigned);
781 static void dwarfout_end_block (unsigned, unsigned);
782 static void dwarfout_end_epilogue (unsigned int, const char *);
783 static void dwarfout_source_line (unsigned int, const char *);
784 static void dwarfout_end_prologue (unsigned int, const char *);
785 static void dwarfout_end_function (unsigned int);
786 static void dwarfout_function_decl (tree);
787 static void dwarfout_global_decl (tree);
788 static void dwarfout_deferred_inline_function (tree);
789 static void dwarfout_file_scope_decl (tree , int);
790 static const char *dwarf_tag_name (unsigned);
791 static const char *dwarf_attr_name (unsigned);
792 static const char *dwarf_stack_op_name (unsigned);
793 static const char *dwarf_typemod_name (unsigned);
794 static const char *dwarf_fmt_byte_name (unsigned);
795 static const char *dwarf_fund_type_name (unsigned);
796 static tree decl_ultimate_origin (tree);
797 static tree block_ultimate_origin (tree);
798 static tree decl_class_context (tree);
799 #if 0
800 static void output_unsigned_leb128 (unsigned long);
801 static void output_signed_leb128 (long);
802 #endif
803 static int fundamental_type_code (tree);
804 static tree root_type_1 (tree, int);
805 static tree root_type (tree);
806 static void write_modifier_bytes_1 (tree, int, int, int);
807 static void write_modifier_bytes (tree, int, int);
808 static inline int type_is_fundamental (tree);
809 static void equate_decl_number_to_die_number (tree);
810 static inline void equate_type_number_to_die_number (tree);
811 static void output_reg_number (rtx);
812 static void output_mem_loc_descriptor (rtx);
813 static void output_loc_descriptor (rtx);
814 static void output_bound_representation (tree, unsigned, char);
815 static void output_enumeral_list (tree);
816 static inline HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
817 static inline tree field_type (tree);
818 static inline unsigned int simple_type_align_in_bits (tree);
819 static inline unsigned HOST_WIDE_INT simple_type_size_in_bits (tree);
820 static HOST_WIDE_INT field_byte_offset (tree);
821 static inline void sibling_attribute (void);
822 static void location_attribute (rtx);
823 static void data_member_location_attribute (tree);
824 static void const_value_attribute (rtx);
825 static void location_or_const_value_attribute (tree);
826 static inline void name_attribute (const char *);
827 static inline void fund_type_attribute (unsigned);
828 static void mod_fund_type_attribute (tree, int, int);
829 static inline void user_def_type_attribute (tree);
830 static void mod_u_d_type_attribute (tree, int, int);
831 #ifdef USE_ORDERING_ATTRIBUTE
832 static inline void ordering_attribute (unsigned);
833 #endif /* defined(USE_ORDERING_ATTRIBUTE) */
834 static void subscript_data_attribute (tree);
835 static void byte_size_attribute (tree);
836 static inline void bit_offset_attribute (tree);
837 static inline void bit_size_attribute (tree);
838 static inline void element_list_attribute (tree);
839 static inline void stmt_list_attribute (const char *);
840 static inline void low_pc_attribute (const char *);
841 static inline void high_pc_attribute (const char *);
842 static inline void body_begin_attribute (const char *);
843 static inline void body_end_attribute (const char *);
844 static inline void language_attribute (unsigned);
845 static inline void member_attribute (tree);
846 #if 0
847 static inline void string_length_attribute (tree);
848 #endif
849 static inline void comp_dir_attribute (const char *);
850 static inline void sf_names_attribute (const char *);
851 static inline void src_info_attribute (const char *);
852 static inline void mac_info_attribute (const char *);
853 static inline void prototyped_attribute (tree);
854 static inline void producer_attribute (const char *);
855 static inline void inline_attribute (tree);
856 static inline void containing_type_attribute (tree);
857 static inline void abstract_origin_attribute (tree);
858 #ifdef DWARF_DECL_COORDINATES
859 static inline void src_coords_attribute (unsigned, unsigned);
860 #endif /* defined(DWARF_DECL_COORDINATES) */
861 static inline void pure_or_virtual_attribute (tree);
862 static void name_and_src_coords_attributes (tree);
863 static void type_attribute (tree, int, int);
864 static const char *type_tag (tree);
865 static inline void dienum_push (void);
866 static inline void dienum_pop (void);
867 static inline tree member_declared_type (tree);
868 static const char *function_start_label (tree);
869 static void output_array_type_die (void *);
870 static void output_set_type_die (void *);
871 #if 0
872 static void output_entry_point_die (void *);
873 #endif
874 static void output_inlined_enumeration_type_die (void *);
875 static void output_inlined_structure_type_die (void *);
876 static void output_inlined_union_type_die (void *);
877 static void output_enumeration_type_die (void *);
878 static void output_formal_parameter_die (void *);
879 static void output_global_subroutine_die (void *);
880 static void output_global_variable_die (void *);
881 static void output_label_die (void *);
882 static void output_lexical_block_die (void *);
883 static void output_inlined_subroutine_die (void *);
884 static void output_local_variable_die (void *);
885 static void output_member_die (void *);
886 #if 0
887 static void output_pointer_type_die (void *);
888 static void output_reference_type_die (void *);
889 #endif
890 static void output_ptr_to_mbr_type_die (void *);
891 static void output_compile_unit_die (void *);
892 static void output_string_type_die (void *);
893 static void output_inheritance_die (void *);
894 static void output_structure_type_die (void *);
895 static void output_local_subroutine_die (void *);
896 static void output_subroutine_type_die (void *);
897 static void output_typedef_die (void *);
898 static void output_union_type_die (void *);
899 static void output_unspecified_parameters_die (void *);
900 static void output_padded_null_die (void *);
901 static void output_die (void (*)(void *), void *);
902 static void end_sibling_chain (void);
903 static void output_formal_types (tree);
904 static void pend_type (tree);
905 static int type_ok_for_scope (tree, tree);
906 static void output_pending_types_for_scope (tree);
907 static void output_type (tree, tree);
908 static void output_tagged_type_instantiation (tree);
909 static void output_block (tree, int);
910 static void output_decls_for_scope (tree, int);
911 static void output_decl (tree, tree);
912 static void shuffle_filename_entry (filename_entry *);
913 static void generate_new_sfname_entry (void);
914 static unsigned lookup_filename (const char *);
915 static void generate_srcinfo_entry (unsigned, unsigned);
916 static void generate_macinfo_entry (unsigned int, rtx, const char *);
917 static int is_pseudo_reg (rtx);
918 static tree type_main_variant (tree);
919 static int is_tagged_type (tree);
920 static int is_redundant_typedef (tree);
921 static void add_incomplete_type (tree);
922 static void retry_incomplete_types (void);
923 \f
924 /* Definitions of defaults for assembler-dependent names of various
925    pseudo-ops and section names.
926
927    Theses may be overridden in your tm.h file (if necessary) for your
928    particular assembler.  The default values provided here correspond to
929    what is expected by "standard" AT&T System V.4 assemblers.  */
930
931 #ifndef FILE_ASM_OP
932 #define FILE_ASM_OP             "\t.file\t"
933 #endif
934 #ifndef SET_ASM_OP
935 #define SET_ASM_OP              "\t.set\t"
936 #endif
937
938 /* Pseudo-ops for pushing the current section onto the section stack (and
939    simultaneously changing to a new section) and for popping back to the
940    section we were in immediately before this one.  Note that most svr4
941    assemblers only maintain a one level stack... you can push all the
942    sections you want, but you can only pop out one level.  (The sparc
943    svr4 assembler is an exception to this general rule.)  That's
944    OK because we only use at most one level of the section stack herein.  */
945
946 #ifndef PUSHSECTION_ASM_OP
947 #define PUSHSECTION_ASM_OP      "\t.section\t"
948 #endif
949 #ifndef POPSECTION_ASM_OP
950 #define POPSECTION_ASM_OP       "\t.previous"
951 #endif
952
953 /* The default format used by the ASM_OUTPUT_PUSH_SECTION macro (see below)
954    to print the PUSHSECTION_ASM_OP and the section name.  The default here
955    works for almost all svr4 assemblers, except for the sparc, where the
956    section name must be enclosed in double quotes.  (See sparcv4.h.)  */
957
958 #ifndef PUSHSECTION_FORMAT
959 #define PUSHSECTION_FORMAT      "%s%s\n"
960 #endif
961
962 #ifndef DEBUG_SECTION
963 #define DEBUG_SECTION           ".debug"
964 #endif
965 #ifndef LINE_SECTION
966 #define LINE_SECTION            ".line"
967 #endif
968 #ifndef DEBUG_SFNAMES_SECTION
969 #define DEBUG_SFNAMES_SECTION   ".debug_sfnames"
970 #endif
971 #ifndef DEBUG_SRCINFO_SECTION
972 #define DEBUG_SRCINFO_SECTION   ".debug_srcinfo"
973 #endif
974 #ifndef DEBUG_MACINFO_SECTION
975 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
976 #endif
977 #ifndef DEBUG_PUBNAMES_SECTION
978 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
979 #endif
980 #ifndef DEBUG_ARANGES_SECTION
981 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
982 #endif
983 #ifndef TEXT_SECTION_NAME
984 #define TEXT_SECTION_NAME       ".text"
985 #endif
986 #ifndef DATA_SECTION_NAME
987 #define DATA_SECTION_NAME       ".data"
988 #endif
989 #ifndef DATA1_SECTION_NAME
990 #define DATA1_SECTION_NAME      ".data1"
991 #endif
992 #ifndef RODATA_SECTION_NAME
993 #define RODATA_SECTION_NAME     ".rodata"
994 #endif
995 #ifndef RODATA1_SECTION_NAME
996 #define RODATA1_SECTION_NAME    ".rodata1"
997 #endif
998 #ifndef BSS_SECTION_NAME
999 #define BSS_SECTION_NAME        ".bss"
1000 #endif
1001 \f
1002 /* Definitions of defaults for formats and names of various special
1003    (artificial) labels which may be generated within this file (when
1004    the -g options is used and DWARF_DEBUGGING_INFO is in effect.
1005
1006    If necessary, these may be overridden from within your tm.h file,
1007    but typically, you should never need to override these.
1008
1009    These labels have been hacked (temporarily) so that they all begin with
1010    a `.L' sequence so as to appease the stock sparc/svr4 assembler and the
1011    stock m88k/svr4 assembler, both of which need to see .L at the start of
1012    a label in order to prevent that label from going into the linker symbol
1013    table).  When I get time, I'll have to fix this the right way so that we
1014    will use ASM_GENERATE_INTERNAL_LABEL and (*targetm.asm_out.internal_label) herein,
1015    but that will require a rather massive set of changes.  For the moment,
1016    the following definitions out to produce the right results for all svr4
1017    and svr3 assemblers. -- rfg
1018 */
1019
1020 #ifndef TEXT_BEGIN_LABEL
1021 #define TEXT_BEGIN_LABEL        "*.L_text_b"
1022 #endif
1023 #ifndef TEXT_END_LABEL
1024 #define TEXT_END_LABEL          "*.L_text_e"
1025 #endif
1026
1027 #ifndef DATA_BEGIN_LABEL
1028 #define DATA_BEGIN_LABEL        "*.L_data_b"
1029 #endif
1030 #ifndef DATA_END_LABEL
1031 #define DATA_END_LABEL          "*.L_data_e"
1032 #endif
1033
1034 #ifndef DATA1_BEGIN_LABEL
1035 #define DATA1_BEGIN_LABEL       "*.L_data1_b"
1036 #endif
1037 #ifndef DATA1_END_LABEL
1038 #define DATA1_END_LABEL         "*.L_data1_e"
1039 #endif
1040
1041 #ifndef RODATA_BEGIN_LABEL
1042 #define RODATA_BEGIN_LABEL      "*.L_rodata_b"
1043 #endif
1044 #ifndef RODATA_END_LABEL
1045 #define RODATA_END_LABEL        "*.L_rodata_e"
1046 #endif
1047
1048 #ifndef RODATA1_BEGIN_LABEL
1049 #define RODATA1_BEGIN_LABEL     "*.L_rodata1_b"
1050 #endif
1051 #ifndef RODATA1_END_LABEL
1052 #define RODATA1_END_LABEL       "*.L_rodata1_e"
1053 #endif
1054
1055 #ifndef BSS_BEGIN_LABEL
1056 #define BSS_BEGIN_LABEL         "*.L_bss_b"
1057 #endif
1058 #ifndef BSS_END_LABEL
1059 #define BSS_END_LABEL           "*.L_bss_e"
1060 #endif
1061
1062 #ifndef LINE_BEGIN_LABEL
1063 #define LINE_BEGIN_LABEL        "*.L_line_b"
1064 #endif
1065 #ifndef LINE_LAST_ENTRY_LABEL
1066 #define LINE_LAST_ENTRY_LABEL   "*.L_line_last"
1067 #endif
1068 #ifndef LINE_END_LABEL
1069 #define LINE_END_LABEL          "*.L_line_e"
1070 #endif
1071
1072 #ifndef DEBUG_BEGIN_LABEL
1073 #define DEBUG_BEGIN_LABEL       "*.L_debug_b"
1074 #endif
1075 #ifndef SFNAMES_BEGIN_LABEL
1076 #define SFNAMES_BEGIN_LABEL     "*.L_sfnames_b"
1077 #endif
1078 #ifndef SRCINFO_BEGIN_LABEL
1079 #define SRCINFO_BEGIN_LABEL     "*.L_srcinfo_b"
1080 #endif
1081 #ifndef MACINFO_BEGIN_LABEL
1082 #define MACINFO_BEGIN_LABEL     "*.L_macinfo_b"
1083 #endif
1084
1085 #ifndef DEBUG_ARANGES_BEGIN_LABEL
1086 #define DEBUG_ARANGES_BEGIN_LABEL "*.L_debug_aranges_begin"
1087 #endif
1088 #ifndef DEBUG_ARANGES_END_LABEL
1089 #define DEBUG_ARANGES_END_LABEL "*.L_debug_aranges_end"
1090 #endif
1091
1092 #ifndef DIE_BEGIN_LABEL_FMT
1093 #define DIE_BEGIN_LABEL_FMT     "*.L_D%u"
1094 #endif
1095 #ifndef DIE_END_LABEL_FMT
1096 #define DIE_END_LABEL_FMT       "*.L_D%u_e"
1097 #endif
1098 #ifndef PUB_DIE_LABEL_FMT
1099 #define PUB_DIE_LABEL_FMT       "*.L_P%u"
1100 #endif
1101 #ifndef BLOCK_BEGIN_LABEL_FMT
1102 #define BLOCK_BEGIN_LABEL_FMT   "*.L_B%u"
1103 #endif
1104 #ifndef BLOCK_END_LABEL_FMT
1105 #define BLOCK_END_LABEL_FMT     "*.L_B%u_e"
1106 #endif
1107 #ifndef SS_BEGIN_LABEL_FMT
1108 #define SS_BEGIN_LABEL_FMT      "*.L_s%u"
1109 #endif
1110 #ifndef SS_END_LABEL_FMT
1111 #define SS_END_LABEL_FMT        "*.L_s%u_e"
1112 #endif
1113 #ifndef EE_BEGIN_LABEL_FMT
1114 #define EE_BEGIN_LABEL_FMT      "*.L_e%u"
1115 #endif
1116 #ifndef EE_END_LABEL_FMT
1117 #define EE_END_LABEL_FMT        "*.L_e%u_e"
1118 #endif
1119 #ifndef MT_BEGIN_LABEL_FMT
1120 #define MT_BEGIN_LABEL_FMT      "*.L_t%u"
1121 #endif
1122 #ifndef MT_END_LABEL_FMT
1123 #define MT_END_LABEL_FMT        "*.L_t%u_e"
1124 #endif
1125 #ifndef LOC_BEGIN_LABEL_FMT
1126 #define LOC_BEGIN_LABEL_FMT     "*.L_l%u"
1127 #endif
1128 #ifndef LOC_END_LABEL_FMT
1129 #define LOC_END_LABEL_FMT       "*.L_l%u_e"
1130 #endif
1131 #ifndef BOUND_BEGIN_LABEL_FMT
1132 #define BOUND_BEGIN_LABEL_FMT   "*.L_b%u_%u_%c"
1133 #endif
1134 #ifndef BOUND_END_LABEL_FMT
1135 #define BOUND_END_LABEL_FMT     "*.L_b%u_%u_%c_e"
1136 #endif
1137 #ifndef BODY_BEGIN_LABEL_FMT
1138 #define BODY_BEGIN_LABEL_FMT    "*.L_b%u"
1139 #endif
1140 #ifndef BODY_END_LABEL_FMT
1141 #define BODY_END_LABEL_FMT      "*.L_b%u_e"
1142 #endif
1143 #ifndef FUNC_END_LABEL_FMT
1144 #define FUNC_END_LABEL_FMT      "*.L_f%u_e"
1145 #endif
1146 #ifndef TYPE_NAME_FMT
1147 #define TYPE_NAME_FMT           "*.L_T%u"
1148 #endif
1149 #ifndef DECL_NAME_FMT
1150 #define DECL_NAME_FMT           "*.L_E%u"
1151 #endif
1152 #ifndef LINE_CODE_LABEL_FMT
1153 #define LINE_CODE_LABEL_FMT     "*.L_LC%u"
1154 #endif
1155 #ifndef SFNAMES_ENTRY_LABEL_FMT
1156 #define SFNAMES_ENTRY_LABEL_FMT "*.L_F%u"
1157 #endif
1158 #ifndef LINE_ENTRY_LABEL_FMT
1159 #define LINE_ENTRY_LABEL_FMT    "*.L_LE%u"
1160 #endif
1161 \f
1162 /* Definitions of defaults for various types of primitive assembly language
1163    output operations.
1164
1165    If necessary, these may be overridden from within your tm.h file,
1166    but typically, you shouldn't need to override these.  */
1167
1168 #ifndef ASM_OUTPUT_PUSH_SECTION
1169 #define ASM_OUTPUT_PUSH_SECTION(FILE, SECTION) \
1170   fprintf ((FILE), PUSHSECTION_FORMAT, PUSHSECTION_ASM_OP, SECTION)
1171 #endif
1172
1173 #ifndef ASM_OUTPUT_POP_SECTION
1174 #define ASM_OUTPUT_POP_SECTION(FILE) \
1175   fprintf ((FILE), "%s\n", POPSECTION_ASM_OP)
1176 #endif
1177
1178 #ifndef ASM_OUTPUT_DWARF_DELTA2
1179 #define ASM_OUTPUT_DWARF_DELTA2(FILE,LABEL1,LABEL2)                     \
1180   dw2_asm_output_delta (2, LABEL1, LABEL2, NULL)
1181 #endif
1182
1183 #ifndef ASM_OUTPUT_DWARF_DELTA4
1184 #define ASM_OUTPUT_DWARF_DELTA4(FILE,LABEL1,LABEL2)                     \
1185   dw2_asm_output_delta (4, LABEL1, LABEL2, NULL)
1186 #endif
1187
1188 #ifndef ASM_OUTPUT_DWARF_TAG
1189 #define ASM_OUTPUT_DWARF_TAG(FILE,TAG)                                  \
1190   dw2_asm_output_data (2, TAG, "%s", dwarf_tag_name (TAG));
1191 #endif
1192
1193 #ifndef ASM_OUTPUT_DWARF_ATTRIBUTE
1194 #define ASM_OUTPUT_DWARF_ATTRIBUTE(FILE,ATTR)                           \
1195   dw2_asm_output_data (2, ATTR, "%s", dwarf_attr_name (ATTR))
1196 #endif
1197
1198 #ifndef ASM_OUTPUT_DWARF_STACK_OP
1199 #define ASM_OUTPUT_DWARF_STACK_OP(FILE,OP)                              \
1200   dw2_asm_output_data (1, OP, "%s", dwarf_stack_op_name (OP))
1201 #endif
1202
1203 #ifndef ASM_OUTPUT_DWARF_FUND_TYPE
1204 #define ASM_OUTPUT_DWARF_FUND_TYPE(FILE,FT)                             \
1205   dw2_asm_output_data (2, FT, "%s", dwarf_fund_type_name (FT))
1206 #endif
1207
1208 #ifndef ASM_OUTPUT_DWARF_FMT_BYTE
1209 #define ASM_OUTPUT_DWARF_FMT_BYTE(FILE,FMT)                             \
1210   dw2_asm_output_data (1, FMT, "%s", dwarf_fmt_byte_name (FMT));
1211 #endif
1212
1213 #ifndef ASM_OUTPUT_DWARF_TYPE_MODIFIER
1214 #define ASM_OUTPUT_DWARF_TYPE_MODIFIER(FILE,MOD)                        \
1215   dw2_asm_output_data (1, MOD, "%s", dwarf_typemod_name (MOD));
1216 #endif
1217 \f
1218 #ifndef ASM_OUTPUT_DWARF_ADDR
1219 #define ASM_OUTPUT_DWARF_ADDR(FILE,LABEL)                               \
1220   dw2_asm_output_addr (4, LABEL, NULL)
1221 #endif
1222
1223 #ifndef ASM_OUTPUT_DWARF_ADDR_CONST
1224 #define ASM_OUTPUT_DWARF_ADDR_CONST(FILE,RTX)                           \
1225   dw2_asm_output_addr_rtx (4, RTX, NULL)
1226 #endif
1227
1228 #ifndef ASM_OUTPUT_DWARF_REF
1229 #define ASM_OUTPUT_DWARF_REF(FILE,LABEL)                                \
1230   dw2_asm_output_addr (4, LABEL, NULL)
1231 #endif
1232
1233 #ifndef ASM_OUTPUT_DWARF_DATA1
1234 #define ASM_OUTPUT_DWARF_DATA1(FILE,VALUE) \
1235   dw2_asm_output_data (1, VALUE, NULL)
1236 #endif
1237
1238 #ifndef ASM_OUTPUT_DWARF_DATA2
1239 #define ASM_OUTPUT_DWARF_DATA2(FILE,VALUE) \
1240   dw2_asm_output_data (2, VALUE, NULL)
1241 #endif
1242
1243 #ifndef ASM_OUTPUT_DWARF_DATA4
1244 #define ASM_OUTPUT_DWARF_DATA4(FILE,VALUE) \
1245   dw2_asm_output_data (4, VALUE, NULL)
1246 #endif
1247
1248 #ifndef ASM_OUTPUT_DWARF_DATA8
1249 #define ASM_OUTPUT_DWARF_DATA8(FILE,HIGH_VALUE,LOW_VALUE)               \
1250   dw2_asm_output_data (8, VALUE, NULL)
1251 #endif
1252
1253 /* ASM_OUTPUT_DWARF_STRING is defined to output an ascii string, but to
1254    NOT issue a trailing newline. We define ASM_OUTPUT_DWARF_STRING_NEWLINE
1255    based on whether ASM_OUTPUT_DWARF_STRING is defined or not. If it is
1256    defined, we call it, then issue the line feed. If not, we supply a
1257    default definition of calling ASM_OUTPUT_ASCII */
1258
1259 #ifndef ASM_OUTPUT_DWARF_STRING
1260 #define ASM_OUTPUT_DWARF_STRING_NEWLINE(FILE,P) \
1261   ASM_OUTPUT_ASCII ((FILE), P, strlen (P)+1)
1262 #else
1263 #define ASM_OUTPUT_DWARF_STRING_NEWLINE(FILE,P) \
1264   ASM_OUTPUT_DWARF_STRING (FILE,P), ASM_OUTPUT_DWARF_STRING (FILE,"\n")
1265 #endif
1266
1267 \f
1268 /* The debug hooks structure.  */
1269 const struct gcc_debug_hooks dwarf_debug_hooks =
1270 {
1271   dwarfout_init,
1272   dwarfout_finish,
1273   dwarfout_define,
1274   dwarfout_undef,
1275   dwarfout_start_source_file_check,
1276   dwarfout_end_source_file_check,
1277   dwarfout_begin_block,
1278   dwarfout_end_block,
1279   debug_true_tree,              /* ignore_block */
1280   dwarfout_source_line,         /* source_line */
1281   dwarfout_source_line,         /* begin_prologue */
1282   dwarfout_end_prologue,
1283   dwarfout_end_epilogue,
1284   debug_nothing_tree,           /* begin_function */
1285   dwarfout_end_function,
1286   dwarfout_function_decl,
1287   dwarfout_global_decl,
1288   dwarfout_deferred_inline_function,
1289   debug_nothing_tree,           /* outlining_inline_function */
1290   debug_nothing_rtx,            /* label */
1291   debug_nothing_int             /* handle_pch */
1292 };
1293 \f
1294 /************************ general utility functions **************************/
1295
1296 static inline int
1297 is_pseudo_reg (rtx rtl)
1298 {
1299   return (((GET_CODE (rtl) == REG) && (REGNO (rtl) >= FIRST_PSEUDO_REGISTER))
1300           || ((GET_CODE (rtl) == SUBREG)
1301               && (REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER)));
1302 }
1303
1304 static inline tree
1305 type_main_variant (tree type)
1306 {
1307   type = TYPE_MAIN_VARIANT (type);
1308
1309   /* There really should be only one main variant among any group of variants
1310      of a given type (and all of the MAIN_VARIANT values for all members of
1311      the group should point to that one type) but sometimes the C front-end
1312      messes this up for array types, so we work around that bug here.  */
1313
1314   if (TREE_CODE (type) == ARRAY_TYPE)
1315     {
1316       while (type != TYPE_MAIN_VARIANT (type))
1317         type = TYPE_MAIN_VARIANT (type);
1318     }
1319
1320   return type;
1321 }
1322
1323 /* Return nonzero if the given type node represents a tagged type.  */
1324
1325 static inline int
1326 is_tagged_type (tree type)
1327 {
1328   enum tree_code code = TREE_CODE (type);
1329
1330   return (code == RECORD_TYPE || code == UNION_TYPE
1331           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
1332 }
1333
1334 static const char *
1335 dwarf_tag_name (unsigned int tag)
1336 {
1337   switch (tag)
1338     {
1339     case TAG_padding:                   return "TAG_padding";
1340     case TAG_array_type:                return "TAG_array_type";
1341     case TAG_class_type:                return "TAG_class_type";
1342     case TAG_entry_point:               return "TAG_entry_point";
1343     case TAG_enumeration_type:          return "TAG_enumeration_type";
1344     case TAG_formal_parameter:          return "TAG_formal_parameter";
1345     case TAG_global_subroutine:         return "TAG_global_subroutine";
1346     case TAG_global_variable:           return "TAG_global_variable";
1347     case TAG_label:                     return "TAG_label";
1348     case TAG_lexical_block:             return "TAG_lexical_block";
1349     case TAG_local_variable:            return "TAG_local_variable";
1350     case TAG_member:                    return "TAG_member";
1351     case TAG_pointer_type:              return "TAG_pointer_type";
1352     case TAG_reference_type:            return "TAG_reference_type";
1353     case TAG_compile_unit:              return "TAG_compile_unit";
1354     case TAG_string_type:               return "TAG_string_type";
1355     case TAG_structure_type:            return "TAG_structure_type";
1356     case TAG_subroutine:                return "TAG_subroutine";
1357     case TAG_subroutine_type:           return "TAG_subroutine_type";
1358     case TAG_typedef:                   return "TAG_typedef";
1359     case TAG_union_type:                return "TAG_union_type";
1360     case TAG_unspecified_parameters:    return "TAG_unspecified_parameters";
1361     case TAG_variant:                   return "TAG_variant";
1362     case TAG_common_block:              return "TAG_common_block";
1363     case TAG_common_inclusion:          return "TAG_common_inclusion";
1364     case TAG_inheritance:               return "TAG_inheritance";
1365     case TAG_inlined_subroutine:        return "TAG_inlined_subroutine";
1366     case TAG_module:                    return "TAG_module";
1367     case TAG_ptr_to_member_type:        return "TAG_ptr_to_member_type";
1368     case TAG_set_type:                  return "TAG_set_type";
1369     case TAG_subrange_type:             return "TAG_subrange_type";
1370     case TAG_with_stmt:                 return "TAG_with_stmt";
1371
1372     /* GNU extensions.  */
1373
1374     case TAG_format_label:              return "TAG_format_label";
1375     case TAG_namelist:                  return "TAG_namelist";
1376     case TAG_function_template:         return "TAG_function_template";
1377     case TAG_class_template:            return "TAG_class_template";
1378
1379     default:                            return "TAG_<unknown>";
1380     }
1381 }
1382
1383 static const char *
1384 dwarf_attr_name (unsigned int attr)
1385 {
1386   switch (attr)
1387     {
1388     case AT_sibling:                    return "AT_sibling";
1389     case AT_location:                   return "AT_location";
1390     case AT_name:                       return "AT_name";
1391     case AT_fund_type:                  return "AT_fund_type";
1392     case AT_mod_fund_type:              return "AT_mod_fund_type";
1393     case AT_user_def_type:              return "AT_user_def_type";
1394     case AT_mod_u_d_type:               return "AT_mod_u_d_type";
1395     case AT_ordering:                   return "AT_ordering";
1396     case AT_subscr_data:                return "AT_subscr_data";
1397     case AT_byte_size:                  return "AT_byte_size";
1398     case AT_bit_offset:                 return "AT_bit_offset";
1399     case AT_bit_size:                   return "AT_bit_size";
1400     case AT_element_list:               return "AT_element_list";
1401     case AT_stmt_list:                  return "AT_stmt_list";
1402     case AT_low_pc:                     return "AT_low_pc";
1403     case AT_high_pc:                    return "AT_high_pc";
1404     case AT_language:                   return "AT_language";
1405     case AT_member:                     return "AT_member";
1406     case AT_discr:                      return "AT_discr";
1407     case AT_discr_value:                return "AT_discr_value";
1408     case AT_string_length:              return "AT_string_length";
1409     case AT_common_reference:           return "AT_common_reference";
1410     case AT_comp_dir:                   return "AT_comp_dir";
1411     case AT_const_value_string:         return "AT_const_value_string";
1412     case AT_const_value_data2:          return "AT_const_value_data2";
1413     case AT_const_value_data4:          return "AT_const_value_data4";
1414     case AT_const_value_data8:          return "AT_const_value_data8";
1415     case AT_const_value_block2:         return "AT_const_value_block2";
1416     case AT_const_value_block4:         return "AT_const_value_block4";
1417     case AT_containing_type:            return "AT_containing_type";
1418     case AT_default_value_addr:         return "AT_default_value_addr";
1419     case AT_default_value_data2:        return "AT_default_value_data2";
1420     case AT_default_value_data4:        return "AT_default_value_data4";
1421     case AT_default_value_data8:        return "AT_default_value_data8";
1422     case AT_default_value_string:       return "AT_default_value_string";
1423     case AT_friends:                    return "AT_friends";
1424     case AT_inline:                     return "AT_inline";
1425     case AT_is_optional:                return "AT_is_optional";
1426     case AT_lower_bound_ref:            return "AT_lower_bound_ref";
1427     case AT_lower_bound_data2:          return "AT_lower_bound_data2";
1428     case AT_lower_bound_data4:          return "AT_lower_bound_data4";
1429     case AT_lower_bound_data8:          return "AT_lower_bound_data8";
1430     case AT_private:                    return "AT_private";
1431     case AT_producer:                   return "AT_producer";
1432     case AT_program:                    return "AT_program";
1433     case AT_protected:                  return "AT_protected";
1434     case AT_prototyped:                 return "AT_prototyped";
1435     case AT_public:                     return "AT_public";
1436     case AT_pure_virtual:               return "AT_pure_virtual";
1437     case AT_return_addr:                return "AT_return_addr";
1438     case AT_abstract_origin:            return "AT_abstract_origin";
1439     case AT_start_scope:                return "AT_start_scope";
1440     case AT_stride_size:                return "AT_stride_size";
1441     case AT_upper_bound_ref:            return "AT_upper_bound_ref";
1442     case AT_upper_bound_data2:          return "AT_upper_bound_data2";
1443     case AT_upper_bound_data4:          return "AT_upper_bound_data4";
1444     case AT_upper_bound_data8:          return "AT_upper_bound_data8";
1445     case AT_virtual:                    return "AT_virtual";
1446
1447     /* GNU extensions */
1448
1449     case AT_sf_names:                   return "AT_sf_names";
1450     case AT_src_info:                   return "AT_src_info";
1451     case AT_mac_info:                   return "AT_mac_info";
1452     case AT_src_coords:                 return "AT_src_coords";
1453     case AT_body_begin:                 return "AT_body_begin";
1454     case AT_body_end:                   return "AT_body_end";
1455
1456     default:                            return "AT_<unknown>";
1457     }
1458 }
1459
1460 static const char *
1461 dwarf_stack_op_name (unsigned int op)
1462 {
1463   switch (op)
1464     {
1465     case OP_REG:                return "OP_REG";
1466     case OP_BASEREG:            return "OP_BASEREG";
1467     case OP_ADDR:               return "OP_ADDR";
1468     case OP_CONST:              return "OP_CONST";
1469     case OP_DEREF2:             return "OP_DEREF2";
1470     case OP_DEREF4:             return "OP_DEREF4";
1471     case OP_ADD:                return "OP_ADD";
1472     default:                    return "OP_<unknown>";
1473     }
1474 }
1475
1476 static const char *
1477 dwarf_typemod_name (unsigned int mod)
1478 {
1479   switch (mod)
1480     {
1481     case MOD_pointer_to:        return "MOD_pointer_to";
1482     case MOD_reference_to:      return "MOD_reference_to";
1483     case MOD_const:             return "MOD_const";
1484     case MOD_volatile:          return "MOD_volatile";
1485     default:                    return "MOD_<unknown>";
1486     }
1487 }
1488
1489 static const char *
1490 dwarf_fmt_byte_name (unsigned int fmt)
1491 {
1492   switch (fmt)
1493     {
1494     case FMT_FT_C_C:    return "FMT_FT_C_C";
1495     case FMT_FT_C_X:    return "FMT_FT_C_X";
1496     case FMT_FT_X_C:    return "FMT_FT_X_C";
1497     case FMT_FT_X_X:    return "FMT_FT_X_X";
1498     case FMT_UT_C_C:    return "FMT_UT_C_C";
1499     case FMT_UT_C_X:    return "FMT_UT_C_X";
1500     case FMT_UT_X_C:    return "FMT_UT_X_C";
1501     case FMT_UT_X_X:    return "FMT_UT_X_X";
1502     case FMT_ET:        return "FMT_ET";
1503     default:            return "FMT_<unknown>";
1504     }
1505 }
1506
1507 static const char *
1508 dwarf_fund_type_name (unsigned int ft)
1509 {
1510   switch (ft)
1511     {
1512     case FT_char:               return "FT_char";
1513     case FT_signed_char:        return "FT_signed_char";
1514     case FT_unsigned_char:      return "FT_unsigned_char";
1515     case FT_short:              return "FT_short";
1516     case FT_signed_short:       return "FT_signed_short";
1517     case FT_unsigned_short:     return "FT_unsigned_short";
1518     case FT_integer:            return "FT_integer";
1519     case FT_signed_integer:     return "FT_signed_integer";
1520     case FT_unsigned_integer:   return "FT_unsigned_integer";
1521     case FT_long:               return "FT_long";
1522     case FT_signed_long:        return "FT_signed_long";
1523     case FT_unsigned_long:      return "FT_unsigned_long";
1524     case FT_pointer:            return "FT_pointer";
1525     case FT_float:              return "FT_float";
1526     case FT_dbl_prec_float:     return "FT_dbl_prec_float";
1527     case FT_ext_prec_float:     return "FT_ext_prec_float";
1528     case FT_complex:            return "FT_complex";
1529     case FT_dbl_prec_complex:   return "FT_dbl_prec_complex";
1530     case FT_void:               return "FT_void";
1531     case FT_boolean:            return "FT_boolean";
1532     case FT_ext_prec_complex:   return "FT_ext_prec_complex";
1533     case FT_label:              return "FT_label";
1534
1535     /* GNU extensions.  */
1536
1537     case FT_long_long:          return "FT_long_long";
1538     case FT_signed_long_long:   return "FT_signed_long_long";
1539     case FT_unsigned_long_long: return "FT_unsigned_long_long";
1540
1541     case FT_int8:               return "FT_int8";
1542     case FT_signed_int8:        return "FT_signed_int8";
1543     case FT_unsigned_int8:      return "FT_unsigned_int8";
1544     case FT_int16:              return "FT_int16";
1545     case FT_signed_int16:       return "FT_signed_int16";
1546     case FT_unsigned_int16:     return "FT_unsigned_int16";
1547     case FT_int32:              return "FT_int32";
1548     case FT_signed_int32:       return "FT_signed_int32";
1549     case FT_unsigned_int32:     return "FT_unsigned_int32";
1550     case FT_int64:              return "FT_int64";
1551     case FT_signed_int64:       return "FT_signed_int64";
1552     case FT_unsigned_int64:     return "FT_unsigned_int64";
1553     case FT_int128:             return "FT_int128";
1554     case FT_signed_int128:      return "FT_signed_int128";
1555     case FT_unsigned_int128:    return "FT_unsigned_int128";
1556
1557     case FT_real32:             return "FT_real32";
1558     case FT_real64:             return "FT_real64";
1559     case FT_real96:             return "FT_real96";
1560     case FT_real128:            return "FT_real128";
1561
1562     default:                    return "FT_<unknown>";
1563     }
1564 }
1565
1566 /* Determine the "ultimate origin" of a decl.  The decl may be an
1567    inlined instance of an inlined instance of a decl which is local
1568    to an inline function, so we have to trace all of the way back
1569    through the origin chain to find out what sort of node actually
1570    served as the original seed for the given block.  */
1571
1572 static tree
1573 decl_ultimate_origin (tree decl)
1574 {
1575 #ifdef ENABLE_CHECKING
1576   if (DECL_FROM_INLINE (DECL_ORIGIN (decl)))
1577     /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
1578        most distant ancestor, this should never happen.  */
1579     abort ();
1580 #endif
1581
1582   return DECL_ABSTRACT_ORIGIN (decl);
1583 }
1584
1585 /* Determine the "ultimate origin" of a block.  The block may be an
1586    inlined instance of an inlined instance of a block which is local
1587    to an inline function, so we have to trace all of the way back
1588    through the origin chain to find out what sort of node actually
1589    served as the original seed for the given block.  */
1590
1591 static tree
1592 block_ultimate_origin (tree block)
1593 {
1594   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
1595
1596   if (immediate_origin == NULL)
1597     return NULL;
1598   else
1599     {
1600       tree ret_val;
1601       tree lookahead = immediate_origin;
1602
1603       do
1604         {
1605           ret_val = lookahead;
1606           lookahead = (TREE_CODE (ret_val) == BLOCK)
1607                        ? BLOCK_ABSTRACT_ORIGIN (ret_val)
1608                        : NULL;
1609         }
1610       while (lookahead != NULL && lookahead != ret_val);
1611       return ret_val;
1612     }
1613 }
1614
1615 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
1616    of a virtual function may refer to a base class, so we check the 'this'
1617    parameter.  */
1618
1619 static tree
1620 decl_class_context (tree decl)
1621 {
1622   tree context = NULL_TREE;
1623   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
1624     context = DECL_CONTEXT (decl);
1625   else
1626     context = TYPE_MAIN_VARIANT
1627       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
1628
1629   if (context && !TYPE_P (context))
1630     context = NULL_TREE;
1631
1632   return context;
1633 }
1634
1635 #if 0
1636 static void
1637 output_unsigned_leb128 (unsigned long value)
1638 {
1639   unsigned long orig_value = value;
1640
1641   do
1642     {
1643       unsigned byte = (value & 0x7f);
1644
1645       value >>= 7;
1646       if (value != 0)   /* more bytes to follow */
1647         byte |= 0x80;
1648       dw2_asm_output_data (1, byte, "\t%s ULEB128 number - value = %lu",
1649                            orig_value);
1650     }
1651   while (value != 0);
1652 }
1653
1654 static void
1655 output_signed_leb128 (long value)
1656 {
1657   long orig_value = value;
1658   int negative = (value < 0);
1659   int more;
1660
1661   do
1662     {
1663       unsigned byte = (value & 0x7f);
1664
1665       value >>= 7;
1666       if (negative)
1667         value |= 0xfe000000;  /* manually sign extend */
1668       if (((value == 0) && ((byte & 0x40) == 0))
1669           || ((value == -1) && ((byte & 0x40) == 1)))
1670         more = 0;
1671       else
1672         {
1673           byte |= 0x80;
1674           more = 1;
1675         }
1676       dw2_asm_output_data (1, byte, "\t%s SLEB128 number - value = %ld",
1677                            orig_value);
1678     }
1679   while (more);
1680 }
1681 #endif
1682 \f
1683 /**************** utility functions for attribute functions ******************/
1684
1685 /* Given a pointer to a tree node for some type, return a Dwarf fundamental
1686    type code for the given type.
1687
1688    This routine must only be called for GCC type nodes that correspond to
1689    Dwarf fundamental types.
1690
1691    The current Dwarf draft specification calls for Dwarf fundamental types
1692    to accurately reflect the fact that a given type was either a "plain"
1693    integral type or an explicitly "signed" integral type.  Unfortunately,
1694    we can't always do this, because GCC may already have thrown away the
1695    information about the precise way in which the type was originally
1696    specified, as in:
1697
1698         typedef signed int my_type;
1699
1700         struct s { my_type f; };
1701
1702    Since we may be stuck here without enough information to do exactly
1703    what is called for in the Dwarf draft specification, we do the best
1704    that we can under the circumstances and always use the "plain" integral
1705    fundamental type codes for int, short, and long types.  That's probably
1706    good enough.  The additional accuracy called for in the current DWARF
1707    draft specification is probably never even useful in practice.  */
1708
1709 static int
1710 fundamental_type_code (tree type)
1711 {
1712   if (TREE_CODE (type) == ERROR_MARK)
1713     return 0;
1714
1715   switch (TREE_CODE (type))
1716     {
1717       case ERROR_MARK:
1718         return FT_void;
1719
1720       case VOID_TYPE:
1721         return FT_void;
1722
1723       case INTEGER_TYPE:
1724         /* Carefully distinguish all the standard types of C,
1725            without messing up if the language is not C.
1726            Note that we check only for the names that contain spaces;
1727            other names might occur by coincidence in other languages.  */
1728         if (TYPE_NAME (type) != 0
1729             && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1730             && DECL_NAME (TYPE_NAME (type)) != 0
1731             && TREE_CODE (DECL_NAME (TYPE_NAME (type))) == IDENTIFIER_NODE)
1732           {
1733             const char *const name =
1734               IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1735
1736             if (!strcmp (name, "unsigned char"))
1737               return FT_unsigned_char;
1738             if (!strcmp (name, "signed char"))
1739               return FT_signed_char;
1740             if (!strcmp (name, "unsigned int"))
1741               return FT_unsigned_integer;
1742             if (!strcmp (name, "short int"))
1743               return FT_short;
1744             if (!strcmp (name, "short unsigned int"))
1745               return FT_unsigned_short;
1746             if (!strcmp (name, "long int"))
1747               return FT_long;
1748             if (!strcmp (name, "long unsigned int"))
1749               return FT_unsigned_long;
1750             if (!strcmp (name, "long long int"))
1751               return FT_long_long;              /* Not grok'ed by svr4 SDB */
1752             if (!strcmp (name, "long long unsigned int"))
1753               return FT_unsigned_long_long;     /* Not grok'ed by svr4 SDB */
1754           }
1755
1756         /* Most integer types will be sorted out above, however, for the
1757            sake of special `array index' integer types, the following code
1758            is also provided.  */
1759
1760         if (TYPE_PRECISION (type) == INT_TYPE_SIZE)
1761           return (TREE_UNSIGNED (type) ? FT_unsigned_integer : FT_integer);
1762
1763         if (TYPE_PRECISION (type) == LONG_TYPE_SIZE)
1764           return (TREE_UNSIGNED (type) ? FT_unsigned_long : FT_long);
1765
1766         if (TYPE_PRECISION (type) == LONG_LONG_TYPE_SIZE)
1767           return (TREE_UNSIGNED (type) ? FT_unsigned_long_long : FT_long_long);
1768
1769         if (TYPE_PRECISION (type) == SHORT_TYPE_SIZE)
1770           return (TREE_UNSIGNED (type) ? FT_unsigned_short : FT_short);
1771
1772         if (TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
1773           return (TREE_UNSIGNED (type) ? FT_unsigned_char : FT_char);
1774
1775         if (TYPE_MODE (type) == TImode)
1776           return (TREE_UNSIGNED (type) ? FT_unsigned_int128 : FT_int128);
1777
1778         /* In C++, __java_boolean is an INTEGER_TYPE with precision == 1 */
1779         if (TYPE_PRECISION (type) == 1)
1780           return FT_boolean;
1781
1782         abort ();
1783
1784       case REAL_TYPE:
1785         /* Carefully distinguish all the standard types of C,
1786            without messing up if the language is not C.  */
1787         if (TYPE_NAME (type) != 0
1788             && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
1789             && DECL_NAME (TYPE_NAME (type)) != 0
1790             && TREE_CODE (DECL_NAME (TYPE_NAME (type))) == IDENTIFIER_NODE)
1791           {
1792             const char *const name =
1793               IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1794
1795             /* Note that here we can run afoul of a serious bug in "classic"
1796                svr4 SDB debuggers.  They don't seem to understand the
1797                FT_ext_prec_float type (even though they should).  */
1798
1799             if (!strcmp (name, "long double"))
1800               return FT_ext_prec_float;
1801           }
1802
1803         if (TYPE_PRECISION (type) == DOUBLE_TYPE_SIZE)
1804           {
1805             /* On the SH, when compiling with -m3e or -m4-single-only, both
1806                float and double are 32 bits.  But since the debugger doesn't
1807                know about the subtarget, it always thinks double is 64 bits.
1808                So we have to tell the debugger that the type is float to
1809                make the output of the 'print' command etc. readable.  */
1810             if (DOUBLE_TYPE_SIZE == FLOAT_TYPE_SIZE && FLOAT_TYPE_SIZE == 32)
1811               return FT_float;
1812             return FT_dbl_prec_float;
1813           }
1814         if (TYPE_PRECISION (type) == FLOAT_TYPE_SIZE)
1815           return FT_float;
1816
1817         /* Note that here we can run afoul of a serious bug in "classic"
1818            svr4 SDB debuggers.  They don't seem to understand the
1819            FT_ext_prec_float type (even though they should).  */
1820
1821         if (TYPE_PRECISION (type) == LONG_DOUBLE_TYPE_SIZE)
1822           return FT_ext_prec_float;
1823         abort ();
1824
1825       case COMPLEX_TYPE:
1826         return FT_complex;      /* GNU FORTRAN COMPLEX type.  */
1827
1828       case CHAR_TYPE:
1829         return FT_char;         /* GNU Pascal CHAR type.  Not used in C.  */
1830
1831       case BOOLEAN_TYPE:
1832         return FT_boolean;      /* GNU FORTRAN BOOLEAN type.  */
1833
1834       default:
1835         abort ();       /* No other TREE_CODEs are Dwarf fundamental types.  */
1836     }
1837   return 0;
1838 }
1839 \f
1840 /* Given a pointer to an arbitrary ..._TYPE tree node, return a pointer to
1841    the Dwarf "root" type for the given input type.  The Dwarf "root" type
1842    of a given type is generally the same as the given type, except that if
1843    the  given type is a pointer or reference type, then the root type of
1844    the given type is the root type of the "basis" type for the pointer or
1845    reference type.  (This definition of the "root" type is recursive.)
1846    Also, the root type of a `const' qualified type or a `volatile'
1847    qualified type is the root type of the given type without the
1848    qualifiers.  */
1849
1850 static tree
1851 root_type_1 (tree type, int count)
1852 {
1853   /* Give up after searching 1000 levels, in case this is a recursive
1854      pointer type.  Such types are possible in Ada, but it is not possible
1855      to represent them in DWARF1 debug info.  */
1856   if (count > 1000)
1857     return error_mark_node;
1858
1859   switch (TREE_CODE (type))
1860     {
1861       case ERROR_MARK:
1862         return error_mark_node;
1863
1864       case POINTER_TYPE:
1865       case REFERENCE_TYPE:
1866         return root_type_1 (TREE_TYPE (type), count+1);
1867
1868       default:
1869         return type;
1870     }
1871 }
1872
1873 static tree
1874 root_type (tree type)
1875 {
1876   type = root_type_1 (type, 0);
1877   if (type != error_mark_node)
1878     type = type_main_variant (type);
1879   return type;
1880 }
1881
1882 /* Given a pointer to an arbitrary ..._TYPE tree node, write out a sequence
1883    of zero or more Dwarf "type-modifier" bytes applicable to the type.  */
1884
1885 static void
1886 write_modifier_bytes_1 (tree type, int decl_const, int decl_volatile, int count)
1887 {
1888   if (TREE_CODE (type) == ERROR_MARK)
1889     return;
1890
1891   /* Give up after searching 1000 levels, in case this is a recursive
1892      pointer type.  Such types are possible in Ada, but it is not possible
1893      to represent them in DWARF1 debug info.  */
1894   if (count > 1000)
1895     return;
1896
1897   if (TYPE_READONLY (type) || decl_const)
1898     ASM_OUTPUT_DWARF_TYPE_MODIFIER (asm_out_file, MOD_const);
1899   if (TYPE_VOLATILE (type) || decl_volatile)
1900     ASM_OUTPUT_DWARF_TYPE_MODIFIER (asm_out_file, MOD_volatile);
1901   switch (TREE_CODE (type))
1902     {
1903       case POINTER_TYPE:
1904         ASM_OUTPUT_DWARF_TYPE_MODIFIER (asm_out_file, MOD_pointer_to);
1905         write_modifier_bytes_1 (TREE_TYPE (type), 0, 0, count+1);
1906         return;
1907
1908       case REFERENCE_TYPE:
1909         ASM_OUTPUT_DWARF_TYPE_MODIFIER (asm_out_file, MOD_reference_to);
1910         write_modifier_bytes_1 (TREE_TYPE (type), 0, 0, count+1);
1911         return;
1912
1913       case ERROR_MARK:
1914       default:
1915         return;
1916     }
1917 }
1918
1919 static void
1920 write_modifier_bytes (tree type, int decl_const, int decl_volatile)
1921 {
1922   write_modifier_bytes_1 (type, decl_const, decl_volatile, 0);
1923 }
1924 \f
1925 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
1926    given input type is a Dwarf "fundamental" type.  Otherwise return zero.  */
1927
1928 static inline int
1929 type_is_fundamental (tree type)
1930 {
1931   switch (TREE_CODE (type))
1932     {
1933       case ERROR_MARK:
1934       case VOID_TYPE:
1935       case INTEGER_TYPE:
1936       case REAL_TYPE:
1937       case COMPLEX_TYPE:
1938       case BOOLEAN_TYPE:
1939       case CHAR_TYPE:
1940         return 1;
1941
1942       case SET_TYPE:
1943       case ARRAY_TYPE:
1944       case RECORD_TYPE:
1945       case UNION_TYPE:
1946       case QUAL_UNION_TYPE:
1947       case ENUMERAL_TYPE:
1948       case FUNCTION_TYPE:
1949       case METHOD_TYPE:
1950       case POINTER_TYPE:
1951       case REFERENCE_TYPE:
1952       case FILE_TYPE:
1953       case OFFSET_TYPE:
1954       case LANG_TYPE:
1955       case VECTOR_TYPE:
1956         return 0;
1957
1958       default:
1959         abort ();
1960     }
1961   return 0;
1962 }
1963
1964 /* Given a pointer to some ..._DECL tree node, generate an assembly language
1965    equate directive which will associate a symbolic name with the current DIE.
1966
1967    The name used is an artificial label generated from the DECL_UID number
1968    associated with the given decl node.  The name it gets equated to is the
1969    symbolic label that we (previously) output at the start of the DIE that
1970    we are currently generating.
1971
1972    Calling this function while generating some "decl related" form of DIE
1973    makes it possible to later refer to the DIE which represents the given
1974    decl simply by re-generating the symbolic name from the ..._DECL node's
1975    UID number.  */
1976
1977 static void
1978 equate_decl_number_to_die_number (tree decl)
1979 {
1980   /* In the case where we are generating a DIE for some ..._DECL node
1981      which represents either some inline function declaration or some
1982      entity declared within an inline function declaration/definition,
1983      setup a symbolic name for the current DIE so that we have a name
1984      for this DIE that we can easily refer to later on within
1985      AT_abstract_origin attributes.  */
1986
1987   char decl_label[MAX_ARTIFICIAL_LABEL_BYTES];
1988   char die_label[MAX_ARTIFICIAL_LABEL_BYTES];
1989
1990   sprintf (decl_label, DECL_NAME_FMT, DECL_UID (decl));
1991   sprintf (die_label, DIE_BEGIN_LABEL_FMT, current_dienum);
1992   ASM_OUTPUT_DEF (asm_out_file, decl_label, die_label);
1993 }
1994
1995 /* Given a pointer to some ..._TYPE tree node, generate an assembly language
1996    equate directive which will associate a symbolic name with the current DIE.
1997
1998    The name used is an artificial label generated from the TYPE_UID number
1999    associated with the given type node.  The name it gets equated to is the
2000    symbolic label that we (previously) output at the start of the DIE that
2001    we are currently generating.
2002
2003    Calling this function while generating some "type related" form of DIE
2004    makes it easy to later refer to the DIE which represents the given type
2005    simply by re-generating the alternative name from the ..._TYPE node's
2006    UID number.  */
2007
2008 static inline void
2009 equate_type_number_to_die_number (tree type)
2010 {
2011   char type_label[MAX_ARTIFICIAL_LABEL_BYTES];
2012   char die_label[MAX_ARTIFICIAL_LABEL_BYTES];
2013
2014   /* We are generating a DIE to represent the main variant of this type
2015      (i.e the type without any const or volatile qualifiers) so in order
2016      to get the equate to come out right, we need to get the main variant
2017      itself here.  */
2018
2019   type = type_main_variant (type);
2020
2021   sprintf (type_label, TYPE_NAME_FMT, TYPE_UID (type));
2022   sprintf (die_label, DIE_BEGIN_LABEL_FMT, current_dienum);
2023   ASM_OUTPUT_DEF (asm_out_file, type_label, die_label);
2024 }
2025
2026 static void
2027 output_reg_number (rtx rtl)
2028 {
2029   unsigned regno = REGNO (rtl);
2030
2031   if (regno >= DWARF_FRAME_REGISTERS)
2032     {
2033       warning ("%Jinternal regno botch: '%D' has regno = %d\n",
2034                dwarf_last_decl, dwarf_last_decl, regno);
2035       regno = 0;
2036     }
2037   dw2_assemble_integer (4, GEN_INT (DBX_REGISTER_NUMBER (regno)));
2038   if (flag_debug_asm)
2039     {
2040       fprintf (asm_out_file, "\t%s ", ASM_COMMENT_START);
2041       PRINT_REG (rtl, 0, asm_out_file);
2042     }
2043   fputc ('\n', asm_out_file);
2044 }
2045
2046 /* The following routine is a nice and simple transducer.  It converts the
2047    RTL for a variable or parameter (resident in memory) into an equivalent
2048    Dwarf representation of a mechanism for getting the address of that same
2049    variable onto the top of a hypothetical "address evaluation" stack.
2050
2051    When creating memory location descriptors, we are effectively trans-
2052    forming the RTL for a memory-resident object into its Dwarf postfix
2053    expression equivalent.  This routine just recursively descends an
2054    RTL tree, turning it into Dwarf postfix code as it goes.  */
2055
2056 static void
2057 output_mem_loc_descriptor (rtx rtl)
2058 {
2059   /* Note that for a dynamically sized array, the location we will
2060      generate a description of here will be the lowest numbered location
2061      which is actually within the array.  That's *not* necessarily the
2062      same as the zeroth element of the array.  */
2063
2064   rtl = (*targetm.delegitimize_address) (rtl);
2065
2066   switch (GET_CODE (rtl))
2067     {
2068       case SUBREG:
2069
2070         /* The case of a subreg may arise when we have a local (register)
2071            variable or a formal (register) parameter which doesn't quite
2072            fill up an entire register.  For now, just assume that it is
2073            legitimate to make the Dwarf info refer to the whole register
2074            which contains the given subreg.  */
2075
2076         rtl = SUBREG_REG (rtl);
2077         /* Drop thru.  */
2078
2079       case REG:
2080
2081         /* Whenever a register number forms a part of the description of
2082            the method for calculating the (dynamic) address of a memory
2083            resident object, DWARF rules require the register number to
2084            be referred to as a "base register".  This distinction is not
2085            based in any way upon what category of register the hardware
2086            believes the given register belongs to.  This is strictly
2087            DWARF terminology we're dealing with here.
2088
2089            Note that in cases where the location of a memory-resident data
2090            object could be expressed as:
2091
2092                     OP_ADD (OP_BASEREG (basereg), OP_CONST (0))
2093
2094            the actual DWARF location descriptor that we generate may just
2095            be OP_BASEREG (basereg).  This may look deceptively like the
2096            object in question was allocated to a register (rather than
2097            in memory) so DWARF consumers need to be aware of the subtle
2098            distinction between OP_REG and OP_BASEREG.  */
2099
2100         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_BASEREG);
2101         output_reg_number (rtl);
2102         break;
2103
2104       case MEM:
2105         output_mem_loc_descriptor (XEXP (rtl, 0));
2106         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_DEREF4);
2107         break;
2108
2109       case CONST:
2110       case SYMBOL_REF:
2111         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_ADDR);
2112         ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file, rtl);
2113         break;
2114
2115       case PLUS:
2116         output_mem_loc_descriptor (XEXP (rtl, 0));
2117         output_mem_loc_descriptor (XEXP (rtl, 1));
2118         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_ADD);
2119         break;
2120
2121       case CONST_INT:
2122         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_CONST);
2123         ASM_OUTPUT_DWARF_DATA4 (asm_out_file, INTVAL (rtl));
2124         break;
2125
2126       case MULT:
2127         /* If a pseudo-reg is optimized away, it is possible for it to
2128            be replaced with a MEM containing a multiply.  Use a GNU extension
2129            to describe it.  */
2130         output_mem_loc_descriptor (XEXP (rtl, 0));
2131         output_mem_loc_descriptor (XEXP (rtl, 1));
2132         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_MULT);
2133         break;
2134
2135       default:
2136         abort ();
2137     }
2138 }
2139
2140 /* Output a proper Dwarf location descriptor for a variable or parameter
2141    which is either allocated in a register or in a memory location.  For
2142    a register, we just generate an OP_REG and the register number.  For a
2143    memory location we provide a Dwarf postfix expression describing how to
2144    generate the (dynamic) address of the object onto the address stack.  */
2145
2146 static void
2147 output_loc_descriptor (rtx rtl)
2148 {
2149   switch (GET_CODE (rtl))
2150     {
2151     case SUBREG:
2152
2153         /* The case of a subreg may arise when we have a local (register)
2154            variable or a formal (register) parameter which doesn't quite
2155            fill up an entire register.  For now, just assume that it is
2156            legitimate to make the Dwarf info refer to the whole register
2157            which contains the given subreg.  */
2158
2159         rtl = SUBREG_REG (rtl);
2160         /* Drop thru.  */
2161
2162     case REG:
2163         ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_REG);
2164         output_reg_number (rtl);
2165         break;
2166
2167     case MEM:
2168       output_mem_loc_descriptor (XEXP (rtl, 0));
2169       break;
2170
2171     default:
2172       abort ();         /* Should never happen */
2173     }
2174 }
2175
2176 /* Given a tree node describing an array bound (either lower or upper)
2177    output a representation for that bound.  DIM_NUM is used for
2178    multi-dimensional arrays and U_OR_L designates upper or lower
2179    bound.  */
2180
2181 static void
2182 output_bound_representation (tree bound, unsigned int dim_num, char u_or_l)
2183 {
2184   switch (TREE_CODE (bound))
2185     {
2186
2187     case ERROR_MARK:
2188       return;
2189
2190       /* All fixed-bounds are represented by INTEGER_CST nodes.  */
2191
2192     case INTEGER_CST:
2193       if (host_integerp (bound, 0))
2194         ASM_OUTPUT_DWARF_DATA4 (asm_out_file, tree_low_cst (bound, 0));
2195       break;
2196
2197     default:
2198
2199       /* Dynamic bounds may be represented by NOP_EXPR nodes containing
2200          SAVE_EXPR nodes, in which case we can do something, or as
2201          an expression, which we cannot represent.  */
2202       {
2203         char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2204         char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2205
2206         sprintf (begin_label, BOUND_BEGIN_LABEL_FMT,
2207                  current_dienum, dim_num, u_or_l);
2208
2209         sprintf (end_label, BOUND_END_LABEL_FMT,
2210                  current_dienum, dim_num, u_or_l);
2211
2212         ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
2213         ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2214
2215         /* If optimization is turned on, the SAVE_EXPRs that describe
2216            how to access the upper bound values are essentially bogus.
2217            They only describe (at best) how to get at these values at
2218            the points in the generated code right after they have just
2219            been computed.  Worse yet, in the typical case, the upper
2220            bound values will not even *be* computed in the optimized
2221            code, so these SAVE_EXPRs are entirely bogus.
2222
2223            In order to compensate for this fact, we check here to see
2224            if optimization is enabled, and if so, we effectively create
2225            an empty location description for the (unknown and unknowable)
2226            upper bound.
2227
2228            This should not cause too much trouble for existing (stupid?)
2229            debuggers because they have to deal with empty upper bounds
2230            location descriptions anyway in order to be able to deal with
2231            incomplete array types.
2232
2233            Of course an intelligent debugger (GDB?) should be able to
2234            comprehend that a missing upper bound specification in a
2235            array type used for a storage class `auto' local array variable
2236            indicates that the upper bound is both unknown (at compile-
2237            time) and unknowable (at run-time) due to optimization.  */
2238
2239         if (! optimize)
2240           {
2241             while (TREE_CODE (bound) == NOP_EXPR
2242                    || TREE_CODE (bound) == CONVERT_EXPR)
2243               bound = TREE_OPERAND (bound, 0);
2244
2245             if (TREE_CODE (bound) == SAVE_EXPR
2246                 && SAVE_EXPR_RTL (bound))
2247               output_loc_descriptor
2248                 (eliminate_regs (SAVE_EXPR_RTL (bound), 0, NULL_RTX));
2249           }
2250
2251         ASM_OUTPUT_LABEL (asm_out_file, end_label);
2252       }
2253       break;
2254
2255     }
2256 }
2257
2258 /* Recursive function to output a sequence of value/name pairs for
2259    enumeration constants in reversed order.  This is called from
2260    enumeration_type_die.  */
2261
2262 static void
2263 output_enumeral_list (tree link)
2264 {
2265   if (link)
2266     {
2267       output_enumeral_list (TREE_CHAIN (link));
2268
2269       if (host_integerp (TREE_VALUE (link), 0))
2270         ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
2271                                 tree_low_cst (TREE_VALUE (link), 0));
2272
2273       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file,
2274                                IDENTIFIER_POINTER (TREE_PURPOSE (link)));
2275     }
2276 }
2277
2278 /* Given an unsigned value, round it up to the lowest multiple of `boundary'
2279    which is not less than the value itself.  */
2280
2281 static inline HOST_WIDE_INT
2282 ceiling (HOST_WIDE_INT value, unsigned int boundary)
2283 {
2284   return (((value + boundary - 1) / boundary) * boundary);
2285 }
2286
2287 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
2288    pointer to the declared type for the relevant field variable, or return
2289    `integer_type_node' if the given node turns out to be an ERROR_MARK node.  */
2290
2291 static inline tree
2292 field_type (tree decl)
2293 {
2294   tree type;
2295
2296   if (TREE_CODE (decl) == ERROR_MARK)
2297     return integer_type_node;
2298
2299   type = DECL_BIT_FIELD_TYPE (decl);
2300   if (type == NULL)
2301     type = TREE_TYPE (decl);
2302   return type;
2303 }
2304
2305 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
2306    node, return the alignment in bits for the type, or else return
2307    BITS_PER_WORD if the node actually turns out to be an ERROR_MARK node.  */
2308
2309 static inline unsigned int
2310 simple_type_align_in_bits (tree type)
2311 {
2312   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
2313 }
2314
2315 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
2316    node, return the size in bits for the type if it is a constant, or
2317    else return the alignment for the type if the type's size is not
2318    constant, or else return BITS_PER_WORD if the type actually turns out
2319    to be an ERROR_MARK node.  */
2320
2321 static inline unsigned HOST_WIDE_INT
2322 simple_type_size_in_bits (tree type)
2323 {
2324   tree type_size_tree;
2325
2326   if (TREE_CODE (type) == ERROR_MARK)
2327     return BITS_PER_WORD;
2328   type_size_tree = TYPE_SIZE (type);
2329
2330   if (type_size_tree == NULL_TREE)
2331     return 0;
2332   if (! host_integerp (type_size_tree, 1))
2333     return TYPE_ALIGN (type);
2334   return tree_low_cst (type_size_tree, 1);
2335 }
2336
2337 /* Given a pointer to what is assumed to be a FIELD_DECL node, compute and
2338    return the byte offset of the lowest addressed byte of the "containing
2339    object" for the given FIELD_DECL, or return 0 if we are unable to deter-
2340    mine what that offset is, either because the argument turns out to be a
2341    pointer to an ERROR_MARK node, or because the offset is actually variable.
2342    (We can't handle the latter case just yet.)  */
2343
2344 static HOST_WIDE_INT
2345 field_byte_offset (tree decl)
2346 {
2347   unsigned int type_align_in_bytes;
2348   unsigned int type_align_in_bits;
2349   unsigned HOST_WIDE_INT type_size_in_bits;
2350   HOST_WIDE_INT object_offset_in_align_units;
2351   HOST_WIDE_INT object_offset_in_bits;
2352   HOST_WIDE_INT object_offset_in_bytes;
2353   tree type;
2354   tree field_size_tree;
2355   HOST_WIDE_INT bitpos_int;
2356   HOST_WIDE_INT deepest_bitpos;
2357   unsigned HOST_WIDE_INT field_size_in_bits;
2358
2359   if (TREE_CODE (decl) == ERROR_MARK)
2360     return 0;
2361
2362   if (TREE_CODE (decl) != FIELD_DECL)
2363     abort ();
2364
2365   type = field_type (decl);
2366   field_size_tree = DECL_SIZE (decl);
2367
2368   /* The size could be unspecified if there was an error, or for
2369      a flexible array member.  */
2370   if (! field_size_tree)
2371     field_size_tree = bitsize_zero_node;
2372
2373   /* We cannot yet cope with fields whose positions or sizes are variable,
2374      so for now, when we see such things, we simply return 0.  Someday,
2375      we may be able to handle such cases, but it will be damn difficult.  */
2376
2377   if (! host_integerp (bit_position (decl), 0)
2378       || ! host_integerp (field_size_tree, 1))
2379     return 0;
2380
2381   bitpos_int = int_bit_position (decl);
2382   field_size_in_bits = tree_low_cst (field_size_tree, 1);
2383
2384   type_size_in_bits = simple_type_size_in_bits (type);
2385   type_align_in_bits = simple_type_align_in_bits (type);
2386   type_align_in_bytes = type_align_in_bits / BITS_PER_UNIT;
2387
2388   /* Note that the GCC front-end doesn't make any attempt to keep track
2389      of the starting bit offset (relative to the start of the containing
2390      structure type) of the hypothetical "containing object" for a bit-
2391      field.  Thus, when computing the byte offset value for the start of
2392      the "containing object" of a bit-field, we must deduce this infor-
2393      mation on our own.
2394
2395      This can be rather tricky to do in some cases.  For example, handling
2396      the following structure type definition when compiling for an i386/i486
2397      target (which only aligns long long's to 32-bit boundaries) can be very
2398      tricky:
2399
2400                 struct S {
2401                         int             field1;
2402                         long long       field2:31;
2403                 };
2404
2405      Fortunately, there is a simple rule-of-thumb which can be used in such
2406      cases.  When compiling for an i386/i486, GCC will allocate 8 bytes for
2407      the structure shown above.  It decides to do this based upon one simple
2408      rule for bit-field allocation.  Quite simply, GCC allocates each "con-
2409      taining object" for each bit-field at the first (i.e. lowest addressed)
2410      legitimate alignment boundary (based upon the required minimum alignment
2411      for the declared type of the field) which it can possibly use, subject
2412      to the condition that there is still enough available space remaining
2413      in the containing object (when allocated at the selected point) to
2414      fully accommodate all of the bits of the bit-field itself.
2415
2416      This simple rule makes it obvious why GCC allocates 8 bytes for each
2417      object of the structure type shown above.  When looking for a place to
2418      allocate the "containing object" for `field2', the compiler simply tries
2419      to allocate a 64-bit "containing object" at each successive 32-bit
2420      boundary (starting at zero) until it finds a place to allocate that 64-
2421      bit field such that at least 31 contiguous (and previously unallocated)
2422      bits remain within that selected 64 bit field.  (As it turns out, for
2423      the example above, the compiler finds that it is OK to allocate the
2424      "containing object" 64-bit field at bit-offset zero within the
2425      structure type.)
2426
2427      Here we attempt to work backwards from the limited set of facts we're
2428      given, and we try to deduce from those facts, where GCC must have
2429      believed that the containing object started (within the structure type).
2430
2431      The value we deduce is then used (by the callers of this routine) to
2432      generate AT_location and AT_bit_offset attributes for fields (both
2433      bit-fields and, in the case of AT_location, regular fields as well).  */
2434
2435   /* Figure out the bit-distance from the start of the structure to the
2436      "deepest" bit of the bit-field.  */
2437   deepest_bitpos = bitpos_int + field_size_in_bits;
2438
2439   /* This is the tricky part.  Use some fancy footwork to deduce where the
2440      lowest addressed bit of the containing object must be.  */
2441   object_offset_in_bits
2442     = ceiling (deepest_bitpos, type_align_in_bits) - type_size_in_bits;
2443
2444   /* Compute the offset of the containing object in "alignment units".  */
2445   object_offset_in_align_units = object_offset_in_bits / type_align_in_bits;
2446
2447   /* Compute the offset of the containing object in bytes.  */
2448   object_offset_in_bytes = object_offset_in_align_units * type_align_in_bytes;
2449
2450   /* The above code assumes that the field does not cross an alignment
2451      boundary.  This can happen if PCC_BITFIELD_TYPE_MATTERS is not defined,
2452      or if the structure is packed.  If this happens, then we get an object
2453      which starts after the bitfield, which means that the bit offset is
2454      negative.  Gdb fails when given negative bit offsets.  We avoid this
2455      by recomputing using the first bit of the bitfield.  This will give
2456      us an object which does not completely contain the bitfield, but it
2457      will be aligned, and it will contain the first bit of the bitfield.
2458
2459      However, only do this for a BYTES_BIG_ENDIAN target.  For a
2460      ! BYTES_BIG_ENDIAN target, bitpos_int + field_size_in_bits is the first
2461      first bit of the bitfield.  If we recompute using bitpos_int + 1 below,
2462      then we end up computing the object byte offset for the wrong word of the
2463      desired bitfield, which in turn causes the field offset to be negative
2464      in bit_offset_attribute.  */
2465   if (BYTES_BIG_ENDIAN
2466       && object_offset_in_bits > bitpos_int)
2467     {
2468       deepest_bitpos = bitpos_int + 1;
2469       object_offset_in_bits
2470         = ceiling (deepest_bitpos, type_align_in_bits) - type_size_in_bits;
2471       object_offset_in_align_units = (object_offset_in_bits
2472                                       / type_align_in_bits);
2473       object_offset_in_bytes = (object_offset_in_align_units
2474                                 * type_align_in_bytes);
2475     }
2476
2477   return object_offset_in_bytes;
2478 }
2479
2480 /****************************** attributes *********************************/
2481
2482 /* The following routines are responsible for writing out the various types
2483    of Dwarf attributes (and any following data bytes associated with them).
2484    These routines are listed in order based on the numerical codes of their
2485    associated attributes.  */
2486
2487 /* Generate an AT_sibling attribute.  */
2488
2489 static inline void
2490 sibling_attribute (void)
2491 {
2492   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2493
2494   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_sibling);
2495   sprintf (label, DIE_BEGIN_LABEL_FMT, NEXT_DIE_NUM);
2496   ASM_OUTPUT_DWARF_REF (asm_out_file, label);
2497 }
2498
2499 /* Output the form of location attributes suitable for whole variables and
2500    whole parameters.  Note that the location attributes for struct fields
2501    are generated by the routine `data_member_location_attribute' below.  */
2502
2503 static void
2504 location_attribute (rtx rtl)
2505 {
2506   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2507   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2508
2509   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_location);
2510   sprintf (begin_label, LOC_BEGIN_LABEL_FMT, current_dienum);
2511   sprintf (end_label, LOC_END_LABEL_FMT, current_dienum);
2512   ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
2513   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2514
2515   /* Handle a special case.  If we are about to output a location descriptor
2516      for a variable or parameter which has been optimized out of existence,
2517      don't do that.  Instead we output a zero-length location descriptor
2518      value as part of the location attribute.
2519
2520      A variable which has been optimized out of existence will have a
2521      DECL_RTL value which denotes a pseudo-reg.
2522
2523      Currently, in some rare cases, variables can have DECL_RTL values
2524      which look like (MEM (REG pseudo-reg#)).  These cases are due to
2525      bugs elsewhere in the compiler.  We treat such cases
2526      as if the variable(s) in question had been optimized out of existence.
2527
2528      Note that in all cases where we wish to express the fact that a
2529      variable has been optimized out of existence, we do not simply
2530      suppress the generation of the entire location attribute because
2531      the absence of a location attribute in certain kinds of DIEs is
2532      used to indicate something else entirely... i.e. that the DIE
2533      represents an object declaration, but not a definition.  So saith
2534      the PLSIG.
2535   */
2536
2537   if (! is_pseudo_reg (rtl)
2538       && (GET_CODE (rtl) != MEM || ! is_pseudo_reg (XEXP (rtl, 0))))
2539     output_loc_descriptor (rtl);
2540
2541   ASM_OUTPUT_LABEL (asm_out_file, end_label);
2542 }
2543
2544 /* Output the specialized form of location attribute used for data members
2545    of struct and union types.
2546
2547    In the special case of a FIELD_DECL node which represents a bit-field,
2548    the "offset" part of this special location descriptor must indicate the
2549    distance in bytes from the lowest-addressed byte of the containing
2550    struct or union type to the lowest-addressed byte of the "containing
2551    object" for the bit-field.  (See the `field_byte_offset' function above.)
2552
2553    For any given bit-field, the "containing object" is a hypothetical
2554    object (of some integral or enum type) within which the given bit-field
2555    lives.  The type of this hypothetical "containing object" is always the
2556    same as the declared type of the individual bit-field itself (for GCC
2557    anyway... the DWARF spec doesn't actually mandate this).
2558
2559    Note that it is the size (in bytes) of the hypothetical "containing
2560    object" which will be given in the AT_byte_size attribute for this
2561    bit-field.  (See the `byte_size_attribute' function below.)  It is
2562    also used when calculating the value of the AT_bit_offset attribute.
2563    (See the `bit_offset_attribute' function below.)  */
2564
2565 static void
2566 data_member_location_attribute (tree t)
2567 {
2568   unsigned object_offset_in_bytes;
2569   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2570   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2571
2572   if (TREE_CODE (t) == TREE_VEC)
2573     object_offset_in_bytes = tree_low_cst (BINFO_OFFSET (t), 0);
2574   else
2575     object_offset_in_bytes = field_byte_offset (t);
2576
2577   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_location);
2578   sprintf (begin_label, LOC_BEGIN_LABEL_FMT, current_dienum);
2579   sprintf (end_label, LOC_END_LABEL_FMT, current_dienum);
2580   ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
2581   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2582   ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_CONST);
2583   ASM_OUTPUT_DWARF_DATA4 (asm_out_file, object_offset_in_bytes);
2584   ASM_OUTPUT_DWARF_STACK_OP (asm_out_file, OP_ADD);
2585   ASM_OUTPUT_LABEL (asm_out_file, end_label);
2586 }
2587
2588 /* Output an AT_const_value attribute for a variable or a parameter which
2589    does not have a "location" either in memory or in a register.  These
2590    things can arise in GNU C when a constant is passed as an actual
2591    parameter to an inlined function.  They can also arise in C++ where
2592    declared constants do not necessarily get memory "homes".  */
2593
2594 static void
2595 const_value_attribute (rtx rtl)
2596 {
2597   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2598   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2599
2600   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_const_value_block4);
2601   sprintf (begin_label, LOC_BEGIN_LABEL_FMT, current_dienum);
2602   sprintf (end_label, LOC_END_LABEL_FMT, current_dienum);
2603   ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, end_label, begin_label);
2604   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2605
2606   switch (GET_CODE (rtl))
2607     {
2608       case CONST_INT:
2609         /* Note that a CONST_INT rtx could represent either an integer or
2610            a floating-point constant.  A CONST_INT is used whenever the
2611            constant will fit into a single word.  In all such cases, the
2612            original mode of the constant value is wiped out, and the
2613            CONST_INT rtx is assigned VOIDmode.  Since we no longer have
2614            precise mode information for these constants, we always just
2615            output them using 4 bytes.  */
2616
2617         ASM_OUTPUT_DWARF_DATA4 (asm_out_file, (unsigned) INTVAL (rtl));
2618         break;
2619
2620       case CONST_DOUBLE:
2621         /* Note that a CONST_DOUBLE rtx could represent either an integer
2622            or a floating-point constant.  A CONST_DOUBLE is used whenever
2623            the constant requires more than one word in order to be adequately
2624            represented.  In all such cases, the original mode of the constant
2625            value is preserved as the mode of the CONST_DOUBLE rtx, but for
2626            simplicity we always just output CONST_DOUBLEs using 8 bytes.  */
2627
2628         ASM_OUTPUT_DWARF_DATA8 (asm_out_file,
2629                                 (unsigned int) CONST_DOUBLE_HIGH (rtl),
2630                                 (unsigned int) CONST_DOUBLE_LOW (rtl));
2631         break;
2632
2633       case CONST_STRING:
2634         ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, XSTR (rtl, 0));
2635         break;
2636
2637       case SYMBOL_REF:
2638       case LABEL_REF:
2639       case CONST:
2640         ASM_OUTPUT_DWARF_ADDR_CONST (asm_out_file, rtl);
2641         break;
2642
2643       case PLUS:
2644         /* In cases where an inlined instance of an inline function is passed
2645            the address of an `auto' variable (which is local to the caller)
2646            we can get a situation where the DECL_RTL of the artificial
2647            local variable (for the inlining) which acts as a stand-in for
2648            the corresponding formal parameter (of the inline function)
2649            will look like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).
2650            This is not exactly a compile-time constant expression, but it
2651            isn't the address of the (artificial) local variable either.
2652            Rather, it represents the *value* which the artificial local
2653            variable always has during its lifetime.  We currently have no
2654            way to represent such quasi-constant values in Dwarf, so for now
2655            we just punt and generate an AT_const_value attribute with form
2656            FORM_BLOCK4 and a length of zero.  */
2657         break;
2658
2659       default:
2660         abort ();  /* No other kinds of rtx should be possible here.  */
2661     }
2662
2663   ASM_OUTPUT_LABEL (asm_out_file, end_label);
2664 }
2665
2666 /* Generate *either* an AT_location attribute or else an AT_const_value
2667    data attribute for a variable or a parameter.  We generate the
2668    AT_const_value attribute only in those cases where the given
2669    variable or parameter does not have a true "location" either in
2670    memory or in a register.  This can happen (for example) when a
2671    constant is passed as an actual argument in a call to an inline
2672    function.  (It's possible that these things can crop up in other
2673    ways also.)  Note that one type of constant value which can be
2674    passed into an inlined function is a constant pointer.  This can
2675    happen for example if an actual argument in an inlined function
2676    call evaluates to a compile-time constant address.  */
2677
2678 static void
2679 location_or_const_value_attribute (tree decl)
2680 {
2681   rtx rtl;
2682
2683   if (TREE_CODE (decl) == ERROR_MARK)
2684     return;
2685
2686   if ((TREE_CODE (decl) != VAR_DECL) && (TREE_CODE (decl) != PARM_DECL))
2687     {
2688       /* Should never happen.  */
2689       abort ();
2690       return;
2691     }
2692
2693   /* Here we have to decide where we are going to say the parameter "lives"
2694      (as far as the debugger is concerned).  We only have a couple of choices.
2695      GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.  DECL_RTL
2696      normally indicates where the parameter lives during most of the activa-
2697      tion of the function.  If optimization is enabled however, this could
2698      be either NULL or else a pseudo-reg.  Both of those cases indicate that
2699      the parameter doesn't really live anywhere (as far as the code generation
2700      parts of GCC are concerned) during most of the function's activation.
2701      That will happen (for example) if the parameter is never referenced
2702      within the function.
2703
2704      We could just generate a location descriptor here for all non-NULL
2705      non-pseudo values of DECL_RTL and ignore all of the rest, but we can
2706      be a little nicer than that if we also consider DECL_INCOMING_RTL in
2707      cases where DECL_RTL is NULL or is a pseudo-reg.
2708
2709      Note however that we can only get away with using DECL_INCOMING_RTL as
2710      a backup substitute for DECL_RTL in certain limited cases.  In cases
2711      where DECL_ARG_TYPE(decl) indicates the same type as TREE_TYPE(decl)
2712      we can be sure that the parameter was passed using the same type as it
2713      is declared to have within the function, and that its DECL_INCOMING_RTL
2714      points us to a place where a value of that type is passed.  In cases
2715      where DECL_ARG_TYPE(decl) and TREE_TYPE(decl) are different types
2716      however, we cannot (in general) use DECL_INCOMING_RTL as a backup
2717      substitute for DECL_RTL because in these cases, DECL_INCOMING_RTL
2718      points us to a value of some type which is *different* from the type
2719      of the parameter itself.  Thus, if we tried to use DECL_INCOMING_RTL
2720      to generate a location attribute in such cases, the debugger would
2721      end up (for example) trying to fetch a `float' from a place which
2722      actually contains the first part of a `double'.  That would lead to
2723      really incorrect and confusing output at debug-time, and we don't
2724      want that now do we?
2725
2726      So in general, we DO NOT use DECL_INCOMING_RTL as a backup for DECL_RTL
2727      in cases where DECL_ARG_TYPE(decl) != TREE_TYPE(decl).  There are a
2728      couple of cute exceptions however.  On little-endian machines we can
2729      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE(decl) is
2730      not the same as TREE_TYPE(decl) but only when DECL_ARG_TYPE(decl) is
2731      an integral type which is smaller than TREE_TYPE(decl).  These cases
2732      arise when (on a little-endian machine) a non-prototyped function has
2733      a parameter declared to be of type `short' or `char'.  In such cases,
2734      TREE_TYPE(decl) will be `short' or `char', DECL_ARG_TYPE(decl) will be
2735      `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
2736      passed `int' value.  If the debugger then uses that address to fetch a
2737      `short' or a `char' (on a little-endian machine) the result will be the
2738      correct data, so we allow for such exceptional cases below.
2739
2740      Note that our goal here is to describe the place where the given formal
2741      parameter lives during most of the function's activation (i.e. between
2742      the end of the prologue and the start of the epilogue).  We'll do that
2743      as best as we can.  Note however that if the given formal parameter is
2744      modified sometime during the execution of the function, then a stack
2745      backtrace (at debug-time) will show the function as having been called
2746      with the *new* value rather than the value which was originally passed
2747      in.  This happens rarely enough that it is not a major problem, but it
2748      *is* a problem, and I'd like to fix it.  A future version of dwarfout.c
2749      may generate two additional attributes for any given TAG_formal_parameter
2750      DIE which will describe the "passed type" and the "passed location" for
2751      the given formal parameter in addition to the attributes we now generate
2752      to indicate the "declared type" and the "active location" for each
2753      parameter.  This additional set of attributes could be used by debuggers
2754      for stack backtraces.
2755
2756      Separately, note that sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL
2757      can be NULL also.  This happens (for example) for inlined-instances of
2758      inline function formal parameters which are never referenced.  This really
2759      shouldn't be happening.  All PARM_DECL nodes should get valid non-NULL
2760      DECL_INCOMING_RTL values, but integrate.c doesn't currently generate
2761      these values for inlined instances of inline function parameters, so
2762      when we see such cases, we are just out-of-luck for the time
2763      being (until integrate.c gets fixed).
2764   */
2765
2766   /* Use DECL_RTL as the "location" unless we find something better.  */
2767   rtl = DECL_RTL (decl);
2768
2769   if (TREE_CODE (decl) == PARM_DECL)
2770     if (rtl == NULL_RTX || is_pseudo_reg (rtl))
2771       {
2772         /* This decl represents a formal parameter which was optimized out.  */
2773         tree declared_type = type_main_variant (TREE_TYPE (decl));
2774         tree passed_type = type_main_variant (DECL_ARG_TYPE (decl));
2775
2776         /* Note that DECL_INCOMING_RTL may be NULL in here, but we handle
2777            *all* cases where (rtl == NULL_RTX) just below.  */
2778
2779         if (declared_type == passed_type)
2780           rtl = DECL_INCOMING_RTL (decl);
2781         else if (! BYTES_BIG_ENDIAN)
2782           if (TREE_CODE (declared_type) == INTEGER_TYPE)
2783             /* NMS WTF? */
2784             if (TYPE_SIZE (declared_type) <= TYPE_SIZE (passed_type))
2785               rtl = DECL_INCOMING_RTL (decl);
2786       }
2787
2788   if (rtl == NULL_RTX)
2789     return;
2790
2791   rtl = eliminate_regs (rtl, 0, NULL_RTX);
2792 #ifdef LEAF_REG_REMAP
2793   if (current_function_uses_only_leaf_regs)
2794     leaf_renumber_regs_insn (rtl);
2795 #endif
2796
2797   switch (GET_CODE (rtl))
2798     {
2799     case ADDRESSOF:
2800       /* The address of a variable that was optimized away; don't emit
2801          anything.  */
2802       break;
2803
2804     case CONST_INT:
2805     case CONST_DOUBLE:
2806     case CONST_STRING:
2807     case SYMBOL_REF:
2808     case LABEL_REF:
2809     case CONST:
2810     case PLUS:  /* DECL_RTL could be (plus (reg ...) (const_int ...)) */
2811       const_value_attribute (rtl);
2812       break;
2813
2814     case MEM:
2815     case REG:
2816     case SUBREG:
2817       location_attribute (rtl);
2818       break;
2819
2820     case CONCAT:
2821       /* ??? CONCAT is used for complex variables, which may have the real
2822          part stored in one place and the imag part stored somewhere else.
2823          DWARF1 has no way to describe a variable that lives in two different
2824          places, so we just describe where the first part lives, and hope that
2825          the second part is stored after it.  */
2826       location_attribute (XEXP (rtl, 0));
2827       break;
2828
2829     default:
2830       abort ();         /* Should never happen.  */
2831     }
2832 }
2833
2834 /* Generate an AT_name attribute given some string value to be included as
2835    the value of the attribute.  */
2836
2837 static inline void
2838 name_attribute (const char *name_string)
2839 {
2840   if (name_string && *name_string)
2841     {
2842       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_name);
2843       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, name_string);
2844     }
2845 }
2846
2847 static inline void
2848 fund_type_attribute (unsigned int ft_code)
2849 {
2850   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_fund_type);
2851   ASM_OUTPUT_DWARF_FUND_TYPE (asm_out_file, ft_code);
2852 }
2853
2854 static void
2855 mod_fund_type_attribute (tree type, int decl_const, int decl_volatile)
2856 {
2857   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2858   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2859
2860   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_mod_fund_type);
2861   sprintf (begin_label, MT_BEGIN_LABEL_FMT, current_dienum);
2862   sprintf (end_label, MT_END_LABEL_FMT, current_dienum);
2863   ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
2864   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2865   write_modifier_bytes (type, decl_const, decl_volatile);
2866   ASM_OUTPUT_DWARF_FUND_TYPE (asm_out_file,
2867                               fundamental_type_code (root_type (type)));
2868   ASM_OUTPUT_LABEL (asm_out_file, end_label);
2869 }
2870
2871 static inline void
2872 user_def_type_attribute (tree type)
2873 {
2874   char ud_type_name[MAX_ARTIFICIAL_LABEL_BYTES];
2875
2876   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_user_def_type);
2877   sprintf (ud_type_name, TYPE_NAME_FMT, TYPE_UID (type));
2878   ASM_OUTPUT_DWARF_REF (asm_out_file, ud_type_name);
2879 }
2880
2881 static void
2882 mod_u_d_type_attribute (tree type, int decl_const, int decl_volatile)
2883 {
2884   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2885   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2886   char ud_type_name[MAX_ARTIFICIAL_LABEL_BYTES];
2887
2888   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_mod_u_d_type);
2889   sprintf (begin_label, MT_BEGIN_LABEL_FMT, current_dienum);
2890   sprintf (end_label, MT_END_LABEL_FMT, current_dienum);
2891   ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
2892   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2893   write_modifier_bytes (type, decl_const, decl_volatile);
2894   sprintf (ud_type_name, TYPE_NAME_FMT, TYPE_UID (root_type (type)));
2895   ASM_OUTPUT_DWARF_REF (asm_out_file, ud_type_name);
2896   ASM_OUTPUT_LABEL (asm_out_file, end_label);
2897 }
2898
2899 #ifdef USE_ORDERING_ATTRIBUTE
2900 static inline void
2901 ordering_attribute (unsigned ordering)
2902 {
2903   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_ordering);
2904   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, ordering);
2905 }
2906 #endif /* defined(USE_ORDERING_ATTRIBUTE) */
2907
2908 /* Note that the block of subscript information for an array type also
2909    includes information about the element type of type given array type.  */
2910
2911 static void
2912 subscript_data_attribute (tree type)
2913 {
2914   unsigned dimension_number;
2915   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
2916   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
2917
2918   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_subscr_data);
2919   sprintf (begin_label, SS_BEGIN_LABEL_FMT, current_dienum);
2920   sprintf (end_label, SS_END_LABEL_FMT, current_dienum);
2921   ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
2922   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
2923
2924   /* The GNU compilers represent multidimensional array types as sequences
2925      of one dimensional array types whose element types are themselves array
2926      types.  Here we squish that down, so that each multidimensional array
2927      type gets only one array_type DIE in the Dwarf debugging info.  The
2928      draft Dwarf specification say that we are allowed to do this kind
2929      of compression in C (because there is no difference between an
2930      array or arrays and a multidimensional array in C) but for other
2931      source languages (e.g. Ada) we probably shouldn't do this.  */
2932
2933   for (dimension_number = 0;
2934         TREE_CODE (type) == ARRAY_TYPE;
2935         type = TREE_TYPE (type), dimension_number++)
2936     {
2937       tree domain = TYPE_DOMAIN (type);
2938
2939       /* Arrays come in three flavors.  Unspecified bounds, fixed
2940          bounds, and (in GNU C only) variable bounds.  Handle all
2941          three forms here.  */
2942
2943       if (domain)
2944         {
2945           /* We have an array type with specified bounds.  */
2946
2947           tree lower = TYPE_MIN_VALUE (domain);
2948           tree upper = TYPE_MAX_VALUE (domain);
2949
2950           /* Handle only fundamental types as index types for now.  */
2951           if (! type_is_fundamental (domain))
2952             abort ();
2953
2954           /* Output the representation format byte for this dimension.  */
2955           ASM_OUTPUT_DWARF_FMT_BYTE (asm_out_file,
2956                   FMT_CODE (1, TREE_CODE (lower) == INTEGER_CST,
2957                             upper && TREE_CODE (upper) == INTEGER_CST));
2958
2959           /* Output the index type for this dimension.  */
2960           ASM_OUTPUT_DWARF_FUND_TYPE (asm_out_file,
2961                                       fundamental_type_code (domain));
2962
2963           /* Output the representation for the lower bound.  */
2964           output_bound_representation (lower, dimension_number, 'l');
2965
2966           /* Output the representation for the upper bound.  */
2967           if (upper)
2968             output_bound_representation (upper, dimension_number, 'u');
2969           else
2970             ASM_OUTPUT_DWARF_DATA2 (asm_out_file, 0);
2971         }
2972       else
2973         {
2974           /* We have an array type with an unspecified length.  For C and
2975              C++ we can assume that this really means that (a) the index
2976              type is an integral type, and (b) the lower bound is zero.
2977              Note that Dwarf defines the representation of an unspecified
2978              (upper) bound as being a zero-length location description.  */
2979
2980           /* Output the array-bounds format byte.  */
2981
2982           ASM_OUTPUT_DWARF_FMT_BYTE (asm_out_file, FMT_FT_C_X);
2983
2984           /* Output the (assumed) index type.  */
2985
2986           ASM_OUTPUT_DWARF_FUND_TYPE (asm_out_file, FT_integer);
2987
2988           /* Output the (assumed) lower bound (constant) value.  */
2989
2990           ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
2991
2992           /* Output the (empty) location description for the upper bound.  */
2993
2994           ASM_OUTPUT_DWARF_DATA2 (asm_out_file, 0);
2995         }
2996     }
2997
2998   /* Output the prefix byte that says that the element type is coming up.  */
2999
3000   ASM_OUTPUT_DWARF_FMT_BYTE (asm_out_file, FMT_ET);
3001
3002   /* Output a representation of the type of the elements of this array type.  */
3003
3004   type_attribute (type, 0, 0);
3005
3006   ASM_OUTPUT_LABEL (asm_out_file, end_label);
3007 }
3008
3009 static void
3010 byte_size_attribute (tree tree_node)
3011 {
3012   unsigned size;
3013
3014   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_byte_size);
3015   switch (TREE_CODE (tree_node))
3016     {
3017       case ERROR_MARK:
3018         size = 0;
3019         break;
3020
3021       case ENUMERAL_TYPE:
3022       case RECORD_TYPE:
3023       case UNION_TYPE:
3024       case QUAL_UNION_TYPE:
3025       case ARRAY_TYPE:
3026         size = int_size_in_bytes (tree_node);
3027         break;
3028
3029       case FIELD_DECL:
3030         /* For a data member of a struct or union, the AT_byte_size is
3031            generally given as the number of bytes normally allocated for
3032            an object of the *declared* type of the member itself.  This
3033            is true even for bit-fields.  */
3034         size = simple_type_size_in_bits (field_type (tree_node))
3035                / BITS_PER_UNIT;
3036         break;
3037
3038       default:
3039         abort ();
3040     }
3041
3042   /* Note that `size' might be -1 when we get to this point.  If it
3043      is, that indicates that the byte size of the entity in question
3044      is variable.  We have no good way of expressing this fact in Dwarf
3045      at the present time, so just let the -1 pass on through.  */
3046
3047   ASM_OUTPUT_DWARF_DATA4 (asm_out_file, size);
3048 }
3049
3050 /* For a FIELD_DECL node which represents a bit-field, output an attribute
3051    which specifies the distance in bits from the highest order bit of the
3052    "containing object" for the bit-field to the highest order bit of the
3053    bit-field itself.
3054
3055    For any given bit-field, the "containing object" is a hypothetical
3056    object (of some integral or enum type) within which the given bit-field
3057    lives.  The type of this hypothetical "containing object" is always the
3058    same as the declared type of the individual bit-field itself.
3059
3060    The determination of the exact location of the "containing object" for
3061    a bit-field is rather complicated.  It's handled by the `field_byte_offset'
3062    function (above).
3063
3064    Note that it is the size (in bytes) of the hypothetical "containing
3065    object" which will be given in the AT_byte_size attribute for this
3066    bit-field.  (See `byte_size_attribute' above.) */
3067
3068 static inline void
3069 bit_offset_attribute (tree decl)
3070 {
3071   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
3072   tree type = DECL_BIT_FIELD_TYPE (decl);
3073   HOST_WIDE_INT bitpos_int;
3074   HOST_WIDE_INT highest_order_object_bit_offset;
3075   HOST_WIDE_INT highest_order_field_bit_offset;
3076   HOST_WIDE_INT bit_offset;
3077
3078   /* Must be a bit field.  */
3079   if (!type
3080       || TREE_CODE (decl) != FIELD_DECL)
3081     abort ();
3082
3083   /* We can't yet handle bit-fields whose offsets or sizes are variable, so
3084      if we encounter such things, just return without generating any
3085      attribute whatsoever.  */
3086
3087   if (! host_integerp (bit_position (decl), 0)
3088       || ! host_integerp (DECL_SIZE (decl), 1))
3089     return;
3090
3091   bitpos_int = int_bit_position (decl);
3092
3093   /* Note that the bit offset is always the distance (in bits) from the
3094      highest-order bit of the "containing object" to the highest-order
3095      bit of the bit-field itself.  Since the "high-order end" of any
3096      object or field is different on big-endian and little-endian machines,
3097      the computation below must take account of these differences.  */
3098
3099   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
3100   highest_order_field_bit_offset = bitpos_int;
3101
3102   if (! BYTES_BIG_ENDIAN)
3103     {
3104       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 1);
3105       highest_order_object_bit_offset += simple_type_size_in_bits (type);
3106     }
3107
3108   bit_offset =
3109     (! BYTES_BIG_ENDIAN
3110      ? highest_order_object_bit_offset - highest_order_field_bit_offset
3111      : highest_order_field_bit_offset - highest_order_object_bit_offset);
3112
3113   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_bit_offset);
3114   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, bit_offset);
3115 }
3116
3117 /* For a FIELD_DECL node which represents a bit field, output an attribute
3118    which specifies the length in bits of the given field.  */
3119
3120 static inline void
3121 bit_size_attribute (tree decl)
3122 {
3123   /* Must be a field and a bit field.  */
3124   if (TREE_CODE (decl) != FIELD_DECL
3125       || ! DECL_BIT_FIELD_TYPE (decl))
3126     abort ();
3127
3128   if (host_integerp (DECL_SIZE (decl), 1))
3129     {
3130       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_bit_size);
3131       ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
3132                               tree_low_cst (DECL_SIZE (decl), 1));
3133     }
3134 }
3135
3136 /* The following routine outputs the `element_list' attribute for enumeration
3137    type DIEs.  The element_lits attribute includes the names and values of
3138    all of the enumeration constants associated with the given enumeration
3139    type.  */
3140
3141 static inline void
3142 element_list_attribute (tree element)
3143 {
3144   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
3145   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3146
3147   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_element_list);
3148   sprintf (begin_label, EE_BEGIN_LABEL_FMT, current_dienum);
3149   sprintf (end_label, EE_END_LABEL_FMT, current_dienum);
3150   ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, end_label, begin_label);
3151   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
3152
3153   /* Here we output a list of value/name pairs for each enumeration constant
3154      defined for this enumeration type (as required), but we do it in REVERSE
3155      order.  The order is the one required by the draft #5 Dwarf specification
3156      published by the UI/PLSIG.  */
3157
3158   output_enumeral_list (element);   /* Recursively output the whole list.  */
3159
3160   ASM_OUTPUT_LABEL (asm_out_file, end_label);
3161 }
3162
3163 /* Generate an AT_stmt_list attribute.  These are normally present only in
3164    DIEs with a TAG_compile_unit tag.  */
3165
3166 static inline void
3167 stmt_list_attribute (const char *label)
3168 {
3169   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_stmt_list);
3170   /* Don't use ASM_OUTPUT_DWARF_DATA4 here.  */
3171   ASM_OUTPUT_DWARF_ADDR (asm_out_file, label);
3172 }
3173
3174 /* Generate an AT_low_pc attribute for a label DIE, a lexical_block DIE or
3175    for a subroutine DIE.  */
3176
3177 static inline void
3178 low_pc_attribute (const char *asm_low_label)
3179 {
3180   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_low_pc);
3181   ASM_OUTPUT_DWARF_ADDR (asm_out_file, asm_low_label);
3182 }
3183
3184 /* Generate an AT_high_pc attribute for a lexical_block DIE or for a
3185    subroutine DIE.  */
3186
3187 static inline void
3188 high_pc_attribute (const char *asm_high_label)
3189 {
3190   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_high_pc);
3191   ASM_OUTPUT_DWARF_ADDR (asm_out_file, asm_high_label);
3192 }
3193
3194 /* Generate an AT_body_begin attribute for a subroutine DIE.  */
3195
3196 static inline void
3197 body_begin_attribute (const char *asm_begin_label)
3198 {
3199   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_body_begin);
3200   ASM_OUTPUT_DWARF_ADDR (asm_out_file, asm_begin_label);
3201 }
3202
3203 /* Generate an AT_body_end attribute for a subroutine DIE.  */
3204
3205 static inline void
3206 body_end_attribute (const char *asm_end_label)
3207 {
3208   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_body_end);
3209   ASM_OUTPUT_DWARF_ADDR (asm_out_file, asm_end_label);
3210 }
3211
3212 /* Generate an AT_language attribute given a LANG value.  These attributes
3213    are used only within TAG_compile_unit DIEs.  */
3214
3215 static inline void
3216 language_attribute (unsigned int language_code)
3217 {
3218   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_language);
3219   ASM_OUTPUT_DWARF_DATA4 (asm_out_file, language_code);
3220 }
3221
3222 static inline void
3223 member_attribute (tree context)
3224 {
3225   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3226
3227   /* Generate this attribute only for members in C++.  */
3228
3229   if (context != NULL && is_tagged_type (context))
3230     {
3231       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_member);
3232       sprintf (label, TYPE_NAME_FMT, TYPE_UID (context));
3233       ASM_OUTPUT_DWARF_REF (asm_out_file, label);
3234     }
3235 }
3236
3237 #if 0
3238 #ifndef SL_BEGIN_LABEL_FMT
3239 #define SL_BEGIN_LABEL_FMT      "*.L_sl%u"
3240 #endif
3241 #ifndef SL_END_LABEL_FMT
3242 #define SL_END_LABEL_FMT        "*.L_sl%u_e"
3243 #endif
3244
3245 static inline void
3246 string_length_attribute (tree upper_bound)
3247 {
3248   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
3249   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3250
3251   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_string_length);
3252   sprintf (begin_label, SL_BEGIN_LABEL_FMT, current_dienum);
3253   sprintf (end_label, SL_END_LABEL_FMT, current_dienum);
3254   ASM_OUTPUT_DWARF_DELTA2 (asm_out_file, end_label, begin_label);
3255   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
3256   output_bound_representation (upper_bound, 0, 'u');
3257   ASM_OUTPUT_LABEL (asm_out_file, end_label);
3258 }
3259 #endif
3260
3261 static inline void
3262 comp_dir_attribute (const char *dirname)
3263 {
3264   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_comp_dir);
3265   ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, dirname);
3266 }
3267
3268 static inline void
3269 sf_names_attribute (const char *sf_names_start_label)
3270 {
3271   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_sf_names);
3272   /* Don't use ASM_OUTPUT_DWARF_DATA4 here.  */
3273   ASM_OUTPUT_DWARF_ADDR (asm_out_file, sf_names_start_label);
3274 }
3275
3276 static inline void
3277 src_info_attribute (const char *src_info_start_label)
3278 {
3279   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_src_info);
3280   /* Don't use ASM_OUTPUT_DWARF_DATA4 here.  */
3281   ASM_OUTPUT_DWARF_ADDR (asm_out_file, src_info_start_label);
3282 }
3283
3284 static inline void
3285 mac_info_attribute (const char *mac_info_start_label)
3286 {
3287   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_mac_info);
3288   /* Don't use ASM_OUTPUT_DWARF_DATA4 here.  */
3289   ASM_OUTPUT_DWARF_ADDR (asm_out_file, mac_info_start_label);
3290 }
3291
3292 static inline void
3293 prototyped_attribute (tree func_type)
3294 {
3295   if ((strcmp (lang_hooks.name, "GNU C") == 0)
3296       && (TYPE_ARG_TYPES (func_type) != NULL))
3297     {
3298       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_prototyped);
3299       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
3300     }
3301 }
3302
3303 static inline void
3304 producer_attribute (const char *producer)
3305 {
3306   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_producer);
3307   ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, producer);
3308 }
3309
3310 static inline void
3311 inline_attribute (tree decl)
3312 {
3313   if (DECL_INLINE (decl))
3314     {
3315       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_inline);
3316       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
3317     }
3318 }
3319
3320 static inline void
3321 containing_type_attribute (tree containing_type)
3322 {
3323   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3324
3325   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_containing_type);
3326   sprintf (label, TYPE_NAME_FMT, TYPE_UID (containing_type));
3327   ASM_OUTPUT_DWARF_REF (asm_out_file, label);
3328 }
3329
3330 static inline void
3331 abstract_origin_attribute (tree origin)
3332 {
3333   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3334
3335   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_abstract_origin);
3336   switch (TREE_CODE_CLASS (TREE_CODE (origin)))
3337     {
3338     case 'd':
3339       sprintf (label, DECL_NAME_FMT, DECL_UID (origin));
3340       break;
3341
3342     case 't':
3343       sprintf (label, TYPE_NAME_FMT, TYPE_UID (origin));
3344       break;
3345
3346     default:
3347       abort ();         /* Should never happen.  */
3348
3349     }
3350   ASM_OUTPUT_DWARF_REF (asm_out_file, label);
3351 }
3352
3353 #ifdef DWARF_DECL_COORDINATES
3354 static inline void
3355 src_coords_attribute (unsigned src_fileno, unsigned src_lineno)
3356 {
3357   ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_src_coords);
3358   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, src_fileno);
3359   ASM_OUTPUT_DWARF_DATA2 (asm_out_file, src_lineno);
3360 }
3361 #endif /* defined(DWARF_DECL_COORDINATES) */
3362
3363 static inline void
3364 pure_or_virtual_attribute (tree func_decl)
3365 {
3366   if (DECL_VIRTUAL_P (func_decl))
3367     {
3368 #if 0 /* DECL_ABSTRACT_VIRTUAL_P is C++-specific.  */
3369       if (DECL_ABSTRACT_VIRTUAL_P (func_decl))
3370         ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_pure_virtual);
3371       else
3372 #endif
3373         ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_virtual);
3374       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
3375     }
3376 }
3377
3378 /************************* end of attributes *****************************/
3379
3380 /********************* utility routines for DIEs *************************/
3381
3382 /* Output an AT_name attribute and an AT_src_coords attribute for the
3383    given decl, but only if it actually has a name.  */
3384
3385 static void
3386 name_and_src_coords_attributes (tree decl)
3387 {
3388   tree decl_name = DECL_NAME (decl);
3389
3390   if (decl_name && IDENTIFIER_POINTER (decl_name))
3391     {
3392       name_attribute (IDENTIFIER_POINTER (decl_name));
3393 #ifdef DWARF_DECL_COORDINATES
3394       {
3395         register unsigned file_index;
3396
3397         /* This is annoying, but we have to pop out of the .debug section
3398            for a moment while we call `lookup_filename' because calling it
3399            may cause a temporary switch into the .debug_sfnames section and
3400            most svr4 assemblers are not smart enough to be able to nest
3401            section switches to any depth greater than one.  Note that we
3402            also can't skirt this issue by delaying all output to the
3403            .debug_sfnames section unit the end of compilation because that
3404            would cause us to have inter-section forward references and
3405            Fred Fish sez that m68k/svr4 assemblers botch those.  */
3406
3407         ASM_OUTPUT_POP_SECTION (asm_out_file);
3408         file_index = lookup_filename (DECL_SOURCE_FILE (decl));
3409         ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SECTION);
3410
3411         src_coords_attribute (file_index, DECL_SOURCE_LINE (decl));
3412       }
3413 #endif /* defined(DWARF_DECL_COORDINATES) */
3414     }
3415 }
3416
3417 /* Many forms of DIEs contain a "type description" part.  The following
3418    routine writes out these "type descriptor" parts.  */
3419
3420 static void
3421 type_attribute (tree type, int decl_const, int decl_volatile)
3422 {
3423   enum tree_code code = TREE_CODE (type);
3424   int root_type_modified;
3425
3426   if (code == ERROR_MARK)
3427     return;
3428
3429   /* Handle a special case.  For functions whose return type is void,
3430      we generate *no* type attribute.  (Note that no object may have
3431      type `void', so this only applies to function return types.  */
3432
3433   if (code == VOID_TYPE)
3434     return;
3435
3436   /* If this is a subtype, find the underlying type.  Eventually,
3437      this should write out the appropriate subtype info.  */
3438   while ((code == INTEGER_TYPE || code == REAL_TYPE)
3439          && TREE_TYPE (type) != 0)
3440     type = TREE_TYPE (type), code = TREE_CODE (type);
3441
3442   root_type_modified = (code == POINTER_TYPE || code == REFERENCE_TYPE
3443                         || decl_const || decl_volatile
3444                         || TYPE_READONLY (type) || TYPE_VOLATILE (type));
3445
3446   if (type_is_fundamental (root_type (type)))
3447     {
3448       if (root_type_modified)
3449         mod_fund_type_attribute (type, decl_const, decl_volatile);
3450       else
3451         fund_type_attribute (fundamental_type_code (type));
3452     }
3453   else
3454     {
3455       if (root_type_modified)
3456         mod_u_d_type_attribute (type, decl_const, decl_volatile);
3457       else
3458         /* We have to get the type_main_variant here (and pass that to the
3459            `user_def_type_attribute' routine) because the ..._TYPE node we
3460            have might simply be a *copy* of some original type node (where
3461            the copy was created to help us keep track of typedef names)
3462            and that copy might have a different TYPE_UID from the original
3463            ..._TYPE node.  (Note that when `equate_type_number_to_die_number'
3464            is labeling a given type DIE for future reference, it always and
3465            only creates labels for DIEs representing *main variants*, and it
3466            never even knows about non-main-variants.)  */
3467         user_def_type_attribute (type_main_variant (type));
3468     }
3469 }
3470
3471 /* Given a tree pointer to a struct, class, union, or enum type node, return
3472    a pointer to the (string) tag name for the given type, or zero if the
3473    type was declared without a tag.  */
3474
3475 static const char *
3476 type_tag (tree type)
3477 {
3478   const char *name = 0;
3479
3480   if (TYPE_NAME (type) != 0)
3481     {
3482       tree t = 0;
3483
3484       /* Find the IDENTIFIER_NODE for the type name.  */
3485       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
3486         t = TYPE_NAME (type);
3487
3488       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
3489          a TYPE_DECL node, regardless of whether or not a `typedef' was
3490          involved.  */
3491       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
3492                && ! DECL_IGNORED_P (TYPE_NAME (type)))
3493           t = DECL_NAME (TYPE_NAME (type));
3494
3495       /* Now get the name as a string, or invent one.  */
3496       if (t != 0)
3497         name = IDENTIFIER_POINTER (t);
3498     }
3499
3500   return (name == 0 || *name == '\0') ? 0 : name;
3501 }
3502
3503 static inline void
3504 dienum_push (void)
3505 {
3506   /* Start by checking if the pending_sibling_stack needs to be expanded.
3507      If necessary, expand it.  */
3508
3509   if (pending_siblings == pending_siblings_allocated)
3510     {
3511       pending_siblings_allocated += PENDING_SIBLINGS_INCREMENT;
3512       pending_sibling_stack
3513         = xrealloc (pending_sibling_stack,
3514                     pending_siblings_allocated * sizeof(unsigned));
3515     }
3516
3517   pending_siblings++;
3518   NEXT_DIE_NUM = next_unused_dienum++;
3519 }
3520
3521 /* Pop the sibling stack so that the most recently pushed DIEnum becomes the
3522    NEXT_DIE_NUM.  */
3523
3524 static inline void
3525 dienum_pop (void)
3526 {
3527   pending_siblings--;
3528 }
3529
3530 static inline tree
3531 member_declared_type (tree member)
3532 {
3533   return (DECL_BIT_FIELD_TYPE (member))
3534            ? DECL_BIT_FIELD_TYPE (member)
3535            : TREE_TYPE (member);
3536 }
3537
3538 /* Get the function's label, as described by its RTL.
3539    This may be different from the DECL_NAME name used
3540    in the source file.  */
3541
3542 static const char *
3543 function_start_label (tree decl)
3544 {
3545   rtx x;
3546   const char *fnname;
3547
3548   x = DECL_RTL (decl);
3549   if (GET_CODE (x) != MEM)
3550     abort ();
3551   x = XEXP (x, 0);
3552   if (GET_CODE (x) != SYMBOL_REF)
3553                abort ();
3554   fnname = XSTR (x, 0);
3555   return fnname;
3556 }
3557
3558
3559 /******************************* DIEs ************************************/
3560
3561 /* Output routines for individual types of DIEs.  */
3562
3563 /* Note that every type of DIE (except a null DIE) gets a sibling.  */
3564
3565 static void
3566 output_array_type_die (void *arg)
3567 {
3568   tree type = arg;
3569
3570   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_array_type);
3571   sibling_attribute ();
3572   equate_type_number_to_die_number (type);
3573   member_attribute (TYPE_CONTEXT (type));
3574
3575   /* I believe that we can default the array ordering.  SDB will probably
3576      do the right things even if AT_ordering is not present.  It's not
3577      even an issue until we start to get into multidimensional arrays
3578      anyway.  If SDB is ever caught doing the Wrong Thing for multi-
3579      dimensional arrays, then we'll have to put the AT_ordering attribute
3580      back in.  (But if and when we find out that we need to put these in,
3581      we will only do so for multidimensional arrays.  After all, we don't
3582      want to waste space in the .debug section now do we?)  */
3583
3584 #ifdef USE_ORDERING_ATTRIBUTE
3585   ordering_attribute (ORD_row_major);
3586 #endif /* defined(USE_ORDERING_ATTRIBUTE) */
3587
3588   subscript_data_attribute (type);
3589 }
3590
3591 static void
3592 output_set_type_die (void *arg)
3593 {
3594   tree type = arg;
3595
3596   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_set_type);
3597   sibling_attribute ();
3598   equate_type_number_to_die_number (type);
3599   member_attribute (TYPE_CONTEXT (type));
3600   type_attribute (TREE_TYPE (type), 0, 0);
3601 }
3602
3603 #if 0
3604 /* Implement this when there is a GNU FORTRAN or GNU Ada front end.  */
3605
3606 static void
3607 output_entry_point_die (void *arg)
3608 {
3609   tree decl = arg;
3610   tree origin = decl_ultimate_origin (decl);
3611
3612   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_entry_point);
3613   sibling_attribute ();
3614   dienum_push ();
3615   if (origin != NULL)
3616     abstract_origin_attribute (origin);
3617   else
3618     {
3619       name_and_src_coords_attributes (decl);
3620       member_attribute (DECL_CONTEXT (decl));
3621       type_attribute (TREE_TYPE (TREE_TYPE (decl)), 0, 0);
3622     }
3623   if (DECL_ABSTRACT (decl))
3624     equate_decl_number_to_die_number (decl);
3625   else
3626     low_pc_attribute (function_start_label (decl));
3627 }
3628 #endif
3629
3630 /* Output a DIE to represent an inlined instance of an enumeration type.  */
3631
3632 static void
3633 output_inlined_enumeration_type_die (void *arg)
3634 {
3635   tree type = arg;
3636
3637   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_enumeration_type);
3638   sibling_attribute ();
3639   if (!TREE_ASM_WRITTEN (type))
3640     abort ();
3641   abstract_origin_attribute (type);
3642 }
3643
3644 /* Output a DIE to represent an inlined instance of a structure type.  */
3645
3646 static void
3647 output_inlined_structure_type_die (void *arg)
3648 {
3649   tree type = arg;
3650
3651   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_structure_type);
3652   sibling_attribute ();
3653   if (!TREE_ASM_WRITTEN (type))
3654     abort ();
3655   abstract_origin_attribute (type);
3656 }
3657
3658 /* Output a DIE to represent an inlined instance of a union type.  */
3659
3660 static void
3661 output_inlined_union_type_die (void *arg)
3662 {
3663   tree type = arg;
3664
3665   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_union_type);
3666   sibling_attribute ();
3667   if (!TREE_ASM_WRITTEN (type))
3668     abort ();
3669   abstract_origin_attribute (type);
3670 }
3671
3672 /* Output a DIE to represent an enumeration type.  Note that these DIEs
3673    include all of the information about the enumeration values also.
3674    This information is encoded into the element_list attribute.  */
3675
3676 static void
3677 output_enumeration_type_die (void *arg)
3678 {
3679   tree type = arg;
3680
3681   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_enumeration_type);
3682   sibling_attribute ();
3683   equate_type_number_to_die_number (type);
3684   name_attribute (type_tag (type));
3685   member_attribute (TYPE_CONTEXT (type));
3686
3687   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
3688      given enum type is incomplete, do not generate the AT_byte_size
3689      attribute or the AT_element_list attribute.  */
3690
3691   if (COMPLETE_TYPE_P (type))
3692     {
3693       byte_size_attribute (type);
3694       element_list_attribute (TYPE_FIELDS (type));
3695     }
3696 }
3697
3698 /* Output a DIE to represent either a real live formal parameter decl or
3699    to represent just the type of some formal parameter position in some
3700    function type.
3701
3702    Note that this routine is a bit unusual because its argument may be
3703    a ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
3704    represents an inlining of some PARM_DECL) or else some sort of a
3705    ..._TYPE node.  If it's the former then this function is being called
3706    to output a DIE to represent a formal parameter object (or some inlining
3707    thereof).  If it's the latter, then this function is only being called
3708    to output a TAG_formal_parameter DIE to stand as a placeholder for some
3709    formal argument type of some subprogram type.  */
3710
3711 static void
3712 output_formal_parameter_die (void *arg)
3713 {
3714   tree node = arg;
3715
3716   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_formal_parameter);
3717   sibling_attribute ();
3718
3719   switch (TREE_CODE_CLASS (TREE_CODE (node)))
3720     {
3721     case 'd':   /* We were called with some kind of a ..._DECL node.  */
3722       {
3723         register tree origin = decl_ultimate_origin (node);
3724
3725         if (origin != NULL)
3726           abstract_origin_attribute (origin);
3727         else
3728           {
3729             name_and_src_coords_attributes (node);
3730             type_attribute (TREE_TYPE (node),
3731                             TREE_READONLY (node), TREE_THIS_VOLATILE (node));
3732           }
3733         if (DECL_ABSTRACT (node))
3734           equate_decl_number_to_die_number (node);
3735         else
3736           location_or_const_value_attribute (node);
3737       }
3738       break;
3739
3740     case 't':   /* We were called with some kind of a ..._TYPE node.  */
3741       type_attribute (node, 0, 0);
3742       break;
3743
3744     default:
3745       abort (); /* Should never happen.  */
3746     }
3747 }
3748
3749 /* Output a DIE to represent a declared function (either file-scope
3750    or block-local) which has "external linkage" (according to ANSI-C).  */
3751
3752 static void
3753 output_global_subroutine_die (void *arg)
3754 {
3755   tree decl = arg;
3756   tree origin = decl_ultimate_origin (decl);
3757
3758   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_global_subroutine);
3759   sibling_attribute ();
3760   dienum_push ();
3761   if (origin != NULL)
3762     abstract_origin_attribute (origin);
3763   else
3764     {
3765       tree type = TREE_TYPE (decl);
3766
3767       name_and_src_coords_attributes (decl);
3768       inline_attribute (decl);
3769       prototyped_attribute (type);
3770       member_attribute (DECL_CONTEXT (decl));
3771       type_attribute (TREE_TYPE (type), 0, 0);
3772       pure_or_virtual_attribute (decl);
3773     }
3774   if (DECL_ABSTRACT (decl))
3775     equate_decl_number_to_die_number (decl);
3776   else
3777     {
3778       if (! DECL_EXTERNAL (decl) && ! in_class
3779           && decl == current_function_decl)
3780         {
3781           char label[MAX_ARTIFICIAL_LABEL_BYTES];
3782
3783           low_pc_attribute (function_start_label (decl));
3784           sprintf (label, FUNC_END_LABEL_FMT, current_function_funcdef_no);
3785           high_pc_attribute (label);
3786           if (use_gnu_debug_info_extensions)
3787             {
3788               sprintf (label, BODY_BEGIN_LABEL_FMT,
3789                        current_function_funcdef_no);
3790               body_begin_attribute (label);
3791               sprintf (label, BODY_END_LABEL_FMT, current_function_funcdef_no);
3792               body_end_attribute (label);
3793             }
3794         }
3795     }
3796 }
3797
3798 /* Output a DIE to represent a declared data object (either file-scope
3799    or block-local) which has "external linkage" (according to ANSI-C).  */
3800
3801 static void
3802 output_global_variable_die (void *arg)
3803 {
3804   tree decl = arg;
3805   tree origin = decl_ultimate_origin (decl);
3806
3807   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_global_variable);
3808   sibling_attribute ();
3809   if (origin != NULL)
3810     abstract_origin_attribute (origin);
3811   else
3812     {
3813       name_and_src_coords_attributes (decl);
3814       member_attribute (DECL_CONTEXT (decl));
3815       type_attribute (TREE_TYPE (decl),
3816                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl));
3817     }
3818   if (DECL_ABSTRACT (decl))
3819     equate_decl_number_to_die_number (decl);
3820   else
3821     {
3822       if (! DECL_EXTERNAL (decl) && ! in_class
3823           && current_function_decl == decl_function_context (decl))
3824         location_or_const_value_attribute (decl);
3825     }
3826 }
3827
3828 static void
3829 output_label_die (void *arg)
3830 {
3831   tree decl = arg;
3832   tree origin = decl_ultimate_origin (decl);
3833
3834   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_label);
3835   sibling_attribute ();
3836   if (origin != NULL)
3837     abstract_origin_attribute (origin);
3838   else
3839     name_and_src_coords_attributes (decl);
3840   if (DECL_ABSTRACT (decl))
3841     equate_decl_number_to_die_number (decl);
3842   else
3843     {
3844       rtx insn = DECL_RTL (decl);
3845
3846       /* Deleted labels are programmer specified labels which have been
3847          eliminated because of various optimizations.  We still emit them
3848          here so that it is possible to put breakpoints on them.  */
3849       if (GET_CODE (insn) == CODE_LABEL
3850           || ((GET_CODE (insn) == NOTE
3851                && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
3852         {
3853           char label[MAX_ARTIFICIAL_LABEL_BYTES];
3854
3855           /* When optimization is enabled (via -O) some parts of the compiler
3856              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
3857              represent source-level labels which were explicitly declared by
3858              the user.  This really shouldn't be happening though, so catch
3859              it if it ever does happen.  */
3860
3861           if (INSN_DELETED_P (insn))
3862             abort ();   /* Should never happen.  */
3863
3864           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
3865           low_pc_attribute (label);
3866         }
3867     }
3868 }
3869
3870 static void
3871 output_lexical_block_die (void *arg)
3872 {
3873   tree stmt = arg;
3874
3875   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_lexical_block);
3876   sibling_attribute ();
3877   dienum_push ();
3878   if (! BLOCK_ABSTRACT (stmt))
3879     {
3880       char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
3881       char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3882
3883       sprintf (begin_label, BLOCK_BEGIN_LABEL_FMT, BLOCK_NUMBER (stmt));
3884       low_pc_attribute (begin_label);
3885       sprintf (end_label, BLOCK_END_LABEL_FMT, BLOCK_NUMBER (stmt));
3886       high_pc_attribute (end_label);
3887     }
3888 }
3889
3890 static void
3891 output_inlined_subroutine_die (void *arg)
3892 {
3893   tree stmt = arg;
3894
3895   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_inlined_subroutine);
3896   sibling_attribute ();
3897   dienum_push ();
3898   abstract_origin_attribute (block_ultimate_origin (stmt));
3899   if (! BLOCK_ABSTRACT (stmt))
3900     {
3901       char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
3902       char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3903
3904       sprintf (begin_label, BLOCK_BEGIN_LABEL_FMT, BLOCK_NUMBER (stmt));
3905       low_pc_attribute (begin_label);
3906       sprintf (end_label, BLOCK_END_LABEL_FMT, BLOCK_NUMBER (stmt));
3907       high_pc_attribute (end_label);
3908     }
3909 }
3910
3911 /* Output a DIE to represent a declared data object (either file-scope
3912    or block-local) which has "internal linkage" (according to ANSI-C).  */
3913
3914 static void
3915 output_local_variable_die (void *arg)
3916 {
3917   tree decl = arg;
3918   tree origin = decl_ultimate_origin (decl);
3919
3920   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_local_variable);
3921   sibling_attribute ();
3922   if (origin != NULL)
3923     abstract_origin_attribute (origin);
3924   else
3925     {
3926       name_and_src_coords_attributes (decl);
3927       member_attribute (DECL_CONTEXT (decl));
3928       type_attribute (TREE_TYPE (decl),
3929                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl));
3930     }
3931   if (DECL_ABSTRACT (decl))
3932     equate_decl_number_to_die_number (decl);
3933   else
3934     location_or_const_value_attribute (decl);
3935 }
3936
3937 static void
3938 output_member_die (void *arg)
3939 {
3940   tree decl = arg;
3941
3942   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_member);
3943   sibling_attribute ();
3944   name_and_src_coords_attributes (decl);
3945   member_attribute (DECL_CONTEXT (decl));
3946   type_attribute (member_declared_type (decl),
3947                   TREE_READONLY (decl), TREE_THIS_VOLATILE (decl));
3948   if (DECL_BIT_FIELD_TYPE (decl))       /* If this is a bit field...  */
3949     {
3950       byte_size_attribute (decl);
3951       bit_size_attribute (decl);
3952       bit_offset_attribute (decl);
3953     }
3954   data_member_location_attribute (decl);
3955 }
3956
3957 #if 0
3958 /* Don't generate either pointer_type DIEs or reference_type DIEs.  Use
3959    modified types instead.
3960
3961    We keep this code here just in case these types of DIEs may be
3962    needed to represent certain things in other languages (e.g. Pascal)
3963    someday.  */
3964
3965 static void
3966 output_pointer_type_die (void *arg)
3967 {
3968   tree type = arg;
3969
3970   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_pointer_type);
3971   sibling_attribute ();
3972   equate_type_number_to_die_number (type);
3973   member_attribute (TYPE_CONTEXT (type));
3974   type_attribute (TREE_TYPE (type), 0, 0);
3975 }
3976
3977 static void
3978 output_reference_type_die (void *arg)
3979 {
3980   tree type = arg;
3981
3982   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_reference_type);
3983   sibling_attribute ();
3984   equate_type_number_to_die_number (type);
3985   member_attribute (TYPE_CONTEXT (type));
3986   type_attribute (TREE_TYPE (type), 0, 0);
3987 }
3988 #endif
3989
3990 static void
3991 output_ptr_to_mbr_type_die (void *arg)
3992 {
3993   tree type = arg;
3994
3995   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_ptr_to_member_type);
3996   sibling_attribute ();
3997   equate_type_number_to_die_number (type);
3998   member_attribute (TYPE_CONTEXT (type));
3999   containing_type_attribute (TYPE_OFFSET_BASETYPE (type));
4000   type_attribute (TREE_TYPE (type), 0, 0);
4001 }
4002
4003 static void
4004 output_compile_unit_die (void *arg)
4005 {
4006   const char *main_input_filename = arg;
4007   const char *language_string = lang_hooks.name;
4008
4009   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_compile_unit);
4010   sibling_attribute ();
4011   dienum_push ();
4012   name_attribute (main_input_filename);
4013
4014   {
4015     char producer[250];
4016
4017     sprintf (producer, "%s %s", language_string, version_string);
4018     producer_attribute (producer);
4019   }
4020
4021   if (strcmp (language_string, "GNU C++") == 0)
4022     language_attribute (LANG_C_PLUS_PLUS);
4023   else if (strcmp (language_string, "GNU Ada") == 0)
4024     language_attribute (LANG_ADA83);
4025   else if (strcmp (language_string, "GNU F77") == 0)
4026     language_attribute (LANG_FORTRAN77);
4027   else if (strcmp (language_string, "GNU Pascal") == 0)
4028     language_attribute (LANG_PASCAL83);
4029   else if (strcmp (language_string, "GNU Java") == 0)
4030     language_attribute (LANG_JAVA);
4031   else
4032     language_attribute (LANG_C89);
4033   low_pc_attribute (TEXT_BEGIN_LABEL);
4034   high_pc_attribute (TEXT_END_LABEL);
4035   if (debug_info_level >= DINFO_LEVEL_NORMAL)
4036     stmt_list_attribute (LINE_BEGIN_LABEL);
4037
4038   {
4039     const char *wd = get_src_pwd ();
4040     if (wd)
4041       comp_dir_attribute (wd);
4042   }
4043
4044   if (debug_info_level >= DINFO_LEVEL_NORMAL && use_gnu_debug_info_extensions)
4045     {
4046       sf_names_attribute (SFNAMES_BEGIN_LABEL);
4047       src_info_attribute (SRCINFO_BEGIN_LABEL);
4048       if (debug_info_level >= DINFO_LEVEL_VERBOSE)
4049         mac_info_attribute (MACINFO_BEGIN_LABEL);
4050     }
4051 }
4052
4053 static void
4054 output_string_type_die (void *arg)
4055 {
4056   tree type = arg;
4057
4058   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_string_type);
4059   sibling_attribute ();
4060   equate_type_number_to_die_number (type);
4061   member_attribute (TYPE_CONTEXT (type));
4062   /* This is a fixed length string.  */
4063   byte_size_attribute (type);
4064 }
4065
4066 static void
4067 output_inheritance_die (void *arg)
4068 {
4069   tree binfo = ((tree *)arg)[0];
4070   tree access = ((tree *)arg)[1];
4071
4072   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_inheritance);
4073   sibling_attribute ();
4074   type_attribute (BINFO_TYPE (binfo), 0, 0);
4075   data_member_location_attribute (binfo);
4076   if (TREE_VIA_VIRTUAL (binfo))
4077     {
4078       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_virtual);
4079       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
4080     }
4081   if (access == access_public_node)
4082     {
4083       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_public);
4084       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
4085     }
4086   else if (access == access_protected_node)
4087     {
4088       ASM_OUTPUT_DWARF_ATTRIBUTE (asm_out_file, AT_protected);
4089       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
4090     }
4091 }
4092
4093 static void
4094 output_structure_type_die (void *arg)
4095 {
4096   tree type = arg;
4097
4098   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_structure_type);
4099   sibling_attribute ();
4100   equate_type_number_to_die_number (type);
4101   name_attribute (type_tag (type));
4102   member_attribute (TYPE_CONTEXT (type));
4103
4104   /* If this type has been completed, then give it a byte_size attribute
4105      and prepare to give a list of members.  Otherwise, don't do either of
4106      these things.  In the latter case, we will not be generating a list
4107      of members (since we don't have any idea what they might be for an
4108      incomplete type).  */
4109
4110   if (COMPLETE_TYPE_P (type))
4111     {
4112       dienum_push ();
4113       byte_size_attribute (type);
4114     }
4115 }
4116
4117 /* Output a DIE to represent a declared function (either file-scope
4118    or block-local) which has "internal linkage" (according to ANSI-C).  */
4119
4120 static void
4121 output_local_subroutine_die (void *arg)
4122 {
4123   tree decl = arg;
4124   tree origin = decl_ultimate_origin (decl);
4125
4126   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_subroutine);
4127   sibling_attribute ();
4128   dienum_push ();
4129   if (origin != NULL)
4130     abstract_origin_attribute (origin);
4131   else
4132     {
4133       tree type = TREE_TYPE (decl);
4134
4135       name_and_src_coords_attributes (decl);
4136       inline_attribute (decl);
4137       prototyped_attribute (type);
4138       member_attribute (DECL_CONTEXT (decl));
4139       type_attribute (TREE_TYPE (type), 0, 0);
4140       pure_or_virtual_attribute (decl);
4141     }
4142   if (DECL_ABSTRACT (decl))
4143     equate_decl_number_to_die_number (decl);
4144   else
4145     {
4146       /* Avoid getting screwed up in cases where a function was declared
4147          static but where no definition was ever given for it.  */
4148
4149       if (TREE_ASM_WRITTEN (decl))
4150         {
4151           char label[MAX_ARTIFICIAL_LABEL_BYTES];
4152           low_pc_attribute (function_start_label (decl));
4153           sprintf (label, FUNC_END_LABEL_FMT, current_function_funcdef_no);
4154           high_pc_attribute (label);
4155           if (use_gnu_debug_info_extensions)
4156             {
4157               sprintf (label, BODY_BEGIN_LABEL_FMT,
4158                        current_function_funcdef_no);
4159               body_begin_attribute (label);
4160               sprintf (label, BODY_END_LABEL_FMT, current_function_funcdef_no);
4161               body_end_attribute (label);
4162             }
4163         }
4164     }
4165 }
4166
4167 static void
4168 output_subroutine_type_die (void *arg)
4169 {
4170   tree type = arg;
4171   tree return_type = TREE_TYPE (type);
4172
4173   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_subroutine_type);
4174   sibling_attribute ();
4175   dienum_push ();
4176   equate_type_number_to_die_number (type);
4177   prototyped_attribute (type);
4178   member_attribute (TYPE_CONTEXT (type));
4179   type_attribute (return_type, 0, 0);
4180 }
4181
4182 static void
4183 output_typedef_die (void *arg)
4184 {
4185   tree decl = arg;
4186   tree origin = decl_ultimate_origin (decl);
4187
4188   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_typedef);
4189   sibling_attribute ();
4190   if (origin != NULL)
4191     abstract_origin_attribute (origin);
4192   else
4193     {
4194       name_and_src_coords_attributes (decl);
4195       member_attribute (DECL_CONTEXT (decl));
4196       type_attribute (TREE_TYPE (decl),
4197                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl));
4198     }
4199   if (DECL_ABSTRACT (decl))
4200     equate_decl_number_to_die_number (decl);
4201 }
4202
4203 static void
4204 output_union_type_die (void *arg)
4205 {
4206   tree type = arg;
4207
4208   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_union_type);
4209   sibling_attribute ();
4210   equate_type_number_to_die_number (type);
4211   name_attribute (type_tag (type));
4212   member_attribute (TYPE_CONTEXT (type));
4213
4214   /* If this type has been completed, then give it a byte_size attribute
4215      and prepare to give a list of members.  Otherwise, don't do either of
4216      these things.  In the latter case, we will not be generating a list
4217      of members (since we don't have any idea what they might be for an
4218      incomplete type).  */
4219
4220   if (COMPLETE_TYPE_P (type))
4221     {
4222       dienum_push ();
4223       byte_size_attribute (type);
4224     }
4225 }
4226
4227 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
4228    at the end of an (ANSI prototyped) formal parameters list.  */
4229
4230 static void
4231 output_unspecified_parameters_die (void *arg)
4232 {
4233   tree decl_or_type = arg;
4234
4235   ASM_OUTPUT_DWARF_TAG (asm_out_file, TAG_unspecified_parameters);
4236   sibling_attribute ();
4237
4238   /* This kludge is here only for the sake of being compatible with what
4239      the USL CI5 C compiler does.  The specification of Dwarf Version 1
4240      doesn't say that TAG_unspecified_parameters DIEs should contain any
4241      attributes other than the AT_sibling attribute, but they are certainly
4242      allowed to contain additional attributes, and the CI5 compiler
4243      generates AT_name, AT_fund_type, and AT_location attributes within
4244      TAG_unspecified_parameters DIEs which appear in the child lists for
4245      DIEs representing function definitions, so we do likewise here.  */
4246
4247   if (TREE_CODE (decl_or_type) == FUNCTION_DECL && DECL_INITIAL (decl_or_type))
4248     {
4249       name_attribute ("...");
4250       fund_type_attribute (FT_pointer);
4251       /* location_attribute (?); */
4252     }
4253 }
4254
4255 static void
4256 output_padded_null_die (void *arg ATTRIBUTE_UNUSED)
4257 {
4258   ASM_OUTPUT_ALIGN (asm_out_file, 2);   /* 2**2 == 4 */
4259 }
4260
4261 /*************************** end of DIEs *********************************/
4262
4263 /* Generate some type of DIE.  This routine generates the generic outer
4264    wrapper stuff which goes around all types of DIE's (regardless of their
4265    TAGs.  All forms of DIEs start with a DIE-specific label, followed by a
4266    DIE-length word, followed by the guts of the DIE itself.  After the guts
4267    of the DIE, there must always be a terminator label for the DIE.  */
4268
4269 static void
4270 output_die (void (*die_specific_output_function) (void *), void *param)
4271 {
4272   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
4273   char end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4274
4275   current_dienum = NEXT_DIE_NUM;
4276   NEXT_DIE_NUM = next_unused_dienum;
4277
4278   sprintf (begin_label, DIE_BEGIN_LABEL_FMT, current_dienum);
4279   sprintf (end_label, DIE_END_LABEL_FMT, current_dienum);
4280
4281   /* Write a label which will act as the name for the start of this DIE.  */
4282
4283   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
4284
4285   /* Write the DIE-length word.  */
4286
4287   ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, end_label, begin_label);
4288
4289   /* Fill in the guts of the DIE.  */
4290
4291   next_unused_dienum++;
4292   die_specific_output_function (param);
4293
4294   /* Write a label which will act as the name for the end of this DIE.  */
4295
4296   ASM_OUTPUT_LABEL (asm_out_file, end_label);
4297 }
4298
4299 static void
4300 end_sibling_chain (void)
4301 {
4302   char begin_label[MAX_ARTIFICIAL_LABEL_BYTES];
4303
4304   current_dienum = NEXT_DIE_NUM;
4305   NEXT_DIE_NUM = next_unused_dienum;
4306
4307   sprintf (begin_label, DIE_BEGIN_LABEL_FMT, current_dienum);
4308
4309   /* Write a label which will act as the name for the start of this DIE.  */
4310
4311   ASM_OUTPUT_LABEL (asm_out_file, begin_label);
4312
4313   /* Write the DIE-length word.  */
4314
4315   ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 4);
4316
4317   dienum_pop ();
4318 }
4319 \f
4320 /* Generate a list of nameless TAG_formal_parameter DIEs (and perhaps a
4321    TAG_unspecified_parameters DIE) to represent the types of the formal
4322    parameters as specified in some function type specification (except
4323    for those which appear as part of a function *definition*).
4324
4325    Note that we must be careful here to output all of the parameter
4326    DIEs *before* we output any DIEs needed to represent the types of
4327    the formal parameters.  This keeps svr4 SDB happy because it
4328    (incorrectly) thinks that the first non-parameter DIE it sees ends
4329    the formal parameter list.  */
4330
4331 static void
4332 output_formal_types (tree function_or_method_type)
4333 {
4334   tree link;
4335   tree formal_type = NULL;
4336   tree first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
4337
4338   /* Set TREE_ASM_WRITTEN while processing the parameters, lest we
4339      get bogus recursion when outputting tagged types local to a
4340      function declaration.  */
4341   int save_asm_written = TREE_ASM_WRITTEN (function_or_method_type);
4342   TREE_ASM_WRITTEN (function_or_method_type) = 1;
4343
4344   /* In the case where we are generating a formal types list for a C++
4345      non-static member function type, skip over the first thing on the
4346      TYPE_ARG_TYPES list because it only represents the type of the
4347      hidden `this pointer'.  The debugger should be able to figure
4348      out (without being explicitly told) that this non-static member
4349      function type takes a `this pointer' and should be able to figure
4350      what the type of that hidden parameter is from the AT_member
4351      attribute of the parent TAG_subroutine_type DIE.  */
4352
4353   if (TREE_CODE (function_or_method_type) == METHOD_TYPE)
4354     first_parm_type = TREE_CHAIN (first_parm_type);
4355
4356   /* Make our first pass over the list of formal parameter types and output
4357      a TAG_formal_parameter DIE for each one.  */
4358
4359   for (link = first_parm_type; link; link = TREE_CHAIN (link))
4360     {
4361       formal_type = TREE_VALUE (link);
4362       if (formal_type == void_type_node)
4363         break;
4364
4365       /* Output a (nameless) DIE to represent the formal parameter itself.  */
4366
4367       output_die (output_formal_parameter_die, formal_type);
4368     }
4369
4370   /* If this function type has an ellipsis, add a TAG_unspecified_parameters
4371      DIE to the end of the parameter list.  */
4372
4373   if (formal_type != void_type_node)
4374     output_die (output_unspecified_parameters_die, function_or_method_type);
4375
4376   /* Make our second (and final) pass over the list of formal parameter types
4377      and output DIEs to represent those types (as necessary).  */
4378
4379   for (link = TYPE_ARG_TYPES (function_or_method_type);
4380        link;
4381        link = TREE_CHAIN (link))
4382     {
4383       formal_type = TREE_VALUE (link);
4384       if (formal_type == void_type_node)
4385         break;
4386
4387       output_type (formal_type, function_or_method_type);
4388     }
4389
4390   TREE_ASM_WRITTEN (function_or_method_type) = save_asm_written;
4391 }
4392 \f
4393 /* Remember a type in the pending_types_list.  */
4394
4395 static void
4396 pend_type (tree type)
4397 {
4398   if (pending_types == pending_types_allocated)
4399     {
4400       pending_types_allocated += PENDING_TYPES_INCREMENT;
4401       pending_types_list
4402         = xrealloc (pending_types_list,
4403                     sizeof (tree) * pending_types_allocated);
4404     }
4405   pending_types_list[pending_types++] = type;
4406
4407   /* Mark the pending type as having been output already (even though
4408      it hasn't been).  This prevents the type from being added to the
4409      pending_types_list more than once.  */
4410
4411   TREE_ASM_WRITTEN (type) = 1;
4412 }
4413
4414 /* Return nonzero if it is legitimate to output DIEs to represent a
4415    given type while we are generating the list of child DIEs for some
4416    DIE (e.g. a function or lexical block DIE) associated with a given scope.
4417
4418    See the comments within the function for a description of when it is
4419    considered legitimate to output DIEs for various kinds of types.
4420
4421    Note that TYPE_CONTEXT(type) may be NULL (to indicate global scope)
4422    or it may point to a BLOCK node (for types local to a block), or to a
4423    FUNCTION_DECL node (for types local to the heading of some function
4424    definition), or to a FUNCTION_TYPE node (for types local to the
4425    prototyped parameter list of a function type specification), or to a
4426    RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE node
4427    (in the case of C++ nested types).
4428
4429    The `scope' parameter should likewise be NULL or should point to a
4430    BLOCK node, a FUNCTION_DECL node, a FUNCTION_TYPE node, a RECORD_TYPE
4431    node, a UNION_TYPE node, or a QUAL_UNION_TYPE node.
4432
4433    This function is used only for deciding when to "pend" and when to
4434    "un-pend" types to/from the pending_types_list.
4435
4436    Note that we sometimes make use of this "type pending" feature in a
4437    rather twisted way to temporarily delay the production of DIEs for the
4438    types of formal parameters.  (We do this just to make svr4 SDB happy.)
4439    It order to delay the production of DIEs representing types of formal
4440    parameters, callers of this function supply `fake_containing_scope' as
4441    the `scope' parameter to this function.  Given that fake_containing_scope
4442    is a tagged type which is *not* the containing scope for *any* other type,
4443    the desired effect is achieved, i.e. output of DIEs representing types
4444    is temporarily suspended, and any type DIEs which would have otherwise
4445    been output are instead placed onto the pending_types_list.  Later on,
4446    we force these (temporarily pended) types to be output simply by calling
4447    `output_pending_types_for_scope' with an actual argument equal to the
4448    true scope of the types we temporarily pended.  */
4449
4450 static inline int
4451 type_ok_for_scope (tree type, tree scope)
4452 {
4453   /* Tagged types (i.e. struct, union, and enum types) must always be
4454      output only in the scopes where they actually belong (or else the
4455      scoping of their own tag names and the scoping of their member
4456      names will be incorrect).  Non-tagged-types on the other hand can
4457      generally be output anywhere, except that svr4 SDB really doesn't
4458      want to see them nested within struct or union types, so here we
4459      say it is always OK to immediately output any such a (non-tagged)
4460      type, so long as we are not within such a context.  Note that the
4461      only kinds of non-tagged types which we will be dealing with here
4462      (for C and C++ anyway) will be array types and function types.  */
4463
4464   return is_tagged_type (type)
4465          ? (TYPE_CONTEXT (type) == scope
4466             /* Ignore namespaces for the moment.  */
4467             || (scope == NULL_TREE
4468                 && TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL)
4469             || (scope == NULL_TREE && is_tagged_type (TYPE_CONTEXT (type))
4470                 && TREE_ASM_WRITTEN (TYPE_CONTEXT (type))))
4471          : (scope == NULL_TREE || ! is_tagged_type (scope));
4472 }
4473
4474 /* Output any pending types (from the pending_types list) which we can output
4475    now (taking into account the scope that we are working on now).
4476
4477    For each type output, remove the given type from the pending_types_list
4478    *before* we try to output it.
4479
4480    Note that we have to process the list in beginning-to-end order,
4481    because the call made here to output_type may cause yet more types
4482    to be added to the end of the list, and we may have to output some
4483    of them too.  */
4484
4485 static void
4486 output_pending_types_for_scope (tree containing_scope)
4487 {
4488   unsigned i;
4489
4490   for (i = 0; i < pending_types; )
4491     {
4492       tree type = pending_types_list[i];
4493
4494       if (type_ok_for_scope (type, containing_scope))
4495         {
4496           tree *mover;
4497           tree *limit;
4498
4499           pending_types--;
4500           limit = &pending_types_list[pending_types];
4501           for (mover = &pending_types_list[i]; mover < limit; mover++)
4502             *mover = *(mover+1);
4503
4504           /* Un-mark the type as having been output already (because it
4505              hasn't been, really).  Then call output_type to generate a
4506              Dwarf representation of it.  */
4507
4508           TREE_ASM_WRITTEN (type) = 0;
4509           output_type (type, containing_scope);
4510
4511           /* Don't increment the loop counter in this case because we
4512              have shifted all of the subsequent pending types down one
4513              element in the pending_types_list array.  */
4514         }
4515       else
4516         i++;
4517     }
4518 }
4519
4520 /* Remember a type in the incomplete_types_list.  */
4521
4522 static void
4523 add_incomplete_type (tree type)
4524 {
4525   if (incomplete_types == incomplete_types_allocated)
4526     {
4527       incomplete_types_allocated += INCOMPLETE_TYPES_INCREMENT;
4528       incomplete_types_list
4529         = xrealloc (incomplete_types_list,
4530                     sizeof (tree) * incomplete_types_allocated);
4531     }
4532
4533   incomplete_types_list[incomplete_types++] = type;
4534 }
4535
4536 /* Walk through the list of incomplete types again, trying once more to
4537    emit full debugging info for them.  */
4538
4539 static void
4540 retry_incomplete_types (void)
4541 {
4542   tree type;
4543
4544   finalizing = 1;
4545   while (incomplete_types)
4546     {
4547       --incomplete_types;
4548       type = incomplete_types_list[incomplete_types];
4549       output_type (type, NULL_TREE);
4550     }
4551 }
4552
4553 static void
4554 output_type (tree type, tree containing_scope)
4555 {
4556   if (type == 0 || type == error_mark_node)
4557     return;
4558
4559   /* We are going to output a DIE to represent the unqualified version of
4560      this type (i.e. without any const or volatile qualifiers) so get
4561      the main variant (i.e. the unqualified version) of this type now.  */
4562
4563   type = type_main_variant (type);
4564
4565   if (TREE_ASM_WRITTEN (type))
4566     {
4567       if (finalizing && AGGREGATE_TYPE_P (type))
4568         {
4569           tree member;
4570
4571           /* Some of our nested types might not have been defined when we
4572              were written out before; force them out now.  */
4573
4574           for (member = TYPE_FIELDS (type); member;
4575                member = TREE_CHAIN (member))
4576             if (TREE_CODE (member) == TYPE_DECL
4577                 && ! TREE_ASM_WRITTEN (TREE_TYPE (member)))
4578               output_type (TREE_TYPE (member), containing_scope);
4579         }
4580       return;
4581     }
4582
4583   /* If this is a nested type whose containing class hasn't been
4584      written out yet, writing it out will cover this one, too.  */
4585
4586   if (TYPE_CONTEXT (type)
4587       && TYPE_P (TYPE_CONTEXT (type))
4588       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
4589     {
4590       output_type (TYPE_CONTEXT (type), containing_scope);
4591       return;
4592     }
4593
4594   /* Don't generate any DIEs for this type now unless it is OK to do so
4595      (based upon what `type_ok_for_scope' tells us).  */
4596
4597   if (! type_ok_for_scope (type, containing_scope))
4598     {
4599       pend_type (type);
4600       return;
4601     }
4602
4603   switch (TREE_CODE (type))
4604     {
4605       case ERROR_MARK:
4606         break;
4607
4608       case VECTOR_TYPE:
4609         output_type (TYPE_DEBUG_REPRESENTATION_TYPE (type), containing_scope);
4610         break;
4611
4612       case POINTER_TYPE:
4613       case REFERENCE_TYPE:
4614         /* Prevent infinite recursion in cases where this is a recursive
4615            type.  Recursive types are possible in Ada.  */
4616         TREE_ASM_WRITTEN (type) = 1;
4617         /* For these types, all that is required is that we output a DIE
4618            (or a set of DIEs) to represent the "basis" type.  */
4619         output_type (TREE_TYPE (type), containing_scope);
4620         break;
4621
4622       case OFFSET_TYPE:
4623         /* This code is used for C++ pointer-to-data-member types.  */
4624         /* Output a description of the relevant class type.  */
4625         output_type (TYPE_OFFSET_BASETYPE (type), containing_scope);
4626         /* Output a description of the type of the object pointed to.  */
4627         output_type (TREE_TYPE (type), containing_scope);
4628         /* Now output a DIE to represent this pointer-to-data-member type
4629            itself.  */
4630         output_die (output_ptr_to_mbr_type_die, type);
4631         break;
4632
4633       case SET_TYPE:
4634         output_type (TYPE_DOMAIN (type), containing_scope);
4635         output_die (output_set_type_die, type);
4636         break;
4637
4638       case FILE_TYPE:
4639         output_type (TREE_TYPE (type), containing_scope);
4640         abort ();       /* No way to represent these in Dwarf yet!  */
4641         break;
4642
4643       case FUNCTION_TYPE:
4644         /* Force out return type (in case it wasn't forced out already).  */
4645         output_type (TREE_TYPE (type), containing_scope);
4646         output_die (output_subroutine_type_die, type);
4647         output_formal_types (type);
4648         end_sibling_chain ();
4649         break;
4650
4651       case METHOD_TYPE:
4652         /* Force out return type (in case it wasn't forced out already).  */
4653         output_type (TREE_TYPE (type), containing_scope);
4654         output_die (output_subroutine_type_die, type);
4655         output_formal_types (type);
4656         end_sibling_chain ();
4657         break;
4658
4659       case ARRAY_TYPE:
4660         if (TYPE_STRING_FLAG (type) && TREE_CODE(TREE_TYPE(type)) == CHAR_TYPE)
4661           {
4662             output_type (TREE_TYPE (type), containing_scope);
4663             output_die (output_string_type_die, type);
4664           }
4665         else
4666           {
4667             tree element_type;
4668
4669             element_type = TREE_TYPE (type);
4670             while (TREE_CODE (element_type) == ARRAY_TYPE)
4671               element_type = TREE_TYPE (element_type);
4672
4673             output_type (element_type, containing_scope);
4674             output_die (output_array_type_die, type);
4675           }
4676         break;
4677
4678       case ENUMERAL_TYPE:
4679       case RECORD_TYPE:
4680       case UNION_TYPE:
4681       case QUAL_UNION_TYPE:
4682
4683         /* For a non-file-scope tagged type, we can always go ahead and
4684            output a Dwarf description of this type right now, even if
4685            the type in question is still incomplete, because if this
4686            local type *was* ever completed anywhere within its scope,
4687            that complete definition would already have been attached to
4688            this RECORD_TYPE, UNION_TYPE, QUAL_UNION_TYPE or ENUMERAL_TYPE
4689            node by the time we reach this point.  That's true because of the
4690            way the front-end does its processing of file-scope declarations (of
4691            functions and class types) within which other types might be
4692            nested.  The C and C++ front-ends always gobble up such "local
4693            scope" things en-mass before they try to output *any* debugging
4694            information for any of the stuff contained inside them and thus,
4695            we get the benefit here of what is (in effect) a pre-resolution
4696            of forward references to tagged types in local scopes.
4697
4698            Note however that for file-scope tagged types we cannot assume
4699            that such pre-resolution of forward references has taken place.
4700            A given file-scope tagged type may appear to be incomplete when
4701            we reach this point, but it may yet be given a full definition
4702            (at file-scope) later on during compilation.  In order to avoid
4703            generating a premature (and possibly incorrect) set of Dwarf
4704            DIEs for such (as yet incomplete) file-scope tagged types, we
4705            generate nothing at all for as-yet incomplete file-scope tagged
4706            types here unless we are making our special "finalization" pass
4707            for file-scope things at the very end of compilation.  At that
4708            time, we will certainly know as much about each file-scope tagged
4709            type as we are ever going to know, so at that point in time, we
4710            can safely generate correct Dwarf descriptions for these file-
4711            scope tagged types.  */
4712
4713         if (!COMPLETE_TYPE_P (type)
4714             && (TYPE_CONTEXT (type) == NULL
4715                 || AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
4716                 || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL)
4717             && !finalizing)
4718           {
4719             /* We don't need to do this for function-local types.  */
4720             if (! decl_function_context (TYPE_STUB_DECL (type)))
4721               add_incomplete_type (type);
4722             return;     /* EARLY EXIT!  Avoid setting TREE_ASM_WRITTEN.  */
4723           }
4724
4725         /* Prevent infinite recursion in cases where the type of some
4726            member of this type is expressed in terms of this type itself.  */
4727
4728         TREE_ASM_WRITTEN (type) = 1;
4729
4730         /* Output a DIE to represent the tagged type itself.  */
4731
4732         switch (TREE_CODE (type))
4733           {
4734           case ENUMERAL_TYPE:
4735             output_die (output_enumeration_type_die, type);
4736             return;  /* a special case -- nothing left to do so just return */
4737
4738           case RECORD_TYPE:
4739             output_die (output_structure_type_die, type);
4740             break;
4741
4742           case UNION_TYPE:
4743           case QUAL_UNION_TYPE:
4744             output_die (output_union_type_die, type);
4745             break;
4746
4747           default:
4748             abort ();   /* Should never happen.  */
4749           }
4750
4751         /* If this is not an incomplete type, output descriptions of
4752            each of its members.
4753
4754            Note that as we output the DIEs necessary to represent the
4755            members of this record or union type, we will also be trying
4756            to output DIEs to represent the *types* of those members.
4757            However the `output_type' function (above) will specifically
4758            avoid generating type DIEs for member types *within* the list
4759            of member DIEs for this (containing) type except for those
4760            types (of members) which are explicitly marked as also being
4761            members of this (containing) type themselves.  The g++ front-
4762            end can force any given type to be treated as a member of some
4763            other (containing) type by setting the TYPE_CONTEXT of the
4764            given (member) type to point to the TREE node representing the
4765            appropriate (containing) type.
4766         */
4767
4768         if (COMPLETE_TYPE_P (type))
4769           {
4770             tree binfo = TYPE_BINFO (type);
4771
4772             /* First output info about the base classes.  */
4773             if (binfo)
4774               {
4775                 tree bases = BINFO_BASETYPES (binfo);
4776                 tree accesses = BINFO_BASEACCESSES (binfo);
4777                 register int n_bases = BINFO_N_BASETYPES (binfo);
4778                 register int i;
4779
4780                 for (i = 0; i < n_bases; i++)
4781                   {
4782                     tree arg[2];
4783
4784                     arg[0] = TREE_VEC_ELT (bases, i);
4785                     arg[1] = (accesses ? TREE_VEC_ELT (accesses, i)
4786                               : access_public_node);
4787                     output_type (BINFO_TYPE (binfo), containing_scope);
4788                     output_die (output_inheritance_die, arg);
4789                   }
4790               }
4791
4792             ++in_class;
4793
4794             {
4795               tree normal_member;
4796
4797               /* Now output info about the data members and type members.  */
4798
4799               for (normal_member = TYPE_FIELDS (type);
4800                    normal_member;
4801                    normal_member = TREE_CHAIN (normal_member))
4802                 output_decl (normal_member, type);
4803             }
4804
4805             {
4806               tree func_member;
4807
4808               /* Now output info about the function members (if any).  */
4809
4810               for (func_member = TYPE_METHODS (type);
4811                    func_member;
4812                    func_member = TREE_CHAIN (func_member))
4813                 {
4814                   /* Don't include clones in the member list.  */
4815                   if (DECL_ABSTRACT_ORIGIN (func_member))
4816                     continue;
4817
4818                   output_decl (func_member, type);
4819                 }
4820             }
4821
4822             --in_class;
4823
4824             /* RECORD_TYPEs, UNION_TYPEs, and QUAL_UNION_TYPEs are themselves
4825                scopes (at least in C++) so we must now output any nested
4826                pending types which are local just to this type.  */
4827
4828             output_pending_types_for_scope (type);
4829
4830             end_sibling_chain ();       /* Terminate member chain.  */
4831           }
4832
4833         break;
4834
4835       case VOID_TYPE:
4836       case INTEGER_TYPE:
4837       case REAL_TYPE:
4838       case COMPLEX_TYPE:
4839       case BOOLEAN_TYPE:
4840       case CHAR_TYPE:
4841         break;          /* No DIEs needed for fundamental types.  */
4842
4843       case LANG_TYPE:   /* No Dwarf representation currently defined.  */
4844         break;
4845
4846       default:
4847         abort ();
4848     }
4849
4850   TREE_ASM_WRITTEN (type) = 1;
4851 }
4852
4853 static void
4854 output_tagged_type_instantiation (tree type)
4855 {
4856   if (type == 0 || type == error_mark_node)
4857     return;
4858
4859   /* We are going to output a DIE to represent the unqualified version of
4860      this type (i.e. without any const or volatile qualifiers) so make
4861      sure that we have the main variant (i.e. the unqualified version) of
4862      this type now.  */
4863
4864   if (type != type_main_variant (type))
4865     abort ();
4866
4867   if (!TREE_ASM_WRITTEN (type))
4868     abort ();
4869
4870   switch (TREE_CODE (type))
4871     {
4872       case ERROR_MARK:
4873         break;
4874
4875       case ENUMERAL_TYPE:
4876         output_die (output_inlined_enumeration_type_die, type);
4877         break;
4878
4879       case RECORD_TYPE:
4880         output_die (output_inlined_structure_type_die, type);
4881         break;
4882
4883       case UNION_TYPE:
4884       case QUAL_UNION_TYPE:
4885         output_die (output_inlined_union_type_die, type);
4886         break;
4887
4888       default:
4889         abort ();       /* Should never happen.  */
4890     }
4891 }
4892 \f
4893 /* Output a TAG_lexical_block DIE followed by DIEs to represent all of
4894    the things which are local to the given block.  */
4895
4896 static void
4897 output_block (tree stmt, int depth)
4898 {
4899   int must_output_die = 0;
4900   tree origin;
4901   enum tree_code origin_code;
4902
4903   /* Ignore blocks never really used to make RTL.  */
4904
4905   if (! stmt || ! TREE_USED (stmt)
4906       || (!TREE_ASM_WRITTEN (stmt) && !BLOCK_ABSTRACT (stmt)))
4907     return;
4908
4909   /* Determine the "ultimate origin" of this block.  This block may be an
4910      inlined instance of an inlined instance of inline function, so we
4911      have to trace all of the way back through the origin chain to find
4912      out what sort of node actually served as the original seed for the
4913      creation of the current block.  */
4914
4915   origin = block_ultimate_origin (stmt);
4916   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
4917
4918   /* Determine if we need to output any Dwarf DIEs at all to represent this
4919      block.  */
4920
4921   if (origin_code == FUNCTION_DECL)
4922     /* The outer scopes for inlinings *must* always be represented.  We
4923        generate TAG_inlined_subroutine DIEs for them.  (See below.)  */
4924     must_output_die = 1;
4925   else
4926     {
4927       /* In the case where the current block represents an inlining of the
4928          "body block" of an inline function, we must *NOT* output any DIE
4929          for this block because we have already output a DIE to represent
4930          the whole inlined function scope and the "body block" of any
4931          function doesn't really represent a different scope according to
4932          ANSI C rules.  So we check here to make sure that this block does
4933          not represent a "body block inlining" before trying to set the
4934          `must_output_die' flag.  */
4935
4936       if (! is_body_block (origin ? origin : stmt))
4937         {
4938           /* Determine if this block directly contains any "significant"
4939              local declarations which we will need to output DIEs for.  */
4940
4941           if (debug_info_level > DINFO_LEVEL_TERSE)
4942             /* We are not in terse mode so *any* local declaration counts
4943                as being a "significant" one.  */
4944             must_output_die = (BLOCK_VARS (stmt) != NULL);
4945           else
4946             {
4947               tree decl;
4948
4949               /* We are in terse mode, so only local (nested) function
4950                  definitions count as "significant" local declarations.  */
4951
4952               for (decl = BLOCK_VARS (stmt); decl; decl = TREE_CHAIN (decl))
4953                 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
4954                   {
4955                     must_output_die = 1;
4956                     break;
4957                   }
4958             }
4959         }
4960     }
4961
4962   /* It would be a waste of space to generate a Dwarf TAG_lexical_block
4963      DIE for any block which contains no significant local declarations
4964      at all.  Rather, in such cases we just call `output_decls_for_scope'
4965      so that any needed Dwarf info for any sub-blocks will get properly
4966      generated.  Note that in terse mode, our definition of what constitutes
4967      a "significant" local declaration gets restricted to include only
4968      inlined function instances and local (nested) function definitions.  */
4969
4970   if (origin_code == FUNCTION_DECL && BLOCK_ABSTRACT (stmt))
4971     /* We don't care about an abstract inlined subroutine.  */;
4972   else if (must_output_die)
4973     {
4974       output_die ((origin_code == FUNCTION_DECL)
4975                     ? output_inlined_subroutine_die
4976                     : output_lexical_block_die,
4977                   stmt);
4978       output_decls_for_scope (stmt, depth);
4979       end_sibling_chain ();
4980     }
4981   else
4982     output_decls_for_scope (stmt, depth);
4983 }
4984
4985 /* Output all of the decls declared within a given scope (also called
4986    a `binding contour') and (recursively) all of it's sub-blocks.  */
4987
4988 static void
4989 output_decls_for_scope (tree stmt, int depth)
4990 {
4991   /* Ignore blocks never really used to make RTL.  */
4992
4993   if (! stmt || ! TREE_USED (stmt))
4994     return;
4995
4996   /* Output the DIEs to represent all of the data objects, functions,
4997      typedefs, and tagged types declared directly within this block
4998      but not within any nested sub-blocks.  */
4999
5000   {
5001     tree decl;
5002
5003     for (decl = BLOCK_VARS (stmt); decl; decl = TREE_CHAIN (decl))
5004       output_decl (decl, stmt);
5005   }
5006
5007   output_pending_types_for_scope (stmt);
5008
5009   /* Output the DIEs to represent all sub-blocks (and the items declared
5010      therein) of this block.     */
5011
5012   {
5013     tree subblocks;
5014
5015     for (subblocks = BLOCK_SUBBLOCKS (stmt);
5016          subblocks;
5017          subblocks = BLOCK_CHAIN (subblocks))
5018       output_block (subblocks, depth + 1);
5019   }
5020 }
5021
5022 /* Is this a typedef we can avoid emitting?  */
5023
5024 static inline int
5025 is_redundant_typedef (tree decl)
5026 {
5027   if (TYPE_DECL_IS_STUB (decl))
5028     return 1;
5029   if (DECL_ARTIFICIAL (decl)
5030       && DECL_CONTEXT (decl)
5031       && is_tagged_type (DECL_CONTEXT (decl))
5032       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
5033       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
5034     /* Also ignore the artificial member typedef for the class name.  */
5035     return 1;
5036   return 0;
5037 }
5038
5039 /* Output Dwarf .debug information for a decl described by DECL.  */
5040
5041 static void
5042 output_decl (tree decl, tree containing_scope)
5043 {
5044   /* Make a note of the decl node we are going to be working on.  We may
5045      need to give the user the source coordinates of where it appeared in
5046      case we notice (later on) that something about it looks screwy.  */
5047
5048   dwarf_last_decl = decl;
5049
5050   if (TREE_CODE (decl) == ERROR_MARK)
5051     return;
5052
5053   /* If a structure is declared within an initialization, e.g. as the
5054      operand of a sizeof, then it will not have a name.  We don't want
5055      to output a DIE for it, as the tree nodes are in the temporary obstack */
5056
5057   if ((TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5058        || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE)
5059       && ((DECL_NAME (decl) == 0 && TYPE_NAME (TREE_TYPE (decl)) == 0)
5060           || (TYPE_FIELDS (TREE_TYPE (decl))
5061               && (TREE_CODE (TYPE_FIELDS (TREE_TYPE (decl))) == ERROR_MARK))))
5062     return;
5063
5064   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
5065
5066   if (DECL_IGNORED_P (decl))
5067     return;
5068
5069   switch (TREE_CODE (decl))
5070     {
5071     case CONST_DECL:
5072       /* The individual enumerators of an enum type get output when we
5073          output the Dwarf representation of the relevant enum type itself.  */
5074       break;
5075
5076     case FUNCTION_DECL:
5077       /* If we are in terse mode, don't output any DIEs to represent
5078          mere function declarations.  Also, if we are conforming
5079          to the DWARF version 1 specification, don't output DIEs for
5080          mere function declarations.  */
5081
5082       if (DECL_INITIAL (decl) == NULL_TREE)
5083 #if (DWARF_VERSION > 1)
5084         if (debug_info_level <= DINFO_LEVEL_TERSE)
5085 #endif
5086           break;
5087
5088       /* Before we describe the FUNCTION_DECL itself, make sure that we
5089          have described its return type.  */
5090
5091       output_type (TREE_TYPE (TREE_TYPE (decl)), containing_scope);
5092
5093       {
5094         /* And its containing type.  */
5095         register tree origin = decl_class_context (decl);
5096         if (origin)
5097           output_type (origin, containing_scope);
5098       }
5099
5100       /* If we're emitting an out-of-line copy of an inline function,
5101          set up to refer to the abstract instance emitted from
5102          dwarfout_deferred_inline_function.  */
5103       if (DECL_INLINE (decl) && ! DECL_ABSTRACT (decl)
5104           && ! (containing_scope && TYPE_P (containing_scope)))
5105         set_decl_origin_self (decl);
5106
5107       /* If the following DIE will represent a function definition for a
5108          function with "extern" linkage, output a special "pubnames" DIE
5109          label just ahead of the actual DIE.  A reference to this label
5110          was already generated in the .debug_pubnames section sub-entry
5111          for this function definition.  */
5112
5113       if (TREE_PUBLIC (decl))
5114         {
5115           char label[MAX_ARTIFICIAL_LABEL_BYTES];
5116
5117           sprintf (label, PUB_DIE_LABEL_FMT, next_pubname_number++);
5118           ASM_OUTPUT_LABEL (asm_out_file, label);
5119         }
5120
5121       /* Now output a DIE to represent the function itself.  */
5122
5123       output_die (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
5124                                 ? output_global_subroutine_die
5125                                 : output_local_subroutine_die,
5126                   decl);
5127
5128       /* Now output descriptions of the arguments for this function.
5129          This gets (unnecessarily?) complex because of the fact that
5130          the DECL_ARGUMENT list for a FUNCTION_DECL doesn't indicate
5131          cases where there was a trailing `...' at the end of the formal
5132          parameter list.  In order to find out if there was a trailing
5133          ellipsis or not, we must instead look at the type associated
5134          with the FUNCTION_DECL.  This will be a node of type FUNCTION_TYPE.
5135          If the chain of type nodes hanging off of this FUNCTION_TYPE node
5136          ends with a void_type_node then there should *not* be an ellipsis
5137          at the end.  */
5138
5139       /* In the case where we are describing a mere function declaration, all
5140          we need to do here (and all we *can* do here) is to describe
5141          the *types* of its formal parameters.  */
5142
5143       if (decl != current_function_decl || in_class)
5144         output_formal_types (TREE_TYPE (decl));
5145       else
5146         {
5147           /* Generate DIEs to represent all known formal parameters.  */
5148
5149           tree arg_decls = DECL_ARGUMENTS (decl);
5150           tree parm;
5151
5152           /* WARNING!  Kludge zone ahead!  Here we have a special
5153              hack for svr4 SDB compatibility.  Instead of passing the
5154              current FUNCTION_DECL node as the second parameter (i.e.
5155              the `containing_scope' parameter) to `output_decl' (as
5156              we ought to) we instead pass a pointer to our own private
5157              fake_containing_scope node.  That node is a RECORD_TYPE
5158              node which NO OTHER TYPE may ever actually be a member of.
5159
5160              This pointer will ultimately get passed into `output_type'
5161              as its `containing_scope' parameter.  `Output_type' will
5162              then perform its part in the hack... i.e. it will pend
5163              the type of the formal parameter onto the pending_types
5164              list.  Later on, when we are done generating the whole
5165              sequence of formal parameter DIEs for this function
5166              definition, we will un-pend all previously pended types
5167              of formal parameters for this function definition.
5168
5169              This whole kludge prevents any type DIEs from being
5170              mixed in with the formal parameter DIEs.  That's good
5171              because svr4 SDB believes that the list of formal
5172              parameter DIEs for a function ends wherever the first
5173              non-formal-parameter DIE appears.  Thus, we have to
5174              keep the formal parameter DIEs segregated.  They must
5175              all appear (consecutively) at the start of the list of
5176              children for the DIE representing the function definition.
5177              Then (and only then) may we output any additional DIEs
5178              needed to represent the types of these formal parameters.
5179           */
5180
5181           /*
5182              When generating DIEs, generate the unspecified_parameters
5183              DIE instead if we come across the arg "__builtin_va_alist"
5184           */
5185
5186           for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
5187             if (TREE_CODE (parm) == PARM_DECL)
5188               {
5189                 if (DECL_NAME(parm) &&
5190                     !strcmp(IDENTIFIER_POINTER(DECL_NAME(parm)),
5191                             "__builtin_va_alist") )
5192                   output_die (output_unspecified_parameters_die, decl);
5193                 else
5194                   output_decl (parm, fake_containing_scope);
5195               }
5196
5197           /*
5198              Now that we have finished generating all of the DIEs to
5199              represent the formal parameters themselves, force out
5200              any DIEs needed to represent their types.  We do this
5201              simply by un-pending all previously pended types which
5202              can legitimately go into the chain of children DIEs for
5203              the current FUNCTION_DECL.
5204           */
5205
5206           output_pending_types_for_scope (decl);
5207
5208           /*
5209             Decide whether we need an unspecified_parameters DIE at the end.
5210             There are 2 more cases to do this for:
5211             1) the ansi ... declaration - this is detectable when the end
5212                 of the arg list is not a void_type_node
5213             2) an unprototyped function declaration (not a definition).  This
5214                 just means that we have no info about the parameters at all.
5215           */
5216
5217           {
5218             tree fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
5219
5220             if (fn_arg_types)
5221               {
5222               /* This is the prototyped case, check for....  */
5223               if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
5224                 output_die (output_unspecified_parameters_die, decl);
5225               }
5226             else
5227               {
5228                 /* This is unprototyped, check for undefined (just declaration).  */
5229                 if (!DECL_INITIAL (decl))
5230                   output_die (output_unspecified_parameters_die, decl);
5231               }
5232           }
5233
5234           /* Output Dwarf info for all of the stuff within the body of the
5235              function (if it has one - it may be just a declaration).  */
5236
5237           {
5238             tree outer_scope = DECL_INITIAL (decl);
5239
5240             if (outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
5241               {
5242                 /* Note that here, `outer_scope' is a pointer to the outermost
5243                    BLOCK node created to represent a function.
5244                    This outermost BLOCK actually represents the outermost
5245                    binding contour for the function, i.e. the contour in which
5246                    the function's formal parameters and labels get declared.
5247
5248                    Curiously, it appears that the front end doesn't actually
5249                    put the PARM_DECL nodes for the current function onto the
5250                    BLOCK_VARS list for this outer scope.  (They are strung
5251                    off of the DECL_ARGUMENTS list for the function instead.)
5252                    The BLOCK_VARS list for the `outer_scope' does provide us
5253                    with a list of the LABEL_DECL nodes for the function however,
5254                    and we output DWARF info for those here.
5255
5256                    Just within the `outer_scope' there will be a BLOCK node
5257                    representing the function's outermost pair of curly braces,
5258                    and any blocks used for the base and member initializers of
5259                    a C++ constructor function.  */
5260
5261                 output_decls_for_scope (outer_scope, 0);
5262
5263                 /* Finally, force out any pending types which are local to the
5264                    outermost block of this function definition.  These will
5265                    all have a TYPE_CONTEXT which points to the FUNCTION_DECL
5266                    node itself.  */
5267
5268                 output_pending_types_for_scope (decl);
5269               }
5270           }
5271         }
5272
5273       /* Generate a terminator for the list of stuff `owned' by this
5274          function.  */
5275
5276       end_sibling_chain ();
5277
5278       break;
5279
5280     case TYPE_DECL:
5281       /* If we are in terse mode, don't generate any DIEs to represent
5282          any actual typedefs.  Note that even when we are in terse mode,
5283          we must still output DIEs to represent those tagged types which
5284          are used (directly or indirectly) in the specification of either
5285          a return type or a formal parameter type of some function.  */
5286
5287       if (debug_info_level <= DINFO_LEVEL_TERSE)
5288         if (! TYPE_DECL_IS_STUB (decl)
5289             || (! TYPE_USED_FOR_FUNCTION (TREE_TYPE (decl)) && ! in_class))
5290           return;
5291
5292       /* In the special case of a TYPE_DECL node representing
5293          the declaration of some type tag, if the given TYPE_DECL is
5294          marked as having been instantiated from some other (original)
5295          TYPE_DECL node (e.g. one which was generated within the original
5296          definition of an inline function) we have to generate a special
5297          (abbreviated) TAG_structure_type, TAG_union_type, or
5298          TAG_enumeration-type DIE here.  */
5299
5300       if (TYPE_DECL_IS_STUB (decl) && DECL_ABSTRACT_ORIGIN (decl))
5301         {
5302           output_tagged_type_instantiation (TREE_TYPE (decl));
5303           return;
5304         }
5305
5306       output_type (TREE_TYPE (decl), containing_scope);
5307
5308       if (! is_redundant_typedef (decl))
5309         /* Output a DIE to represent the typedef itself.  */
5310         output_die (output_typedef_die, decl);
5311       break;
5312
5313     case LABEL_DECL:
5314       if (debug_info_level >= DINFO_LEVEL_NORMAL)
5315         output_die (output_label_die, decl);
5316       break;
5317
5318     case VAR_DECL:
5319       /* If we are conforming to the DWARF version 1 specification, don't
5320          generated any DIEs to represent mere external object declarations.  */
5321
5322 #if (DWARF_VERSION <= 1)
5323       if (DECL_EXTERNAL (decl) && ! TREE_PUBLIC (decl))
5324         break;
5325 #endif
5326
5327       /* If we are in terse mode, don't generate any DIEs to represent
5328          any variable declarations or definitions.  */
5329
5330       if (debug_info_level <= DINFO_LEVEL_TERSE)
5331         break;
5332
5333       /* Output any DIEs that are needed to specify the type of this data
5334          object.  */
5335
5336       output_type (TREE_TYPE (decl), containing_scope);
5337
5338       {
5339         /* And its containing type.  */
5340         register tree origin = decl_class_context (decl);
5341         if (origin)
5342           output_type (origin, containing_scope);
5343       }
5344
5345       /* If the following DIE will represent a data object definition for a
5346          data object with "extern" linkage, output a special "pubnames" DIE
5347          label just ahead of the actual DIE.  A reference to this label
5348          was already generated in the .debug_pubnames section sub-entry
5349          for this data object definition.  */
5350
5351       if (TREE_PUBLIC (decl) && ! DECL_ABSTRACT (decl))
5352         {
5353           char label[MAX_ARTIFICIAL_LABEL_BYTES];
5354
5355           sprintf (label, PUB_DIE_LABEL_FMT, next_pubname_number++);
5356           ASM_OUTPUT_LABEL (asm_out_file, label);
5357         }
5358
5359       /* Now output the DIE to represent the data object itself.  This gets
5360          complicated because of the possibility that the VAR_DECL really
5361          represents an inlined instance of a formal parameter for an inline
5362          function.  */
5363
5364       {
5365         void (*func) (void *);
5366         register tree origin = decl_ultimate_origin (decl);
5367
5368         if (origin != NULL && TREE_CODE (origin) == PARM_DECL)
5369           func = output_formal_parameter_die;
5370         else
5371           {
5372             if (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
5373               func = output_global_variable_die;
5374             else
5375               func = output_local_variable_die;
5376           }
5377         output_die (func, decl);
5378       }
5379       break;
5380
5381     case FIELD_DECL:
5382       /* Ignore the nameless fields that are used to skip bits.  */
5383       if (DECL_NAME (decl) != 0)
5384         {
5385           output_type (member_declared_type (decl), containing_scope);
5386           output_die (output_member_die, decl);
5387         }
5388       break;
5389
5390     case PARM_DECL:
5391      /* Force out the type of this formal, if it was not forced out yet.
5392         Note that here we can run afoul of a bug in "classic" svr4 SDB.
5393         It should be able to grok the presence of type DIEs within a list
5394         of TAG_formal_parameter DIEs, but it doesn't.  */
5395
5396       output_type (TREE_TYPE (decl), containing_scope);
5397       output_die (output_formal_parameter_die, decl);
5398       break;
5399
5400     case NAMESPACE_DECL:
5401       /* Ignore for now.  */
5402       break;
5403
5404     default:
5405       abort ();
5406     }
5407 }
5408 \f
5409 /* Output debug information for a function.  */
5410 static void
5411 dwarfout_function_decl (tree decl)
5412 {
5413   dwarfout_file_scope_decl (decl, 0);
5414 }
5415
5416 /* Debug information for a global DECL.  Called from toplev.c after
5417    compilation proper has finished.  */
5418 static void
5419 dwarfout_global_decl (tree decl)
5420 {
5421   /* Output DWARF information for file-scope tentative data object
5422      declarations, file-scope (extern) function declarations (which
5423      had no corresponding body) and file-scope tagged type
5424      declarations and definitions which have not yet been forced out.  */
5425
5426   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
5427     dwarfout_file_scope_decl (decl, 1);
5428 }
5429
5430 /* DECL is an inline function, whose body is present, but which is not
5431    being output at this point.  (We're putting that off until we need
5432    to do it.)  */
5433 static void
5434 dwarfout_deferred_inline_function (tree decl)
5435 {
5436   /* Generate the DWARF info for the "abstract" instance of a function
5437      which we may later generate inlined and/or out-of-line instances
5438      of.  */
5439   if ((DECL_INLINE (decl) || DECL_ABSTRACT (decl))
5440       && ! DECL_ABSTRACT_ORIGIN (decl))
5441     {
5442       /* The front-end may not have set CURRENT_FUNCTION_DECL, but the
5443          DWARF code expects it to be set in this case.  Intuitively,
5444          DECL is the function we just finished defining, so setting
5445          CURRENT_FUNCTION_DECL is sensible.  */
5446       tree saved_cfd = current_function_decl;
5447       int was_abstract = DECL_ABSTRACT (decl);
5448       current_function_decl = decl;
5449
5450       /* Let the DWARF code do its work.  */
5451       set_decl_abstract_flags (decl, 1);
5452       dwarfout_file_scope_decl (decl, 0);
5453       if (! was_abstract)
5454         set_decl_abstract_flags (decl, 0);
5455
5456       /* Reset CURRENT_FUNCTION_DECL.  */
5457       current_function_decl = saved_cfd;
5458     }
5459 }
5460
5461 static void
5462 dwarfout_file_scope_decl (tree decl, int set_finalizing)
5463 {
5464   if (TREE_CODE (decl) == ERROR_MARK)
5465     return;
5466
5467   /* If this ..._DECL node is marked to be ignored, then ignore it.  */
5468
5469   if (DECL_IGNORED_P (decl))
5470     return;
5471
5472   switch (TREE_CODE (decl))
5473     {
5474     case FUNCTION_DECL:
5475
5476       /* Ignore this FUNCTION_DECL if it refers to a builtin declaration of
5477          a builtin function.  Explicit programmer-supplied declarations of
5478          these same functions should NOT be ignored however.  */
5479
5480       if (DECL_EXTERNAL (decl) && DECL_FUNCTION_CODE (decl))
5481         return;
5482
5483       /* What we would really like to do here is to filter out all mere
5484          file-scope declarations of file-scope functions which are never
5485          referenced later within this translation unit (and keep all of
5486          ones that *are* referenced later on) but we aren't clairvoyant,
5487          so we have no idea which functions will be referenced in the
5488          future (i.e. later on within the current translation unit).
5489          So here we just ignore all file-scope function declarations
5490          which are not also definitions.  If and when the debugger needs
5491          to know something about these functions, it will have to hunt
5492          around and find the DWARF information associated with the
5493          *definition* of the function.
5494
5495          Note that we can't just check `DECL_EXTERNAL' to find out which
5496          FUNCTION_DECL nodes represent definitions and which ones represent
5497          mere declarations.  We have to check `DECL_INITIAL' instead.  That's
5498          because the C front-end supports some weird semantics for "extern
5499          inline" function definitions.  These can get inlined within the
5500          current translation unit (an thus, we need to generate DWARF info
5501          for their abstract instances so that the DWARF info for the
5502          concrete inlined instances can have something to refer to) but
5503          the compiler never generates any out-of-lines instances of such
5504          things (despite the fact that they *are* definitions).  The
5505          important point is that the C front-end marks these "extern inline"
5506          functions as DECL_EXTERNAL, but we need to generate DWARF for them
5507          anyway.
5508
5509          Note that the C++ front-end also plays some similar games for inline
5510          function definitions appearing within include files which also
5511          contain `#pragma interface' pragmas.  */
5512
5513       if (DECL_INITIAL (decl) == NULL_TREE)
5514         return;
5515
5516       if (TREE_PUBLIC (decl)
5517           && ! DECL_EXTERNAL (decl)
5518           && ! DECL_ABSTRACT (decl))
5519         {
5520           char label[MAX_ARTIFICIAL_LABEL_BYTES];
5521
5522           /* Output a .debug_pubnames entry for a public function
5523              defined in this compilation unit.  */
5524
5525           fputc ('\n', asm_out_file);
5526           ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_PUBNAMES_SECTION);
5527           sprintf (label, PUB_DIE_LABEL_FMT, next_pubname_number);
5528           ASM_OUTPUT_DWARF_ADDR (asm_out_file, label);
5529           ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file,
5530                                    IDENTIFIER_POINTER (DECL_NAME (decl)));
5531           ASM_OUTPUT_POP_SECTION (asm_out_file);
5532         }
5533
5534       break;
5535
5536     case VAR_DECL:
5537
5538       /* Ignore this VAR_DECL if it refers to a file-scope extern data
5539          object declaration and if the declaration was never even
5540          referenced from within this entire compilation unit.  We
5541          suppress these DIEs in order to save space in the .debug section
5542          (by eliminating entries which are probably useless).  Note that
5543          we must not suppress block-local extern declarations (whether
5544          used or not) because that would screw-up the debugger's name
5545          lookup mechanism and cause it to miss things which really ought
5546          to be in scope at a given point.  */
5547
5548       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
5549         return;
5550
5551       if (TREE_PUBLIC (decl)
5552           && ! DECL_EXTERNAL (decl)
5553           && GET_CODE (DECL_RTL (decl)) == MEM
5554           && ! DECL_ABSTRACT (decl))
5555         {
5556           char label[MAX_ARTIFICIAL_LABEL_BYTES];
5557
5558           if (debug_info_level >= DINFO_LEVEL_NORMAL)
5559             {
5560               /* Output a .debug_pubnames entry for a public variable
5561                  defined in this compilation unit.  */
5562
5563               fputc ('\n', asm_out_file);
5564               ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_PUBNAMES_SECTION);
5565               sprintf (label, PUB_DIE_LABEL_FMT, next_pubname_number);
5566               ASM_OUTPUT_DWARF_ADDR (asm_out_file, label);
5567               ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file,
5568                                        IDENTIFIER_POINTER (DECL_NAME (decl)));
5569               ASM_OUTPUT_POP_SECTION (asm_out_file);
5570             }
5571
5572           if (DECL_INITIAL (decl) == NULL)
5573             {
5574               /* Output a .debug_aranges entry for a public variable
5575                  which is tentatively defined in this compilation unit.  */
5576
5577               fputc ('\n', asm_out_file);
5578               ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_ARANGES_SECTION);
5579               ASM_OUTPUT_DWARF_ADDR (asm_out_file,
5580                               IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
5581               ASM_OUTPUT_DWARF_DATA4 (asm_out_file,
5582                         (unsigned) int_size_in_bytes (TREE_TYPE (decl)));
5583               ASM_OUTPUT_POP_SECTION (asm_out_file);
5584             }
5585         }
5586
5587       /* If we are in terse mode, don't generate any DIEs to represent
5588          any variable declarations or definitions.  */
5589
5590       if (debug_info_level <= DINFO_LEVEL_TERSE)
5591         return;
5592
5593       break;
5594
5595     case TYPE_DECL:
5596       /* Don't bother trying to generate any DIEs to represent any of the
5597          normal built-in types for the language we are compiling, except
5598          in cases where the types in question are *not* DWARF fundamental
5599          types.  We make an exception in the case of non-fundamental types
5600          for the sake of Objective-C (and perhaps C++) because the GNU
5601          front-ends for these languages may in fact create certain "built-in"
5602          types which are (for example) RECORD_TYPEs.  In such cases, we
5603          really need to output these (non-fundamental) types because other
5604          DIEs may contain references to them.  */
5605
5606       /* Also ignore language dependent types here, because they are probably
5607          also built-in types.  If we didn't ignore them, then we would get
5608          references to undefined labels because output_type doesn't support
5609          them.   So, for now, we need to ignore them to avoid assembler
5610          errors.  */
5611
5612       /* ??? This code is different than the equivalent code in dwarf2out.c.
5613          The dwarf2out.c code is probably more correct.  */
5614
5615       if (DECL_SOURCE_LINE (decl) == 0
5616           && (type_is_fundamental (TREE_TYPE (decl))
5617               || TREE_CODE (TREE_TYPE (decl)) == LANG_TYPE))
5618         return;
5619
5620       /* If we are in terse mode, don't generate any DIEs to represent
5621          any actual typedefs.  Note that even when we are in terse mode,
5622          we must still output DIEs to represent those tagged types which
5623          are used (directly or indirectly) in the specification of either
5624          a return type or a formal parameter type of some function.  */
5625
5626       if (debug_info_level <= DINFO_LEVEL_TERSE)
5627         if (! TYPE_DECL_IS_STUB (decl)
5628             || ! TYPE_USED_FOR_FUNCTION (TREE_TYPE (decl)))
5629           return;
5630
5631       break;
5632
5633     default:
5634       return;
5635     }
5636
5637   fputc ('\n', asm_out_file);
5638   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SECTION);
5639   finalizing = set_finalizing;
5640   output_decl (decl, NULL_TREE);
5641
5642   /* NOTE:  The call above to `output_decl' may have caused one or more
5643      file-scope named types (i.e. tagged types) to be placed onto the
5644      pending_types_list.  We have to get those types off of that list
5645      at some point, and this is the perfect time to do it.  If we didn't
5646      take them off now, they might still be on the list when cc1 finally
5647      exits.  That might be OK if it weren't for the fact that when we put
5648      types onto the pending_types_list, we set the TREE_ASM_WRITTEN flag
5649      for these types, and that causes them never to be output unless
5650      `output_pending_types_for_scope' takes them off of the list and un-sets
5651      their TREE_ASM_WRITTEN flags.  */
5652
5653   output_pending_types_for_scope (NULL_TREE);
5654
5655   /* The above call should have totally emptied the pending_types_list
5656      if this is not a nested function or class.  If this is a nested type,
5657      then the remaining pending_types will be emitted when the containing type
5658      is handled.  */
5659
5660   if (! DECL_CONTEXT (decl))
5661     {
5662       if (pending_types != 0)
5663         abort ();
5664     }
5665
5666   ASM_OUTPUT_POP_SECTION (asm_out_file);
5667 }
5668 \f
5669 /* Output a marker (i.e. a label) for the beginning of the generated code
5670    for a lexical block.  */
5671
5672 static void
5673 dwarfout_begin_block (unsigned int line ATTRIBUTE_UNUSED,
5674                       unsigned int blocknum)
5675 {
5676   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5677
5678   function_section (current_function_decl);
5679   sprintf (label, BLOCK_BEGIN_LABEL_FMT, blocknum);
5680   ASM_OUTPUT_LABEL (asm_out_file, label);
5681 }
5682
5683 /* Output a marker (i.e. a label) for the end of the generated code
5684    for a lexical block.  */
5685
5686 static void
5687 dwarfout_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
5688 {
5689   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5690
5691   function_section (current_function_decl);
5692   sprintf (label, BLOCK_END_LABEL_FMT, blocknum);
5693   ASM_OUTPUT_LABEL (asm_out_file, label);
5694 }
5695
5696 /* Output a marker (i.e. a label) for the point in the generated code where
5697    the real body of the function begins (after parameters have been moved
5698    to their home locations).  */
5699
5700 static void
5701 dwarfout_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
5702                        const char *file ATTRIBUTE_UNUSED)
5703 {
5704   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5705
5706   if (! use_gnu_debug_info_extensions)
5707     return;
5708
5709   function_section (current_function_decl);
5710   sprintf (label, BODY_BEGIN_LABEL_FMT, current_function_funcdef_no);
5711   ASM_OUTPUT_LABEL (asm_out_file, label);
5712 }
5713
5714 /* Output a marker (i.e. a label) for the point in the generated code where
5715    the real body of the function ends (just before the epilogue code).  */
5716
5717 static void
5718 dwarfout_end_function (unsigned int line ATTRIBUTE_UNUSED)
5719 {
5720   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5721
5722   if (! use_gnu_debug_info_extensions)
5723     return;
5724   function_section (current_function_decl);
5725   sprintf (label, BODY_END_LABEL_FMT, current_function_funcdef_no);
5726   ASM_OUTPUT_LABEL (asm_out_file, label);
5727 }
5728
5729 /* Output a marker (i.e. a label) for the absolute end of the generated code
5730    for a function definition.  This gets called *after* the epilogue code
5731    has been generated.  */
5732
5733 static void
5734 dwarfout_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
5735                        const char *file ATTRIBUTE_UNUSED)
5736 {
5737   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5738
5739   /* Output a label to mark the endpoint of the code generated for this
5740      function.  */
5741
5742   sprintf (label, FUNC_END_LABEL_FMT, current_function_funcdef_no);
5743   ASM_OUTPUT_LABEL (asm_out_file, label);
5744 }
5745
5746 static void
5747 shuffle_filename_entry (filename_entry *new_zeroth)
5748 {
5749   filename_entry temp_entry;
5750   filename_entry *limit_p;
5751   filename_entry *move_p;
5752
5753   if (new_zeroth == &filename_table[0])
5754     return;
5755
5756   temp_entry = *new_zeroth;
5757
5758   /* Shift entries up in the table to make room at [0].  */
5759
5760   limit_p = &filename_table[0];
5761   for (move_p = new_zeroth; move_p > limit_p; move_p--)
5762     *move_p = *(move_p-1);
5763
5764   /* Install the found entry at [0].  */
5765
5766   filename_table[0] = temp_entry;
5767 }
5768
5769 /* Create a new (string) entry for the .debug_sfnames section.  */
5770
5771 static void
5772 generate_new_sfname_entry (void)
5773 {
5774   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5775
5776   fputc ('\n', asm_out_file);
5777   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SFNAMES_SECTION);
5778   sprintf (label, SFNAMES_ENTRY_LABEL_FMT, filename_table[0].number);
5779   ASM_OUTPUT_LABEL (asm_out_file, label);
5780   ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file,
5781                                    filename_table[0].name
5782                                    ? filename_table[0].name
5783                                    : "");
5784   ASM_OUTPUT_POP_SECTION (asm_out_file);
5785 }
5786
5787 /* Lookup a filename (in the list of filenames that we know about here in
5788    dwarfout.c) and return its "index".  The index of each (known) filename
5789    is just a unique number which is associated with only that one filename.
5790    We need such numbers for the sake of generating labels (in the
5791    .debug_sfnames section) and references to those unique labels (in the
5792    .debug_srcinfo and .debug_macinfo sections).
5793
5794    If the filename given as an argument is not found in our current list,
5795    add it to the list and assign it the next available unique index number.
5796
5797    Whatever we do (i.e. whether we find a pre-existing filename or add a new
5798    one), we shuffle the filename found (or added) up to the zeroth entry of
5799    our list of filenames (which is always searched linearly).  We do this so
5800    as to optimize the most common case for these filename lookups within
5801    dwarfout.c.  The most common case by far is the case where we call
5802    lookup_filename to lookup the very same filename that we did a lookup
5803    on the last time we called lookup_filename.  We make sure that this
5804    common case is fast because such cases will constitute 99.9% of the
5805    lookups we ever do (in practice).
5806
5807    If we add a new filename entry to our table, we go ahead and generate
5808    the corresponding entry in the .debug_sfnames section right away.
5809    Doing so allows us to avoid tickling an assembler bug (present in some
5810    m68k assemblers) which yields assembly-time errors in cases where the
5811    difference of two label addresses is taken and where the two labels
5812    are in a section *other* than the one where the difference is being
5813    calculated, and where at least one of the two symbol references is a
5814    forward reference.  (This bug could be tickled by our .debug_srcinfo
5815    entries if we don't output their corresponding .debug_sfnames entries
5816    before them.) */
5817
5818 static unsigned
5819 lookup_filename (const char *file_name)
5820 {
5821   filename_entry *search_p;
5822   filename_entry *limit_p = &filename_table[ft_entries];
5823
5824   for (search_p = filename_table; search_p < limit_p; search_p++)
5825     if (!strcmp (file_name, search_p->name))
5826       {
5827         /* When we get here, we have found the filename that we were
5828            looking for in the filename_table.  Now we want to make sure
5829            that it gets moved to the zero'th entry in the table (if it
5830            is not already there) so that subsequent attempts to find the
5831            same filename will find it as quickly as possible.  */
5832
5833         shuffle_filename_entry (search_p);
5834         return filename_table[0].number;
5835       }
5836
5837   /* We come here whenever we have a new filename which is not registered
5838      in the current table.  Here we add it to the table.  */
5839
5840   /* Prepare to add a new table entry by making sure there is enough space
5841      in the table to do so.  If not, expand the current table.  */
5842
5843   if (ft_entries == ft_entries_allocated)
5844     {
5845       ft_entries_allocated += FT_ENTRIES_INCREMENT;
5846       filename_table
5847         = xrealloc (filename_table,
5848                     ft_entries_allocated * sizeof (filename_entry));
5849     }
5850
5851   /* Initially, add the new entry at the end of the filename table.  */
5852
5853   filename_table[ft_entries].number = ft_entries;
5854   filename_table[ft_entries].name = xstrdup (file_name);
5855
5856   /* Shuffle the new entry into filename_table[0].  */
5857
5858   shuffle_filename_entry (&filename_table[ft_entries]);
5859
5860   if (debug_info_level >= DINFO_LEVEL_NORMAL)
5861     generate_new_sfname_entry ();
5862
5863   ft_entries++;
5864   return filename_table[0].number;
5865 }
5866
5867 static void
5868 generate_srcinfo_entry (unsigned int line_entry_num, unsigned int files_entry_num)
5869 {
5870   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5871
5872   fputc ('\n', asm_out_file);
5873   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SRCINFO_SECTION);
5874   sprintf (label, LINE_ENTRY_LABEL_FMT, line_entry_num);
5875   ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, label, LINE_BEGIN_LABEL);
5876   sprintf (label, SFNAMES_ENTRY_LABEL_FMT, files_entry_num);
5877   ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, label, SFNAMES_BEGIN_LABEL);
5878   ASM_OUTPUT_POP_SECTION (asm_out_file);
5879 }
5880
5881 static void
5882 dwarfout_source_line (unsigned int line, const char *filename)
5883 {
5884   if (debug_info_level >= DINFO_LEVEL_NORMAL
5885       /* We can't emit line number info for functions in separate sections,
5886          because the assembler can't subtract labels in different sections.  */
5887       && DECL_SECTION_NAME (current_function_decl) == NULL_TREE)
5888     {
5889       char label[MAX_ARTIFICIAL_LABEL_BYTES];
5890       static unsigned last_line_entry_num = 0;
5891       static unsigned prev_file_entry_num = (unsigned) -1;
5892       unsigned this_file_entry_num;
5893
5894       function_section (current_function_decl);
5895       sprintf (label, LINE_CODE_LABEL_FMT, ++last_line_entry_num);
5896       ASM_OUTPUT_LABEL (asm_out_file, label);
5897
5898       fputc ('\n', asm_out_file);
5899
5900       if (use_gnu_debug_info_extensions)
5901         this_file_entry_num = lookup_filename (filename);
5902       else
5903         this_file_entry_num = (unsigned) -1;
5904
5905       ASM_OUTPUT_PUSH_SECTION (asm_out_file, LINE_SECTION);
5906       if (this_file_entry_num != prev_file_entry_num)
5907         {
5908           char line_entry_label[MAX_ARTIFICIAL_LABEL_BYTES];
5909
5910           sprintf (line_entry_label, LINE_ENTRY_LABEL_FMT, last_line_entry_num);
5911           ASM_OUTPUT_LABEL (asm_out_file, line_entry_label);
5912         }
5913
5914       {
5915         const char *tail = strrchr (filename, '/');
5916
5917         if (tail != NULL)
5918           filename = tail;
5919       }
5920
5921       dw2_asm_output_data (4, line, "%s:%u", filename, line);
5922       ASM_OUTPUT_DWARF_DATA2 (asm_out_file, 0xffff);
5923       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, label, TEXT_BEGIN_LABEL);
5924       ASM_OUTPUT_POP_SECTION (asm_out_file);
5925
5926       if (this_file_entry_num != prev_file_entry_num)
5927         generate_srcinfo_entry (last_line_entry_num, this_file_entry_num);
5928       prev_file_entry_num = this_file_entry_num;
5929     }
5930 }
5931
5932 /* Generate an entry in the .debug_macinfo section.  */
5933
5934 static void
5935 generate_macinfo_entry (unsigned int type, rtx offset, const char *string)
5936 {
5937   if (! use_gnu_debug_info_extensions)
5938     return;
5939
5940   fputc ('\n', asm_out_file);
5941   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
5942   assemble_integer (gen_rtx_PLUS (SImode, GEN_INT (type << 24), offset),
5943                     4, BITS_PER_UNIT, 1);
5944   ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, string);
5945   ASM_OUTPUT_POP_SECTION (asm_out_file);
5946 }
5947
5948 /* Wrapper for toplev.c callback to check debug info level.  */
5949 static void
5950 dwarfout_start_source_file_check (unsigned int line, const char *filename)
5951 {
5952   if (debug_info_level == DINFO_LEVEL_VERBOSE)
5953     dwarfout_start_source_file (line, filename);
5954 }
5955
5956 static void
5957 dwarfout_start_source_file (unsigned int line ATTRIBUTE_UNUSED,
5958                             const char *filename)
5959 {
5960   char label[MAX_ARTIFICIAL_LABEL_BYTES];
5961   const char *label1, *label2;
5962
5963   sprintf (label, SFNAMES_ENTRY_LABEL_FMT, lookup_filename (filename));
5964   label1 = (*label == '*') + label;
5965   label2 = (*SFNAMES_BEGIN_LABEL == '*') + SFNAMES_BEGIN_LABEL;
5966   generate_macinfo_entry (MACINFO_start,
5967                           gen_rtx_MINUS (Pmode,
5968                                          gen_rtx_SYMBOL_REF (Pmode, label1),
5969                                          gen_rtx_SYMBOL_REF (Pmode, label2)),
5970                           "");
5971 }
5972
5973 /* Wrapper for toplev.c callback to check debug info level.  */
5974 static void
5975 dwarfout_end_source_file_check (unsigned int lineno)
5976 {
5977   if (debug_info_level == DINFO_LEVEL_VERBOSE)
5978     dwarfout_end_source_file (lineno);
5979 }
5980
5981 static void
5982 dwarfout_end_source_file (unsigned int lineno)
5983 {
5984   generate_macinfo_entry (MACINFO_resume, GEN_INT (lineno), "");
5985 }
5986
5987 /* Called from check_newline in c-parse.y.  The `buffer' parameter
5988    contains the tail part of the directive line, i.e. the part which
5989    is past the initial whitespace, #, whitespace, directive-name,
5990    whitespace part.  */
5991
5992 static void
5993 dwarfout_define (unsigned int lineno, const char *buffer)
5994 {
5995   static int initialized = 0;
5996
5997   if (!initialized)
5998     {
5999       dwarfout_start_source_file (0, primary_filename);
6000       initialized = 1;
6001     }
6002   generate_macinfo_entry (MACINFO_define, GEN_INT (lineno), buffer);
6003 }
6004
6005 /* Called from check_newline in c-parse.y.  The `buffer' parameter
6006    contains the tail part of the directive line, i.e. the part which
6007    is past the initial whitespace, #, whitespace, directive-name,
6008    whitespace part.  */
6009
6010 static void
6011 dwarfout_undef (unsigned int lineno, const char *buffer)
6012 {
6013   generate_macinfo_entry (MACINFO_undef, GEN_INT (lineno), buffer);
6014 }
6015
6016 /* Set up for Dwarf output at the start of compilation.  */
6017
6018 static void
6019 dwarfout_init (const char *main_input_filename)
6020 {
6021   warning ("support for the DWARF1 debugging format is deprecated");
6022
6023   /* Remember the name of the primary input file.  */
6024
6025   primary_filename = main_input_filename;
6026
6027   /* Allocate the initial hunk of the pending_sibling_stack.  */
6028
6029   pending_sibling_stack
6030     = xmalloc (PENDING_SIBLINGS_INCREMENT * sizeof (unsigned));
6031   pending_siblings_allocated = PENDING_SIBLINGS_INCREMENT;
6032   pending_siblings = 1;
6033
6034   /* Allocate the initial hunk of the filename_table.  */
6035
6036   filename_table = xmalloc (FT_ENTRIES_INCREMENT * sizeof (filename_entry));
6037   ft_entries_allocated = FT_ENTRIES_INCREMENT;
6038   ft_entries = 0;
6039
6040   /* Allocate the initial hunk of the pending_types_list.  */
6041
6042   pending_types_list = xmalloc (PENDING_TYPES_INCREMENT * sizeof (tree));
6043   pending_types_allocated = PENDING_TYPES_INCREMENT;
6044   pending_types = 0;
6045
6046   /* Create an artificial RECORD_TYPE node which we can use in our hack
6047      to get the DIEs representing types of formal parameters to come out
6048      only *after* the DIEs for the formal parameters themselves.  */
6049
6050   fake_containing_scope = make_node (RECORD_TYPE);
6051
6052   /* Output a starting label for the .text section.  */
6053
6054   fputc ('\n', asm_out_file);
6055   ASM_OUTPUT_PUSH_SECTION (asm_out_file, TEXT_SECTION_NAME);
6056   ASM_OUTPUT_LABEL (asm_out_file, TEXT_BEGIN_LABEL);
6057   ASM_OUTPUT_POP_SECTION (asm_out_file);
6058
6059   /* Output a starting label for the .data section.  */
6060
6061   fputc ('\n', asm_out_file);
6062   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DATA_SECTION_NAME);
6063   ASM_OUTPUT_LABEL (asm_out_file, DATA_BEGIN_LABEL);
6064   ASM_OUTPUT_POP_SECTION (asm_out_file);
6065
6066 #if 0 /* GNU C doesn't currently use .data1.  */
6067   /* Output a starting label for the .data1 section.  */
6068
6069   fputc ('\n', asm_out_file);
6070   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DATA1_SECTION_NAME);
6071   ASM_OUTPUT_LABEL (asm_out_file, DATA1_BEGIN_LABEL);
6072   ASM_OUTPUT_POP_SECTION (asm_out_file);
6073 #endif
6074
6075   /* Output a starting label for the .rodata section.  */
6076
6077   fputc ('\n', asm_out_file);
6078   ASM_OUTPUT_PUSH_SECTION (asm_out_file, RODATA_SECTION_NAME);
6079   ASM_OUTPUT_LABEL (asm_out_file, RODATA_BEGIN_LABEL);
6080   ASM_OUTPUT_POP_SECTION (asm_out_file);
6081
6082 #if 0 /* GNU C doesn't currently use .rodata1.  */
6083   /* Output a starting label for the .rodata1 section.  */
6084
6085   fputc ('\n', asm_out_file);
6086   ASM_OUTPUT_PUSH_SECTION (asm_out_file, RODATA1_SECTION_NAME);
6087   ASM_OUTPUT_LABEL (asm_out_file, RODATA1_BEGIN_LABEL);
6088   ASM_OUTPUT_POP_SECTION (asm_out_file);
6089 #endif
6090
6091   /* Output a starting label for the .bss section.  */
6092
6093   fputc ('\n', asm_out_file);
6094   ASM_OUTPUT_PUSH_SECTION (asm_out_file, BSS_SECTION_NAME);
6095   ASM_OUTPUT_LABEL (asm_out_file, BSS_BEGIN_LABEL);
6096   ASM_OUTPUT_POP_SECTION (asm_out_file);
6097
6098   if (debug_info_level >= DINFO_LEVEL_NORMAL)
6099     {
6100       if (use_gnu_debug_info_extensions)
6101         {
6102           /* Output a starting label and an initial (compilation directory)
6103              entry for the .debug_sfnames section.  The starting label will be
6104              referenced by the initial entry in the .debug_srcinfo section.  */
6105
6106           fputc ('\n', asm_out_file);
6107           ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SFNAMES_SECTION);
6108           ASM_OUTPUT_LABEL (asm_out_file, SFNAMES_BEGIN_LABEL);
6109           {
6110             const char *pwd = get_src_pwd ();
6111             char *dirname;
6112
6113             if (!pwd)
6114               fatal_error ("can't get current directory: %m");
6115
6116             dirname = concat (pwd, "/", NULL);
6117             ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, dirname);
6118             free (dirname);
6119           }
6120           ASM_OUTPUT_POP_SECTION (asm_out_file);
6121         }
6122
6123       if (debug_info_level >= DINFO_LEVEL_VERBOSE
6124           && use_gnu_debug_info_extensions)
6125         {
6126           /* Output a starting label for the .debug_macinfo section.  This
6127              label will be referenced by the AT_mac_info attribute in the
6128              TAG_compile_unit DIE.  */
6129
6130           fputc ('\n', asm_out_file);
6131           ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
6132           ASM_OUTPUT_LABEL (asm_out_file, MACINFO_BEGIN_LABEL);
6133           ASM_OUTPUT_POP_SECTION (asm_out_file);
6134         }
6135
6136       /* Generate the initial entry for the .line section.  */
6137
6138       fputc ('\n', asm_out_file);
6139       ASM_OUTPUT_PUSH_SECTION (asm_out_file, LINE_SECTION);
6140       ASM_OUTPUT_LABEL (asm_out_file, LINE_BEGIN_LABEL);
6141       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, LINE_END_LABEL, LINE_BEGIN_LABEL);
6142       ASM_OUTPUT_DWARF_ADDR (asm_out_file, TEXT_BEGIN_LABEL);
6143       ASM_OUTPUT_POP_SECTION (asm_out_file);
6144
6145       if (use_gnu_debug_info_extensions)
6146         {
6147           /* Generate the initial entry for the .debug_srcinfo section.  */
6148
6149           fputc ('\n', asm_out_file);
6150           ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SRCINFO_SECTION);
6151           ASM_OUTPUT_LABEL (asm_out_file, SRCINFO_BEGIN_LABEL);
6152           ASM_OUTPUT_DWARF_ADDR (asm_out_file, LINE_BEGIN_LABEL);
6153           ASM_OUTPUT_DWARF_ADDR (asm_out_file, SFNAMES_BEGIN_LABEL);
6154           ASM_OUTPUT_DWARF_ADDR (asm_out_file, TEXT_BEGIN_LABEL);
6155           ASM_OUTPUT_DWARF_ADDR (asm_out_file, TEXT_END_LABEL);
6156 #ifdef DWARF_TIMESTAMPS
6157           ASM_OUTPUT_DWARF_DATA4 (asm_out_file, time (NULL));
6158 #else
6159           ASM_OUTPUT_DWARF_DATA4 (asm_out_file, -1);
6160 #endif
6161           ASM_OUTPUT_POP_SECTION (asm_out_file);
6162         }
6163
6164       /* Generate the initial entry for the .debug_pubnames section.  */
6165
6166       fputc ('\n', asm_out_file);
6167       ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_PUBNAMES_SECTION);
6168       ASM_OUTPUT_DWARF_ADDR (asm_out_file, DEBUG_BEGIN_LABEL);
6169       ASM_OUTPUT_POP_SECTION (asm_out_file);
6170
6171       /* Generate the initial entry for the .debug_aranges section.  */
6172
6173       fputc ('\n', asm_out_file);
6174       ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_ARANGES_SECTION);
6175       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file,
6176                                DEBUG_ARANGES_END_LABEL,
6177                                DEBUG_ARANGES_BEGIN_LABEL);
6178       ASM_OUTPUT_LABEL (asm_out_file, DEBUG_ARANGES_BEGIN_LABEL);
6179       ASM_OUTPUT_DWARF_DATA1 (asm_out_file, 1);
6180       ASM_OUTPUT_DWARF_ADDR (asm_out_file, DEBUG_BEGIN_LABEL);
6181       ASM_OUTPUT_POP_SECTION (asm_out_file);
6182     }
6183
6184   /* Setup first DIE number == 1.  */
6185   NEXT_DIE_NUM = next_unused_dienum++;
6186
6187   /* Generate the initial DIE for the .debug section.  Note that the
6188      (string) value given in the AT_name attribute of the TAG_compile_unit
6189      DIE will (typically) be a relative pathname and that this pathname
6190      should be taken as being relative to the directory from which the
6191      compiler was invoked when the given (base) source file was compiled.  */
6192
6193   fputc ('\n', asm_out_file);
6194   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SECTION);
6195   ASM_OUTPUT_LABEL (asm_out_file, DEBUG_BEGIN_LABEL);
6196   output_die (output_compile_unit_die, (void *) main_input_filename);
6197   ASM_OUTPUT_POP_SECTION (asm_out_file);
6198
6199   fputc ('\n', asm_out_file);
6200 }
6201
6202 /* Output stuff that dwarf requires at the end of every file.  */
6203
6204 static void
6205 dwarfout_finish (const char *main_input_filename ATTRIBUTE_UNUSED)
6206 {
6207   char label[MAX_ARTIFICIAL_LABEL_BYTES];
6208
6209   fputc ('\n', asm_out_file);
6210   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SECTION);
6211   retry_incomplete_types ();
6212   fputc ('\n', asm_out_file);
6213
6214   /* Mark the end of the chain of siblings which represent all file-scope
6215      declarations in this compilation unit.  */
6216
6217   /* The (null) DIE which represents the terminator for the (sibling linked)
6218      list of file-scope items is *special*.  Normally, we would just call
6219      end_sibling_chain at this point in order to output a word with the
6220      value `4' and that word would act as the terminator for the list of
6221      DIEs describing file-scope items.  Unfortunately, if we were to simply
6222      do that, the label that would follow this DIE in the .debug section
6223      (i.e. `..D2') would *not* be properly aligned (as it must be on some
6224      machines) to a 4 byte boundary.
6225
6226      In order to force the label `..D2' to get aligned to a 4 byte boundary,
6227      the trick used is to insert extra (otherwise useless) padding bytes
6228      into the (null) DIE that we know must precede the ..D2 label in the
6229      .debug section.  The amount of padding required can be anywhere between
6230      0 and 3 bytes.  The length word at the start of this DIE (i.e. the one
6231      with the padding) would normally contain the value 4, but now it will
6232      also have to include the padding bytes, so it will instead have some
6233      value in the range 4..7.
6234
6235      Fortunately, the rules of Dwarf say that any DIE whose length word
6236      contains *any* value less than 8 should be treated as a null DIE, so
6237      this trick works out nicely.  Clever, eh?  Don't give me any credit
6238      (or blame).  I didn't think of this scheme.  I just conformed to it.
6239   */
6240
6241   output_die (output_padded_null_die, (void *) 0);
6242   dienum_pop ();
6243
6244   sprintf (label, DIE_BEGIN_LABEL_FMT, NEXT_DIE_NUM);
6245   ASM_OUTPUT_LABEL (asm_out_file, label);       /* should be ..D2 */
6246   ASM_OUTPUT_POP_SECTION (asm_out_file);
6247
6248   /* Output a terminator label for the .text section.  */
6249
6250   fputc ('\n', asm_out_file);
6251   ASM_OUTPUT_PUSH_SECTION (asm_out_file, TEXT_SECTION_NAME);
6252   ASM_OUTPUT_LABEL (asm_out_file, TEXT_END_LABEL);
6253   ASM_OUTPUT_POP_SECTION (asm_out_file);
6254
6255   /* Output a terminator label for the .data section.  */
6256
6257   fputc ('\n', asm_out_file);
6258   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DATA_SECTION_NAME);
6259   ASM_OUTPUT_LABEL (asm_out_file, DATA_END_LABEL);
6260   ASM_OUTPUT_POP_SECTION (asm_out_file);
6261
6262 #if 0 /* GNU C doesn't currently use .data1.  */
6263   /* Output a terminator label for the .data1 section.  */
6264
6265   fputc ('\n', asm_out_file);
6266   ASM_OUTPUT_PUSH_SECTION (asm_out_file, DATA1_SECTION_NAME);
6267   ASM_OUTPUT_LABEL (asm_out_file, DATA1_END_LABEL);
6268   ASM_OUTPUT_POP_SECTION (asm_out_file);
6269 #endif
6270
6271   /* Output a terminator label for the .rodata section.  */
6272
6273   fputc ('\n', asm_out_file);
6274   ASM_OUTPUT_PUSH_SECTION (asm_out_file, RODATA_SECTION_NAME);
6275   ASM_OUTPUT_LABEL (asm_out_file, RODATA_END_LABEL);
6276   ASM_OUTPUT_POP_SECTION (asm_out_file);
6277
6278 #if 0 /* GNU C doesn't currently use .rodata1.  */
6279   /* Output a terminator label for the .rodata1 section.  */
6280
6281   fputc ('\n', asm_out_file);
6282   ASM_OUTPUT_PUSH_SECTION (asm_out_file, RODATA1_SECTION_NAME);
6283   ASM_OUTPUT_LABEL (asm_out_file, RODATA1_END_LABEL);
6284   ASM_OUTPUT_POP_SECTION (asm_out_file);
6285 #endif
6286
6287   /* Output a terminator label for the .bss section.  */
6288
6289   fputc ('\n', asm_out_file);
6290   ASM_OUTPUT_PUSH_SECTION (asm_out_file, BSS_SECTION_NAME);
6291   ASM_OUTPUT_LABEL (asm_out_file, BSS_END_LABEL);
6292   ASM_OUTPUT_POP_SECTION (asm_out_file);
6293
6294   if (debug_info_level >= DINFO_LEVEL_NORMAL)
6295     {
6296       /* Output a terminating entry for the .line section.  */
6297
6298       fputc ('\n', asm_out_file);
6299       ASM_OUTPUT_PUSH_SECTION (asm_out_file, LINE_SECTION);
6300       ASM_OUTPUT_LABEL (asm_out_file, LINE_LAST_ENTRY_LABEL);
6301       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
6302       ASM_OUTPUT_DWARF_DATA2 (asm_out_file, 0xffff);
6303       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, TEXT_END_LABEL, TEXT_BEGIN_LABEL);
6304       ASM_OUTPUT_LABEL (asm_out_file, LINE_END_LABEL);
6305       ASM_OUTPUT_POP_SECTION (asm_out_file);
6306
6307       if (use_gnu_debug_info_extensions)
6308         {
6309           /* Output a terminating entry for the .debug_srcinfo section.  */
6310
6311           fputc ('\n', asm_out_file);
6312           ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_SRCINFO_SECTION);
6313           ASM_OUTPUT_DWARF_DELTA4 (asm_out_file,
6314                                    LINE_LAST_ENTRY_LABEL, LINE_BEGIN_LABEL);
6315           ASM_OUTPUT_DWARF_DATA4 (asm_out_file, -1);
6316           ASM_OUTPUT_POP_SECTION (asm_out_file);
6317         }
6318
6319       if (debug_info_level >= DINFO_LEVEL_VERBOSE)
6320         {
6321           /* Output terminating entries for the .debug_macinfo section.  */
6322
6323           dwarfout_end_source_file (0);
6324
6325           fputc ('\n', asm_out_file);
6326           ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_MACINFO_SECTION);
6327           ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
6328           ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
6329           ASM_OUTPUT_POP_SECTION (asm_out_file);
6330         }
6331
6332       /* Generate the terminating entry for the .debug_pubnames section.  */
6333
6334       fputc ('\n', asm_out_file);
6335       ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_PUBNAMES_SECTION);
6336       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
6337       ASM_OUTPUT_DWARF_STRING_NEWLINE (asm_out_file, "");
6338       ASM_OUTPUT_POP_SECTION (asm_out_file);
6339
6340       /* Generate the terminating entries for the .debug_aranges section.
6341
6342          Note that we want to do this only *after* we have output the end
6343          labels (for the various program sections) which we are going to
6344          refer to here.  This allows us to work around a bug in the m68k
6345          svr4 assembler.  That assembler gives bogus assembly-time errors
6346          if (within any given section) you try to take the difference of
6347          two relocatable symbols, both of which are located within some
6348          other section, and if one (or both?) of the symbols involved is
6349          being forward-referenced.  By generating the .debug_aranges
6350          entries at this late point in the assembly output, we skirt the
6351          issue simply by avoiding forward-references.
6352       */
6353
6354       fputc ('\n', asm_out_file);
6355       ASM_OUTPUT_PUSH_SECTION (asm_out_file, DEBUG_ARANGES_SECTION);
6356
6357       ASM_OUTPUT_DWARF_ADDR (asm_out_file, TEXT_BEGIN_LABEL);
6358       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, TEXT_END_LABEL, TEXT_BEGIN_LABEL);
6359
6360       ASM_OUTPUT_DWARF_ADDR (asm_out_file, DATA_BEGIN_LABEL);
6361       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, DATA_END_LABEL, DATA_BEGIN_LABEL);
6362
6363 #if 0 /* GNU C doesn't currently use .data1.  */
6364       ASM_OUTPUT_DWARF_ADDR (asm_out_file, DATA1_BEGIN_LABEL);
6365       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, DATA1_END_LABEL,
6366                                              DATA1_BEGIN_LABEL);
6367 #endif
6368
6369       ASM_OUTPUT_DWARF_ADDR (asm_out_file, RODATA_BEGIN_LABEL);
6370       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, RODATA_END_LABEL,
6371                                              RODATA_BEGIN_LABEL);
6372
6373 #if 0 /* GNU C doesn't currently use .rodata1.  */
6374       ASM_OUTPUT_DWARF_ADDR (asm_out_file, RODATA1_BEGIN_LABEL);
6375       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, RODATA1_END_LABEL,
6376                                              RODATA1_BEGIN_LABEL);
6377 #endif
6378
6379       ASM_OUTPUT_DWARF_ADDR (asm_out_file, BSS_BEGIN_LABEL);
6380       ASM_OUTPUT_DWARF_DELTA4 (asm_out_file, BSS_END_LABEL, BSS_BEGIN_LABEL);
6381
6382       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
6383       ASM_OUTPUT_DWARF_DATA4 (asm_out_file, 0);
6384
6385       ASM_OUTPUT_LABEL (asm_out_file, DEBUG_ARANGES_END_LABEL);
6386       ASM_OUTPUT_POP_SECTION (asm_out_file);
6387     }
6388
6389   /* There should not be any pending types left at the end.  We need
6390      this now because it may not have been checked on the last call to
6391      dwarfout_file_scope_decl.  */
6392   if (pending_types != 0)
6393     abort ();
6394 }
6395
6396 #endif /* DWARF_DEBUGGING_INFO */