stor-layout.c (finish_builtin_struct): Copy fields into the variants.
[gcc.git] / gcc / wide-int-print.cc
1 /* Printing operations with very long integers.
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 Contributed by Kenneth Zadeck <zadeck@naturalbridge.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "hwint.h"
26 #include "wide-int.h"
27 #include "wide-int-print.h"
28
29 /*
30 * public printing routines.
31 */
32
33 #define BLOCKS_NEEDED(PREC) \
34 (((PREC) + HOST_BITS_PER_WIDE_INT - 1) / HOST_BITS_PER_WIDE_INT)
35
36 void
37 print_dec (const wide_int_ref &wi, char *buf, signop sgn)
38 {
39 if (sgn == SIGNED)
40 print_decs (wi, buf);
41 else
42 print_decu (wi, buf);
43 }
44
45 void
46 print_dec (const wide_int_ref &wi, FILE *file, signop sgn)
47 {
48 if (sgn == SIGNED)
49 print_decs (wi, file);
50 else
51 print_decu (wi, file);
52 }
53
54
55 /* Try to print the signed self in decimal to BUF if the number fits
56 in a HWI. Other print in hex. */
57
58 void
59 print_decs (const wide_int_ref &wi, char *buf)
60 {
61 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
62 || (wi.get_len () == 1))
63 {
64 if (wi::neg_p (wi))
65 sprintf (buf, "-" HOST_WIDE_INT_PRINT_UNSIGNED, -wi.to_shwi ());
66 else
67 sprintf (buf, HOST_WIDE_INT_PRINT_DEC, wi.to_shwi ());
68 }
69 else
70 print_hex (wi, buf);
71 }
72
73 /* Try to print the signed self in decimal to FILE if the number fits
74 in a HWI. Other print in hex. */
75
76 void
77 print_decs (const wide_int_ref &wi, FILE *file)
78 {
79 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
80 print_decs (wi, buf);
81 fputs (buf, file);
82 }
83
84 /* Try to print the unsigned self in decimal to BUF if the number fits
85 in a HWI. Other print in hex. */
86
87 void
88 print_decu (const wide_int_ref &wi, char *buf)
89 {
90 if ((wi.get_precision () <= HOST_BITS_PER_WIDE_INT)
91 || (wi.get_len () == 1 && !wi::neg_p (wi)))
92 sprintf (buf, HOST_WIDE_INT_PRINT_UNSIGNED, wi.to_uhwi ());
93 else
94 print_hex (wi, buf);
95 }
96
97 /* Try to print the signed self in decimal to FILE if the number fits
98 in a HWI. Other print in hex. */
99
100 void
101 print_decu (const wide_int_ref &wi, FILE *file)
102 {
103 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
104 print_decu (wi, buf);
105 fputs (buf, file);
106 }
107
108 void
109 print_hex (const wide_int_ref &wi, char *buf)
110 {
111 int i = wi.get_len ();
112
113 if (wi == 0)
114 buf += sprintf (buf, "0x0");
115 else
116 {
117 if (wi::neg_p (wi))
118 {
119 int j;
120 /* If the number is negative, we may need to pad value with
121 0xFFF... because the leading elements may be missing and
122 we do not print a '-' with hex. */
123 buf += sprintf (buf, "0x");
124 for (j = BLOCKS_NEEDED (wi.get_precision ()); j > i; j--)
125 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, (HOST_WIDE_INT) -1);
126
127 }
128 else
129 buf += sprintf (buf, "0x"HOST_WIDE_INT_PRINT_HEX_PURE, wi.elt (--i));
130
131 while (--i >= 0)
132 buf += sprintf (buf, HOST_WIDE_INT_PRINT_PADDED_HEX, wi.elt (i));
133 }
134 }
135
136 /* Print one big hex number to FILE. Note that some assemblers may not
137 accept this for large modes. */
138 void
139 print_hex (const wide_int_ref &wi, FILE *file)
140 {
141 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
142 print_hex (wi, buf);
143 fputs (buf, file);
144 }
145