18#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 if (
const Init *V = RV.getValue()) {
109 RV.getNameInitAsString() +
"' in '" +
110 R.getNameInitAsString() +
111 "' could not be fully resolved: " +
112 RV.getValue()->getAsString());
128 NewName = BinOp->Fold(&CurRec);
148 bool TrackReferenceLocs)
const {
150 auto It = Vars.find(Name->getValue());
151 if (It != Vars.end())
154 auto FindValueInArgs = [&](
Record *Rec,
161 assert(RV &&
"Template arg doesn't exist??");
163 if (TrackReferenceLocs)
167 return Name->getValue() ==
"NAME"
181 if (TrackReferenceLocs)
182 RV->addReferenceLoc(NameLoc);
187 if (CurRec->isClass())
188 if (
auto *V = FindValueInArgs(CurRec, Name))
195 if (CurLoop->IterVar) {
197 if (IterVar && IterVar->getNameInit() == Name)
205 if (
auto *V = FindValueInArgs(&CurMultiClass->Rec, Name))
213 return Parent->getVar(Records, ParsingMultiClass, Name, NameLoc,
221 CurRec = &CurMultiClass->
Rec;
226 return Error(
Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
228 "' is incompatible with " +
229 "previous definition of type '" +
230 ERV->getType()->getAsString() +
"'");
241 bool AllowSelfAssignment,
bool OverrideDefLoc) {
246 CurRec = &CurMultiClass->Rec;
248 RecordVal *RV = CurRec->
getValue(ValName);
257 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
258 return Error(Loc,
"Recursion / self-assignment forbidden");
262 if (!BitList.
empty()) {
266 "' is not a bits type");
271 return Error(Loc,
"Initializer is not compatible with bit range");
276 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
277 unsigned Bit = BitList[i];
279 return Error(Loc,
"Cannot set bit #" + Twine(Bit) +
" of value '" +
285 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
287 NewBits[i] = CurVal->getBit(i);
293 std::string InitType;
295 InitType = (Twine(
"' of type bit initializer with length ") +
296 Twine(BI->getNumBits()))
300 (Twine(
"' of type '") + TI->getType()->getAsString() +
"'").str();
304 "' is incompatible with value '" +
V->getAsString() +
313 const Record *SC = SubClass.
Rec;
314 MapResolver
R(CurRec);
319 if (!
Field.isTemplateArg())
323 if (resolveArgumentsOfClass(R, SC, SubClass.
TemplateArgs,
347 "Already subclass of '" + SC->
getName() +
"'!\n");
354 return AddSubClass(
Entry.Rec.get(), SubClass);
359 for (
auto &
E :
Entry.Loop->Entries) {
360 if (AddSubClass(
E, SubClass))
370bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
375 if (resolveArgumentsOfMultiClass(
388 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion + !!
E.Dump) == 1 &&
389 "RecordsEntry has invalid number of items");
392 if (!Loops.empty()) {
393 Loops.back()->Entries.push_back(std::move(
E));
400 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
401 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
406 CurMultiClass->Entries.push_back(std::move(
E));
412 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
422 return addDefOne(std::move(
E.Rec));
430bool TGParser::resolve(
const ForeachLoop &
Loop, SubstStack &Substs,
bool Final,
431 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
434 for (
const auto &S : Substs)
435 R.set(S.first, S.second);
446 const Init *OldLHS = TI->getLHS();
450 PrintError(Loop.
Loc, Twine(
"unable to resolve if condition '") +
452 "' at end of containing scope");
455 const Init *MHS = TI->getMHS();
456 const Init *
RHS = TI->getRHS();
466 return resolve(Loop.
Entries, Substs, Final, &Dest->back().Loop->Entries,
471 List->getAsString() +
"', expected a list");
476 for (
auto *Elt : *LI) {
493bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
494 SubstStack &Substs,
bool Final,
495 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
497 for (
auto &
E : Source) {
499 Error = resolve(*
E.Loop, Substs, Final, Dest);
501 }
else if (
E.Assertion) {
503 for (
const auto &S : Substs)
504 R.set(S.first, S.second);
505 const Init *Condition =
E.Assertion->Condition->resolveReferences(R);
506 const Init *Message =
E.Assertion->Message->resolveReferences(R);
509 Dest->push_back(std::make_unique<Record::AssertionInfo>(
510 E.Assertion->Loc, Condition, Message));
516 for (
const auto &S : Substs)
517 R.set(S.first, S.second);
522 std::make_unique<Record::DumpInfo>(
E.Dump->Loc, Message));
527 auto Rec = std::make_unique<Record>(*
E.Rec);
529 Rec->appendLoc(*Loc);
531 MapResolver
R(Rec.get());
532 for (
const auto &S : Substs)
533 R.set(S.first, S.second);
534 Rec->resolveReferences(R);
537 Dest->push_back(std::move(Rec));
539 Error = addDefOne(std::move(Rec));
548bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
549 const Init *NewName =
nullptr;
550 if (
const Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
551 if (!Rec->isAnonymous()) {
553 "def already exists: " + Rec->getNameInitAsString());
554 PrintNote(Prev->getLoc(),
"location of previous definition");
557 NewName = Records.getNewAnonymousName();
560 Rec->resolveReferences(NewName);
564 PrintError(Rec->getLoc(), Twine(
"record name '") +
565 Rec->getNameInit()->getAsString() +
566 "' could not be fully resolved");
571 Rec->checkRecordAssertions();
574 Rec->emitRecordDumps();
577 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
579 for (DefsetRecord *Defset : Defsets) {
580 DefInit *
I = Rec->getDefInit();
581 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
582 PrintError(Rec->getLoc(), Twine(
"adding record of incompatible type '") +
583 I->getType()->getAsString() +
591 Records.addDef(std::move(Rec));
595bool TGParser::resolveArguments(
const Record *Rec,
597 SMLoc Loc, ArgValueHandler ArgValueHandler) {
600 "Too many template arguments allowed");
604 for (
auto *Arg : ArgValues) {
605 const Init *ArgName =
nullptr;
606 const Init *ArgValue = Arg->getValue();
607 if (Arg->isPositional())
608 ArgName = ArgNames[Arg->getIndex()];
610 ArgName = Arg->getName();
614 return Error(Loc,
"We can only specify the template argument '" +
617 ArgValueHandler(ArgName, ArgValue);
622 for (
auto *UnsolvedArgName : UnsolvedArgNames) {
625 std::string
Name = UnsolvedArgName->getAsUnquotedString();
626 Error(Loc,
"value not specified for template argument '" + Name +
"'");
631 ArgValueHandler(UnsolvedArgName,
Default);
642 return resolveArguments(
644 [&](
const Init *Name,
const Init *
Value) {
R.set(Name,
Value); });
649bool TGParser::resolveArgumentsOfMultiClass(
654 return resolveArguments(&MC->
Rec, ArgValues, Loc,
655 [&](
const Init *Name,
const Init *
Value) {
656 Substs.emplace_back(Name, Value);
665 if (Lex.getCode() == K) {
679 switch (Lex.getCode()) {
691 Record *CurRec =
nullptr;
693 CurRec = &CurMultiClass->Rec;
702 HasReferenceResolver
R(NameStr);
703 Name->resolveReferences(R);
717const Record *TGParser::ParseClassID() {
719 TokError(
"expected name for ClassID");
723 const Record *
Result = Records.getClass(Lex.getCurStrVal());
725 std::string Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
726 if (MultiClasses[Lex.getCurStrVal()].get())
727 TokError(Msg +
". Use 'defm' if you meant to use multiclass '" +
728 Lex.getCurStrVal() +
"'");
731 }
else if (TrackReferenceLocs) {
732 Result->appendReferenceLoc(Lex.getLocRange());
746 TokError(
"expected name for MultiClassID");
752 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
767 Result.RefRange.Start = Lex.getLoc();
770 if (MultiClass *MC = ParseMultiClassID())
773 Result.Rec = ParseClassID();
780 Result.RefRange.End = Lex.getLoc();
785 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
791 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
796 Result.RefRange.End = Lex.getLoc();
808TGParser::ParseSubMultiClassReference(
MultiClass *CurMC) {
809 SubMultiClassReference
Result;
810 Result.RefRange.Start = Lex.getLoc();
812 Result.MC = ParseMultiClassID();
818 Result.RefRange.End = Lex.getLoc();
823 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, &CurMC->
Rec,
829 Result.RefRange.End = Lex.getLoc();
845 auto LHSLoc = Lex.getLoc();
846 auto *CurVal = ParseValue(CurRec);
851 const TypedInit *
RHS =
nullptr;
852 switch (Lex.getCode()) {
856 auto RHSLoc = Lex.getLoc();
857 CurVal = ParseValue(CurRec);
863 "expected int...int, got " + Twine(
RHS->
getType()->getAsString()));
869 auto i = -Lex.getCurIntVal();
871 TokError(
"invalid range, cannot be negative");
888 "expected int...int, got " + Twine(
LHS->
getType()->getAsString()));
908const TypedInit *TGParser::ParseSliceElements(
Record *CurRec,
bool Single) {
909 const TypedInit *CurVal;
913 auto FlushElems = [&] {
914 if (!Elems.
empty()) {
921 auto LHSLoc = Lex.getLoc();
922 CurVal = ParseSliceElement(CurRec);
925 auto *CurValTy = CurVal->
getType();
930 "expected list<int>, got " + Twine(ListValTy->getAsString()));
940 "unhandled type " + Twine(CurValTy->getAsString()) +
" in range");
967 const TypedInit *
Result =
nullptr;
968 for (
auto *Slice : Slices) {
984 const Init *CurVal = FirstItem;
986 CurVal = ParseValue(
nullptr);
990 return TokError(
"expected integer or bitrange");
992 int64_t
Start =
II->getValue();
996 return TokError(
"invalid range, cannot be negative");
998 switch (Lex.getCode()) {
1007 const Init *I_End = ParseValue(
nullptr);
1010 TokError(
"expected integer value as end of range");
1014 End = II_End->getValue();
1018 End = -Lex.getCurIntVal();
1024 return TokError(
"invalid range, cannot be negative");
1042 if (ParseRangePiece(Result)) {
1048 if (ParseRangePiece(Result)) {
1058 SMLoc StartLoc = Lex.getLoc();
1063 ParseRangeList(Ranges);
1068 TokError(
"expected '>' at end of range list");
1069 return Error(StartLoc,
"to match this '<'");
1078 SMLoc StartLoc = Lex.getLoc();
1083 ParseRangeList(Ranges);
1088 TokError(
"expected '}' at end of bit list");
1089 return Error(StartLoc,
"to match this '{'");
1105const RecTy *TGParser::ParseType() {
1106 switch (Lex.getCode()) {
1108 TokError(
"Unknown token when expecting a type");
1124 auto I = TypeAliases.find(Lex.getCurStrVal());
1125 if (
I != TypeAliases.end()) {
1129 if (
const Record *R = ParseClassID())
1136 TokError(
"expected '<' after bits type");
1140 TokError(
"expected integer in bits<n> type");
1143 uint64_t Val = Lex.getCurIntVal();
1145 TokError(
"expected '>' at end of bits<n> type");
1153 TokError(
"expected '<' after list type");
1157 const RecTy *SubType = ParseType();
1162 TokError(
"expected '>' at end of list<ty> type");
1173 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass, Name, NameLoc,
1174 TrackReferenceLocs))
1177 if (
Mode == ParseNameMode)
1180 if (
const Init *
I = Records.getGlobal(
Name->getValue())) {
1182 if (TrackReferenceLocs) {
1184 Def->getDef()->appendReferenceLoc(NameLoc);
1191 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1195 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1203const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1204 switch (Lex.getCode()) {
1223 const RecTy *
Type =
nullptr;
1225 switch (Lex.getCode()) {
1232 Type = ParseOperatorType();
1235 TokError(
"did not get type for unary operator");
1294 Type = ParseOperatorType();
1297 TokError(
"did not get type for unary operator");
1302 TokError(
"type for !getdagop must be a record type");
1322 TokError(
"expected '(' after unary operator");
1326 const Init *
LHS = ParseValue(CurRec);
1335 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1337 "expected string, list, or dag type argument in unary operator");
1343 "expected string, list, or dag type argument in unary operator");
1353 if (!LHSl && !LHSt) {
1354 TokError(
"expected list type argument in unary operator");
1359 TokError(
"expected list type argument in unary operator");
1364 if (LHSl && LHSl->empty()) {
1365 TokError(
"empty list argument in unary operator");
1368 bool UseElementType =
1371 const Init *Item = LHSl->getElement(0);
1374 TokError(
"untyped list element in unary operator");
1377 Type = UseElementType ? Itemt->getType()
1380 assert(LHSt &&
"expected list type argument in unary operator");
1382 Type = UseElementType ? LType->getElementType() : LType;
1400 TokError(
"expected ')' in unary operator");
1410 const RecTy *
Type = ParseOperatorType();
1415 TokError(
"expected '(' after type of !isa");
1419 const Init *
LHS = ParseValue(CurRec);
1435 const RecTy *
Type = ParseOperatorType();
1440 TokError(
"expected '(' after type of !exists");
1444 SMLoc ExprLoc = Lex.getLoc();
1445 const Init *Expr = ParseValue(CurRec);
1451 Error(ExprLoc,
"expected string type argument in !exists operator");
1458 "expected string type argument in !exists operator, please "
1459 "use !isa instead");
1465 Error(ExprLoc,
"expected string type argument in !exists operator");
1470 TokError(
"expected ')' in !exists");
1481 const RecTy *
Type = ParseOperatorType();
1486 TokError(
"expected '(' after type of !instances");
1493 SMLoc RegexLoc = Lex.getLoc();
1494 Regex = ParseValue(CurRec);
1498 Error(RegexLoc,
"expected string type argument in !instances operator");
1504 Error(RegexLoc,
"expected string type argument in !instances operator");
1513 TokError(
"expected ')' in !instances");
1548 SMLoc OpLoc = Lex.getLoc();
1638 const RecTy *
Type =
nullptr;
1639 const RecTy *ArgType =
nullptr;
1653 Type = ParseOperatorType();
1655 TokError(
"did not get type for !getdagarg operator");
1710 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1712 "', got '" +
Type->getAsString() +
"'");
1717 TokError(
"expected '(' after binary operator");
1726 SMLoc InitLoc = Lex.getLoc();
1727 InitList.
push_back(ParseValue(CurRec, ArgType));
1728 if (!InitList.
back())
1732 if (!InitListBack) {
1733 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1734 InitList.
back()->getAsString() +
"'"));
1737 const RecTy *ListType = InitListBack->getType();
1746 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1752 if (ItemType && InitList.
size() == 1) {
1755 Twine(
"expected output type to be a list, got type '") +
1760 Error(OpLoc, Twine(
"expected first arg type to be '") +
1762 "', got value of type '" +
1771 Error(InitLoc, Twine(
"expected second parameter to be an int, got "
1772 "value of type '") +
1780 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1790 Error(InitLoc, Twine(
"expected bit, bits, int, string, or record; "
1791 "got value of type '") +
1804 Error(InitLoc, Twine(
"expected bit, bits, int, or string; "
1805 "got value of type '") +
1811 switch (InitList.
size()) {
1817 Twine(
"expected list of string, int, bits, or bit; "
1818 "got value of type '") +
1825 Error(InitLoc, Twine(
"expected second argument to be a string, "
1826 "got value of type '") +
1843 Error(InitLoc, Twine(
"expected value of type '") +
1886 TokError(
"expected ')' in operator");
1906 while (InitList.
size() > 2) {
1913 if (InitList.
size() == 2)
1917 Error(OpLoc,
"expected two operands to operator");
1923 return ParseOperationForEachFilter(CurRec, ItemType);
1927 SMLoc OpLoc = Lex.getLoc();
1931 TokError(
"expected '(' after !range operator");
1936 bool FirstArgIsList =
false;
1938 if (
Args.size() >= 3) {
1939 TokError(
"expected at most three values of integer");
1943 SMLoc InitLoc = Lex.getLoc();
1944 Args.push_back(ParseValue(CurRec));
1950 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1951 Args.back()->getAsString() +
"'"));
1955 const RecTy *ArgBackType = ArgBack->getType();
1956 if (!FirstArgIsList ||
Args.size() == 1) {
1958 FirstArgIsList =
true;
1962 if (
Args.size() != 1)
1963 Error(InitLoc, Twine(
"expected value of type 'int', got '" +
1966 Error(InitLoc, Twine(
"expected list or int, got value of type '") +
1973 Error(InitLoc, Twine(
"expected one list, got extra value of type '") +
1982 TokError(
"expected ')' in operator");
1986 const Init *
LHS, *MHS, *
RHS;
1987 auto ArgCount =
Args.size();
1990 const auto *Arg0Ty = Arg0->getType();
1991 if (ArgCount == 1) {
2011 if (ArgCount == 3) {
2032 const RecTy *
Type =
nullptr;
2062 TokError(
"expected '(' after ternary operator");
2066 const Init *
LHS = ParseValue(CurRec);
2071 TokError(
"expected ',' in ternary operator");
2075 SMLoc MHSLoc = Lex.getLoc();
2076 const Init *MHS = ParseValue(CurRec, ItemType);
2081 TokError(
"expected ',' in ternary operator");
2085 SMLoc RHSLoc = Lex.getLoc();
2086 const Init *
RHS = ParseValue(CurRec, ItemType);
2091 TokError(
"expected ')' in binary operator");
2101 Error(MHSLoc,
"could not determine type of the child list in !dag");
2105 Error(MHSLoc, Twine(
"expected list of children, got type '") +
2106 MHSt->getType()->getAsString() +
"'");
2112 Error(RHSLoc,
"could not determine type of the name list in !dag");
2116 Error(RHSLoc, Twine(
"expected list<string>, got type '") +
2117 RHSt->getType()->getAsString() +
"'");
2121 if (!MHSt && !RHSt) {
2123 "cannot have both unset children and unset names in !dag");
2129 const RecTy *MHSTy =
nullptr;
2130 const RecTy *RHSTy =
nullptr;
2133 MHSTy = MHSt->getType();
2140 RHSTy = RHSt->getType();
2152 if (!MHSTy || !RHSTy) {
2153 TokError(
"could not get type for !if");
2168 TokError(
"could not get type for !subst");
2171 Type = RHSt->getType();
2177 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2178 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2188 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2189 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2197 Error(RHSLoc, Twine(
"expected string or unset name, got type '") +
2198 RHSt->getType()->getAsString() +
"'");
2208 return ParseOperationSubstr(CurRec, ItemType);
2211 return ParseOperationFind(CurRec, ItemType);
2214 return ParseOperationCond(CurRec, ItemType);
2220 TokError(
"expected '(' after !foldl");
2224 const Init *StartUntyped = ParseValue(CurRec);
2230 TokError(Twine(
"could not get type of !foldl start: '") +
2236 TokError(
"expected ',' in !foldl");
2240 const Init *ListUntyped = ParseValue(CurRec);
2246 TokError(Twine(
"could not get type of !foldl list: '") +
2253 TokError(Twine(
"!foldl list must be a list, but is of type '") +
2254 List->getType()->getAsString());
2259 TokError(
"expected ',' in !foldl");
2264 TokError(
"third argument of !foldl must be an identifier");
2270 TokError((Twine(
"left !foldl variable '") +
A->getAsString() +
2271 "' already defined")
2277 TokError(
"expected ',' in !foldl");
2282 TokError(
"fourth argument of !foldl must be an identifier");
2288 TokError((Twine(
"right !foldl variable '") +
B->getAsString() +
2289 "' already defined")
2295 TokError(
"expected ',' in !foldl");
2302 std::unique_ptr<Record> ParseRecTmp;
2303 Record *ParseRec = CurRec;
2307 ParseRec = ParseRecTmp.get();
2310 TGVarScope *FoldScope =
PushScope(ParseRec);
2314 const Init *ExprUntyped = ParseValue(ParseRec);
2315 ParseRec->removeValue(
A);
2316 ParseRec->removeValue(
B);
2323 TokError(
"could not get type of !foldl expression");
2327 if (Expr->getType() !=
Start->getType()) {
2328 TokError(Twine(
"!foldl expression must be of same type as start (") +
2329 Start->getType()->getAsString() +
"), but is of type " +
2335 TokError(
"expected ')' in fold operator");
2350const RecTy *TGParser::ParseOperatorType() {
2351 const RecTy *
Type =
nullptr;
2354 TokError(
"expected type name for operator");
2359 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2364 TokError(
"expected type name for operator");
2369 TokError(
"expected type name for operator");
2379const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2380 const RecTy *ItemType) {
2387 TokError(
"expected '(' after !substr operator");
2391 const Init *
LHS = ParseValue(CurRec);
2396 TokError(
"expected ',' in !substr operator");
2400 SMLoc MHSLoc = Lex.getLoc();
2401 const Init *MHS = ParseValue(CurRec);
2405 SMLoc RHSLoc = Lex.getLoc();
2408 RHSLoc = Lex.getLoc();
2409 RHS = ParseValue(CurRec);
2417 TokError(
"expected ')' in !substr operator");
2421 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2422 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2423 "', got '" +
Type->getAsString() +
"'");
2428 TokError(
"could not determine type of the string in !substr");
2432 TokError(Twine(
"expected string, got type '") +
2433 LHSt->getType()->getAsString() +
"'");
2439 TokError(
"could not determine type of the start position in !substr");
2443 Error(MHSLoc, Twine(
"expected int, got type '") +
2444 MHSt->getType()->getAsString() +
"'");
2451 TokError(
"could not determine type of the length in !substr");
2455 TokError(Twine(
"expected int, got type '") +
2456 RHSt->getType()->getAsString() +
"'");
2467const Init *TGParser::ParseOperationFind(
Record *CurRec,
2468 const RecTy *ItemType) {
2475 TokError(
"expected '(' after !find operator");
2479 const Init *
LHS = ParseValue(CurRec);
2484 TokError(
"expected ',' in !find operator");
2488 SMLoc MHSLoc = Lex.getLoc();
2489 const Init *MHS = ParseValue(CurRec);
2493 SMLoc RHSLoc = Lex.getLoc();
2496 RHSLoc = Lex.getLoc();
2497 RHS = ParseValue(CurRec);
2505 TokError(
"expected ')' in !find operator");
2509 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2510 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2511 "', got '" +
Type->getAsString() +
"'");
2516 TokError(
"could not determine type of the source string in !find");
2520 TokError(Twine(
"expected string, got type '") +
2521 LHSt->getType()->getAsString() +
"'");
2527 TokError(
"could not determine type of the target string in !find");
2531 Error(MHSLoc, Twine(
"expected string, got type '") +
2532 MHSt->getType()->getAsString() +
"'");
2539 TokError(
"could not determine type of the start position in !find");
2543 TokError(Twine(
"expected int, got type '") +
2544 RHSt->getType()->getAsString() +
"'");
2556const Init *TGParser::ParseOperationForEachFilter(
Record *CurRec,
2557 const RecTy *ItemType) {
2558 SMLoc OpLoc = Lex.getLoc();
2562 TokError(
"expected '(' after !foreach/!filter");
2567 TokError(
"first argument of !foreach/!filter must be an identifier");
2575 TokError((Twine(
"iteration variable '") +
LHS->getAsString() +
2576 "' is already defined")
2582 TokError(
"expected ',' in !foreach/!filter");
2586 const Init *MHS = ParseValue(CurRec);
2591 TokError(
"expected ',' in !foreach/!filter");
2597 TokError(
"could not get type of !foreach/!filter list or dag");
2601 const RecTy *InEltType =
nullptr;
2602 const RecTy *ExprEltType =
nullptr;
2606 InEltType = InListTy->getElementType();
2610 ? OutListTy->getElementType()
2613 Error(OpLoc,
"expected value of type '" +
2615 "', but got list type");
2621 TokError(
"!filter must have a list argument");
2624 InEltType = InDagTy;
2627 "', but got dag type");
2633 TokError(
"!foreach must have a list or dag argument");
2635 TokError(
"!filter must have a list argument");
2641 std::unique_ptr<Record> ParseRecTmp;
2642 Record *ParseRec = CurRec;
2646 ParseRec = ParseRecTmp.get();
2648 TGVarScope *TempScope =
PushScope(ParseRec);
2650 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2651 ParseRec->removeValue(
LHS);
2657 TokError(
"expected ')' in !foreach/!filter");
2661 const RecTy *OutType = InEltType;
2665 TokError(
"could not get type of !foreach result expression");
2679const Init *TGParser::ParseOperationCond(
Record *CurRec,
2680 const RecTy *ItemType) {
2684 TokError(
"expected '(' after !cond operator");
2695 const Init *
V = ParseValue(CurRec);
2701 TokError(
"expected ':' following a condition in !cond operator");
2705 V = ParseValue(CurRec, ItemType);
2714 TokError(
"expected ',' or ')' following a value in !cond operator");
2719 if (Case.
size() < 1) {
2721 "there should be at least 1 'condition : value' in the !cond operator");
2726 const RecTy *
Type =
nullptr;
2727 for (
const Init *V : Val) {
2728 const RecTy *VTy =
nullptr;
2730 VTy = Vt->getType();
2736 if (
Type ==
nullptr) {
2743 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
2753 TokError(
"could not determine type for !cond from its arguments");
2786const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2788 const Init *
R =
nullptr;
2793 return ParseOperation(CurRec, ItemType);
2797 TokError(
"Unknown or reserved token when parsing a value");
2813 auto BinaryVal = Lex.getCurBinaryIntVal();
2815 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2822 std::string Val = Lex.getCurStrVal();
2827 Val += Lex.getCurStrVal();
2843 SMRange NameLoc = Lex.getLocRange();
2849 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
2854 const Record *
Class = Records.getClass(
Name->getValue());
2857 "Expected a class name, got '" +
Name->getValue() +
"'");
2864 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
2867 if (CheckTemplateArgValues(Args, ArgLocs, Class))
2870 if (resolveArguments(Class, Args, NameLoc.
Start))
2873 if (TrackReferenceLocs)
2874 Class->appendReferenceLoc(NameLoc);
2878 SMLoc BraceLoc = Lex.getLoc();
2883 ParseValueList(Vals, CurRec);
2888 TokError(
"expected '}' at end of bit list value");
2897 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
2903 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2910 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2919 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
2920 ") is not convertable to a bit");
2925 std::reverse(NewBits.
begin(), NewBits.
end());
2932 const RecTy *DeducedEltTy =
nullptr;
2933 const ListRecTy *GivenListTy =
nullptr;
2938 TokError(Twine(
"Encountered a list when expecting a ") +
2942 GivenListTy = ListType;
2946 ParseValueList(Vals, CurRec,
2952 TokError(
"expected ']' at end of list value");
2956 const RecTy *GivenEltTy =
nullptr;
2959 GivenEltTy = ParseType();
2966 TokError(
"expected '>' at end of list element type");
2972 const RecTy *EltTy =
nullptr;
2973 for (
const Init *V : Vals) {
2979 TokError(
"Incompatible types in list elements");
2983 EltTy = TArg->getType();
2992 TokError(
"Incompatible types in list elements");
3009 TokError(Twine(
"Element type mismatch for list: element type '") +
3015 DeducedEltTy = EltTy;
3026 TokError(
"expected identifier or list of value types in dag init");
3030 const Init *
Operator = ParseValue(CurRec);
3035 const StringInit *OperatorName =
nullptr;
3038 TokError(
"expected variable name in dag operator");
3047 ParseDagArgList(DagArgs, CurRec);
3048 if (DagArgs.
empty())
3053 TokError(
"expected ')' in dag init");
3071const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3073 SMLoc LHSLoc = Lex.getLoc();
3074 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3080 switch (Lex.getCode()) {
3084 if (
Mode == ParseNameMode)
3088 SMLoc CurlyLoc = Lex.getLoc();
3090 SmallVector<unsigned, 16>
Ranges;
3091 ParseRangeList(Ranges);
3099 Error(CurlyLoc,
"Invalid bit range for value");
3105 TokError(
"expected '}' at end of bit range list");
3113 Error(LHSLoc,
"Invalid value, list expected");
3120 "' is invalid, list expected");
3125 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3134 LHSTy->getElementType())
3142 TokError(
"expected ']' at end of list slice");
3149 TokError(
"expected field identifier after '.'");
3152 SMRange FieldNameLoc = Lex.getLocRange();
3153 const StringInit *FieldName =
3155 if (!
Result->getFieldType(FieldName)) {
3156 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3157 Result->getAsString() +
"'");
3162 if (TrackReferenceLocs) {
3164 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3165 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3168 for (
const Record *R : RecTy->getClasses())
3169 if (
const auto *RV =
R->getValue(FieldName))
3170 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3181 SMLoc PasteLoc = Lex.getLoc();
3184 Error(PasteLoc,
"LHS of paste is not typed!");
3192 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3194 switch (Lex.getCode()) {
3201 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3218 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3224 const TypedInit *
RHS =
nullptr;
3227 switch (Lex.getCode()) {
3240 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3245 Error(PasteLoc,
"RHS of paste is not typed!");
3255 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3276void TGParser::ParseDagArgList(
3289 const Init *Val = ParseValue(CurRec);
3296 const StringInit *
VarName =
nullptr;
3299 TokError(
"expected variable name in dag literal");
3307 Result.emplace_back(Val, VarName);
3322 Result.push_back(ParseValue(CurRec, ItemType));
3332 Result.push_back(ParseValue(CurRec, ItemType));
3348bool TGParser::ParseTemplateArgValueList(
3351 assert(
Result.empty() &&
"Result vector is not empty");
3357 bool HasNamedArg =
false;
3358 unsigned ArgIndex = 0;
3360 if (ArgIndex >= TArgs.
size()) {
3361 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3368 const Init *
Value = ParseValue(
3370 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3377 return Error(ValueLoc,
3378 "The name of named argument should be a valid identifier");
3382 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3384 return Error(ValueLoc,
3385 "Argument " +
Name->getAsString() +
" doesn't exist");
3388 ValueLoc = Lex.getLoc();
3389 Value = ParseValue(CurRec, NamedArg->getType());
3392 return Error(ValueLoc,
3393 "The value of named argument should be initialized, "
3395 Value->getAsString() +
"'");
3402 return Error(ValueLoc,
3403 "Positional argument should be put before named argument");
3411 return TokError(
"Expected comma before next argument");
3426const Init *TGParser::ParseDeclaration(
Record *CurRec,
3427 bool ParsingTemplateArgs) {
3431 const RecTy *
Type = ParseType();
3436 TokError(
"Expected identifier in declaration");
3440 std::string Str = Lex.getCurStrVal();
3441 if (Str ==
"NAME") {
3442 TokError(
"'" + Str +
"' is a reserved variable name");
3446 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3447 TokError(
"local variable of this name already exists");
3451 SMLoc IdLoc = Lex.getLoc();
3456 if (!ParsingTemplateArgs) {
3457 BadField = AddValue(CurRec, IdLoc,
3458 RecordVal(DeclName, IdLoc,
Type,
3461 }
else if (CurRec) {
3464 AddValue(CurRec, IdLoc,
3467 assert(CurMultiClass &&
"invalid context for template argument");
3470 AddValue(CurRec, IdLoc,
3478 SMLoc ValLoc = Lex.getLoc();
3479 const Init *Val = ParseValue(CurRec,
Type);
3481 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3502TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3504 TokError(
"Expected identifier in foreach declaration");
3513 TokError(
"Expected '=' in foreach declaration");
3517 const RecTy *IterType =
nullptr;
3518 SmallVector<unsigned, 16>
Ranges;
3520 switch (Lex.getCode()) {
3523 ParseRangeList(Ranges);
3525 TokError(
"expected '}' at end of bit range list");
3532 SMLoc ValueLoc = Lex.getLoc();
3533 const Init *
I = ParseValue(
nullptr);
3539 ForeachListValue =
I;
3545 if (ParseRangePiece(Ranges, TI))
3550 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3551 if (CurMultiClass) {
3552 PrintNote({},
"references to multiclass template arguments cannot be "
3553 "resolved at this time");
3560 assert(!IterType &&
"Type already initialized?");
3562 std::vector<Init *> Values;
3563 for (
unsigned R : Ranges)
3581bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3585 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3588 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3596 SMLoc Loc = Lex.getLoc();
3597 TemplArg = ParseDeclaration(CurRec,
true );
3602 return Error(Loc,
"template argument with the same name has already been "
3609 return TokError(
"expected '>' at end of template argument list");
3621bool TGParser::ParseBodyItem(
Record *CurRec) {
3623 return ParseAssert(
nullptr, CurRec);
3626 return ParseDefvar(CurRec);
3629 return ParseDump(
nullptr, CurRec);
3632 if (!ParseDeclaration(CurRec,
false))
3636 return TokError(
"expected ';' after declaration");
3642 return TokError(
"expected field identifier after let");
3644 SMLoc IdLoc = Lex.getLoc();
3645 const StringInit *FieldName =
StringInit::get(Records, Lex.getCurStrVal());
3648 SmallVector<unsigned, 16> BitList;
3649 if (ParseOptionalBitList(BitList))
3651 std::reverse(BitList.
begin(), BitList.
end());
3654 return TokError(
"expected '=' in let expression");
3658 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3667 const Init *Val = ParseValue(CurRec,
Type);
3672 return TokError(
"expected ';' after let expression");
3674 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
3684bool TGParser::ParseBody(
Record *CurRec) {
3690 return TokError(
"Expected '{' to start body or ';' for declaration only");
3693 if (ParseBodyItem(CurRec))
3700 SMLoc SemiLoc = Lex.getLoc();
3702 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3703 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3711bool TGParser::ApplyLetStack(
Record *CurRec) {
3712 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3713 for (LetRecord &LR : LetInfo)
3714 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
3722 return ApplyLetStack(
Entry.Rec.get());
3725 if (
Entry.Assertion)
3732 for (
auto &
E :
Entry.Loop->Entries) {
3733 if (ApplyLetStack(
E))
3749bool TGParser::ParseObjectBody(
Record *CurRec) {
3751 TGVarScope *ObjectScope =
PushScope(CurRec);
3756 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3763 if (AddSubClass(CurRec, SubClass))
3768 SubClass = ParseSubClassReference(CurRec,
false);
3772 if (ApplyLetStack(CurRec))
3775 bool Result = ParseBody(CurRec);
3785bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3786 SMLoc DefLoc = Lex.getLoc();
3793 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
3796 std::unique_ptr<Record> CurRec;
3797 const Init *
Name = ParseObjectName(CurMultiClass);
3802 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
3805 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
3808 if (ParseObjectBody(CurRec.get()))
3811 return addEntry(std::move(CurRec));
3818bool TGParser::ParseDefset() {
3823 Defset.Loc = Lex.getLoc();
3824 const RecTy *
Type = ParseType();
3832 return TokError(
"expected identifier");
3833 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
3834 if (Records.getGlobal(DeclName->
getValue()))
3835 return TokError(
"def or global variable of this name already exists");
3841 SMLoc BraceLoc = Lex.getLoc();
3844 Defsets.push_back(&Defset);
3845 bool Err = ParseObjectList(
nullptr);
3851 TokError(
"expected '}' at end of defset");
3852 return Error(BraceLoc,
"to match this '{'");
3855 Records.addExtraGlobal(DeclName->
getValue(),
3864bool TGParser::ParseDeftype() {
3869 return TokError(
"expected identifier");
3871 const std::string
TypeName = Lex.getCurStrVal();
3872 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
3873 return TokError(
"type of this name '" + TypeName +
"' already exists");
3879 SMLoc Loc = Lex.getLoc();
3880 const RecTy *
Type = ParseType();
3885 return Error(Loc,
"cannot define type alias for class type '" +
3886 Type->getAsString() +
"'");
3900bool TGParser::ParseDefvar(
Record *CurRec) {
3905 return TokError(
"expected identifier");
3906 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
3907 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
3908 return TokError(
"local variable of this name already exists");
3913 if (V && !
V->isTemplateArg())
3914 return TokError(
"field of this name already exists");
3919 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
3920 return TokError(
"def or global variable of this name already exists");
3926 const Init *
Value = ParseValue(CurRec);
3933 if (!CurScope->isOutermost())
3947bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
3948 SMLoc Loc = Lex.getLoc();
3954 const Init *ListValue =
nullptr;
3955 const VarInit *IterName = ParseForeachDeclaration(ListValue);
3957 return TokError(
"expected declaration in for");
3963 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
3965 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
3966 Loops.push_back(std::move(TheLoop));
3970 if (ParseObject(CurMultiClass))
3973 SMLoc BraceLoc = Lex.getLoc();
3978 if (ParseObjectList(CurMultiClass))
3982 TokError(
"expected '}' at end of foreach command");
3983 return Error(BraceLoc,
"to match this '{'");
3990 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
3993 return addEntry(std::move(Loop));
4001bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4002 SMLoc Loc = Lex.getLoc();
4008 const Init *Condition = ParseValue(
nullptr);
4022 const ListInit *SingletonList =
4028 const Init *ThenClauseList =
4032 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4034 if (ParseIfBody(CurMultiClass,
"then"))
4037 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4040 if (addEntry(std::move(Loop)))
4050 const Init *ElseClauseList =
4055 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4057 if (ParseIfBody(CurMultiClass,
"else"))
4060 Loop = std::move(Loops.back());
4063 if (addEntry(std::move(Loop)))
4081 if (ParseObject(CurMultiClass))
4084 SMLoc BraceLoc = Lex.getLoc();
4089 if (ParseObjectList(CurMultiClass))
4093 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4094 return Error(BraceLoc,
"to match this '{'");
4109 SMLoc ConditionLoc = Lex.getLoc();
4110 const Init *Condition = ParseValue(CurRec);
4115 TokError(
"expected ',' in assert statement");
4119 const Init *Message = ParseValue(CurRec);
4127 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4129 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4138bool TGParser::ParseClass() {
4143 return TokError(
"expected class name after 'class' keyword");
4145 const std::string &
Name = Lex.getCurStrVal();
4146 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4153 "' already defined");
4158 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4160 CurRec = NewRec.get();
4161 Records.addClass(std::move(NewRec));
4164 if (TypeAliases.count(Name))
4165 return TokError(
"there is already a defined type alias '" + Name +
"'");
4170 TGVarScope *ClassScope =
PushScope(CurRec);
4173 if (ParseTemplateArgList(CurRec))
4176 if (ParseObjectBody(CurRec))
4179 if (!NoWarnOnUnusedTemplateArgs)
4195 TokError(
"expected identifier in let definition");
4201 SMLoc NameLoc = Lex.getLoc();
4205 SmallVector<unsigned, 16>
Bits;
4206 if (ParseOptionalRangeList(Bits)) {
4210 std::reverse(
Bits.begin(),
Bits.end());
4213 TokError(
"expected '=' in let expression");
4218 const Init *Val = ParseValue(
nullptr);
4225 Result.emplace_back(Name, Bits, Val, NameLoc);
4235bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4241 ParseLetList(LetInfo);
4242 if (LetInfo.
empty())
4244 LetStack.push_back(std::move(LetInfo));
4247 return TokError(
"expected 'in' at end of top-level 'let'");
4252 if (ParseObject(CurMultiClass))
4255 SMLoc BraceLoc = Lex.getLoc();
4263 if (ParseObjectList(CurMultiClass))
4267 TokError(
"expected '}' at end of top level let command");
4268 return Error(BraceLoc,
"to match this '{'");
4275 LetStack.pop_back();
4292bool TGParser::ParseMultiClass() {
4297 return TokError(
"expected identifier after multiclass for name");
4298 std::string
Name = Lex.getCurStrVal();
4300 auto Result = MultiClasses.try_emplace(
4301 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4304 return TokError(
"multiclass '" + Name +
"' already defined");
4306 CurMultiClass =
Result.first->second.get();
4310 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4314 if (ParseTemplateArgList(
nullptr))
4317 bool inherits =
false;
4324 SubMultiClassReference SubMultiClass =
4325 ParseSubMultiClassReference(CurMultiClass);
4328 if (!SubMultiClass.
MC)
4332 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4337 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4343 return TokError(
"expected '{' in multiclass definition");
4345 return TokError(
"expected ';' in multiclass definition");
4348 return TokError(
"multiclass must contain at least one def");
4351 switch (Lex.getCode()) {
4353 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4354 "'foreach', 'if', or 'let' in multiclass body");
4364 if (ParseObject(CurMultiClass))
4372 SMLoc SemiLoc = Lex.getLoc();
4374 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4375 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4379 if (!NoWarnOnUnusedTemplateArgs)
4380 CurMultiClass->Rec.checkUnusedTemplateArgs();
4383 CurMultiClass =
nullptr;
4391bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4395 const Init *DefmName = ParseObjectName(CurMultiClass);
4399 DefmName = Records.getNewAnonymousName();
4408 return TokError(
"expected ':' after defm identifier");
4411 std::vector<RecordsEntry> NewEntries;
4414 bool InheritFromClass =
false;
4419 SMLoc SubClassLoc = Lex.getLoc();
4420 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4430 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4431 assert(MC &&
"Didn't lookup multiclass correctly?");
4434 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4438 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4439 &NewEntries, &SubClassLoc))
4446 return TokError(
"expected identifier");
4448 SubClassLoc = Lex.getLoc();
4452 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4454 if (InheritFromClass)
4457 Ref = ParseSubClassReference(
nullptr,
true);
4460 if (InheritFromClass) {
4463 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4471 for (
auto &
E : NewEntries) {
4473 if (AddSubClass(
E, SubClass))
4479 SubClass = ParseSubClassReference(
nullptr,
false);
4483 for (
auto &
E : NewEntries) {
4484 if (ApplyLetStack(
E))
4487 addEntry(std::move(
E));
4491 return TokError(
"expected ';' at end of defm");
4509 switch (Lex.getCode()) {
4512 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4514 return ParseAssert(MC);
4516 return ParseDef(MC);
4518 return ParseDefm(MC);
4520 return ParseDeftype();
4522 return ParseDefvar();
4524 return ParseDump(MC);
4526 return ParseForeach(MC);
4530 return ParseTopLevelLet(MC);
4533 return TokError(
"defset is not allowed inside multiclass");
4534 return ParseDefset();
4537 return TokError(
"class is not allowed inside multiclass");
4539 return TokError(
"class is not allowed inside foreach loop");
4540 return ParseClass();
4543 return TokError(
"multiclass is not allowed inside foreach loop");
4544 return ParseMultiClass();
4550bool TGParser::ParseObjectList(
MultiClass *MC) {
4552 if (ParseObject(MC))
4561 if (ParseObjectList())
4569 return TokError(
"Unexpected token at top level");
4576bool TGParser::CheckTemplateArgValues(
4580 "expected as many values as locations");
4584 bool HasError =
false;
4586 const Init *ArgName =
nullptr;
4587 if (
Value->isPositional())
4588 ArgName = TArgs[
Value->getIndex()];
4589 if (
Value->isNamed())
4596 auto *CastValue = ArgValue->
getCastTo(ArgType);
4600 "result of template arg value cast has wrong type");
4604 Loc,
"Value specified for template argument '" +
4606 ArgValue->getType()->
getAsString() +
"; expected type " +
4615#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4624 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4625 <<
ListValue->getAsString() <<
" in {\n";
4634 errs() <<
"Record:\n";
4637 errs() <<
"Defs:\n";
4649 const Init *Message = ParseValue(CurRec);
4669 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.
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)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - 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 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)
StringRef - 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.
FunctionAddr VTableAddr Value
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void PrintError(const Twine &Msg)
std::string utostr(uint64_t X, bool isNeg=false)
bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
void PrintNote(const Twine &Msg)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
FunctionAddr VTableAddr Next
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void dumpMessage(SMLoc Loc, const Init *Message)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
@ Default
The result values are 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
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