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();
2770 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
"' and '" +
2772 return std::nullopt;
2782const Init *TGParser::ParseOperationCond(
Record *CurRec,
2783 const RecTy *ItemType) {
2787 TokError(
"expected '(' after !cond operator");
2798 const Init *
V = ParseValue(CurRec);
2804 TokError(
"expected ':' following a condition in !cond operator");
2808 V = ParseValue(CurRec, ItemType);
2817 TokError(
"expected ',' or ')' following a value in !cond operator");
2822 if (Case.
size() < 1) {
2824 "there should be at least 1 'condition : value' in the !cond operator");
2829 std::optional<const RecTy *> TypeOpt = resolveInitTypes(Val,
"for !cond");
2832 const RecTy *
Type = *TypeOpt;
2834 TokError(
"could not determine type for !cond from its arguments");
2841const Init *TGParser::ParseOperationSwitch(
Record *CurRec,
2842 const RecTy *ItemType) {
2846 TokError(
"expected '(' after !switch operator");
2851 const Init *
Key = ParseValue(CurRec);
2858 TokError(
"expected ',' after key in !switch operator");
2866 const Init *
V = ParseValue(CurRec);
2874 if (
const Init *Coerced =
V->convertInitializerTo(ItemType))
2883 TokError(
"expected ':' after case key, or ')' to close !switch operator");
2888 V = ParseValue(CurRec, ItemType);
2894 TokError(
"expected ',' after case value in !switch operator");
2899 "inconsistent keys and values for !switch");
2901 if (KeyAndCases.
size() < 2) {
2903 "there should be at least 1 'case: value' in the !switch operator");
2908 std::optional<const RecTy *> ValTypeOpt =
2909 resolveInitTypes(Vals,
"for !switch values");
2912 const RecTy *
ValType = *ValTypeOpt;
2914 TokError(
"could not determine type for !switch from its arguments");
2921 if (!resolveInitTypes(KeyAndCases,
"between !switch key and case keys"))
2927 size_t ValsSize = Vals.
size();
2934 assert(Conds.
size() == ValsSize &&
"inconsistent !switch to !cond reduction");
2965const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2967 const Init *
R =
nullptr;
2972 return ParseOperation(CurRec, ItemType);
2976 TokError(
"Unknown or reserved token when parsing a value");
2992 auto BinaryVal = Lex.getCurBinaryIntVal();
2994 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
3001 std::string Val = Lex.getCurStrVal();
3006 Val += Lex.getCurStrVal();
3022 SMRange NameLoc = Lex.getLocRange();
3028 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
3036 "Expected a class name, got '" +
Name->getValue() +
"'");
3043 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
3046 if (CheckTemplateArgValues(Args, ArgLocs, Class))
3049 if (resolveArguments(Class, Args, NameLoc.
Start))
3052 if (TrackReferenceLocs)
3053 Class->appendReferenceLoc(NameLoc);
3057 SMLoc BraceLoc = Lex.getLoc();
3062 ParseValueList(Vals, CurRec);
3067 TokError(
"expected '}' at end of bit list value");
3076 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
3082 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
3089 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
3098 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
3099 ") is not convertable to a bit");
3104 std::reverse(NewBits.
begin(), NewBits.
end());
3111 const RecTy *DeducedEltTy =
nullptr;
3112 const ListRecTy *GivenListTy =
nullptr;
3117 TokError(Twine(
"Encountered a list when expecting a ") +
3121 GivenListTy = ListType;
3125 ParseValueList(Vals, CurRec,
3131 TokError(
"expected ']' at end of list value");
3135 const RecTy *GivenEltTy =
nullptr;
3138 GivenEltTy = ParseType();
3145 TokError(
"expected '>' at end of list element type");
3151 const RecTy *EltTy =
nullptr;
3152 for (
const Init *V : Vals) {
3158 TokError(
"Incompatible types in list elements");
3162 EltTy = TArg->getType();
3171 TokError(
"Incompatible types in list elements");
3188 TokError(Twine(
"Element type mismatch for list: element type '") +
3194 DeducedEltTy = EltTy;
3205 TokError(
"expected identifier or list of value types in dag init");
3209 const Init *
Operator = ParseValue(CurRec);
3214 const StringInit *OperatorName =
nullptr;
3217 TokError(
"expected variable name in dag operator");
3226 ParseDagArgList(DagArgs, CurRec);
3227 if (DagArgs.
empty())
3232 TokError(
"expected ')' in dag init");
3250const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3252 SMLoc LHSLoc = Lex.getLoc();
3253 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3259 switch (Lex.getCode()) {
3263 if (
Mode == ParseNameMode)
3267 SMLoc CurlyLoc = Lex.getLoc();
3269 SmallVector<unsigned, 16>
Ranges;
3270 ParseRangeList(Ranges);
3278 Error(CurlyLoc,
"Invalid bit range for value");
3284 TokError(
"expected '}' at end of bit range list");
3292 Error(LHSLoc,
"Invalid value, list expected");
3299 "' is invalid, list expected");
3304 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3313 LHSTy->getElementType())
3321 TokError(
"expected ']' at end of list slice");
3328 TokError(
"expected field identifier after '.'");
3331 SMRange FieldNameLoc = Lex.getLocRange();
3332 const StringInit *FieldName =
3334 if (!
Result->getFieldType(FieldName)) {
3335 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3336 Result->getAsString() +
"'");
3341 if (TrackReferenceLocs) {
3343 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3344 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3347 for (
const Record *R : RecTy->getClasses())
3348 if (
const auto *RV =
R->getValue(FieldName))
3349 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3360 SMLoc PasteLoc = Lex.getLoc();
3363 Error(PasteLoc,
"LHS of paste is not typed!");
3371 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3373 switch (Lex.getCode()) {
3380 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3397 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3403 const TypedInit *
RHS =
nullptr;
3406 switch (Lex.getCode()) {
3419 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3424 Error(PasteLoc,
"RHS of paste is not typed!");
3434 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3455void TGParser::ParseDagArgList(
3468 const Init *Val = ParseValue(CurRec);
3475 const StringInit *
VarName =
nullptr;
3478 TokError(
"expected variable name in dag literal");
3486 Result.emplace_back(Val, VarName);
3501 Result.push_back(ParseValue(CurRec, ItemType));
3511 Result.push_back(ParseValue(CurRec, ItemType));
3527bool TGParser::ParseTemplateArgValueList(
3530 assert(
Result.empty() &&
"Result vector is not empty");
3536 bool HasNamedArg =
false;
3537 unsigned ArgIndex = 0;
3539 if (ArgIndex >= TArgs.
size()) {
3540 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3547 const Init *
Value = ParseValue(
3549 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3556 return Error(ValueLoc,
3557 "The name of named argument should be a valid identifier");
3561 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3563 return Error(ValueLoc,
3564 "Argument " +
Name->getAsString() +
" doesn't exist");
3567 ValueLoc = Lex.getLoc();
3568 Value = ParseValue(CurRec, NamedArg->getType());
3571 return Error(ValueLoc,
3572 "The value of named argument should be initialized, "
3574 Value->getAsString() +
"'");
3581 return Error(ValueLoc,
3582 "Positional argument should be put before named argument");
3590 return TokError(
"Expected comma before next argument");
3605const Init *TGParser::ParseDeclaration(
Record *CurRec,
3606 bool ParsingTemplateArgs) {
3610 const RecTy *
Type = ParseType();
3615 TokError(
"Expected identifier in declaration");
3619 std::string Str = Lex.getCurStrVal();
3620 if (Str ==
"NAME") {
3621 TokError(
"'" + Str +
"' is a reserved variable name");
3625 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3626 TokError(
"local variable of this name already exists");
3630 SMLoc IdLoc = Lex.getLoc();
3635 if (!ParsingTemplateArgs) {
3636 BadField = AddValue(CurRec, IdLoc,
3637 RecordVal(DeclName, IdLoc,
Type,
3640 }
else if (CurRec) {
3643 AddValue(CurRec, IdLoc,
3646 assert(CurMultiClass &&
"invalid context for template argument");
3649 AddValue(CurRec, IdLoc,
3657 SMLoc ValLoc = Lex.getLoc();
3658 const Init *Val = ParseValue(CurRec,
Type);
3660 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3681TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3683 TokError(
"Expected identifier in foreach declaration");
3692 TokError(
"Expected '=' in foreach declaration");
3696 const RecTy *IterType =
nullptr;
3697 SmallVector<unsigned, 16>
Ranges;
3699 switch (Lex.getCode()) {
3702 ParseRangeList(Ranges);
3704 TokError(
"expected '}' at end of bit range list");
3711 SMLoc ValueLoc = Lex.getLoc();
3712 const Init *
I = ParseValue(
nullptr);
3718 ForeachListValue =
I;
3724 if (ParseRangePiece(Ranges, TI))
3729 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3730 if (CurMultiClass) {
3731 PrintNote({},
"references to multiclass template arguments cannot be "
3732 "resolved at this time");
3739 assert(!IterType &&
"Type already initialized?");
3741 std::vector<Init *> Values;
3742 for (
unsigned R : Ranges)
3760bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3764 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3767 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3775 SMLoc Loc = Lex.getLoc();
3776 TemplArg = ParseDeclaration(CurRec,
true );
3781 return Error(Loc,
"template argument with the same name has already been "
3788 return TokError(
"expected '>' at end of template argument list");
3805 SMLoc Loc = Lex.getLoc();
3807 std::string CurStr = Lex.getCurStrVal();
3809 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3819 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3837bool TGParser::ParseBodyItem(
Record *CurRec) {
3839 return ParseAssert(
nullptr, CurRec);
3842 return ParseDefvar(CurRec);
3845 return ParseDump(
nullptr, CurRec);
3848 if (!ParseDeclaration(CurRec,
false))
3852 return TokError(
"expected ';' after declaration");
3860 return TokError(
"expected field identifier after let");
3862 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3865 SmallVector<unsigned, 16> BitList;
3866 if (ParseOptionalRangeList(BitList))
3868 std::reverse(BitList.
begin(), BitList.
end());
3871 return TokError(
"expected '=' in let expression");
3875 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3884 const Init *Val = ParseValue(CurRec,
Type);
3889 return TokError(
"expected ';' after let expression");
3891 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3902bool TGParser::ParseBody(
Record *CurRec) {
3908 return TokError(
"Expected '{' to start body or ';' for declaration only");
3911 if (ParseBodyItem(CurRec))
3918 SMLoc SemiLoc = Lex.getLoc();
3920 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3921 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3929bool TGParser::ApplyLetStack(
Record *CurRec) {
3930 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3931 for (LetRecord &LR : LetInfo)
3932 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3942 return ApplyLetStack(
Entry.Rec.get());
3945 if (
Entry.Assertion)
3952 for (
auto &
E :
Entry.Loop->Entries) {
3953 if (ApplyLetStack(
E))
3969bool TGParser::ParseObjectBody(
Record *CurRec) {
3971 TGVarScope *ObjectScope =
PushScope(CurRec);
3976 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3983 if (AddSubClass(CurRec, SubClass))
3988 SubClass = ParseSubClassReference(CurRec,
false);
3992 if (ApplyLetStack(CurRec))
3995 bool Result = ParseBody(CurRec);
4005bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
4006 SMLoc DefLoc = Lex.getLoc();
4013 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
4016 std::unique_ptr<Record> CurRec;
4017 const Init *
Name = ParseObjectName(CurMultiClass);
4022 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
4025 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
4028 if (ParseObjectBody(CurRec.get()))
4031 return addEntry(std::move(CurRec));
4038bool TGParser::ParseDefset() {
4043 Defset.Loc = Lex.getLoc();
4044 const RecTy *
Type = ParseType();
4052 return TokError(
"expected identifier");
4053 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4054 if (Records.getGlobal(DeclName->
getValue()))
4055 return TokError(
"def or global variable of this name already exists");
4061 SMLoc BraceLoc = Lex.getLoc();
4064 Defsets.push_back(&Defset);
4065 bool Err = ParseObjectList(
nullptr);
4071 TokError(
"expected '}' at end of defset");
4072 return Error(BraceLoc,
"to match this '{'");
4075 Records.addExtraGlobal(DeclName->
getValue(),
4084bool TGParser::ParseDeftype() {
4089 return TokError(
"expected identifier");
4091 const std::string
TypeName = Lex.getCurStrVal();
4092 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
4093 return TokError(
"type of this name '" + TypeName +
"' already exists");
4099 SMLoc Loc = Lex.getLoc();
4100 const RecTy *
Type = ParseType();
4105 return Error(Loc,
"cannot define type alias for class type '" +
4106 Type->getAsString() +
"'");
4120bool TGParser::ParseDefvar(
Record *CurRec) {
4125 return TokError(
"expected identifier");
4126 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4127 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
4128 return TokError(
"local variable of this name already exists");
4133 if (V && !
V->isTemplateArg())
4134 return TokError(
"field of this name already exists");
4139 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
4140 return TokError(
"def or global variable of this name already exists");
4146 const Init *
Value = ParseValue(CurRec);
4153 if (!CurScope->isOutermost())
4167bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4168 SMLoc Loc = Lex.getLoc();
4174 const Init *ListValue =
nullptr;
4175 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4177 return TokError(
"expected declaration in for");
4183 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4185 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4186 Loops.push_back(std::move(TheLoop));
4190 if (ParseObject(CurMultiClass))
4193 SMLoc BraceLoc = Lex.getLoc();
4198 if (ParseObjectList(CurMultiClass))
4202 TokError(
"expected '}' at end of foreach command");
4203 return Error(BraceLoc,
"to match this '{'");
4210 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4213 return addEntry(std::move(Loop));
4221bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4222 SMLoc Loc = Lex.getLoc();
4228 const Init *Condition = ParseValue(
nullptr);
4242 const ListInit *SingletonList =
4248 const Init *ThenClauseList =
4252 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4254 if (ParseIfBody(CurMultiClass,
"then"))
4257 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4260 if (addEntry(std::move(Loop)))
4270 const Init *ElseClauseList =
4275 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4277 if (ParseIfBody(CurMultiClass,
"else"))
4280 Loop = std::move(Loops.back());
4283 if (addEntry(std::move(Loop)))
4301 if (ParseObject(CurMultiClass))
4304 SMLoc BraceLoc = Lex.getLoc();
4309 if (ParseObjectList(CurMultiClass))
4313 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4314 return Error(BraceLoc,
"to match this '{'");
4329 SMLoc ConditionLoc = Lex.getLoc();
4330 const Init *Condition = ParseValue(CurRec);
4335 TokError(
"expected ',' in assert statement");
4339 const Init *Message = ParseValue(CurRec);
4347 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4349 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4358bool TGParser::ParseClass() {
4363 return TokError(
"expected class name after 'class' keyword");
4365 const std::string &
Name = Lex.getCurStrVal();
4366 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4373 "' already defined");
4378 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4380 CurRec = NewRec.get();
4381 Records.addClass(std::move(NewRec));
4384 if (TypeAliases.count(Name))
4385 return TokError(
"there is already a defined type alias '" + Name +
"'");
4390 TGVarScope *ClassScope =
PushScope(CurRec);
4393 if (ParseTemplateArgList(CurRec))
4396 if (ParseObjectBody(CurRec))
4399 if (!NoWarnOnUnusedTemplateArgs)
4415 TokError(
"expected identifier in let definition");
4420 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4424 SmallVector<unsigned, 16>
Bits;
4425 if (ParseOptionalRangeList(Bits)) {
4429 std::reverse(
Bits.begin(),
Bits.end());
4432 TokError(
"expected '=' in let expression");
4437 const Init *Val = ParseValue(
nullptr);
4444 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4454bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4460 ParseLetList(LetInfo);
4461 if (LetInfo.
empty())
4463 LetStack.push_back(std::move(LetInfo));
4466 return TokError(
"expected 'in' at end of top-level 'let'");
4471 if (ParseObject(CurMultiClass))
4474 SMLoc BraceLoc = Lex.getLoc();
4482 if (ParseObjectList(CurMultiClass))
4486 TokError(
"expected '}' at end of top level let command");
4487 return Error(BraceLoc,
"to match this '{'");
4494 LetStack.pop_back();
4511bool TGParser::ParseMultiClass() {
4516 return TokError(
"expected identifier after multiclass for name");
4517 std::string
Name = Lex.getCurStrVal();
4519 auto Result = MultiClasses.try_emplace(
4520 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4523 return TokError(
"multiclass '" + Name +
"' already defined");
4525 CurMultiClass =
Result.first->second.get();
4529 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4533 if (ParseTemplateArgList(
nullptr))
4536 bool inherits =
false;
4543 SubMultiClassReference SubMultiClass =
4544 ParseSubMultiClassReference(CurMultiClass);
4547 if (!SubMultiClass.
MC)
4551 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4556 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4562 return TokError(
"expected '{' in multiclass definition");
4564 return TokError(
"expected ';' in multiclass definition");
4567 return TokError(
"multiclass must contain at least one def");
4570 switch (Lex.getCode()) {
4572 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4573 "'foreach', 'if', or 'let' in multiclass body");
4583 if (ParseObject(CurMultiClass))
4591 SMLoc SemiLoc = Lex.getLoc();
4593 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4594 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4598 if (!NoWarnOnUnusedTemplateArgs)
4599 CurMultiClass->Rec.checkUnusedTemplateArgs();
4602 CurMultiClass =
nullptr;
4610bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4614 const Init *DefmName = ParseObjectName(CurMultiClass);
4618 DefmName = Records.getNewAnonymousName();
4627 return TokError(
"expected ':' after defm identifier");
4630 std::vector<RecordsEntry> NewEntries;
4633 bool InheritFromClass =
false;
4638 SMLoc SubClassLoc = Lex.getLoc();
4639 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4649 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4650 assert(MC &&
"Didn't lookup multiclass correctly?");
4653 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4657 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4658 &NewEntries, &SubClassLoc))
4665 return TokError(
"expected identifier");
4667 SubClassLoc = Lex.getLoc();
4671 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4673 if (InheritFromClass)
4676 Ref = ParseSubClassReference(
nullptr,
true);
4679 if (InheritFromClass) {
4682 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4690 for (
auto &
E : NewEntries) {
4692 if (AddSubClass(
E, SubClass))
4698 SubClass = ParseSubClassReference(
nullptr,
false);
4702 for (
auto &
E : NewEntries) {
4703 if (ApplyLetStack(
E))
4706 addEntry(std::move(
E));
4710 return TokError(
"expected ';' at end of defm");
4728 switch (Lex.getCode()) {
4731 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4733 return ParseAssert(MC);
4735 return ParseDef(MC);
4737 return ParseDefm(MC);
4739 return ParseDeftype();
4741 return ParseDefvar();
4743 return ParseDump(MC);
4745 return ParseForeach(MC);
4749 return ParseTopLevelLet(MC);
4752 return TokError(
"defset is not allowed inside multiclass");
4753 return ParseDefset();
4756 return TokError(
"class is not allowed inside multiclass");
4758 return TokError(
"class is not allowed inside foreach loop");
4759 return ParseClass();
4762 return TokError(
"multiclass is not allowed inside foreach loop");
4763 return ParseMultiClass();
4769bool TGParser::ParseObjectList(
MultiClass *MC) {
4771 if (ParseObject(MC))
4780 if (ParseObjectList())
4788 return TokError(
"Unexpected token at top level");
4795bool TGParser::CheckTemplateArgValues(
4799 "expected as many values as locations");
4803 bool HasError =
false;
4805 const Init *ArgName =
nullptr;
4806 if (
Value->isPositional())
4807 ArgName = TArgs[
Value->getIndex()];
4808 if (
Value->isNamed())
4815 auto *CastValue = ArgValue->
getCastTo(ArgType);
4819 "result of template arg value cast has wrong type");
4823 Loc,
"Value specified for template argument '" +
4825 ArgValue->getType()->
getAsString() +
"; expected type " +
4834#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4843 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4844 <<
ListValue->getAsString() <<
" in {\n";
4853 errs() <<
"Record:\n";
4856 errs() <<
"Defs:\n";
4868 const Init *Message = ParseValue(CurRec);
4888 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