OSDN Git Service

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