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

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-binutils/] [binutils-2.19.1/] [elfcpp/] [elfcpp_swap.h] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 jlechner
// elfcpp_swap.h -- Handle swapping for elfcpp   -*- C++ -*-
2
 
3
// Copyright 2006, 2007, Free Software Foundation, Inc.
4
// Written by Ian Lance Taylor <iant@google.com>.
5
 
6
// This file is part of elfcpp.
7
 
8
// This program is free software; you can redistribute it and/or
9
// modify it under the terms of the GNU Library General Public License
10
// as published by the Free Software Foundation; either version 2, or
11
// (at your option) any later version.
12
 
13
// In addition to the permissions in the GNU Library General Public
14
// License, the Free Software Foundation gives you unlimited
15
// permission to link the compiled version of this file into
16
// combinations with other programs, and to distribute those
17
// combinations without any restriction coming from the use of this
18
// file.  (The Library Public License restrictions do apply in other
19
// respects; for example, they cover modification of the file, and
20
/// distribution when not linked into a combined executable.)
21
 
22
// This program is distributed in the hope that it will be useful, but
23
// WITHOUT ANY WARRANTY; without even the implied warranty of
24
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25
// Library General Public License for more details.
26
 
27
// You should have received a copy of the GNU Library General Public
28
// License along with this program; if not, write to the Free Software
29
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30
// 02110-1301, USA.
31
 
32
// This header file defines basic template classes to efficiently swap
33
// numbers between host form and target form.  When the host and
34
// target have the same endianness, these turn into no-ops.
35
 
36
#ifndef ELFCPP_SWAP_H
37
#define ELFCPP_SWAP_H
38
 
39
#include <stdint.h>
40
#include <endian.h>
41
#include <byteswap.h>
42
 
43
namespace elfcpp
44
{
45
 
46
// Endian simply indicates whether the host is big endian or not.
47
 
48
struct Endian
49
{
50
 public:
51
  // Used for template specializations.
52
  static const bool host_big_endian = __BYTE_ORDER == __BIG_ENDIAN;
53
};
54
 
55
// Valtype_base is a template based on size (8, 16, 32, 64) which
56
// defines the type Valtype as the unsigned integer, and
57
// Signed_valtype as the signed integer, of the specified size.
58
 
59
template<int size>
60
struct Valtype_base;
61
 
62
template<>
63
struct Valtype_base<8>
64
{
65
  typedef uint8_t Valtype;
66
  typedef int8_t Signed_valtype;
67
};
68
 
69
template<>
70
struct Valtype_base<16>
71
{
72
  typedef uint16_t Valtype;
73
  typedef int16_t Signed_valtype;
74
};
75
 
76
template<>
77
struct Valtype_base<32>
78
{
79
  typedef uint32_t Valtype;
80
  typedef int32_t Signed_valtype;
81
};
82
 
83
template<>
84
struct Valtype_base<64>
85
{
86
  typedef uint64_t Valtype;
87
  typedef int64_t Signed_valtype;
88
};
89
 
90
// Convert_endian is a template based on size and on whether the host
91
// and target have the same endianness.  It defines the type Valtype
92
// as Valtype_base does, and also defines a function convert_host
93
// which takes an argument of type Valtype and returns the same value,
94
// but swapped if the host and target have different endianness.
95
 
96
template<int size, bool same_endian>
97
struct Convert_endian;
98
 
99
template<int size>
100
struct Convert_endian<size, true>
101
{
102
  typedef typename Valtype_base<size>::Valtype Valtype;
103
 
104
  static inline Valtype
105
  convert_host(Valtype v)
106
  { return v; }
107
};
108
 
109
template<>
110
struct Convert_endian<8, false>
111
{
112
  typedef Valtype_base<8>::Valtype Valtype;
113
 
114
  static inline Valtype
115
  convert_host(Valtype v)
116
  { return v; }
117
};
118
 
119
template<>
120
struct Convert_endian<16, false>
121
{
122
  typedef Valtype_base<16>::Valtype Valtype;
123
 
124
  static inline Valtype
125
  convert_host(Valtype v)
126
  { return bswap_16(v); }
127
};
128
 
129
template<>
130
struct Convert_endian<32, false>
131
{
132
  typedef Valtype_base<32>::Valtype Valtype;
133
 
134
  static inline Valtype
135
  convert_host(Valtype v)
136
  { return bswap_32(v); }
137
};
138
 
139
template<>
140
struct Convert_endian<64, false>
141
{
142
  typedef Valtype_base<64>::Valtype Valtype;
143
 
144
  static inline Valtype
145
  convert_host(Valtype v)
146
  { return bswap_64(v); }
147
};
148
 
149
// Convert is a template based on size and on whether the target is
150
// big endian.  It defines Valtype and convert_host like
151
// Convert_endian.  That is, it is just like Convert_endian except in
152
// the meaning of the second template parameter.
153
 
154
template<int size, bool big_endian>
155
struct Convert
156
{
157
  typedef typename Valtype_base<size>::Valtype Valtype;
158
 
159
  static inline Valtype
160
  convert_host(Valtype v)
161
  {
162
    return Convert_endian<size, big_endian == Endian::host_big_endian>
163
      ::convert_host(v);
164
  }
165
};
166
 
167
// Swap is a template based on size and on whether the target is big
168
// endian.  It defines the type Valtype and the functions readval and
169
// writeval.  The functions read and write values of the appropriate
170
// size out of buffers, swapping them if necessary.  readval and
171
// writeval are overloaded to take pointers to the appropriate type or
172
// pointers to unsigned char.
173
 
174
template<int size, bool big_endian>
175
struct Swap
176
{
177
  typedef typename Valtype_base<size>::Valtype Valtype;
178
 
179
  static inline Valtype
180
  readval(const Valtype* wv)
181
  { return Convert<size, big_endian>::convert_host(*wv); }
182
 
183
  static inline void
184
  writeval(Valtype* wv, Valtype v)
185
  { *wv = Convert<size, big_endian>::convert_host(v); }
186
 
187
  static inline Valtype
188
  readval(const unsigned char* wv)
189
  { return readval(reinterpret_cast<const Valtype*>(wv)); }
190
 
191
  static inline void
192
  writeval(unsigned char* wv, Valtype v)
193
  { writeval(reinterpret_cast<Valtype*>(wv), v); }
194
};
195
 
196
// We need to specialize the 8-bit version of Swap to avoid
197
// conflicting overloads, since both versions of readval and writeval
198
// will have the same type parameters.
199
 
200
template<bool big_endian>
201
struct Swap<8, big_endian>
202
{
203
  typedef typename Valtype_base<8>::Valtype Valtype;
204
 
205
  static inline Valtype
206
  readval(const Valtype* wv)
207
  { return *wv; }
208
 
209
  static inline void
210
  writeval(Valtype* wv, Valtype v)
211
  { *wv = v; }
212
};
213
 
214
// Swap_unaligned is a template based on size and on whether the
215
// target is big endian.  It defines the type Valtype and the
216
// functions readval and writeval.  The functions read and write
217
// values of the appropriate size out of buffers which may be
218
// misaligned.
219
 
220
template<int size, bool big_endian>
221
struct Swap_unaligned;
222
 
223
template<bool big_endian>
224
struct Swap_unaligned<8, big_endian>
225
{
226
  typedef typename Valtype_base<8>::Valtype Valtype;
227
 
228
  static inline Valtype
229
  readval(const unsigned char* wv)
230
  { return *wv; }
231
 
232
  static inline void
233
  writeval(unsigned char* wv, Valtype v)
234
  { *wv = v; }
235
};
236
 
237
template<>
238
struct Swap_unaligned<16, false>
239
{
240
  typedef Valtype_base<16>::Valtype Valtype;
241
 
242
  static inline Valtype
243
  readval(const unsigned char* wv)
244
  {
245
    return (wv[1] << 8) | wv[0];
246
  }
247
 
248
  static inline void
249
  writeval(unsigned char* wv, Valtype v)
250
  {
251
    wv[1] = v >> 8;
252
    wv[0] = v;
253
  }
254
};
255
 
256
template<>
257
struct Swap_unaligned<16, true>
258
{
259
  typedef Valtype_base<16>::Valtype Valtype;
260
 
261
  static inline Valtype
262
  readval(const unsigned char* wv)
263
  {
264
    return (wv[0] << 8) | wv[1];
265
  }
266
 
267
  static inline void
268
  writeval(unsigned char* wv, Valtype v)
269
  {
270
    wv[0] = v >> 8;
271
    wv[1] = v;
272
  }
273
};
274
 
275
template<>
276
struct Swap_unaligned<32, false>
277
{
278
  typedef Valtype_base<32>::Valtype Valtype;
279
 
280
  static inline Valtype
281
  readval(const unsigned char* wv)
282
  {
283
    return (wv[3] << 24) | (wv[2] << 16) | (wv[1] << 8) | wv[0];
284
  }
285
 
286
  static inline void
287
  writeval(unsigned char* wv, Valtype v)
288
  {
289
    wv[3] = v >> 24;
290
    wv[2] = v >> 16;
291
    wv[1] = v >> 8;
292
    wv[0] = v;
293
  }
294
};
295
 
296
template<>
297
struct Swap_unaligned<32, true>
298
{
299
  typedef Valtype_base<32>::Valtype Valtype;
300
 
301
  static inline Valtype
302
  readval(const unsigned char* wv)
303
  {
304
    return (wv[0] << 24) | (wv[1] << 16) | (wv[2] << 8) | wv[3];
305
  }
306
 
307
  static inline void
308
  writeval(unsigned char* wv, Valtype v)
309
  {
310
    wv[0] = v >> 24;
311
    wv[1] = v >> 16;
312
    wv[2] = v >> 8;
313
    wv[3] = v;
314
  }
315
};
316
 
317
template<>
318
struct Swap_unaligned<64, false>
319
{
320
  typedef Valtype_base<64>::Valtype Valtype;
321
 
322
  static inline Valtype
323
  readval(const unsigned char* wv)
324
  {
325
    return ((static_cast<Valtype>(wv[7]) << 56)
326
            | (static_cast<Valtype>(wv[6]) << 48)
327
            | (static_cast<Valtype>(wv[5]) << 40)
328
            | (static_cast<Valtype>(wv[4]) << 32)
329
            | (static_cast<Valtype>(wv[3]) << 24)
330
            | (static_cast<Valtype>(wv[2]) << 16)
331
            | (static_cast<Valtype>(wv[1]) << 8)
332
            | static_cast<Valtype>(wv[0]));
333
  }
334
 
335
  static inline void
336
  writeval(unsigned char* wv, Valtype v)
337
  {
338
    wv[7] = v >> 56;
339
    wv[6] = v >> 48;
340
    wv[5] = v >> 40;
341
    wv[4] = v >> 32;
342
    wv[3] = v >> 24;
343
    wv[2] = v >> 16;
344
    wv[1] = v >> 8;
345
    wv[0] = v;
346
  }
347
};
348
 
349
template<>
350
struct Swap_unaligned<64, true>
351
{
352
  typedef Valtype_base<64>::Valtype Valtype;
353
 
354
  static inline Valtype
355
  readval(const unsigned char* wv)
356
  {
357
    return ((static_cast<Valtype>(wv[0]) << 56)
358
            | (static_cast<Valtype>(wv[1]) << 48)
359
            | (static_cast<Valtype>(wv[2]) << 40)
360
            | (static_cast<Valtype>(wv[3]) << 32)
361
            | (static_cast<Valtype>(wv[4]) << 24)
362
            | (static_cast<Valtype>(wv[5]) << 16)
363
            | (static_cast<Valtype>(wv[6]) << 8)
364
            | static_cast<Valtype>(wv[7]));
365
  }
366
 
367
  static inline void
368
  writeval(unsigned char* wv, Valtype v)
369
  {
370
    wv[0] = v >> 56;
371
    wv[1] = v >> 48;
372
    wv[2] = v >> 40;
373
    wv[3] = v >> 32;
374
    wv[4] = v >> 24;
375
    wv[5] = v >> 16;
376
    wv[6] = v >> 8;
377
    wv[7] = v;
378
  }
379
};
380
 
381
} // End namespace elfcpp.
382
 
383
#endif // !defined(ELFCPP_SWAP_H)

powered by: WebSVN 2.1.0

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