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 RV->addReferenceLoc(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,
223 CurRec = &CurMultiClass->
Rec;
228 return Error(
Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
230 "' is incompatible with " +
231 "previous definition of type '" +
232 ERV->getType()->getAsString() +
"'");
243 bool AllowSelfAssignment,
bool OverrideDefLoc,
249 CurRec = &CurMultiClass->Rec;
251 RecordVal *RV = CurRec->
getValue(ValName);
260 if (!BitList.
empty())
261 return Error(Loc,
"Cannot use append/prepend with bit range");
263 const Init *CurrentValue = RV->
getValue();
264 const RecTy *FieldType = RV->
getType();
270 const Init *
LHS = IsAppendMode ? CurrentValue :
V;
271 const Init *
RHS = IsAppendMode ?
V : CurrentValue;
281 return Error(Loc, Twine(
"Cannot ") +
282 (IsAppendMode ?
"append to" :
"prepend to") +
284 "' of type '" + FieldType->getAsString() +
285 "' (expected list, string, code, or dag)");
295 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
296 return Error(Loc,
"Recursion / self-assignment forbidden");
300 if (!BitList.
empty()) {
304 "' is not a bits type");
309 return Error(Loc,
"Initializer is not compatible with bit range");
314 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
315 unsigned Bit = BitList[i];
317 return Error(Loc,
"Cannot set bit #" + Twine(Bit) +
" of value '" +
323 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
325 NewBits[i] = CurVal->getBit(i);
331 std::string InitType;
333 InitType = (Twine(
"' of type bit initializer with length ") +
334 Twine(BI->getNumBits()))
338 (Twine(
"' of type '") + TI->getType()->getAsString() +
"'").str();
342 "' is incompatible with value '" +
V->getAsString() +
352 MapResolver
R(CurRec);
357 if (!
Field.isTemplateArg())
361 if (resolveArgumentsOfClass(R, SC, SubClass.
TemplateArgs,
385 "Already subclass of '" + SC->
getName() +
"'!\n");
392 return AddSubClass(
Entry.Rec.get(), SubClass);
397 for (
auto &
E :
Entry.Loop->Entries) {
398 if (AddSubClass(
E, SubClass))
408bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
413 if (resolveArgumentsOfMultiClass(
426 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion + !!
E.Dump) == 1 &&
427 "RecordsEntry has invalid number of items");
430 if (!Loops.empty()) {
431 Loops.back()->Entries.push_back(std::move(
E));
438 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
439 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
444 CurMultiClass->Entries.push_back(std::move(
E));
450 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
460 return addDefOne(std::move(
E.Rec));
468bool TGParser::resolve(
const ForeachLoop &
Loop, SubstStack &Substs,
bool Final,
469 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
472 for (
const auto &S : Substs)
473 R.set(S.first, S.second);
484 const Init *OldLHS = TI->getLHS();
488 PrintError(Loop.
Loc, Twine(
"unable to resolve if condition '") +
490 "' at end of containing scope");
493 const Init *MHS = TI->getMHS();
494 const Init *
RHS = TI->getRHS();
504 return resolve(Loop.
Entries, Substs, Final, &Dest->back().Loop->Entries,
509 List->getAsString() +
"', expected a list");
514 for (
auto *Elt : *LI) {
531bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
532 SubstStack &Substs,
bool Final,
533 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
535 for (
auto &
E : Source) {
537 Error = resolve(*
E.Loop, Substs, Final, Dest);
539 }
else if (
E.Assertion) {
541 for (
const auto &S : Substs)
542 R.set(S.first, S.second);
543 const Init *Condition =
E.Assertion->Condition->resolveReferences(R);
544 const Init *Message =
E.Assertion->Message->resolveReferences(R);
547 Dest->push_back(std::make_unique<Record::AssertionInfo>(
548 E.Assertion->Loc, Condition, Message));
554 for (
const auto &S : Substs)
555 R.set(S.first, S.second);
560 std::make_unique<Record::DumpInfo>(
E.Dump->Loc, Message));
565 auto Rec = std::make_unique<Record>(*
E.Rec);
567 Rec->appendLoc(*Loc);
569 MapResolver
R(Rec.get());
570 for (
const auto &S : Substs)
571 R.set(S.first, S.second);
572 Rec->resolveReferences(R);
575 Dest->push_back(std::move(Rec));
577 Error = addDefOne(std::move(Rec));
586bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
587 const Init *NewName =
nullptr;
588 if (
const Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
589 if (!Rec->isAnonymous()) {
591 "def already exists: " + Rec->getNameInitAsString());
592 PrintNote(Prev->getLoc(),
"location of previous definition");
595 NewName = Records.getNewAnonymousName();
598 Rec->resolveReferences(NewName);
602 PrintError(Rec->getLoc(), Twine(
"record name '") +
603 Rec->getNameInit()->getAsString() +
604 "' could not be fully resolved");
609 Rec->checkRecordAssertions();
612 Rec->emitRecordDumps();
615 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
617 for (DefsetRecord *Defset : Defsets) {
618 DefInit *
I = Rec->getDefInit();
619 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
620 PrintError(Rec->getLoc(), Twine(
"adding record of incompatible type '") +
621 I->getType()->getAsString() +
629 Records.addDef(std::move(Rec));
633bool TGParser::resolveArguments(
const Record *Rec,
635 SMLoc Loc, ArgValueHandler ArgValueHandler) {
638 "Too many template arguments allowed");
642 for (
auto *Arg : ArgValues) {
643 const Init *ArgName =
nullptr;
644 const Init *ArgValue = Arg->getValue();
645 if (Arg->isPositional())
646 ArgName = ArgNames[Arg->getIndex()];
648 ArgName = Arg->getName();
652 return Error(Loc,
"We can only specify the template argument '" +
655 ArgValueHandler(ArgName, ArgValue);
660 for (
auto *UnsolvedArgName : UnsolvedArgNames) {
663 std::string
Name = UnsolvedArgName->getAsUnquotedString();
664 Error(Loc,
"value not specified for template argument '" + Name +
"'");
669 ArgValueHandler(UnsolvedArgName,
Default);
680 return resolveArguments(
682 [&](
const Init *Name,
const Init *
Value) {
R.set(Name,
Value); });
687bool TGParser::resolveArgumentsOfMultiClass(
692 return resolveArguments(&MC->
Rec, ArgValues, Loc,
693 [&](
const Init *Name,
const Init *
Value) {
694 Substs.emplace_back(Name, Value);
703 if (Lex.getCode() == K) {
717 switch (Lex.getCode()) {
731 CurRec = &CurMultiClass->Rec;
740 HasReferenceResolver
R(NameStr);
741 Name->resolveReferences(R);
755const Record *TGParser::ParseClassID() {
757 TokError(
"expected name for ClassID");
761 const Record *
Result = Records.getClass(Lex.getCurStrVal());
763 std::string Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
764 if (MultiClasses[Lex.getCurStrVal()].get())
765 TokError(Msg +
". Use 'defm' if you meant to use multiclass '" +
766 Lex.getCurStrVal() +
"'");
769 }
else if (TrackReferenceLocs) {
770 Result->appendReferenceLoc(Lex.getLocRange());
784 TokError(
"expected name for MultiClassID");
790 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
805 Result.RefRange.Start = Lex.getLoc();
808 if (MultiClass *MC = ParseMultiClassID())
811 Result.Rec = ParseClassID();
818 Result.RefRange.End = Lex.getLoc();
823 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
829 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
834 Result.RefRange.End = Lex.getLoc();
846TGParser::ParseSubMultiClassReference(
MultiClass *CurMC) {
847 SubMultiClassReference
Result;
848 Result.RefRange.Start = Lex.getLoc();
850 Result.MC = ParseMultiClassID();
856 Result.RefRange.End = Lex.getLoc();
861 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, &CurMC->
Rec,
867 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs, &
Result.MC->Rec)) {
872 Result.RefRange.End = Lex.getLoc();
888 auto LHSLoc = Lex.getLoc();
889 auto *CurVal = ParseValue(CurRec);
894 const TypedInit *
RHS =
nullptr;
895 switch (Lex.getCode()) {
899 auto RHSLoc = Lex.getLoc();
900 CurVal = ParseValue(CurRec);
906 "expected int...int, got " + Twine(
RHS->
getType()->getAsString()));
912 auto i = -Lex.getCurIntVal();
914 TokError(
"invalid range, cannot be negative");
931 "expected int...int, got " + Twine(
LHS->
getType()->getAsString()));
951const TypedInit *TGParser::ParseSliceElements(
Record *CurRec,
bool Single) {
952 const TypedInit *CurVal;
956 auto FlushElems = [&] {
957 if (!Elems.
empty()) {
964 auto LHSLoc = Lex.getLoc();
965 CurVal = ParseSliceElement(CurRec);
968 auto *CurValTy = CurVal->
getType();
973 "expected list<int>, got " + Twine(ListValTy->getAsString()));
983 "unhandled type " + Twine(CurValTy->getAsString()) +
" in range");
1010 const TypedInit *
Result =
nullptr;
1011 for (
auto *Slice : Slices) {
1027 const Init *CurVal = FirstItem;
1029 CurVal = ParseValue(
nullptr);
1033 return TokError(
"expected integer or bitrange");
1035 int64_t
Start =
II->getValue();
1039 return TokError(
"invalid range, cannot be negative");
1041 switch (Lex.getCode()) {
1050 const Init *I_End = ParseValue(
nullptr);
1053 TokError(
"expected integer value as end of range");
1057 End = II_End->getValue();
1061 End = -Lex.getCurIntVal();
1067 return TokError(
"invalid range, cannot be negative");
1085 if (ParseRangePiece(Result)) {
1091 if (ParseRangePiece(Result)) {
1101 SMLoc StartLoc = Lex.getLoc();
1106 ParseRangeList(Ranges);
1111 TokError(
"expected '}' at end of bit list");
1112 return Error(StartLoc,
"to match this '{'");
1128const RecTy *TGParser::ParseType() {
1129 switch (Lex.getCode()) {
1131 TokError(
"Unknown token when expecting a type");
1147 auto I = TypeAliases.find(Lex.getCurStrVal());
1148 if (
I != TypeAliases.end()) {
1152 if (
const Record *R = ParseClassID())
1159 TokError(
"expected '<' after bits type");
1163 TokError(
"expected integer in bits<n> type");
1166 uint64_t Val = Lex.getCurIntVal();
1168 TokError(
"expected '>' at end of bits<n> type");
1176 TokError(
"expected '<' after list type");
1180 const RecTy *SubType = ParseType();
1185 TokError(
"expected '>' at end of list<ty> type");
1196 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass, Name, NameLoc,
1197 TrackReferenceLocs))
1200 if (
Mode == ParseNameMode)
1203 if (
const Init *
I = Records.getGlobal(
Name->getValue())) {
1205 if (TrackReferenceLocs) {
1207 Def->getDef()->appendReferenceLoc(NameLoc);
1214 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1218 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1226const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1227 switch (Lex.getCode()) {
1246 const RecTy *
Type =
nullptr;
1248 switch (Lex.getCode()) {
1255 Type = ParseOperatorType();
1258 TokError(
"did not get type for unary operator");
1317 Type = ParseOperatorType();
1320 TokError(
"did not get type for unary operator");
1325 TokError(
"type for !getdagop must be a record type");
1345 TokError(
"expected '(' after unary operator");
1349 const Init *
LHS = ParseValue(CurRec);
1358 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1360 "expected string, list, or dag type argument in unary operator");
1366 "expected string, list, or dag type argument in unary operator");
1376 if (!LHSl && !LHSt) {
1377 TokError(
"expected list type argument in unary operator");
1382 TokError(
"expected list type argument in unary operator");
1387 if (LHSl && LHSl->empty()) {
1388 TokError(
"empty list argument in unary operator");
1391 bool UseElementType =
1394 const Init *Item = LHSl->getElement(0);
1397 TokError(
"untyped list element in unary operator");
1400 Type = UseElementType ? Itemt->getType()
1403 assert(LHSt &&
"expected list type argument in unary operator");
1405 Type = UseElementType ? LType->getElementType() : LType;
1423 TokError(
"expected ')' in unary operator");
1433 const RecTy *
Type = ParseOperatorType();
1438 TokError(
"expected '(' after type of !isa");
1442 const Init *
LHS = ParseValue(CurRec);
1458 const RecTy *
Type = ParseOperatorType();
1463 TokError(
"expected '(' after type of !exists");
1467 SMLoc ExprLoc = Lex.getLoc();
1468 const Init *Expr = ParseValue(CurRec);
1474 Error(ExprLoc,
"expected string type argument in !exists operator");
1481 "expected string type argument in !exists operator, please "
1482 "use !isa instead");
1488 Error(ExprLoc,
"expected string type argument in !exists operator");
1493 TokError(
"expected ')' in !exists");
1504 const RecTy *
Type = ParseOperatorType();
1509 TokError(
"expected '(' after type of !instances");
1516 SMLoc RegexLoc = Lex.getLoc();
1517 Regex = ParseValue(CurRec);
1521 Error(RegexLoc,
"expected string type argument in !instances operator");
1527 Error(RegexLoc,
"expected string type argument in !instances operator");
1536 TokError(
"expected ')' in !instances");
1571 SMLoc OpLoc = Lex.getLoc();
1661 const RecTy *
Type =
nullptr;
1662 const RecTy *ArgType =
nullptr;
1676 Type = ParseOperatorType();
1678 TokError(
"did not get type for !getdagarg operator");
1733 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1735 "', got '" +
Type->getAsString() +
"'");
1740 TokError(
"expected '(' after binary operator");
1749 SMLoc InitLoc = Lex.getLoc();
1750 InitList.
push_back(ParseValue(CurRec, ArgType));
1751 if (!InitList.
back())
1755 if (!InitListBack) {
1756 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1757 InitList.
back()->getAsString() +
"'"));
1760 const RecTy *ListType = InitListBack->getType();
1769 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1775 if (ItemType && InitList.
size() == 1) {
1778 Twine(
"expected output type to be a list, got type '") +
1783 Error(OpLoc, Twine(
"expected first arg type to be '") +
1785 "', got value of type '" +
1794 Error(InitLoc, Twine(
"expected second parameter to be an int, got "
1795 "value of type '") +
1803 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1813 Error(InitLoc, Twine(
"expected bit, bits, int, string, or record; "
1814 "got value of type '") +
1827 Error(InitLoc, Twine(
"expected bit, bits, int, or string; "
1828 "got value of type '") +
1834 switch (InitList.
size()) {
1840 Twine(
"expected list of string, int, bits, or bit; "
1841 "got value of type '") +
1848 Error(InitLoc, Twine(
"expected second argument to be a string, "
1849 "got value of type '") +
1866 Error(InitLoc, Twine(
"expected value of type '") +
1909 TokError(
"expected ')' in operator");
1929 while (InitList.
size() > 2) {
1936 if (InitList.
size() == 2)
1940 Error(OpLoc,
"expected two operands to operator");
1947 return ParseOperationListComprehension(CurRec, ItemType);
1951 SMLoc OpLoc = Lex.getLoc();
1955 TokError(
"expected '(' after !range operator");
1960 bool FirstArgIsList =
false;
1962 if (
Args.size() >= 3) {
1963 TokError(
"expected at most three values of integer");
1967 SMLoc InitLoc = Lex.getLoc();
1968 Args.push_back(ParseValue(CurRec));
1974 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1975 Args.back()->getAsString() +
"'"));
1979 const RecTy *ArgBackType = ArgBack->getType();
1980 if (!FirstArgIsList ||
Args.size() == 1) {
1982 FirstArgIsList =
true;
1986 if (
Args.size() != 1)
1987 Error(InitLoc, Twine(
"expected value of type 'int', got '" +
1990 Error(InitLoc, Twine(
"expected list or int, got value of type '") +
1997 Error(InitLoc, Twine(
"expected one list, got extra value of type '") +
2006 TokError(
"expected ')' in operator");
2010 const Init *
LHS, *MHS, *
RHS;
2011 auto ArgCount =
Args.size();
2014 const auto *Arg0Ty = Arg0->getType();
2015 if (ArgCount == 1) {
2035 if (ArgCount == 3) {
2056 const RecTy *
Type =
nullptr;
2086 TokError(
"expected '(' after ternary operator");
2090 const Init *
LHS = ParseValue(CurRec);
2095 TokError(
"expected ',' in ternary operator");
2099 SMLoc MHSLoc = Lex.getLoc();
2100 const Init *MHS = ParseValue(CurRec, ItemType);
2105 TokError(
"expected ',' in ternary operator");
2109 SMLoc RHSLoc = Lex.getLoc();
2110 const Init *
RHS = ParseValue(CurRec, ItemType);
2115 TokError(
"expected ')' in binary operator");
2125 Error(MHSLoc,
"could not determine type of the child list in !dag");
2129 Error(MHSLoc, Twine(
"expected list of children, got type '") +
2130 MHSt->getType()->getAsString() +
"'");
2136 Error(RHSLoc,
"could not determine type of the name list in !dag");
2140 Error(RHSLoc, Twine(
"expected list<string>, got type '") +
2141 RHSt->getType()->getAsString() +
"'");
2145 if (!MHSt && !RHSt) {
2147 "cannot have both unset children and unset names in !dag");
2153 const RecTy *MHSTy =
nullptr;
2154 const RecTy *RHSTy =
nullptr;
2157 MHSTy = MHSt->getType();
2164 RHSTy = RHSt->getType();
2176 if (!MHSTy || !RHSTy) {
2177 TokError(
"could not get type for !if");
2192 TokError(
"could not get type for !subst");
2195 Type = RHSt->getType();
2201 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2202 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2212 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2213 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2221 Error(RHSLoc, Twine(
"expected string or unset name, got type '") +
2222 RHSt->getType()->getAsString() +
"'");
2232 return ParseOperationSubstr(CurRec, ItemType);
2235 return ParseOperationFind(CurRec, ItemType);
2238 return ParseOperationCond(CurRec, ItemType);
2241 return ParseOperationSwitch(CurRec, ItemType);
2247 TokError(
"expected '(' after !foldl");
2251 const Init *StartUntyped = ParseValue(CurRec);
2257 TokError(Twine(
"could not get type of !foldl start: '") +
2263 TokError(
"expected ',' in !foldl");
2267 const Init *ListUntyped = ParseValue(CurRec);
2273 TokError(Twine(
"could not get type of !foldl list: '") +
2280 TokError(Twine(
"!foldl list must be a list, but is of type '") +
2281 List->getType()->getAsString());
2286 TokError(
"expected ',' in !foldl");
2291 TokError(
"third argument of !foldl must be an identifier");
2297 TokError((Twine(
"left !foldl variable '") +
A->getAsString() +
2298 "' already defined")
2304 TokError(
"expected ',' in !foldl");
2309 TokError(
"fourth argument of !foldl must be an identifier");
2315 TokError((Twine(
"right !foldl variable '") +
B->getAsString() +
2316 "' already defined")
2322 TokError(
"expected ',' in !foldl");
2329 std::unique_ptr<Record> ParseRecTmp;
2330 Record *ParseRec = CurRec;
2334 ParseRec = ParseRecTmp.get();
2337 TGVarScope *FoldScope =
PushScope(ParseRec);
2341 const Init *ExprUntyped = ParseValue(ParseRec);
2342 ParseRec->removeValue(
A);
2343 ParseRec->removeValue(
B);
2350 TokError(
"could not get type of !foldl expression");
2354 if (Expr->getType() !=
Start->getType()) {
2355 TokError(Twine(
"!foldl expression must be of same type as start (") +
2356 Start->getType()->getAsString() +
"), but is of type " +
2362 TokError(
"expected ')' in fold operator");
2377const RecTy *TGParser::ParseOperatorType() {
2378 const RecTy *
Type =
nullptr;
2381 TokError(
"expected type name for operator");
2386 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2391 TokError(
"expected type name for operator");
2396 TokError(
"expected type name for operator");
2406const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2407 const RecTy *ItemType) {
2414 TokError(
"expected '(' after !substr operator");
2418 const Init *
LHS = ParseValue(CurRec);
2423 TokError(
"expected ',' in !substr operator");
2427 SMLoc MHSLoc = Lex.getLoc();
2428 const Init *MHS = ParseValue(CurRec);
2432 SMLoc RHSLoc = Lex.getLoc();
2435 RHSLoc = Lex.getLoc();
2436 RHS = ParseValue(CurRec);
2444 TokError(
"expected ')' in !substr operator");
2448 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2449 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2450 "', got '" +
Type->getAsString() +
"'");
2455 TokError(
"could not determine type of the string in !substr");
2459 TokError(Twine(
"expected string, got type '") +
2460 LHSt->getType()->getAsString() +
"'");
2466 TokError(
"could not determine type of the start position in !substr");
2470 Error(MHSLoc, Twine(
"expected int, got type '") +
2471 MHSt->getType()->getAsString() +
"'");
2478 TokError(
"could not determine type of the length in !substr");
2482 TokError(Twine(
"expected int, got type '") +
2483 RHSt->getType()->getAsString() +
"'");
2494const Init *TGParser::ParseOperationFind(
Record *CurRec,
2495 const RecTy *ItemType) {
2502 TokError(
"expected '(' after !find operator");
2506 const Init *
LHS = ParseValue(CurRec);
2511 TokError(
"expected ',' in !find operator");
2515 SMLoc MHSLoc = Lex.getLoc();
2516 const Init *MHS = ParseValue(CurRec);
2520 SMLoc RHSLoc = Lex.getLoc();
2523 RHSLoc = Lex.getLoc();
2524 RHS = ParseValue(CurRec);
2532 TokError(
"expected ')' in !find operator");
2536 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2537 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2538 "', got '" +
Type->getAsString() +
"'");
2543 TokError(
"could not determine type of the source string in !find");
2547 TokError(Twine(
"expected string, got type '") +
2548 LHSt->getType()->getAsString() +
"'");
2554 TokError(
"could not determine type of the target string in !find");
2558 Error(MHSLoc, Twine(
"expected string, got type '") +
2559 MHSt->getType()->getAsString() +
"'");
2566 TokError(
"could not determine type of the start position in !find");
2570 TokError(Twine(
"expected int, got type '") +
2571 RHSt->getType()->getAsString() +
"'");
2584const Init *TGParser::ParseOperationListComprehension(
Record *CurRec,
2585 const RecTy *ItemType) {
2586 SMLoc OpLoc = Lex.getLoc();
2590 TokError(
"expected '(' after !foreach/!filter");
2595 TokError(
"first argument of !foreach/!filter must be an identifier");
2603 TokError((Twine(
"iteration variable '") +
LHS->getAsString() +
2604 "' 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) {
2756 const RecTy *VTy =
nullptr;
2758 VTy = Vt->getType();
2766 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
"' and '" +
2768 return std::nullopt;
2778const Init *TGParser::ParseOperationCond(
Record *CurRec,
2779 const RecTy *ItemType) {
2783 TokError(
"expected '(' after !cond operator");
2794 const Init *
V = ParseValue(CurRec);
2800 TokError(
"expected ':' following a condition in !cond operator");
2804 V = ParseValue(CurRec, ItemType);
2813 TokError(
"expected ',' or ')' following a value in !cond operator");
2818 if (Cases.
size() < 1) {
2820 "there should be at least 1 'condition : value' in the !cond operator");
2825 std::optional<const RecTy *> TypeOpt = resolveInitTypes(Vals,
"for !cond");
2828 const RecTy *
Type = *TypeOpt;
2830 TokError(
"could not determine type for !cond from its arguments");
2837const Init *TGParser::ParseOperationSwitch(
Record *CurRec,
2838 const RecTy *ItemType) {
2842 TokError(
"expected '(' after !switch operator");
2847 const Init *
Key = ParseValue(CurRec);
2854 TokError(
"expected ',' after key in !switch operator");
2862 const Init *
V = ParseValue(CurRec);
2871 if (
const Init *Coerced =
V->convertInitializerTo(ItemType))
2881 TokError(
"expected ':' after case key, or ')' to close !switch operator");
2886 V = ParseValue(CurRec, ItemType);
2892 TokError(
"expected ',' after case value in !switch operator");
2897 "inconsistent keys and values for !switch");
2899 if (KeyAndCases.
size() < 2) {
2901 "there should be at least 1 'case: value' in the !switch operator");
2906 std::optional<const RecTy *> ValTypeOpt =
2907 resolveInitTypes(Vals,
"for !switch values");
2910 const RecTy *
ValType = *ValTypeOpt;
2912 TokError(
"could not determine type for !switch from its arguments");
2919 if (!resolveInitTypes(KeyAndCases,
"between !switch key and case keys"))
2925 size_t ValsSize = Vals.
size();
2932 assert(Conds.
size() == ValsSize &&
"inconsistent !switch to !cond reduction");
2963const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2965 const Init *
R =
nullptr;
2970 return ParseOperation(CurRec, ItemType);
2974 TokError(
"Unknown or reserved token when parsing a value");
2990 auto BinaryVal = Lex.getCurBinaryIntVal();
2992 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2999 std::string Val = Lex.getCurStrVal();
3004 Val += Lex.getCurStrVal();
3020 SMRange NameLoc = Lex.getLocRange();
3026 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
3034 "Expected a class name, got '" +
Name->getValue() +
"'");
3041 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
3044 if (CheckTemplateArgValues(Args, ArgLocs, Class))
3047 if (resolveArguments(Class, Args, NameLoc.
Start))
3050 if (TrackReferenceLocs)
3051 Class->appendReferenceLoc(NameLoc);
3055 SMLoc BraceLoc = Lex.getLoc();
3060 ParseValueList(Vals, CurRec);
3065 TokError(
"expected '}' at end of bit list value");
3074 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
3080 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
3087 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
3096 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
3097 ") is not convertable to a bit");
3102 std::reverse(NewBits.
begin(), NewBits.
end());
3109 const RecTy *DeducedEltTy =
nullptr;
3110 const ListRecTy *GivenListTy =
nullptr;
3115 TokError(Twine(
"Encountered a list when expecting a ") +
3119 GivenListTy = ListType;
3123 ParseValueList(Vals, CurRec,
3129 TokError(
"expected ']' at end of list value");
3133 const RecTy *GivenEltTy =
nullptr;
3136 GivenEltTy = ParseType();
3143 TokError(
"expected '>' at end of list element type");
3149 const RecTy *EltTy =
nullptr;
3150 for (
const Init *V : Vals) {
3156 TokError(
"Incompatible types in list elements");
3160 EltTy = TArg->getType();
3169 TokError(
"Incompatible types in list elements");
3186 TokError(Twine(
"Element type mismatch for list: element type '") +
3192 DeducedEltTy = EltTy;
3203 TokError(
"expected identifier or list of value types in dag init");
3207 const Init *
Operator = ParseValue(CurRec);
3212 const StringInit *OperatorName =
nullptr;
3215 TokError(
"expected variable name in dag operator");
3224 ParseDagArgList(DagArgs, CurRec);
3225 if (DagArgs.
empty())
3230 TokError(
"expected ')' in dag init");
3248const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3250 SMLoc LHSLoc = Lex.getLoc();
3251 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3257 switch (Lex.getCode()) {
3261 if (
Mode == ParseNameMode)
3265 SMLoc CurlyLoc = Lex.getLoc();
3267 SmallVector<unsigned, 16>
Ranges;
3268 ParseRangeList(Ranges);
3276 Error(CurlyLoc,
"Invalid bit range for value");
3282 TokError(
"expected '}' at end of bit range list");
3290 Error(LHSLoc,
"Invalid value, list expected");
3297 "' is invalid, list expected");
3302 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3311 LHSTy->getElementType())
3319 TokError(
"expected ']' at end of list slice");
3326 TokError(
"expected field identifier after '.'");
3329 SMRange FieldNameLoc = Lex.getLocRange();
3330 const StringInit *FieldName =
3332 if (!
Result->getFieldType(FieldName)) {
3333 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3334 Result->getAsString() +
"'");
3339 if (TrackReferenceLocs) {
3341 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3342 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3345 for (
const Record *R : RecTy->getClasses())
3346 if (
const auto *RV =
R->getValue(FieldName))
3347 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3358 SMLoc PasteLoc = Lex.getLoc();
3361 Error(PasteLoc,
"LHS of paste is not typed!");
3369 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3371 switch (Lex.getCode()) {
3378 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3395 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3401 const TypedInit *
RHS =
nullptr;
3404 switch (Lex.getCode()) {
3417 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3422 Error(PasteLoc,
"RHS of paste is not typed!");
3432 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3453void TGParser::ParseDagArgList(
3466 const Init *Val = ParseValue(CurRec);
3473 const StringInit *
VarName =
nullptr;
3476 TokError(
"expected variable name in dag literal");
3484 Result.emplace_back(Val, VarName);
3499 Result.push_back(ParseValue(CurRec, ItemType));
3509 Result.push_back(ParseValue(CurRec, ItemType));
3525bool TGParser::ParseTemplateArgValueList(
3528 assert(
Result.empty() &&
"Result vector is not empty");
3534 bool HasNamedArg =
false;
3535 unsigned ArgIndex = 0;
3537 if (ArgIndex >= TArgs.
size()) {
3538 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3545 const Init *
Value = ParseValue(
3547 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3554 return Error(ValueLoc,
3555 "The name of named argument should be a valid identifier");
3559 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3561 return Error(ValueLoc,
3562 "Argument " +
Name->getAsString() +
" doesn't exist");
3565 ValueLoc = Lex.getLoc();
3566 Value = ParseValue(CurRec, NamedArg->getType());
3569 return Error(ValueLoc,
3570 "The value of named argument should be initialized, "
3572 Value->getAsString() +
"'");
3579 return Error(ValueLoc,
3580 "Positional argument should be put before named argument");
3588 return TokError(
"Expected comma before next argument");
3603const Init *TGParser::ParseDeclaration(
Record *CurRec,
3604 bool ParsingTemplateArgs) {
3608 const RecTy *
Type = ParseType();
3613 TokError(
"Expected identifier in declaration");
3617 std::string Str = Lex.getCurStrVal();
3618 if (Str ==
"NAME") {
3619 TokError(
"'" + Str +
"' is a reserved variable name");
3623 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3624 TokError(
"local variable of this name already exists");
3628 SMLoc IdLoc = Lex.getLoc();
3633 if (!ParsingTemplateArgs) {
3634 BadField = AddValue(CurRec, IdLoc,
3635 RecordVal(DeclName, IdLoc,
Type,
3638 }
else if (CurRec) {
3641 AddValue(CurRec, IdLoc,
3644 assert(CurMultiClass &&
"invalid context for template argument");
3647 AddValue(CurRec, IdLoc,
3655 SMLoc ValLoc = Lex.getLoc();
3656 const Init *Val = ParseValue(CurRec,
Type);
3658 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3679TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3681 TokError(
"Expected identifier in foreach declaration");
3690 TokError(
"Expected '=' in foreach declaration");
3694 const RecTy *IterType =
nullptr;
3695 SmallVector<unsigned, 16>
Ranges;
3697 switch (Lex.getCode()) {
3700 ParseRangeList(Ranges);
3702 TokError(
"expected '}' at end of bit range list");
3709 SMLoc ValueLoc = Lex.getLoc();
3710 const Init *
I = ParseValue(
nullptr);
3716 ForeachListValue =
I;
3722 if (ParseRangePiece(Ranges, TI))
3727 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3728 if (CurMultiClass) {
3729 PrintNote({},
"references to multiclass template arguments cannot be "
3730 "resolved at this time");
3737 assert(!IterType &&
"Type already initialized?");
3739 std::vector<Init *> Values;
3740 for (
unsigned R : Ranges)
3758bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3762 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3765 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3773 SMLoc Loc = Lex.getLoc();
3774 TemplArg = ParseDeclaration(CurRec,
true );
3779 return Error(Loc,
"template argument with the same name has already been "
3786 return TokError(
"expected '>' at end of template argument list");
3803 SMLoc Loc = Lex.getLoc();
3805 std::string CurStr = Lex.getCurStrVal();
3807 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3817 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3835bool TGParser::ParseBodyItem(
Record *CurRec) {
3837 return ParseAssert(
nullptr, CurRec);
3840 return ParseDefvar(CurRec);
3843 return ParseDump(
nullptr, CurRec);
3846 if (!ParseDeclaration(CurRec,
false))
3850 return TokError(
"expected ';' after declaration");
3858 return TokError(
"expected field identifier after let");
3860 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3863 SmallVector<unsigned, 16> BitList;
3864 if (ParseOptionalRangeList(BitList))
3866 std::reverse(BitList.
begin(), BitList.
end());
3869 return TokError(
"expected '=' in let expression");
3873 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3882 const Init *Val = ParseValue(CurRec,
Type);
3887 return TokError(
"expected ';' after let expression");
3889 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3900bool TGParser::ParseBody(
Record *CurRec) {
3906 return TokError(
"Expected '{' to start body or ';' for declaration only");
3909 if (ParseBodyItem(CurRec))
3916 SMLoc SemiLoc = Lex.getLoc();
3918 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3919 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3927bool TGParser::ApplyLetStack(
Record *CurRec) {
3928 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3929 for (LetRecord &LR : LetInfo)
3930 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3940 return ApplyLetStack(
Entry.Rec.get());
3943 if (
Entry.Assertion)
3950 for (
auto &
E :
Entry.Loop->Entries) {
3951 if (ApplyLetStack(
E))
3967bool TGParser::ParseObjectBody(
Record *CurRec) {
3969 TGVarScope *ObjectScope =
PushScope(CurRec);
3974 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3981 if (AddSubClass(CurRec, SubClass))
3986 SubClass = ParseSubClassReference(CurRec,
false);
3990 if (ApplyLetStack(CurRec))
3993 bool Result = ParseBody(CurRec);
4003bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
4004 SMLoc DefLoc = Lex.getLoc();
4011 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
4014 std::unique_ptr<Record> CurRec;
4015 const Init *
Name = ParseObjectName(CurMultiClass);
4020 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
4023 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
4026 if (ParseObjectBody(CurRec.get()))
4029 return addEntry(std::move(CurRec));
4036bool TGParser::ParseDefset() {
4041 Defset.Loc = Lex.getLoc();
4042 const RecTy *
Type = ParseType();
4050 return TokError(
"expected identifier");
4051 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4052 if (Records.getGlobal(DeclName->
getValue()))
4053 return TokError(
"def or global variable of this name already exists");
4059 SMLoc BraceLoc = Lex.getLoc();
4062 Defsets.push_back(&Defset);
4063 bool Err = ParseObjectList(
nullptr);
4069 TokError(
"expected '}' at end of defset");
4070 return Error(BraceLoc,
"to match this '{'");
4073 Records.addExtraGlobal(DeclName->
getValue(),
4082bool TGParser::ParseDeftype() {
4087 return TokError(
"expected identifier");
4089 const std::string
TypeName = Lex.getCurStrVal();
4090 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
4091 return TokError(
"type of this name '" + TypeName +
"' already exists");
4097 SMLoc Loc = Lex.getLoc();
4098 const RecTy *
Type = ParseType();
4103 return Error(Loc,
"cannot define type alias for class type '" +
4104 Type->getAsString() +
"'");
4118bool TGParser::ParseDefvar(
Record *CurRec) {
4123 return TokError(
"expected identifier");
4124 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4125 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
4126 return TokError(
"local variable of this name already exists");
4131 if (V && !
V->isTemplateArg())
4132 return TokError(
"field of this name already exists");
4137 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
4138 return TokError(
"def or global variable of this name already exists");
4144 const Init *
Value = ParseValue(CurRec);
4151 if (!CurScope->isOutermost())
4165bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4166 SMLoc Loc = Lex.getLoc();
4172 const Init *ListValue =
nullptr;
4173 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4175 return TokError(
"expected declaration in for");
4181 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4183 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4184 Loops.push_back(std::move(TheLoop));
4188 if (ParseObject(CurMultiClass))
4191 SMLoc BraceLoc = Lex.getLoc();
4196 if (ParseObjectList(CurMultiClass))
4200 TokError(
"expected '}' at end of foreach command");
4201 return Error(BraceLoc,
"to match this '{'");
4208 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4211 return addEntry(std::move(Loop));
4219bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4220 SMLoc Loc = Lex.getLoc();
4226 const Init *Condition = ParseValue(
nullptr);
4240 const ListInit *SingletonList =
4246 const Init *ThenClauseList =
4250 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4252 if (ParseIfBody(CurMultiClass,
"then"))
4255 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4258 if (addEntry(std::move(Loop)))
4268 const Init *ElseClauseList =
4273 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4275 if (ParseIfBody(CurMultiClass,
"else"))
4278 Loop = std::move(Loops.back());
4281 if (addEntry(std::move(Loop)))
4299 if (ParseObject(CurMultiClass))
4302 SMLoc BraceLoc = Lex.getLoc();
4307 if (ParseObjectList(CurMultiClass))
4311 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4312 return Error(BraceLoc,
"to match this '{'");
4327 SMLoc ConditionLoc = Lex.getLoc();
4328 const Init *Condition = ParseValue(CurRec);
4333 TokError(
"expected ',' in assert statement");
4337 const Init *Message = ParseValue(CurRec);
4345 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4347 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4356bool TGParser::ParseClass() {
4361 return TokError(
"expected class name after 'class' keyword");
4363 const std::string &
Name = Lex.getCurStrVal();
4364 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4371 "' already defined");
4376 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4378 CurRec = NewRec.get();
4379 Records.addClass(std::move(NewRec));
4382 if (TypeAliases.count(Name))
4383 return TokError(
"there is already a defined type alias '" + Name +
"'");
4388 TGVarScope *ClassScope =
PushScope(CurRec);
4391 if (ParseTemplateArgList(CurRec))
4394 if (ParseObjectBody(CurRec))
4397 if (!NoWarnOnUnusedTemplateArgs)
4413 TokError(
"expected identifier in let definition");
4418 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4422 SmallVector<unsigned, 16>
Bits;
4423 if (ParseOptionalRangeList(Bits)) {
4427 std::reverse(
Bits.begin(),
Bits.end());
4430 TokError(
"expected '=' in let expression");
4435 const Init *Val = ParseValue(
nullptr);
4442 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4452bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4458 ParseLetList(LetInfo);
4459 if (LetInfo.
empty())
4461 LetStack.push_back(std::move(LetInfo));
4464 return TokError(
"expected 'in' at end of top-level 'let'");
4469 if (ParseObject(CurMultiClass))
4472 SMLoc BraceLoc = Lex.getLoc();
4480 if (ParseObjectList(CurMultiClass))
4484 TokError(
"expected '}' at end of top level let command");
4485 return Error(BraceLoc,
"to match this '{'");
4492 LetStack.pop_back();
4509bool TGParser::ParseMultiClass() {
4514 return TokError(
"expected identifier after multiclass for name");
4515 std::string
Name = Lex.getCurStrVal();
4517 auto Result = MultiClasses.try_emplace(
4518 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4521 return TokError(
"multiclass '" + Name +
"' already defined");
4523 CurMultiClass =
Result.first->second.get();
4527 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4531 if (ParseTemplateArgList(
nullptr))
4534 bool inherits =
false;
4541 SubMultiClassReference SubMultiClass =
4542 ParseSubMultiClassReference(CurMultiClass);
4545 if (!SubMultiClass.
MC)
4549 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4554 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4560 return TokError(
"expected '{' in multiclass definition");
4562 return TokError(
"expected ';' in multiclass definition");
4565 return TokError(
"multiclass must contain at least one def");
4568 switch (Lex.getCode()) {
4570 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4571 "'foreach', 'if', or 'let' in multiclass body");
4581 if (ParseObject(CurMultiClass))
4589 SMLoc SemiLoc = Lex.getLoc();
4591 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4592 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4596 if (!NoWarnOnUnusedTemplateArgs)
4597 CurMultiClass->Rec.checkUnusedTemplateArgs();
4600 CurMultiClass =
nullptr;
4608bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4612 const Init *DefmName = ParseObjectName(CurMultiClass);
4616 DefmName = Records.getNewAnonymousName();
4625 return TokError(
"expected ':' after defm identifier");
4628 std::vector<RecordsEntry> NewEntries;
4631 bool InheritFromClass =
false;
4636 SMLoc SubClassLoc = Lex.getLoc();
4637 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4647 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4648 assert(MC &&
"Didn't lookup multiclass correctly?");
4651 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4655 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4656 &NewEntries, &SubClassLoc))
4663 return TokError(
"expected identifier");
4665 SubClassLoc = Lex.getLoc();
4669 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4671 if (InheritFromClass)
4674 Ref = ParseSubClassReference(
nullptr,
true);
4677 if (InheritFromClass) {
4680 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4688 for (
auto &
E : NewEntries) {
4690 if (AddSubClass(
E, SubClass))
4696 SubClass = ParseSubClassReference(
nullptr,
false);
4700 for (
auto &
E : NewEntries) {
4701 if (ApplyLetStack(
E))
4704 addEntry(std::move(
E));
4708 return TokError(
"expected ';' at end of defm");
4726 switch (Lex.getCode()) {
4729 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4731 return ParseAssert(MC);
4733 return ParseDef(MC);
4735 return ParseDefm(MC);
4737 return ParseDeftype();
4739 return ParseDefvar();
4741 return ParseDump(MC);
4743 return ParseForeach(MC);
4747 return ParseTopLevelLet(MC);
4750 return TokError(
"defset is not allowed inside multiclass");
4751 return ParseDefset();
4754 return TokError(
"class is not allowed inside multiclass");
4756 return TokError(
"class is not allowed inside foreach loop");
4757 return ParseClass();
4760 return TokError(
"multiclass is not allowed inside foreach loop");
4761 return ParseMultiClass();
4767bool TGParser::ParseObjectList(
MultiClass *MC) {
4769 if (ParseObject(MC))
4778 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.
void addReferenceLoc(SMRange Loc)
Add a reference to this record value.
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()
std::string getNameInitAsString() const
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.
FunctionAddr VTableAddr Value
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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
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.
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:
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.
FunctionAddr VTableAddr Next
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.
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.
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
@ Default
The result value is uniform if and only if all operands are uniform.
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