Blob Blame Raw
From 925ea9d5443949cfd916504a9ed04ada8b750e27 Mon Sep 17 00:00:00 2001
From: Lauri Kasanen <cand@gmx.com>
Date: Tue, 16 Jul 2019 18:39:37 +0300
Subject: [PATCH 13/29] cc65: Add support for binary literals

Binary literals, 0b001, are a GCC extension in C and a C++14 feature.
---
 src/cc65/scanner.c |   7 +-
 test/val/binlit.c  | 533 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 538 insertions(+), 2 deletions(-)
 create mode 100644 test/val/binlit.c

diff --git a/src/cc65/scanner.c b/src/cc65/scanner.c
index 695085e9..3e829aab 100644
--- a/src/cc65/scanner.c
+++ b/src/cc65/scanner.c
@@ -464,8 +464,8 @@ static void NumericConst (void)
     unsigned DigitVal;
     unsigned long IVal;         /* Value */
 
-    /* Check for a leading hex or octal prefix and determine the possible
-    ** integer types.
+    /* Check for a leading hex, octal or binary prefix and determine the
+    ** possible integer types.
     */
     if (CurC == '0') {
         /* Gobble 0 and examine next char */
@@ -473,6 +473,9 @@ static void NumericConst (void)
         if (toupper (CurC) == 'X') {
             Base = Prefix = 16;
             NextChar ();        /* gobble "x" */
+        } else if (toupper (CurC) == 'B' && IS_Get (&Standard) >= STD_CC65) {
+            Base = Prefix = 2;
+            NextChar ();        /* gobble 'b' */
         } else {
             Base = 10;          /* Assume 10 for now - see below */
             Prefix = 8;         /* Actual prefix says octal */
diff --git a/test/val/binlit.c b/test/val/binlit.c
new file mode 100644
index 00000000..2b6c1ca8
--- /dev/null
+++ b/test/val/binlit.c
@@ -0,0 +1,533 @@
+static unsigned char small[256];
+static unsigned big[256];
+
+int main() {
+
+	unsigned i;
+
+	small[0] = 0b0;
+	small[1] = 0b1;
+	small[2] = 0b10;
+	small[3] = 0b11;
+	small[4] = 0b100;
+	small[5] = 0b101;
+	small[6] = 0b110;
+	small[7] = 0b111;
+	small[8] = 0b1000;
+	small[9] = 0b1001;
+	small[10] = 0b1010;
+	small[11] = 0b1011;
+	small[12] = 0b1100;
+	small[13] = 0b1101;
+	small[14] = 0b1110;
+	small[15] = 0b1111;
+	small[16] = 0b10000;
+	small[17] = 0b10001;
+	small[18] = 0b10010;
+	small[19] = 0b10011;
+	small[20] = 0b10100;
+	small[21] = 0b10101;
+	small[22] = 0b10110;
+	small[23] = 0b10111;
+	small[24] = 0b11000;
+	small[25] = 0b11001;
+	small[26] = 0b11010;
+	small[27] = 0b11011;
+	small[28] = 0b11100;
+	small[29] = 0b11101;
+	small[30] = 0b11110;
+	small[31] = 0b11111;
+	small[32] = 0b100000;
+	small[33] = 0b100001;
+	small[34] = 0b100010;
+	small[35] = 0b100011;
+	small[36] = 0b100100;
+	small[37] = 0b100101;
+	small[38] = 0b100110;
+	small[39] = 0b100111;
+	small[40] = 0b101000;
+	small[41] = 0b101001;
+	small[42] = 0b101010;
+	small[43] = 0b101011;
+	small[44] = 0b101100;
+	small[45] = 0b101101;
+	small[46] = 0b101110;
+	small[47] = 0b101111;
+	small[48] = 0b110000;
+	small[49] = 0b110001;
+	small[50] = 0b110010;
+	small[51] = 0b110011;
+	small[52] = 0b110100;
+	small[53] = 0b110101;
+	small[54] = 0b110110;
+	small[55] = 0b110111;
+	small[56] = 0b111000;
+	small[57] = 0b111001;
+	small[58] = 0b111010;
+	small[59] = 0b111011;
+	small[60] = 0b111100;
+	small[61] = 0b111101;
+	small[62] = 0b111110;
+	small[63] = 0b111111;
+	small[64] = 0b1000000;
+	small[65] = 0b1000001;
+	small[66] = 0b1000010;
+	small[67] = 0b1000011;
+	small[68] = 0b1000100;
+	small[69] = 0b1000101;
+	small[70] = 0b1000110;
+	small[71] = 0b1000111;
+	small[72] = 0b1001000;
+	small[73] = 0b1001001;
+	small[74] = 0b1001010;
+	small[75] = 0b1001011;
+	small[76] = 0b1001100;
+	small[77] = 0b1001101;
+	small[78] = 0b1001110;
+	small[79] = 0b1001111;
+	small[80] = 0b1010000;
+	small[81] = 0b1010001;
+	small[82] = 0b1010010;
+	small[83] = 0b1010011;
+	small[84] = 0b1010100;
+	small[85] = 0b1010101;
+	small[86] = 0b1010110;
+	small[87] = 0b1010111;
+	small[88] = 0b1011000;
+	small[89] = 0b1011001;
+	small[90] = 0b1011010;
+	small[91] = 0b1011011;
+	small[92] = 0b1011100;
+	small[93] = 0b1011101;
+	small[94] = 0b1011110;
+	small[95] = 0b1011111;
+	small[96] = 0b1100000;
+	small[97] = 0b1100001;
+	small[98] = 0b1100010;
+	small[99] = 0b1100011;
+	small[100] = 0b1100100;
+	small[101] = 0b1100101;
+	small[102] = 0b1100110;
+	small[103] = 0b1100111;
+	small[104] = 0b1101000;
+	small[105] = 0b1101001;
+	small[106] = 0b1101010;
+	small[107] = 0b1101011;
+	small[108] = 0b1101100;
+	small[109] = 0b1101101;
+	small[110] = 0b1101110;
+	small[111] = 0b1101111;
+	small[112] = 0b1110000;
+	small[113] = 0b1110001;
+	small[114] = 0b1110010;
+	small[115] = 0b1110011;
+	small[116] = 0b1110100;
+	small[117] = 0b1110101;
+	small[118] = 0b1110110;
+	small[119] = 0b1110111;
+	small[120] = 0b1111000;
+	small[121] = 0b1111001;
+	small[122] = 0b1111010;
+	small[123] = 0b1111011;
+	small[124] = 0b1111100;
+	small[125] = 0b1111101;
+	small[126] = 0b1111110;
+	small[127] = 0b1111111;
+	small[128] = 0b10000000;
+	small[129] = 0b10000001;
+	small[130] = 0b10000010;
+	small[131] = 0b10000011;
+	small[132] = 0b10000100;
+	small[133] = 0b10000101;
+	small[134] = 0b10000110;
+	small[135] = 0b10000111;
+	small[136] = 0b10001000;
+	small[137] = 0b10001001;
+	small[138] = 0b10001010;
+	small[139] = 0b10001011;
+	small[140] = 0b10001100;
+	small[141] = 0b10001101;
+	small[142] = 0b10001110;
+	small[143] = 0b10001111;
+	small[144] = 0b10010000;
+	small[145] = 0b10010001;
+	small[146] = 0b10010010;
+	small[147] = 0b10010011;
+	small[148] = 0b10010100;
+	small[149] = 0b10010101;
+	small[150] = 0b10010110;
+	small[151] = 0b10010111;
+	small[152] = 0b10011000;
+	small[153] = 0b10011001;
+	small[154] = 0b10011010;
+	small[155] = 0b10011011;
+	small[156] = 0b10011100;
+	small[157] = 0b10011101;
+	small[158] = 0b10011110;
+	small[159] = 0b10011111;
+	small[160] = 0b10100000;
+	small[161] = 0b10100001;
+	small[162] = 0b10100010;
+	small[163] = 0b10100011;
+	small[164] = 0b10100100;
+	small[165] = 0b10100101;
+	small[166] = 0b10100110;
+	small[167] = 0b10100111;
+	small[168] = 0b10101000;
+	small[169] = 0b10101001;
+	small[170] = 0b10101010;
+	small[171] = 0b10101011;
+	small[172] = 0b10101100;
+	small[173] = 0b10101101;
+	small[174] = 0b10101110;
+	small[175] = 0b10101111;
+	small[176] = 0b10110000;
+	small[177] = 0b10110001;
+	small[178] = 0b10110010;
+	small[179] = 0b10110011;
+	small[180] = 0b10110100;
+	small[181] = 0b10110101;
+	small[182] = 0b10110110;
+	small[183] = 0b10110111;
+	small[184] = 0b10111000;
+	small[185] = 0b10111001;
+	small[186] = 0b10111010;
+	small[187] = 0b10111011;
+	small[188] = 0b10111100;
+	small[189] = 0b10111101;
+	small[190] = 0b10111110;
+	small[191] = 0b10111111;
+	small[192] = 0b11000000;
+	small[193] = 0b11000001;
+	small[194] = 0b11000010;
+	small[195] = 0b11000011;
+	small[196] = 0b11000100;
+	small[197] = 0b11000101;
+	small[198] = 0b11000110;
+	small[199] = 0b11000111;
+	small[200] = 0b11001000;
+	small[201] = 0b11001001;
+	small[202] = 0b11001010;
+	small[203] = 0b11001011;
+	small[204] = 0b11001100;
+	small[205] = 0b11001101;
+	small[206] = 0b11001110;
+	small[207] = 0b11001111;
+	small[208] = 0b11010000;
+	small[209] = 0b11010001;
+	small[210] = 0b11010010;
+	small[211] = 0b11010011;
+	small[212] = 0b11010100;
+	small[213] = 0b11010101;
+	small[214] = 0b11010110;
+	small[215] = 0b11010111;
+	small[216] = 0b11011000;
+	small[217] = 0b11011001;
+	small[218] = 0b11011010;
+	small[219] = 0b11011011;
+	small[220] = 0b11011100;
+	small[221] = 0b11011101;
+	small[222] = 0b11011110;
+	small[223] = 0b11011111;
+	small[224] = 0b11100000;
+	small[225] = 0b11100001;
+	small[226] = 0b11100010;
+	small[227] = 0b11100011;
+	small[228] = 0b11100100;
+	small[229] = 0b11100101;
+	small[230] = 0b11100110;
+	small[231] = 0b11100111;
+	small[232] = 0b11101000;
+	small[233] = 0b11101001;
+	small[234] = 0b11101010;
+	small[235] = 0b11101011;
+	small[236] = 0b11101100;
+	small[237] = 0b11101101;
+	small[238] = 0b11101110;
+	small[239] = 0b11101111;
+	small[240] = 0b11110000;
+	small[241] = 0b11110001;
+	small[242] = 0b11110010;
+	small[243] = 0b11110011;
+	small[244] = 0b11110100;
+	small[245] = 0b11110101;
+	small[246] = 0b11110110;
+	small[247] = 0b11110111;
+	small[248] = 0b11111000;
+	small[249] = 0b11111001;
+	small[250] = 0b11111010;
+	small[251] = 0b11111011;
+	small[252] = 0b11111100;
+	small[253] = 0b11111101;
+	small[254] = 0b11111110;
+	small[255] = 0b11111111;
+
+	for (i = 0; i < 256; i++) {
+		if (small[i] != i)
+			return 1;
+	}
+
+	big[0] = 0b1111111100000000;
+	big[1] = 0b1111111100000001;
+	big[2] = 0b1111111100000010;
+	big[3] = 0b1111111100000011;
+	big[4] = 0b1111111100000100;
+	big[5] = 0b1111111100000101;
+	big[6] = 0b1111111100000110;
+	big[7] = 0b1111111100000111;
+	big[8] = 0b1111111100001000;
+	big[9] = 0b1111111100001001;
+	big[10] = 0b1111111100001010;
+	big[11] = 0b1111111100001011;
+	big[12] = 0b1111111100001100;
+	big[13] = 0b1111111100001101;
+	big[14] = 0b1111111100001110;
+	big[15] = 0b1111111100001111;
+	big[16] = 0b1111111100010000;
+	big[17] = 0b1111111100010001;
+	big[18] = 0b1111111100010010;
+	big[19] = 0b1111111100010011;
+	big[20] = 0b1111111100010100;
+	big[21] = 0b1111111100010101;
+	big[22] = 0b1111111100010110;
+	big[23] = 0b1111111100010111;
+	big[24] = 0b1111111100011000;
+	big[25] = 0b1111111100011001;
+	big[26] = 0b1111111100011010;
+	big[27] = 0b1111111100011011;
+	big[28] = 0b1111111100011100;
+	big[29] = 0b1111111100011101;
+	big[30] = 0b1111111100011110;
+	big[31] = 0b1111111100011111;
+	big[32] = 0b1111111100100000;
+	big[33] = 0b1111111100100001;
+	big[34] = 0b1111111100100010;
+	big[35] = 0b1111111100100011;
+	big[36] = 0b1111111100100100;
+	big[37] = 0b1111111100100101;
+	big[38] = 0b1111111100100110;
+	big[39] = 0b1111111100100111;
+	big[40] = 0b1111111100101000;
+	big[41] = 0b1111111100101001;
+	big[42] = 0b1111111100101010;
+	big[43] = 0b1111111100101011;
+	big[44] = 0b1111111100101100;
+	big[45] = 0b1111111100101101;
+	big[46] = 0b1111111100101110;
+	big[47] = 0b1111111100101111;
+	big[48] = 0b1111111100110000;
+	big[49] = 0b1111111100110001;
+	big[50] = 0b1111111100110010;
+	big[51] = 0b1111111100110011;
+	big[52] = 0b1111111100110100;
+	big[53] = 0b1111111100110101;
+	big[54] = 0b1111111100110110;
+	big[55] = 0b1111111100110111;
+	big[56] = 0b1111111100111000;
+	big[57] = 0b1111111100111001;
+	big[58] = 0b1111111100111010;
+	big[59] = 0b1111111100111011;
+	big[60] = 0b1111111100111100;
+	big[61] = 0b1111111100111101;
+	big[62] = 0b1111111100111110;
+	big[63] = 0b1111111100111111;
+	big[64] = 0b1111111101000000;
+	big[65] = 0b1111111101000001;
+	big[66] = 0b1111111101000010;
+	big[67] = 0b1111111101000011;
+	big[68] = 0b1111111101000100;
+	big[69] = 0b1111111101000101;
+	big[70] = 0b1111111101000110;
+	big[71] = 0b1111111101000111;
+	big[72] = 0b1111111101001000;
+	big[73] = 0b1111111101001001;
+	big[74] = 0b1111111101001010;
+	big[75] = 0b1111111101001011;
+	big[76] = 0b1111111101001100;
+	big[77] = 0b1111111101001101;
+	big[78] = 0b1111111101001110;
+	big[79] = 0b1111111101001111;
+	big[80] = 0b1111111101010000;
+	big[81] = 0b1111111101010001;
+	big[82] = 0b1111111101010010;
+	big[83] = 0b1111111101010011;
+	big[84] = 0b1111111101010100;
+	big[85] = 0b1111111101010101;
+	big[86] = 0b1111111101010110;
+	big[87] = 0b1111111101010111;
+	big[88] = 0b1111111101011000;
+	big[89] = 0b1111111101011001;
+	big[90] = 0b1111111101011010;
+	big[91] = 0b1111111101011011;
+	big[92] = 0b1111111101011100;
+	big[93] = 0b1111111101011101;
+	big[94] = 0b1111111101011110;
+	big[95] = 0b1111111101011111;
+	big[96] = 0b1111111101100000;
+	big[97] = 0b1111111101100001;
+	big[98] = 0b1111111101100010;
+	big[99] = 0b1111111101100011;
+	big[100] = 0b1111111101100100;
+	big[101] = 0b1111111101100101;
+	big[102] = 0b1111111101100110;
+	big[103] = 0b1111111101100111;
+	big[104] = 0b1111111101101000;
+	big[105] = 0b1111111101101001;
+	big[106] = 0b1111111101101010;
+	big[107] = 0b1111111101101011;
+	big[108] = 0b1111111101101100;
+	big[109] = 0b1111111101101101;
+	big[110] = 0b1111111101101110;
+	big[111] = 0b1111111101101111;
+	big[112] = 0b1111111101110000;
+	big[113] = 0b1111111101110001;
+	big[114] = 0b1111111101110010;
+	big[115] = 0b1111111101110011;
+	big[116] = 0b1111111101110100;
+	big[117] = 0b1111111101110101;
+	big[118] = 0b1111111101110110;
+	big[119] = 0b1111111101110111;
+	big[120] = 0b1111111101111000;
+	big[121] = 0b1111111101111001;
+	big[122] = 0b1111111101111010;
+	big[123] = 0b1111111101111011;
+	big[124] = 0b1111111101111100;
+	big[125] = 0b1111111101111101;
+	big[126] = 0b1111111101111110;
+	big[127] = 0b1111111101111111;
+	big[128] = 0b1111111110000000;
+	big[129] = 0b1111111110000001;
+	big[130] = 0b1111111110000010;
+	big[131] = 0b1111111110000011;
+	big[132] = 0b1111111110000100;
+	big[133] = 0b1111111110000101;
+	big[134] = 0b1111111110000110;
+	big[135] = 0b1111111110000111;
+	big[136] = 0b1111111110001000;
+	big[137] = 0b1111111110001001;
+	big[138] = 0b1111111110001010;
+	big[139] = 0b1111111110001011;
+	big[140] = 0b1111111110001100;
+	big[141] = 0b1111111110001101;
+	big[142] = 0b1111111110001110;
+	big[143] = 0b1111111110001111;
+	big[144] = 0b1111111110010000;
+	big[145] = 0b1111111110010001;
+	big[146] = 0b1111111110010010;
+	big[147] = 0b1111111110010011;
+	big[148] = 0b1111111110010100;
+	big[149] = 0b1111111110010101;
+	big[150] = 0b1111111110010110;
+	big[151] = 0b1111111110010111;
+	big[152] = 0b1111111110011000;
+	big[153] = 0b1111111110011001;
+	big[154] = 0b1111111110011010;
+	big[155] = 0b1111111110011011;
+	big[156] = 0b1111111110011100;
+	big[157] = 0b1111111110011101;
+	big[158] = 0b1111111110011110;
+	big[159] = 0b1111111110011111;
+	big[160] = 0b1111111110100000;
+	big[161] = 0b1111111110100001;
+	big[162] = 0b1111111110100010;
+	big[163] = 0b1111111110100011;
+	big[164] = 0b1111111110100100;
+	big[165] = 0b1111111110100101;
+	big[166] = 0b1111111110100110;
+	big[167] = 0b1111111110100111;
+	big[168] = 0b1111111110101000;
+	big[169] = 0b1111111110101001;
+	big[170] = 0b1111111110101010;
+	big[171] = 0b1111111110101011;
+	big[172] = 0b1111111110101100;
+	big[173] = 0b1111111110101101;
+	big[174] = 0b1111111110101110;
+	big[175] = 0b1111111110101111;
+	big[176] = 0b1111111110110000;
+	big[177] = 0b1111111110110001;
+	big[178] = 0b1111111110110010;
+	big[179] = 0b1111111110110011;
+	big[180] = 0b1111111110110100;
+	big[181] = 0b1111111110110101;
+	big[182] = 0b1111111110110110;
+	big[183] = 0b1111111110110111;
+	big[184] = 0b1111111110111000;
+	big[185] = 0b1111111110111001;
+	big[186] = 0b1111111110111010;
+	big[187] = 0b1111111110111011;
+	big[188] = 0b1111111110111100;
+	big[189] = 0b1111111110111101;
+	big[190] = 0b1111111110111110;
+	big[191] = 0b1111111110111111;
+	big[192] = 0b1111111111000000;
+	big[193] = 0b1111111111000001;
+	big[194] = 0b1111111111000010;
+	big[195] = 0b1111111111000011;
+	big[196] = 0b1111111111000100;
+	big[197] = 0b1111111111000101;
+	big[198] = 0b1111111111000110;
+	big[199] = 0b1111111111000111;
+	big[200] = 0b1111111111001000;
+	big[201] = 0b1111111111001001;
+	big[202] = 0b1111111111001010;
+	big[203] = 0b1111111111001011;
+	big[204] = 0b1111111111001100;
+	big[205] = 0b1111111111001101;
+	big[206] = 0b1111111111001110;
+	big[207] = 0b1111111111001111;
+	big[208] = 0b1111111111010000;
+	big[209] = 0b1111111111010001;
+	big[210] = 0b1111111111010010;
+	big[211] = 0b1111111111010011;
+	big[212] = 0b1111111111010100;
+	big[213] = 0b1111111111010101;
+	big[214] = 0b1111111111010110;
+	big[215] = 0b1111111111010111;
+	big[216] = 0b1111111111011000;
+	big[217] = 0b1111111111011001;
+	big[218] = 0b1111111111011010;
+	big[219] = 0b1111111111011011;
+	big[220] = 0b1111111111011100;
+	big[221] = 0b1111111111011101;
+	big[222] = 0b1111111111011110;
+	big[223] = 0b1111111111011111;
+	big[224] = 0b1111111111100000;
+	big[225] = 0b1111111111100001;
+	big[226] = 0b1111111111100010;
+	big[227] = 0b1111111111100011;
+	big[228] = 0b1111111111100100;
+	big[229] = 0b1111111111100101;
+	big[230] = 0b1111111111100110;
+	big[231] = 0b1111111111100111;
+	big[232] = 0b1111111111101000;
+	big[233] = 0b1111111111101001;
+	big[234] = 0b1111111111101010;
+	big[235] = 0b1111111111101011;
+	big[236] = 0b1111111111101100;
+	big[237] = 0b1111111111101101;
+	big[238] = 0b1111111111101110;
+	big[239] = 0b1111111111101111;
+	big[240] = 0b1111111111110000;
+	big[241] = 0b1111111111110001;
+	big[242] = 0b1111111111110010;
+	big[243] = 0b1111111111110011;
+	big[244] = 0b1111111111110100;
+	big[245] = 0b1111111111110101;
+	big[246] = 0b1111111111110110;
+	big[247] = 0b1111111111110111;
+	big[248] = 0b1111111111111000;
+	big[249] = 0b1111111111111001;
+	big[250] = 0b1111111111111010;
+	big[251] = 0b1111111111111011;
+	big[252] = 0b1111111111111100;
+	big[253] = 0b1111111111111101;
+	big[254] = 0b1111111111111110;
+	big[255] = 0b1111111111111111;
+
+	for (i = 0; i < 256; i++) {
+		if (big[i] != i + 65280U)
+			return 1;
+	}
+
+	return 0;
+}
-- 
2.21.0