20#include "llvm/Config/llvm-config.h"
67#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
69 errs() <<
"Multiclass:\n";
73 errs() <<
"Template args:\n";
81 for (
unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
82 const Init *Bit = BV->getBit(i);
83 bool IsReference =
false;
86 if (R.getValue(VI->getName()))
92 if (!(IsReference || Bit->isConcrete()))
99 for (
const RecordVal &RV : R.getValues()) {
104 if (RV.isNonconcreteOK())
107 if (
const Init *V = RV.getValue()) {
111 RV.getNameInitAsString() +
"' in '" +
112 R.getNameInitAsString() +
113 "' could not be fully resolved: " +
114 RV.getValue()->getAsString());
130 NewName = BinOp->Fold(&CurRec);
150 bool TrackReferenceLocs)
const {
152 auto It = Vars.find(Name->getValue());
153 if (It != Vars.end())
156 auto FindValueInArgs = [&](
Record *Rec,
163 assert(RV &&
"Template arg doesn't exist??");
165 if (TrackReferenceLocs)
169 return Name->getValue() ==
"NAME"
183 if (TrackReferenceLocs)
184 CurRec->appendValueReferenceLoc(Name, NameLoc);
189 if (CurRec->isClass())
190 if (
auto *V = FindValueInArgs(CurRec, Name))
197 if (CurLoop->IterVar) {
198 const VarInit *IterVar = CurLoop->IterVar;
207 if (
auto *V = FindValueInArgs(&CurMultiClass->Rec, Name))
215 return Parent->getVar(Records, ParsingMultiClass, Name, NameLoc,
224 CurRec = &CurMultiClass->
Rec;
229 return Error(
Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
231 "' is incompatible with " +
232 "previous definition of type '" +
233 ERV->getType()->getAsString() +
"'");
246 bool AllowSelfAssignment,
bool OverrideDefLoc,
252 CurRec = &CurMultiClass->Rec;
254 RecordVal *RV = CurRec->
getValue(ValName);
263 if (!BitList.
empty())
264 return Error(Loc,
"Cannot use append/prepend with bit range");
266 const Init *CurrentValue = RV->
getValue();
267 const RecTy *FieldType = RV->
getType();
273 const Init *
LHS = IsAppendMode ? CurrentValue :
V;
274 const Init *
RHS = IsAppendMode ?
V : CurrentValue;
284 return Error(Loc, Twine(
"Cannot ") +
285 (IsAppendMode ?
"append to" :
"prepend to") +
287 "' of type '" + FieldType->getAsString() +
288 "' (expected list, string, code, or dag)");
298 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
299 return Error(Loc,
"Recursion / self-assignment forbidden");
303 if (!BitList.
empty()) {
307 "' is not a bits type");
312 return Error(Loc,
"Initializer is not compatible with bit range");
317 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
318 unsigned Bit = BitList[i];
320 return Error(Loc,
"Cannot set bit #" + Twine(Bit) +
" of value '" +
326 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
328 NewBits[i] = CurVal->getBit(i);
334 std::string InitType;
336 InitType = (Twine(
"' of type bit initializer with length ") +
337 Twine(BI->getNumBits()))
341 (Twine(
"' of type '") + TI->getType()->getAsString() +
"'").str();
345 "' is incompatible with value '" +
V->getAsString() +
355 MapResolver
R(CurRec);
360 if (!
Field.isTemplateArg())
364 if (resolveArgumentsOfClass(R, SC, SubClass.
TemplateArgs,
388 "Already subclass of '" + SC->
getName() +
"'!\n");
395 return AddSubClass(
Entry.Rec.get(), SubClass);
400 for (
auto &
E :
Entry.Loop->Entries) {
401 if (AddSubClass(
E, SubClass))
411bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
416 if (resolveArgumentsOfMultiClass(
429 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion + !!
E.Dump) == 1 &&
430 "RecordsEntry has invalid number of items");
433 if (!Loops.empty()) {
434 Loops.back()->Entries.push_back(std::move(
E));
441 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
442 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
447 CurMultiClass->Entries.push_back(std::move(
E));
453 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
463 return addDefOne(std::move(
E.Rec));
471bool TGParser::resolve(
const ForeachLoop &
Loop, SubstStack &Substs,
bool Final,
472 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
475 for (
const auto &S : Substs)
476 R.set(S.first, S.second);
477 const Init *
List =
Loop.ListValue->resolveReferences(R);
487 const Init *OldLHS = TI->getLHS();
493 "' at end of containing scope");
496 const Init *MHS = TI->getMHS();
497 const Init *
RHS = TI->getRHS();
506 std::make_unique<ForeachLoop>(
Loop.Loc,
Loop.IterVar,
List));
507 return resolve(
Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
512 List->getAsString() +
"', expected a list");
517 for (
auto *Elt : *LI) {
519 Substs.emplace_back(
Loop.IterVar->getNameInit(), Elt);
520 Error = resolve(
Loop.Entries, Substs, Final, Dest);
534bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
535 SubstStack &Substs,
bool Final,
536 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
538 for (
auto &
E : Source) {
540 Error = resolve(*
E.Loop, Substs, Final, Dest);
542 }
else if (
E.Assertion) {
544 for (
const auto &S : Substs)
545 R.set(S.first, S.second);
546 const Init *Condition =
E.Assertion->Condition->resolveReferences(R);
547 const Init *Message =
E.Assertion->Message->resolveReferences(R);
550 Dest->push_back(std::make_unique<Record::AssertionInfo>(
551 E.Assertion->Loc, Condition, Message));
557 for (
const auto &S : Substs)
558 R.set(S.first, S.second);
563 std::make_unique<Record::DumpInfo>(
E.Dump->Loc, Message));
568 auto Rec = std::make_unique<Record>(*
E.Rec);
570 Rec->appendLoc(*Loc);
572 MapResolver
R(Rec.get());
573 for (
const auto &S : Substs)
574 R.set(S.first, S.second);
575 Rec->resolveReferences(R);
578 Dest->push_back(std::move(Rec));
580 Error = addDefOne(std::move(Rec));
589bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
590 const Init *NewName =
nullptr;
591 if (
const Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
592 if (!Rec->isAnonymous()) {
594 "def already exists: " + Rec->getNameInitAsString());
595 PrintNote(Prev->getLoc(),
"location of previous definition");
598 NewName = Records.getNewAnonymousName();
601 Rec->resolveReferences(NewName);
605 PrintError(Rec->getLoc(), Twine(
"record name '") +
606 Rec->getNameInit()->getAsString() +
607 "' could not be fully resolved");
612 Rec->checkRecordAssertions();
615 Rec->emitRecordDumps();
618 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
620 for (DefsetRecord *Defset : Defsets) {
621 DefInit *
I = Rec->getDefInit();
622 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
623 PrintError(Rec->getLoc(), Twine(
"adding record of incompatible type '") +
624 I->getType()->getAsString() +
632 Records.addDef(std::move(Rec));
636bool TGParser::resolveArguments(
const Record *Rec,
638 SMLoc Loc, ArgValueHandler ArgValueHandler) {
641 "Too many template arguments allowed");
645 for (
auto *Arg : ArgValues) {
646 const Init *ArgName =
nullptr;
647 const Init *ArgValue = Arg->getValue();
648 if (Arg->isPositional())
649 ArgName = ArgNames[Arg->getIndex()];
651 ArgName = Arg->getName();
655 return Error(Loc,
"We can only specify the template argument '" +
658 ArgValueHandler(ArgName, ArgValue);
663 for (
auto *UnsolvedArgName : UnsolvedArgNames) {
666 std::string
Name = UnsolvedArgName->getAsUnquotedString();
667 Error(Loc,
"value not specified for template argument '" + Name +
"'");
672 ArgValueHandler(UnsolvedArgName,
Default);
683 return resolveArguments(
685 [&](
const Init *Name,
const Init *
Value) {
R.set(Name,
Value); });
690bool TGParser::resolveArgumentsOfMultiClass(
695 return resolveArguments(&MC->
Rec, ArgValues, Loc,
696 [&](
const Init *Name,
const Init *
Value) {
697 Substs.emplace_back(Name, Value);
706 if (Lex.getCode() == K) {
720 switch (Lex.getCode()) {
734 CurRec = &CurMultiClass->Rec;
743 HasReferenceResolver
R(NameStr);
744 Name->resolveReferences(R);
758const Record *TGParser::ParseClassID() {
760 TokError(
"expected name for ClassID");
764 const Record *
Result = Records.getClass(Lex.getCurStrVal());
766 std::string
Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
767 if (MultiClasses[Lex.getCurStrVal()].get())
768 TokError(
Msg +
". Use 'defm' if you meant to use multiclass '" +
769 Lex.getCurStrVal() +
"'");
772 }
else if (TrackReferenceLocs) {
773 Result->appendReferenceLoc(Lex.getLocRange());
787 TokError(
"expected name for MultiClassID");
793 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
808 Result.RefRange.Start = Lex.getLoc();
811 if (MultiClass *MC = ParseMultiClassID())
814 Result.Rec = ParseClassID();
821 Result.RefRange.End = Lex.getLoc();
826 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
832 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
837 Result.RefRange.End = Lex.getLoc();
849TGParser::ParseSubMultiClassReference(
MultiClass *CurMC) {
850 SubMultiClassReference
Result;
851 Result.RefRange.Start = Lex.getLoc();
853 Result.MC = ParseMultiClassID();
859 Result.RefRange.End = Lex.getLoc();
864 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, &CurMC->
Rec,
870 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs, &
Result.MC->Rec)) {
875 Result.RefRange.End = Lex.getLoc();
891 auto LHSLoc = Lex.getLoc();
892 auto *CurVal = ParseValue(CurRec);
897 const TypedInit *
RHS =
nullptr;
898 switch (Lex.getCode()) {
902 auto RHSLoc = Lex.getLoc();
903 CurVal = ParseValue(CurRec);
909 "expected int...int, got " + Twine(
RHS->
getType()->getAsString()));
915 auto i = -Lex.getCurIntVal();
917 TokError(
"invalid range, cannot be negative");
934 "expected int...int, got " + Twine(
LHS->
getType()->getAsString()));
955 const TypedInit *CurVal;
959 auto FlushElems = [&] {
960 if (!Elems.
empty()) {
967 auto LHSLoc = Lex.getLoc();
968 CurVal = ParseSliceElement(CurRec);
971 auto *CurValTy = CurVal->
getType();
976 "expected list<int>, got " + Twine(ListValTy->getAsString()));
986 "unhandled type " + Twine(CurValTy->getAsString()) +
" in range");
1013 const TypedInit *
Result =
nullptr;
1014 for (
auto *Slice : Slices) {
1030 const Init *CurVal = FirstItem;
1032 CurVal = ParseValue(
nullptr);
1036 return TokError(
"expected integer or bitrange");
1038 int64_t
Start =
II->getValue();
1042 return TokError(
"invalid range, cannot be negative");
1044 switch (Lex.getCode()) {
1053 const Init *I_End = ParseValue(
nullptr);
1056 TokError(
"expected integer value as end of range");
1060 End = II_End->getValue();
1064 End = -Lex.getCurIntVal();
1070 return TokError(
"invalid range, cannot be negative");
1088 if (ParseRangePiece(Result)) {
1094 if (ParseRangePiece(Result)) {
1104 SMLoc StartLoc = Lex.getLoc();
1109 ParseRangeList(Ranges);
1114 TokError(
"expected '}' at end of bit list");
1115 return Error(StartLoc,
"to match this '{'");
1131const RecTy *TGParser::ParseType() {
1132 switch (Lex.getCode()) {
1134 TokError(
"Unknown token when expecting a type");
1150 auto I = TypeAliases.find(Lex.getCurStrVal());
1151 if (
I != TypeAliases.end()) {
1155 if (
const Record *R = ParseClassID())
1162 TokError(
"expected '<' after bits type");
1166 TokError(
"expected integer in bits<n> type");
1171 TokError(
"expected '>' at end of bits<n> type");
1179 TokError(
"expected '<' after list type");
1183 const RecTy *SubType = ParseType();
1188 TokError(
"expected '>' at end of list<ty> type");
1199 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass, Name, NameLoc,
1200 TrackReferenceLocs))
1203 if (
Mode == ParseNameMode)
1206 if (
const Init *
I = Records.getGlobal(
Name->getValue())) {
1208 if (TrackReferenceLocs) {
1210 Def->getDef()->appendReferenceLoc(NameLoc);
1217 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1221 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1229const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1230 switch (Lex.getCode()) {
1249 const RecTy *
Type =
nullptr;
1251 switch (Lex.getCode()) {
1258 Type = ParseOperatorType();
1261 TokError(
"did not get type for unary operator");
1320 Type = ParseOperatorType();
1323 TokError(
"did not get type for unary operator");
1328 TokError(
"type for !getdagop must be a record type");
1348 TokError(
"expected '(' after unary operator");
1352 const Init *
LHS = ParseValue(CurRec);
1361 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1363 "expected string, list, or dag type argument in unary operator");
1369 "expected string, list, or dag type argument in unary operator");
1379 if (!LHSl && !LHSt) {
1380 TokError(
"expected list type argument in unary operator");
1385 TokError(
"expected list type argument in unary operator");
1390 if (LHSl && LHSl->empty()) {
1391 TokError(
"empty list argument in unary operator");
1394 bool UseElementType =
1397 const Init *Item = LHSl->getElement(0);
1400 TokError(
"untyped list element in unary operator");
1403 Type = UseElementType ? Itemt->getType()
1406 assert(LHSt &&
"expected list type argument in unary operator");
1408 Type = UseElementType ? LType->getElementType() : LType;
1426 TokError(
"expected ')' in unary operator");
1436 const RecTy *
Type = ParseOperatorType();
1441 TokError(
"expected '(' after type of !isa");
1445 const Init *
LHS = ParseValue(CurRec);
1461 const RecTy *
Type = ParseOperatorType();
1466 TokError(
"expected '(' after type of !exists");
1470 SMLoc ExprLoc = Lex.getLoc();
1471 const Init *Expr = ParseValue(CurRec);
1477 Error(ExprLoc,
"expected string type argument in !exists operator");
1484 "expected string type argument in !exists operator, please "
1485 "use !isa instead");
1491 Error(ExprLoc,
"expected string type argument in !exists operator");
1496 TokError(
"expected ')' in !exists");
1507 const RecTy *
Type = ParseOperatorType();
1512 TokError(
"expected '(' after type of !instances");
1519 SMLoc RegexLoc = Lex.getLoc();
1520 Regex = ParseValue(CurRec);
1524 Error(RegexLoc,
"expected string type argument in !instances operator");
1530 Error(RegexLoc,
"expected string type argument in !instances operator");
1539 TokError(
"expected ')' in !instances");
1574 SMLoc OpLoc = Lex.getLoc();
1664 const RecTy *
Type =
nullptr;
1665 const RecTy *ArgType =
nullptr;
1679 Type = ParseOperatorType();
1681 TokError(
"did not get type for !getdagarg operator");
1736 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1738 "', got '" +
Type->getAsString() +
"'");
1743 TokError(
"expected '(' after binary operator");
1752 SMLoc InitLoc = Lex.getLoc();
1753 InitList.
push_back(ParseValue(CurRec, ArgType));
1754 if (!InitList.
back())
1758 if (!InitListBack) {
1759 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1760 InitList.
back()->getAsString() +
"'"));
1763 const RecTy *ListType = InitListBack->getType();
1772 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1778 if (ItemType && InitList.
size() == 1) {
1781 Twine(
"expected output type to be a list, got type '") +
1786 Error(OpLoc, Twine(
"expected first arg type to be '") +
1788 "', got value of type '" +
1797 Error(InitLoc, Twine(
"expected second parameter to be an int, got "
1798 "value of type '") +
1806 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1816 Error(InitLoc, Twine(
"expected bit, bits, int, string, or record; "
1817 "got value of type '") +
1830 Error(InitLoc, Twine(
"expected bit, bits, int, or string; "
1831 "got value of type '") +
1837 switch (InitList.
size()) {
1843 Twine(
"expected list of string, int, bits, or bit; "
1844 "got value of type '") +
1851 Error(InitLoc, Twine(
"expected second argument to be a string, "
1852 "got value of type '") +
1869 Error(InitLoc, Twine(
"expected value of type '") +
1912 TokError(
"expected ')' in operator");
1932 while (InitList.
size() > 2) {
1939 if (InitList.
size() == 2)
1943 Error(OpLoc,
"expected two operands to operator");
1950 return ParseOperationListComprehension(CurRec, ItemType);
1954 SMLoc OpLoc = Lex.getLoc();
1958 TokError(
"expected '(' after !range operator");
1963 bool FirstArgIsList =
false;
1965 if (
Args.size() >= 3) {
1966 TokError(
"expected at most three values of integer");
1970 SMLoc InitLoc = Lex.getLoc();
1971 Args.push_back(ParseValue(CurRec));
1977 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1978 Args.back()->getAsString() +
"'"));
1982 const RecTy *ArgBackType = ArgBack->getType();
1983 if (!FirstArgIsList ||
Args.size() == 1) {
1985 FirstArgIsList =
true;
1989 if (
Args.size() != 1)
1990 Error(InitLoc, Twine(
"expected value of type 'int', got '" +
1993 Error(InitLoc, Twine(
"expected list or int, got value of type '") +
2000 Error(InitLoc, Twine(
"expected one list, got extra value of type '") +
2009 TokError(
"expected ')' in operator");
2013 const Init *
LHS, *MHS, *
RHS;
2014 auto ArgCount =
Args.size();
2017 const auto *Arg0Ty = Arg0->getType();
2018 if (ArgCount == 1) {
2038 if (ArgCount == 3) {
2059 const RecTy *
Type =
nullptr;
2089 TokError(
"expected '(' after ternary operator");
2093 const Init *
LHS = ParseValue(CurRec);
2098 TokError(
"expected ',' in ternary operator");
2102 SMLoc MHSLoc = Lex.getLoc();
2103 const Init *MHS = ParseValue(CurRec, ItemType);
2108 TokError(
"expected ',' in ternary operator");
2112 SMLoc RHSLoc = Lex.getLoc();
2113 const Init *
RHS = ParseValue(CurRec, ItemType);
2118 TokError(
"expected ')' in binary operator");
2128 Error(MHSLoc,
"could not determine type of the child list in !dag");
2132 Error(MHSLoc, Twine(
"expected list of children, got type '") +
2133 MHSt->getType()->getAsString() +
"'");
2139 Error(RHSLoc,
"could not determine type of the name list in !dag");
2143 Error(RHSLoc, Twine(
"expected list<string>, got type '") +
2144 RHSt->getType()->getAsString() +
"'");
2148 if (!MHSt && !RHSt) {
2150 "cannot have both unset children and unset names in !dag");
2156 const RecTy *MHSTy =
nullptr;
2157 const RecTy *RHSTy =
nullptr;
2160 MHSTy = MHSt->getType();
2167 RHSTy = RHSt->getType();
2179 if (!MHSTy || !RHSTy) {
2180 TokError(
"could not get type for !if");
2195 TokError(
"could not get type for !subst");
2198 Type = RHSt->getType();
2204 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2205 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2215 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2216 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2224 Error(RHSLoc, Twine(
"expected string or unset name, got type '") +
2225 RHSt->getType()->getAsString() +
"'");
2235 return ParseOperationSubstr(CurRec, ItemType);
2238 return ParseOperationFind(CurRec, ItemType);
2241 return ParseOperationCond(CurRec, ItemType);
2244 return ParseOperationSwitch(CurRec, ItemType);
2250 TokError(
"expected '(' after !foldl");
2254 const Init *StartUntyped = ParseValue(CurRec);
2260 TokError(Twine(
"could not get type of !foldl start: '") +
2266 TokError(
"expected ',' in !foldl");
2270 const Init *ListUntyped = ParseValue(CurRec);
2276 TokError(Twine(
"could not get type of !foldl list: '") +
2283 TokError(Twine(
"!foldl list must be a list, but is of type '") +
2284 List->getType()->getAsString());
2289 TokError(
"expected ',' in !foldl");
2294 TokError(
"third argument of !foldl must be an identifier");
2300 TokError(Twine(
"left !foldl variable '") +
A->getAsString() +
2301 "' already defined");
2306 TokError(
"expected ',' in !foldl");
2311 TokError(
"fourth argument of !foldl must be an identifier");
2317 TokError(Twine(
"right !foldl variable '") +
B->getAsString() +
2318 "' already defined");
2323 TokError(
"expected ',' in !foldl");
2330 std::unique_ptr<Record> ParseRecTmp;
2331 Record *ParseRec = CurRec;
2335 ParseRec = ParseRecTmp.get();
2338 TGVarScope *FoldScope =
PushScope(ParseRec);
2342 const Init *ExprUntyped = ParseValue(ParseRec);
2343 ParseRec->removeValue(
A);
2344 ParseRec->removeValue(
B);
2351 TokError(
"could not get type of !foldl expression");
2355 if (Expr->getType() !=
Start->getType()) {
2356 TokError(Twine(
"!foldl expression must be of same type as start (") +
2357 Start->getType()->getAsString() +
"), but is of type " +
2363 TokError(
"expected ')' in fold operator");
2378const RecTy *TGParser::ParseOperatorType() {
2379 const RecTy *
Type =
nullptr;
2382 TokError(
"expected type name for operator");
2387 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2392 TokError(
"expected type name for operator");
2397 TokError(
"expected type name for operator");
2407const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2408 const RecTy *ItemType) {
2415 TokError(
"expected '(' after !substr operator");
2419 const Init *
LHS = ParseValue(CurRec);
2424 TokError(
"expected ',' in !substr operator");
2428 SMLoc MHSLoc = Lex.getLoc();
2429 const Init *MHS = ParseValue(CurRec);
2433 SMLoc RHSLoc = Lex.getLoc();
2436 RHSLoc = Lex.getLoc();
2437 RHS = ParseValue(CurRec);
2445 TokError(
"expected ')' in !substr operator");
2449 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2450 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2451 "', got '" +
Type->getAsString() +
"'");
2456 TokError(
"could not determine type of the string in !substr");
2460 TokError(Twine(
"expected string, got type '") +
2461 LHSt->getType()->getAsString() +
"'");
2467 TokError(
"could not determine type of the start position in !substr");
2471 Error(MHSLoc, Twine(
"expected int, got type '") +
2472 MHSt->getType()->getAsString() +
"'");
2479 TokError(
"could not determine type of the length in !substr");
2483 TokError(Twine(
"expected int, got type '") +
2484 RHSt->getType()->getAsString() +
"'");
2495const Init *TGParser::ParseOperationFind(
Record *CurRec,
2496 const RecTy *ItemType) {
2503 TokError(
"expected '(' after !find operator");
2507 const Init *
LHS = ParseValue(CurRec);
2512 TokError(
"expected ',' in !find operator");
2516 SMLoc MHSLoc = Lex.getLoc();
2517 const Init *MHS = ParseValue(CurRec);
2521 SMLoc RHSLoc = Lex.getLoc();
2524 RHSLoc = Lex.getLoc();
2525 RHS = ParseValue(CurRec);
2533 TokError(
"expected ')' in !find operator");
2537 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2538 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2539 "', got '" +
Type->getAsString() +
"'");
2544 TokError(
"could not determine type of the source string in !find");
2548 TokError(Twine(
"expected string, got type '") +
2549 LHSt->getType()->getAsString() +
"'");
2555 TokError(
"could not determine type of the target string in !find");
2559 Error(MHSLoc, Twine(
"expected string, got type '") +
2560 MHSt->getType()->getAsString() +
"'");
2567 TokError(
"could not determine type of the start position in !find");
2571 TokError(Twine(
"expected int, got type '") +
2572 RHSt->getType()->getAsString() +
"'");
2585const Init *TGParser::ParseOperationListComprehension(
Record *CurRec,
2586 const RecTy *ItemType) {
2587 SMLoc OpLoc = Lex.getLoc();
2591 TokError(
"expected '(' after !foreach/!filter");
2596 TokError(
"first argument of !foreach/!filter must be an identifier");
2604 TokError(Twine(
"iteration variable '") +
LHS->getAsString() +
2605 "' is already defined");
2610 TokError(
"expected ',' in !foreach/!filter");
2614 const Init *MHS = ParseValue(CurRec);
2619 TokError(
"expected ',' in !foreach/!filter");
2625 TokError(
"could not get type of !foreach/!filter list or dag");
2629 const RecTy *InEltType =
nullptr;
2630 const RecTy *ExprEltType =
nullptr;
2634 InEltType = InListTy->getElementType();
2639 ExprEltType = OutListTy->getElementType();
2645 ExprEltType =
nullptr;
2651 Error(OpLoc,
"expected value of type '" +
2653 "', but got list type");
2660 TokError(
"!filter must have a list argument");
2663 TokError(
"!sort must have a list argument");
2670 InEltType = InDagTy;
2673 "', but got dag type");
2680 TokError(
"!foreach must have a list or dag argument");
2683 TokError(
"!filter must have a list argument");
2686 TokError(
"!sort must have a list argument");
2695 std::unique_ptr<Record> ParseRecTmp;
2696 Record *ParseRec = CurRec;
2700 ParseRec = ParseRecTmp.get();
2702 TGVarScope *TempScope =
PushScope(ParseRec);
2704 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2705 ParseRec->removeValue(
LHS);
2711 TokError(
"expected ')' in !foreach/!filter");
2715 const RecTy *OutType;
2721 OutType = InEltType;
2725 TokError(
"could not get type of !foreach result expression");
2749std::optional<const RecTy *>
2751 const RecTy *
Type =
nullptr;
2752 for (
const Init *V : Inits) {
2764 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
"' and '" +
2766 return std::nullopt;
2776const Init *TGParser::ParseOperationCond(
Record *CurRec,
2777 const RecTy *ItemType) {
2781 TokError(
"expected '(' after !cond operator");
2792 const Init *
V = ParseValue(CurRec);
2798 TokError(
"expected ':' following a condition in !cond operator");
2802 V = ParseValue(CurRec, ItemType);
2811 TokError(
"expected ',' or ')' following a value in !cond operator");
2816 if (Cases.
size() < 1) {
2818 "there should be at least 1 'condition : value' in the !cond operator");
2823 std::optional<const RecTy *> TypeOpt = resolveInitTypes(Vals,
"for !cond");
2826 const RecTy *
Type = *TypeOpt;
2828 TokError(
"could not determine type for !cond from its arguments");
2835const Init *TGParser::ParseOperationSwitch(
Record *CurRec,
2836 const RecTy *ItemType) {
2840 TokError(
"expected '(' after !switch operator");
2845 const Init *
Key = ParseValue(CurRec);
2852 TokError(
"expected ',' after key in !switch operator");
2860 const Init *
V = ParseValue(CurRec);
2869 if (
const Init *Coerced =
V->convertInitializerTo(ItemType))
2879 TokError(
"expected ':' after case key, or ')' to close !switch operator");
2884 V = ParseValue(CurRec, ItemType);
2890 TokError(
"expected ',' after case value in !switch operator");
2895 "inconsistent keys and values for !switch");
2897 if (KeyAndCases.
size() < 2) {
2899 "there should be at least 1 'case: value' in the !switch operator");
2904 std::optional<const RecTy *> ValTypeOpt =
2905 resolveInitTypes(Vals,
"for !switch values");
2908 const RecTy *
ValType = *ValTypeOpt;
2910 TokError(
"could not determine type for !switch from its arguments");
2917 if (!resolveInitTypes(KeyAndCases,
"between !switch key and case keys"))
2923 size_t ValsSize = Vals.
size();
2930 assert(Conds.
size() == ValsSize &&
"inconsistent !switch to !cond reduction");
2961const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2963 const Init *
R =
nullptr;
2968 return ParseOperation(CurRec, ItemType);
2972 TokError(
"Unknown or reserved token when parsing a value");
2988 auto BinaryVal = Lex.getCurBinaryIntVal();
2990 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2997 std::string Val = Lex.getCurStrVal();
3002 Val += Lex.getCurStrVal();
3018 SMRange NameLoc = Lex.getLocRange();
3024 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
3032 "Expected a class name, got '" +
Name->getValue() +
"'");
3039 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
3042 if (CheckTemplateArgValues(Args, ArgLocs, Class))
3045 if (resolveArguments(Class, Args, NameLoc.
Start))
3048 if (TrackReferenceLocs)
3049 Class->appendReferenceLoc(NameLoc);
3053 SMLoc BraceLoc = Lex.getLoc();
3058 ParseValueList(Vals, CurRec);
3063 TokError(
"expected '}' at end of bit list value");
3072 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
3078 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
3085 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
3094 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
3095 ") is not convertable to a bit");
3100 std::reverse(NewBits.
begin(), NewBits.
end());
3107 const RecTy *DeducedEltTy =
nullptr;
3108 const ListRecTy *GivenListTy =
nullptr;
3113 TokError(Twine(
"Encountered a list when expecting a ") +
3117 GivenListTy = ListType;
3121 ParseValueList(Vals, CurRec,
3127 TokError(
"expected ']' at end of list value");
3131 const RecTy *GivenEltTy =
nullptr;
3134 GivenEltTy = ParseType();
3141 TokError(
"expected '>' at end of list element type");
3147 const RecTy *EltTy =
nullptr;
3148 for (
const Init *V : Vals) {
3154 TokError(
"Incompatible types in list elements");
3158 EltTy = TArg->getType();
3167 TokError(
"Incompatible types in list elements");
3184 TokError(Twine(
"Element type mismatch for list: element type '") +
3190 DeducedEltTy = EltTy;
3201 TokError(
"expected identifier or list of value types in dag init");
3205 const Init *
Operator = ParseValue(CurRec);
3210 const StringInit *OperatorName =
nullptr;
3213 TokError(
"expected variable name in dag operator");
3222 ParseDagArgList(DagArgs, CurRec);
3223 if (DagArgs.
empty())
3228 TokError(
"expected ')' in dag init");
3246const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3248 SMLoc LHSLoc = Lex.getLoc();
3249 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3255 switch (Lex.getCode()) {
3259 if (
Mode == ParseNameMode)
3263 SMLoc CurlyLoc = Lex.getLoc();
3265 SmallVector<unsigned, 16>
Ranges;
3266 ParseRangeList(Ranges);
3274 Error(CurlyLoc,
"Invalid bit range for value");
3280 TokError(
"expected '}' at end of bit range list");
3288 Error(LHSLoc,
"Invalid value, list expected");
3295 "' is invalid, list expected");
3300 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3309 LHSTy->getElementType())
3317 TokError(
"expected ']' at end of list slice");
3324 TokError(
"expected field identifier after '.'");
3327 SMRange FieldNameLoc = Lex.getLocRange();
3328 const StringInit *FieldName =
3330 if (!
Result->getFieldType(FieldName)) {
3331 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3332 Result->getAsString() +
"'");
3337 if (TrackReferenceLocs) {
3339 DI->getDef()->appendValueReferenceLoc(FieldName, FieldNameLoc);
3342 for (
const Record *R : RecTy->getClasses())
3343 if (
R->getValue(FieldName))
3344 R->appendValueReferenceLoc(FieldName, FieldNameLoc);
3355 SMLoc PasteLoc = Lex.getLoc();
3358 Error(PasteLoc,
"LHS of paste is not typed!");
3366 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3368 switch (Lex.getCode()) {
3375 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3392 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3398 const TypedInit *
RHS =
nullptr;
3401 switch (Lex.getCode()) {
3414 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3419 Error(PasteLoc,
"RHS of paste is not typed!");
3429 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3450void TGParser::ParseDagArgList(
3463 const Init *Val = ParseValue(CurRec);
3470 const StringInit *
VarName =
nullptr;
3473 TokError(
"expected variable name in dag literal");
3481 Result.emplace_back(Val, VarName);
3496 Result.push_back(ParseValue(CurRec, ItemType));
3506 Result.push_back(ParseValue(CurRec, ItemType));
3522bool TGParser::ParseTemplateArgValueList(
3525 assert(
Result.empty() &&
"Result vector is not empty");
3531 bool HasNamedArg =
false;
3532 unsigned ArgIndex = 0;
3534 if (ArgIndex >= TArgs.
size()) {
3535 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3542 const Init *
Value = ParseValue(
3544 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3551 return Error(ValueLoc,
3552 "The name of named argument should be a valid identifier");
3556 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3558 return Error(ValueLoc,
3559 "Argument " +
Name->getAsString() +
" doesn't exist");
3562 ValueLoc = Lex.getLoc();
3563 Value = ParseValue(CurRec, NamedArg->getType());
3566 return Error(ValueLoc,
3567 "The value of named argument should be initialized, "
3569 Value->getAsString() +
"'");
3576 return Error(ValueLoc,
3577 "Positional argument should be put before named argument");
3585 return TokError(
"Expected comma before next argument");
3600const Init *TGParser::ParseDeclaration(
Record *CurRec,
3601 bool ParsingTemplateArgs) {
3605 const RecTy *
Type = ParseType();
3610 TokError(
"Expected identifier in declaration");
3614 std::string Str = Lex.getCurStrVal();
3615 if (Str ==
"NAME") {
3616 TokError(
"'" + Str +
"' is a reserved variable name");
3620 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3621 TokError(
"local variable of this name already exists");
3625 SMLoc IdLoc = Lex.getLoc();
3630 if (!ParsingTemplateArgs) {
3631 BadField = AddValue(CurRec, IdLoc,
3632 RecordVal(DeclName, IdLoc,
Type,
3635 }
else if (CurRec) {
3638 AddValue(CurRec, IdLoc,
3641 assert(CurMultiClass &&
"invalid context for template argument");
3644 AddValue(CurRec, IdLoc,
3652 SMLoc ValLoc = Lex.getLoc();
3653 const Init *Val = ParseValue(CurRec,
Type);
3655 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3676TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3678 TokError(
"Expected identifier in foreach declaration");
3687 TokError(
"Expected '=' in foreach declaration");
3691 const RecTy *IterType =
nullptr;
3692 SmallVector<unsigned, 16>
Ranges;
3694 switch (Lex.getCode()) {
3697 ParseRangeList(Ranges);
3699 TokError(
"expected '}' at end of bit range list");
3706 SMLoc ValueLoc = Lex.getLoc();
3707 const Init *
I = ParseValue(
nullptr);
3713 ForeachListValue =
I;
3719 if (ParseRangePiece(Ranges, TI))
3724 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3725 if (CurMultiClass) {
3726 PrintNote({},
"references to multiclass template arguments cannot be "
3727 "resolved at this time");
3734 assert(!IterType &&
"Type already initialized?");
3736 std::vector<Init *>
Values;
3737 for (
unsigned R : Ranges)
3755bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3759 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3762 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3770 SMLoc Loc = Lex.getLoc();
3771 TemplArg = ParseDeclaration(CurRec,
true );
3776 return Error(Loc,
"template argument with the same name has already been "
3783 return TokError(
"expected '>' at end of template argument list");
3800 SMLoc Loc = Lex.getLoc();
3802 std::string CurStr = Lex.getCurStrVal();
3804 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3814 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3832bool TGParser::ParseBodyItem(
Record *CurRec) {
3834 return ParseAssert(
nullptr, CurRec);
3837 return ParseDefvar(CurRec);
3840 return ParseDump(
nullptr, CurRec);
3843 if (!ParseDeclaration(CurRec,
false))
3847 return TokError(
"expected ';' after declaration");
3855 return TokError(
"expected field identifier after let");
3857 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3860 SmallVector<unsigned, 16> BitList;
3861 if (ParseOptionalRangeList(BitList))
3863 std::reverse(BitList.
begin(), BitList.
end());
3866 return TokError(
"expected '=' in let expression");
3870 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3879 const Init *Val = ParseValue(CurRec,
Type);
3884 return TokError(
"expected ';' after let expression");
3886 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3897bool TGParser::ParseBody(
Record *CurRec) {
3903 return TokError(
"Expected '{' to start body or ';' for declaration only");
3906 if (ParseBodyItem(CurRec))
3913 SMLoc SemiLoc = Lex.getLoc();
3915 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3916 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3924bool TGParser::ApplyLetStack(
Record *CurRec) {
3925 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3926 for (LetRecord &LR : LetInfo)
3927 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3937 return ApplyLetStack(
Entry.Rec.get());
3940 if (
Entry.Assertion)
3947 for (
auto &
E :
Entry.Loop->Entries) {
3948 if (ApplyLetStack(
E))
3964bool TGParser::ParseObjectBody(
Record *CurRec) {
3966 TGVarScope *ObjectScope =
PushScope(CurRec);
3971 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3978 if (AddSubClass(CurRec, SubClass))
3983 SubClass = ParseSubClassReference(CurRec,
false);
3987 if (ApplyLetStack(CurRec))
3990 bool Result = ParseBody(CurRec);
4000bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
4001 SMLoc DefLoc = Lex.getLoc();
4008 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
4011 std::unique_ptr<Record> CurRec;
4012 const Init *
Name = ParseObjectName(CurMultiClass);
4017 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
4020 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
4023 if (ParseObjectBody(CurRec.get()))
4026 return addEntry(std::move(CurRec));
4033bool TGParser::ParseDefset() {
4038 Defset.Loc = Lex.getLoc();
4039 const RecTy *
Type = ParseType();
4047 return TokError(
"expected identifier");
4048 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4049 if (Records.getGlobal(DeclName->
getValue()))
4050 return TokError(
"def or global variable of this name already exists");
4056 SMLoc BraceLoc = Lex.getLoc();
4059 Defsets.push_back(&Defset);
4060 bool Err = ParseObjectList(
nullptr);
4066 TokError(
"expected '}' at end of defset");
4067 return Error(BraceLoc,
"to match this '{'");
4070 Records.addExtraGlobal(DeclName->
getValue(),
4079bool TGParser::ParseDeftype() {
4084 return TokError(
"expected identifier");
4086 const std::string
TypeName = Lex.getCurStrVal();
4087 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
4088 return TokError(
"type of this name '" + TypeName +
"' already exists");
4094 SMLoc Loc = Lex.getLoc();
4095 const RecTy *
Type = ParseType();
4100 return Error(Loc,
"cannot define type alias for class type '" +
4101 Type->getAsString() +
"'");
4115bool TGParser::ParseDefvar(
Record *CurRec) {
4120 return TokError(
"expected identifier");
4121 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4122 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
4123 return TokError(
"local variable of this name already exists");
4128 if (V && !
V->isTemplateArg())
4129 return TokError(
"field of this name already exists");
4134 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
4135 return TokError(
"def or global variable of this name already exists");
4141 const Init *
Value = ParseValue(CurRec);
4148 if (!CurScope->isOutermost())
4162bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4163 SMLoc Loc = Lex.getLoc();
4169 const Init *ListValue =
nullptr;
4170 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4172 return TokError(
"expected declaration in for");
4178 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4180 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4181 Loops.push_back(std::move(TheLoop));
4185 if (ParseObject(CurMultiClass))
4188 SMLoc BraceLoc = Lex.getLoc();
4193 if (ParseObjectList(CurMultiClass))
4197 TokError(
"expected '}' at end of foreach command");
4198 return Error(BraceLoc,
"to match this '{'");
4205 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
4208 return addEntry(std::move(
Loop));
4216bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4217 SMLoc Loc = Lex.getLoc();
4223 const Init *Condition = ParseValue(
nullptr);
4237 const ListInit *SingletonList =
4243 const Init *ThenClauseList =
4247 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4249 if (ParseIfBody(CurMultiClass,
"then"))
4252 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
4255 if (addEntry(std::move(
Loop)))
4265 const Init *ElseClauseList =
4270 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4272 if (ParseIfBody(CurMultiClass,
"else"))
4275 Loop = std::move(Loops.back());
4278 if (addEntry(std::move(
Loop)))
4296 if (ParseObject(CurMultiClass))
4299 SMLoc BraceLoc = Lex.getLoc();
4304 if (ParseObjectList(CurMultiClass))
4308 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4309 return Error(BraceLoc,
"to match this '{'");
4324 SMLoc ConditionLoc = Lex.getLoc();
4325 const Init *Condition = ParseValue(CurRec);
4330 TokError(
"expected ',' in assert statement");
4334 const Init *Message = ParseValue(CurRec);
4342 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4344 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4353bool TGParser::ParseClass() {
4358 return TokError(
"expected class name after 'class' keyword");
4360 const std::string &
Name = Lex.getCurStrVal();
4361 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4368 "' already defined");
4373 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4375 CurRec = NewRec.get();
4376 Records.addClass(std::move(NewRec));
4379 if (TypeAliases.count(Name))
4380 return TokError(
"there is already a defined type alias '" + Name +
"'");
4385 TGVarScope *ClassScope =
PushScope(CurRec);
4388 if (ParseTemplateArgList(CurRec))
4391 if (ParseObjectBody(CurRec))
4394 if (!NoWarnOnUnusedTemplateArgs)
4410 TokError(
"expected identifier in let definition");
4415 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4419 SmallVector<unsigned, 16>
Bits;
4420 if (ParseOptionalRangeList(Bits)) {
4424 std::reverse(
Bits.begin(),
Bits.end());
4427 TokError(
"expected '=' in let expression");
4432 const Init *Val = ParseValue(
nullptr);
4439 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4449bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4455 ParseLetList(LetInfo);
4456 if (LetInfo.
empty())
4458 LetStack.push_back(std::move(LetInfo));
4461 return TokError(
"expected 'in' at end of top-level 'let'");
4466 if (ParseObject(CurMultiClass))
4469 SMLoc BraceLoc = Lex.getLoc();
4477 if (ParseObjectList(CurMultiClass))
4481 TokError(
"expected '}' at end of top level let command");
4482 return Error(BraceLoc,
"to match this '{'");
4489 LetStack.pop_back();
4506bool TGParser::ParseMultiClass() {
4511 return TokError(
"expected identifier after multiclass for name");
4512 std::string
Name = Lex.getCurStrVal();
4514 auto Result = MultiClasses.try_emplace(
4515 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4518 return TokError(
"multiclass '" + Name +
"' already defined");
4520 CurMultiClass =
Result.first->second.get();
4524 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4528 if (ParseTemplateArgList(
nullptr))
4531 bool inherits =
false;
4538 SubMultiClassReference SubMultiClass =
4539 ParseSubMultiClassReference(CurMultiClass);
4542 if (!SubMultiClass.
MC)
4546 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4551 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4557 return TokError(
"expected '{' in multiclass definition");
4559 return TokError(
"expected ';' in multiclass definition");
4562 return TokError(
"multiclass must contain at least one def");
4565 switch (Lex.getCode()) {
4567 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4568 "'foreach', 'if', or 'let' in multiclass body");
4578 if (ParseObject(CurMultiClass))
4586 SMLoc SemiLoc = Lex.getLoc();
4588 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4589 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4593 if (!NoWarnOnUnusedTemplateArgs)
4594 CurMultiClass->Rec.checkUnusedTemplateArgs();
4597 CurMultiClass =
nullptr;
4605bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4609 const Init *DefmName = ParseObjectName(CurMultiClass);
4613 DefmName = Records.getNewAnonymousName();
4622 return TokError(
"expected ':' after defm identifier");
4625 std::vector<RecordsEntry> NewEntries;
4628 bool InheritFromClass =
false;
4633 SMLoc SubClassLoc = Lex.getLoc();
4634 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4644 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4645 assert(MC &&
"Didn't lookup multiclass correctly?");
4648 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4652 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4653 &NewEntries, &SubClassLoc))
4660 return TokError(
"expected identifier");
4662 SubClassLoc = Lex.getLoc();
4666 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4668 if (InheritFromClass)
4671 Ref = ParseSubClassReference(
nullptr,
true);
4674 if (InheritFromClass) {
4677 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4685 for (
auto &
E : NewEntries) {
4687 if (AddSubClass(
E, SubClass))
4693 SubClass = ParseSubClassReference(
nullptr,
false);
4697 for (
auto &
E : NewEntries) {
4698 if (ApplyLetStack(
E))
4701 addEntry(std::move(
E));
4705 return TokError(
"expected ';' at end of defm");
4723 switch (Lex.getCode()) {
4726 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4728 return ParseAssert(MC);
4730 return ParseDef(MC);
4732 return ParseDefm(MC);
4734 return ParseDeftype();
4736 return ParseDefvar();
4738 return ParseDump(MC);
4740 return ParseForeach(MC);
4744 return ParseTopLevelLet(MC);
4747 return TokError(
"defset is not allowed inside multiclass");
4748 return ParseDefset();
4751 return TokError(
"class is not allowed inside multiclass");
4753 return TokError(
"class is not allowed inside foreach loop");
4754 return ParseClass();
4757 return TokError(
"multiclass is not allowed inside foreach loop");
4758 return ParseMultiClass();
4764bool TGParser::ParseObjectList(
MultiClass *MC) {
4766 if (ParseObject(MC))
4775 if (ParseObjectList())
4786 return TokError(
"Unexpected token at top level");
4793bool TGParser::CheckTemplateArgValues(
4797 "expected as many values as locations");
4801 bool HasError =
false;
4803 const Init *ArgName =
nullptr;
4804 if (
Value->isPositional())
4805 ArgName = TArgs[
Value->getIndex()];
4806 if (
Value->isNamed())
4813 auto *CastValue = ArgValue->
getCastTo(ArgType);
4817 "result of template arg value cast has wrong type");
4821 Loc,
"Value specified for template argument '" +
4823 ArgValue->getType()->
getAsString() +
"; expected type " +
4832#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4841 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4842 <<
ListValue->getAsString() <<
" in {\n";
4851 errs() <<
"Record:\n";
4854 errs() <<
"Defs:\n";
4866 const Init *Message = ParseValue(CurRec);
4886 addEntry(std::make_unique<Record::DumpInfo>(
Loc, ResolvedMessage));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
PowerPC Reduce CR logical Operation
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static bool checkBitsConcrete(Record &R, const RecordVal &RV)
static const Init * QualifyName(const Record &CurRec, const Init *Name)
Return an Init with a qualifier prefix referring to CurRec's name.
static const Init * QualifiedNameOfImplicitName(const Record &Rec)
Return the qualified version of the implicit 'NAME' template argument.
static void checkConcrete(Record &R)
static SymbolRef::Type getType(const Symbol *Sym)
static const ArgumentInit * get(const Init *Value, ArgAuxType Aux)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
bool empty() const
Check if the array is empty.
static const BinOpInit * get(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
static const Init * getStrConcat(const Init *lhs, const Init *rhs)
static const Init * getListConcat(const TypedInit *lhs, const Init *rhs)
const Init * Fold(const Record *CurRec) const
static BitInit * get(RecordKeeper &RK, bool V)
static const BitRecTy * get(RecordKeeper &RK)
static BitsInit * get(RecordKeeper &RK, ArrayRef< const Init * > Range)
static const BitsRecTy * get(RecordKeeper &RK, unsigned Sz)
const Init * Fold(const Record *CurRec) const
static const CondOpInit * get(ArrayRef< const Init * > Conds, ArrayRef< const Init * > Values, const RecTy *Type)
static const DagInit * get(const Init *V, const StringInit *VN, ArrayRef< const Init * > Args, ArrayRef< const StringInit * > ArgNames)
static const DagRecTy * get(RecordKeeper &RK)
static const ExistsOpInit * get(const RecTy *CheckType, const Init *Expr)
const Init * Fold(const Record *CurRec) const
static const FieldInit * get(const Init *R, const StringInit *FN)
const Init * Fold(const Record *CurRec) const
static const FoldOpInit * get(const Init *Start, const Init *List, const Init *A, const Init *B, const Init *Expr, const RecTy *Type)
Do not resolve anything, but keep track of whether a given variable was referenced.
virtual const Init * resolveReferences(Resolver &R) const
This function is used by classes that refer to other variables which may not be defined at the time t...
virtual std::string getAsUnquotedString() const
Convert this value to a literal form, without adding quotes around a string.
virtual std::string getAsString() const =0
Convert this value to a literal form.
virtual const Init * getBit(unsigned Bit) const =0
Get the Init value of the specified bit.
virtual const Init * getCastTo(const RecTy *Ty) const =0
If this value is convertible to type Ty, return a value whose type is Ty, generating a !...
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
static const InstancesOpInit * get(const RecTy *Type, const Init *Regex)
static IntInit * get(RecordKeeper &RK, int64_t V)
static const IntRecTy * get(RecordKeeper &RK)
static const IsAOpInit * get(const RecTy *CheckType, const Init *Expr)
const Init * Fold() const
static const ListInit * get(ArrayRef< const Init * > Range, const RecTy *EltTy)
const RecTy * getElementType() const
static const ListRecTy * get(const RecTy *T)
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Represents a single loop in the control flow graph.
Resolve arbitrary mappings.
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of 'this' type can be converted to the specified type.
virtual std::string getAsString() const =0
const ListRecTy * getListTy() const
Returns the type representing list<thistype>.
static const RecordRecTy * get(RecordKeeper &RK, ArrayRef< const Record * > Classes)
Get the record type with the given non-redundant list of superclasses.
This class represents a field in a record, including its name, type, value, and source location.
std::string getNameInitAsString() const
Get the name of the field as a std::string.
void setUsed(bool Used)
Whether this value is used.
bool setValue(const Init *V)
Set the value of the field from an Init.
const Init * getValue() const
Get the value of the field as an Init.
StringRef getName() const
Get the name of the field as a StringRef.
const Init * getNameInit() const
Get the name of the field as an Init.
const RecTy * getType() const
Get the type of the field value as a RecTy.
const RecordRecTy * getType() const
void addDump(SMLoc Loc, const Init *Message)
void checkUnusedTemplateArgs()
void appendValueReferenceLoc(const Init *Name, SMRange Loc) const
Add a reference to the value named Name.
std::string getNameInitAsString() const
void appendValueReferenceLocs(const Record &From, const Init *Name) const
Add the references to the value named Name in From.
RecordKeeper & getRecords() const
const RecordVal * getValue(const Init *Name) const
void addTemplateArg(const Init *Name)
bool isMultiClass() const
void addValue(const RecordVal &RV)
void addAssertion(SMLoc Loc, const Init *Condition, const Init *Message)
ArrayRef< std::pair< const Record *, SMRange > > getDirectSuperClasses() const
Return the direct superclasses of this record.
StringRef getName() const
bool isTemplateArg(const Init *Name) const
void appendDumps(const Record *Rec)
bool isSubClassOf(const Record *R) const
ArrayRef< RecordVal > getValues() const
SMLoc getFieldLoc(StringRef FieldName) const
Return the source location for the named field.
void resolveReferences(const Init *NewName=nullptr)
If there are any field references that refer to fields that have been filled in, we can propagate the...
ArrayRef< const Init * > getTemplateArgs() const
void updateClassLoc(SMLoc Loc)
void addDirectSuperClass(const Record *R, SMRange Range)
void appendAssertions(const Record *Rec)
const Init * getNameInit() const
void setFinal(bool Final)
Represents a location in source code.
Represents a range in source code.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
"foo" - Represent an initialization by a string value.
static const StringInit * get(RecordKeeper &RK, StringRef, StringFormat Fmt=SF_String)
StringRef getValue() const
static const StringRecTy * get(RecordKeeper &RK)
Represent a constant reference to a string, i.e.
tgtok::TokKind getCode() const
void PopScope(TGVarScope *ExpectedStackTop)
bool Error(SMLoc L, const Twine &Msg) const
bool TokError(const Twine &Msg) const
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
const Init * getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass, const StringInit *Name, SMRange NameLoc, bool TrackReferenceLocs) const
const Init * Fold(const Record *CurRec) const
static const TernOpInit * get(TernaryOp opc, const Init *lhs, const Init *mhs, const Init *rhs, const RecTy *Type)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This is the common superclass of types that have a specific, explicit type, stored in ValueTy.
const RecTy * getType() const
Get the type of the Init as a RecTy.
static const UnOpInit * get(UnaryOp opc, const Init *lhs, const RecTy *Type)
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
static UnsetInit * get(RecordKeeper &RK)
Get the singleton unset Init.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
static const VarDefInit * get(SMLoc Loc, const Record *Class, ArrayRef< const ArgumentInit * > Args)
const Init * Fold() const
'Opcode' - Represent a reference to an entire variable object.
static const VarInit * get(StringRef VN, const RecTy *T)
const Init * getNameInit() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ Resolved
Queried, materialization begun.
NodeAddr< DefNode * > Def
NodeAddr< CodeNode * > Code
static bool isBangOperator(tgtok::TokKind Kind)
isBangOperator - Return true if this is a bang operator.
static bool isObjectStart(tgtok::TokKind Kind)
isObjectStart - Return true if this is a valid first token for a statement.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
RelativeUniformCounterPtr Values
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void PrintError(const Twine &Msg)
std::string utostr(uint64_t X, bool isNeg=false)
LetMode
Specifies how a 'let' assignment interacts with the existing field value.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
LLVM_ABI void PrintNote(const Twine &Msg)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void dumpMessage(SMLoc Loc, const Init *Message)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
@ End
Run the machine verifier once, at the end of the pipeline.
Implement std::hash so that hash_code can be used in STL containers.
ForeachLoop - Record the iteration state associated with a for loop.
std::vector< RecordsEntry > Entries
Parsed let mode keyword and field name (e.g.
std::vector< RecordsEntry > Entries
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
std::unique_ptr< ForeachLoop > Loop
std::unique_ptr< Record::AssertionInfo > Assertion
std::unique_ptr< Record::DumpInfo > Dump
std::unique_ptr< Record > Rec
SubClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs
SubMultiClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs