2002-05-09 Hassan Aurag <aurag@cae.com>
[official-gcc.git] / gcc / doc / vms.texi
blob5ab266695a8453e72e6a6926227a16484bfb2fca
1 @c Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
2 @c 1999, 2000, 2001 Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
6 @node VMS
7 @chapter Using GCC on VMS
9 @c prevent bad page break with this line
10 Here is how to use GCC on VMS@.
12 @menu
13 * Include Files and VMS::  Where the preprocessor looks for the include files.
14 * Global Declarations::    How to do globaldef, globalref and globalvalue with
15                            GCC.
16 * VMS Misc::               Misc information.
17 @end menu
19 @node Include Files and VMS
20 @section Include Files and VMS
22 @cindex include files and VMS
23 @cindex VMS and include files
24 @cindex header files and VMS
25 Due to the differences between the filesystems of Unix and VMS, GCC
26 attempts to translate file names in @samp{#include} into names that VMS
27 will understand.  The basic strategy is to prepend a prefix to the
28 specification of the include file, convert the whole filename to a VMS
29 filename, and then try to open the file.  GCC tries various prefixes
30 one by one until one of them succeeds:
32 @enumerate
33 @item
34 The first prefix is the @samp{GNU_CC_INCLUDE:} logical name: this is
35 where GNU C header files are traditionally stored.  If you wish to store
36 header files in non-standard locations, then you can assign the logical
37 @samp{GNU_CC_INCLUDE} to be a search list, where each element of the
38 list is suitable for use with a rooted logical.
40 @item
41 The next prefix tried is @samp{SYS$SYSROOT:[SYSLIB.]}.  This is where
42 VAX-C header files are traditionally stored.
44 @item
45 If the include file specification by itself is a valid VMS filename, the
46 preprocessor then uses this name with no prefix in an attempt to open
47 the include file.
49 @item
50 If the file specification is not a valid VMS filename (i.e.@: does not
51 contain a device or a directory specifier, and contains a @samp{/}
52 character), the preprocessor tries to convert it from Unix syntax to
53 VMS syntax.
55 Conversion works like this: the first directory name becomes a device,
56 and the rest of the directories are converted into VMS-format directory
57 names.  For example, the name @file{X11/foobar.h} is
58 translated to @file{X11:[000000]foobar.h} or @file{X11:foobar.h},
59 whichever one can be opened.  This strategy allows you to assign a
60 logical name to point to the actual location of the header files.
62 @item
63 If none of these strategies succeeds, the @samp{#include} fails.
64 @end enumerate
66 Include directives of the form:
68 @example
69 #include foobar
70 @end example
72 @noindent
73 are a common source of incompatibility between VAX-C and GCC@.  VAX-C
74 treats this much like a standard @code{#include <foobar.h>} directive.
75 That is incompatible with the ISO C behavior implemented by GCC: to
76 expand the name @code{foobar} as a macro.  Macro expansion should
77 eventually yield one of the two standard formats for @code{#include}:
79 @example
80 #include "@var{file}"
81 #include <@var{file}>
82 @end example
84 If you have this problem, the best solution is to modify the source to
85 convert the @code{#include} directives to one of the two standard forms.
86 That will work with either compiler.  If you want a quick and dirty fix,
87 define the file names as macros with the proper expansion, like this:
89 @example
90 #define stdio <stdio.h>
91 @end example
93 @noindent
94 This will work, as long as the name doesn't conflict with anything else
95 in the program.
97 Another source of incompatibility is that VAX-C assumes that:
99 @example
100 #include "foobar"
101 @end example
103 @noindent
104 is actually asking for the file @file{foobar.h}.  GCC does not
105 make this assumption, and instead takes what you ask for literally;
106 it tries to read the file @file{foobar}.  The best way to avoid this
107 problem is to always specify the desired file extension in your include
108 directives.
110 GCC for VMS is distributed with a set of include files that is
111 sufficient to compile most general purpose programs.  Even though the
112 GCC distribution does not contain header files to define constants
113 and structures for some VMS system-specific functions, there is no
114 reason why you cannot use GCC with any of these functions.  You first
115 may have to generate or create header files, either by using the public
116 domain utility @code{UNSDL} (which can be found on a DECUS tape), or by
117 extracting the relevant modules from one of the system macro libraries,
118 and using an editor to construct a C header file.
120 A @code{#include} file name cannot contain a DECNET node name.  The
121 preprocessor reports an I/O error if you attempt to use a node name,
122 whether explicitly, or implicitly via a logical name.
124 @node Global Declarations
125 @section Global Declarations and VMS
127 @findex GLOBALREF
128 @findex GLOBALDEF
129 @findex GLOBALVALUEDEF
130 @findex GLOBALVALUEREF
131 GCC does not provide the @code{globalref}, @code{globaldef} and
132 @code{globalvalue} keywords of VAX-C@.  You can get the same effect with
133 an obscure feature of GAS, the GNU assembler.  (This requires GAS
134 version 1.39 or later.)  The following macros allow you to use this
135 feature in a fairly natural way:
137 @smallexample
138 #ifdef __GNUC__
139 #define GLOBALREF(TYPE,NAME)                      \
140   TYPE NAME                                       \
141   asm ("_$$PsectAttributes_GLOBALSYMBOL$$" #NAME)
142 #define GLOBALDEF(TYPE,NAME,VALUE)                \
143   TYPE NAME                                       \
144   asm ("_$$PsectAttributes_GLOBALSYMBOL$$" #NAME) \
145     = VALUE
146 #define GLOBALVALUEREF(TYPE,NAME)                 \
147   const TYPE NAME[1]                              \
148   asm ("_$$PsectAttributes_GLOBALVALUE$$" #NAME)
149 #define GLOBALVALUEDEF(TYPE,NAME,VALUE)           \
150   const TYPE NAME[1]                              \
151   asm ("_$$PsectAttributes_GLOBALVALUE$$" #NAME)  \
152     = @{VALUE@}
153 #else
154 #define GLOBALREF(TYPE,NAME) \
155   globalref TYPE NAME
156 #define GLOBALDEF(TYPE,NAME,VALUE) \
157   globaldef TYPE NAME = VALUE
158 #define GLOBALVALUEDEF(TYPE,NAME,VALUE) \
159   globalvalue TYPE NAME = VALUE
160 #define GLOBALVALUEREF(TYPE,NAME) \
161   globalvalue TYPE NAME
162 #endif
163 @end smallexample
165 @noindent
166 (The @code{_$$PsectAttributes_GLOBALSYMBOL} prefix at the start of the
167 name is removed by the assembler, after it has modified the attributes
168 of the symbol).  These macros are provided in the VMS binaries
169 distribution in a header file @file{GNU_HACKS.H}.  An example of the
170 usage is:
172 @example
173 GLOBALREF (int, ijk);
174 GLOBALDEF (int, jkl, 0);
175 @end example
177 The macros @code{GLOBALREF} and @code{GLOBALDEF} cannot be used
178 straightforwardly for arrays, since there is no way to insert the array
179 dimension into the declaration at the right place.  However, you can
180 declare an array with these macros if you first define a typedef for the
181 array type, like this:
183 @example
184 typedef int intvector[10];
185 GLOBALREF (intvector, foo);
186 @end example
188 Array and structure initializers will also break the macros; you can
189 define the initializer to be a macro of its own, or you can expand the
190 @code{GLOBALDEF} macro by hand.  You may find a case where you wish to
191 use the @code{GLOBALDEF} macro with a large array, but you are not
192 interested in explicitly initializing each element of the array.  In
193 such cases you can use an initializer like: @code{@{0,@}}, which will
194 initialize the entire array to @code{0}.
196 A shortcoming of this implementation is that a variable declared with
197 @code{GLOBALVALUEREF} or @code{GLOBALVALUEDEF} is always an array.  For
198 example, the declaration:
200 @example
201 GLOBALVALUEREF(int, ijk);
202 @end example
204 @noindent
205 declares the variable @code{ijk} as an array of type @code{int [1]}.
206 This is done because a globalvalue is actually a constant; its ``value''
207 is what the linker would normally consider an address.  That is not how
208 an integer value works in C, but it is how an array works.  So treating
209 the symbol as an array name gives consistent results---with the
210 exception that the value seems to have the wrong type.  @strong{Don't
211 try to access an element of the array.}  It doesn't have any elements.
212 The array ``address'' may not be the address of actual storage.
214 The fact that the symbol is an array may lead to warnings where the
215 variable is used.  Insert type casts to avoid the warnings.  Here is an
216 example; it takes advantage of the ISO C feature allowing macros that
217 expand to use the same name as the macro itself.
219 @example
220 GLOBALVALUEREF (int, ss$_normal);
221 GLOBALVALUEDEF (int, xyzzy,123);
222 #ifdef __GNUC__
223 #define ss$_normal ((int) ss$_normal)
224 #define xyzzy ((int) xyzzy)
225 #endif
226 @end example
228 Don't use @code{globaldef} or @code{globalref} with a variable whose
229 type is an enumeration type; this is not implemented.  Instead, make the
230 variable an integer, and use a @code{globalvaluedef} for each of the
231 enumeration values.  An example of this would be:
233 @example
234 #ifdef __GNUC__
235 GLOBALDEF (int, color, 0);
236 GLOBALVALUEDEF (int, RED, 0);
237 GLOBALVALUEDEF (int, BLUE, 1);
238 GLOBALVALUEDEF (int, GREEN, 3);
239 #else
240 enum globaldef color @{RED, BLUE, GREEN = 3@};
241 #endif
242 @end example
244 @node VMS Misc
245 @section Other VMS Issues
247 @cindex exit status and VMS
248 @cindex return value of @code{main}
249 @cindex @code{main} and the exit status
250 GCC automatically arranges for @code{main} to return 1 by default if
251 you fail to specify an explicit return value.  This will be interpreted
252 by VMS as a status code indicating a normal successful completion.
253 Version 1 of GCC did not provide this default.
255 GCC on VMS works only with the GNU assembler, GAS@.  You need version
256 1.37 or later of GAS in order to produce value debugging information for
257 the VMS debugger.  Use the ordinary VMS linker with the object files
258 produced by GAS@.
260 @cindex shared VMS run time system
261 @cindex @file{VAXCRTL}
262 Under previous versions of GCC, the generated code would occasionally
263 give strange results when linked to the sharable @file{VAXCRTL} library.
264 Now this should work.
266 A caveat for use of @code{const} global variables: the @code{const}
267 modifier must be specified in every external declaration of the variable
268 in all of the source files that use that variable.  Otherwise the linker
269 will issue warnings about conflicting attributes for the variable.  Your
270 program will still work despite the warnings, but the variable will be
271 placed in writable storage.
273 @cindex name augmentation
274 @cindex case sensitivity and VMS
275 @cindex VMS and case sensitivity
276 Although the VMS linker does distinguish between upper and lower case
277 letters in global symbols, most VMS compilers convert all such symbols
278 into upper case and most run-time library routines also have upper case
279 names.  To be able to reliably call such routines, GCC (by means of
280 the assembler GAS) converts global symbols into upper case like other
281 VMS compilers.  However, since the usual practice in C is to distinguish
282 case, GCC (via GAS) tries to preserve usual C behavior by augmenting
283 each name that is not all lower case.  This means truncating the name
284 to at most 23 characters and then adding more characters at the end
285 which encode the case pattern of those 23.   Names which contain at
286 least one dollar sign are an exception; they are converted directly into
287 upper case without augmentation.
289 Name augmentation yields bad results for programs that use precompiled
290 libraries (such as Xlib) which were generated by another compiler.  You
291 can use the compiler option @samp{/NOCASE_HACK} to inhibit augmentation;
292 it makes external C functions and variables case-independent as is usual
293 on VMS@.  Alternatively, you could write all references to the functions
294 and variables in such libraries using lower case; this will work on VMS,
295 but is not portable to other systems.  The compiler option @samp{/NAMES}
296 also provides control over global name handling.
298 Function and variable names are handled somewhat differently with G++.
299 The GNU C++ compiler performs @dfn{name mangling} on function
300 names, which means that it adds information to the function name to
301 describe the data types of the arguments that the function takes.  One
302 result of this is that the name of a function can become very long.
303 Since the VMS linker only recognizes the first 31 characters in a name,
304 special action is taken to ensure that each function and variable has a
305 unique name that can be represented in 31 characters.
307 If the name (plus a name augmentation, if required) is less than 32
308 characters in length, then no special action is performed.  If the name
309 is longer than 31 characters, the assembler (GAS) will generate a
310 hash string based upon the function name, truncate the function name to
311 23 characters, and append the hash string to the truncated name.  If the
312 @samp{/VERBOSE} compiler option is used, the assembler will print both
313 the full and truncated names of each symbol that is truncated.
315 The @samp{/NOCASE_HACK} compiler option should not be used when you are
316 compiling programs that use libg++.  libg++ has several instances of
317 objects (i.e.  @code{Filebuf} and @code{filebuf}) which become
318 indistinguishable in a case-insensitive environment.  This leads to
319 cases where you need to inhibit augmentation selectively (if you were
320 using libg++ and Xlib in the same program, for example).  There is no
321 special feature for doing this, but you can get the result by defining a
322 macro for each mixed case symbol for which you wish to inhibit
323 augmentation.  The macro should expand into the lower case equivalent of
324 itself.  For example:
326 @example
327 #define StuDlyCapS studlycaps
328 @end example
330 These macro definitions can be placed in a header file to minimize the
331 number of changes to your source code.