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);
2870 if (
const Init *Coerced =
V->convertInitializerTo(ItemType))
2879 TokError(
"expected ':' after case key, or ')' to close !switch operator");
2884 V = ParseValue(CurRec, ItemType);
2890 TokError(
"expected ',' after case value in !switch operator");
2895 "inconsistent keys and values for !switch");
2897 if (KeyAndCases.
size() < 2) {
2899 "there should be at least 1 'case: value' in the !switch operator");
2904 std::optional<const RecTy *> ValTypeOpt =
2905 resolveInitTypes(Vals,
"for !switch values");
2908 const RecTy *
ValType = *ValTypeOpt;
2910 TokError(
"could not determine type for !switch from its arguments");
2917 if (!resolveInitTypes(KeyAndCases,
"between !switch key and case keys"))
2923 size_t ValsSize = Vals.
size();
2930 assert(Conds.
size() == ValsSize &&
"inconsistent !switch to !cond reduction");
2961const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2963 const Init *
R =
nullptr;
2968 return ParseOperation(CurRec, ItemType);
2972 TokError(
"Unknown or reserved token when parsing a value");
2988 auto BinaryVal = Lex.getCurBinaryIntVal();
2990 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2997 std::string Val = Lex.getCurStrVal();
3002 Val += Lex.getCurStrVal();
3018 SMRange NameLoc = Lex.getLocRange();
3024 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
3032 "Expected a class name, got '" +
Name->getValue() +
"'");
3039 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
3042 if (CheckTemplateArgValues(Args, ArgLocs, Class))
3045 if (resolveArguments(Class, Args, NameLoc.
Start))
3048 if (TrackReferenceLocs)
3049 Class->appendReferenceLoc(NameLoc);
3053 SMLoc BraceLoc = Lex.getLoc();
3058 ParseValueList(Vals, CurRec);
3063 TokError(
"expected '}' at end of bit list value");
3072 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
3078 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
3085 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
3094 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
3095 ") is not convertable to a bit");
3100 std::reverse(NewBits.
begin(), NewBits.
end());
3107 const RecTy *DeducedEltTy =
nullptr;
3108 const ListRecTy *GivenListTy =
nullptr;
3113 TokError(Twine(
"Encountered a list when expecting a ") +
3117 GivenListTy = ListType;
3121 ParseValueList(Vals, CurRec,
3127 TokError(
"expected ']' at end of list value");
3131 const RecTy *GivenEltTy =
nullptr;
3134 GivenEltTy = ParseType();
3141 TokError(
"expected '>' at end of list element type");
3147 const RecTy *EltTy =
nullptr;
3148 for (
const Init *V : Vals) {
3154 TokError(
"Incompatible types in list elements");
3158 EltTy = TArg->getType();
3167 TokError(
"Incompatible types in list elements");
3184 TokError(Twine(
"Element type mismatch for list: element type '") +
3190 DeducedEltTy = EltTy;
3201 TokError(
"expected identifier or list of value types in dag init");
3205 const Init *
Operator = ParseValue(CurRec);
3210 const StringInit *OperatorName =
nullptr;
3213 TokError(
"expected variable name in dag operator");
3222 ParseDagArgList(DagArgs, CurRec);
3223 if (DagArgs.
empty())
3228 TokError(
"expected ')' in dag init");
3246const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3248 SMLoc LHSLoc = Lex.getLoc();
3249 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3255 switch (Lex.getCode()) {
3259 if (
Mode == ParseNameMode)
3263 SMLoc CurlyLoc = Lex.getLoc();
3265 SmallVector<unsigned, 16>
Ranges;
3266 ParseRangeList(Ranges);
3274 Error(CurlyLoc,
"Invalid bit range for value");
3280 TokError(
"expected '}' at end of bit range list");
3288 Error(LHSLoc,
"Invalid value, list expected");
3295 "' is invalid, list expected");
3300 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3309 LHSTy->getElementType())
3317 TokError(
"expected ']' at end of list slice");
3324 TokError(
"expected field identifier after '.'");
3327 SMRange FieldNameLoc = Lex.getLocRange();
3328 const StringInit *FieldName =
3330 if (!
Result->getFieldType(FieldName)) {
3331 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3332 Result->getAsString() +
"'");
3337 if (TrackReferenceLocs) {
3339 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3340 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3343 for (
const Record *R : RecTy->getClasses())
3344 if (
const auto *RV =
R->getValue(FieldName))
3345 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3356 SMLoc PasteLoc = Lex.getLoc();
3359 Error(PasteLoc,
"LHS of paste is not typed!");
3367 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3369 switch (Lex.getCode()) {
3376 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3393 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3399 const TypedInit *
RHS =
nullptr;
3402 switch (Lex.getCode()) {
3415 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3420 Error(PasteLoc,
"RHS of paste is not typed!");
3430 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3451void TGParser::ParseDagArgList(
3464 const Init *Val = ParseValue(CurRec);
3471 const StringInit *
VarName =
nullptr;
3474 TokError(
"expected variable name in dag literal");
3482 Result.emplace_back(Val, VarName);
3497 Result.push_back(ParseValue(CurRec, ItemType));
3507 Result.push_back(ParseValue(CurRec, ItemType));
3523bool TGParser::ParseTemplateArgValueList(
3526 assert(
Result.empty() &&
"Result vector is not empty");
3532 bool HasNamedArg =
false;
3533 unsigned ArgIndex = 0;
3535 if (ArgIndex >= TArgs.
size()) {
3536 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3543 const Init *
Value = ParseValue(
3545 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3552 return Error(ValueLoc,
3553 "The name of named argument should be a valid identifier");
3557 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3559 return Error(ValueLoc,
3560 "Argument " +
Name->getAsString() +
" doesn't exist");
3563 ValueLoc = Lex.getLoc();
3564 Value = ParseValue(CurRec, NamedArg->getType());
3567 return Error(ValueLoc,
3568 "The value of named argument should be initialized, "
3570 Value->getAsString() +
"'");
3577 return Error(ValueLoc,
3578 "Positional argument should be put before named argument");
3586 return TokError(
"Expected comma before next argument");
3601const Init *TGParser::ParseDeclaration(
Record *CurRec,
3602 bool ParsingTemplateArgs) {
3606 const RecTy *
Type = ParseType();
3611 TokError(
"Expected identifier in declaration");
3615 std::string Str = Lex.getCurStrVal();
3616 if (Str ==
"NAME") {
3617 TokError(
"'" + Str +
"' is a reserved variable name");
3621 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3622 TokError(
"local variable of this name already exists");
3626 SMLoc IdLoc = Lex.getLoc();
3631 if (!ParsingTemplateArgs) {
3632 BadField = AddValue(CurRec, IdLoc,
3633 RecordVal(DeclName, IdLoc,
Type,
3636 }
else if (CurRec) {
3639 AddValue(CurRec, IdLoc,
3642 assert(CurMultiClass &&
"invalid context for template argument");
3645 AddValue(CurRec, IdLoc,
3653 SMLoc ValLoc = Lex.getLoc();
3654 const Init *Val = ParseValue(CurRec,
Type);
3656 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3677TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3679 TokError(
"Expected identifier in foreach declaration");
3688 TokError(
"Expected '=' in foreach declaration");
3692 const RecTy *IterType =
nullptr;
3693 SmallVector<unsigned, 16>
Ranges;
3695 switch (Lex.getCode()) {
3698 ParseRangeList(Ranges);
3700 TokError(
"expected '}' at end of bit range list");
3707 SMLoc ValueLoc = Lex.getLoc();
3708 const Init *
I = ParseValue(
nullptr);
3714 ForeachListValue =
I;
3720 if (ParseRangePiece(Ranges, TI))
3725 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3726 if (CurMultiClass) {
3727 PrintNote({},
"references to multiclass template arguments cannot be "
3728 "resolved at this time");
3735 assert(!IterType &&
"Type already initialized?");
3737 std::vector<Init *> Values;
3738 for (
unsigned R : Ranges)
3756bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3760 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3763 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3771 SMLoc Loc = Lex.getLoc();
3772 TemplArg = ParseDeclaration(CurRec,
true );
3777 return Error(Loc,
"template argument with the same name has already been "
3784 return TokError(
"expected '>' at end of template argument list");
3801 SMLoc Loc = Lex.getLoc();
3803 std::string CurStr = Lex.getCurStrVal();
3805 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3815 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3833bool TGParser::ParseBodyItem(
Record *CurRec) {
3835 return ParseAssert(
nullptr, CurRec);
3838 return ParseDefvar(CurRec);
3841 return ParseDump(
nullptr, CurRec);
3844 if (!ParseDeclaration(CurRec,
false))
3848 return TokError(
"expected ';' after declaration");
3856 return TokError(
"expected field identifier after let");
3858 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3861 SmallVector<unsigned, 16> BitList;
3862 if (ParseOptionalRangeList(BitList))
3864 std::reverse(BitList.
begin(), BitList.
end());
3867 return TokError(
"expected '=' in let expression");
3871 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3880 const Init *Val = ParseValue(CurRec,
Type);
3885 return TokError(
"expected ';' after let expression");
3887 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3898bool TGParser::ParseBody(
Record *CurRec) {
3904 return TokError(
"Expected '{' to start body or ';' for declaration only");
3907 if (ParseBodyItem(CurRec))
3914 SMLoc SemiLoc = Lex.getLoc();
3916 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3917 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3925bool TGParser::ApplyLetStack(
Record *CurRec) {
3926 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3927 for (LetRecord &LR : LetInfo)
3928 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3938 return ApplyLetStack(
Entry.Rec.get());
3941 if (
Entry.Assertion)
3948 for (
auto &
E :
Entry.Loop->Entries) {
3949 if (ApplyLetStack(
E))
3965bool TGParser::ParseObjectBody(
Record *CurRec) {
3967 TGVarScope *ObjectScope =
PushScope(CurRec);
3972 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3979 if (AddSubClass(CurRec, SubClass))
3984 SubClass = ParseSubClassReference(CurRec,
false);
3988 if (ApplyLetStack(CurRec))
3991 bool Result = ParseBody(CurRec);
4001bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
4002 SMLoc DefLoc = Lex.getLoc();
4009 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
4012 std::unique_ptr<Record> CurRec;
4013 const Init *
Name = ParseObjectName(CurMultiClass);
4018 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
4021 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
4024 if (ParseObjectBody(CurRec.get()))
4027 return addEntry(std::move(CurRec));
4034bool TGParser::ParseDefset() {
4039 Defset.Loc = Lex.getLoc();
4040 const RecTy *
Type = ParseType();
4048 return TokError(
"expected identifier");
4049 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4050 if (Records.getGlobal(DeclName->
getValue()))
4051 return TokError(
"def or global variable of this name already exists");
4057 SMLoc BraceLoc = Lex.getLoc();
4060 Defsets.push_back(&Defset);
4061 bool Err = ParseObjectList(
nullptr);
4067 TokError(
"expected '}' at end of defset");
4068 return Error(BraceLoc,
"to match this '{'");
4071 Records.addExtraGlobal(DeclName->
getValue(),
4080bool TGParser::ParseDeftype() {
4085 return TokError(
"expected identifier");
4087 const std::string
TypeName = Lex.getCurStrVal();
4088 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
4089 return TokError(
"type of this name '" + TypeName +
"' already exists");
4095 SMLoc Loc = Lex.getLoc();
4096 const RecTy *
Type = ParseType();
4101 return Error(Loc,
"cannot define type alias for class type '" +
4102 Type->getAsString() +
"'");
4116bool TGParser::ParseDefvar(
Record *CurRec) {
4121 return TokError(
"expected identifier");
4122 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4123 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
4124 return TokError(
"local variable of this name already exists");
4129 if (V && !
V->isTemplateArg())
4130 return TokError(
"field of this name already exists");
4135 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
4136 return TokError(
"def or global variable of this name already exists");
4142 const Init *
Value = ParseValue(CurRec);
4149 if (!CurScope->isOutermost())
4163bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4164 SMLoc Loc = Lex.getLoc();
4170 const Init *ListValue =
nullptr;
4171 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4173 return TokError(
"expected declaration in for");
4179 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4181 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4182 Loops.push_back(std::move(TheLoop));
4186 if (ParseObject(CurMultiClass))
4189 SMLoc BraceLoc = Lex.getLoc();
4194 if (ParseObjectList(CurMultiClass))
4198 TokError(
"expected '}' at end of foreach command");
4199 return Error(BraceLoc,
"to match this '{'");
4206 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4209 return addEntry(std::move(Loop));
4217bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4218 SMLoc Loc = Lex.getLoc();
4224 const Init *Condition = ParseValue(
nullptr);
4238 const ListInit *SingletonList =
4244 const Init *ThenClauseList =
4248 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4250 if (ParseIfBody(CurMultiClass,
"then"))
4253 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4256 if (addEntry(std::move(Loop)))
4266 const Init *ElseClauseList =
4271 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4273 if (ParseIfBody(CurMultiClass,
"else"))
4276 Loop = std::move(Loops.back());
4279 if (addEntry(std::move(Loop)))
4297 if (ParseObject(CurMultiClass))
4300 SMLoc BraceLoc = Lex.getLoc();
4305 if (ParseObjectList(CurMultiClass))
4309 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4310 return Error(BraceLoc,
"to match this '{'");
4325 SMLoc ConditionLoc = Lex.getLoc();
4326 const Init *Condition = ParseValue(CurRec);
4331 TokError(
"expected ',' in assert statement");
4335 const Init *Message = ParseValue(CurRec);
4343 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4345 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4354bool TGParser::ParseClass() {
4359 return TokError(
"expected class name after 'class' keyword");
4361 const std::string &
Name = Lex.getCurStrVal();
4362 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4369 "' already defined");
4374 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4376 CurRec = NewRec.get();
4377 Records.addClass(std::move(NewRec));
4380 if (TypeAliases.count(Name))
4381 return TokError(
"there is already a defined type alias '" + Name +
"'");
4386 TGVarScope *ClassScope =
PushScope(CurRec);
4389 if (ParseTemplateArgList(CurRec))
4392 if (ParseObjectBody(CurRec))
4395 if (!NoWarnOnUnusedTemplateArgs)
4411 TokError(
"expected identifier in let definition");
4416 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4420 SmallVector<unsigned, 16>
Bits;
4421 if (ParseOptionalRangeList(Bits)) {
4425 std::reverse(
Bits.begin(),
Bits.end());
4428 TokError(
"expected '=' in let expression");
4433 const Init *Val = ParseValue(
nullptr);
4440 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4450bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4456 ParseLetList(LetInfo);
4457 if (LetInfo.
empty())
4459 LetStack.push_back(std::move(LetInfo));
4462 return TokError(
"expected 'in' at end of top-level 'let'");
4467 if (ParseObject(CurMultiClass))
4470 SMLoc BraceLoc = Lex.getLoc();
4478 if (ParseObjectList(CurMultiClass))
4482 TokError(
"expected '}' at end of top level let command");
4483 return Error(BraceLoc,
"to match this '{'");
4490 LetStack.pop_back();
4507bool TGParser::ParseMultiClass() {
4512 return TokError(
"expected identifier after multiclass for name");
4513 std::string
Name = Lex.getCurStrVal();
4515 auto Result = MultiClasses.try_emplace(
4516 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4519 return TokError(
"multiclass '" + Name +
"' already defined");
4521 CurMultiClass =
Result.first->second.get();
4525 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4529 if (ParseTemplateArgList(
nullptr))
4532 bool inherits =
false;
4539 SubMultiClassReference SubMultiClass =
4540 ParseSubMultiClassReference(CurMultiClass);
4543 if (!SubMultiClass.
MC)
4547 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4552 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4558 return TokError(
"expected '{' in multiclass definition");
4560 return TokError(
"expected ';' in multiclass definition");
4563 return TokError(
"multiclass must contain at least one def");
4566 switch (Lex.getCode()) {
4568 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4569 "'foreach', 'if', or 'let' in multiclass body");
4579 if (ParseObject(CurMultiClass))
4587 SMLoc SemiLoc = Lex.getLoc();
4589 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4590 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4594 if (!NoWarnOnUnusedTemplateArgs)
4595 CurMultiClass->Rec.checkUnusedTemplateArgs();
4598 CurMultiClass =
nullptr;
4606bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4610 const Init *DefmName = ParseObjectName(CurMultiClass);
4614 DefmName = Records.getNewAnonymousName();
4623 return TokError(
"expected ':' after defm identifier");
4626 std::vector<RecordsEntry> NewEntries;
4629 bool InheritFromClass =
false;
4634 SMLoc SubClassLoc = Lex.getLoc();
4635 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4645 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4646 assert(MC &&
"Didn't lookup multiclass correctly?");
4649 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4653 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4654 &NewEntries, &SubClassLoc))
4661 return TokError(
"expected identifier");
4663 SubClassLoc = Lex.getLoc();
4667 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4669 if (InheritFromClass)
4672 Ref = ParseSubClassReference(
nullptr,
true);
4675 if (InheritFromClass) {
4678 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4686 for (
auto &
E : NewEntries) {
4688 if (AddSubClass(
E, SubClass))
4694 SubClass = ParseSubClassReference(
nullptr,
false);
4698 for (
auto &
E : NewEntries) {
4699 if (ApplyLetStack(
E))
4702 addEntry(std::move(
E));
4706 return TokError(
"expected ';' at end of defm");
4724 switch (Lex.getCode()) {
4727 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4729 return ParseAssert(MC);
4731 return ParseDef(MC);
4733 return ParseDefm(MC);
4735 return ParseDeftype();
4737 return ParseDefvar();
4739 return ParseDump(MC);
4741 return ParseForeach(MC);
4745 return ParseTopLevelLet(MC);
4748 return TokError(
"defset is not allowed inside multiclass");
4749 return ParseDefset();
4752 return TokError(
"class is not allowed inside multiclass");
4754 return TokError(
"class is not allowed inside foreach loop");
4755 return ParseClass();
4758 return TokError(
"multiclass is not allowed inside foreach loop");
4759 return ParseMultiClass();
4765bool TGParser::ParseObjectList(
MultiClass *MC) {
4767 if (ParseObject(MC))
4776 if (ParseObjectList())
4784 return TokError(
"Unexpected token at top level");
4791bool TGParser::CheckTemplateArgValues(
4795 "expected as many values as locations");
4799 bool HasError =
false;
4801 const Init *ArgName =
nullptr;
4802 if (
Value->isPositional())
4803 ArgName = TArgs[
Value->getIndex()];
4804 if (
Value->isNamed())
4811 auto *CastValue = ArgValue->
getCastTo(ArgType);
4815 "result of template arg value cast has wrong type");
4819 Loc,
"Value specified for template argument '" +
4821 ArgValue->getType()->
getAsString() +
"; expected type " +
4830#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4839 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4840 <<
ListValue->getAsString() <<
" in {\n";
4849 errs() <<
"Record:\n";
4852 errs() <<
"Defs:\n";
4864 const Init *Message = ParseValue(CurRec);
4884 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