21#include "llvm/Config/llvm-config.h"
65#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
67 errs() <<
"Multiclass:\n";
71 errs() <<
"Template args:\n";
79 for (
unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
80 const Init *Bit = BV->getBit(i);
81 bool IsReference =
false;
84 if (R.getValue(VI->getName()))
90 if (!(IsReference || Bit->isConcrete()))
97 for (
const RecordVal &RV : R.getValues()) {
102 if (RV.isNonconcreteOK())
105 const Init *
V = RV.getValue();
109 Twine(
"Initializer of '") + RV.getNameInitAsString() +
110 "' in '" + R.getNameInitAsString() +
111 "' could not be fully resolved: " +
V->getAsString());
126 NewName = BinOp->Fold(&CurRec);
142 bool TrackReferenceLocs)
const {
144 auto It = Vars.find(Name->getValue());
145 if (It != Vars.end())
148 auto FindValueInArgs = [&](
Record *Rec) ->
const Init * {
152 if (
RecordVal *RV = Rec->getTemplateArg(ArgName)) {
154 if (TrackReferenceLocs)
155 Rec->appendValueReferenceLoc(ArgName, NameLoc);
158 return Name->getValue() ==
"NAME"
172 if (TrackReferenceLocs)
173 CurRec->appendValueReferenceLoc(Name, NameLoc);
178 if (CurRec->isClass())
179 if (
auto *V = FindValueInArgs(CurRec))
186 if (CurLoop->IterVar) {
187 const VarInit *IterVar = CurLoop->IterVar;
196 if (
auto *V = FindValueInArgs(&CurMultiClass->Rec))
204 return Parent->getVar(Records, ParsingMultiClass, Name, NameLoc,
213 CurRec = &CurMultiClass->
Rec;
218 return Error(
Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
220 "' is incompatible with " +
221 "previous definition of type '" +
222 ERV->getType()->getAsString() +
"'");
236 assert(
V &&
"Cannot set a null value");
239 CurRec = &CurMultiClass->Rec;
242 RecordVal *RV = CurRec->
getValue(ValName);
253 if (!BitList.
empty())
254 return Error(Loc,
"Cannot use append/prepend with bit range");
256 const Init *CurrentValue = RV->
getValue();
257 const RecTy *FieldType = RV->
getType();
263 const Init *
LHS = IsAppendMode ? CurrentValue :
V;
264 const Init *
RHS = IsAppendMode ?
V : CurrentValue;
274 return Error(Loc, Twine(
"Cannot ") +
275 (IsAppendMode ?
"append to" :
"prepend to") +
277 "' of type '" + FieldType->getAsString() +
278 "' (expected list, string, code, or dag)");
288 if (
VI->getNameInit() == ValName)
289 return Error(Loc,
"Recursion / self-assignment forbidden");
293 if (!BitList.
empty()) {
297 "' is not a bits type");
302 return Error(Loc,
"Initializer is not compatible with bit range");
307 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
308 unsigned Bit = BitList[i];
310 return Error(Loc,
"Cannot set bit #" + Twine(Bit) +
" of value '" +
316 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
318 NewBits[i] = CurVal->getBit(i);
324 std::string InitType;
326 InitType = (Twine(
"' of type bit initializer with length ") +
327 Twine(BI->getNumBits()))
331 (Twine(
"' of type '") + TI->getType()->getAsString() +
"'").str();
335 "' is incompatible with value '" +
V->getAsString() +
345 MapResolver
R(CurRec);
353 if (resolveArgumentsOfClass(R, SC, ParentClass.
TemplateArgs,
377 "Already subclass of '" + SC->
getName() +
"'!\n");
385 return AddParentClass(
Entry.Rec.get(), ParentClass);
390 for (
auto &
E :
Entry.Loop->Entries) {
391 if (AddParentClass(
E, ParentClass))
404 if (resolveArgumentsOfMultiClass(
412 return resolve(ParentMC->
Entries, Substs,
false, &CurMultiClass->Entries);
417 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion + !!
E.Dump) == 1 &&
418 "RecordsEntry has invalid number of items");
421 if (!Loops.empty()) {
422 Loops.back()->Entries.push_back(std::move(
E));
429 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
430 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
435 CurMultiClass->Entries.push_back(std::move(
E));
441 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
451 return addDefOne(std::move(
E.Rec));
459bool TGParser::resolve(
const ForeachLoop &
Loop, SubstStack &Substs,
bool Final,
460 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
463 for (
const auto &S : Substs)
464 R.set(S.first, S.second);
465 const Init *
List =
Loop.ListValue->resolveReferences(R);
475 const Init *OldLHS = TI->getLHS();
481 "' at end of containing scope");
484 const Init *MHS = TI->getMHS();
485 const Init *
RHS = TI->getRHS();
494 std::make_unique<ForeachLoop>(
Loop.Loc,
Loop.IterVar,
List));
495 return resolve(
Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
500 List->getAsString() +
"', expected a list");
505 for (
auto *Elt : *LI) {
507 Substs.emplace_back(
Loop.IterVar->getNameInit(), Elt);
508 Error = resolve(
Loop.Entries, Substs, Final, Dest);
522bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
523 SubstStack &Substs,
bool Final,
524 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
526 for (
auto &
E : Source) {
528 Error = resolve(*
E.Loop, Substs, Final, Dest);
530 }
else if (
E.Assertion) {
532 for (
const auto &S : Substs)
533 R.set(S.first, S.second);
534 const Init *Condition =
E.Assertion->Condition->resolveReferences(R);
535 const Init *Message =
E.Assertion->Message->resolveReferences(R);
538 Dest->push_back(std::make_unique<Record::AssertionInfo>(
539 E.Assertion->Loc, Condition, Message));
545 for (
const auto &S : Substs)
546 R.set(S.first, S.second);
551 std::make_unique<Record::DumpInfo>(
E.Dump->Loc, Message));
556 auto Rec = std::make_unique<Record>(*
E.Rec);
558 Rec->appendLoc(*Loc);
560 MapResolver
R(Rec.get());
561 for (
const auto &S : Substs)
562 R.set(S.first, S.second);
563 Rec->resolveReferences(R);
566 Dest->push_back(std::move(Rec));
568 Error = addDefOne(std::move(Rec));
577bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
578 const Init *NewName =
nullptr;
579 if (
const Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
580 if (!Rec->isAnonymous()) {
582 "def already exists: " + Rec->getNameInitAsString());
583 PrintNote(Prev->getLoc(),
"location of previous definition");
586 NewName = Records.getNewAnonymousName();
589 Rec->resolveReferences(NewName);
593 PrintError(Rec->getLoc(), Twine(
"record name '") +
594 Rec->getNameInit()->getAsString() +
595 "' could not be fully resolved");
600 Rec->checkRecordAssertions();
603 Rec->emitRecordDumps();
606 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
608 for (DefsetRecord *Defset : Defsets) {
609 DefInit *
I = Rec->getDefInit();
610 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
611 PrintError(Rec->getLoc(), Twine(
"adding record of incompatible type '") +
612 I->getType()->getAsString() +
620 Records.addDef(std::move(Rec));
624bool TGParser::resolveArguments(
const Record *Rec,
626 SMLoc Loc, ArgValueHandler ArgValueHandler) {
629 "Too many template arguments allowed");
633 for (
auto *Arg : ArgValues) {
634 const Init *ArgName =
nullptr;
635 const Init *ArgValue = Arg->getValue();
636 if (Arg->isPositional())
637 ArgName =
Args[Arg->getIndex()].getNameInit();
639 ArgName = Arg->getName();
642 auto It =
find_if(UnsolvedArgs, [ArgName](
const RecordVal *UnsolvedArg) {
645 if (It == UnsolvedArgs.end())
646 return Error(Loc,
"We can only specify the template argument '" +
649 ArgValueHandler(ArgName, ArgValue);
650 UnsolvedArgs.erase(It);
654 for (
const RecordVal *UnsolvedArg : UnsolvedArgs) {
657 Error(Loc,
"value not specified for template argument '" +
674 return resolveArguments(
676 [&](
const Init *Name,
const Init *
Value) {
R.set(Name,
Value); });
681bool TGParser::resolveArgumentsOfMultiClass(
686 return resolveArguments(&MC->
Rec, ArgValues, Loc,
687 [&](
const Init *Name,
const Init *
Value) {
688 Substs.emplace_back(Name, Value);
697 if (Lex.getCode() == K) {
710const Init *TGParser::ParseObjectName() {
711 switch (Lex.getCode()) {
725 CurRec = &CurMultiClass->Rec;
734 HasReferenceResolver
R(NameStr);
735 Name->resolveReferences(R);
749const Record *TGParser::ParseClassID() {
751 TokError(
"expected name for ClassID");
755 const Record *
Result = Records.getClass(Lex.getCurStrVal());
757 std::string
Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
758 if (MultiClasses[Lex.getCurStrVal()].get())
759 TokError(
Msg +
". Use 'defm' if you meant to use multiclass '" +
760 Lex.getCurStrVal() +
"'");
763 }
else if (TrackReferenceLocs) {
764 Result->appendReferenceLoc(Lex.getLocRange());
778 TokError(
"expected name for MultiClassID");
784 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
798 Result.RefRange.Start = Lex.getLoc();
801 if (MultiClass *MC = ParseMultiClassID())
804 Result.Rec = ParseClassID();
811 Result.RefRange.End = Lex.getLoc();
816 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
822 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
827 Result.RefRange.End = Lex.getLoc();
838 MultiClassReference
Result;
839 Result.RefRange.Start = Lex.getLoc();
841 Result.MC = ParseMultiClassID();
847 Result.RefRange.End = Lex.getLoc();
852 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs,
853 &CurMultiClass->Rec, &
Result.MC->Rec)) {
858 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs, &
Result.MC->Rec)) {
863 Result.RefRange.End = Lex.getLoc();
879 auto LHSLoc = Lex.getLoc();
880 auto *CurVal = ParseValue(CurRec);
885 const TypedInit *
RHS =
nullptr;
886 switch (Lex.getCode()) {
890 auto RHSLoc = Lex.getLoc();
891 CurVal = ParseValue(CurRec);
897 "expected int...int, got " + Twine(
RHS->
getType()->getAsString()));
903 auto i = -Lex.getCurIntVal();
905 TokError(
"invalid range, cannot be negative");
922 "expected int...int, got " + Twine(
LHS->
getType()->getAsString()));
943 const TypedInit *CurVal;
947 auto FlushElems = [&] {
948 if (!Elems.
empty()) {
955 auto LHSLoc = Lex.getLoc();
956 CurVal = ParseSliceElement(CurRec);
959 auto *CurValTy = CurVal->
getType();
964 "expected list<int>, got " + Twine(ListValTy->getAsString()));
974 "unhandled type " + Twine(CurValTy->getAsString()) +
" in range");
1001 const TypedInit *
Result =
nullptr;
1002 for (
auto *Slice : Slices) {
1018 const Init *CurVal = FirstItem;
1020 CurVal = ParseValue(
nullptr);
1024 return TokError(
"expected integer or bitrange");
1026 int64_t
Start =
II->getValue();
1030 return TokError(
"invalid range, cannot be negative");
1032 switch (Lex.getCode()) {
1041 const Init *I_End = ParseValue(
nullptr);
1044 TokError(
"expected integer value as end of range");
1048 End = II_End->getValue();
1052 End = -Lex.getCurIntVal();
1058 return TokError(
"invalid range, cannot be negative");
1076 if (ParseRangePiece(Result)) {
1082 if (ParseRangePiece(Result)) {
1092 SMLoc StartLoc = Lex.getLoc();
1097 ParseRangeList(Ranges);
1102 TokError(
"expected '}' at end of bit list");
1103 return Error(StartLoc,
"to match this '{'");
1119const RecTy *TGParser::ParseType() {
1120 switch (Lex.getCode()) {
1122 TokError(
"Unknown token when expecting a type");
1138 auto I = TypeAliases.find(Lex.getCurStrVal());
1139 if (
I != TypeAliases.end()) {
1143 if (
const Record *R = ParseClassID())
1150 TokError(
"expected '<' after bits type");
1154 TokError(
"expected integer in bits<n> type");
1159 TokError(
"expected '>' at end of bits<n> type");
1167 TokError(
"expected '<' after list type");
1171 const RecTy *SubType = ParseType();
1176 TokError(
"expected '>' at end of list<ty> type");
1187 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass, Name, NameLoc,
1188 TrackReferenceLocs))
1191 if (
Mode == ParseNameMode)
1194 if (
const Init *
I = Records.getGlobal(
Name->getValue())) {
1196 if (TrackReferenceLocs) {
1198 Def->getDef()->appendReferenceLoc(NameLoc);
1205 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1209 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1217const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1218 switch (Lex.getCode()) {
1237 const RecTy *
Type =
nullptr;
1239 switch (Lex.getCode()) {
1246 Type = ParseOperatorType();
1249 TokError(
"did not get type for unary operator");
1308 Type = ParseOperatorType();
1311 TokError(
"did not get type for unary operator");
1316 TokError(
"type for !getdagop must be a record type");
1336 TokError(
"expected '(' after unary operator");
1340 const Init *
LHS = ParseValue(CurRec);
1349 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1351 "expected string, list, or dag type argument in unary operator");
1357 "expected string, list, or dag type argument in unary operator");
1367 if (!LHSl && !LHSt) {
1368 TokError(
"expected list type argument in unary operator");
1373 TokError(
"expected list type argument in unary operator");
1378 if (LHSl && LHSl->empty()) {
1379 TokError(
"empty list argument in unary operator");
1382 bool UseElementType =
1385 const Init *Item = LHSl->getElement(0);
1388 TokError(
"untyped list element in unary operator");
1391 Type = UseElementType ? Itemt->getType()
1394 assert(LHSt &&
"expected list type argument in unary operator");
1396 Type = UseElementType ? LType->getElementType() : LType;
1414 TokError(
"expected ')' in unary operator");
1424 const RecTy *
Type = ParseOperatorType();
1429 TokError(
"expected '(' after type of !isa");
1433 const Init *
LHS = ParseValue(CurRec);
1449 const RecTy *
Type = ParseOperatorType();
1454 TokError(
"expected '(' after type of !exists");
1458 SMLoc ExprLoc = Lex.getLoc();
1459 const Init *Expr = ParseValue(CurRec);
1465 Error(ExprLoc,
"expected string type argument in !exists operator");
1472 "expected string type argument in !exists operator, please "
1473 "use !isa instead");
1479 Error(ExprLoc,
"expected string type argument in !exists operator");
1484 TokError(
"expected ')' in !exists");
1495 const RecTy *
Type = ParseOperatorType();
1500 TokError(
"expected '(' after type of !instances");
1507 SMLoc RegexLoc = Lex.getLoc();
1508 Regex = ParseValue(CurRec);
1512 Error(RegexLoc,
"expected string type argument in !instances operator");
1518 Error(RegexLoc,
"expected string type argument in !instances operator");
1527 TokError(
"expected ')' in !instances");
1562 SMLoc OpLoc = Lex.getLoc();
1652 const RecTy *
Type =
nullptr;
1653 const RecTy *ArgType =
nullptr;
1667 Type = ParseOperatorType();
1669 TokError(
"did not get type for !getdagarg operator");
1724 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1726 "', got '" +
Type->getAsString() +
"'");
1731 TokError(
"expected '(' after binary operator");
1740 SMLoc InitLoc = Lex.getLoc();
1741 InitList.
push_back(ParseValue(CurRec, ArgType));
1742 if (!InitList.
back())
1746 if (!InitListBack) {
1747 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1748 InitList.
back()->getAsString() +
"'"));
1751 const RecTy *ListType = InitListBack->getType();
1760 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1766 if (ItemType && InitList.
size() == 1) {
1769 Twine(
"expected output type to be a list, got type '") +
1774 Error(OpLoc, Twine(
"expected first arg type to be '") +
1776 "', got value of type '" +
1785 Error(InitLoc, Twine(
"expected second parameter to be an int, got "
1786 "value of type '") +
1794 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1804 Error(InitLoc, Twine(
"expected bit, bits, int, string, or record; "
1805 "got value of type '") +
1818 Error(InitLoc, Twine(
"expected bit, bits, int, or string; "
1819 "got value of type '") +
1825 switch (InitList.
size()) {
1831 Twine(
"expected list of string, int, bits, or bit; "
1832 "got value of type '") +
1839 Error(InitLoc, Twine(
"expected second argument to be a string, "
1840 "got value of type '") +
1857 Error(InitLoc, Twine(
"expected value of type '") +
1900 TokError(
"expected ')' in operator");
1920 while (InitList.
size() > 2) {
1927 if (InitList.
size() == 2)
1931 Error(OpLoc,
"expected two operands to operator");
1938 return ParseOperationListComprehension(CurRec, ItemType);
1942 SMLoc OpLoc = Lex.getLoc();
1946 TokError(
"expected '(' after !range operator");
1951 bool FirstArgIsList =
false;
1953 if (
Args.size() >= 3) {
1954 TokError(
"expected at most three values of integer");
1958 SMLoc InitLoc = Lex.getLoc();
1959 Args.push_back(ParseValue(CurRec));
1965 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1966 Args.back()->getAsString() +
"'"));
1970 const RecTy *ArgBackType = ArgBack->getType();
1971 if (!FirstArgIsList ||
Args.size() == 1) {
1973 FirstArgIsList =
true;
1977 if (
Args.size() != 1)
1978 Error(InitLoc, Twine(
"expected value of type 'int', got '" +
1981 Error(InitLoc, Twine(
"expected list or int, got value of type '") +
1988 Error(InitLoc, Twine(
"expected one list, got extra value of type '") +
1997 TokError(
"expected ')' in operator");
2001 const Init *
LHS, *MHS, *
RHS;
2002 auto ArgCount =
Args.size();
2005 const auto *Arg0Ty = Arg0->getType();
2006 if (ArgCount == 1) {
2026 if (ArgCount == 3) {
2047 const RecTy *
Type =
nullptr;
2077 TokError(
"expected '(' after ternary operator");
2081 const Init *
LHS = ParseValue(CurRec);
2086 TokError(
"expected ',' in ternary operator");
2090 SMLoc MHSLoc = Lex.getLoc();
2091 const Init *MHS = ParseValue(CurRec, ItemType);
2096 TokError(
"expected ',' in ternary operator");
2100 SMLoc RHSLoc = Lex.getLoc();
2101 const Init *
RHS = ParseValue(CurRec, ItemType);
2106 TokError(
"expected ')' in binary operator");
2116 Error(MHSLoc,
"could not determine type of the child list in !dag");
2120 Error(MHSLoc, Twine(
"expected list of children, got type '") +
2121 MHSt->getType()->getAsString() +
"'");
2127 Error(RHSLoc,
"could not determine type of the name list in !dag");
2131 Error(RHSLoc, Twine(
"expected list<string>, got type '") +
2132 RHSt->getType()->getAsString() +
"'");
2136 if (!MHSt && !RHSt) {
2138 "cannot have both unset children and unset names in !dag");
2144 const RecTy *MHSTy =
nullptr;
2145 const RecTy *RHSTy =
nullptr;
2148 MHSTy = MHSt->getType();
2155 RHSTy = RHSt->getType();
2167 if (!MHSTy || !RHSTy) {
2168 TokError(
"could not get type for !if");
2183 TokError(
"could not get type for !subst");
2186 Type = RHSt->getType();
2192 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2193 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2203 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2204 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2212 Error(RHSLoc, Twine(
"expected string or unset name, got type '") +
2213 RHSt->getType()->getAsString() +
"'");
2223 return ParseOperationSubstr(CurRec, ItemType);
2226 return ParseOperationFind(CurRec, ItemType);
2229 return ParseOperationCond(CurRec, ItemType);
2232 return ParseOperationSwitch(CurRec, ItemType);
2238 TokError(
"expected '(' after !foldl");
2242 const Init *StartUntyped = ParseValue(CurRec);
2248 TokError(Twine(
"could not get type of !foldl start: '") +
2254 TokError(
"expected ',' in !foldl");
2258 const Init *ListUntyped = ParseValue(CurRec);
2264 TokError(Twine(
"could not get type of !foldl list: '") +
2271 TokError(Twine(
"!foldl list must be a list, but is of type '") +
2272 List->getType()->getAsString());
2277 TokError(
"expected ',' in !foldl");
2282 TokError(
"third argument of !foldl must be an identifier");
2288 TokError(Twine(
"left !foldl variable '") +
A->getAsString() +
2289 "' already defined");
2294 TokError(
"expected ',' in !foldl");
2299 TokError(
"fourth argument of !foldl must be an identifier");
2305 TokError(Twine(
"right !foldl variable '") +
B->getAsString() +
2306 "' already defined");
2311 TokError(
"expected ',' in !foldl");
2318 std::unique_ptr<Record> ParseRecTmp;
2319 Record *ParseRec = CurRec;
2323 ParseRec = ParseRecTmp.get();
2326 TGVarScope *FoldScope =
PushScope(ParseRec);
2330 const Init *ExprUntyped = ParseValue(ParseRec);
2331 ParseRec->removeValue(
A);
2332 ParseRec->removeValue(
B);
2339 TokError(
"could not get type of !foldl expression");
2343 if (Expr->getType() !=
Start->getType()) {
2344 TokError(Twine(
"!foldl expression must be of same type as start (") +
2345 Start->getType()->getAsString() +
"), but is of type " +
2351 TokError(
"expected ')' in fold operator");
2366const RecTy *TGParser::ParseOperatorType() {
2367 const RecTy *
Type =
nullptr;
2370 TokError(
"expected type name for operator");
2375 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2380 TokError(
"expected type name for operator");
2385 TokError(
"expected type name for operator");
2395const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2396 const RecTy *ItemType) {
2403 TokError(
"expected '(' after !substr operator");
2407 const Init *
LHS = ParseValue(CurRec);
2412 TokError(
"expected ',' in !substr operator");
2416 SMLoc MHSLoc = Lex.getLoc();
2417 const Init *MHS = ParseValue(CurRec);
2421 SMLoc RHSLoc = Lex.getLoc();
2424 RHSLoc = Lex.getLoc();
2425 RHS = ParseValue(CurRec);
2433 TokError(
"expected ')' in !substr operator");
2437 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2438 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2439 "', got '" +
Type->getAsString() +
"'");
2444 TokError(
"could not determine type of the string in !substr");
2448 TokError(Twine(
"expected string, got type '") +
2449 LHSt->getType()->getAsString() +
"'");
2455 TokError(
"could not determine type of the start position in !substr");
2459 Error(MHSLoc, Twine(
"expected int, got type '") +
2460 MHSt->getType()->getAsString() +
"'");
2467 TokError(
"could not determine type of the length in !substr");
2471 TokError(Twine(
"expected int, got type '") +
2472 RHSt->getType()->getAsString() +
"'");
2483const Init *TGParser::ParseOperationFind(
Record *CurRec,
2484 const RecTy *ItemType) {
2491 TokError(
"expected '(' after !find operator");
2495 const Init *
LHS = ParseValue(CurRec);
2500 TokError(
"expected ',' in !find operator");
2504 SMLoc MHSLoc = Lex.getLoc();
2505 const Init *MHS = ParseValue(CurRec);
2509 SMLoc RHSLoc = Lex.getLoc();
2512 RHSLoc = Lex.getLoc();
2513 RHS = ParseValue(CurRec);
2521 TokError(
"expected ')' in !find operator");
2525 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2526 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2527 "', got '" +
Type->getAsString() +
"'");
2532 TokError(
"could not determine type of the source string in !find");
2536 TokError(Twine(
"expected string, got type '") +
2537 LHSt->getType()->getAsString() +
"'");
2543 TokError(
"could not determine type of the target string in !find");
2547 Error(MHSLoc, Twine(
"expected string, got type '") +
2548 MHSt->getType()->getAsString() +
"'");
2555 TokError(
"could not determine type of the start position in !find");
2559 TokError(Twine(
"expected int, got type '") +
2560 RHSt->getType()->getAsString() +
"'");
2573const Init *TGParser::ParseOperationListComprehension(
Record *CurRec,
2574 const RecTy *ItemType) {
2575 SMLoc OpLoc = Lex.getLoc();
2579 TokError(
"expected '(' after !foreach/!filter");
2584 TokError(
"first argument of !foreach/!filter must be an identifier");
2592 TokError(Twine(
"iteration variable '") +
LHS->getAsString() +
2593 "' is already defined");
2598 TokError(
"expected ',' in !foreach/!filter");
2602 const Init *MHS = ParseValue(CurRec);
2607 TokError(
"expected ',' in !foreach/!filter");
2613 TokError(
"could not get type of !foreach/!filter list or dag");
2617 const RecTy *InEltType =
nullptr;
2618 const RecTy *ExprEltType =
nullptr;
2622 InEltType = InListTy->getElementType();
2627 ExprEltType = OutListTy->getElementType();
2633 ExprEltType =
nullptr;
2639 Error(OpLoc,
"expected value of type '" +
2641 "', but got list type");
2648 TokError(
"!filter must have a list argument");
2651 TokError(
"!sort must have a list argument");
2658 InEltType = InDagTy;
2661 "', but got dag type");
2668 TokError(
"!foreach must have a list or dag argument");
2671 TokError(
"!filter must have a list argument");
2674 TokError(
"!sort must have a list argument");
2683 std::unique_ptr<Record> ParseRecTmp;
2684 Record *ParseRec = CurRec;
2688 ParseRec = ParseRecTmp.get();
2690 TGVarScope *TempScope =
PushScope(ParseRec);
2692 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2693 ParseRec->removeValue(
LHS);
2699 TokError(
"expected ')' in !foreach/!filter");
2703 const RecTy *OutType;
2709 OutType = InEltType;
2713 TokError(
"could not get type of !foreach result expression");
2737std::optional<const RecTy *>
2739 const RecTy *
Type =
nullptr;
2740 for (
const Init *
V : Inits) {
2752 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
"' and '" +
2754 return std::nullopt;
2764const Init *TGParser::ParseOperationCond(
Record *CurRec,
2765 const RecTy *ItemType) {
2769 TokError(
"expected '(' after !cond operator");
2780 const Init *
V = ParseValue(CurRec);
2786 TokError(
"expected ':' following a condition in !cond operator");
2790 V = ParseValue(CurRec, ItemType);
2799 TokError(
"expected ',' or ')' following a value in !cond operator");
2804 if (Cases.
size() < 1) {
2806 "there should be at least 1 'condition : value' in the !cond operator");
2811 std::optional<const RecTy *> TypeOpt = resolveInitTypes(Vals,
"for !cond");
2814 const RecTy *
Type = *TypeOpt;
2816 TokError(
"could not determine type for !cond from its arguments");
2823const Init *TGParser::ParseOperationSwitch(
Record *CurRec,
2824 const RecTy *ItemType) {
2828 TokError(
"expected '(' after !switch operator");
2833 const Init *
Key = ParseValue(CurRec);
2840 TokError(
"expected ',' after key in !switch operator");
2848 const Init *
V = ParseValue(CurRec);
2857 if (
const Init *Coerced =
V->convertInitializerTo(ItemType))
2867 TokError(
"expected ':' after case key, or ')' to close !switch operator");
2872 V = ParseValue(CurRec, ItemType);
2878 TokError(
"expected ',' after case value in !switch operator");
2883 "inconsistent keys and values for !switch");
2885 if (KeyAndCases.
size() < 2) {
2887 "there should be at least 1 'case: value' in the !switch operator");
2892 std::optional<const RecTy *> ValTypeOpt =
2893 resolveInitTypes(Vals,
"for !switch values");
2896 const RecTy *
ValType = *ValTypeOpt;
2898 TokError(
"could not determine type for !switch from its arguments");
2905 if (!resolveInitTypes(KeyAndCases,
"between !switch key and case keys"))
2911 size_t ValsSize = Vals.
size();
2918 assert(Conds.
size() == ValsSize &&
"inconsistent !switch to !cond reduction");
2949const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2951 const Init *
R =
nullptr;
2956 return ParseOperation(CurRec, ItemType);
2960 TokError(
"Unknown or reserved token when parsing a value");
2976 auto BinaryVal = Lex.getCurBinaryIntVal();
2978 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2985 std::string Val = Lex.getCurStrVal();
2990 Val += Lex.getCurStrVal();
3006 SMRange NameLoc = Lex.getLocRange();
3012 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
3020 "Expected a class name, got '" +
Name->getValue() +
"'");
3027 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
3030 if (CheckTemplateArgValues(Args, ArgLocs, Class))
3033 if (resolveArguments(Class, Args, NameLoc.
Start))
3036 if (TrackReferenceLocs)
3037 Class->appendReferenceLoc(NameLoc);
3041 SMLoc BraceLoc = Lex.getLoc();
3046 ParseValueList(Vals, CurRec);
3051 TokError(
"expected '}' at end of bit list value");
3066 for (
const Init *Bit :
reverse(BI->getBits()))
3073 for (
unsigned Bit :
reverse(
seq(BitsRec->getNumBits())))
3082 Error(BraceLoc,
"Element #" + Twine(
I) +
" (" + Val->getAsString() +
3083 ") is not convertable to a bit");
3088 std::reverse(NewBits.
begin(), NewBits.
end());
3095 const RecTy *DeducedEltTy =
nullptr;
3096 const ListRecTy *GivenListTy =
nullptr;
3101 TokError(Twine(
"Encountered a list when expecting a ") +
3105 GivenListTy = ListType;
3109 ParseValueList(Vals, CurRec,
3115 TokError(
"expected ']' at end of list value");
3119 const RecTy *GivenEltTy =
nullptr;
3122 GivenEltTy = ParseType();
3129 TokError(
"expected '>' at end of list element type");
3135 const RecTy *EltTy =
nullptr;
3136 for (
const Init *
V : Vals) {
3142 TokError(
"Incompatible types in list elements");
3146 EltTy = TArg->getType();
3155 TokError(
"Incompatible types in list elements");
3172 TokError(Twine(
"Element type mismatch for list: element type '") +
3178 DeducedEltTy = EltTy;
3189 TokError(
"expected identifier or list of value types in dag init");
3193 const Init *
Operator = ParseValue(CurRec);
3198 const StringInit *OperatorName =
nullptr;
3201 TokError(
"expected variable name in dag operator");
3210 ParseDagArgList(DagArgs, CurRec);
3211 if (DagArgs.
empty())
3216 TokError(
"expected ')' in dag init");
3234const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3236 SMLoc LHSLoc = Lex.getLoc();
3237 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3243 switch (Lex.getCode()) {
3247 if (
Mode == ParseNameMode)
3251 SMLoc CurlyLoc = Lex.getLoc();
3253 SmallVector<unsigned, 16>
Ranges;
3254 ParseRangeList(Ranges);
3262 Error(CurlyLoc,
"Invalid bit range for value");
3268 TokError(
"expected '}' at end of bit range list");
3276 Error(LHSLoc,
"Invalid value, list expected");
3283 "' is invalid, list expected");
3288 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3297 LHSTy->getElementType())
3305 TokError(
"expected ']' at end of list slice");
3312 TokError(
"expected field identifier after '.'");
3315 SMRange FieldNameLoc = Lex.getLocRange();
3316 const StringInit *FieldName =
3318 if (!
Result->getFieldType(FieldName)) {
3319 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3320 Result->getAsString() +
"'");
3325 if (TrackReferenceLocs) {
3327 DI->getDef()->appendValueReferenceLoc(FieldName, FieldNameLoc);
3330 for (
const Record *R : RecTy->getClasses())
3331 if (
R->getValue(FieldName))
3332 R->appendValueReferenceLoc(FieldName, FieldNameLoc);
3343 SMLoc PasteLoc = Lex.getLoc();
3346 Error(PasteLoc,
"LHS of paste is not typed!");
3354 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3356 switch (Lex.getCode()) {
3363 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3380 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3386 const TypedInit *
RHS =
nullptr;
3389 switch (Lex.getCode()) {
3402 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3407 Error(PasteLoc,
"RHS of paste is not typed!");
3417 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3438void TGParser::ParseDagArgList(
3451 const Init *Val = ParseValue(CurRec);
3458 const StringInit *
VarName =
nullptr;
3461 TokError(
"expected variable name in dag literal");
3469 Result.emplace_back(Val, VarName);
3484 Result.push_back(ParseValue(CurRec, ItemType));
3494 Result.push_back(ParseValue(CurRec, ItemType));
3510bool TGParser::ParseTemplateArgValueList(
3513 assert(
Result.empty() &&
"Result vector is not empty");
3519 bool HasNamedArg =
false;
3520 unsigned ArgIndex = 0;
3522 if (ArgIndex >= TArgs.
size()) {
3523 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3531 ParseValue(CurRec, HasNamedArg ?
nullptr : TArgs[ArgIndex].
getType());
3538 return Error(ValueLoc,
3539 "The name of named argument should be a valid identifier");
3543 const RecordVal *NamedArg = ArgsRec->
getTemplateArg(QualifiedName);
3545 return Error(ValueLoc,
3546 "Argument " +
Name->getAsString() +
" doesn't exist");
3549 ValueLoc = Lex.getLoc();
3553 return Error(ValueLoc,
3554 "The value of named argument should be initialized, "
3556 Value->getAsString() +
"'");
3563 return Error(ValueLoc,
3564 "Positional argument should be put before named argument");
3572 return TokError(
"Expected comma before next argument");
3587bool TGParser::ParseDeclaration(
Record *CurRec,
bool ParsingTemplateArgs) {
3588 SMLoc DeclLoc = Lex.getLoc();
3593 const RecTy *
Type = ParseType();
3598 return TokError(
"Expected identifier in declaration");
3600 std::string Str = Lex.getCurStrVal();
3602 return TokError(
"'" + Str +
"' is a reserved variable name");
3604 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str))
3605 return TokError(
"local variable of this name already exists");
3607 SMLoc IdLoc = Lex.getLoc();
3611 Record *Rec = CurRec ? CurRec : &CurMultiClass->Rec;
3616 if (!ParsingTemplateArgs &&
3617 AddValue(Rec, IdLoc,
3618 RecordVal(DeclName, IdLoc,
Type,
3624 const Init *Val =
nullptr;
3626 ValLoc = Lex.getLoc();
3627 Val = ParseValue(CurRec,
Type);
3630 if (ParsingTemplateArgs) {
3634 return Error(DeclLoc,
"template argument with the same name has already "
3641 SetValue(Rec, ValLoc, DeclName, {}, Val,
false);
3654TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3656 TokError(
"Expected identifier in foreach declaration");
3665 TokError(
"Expected '=' in foreach declaration");
3669 const RecTy *IterType =
nullptr;
3670 SmallVector<unsigned, 16>
Ranges;
3672 switch (Lex.getCode()) {
3675 ParseRangeList(Ranges);
3677 TokError(
"expected '}' at end of bit range list");
3684 SMLoc ValueLoc = Lex.getLoc();
3685 const Init *
I = ParseValue(
nullptr);
3691 ForeachListValue =
I;
3697 if (ParseRangePiece(Ranges, TI))
3702 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3703 if (CurMultiClass) {
3704 PrintNote({},
"references to multiclass template arguments cannot be "
3705 "resolved at this time");
3712 assert(!IterType &&
"Type already initialized?");
3714 std::vector<Init *>
Values;
3715 for (
unsigned R : Ranges)
3733bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3740 if (ParseDeclaration(CurRec,
true))
3745 return TokError(
"expected '>' at end of template argument list");
3762 SMLoc Loc = Lex.getLoc();
3764 std::string CurStr = Lex.getCurStrVal();
3766 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3776 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3794bool TGParser::ParseBodyItem(
Record *CurRec) {
3796 return ParseAssert(CurRec);
3799 return ParseDefvar(CurRec);
3802 return ParseDump(CurRec);
3805 if (ParseDeclaration(CurRec,
false))
3809 return TokError(
"expected ';' after declaration");
3817 return TokError(
"expected field identifier after let");
3819 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3822 SmallVector<unsigned, 16> BitList;
3823 if (ParseOptionalRangeList(BitList))
3825 std::reverse(BitList.
begin(), BitList.
end());
3828 return TokError(
"expected '=' in let expression");
3832 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3841 const Init *Val = ParseValue(CurRec,
Type);
3846 return TokError(
"expected ';' after let expression");
3848 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3859bool TGParser::ParseBody(
Record *CurRec) {
3865 return TokError(
"Expected '{' to start body or ';' for declaration only");
3868 if (ParseBodyItem(CurRec))
3875 SMLoc SemiLoc = Lex.getLoc();
3877 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3878 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3886bool TGParser::ApplyLetStack(
Record *CurRec) {
3887 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3888 for (LetRecord &LR : LetInfo)
3889 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3898 return ApplyLetStack(
Entry.Rec.get());
3901 if (
Entry.Assertion)
3908 for (
auto &
E :
Entry.Loop->Entries) {
3909 if (ApplyLetStack(
E))
3925bool TGParser::ParseObjectBody(
Record *CurRec) {
3927 TGVarScope *ObjectScope =
PushScope(CurRec);
3932 ClassReference ParentClass = ParseClassReference(CurRec,
false);
3935 if (!ParentClass.
Rec)
3939 if (AddParentClass(CurRec, ParentClass))
3944 ParentClass = ParseClassReference(CurRec,
false);
3948 if (ApplyLetStack(CurRec))
3951 bool Result = ParseBody(CurRec);
3961bool TGParser::ParseDef() {
3962 SMLoc DefLoc = Lex.getLoc();
3969 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
3972 std::unique_ptr<Record> CurRec;
3973 const Init *
Name = ParseObjectName();
3978 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
3981 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
3984 if (ParseObjectBody(CurRec.get()))
3987 return addEntry(std::move(CurRec));
3994bool TGParser::ParseDefset() {
3999 Defset.Loc = Lex.getLoc();
4000 const RecTy *
Type = ParseType();
4008 return TokError(
"expected identifier");
4009 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4010 if (Records.getGlobal(DeclName->
getValue()))
4011 return TokError(
"def or global variable of this name already exists");
4017 SMLoc BraceLoc = Lex.getLoc();
4020 Defsets.push_back(&Defset);
4021 bool Err = ParseObjectList();
4027 TokError(
"expected '}' at end of defset");
4028 return Error(BraceLoc,
"to match this '{'");
4031 Records.addExtraGlobal(DeclName->
getValue(),
4040bool TGParser::ParseDeftype() {
4045 return TokError(
"expected identifier");
4047 const std::string
TypeName = Lex.getCurStrVal();
4048 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
4049 return TokError(
"type of this name '" + TypeName +
"' already exists");
4055 SMLoc Loc = Lex.getLoc();
4056 const RecTy *
Type = ParseType();
4061 return Error(Loc,
"cannot define type alias for class type '" +
4062 Type->getAsString() +
"'");
4076bool TGParser::ParseDefvar(
Record *CurRec) {
4081 return TokError(
"expected identifier");
4082 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
4083 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
4084 return TokError(
"local variable of this name already exists");
4088 return TokError(
"field of this name already exists");
4092 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
4093 return TokError(
"def or global variable of this name already exists");
4099 const Init *
Value = ParseValue(CurRec);
4106 if (!CurScope->isOutermost())
4120bool TGParser::ParseForeach() {
4121 SMLoc Loc = Lex.getLoc();
4127 const Init *ListValue =
nullptr;
4128 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4130 return TokError(
"expected declaration in for");
4136 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4138 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4139 Loops.push_back(std::move(TheLoop));
4146 SMLoc BraceLoc = Lex.getLoc();
4151 if (ParseObjectList())
4155 TokError(
"expected '}' at end of foreach command");
4156 return Error(BraceLoc,
"to match this '{'");
4163 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
4166 return addEntry(std::move(
Loop));
4174bool TGParser::ParseIf() {
4175 SMLoc Loc = Lex.getLoc();
4181 const Init *Condition = ParseValue(
nullptr);
4195 const ListInit *SingletonList =
4201 const Init *ThenClauseList =
4205 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4207 if (ParseIfBody(
"then"))
4210 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
4213 if (addEntry(std::move(
Loop)))
4223 const Init *ElseClauseList =
4228 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4230 if (ParseIfBody(
"else"))
4233 Loop = std::move(Loops.back());
4236 if (addEntry(std::move(
Loop)))
4248bool TGParser::ParseIfBody(
StringRef Kind) {
4257 SMLoc BraceLoc = Lex.getLoc();
4262 if (ParseObjectList())
4266 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4267 return Error(BraceLoc,
"to match this '{'");
4278bool TGParser::ParseAssert(
Record *CurRec) {
4282 SMLoc ConditionLoc = Lex.getLoc();
4283 const Init *Condition = ParseValue(CurRec);
4288 TokError(
"expected ',' in assert statement");
4292 const Init *Message = ParseValue(CurRec);
4300 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4302 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4311bool TGParser::ParseClass() {
4316 return TokError(
"expected class name after 'class' keyword");
4318 const std::string &
Name = Lex.getCurStrVal();
4319 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4326 "' already defined");
4331 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4333 CurRec = NewRec.get();
4334 Records.addClass(std::move(NewRec));
4337 if (TypeAliases.count(Name))
4338 return TokError(
"there is already a defined type alias '" + Name +
"'");
4343 TGVarScope *ClassScope =
PushScope(CurRec);
4346 if (ParseTemplateArgList(CurRec))
4349 if (ParseObjectBody(CurRec))
4352 if (!NoWarnOnUnusedTemplateArgs)
4368 TokError(
"expected identifier in let definition");
4373 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4377 SmallVector<unsigned, 16>
Bits;
4378 if (ParseOptionalRangeList(Bits)) {
4382 std::reverse(
Bits.begin(),
Bits.end());
4385 TokError(
"expected '=' in let expression");
4390 const Init *Val = ParseValue(
nullptr);
4397 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4407bool TGParser::ParseTopLevelLet() {
4413 ParseLetList(LetInfo);
4414 if (LetInfo.
empty())
4416 LetStack.push_back(std::move(LetInfo));
4419 return TokError(
"expected 'in' at end of top-level 'let'");
4427 SMLoc BraceLoc = Lex.getLoc();
4435 if (ParseObjectList())
4439 TokError(
"expected '}' at end of top level let command");
4440 return Error(BraceLoc,
"to match this '{'");
4447 LetStack.pop_back();
4464bool TGParser::ParseMultiClass() {
4469 return TokError(
"expected identifier after multiclass for name");
4470 std::string
Name = Lex.getCurStrVal();
4472 auto Result = MultiClasses.try_emplace(
4473 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4476 return TokError(
"multiclass '" + Name +
"' already defined");
4478 CurMultiClass =
Result.first->second.get();
4482 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4486 if (ParseTemplateArgList(
nullptr))
4489 bool inherits =
false;
4496 MultiClassReference ParentMultiClass = ParseMultiClassReference();
4499 if (!ParentMultiClass.
MC)
4503 if (AddParentMultiClass(ParentMultiClass))
4508 ParentMultiClass = ParseMultiClassReference();
4514 return TokError(
"expected '{' in multiclass definition");
4516 return TokError(
"expected ';' in multiclass definition");
4519 return TokError(
"multiclass must contain at least one def");
4522 switch (Lex.getCode()) {
4524 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4525 "'foreach', 'if', or 'let' in multiclass body");
4543 SMLoc SemiLoc = Lex.getLoc();
4545 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4546 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4550 if (!NoWarnOnUnusedTemplateArgs)
4551 CurMultiClass->Rec.checkUnusedTemplateArgs();
4554 CurMultiClass =
nullptr;
4562bool TGParser::ParseDefm() {
4566 const Init *DefmName = ParseObjectName();
4570 DefmName = Records.getNewAnonymousName();
4579 return TokError(
"expected ':' after defm identifier");
4582 std::vector<RecordsEntry> NewEntries;
4585 bool InheritFromClass =
false;
4590 SMLoc ParentClassLoc = Lex.getLoc();
4591 ClassReference
Ref = ParseClassReference(
nullptr,
true);
4601 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4602 assert(MC &&
"Didn't lookup multiclass correctly?");
4605 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4609 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4610 &NewEntries, &ParentClassLoc))
4617 return TokError(
"expected identifier");
4619 ParentClassLoc = Lex.getLoc();
4623 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4625 if (InheritFromClass)
4628 Ref = ParseClassReference(
nullptr,
true);
4631 if (InheritFromClass) {
4634 ClassReference ParentClass = ParseClassReference(
nullptr,
false);
4637 if (!ParentClass.
Rec)
4642 for (
auto &
E : NewEntries) {
4644 if (AddParentClass(
E, ParentClass))
4650 ParentClass = ParseClassReference(
nullptr,
false);
4654 for (
auto &
E : NewEntries) {
4655 if (ApplyLetStack(
E))
4658 addEntry(std::move(
E));
4662 return TokError(
"expected ';' at end of defm");
4679bool TGParser::ParseObject() {
4680 switch (Lex.getCode()) {
4683 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4685 return ParseAssert();
4691 return ParseDeftype();
4693 return ParseDefvar();
4697 return ParseForeach();
4701 return ParseTopLevelLet();
4704 return TokError(
"defset is not allowed inside multiclass");
4705 return ParseDefset();
4708 return TokError(
"class is not allowed inside multiclass");
4710 return TokError(
"class is not allowed inside foreach loop");
4711 return ParseClass();
4714 return TokError(
"multiclass is not allowed inside foreach loop");
4715 return ParseMultiClass();
4721bool TGParser::ParseObjectList() {
4732 if (ParseObjectList())
4743 return TokError(
"Unexpected token at top level");
4750bool TGParser::CheckTemplateArgValues(
4754 "expected as many values as locations");
4758 bool HasError =
false;
4761 ? &TArgs[
Value->getIndex()]
4766 auto *CastValue = ArgValue->
getCastTo(ArgType);
4770 "result of template arg value cast has wrong type");
4774 Loc,
"Value specified for template argument '" +
4776 ArgValue->getType()->
getAsString() +
"; expected type " +
4785#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4794 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4795 <<
ListValue->getAsString() <<
" in {\n";
4804 errs() <<
"Record:\n";
4807 errs() <<
"Defs:\n";
4813bool TGParser::ParseDump(
Record *CurRec) {
4819 const Init *Message = ParseValue(CurRec);
4839 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")))
Provides some synthesis utilities to produce sequences of values.
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.
bool setValue(const Init *V)
Set the value of the field from an Init.
SMLoc getLoc() const
Get the source location of the point where the field was defined.
const Init * getValue() const
Get the value of the field as an Init.
StringRef getName() const
Get the name of the field as a StringRef.
const Init * getNameInit() const
Get the name of the field as an Init.
const RecTy * getType() const
Get the type of the field value as a RecTy.
const RecordRecTy * getType() const
void addDump(SMLoc Loc, const Init *Message)
void checkUnusedTemplateArgs()
bool addTemplateArg(const RecordVal &Arg)
Add a template argument.
std::string getNameInitAsString() const
void appendValueReferenceLocs(const Record &From, const Init *Name) const
Add the references to the value named Name in From.
RecordKeeper & getRecords() const
const RecordVal * getValue(const Init *Name) const
Return the field with the given name, or null if there is none.
const RecordVal * getTemplateArg(const Init *Name) const
Return the template argument with the given name, or null if there is none.
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
ArrayRef< RecordVal > getTemplateArgs() const
Return the template arguments of this class or multiclass.
void appendDumps(const Record *Rec)
bool isSubClassOf(const Record *R) const
ArrayRef< RecordVal > getValues() const
Return the fields of this record.
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...
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)
LLVM_NO_SANITIZE_CFI_UNRELATED_CAST iterator begin()
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
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
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)
auto reverse(ContainerTy &&C)
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.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
@ End
Run the machine verifier once, at the end of the pipeline.
Implement std::hash so that hash_code can be used in STL containers.
SmallVector< const ArgumentInit *, 4 > TemplateArgs
ForeachLoop - Record the iteration state associated with a for loop.
std::vector< RecordsEntry > Entries
Parsed let mode keyword and field name (e.g.
SmallVector< const ArgumentInit *, 4 > TemplateArgs
MultiClassReference()=default
std::vector< RecordsEntry > Entries
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
std::unique_ptr< ForeachLoop > Loop
RecordsEntry(std::unique_ptr< Record > Rec)
std::unique_ptr< Record::AssertionInfo > Assertion
std::unique_ptr< Record::DumpInfo > Dump
std::unique_ptr< Record > Rec