1/* Output variables, constants and external declarations, for GNU compiler.
2   Copyright (C) 1988, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING.  If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.  */
20
21#define VMS_TARGET
22
23/* This enables certain macros in vax.h, which will make an indirect
24   reference to an external symbol an invalid address.  This needs to be
25   defined before we include vax.h, since it determines which macros
26   are used for GO_IF_*.  */
27
28#define NO_EXTERNAL_INDIRECT_ADDRESS
29
30#include "vax/vax.h"
31
32#undef LIB_SPEC
33#undef CPP_PREDEFINES
34#undef TARGET_NAME
35#undef TARGET_DEFAULT
36#undef CALL_USED_REGISTERS
37#undef MAYBE_VMS_FUNCTION_PROLOGUE
38#undef STARTING_FRAME_OFFSET
39
40/* Predefine this in CPP because VMS limits the size of command options
41   and GNU CPP is not used on VMS except with GNU C.  */
42#define CPP_PREDEFINES \
43"-Dvax -Dvms -DVMS -D__vax__ -D__vms__ -D__VMS__\
44 -D__GNUC__=2 -D__GNUC_MINOR__=7 -Asystem(vms) -Acpu(vax) -Amachine(vax)"
45
46/* These match the definitions used in VAXCRTL, the VMS C run-time library */
47
48#define SIZE_TYPE	"unsigned int"
49#define PTRDIFF_TYPE	"int"
50#define WCHAR_TYPE	"unsigned int"
51#define WCHAR_TYPE_SIZE	32	/* in bits */
52
53/* Use memcpy for structure copying, and so forth.  */
54#define TARGET_MEM_FUNCTIONS
55
56/* Strictly speaking, VMS does not use DBX at all, but the interpreter built
57   into gas only speaks straight DBX.  */
58
59#define DEFAULT_GDB_EXTENSIONS 0
60
61#define TARGET_DEFAULT 1
62#define TARGET_NAME "vax/vms"
63
64/* The structure return address arrives as an "argument" on VMS.  */
65#undef STRUCT_VALUE_REGNUM
66#define STRUCT_VALUE 0
67#undef PCC_STATIC_STRUCT_RETURN
68
69#define CALL_USED_REGISTERS {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1}
70
71/* The run-time library routine VAXC$ESTABLISH (necessary when mixing
72   VMS exception handling and setjmp/longjmp in the same program) requires
73   that a hidden automatic variable at the top of the stack be reserved
74   for its use.  We accomplish this by simply adding 4 bytes to the local
75   stack for all functions, and making sure that normal local variables
76   are 4 bytes lower on the stack then they would otherwise have been.  */
77
78#define STARTING_FRAME_OFFSET -4
79
80#define __MAIN_NAME " main("
81/*
82 * The MAYBE_VMS_FUNCTION_PROLOGUE macro works for both gcc and g++.  It
83 * first checks to see if the current routine is "main", which will only
84 * happen for GCC, and add the jsb if it is.  If is not the case then try and
85 * see if __MAIN_NAME is part of current_function_name, which will only happen
86 * if we are running g++, and add the jsb if it is.  In gcc there should never
87 * be a paren in the function name, and in g++ there is always a "(" in the
88 * function name, thus there should never be any confusion.
89 *
90 * Adjusting the stack pointer by 4 before calling C$MAIN_ARGS is required
91 * when linking with the VMS POSIX version of the C run-time library; using
92 * `subl2 $4,r0' is adequate but we use `clrl -(sp)' instead.  The extra 4
93 * bytes could be removed after the call because STARTING_FRAME_OFFSET's
94 * setting of -4 will end up adding them right back again, but don't bother.
95 */
96#define MAYBE_VMS_FUNCTION_PROLOGUE(FILE)	\
97{ extern char *current_function_name;		\
98  char *p = current_function_name;		\
99  int is_main = strcmp ("main", p) == 0;	\
100  while (!is_main && *p != '\0')		\
101    {						\
102      if (*p == *__MAIN_NAME			\
103	  && strncmp (p, __MAIN_NAME, sizeof __MAIN_NAME - sizeof "") == 0) \
104	is_main = 1;				\
105      else					\
106	p++;					\
107    }						\
108  if (is_main)					\
109    fprintf (FILE, "\t%s\n\t%s\n", "clrl -(sp)", "jsb _C$MAIN_ARGS");	\
110}
111
112/* This macro definition sets up a default value for `main' to return.  */
113#define DEFAULT_MAIN_RETURN  c_expand_return (integer_one_node)
114
115/* This makes use of a hook in varasm.c to mark all external variables
116   for us.  We use this to make sure that external variables are correctly
117   addressed.  Under VMS there is some brain damage in the linker that requires
118   us to do this.  */
119
120#define ENCODE_SECTION_INFO(decl)  				\
121  if (DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)) 		\
122    SYMBOL_REF_FLAG (XEXP (DECL_RTL (decl), 0)) = 1;
123
124/* This is how to output a command to make the user-level label named NAME
125   defined for reference from other files.  */
126
127#undef ASM_GLOBALIZE_LABEL
128#define ASM_GLOBALIZE_LABEL(FILE,NAME)		\
129  do { fputs (".globl ", FILE);			\
130       assemble_name (FILE, NAME);		\
131       fputs ("\n", FILE);			\
132       vms_check_external (NULL_TREE, NAME, 0); \
133     } while (0)
134
135/* Under VMS we write the actual size of the storage to be allocated even
136   though the symbol is external.  Although it is possible to give external
137   symbols a size of 0 (as unix does), the VMS linker does not make the
138   distinction between a variable definition and an external reference of a
139   variable, and thus the linker will not complain about a missing definition.
140   If we followed the unix example of giving external symbols a size of
141   zero, you tried to link a program where a given variable was externally
142   defined but none of the object modules contained a non-extern definition,
143   the linker would allocate 0 bytes for the variable, and any attempt to
144   use that variable would use the storage allocated to some other variable.
145
146   We must also select either const_section or data_section: this will indicate
147   whether or not the variable will get the readonly bit set.  Since the
148   VMS linker does not distinguish between a variable's definition and an
149   external reference, all usages of a given variable must have the readonly
150   bit set the same way, or the linker will get confused and give warning
151   messages.  */
152
153/* We used to round the size up to a multiple of 4,
154   but that causes linker errors sometimes when the variable was initialized
155   since the size of its definition was not likewise rounded up.  */
156
157/* Note:  the original ASM_OUTPUT_EXTERNAL code has been moved into
158   vms_check_external and vms_flush_pending_externals.  */
159
160#define ASM_OUTPUT_EXTERNAL(FILE,DECL,NAME)				\
161{ if (DECL_INITIAL (DECL) == 0 && TREE_CODE (DECL) != FUNCTION_DECL)	\
162    vms_check_external ((DECL), (NAME), 1);				\
163}
164
165/* ASM_OUTPUT_EXTERNAL will have wait until after an initializer is
166   completed in order to switch sections for an external object, so
167   use the DECLARE_OBJECT hooks to manage deferred declarations.  */
168
169/* This is the default action for ASM_DECLARE_OBJECT_NAME, but if it
170   is explicitly defined, then ASM_FINISH_DECLARE_OBJECT will be used.  */
171
172#define ASM_DECLARE_OBJECT_NAME(ASM_OUT_FILE,NAME,DECL)		\
173  ASM_OUTPUT_LABEL ((ASM_OUT_FILE), (NAME))
174
175/* We don't need to do anything special to finish the current object, but it
176   should now be safe to output any deferred external global declarations.  */
177
178#define ASM_FINISH_DECLARE_OBJECT(FILE,DECL,TOPLVL,ATEND)		\
179  vms_flush_pending_externals(FILE)
180
181/* Anything still pending must be flushed at the very end.  */
182
183#define ASM_FILE_END(STREAM)						\
184  vms_flush_pending_externals(STREAM)
185
186/* Here we redefine ASM_OUTPUT_COMMON to select the data_section or the
187   const_section before writing the ".const" assembler directive.
188   If we were specifying a size of zero for external variables, we would
189   not have to select a section, since the assembler can assume that
190   when the size > 0, the storage is for a non-external, uninitialized
191   variable (for which a "const" declaration would be senseless),
192   and the assembler can make the storage read/write.
193
194   Since the ".const" directive specifies the actual size of the storage used
195   for both external and non-external variables, the assembler cannot
196   make this assumption, and thus it has no way of deciding if storage should
197   be read/write or read-only.  To resolve this, we give the assembler some
198   assistance, in the form of a ".const" or a ".data" directive.
199
200   Under GCC 1.40, external variables were declared with a size of zero.
201   The GNU assembler, GAS, will recognize the "-2" switch when built for VMS;
202   when compiling programs with GCC 2.n this switch should be used or the
203   assembler will not give the read-only attribute to external constants.
204   Failure to use this switch will result in linker warning messages about
205   mismatched psect attributes.  */
206
207#undef ASM_OUTPUT_COMMON
208
209#define ASM_OUTPUT_COMMON(FILE, NAME, SIZE, ROUNDED)		\
210( ((TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl))	\
211   ? (const_section (), 0) : (data_section (), 0)),		\
212  fputs (".comm ", (FILE)),					\
213  assemble_name ((FILE), (NAME)),				\
214  fprintf ((FILE), ",%u\n", (SIZE)))
215
216/* We define this to prevent the name mangler from putting dollar signs into
217   function names.  This isn't really needed, but it has been here for
218   some time and  removing it would cause the object files generated by the
219   compiler to be incompatible with the object files from a compiler that
220   had this defined.  Since it does no harm, we leave it in.  */
221
222#define NO_DOLLAR_IN_LABEL
223
224/* Add a "const" section.  This is viewed by the assembler as being nearly
225   the same as the "data" section, with the only difference being that a
226   flag is set for variables declared while in the const section.  This
227   flag is used to determine whether or not the read/write bit should be
228   set in the Psect definition.  */
229
230#define EXTRA_SECTIONS in_const
231
232#define EXTRA_SECTION_FUNCTIONS				\
233void							\
234const_section ()					\
235{							\
236  if (in_section != in_const) {				\
237    fprintf(asm_out_file,".const\n");			\
238    in_section = in_const;				\
239  }							\
240}
241
242/* This macro contains the logic to decide which section a variable
243   should be stored in.  Static constant variables go in the text_section,
244   non-const variables go in the data_section, and non-static const
245   variables go in the const_section.
246
247   Since this macro is used in a number of places, we must also be able
248   to decide where to place string constants.  */
249
250#define SELECT_SECTION(T,RELOC)						\
251{									\
252  if (TREE_CODE (T) == VAR_DECL)					\
253    {									\
254      if (TREE_READONLY (T) && ! TREE_THIS_VOLATILE (T)			\
255	  && DECL_INITIAL (T)						\
256	  && (DECL_INITIAL (T) == error_mark_node			\
257	      || TREE_CONSTANT (DECL_INITIAL (T))))			\
258	{								\
259	  if (TREE_PUBLIC (T))						\
260	    const_section ();						\
261	  else								\
262	    text_section ();						\
263	}								\
264      else								\
265	data_section ();						\
266    }									\
267  if (TREE_CODE_CLASS (TREE_CODE (T)) == 'c')				\
268    {									\
269      if ((TREE_CODE (T) == STRING_CST && flag_writable_strings))	\
270	data_section ();						\
271      else								\
272	text_section ();						\
273    }									\
274}
275
276/* This is used by a hook in varasm.c to write the assembler directives
277   that are needed to tell the startup code which constructors need to
278   be run.  */
279
280#define ASM_OUTPUT_CONSTRUCTOR(FILE,NAME)				\
281{									\
282  fprintf ((FILE),".globl $$PsectAttributes_NOOVR$$__gxx_init_1\n"); 	\
283  data_section();							\
284  fprintf ((FILE),"$$PsectAttributes_NOOVR$$__gxx_init_1:\n\t.long\t"); \
285  assemble_name ((FILE), (NAME));					\
286  fputc ('\n', (FILE));							\
287}
288
289/* This is used by a hook in varasm.c to write the assembler directives
290   that are needed to tell the startup code which destructors need to
291   be run.  */
292
293#define ASM_OUTPUT_DESTRUCTOR(FILE,NAME)				\
294{									\
295  fprintf ((FILE),".globl $$PsectAttributes_NOOVR$$__gxx_clean_1\n"); 	\
296  data_section();							\
297  fprintf ((FILE),"$$PsectAttributes_NOOVR$$__gxx_clean_1:\n\t.long\t");\
298  assemble_name ((FILE), (NAME));					\
299  fputc ('\n', (FILE));							\
300}
301
302/* True for VMS V4.6 and later.  */
303#define HAVE_ATEXIT
304
305/* The following definitions are used in libgcc2.c with the __main
306   function.  The _SHR symbol is used when the sharable image library
307   for the C++ library is used - this is picked up automatically by the linker
308   and this symbol points to the start of __CTOR_LIST__ from the C++ library.
309   If the C++ library is not used, then __CTOR_LIST_SHR__ occurs just after
310   __CTOR_LIST__, and essentially points to the same list as __CTOR_LIST.  */
311
312#ifdef L__main
313
314#define __CTOR_LIST__ __gxx_init_0
315#define __CTOR_LIST_END__ __gxx_init_2
316
317#define __CTOR_LIST_SHR__ $$PsectAttributes_NOSHR$$__gxx_init_0_shr
318#define __CTOR_LIST_SHR_END__ $$PsectAttributes_NOSHR$$__gxx_init_2_shr
319
320#define DO_GLOBAL_CTORS_BODY						\
321do {									\
322  func_ptr *p;								\
323  extern func_ptr __CTOR_LIST__[1], __CTOR_LIST_END__[1];		\
324  extern func_ptr __CTOR_LIST_SHR__[1], __CTOR_LIST_SHR_END__[1];	\
325  if (&__CTOR_LIST_SHR__[0] != &__CTOR_LIST__[1])			\
326    for (p = __CTOR_LIST_SHR__ + 1; p < __CTOR_LIST_SHR_END__ ; p++ )	\
327      if (*p) (*p) ();							\
328  for (p = __CTOR_LIST__ + 1; p < __CTOR_LIST_END__ ; p++ )		\
329    if (*p) (*p) ();							\
330  do {	/* arrange for `return' from main() to pass through exit() */	\
331      __label__ foo;							\
332      int *callers_caller_fp = (int *) __builtin_frame_address (3);	\
333      register int retval asm ("r0");					\
334      callers_caller_fp[4] = (int) && foo;				\
335      break;		/* out of do-while block */			\
336    foo:								\
337      exit (retval);							\
338  } while (0);								\
339} while (0)
340
341#define __DTOR_LIST__ __gxx_clean_0
342#define __DTOR_LIST_END__ __gxx_clean_2
343
344#define __DTOR_LIST_SHR__ $$PsectAttributes_NOSHR$$__gxx_clean_0_shr
345#define __DTOR_LIST_SHR_END__ $$PsectAttributes_NOSHR$$__gxx_clean_2_shr
346
347#define DO_GLOBAL_DTORS_BODY						\
348do {									\
349  func_ptr *p;								\
350  extern func_ptr __DTOR_LIST__[1], __DTOR_LIST_END__[1];		\
351  extern func_ptr __DTOR_LIST_SHR__[1], __DTOR_LIST_SHR_END__[1];	\
352  for (p = __DTOR_LIST__ + 1; p < __DTOR_LIST_END__ ; p++ )		\
353    if (*p) (*p) ();							\
354  if (&__DTOR_LIST_SHR__[0] != &__DTOR_LIST__[1])			\
355    for (p = __DTOR_LIST_SHR__ + 1; p < __DTOR_LIST_SHR_END__ ; p++ )	\
356      if (*p) (*p) ();							\
357} while (0)
358
359#endif /* L__main */
360
361/* Specify the list of include file directories.  */
362#define INCLUDE_DEFAULTS \
363{									\
364  { "GNU_GXX_INCLUDE:", "G++", 1, 1 },					\
365  { "GNU_CC_INCLUDE:", "GCC", 0, 0 },	/* GNU includes */		\
366  { "SYS$SYSROOT:[SYSLIB.]", 0, 0, 0 }, /* VAX-11 "C" includes */	\
367  { ".", 0, 0, 1 },		/* Make normal VMS filespecs work.  */	\
368  { 0, 0, 0, 0 }							\
369}
370