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");
2303 TokError(
"expected ',' in !foldl");
2308 TokError(
"fourth argument of !foldl must be an identifier");
2314 TokError(Twine(
"right !foldl variable '") +
B->getAsString() +
2315 "' already defined");
2320 TokError(
"expected ',' in !foldl");
2327 std::unique_ptr<Record> ParseRecTmp;
2328 Record *ParseRec = CurRec;
2332 ParseRec = ParseRecTmp.get();
2335 TGVarScope *FoldScope =
PushScope(ParseRec);
2339 const Init *ExprUntyped = ParseValue(ParseRec);
2340 ParseRec->removeValue(
A);
2341 ParseRec->removeValue(
B);
2348 TokError(
"could not get type of !foldl expression");
2352 if (Expr->getType() !=
Start->getType()) {
2353 TokError(Twine(
"!foldl expression must be of same type as start (") +
2354 Start->getType()->getAsString() +
"), but is of type " +
2360 TokError(
"expected ')' in fold operator");
2375const RecTy *TGParser::ParseOperatorType() {
2376 const RecTy *
Type =
nullptr;
2379 TokError(
"expected type name for operator");
2384 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2389 TokError(
"expected type name for operator");
2394 TokError(
"expected type name for operator");
2404const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2405 const RecTy *ItemType) {
2412 TokError(
"expected '(' after !substr operator");
2416 const Init *
LHS = ParseValue(CurRec);
2421 TokError(
"expected ',' in !substr operator");
2425 SMLoc MHSLoc = Lex.getLoc();
2426 const Init *MHS = ParseValue(CurRec);
2430 SMLoc RHSLoc = Lex.getLoc();
2433 RHSLoc = Lex.getLoc();
2434 RHS = ParseValue(CurRec);
2442 TokError(
"expected ')' in !substr operator");
2446 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2447 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2448 "', got '" +
Type->getAsString() +
"'");
2453 TokError(
"could not determine type of the string in !substr");
2457 TokError(Twine(
"expected string, got type '") +
2458 LHSt->getType()->getAsString() +
"'");
2464 TokError(
"could not determine type of the start position in !substr");
2468 Error(MHSLoc, Twine(
"expected int, got type '") +
2469 MHSt->getType()->getAsString() +
"'");
2476 TokError(
"could not determine type of the length in !substr");
2480 TokError(Twine(
"expected int, got type '") +
2481 RHSt->getType()->getAsString() +
"'");
2492const Init *TGParser::ParseOperationFind(
Record *CurRec,
2493 const RecTy *ItemType) {
2500 TokError(
"expected '(' after !find operator");
2504 const Init *
LHS = ParseValue(CurRec);
2509 TokError(
"expected ',' in !find operator");
2513 SMLoc MHSLoc = Lex.getLoc();
2514 const Init *MHS = ParseValue(CurRec);
2518 SMLoc RHSLoc = Lex.getLoc();
2521 RHSLoc = Lex.getLoc();
2522 RHS = ParseValue(CurRec);
2530 TokError(
"expected ')' in !find operator");
2534 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2535 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2536 "', got '" +
Type->getAsString() +
"'");
2541 TokError(
"could not determine type of the source string in !find");
2545 TokError(Twine(
"expected string, got type '") +
2546 LHSt->getType()->getAsString() +
"'");
2552 TokError(
"could not determine type of the target string in !find");
2556 Error(MHSLoc, Twine(
"expected string, got type '") +
2557 MHSt->getType()->getAsString() +
"'");
2564 TokError(
"could not determine type of the start position in !find");
2568 TokError(Twine(
"expected int, got type '") +
2569 RHSt->getType()->getAsString() +
"'");
2582const Init *TGParser::ParseOperationListComprehension(
Record *CurRec,
2583 const RecTy *ItemType) {
2584 SMLoc OpLoc = Lex.getLoc();
2588 TokError(
"expected '(' after !foreach/!filter");
2593 TokError(
"first argument of !foreach/!filter must be an identifier");
2601 TokError(Twine(
"iteration variable '") +
LHS->getAsString() +
2602 "' is already defined");
2607 TokError(
"expected ',' in !foreach/!filter");
2611 const Init *MHS = ParseValue(CurRec);
2616 TokError(
"expected ',' in !foreach/!filter");
2622 TokError(
"could not get type of !foreach/!filter list or dag");
2626 const RecTy *InEltType =
nullptr;
2627 const RecTy *ExprEltType =
nullptr;
2631 InEltType = InListTy->getElementType();
2636 ExprEltType = OutListTy->getElementType();
2642 ExprEltType =
nullptr;
2648 Error(OpLoc,
"expected value of type '" +
2650 "', but got list type");
2657 TokError(
"!filter must have a list argument");
2660 TokError(
"!sort must have a list argument");
2667 InEltType = InDagTy;
2670 "', but got dag type");
2677 TokError(
"!foreach must have a list or dag argument");
2680 TokError(
"!filter must have a list argument");
2683 TokError(
"!sort must have a list argument");
2692 std::unique_ptr<Record> ParseRecTmp;
2693 Record *ParseRec = CurRec;
2697 ParseRec = ParseRecTmp.get();
2699 TGVarScope *TempScope =
PushScope(ParseRec);
2701 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2702 ParseRec->removeValue(
LHS);
2708 TokError(
"expected ')' in !foreach/!filter");
2712 const RecTy *OutType;
2718 OutType = InEltType;
2722 TokError(
"could not get type of !foreach result expression");
2746std::optional<const RecTy *>
2748 const RecTy *
Type =
nullptr;
2749 for (
const Init *V : Inits) {
2761 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
"' and '" +
2763 return std::nullopt;
2773const Init *TGParser::ParseOperationCond(
Record *CurRec,
2774 const RecTy *ItemType) {
2778 TokError(
"expected '(' after !cond operator");
2789 const Init *
V = ParseValue(CurRec);
2795 TokError(
"expected ':' following a condition in !cond operator");
2799 V = ParseValue(CurRec, ItemType);
2808 TokError(
"expected ',' or ')' following a value in !cond operator");
2813 if (Cases.
size() < 1) {
2815 "there should be at least 1 'condition : value' in the !cond operator");
2820 std::optional<const RecTy *> TypeOpt = resolveInitTypes(Vals,
"for !cond");
2823 const RecTy *
Type = *TypeOpt;
2825 TokError(
"could not determine type for !cond from its arguments");
2832const Init *TGParser::ParseOperationSwitch(
Record *CurRec,
2833 const RecTy *ItemType) {
2837 TokError(
"expected '(' after !switch operator");
2842 const Init *
Key = ParseValue(CurRec);
2849 TokError(
"expected ',' after key in !switch operator");
2857 const Init *
V = ParseValue(CurRec);
2866 if (
const Init *Coerced =
V->convertInitializerTo(ItemType))
2876 TokError(
"expected ':' after case key, or ')' to close !switch operator");
2881 V = ParseValue(CurRec, ItemType);
2887 TokError(
"expected ',' after case value in !switch operator");
2892 "inconsistent keys and values for !switch");
2894 if (KeyAndCases.
size() < 2) {
2896 "there should be at least 1 'case: value' in the !switch operator");
2901 std::optional<const RecTy *> ValTypeOpt =
2902 resolveInitTypes(Vals,
"for !switch values");
2905 const RecTy *
ValType = *ValTypeOpt;
2907 TokError(
"could not determine type for !switch from its arguments");
2914 if (!resolveInitTypes(KeyAndCases,
"between !switch key and case keys"))
2920 size_t ValsSize = Vals.
size();
2927 assert(Conds.
size() == ValsSize &&
"inconsistent !switch to !cond reduction");
2958const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2960 const Init *
R =
nullptr;
2965 return ParseOperation(CurRec, ItemType);
2969 TokError(
"Unknown or reserved token when parsing a value");
2985 auto BinaryVal = Lex.getCurBinaryIntVal();
2987 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2994 std::string Val = Lex.getCurStrVal();
2999 Val += Lex.getCurStrVal();
3015 SMRange NameLoc = Lex.getLocRange();
3021 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
3029 "Expected a class name, got '" +
Name->getValue() +
"'");
3036 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
3039 if (CheckTemplateArgValues(Args, ArgLocs, Class))
3042 if (resolveArguments(Class, Args, NameLoc.
Start))
3045 if (TrackReferenceLocs)
3046 Class->appendReferenceLoc(NameLoc);
3050 SMLoc BraceLoc = Lex.getLoc();
3055 ParseValueList(Vals, CurRec);
3060 TokError(
"expected '}' at end of bit list value");
3069 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
3075 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
3082 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
3091 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
3092 ") is not convertable to a bit");
3097 std::reverse(NewBits.
begin(), NewBits.
end());
3104 const RecTy *DeducedEltTy =
nullptr;
3105 const ListRecTy *GivenListTy =
nullptr;
3110 TokError(Twine(
"Encountered a list when expecting a ") +
3114 GivenListTy = ListType;
3118 ParseValueList(Vals, CurRec,
3124 TokError(
"expected ']' at end of list value");
3128 const RecTy *GivenEltTy =
nullptr;
3131 GivenEltTy = ParseType();
3138 TokError(
"expected '>' at end of list element type");
3144 const RecTy *EltTy =
nullptr;
3145 for (
const Init *V : Vals) {
3151 TokError(
"Incompatible types in list elements");
3155 EltTy = TArg->getType();
3164 TokError(
"Incompatible types in list elements");
3181 TokError(Twine(
"Element type mismatch for list: element type '") +
3187 DeducedEltTy = EltTy;
3198 TokError(
"expected identifier or list of value types in dag init");
3202 const Init *
Operator = ParseValue(CurRec);
3207 const StringInit *OperatorName =
nullptr;
3210 TokError(
"expected variable name in dag operator");
3219 ParseDagArgList(DagArgs, CurRec);
3220 if (DagArgs.
empty())
3225 TokError(
"expected ')' in dag init");
3243const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3245 SMLoc LHSLoc = Lex.getLoc();
3246 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3252 switch (Lex.getCode()) {
3256 if (
Mode == ParseNameMode)
3260 SMLoc CurlyLoc = Lex.getLoc();
3262 SmallVector<unsigned, 16>
Ranges;
3263 ParseRangeList(Ranges);
3271 Error(CurlyLoc,
"Invalid bit range for value");
3277 TokError(
"expected '}' at end of bit range list");
3285 Error(LHSLoc,
"Invalid value, list expected");
3292 "' is invalid, list expected");
3297 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3306 LHSTy->getElementType())
3314 TokError(
"expected ']' at end of list slice");
3321 TokError(
"expected field identifier after '.'");
3324 SMRange FieldNameLoc = Lex.getLocRange();
3325 const StringInit *FieldName =
3327 if (!
Result->getFieldType(FieldName)) {
3328 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3329 Result->getAsString() +
"'");
3334 if (TrackReferenceLocs) {
3336 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3337 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3340 for (
const Record *R : RecTy->getClasses())
3341 if (
const auto *RV =
R->getValue(FieldName))
3342 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3353 SMLoc PasteLoc = Lex.getLoc();
3356 Error(PasteLoc,
"LHS of paste is not typed!");
3364 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3366 switch (Lex.getCode()) {
3373 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3390 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3396 const TypedInit *
RHS =
nullptr;
3399 switch (Lex.getCode()) {
3412 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3417 Error(PasteLoc,
"RHS of paste is not typed!");
3427 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3448void TGParser::ParseDagArgList(
3461 const Init *Val = ParseValue(CurRec);
3468 const StringInit *
VarName =
nullptr;
3471 TokError(
"expected variable name in dag literal");
3479 Result.emplace_back(Val, VarName);
3494 Result.push_back(ParseValue(CurRec, ItemType));
3504 Result.push_back(ParseValue(CurRec, ItemType));
3520bool TGParser::ParseTemplateArgValueList(
3523 assert(
Result.empty() &&
"Result vector is not empty");
3529 bool HasNamedArg =
false;
3530 unsigned ArgIndex = 0;
3532 if (ArgIndex >= TArgs.
size()) {
3533 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3540 const Init *
Value = ParseValue(
3542 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3549 return Error(ValueLoc,
3550 "The name of named argument should be a valid identifier");
3554 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3556 return Error(ValueLoc,
3557 "Argument " +
Name->getAsString() +
" doesn't exist");
3560 ValueLoc = Lex.getLoc();
3561 Value = ParseValue(CurRec, NamedArg->getType());
3564 return Error(ValueLoc,
3565 "The value of named argument should be initialized, "
3567 Value->getAsString() +
"'");
3574 return Error(ValueLoc,
3575 "Positional argument should be put before named argument");
3583 return TokError(
"Expected comma before next argument");
3598const Init *TGParser::ParseDeclaration(
Record *CurRec,
3599 bool ParsingTemplateArgs) {
3603 const RecTy *
Type = ParseType();
3608 TokError(
"Expected identifier in declaration");
3612 std::string Str = Lex.getCurStrVal();
3613 if (Str ==
"NAME") {
3614 TokError(
"'" + Str +
"' is a reserved variable name");
3618 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3619 TokError(
"local variable of this name already exists");
3623 SMLoc IdLoc = Lex.getLoc();
3628 if (!ParsingTemplateArgs) {
3629 BadField = AddValue(CurRec, IdLoc,
3630 RecordVal(DeclName, IdLoc,
Type,
3633 }
else if (CurRec) {
3636 AddValue(CurRec, IdLoc,
3639 assert(CurMultiClass &&
"invalid context for template argument");
3642 AddValue(CurRec, IdLoc,
3650 SMLoc ValLoc = Lex.getLoc();
3651 const Init *Val = ParseValue(CurRec,
Type);
3653 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3674TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3676 TokError(
"Expected identifier in foreach declaration");
3685 TokError(
"Expected '=' in foreach declaration");
3689 const RecTy *IterType =
nullptr;
3690 SmallVector<unsigned, 16>
Ranges;
3692 switch (Lex.getCode()) {
3695 ParseRangeList(Ranges);
3697 TokError(
"expected '}' at end of bit range list");
3704 SMLoc ValueLoc = Lex.getLoc();
3705 const Init *
I = ParseValue(
nullptr);
3711 ForeachListValue =
I;
3717 if (ParseRangePiece(Ranges, TI))
3722 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3723 if (CurMultiClass) {
3724 PrintNote({},
"references to multiclass template arguments cannot be "
3725 "resolved at this time");
3732 assert(!IterType &&
"Type already initialized?");
3734 std::vector<Init *>
Values;
3735 for (
unsigned R : Ranges)
3753bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3757 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3760 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3768 SMLoc Loc = Lex.getLoc();
3769 TemplArg = ParseDeclaration(CurRec,
true );
3774 return Error(Loc,
"template argument with the same name has already been "
3781 return TokError(
"expected '>' at end of template argument list");
3798 SMLoc Loc = Lex.getLoc();
3800 std::string CurStr = Lex.getCurStrVal();
3802 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3812 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3830bool TGParser::ParseBodyItem(
Record *CurRec) {
3832 return ParseAssert(
nullptr, CurRec);
3835 return ParseDefvar(CurRec);
3838 return ParseDump(
nullptr, CurRec);
3841 if (!ParseDeclaration(CurRec,
false))
3845 return TokError(
"expected ';' after declaration");
3853 return TokError(
"expected field identifier after let");
3855 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3858 SmallVector<unsigned, 16> BitList;
3859 if (ParseOptionalRangeList(BitList))
3861 std::reverse(BitList.
begin(), BitList.
end());
3864 return TokError(
"expected '=' in let expression");
3868 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3877 const Init *Val = ParseValue(CurRec,
Type);
3882 return TokError(
"expected ';' after let expression");
3884 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3895bool TGParser::ParseBody(
Record *CurRec) {
3901 return TokError(
"Expected '{' to start body or ';' for declaration only");
3904 if (ParseBodyItem(CurRec))
3911 SMLoc SemiLoc = Lex.getLoc();
3913 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3914 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3922bool TGParser::ApplyLetStack(
Record *CurRec) {
3923 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3924 for (LetRecord &LR : LetInfo)
3925 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3935 return ApplyLetStack(
Entry.Rec.get());
3938 if (
Entry.Assertion)
3945 for (
auto &
E :
Entry.Loop->Entries) {
3946 if (ApplyLetStack(
E))
3962bool TGParser::ParseObjectBody(
Record *CurRec) {
3964 TGVarScope *ObjectScope =
PushScope(CurRec);
3969 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3976 if (AddSubClass(CurRec, SubClass))
3981 SubClass = ParseSubClassReference(CurRec,
false);
3985 if (ApplyLetStack(CurRec))
3988 bool Result = ParseBody(CurRec);
3998bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3999 SMLoc DefLoc = Lex.getLoc();
4006 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
4009 std::unique_ptr<Record> CurRec;
4010 const Init *
Name = ParseObjectName(CurMultiClass);
4015 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
4018 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
4021 if (ParseObjectBody(CurRec.get()))
4024 return addEntry(std::move(CurRec));
4031bool TGParser::ParseDefset() {
4036 Defset.Loc = Lex.getLoc();
4037 const RecTy *
Type = ParseType();
4045 return TokError(
"expected identifier");
4046 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4047 if (Records.getGlobal(DeclName->
getValue()))
4048 return TokError(
"def or global variable of this name already exists");
4054 SMLoc BraceLoc = Lex.getLoc();
4057 Defsets.push_back(&Defset);
4058 bool Err = ParseObjectList(
nullptr);
4064 TokError(
"expected '}' at end of defset");
4065 return Error(BraceLoc,
"to match this '{'");
4068 Records.addExtraGlobal(DeclName->
getValue(),
4077bool TGParser::ParseDeftype() {
4082 return TokError(
"expected identifier");
4084 const std::string
TypeName = Lex.getCurStrVal();
4085 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
4086 return TokError(
"type of this name '" + TypeName +
"' already exists");
4092 SMLoc Loc = Lex.getLoc();
4093 const RecTy *
Type = ParseType();
4098 return Error(Loc,
"cannot define type alias for class type '" +
4099 Type->getAsString() +
"'");
4113bool TGParser::ParseDefvar(
Record *CurRec) {
4118 return TokError(
"expected identifier");
4119 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4120 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
4121 return TokError(
"local variable of this name already exists");
4126 if (V && !
V->isTemplateArg())
4127 return TokError(
"field of this name already exists");
4132 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
4133 return TokError(
"def or global variable of this name already exists");
4139 const Init *
Value = ParseValue(CurRec);
4146 if (!CurScope->isOutermost())
4160bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4161 SMLoc Loc = Lex.getLoc();
4167 const Init *ListValue =
nullptr;
4168 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4170 return TokError(
"expected declaration in for");
4176 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4178 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4179 Loops.push_back(std::move(TheLoop));
4183 if (ParseObject(CurMultiClass))
4186 SMLoc BraceLoc = Lex.getLoc();
4191 if (ParseObjectList(CurMultiClass))
4195 TokError(
"expected '}' at end of foreach command");
4196 return Error(BraceLoc,
"to match this '{'");
4203 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4206 return addEntry(std::move(Loop));
4214bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4215 SMLoc Loc = Lex.getLoc();
4221 const Init *Condition = ParseValue(
nullptr);
4235 const ListInit *SingletonList =
4241 const Init *ThenClauseList =
4245 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4247 if (ParseIfBody(CurMultiClass,
"then"))
4250 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4253 if (addEntry(std::move(Loop)))
4263 const Init *ElseClauseList =
4268 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4270 if (ParseIfBody(CurMultiClass,
"else"))
4273 Loop = std::move(Loops.back());
4276 if (addEntry(std::move(Loop)))
4294 if (ParseObject(CurMultiClass))
4297 SMLoc BraceLoc = Lex.getLoc();
4302 if (ParseObjectList(CurMultiClass))
4306 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4307 return Error(BraceLoc,
"to match this '{'");
4322 SMLoc ConditionLoc = Lex.getLoc();
4323 const Init *Condition = ParseValue(CurRec);
4328 TokError(
"expected ',' in assert statement");
4332 const Init *Message = ParseValue(CurRec);
4340 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4342 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4351bool TGParser::ParseClass() {
4356 return TokError(
"expected class name after 'class' keyword");
4358 const std::string &
Name = Lex.getCurStrVal();
4359 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4366 "' already defined");
4371 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4373 CurRec = NewRec.get();
4374 Records.addClass(std::move(NewRec));
4377 if (TypeAliases.count(Name))
4378 return TokError(
"there is already a defined type alias '" + Name +
"'");
4383 TGVarScope *ClassScope =
PushScope(CurRec);
4386 if (ParseTemplateArgList(CurRec))
4389 if (ParseObjectBody(CurRec))
4392 if (!NoWarnOnUnusedTemplateArgs)
4408 TokError(
"expected identifier in let definition");
4413 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4417 SmallVector<unsigned, 16>
Bits;
4418 if (ParseOptionalRangeList(Bits)) {
4422 std::reverse(
Bits.begin(),
Bits.end());
4425 TokError(
"expected '=' in let expression");
4430 const Init *Val = ParseValue(
nullptr);
4437 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4447bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4453 ParseLetList(LetInfo);
4454 if (LetInfo.
empty())
4456 LetStack.push_back(std::move(LetInfo));
4459 return TokError(
"expected 'in' at end of top-level 'let'");
4464 if (ParseObject(CurMultiClass))
4467 SMLoc BraceLoc = Lex.getLoc();
4475 if (ParseObjectList(CurMultiClass))
4479 TokError(
"expected '}' at end of top level let command");
4480 return Error(BraceLoc,
"to match this '{'");
4487 LetStack.pop_back();
4504bool TGParser::ParseMultiClass() {
4509 return TokError(
"expected identifier after multiclass for name");
4510 std::string
Name = Lex.getCurStrVal();
4512 auto Result = MultiClasses.try_emplace(
4513 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4516 return TokError(
"multiclass '" + Name +
"' already defined");
4518 CurMultiClass =
Result.first->second.get();
4522 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4526 if (ParseTemplateArgList(
nullptr))
4529 bool inherits =
false;
4536 SubMultiClassReference SubMultiClass =
4537 ParseSubMultiClassReference(CurMultiClass);
4540 if (!SubMultiClass.
MC)
4544 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4549 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4555 return TokError(
"expected '{' in multiclass definition");
4557 return TokError(
"expected ';' in multiclass definition");
4560 return TokError(
"multiclass must contain at least one def");
4563 switch (Lex.getCode()) {
4565 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4566 "'foreach', 'if', or 'let' in multiclass body");
4576 if (ParseObject(CurMultiClass))
4584 SMLoc SemiLoc = Lex.getLoc();
4586 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4587 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4591 if (!NoWarnOnUnusedTemplateArgs)
4592 CurMultiClass->Rec.checkUnusedTemplateArgs();
4595 CurMultiClass =
nullptr;
4603bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4607 const Init *DefmName = ParseObjectName(CurMultiClass);
4611 DefmName = Records.getNewAnonymousName();
4620 return TokError(
"expected ':' after defm identifier");
4623 std::vector<RecordsEntry> NewEntries;
4626 bool InheritFromClass =
false;
4631 SMLoc SubClassLoc = Lex.getLoc();
4632 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4642 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4643 assert(MC &&
"Didn't lookup multiclass correctly?");
4646 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4650 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4651 &NewEntries, &SubClassLoc))
4658 return TokError(
"expected identifier");
4660 SubClassLoc = Lex.getLoc();
4664 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4666 if (InheritFromClass)
4669 Ref = ParseSubClassReference(
nullptr,
true);
4672 if (InheritFromClass) {
4675 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4683 for (
auto &
E : NewEntries) {
4685 if (AddSubClass(
E, SubClass))
4691 SubClass = ParseSubClassReference(
nullptr,
false);
4695 for (
auto &
E : NewEntries) {
4696 if (ApplyLetStack(
E))
4699 addEntry(std::move(
E));
4703 return TokError(
"expected ';' at end of defm");
4721 switch (Lex.getCode()) {
4724 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4726 return ParseAssert(MC);
4728 return ParseDef(MC);
4730 return ParseDefm(MC);
4732 return ParseDeftype();
4734 return ParseDefvar();
4736 return ParseDump(MC);
4738 return ParseForeach(MC);
4742 return ParseTopLevelLet(MC);
4745 return TokError(
"defset is not allowed inside multiclass");
4746 return ParseDefset();
4749 return TokError(
"class is not allowed inside multiclass");
4751 return TokError(
"class is not allowed inside foreach loop");
4752 return ParseClass();
4755 return TokError(
"multiclass is not allowed inside foreach loop");
4756 return ParseMultiClass();
4762bool TGParser::ParseObjectList(
MultiClass *MC) {
4764 if (ParseObject(MC))
4773 if (ParseObjectList())
4781 return TokError(
"Unexpected token at top level");
4788bool TGParser::CheckTemplateArgValues(
4792 "expected as many values as locations");
4796 bool HasError =
false;
4798 const Init *ArgName =
nullptr;
4799 if (
Value->isPositional())
4800 ArgName = TArgs[
Value->getIndex()];
4801 if (
Value->isNamed())
4808 auto *CastValue = ArgValue->
getCastTo(ArgType);
4812 "result of template arg value cast has wrong type");
4816 Loc,
"Value specified for template argument '" +
4818 ArgValue->getType()->
getAsString() +
"; expected type " +
4827#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4836 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4837 <<
ListValue->getAsString() <<
" in {\n";
4846 errs() <<
"Record:\n";
4849 errs() <<
"Defs:\n";
4861 const Init *Message = ParseValue(CurRec);
4881 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.
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
RelativeUniformCounterPtr Values
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI void PrintError(const Twine &Msg)
std::string utostr(uint64_t X, bool isNeg=false)
LetMode
Specifies how a 'let' assignment interacts with the existing field value.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
LLVM_ABI bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
LLVM_ABI void PrintNote(const Twine &Msg)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI void dumpMessage(SMLoc Loc, const Init *Message)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
@ 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