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 12Copyright @copyright{} 2000, 2001, 2002, 2003, 2005 13Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.2 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``GNU General Public License'', the Front-Cover 19texts 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 35This file explains how to port libstdc++-v3 (the GNU C++ library) to 36a 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 60This document explains how to port libstdc++-v3 (the GNU C++ library) to 61a new target. 62 63In order to make the GNU C++ library (libstdc++-v3) work with a new 64target, you must edit some configuration files and provide some new 65header files. Unless this is done, libstdc++-v3 will use generic 66settings which may not be correct for your target; even if they are 67correct, they will likely be inefficient. 68 69Before you get started, make sure that you have a working C library on 70your target. The C library need not precisely comply with any 71particular standard, but should generally conform to the requirements 72imposed by the ANSI/ISO standard. 73 74In addition, you should try to verify that the C++ compiler generally 75works. It is difficult to test the C++ compiler without a working 76library, 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 79a ``host.'' The comment at the top of @file{configure.ac} explains why.) 80 81Here 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 100If you are porting to a new operating system (as opposed to a new chip 101using an existing operating system), you will need to create a new 102directory in the @file{config/os} hierarchy. For example, the IRIX 103configuration files are all in @file{config/os/irix}. There is no set 104way 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 107directories for these two versions of Solaris. On the other hand, both 108Solaris 2.7 and Solaris 2.8 use the @file{config/os/solaris/solaris-2.7} 109directory. The important information is that there needs to be a 110directory under @file{config/os} to store the files for your operating 111system. 112 113You might have to change the @file{configure.host} file to ensure that 114your 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 116if the default will not suffice. The switch statement switches on only 117the OS portion of the standard target triplet; e.g., the @code{solaris2.8} 118in @code{sparc-sun-solaris2.8}. If the new directory is named after the 119OS portion of the triplet (the default), then nothing needs to be changed. 120 121The first file to create in this directory, should be called 122@file{os_defines.h}. This file contains basic macro definitions 123that are required to allow the C++ library to work with your C library. 124 125Several libstdc++-v3 source files unconditionally define the macro 126@code{_POSIX_SOURCE}. On many systems, defining this macro causes 127large portions of the C library header files to be eliminated 128at preprocessing time. Therefore, you may have to @code{#undef} this 129macro, or define other macros (like @code{_LARGEFILE_SOURCE} or 130@code{__EXTENSIONS__}). You won't know what macros to define or 131undefine at this point; you'll have to try compiling the library and 132seeing what goes wrong. If you see errors about calling functions 133that have not been declared, look in your C library headers to see if 134the functions are declared there, and then figure out what macros you 135need to define. You will need to add them to the 136@code{CPLUSPLUS_CPP_SPEC} macro in the GCC configuration file for your 137target. It will not work to simply define these macros in 138@file{os_defines.h}. 139 140At this time, there are a few libstdc++-v3-specific macros which may be 141defined: 142 143@code{_GLIBCXX_USE_C99_CHECK} may be defined to 1 to check C99 144function declarations (which are not covered by specialization below) 145found in system headers against versions found in the library headers 146derived from the standard. 147 148@code{_GLIBCXX_USE_C99_DYNAMIC} may be defined to an expression that 149yields 0 if and only if the system headers are exposing proper support 150for C99 functions (which are not covered by specialization below). If 151defined, it must be 0 while bootstrapping the compiler/rebuilding the 152library. 153 154@code{_GLIBCXX_USE_C99_LONG_LONG_CHECK} may be defined to 1 to check 155the set of C99 long long function declarations found in system headers 156against versions found in the library headers derived from the 157standard. 158 159@code{_GLIBCXX_USE_C99_LONG_LONG_DYNAMIC} may be defined to an 160expression that yields 0 if and only if the system headers are 161exposing proper support for the set of C99 long long functions. If 162defined, it must be 0 while bootstrapping the compiler/rebuilding the 163library. 164 165@code{_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC} may be defined to an 166expression that yields 0 if and only if the system headers 167are exposing proper support for the related set of macros. If defined, 168it must be 0 while bootstrapping the compiler/rebuilding the library. 169 170@code{_GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_CHECK} may be defined 171to 1 to check the related set of function declarations found in system 172headers against versions found in the library headers derived from 173the standard. 174 175@code{_GLIBCXX_USE_C99_FLOAT_TRANSCENDENTALS_DYNAMIC} may be defined 176to an expression that yields 0 if and only if the system headers 177are exposing proper support for the related set of functions. If defined, 178it must be 0 while bootstrapping the compiler/rebuilding the library. 179 180Finally, you should bracket the entire file in an include-guard, like 181this: 182 183@example 184#ifndef _GLIBCXX_OS_DEFINES 185#define _GLIBCXX_OS_DEFINES 186... 187#endif 188@end example 189 190We recommend copying an existing @file{os_defines.h} to use as a 191starting point. 192 193@c --------------------------------------------------------------------- 194@c CPU 195@c --------------------------------------------------------------------- 196 197@node CPU 198@chapter CPU 199 200If you are porting to a new chip (as opposed to a new operating system 201running 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, 203there are no strict rules on how to organize the CPU configuration 204directory, but careful naming choices will allow the configury to find your 205setup files without explicit help. 206 207We recommend that for a target triplet @code{<CPU>-<vendor>-<OS>}, you 208name your configuration directory @file{config/cpu/<CPU>}. If you do this, 209the configury will find the directory by itself. Otherwise you will need to 210edit 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 213Note that some chip families share a single configuration directory, for 214example, @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 218The @code{cpu_include_dir} sets default locations for the files controlling 219@ref{Thread safety} and @ref{Numeric limits}, if the defaults are not 220appropriate for your chip. 221 222 223@c --------------------------------------------------------------------- 224@c Character types 225@c --------------------------------------------------------------------- 226 227@node Character types 228@chapter Character types 229 230The library requires that you provide three header files to implement 231character 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 234certainly need some modification. 235 236The first file to write is @file{ctype_base.h}. This file provides 237some very basic information about character classification. The libstdc++-v3 238library assumes that your C library implements @file{<ctype.h>} by using 239a table (indexed by character code) containing integers, where each of 240these integers is a bit-mask indicating whether the character is 241upper-case, lower-case, alphabetic, etc. The @file{ctype_base.h} 242file gives the type of the integer, and the values of the various bit 243masks. You will have to peer at your own @file{<ctype.h>} to figure out 244how to define the values required by this file. 245 246The @file{ctype_base.h} header file does not need include guards. 247It should contain a single @code{struct} definition called 248@code{ctype_base}. This @code{struct} should contain two type 249declarations, and one enumeration declaration, like this example, taken 250from the IRIX configuration: 251 252@example 253struct 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 276The @code{mask} type is the type of the elements in the table. If your 277C library uses a table to map lower-case numbers to upper-case numbers, 278and vice versa, you should define @code{__to_type} to be the type of the 279elements in that table. If you don't mind taking a minor performance 280penalty, or if your library doesn't implement @code{toupper} and 281@code{tolower} in this way, you can pick any pointer-to-integer type, 282but you must still define the type. 283 284The enumeration should give definitions for all the values in the above 285example, using the values from your native @file{<ctype.h>}. They can 286be given symbolically (as above), or numerically, if you prefer. You do 287not have to include @file{<ctype.h>} in this header; it will always be 288included before @file{ctype_base.h} is included. 289 290The next file to write is @file{ctype_noninline.h}, which also does 291not require include guards. This file defines a few member functions 292that will be included in @file{include/bits/locale_facets.h}. The first 293function that must be written is the @code{ctype<char>::ctype} 294constructor. Here is the IRIX example: 295 296@example 297ctype<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 310There are two parts of this that you might choose to alter. The first, 311and most important, is the line involving @code{__libc_attr}. That is 312IRIX system-dependent code that gets the base of the table mapping 313character codes to attributes. You need to substitute code that obtains 314the address of this table on your system. If you want to use your 315operating system's tables to map upper-case letters to lower-case, and 316vice versa, you should initialize @code{_M_toupper} and 317@code{_M_tolower} with those tables, in similar fashion. 318 319Now, you have to write two functions to convert from upper-case to 320lower-case, and vice versa. Here are the IRIX versions: 321 322@example 323char 324ctype<char>::do_toupper(char __c) const 325@{ return _toupper(__c); @} 326 327char 328ctype<char>::do_tolower(char __c) const 329@{ return _tolower(__c); @} 330@end example 331 332@noindent 333Your 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 337Finally, you have to provide two utility functions that convert strings 338of characters. The versions provided here will always work -- but you 339could use specialized routines for greater performance if you have 340machinery to do that on your system: 341 342@example 343const char* 344ctype<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 354const char* 355ctype<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 366You must also provide the @file{ctype_inline.h} file, which 367contains 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 370In detail, the functions provided test characters for particular 371properties; they are analogous to the functions like @code{isalpha} and 372@code{islower} provided by the C library. 373 374The first function is implemented like this on IRIX: 375 376@example 377bool 378ctype<char>:: 379is(mask __m, char __c) const throw() 380@{ return (_M_table)[(unsigned char)(__c)] & __m; @} 381@end example 382 383@noindent 384The @code{_M_table} is the table passed in above, in the constructor. 385This is the table that contains the bitmasks for each character. The 386implementation here should work on all systems. 387 388The next function is: 389 390@example 391const char* 392ctype<char>:: 393is(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 402This function is similar; it copies the masks for all the characters 403from @code{__low} up until @code{__high} into the vector given by 404@code{__vec}. 405 406The last two functions again are entirely generic: 407 408@example 409const char* 410ctype<char>:: 411scan_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 418const char* 419ctype<char>:: 420scan_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 435The C++ library string functionality requires a couple of atomic 436operations to provide thread-safety. If you don't take any special 437action, the library will use stub versions of these functions that are 438not thread-safe. They will work fine, unless your applications are 439multi-threaded. 440 441If you want to provide custom, safe, versions of these functions, there 442are two distinct approaches. One is to provide a version for your CPU, 443using assembly language constructs. The other is to use the 444thread-safety primitives in your operating system. In either case, you 445make a file called @file{atomicity.h}, and the variable 446@code{ATOMICITYH} must point to this file. 447 448If you are using the assembly-language approach, put this code in 449@file{config/cpu/<chip>/atomicity.h}, where chip is the name of 450your processor (@pxref{CPU}). No additional changes are necessary to 451locate the file in this case; @code{ATOMICITYH} will be set by default. 452 453If you are using the operating system thread-safety primitives approach, 454you can also put this code in the same CPU directory, in which case no more 455work is needed to locate the file. For examples of this approach, 456see the @file{atomicity.h} file for IRIX or IA64. 457 458Alternatively, if the primitives are more closely related to the OS 459than they are to the CPU, you can put the @file{atomicity.h} file in 460the @ref{Operating system} directory instead. In this case, you must 461edit @file{configure.host}, and in the switch statement that handles 462operating systems, override the @code{ATOMICITYH} variable to point to 463the appropriate @code{os_include_dir}. For examples of this approach, 464see the @file{atomicity.h} file for AIX. 465 466With those bits out of the way, you have to actually write 467@file{atomicity.h} itself. This file should be wrapped in an 468include guard named @code{_GLIBCXX_ATOMICITY_H}. It should define one 469type, and two functions. 470 471The type is @code{_Atomic_word}. Here is the version used on IRIX: 472 473@example 474typedef long _Atomic_word; 475@end example 476 477@noindent 478This type must be a signed integral type supporting atomic operations. 479If you're using the OS approach, use the same type used by your system's 480primitives. Otherwise, use the type for which your CPU provides atomic 481primitives. 482 483Then, you must provide two functions. The bodies of these functions 484must be equivalent to those provided here, but using atomic operations: 485 486@example 487static 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 496static 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 511The C++ library requires information about the fundamental data types, 512such as the minimum and maximum representable values of each type. 513You can define each of these values individually, but it is usually 514easiest just to indicate how many bits are used in each of the data 515types and let the library do the rest. For information about the 516macros to define, see the top of @file{include/bits/std_limits.h}. 517 518If you need to define any macros, you can do so in @file{os_defines.h}. 519However, if all operating systems for your CPU are likely to use the 520same values, you can provide a CPU-specific file instead so that you 521do not have to provide the same definitions for each operating system. 522To take that approach, create a new file called @file{cpu_limits.h} in 523your CPU configuration directory (@pxref{CPU}). 524 525@c --------------------------------------------------------------------- 526@c Libtool 527@c --------------------------------------------------------------------- 528 529@node Libtool 530@chapter Libtool 531 532The C++ library is compiled, archived and linked with libtool. 533Explaining the full workings of libtool is beyond the scope of this 534document, but there are a few, particular bits that are necessary for 535porting. 536 537Some 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 540logic 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 542for C++, or C++ definitions which have not been kept up to date. 543 544The C++ run-time library contains initialization code that needs to be 545run as the library is loaded. Often, that requires linking in special 546object files when the C++ library is built as a shared library, or 547taking other system-specific actions. 548 549The libstdc++-v3 library is linked with the C version of libtool, even 550though it is a C++ library. Therefore, the C version of libtool needs to 551ensure that the run-time library initializers are run. The usual way to 552do this is to build the library using @code{gcc -shared}. 553 554If 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 556that sets @code{archive_cmds}. Here, adjust the setting for your 557operating 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 571