1 // $Header$
2 //
3 // Copyright (C) 2002 - 2007, by
4 //
5 // Carlo Wood, Run on IRC <carlo@alinoe.com>
6 // RSA-1024 0x624ACAD5 1997-01-26 Sign & Encrypt
7 // Fingerprint16 = 32 EC A7 B6 AC DB 65 A6 F6 F6 55 DD 1C DC FF 61
8 //
9 // This file may be distributed under the terms of the Q Public License
10 // version 1.0 as appearing in the file LICENSE.QPL included in the
11 // packaging of this file.
12 //
13
14 /*!
15 \addtogroup group_demangle demangle_type() and demangle_symbol()
16 \ingroup group_type_info
17
18 Libcwd comes with its own demangler functions.
19
20 demangle_type() writes the \em mangled type name \p input
21 to the string \p output; \p input should be the mangled name
22 as returned by <CODE>typeid(OBJECT).name()</CODE> (using gcc-2.95.1 or higher).
23
24 demangle_symbol() writes the \em mangled symbol name \p input
25 to the string \p output; \p input should be the mangled name
26 as returned by <CODE>elfxx::asymbol_st::name</CODE>
27 which is what is returned by
28 \ref libcwd::location_ct::mangled_function_name "location_ct::mangled_function_name()"
29 and pc_mangled_function_name().
30
31 The direct use of these functions should be avoided, instead use the function type_info_of().
32
33 */
34
35 //
36 // This file has been tested with gcc-3.x
37 //
38 // The description of how the mangling is done in the new ABI was found on
39 // http://www.codesourcery.com/cxx-abi/abi.html#mangling revision 1.74.
40 //
41 // To compile a standalone demangler:
42 // g++ -g -DSTANDALONE -DCWDEBUG -Iinclude demangle3.cc -Wl,--rpath,`pwd`/.libs -L.libs -lcwd -o c++filt
43 //
44
45 #include "sys.h"
46 #include "cwd_debug.h"
47 #include <libcwd/demangle.h>
48 #include <libcwd/private_assert.h>
49
50 #include <limits>
51
52 #ifdef STANDALONE
53 #ifdef CWDEBUG
54 namespace libcwd {
55 namespace channels {
56 namespace dc {
57 channel_ct demangler("DEMANGLER");
58 } // namespace dc
59 } // namespace channels
60 } // namespace libcwd
61 #endif
62 #define _GLIBCXX_DEMANGLER_DEBUG(x) __Debug(x)
63 #define _GLIBCXX_DEMANGLER_DOUT(cntrl, data) __Dout(cntrl, data)
64 #define _GLIBCXX_DEMANGLER_DOUT_ENTERING(x) \
65 __Dout(dc::demangler|continued_cf|flush_cf, "Entering " << x << "(\"" << &M_str[M_pos] << "\", \"" << output << "\") ")
66 #define _GLIBCXX_DEMANGLER_RETURN \
67 do { \
68 if (M_result) \
69 __Dout(dc::finish, '[' << M_pos << "; \"" << output << "\"]" ); \
70 else \
71 __Dout(dc::finish, "(failed)"); return M_result; \
72 } while(0)
73 #define _GLIBCXX_DEMANGLER_FAILURE \
74 do { \
75 if (M_result) \
76 { \
77 M_result = false; \
78 __Dout(dc::finish, "[failure]"); \
79 } \
80 else \
81 __Dout(dc::finish, "(failed)"); \
82 return false; \
83 } while(0)
84 #define _GLIBCXX_DEMANGLER_DOUT_ENTERING2(x) \
85 do { \
86 __Dout(dc::demangler|continued_cf|flush_cf, "Entering " << x); \
87 if (qualifiers) \
88 __Dout(dc::continued, " [with qualifiers: " << *qualifiers << ']'); \
89 __Dout(dc::continued, "(\"" << &M_str[M_pos] << "\", \"" << prefix << "\", \"" << postfix << "\") "); \
90 } while(0)
91 #define _GLIBCXX_DEMANGLER_RETURN2 \
92 do { \
93 if (M_result) \
94 __Dout(dc::finish, '[' << M_pos << "; \"" << prefix << "\", \"" << postfix << "\"]" ); \
95 else \
96 __Dout(dc::finish, "(failed)"); \
97 return M_result; \
98 } while(0)
99 #define _GLIBCXX_DEMANGLER_DOUT_ENTERING3(x) \
100 do { \
101 __Dout(dc::demangler|continued_cf|flush_cf, "Entering " << x); \
102 __Dout(dc::continued, " [with qualifier list: " << *this << ']'); \
103 __Dout(dc::continued, " (\"" << prefix << "\", \"" << postfix << "\") "); \
104 } while(0)
105 #define _GLIBCXX_DEMANGLER_RETURN3 \
106 do { \
107 __Dout(dc::finish, "[\"" << prefix << "\", \"" << postfix << "\"]" ); \
108 return; \
109 } while(0)
110 #endif // STANDALONE
111
112 #include <demangle.h> // The actual demangler code.
113
114 namespace
115 {
116
117 // class decimal_float
118 //
119 // This internal class represents a floating point value
120 // with a precision of `mantissa_size_c' * 4 digits.
121 // With `mantissa_size_c' set to 5 that gives 20 digits,
122 // being more than 66 bits. However, due to round off
123 // errors, the last `size of binary exponent' bits are
124 // unreliable.
125 //
126 // For IEEE double precision, the size of the exponent
127 // is 11 bits and thus is this class only accurate in
128 // 55 bits. Therefore, only 17 digits may be printed
129 // in that case. This size is precisely significant for
130 // the 52 bits of precision in the IEEE format as well.
131 //
132 // For IEEE single precision, the size of the exponent
133 // is 8 bits and we will have 58 bits of precision. It
134 // makes no sense to print more than 7 digits in that
135 // case because IEEE single precision has only 23 bits
136 // of precision by itself.
137 //
138 // The internal value of a `decimal_float' object is
139 //
140 // ( mantissa[0] +
141 // 10000 * mantissa[1] +
142 // 10000**2 * mantissa[2] +
143 // 10000**3 * mantissa[3] +
144 // 10000**4 * mantissa[4] ) * 10**exponent
145 //
146 // The member `max_precision_reached' is set to true
147 // when the value of the object cannot be multiplied
148 // with 10 anymore without changing the exponent,
149 // in other words, when mantissa[4] >= 1000.
150 // Its value is fuzzy however and only influences
151 // the maximum achievable accuracy.
152
153 static int const mantissa_size_c = 5;
154
155 struct decimal_float_data {
156 unsigned long mantissa[mantissa_size_c];
157 int exponent;
158 bool max_precision_reached;
159 };
160
161 class decimal_float {
162 private:
163 decimal_float_data M_data;
164
165 private:
166 void M_do_overflow(unsigned long prev_borrow);
167 void M_do_carry(void);
168
169 public:
170 void set_2pow2pow(int power);
171 void set_2pow(int power);
172 void set_expstart(int expsize);
173 void set_zero(void);
174 void divide_by_two(bool decrement_exponent);
175 bool decrement_exponent(void);
176 decimal_float& operator+=(decimal_float const& term);
177 decimal_float& operator*=(decimal_float const& factor);
178 void print_to_with_precision(char* buf, int precision) const;
179 };
180
181 // These names must match the corresponding index of the table below
182 // and the p* constants must be contigious and in the given order.
183 // Same for the constants m1023 ... m15.
184 // Name Table-index
185 int const m1023 = 0; // Corresponds with an exponent of 11 bits.
186 #if 0
187 int const m511 = 1;
188 int const m255 = 2;
189 int const m127 = 3; // Corresponds with an exponent of 8 bits.
190 int const m63 = 4;
191 int const m31 = 5;
192 int const m15 = 6;
193 #endif
194 int const m1 = 7;
195 int const m0 = 8; // Must have the same exponent as m1.
196 int const p1 = 9;
197 #if 0
198 int const p2 = 10;
199 int const p4 = 11;
200 int const p8 = 12;
201 int const p16 = 13;
202 int const p32 = 14;
203 int const p64 = 15;
204 int const p128 = 16;
205 int const p256 = 17;
206 int const p512 = 18;
207 int const p1024 = 19;
208 #endif
209
210 decimal_float_data const constants[] = {
211 { { 6915, 3600, 2925, 5369, 1112 }, -327, true }, // m1023; 2 ** -1023
212 { { 3487, 41, 4624, 6681, 1491 }, -173, true }, // m511; 2 ** -511
213 { { 9251, 8888, 1101, 2337, 1727 }, -96, true }, // m255; 2 ** -255
214 { { 5398, 1437, 5411, 4717, 5877 }, -58, true }, // m127; 2 ** -127
215 { { 4340, 5504, 7248, 2021, 1084 }, -38, true }, // m63; 2 ** -63
216 { { 5781, 7392, 7307, 6128, 4656 }, -29, true }, // m31; 2 ** -31
217 { { 0, 0, 1250, 7578, 3051 }, -24, true }, // m15; 2 ** -15
218 { { 5, 0, 0, 0, 0 }, -1, false }, // m1; 2 ** -1
219 { { 10, 0, 0, 0, 0 }, -1, false }, // m0; 2 ** 0
220 { { 2, 0, 0, 0, 0 }, 0, false }, // p1; 2 ** 1
221 { { 4, 0, 0, 0, 0 }, 0, false }, // p2; 2 ** 2
222 { { 16, 0, 0, 0, 0 }, 0, false }, // p4; 2 ** 4
223 { { 256, 0, 0, 0, 0 }, 0, false }, // p8; 2 ** 8
224 { { 5536, 6, 0, 0, 0 }, 0, false }, // p16; 2 ** 16
225 { { 7296, 9496, 42, 0, 0 }, 0, false }, // p32; 2 ** 32
226 { { 1616, 955, 737, 6744, 1844 }, 0, true }, // p64; 2 ** 64
227 { { 6346, 9384, 6920, 8236, 3402 }, 19, true }, // p128; 2 ** 128
228 { { 9542, 3161, 9237, 9208, 1157 }, 58, true }, // p256; 2 ** 256
229 { { 7100, 4259, 9299, 7807, 1340 }, 135, true }, // p512; 2 ** 512
230 { { 9077, 2315, 3486, 6931, 1797 }, 289, true } // p1024; 2 ** 1024
231 };
232
233 // Handle excess digits in the most significant
234 // element of the mantissa array.
M_do_overflow(unsigned long prev_borrow)235 void decimal_float::M_do_overflow(unsigned long prev_borrow)
236 {
237 // assert(M_data.mantissa[mantissa_size_c - 1] > 9999);
238
239 // Even after shifting the excess digits right,
240 // M_data.mantissa[mantissa_size_c - 1] will still be
241 // larger than 999.
242 M_data.max_precision_reached = true;
243
244 // Count the excess digits and update the exponent
245 // already in order to keep the value constant.
246 unsigned long divider = 10;
247 ++M_data.exponent;
248 while (prev_borrow >= divider)
249 {
250 divider *= 10;
251 ++M_data.exponent;
252 }
253
254 // Finally, shift the value in the mantissa a few
255 // digits to the right. Round off what falls off.
256 unsigned long multiplier = 10000 / divider;
257 for (int i = mantissa_size_c - 1; i >= 0; --i)
258 {
259 unsigned long borrow = M_data.mantissa[i] % divider;
260 if (i == 0)
261 M_data.mantissa[i] += divider / 2; // Round off.
262 M_data.mantissa[i] /= divider;
263 M_data.mantissa[i] += prev_borrow * multiplier;
264 prev_borrow = borrow;
265 }
266 }
267
268 // Handle excess digits in the elements of the mantissa array.
M_do_carry(void)269 void decimal_float::M_do_carry(void)
270 {
271 for (int i = 0; i < mantissa_size_c - 1; ++i)
272 {
273 if (M_data.mantissa[i] >= 10000)
274 {
275 M_data.mantissa[i + 1] += M_data.mantissa[i] / 10000;
276 M_data.mantissa[i] %= 10000;
277 }
278 }
279 if (M_data.mantissa[mantissa_size_c - 1] >= 10000)
280 M_do_overflow(0);
281 }
282
283 // Initialize the object to a value of 2 ** (2 ** power).
set_2pow2pow(int power)284 inline void decimal_float::set_2pow2pow(int power)
285 {
286 // assert(0 <= power && power <= 10);
287 std::memcpy(&M_data, &constants[p1 + power], sizeof(decimal_float_data));
288 }
289
290 // Initialize the object to a value of 2 ** power.
set_2pow(int power)291 inline void decimal_float::set_2pow(int power)
292 {
293 // assert(power == 0 || power == 1);
294 std::memcpy(&M_data, &constants[power], sizeof(decimal_float_data));
295 }
296
297 // Initialize the object to a value of 2 ** (1 - (2 ** (expsize - 1))).
set_expstart(int expsize)298 inline void decimal_float::set_expstart(int expsize)
299 {
300 // assert(expsize <= 11 && expsize >= 5);
301 std::memcpy(&M_data, &constants[m1023 + 11 - expsize],
302 sizeof(decimal_float_data));
303 }
304
305 // Initialize the object to zero. Must have the same exponent as m0.
set_zero(void)306 inline void decimal_float::set_zero(void)
307 {
308 std::memcpy(&M_data, &constants[m0], sizeof(decimal_float_data));
309 M_data.mantissa[0] = 0;
310 }
311
312 // Divide the value of this object by two in one of two possible ways:
313 // If `decrement_exponent' is true - then decrement the exponent by
314 // one and multiply the mantissa with five. Otherwise just divide
315 // the mantissa by two.
divide_by_two(bool decrement_exponent)316 void decimal_float::divide_by_two(bool decrement_exponent)
317 {
318 if (decrement_exponent)
319 {
320 for (int i = 0; i < mantissa_size_c; ++i)
321 M_data.mantissa[i] *= 5;
322 M_do_carry();
323 --M_data.exponent;
324 }
325 else
326 {
327 unsigned long prev_borrow = M_data.mantissa[mantissa_size_c - 1] % 2;
328 if ((M_data.mantissa[mantissa_size_c - 1] /= 2) < 1000)
329 M_data.max_precision_reached = false;
330 for (int i = mantissa_size_c - 2; i >= 0; --i)
331 {
332 unsigned long borrow = M_data.mantissa[i] % 2;
333 M_data.mantissa[i] /= 2;
334 M_data.mantissa[i] += prev_borrow * 5000;
335 prev_borrow = borrow;
336 }
337 if (prev_borrow)
338 {
339 // Round off.
340 if (++M_data.mantissa[0] == 10000)
341 M_do_carry();
342 }
343 }
344 }
345
346 // If possible, multiply the mantissa with 10 and
347 // decrement the exponent with one. This is used
348 // to keep the exponent of this object equal to
349 // another object that is being divided by two.
decrement_exponent(void)350 bool decimal_float::decrement_exponent(void)
351 {
352 if (M_data.max_precision_reached)
353 return false;
354 for (int i = 0; i < mantissa_size_c; ++i)
355 M_data.mantissa[i] *= 10;
356 M_do_carry();
357 if (M_data.mantissa[mantissa_size_c - 1] >= 1000)
358 M_data.max_precision_reached = true;
359 --M_data.exponent;
360 return true;
361 }
362
363 // Add two decimal_floats that have the same exponent.
operator +=(decimal_float const & term)364 decimal_float& decimal_float::operator+=(decimal_float const& term)
365 {
366 // assert(M_data.exponent == term.M_data.exponent);
367 for (int i = 0; i < mantissa_size_c; ++i)
368 M_data.mantissa[i] += term.M_data.mantissa[i];
369 M_do_carry();
370 return *this;
371 }
372
373 // The real work. Multiply this object with `factor'.
operator *=(decimal_float const & factor)374 decimal_float& decimal_float::operator*=(decimal_float const& factor)
375 {
376 // Count the number of most-significant mantissa elements (digits)
377 // that contain zero (of either factor), but stop counting if we reach
378 // mantissa_size_c - 1.
379 int zero_digits = 0;
380 while (M_data.mantissa[mantissa_size_c - 1 - zero_digits] == 0)
381 if (++zero_digits == mantissa_size_c - 1)
382 break;
383 if (zero_digits < mantissa_size_c - 1)
384 {
385 int offset = mantissa_size_c - 1 + zero_digits;
386 while (factor.M_data.mantissa[offset - zero_digits] == 0)
387 if (++zero_digits == mantissa_size_c - 1)
388 break;
389 }
390
391 // Set `this_mantissa' to point to the mantissa of this object;
392 // make a copy only when we would overwrite its values when
393 // still needed below.
394 unsigned long tmp_mantissa[mantissa_size_c];
395 unsigned long* this_mantissa;
396 if (zero_digits == 0)
397 this_mantissa = M_data.mantissa;
398 else
399 {
400 std::memcpy(tmp_mantissa, M_data.mantissa, sizeof(tmp_mantissa));
401 this_mantissa = tmp_mantissa;
402 }
403
404 // Set lss (least significant (loop) size, but don't ask me why it is
405 // called that way - it gets a bit complicated here) to, heh, what works.
406 int lss = mantissa_size_c - 1 - zero_digits;
407 // Now hold your breath...
408 M_data.exponent += factor.M_data.exponent + 4 * lss;
409 unsigned long sum = 0;
410 for (int i = 0; i < lss; ++i)
411 sum += this_mantissa[i] * factor.M_data.mantissa[lss - 1 - i];
412 sum += 5000; // Round off.
413 sum /= 10000;
414 for (int j = 0; j < mantissa_size_c; ++j)
415 {
416 int loop_bgn = std::max(0, lss + j - (mantissa_size_c - 1));
417 int loop_end = std::min(mantissa_size_c - 1, lss + j);
418 for (int i = loop_bgn; i <= loop_end; ++i)
419 sum += this_mantissa[i] * factor.M_data.mantissa[lss + j - i];
420 M_data.mantissa[j] = sum;
421 sum /= 10000;
422 M_data.mantissa[j] -= 10000 * sum;
423 }
424 // ... ahhh. Ok. And do overflow management when needed.
425 if (sum > 0)
426 M_do_overflow(sum);
427 return *this;
428 }
429
430 // Print to `buf', writes: "<digit>.<digits>e<sign><exponent>\0".
431 // sizeof(buf) must be at least 7 larger than `precision':
432 // strlen(<digit><digits>) <= precision.
433 // strlen(.e<sign><exponent>) == 6, plus one for the trailing '\0'.
print_to_with_precision(char * buf,int precision) const434 void decimal_float::print_to_with_precision(char* buf, int precision) const
435 {
436 // First do the round off.
437 decimal_float tmp(*this);
438 if (!M_data.max_precision_reached)
439 {
440 // In this case we are probably exact, so we
441 // don't really need to round off. Because leading
442 // zeroes are not printed, add number of leading 0's
443 // to `precision'.
444 for (int i = mantissa_size_c - 1; i >= 0; --i)
445 for(unsigned long shift = 1000; shift; shift /= 10)
446 {
447 if (M_data.mantissa[i] >= shift)
448 {
449 i = 0;
450 break;
451 }
452 ++precision;
453 }
454 }
455 int e = 4 * mantissa_size_c - 1;
456 if (precision < mantissa_size_c * 4)
457 {
458 int cut = e - precision;
459 int cuti = cut / 4;
460 int cutr = cut % 4;
461 unsigned long shift = 10;
462 while(cutr--)
463 shift *= 10;
464 tmp.M_data.mantissa[cuti] += shift / 2; // Round off.
465 if (tmp.M_data.mantissa[cuti] >= 10000)
466 tmp.M_do_carry();
467 }
468
469 bool leading = true;
470 char* p = buf;
471 int tz = 0;
472 for (int i = mantissa_size_c - 1; i >= 0 && precision; --i)
473 {
474 unsigned long digit = tmp.M_data.mantissa[i];
475 for(int shift = 1000; shift; shift /= 10)
476 {
477 int d = digit / shift;
478 digit -= d * shift;
479 if (leading && d != 0)
480 leading = false;
481 if (leading) // Suppress leading zeroes.
482 --e;
483 else
484 {
485 if (d == 0) // Suppress and count trailing zeroes.
486 ++tz;
487 else
488 {
489 if (p == buf + 1)
490 *p++ = '.';
491 while(tz--)
492 *p++ = '0';
493 *p++ = d + '0';
494 tz = 0;
495 }
496 if (--precision == 0)
497 break;
498 }
499 }
500 }
501 e += tmp.M_data.exponent;
502 if (e != 0)
503 {
504 *p++ = 'e';
505 if (e > 0)
506 *p++ = '+';
507 else
508 {
509 *p++ = '-';
510 e = -e;
511 }
512 int shift = 100;
513 leading = true;
514 while(shift)
515 {
516 int d = e / shift;
517 e -= d * shift;
518 if (leading && d != 0)
519 leading = false;
520 if (!leading)
521 *p++ = '0' + d;
522 shift /= 10;
523 }
524 }
525 *p = 0;
526 }
527
528 // print_IEEE_fp(buf, words, nbits_exponent, nbits_fraction, precision)
529 //
530 // buf : Output buffer, the result is written to this buffer
531 // and terminated with a trailing '\0'. The size of
532 // this buffer must be at least `precision + 7'.
533 // words : A pointer to an array with unsigned integers. Only
534 // the 32 least significant bits of each element are
535 // used. The first element contains the word with the
536 // most significant bits. Bit 31 in each element
537 // represents the most significant bit of that 32-bit
538 // word. See the description of IEEE 754-1985 for
539 // the exact meaning. Basically they mean:
540 // SEEEEEEEEMMMMMMMMMMMMMMMMMMMM, where S is the sign
541 // bit, EEEEEEEE the exponent and MMMMMMMMMMMMMMMMMMMM
542 // the mantissa.
543 // nbits_exponent : The number of bits in `words' that represent the
544 // exponent.
545 // nbits_fraction : The total number of bits in `words' representing the
546 // mantissa, a value larger than 52 will result in
547 // inaccurate output.
548 // precision : The maximum number of digits in the mantissa
549 // (including the first digit before the dot) that
550 // will be written to `buf'. This precision should
551 // correspond to the accuracy given by `nbits_fraction'
552 // of course.
553
print_IEEE_fp(char * buf,unsigned long * words,int nbits_exponent,int nbits_fraction,int precision)554 void print_IEEE_fp(char* buf, unsigned long* words,
555 int nbits_exponent, int nbits_fraction, int precision)
556 {
557 // This function was written using
558 // http://www.psc.edu/general/software/packages/ieee/ieee.html
559 // as reference for the IEEE 754-1985 standard.
560
561 // assert( nbits_fraction <= 52 && nbits_exponent <= 11 &&
562 // nbits_exponent >= 5 && precision <= 17 );
563 unsigned long sign_mask = 1 << nbits_exponent;
564 unsigned long exponent_mask = sign_mask - 1;
565 unsigned long sign_and_exponent = words[0] >> (31 - nbits_exponent);
566 unsigned long exponent = sign_and_exponent & exponent_mask;
567 int nelem_fraction = (nbits_fraction + nbits_exponent) / 32 + 1;
568 unsigned long mask_first_elem = 0xffffffffUL >> (nbits_exponent + 1);
569 unsigned long mask_last_elem = ~((1UL << (nelem_fraction * 32 -
570 nbits_fraction - nbits_exponent - 1)) - 1);
571 unsigned long fraction_nonzero = (words[0] & mask_first_elem);
572 if (nelem_fraction == 1)
573 fraction_nonzero &= mask_last_elem;
574 else
575 {
576 fraction_nonzero |= (words[nelem_fraction - 1] & mask_last_elem);
577 for (int i = 1; i < nelem_fraction - 1; ++i)
578 fraction_nonzero |= words[i];
579 }
580 if (exponent == exponent_mask && fraction_nonzero)
581 {
582 strcpy(buf, "nan");
583 return;
584 }
585 if ((sign_and_exponent & sign_mask))
586 *buf++ = '-';
587 if (exponent == exponent_mask && !fraction_nonzero)
588 {
589 strcpy(buf, "inf");
590 return;
591 }
592 bool normalized = true;
593 if (exponent == 0)
594 {
595 if (!fraction_nonzero)
596 {
597 strcpy(buf, "0");
598 return;
599 }
600 else
601 {
602 normalized = false;
603 exponent = 1;
604 }
605 }
606
607 decimal_float result;
608 if (normalized)
609 result.set_2pow(m0); // 1.0 ; The 1 in (1.F)
610 else
611 result.set_zero(); // 0.0 ; The 0 in (0.F)
612 decimal_float bit;
613 bit.set_2pow(m1); // 0.5 ; 2 ** (-bit_cnt - 1)
614 unsigned long mask = 0x40000000 >> nbits_exponent;
615 for (int bit_cnt = 0; bit_cnt < nbits_fraction; ++bit_cnt)
616 {
617 if ((*words & mask))
618 result += bit;
619 bool success = result.decrement_exponent();
620 bit.divide_by_two(success); // Keep exponents equal for easy addition.
621 if (!(mask >>= 1))
622 {
623 mask = 0x80000000;
624 ++words;
625 }
626 }
627 decimal_float two_pow_exponent;
628 two_pow_exponent.set_expstart(nbits_exponent);
629 // 2 ** (1 - 2 ** (nbits_exponent - 1)),
630 // where 1 - 2 ** (nbits_exponent - 1) is
631 // the two's-complement exponent 'offset'.
632 mask = 1 << (nbits_exponent - 1);
633 for (int bit_cnt = nbits_exponent - 1; bit_cnt >= 0; --bit_cnt)
634 {
635 if ((exponent & mask))
636 {
637 bit.set_2pow2pow(bit_cnt); // 2 ** (2 ** bit_cnt)
638 two_pow_exponent *= bit;
639 }
640 mask >>= 1;
641 }
642 result *= two_pow_exponent;
643 result.print_to_with_precision(buf, precision);
644 }
645
646 } // namespace
647
648 namespace libcwd {
649
650 #ifdef STANDALONE
651 static char const* main_in;
652 #endif
653
654 namespace _private_ {
655
656 class implementation_details : public __gnu_cxx::demangler::implementation_details {
657 public:
implementation_details(unsigned int flags)658 implementation_details(unsigned int flags) : __gnu_cxx::demangler::implementation_details(flags) { }
659 protected:
660 virtual bool
661 decode_real(char* output, unsigned long* input, size_t size_of_real) const;
662 };
663
664 // GNU/Linux uses IEEE encoding for floats.
665 bool
decode_real(char * output,unsigned long * input,size_t size_of_real) const666 implementation_details::decode_real(char* output, unsigned long* input, size_t size_of_real) const
667 {
668 if (size_of_real != 4 && size_of_real !=8) // Only decode single and double precision IEEE.
669 return false;
670
671 if (size_of_real == 8)
672 print_IEEE_fp(output, input, 11, 52, 17);
673 else
674 print_IEEE_fp(output, input, 8, 23, 8);
675
676 return true;
677 }
678
679 #if CWDEBUG_ALLOC
680 typedef __gnu_cxx::demangler::session<internal_allocator> session_type;
681 #else
682 typedef __gnu_cxx::demangler::session<std::allocator<char> > session_type;
683 #endif
684
685 //
686 // demangle_symbol
687 //
688 // Demangle `input' and append to `output'.
689 // `input' should be a mangled_function_name as for instance returned
690 // by `libcwd::pc_mangled_function_name'.
691 //
demangle_symbol(char const * input,internal_string & output)692 void demangle_symbol(char const* input, internal_string& output)
693 {
694 #if CWDEBUG_DEBUGM
695 LIBCWD_TSD_DECLARATION;
696 LIBCWD_ASSERT( __libcwd_tsd.internal );
697 #endif
698
699 #ifdef STANDALONE
700 if (input != main_in)
701 Debug( dc::demangler.off() );
702 #endif
703
704 _GLIBCXX_DEMANGLER_DOUT(dc::demangler, "Entering demangle_symbol(\"" << input << "\")");
705
706 if (input == NULL)
707 {
708 output += "(null)";
709 #ifdef STANDALONE
710 if (input != main_in)
711 Debug( dc::demangler.on() );
712 #endif
713 return;
714 }
715
716 //
717 // <mangled-name> ::= _Z <encoding>
718 // <mangled-name> ::= _GLOBAL_ _<type>_ <disambiguation part> // GNU extension, <type> can be I or D.
719 //
720 bool failure = (input[0] != '_');
721
722 if (!failure)
723 {
724 if (input[1] == 'Z')
725 {
726 // C++ name?
727 implementation_details id(implementation_details::style_void);
728 int cnt = session_type::decode_encoding(output, input + 2, INT_MAX, id);
729 if (cnt < 0 || input[cnt + 2] != 0)
730 failure = true;
731 }
732 else if (input[1] == 'G')
733 {
734 // Possible _GLOBAL__ extension?
735 if (!std::strncmp(input, "_GLOBAL__", 9) && (input[9] == 'D' || input[9] == 'I') && input[10] == '_')
736 {
737 if (input[9] == 'D')
738 output.assign("global destructors keyed to ", 28);
739 else
740 output.assign("global constructors keyed to ", 29);
741 // Output the disambiguation part as-is.
742 output += input + 11;
743 }
744 else
745 failure = true;
746 }
747 else
748 failure = true;
749 }
750
751 if (failure)
752 output.assign(input, strlen(input)); // Failure to demangle, return the mangled name.
753
754 #ifdef STANDALONE
755 if (input != main_in)
756 Debug( dc::demangler.on() );
757 #endif
758 }
759
760 //
761 // demangle_type
762 //
763 // Demangle `input' and append to `output'.
764 // `input' should be a mangled type as for returned
765 // by the stdc++ `typeinfo::name()'.
766 //
demangle_type(char const * input,internal_string & output)767 void demangle_type(char const* input, internal_string& output)
768 {
769 #if CWDEBUG_DEBUGM
770 LIBCWD_TSD_DECLARATION;
771 LIBCWD_ASSERT( __libcwd_tsd.internal );
772 #endif
773 #ifdef STANDALONE
774 if (input != main_in)
775 Debug( dc::demangler.off() );
776 #endif
777 _GLIBCXX_DEMANGLER_DOUT(dc::demangler, "Entering demangle_type(\"" << input << "\")");
778 if (input == NULL)
779 {
780 output += "(null)";
781 #ifdef STANDALONE
782 if (input != main_in)
783 Debug( dc::demangler.on() );
784 #endif
785 return;
786 }
787 implementation_details id(implementation_details::style_void);
788 session_type demangler_session(input, INT_MAX, id);
789 if (!demangler_session.decode_type(output) || demangler_session.remaining_input_characters())
790 output.assign(input, strlen(input)); // Failure to demangle, return the mangled type name.
791 #ifdef STANDALONE
792 if (input != main_in)
793 Debug( dc::demangler.on() );
794 #endif
795 }
796
797 } // namespace _private_
798 } // namespace libcwd
799
800 #ifdef STANDALONE
801 #include <iostream>
802
main(int argc,char * argv[])803 int main(int argc, char* argv[])
804 {
805 Debug( libcw_do.on() );
806 Debug( dc::demangler.on() );
807 std::string out;
808 libcwd::main_in = argv[1];
809 libcwd::demangle_symbol(argv[1], out);
810 std::cout << out << std::endl;
811 return 0;
812 }
813
814 #endif // STANDALONE
815
816 namespace libcwd {
817
818 /** \addtogroup group_demangle */
819 /** \{ */
820
821 /**
822 * \brief Demangle mangled symbol name \p input and write the result to string \p output.
823 */
demangle_symbol(char const * input,std::string & output)824 void demangle_symbol(char const* input, std::string& output)
825 {
826 LIBCWD_TSD_DECLARATION;
827 _private_::set_alloc_checking_off(LIBCWD_TSD);
828 {
829 _private_::internal_string result;
830 _private_::demangle_symbol(input, result);
831 _private_::set_alloc_checking_on(LIBCWD_TSD);
832 output.append(result.data(), result.size());
833 _private_::set_alloc_checking_off(LIBCWD_TSD);
834 }
835 _private_::set_alloc_checking_on(LIBCWD_TSD);
836 }
837
838 /**
839 * \brief Demangle mangled type name \p input and write the result to string \p output.
840 */
demangle_type(char const * input,std::string & output)841 void demangle_type(char const* input, std::string& output)
842 {
843 LIBCWD_TSD_DECLARATION;
844 _private_::set_alloc_checking_off(LIBCWD_TSD);
845 {
846 _private_::internal_string result;
847 _private_::demangle_type(input, result);
848 _private_::set_alloc_checking_on(LIBCWD_TSD);
849 output.append(result.data(), result.size());
850 _private_::set_alloc_checking_off(LIBCWD_TSD);
851 }
852 _private_::set_alloc_checking_on(LIBCWD_TSD);
853 }
854
855 /** \} */
856
857 } // namespace libcwd
858
859
860