18 #include "llvm/Config/llvm-config.h" 57 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 59 errs() <<
"Multiclass:\n";
63 errs() <<
"Template args:\n";
73 for (
unsigned i = 0,
e = BV->
getNumBits(); i !=
e; ++i) {
75 bool IsReference =
false;
76 if (
auto VBI = dyn_cast<VarBitInit>(Bit)) {
77 if (
auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
81 }
else if (isa<VarInit>(Bit)) {
99 if (
Init *V = RV.getValue()) {
103 Twine(
"Initializer of '") + RV.getNameInitAsString() +
105 "' could not be fully resolved: " +
106 RV.getValue()->getAsString());
119 if (CurMultiClass && Scoper !=
"::") {
125 if (
BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
126 NewName = BinOp->Fold(&CurRec);
142 CurRec = &CurMultiClass->Rec;
147 return Error(Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
149 "previous definition of type '" +
150 ERV->getType()->getAsString() +
"'");
161 bool AllowSelfAssignment) {
162 if (!V)
return false;
164 if (!CurRec) CurRec = &CurMultiClass->Rec;
175 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
176 return Error(Loc,
"Recursion / self-assignment forbidden");
182 if (!BitList.
empty()) {
186 "' is not a bits type");
191 return Error(Loc,
"Initializer is not compatible with bit range");
196 for (
unsigned i = 0,
e = BitList.
size(); i !=
e; ++i) {
197 unsigned Bit = BitList[i];
199 return Error(Loc,
"Cannot set bit #" +
Twine(Bit) +
" of value '" +
201 NewBits[
Bit] = BI->getBit(i);
204 for (
unsigned i = 0,
e = CurVal->
getNumBits(); i !=
e; ++i)
206 NewBits[i] = CurVal->
getBit(i);
212 std::string InitType;
213 if (
BitsInit *BI = dyn_cast<BitsInit>(V))
214 InitType = (
Twine(
"' of type bit initializer with length ") +
215 Twine(BI->getNumBits())).str();
216 else if (
TypedInit *TI = dyn_cast<TypedInit>(V))
217 InitType = (
Twine(
"' of type '") + TI->getType()->getAsString()).str();
220 "' is incompatible with initializer '" +
240 "More template args specified than expected");
246 for (
unsigned i = 0,
e = TArgs.
size(); i !=
e; ++i) {
254 "Value not specified for template argument #" +
255 Twine(i) +
" (" + TArgs[i]->getAsUnquotedString() +
277 for (
const auto &SCPair : SCs) {
280 "Already subclass of '" + SCPair.first->getName() +
"'!\n");
286 "Already subclass of '" + SC->
getName() +
"'!\n");
293 return AddSubClass(Entry.
Rec.get(), SubClass);
295 for (
auto &
E : Entry.
Loop->Entries) {
296 if (AddSubClass(
E, SubClass))
306 bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
313 "More template args specified than expected");
318 for (
unsigned i = 0,
e = SMCTArgs.
size(); i !=
e; ++i) {
320 TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.
TemplateArgs[i]);
325 "value not specified for template argument #" +
Twine(i) +
326 " (" + SMCTArgs[i]->getAsUnquotedString() +
330 TemplateArgs.emplace_back(SMCTArgs[i], Default);
334 TemplateArgs.emplace_back(
339 return resolve(SMC->
Entries, TemplateArgs,
false, &CurMC->
Entries);
347 if (!
Loops.empty()) {
348 Loops.back()->Entries.push_back(std::move(E));
354 return resolve(*E.
Loop, Stack, CurMultiClass ==
nullptr,
355 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
359 CurMultiClass->Entries.push_back(std::move(E));
363 return addDefOne(std::move(E.
Rec));
372 bool Final, std::vector<RecordsEntry> *Dest,
375 for (
const auto &S : Substs)
376 R.
set(S.first, S.second);
381 Dest->emplace_back(std::make_unique<ForeachLoop>(Loop.
Loc, Loop.
IterVar,
383 return resolve(Loop.
Entries, Substs, Final, &Dest->back().Loop->Entries,
393 for (
auto Elt : *LI) {
396 Error = resolve(Loop.
Entries, Substs, Final, Dest);
410 bool TGParser::resolve(
const std::vector<RecordsEntry> &
Source,
411 SubstStack &Substs,
bool Final,
412 std::vector<RecordsEntry> *Dest,
SMLoc *Loc) {
416 Error = resolve(*E.
Loop, Substs, Final, Dest);
418 auto Rec = std::make_unique<Record>(*E.
Rec);
423 for (
const auto &S : Substs)
424 R.set(S.first, S.second);
428 Dest->push_back(std::move(
Rec));
430 Error = addDefOne(std::move(
Rec));
439 bool TGParser::addDefOne(std::unique_ptr<Record>
Rec) {
440 if (
Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
441 if (!Rec->isAnonymous()) {
443 "def already exists: " + Rec->getNameInitAsString());
444 PrintNote(Prev->getLoc(),
"location of previous definition");
447 Rec->setName(Records.getNewAnonymousName());
450 Rec->resolveReferences();
453 if (!isa<StringInit>(Rec->getNameInit())) {
455 Rec->getNameInit()->getAsString() +
456 "' could not be fully resolved");
461 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
466 PrintError(Rec->getLoc(),
Twine(
"adding record of incompatible type '") +
472 Defset->Elements.push_back(I);
475 Records.addDef(std::move(Rec));
497 switch (Lex.getCode()) {
511 CurRec = &CurMultiClass->
Rec;
534 Record *TGParser::ParseClassID() {
536 TokError(
"expected name for ClassID");
540 Record *Result = Records.getClass(Lex.getCurStrVal());
542 std::string Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
543 if (MultiClasses[Lex.getCurStrVal()].get())
544 TokError(Msg +
". Use 'defm' if you meant to use multiclass '" +
545 Lex.getCurStrVal() +
"'");
561 TokError(
"expected name for MultiClassID");
565 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
567 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
580 ParseSubClassReference(
Record *CurRec,
bool isDefm) {
586 Result.
Rec = &MC->Rec;
588 Result.
Rec = ParseClassID();
590 if (!Result.
Rec)
return Result;
600 TokError(
"subclass reference requires a non-empty list of template values");
601 Result.
Rec =
nullptr;
607 Result.
Rec =
nullptr;
612 TokError(
"expected '>' in template value list");
613 Result.
Rec =
nullptr;
630 ParseSubMultiClassReference(
MultiClass *CurMC) {
634 Result.
MC = ParseMultiClassID();
635 if (!Result.
MC)
return Result;
645 TokError(
"subclass reference requires a non-empty list of template values");
657 TokError(
"expected '>' in template value list");
673 Init *CurVal = FirstItem;
675 CurVal = ParseValue(
nullptr);
677 IntInit *II = dyn_cast_or_null<IntInit>(CurVal);
679 return TokError(
"expected integer or bitrange");
685 return TokError(
"invalid range, cannot be negative");
687 switch (Lex.getCode()) {
694 Init *I_End = ParseValue(
nullptr);
695 IntInit *II_End = dyn_cast_or_null<IntInit>(I_End);
697 TokError(
"expected integer value as end of range");
705 End = -Lex.getCurIntVal();
711 return TokError(
"invalid range, cannot be negative");
715 for (; Start <= End; ++Start)
718 for (; Start >= End; --Start)
729 if (ParseRangePiece(Result)) {
737 if (ParseRangePiece(Result)) {
751 SMLoc StartLoc = Lex.getLoc();
755 ParseRangeList(Ranges);
756 if (Ranges.
empty())
return true;
759 TokError(
"expected '>' at end of range list");
760 return Error(StartLoc,
"to match this '<'");
773 SMLoc StartLoc = Lex.getLoc();
777 ParseRangeList(Ranges);
778 if (Ranges.
empty())
return true;
781 TokError(
"expected '}' at end of bit list");
782 return Error(StartLoc,
"to match this '{'");
799 RecTy *TGParser::ParseType() {
800 switch (Lex.getCode()) {
801 default: TokError(
"Unknown token when expecting a type");
return nullptr;
809 TokError(
"unknown class name");
813 TokError(
"expected '<' after bits type");
817 TokError(
"expected integer in bits<n> type");
820 uint64_t Val = Lex.getCurIntVal();
822 TokError(
"expected '>' at end of bits<n> type");
830 TokError(
"expected '<' after list type");
834 RecTy *SubType = ParseType();
835 if (!SubType)
return nullptr;
838 TokError(
"expected '>' at end of list<ty> type");
856 if ((CurRec && CurRec->
isClass()) || CurMultiClass) {
857 Init *TemplateArgName;
862 TemplateArgName =
QualifyName(*CurRec, CurMultiClass, Name,
":");
864 Record *TemplateRec = CurMultiClass ? &CurMultiClass->
Rec : CurRec;
867 assert(RV &&
"Template arg doesn't exist??");
869 }
else if (Name->
getValue() ==
"NAME") {
879 for (
const auto &L :
Loops) {
887 if (Mode == ParseNameMode)
895 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
899 Error(NameLoc,
"Variable not defined: '" + Name->
getValue() +
"'");
908 switch (Lex.getCode()) {
910 TokError(
"unknown operation");
921 switch (Lex.getCode()) {
927 Type = ParseOperatorType();
930 TokError(
"did not get type for unary operator");
959 Type = ParseOperatorType();
962 TokError(
"did not get type for unary operator");
966 if (!isa<RecordRecTy>(Type)) {
967 TokError(
"type for !getop must be a record type");
977 TokError(
"expected '(' after unary operator");
982 Init *LHS = ParseValue(CurRec);
983 if (!LHS)
return nullptr;
991 if (!LHSl && !LHSs && !LHSt) {
992 TokError(
"expected list or string type argument in unary operator");
998 if (!LType && !SType) {
999 TokError(
"expected list or string type argument in unary operator");
1006 if (!LHSl && !LHSt) {
1007 TokError(
"expected list type argument in unary operator");
1013 if (LHSl && LHSl->
empty()) {
1014 TokError(
"empty list argument in unary operator");
1021 TokError(
"untyped list element in unary operator");
1027 assert(LHSt &&
"expected list type argument in unary operator");
1030 TokError(
"expected list type argument in unary operator");
1039 TokError(
"expected ')' in unary operator");
1055 TokError(
"expected '(' after type of !isa");
1060 Init *LHS = ParseValue(CurRec);
1065 TokError(
"expected ')' in !isa");
1092 SMLoc OpLoc = Lex.getLoc();
1119 RecTy *ArgType =
nullptr;
1164 Error(OpLoc,
Twine(
"expected value of type '") +
1171 TokError(
"expected '(' after binary operator");
1179 SMLoc InitLoc = Lex.getLoc();
1180 InitList.
push_back(ParseValue(CurRec, ArgType));
1181 if (!InitList.
back())
return nullptr;
1189 if (!isa<ListRecTy>(ArgType)) {
1190 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1196 if (ItemType && InitList.
size() == 1) {
1197 if (!isa<ListRecTy>(ItemType)) {
1199 Twine(
"expected output type to be a list, got type '") +
1204 Error(OpLoc,
Twine(
"expected first arg type to be '") +
1206 "', got value of type '" +
1207 cast<ListRecTy>(ItemType)
1214 if (InitList.
size() == 2 && !isa<IntRecTy>(ArgType)) {
1215 Error(InitLoc,
Twine(
"expected second parameter to be an int, got " 1216 "value of type '") +
1226 Error(InitLoc,
Twine(
"expected int, bits, or string; got value of " 1236 Error(InitLoc,
Twine(
"expected value of type '") +
1266 TokError(
"expected ')' in operator");
1276 Type = cast<TypedInit>(InitList.
front())->
getType()->getListTy();
1284 while (InitList.
size() > 2) {
1287 InitList.
back() = RHS;
1291 if (InitList.
size() == 2)
1295 Error(OpLoc,
"expected two operands to operator");
1300 SMLoc OpLoc = Lex.getLoc();
1303 TokError(
"expected '(' after !foreach");
1308 TokError(
"first argument of !foreach must be an identifier");
1314 if (CurRec && CurRec->
getValue(LHS)) {
1316 "' already defined")
1322 TokError(
"expected ',' in ternary operator");
1327 Init *MHS = ParseValue(CurRec);
1332 TokError(
"expected ',' in ternary operator");
1339 TokError(
"could not get type of !foreach input");
1343 RecTy *InEltType =
nullptr;
1344 RecTy *OutEltType =
nullptr;
1348 InEltType = InListTy->getElementType();
1350 if (
ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1351 OutEltType = OutListTy->getElementType();
1355 "', but got !foreach of list type");
1360 InEltType = InDagTy;
1361 if (ItemType && !isa<DagRecTy>(ItemType)) {
1364 "', but got !foreach of dag type");
1369 TokError(
"!foreach must have list or dag input");
1375 std::unique_ptr<Record> ParseRecTmp;
1376 Record *ParseRec = CurRec;
1378 ParseRecTmp = std::make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
1379 ParseRec = ParseRecTmp.get();
1383 Init *RHS = ParseValue(ParseRec, OutEltType);
1389 TokError(
"expected ')' in binary operator");
1396 OutType = InEltType;
1400 TokError(
"could not get type of !foreach result");
1433 TokError(
"expected '(' after ternary operator");
1438 Init *LHS = ParseValue(CurRec);
1439 if (!LHS)
return nullptr;
1442 TokError(
"expected ',' in ternary operator");
1447 SMLoc MHSLoc = Lex.getLoc();
1448 Init *MHS = ParseValue(CurRec, ItemType);
1453 TokError(
"expected ',' in ternary operator");
1458 SMLoc RHSLoc = Lex.getLoc();
1459 Init *RHS = ParseValue(CurRec, ItemType);
1464 TokError(
"expected ')' in binary operator");
1473 if (!MHSt && !isa<UnsetInit>(MHS)) {
1474 Error(MHSLoc,
"could not determine type of the child list in !dag");
1477 if (MHSt && !isa<ListRecTy>(MHSt->
getType())) {
1478 Error(MHSLoc,
Twine(
"expected list of children, got type '") +
1484 if (!RHSt && !isa<UnsetInit>(RHS)) {
1485 Error(RHSLoc,
"could not determine type of the name list in !dag");
1489 Error(RHSLoc,
Twine(
"expected list<string>, got type '") +
1494 if (!MHSt && !RHSt) {
1496 "cannot have both unset children and unset names in !dag");
1502 RecTy *MHSTy =
nullptr;
1503 RecTy *RHSTy =
nullptr;
1505 if (
TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1506 MHSTy = MHSt->getType();
1507 if (
BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1509 if (isa<BitInit>(MHS))
1512 if (
TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
1513 RHSTy = RHSt->getType();
1514 if (
BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
1516 if (isa<BitInit>(RHS))
1520 if (isa<UnsetInit>(MHS))
1522 if (isa<UnsetInit>(RHS))
1525 if (!MHSTy || !RHSTy) {
1526 TokError(
"could not get type for !if");
1541 TokError(
"could not get type for !subst");
1552 return ParseOperationCond(CurRec, ItemType);
1558 TokError(
"expected '(' after !foldl");
1563 Init *StartUntyped = ParseValue(CurRec);
1569 TokError(
Twine(
"could not get type of !foldl start: '") +
1575 TokError(
"expected ',' in !foldl");
1580 Init *ListUntyped = ParseValue(CurRec);
1586 TokError(
Twine(
"could not get type of !foldl list: '") +
1593 TokError(
Twine(
"!foldl list must be a list, but is of type '") +
1599 TokError(
"expected ',' in !foldl");
1604 TokError(
"third argument of !foldl must be an identifier");
1609 if (CurRec && CurRec->
getValue(A)) {
1611 "' already defined")
1617 TokError(
"expected ',' in !foldl");
1622 TokError(
"fourth argument of !foldl must be an identifier");
1627 if (CurRec && CurRec->
getValue(B)) {
1629 "' already defined")
1635 TokError(
"expected ',' in !foldl");
1642 std::unique_ptr<Record> ParseRecTmp;
1643 Record *ParseRec = CurRec;
1645 ParseRecTmp = std::make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
1646 ParseRec = ParseRecTmp.get();
1651 Init *ExprUntyped = ParseValue(ParseRec);
1659 TokError(
"could not get type of !foldl expression");
1664 TokError(
Twine(
"!foldl expression must be of same type as start (") +
1671 TokError(
"expected ')' in fold operator");
1687 RecTy *TGParser::ParseOperatorType() {
1691 TokError(
"expected type name for operator");
1699 TokError(
"expected type name for operator");
1704 TokError(
"expected type name for operator");
1716 TokError(
"expected '(' after !cond operator");
1730 Init *V = ParseValue(CurRec);
1736 TokError(
"expected ':' following a condition in !cond operator");
1741 V = ParseValue(CurRec, ItemType);
1752 TokError(
"expected ',' or ')' following a value in !cond operator");
1758 if (Case.
size() < 1) {
1759 TokError(
"there should be at least 1 'condition : value' in the !cond operator");
1765 for (
Init *V : Val) {
1766 RecTy *VTy =
nullptr;
1767 if (
TypedInit *Vt = dyn_cast<TypedInit>(V))
1768 VTy = Vt->getType();
1769 if (
BitsInit *Vbits = dyn_cast<BitsInit>(V))
1771 if (isa<BitInit>(V))
1774 if (Type ==
nullptr) {
1775 if (!isa<UnsetInit>(V))
1778 if (!isa<UnsetInit>(V)) {
1791 TokError(
"could not determine type for !cond from its arguments");
1821 switch (Lex.getCode()) {
1822 default: TokError(
"Unknown token when parsing a value");
break;
1827 return ParseSimpleValue(CurRec, ItemType, Mode);
1830 auto BinaryVal = Lex.getCurBinaryIntVal();
1832 for (
unsigned i = 0,
e = BinaryVal.second; i !=
e; ++i)
1839 std::string Val = Lex.getCurStrVal();
1844 Val += Lex.getCurStrVal();
1860 SMLoc NameLoc = Lex.getLoc();
1863 return ParseIDValue(CurRec, Name, NameLoc, Mode);
1867 TokError(
"expected non-empty value list");
1876 Error(NameLoc,
"Expected a class name, got '" + Name->
getValue() +
"'");
1881 ParseValueList(Args, CurRec, Class);
1882 if (Args.
empty())
return nullptr;
1885 TokError(
"expected '>' at end of value list");
1892 if (ExpectedArgs.
size() < Args.
size()) {
1894 "More template args specified than expected");
1898 for (
unsigned i = 0,
e = ExpectedArgs.
size(); i !=
e; ++i) {
1900 if (i < Args.
size()) {
1901 if (
TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
1903 if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
1905 "Value specified for template argument #" +
Twine(i) +
" (" +
1907 TI->getType()->getAsString() +
"', expected '" +
1908 ExpectedType->
getAsString() +
"': " + TI->getAsString());
1917 "Value not specified for template argument #" +
Twine(i) +
" (" +
1918 ExpectedArgs[i]->getAsUnquotedString() +
")");
1925 SMLoc BraceLoc = Lex.getLoc();
1930 ParseValueList(Vals, CurRec);
1931 if (Vals.
empty())
return nullptr;
1934 TokError(
"expected '}' at end of bit list value");
1944 for (
unsigned i = 0,
e = Vals.
size(); i !=
e; ++i) {
1949 if (
BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1950 for (
unsigned i = 0,
e = BI->getNumBits(); i !=
e; ++i)
1955 if (
VarInit *
VI = dyn_cast<VarInit>(Vals[i])) {
1956 if (
BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(
VI->getType())) {
1957 for (
unsigned i = 0,
e = BitsRec->getNumBits(); i !=
e; ++i)
1966 Error(BraceLoc,
"Element #" +
Twine(i) +
" (" + Vals[i]->getAsString() +
1967 ") is not convertable to a bit");
1979 RecTy *DeducedEltTy =
nullptr;
1985 TokError(
Twine(
"Type mismatch for list, expected list type, got ") +
1989 GivenListTy = ListType;
1993 ParseValueList(Vals, CurRec,
nullptr,
1995 if (Vals.
empty())
return nullptr;
1998 TokError(
"expected ']' at end of list value");
2003 RecTy *GivenEltTy =
nullptr;
2008 GivenEltTy = ParseType();
2015 TokError(
"expected '>' at end of list element type");
2022 RecTy *EltTy =
nullptr;
2023 for (
Init *V : Vals) {
2029 TokError(
"Incompatible types in list elements");
2042 TokError(
"Incompatible types in list elements");
2051 TokError(
"No type for list");
2059 TokError(
Twine(
"Element type mismatch for list: element type '") +
2065 DeducedEltTy = EltTy;
2074 TokError(
"expected identifier in dag init");
2079 if (!Operator)
return nullptr;
2085 TokError(
"expected variable name in dag operator");
2094 ParseDagArgList(DagArgs, CurRec);
2095 if (DagArgs.
empty())
return nullptr;
2099 TokError(
"expected ')' in dag init");
2138 return ParseOperation(CurRec, ItemType);
2153 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
2154 if (!Result)
return nullptr;
2158 switch (Lex.getCode()) {
2159 default:
return Result;
2161 if (Mode == ParseNameMode)
2165 SMLoc CurlyLoc = Lex.getLoc();
2168 ParseRangeList(Ranges);
2169 if (Ranges.
empty())
return nullptr;
2175 Error(CurlyLoc,
"Invalid bit range for value");
2181 TokError(
"expected '}' at end of bit range list");
2188 SMLoc SquareLoc = Lex.getLoc();
2191 ParseRangeList(Ranges);
2192 if (Ranges.
empty())
return nullptr;
2196 Error(SquareLoc,
"Invalid range for list slice");
2202 TokError(
"expected ']' at end of list slice");
2210 TokError(
"expected field identifier after '.'");
2215 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
2225 SMLoc PasteLoc = Lex.getLoc();
2228 Error(PasteLoc,
"LHS of paste is not typed!");
2233 if (isa<ListRecTy>(LHS->
getType())) {
2236 switch (Lex.getCode()) {
2243 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
2266 switch (Lex.getCode()) {
2279 Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
2282 Error(PasteLoc,
"RHS of paste is not typed!");
2313 void TGParser::ParseDagArgList(
2326 Init *Val = ParseValue(CurRec);
2336 TokError(
"expected variable name in dag literal");
2344 Result.
push_back(std::make_pair(Val, VarName));
2359 RecTy *ItemType = EltTy;
2360 unsigned int ArgN = 0;
2361 if (ArgsRec && !EltTy) {
2363 if (TArgs.
empty()) {
2364 TokError(
"template argument provided to non-template class");
2370 errs() <<
"Cannot find template arg " << ArgN <<
" (" << TArgs[ArgN]
2373 assert(RV &&
"Template argument record not found??");
2377 Result.
push_back(ParseValue(CurRec, ItemType));
2378 if (!Result.
back()) {
2390 if (ArgsRec && !EltTy) {
2392 if (ArgN >= TArgs.
size()) {
2393 TokError(
"too many template arguments");
2398 assert(RV &&
"Template argument record not found??");
2402 Result.
push_back(ParseValue(CurRec, ItemType));
2403 if (!Result.
back()) {
2420 Init *TGParser::ParseDeclaration(
Record *CurRec,
2421 bool ParsingTemplateArgs) {
2424 if (HasField) Lex.Lex();
2427 if (!Type)
return nullptr;
2430 TokError(
"Expected identifier in declaration");
2434 std::string Str = Lex.getCurStrVal();
2435 if (Str ==
"NAME") {
2436 TokError(
"'" + Str +
"' is a reserved variable name");
2440 SMLoc IdLoc = Lex.getLoc();
2444 if (ParsingTemplateArgs) {
2446 DeclName =
QualifyName(*CurRec, CurMultiClass, DeclName,
":");
2450 DeclName =
QualifyName(CurMultiClass->
Rec, CurMultiClass, DeclName,
2455 if (AddValue(CurRec, IdLoc,
RecordVal(DeclName, Type, HasField)))
2461 SMLoc ValLoc = Lex.getLoc();
2462 Init *Val = ParseValue(CurRec, Type);
2482 VarInit *TGParser::ParseForeachDeclaration(
Init *&ForeachListValue) {
2484 TokError(
"Expected identifier in foreach declaration");
2493 TokError(
"Expected '=' in foreach declaration");
2498 RecTy *IterType =
nullptr;
2501 switch (Lex.getCode()) {
2504 ParseRangeList(Ranges);
2506 TokError(
"expected '}' at end of bit range list");
2514 SMLoc ValueLoc = Lex.getLoc();
2515 Init *
I = ParseValue(
nullptr);
2520 if (TI && isa<ListRecTy>(TI->
getType())) {
2521 ForeachListValue =
I;
2522 IterType = cast<ListRecTy>(TI->
getType())->getElementType();
2527 if (ParseRangePiece(Ranges, TI))
2536 if (CurMultiClass) {
2537 PrintNote({},
"references to multiclass template arguments cannot be " 2538 "resolved at this time");
2545 if (!Ranges.
empty()) {
2546 assert(!IterType &&
"Type already initialized?");
2548 std::vector<Init*> Values;
2549 for (
unsigned R : Ranges)
2567 bool TGParser::ParseTemplateArgList(
Record *CurRec) {
2571 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->
Rec;
2574 Init *TemplArg = ParseDeclaration(CurRec,
true);
2584 SMLoc Loc = Lex.getLoc();
2585 TemplArg = ParseDeclaration(CurRec,
true);
2590 return Error(Loc,
"template argument with the same name has already been " 2597 return TokError(
"expected '>' at end of template argument list");
2607 bool TGParser::ParseBodyItem(
Record *CurRec) {
2609 return ParseDefvar();
2612 if (!ParseDeclaration(CurRec,
false))
2616 return TokError(
"expected ';' after declaration");
2623 return TokError(
"expected field identifier after let");
2625 SMLoc IdLoc = Lex.getLoc();
2630 if (ParseOptionalBitList(BitList))
2635 return TokError(
"expected '=' in let expression");
2640 return TokError(
"Value '" + FieldName->
getValue() +
"' unknown!");
2644 Init *Val = ParseValue(CurRec, Type);
2645 if (!Val)
return true;
2648 return TokError(
"expected ';' after let expression");
2651 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2661 bool TGParser::ParseBody(
Record *CurRec) {
2669 return TokError(
"Expected ';' or '{' to start body");
2677 if (ParseBodyItem(CurRec))
2680 PopLocalScope(BodyScope);
2689 bool TGParser::ApplyLetStack(
Record *CurRec) {
2692 if (
SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
2699 return ApplyLetStack(Entry.
Rec.get());
2701 for (
auto &E : Entry.
Loop->Entries) {
2702 if (ApplyLetStack(E))
2718 bool TGParser::ParseObjectBody(
Record *CurRec) {
2727 if (!SubClass.
Rec)
return true;
2730 if (AddSubClass(CurRec, SubClass))
2735 SubClass = ParseSubClassReference(CurRec,
false);
2739 if (ApplyLetStack(CurRec))
2742 return ParseBody(CurRec);
2750 bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
2751 SMLoc DefLoc = Lex.getLoc();
2756 std::unique_ptr<Record> CurRec;
2757 Init *Name = ParseObjectName(CurMultiClass);
2761 if (isa<UnsetInit>(Name))
2762 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
2765 CurRec = std::make_unique<Record>(
Name, DefLoc, Records);
2767 if (ParseObjectBody(CurRec.get()))
2770 return addEntry(std::move(CurRec));
2777 bool TGParser::ParseDefset() {
2782 Defset.
Loc = Lex.getLoc();
2786 if (!isa<ListRecTy>(Type))
2787 return Error(Defset.
Loc,
"expected list type");
2788 Defset.
EltTy = cast<ListRecTy>(Type)->getElementType();
2791 return TokError(
"expected identifier");
2793 if (Records.getGlobal(DeclName->
getValue()))
2794 return TokError(
"def or global variable of this name already exists");
2797 return TokError(
"expected '='");
2799 return TokError(
"expected '{'");
2800 SMLoc BraceLoc = Lex.getLoc();
2803 Defsets.push_back(&Defset);
2804 bool Err = ParseObjectList(
nullptr);
2810 TokError(
"expected '}' at end of defset");
2811 return Error(BraceLoc,
"to match this '{'");
2815 Records.addExtraGlobal(DeclName->
getValue(),
2824 bool TGParser::ParseDefvar() {
2829 return TokError(
"expected identifier");
2831 if (CurLocalScope) {
2832 if (CurLocalScope->varAlreadyDefined(DeclName->
getValue()))
2833 return TokError(
"local variable of this name already exists");
2835 if (Records.getGlobal(DeclName->
getValue()))
2836 return TokError(
"def or global variable of this name already exists");
2840 return TokError(
"expected '='");
2848 return TokError(
"expected ';'");
2865 bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
2866 SMLoc Loc = Lex.getLoc();
2872 Init *ListValue =
nullptr;
2873 VarInit *IterName = ParseForeachDeclaration(ListValue);
2875 return TokError(
"expected declaration in for");
2878 return TokError(
"Unknown tok");
2882 Loops.push_back(std::make_unique<ForeachLoop>(Loc, IterName, ListValue));
2889 if (ParseObject(CurMultiClass))
2892 SMLoc BraceLoc = Lex.getLoc();
2897 if (ParseObjectList(CurMultiClass))
2901 TokError(
"expected '}' at end of foreach command");
2902 return Error(BraceLoc,
"to match this '{'");
2907 PopLocalScope(ForeachScope);
2910 std::unique_ptr<ForeachLoop> Loop = std::move(
Loops.back());
2921 bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
2922 SMLoc Loc = Lex.getLoc();
2928 Init *Condition = ParseValue(
nullptr);
2933 return TokError(
"Unknown tok");
2948 Init *ThenClauseList =
2952 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
2954 if (ParseIfBody(CurMultiClass,
"then"))
2957 std::unique_ptr<ForeachLoop> Loop = std::move(
Loops.back());
2972 Init *ElseClauseList =
2977 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
2979 if (ParseIfBody(CurMultiClass,
"else"))
2982 Loop = std::move(
Loops.back());
3002 if (ParseObject(CurMultiClass))
3005 SMLoc BraceLoc = Lex.getLoc();
3010 if (ParseObjectList(CurMultiClass))
3014 TokError(
"expected '}' at end of '" + Kind +
"' clause");
3015 return Error(BraceLoc,
"to match this '{'");
3021 PopLocalScope(BodyScope);
3029 bool TGParser::ParseClass() {
3034 return TokError(
"expected class name after 'class' keyword");
3036 Record *CurRec = Records.getClass(Lex.getCurStrVal());
3043 "' already defined");
3047 std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
3049 CurRec = NewRec.get();
3050 Records.addClass(std::move(NewRec));
3056 if (ParseTemplateArgList(CurRec))
3059 return ParseObjectBody(CurRec);
3071 TokError(
"expected identifier in let definition");
3077 SMLoc NameLoc = Lex.getLoc();
3082 if (ParseOptionalRangeList(Bits)) {
3089 TokError(
"expected '=' in let expression");
3095 Init *Val = ParseValue(
nullptr);
3116 bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
3122 ParseLetList(LetInfo);
3123 if (LetInfo.
empty())
return true;
3124 LetStack.push_back(std::move(LetInfo));
3127 return TokError(
"expected 'in' at end of top-level 'let'");
3135 if (ParseObject(CurMultiClass))
3138 SMLoc BraceLoc = Lex.getLoc();
3143 if (ParseObjectList(CurMultiClass))
3147 TokError(
"expected '}' at end of top level let command");
3148 return Error(BraceLoc,
"to match this '{'");
3153 PopLocalScope(LetScope);
3156 LetStack.pop_back();
3170 bool TGParser::ParseMultiClass() {
3175 return TokError(
"expected identifier after multiclass for name");
3176 std::string Name = Lex.getCurStrVal();
3179 MultiClasses.
insert(std::make_pair(Name,
3180 std::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
3183 return TokError(
"multiclass '" + Name +
"' already defined");
3185 CurMultiClass = Result.first->second.get();
3190 if (ParseTemplateArgList(
nullptr))
3193 bool inherits =
false;
3203 ParseSubMultiClassReference(CurMultiClass);
3206 if (!SubMultiClass.
MC)
return true;
3209 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
3214 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
3220 return TokError(
"expected '{' in multiclass definition");
3222 return TokError(
"expected ';' in multiclass definition");
3226 return TokError(
"multiclass must contain at least one def");
3232 switch (Lex.getCode()) {
3234 return TokError(
"expected 'let', 'def', 'defm', 'defvar', 'foreach' " 3235 "or 'if' in multiclass body");
3242 if (ParseObject(CurMultiClass))
3249 PopLocalScope(MulticlassScope);
3252 CurMultiClass =
nullptr;
3260 bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
3264 Init *DefmName = ParseObjectName(CurMultiClass);
3267 if (isa<UnsetInit>(DefmName)) {
3268 DefmName = Records.getNewAnonymousName();
3277 return TokError(
"expected ':' after defm identifier");
3280 std::vector<RecordsEntry> NewEntries;
3283 bool InheritFromClass =
false;
3288 SMLoc SubClassLoc = Lex.getLoc();
3292 if (!Ref.
Rec)
return true;
3298 assert(MC &&
"Didn't lookup multiclass correctly?");
3303 if (TArgs.
size() < TemplateVals.
size())
3304 return Error(SubClassLoc,
3305 "more template args specified than multiclass expects");
3308 for (
unsigned i = 0,
e = TArgs.
size(); i !=
e; ++i) {
3309 if (i < TemplateVals.
size()) {
3310 Substs.emplace_back(TArgs[i], TemplateVals[i]);
3314 return Error(SubClassLoc,
3315 "value not specified for template argument #" +
3316 Twine(i) +
" (" + TArgs[i]->getAsUnquotedString() +
3320 Substs.emplace_back(TArgs[i], Default);
3326 if (resolve(MC->
Entries, Substs, CurMultiClass ==
nullptr, &NewEntries,
3334 return TokError(
"expected identifier");
3336 SubClassLoc = Lex.getLoc();
3340 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
3342 if (InheritFromClass)
3345 Ref = ParseSubClassReference(
nullptr,
true);
3348 if (InheritFromClass) {
3354 if (!SubClass.
Rec)
return true;
3358 for (
auto &E : NewEntries) {
3360 if (AddSubClass(E, SubClass))
3366 SubClass = ParseSubClassReference(
nullptr,
false);
3370 for (
auto &E : NewEntries) {
3371 if (ApplyLetStack(E))
3378 return TokError(
"expected ';' at end of defm");
3394 switch (Lex.getCode()) {
3396 return TokError(
"Expected class, def, defm, defset, multiclass, let, " 3398 case tgtok::Let:
return ParseTopLevelLet(MC);
3405 return TokError(
"defset is not allowed inside multiclass");
3406 return ParseDefset();
3408 return ParseDefvar();
3411 return TokError(
"class is not allowed inside multiclass");
3413 return TokError(
"class is not allowed inside foreach loop");
3414 return ParseClass();
3417 return TokError(
"multiclass is not allowed inside foreach loop");
3418 return ParseMultiClass();
3424 bool TGParser::ParseObjectList(
MultiClass *MC) {
3426 if (ParseObject(MC))
3434 if (ParseObjectList())
return true;
3440 return TokError(
"Unexpected input at top level");
3443 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3452 errs() <<
"foreach " << IterVar->getAsString() <<
" = " 3453 << ListValue->getAsString() <<
" in {\n";
3455 for (
const auto &E : Entries)
3462 errs() <<
"Record:\n";
3465 errs() <<
"Defs:\n";
3466 for (
const auto &E : Entries)
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
unsigned getNumBits() const
Represents a range in source code.
StringRef getName() const
reference emplace_back(ArgTypes &&... Args)
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
virtual std::string getAsUnquotedString() const
Convert this value to a string form, without adding quote markers.
static void SetValue(Value *V, GenericValue Val, ExecutionContext &SF)
std::unique_ptr< ForeachLoop > Loop
[AL, AH, CL] - Represent a list of defs
'7' - Represent an initialization by a literal integer value.
virtual RecTy * getFieldType(StringInit *FieldName) const
This method is used to implement the FieldInit class.
'list<Ty>' - Represent a list of values, all of which must be of the specified type.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
!op (X, Y) - Combine two inits.
void push_back(const T &Elt)
AL - Represent a reference to a 'def' in the description.
static Init * getListConcat(TypedInit *lhs, Init *rhs)
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
std::unique_ptr< Record > Rec
Init * Fold(Record *CurRec, bool IsFinal=false) const
static Init * getStrConcat(Init *lhs, Init *rhs)
static VarInit * get(StringRef VN, RecTy *T)
virtual Init * getCastTo(RecTy *Ty) const =0
If this initializer is convertible to Ty, return an initializer whose type is-a Ty, generating a !cast operation if required.
'{ a, b, c }' - Represents an initializer for a BitsRecTy value.
std::string getNameInitAsString() const
C::iterator addEntry(C &Container, StringRef InstallName)
static IntInit * get(int64_t V)
static BitsInit * get(ArrayRef< Init *> Range)
ArrayRef< std::pair< Record *, SMRange > > getSuperClasses() const
void addSuperClass(Record *R, SMRange Range)
static BitsRecTy * get(unsigned Sz)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This is the common super-class of types that have a specific, explicit, type.
RecTy * getElementType() const
static StringRecTy * get()
ArrayRef< Init * > getTemplateArgs() const
virtual bool isComplete() const
This virtual method should be overridden by values that may not be completely specified yet...
The access may reference the value stored in memory.
void PrintNote(const Twine &Msg)
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
virtual Init * convertInitializerBitRange(ArrayRef< unsigned > Bits) const
This method is used to implement the bitrange selection operator.
ArrayRef< SMLoc > getLoc() const
'bits<n>' - Represent a fixed number of bits
bool isTemplateArg(Init *Name) const
bool isSubClassOf(const Record *R) const
static CodeInit * get(StringRef, const SMLoc &Loc)
virtual Init * convertInitListSlice(ArrayRef< unsigned > Elements) const
This method is used to implement the list slice selection operator.
ArrayRef< RecordVal > getValues() const
std::vector< RecordsEntry > Entries
static Init * QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, StringRef Scoper)
Return an Init with a qualifier prefix referring to CurRec's name.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Init * getElement(unsigned i) const
Resolve arbitrary mappings.
Init * getNameInit() const
The instances of the Type class are immutable: once they are created, they are never changed...
virtual bool isConcrete() const
Is this a concrete and fully resolved value without any references or stuck operations? Unset values are concrete.
void resolveReferences()
If there are any field references that refer to fields that have been filled in, we can propagate the...
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void set(Init *Key, Init *Value)
"foo" - Represent an initialization by a string value.
RecordsEntry - Can be either a record or a foreach loop.
const std::string getNameInitAsString() const
static DagInit * get(Init *V, StringInit *VN, ArrayRef< Init *> ArgRange, ArrayRef< StringInit *> NameRange)
'string' - Represent an string value
static BitInit * get(bool V)
static wasm::ValType getType(const TargetRegisterClass *RC)
static bool isObjectStart(tgtok::TokKind K)
isObjectStart - Return true if this is a valid first token for an Object.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool checkBitsConcrete(Record &R, const RecordVal &RV)
static RecordRecTy * get(ArrayRef< Record *> Classes)
Get the record type with the given non-redundant list of superclasses.
ForeachLoop - Record the iteration state associated with a for loop.
static FieldInit * get(Init *R, StringInit *FN)
virtual Init * resolveReferences(Resolver &R) const
This method is used by classes that refer to other variables which may not be defined at the time the...
void addTemplateArg(Init *Name)
virtual bool typeIsA(const RecTy *RHS) const
Return true if 'this' type is equal to or a subtype of RHS.
static Init * QualifiedNameOfImplicitName(Record &Rec, MultiClass *MC=nullptr)
Return the qualified version of the implicit 'NAME' template argument.
const RecordVal * getValue(const Init *Name) const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
This is a utility class that provides an abstraction for the common functionality between Instruction...
static StringInit * get(StringRef)
SmallVector< Init *, 4 > TemplateArgs
LLVM_NODISCARD T pop_back_val()
CHAIN = SC CHAIN, Imm128 - System call.
SmallVector< Init *, 4 > TemplateArgs
static VarDefInit * get(Record *Class, ArrayRef< Init *> Args)
static IsAOpInit * get(RecTy *CheckType, Init *Expr)
void removeValue(Init *Name)
iterator insert(iterator I, T &&Elt)
static FoldOpInit * get(Init *Start, Init *List, Init *A, Init *B, Init *Expr, RecTy *Type)
Init * getBit(unsigned Bit) const override
This method is used to return the initializer for the specified bit.
void appendLoc(SMLoc Loc)
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Init * getNameInit() const
LLVM_NODISCARD bool empty() const
Init * Fold(Record *CurRec) const
Represents a single loop in the control flow graph.
SmallVector< Init *, 16 > Elements
Do not resolve anything, but keep track of whether a given variable was referenced.
void addValue(const RecordVal &RV)
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
Find a common type that T1 and T2 convert to.
'Opcode' - Represent a reference to an entire variable object.
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static ListRecTy * get(RecTy *T)
Init * Fold(Record *CurRec) const
static ListInit * get(ArrayRef< Init *> Range, RecTy *EltTy)
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of 'this' type can be converted to the specified type.
StringRef getValue() const
'dag' - Represent a dag fragment
StringRef getName() const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
virtual std::string getAsString() const =0
Convert this value to a string form.
Lightweight error class with error context and mandatory checking.
std::vector< RecordsEntry > Entries
static CondOpInit * get(ArrayRef< Init *> C, ArrayRef< Init *> V, RecTy *Type)
StringRef - Represent a constant reference to a string, i.e.
virtual std::string getAsString() const =0
Represents a location in source code.
ListRecTy * getListTy()
Returns the type representing list<this>.
Init * getNameInit() const
static void checkConcrete(Record &R)
Init * Fold(Record *CurRec) const
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
bool empty() const
empty - Check if the array is empty.
void PrintError(ArrayRef< SMLoc > ErrorLoc, const Twine &Msg)