19 #include "llvm/Config/llvm-config.h" 58 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 60 errs() <<
"Multiclass:\n";
64 errs() <<
"Template args:\n";
74 for (
unsigned i = 0, e = BV->
getNumBits(); i != e; ++i) {
76 bool IsReference =
false;
77 if (
auto VBI = dyn_cast<VarBitInit>(Bit)) {
78 if (
auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
82 }
else if (isa<VarInit>(Bit)) {
100 if (
Init *V = RV.getValue()) {
104 Twine(
"Initializer of '") + RV.getNameInitAsString() +
106 "' could not be fully resolved: " +
107 RV.getValue()->getAsString());
120 if (CurMultiClass && Scoper !=
"::") {
126 if (
BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
127 NewName = BinOp->Fold(&CurRec);
143 CurRec = &CurMultiClass->Rec;
148 return Error(Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
150 "previous definition of type '" +
151 ERV->getType()->getAsString() +
"'");
162 bool AllowSelfAssignment) {
163 if (!V)
return false;
165 if (!CurRec) CurRec = &CurMultiClass->Rec;
176 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
177 return Error(Loc,
"Recursion / self-assignment forbidden");
183 if (!BitList.
empty()) {
187 "' is not a bits type");
192 return Error(Loc,
"Initializer is not compatible with bit range");
197 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
198 unsigned Bit = BitList[i];
200 return Error(Loc,
"Cannot set bit #" +
Twine(Bit) +
" of value '" +
202 NewBits[
Bit] = BI->getBit(i);
205 for (
unsigned i = 0, e = CurVal->
getNumBits(); i != e; ++i)
207 NewBits[i] = CurVal->
getBit(i);
213 std::string InitType;
214 if (
BitsInit *BI = dyn_cast<BitsInit>(V))
215 InitType = (
Twine(
"' of type bit initializer with length ") +
216 Twine(BI->getNumBits())).str();
217 else if (
TypedInit *TI = dyn_cast<TypedInit>(V))
218 InitType = (
Twine(
"' of type '") + TI->getType()->getAsString()).str();
221 "' is incompatible with initializer '" +
241 "More template args specified than expected");
247 for (
unsigned i = 0, e = TArgs.
size(); i != e; ++i) {
255 "Value not specified for template argument #" +
256 Twine(i) +
" (" + TArgs[i]->getAsUnquotedString() +
278 for (
const auto &SCPair : SCs) {
281 "Already subclass of '" + SCPair.first->getName() +
"'!\n");
287 "Already subclass of '" + SC->
getName() +
"'!\n");
294 return AddSubClass(Entry.
Rec.get(), SubClass);
296 for (
auto &
E : Entry.
Loop->Entries) {
297 if (AddSubClass(
E, SubClass))
307 bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
314 "More template args specified than expected");
319 for (
unsigned i = 0, e = SMCTArgs.
size(); i != e; ++i) {
321 TemplateArgs.emplace_back(SMCTArgs[i], SubMultiClass.
TemplateArgs[i]);
326 "value not specified for template argument #" +
Twine(i) +
327 " (" + SMCTArgs[i]->getAsUnquotedString() +
331 TemplateArgs.emplace_back(SMCTArgs[i], Default);
335 TemplateArgs.emplace_back(
340 return resolve(SMC->
Entries, TemplateArgs,
false, &CurMC->
Entries);
348 if (!
Loops.empty()) {
349 Loops.back()->Entries.push_back(std::move(E));
355 return resolve(*E.
Loop, Stack, CurMultiClass ==
nullptr,
356 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
360 CurMultiClass->Entries.push_back(std::move(E));
364 return addDefOne(std::move(E.
Rec));
373 bool Final, std::vector<RecordsEntry> *Dest,
376 for (
const auto &S : Substs)
377 R.
set(S.first, S.second);
382 Dest->emplace_back(make_unique<ForeachLoop>(Loop.
Loc, Loop.
IterVar,
384 return resolve(Loop.
Entries, Substs, Final, &Dest->back().Loop->Entries,
394 for (
auto Elt : *LI) {
396 Error = resolve(Loop.
Entries, Substs, Final, Dest);
409 bool TGParser::resolve(
const std::vector<RecordsEntry> &
Source,
410 SubstStack &Substs,
bool Final,
411 std::vector<RecordsEntry> *Dest,
SMLoc *Loc) {
415 Error = resolve(*E.
Loop, Substs, Final, Dest);
417 auto Rec = make_unique<Record>(*E.
Rec);
422 for (
const auto &S : Substs)
423 R.set(S.first, S.second);
427 Dest->push_back(std::move(
Rec));
429 Error = addDefOne(std::move(
Rec));
438 bool TGParser::addDefOne(std::unique_ptr<Record>
Rec) {
439 if (
Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
440 if (!Rec->isAnonymous()) {
442 "def already exists: " + Rec->getNameInitAsString());
443 PrintNote(Prev->getLoc(),
"location of previous definition");
446 Rec->setName(Records.getNewAnonymousName());
449 Rec->resolveReferences();
452 if (!isa<StringInit>(Rec->getNameInit())) {
454 Rec->getNameInit()->getAsString() +
455 "' could not be fully resolved");
460 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
465 PrintError(Rec->getLoc(),
Twine(
"adding record of incompatible type '") +
471 Defset->Elements.push_back(I);
474 Records.addDef(std::move(Rec));
496 switch (Lex.getCode()) {
510 CurRec = &CurMultiClass->
Rec;
533 Record *TGParser::ParseClassID() {
535 TokError(
"expected name for ClassID");
539 Record *Result = Records.getClass(Lex.getCurStrVal());
541 TokError(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
554 TokError(
"expected name for MultiClassID");
558 MultiClass *Result = MultiClasses[Lex.getCurStrVal()].get();
560 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
573 ParseSubClassReference(
Record *CurRec,
bool isDefm) {
579 Result.
Rec = &MC->Rec;
581 Result.
Rec = ParseClassID();
583 if (!Result.
Rec)
return Result;
593 TokError(
"subclass reference requires a non-empty list of template values");
594 Result.
Rec =
nullptr;
600 Result.
Rec =
nullptr;
605 TokError(
"expected '>' in template value list");
606 Result.
Rec =
nullptr;
623 ParseSubMultiClassReference(
MultiClass *CurMC) {
627 Result.
MC = ParseMultiClassID();
628 if (!Result.
MC)
return Result;
638 TokError(
"subclass reference requires a non-empty list of template values");
650 TokError(
"expected '>' in template value list");
666 TokError(
"expected integer or bitrange");
669 int64_t Start = Lex.getCurIntVal();
673 return TokError(
"invalid range, cannot be negative");
681 TokError(
"expected integer value as end of range");
684 End = Lex.getCurIntVal();
687 End = -Lex.getCurIntVal();
691 return TokError(
"invalid range, cannot be negative");
696 for (; Start <= End; ++Start)
699 for (; Start >= End; --Start)
710 if (ParseRangePiece(Result)) {
718 if (ParseRangePiece(Result)) {
732 SMLoc StartLoc = Lex.getLoc();
736 ParseRangeList(Ranges);
737 if (Ranges.
empty())
return true;
740 TokError(
"expected '>' at end of range list");
741 return Error(StartLoc,
"to match this '<'");
754 SMLoc StartLoc = Lex.getLoc();
758 ParseRangeList(Ranges);
759 if (Ranges.
empty())
return true;
762 TokError(
"expected '}' at end of bit list");
763 return Error(StartLoc,
"to match this '{'");
780 RecTy *TGParser::ParseType() {
781 switch (Lex.getCode()) {
782 default: TokError(
"Unknown token when expecting a type");
return nullptr;
790 TokError(
"unknown class name");
794 TokError(
"expected '<' after bits type");
798 TokError(
"expected integer in bits<n> type");
801 uint64_t Val = Lex.getCurIntVal();
803 TokError(
"expected '>' at end of bits<n> type");
811 TokError(
"expected '<' after list type");
815 RecTy *SubType = ParseType();
816 if (!SubType)
return nullptr;
819 TokError(
"expected '>' at end of list<ty> type");
837 if ((CurRec && CurRec->
isClass()) || CurMultiClass) {
838 Init *TemplateArgName;
843 TemplateArgName =
QualifyName(*CurRec, CurMultiClass, Name,
":");
845 Record *TemplateRec = CurMultiClass ? &CurMultiClass->
Rec : CurRec;
848 assert(RV &&
"Template arg doesn't exist??");
850 }
else if (Name->
getValue() ==
"NAME") {
856 for (
const auto &L :
Loops) {
862 if (Mode == ParseNameMode)
870 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
874 Error(NameLoc,
"Variable not defined: '" + Name->
getValue() +
"'");
883 switch (Lex.getCode()) {
885 TokError(
"unknown operation");
895 switch (Lex.getCode()) {
901 Type = ParseOperatorType();
904 TokError(
"did not get type for unary operator");
929 TokError(
"expected '(' after unary operator");
934 Init *LHS = ParseValue(CurRec);
935 if (!LHS)
return nullptr;
943 if (!LHSl && !LHSs && !LHSt) {
944 TokError(
"expected list or string type argument in unary operator");
950 if (!LType && !SType) {
951 TokError(
"expected list or string type argument in unary operator");
958 if (!LHSl && !LHSt) {
959 TokError(
"expected list type argument in unary operator");
965 if (LHSl && LHSl->
empty()) {
966 TokError(
"empty list argument in unary operator");
973 TokError(
"untyped list element in unary operator");
979 assert(LHSt &&
"expected list type argument in unary operator");
982 TokError(
"expected list type argument in unary operator");
991 TokError(
"expected ')' in unary operator");
1007 TokError(
"expected '(' after type of !isa");
1012 Init *LHS = ParseValue(CurRec);
1017 TokError(
"expected ')' in !isa");
1041 SMLoc OpLoc = Lex.getLoc();
1065 RecTy *ArgType =
nullptr;
1105 Error(OpLoc,
Twine(
"expected value of type '") +
1112 TokError(
"expected '(' after binary operator");
1120 SMLoc InitLoc = Lex.getLoc();
1121 InitList.
push_back(ParseValue(CurRec, ArgType));
1122 if (!InitList.
back())
return nullptr;
1131 if (!isa<ListRecTy>(ArgType)) {
1132 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1141 Error(InitLoc,
Twine(
"expected int, bits, or string; got value of " 1151 Error(InitLoc,
Twine(
"expected value of type '") +
1168 TokError(
"expected ')' in operator");
1181 while (InitList.
size() > 2) {
1184 InitList.
back() = RHS;
1188 if (InitList.
size() == 2)
1192 Error(OpLoc,
"expected two operands to operator");
1197 SMLoc OpLoc = Lex.getLoc();
1200 TokError(
"expected '(' after !foreach");
1205 TokError(
"first argument of !foreach must be an identifier");
1211 if (CurRec && CurRec->
getValue(LHS)) {
1213 "' already defined")
1219 TokError(
"expected ',' in ternary operator");
1224 Init *MHS = ParseValue(CurRec);
1229 TokError(
"expected ',' in ternary operator");
1236 TokError(
"could not get type of !foreach input");
1240 RecTy *InEltType =
nullptr;
1241 RecTy *OutEltType =
nullptr;
1245 InEltType = InListTy->getElementType();
1247 if (
ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1248 OutEltType = OutListTy->getElementType();
1252 "', but got !foreach of list type");
1257 InEltType = InDagTy;
1258 if (ItemType && !isa<DagRecTy>(ItemType)) {
1261 "', but got !foreach of dag type");
1266 TokError(
"!foreach must have list or dag input");
1272 std::unique_ptr<Record> ParseRecTmp;
1273 Record *ParseRec = CurRec;
1275 ParseRecTmp = make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
1276 ParseRec = ParseRecTmp.get();
1280 Init *RHS = ParseValue(ParseRec, OutEltType);
1286 TokError(
"expected ')' in binary operator");
1293 OutType = InEltType;
1297 TokError(
"could not get type of !foreach result");
1330 TokError(
"expected '(' after ternary operator");
1335 Init *LHS = ParseValue(CurRec);
1336 if (!LHS)
return nullptr;
1339 TokError(
"expected ',' in ternary operator");
1344 SMLoc MHSLoc = Lex.getLoc();
1345 Init *MHS = ParseValue(CurRec, ItemType);
1350 TokError(
"expected ',' in ternary operator");
1355 SMLoc RHSLoc = Lex.getLoc();
1356 Init *RHS = ParseValue(CurRec, ItemType);
1361 TokError(
"expected ')' in binary operator");
1370 if (!MHSt && !isa<UnsetInit>(MHS)) {
1371 Error(MHSLoc,
"could not determine type of the child list in !dag");
1374 if (MHSt && !isa<ListRecTy>(MHSt->
getType())) {
1375 Error(MHSLoc,
Twine(
"expected list of children, got type '") +
1381 if (!RHSt && !isa<UnsetInit>(RHS)) {
1382 Error(RHSLoc,
"could not determine type of the name list in !dag");
1386 Error(RHSLoc,
Twine(
"expected list<string>, got type '") +
1391 if (!MHSt && !RHSt) {
1393 "cannot have both unset children and unset names in !dag");
1399 RecTy *MHSTy =
nullptr;
1400 RecTy *RHSTy =
nullptr;
1402 if (
TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1403 MHSTy = MHSt->getType();
1404 if (
BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1406 if (isa<BitInit>(MHS))
1409 if (
TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
1410 RHSTy = RHSt->getType();
1411 if (
BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
1413 if (isa<BitInit>(RHS))
1417 if (isa<UnsetInit>(MHS))
1419 if (isa<UnsetInit>(RHS))
1422 if (!MHSTy || !RHSTy) {
1423 TokError(
"could not get type for !if");
1438 TokError(
"could not get type for !subst");
1452 TokError(
"expected '(' after !foldl");
1457 Init *StartUntyped = ParseValue(CurRec);
1463 TokError(
Twine(
"could not get type of !foldl start: '") +
1469 TokError(
"expected ',' in !foldl");
1474 Init *ListUntyped = ParseValue(CurRec);
1480 TokError(
Twine(
"could not get type of !foldl list: '") +
1487 TokError(
Twine(
"!foldl list must be a list, but is of type '") +
1493 TokError(
"expected ',' in !foldl");
1498 TokError(
"third argument of !foldl must be an identifier");
1503 if (CurRec && CurRec->
getValue(A)) {
1505 "' already defined")
1511 TokError(
"expected ',' in !foldl");
1516 TokError(
"fourth argument of !foldl must be an identifier");
1521 if (CurRec && CurRec->
getValue(B)) {
1523 "' already defined")
1529 TokError(
"expected ',' in !foldl");
1536 std::unique_ptr<Record> ParseRecTmp;
1537 Record *ParseRec = CurRec;
1539 ParseRecTmp = make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
1540 ParseRec = ParseRecTmp.get();
1545 Init *ExprUntyped = ParseValue(ParseRec);
1553 TokError(
"could not get type of !foldl expression");
1558 TokError(
Twine(
"!foldl expression must be of same type as start (") +
1565 TokError(
"expected ')' in fold operator");
1581 RecTy *TGParser::ParseOperatorType() {
1585 TokError(
"expected type name for operator");
1593 TokError(
"expected type name for operator");
1598 TokError(
"expected type name for operator");
1628 switch (Lex.getCode()) {
1629 default: TokError(
"Unknown token when parsing a value");
break;
1634 return ParseSimpleValue(CurRec, ItemType, Mode);
1637 auto BinaryVal = Lex.getCurBinaryIntVal();
1639 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
1646 std::string Val = Lex.getCurStrVal();
1651 Val += Lex.getCurStrVal();
1667 SMLoc NameLoc = Lex.getLoc();
1670 return ParseIDValue(CurRec, Name, NameLoc, Mode);
1674 TokError(
"expected non-empty value list");
1683 Error(NameLoc,
"Expected a class name, got '" + Name->
getValue() +
"'");
1688 ParseValueList(Args, CurRec, Class);
1689 if (Args.
empty())
return nullptr;
1692 TokError(
"expected '>' at end of value list");
1699 if (ExpectedArgs.
size() < Args.
size()) {
1701 "More template args specified than expected");
1705 for (
unsigned i = 0, e = ExpectedArgs.
size(); i != e; ++i) {
1707 if (i < Args.
size()) {
1708 if (
TypedInit *TI = dyn_cast<TypedInit>(Args[i])) {
1710 if (!TI->getType()->typeIsConvertibleTo(ExpectedType)) {
1712 "Value specified for template argument #" +
Twine(i) +
" (" +
1714 TI->getType()->getAsString() +
"', expected '" +
1715 ExpectedType->
getAsString() +
"': " + TI->getAsString());
1724 "Value not specified for template argument #" +
Twine(i) +
" (" +
1725 ExpectedArgs[i]->getAsUnquotedString() +
")");
1732 SMLoc BraceLoc = Lex.getLoc();
1737 ParseValueList(Vals, CurRec);
1738 if (Vals.
empty())
return nullptr;
1741 TokError(
"expected '}' at end of bit list value");
1751 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
1756 if (
BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1757 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1758 NewBits.
push_back(BI->getBit((e - i) - 1));
1762 if (
VarInit *
VI = dyn_cast<VarInit>(Vals[i])) {
1763 if (
BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(
VI->getType())) {
1764 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1773 Error(BraceLoc,
"Element #" +
Twine(i) +
" (" + Vals[i]->getAsString() +
1774 ") is not convertable to a bit");
1786 RecTy *DeducedEltTy =
nullptr;
1792 TokError(
Twine(
"Type mismatch for list, expected list type, got ") +
1796 GivenListTy = ListType;
1800 ParseValueList(Vals, CurRec,
nullptr,
1802 if (Vals.
empty())
return nullptr;
1805 TokError(
"expected ']' at end of list value");
1810 RecTy *GivenEltTy =
nullptr;
1815 GivenEltTy = ParseType();
1822 TokError(
"expected '>' at end of list element type");
1829 RecTy *EltTy =
nullptr;
1830 for (
Init *V : Vals) {
1836 TokError(
"Incompatible types in list elements");
1849 TokError(
"Incompatible types in list elements");
1858 TokError(
"No type for list");
1866 TokError(
Twine(
"Element type mismatch for list: element type '") +
1872 DeducedEltTy = EltTy;
1880 TokError(
"expected identifier in dag init");
1885 if (!Operator)
return nullptr;
1891 TokError(
"expected variable name in dag operator");
1900 ParseDagArgList(DagArgs, CurRec);
1901 if (DagArgs.
empty())
return nullptr;
1905 TokError(
"expected ')' in dag init");
1939 return ParseOperation(CurRec, ItemType);
1954 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
1955 if (!Result)
return nullptr;
1959 switch (Lex.getCode()) {
1960 default:
return Result;
1962 if (Mode == ParseNameMode)
1966 SMLoc CurlyLoc = Lex.getLoc();
1969 ParseRangeList(Ranges);
1970 if (Ranges.
empty())
return nullptr;
1976 Error(CurlyLoc,
"Invalid bit range for value");
1982 TokError(
"expected '}' at end of bit range list");
1989 SMLoc SquareLoc = Lex.getLoc();
1992 ParseRangeList(Ranges);
1993 if (Ranges.
empty())
return nullptr;
1997 Error(SquareLoc,
"Invalid range for list slice");
2003 TokError(
"expected ']' at end of list slice");
2011 TokError(
"expected field identifier after '.'");
2016 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
2026 SMLoc PasteLoc = Lex.getLoc();
2033 Error(PasteLoc,
"LHS of paste is not typed!");
2051 switch (Lex.getCode()) {
2064 Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
2067 Error(PasteLoc,
"RHS of paste is not typed!");
2097 void TGParser::ParseDagArgList(
2110 Init *Val = ParseValue(CurRec);
2120 TokError(
"expected variable name in dag literal");
2128 Result.
push_back(std::make_pair(Val, VarName));
2143 RecTy *ItemType = EltTy;
2144 unsigned int ArgN = 0;
2145 if (ArgsRec && !EltTy) {
2147 if (TArgs.
empty()) {
2148 TokError(
"template argument provided to non-template class");
2154 errs() <<
"Cannot find template arg " << ArgN <<
" (" << TArgs[ArgN]
2157 assert(RV &&
"Template argument record not found??");
2161 Result.
push_back(ParseValue(CurRec, ItemType));
2162 if (!Result.
back()) {
2170 if (ArgsRec && !EltTy) {
2172 if (ArgN >= TArgs.
size()) {
2173 TokError(
"too many template arguments");
2178 assert(RV &&
"Template argument record not found??");
2182 Result.
push_back(ParseValue(CurRec, ItemType));
2183 if (!Result.
back()) {
2200 Init *TGParser::ParseDeclaration(
Record *CurRec,
2201 bool ParsingTemplateArgs) {
2204 if (HasField) Lex.Lex();
2207 if (!Type)
return nullptr;
2210 TokError(
"Expected identifier in declaration");
2214 std::string Str = Lex.getCurStrVal();
2215 if (Str ==
"NAME") {
2216 TokError(
"'" + Str +
"' is a reserved variable name");
2220 SMLoc IdLoc = Lex.getLoc();
2224 if (ParsingTemplateArgs) {
2226 DeclName =
QualifyName(*CurRec, CurMultiClass, DeclName,
":");
2230 DeclName =
QualifyName(CurMultiClass->
Rec, CurMultiClass, DeclName,
2235 if (AddValue(CurRec, IdLoc,
RecordVal(DeclName, Type, HasField)))
2241 SMLoc ValLoc = Lex.getLoc();
2242 Init *Val = ParseValue(CurRec, Type);
2262 VarInit *TGParser::ParseForeachDeclaration(
Init *&ForeachListValue) {
2264 TokError(
"Expected identifier in foreach declaration");
2273 TokError(
"Expected '=' in foreach declaration");
2278 RecTy *IterType =
nullptr;
2281 switch (Lex.getCode()) {
2283 if (ParseRangePiece(Ranges))
2290 ParseRangeList(Ranges);
2292 TokError(
"expected '}' at end of bit range list");
2300 SMLoc ValueLoc = Lex.getLoc();
2301 Init *
I = ParseValue(
nullptr);
2303 if (!TI || !isa<ListRecTy>(TI->
getType())) {
2309 PrintNote({},
"references to multiclass template arguments cannot be " 2310 "resolved at this time");
2313 ForeachListValue =
I;
2314 IterType = cast<ListRecTy>(TI->
getType())->getElementType();
2319 if (!Ranges.
empty()) {
2320 assert(!IterType &&
"Type already initialized?");
2322 std::vector<Init*> Values;
2323 for (
unsigned R : Ranges)
2341 bool TGParser::ParseTemplateArgList(
Record *CurRec) {
2345 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->
Rec;
2348 Init *TemplArg = ParseDeclaration(CurRec,
true);
2358 SMLoc Loc = Lex.getLoc();
2359 TemplArg = ParseDeclaration(CurRec,
true);
2364 return Error(Loc,
"template argument with the same name has already been " 2371 return TokError(
"expected '>' at end of template argument list");
2380 bool TGParser::ParseBodyItem(
Record *CurRec) {
2382 if (!ParseDeclaration(CurRec,
false))
2386 return TokError(
"expected ';' after declaration");
2393 return TokError(
"expected field identifier after let");
2395 SMLoc IdLoc = Lex.getLoc();
2400 if (ParseOptionalBitList(BitList))
2405 return TokError(
"expected '=' in let expression");
2410 return TokError(
"Value '" + FieldName->
getValue() +
"' unknown!");
2414 Init *Val = ParseValue(CurRec, Type);
2415 if (!Val)
return true;
2418 return TokError(
"expected ';' after let expression");
2421 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2431 bool TGParser::ParseBody(
Record *CurRec) {
2439 return TokError(
"Expected ';' or '{' to start body");
2444 if (ParseBodyItem(CurRec))
2454 bool TGParser::ApplyLetStack(
Record *CurRec) {
2457 if (
SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
2464 return ApplyLetStack(Entry.
Rec.get());
2466 for (
auto &E : Entry.
Loop->Entries) {
2467 if (ApplyLetStack(E))
2483 bool TGParser::ParseObjectBody(
Record *CurRec) {
2492 if (!SubClass.
Rec)
return true;
2495 if (AddSubClass(CurRec, SubClass))
2500 SubClass = ParseSubClassReference(CurRec,
false);
2504 if (ApplyLetStack(CurRec))
2507 return ParseBody(CurRec);
2515 bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
2516 SMLoc DefLoc = Lex.getLoc();
2521 std::unique_ptr<Record> CurRec;
2522 Init *Name = ParseObjectName(CurMultiClass);
2526 if (isa<UnsetInit>(Name))
2527 CurRec = make_unique<Record>(Records.getNewAnonymousName(), DefLoc, Records,
2530 CurRec = make_unique<Record>(
Name, DefLoc, Records);
2532 if (ParseObjectBody(CurRec.get()))
2535 return addEntry(std::move(CurRec));
2542 bool TGParser::ParseDefset() {
2547 Defset.
Loc = Lex.getLoc();
2551 if (!isa<ListRecTy>(Type))
2552 return Error(Defset.
Loc,
"expected list type");
2553 Defset.
EltTy = cast<ListRecTy>(Type)->getElementType();
2556 return TokError(
"expected identifier");
2558 if (Records.getGlobal(DeclName->
getValue()))
2559 return TokError(
"def or global variable of this name already exists");
2562 return TokError(
"expected '='");
2564 return TokError(
"expected '{'");
2565 SMLoc BraceLoc = Lex.getLoc();
2568 Defsets.push_back(&Defset);
2569 bool Err = ParseObjectList(
nullptr);
2575 TokError(
"expected '}' at end of defset");
2576 return Error(BraceLoc,
"to match this '{'");
2580 Records.addExtraGlobal(DeclName->
getValue(),
2591 bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
2592 SMLoc Loc = Lex.getLoc();
2598 Init *ListValue =
nullptr;
2599 VarInit *IterName = ParseForeachDeclaration(ListValue);
2601 return TokError(
"expected declaration in for");
2604 return TokError(
"Unknown tok");
2608 Loops.push_back(llvm::make_unique<ForeachLoop>(Loc, IterName, ListValue));
2612 if (ParseObject(CurMultiClass))
2615 SMLoc BraceLoc = Lex.getLoc();
2620 if (ParseObjectList(CurMultiClass))
2624 TokError(
"expected '}' at end of foreach command");
2625 return Error(BraceLoc,
"to match this '{'");
2631 std::unique_ptr<ForeachLoop> Loop = std::move(
Loops.back());
2634 return addEntry(std::move(Loop));
2641 bool TGParser::ParseClass() {
2646 return TokError(
"expected class name after 'class' keyword");
2648 Record *CurRec = Records.getClass(Lex.getCurStrVal());
2655 "' already defined");
2659 llvm::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(), Records,
2661 CurRec = NewRec.get();
2662 Records.addClass(std::move(NewRec));
2668 if (ParseTemplateArgList(CurRec))
2671 return ParseObjectBody(CurRec);
2683 TokError(
"expected identifier in let definition");
2689 SMLoc NameLoc = Lex.getLoc();
2694 if (ParseOptionalRangeList(Bits)) {
2701 TokError(
"expected '=' in let expression");
2707 Init *Val = ParseValue(
nullptr);
2728 bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
2734 ParseLetList(LetInfo);
2735 if (LetInfo.
empty())
return true;
2736 LetStack.push_back(std::move(LetInfo));
2739 return TokError(
"expected 'in' at end of top-level 'let'");
2745 if (ParseObject(CurMultiClass))
2748 SMLoc BraceLoc = Lex.getLoc();
2753 if (ParseObjectList(CurMultiClass))
2757 TokError(
"expected '}' at end of top level let command");
2758 return Error(BraceLoc,
"to match this '{'");
2764 LetStack.pop_back();
2778 bool TGParser::ParseMultiClass() {
2783 return TokError(
"expected identifier after multiclass for name");
2784 std::string Name = Lex.getCurStrVal();
2787 MultiClasses.
insert(std::make_pair(Name,
2788 llvm::make_unique<MultiClass>(Name, Lex.getLoc(),Records)));
2791 return TokError(
"multiclass '" + Name +
"' already defined");
2793 CurMultiClass = Result.first->second.get();
2798 if (ParseTemplateArgList(
nullptr))
2801 bool inherits =
false;
2811 ParseSubMultiClassReference(CurMultiClass);
2814 if (!SubMultiClass.
MC)
return true;
2817 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2822 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2828 return TokError(
"expected '{' in multiclass definition");
2830 return TokError(
"expected ';' in multiclass definition");
2834 return TokError(
"multiclass must contain at least one def");
2837 switch (Lex.getCode()) {
2839 return TokError(
"expected 'let', 'def', 'defm' or 'foreach' in " 2845 if (ParseObject(CurMultiClass))
2853 CurMultiClass =
nullptr;
2861 bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
2865 Init *DefmName = ParseObjectName(CurMultiClass);
2868 if (isa<UnsetInit>(DefmName)) {
2869 DefmName = Records.getNewAnonymousName();
2878 return TokError(
"expected ':' after defm identifier");
2881 std::vector<RecordsEntry> NewEntries;
2884 bool InheritFromClass =
false;
2889 SMLoc SubClassLoc = Lex.getLoc();
2893 if (!Ref.
Rec)
return true;
2899 assert(MC &&
"Didn't lookup multiclass correctly?");
2904 if (TArgs.
size() < TemplateVals.
size())
2905 return Error(SubClassLoc,
2906 "more template args specified than multiclass expects");
2909 for (
unsigned i = 0, e = TArgs.
size(); i != e; ++i) {
2910 if (i < TemplateVals.
size()) {
2911 Substs.emplace_back(TArgs[i], TemplateVals[i]);
2915 return Error(SubClassLoc,
2916 "value not specified for template argument #" +
2917 Twine(i) +
" (" + TArgs[i]->getAsUnquotedString() +
2921 Substs.emplace_back(TArgs[i], Default);
2927 if (resolve(MC->
Entries, Substs, CurMultiClass ==
nullptr, &NewEntries,
2935 return TokError(
"expected identifier");
2937 SubClassLoc = Lex.getLoc();
2941 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
2943 if (InheritFromClass)
2946 Ref = ParseSubClassReference(
nullptr,
true);
2949 if (InheritFromClass) {
2955 if (!SubClass.
Rec)
return true;
2959 for (
auto &E : NewEntries) {
2961 if (AddSubClass(E, SubClass))
2967 SubClass = ParseSubClassReference(
nullptr,
false);
2971 for (
auto &E : NewEntries) {
2972 if (ApplyLetStack(E))
2975 addEntry(std::move(E));
2979 return TokError(
"expected ';' at end of defm");
2993 switch (Lex.getCode()) {
2995 return TokError(
"Expected class, def, defm, defset, multiclass, let or " 2997 case tgtok::Let:
return ParseTopLevelLet(MC);
3003 return TokError(
"defset is not allowed inside multiclass");
3004 return ParseDefset();
3007 return TokError(
"class is not allowed inside multiclass");
3009 return TokError(
"class is not allowed inside foreach loop");
3010 return ParseClass();
3013 return TokError(
"multiclass is not allowed inside foreach loop");
3014 return ParseMultiClass();
3020 bool TGParser::ParseObjectList(
MultiClass *MC) {
3022 if (ParseObject(MC))
3030 if (ParseObjectList())
return true;
3036 return TokError(
"Unexpected input at top level");
3039 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3048 errs() <<
"foreach " << IterVar->getAsString() <<
" = " 3049 << ListValue->getAsString() <<
" in {\n";
3051 for (
const auto &E : Entries)
3058 errs() <<
"Record:\n";
3061 errs() <<
"Defs:\n";
3062 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
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
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
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.
!op (X, Y) - Combine two inits.
void push_back(const T &Elt)
AL - Represent a reference to a 'def' in the description.
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
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)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
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.
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
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
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 CodeInit * get(StringRef)
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)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Init * getNameInit() const
void emplace_back(ArgTypes &&... Args)
LLVM_NODISCARD bool empty() 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)
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())
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
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 PrintNote(ArrayRef< SMLoc > NoteLoc, const Twine &Msg)
void PrintError(ArrayRef< SMLoc > ErrorLoc, const Twine &Msg)