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