OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [gcc-4.5.1/] [gcc-4.5.1-or32-1.0rc4/] [libstdc++-v3/] [doc/] [xml/] [manual/] [internals.xml] - Blame information for rev 519

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.