OSDN Git Service

* gcc.target/i386/align-main-1.c (check): Mark noinline.
[pf3gnuchains/gcc-fork.git] / gcc / vmsdbgout.c
1 /* Output VMS debug format symbol table information from GCC.
2    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
4    Free Software Foundation, Inc.
5    Contributed by Douglas B. Rupp (rupp@gnat.com).
6    Updated by Bernard W. Giroud (bgiroud@users.sourceforge.net).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28
29 #ifdef VMS_DEBUGGING_INFO
30 #include "tree.h"
31 #include "version.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "output.h"
35 #include "vmsdbg.h"
36 #include "debug.h"
37 #include "langhooks.h"
38 #include "function.h"
39 #include "target.h"
40
41 /* Difference in seconds between the VMS Epoch and the Unix Epoch */
42 static const long long vms_epoch_offset = 3506716800ll;
43
44 /* NOTE: In the comments in this file, many references are made to "Debug
45    Symbol Table".  This term is abbreviated as `DST' throughout the remainder
46    of this file.  */
47
48 typedef struct dst_line_info_struct *dst_line_info_ref;
49
50 /* Each entry in the line_info_table maintains the file and
51    line number associated with the label generated for that
52    entry.  The label gives the PC value associated with
53    the line number entry.  */
54 typedef struct dst_line_info_struct
55 {
56   unsigned long dst_file_num;
57   unsigned long dst_line_num;
58 }
59 dst_line_info_entry;
60
61 typedef struct dst_file_info_struct *dst_file_info_ref;
62
63 typedef struct dst_file_info_struct
64 {
65   char *file_name;
66   unsigned int max_line;
67   unsigned int listing_line_start;
68   long long cdt;
69   long ebk;
70   short ffb;
71   char rfo;
72   char flen;
73 }
74 dst_file_info_entry;
75
76 /* How to start an assembler comment.  */
77 #ifndef ASM_COMMENT_START
78 #define ASM_COMMENT_START ";#"
79 #endif
80
81 /* Maximum size (in bytes) of an artificially generated label.  */
82 #define MAX_ARTIFICIAL_LABEL_BYTES      30
83
84 /* Make sure we know the sizes of the various types debug can describe. These
85    are only defaults.  If the sizes are different for your target, you should
86    override these values by defining the appropriate symbols in your tm.h
87    file.  */
88 #ifndef PTR_SIZE
89 #define PTR_SIZE 4 /* Must be 32 bits for VMS debug info */
90 #endif
91
92 /* Pointer to a structure of filenames referenced by this compilation unit.  */
93 static dst_file_info_ref file_info_table;
94
95 /* Total number of entries in the table (i.e. array) pointed to by
96    `file_info_table'.  This is the *total* and includes both used and unused
97    slots.  */
98 static unsigned int file_info_table_allocated;
99
100 /* Number of entries in the file_info_table which are actually in use.  */
101 static unsigned int file_info_table_in_use;
102
103 /* Size (in elements) of increments by which we may expand the filename
104    table.  */
105 #define FILE_TABLE_INCREMENT 64
106
107 /* A structure to hold basic information for the VMS end
108    routine.  */
109
110 typedef struct vms_func_struct
111 {
112   const char *vms_func_name;
113   unsigned funcdef_number;
114 }
115 vms_func_node;
116
117 typedef struct vms_func_struct *vms_func_ref;
118
119 static unsigned int func_table_allocated;
120 static unsigned int func_table_in_use;
121 #define FUNC_TABLE_INCREMENT 256
122
123 /* A pointer to the base of a table that contains frame description
124    information for each routine.  */
125 static vms_func_ref func_table;
126
127 /* Local pointer to the name of the main input file.  Initialized in
128    vmsdbgout_init.  */
129 static const char *primary_filename;
130
131 static char *module_producer;
132 static unsigned int module_language;
133
134 /* A pointer to the base of a table that contains line information
135    for each source code line in .text in the compilation unit.  */
136 static dst_line_info_ref line_info_table;
137
138 /* Number of elements currently allocated for line_info_table.  */
139 static unsigned int line_info_table_allocated;
140
141 /* Number of elements in line_info_table currently in use.  */
142 static unsigned int line_info_table_in_use;
143
144 /* Size (in elements) of increments by which we may expand line_info_table.  */
145 #define LINE_INFO_TABLE_INCREMENT 1024
146
147 /* Forward declarations for functions defined in this file.  */
148 static char *full_name (const char *);
149 static unsigned int lookup_filename (const char *);
150 static void addr_const_to_string (char *, rtx);
151 static int write_debug_header (DST_HEADER *, const char *, int);
152 static int write_debug_addr (const char *, const char *, int);
153 static int write_debug_data1 (unsigned int, const char *, int);
154 static int write_debug_data2 (unsigned int, const char *, int);
155 static int write_debug_data4 (unsigned long, const char *, int);
156 static int write_debug_data8 (unsigned long long, const char *, int);
157 static int write_debug_delta4 (const char *, const char *, const char *, int);
158 static int write_debug_string (const char *, const char *, int);
159 static int write_modbeg (int);
160 static int write_modend (int);
161 static int write_rtnbeg (int, int);
162 static int write_rtnend (int, int);
163 static int write_pclines (int);
164 static int write_srccorr (int, dst_file_info_entry, int);
165 static int write_srccorrs (int);
166
167 static void vmsdbgout_init (const char *);
168 static void vmsdbgout_finish (const char *);
169 static void vmsdbgout_define (unsigned int, const char *);
170 static void vmsdbgout_undef (unsigned int, const char *);
171 static void vmsdbgout_start_source_file (unsigned int, const char *);
172 static void vmsdbgout_end_source_file (unsigned int);
173 static void vmsdbgout_begin_block (unsigned int, unsigned int);
174 static void vmsdbgout_end_block (unsigned int, unsigned int);
175 static bool vmsdbgout_ignore_block (const_tree);
176 static void vmsdbgout_source_line (unsigned int, const char *);
177 static void vmsdbgout_begin_prologue (unsigned int, const char *);
178 static void vmsdbgout_end_prologue (unsigned int, const char *);
179 static void vmsdbgout_end_function (unsigned int);
180 static void vmsdbgout_end_epilogue (unsigned int, const char *);
181 static void vmsdbgout_begin_function (tree);
182 static void vmsdbgout_decl (tree);
183 static void vmsdbgout_global_decl (tree);
184 static void vmsdbgout_abstract_function (tree);
185
186 /* The debug hooks structure.  */
187
188 const struct gcc_debug_hooks vmsdbg_debug_hooks
189 = {vmsdbgout_init,
190    vmsdbgout_finish,
191    vmsdbgout_define,
192    vmsdbgout_undef,
193    vmsdbgout_start_source_file,
194    vmsdbgout_end_source_file,
195    vmsdbgout_begin_block,
196    vmsdbgout_end_block,
197    vmsdbgout_ignore_block,
198    vmsdbgout_source_line,
199    vmsdbgout_begin_prologue,
200    vmsdbgout_end_prologue,
201    vmsdbgout_end_epilogue,
202    vmsdbgout_begin_function,
203    vmsdbgout_end_function,
204    vmsdbgout_decl,
205    vmsdbgout_global_decl,
206    debug_nothing_tree_int,        /* type_decl */
207    debug_nothing_tree_tree_tree_bool, /* imported_module_or_decl */
208    debug_nothing_tree,            /* deferred_inline_function */
209    vmsdbgout_abstract_function,
210    debug_nothing_rtx,             /* label */
211    debug_nothing_int,             /* handle_pch */
212    debug_nothing_rtx,             /* var_location */
213    debug_nothing_void,            /* switch_text_section */
214    debug_nothing_tree_tree,       /* set_name */
215    0                              /* start_end_main_source_file */
216 };
217
218 /* Definitions of defaults for assembler-dependent names of various
219    pseudo-ops and section names.
220    Theses may be overridden in the tm.h file (if necessary) for a particular
221    assembler.  */
222 #ifdef UNALIGNED_SHORT_ASM_OP
223 #undef UNALIGNED_SHORT_ASM_OP
224 #endif
225 #define UNALIGNED_SHORT_ASM_OP  ".word"
226
227 #ifdef UNALIGNED_INT_ASM_OP
228 #undef UNALIGNED_INT_ASM_OP
229 #endif
230 #define UNALIGNED_INT_ASM_OP    ".long"
231
232 #ifdef UNALIGNED_LONG_ASM_OP
233 #undef UNALIGNED_LONG_ASM_OP
234 #endif
235 #define UNALIGNED_LONG_ASM_OP   ".long"
236
237 #ifdef UNALIGNED_DOUBLE_INT_ASM_OP
238 #undef UNALIGNED_DOUBLE_INT_ASM_OP
239 #endif
240 #define UNALIGNED_DOUBLE_INT_ASM_OP     ".quad"
241
242 #ifdef ASM_BYTE_OP
243 #undef ASM_BYTE_OP
244 #endif
245 #define ASM_BYTE_OP     ".byte"
246
247 #define NUMBYTES(I) ((I) < 256 ? 1 : (I) < 65536 ? 2 : 4)
248
249 #define NUMBYTES0(I) ((I) < 128 ? 0 : (I) < 65536 ? 2 : 4)
250
251 #ifndef UNALIGNED_PTR_ASM_OP
252 #define UNALIGNED_PTR_ASM_OP \
253   (PTR_SIZE == 8 ? UNALIGNED_DOUBLE_INT_ASM_OP : UNALIGNED_INT_ASM_OP)
254 #endif
255
256 #ifndef UNALIGNED_OFFSET_ASM_OP
257 #define UNALIGNED_OFFSET_ASM_OP(OFFSET) \
258   (NUMBYTES(OFFSET) == 4 \
259    ? UNALIGNED_LONG_ASM_OP \
260    : (NUMBYTES(OFFSET) == 2 ? UNALIGNED_SHORT_ASM_OP : ASM_BYTE_OP))
261 #endif
262
263 /* Definitions of defaults for formats and names of various special
264    (artificial) labels which may be generated within this file (when the -g
265    options is used and VMS_DEBUGGING_INFO is in effect.  If necessary, these
266    may be overridden from within the tm.h file, but typically, overriding these
267    defaults is unnecessary.  */
268
269 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
270
271 #ifndef TEXT_END_LABEL
272 #define TEXT_END_LABEL          "Lvetext"
273 #endif
274 #ifndef FUNC_BEGIN_LABEL
275 #define FUNC_BEGIN_LABEL        "LVFB"
276 #endif
277 #ifndef FUNC_PROLOG_LABEL
278 #define FUNC_PROLOG_LABEL       "LVFP"
279 #endif
280 #ifndef FUNC_END_LABEL
281 #define FUNC_END_LABEL          "LVFE"
282 #endif
283 #ifndef BLOCK_BEGIN_LABEL
284 #define BLOCK_BEGIN_LABEL       "LVBB"
285 #endif
286 #ifndef BLOCK_END_LABEL
287 #define BLOCK_END_LABEL         "LVBE"
288 #endif
289 #ifndef LINE_CODE_LABEL
290 #define LINE_CODE_LABEL         "LVM"
291 #endif
292
293 #ifndef ASM_OUTPUT_DEBUG_DELTA2
294 #define ASM_OUTPUT_DEBUG_DELTA2(FILE,LABEL1,LABEL2)                      \
295   do                                                                     \
296     {                                                                    \
297       fprintf ((FILE), "\t%s\t", UNALIGNED_SHORT_ASM_OP);                \
298       assemble_name (FILE, LABEL1);                                      \
299       fprintf (FILE, "-");                                               \
300       assemble_name (FILE, LABEL2);                                      \
301     }                                                                    \
302   while (0)
303 #endif
304
305 #ifndef ASM_OUTPUT_DEBUG_DELTA4
306 #define ASM_OUTPUT_DEBUG_DELTA4(FILE,LABEL1,LABEL2)                      \
307   do                                                                     \
308     {                                                                    \
309       fprintf ((FILE), "\t%s\t", UNALIGNED_INT_ASM_OP);                  \
310       assemble_name (FILE, LABEL1);                                      \
311       fprintf (FILE, "-");                                               \
312       assemble_name (FILE, LABEL2);                                      \
313     }                                                                    \
314   while (0)
315 #endif
316
317 #ifndef ASM_OUTPUT_DEBUG_ADDR_DELTA
318 #define ASM_OUTPUT_DEBUG_ADDR_DELTA(FILE,LABEL1,LABEL2)                  \
319   do                                                                     \
320     {                                                                    \
321       fprintf ((FILE), "\t%s\t", UNALIGNED_PTR_ASM_OP);                  \
322       assemble_name (FILE, LABEL1);                                      \
323       fprintf (FILE, "-");                                               \
324       assemble_name (FILE, LABEL2);                                      \
325     }                                                                    \
326   while (0)
327 #endif
328
329 #ifndef ASM_OUTPUT_DEBUG_ADDR
330 #define ASM_OUTPUT_DEBUG_ADDR(FILE,LABEL)                                \
331   do                                                                     \
332     {                                                                    \
333       fprintf ((FILE), "\t%s\t", UNALIGNED_PTR_ASM_OP);                  \
334       assemble_name (FILE, LABEL);                                       \
335     }                                                                    \
336   while (0)
337 #endif
338
339 #ifndef ASM_OUTPUT_DEBUG_ADDR_CONST
340 #define ASM_OUTPUT_DEBUG_ADDR_CONST(FILE,ADDR)                          \
341   fprintf ((FILE), "\t%s\t%s", UNALIGNED_PTR_ASM_OP, (ADDR))
342 #endif
343
344 #ifndef ASM_OUTPUT_DEBUG_DATA1
345 #define ASM_OUTPUT_DEBUG_DATA1(FILE,VALUE) \
346   fprintf ((FILE), "\t%s\t0x%x", ASM_BYTE_OP, (unsigned char) VALUE)
347 #endif
348
349 #ifndef ASM_OUTPUT_DEBUG_DATA2
350 #define ASM_OUTPUT_DEBUG_DATA2(FILE,VALUE) \
351   fprintf ((FILE), "\t%s\t0x%x", UNALIGNED_SHORT_ASM_OP, \
352            (unsigned short) VALUE)
353 #endif
354
355 #ifndef ASM_OUTPUT_DEBUG_DATA4
356 #define ASM_OUTPUT_DEBUG_DATA4(FILE,VALUE) \
357   fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_INT_ASM_OP, (unsigned long) VALUE)
358 #endif
359
360 #ifndef ASM_OUTPUT_DEBUG_DATA
361 #define ASM_OUTPUT_DEBUG_DATA(FILE,VALUE) \
362   fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_OFFSET_ASM_OP(VALUE), VALUE)
363 #endif
364
365 #ifndef ASM_OUTPUT_DEBUG_ADDR_DATA
366 #define ASM_OUTPUT_DEBUG_ADDR_DATA(FILE,VALUE) \
367   fprintf ((FILE), "\t%s\t0x%lx", UNALIGNED_PTR_ASM_OP, \
368            (unsigned long) VALUE)
369 #endif
370
371 #ifndef ASM_OUTPUT_DEBUG_DATA8
372 #define ASM_OUTPUT_DEBUG_DATA8(FILE,VALUE) \
373   fprintf ((FILE), "\t%s\t0x%llx", UNALIGNED_DOUBLE_INT_ASM_OP, \
374                                  (unsigned long long) VALUE)
375 #endif
376
377 /* This is similar to the default ASM_OUTPUT_ASCII, except that no trailing
378    newline is produced.  When flag_verbose_asm is asserted, we add commentary
379    at the end of the line, so we must avoid output of a newline here.  */
380 #ifndef ASM_OUTPUT_DEBUG_STRING
381 #define ASM_OUTPUT_DEBUG_STRING(FILE,P)         \
382   do                                            \
383     {                                           \
384       register int slen = strlen(P);            \
385       register const char *p = (P);             \
386       register int i;                           \
387       fprintf (FILE, "\t.ascii \"");            \
388       for (i = 0; i < slen; i++)                \
389         {                                       \
390           register int c = p[i];                \
391           if (c == '\"' || c == '\\')           \
392             putc ('\\', FILE);                  \
393           if (c >= ' ' && c < 0177)             \
394             putc (c, FILE);                     \
395           else                                  \
396             fprintf (FILE, "\\%o", c);          \
397         }                                       \
398       fprintf (FILE, "\"");                     \
399     }                                           \
400   while (0)
401 #endif
402
403 /* Convert a reference to the assembler name of a C-level name.  This
404    macro has the same effect as ASM_OUTPUT_LABELREF, but copies to
405    a string rather than writing to a file.  */
406 #ifndef ASM_NAME_TO_STRING
407 #define ASM_NAME_TO_STRING(STR, NAME)           \
408   do                                            \
409     {                                           \
410       if ((NAME)[0] == '*')                     \
411         strcpy (STR, NAME+1);                   \
412       else                                      \
413         strcpy (STR, NAME);                     \
414     }                                           \
415   while (0)
416 #endif
417
418 \f
419 /* General utility functions.  */
420
421 /* Convert an integer constant expression into assembler syntax.  Addition and
422    subtraction are the only arithmetic that may appear in these expressions.
423    This is an adaptation of output_addr_const in final.c.  Here, the target
424    of the conversion is a string buffer.  We can't use output_addr_const
425    directly, because it writes to a file.  */
426
427 static void
428 addr_const_to_string (char *str, rtx x)
429 {
430   char buf1[256];
431   char buf2[256];
432
433  restart:
434   str[0] = '\0';
435   switch (GET_CODE (x))
436     {
437     case PC:
438       gcc_assert (flag_pic);
439       strcat (str, ",");
440       break;
441
442     case SYMBOL_REF:
443       ASM_NAME_TO_STRING (buf1, XSTR (x, 0));
444       strcat (str, buf1);
445       break;
446
447     case LABEL_REF:
448       ASM_GENERATE_INTERNAL_LABEL (buf1, "L", CODE_LABEL_NUMBER (XEXP (x, 0)));
449       ASM_NAME_TO_STRING (buf2, buf1);
450       strcat (str, buf2);
451       break;
452
453     case CODE_LABEL:
454       ASM_GENERATE_INTERNAL_LABEL (buf1, "L", CODE_LABEL_NUMBER (x));
455       ASM_NAME_TO_STRING (buf2, buf1);
456       strcat (str, buf2);
457       break;
458
459     case CONST_INT:
460       sprintf (buf1, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
461       strcat (str, buf1);
462       break;
463
464     case CONST:
465       /* This used to output parentheses around the expression, but that does
466          not work on the 386 (either ATT or BSD assembler).  */
467       addr_const_to_string (buf1, XEXP (x, 0));
468       strcat (str, buf1);
469       break;
470
471     case CONST_DOUBLE:
472       if (GET_MODE (x) == VOIDmode)
473         {
474           /* We can use %d if the number is one word and positive.  */
475           if (CONST_DOUBLE_HIGH (x))
476             sprintf (buf1, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
477                      CONST_DOUBLE_HIGH (x), CONST_DOUBLE_LOW (x));
478           else if (CONST_DOUBLE_LOW (x) < 0)
479             sprintf (buf1, HOST_WIDE_INT_PRINT_HEX, CONST_DOUBLE_LOW (x));
480           else
481             sprintf (buf1, HOST_WIDE_INT_PRINT_DEC,
482                      CONST_DOUBLE_LOW (x));
483           strcat (str, buf1);
484         }
485       else
486         /* We can't handle floating point constants; PRINT_OPERAND must
487            handle them.  */
488         output_operand_lossage ("floating constant misused");
489       break;
490
491     case PLUS:
492       /* Some assemblers need integer constants to appear last (eg masm).  */
493       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
494         {
495           addr_const_to_string (buf1, XEXP (x, 1));
496           strcat (str, buf1);
497           if (INTVAL (XEXP (x, 0)) >= 0)
498             strcat (str, "+");
499           addr_const_to_string (buf1, XEXP (x, 0));
500           strcat (str, buf1);
501         }
502       else
503         {
504           addr_const_to_string (buf1, XEXP (x, 0));
505           strcat (str, buf1);
506           if (INTVAL (XEXP (x, 1)) >= 0)
507             strcat (str, "+");
508           addr_const_to_string (buf1, XEXP (x, 1));
509           strcat (str, buf1);
510         }
511       break;
512
513     case MINUS:
514       /* Avoid outputting things like x-x or x+5-x, since some assemblers
515          can't handle that.  */
516       x = simplify_subtraction (x);
517       if (GET_CODE (x) != MINUS)
518         goto restart;
519
520       addr_const_to_string (buf1, XEXP (x, 0));
521       strcat (str, buf1);
522       strcat (str, "-");
523       if (GET_CODE (XEXP (x, 1)) == CONST_INT
524           && INTVAL (XEXP (x, 1)) < 0)
525         {
526           strcat (str, "(");
527           addr_const_to_string (buf1, XEXP (x, 1));
528           strcat (str, buf1);
529           strcat (str, ")");
530         }
531       else
532         {
533           addr_const_to_string (buf1, XEXP (x, 1));
534           strcat (str, buf1);
535         }
536       break;
537
538     case ZERO_EXTEND:
539     case SIGN_EXTEND:
540       addr_const_to_string (buf1, XEXP (x, 0));
541       strcat (str, buf1);
542       break;
543
544     default:
545       output_operand_lossage ("invalid expression as operand");
546     }
547 }
548
549 /* Output the debug header HEADER.  Also output COMMENT if flag_verbose_asm is
550    set.  Return the header size.  Just return the size if DOSIZEONLY is
551    nonzero.  */
552
553 static int
554 write_debug_header (DST_HEADER *header, const char *comment, int dosizeonly)
555 {
556   if (!dosizeonly)
557     {
558       ASM_OUTPUT_DEBUG_DATA2 (asm_out_file,
559                               header->dst__header_length.dst_w_length);
560
561       if (flag_verbose_asm)
562         fprintf (asm_out_file, "\t%s record length", ASM_COMMENT_START);
563       fputc ('\n', asm_out_file);
564
565       ASM_OUTPUT_DEBUG_DATA2 (asm_out_file,
566                               header->dst__header_type.dst_w_type);
567
568       if (flag_verbose_asm)
569         fprintf (asm_out_file, "\t%s record type (%s)", ASM_COMMENT_START,
570                  comment);
571
572       fputc ('\n', asm_out_file);
573     }
574
575   return 4;
576 }
577
578 /* Output the address of SYMBOL.  Also output COMMENT if flag_verbose_asm is
579    set.  Return the address size.  Just return the size if DOSIZEONLY is
580    nonzero.  */
581
582 static int
583 write_debug_addr (const char *symbol, const char *comment, int dosizeonly)
584 {
585   if (!dosizeonly)
586     {
587       ASM_OUTPUT_DEBUG_ADDR (asm_out_file, symbol);
588       if (flag_verbose_asm)
589         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
590       fputc ('\n', asm_out_file);
591     }
592
593   return PTR_SIZE;
594 }
595
596 /* Output the single byte DATA1.  Also output COMMENT if flag_verbose_asm is
597    set.  Return the data size.  Just return the size if DOSIZEONLY is
598    nonzero.  */
599
600 static int
601 write_debug_data1 (unsigned int data1, const char *comment, int dosizeonly)
602 {
603   if (!dosizeonly)
604     {
605       ASM_OUTPUT_DEBUG_DATA1 (asm_out_file, data1);
606       if (flag_verbose_asm)
607         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
608       fputc ('\n', asm_out_file);
609     }
610
611   return 1;
612 }
613
614 /* Output the single word DATA2.  Also output COMMENT if flag_verbose_asm is
615    set.  Return the data size.  Just return the size if DOSIZEONLY is
616    nonzero.  */
617
618 static int
619 write_debug_data2 (unsigned int data2, const char *comment, int dosizeonly)
620 {
621   if (!dosizeonly)
622     {
623       ASM_OUTPUT_DEBUG_DATA2 (asm_out_file, data2);
624       if (flag_verbose_asm)
625         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
626       fputc ('\n', asm_out_file);
627     }
628
629   return 2;
630 }
631
632 /* Output double word DATA4.  Also output COMMENT if flag_verbose_asm is set.
633    Return the data size.  Just return the size if DOSIZEONLY is nonzero.  */
634
635 static int
636 write_debug_data4 (unsigned long data4, const char *comment, int dosizeonly)
637 {
638   if (!dosizeonly)
639     {
640       ASM_OUTPUT_DEBUG_DATA4 (asm_out_file, data4);
641       if (flag_verbose_asm)
642         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
643       fputc ('\n', asm_out_file);
644     }
645
646   return 4;
647 }
648
649 /* Output quad word DATA8.  Also output COMMENT if flag_verbose_asm is set.
650    Return the data size.  Just return the size if DOSIZEONLY is nonzero.  */
651
652 static int
653 write_debug_data8 (unsigned long long data8, const char *comment,
654                    int dosizeonly)
655 {
656   if (!dosizeonly)
657     {
658       ASM_OUTPUT_DEBUG_DATA8 (asm_out_file, data8);
659       if (flag_verbose_asm)
660         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
661       fputc ('\n', asm_out_file);
662     }
663
664   return 8;
665 }
666
667 /* Output the difference between LABEL1 and LABEL2.  Also output COMMENT if
668    flag_verbose_asm is set.  Return the data size.  Just return the size if
669    DOSIZEONLY is nonzero.  */
670
671 static int
672 write_debug_delta4 (const char *label1, const char *label2,
673                     const char *comment, int dosizeonly)
674 {
675   if (!dosizeonly)
676     {
677       ASM_OUTPUT_DEBUG_DELTA4 (asm_out_file, label1, label2);
678       if (flag_verbose_asm)
679         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
680       fputc ('\n', asm_out_file);
681     }
682
683   return 4;
684 }
685
686 /* Output a character string STRING.  Also write COMMENT if flag_verbose_asm is
687    set.  Return the string length.  Just return the length if DOSIZEONLY is
688    nonzero.  */
689
690 static int
691 write_debug_string (const char *string, const char *comment, int dosizeonly)
692 {
693   if (!dosizeonly)
694     {
695       ASM_OUTPUT_DEBUG_STRING (asm_out_file, string);
696       if (flag_verbose_asm)
697         fprintf (asm_out_file, "\t%s %s", ASM_COMMENT_START, comment);
698       fputc ('\n', asm_out_file);
699     }
700
701   return strlen (string);
702 }
703
704 /* Output a module begin header and return the header size.  Just return the
705    size if DOSIZEONLY is nonzero.  */
706
707 static int
708 write_modbeg (int dosizeonly)
709 {
710   DST_MODULE_BEGIN modbeg;
711   DST_MB_TRLR mb_trlr;
712   int i;
713   char *module_name, *m;
714   int modnamelen;
715   int prodnamelen;
716   int totsize = 0;
717
718   /* Assumes primary filename has Unix syntax file spec.  */
719   module_name = xstrdup (lbasename (primary_filename));
720
721   m = strrchr (module_name, '.');
722   if (m)
723     *m = 0;
724
725   modnamelen = strlen (module_name);
726   for (i = 0; i < modnamelen; i++)
727     module_name[i] = TOUPPER (module_name[i]);
728
729   prodnamelen = strlen (module_producer);
730
731   modbeg.dst_a_modbeg_header.dst__header_length.dst_w_length
732     = DST_K_MODBEG_SIZE + modnamelen + DST_K_MB_TRLR_SIZE + prodnamelen - 1;
733   modbeg.dst_a_modbeg_header.dst__header_type.dst_w_type = DST_K_MODBEG;
734   modbeg.dst_b_modbeg_flags.dst_v_modbeg_hide = 0;
735   modbeg.dst_b_modbeg_flags.dst_v_modbeg_version = 1;
736   modbeg.dst_b_modbeg_flags.dst_v_modbeg_unused = 0;
737   modbeg.dst_b_modbeg_unused = 0;
738   modbeg.dst_l_modbeg_language = module_language;
739   modbeg.dst_w_version_major = DST_K_VERSION_MAJOR;
740   modbeg.dst_w_version_minor = DST_K_VERSION_MINOR;
741   modbeg.dst_b_modbeg_name = strlen (module_name);
742
743   mb_trlr.dst_b_compiler = strlen (module_producer);
744
745   totsize += write_debug_header (&modbeg.dst_a_modbeg_header,
746                                  "modbeg", dosizeonly);
747   totsize += write_debug_data1 (*((char *) &modbeg.dst_b_modbeg_flags),
748                                 "flags", dosizeonly);
749   totsize += write_debug_data1 (modbeg.dst_b_modbeg_unused,
750                                 "unused", dosizeonly);
751   totsize += write_debug_data4 (modbeg.dst_l_modbeg_language,
752                                 "language", dosizeonly);
753   totsize += write_debug_data2 (modbeg.dst_w_version_major,
754                                 "DST major version", dosizeonly);
755   totsize += write_debug_data2 (modbeg.dst_w_version_minor,
756                                 "DST minor version", dosizeonly);
757   totsize += write_debug_data1 (modbeg.dst_b_modbeg_name,
758                                 "length of module name", dosizeonly);
759   totsize += write_debug_string (module_name, "module name", dosizeonly);
760   totsize += write_debug_data1 (mb_trlr.dst_b_compiler,
761                                 "length of compiler name", dosizeonly);
762   totsize += write_debug_string (module_producer, "compiler name", dosizeonly);
763
764   return totsize;
765 }
766
767 /* Output a module end trailer and return the trailer size.   Just return
768    the size if DOSIZEONLY is nonzero.  */
769
770 static int
771 write_modend (int dosizeonly)
772 {
773   DST_MODULE_END modend;
774   int totsize = 0;
775
776   modend.dst_a_modend_header.dst__header_length.dst_w_length
777    = DST_K_MODEND_SIZE - 1;
778   modend.dst_a_modend_header.dst__header_type.dst_w_type = DST_K_MODEND;
779
780   totsize += write_debug_header (&modend.dst_a_modend_header, "modend",
781                                  dosizeonly);
782
783   return totsize;
784 }
785
786 /* Output a routine begin header routine RTNNUM and return the header size.
787    Just return the size if DOSIZEONLY is nonzero.  */
788
789 static int
790 write_rtnbeg (int rtnnum, int dosizeonly)
791 {
792   const char *rtnname;
793   int rtnnamelen;
794   char *rtnentryname;
795   int totsize = 0;
796   char label[MAX_ARTIFICIAL_LABEL_BYTES];
797   DST_ROUTINE_BEGIN rtnbeg;
798   DST_PROLOG prolog;
799   vms_func_ref fde = &func_table[rtnnum];
800
801   rtnname = fde->vms_func_name;
802   rtnnamelen = strlen (rtnname);
803   rtnentryname = concat (rtnname, "..en", NULL);
804
805   if (!strcmp (rtnname, "main"))
806     {
807       DST_HEADER header;
808       const char *go = "TRANSFER$BREAK$GO";
809
810       /* This command isn't documented in DSTRECORDS, so it's made to
811          look like what DEC C does */
812
813       /* header size - 1st byte + flag byte + STO_LW size
814          + string count byte + string length */
815       header.dst__header_length.dst_w_length
816         = DST_K_DST_HEADER_SIZE - 1 + 1 + 4 + 1 + strlen (go);
817       header.dst__header_type.dst_w_type = 0x17;
818
819       totsize += write_debug_header (&header, "transfer", dosizeonly);
820
821       /* I think this is a flag byte, but I don't know what this flag means */
822       totsize += write_debug_data1 (0x1, "flags ???", dosizeonly);
823
824       /* Routine Begin PD Address */
825       totsize += write_debug_addr (rtnname, "main procedure descriptor",
826                                    dosizeonly);
827       totsize += write_debug_data1 (strlen (go), "length of main_name",
828                                     dosizeonly);
829       totsize += write_debug_string (go, "main name", dosizeonly);
830     }
831
832   /* The header length never includes the length byte.  */
833   rtnbeg.dst_a_rtnbeg_header.dst__header_length.dst_w_length
834    = DST_K_RTNBEG_SIZE + rtnnamelen - 1;
835   rtnbeg.dst_a_rtnbeg_header.dst__header_type.dst_w_type = DST_K_RTNBEG;
836   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_unused = 0;
837   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_unalloc = 0;
838   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_prototype = 0;
839   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_inlined = 0;
840   rtnbeg.dst_b_rtnbeg_flags.dst_v_rtnbeg_no_call = 1;
841   rtnbeg.dst_b_rtnbeg_name = rtnnamelen;
842
843   totsize += write_debug_header (&rtnbeg.dst_a_rtnbeg_header, "rtnbeg",
844                                  dosizeonly);
845   totsize += write_debug_data1 (*((char *) &rtnbeg.dst_b_rtnbeg_flags),
846                                 "flags", dosizeonly);
847
848   /* Routine Begin Address */
849   totsize += write_debug_addr (rtnentryname, "routine entry name", dosizeonly);
850
851   /* Routine Begin PD Address */
852   totsize += write_debug_addr (rtnname, "routine procedure descriptor",
853                                dosizeonly);
854
855   /* Routine Begin Name */
856   totsize += write_debug_data1 (rtnbeg.dst_b_rtnbeg_name,
857                                 "length of routine name", dosizeonly);
858
859   totsize += write_debug_string (rtnname, "routine name", dosizeonly);
860
861   free (rtnentryname);
862
863   if (debug_info_level > DINFO_LEVEL_TERSE)
864     {
865       prolog.dst_a_prolog_header.dst__header_length.dst_w_length
866         = DST_K_PROLOG_SIZE - 1;
867       prolog.dst_a_prolog_header.dst__header_type.dst_w_type = DST_K_PROLOG;
868
869       totsize += write_debug_header (&prolog.dst_a_prolog_header, "prolog",
870                                      dosizeonly);
871
872       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_PROLOG_LABEL, fde->funcdef_number);
873       totsize += write_debug_addr (label, "prolog breakpoint addr",
874                                    dosizeonly);
875     }
876
877   return totsize;
878 }
879
880 /* Output a routine end trailer for routine RTNNUM and return the header size.
881    Just return the size if DOSIZEONLY is nonzero.  */
882
883 static int
884 write_rtnend (int rtnnum, int dosizeonly)
885 {
886   DST_ROUTINE_END rtnend;
887   char label1[MAX_ARTIFICIAL_LABEL_BYTES];
888   char label2[MAX_ARTIFICIAL_LABEL_BYTES];
889   int totsize;
890   vms_func_ref fde = &func_table[rtnnum];
891   int corrected_rtnnum = fde->funcdef_number;
892
893   totsize = 0;
894
895   rtnend.dst_a_rtnend_header.dst__header_length.dst_w_length
896    = DST_K_RTNEND_SIZE - 1;
897   rtnend.dst_a_rtnend_header.dst__header_type.dst_w_type = DST_K_RTNEND;
898   rtnend.dst_b_rtnend_unused = 0;
899   rtnend.dst_l_rtnend_size = 0; /* Calculated below.  */
900
901   totsize += write_debug_header (&rtnend.dst_a_rtnend_header, "rtnend",
902                                  dosizeonly);
903   totsize += write_debug_data1 (rtnend.dst_b_rtnend_unused, "unused",
904                                 dosizeonly);
905
906   ASM_GENERATE_INTERNAL_LABEL (label1, FUNC_BEGIN_LABEL, corrected_rtnnum);
907   ASM_GENERATE_INTERNAL_LABEL (label2, FUNC_END_LABEL, corrected_rtnnum);
908   totsize += write_debug_delta4 (label2, label1, "routine size", dosizeonly);
909
910   return totsize;
911 }
912
913 #define K_DELTA_PC(I) \
914  ((I) < 128 ? -(I) : (I) < 65536 ? DST_K_DELTA_PC_W : DST_K_DELTA_PC_L)
915
916 #define K_SET_LINUM(I) \
917  ((I) < 256 ? DST_K_SET_LINUM_B \
918   : (I) < 65536 ? DST_K_SET_LINUM : DST_K_SET_LINUM_L)
919
920 #define K_INCR_LINUM(I) \
921  ((I) < 256 ? DST_K_INCR_LINUM \
922   : (I) < 65536 ? DST_K_INCR_LINUM_W : DST_K_INCR_LINUM_L)
923
924 /* Output the PC to line number correlations and return the size.  Just return
925    the size if DOSIZEONLY is nonzero */
926
927 static int
928 write_pclines (int dosizeonly)
929 {
930   unsigned i;
931   int fn;
932   int ln, lastln;
933   int linestart = 0;
934   int max_line;
935   DST_LINE_NUM_HEADER line_num;
936   DST_PCLINE_COMMANDS pcline;
937   char label[MAX_ARTIFICIAL_LABEL_BYTES];
938   char lastlabel[MAX_ARTIFICIAL_LABEL_BYTES];
939   int totsize = 0;
940   char buff[256];
941
942   max_line = file_info_table[1].max_line;
943   file_info_table[1].listing_line_start = linestart;
944   linestart = linestart + ((max_line / 100000) + 1) * 100000;
945
946   for (i = 2; i < file_info_table_in_use; i++)
947     {
948       max_line = file_info_table[i].max_line;
949       file_info_table[i].listing_line_start = linestart;
950       linestart = linestart + ((max_line / 10000) + 1) * 10000;
951     }
952
953   /* Set starting address to beginning of text section.  */
954   line_num.dst_a_line_num_header.dst__header_length.dst_w_length = 8;
955   line_num.dst_a_line_num_header.dst__header_type.dst_w_type = DST_K_LINE_NUM;
956   pcline.dst_b_pcline_command = DST_K_SET_ABS_PC;
957
958   totsize += write_debug_header (&line_num.dst_a_line_num_header,
959                                  "line_num", dosizeonly);
960   totsize += write_debug_data1 (pcline.dst_b_pcline_command,
961                                 "line_num (SET ABS PC)", dosizeonly);
962
963   if (dosizeonly)
964     totsize += 4;
965   else
966     {
967       ASM_OUTPUT_DEBUG_ADDR (asm_out_file, TEXT_SECTION_ASM_OP);
968       if (flag_verbose_asm)
969         fprintf (asm_out_file, "\t%s line_num", ASM_COMMENT_START);
970       fputc ('\n', asm_out_file);
971     }
972
973   fn = line_info_table[1].dst_file_num;
974   ln = (file_info_table[fn].listing_line_start
975         + line_info_table[1].dst_line_num);
976   line_num.dst_a_line_num_header.dst__header_length.dst_w_length = 4 + 4;
977   pcline.dst_b_pcline_command = DST_K_SET_LINUM_L;
978
979   totsize += write_debug_header (&line_num.dst_a_line_num_header,
980                                  "line_num", dosizeonly);
981   totsize += write_debug_data1 (pcline.dst_b_pcline_command,
982                                 "line_num (SET LINUM LONG)", dosizeonly);
983
984   sprintf (buff, "line_num (%d)", ln ? ln - 1 : 0);
985   totsize += write_debug_data4 (ln ? ln - 1 : 0, buff, dosizeonly);
986
987   lastln = ln;
988   strcpy (lastlabel, TEXT_SECTION_ASM_OP);
989   for (i = 1; i < line_info_table_in_use; i++)
990     {
991       int extrabytes;
992
993       fn = line_info_table[i].dst_file_num;
994       ln = (file_info_table[fn].listing_line_start
995             + line_info_table[i].dst_line_num);
996
997       if (ln - lastln > 1)
998         extrabytes = 5; /* NUMBYTES (ln - lastln - 1) + 1; */
999       else if (ln <= lastln)
1000         extrabytes = 5; /* NUMBYTES (ln - 1) + 1; */
1001       else
1002         extrabytes = 0;
1003
1004       line_num.dst_a_line_num_header.dst__header_length.dst_w_length
1005         = 8 + extrabytes;
1006
1007       totsize += write_debug_header
1008         (&line_num.dst_a_line_num_header, "line_num", dosizeonly);
1009
1010       if (ln - lastln > 1)
1011         {
1012           int lndif = ln - lastln - 1;
1013
1014           /* K_INCR_LINUM (lndif); */
1015           pcline.dst_b_pcline_command = DST_K_INCR_LINUM_L;
1016
1017           totsize += write_debug_data1 (pcline.dst_b_pcline_command,
1018                                         "line_num (INCR LINUM LONG)",
1019                                         dosizeonly);
1020
1021           sprintf (buff, "line_num (%d)", lndif);
1022           totsize += write_debug_data4 (lndif, buff, dosizeonly);
1023         }
1024       else if (ln <= lastln)
1025         {
1026           /* K_SET_LINUM (ln-1); */
1027           pcline.dst_b_pcline_command = DST_K_SET_LINUM_L;
1028
1029           totsize += write_debug_data1 (pcline.dst_b_pcline_command,
1030                                         "line_num (SET LINUM LONG)",
1031                                         dosizeonly);
1032
1033           sprintf (buff, "line_num (%d)", ln - 1);
1034           totsize += write_debug_data4 (ln - 1, buff, dosizeonly);
1035         }
1036
1037       pcline.dst_b_pcline_command = DST_K_DELTA_PC_L;
1038
1039       totsize += write_debug_data1 (pcline.dst_b_pcline_command,
1040                                     "line_num (DELTA PC LONG)", dosizeonly);
1041
1042       ASM_GENERATE_INTERNAL_LABEL (label, LINE_CODE_LABEL, i);
1043       totsize += write_debug_delta4 (label, lastlabel, "increment line_num",
1044                                      dosizeonly);
1045
1046       lastln = ln;
1047       strcpy (lastlabel, label);
1048     }
1049
1050   return totsize;
1051 }
1052
1053 /* Output a source correlation for file FILEID using information saved in
1054    FILE_INFO_ENTRY and return the size.  Just return the size if DOSIZEONLY is
1055    nonzero.  */
1056
1057 static int
1058 write_srccorr (int fileid, dst_file_info_entry file_info_entry,
1059                int dosizeonly)
1060 {
1061   int src_command_size;
1062   int linesleft = file_info_entry.max_line;
1063   int linestart = file_info_entry.listing_line_start;
1064   int flen = file_info_entry.flen;
1065   int linestodo = 0;
1066   DST_SOURCE_CORR src_header;
1067   DST_SRC_COMMAND src_command;
1068   DST_SRC_COMMAND src_command_sf;
1069   DST_SRC_COMMAND src_command_sl;
1070   DST_SRC_COMMAND src_command_sr;
1071   DST_SRC_COMMAND src_command_dl;
1072   DST_SRC_CMDTRLR src_cmdtrlr;
1073   char buff[256];
1074   int totsize = 0;
1075
1076   if (fileid == 1)
1077     {
1078       src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
1079         = DST_K_SOURCE_CORR_HEADER_SIZE + 1 - 1;
1080       src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
1081         = DST_K_SOURCE;
1082       src_command.dst_b_src_command = DST_K_SRC_FORMFEED;
1083
1084       totsize += write_debug_header (&src_header.dst_a_source_corr_header,
1085                                      "source corr", dosizeonly);
1086
1087       totsize += write_debug_data1 (src_command.dst_b_src_command,
1088                                     "source_corr (SRC FORMFEED)",
1089                                     dosizeonly);
1090     }
1091
1092   src_command_size
1093     = DST_K_SRC_COMMAND_SIZE + flen + DST_K_SRC_CMDTRLR_SIZE;
1094   src_command.dst_b_src_command = DST_K_SRC_DECLFILE;
1095   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_length
1096     = src_command_size - 2;
1097   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_flags = 0;
1098   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_fileid
1099     = fileid;
1100   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_q_src_df_rms_cdt
1101     = file_info_entry.cdt;
1102   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_l_src_df_rms_ebk
1103     = file_info_entry.ebk;
1104   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_rms_ffb
1105     = file_info_entry.ffb;
1106   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_rms_rfo
1107     = file_info_entry.rfo;
1108   src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_filename
1109     = file_info_entry.flen;
1110
1111   src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
1112     = DST_K_SOURCE_CORR_HEADER_SIZE + src_command_size - 1;
1113   src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
1114     = DST_K_SOURCE;
1115
1116   src_cmdtrlr.dst_b_src_df_libmodname = 0;
1117
1118   totsize += write_debug_header (&src_header.dst_a_source_corr_header,
1119                                  "source corr", dosizeonly);
1120   totsize += write_debug_data1 (src_command.dst_b_src_command,
1121                                 "source_corr (DECL SRC FILE)", dosizeonly);
1122   totsize += write_debug_data1
1123     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_length,
1124      "source_corr (length)", dosizeonly);
1125
1126   totsize += write_debug_data1
1127     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_flags,
1128      "source_corr (flags)", dosizeonly);
1129
1130   totsize += write_debug_data2
1131     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_fileid,
1132      "source_corr (fileid)", dosizeonly);
1133
1134   totsize += write_debug_data8
1135     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_q_src_df_rms_cdt,
1136      "source_corr (creation date)", dosizeonly);
1137
1138   totsize += write_debug_data4
1139     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_l_src_df_rms_ebk,
1140      "source_corr (EOF block number)", dosizeonly);
1141
1142   totsize += write_debug_data2
1143     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_w_src_df_rms_ffb,
1144      "source_corr (first free byte)", dosizeonly);
1145
1146   totsize += write_debug_data1
1147     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_rms_rfo,
1148      "source_corr (record and file organization)", dosizeonly);
1149
1150   totsize += write_debug_data1
1151     (src_command.dst_a_src_cmd_fields.dst_a_src_decl_src.dst_b_src_df_filename,
1152      "source_corr (filename length)", dosizeonly);
1153
1154   totsize += write_debug_string (remap_debug_filename (
1155                                     file_info_entry.file_name),
1156                                  "source file name", dosizeonly);
1157   totsize += write_debug_data1 (src_cmdtrlr.dst_b_src_df_libmodname,
1158                                 "source_corr (libmodname)", dosizeonly);
1159
1160   src_command_sf.dst_b_src_command = DST_K_SRC_SETFILE;
1161   src_command_sf.dst_a_src_cmd_fields.dst_w_src_unsword = fileid;
1162
1163   src_command_sr.dst_b_src_command = DST_K_SRC_SETREC_W;
1164   src_command_sr.dst_a_src_cmd_fields.dst_w_src_unsword = 1;
1165
1166   src_command_sl.dst_b_src_command = DST_K_SRC_SETLNUM_L;
1167   src_command_sl.dst_a_src_cmd_fields.dst_l_src_unslong = linestart + 1;
1168
1169   src_command_dl.dst_b_src_command = DST_K_SRC_DEFLINES_W;
1170
1171   if (linesleft > 65534)
1172     linesleft = linesleft - 65534, linestodo = 65534;
1173   else
1174     linestodo = linesleft, linesleft = 0;
1175
1176   src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword = linestodo;
1177
1178   src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
1179     = DST_K_SOURCE_CORR_HEADER_SIZE + 3 + 3 + 5 + 3 - 1;
1180   src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
1181     = DST_K_SOURCE;
1182
1183   if (src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword)
1184     {
1185       totsize += write_debug_header (&src_header.dst_a_source_corr_header,
1186                                      "source corr", dosizeonly);
1187
1188       totsize += write_debug_data1 (src_command_sf.dst_b_src_command,
1189                                     "source_corr (src setfile)", dosizeonly);
1190
1191       totsize += write_debug_data2
1192         (src_command_sf.dst_a_src_cmd_fields.dst_w_src_unsword,
1193          "source_corr (fileid)", dosizeonly);
1194
1195       totsize += write_debug_data1 (src_command_sr.dst_b_src_command,
1196                                     "source_corr (setrec)", dosizeonly);
1197
1198       totsize += write_debug_data2
1199         (src_command_sr.dst_a_src_cmd_fields.dst_w_src_unsword,
1200          "source_corr (recnum)", dosizeonly);
1201
1202       totsize += write_debug_data1 (src_command_sl.dst_b_src_command,
1203                                     "source_corr (setlnum)", dosizeonly);
1204
1205       totsize += write_debug_data4
1206         (src_command_sl.dst_a_src_cmd_fields.dst_l_src_unslong,
1207          "source_corr (linenum)", dosizeonly);
1208
1209       totsize += write_debug_data1 (src_command_dl.dst_b_src_command,
1210                                     "source_corr (deflines)", dosizeonly);
1211
1212       sprintf (buff, "source_corr (%d)",
1213                src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword);
1214       totsize += write_debug_data2
1215         (src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword,
1216          buff, dosizeonly);
1217
1218       while (linesleft > 0)
1219         {
1220           src_header.dst_a_source_corr_header.dst__header_length.dst_w_length
1221             = DST_K_SOURCE_CORR_HEADER_SIZE + 3 - 1;
1222           src_header.dst_a_source_corr_header.dst__header_type.dst_w_type
1223             = DST_K_SOURCE;
1224           src_command_dl.dst_b_src_command = DST_K_SRC_DEFLINES_W;
1225
1226           if (linesleft > 65534)
1227             linesleft = linesleft - 65534, linestodo = 65534;
1228           else
1229             linestodo = linesleft, linesleft = 0;
1230
1231           src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword = linestodo;
1232
1233           totsize += write_debug_header (&src_header.dst_a_source_corr_header,
1234                                          "source corr", dosizeonly);
1235           totsize += write_debug_data1 (src_command_dl.dst_b_src_command,
1236                                         "source_corr (deflines)", dosizeonly);
1237           sprintf (buff, "source_corr (%d)",
1238                    src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword);
1239           totsize += write_debug_data2
1240             (src_command_dl.dst_a_src_cmd_fields.dst_w_src_unsword,
1241              buff, dosizeonly);
1242         }
1243     }
1244
1245   return totsize;
1246 }
1247
1248 /* Output all the source correlation entries and return the size.  Just return
1249    the size if DOSIZEONLY is nonzero.  */
1250
1251 static int
1252 write_srccorrs (int dosizeonly)
1253 {
1254   unsigned int i;
1255   int totsize = 0;
1256
1257   for (i = 1; i < file_info_table_in_use; i++)
1258     totsize += write_srccorr (i, file_info_table[i], dosizeonly);
1259
1260   return totsize;
1261 }
1262 \f
1263 /* Output a marker (i.e. a label) for the beginning of a function, before
1264    the prologue.  */
1265
1266 static void
1267 vmsdbgout_begin_prologue (unsigned int line, const char *file)
1268 {
1269   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1270
1271   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1272     (*dwarf2_debug_hooks.begin_prologue) (line, file);
1273
1274   if (debug_info_level > DINFO_LEVEL_NONE)
1275     {
1276       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1277                                    current_function_funcdef_no);
1278       ASM_OUTPUT_LABEL (asm_out_file, label);
1279     }
1280 }
1281
1282 /* Output a marker (i.e. a label) for the beginning of a function, after
1283    the prologue.  */
1284
1285 static void
1286 vmsdbgout_end_prologue (unsigned int line, const char *file)
1287 {
1288   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1289
1290   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1291     (*dwarf2_debug_hooks.end_prologue) (line, file);
1292
1293   if (debug_info_level > DINFO_LEVEL_TERSE)
1294     {
1295       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_PROLOG_LABEL,
1296                                    current_function_funcdef_no);
1297       ASM_OUTPUT_LABEL (asm_out_file, label);
1298
1299       /* VMS PCA expects every PC range to correlate to some line and file.  */
1300       vmsdbgout_source_line (line, file);
1301     }
1302 }
1303
1304 /* No output for VMS debug, but make obligatory call to Dwarf2 debug */
1305
1306 static void
1307 vmsdbgout_end_function (unsigned int line)
1308 {
1309   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1310     (*dwarf2_debug_hooks.end_function) (line);
1311 }
1312
1313 /* Output a marker (i.e. a label) for the absolute end of the generated code
1314    for a function definition.  This gets called *after* the epilogue code has
1315    been generated.  */
1316
1317 static void
1318 vmsdbgout_end_epilogue (unsigned int line, const char *file)
1319 {
1320   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1321
1322   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1323     (*dwarf2_debug_hooks.end_epilogue) (line, file);
1324
1325   if (debug_info_level > DINFO_LEVEL_NONE)
1326     {
1327       /* Output a label to mark the endpoint of the code generated for this
1328          function.  */
1329       ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1330                                    current_function_funcdef_no);
1331       ASM_OUTPUT_LABEL (asm_out_file, label);
1332
1333       /* VMS PCA expects every PC range to correlate to some line and file.  */
1334       vmsdbgout_source_line (line, file);
1335     }
1336 }
1337
1338 /* Output a marker (i.e. a label) for the beginning of the generated code for
1339    a lexical block.  */
1340
1341 static void
1342 vmsdbgout_begin_block (register unsigned line, register unsigned blocknum)
1343 {
1344   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1345     (*dwarf2_debug_hooks.begin_block) (line, blocknum);
1346
1347   if (debug_info_level > DINFO_LEVEL_TERSE)
1348     targetm.asm_out.internal_label (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
1349 }
1350
1351 /* Output a marker (i.e. a label) for the end of the generated code for a
1352    lexical block.  */
1353
1354 static void
1355 vmsdbgout_end_block (register unsigned line, register unsigned blocknum)
1356 {
1357   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1358     (*dwarf2_debug_hooks.end_block) (line, blocknum);
1359
1360   if (debug_info_level > DINFO_LEVEL_TERSE)
1361     targetm.asm_out.internal_label (asm_out_file, BLOCK_END_LABEL, blocknum);
1362 }
1363
1364 /* Not implemented in VMS Debug.  */
1365
1366 static bool
1367 vmsdbgout_ignore_block (const_tree block)
1368 {
1369   bool retval = 0;
1370
1371   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1372     retval = (*dwarf2_debug_hooks.ignore_block) (block);
1373
1374   return retval;
1375 }
1376
1377 /* Add an entry for function DECL into the func_table.  */
1378
1379 static void
1380 vmsdbgout_begin_function (tree decl)
1381 {
1382   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1383   vms_func_ref fde;
1384
1385   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1386     (*dwarf2_debug_hooks.begin_function) (decl);
1387
1388   if (func_table_in_use == func_table_allocated)
1389     {
1390       func_table_allocated += FUNC_TABLE_INCREMENT;
1391       func_table
1392         = (vms_func_ref) xrealloc (func_table,
1393                                    func_table_allocated * sizeof (vms_func_node));
1394     }
1395
1396   /* Add the new entry to the end of the function name table.  */
1397   fde = &func_table[func_table_in_use++];
1398   fde->vms_func_name = xstrdup (name);
1399   fde->funcdef_number = current_function_funcdef_no;
1400
1401 }
1402
1403 static char fullname_buff [4096];
1404
1405 /* Return the full file specification for FILENAME.  The specification must be
1406    in VMS syntax in order to be processed by VMS Debug.  */
1407
1408 static char *
1409 full_name (const char *filename)
1410 {
1411 #ifdef VMS
1412   FILE *fp = fopen (filename, "r");
1413
1414   fgetname (fp, fullname_buff, 1);
1415   fclose (fp);
1416 #else
1417   getcwd (fullname_buff, sizeof (fullname_buff));
1418
1419   strcat (fullname_buff, "/");
1420   strcat (fullname_buff, filename);
1421
1422   /* ??? Insert hairy code here to translate Unix style file specification
1423      to VMS style.  */
1424 #endif
1425
1426   return fullname_buff;
1427 }
1428
1429 /* Lookup a filename (in the list of filenames that we know about here in
1430    vmsdbgout.c) and return its "index".  The index of each (known) filename is
1431    just a unique number which is associated with only that one filename.  We
1432    need such numbers for the sake of generating labels  and references
1433    to those files numbers.  If the filename given as an argument is not
1434    found in our current list, add it to the list and assign it the next
1435    available unique index number.  In order to speed up searches, we remember
1436    the index of the filename was looked up last.  This handles the majority of
1437    all searches.  */
1438
1439 static unsigned int
1440 lookup_filename (const char *file_name)
1441 {
1442   static unsigned int last_file_lookup_index = 0;
1443   register char *fn;
1444   register unsigned i;
1445   const char *fnam;
1446   long long cdt;
1447   long ebk;
1448   short ffb;
1449   char rfo;
1450   char flen;
1451   struct stat statbuf;
1452
1453   if (stat (file_name, &statbuf) == 0)
1454     {
1455       long gmtoff;
1456 #ifdef VMS
1457       struct tm *ts;
1458
1459       /* Adjust for GMT.  */
1460       ts = (struct tm *) localtime (&statbuf.st_ctime);
1461       gmtoff = ts->tm_gmtoff;
1462
1463       /* VMS has multiple file format types.  */
1464       rfo = statbuf.st_fab_rfm;
1465 #else
1466       /* Is GMT adjustment an issue with a cross-compiler? */
1467       gmtoff = 0;
1468
1469       /* Assume stream LF type file.  */
1470       rfo = 2;
1471 #endif
1472       cdt = 10000000 * (statbuf.st_ctime + gmtoff + vms_epoch_offset);
1473       ebk = statbuf.st_size / 512 + 1;
1474       ffb = statbuf.st_size - ((statbuf.st_size / 512) * 512);
1475       fnam = full_name (file_name);
1476       flen = strlen (fnam);
1477     }
1478   else
1479     {
1480       cdt = 0;
1481       ebk = 0;
1482       ffb = 0;
1483       rfo = 0;
1484       fnam = "";
1485       flen = 0;
1486     }
1487
1488   /* Check to see if the file name that was searched on the previous call
1489      matches this file name. If so, return the index.  */
1490   if (last_file_lookup_index != 0)
1491     {
1492       fn = file_info_table[last_file_lookup_index].file_name;
1493       if (strcmp (fnam, fn) == 0)
1494         return last_file_lookup_index;
1495     }
1496
1497   /* Didn't match the previous lookup, search the table */
1498   for (i = 1; i < file_info_table_in_use; ++i)
1499     {
1500       fn = file_info_table[i].file_name;
1501       if (strcmp (fnam, fn) == 0)
1502         {
1503           last_file_lookup_index = i;
1504           return i;
1505         }
1506     }
1507
1508   /* Prepare to add a new table entry by making sure there is enough space in
1509      the table to do so.  If not, expand the current table.  */
1510   if (file_info_table_in_use == file_info_table_allocated)
1511     {
1512
1513       file_info_table_allocated += FILE_TABLE_INCREMENT;
1514       file_info_table = XRESIZEVEC (dst_file_info_entry, file_info_table,
1515                                     file_info_table_allocated);
1516     }
1517
1518   /* Add the new entry to the end of the filename table.  */
1519   file_info_table[file_info_table_in_use].file_name = xstrdup (fnam);
1520   file_info_table[file_info_table_in_use].max_line = 0;
1521   file_info_table[file_info_table_in_use].cdt = cdt;
1522   file_info_table[file_info_table_in_use].ebk = ebk;
1523   file_info_table[file_info_table_in_use].ffb = ffb;
1524   file_info_table[file_info_table_in_use].rfo = rfo;
1525   file_info_table[file_info_table_in_use].flen = flen;
1526
1527   last_file_lookup_index = file_info_table_in_use++;
1528   return last_file_lookup_index;
1529 }
1530
1531 /* Output a label to mark the beginning of a source code line entry
1532    and record information relating to this source line, in
1533    'line_info_table' for later output of the .debug_line section.  */
1534
1535 static void
1536 vmsdbgout_source_line (register unsigned line, register const char *filename)
1537 {
1538   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1539     (*dwarf2_debug_hooks.source_line) (line, filename);
1540
1541   if (debug_info_level >= DINFO_LEVEL_TERSE)
1542     {
1543       dst_line_info_ref line_info;
1544
1545       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
1546                                       line_info_table_in_use);
1547
1548       /* Expand the line info table if necessary.  */
1549       if (line_info_table_in_use == line_info_table_allocated)
1550         {
1551           line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
1552           line_info_table = XRESIZEVEC (dst_line_info_entry, line_info_table,
1553                                         line_info_table_allocated);
1554         }
1555
1556       /* Add the new entry at the end of the line_info_table.  */
1557       line_info = &line_info_table[line_info_table_in_use++];
1558       line_info->dst_file_num = lookup_filename (filename);
1559       line_info->dst_line_num = line;
1560       if (line > file_info_table[line_info->dst_file_num].max_line)
1561         file_info_table[line_info->dst_file_num].max_line = line;
1562     }
1563 }
1564
1565 /* Record the beginning of a new source file, for later output.
1566    At present, unimplemented.  */
1567
1568 static void
1569 vmsdbgout_start_source_file (unsigned int lineno, const char *filename)
1570 {
1571   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1572     (*dwarf2_debug_hooks.start_source_file) (lineno, filename);
1573 }
1574
1575 /* Record the end of a source file, for later output.
1576    At present, unimplemented.  */
1577
1578 static void
1579 vmsdbgout_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
1580 {
1581   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1582     (*dwarf2_debug_hooks.end_source_file) (lineno);
1583 }
1584
1585 /* Set up for Debug output at the start of compilation.  */
1586
1587 static void
1588 vmsdbgout_init (const char *main_input_filename)
1589 {
1590   const char *language_string = lang_hooks.name;
1591
1592   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1593     (*dwarf2_debug_hooks.init) (main_input_filename);
1594
1595   if (debug_info_level == DINFO_LEVEL_NONE)
1596     return;
1597
1598   /* Remember the name of the primary input file.  */
1599   primary_filename = main_input_filename;
1600
1601   /* Allocate the initial hunk of the file_info_table.  */
1602   file_info_table = XCNEWVEC (dst_file_info_entry, FILE_TABLE_INCREMENT);
1603   file_info_table_allocated = FILE_TABLE_INCREMENT;
1604
1605   /* Skip the first entry - file numbers begin at 1 */
1606   file_info_table_in_use = 1;
1607
1608   func_table = (vms_func_ref) xcalloc (FUNC_TABLE_INCREMENT, sizeof (vms_func_node));
1609   func_table_allocated = FUNC_TABLE_INCREMENT;
1610   func_table_in_use = 1;
1611
1612   /* Allocate the initial hunk of the line_info_table.  */
1613   line_info_table = XCNEWVEC (dst_line_info_entry, LINE_INFO_TABLE_INCREMENT);
1614   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
1615   /* zero-th entry is allocated, but unused */
1616   line_info_table_in_use = 1;
1617
1618   lookup_filename (primary_filename);
1619
1620   if (!strcmp (language_string, "GNU C"))
1621     module_language = DST_K_C;
1622   else if (!strcmp (language_string, "GNU C++"))
1623     module_language = DST_K_CXX;
1624   else if (!strcmp (language_string, "GNU Ada"))
1625     module_language = DST_K_ADA;
1626   else if (!strcmp (language_string, "GNU F77"))
1627     module_language = DST_K_FORTRAN;
1628   else
1629     module_language = DST_K_UNKNOWN;
1630
1631   module_producer = concat (language_string, " ", version_string, NULL);
1632
1633   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
1634
1635 }
1636
1637 /* Not implemented in VMS Debug.  */
1638
1639 static void
1640 vmsdbgout_define (unsigned int lineno, const char *buffer)
1641 {
1642   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1643     (*dwarf2_debug_hooks.define) (lineno, buffer);
1644 }
1645
1646 /* Not implemented in VMS Debug.  */
1647
1648 static void
1649 vmsdbgout_undef (unsigned int lineno, const char *buffer)
1650 {
1651   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1652     (*dwarf2_debug_hooks.undef) (lineno, buffer);
1653 }
1654
1655 /* Not implemented in VMS Debug.  */
1656
1657 static void
1658 vmsdbgout_decl (tree decl)
1659 {
1660   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1661     (*dwarf2_debug_hooks.function_decl) (decl);
1662 }
1663
1664 /* Not implemented in VMS Debug.  */
1665
1666 static void
1667 vmsdbgout_global_decl (tree decl)
1668 {
1669   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1670     (*dwarf2_debug_hooks.global_decl) (decl);
1671 }
1672
1673 /* Not implemented in VMS Debug.  */
1674
1675 static void
1676 vmsdbgout_abstract_function (tree decl)
1677 {
1678   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1679     (*dwarf2_debug_hooks.outlining_inline_function) (decl);
1680 }
1681
1682 /* Output stuff that Debug requires at the end of every file and generate the
1683    VMS Debug debugging info.  */
1684
1685 static void
1686 vmsdbgout_finish (const char *main_input_filename ATTRIBUTE_UNUSED)
1687 {
1688   unsigned int i;
1689   int totsize;
1690
1691   if (write_symbols == VMS_AND_DWARF2_DEBUG)
1692     (*dwarf2_debug_hooks.finish) (main_input_filename);
1693
1694   if (debug_info_level == DINFO_LEVEL_NONE)
1695     return;
1696
1697   /* Output a terminator label for the .text section.  */
1698   switch_to_section (text_section);
1699   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
1700
1701   /* Output debugging information.
1702      Warning! Do not change the name of the .vmsdebug section without
1703      changing it in the assembler also.  */
1704   switch_to_section (get_named_section (NULL, ".vmsdebug", 0));
1705   ASM_OUTPUT_ALIGN (asm_out_file, 0);
1706
1707   totsize = write_modbeg (1);
1708   for (i = 1; i < func_table_in_use; i++)
1709     {
1710       totsize += write_rtnbeg (i, 1);
1711       totsize += write_rtnend (i, 1);
1712     }
1713   totsize += write_pclines (1);
1714
1715   write_modbeg (0);
1716   for (i = 1; i < func_table_in_use; i++)
1717     {
1718       write_rtnbeg (i, 0);
1719       write_rtnend (i, 0);
1720     }
1721   write_pclines (0);
1722
1723   if (debug_info_level > DINFO_LEVEL_TERSE)
1724     {
1725       totsize = write_srccorrs (1);
1726       write_srccorrs (0);
1727     }
1728
1729   totsize = write_modend (1);
1730   write_modend (0);
1731 }
1732 #endif /* VMS_DEBUGGING_INFO */