OSDN Git Service

2005-05-01 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / docs / html / 17_intro / porting.texi
1 \input texinfo
2
3 @c ---------------------------------------------------------------------
4 @c Prologue
5 @c ---------------------------------------------------------------------
6
7 @setfilename porting.info
8 @settitle Porting libstdc++-v3
9 @setchapternewpage odd
10
11 @copying
12 Copyright @copyright{} 2000, 2001, 2002, 2003, 2005 
13 Free Software Foundation, Inc.
14
15 Permission is granted to copy, distribute and/or modify this document
16 under the terms of the GNU Free Documentation License, Version 1.2 or
17 any later version published by the Free Software Foundation; with the
18 Invariant Sections being ``GNU General Public License'', the Front-Cover
19 texts being (a) (see below), and with the Back-Cover Texts being (b)
20 (see below).  A copy of the license is included in the section entitled
21 ``GNU Free Documentation License''.
22
23 (a) The FSF's Front-Cover Text is:
24
25      A GNU Manual
26
27 (b) The FSF's Back-Cover Text is:
28
29      You have freedom to copy and modify this GNU Manual, like GNU
30      software.  Copies published by the Free Software Foundation raise
31      funds for GNU development.
32 @end copying
33
34 @ifinfo
35 This file explains how to port libstdc++-v3 (the GNU C++ library) to 
36 a new target.
37
38 @insertcopying
39 @end ifinfo
40
41 @c ---------------------------------------------------------------------
42 @c Titlepage
43 @c ---------------------------------------------------------------------
44
45 @titlepage
46 @title Porting libstdc++-v3
47 @author Mark Mitchell
48 @page
49 @vskip 0pt plus 1filll
50 @insertcopying
51 @end titlepage
52
53 @c ---------------------------------------------------------------------
54 @c Top
55 @c ---------------------------------------------------------------------
56
57 @node Top
58 @top Porting libstdc++-v3
59
60 This document explains how to port libstdc++-v3 (the GNU C++ library) to 
61 a new target.
62
63 In order to make the GNU C++ library (libstdc++-v3) work with a new
64 target, you must edit some configuration files and provide some new
65 header files.  Unless this is done, libstdc++-v3 will use generic
66 settings which may not be correct for your target; even if they are
67 correct, they will likely be inefficient.
68
69 Before you get started, make sure that you have a working C library on
70 your target.  The C library need not precisely comply with any
71 particular standard, but should generally conform to the requirements
72 imposed by the ANSI/ISO standard.
73
74 In addition, you should try to verify that the C++ compiler generally
75 works.  It is difficult to test the C++ compiler without a working
76 library, but you should at least try some minimal test cases.
77
78 (Note that what we think of as a ``target,'' the library refers to as
79 a ``host.''  The comment at the top of @file{configure.ac} explains why.)
80
81 Here are the primary steps required to port the library:
82
83 @menu
84 * Operating system::    Configuring for your operating system.
85 * CPU::                 Configuring for your processor chip.
86 * Character types::     Implementing character classification.
87 * Thread safety::       Implementing atomic operations.
88 * Numeric limits::      Implementing numeric limits.
89 * Libtool::             Using libtool.
90 * GNU Free Documentation License:: How you can copy and share this manual.
91 @end menu
92
93 @c ---------------------------------------------------------------------
94 @c Operating system
95 @c ---------------------------------------------------------------------
96
97 @node Operating system
98 @chapter Operating system
99
100 If you are porting to a new operating system (as opposed to a new chip
101 using an existing operating system), you will need to create a new
102 directory in the @file{config/os} hierarchy.  For example, the IRIX
103 configuration files are all in @file{config/os/irix}.  There is no set
104 way to organize the OS configuration directory.  For example,
105 @file{config/os/solaris/solaris-2.6} and
106 @file{config/os/solaris/solaris-2.7} are used as configuration
107 directories for these two versions of Solaris.  On the other hand, both
108 Solaris 2.7 and Solaris 2.8 use the @file{config/os/solaris/solaris-2.7}
109 directory.  The important information is that there needs to be a
110 directory under @file{config/os} to store the files for your operating
111 system.
112
113 You might have to change the @file{configure.host} file to ensure that
114 your new directory is activated.  Look for the switch statement that sets
115 @code{os_include_dir}, and add a pattern to handle your operating system
116 if the default will not suffice.  The switch statement switches on only
117 the OS portion of the standard target triplet; e.g., the @code{solaris2.8}
118 in @code{sparc-sun-solaris2.8}.  If the new directory is named after the
119 OS portion of the triplet (the default), then nothing needs to be changed.
120
121 The first file to create in this directory, should be called
122 @file{os_defines.h}.  This file contains basic macro definitions
123 that are required to allow the C++ library to work with your C library.
124
125 Several libstdc++-v3 source files unconditionally define the macro
126 @code{_POSIX_SOURCE}.  On many systems, defining this macro causes
127 large portions of the C library header files to be eliminated
128 at preprocessing time.  Therefore, you may have to @code{#undef} this
129 macro, or define other macros (like @code{_LARGEFILE_SOURCE} or
130 @code{__EXTENSIONS__}).  You won't know what macros to define or
131 undefine at this point; you'll have to try compiling the library and
132 seeing what goes wrong.  If you see errors about calling functions
133 that have not been declared, look in your C library headers to see if
134 the functions are declared there, and then figure out what macros you
135 need to define.  You will need to add them to the
136 @code{CPLUSPLUS_CPP_SPEC} macro in the GCC configuration file for your
137 target.  It will not work to simply define these macros in
138 @file{os_defines.h}.
139
140 At this time, there are a few libstdc++-v3-specific macros which may be
141 defined:
142
143 @code{_GLIBCXX_USE_C99_CHECK} may be defined to 1 to check C99
144 function declarations (which are not covered by specialization below)
145 found in system headers against versions found in the library headers
146 derived from the standard.
147
148 @code{_GLIBCXX_USE_C99_DYNAMIC} may be defined to an expression that
149 yields 0 if and only if the system headers are exposing proper support
150 for C99 functions (which are not covered by specialization below).  If
151 defined, it must be 0 while bootstrapping the compiler/rebuilding the
152 library.
153
154 @code{_GLIBCXX_USE_C99_LONG_LONG_CHECK} may be defined to 1 to check
155 the set of C99 long long function declarations found in system headers
156 against versions found in the library headers derived from the
157 standard.
158
159 @code{_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC} may be defined to an
160 expression that yields 0 if and only if the system headers are
161 exposing proper support for the set of C99 long long functions.  If
162 defined, it must be 0 while bootstrapping the compiler/rebuilding the
163 library.
164
165 @code{_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC} may be defined to an
166 expression that yields 0 if and only if the system headers
167 are exposing proper support for the related set of macros.  If defined,
168 it must be 0 while bootstrapping the compiler/rebuilding the library.
169
170 @code{_GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_CHECK} may be defined
171 to 1 to check the related set of function declarations found in system
172 headers against versions found in the library headers derived from
173 the standard.
174
175 @code{_GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC} may be defined
176 to an expression that yields 0 if and only if the system headers
177 are exposing proper support for the related set of functions.  If defined,
178 it must be 0 while bootstrapping the compiler/rebuilding the library.
179
180 Finally, you should bracket the entire file in an include-guard, like
181 this:
182
183 @example
184 #ifndef _GLIBCXX_OS_DEFINES
185 #define _GLIBCXX_OS_DEFINES
186 ...
187 #endif
188 @end example
189
190 We recommend copying an existing @file{os_defines.h} to use as a
191 starting point.
192
193 @c ---------------------------------------------------------------------
194 @c CPU
195 @c ---------------------------------------------------------------------
196
197 @node CPU
198 @chapter CPU
199
200 If you are porting to a new chip (as opposed to a new operating system
201 running on an existing chip), you will need to create a new directory in the
202 @file{config/cpu} hierarchy.  Much like the @ref{Operating system} setup,
203 there are no strict rules on how to organize the CPU configuration
204 directory, but careful naming choices will allow the configury to find your
205 setup files without explicit help.
206
207 We recommend that for a target triplet @code{<CPU>-<vendor>-<OS>}, you
208 name your configuration directory @file{config/cpu/<CPU>}.  If you do this,
209 the configury will find the directory by itself.  Otherwise you will need to
210 edit the @file{configure.host} file and, in the switch statement that sets
211 @code{cpu_include_dir}, add a pattern to handle your chip.
212
213 Note that some chip families share a single configuration directory, for
214 example, @code{alpha}, @code{alphaev5}, and @code{alphaev6} all use the
215 @file{config/cpu/alpha} directory, and there is an entry in the
216 @file{configure.host} switch statement to handle this.
217
218 The @code{cpu_include_dir} sets default locations for the files controlling
219 @ref{Thread safety} and @ref{Numeric limits}, if the defaults are not
220 appropriate for your chip.
221
222
223 @c ---------------------------------------------------------------------
224 @c Character types
225 @c ---------------------------------------------------------------------
226
227 @node Character types
228 @chapter Character types
229
230 The library requires that you provide three header files to implement
231 character classification, analogous to that provided by the C libraries
232 @file{<ctype.h>} header.  You can model these on the files provided in
233 @file{config/os/generic}.  However, these files will almost
234 certainly need some modification.
235
236 The first file to write is @file{ctype_base.h}.  This file provides
237 some very basic information about character classification.  The libstdc++-v3
238 library assumes that your C library implements @file{<ctype.h>} by using
239 a table (indexed by character code) containing integers, where each of
240 these integers is a bit-mask indicating whether the character is
241 upper-case, lower-case, alphabetic, etc.  The @file{ctype_base.h}
242 file gives the type of the integer, and the values of the various bit
243 masks.  You will have to peer at your own @file{<ctype.h>} to figure out
244 how to define the values required by this file.
245
246 The @file{ctype_base.h} header file does not need include guards.
247 It should contain a single @code{struct} definition called
248 @code{ctype_base}.  This @code{struct} should contain two type
249 declarations, and one enumeration declaration, like this example, taken
250 from the IRIX configuration:
251
252 @example
253 struct ctype_base
254 @{
255   typedef unsigned int  mask;
256   typedef int*          __to_type;
257
258   enum
259   @{
260     space = _ISspace,
261     print = _ISprint,
262     cntrl = _IScntrl,
263     upper = _ISupper,
264     lower = _ISlower,
265     alpha = _ISalpha,
266     digit = _ISdigit,
267     punct = _ISpunct,
268     xdigit = _ISxdigit,
269     alnum = _ISalnum,
270     graph = _ISgraph
271   @};
272 @};
273 @end example
274
275 @noindent
276 The @code{mask} type is the type of the elements in the table.  If your
277 C library uses a table to map lower-case numbers to upper-case numbers,
278 and vice versa, you should define @code{__to_type} to be the type of the
279 elements in that table.  If you don't mind taking a minor performance
280 penalty, or if your library doesn't implement @code{toupper} and
281 @code{tolower} in this way, you can pick any pointer-to-integer type,
282 but you must still define the type.
283
284 The enumeration should give definitions for all the values in the above
285 example, using the values from your native @file{<ctype.h>}.  They can
286 be given symbolically (as above), or numerically, if you prefer.  You do
287 not have to include @file{<ctype.h>} in this header; it will always be
288 included before @file{ctype_base.h} is included.
289
290 The next file to write is @file{ctype_noninline.h}, which also does
291 not require include guards.  This file defines a few member functions
292 that will be included in @file{include/bits/locale_facets.h}.  The first
293 function that must be written is the @code{ctype<char>::ctype}
294 constructor.  Here is the IRIX example:
295
296 @example
297 ctype<char>::ctype(const mask* __table = 0, bool __del = false, 
298       size_t __refs = 0)
299   : _Ctype_nois<char>(__refs), _M_del(__table != 0 && __del), 
300     _M_toupper(NULL),
301     _M_tolower(NULL),
302     _M_ctable(NULL), 
303     _M_table(!__table
304              ? (const mask*) (__libc_attr._ctype_tbl->_class + 1)
305              : __table) 
306   @{ @}
307 @end example
308
309 @noindent
310 There are two parts of this that you might choose to alter. The first,
311 and most important, is the line involving @code{__libc_attr}.  That is
312 IRIX system-dependent code that gets the base of the table mapping
313 character codes to attributes.  You need to substitute code that obtains
314 the address of this table on your system.  If you want to use your
315 operating system's tables to map upper-case letters to lower-case, and
316 vice versa, you should initialize @code{_M_toupper} and
317 @code{_M_tolower} with those tables, in similar fashion.
318
319 Now, you have to write two functions to convert from upper-case to
320 lower-case, and vice versa.  Here are the IRIX versions:
321
322 @example
323 char
324 ctype<char>::do_toupper(char __c) const
325 @{ return _toupper(__c); @}
326
327 char
328 ctype<char>::do_tolower(char __c) const
329 @{ return _tolower(__c); @}
330 @end example
331
332 @noindent
333 Your C library provides equivalents to IRIX's @code{_toupper} and
334 @code{_tolower}.  If you initialized @code{_M_toupper} and
335 @code{_M_tolower} above, then you could use those tables instead.
336
337 Finally, you have to provide two utility functions that convert strings
338 of characters.  The versions provided here will always work -- but you
339 could use specialized routines for greater performance if you have
340 machinery to do that on your system:
341
342 @example
343 const char*
344 ctype<char>::do_toupper(char* __low, const char* __high) const
345 @{
346   while (__low < __high)
347     @{
348       *__low = do_toupper(*__low);
349       ++__low;
350     @}
351   return __high;
352 @}
353
354 const char* 
355 ctype<char>::do_tolower(char* __low, const char* __high) const
356 @{
357   while (__low < __high)
358     @{
359       *__low = do_tolower(*__low);
360       ++__low;
361     @}
362   return __high;
363 @}
364 @end example
365
366 You must also provide the @file{ctype_inline.h} file, which
367 contains a few more functions.  On most systems, you can just copy
368 @file{config/os/generic/ctype_inline.h} and use it on your system.
369
370 In detail, the functions provided test characters for particular
371 properties; they are analogous to the functions like @code{isalpha} and
372 @code{islower} provided by the C library.
373
374 The first function is implemented like this on IRIX:
375
376 @example
377 bool
378 ctype<char>::
379 is(mask __m, char __c) const throw()
380 @{ return (_M_table)[(unsigned char)(__c)] & __m; @}
381 @end example
382
383 @noindent
384 The @code{_M_table} is the table passed in above, in the constructor.
385 This is the table that contains the bitmasks for each character.  The
386 implementation here should work on all systems.
387
388 The next function is:
389
390 @example
391 const char*
392 ctype<char>::
393 is(const char* __low, const char* __high, mask* __vec) const throw()
394 @{
395   while (__low < __high)
396     *__vec++ = (_M_table)[(unsigned char)(*__low++)];
397   return __high;
398 @}
399 @end example
400
401 @noindent
402 This function is similar; it copies the masks for all the characters
403 from @code{__low} up until @code{__high} into the vector given by
404 @code{__vec}.
405
406 The last two functions again are entirely generic:
407
408 @example
409 const char*
410 ctype<char>::
411 scan_is(mask __m, const char* __low, const char* __high) const throw()
412 @{
413   while (__low < __high && !this->is(__m, *__low))
414     ++__low;
415   return __low;
416 @}
417
418 const char*
419 ctype<char>::
420 scan_not(mask __m, const char* __low, const char* __high) const throw()
421 @{
422   while (__low < __high && this->is(__m, *__low))
423     ++__low;
424   return __low;
425 @}
426 @end example
427
428 @c ---------------------------------------------------------------------
429 @c Thread safety
430 @c ---------------------------------------------------------------------
431
432 @node Thread safety
433 @chapter Thread safety
434
435 The C++ library string functionality requires a couple of atomic
436 operations to provide thread-safety.  If you don't take any special
437 action, the library will use stub versions of these functions that are
438 not thread-safe.  They will work fine, unless your applications are
439 multi-threaded.
440
441 If you want to provide custom, safe, versions of these functions, there
442 are two distinct approaches.  One is to provide a version for your CPU,
443 using assembly language constructs.  The other is to use the
444 thread-safety primitives in your operating system.  In either case, you
445 make a file called @file{atomicity.h}, and the variable
446 @code{ATOMICITYH} must point to this file.
447
448 If you are using the assembly-language approach, put this code in
449 @file{config/cpu/<chip>/atomicity.h}, where chip is the name of
450 your processor (@pxref{CPU}).  No additional changes are necessary to
451 locate the file in this case; @code{ATOMICITYH} will be set by default.
452
453 If you are using the operating system thread-safety primitives approach,
454 you can also put this code in the same CPU directory, in which case no more
455 work is needed to locate the file.  For examples of this approach,
456 see the @file{atomicity.h} file for IRIX or IA64.
457
458 Alternatively, if the primitives are more closely related to the OS
459 than they are to the CPU, you can put the @file{atomicity.h} file in
460 the @ref{Operating system} directory instead.  In this case, you must
461 edit @file{configure.host}, and in the switch statement that handles
462 operating systems, override the @code{ATOMICITYH} variable to point to
463 the appropriate @code{os_include_dir}.  For examples of this approach,
464 see the @file{atomicity.h} file for AIX.
465
466 With those bits out of the way, you have to actually write
467 @file{atomicity.h} itself.  This file should be wrapped in an
468 include guard named @code{_GLIBCXX_ATOMICITY_H}.  It should define one
469 type, and two functions.  
470
471 The type is @code{_Atomic_word}.  Here is the version used on IRIX:
472
473 @example
474 typedef long _Atomic_word;
475 @end example
476
477 @noindent
478 This type must be a signed integral type supporting atomic operations.
479 If you're using the OS approach, use the same type used by your system's
480 primitives.  Otherwise, use the type for which your CPU provides atomic
481 primitives.
482
483 Then, you must provide two functions.  The bodies of these functions
484 must be equivalent to those provided here, but using atomic operations:
485
486 @example
487 static inline _Atomic_word
488 __attribute__ ((__unused__))
489 __exchange_and_add (_Atomic_word* __mem, int __val)
490 @{
491   _Atomic_word __result = *__mem;
492   *__mem += __val;
493   return __result;
494 @}
495
496 static inline void
497 __attribute__ ((__unused__))
498 __atomic_add (_Atomic_word* __mem, int __val)
499 @{
500   *__mem += __val;
501 @}
502 @end example
503
504 @c ---------------------------------------------------------------------
505 @c Numeric limits
506 @c ---------------------------------------------------------------------
507
508 @node Numeric limits
509 @chapter Numeric limits
510
511 The C++ library requires information about the fundamental data types,
512 such as the minimum and maximum representable values of each type.
513 You can define each of these values individually, but it is usually
514 easiest just to indicate how many bits are used in each of the data
515 types and let the library do the rest.  For information about the
516 macros to define, see the top of @file{include/bits/std_limits.h}.
517
518 If you need to define any macros, you can do so in @file{os_defines.h}.
519 However, if all operating systems for your CPU are likely to use the
520 same values, you can provide a CPU-specific file instead so that you
521 do not have to provide the same definitions for each operating system.
522 To take that approach, create a new file called @file{cpu_limits.h} in
523 your CPU configuration directory (@pxref{CPU}).
524
525 @c ---------------------------------------------------------------------
526 @c Libtool
527 @c ---------------------------------------------------------------------
528
529 @node Libtool
530 @chapter Libtool
531
532 The C++ library is compiled, archived and linked with libtool.
533 Explaining the full workings of libtool is beyond the scope of this
534 document, but there are a few, particular bits that are necessary for
535 porting.
536
537 Some parts of the libstdc++-v3 library are compiled with the libtool
538 @code{--tags CXX} option (the C++ definitions for libtool).  Therefore,
539 @file{ltcf-cxx.sh} in the top-level directory needs to have the correct
540 logic to compile and archive objects equivalent to the C version of libtool,
541 @file{ltcf-c.sh}.  Some libtool targets have definitions for C but not
542 for C++, or C++ definitions which have not been kept up to date.
543
544 The C++ run-time library contains initialization code that needs to be
545 run as the library is loaded.  Often, that requires linking in special
546 object files when the C++ library is built as a shared library, or
547 taking other system-specific actions.
548
549 The libstdc++-v3 library is linked with the C version of libtool, even
550 though it is a C++ library.  Therefore, the C version of libtool needs to
551 ensure that the run-time library initializers are run.  The usual way to
552 do this is to build the library using @code{gcc -shared}.
553
554 If you need to change how the library is linked, look at
555 @file{ltcf-c.sh} in the top-level directory.  Find the switch statement
556 that sets @code{archive_cmds}.  Here, adjust the setting for your
557 operating system.
558
559 @c ---------------------------------------------------------------------
560 @c GFDL
561 @c ---------------------------------------------------------------------
562
563 @include fdl.texi
564
565 @c ---------------------------------------------------------------------
566 @c Epilogue
567 @c ---------------------------------------------------------------------
568
569 @contents
570 @bye