llvm.org GIT mirror llvm / 42c1227
Change TableGen so that binary literals such as 0b001 are now sized. Instead of these becoming an integer literal internally, they now become bits<n> values. Prior to this change, 0b001 was 1 bit long. This is confusing as clearly the user gave 3 bits. This new type holds both the literal value and the size, and so can ensure sizes match on initializers. For example, this used to be legal bits<1> x = 0b00; but now it must be written as bits<2> x = 0b00; git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215084 91177308-0d34-0410-b5e6-96231b3b80d8 Pete Cooper 5 years ago
4 changed file(s) with 56 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
410410 if (CurPtr == NumStart)
411411 return ReturnError(CurPtr-2, "Invalid binary number");
412412 CurIntVal = strtoll(NumStart, nullptr, 2);
413 return tgtok::IntVal;
413 return tgtok::BinaryIntVal;
414414 }
415415 }
416416
5151
5252 // Integer value.
5353 IntVal,
54
55 // Binary constant. Note that these are sized according to the number of
56 // bits given.
57 BinaryIntVal,
5458
5559 // String valued tokens.
5660 Id, StrVal, VarName, CodeFragment
104108 assert(CurCode == tgtok::IntVal && "This token isn't an integer");
105109 return CurIntVal;
106110 }
111 std::pair getCurBinaryIntVal() const {
112 assert(CurCode == tgtok::BinaryIntVal &&
113 "This token isn't a binary integer");
114 return std::make_pair(CurIntVal, (CurPtr - TokStart)-2);
115 }
107116
108117 SMLoc getLoc() const;
109118
11811181 Lex.Lex(); // Skip '#'.
11821182 return ParseSimpleValue(CurRec, ItemType, Mode);
11831183 case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break;
1184 case tgtok::BinaryIntVal: {
1185 auto BinaryVal = Lex.getCurBinaryIntVal();
1186 SmallVector Bits(BinaryVal.second);
1187 for (unsigned i = 0, e = BinaryVal.second; i != e; ++i)
1188 Bits[i] = BitInit::get(BinaryVal.first & (1 << i));
1189 R = BitsInit::get(Bits);
1190 Lex.Lex();
1191 break;
1192 }
11841193 case tgtok::StrVal: {
11851194 std::string Val = Lex.getCurStrVal();
11861195 Lex.Lex();
1919 // CHECK: bits<2> b = { 1, 0 };
2020 // CHECK: bits<2> c = { 1, 1 };
2121 // CHECK: }
22
23 def {
24 bits<2> B1 = 0b011; // bitfield is too small, reject
25 bits<3> B2 = 0b011; // ok
26
27 bits<2> C1 = 0b111; // bitfield is too small, reject
28 bits<3> C2 = 0b111; // ok
29
30 bits<2> D1 = { 0, 0 }; // ok
31 bits<2> D2 = { 0b00 }; // ok
32 bits<3> D3 = { 0, 0 }; // type mismatch. RHS doesn't have enough bits
33 bits<3> D4 = { 0b00 }; // type mismatch. RHS doesn't have enough bits
34 bits<1> D5 = { 0 }; // ok
35 bits<1> D6 = { 1 }; // ok
36 bits<1> D7 = { 3 }; // type mismatch. LHS doesn't have enough bits
37 bits<2> D8 = { 0 }; // type mismatch. RHS doesn't have enough bits
38
39 bits<8> E;
40 let E{7-0} = {0,0,1,?,?,?,?,?};
41 let E{3-0} = 0b0010;
42 }
43
44 // CHECK: def {{.*}} {
45 // CHECK: bits<2> B1;
46 // CHECK: bits<3> B2 = { 0, 1, 1 };
47 // CHECK: bits<2> C1;
48 // CHECK: bits<3> C2 = { 1, 1, 1 };
49 // CHECK: bits<2> D1 = { 0, 0 };
50 // CHECK: bits<2> D2 = { 0, 0 };
51 // CHECK: bits<3> D3;
52 // CHECK: bits<3> D4;
53 // CHECK: bits<1> D5 = { 0 };
54 // CHECK: bits<1> D6 = { 1 };
55 // CHECK: bits<1> D7 = { ? };
56 // CHECK: bits<2> D8;
57 // CHECK: bits<8> E = { 0, 0, 1, ?, 0, 0, 1, 0 };
58 // CHECK: }