]>
Commit | Line | Data |
---|---|---|
1321be35 ER |
1 | --- ./gcc/doc/extend.texi.orig Sat Aug 6 15:26:27 2005 |
2 | +++ ./gcc/doc/extend.texi Mon Aug 22 00:14:05 2005 | |
3 | @@ -79,6 +79,7 @@ | |
4 | * Pragmas:: Pragmas accepted by GCC. | |
5 | * Unnamed Fields:: Unnamed struct/union fields within structs/unions. | |
6 | * Thread-Local:: Per-thread variables. | |
7 | +* Binary constants:: Binary constants using the @samp{0b} prefix. | |
8 | @end menu | |
9 | ||
10 | @node Statement Exprs | |
11 | @@ -9742,6 +9743,28 @@ | |
12 | Non-@code{static} members shall not be @code{__thread}. | |
13 | @end quotation | |
14 | @end itemize | |
15 | + | |
16 | +@node Binary constants | |
17 | +@section Binary constants using the @samp{0b} prefix | |
18 | +@cindex Binary constants using the @samp{0b} prefix | |
19 | + | |
20 | +Integer constants can be written as binary constants, consisting of a | |
21 | +sequence of @samp{0} and @samp{1} digits, prefixed by @samp{0b} or | |
22 | +@samp{0B}. This is particularly useful in environments that operate a | |
23 | +lot on the bit-level (like microcontrollers). | |
24 | + | |
25 | +The following statements are identical: | |
26 | + | |
27 | +@smallexample | |
28 | +i = 42; | |
29 | +i = 0x2a; | |
30 | +i = 052; | |
31 | +i = 0b101010; | |
32 | +@end smallexample | |
33 | + | |
34 | +The type of these constants follows the same rules as for octal or | |
35 | +hexadecimal integer constants, so suffixes like @samp{L} or @samp{UL} | |
36 | +can be applied. | |
37 | ||
38 | @node C++ Extensions | |
39 | @chapter Extensions to the C++ Language | |
40 | --- ./libcpp/include/cpplib.h.orig Wed Jun 29 04:34:39 2005 | |
41 | +++ ./libcpp/include/cpplib.h Mon Aug 22 00:14:05 2005 | |
42 | @@ -729,6 +729,7 @@ | |
43 | #define CPP_N_DECIMAL 0x0100 | |
44 | #define CPP_N_HEX 0x0200 | |
45 | #define CPP_N_OCTAL 0x0400 | |
46 | +#define CPP_N_BINARY 0x0800 | |
47 | ||
48 | #define CPP_N_UNSIGNED 0x1000 /* Properties. */ | |
49 | #define CPP_N_IMAGINARY 0x2000 | |
50 | --- ./libcpp/expr.c.orig Wed Jun 29 04:34:36 2005 | |
51 | +++ ./libcpp/expr.c Mon Aug 22 12:02:28 2005 | |
52 | @@ -171,6 +171,11 @@ | |
53 | radix = 16; | |
54 | str++; | |
55 | } | |
56 | + else if ((*str == 'b' || *str == 'B') && (str[1] == '0' || str[1] == '1')) | |
57 | + { | |
58 | + radix = 2; | |
59 | + str++; | |
60 | + } | |
61 | } | |
62 | ||
63 | /* Now scan for a well-formed integer or float. */ | |
64 | @@ -209,10 +214,22 @@ | |
65 | radix = 10; | |
66 | ||
67 | if (max_digit >= radix) | |
68 | - SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit); | |
69 | + { | |
70 | + if (radix == 2) | |
71 | + SYNTAX_ERROR2 ("invalid digit \"%c\" in binary constant", '0' + max_digit); | |
72 | + else | |
73 | + SYNTAX_ERROR2 ("invalid digit \"%c\" in octal constant", '0' + max_digit); | |
74 | + } | |
75 | ||
76 | if (float_flag != NOT_FLOAT) | |
77 | { | |
78 | + if (radix == 2) | |
79 | + { | |
80 | + cpp_error (pfile, CPP_DL_ERROR, | |
81 | + "invalid prefix \"0b\" for floating constant"); | |
82 | + return CPP_N_INVALID; | |
83 | + } | |
84 | + | |
85 | if (radix == 16 && CPP_PEDANTIC (pfile) && !CPP_OPTION (pfile, c99)) | |
86 | cpp_error (pfile, CPP_DL_PEDWARN, | |
87 | "use of C99 hexadecimal floating constant"); | |
88 | @@ -288,11 +305,16 @@ | |
89 | if ((result & CPP_N_IMAGINARY) && CPP_PEDANTIC (pfile)) | |
90 | cpp_error (pfile, CPP_DL_PEDWARN, | |
91 | "imaginary constants are a GCC extension"); | |
92 | + if (radix == 2 && CPP_PEDANTIC (pfile)) | |
93 | + cpp_error (pfile, CPP_DL_PEDWARN, | |
94 | + "binary constants are a GCC extension"); | |
95 | ||
96 | if (radix == 10) | |
97 | result |= CPP_N_DECIMAL; | |
98 | else if (radix == 16) | |
99 | result |= CPP_N_HEX; | |
100 | + else if (radix == 2) | |
101 | + result |= CPP_N_BINARY; | |
102 | else | |
103 | result |= CPP_N_OCTAL; | |
104 | ||
105 | @@ -343,6 +365,11 @@ | |
106 | base = 16; | |
107 | p += 2; | |
108 | } | |
109 | + else if ((type & CPP_N_RADIX) == CPP_N_BINARY) | |
110 | + { | |
111 | + base = 2; | |
112 | + p += 2; | |
113 | + } | |
114 | ||
115 | /* We can add a digit to numbers strictly less than this without | |
116 | needing the precision and slowness of double integers. */ | |
117 | @@ -398,12 +425,25 @@ | |
118 | append_digit (cpp_num num, int digit, int base, size_t precision) | |
119 | { | |
120 | cpp_num result; | |
121 | - unsigned int shift = 3 + (base == 16); | |
122 | + unsigned int shift; | |
123 | bool overflow; | |
124 | cpp_num_part add_high, add_low; | |
125 | ||
126 | - /* Multiply by 8 or 16. Catching this overflow here means we don't | |
127 | + /* Multiply by 2, 8 or 16. Catching this overflow here means we don't | |
128 | need to worry about add_high overflowing. */ | |
129 | + switch (base) | |
130 | + { | |
131 | + case 2: | |
132 | + shift = 1; | |
133 | + break; | |
134 | + | |
135 | + case 16: | |
136 | + shift = 4; | |
137 | + break; | |
138 | + | |
139 | + default: | |
140 | + shift = 3; | |
141 | + } | |
142 | overflow = !!(num.high >> (PART_PRECISION - shift)); | |
143 | result.high = num.high << shift; | |
144 | result.low = num.low << shift; |