19#include "llvm/Config/llvm-config.h"
66#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
68 errs() <<
"Multiclass:\n";
72 errs() <<
"Template args:\n";
80 for (
unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
81 const Init *Bit = BV->getBit(i);
82 bool IsReference =
false;
85 if (R.getValue(VI->getName()))
91 if (!(IsReference || Bit->isConcrete()))
98 for (
const RecordVal &RV : R.getValues()) {
103 if (RV.isNonconcreteOK())
106 if (
const Init *V = RV.getValue()) {
110 RV.getNameInitAsString() +
"' in '" +
111 R.getNameInitAsString() +
112 "' could not be fully resolved: " +
113 RV.getValue()->getAsString());
129 NewName = BinOp->Fold(&CurRec);
149 bool TrackReferenceLocs)
const {
151 auto It = Vars.find(Name->getValue());
152 if (It != Vars.end())
155 auto FindValueInArgs = [&](
Record *Rec,
162 assert(RV &&
"Template arg doesn't exist??");
164 if (TrackReferenceLocs)
168 return Name->getValue() ==
"NAME"
182 if (TrackReferenceLocs)
183 RV->addReferenceLoc(NameLoc);
188 if (CurRec->isClass())
189 if (
auto *V = FindValueInArgs(CurRec, Name))
196 if (CurLoop->IterVar) {
198 if (IterVar && IterVar->getNameInit() == Name)
206 if (
auto *V = FindValueInArgs(&CurMultiClass->Rec, Name))
214 return Parent->getVar(Records, ParsingMultiClass, Name, NameLoc,
222 CurRec = &CurMultiClass->
Rec;
227 return Error(
Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
229 "' is incompatible with " +
230 "previous definition of type '" +
231 ERV->getType()->getAsString() +
"'");
242 bool AllowSelfAssignment,
bool OverrideDefLoc,
248 CurRec = &CurMultiClass->Rec;
250 RecordVal *RV = CurRec->
getValue(ValName);
259 if (!BitList.
empty())
260 return Error(Loc,
"Cannot use append/prepend with bit range");
262 const Init *CurrentValue = RV->
getValue();
263 const RecTy *FieldType = RV->
getType();
269 const Init *
LHS = IsAppendMode ? CurrentValue :
V;
270 const Init *
RHS = IsAppendMode ?
V : CurrentValue;
280 return Error(Loc, Twine(
"Cannot ") +
281 (IsAppendMode ?
"append to" :
"prepend to") +
283 "' of type '" + FieldType->getAsString() +
284 "' (expected list, string, code, or dag)");
294 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
295 return Error(Loc,
"Recursion / self-assignment forbidden");
299 if (!BitList.
empty()) {
303 "' is not a bits type");
308 return Error(Loc,
"Initializer is not compatible with bit range");
313 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
314 unsigned Bit = BitList[i];
316 return Error(Loc,
"Cannot set bit #" + Twine(Bit) +
" of value '" +
322 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
324 NewBits[i] = CurVal->getBit(i);
330 std::string InitType;
332 InitType = (Twine(
"' of type bit initializer with length ") +
333 Twine(BI->getNumBits()))
337 (Twine(
"' of type '") + TI->getType()->getAsString() +
"'").str();
341 "' is incompatible with value '" +
V->getAsString() +
351 MapResolver
R(CurRec);
356 if (!
Field.isTemplateArg())
360 if (resolveArgumentsOfClass(R, SC, SubClass.
TemplateArgs,
384 "Already subclass of '" + SC->
getName() +
"'!\n");
391 return AddSubClass(
Entry.Rec.get(), SubClass);
396 for (
auto &
E :
Entry.Loop->Entries) {
397 if (AddSubClass(
E, SubClass))
407bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
412 if (resolveArgumentsOfMultiClass(
425 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion + !!
E.Dump) == 1 &&
426 "RecordsEntry has invalid number of items");
429 if (!Loops.empty()) {
430 Loops.back()->Entries.push_back(std::move(
E));
437 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
438 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
443 CurMultiClass->Entries.push_back(std::move(
E));
449 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
459 return addDefOne(std::move(
E.Rec));
467bool TGParser::resolve(
const ForeachLoop &
Loop, SubstStack &Substs,
bool Final,
468 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
471 for (
const auto &S : Substs)
472 R.set(S.first, S.second);
483 const Init *OldLHS = TI->getLHS();
487 PrintError(Loop.
Loc, Twine(
"unable to resolve if condition '") +
489 "' at end of containing scope");
492 const Init *MHS = TI->getMHS();
493 const Init *
RHS = TI->getRHS();
503 return resolve(Loop.
Entries, Substs, Final, &Dest->back().Loop->Entries,
508 List->getAsString() +
"', expected a list");
513 for (
auto *Elt : *LI) {
530bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
531 SubstStack &Substs,
bool Final,
532 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
534 for (
auto &
E : Source) {
536 Error = resolve(*
E.Loop, Substs, Final, Dest);
538 }
else if (
E.Assertion) {
540 for (
const auto &S : Substs)
541 R.set(S.first, S.second);
542 const Init *Condition =
E.Assertion->Condition->resolveReferences(R);
543 const Init *Message =
E.Assertion->Message->resolveReferences(R);
546 Dest->push_back(std::make_unique<Record::AssertionInfo>(
547 E.Assertion->Loc, Condition, Message));
553 for (
const auto &S : Substs)
554 R.set(S.first, S.second);
559 std::make_unique<Record::DumpInfo>(
E.Dump->Loc, Message));
564 auto Rec = std::make_unique<Record>(*
E.Rec);
566 Rec->appendLoc(*Loc);
568 MapResolver
R(Rec.get());
569 for (
const auto &S : Substs)
570 R.set(S.first, S.second);
571 Rec->resolveReferences(R);
574 Dest->push_back(std::move(Rec));
576 Error = addDefOne(std::move(Rec));
585bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
586 const Init *NewName =
nullptr;
587 if (
const Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
588 if (!Rec->isAnonymous()) {
590 "def already exists: " + Rec->getNameInitAsString());
591 PrintNote(Prev->getLoc(),
"location of previous definition");
594 NewName = Records.getNewAnonymousName();
597 Rec->resolveReferences(NewName);
601 PrintError(Rec->getLoc(), Twine(
"record name '") +
602 Rec->getNameInit()->getAsString() +
603 "' could not be fully resolved");
608 Rec->checkRecordAssertions();
611 Rec->emitRecordDumps();
614 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
616 for (DefsetRecord *Defset : Defsets) {
617 DefInit *
I = Rec->getDefInit();
618 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
619 PrintError(Rec->getLoc(), Twine(
"adding record of incompatible type '") +
620 I->getType()->getAsString() +
628 Records.addDef(std::move(Rec));
632bool TGParser::resolveArguments(
const Record *Rec,
634 SMLoc Loc, ArgValueHandler ArgValueHandler) {
637 "Too many template arguments allowed");
641 for (
auto *Arg : ArgValues) {
642 const Init *ArgName =
nullptr;
643 const Init *ArgValue = Arg->getValue();
644 if (Arg->isPositional())
645 ArgName = ArgNames[Arg->getIndex()];
647 ArgName = Arg->getName();
651 return Error(Loc,
"We can only specify the template argument '" +
654 ArgValueHandler(ArgName, ArgValue);
659 for (
auto *UnsolvedArgName : UnsolvedArgNames) {
662 std::string
Name = UnsolvedArgName->getAsUnquotedString();
663 Error(Loc,
"value not specified for template argument '" + Name +
"'");
668 ArgValueHandler(UnsolvedArgName,
Default);
679 return resolveArguments(
681 [&](
const Init *Name,
const Init *
Value) {
R.set(Name,
Value); });
686bool TGParser::resolveArgumentsOfMultiClass(
691 return resolveArguments(&MC->
Rec, ArgValues, Loc,
692 [&](
const Init *Name,
const Init *
Value) {
693 Substs.emplace_back(Name, Value);
702 if (Lex.getCode() == K) {
716 switch (Lex.getCode()) {
730 CurRec = &CurMultiClass->Rec;
739 HasReferenceResolver
R(NameStr);
740 Name->resolveReferences(R);
754const Record *TGParser::ParseClassID() {
756 TokError(
"expected name for ClassID");
760 const Record *
Result = Records.getClass(Lex.getCurStrVal());
762 std::string Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
763 if (MultiClasses[Lex.getCurStrVal()].get())
764 TokError(Msg +
". Use 'defm' if you meant to use multiclass '" +
765 Lex.getCurStrVal() +
"'");
768 }
else if (TrackReferenceLocs) {
769 Result->appendReferenceLoc(Lex.getLocRange());
783 TokError(
"expected name for MultiClassID");
789 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
804 Result.RefRange.Start = Lex.getLoc();
807 if (MultiClass *MC = ParseMultiClassID())
810 Result.Rec = ParseClassID();
817 Result.RefRange.End = Lex.getLoc();
822 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
828 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
833 Result.RefRange.End = Lex.getLoc();
845TGParser::ParseSubMultiClassReference(
MultiClass *CurMC) {
846 SubMultiClassReference
Result;
847 Result.RefRange.Start = Lex.getLoc();
849 Result.MC = ParseMultiClassID();
855 Result.RefRange.End = Lex.getLoc();
860 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, &CurMC->
Rec,
866 Result.RefRange.End = Lex.getLoc();
882 auto LHSLoc = Lex.getLoc();
883 auto *CurVal = ParseValue(CurRec);
888 const TypedInit *
RHS =
nullptr;
889 switch (Lex.getCode()) {
893 auto RHSLoc = Lex.getLoc();
894 CurVal = ParseValue(CurRec);
900 "expected int...int, got " + Twine(
RHS->
getType()->getAsString()));
906 auto i = -Lex.getCurIntVal();
908 TokError(
"invalid range, cannot be negative");
925 "expected int...int, got " + Twine(
LHS->
getType()->getAsString()));
945const TypedInit *TGParser::ParseSliceElements(
Record *CurRec,
bool Single) {
946 const TypedInit *CurVal;
950 auto FlushElems = [&] {
951 if (!Elems.
empty()) {
958 auto LHSLoc = Lex.getLoc();
959 CurVal = ParseSliceElement(CurRec);
962 auto *CurValTy = CurVal->
getType();
967 "expected list<int>, got " + Twine(ListValTy->getAsString()));
977 "unhandled type " + Twine(CurValTy->getAsString()) +
" in range");
1004 const TypedInit *
Result =
nullptr;
1005 for (
auto *Slice : Slices) {
1021 const Init *CurVal = FirstItem;
1023 CurVal = ParseValue(
nullptr);
1027 return TokError(
"expected integer or bitrange");
1029 int64_t
Start =
II->getValue();
1033 return TokError(
"invalid range, cannot be negative");
1035 switch (Lex.getCode()) {
1044 const Init *I_End = ParseValue(
nullptr);
1047 TokError(
"expected integer value as end of range");
1051 End = II_End->getValue();
1055 End = -Lex.getCurIntVal();
1061 return TokError(
"invalid range, cannot be negative");
1079 if (ParseRangePiece(Result)) {
1085 if (ParseRangePiece(Result)) {
1095 SMLoc StartLoc = Lex.getLoc();
1100 ParseRangeList(Ranges);
1105 TokError(
"expected '}' at end of bit list");
1106 return Error(StartLoc,
"to match this '{'");
1122const RecTy *TGParser::ParseType() {
1123 switch (Lex.getCode()) {
1125 TokError(
"Unknown token when expecting a type");
1141 auto I = TypeAliases.find(Lex.getCurStrVal());
1142 if (
I != TypeAliases.end()) {
1146 if (
const Record *R = ParseClassID())
1153 TokError(
"expected '<' after bits type");
1157 TokError(
"expected integer in bits<n> type");
1160 uint64_t Val = Lex.getCurIntVal();
1162 TokError(
"expected '>' at end of bits<n> type");
1170 TokError(
"expected '<' after list type");
1174 const RecTy *SubType = ParseType();
1179 TokError(
"expected '>' at end of list<ty> type");
1190 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass, Name, NameLoc,
1191 TrackReferenceLocs))
1194 if (
Mode == ParseNameMode)
1197 if (
const Init *
I = Records.getGlobal(
Name->getValue())) {
1199 if (TrackReferenceLocs) {
1201 Def->getDef()->appendReferenceLoc(NameLoc);
1208 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1212 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1220const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1221 switch (Lex.getCode()) {
1240 const RecTy *
Type =
nullptr;
1242 switch (Lex.getCode()) {
1249 Type = ParseOperatorType();
1252 TokError(
"did not get type for unary operator");
1311 Type = ParseOperatorType();
1314 TokError(
"did not get type for unary operator");
1319 TokError(
"type for !getdagop must be a record type");
1339 TokError(
"expected '(' after unary operator");
1343 const Init *
LHS = ParseValue(CurRec);
1352 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1354 "expected string, list, or dag type argument in unary operator");
1360 "expected string, list, or dag type argument in unary operator");
1370 if (!LHSl && !LHSt) {
1371 TokError(
"expected list type argument in unary operator");
1376 TokError(
"expected list type argument in unary operator");
1381 if (LHSl && LHSl->empty()) {
1382 TokError(
"empty list argument in unary operator");
1385 bool UseElementType =
1388 const Init *Item = LHSl->getElement(0);
1391 TokError(
"untyped list element in unary operator");
1394 Type = UseElementType ? Itemt->getType()
1397 assert(LHSt &&
"expected list type argument in unary operator");
1399 Type = UseElementType ? LType->getElementType() : LType;
1417 TokError(
"expected ')' in unary operator");
1427 const RecTy *
Type = ParseOperatorType();
1432 TokError(
"expected '(' after type of !isa");
1436 const Init *
LHS = ParseValue(CurRec);
1452 const RecTy *
Type = ParseOperatorType();
1457 TokError(
"expected '(' after type of !exists");
1461 SMLoc ExprLoc = Lex.getLoc();
1462 const Init *Expr = ParseValue(CurRec);
1468 Error(ExprLoc,
"expected string type argument in !exists operator");
1475 "expected string type argument in !exists operator, please "
1476 "use !isa instead");
1482 Error(ExprLoc,
"expected string type argument in !exists operator");
1487 TokError(
"expected ')' in !exists");
1498 const RecTy *
Type = ParseOperatorType();
1503 TokError(
"expected '(' after type of !instances");
1510 SMLoc RegexLoc = Lex.getLoc();
1511 Regex = ParseValue(CurRec);
1515 Error(RegexLoc,
"expected string type argument in !instances operator");
1521 Error(RegexLoc,
"expected string type argument in !instances operator");
1530 TokError(
"expected ')' in !instances");
1565 SMLoc OpLoc = Lex.getLoc();
1655 const RecTy *
Type =
nullptr;
1656 const RecTy *ArgType =
nullptr;
1670 Type = ParseOperatorType();
1672 TokError(
"did not get type for !getdagarg operator");
1727 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1729 "', got '" +
Type->getAsString() +
"'");
1734 TokError(
"expected '(' after binary operator");
1743 SMLoc InitLoc = Lex.getLoc();
1744 InitList.
push_back(ParseValue(CurRec, ArgType));
1745 if (!InitList.
back())
1749 if (!InitListBack) {
1750 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1751 InitList.
back()->getAsString() +
"'"));
1754 const RecTy *ListType = InitListBack->getType();
1763 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1769 if (ItemType && InitList.
size() == 1) {
1772 Twine(
"expected output type to be a list, got type '") +
1777 Error(OpLoc, Twine(
"expected first arg type to be '") +
1779 "', got value of type '" +
1788 Error(InitLoc, Twine(
"expected second parameter to be an int, got "
1789 "value of type '") +
1797 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1807 Error(InitLoc, Twine(
"expected bit, bits, int, string, or record; "
1808 "got value of type '") +
1821 Error(InitLoc, Twine(
"expected bit, bits, int, or string; "
1822 "got value of type '") +
1828 switch (InitList.
size()) {
1834 Twine(
"expected list of string, int, bits, or bit; "
1835 "got value of type '") +
1842 Error(InitLoc, Twine(
"expected second argument to be a string, "
1843 "got value of type '") +
1860 Error(InitLoc, Twine(
"expected value of type '") +
1903 TokError(
"expected ')' in operator");
1923 while (InitList.
size() > 2) {
1930 if (InitList.
size() == 2)
1934 Error(OpLoc,
"expected two operands to operator");
1940 return ParseOperationForEachFilter(CurRec, ItemType);
1944 SMLoc OpLoc = Lex.getLoc();
1948 TokError(
"expected '(' after !range operator");
1953 bool FirstArgIsList =
false;
1955 if (
Args.size() >= 3) {
1956 TokError(
"expected at most three values of integer");
1960 SMLoc InitLoc = Lex.getLoc();
1961 Args.push_back(ParseValue(CurRec));
1967 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1968 Args.back()->getAsString() +
"'"));
1972 const RecTy *ArgBackType = ArgBack->getType();
1973 if (!FirstArgIsList ||
Args.size() == 1) {
1975 FirstArgIsList =
true;
1979 if (
Args.size() != 1)
1980 Error(InitLoc, Twine(
"expected value of type 'int', got '" +
1983 Error(InitLoc, Twine(
"expected list or int, got value of type '") +
1990 Error(InitLoc, Twine(
"expected one list, got extra value of type '") +
1999 TokError(
"expected ')' in operator");
2003 const Init *
LHS, *MHS, *
RHS;
2004 auto ArgCount =
Args.size();
2007 const auto *Arg0Ty = Arg0->getType();
2008 if (ArgCount == 1) {
2028 if (ArgCount == 3) {
2049 const RecTy *
Type =
nullptr;
2079 TokError(
"expected '(' after ternary operator");
2083 const Init *
LHS = ParseValue(CurRec);
2088 TokError(
"expected ',' in ternary operator");
2092 SMLoc MHSLoc = Lex.getLoc();
2093 const Init *MHS = ParseValue(CurRec, ItemType);
2098 TokError(
"expected ',' in ternary operator");
2102 SMLoc RHSLoc = Lex.getLoc();
2103 const Init *
RHS = ParseValue(CurRec, ItemType);
2108 TokError(
"expected ')' in binary operator");
2118 Error(MHSLoc,
"could not determine type of the child list in !dag");
2122 Error(MHSLoc, Twine(
"expected list of children, got type '") +
2123 MHSt->getType()->getAsString() +
"'");
2129 Error(RHSLoc,
"could not determine type of the name list in !dag");
2133 Error(RHSLoc, Twine(
"expected list<string>, got type '") +
2134 RHSt->getType()->getAsString() +
"'");
2138 if (!MHSt && !RHSt) {
2140 "cannot have both unset children and unset names in !dag");
2146 const RecTy *MHSTy =
nullptr;
2147 const RecTy *RHSTy =
nullptr;
2150 MHSTy = MHSt->getType();
2157 RHSTy = RHSt->getType();
2169 if (!MHSTy || !RHSTy) {
2170 TokError(
"could not get type for !if");
2185 TokError(
"could not get type for !subst");
2188 Type = RHSt->getType();
2194 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2195 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2205 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2206 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2214 Error(RHSLoc, Twine(
"expected string or unset name, got type '") +
2215 RHSt->getType()->getAsString() +
"'");
2225 return ParseOperationSubstr(CurRec, ItemType);
2228 return ParseOperationFind(CurRec, ItemType);
2231 return ParseOperationCond(CurRec, ItemType);
2237 TokError(
"expected '(' after !foldl");
2241 const Init *StartUntyped = ParseValue(CurRec);
2247 TokError(Twine(
"could not get type of !foldl start: '") +
2253 TokError(
"expected ',' in !foldl");
2257 const Init *ListUntyped = ParseValue(CurRec);
2263 TokError(Twine(
"could not get type of !foldl list: '") +
2270 TokError(Twine(
"!foldl list must be a list, but is of type '") +
2271 List->getType()->getAsString());
2276 TokError(
"expected ',' in !foldl");
2281 TokError(
"third argument of !foldl must be an identifier");
2287 TokError((Twine(
"left !foldl variable '") +
A->getAsString() +
2288 "' 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")
2312 TokError(
"expected ',' in !foldl");
2319 std::unique_ptr<Record> ParseRecTmp;
2320 Record *ParseRec = CurRec;
2324 ParseRec = ParseRecTmp.get();
2327 TGVarScope *FoldScope =
PushScope(ParseRec);
2331 const Init *ExprUntyped = ParseValue(ParseRec);
2332 ParseRec->removeValue(
A);
2333 ParseRec->removeValue(
B);
2340 TokError(
"could not get type of !foldl expression");
2344 if (Expr->getType() !=
Start->getType()) {
2345 TokError(Twine(
"!foldl expression must be of same type as start (") +
2346 Start->getType()->getAsString() +
"), but is of type " +
2352 TokError(
"expected ')' in fold operator");
2367const RecTy *TGParser::ParseOperatorType() {
2368 const RecTy *
Type =
nullptr;
2371 TokError(
"expected type name for operator");
2376 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2381 TokError(
"expected type name for operator");
2386 TokError(
"expected type name for operator");
2396const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2397 const RecTy *ItemType) {
2404 TokError(
"expected '(' after !substr operator");
2408 const Init *
LHS = ParseValue(CurRec);
2413 TokError(
"expected ',' in !substr operator");
2417 SMLoc MHSLoc = Lex.getLoc();
2418 const Init *MHS = ParseValue(CurRec);
2422 SMLoc RHSLoc = Lex.getLoc();
2425 RHSLoc = Lex.getLoc();
2426 RHS = ParseValue(CurRec);
2434 TokError(
"expected ')' in !substr operator");
2438 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2439 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2440 "', got '" +
Type->getAsString() +
"'");
2445 TokError(
"could not determine type of the string in !substr");
2449 TokError(Twine(
"expected string, got type '") +
2450 LHSt->getType()->getAsString() +
"'");
2456 TokError(
"could not determine type of the start position in !substr");
2460 Error(MHSLoc, Twine(
"expected int, got type '") +
2461 MHSt->getType()->getAsString() +
"'");
2468 TokError(
"could not determine type of the length in !substr");
2472 TokError(Twine(
"expected int, got type '") +
2473 RHSt->getType()->getAsString() +
"'");
2484const Init *TGParser::ParseOperationFind(
Record *CurRec,
2485 const RecTy *ItemType) {
2492 TokError(
"expected '(' after !find operator");
2496 const Init *
LHS = ParseValue(CurRec);
2501 TokError(
"expected ',' in !find operator");
2505 SMLoc MHSLoc = Lex.getLoc();
2506 const Init *MHS = ParseValue(CurRec);
2510 SMLoc RHSLoc = Lex.getLoc();
2513 RHSLoc = Lex.getLoc();
2514 RHS = ParseValue(CurRec);
2522 TokError(
"expected ')' in !find operator");
2526 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2527 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2528 "', got '" +
Type->getAsString() +
"'");
2533 TokError(
"could not determine type of the source string in !find");
2537 TokError(Twine(
"expected string, got type '") +
2538 LHSt->getType()->getAsString() +
"'");
2544 TokError(
"could not determine type of the target string in !find");
2548 Error(MHSLoc, Twine(
"expected string, got type '") +
2549 MHSt->getType()->getAsString() +
"'");
2556 TokError(
"could not determine type of the start position in !find");
2560 TokError(Twine(
"expected int, got type '") +
2561 RHSt->getType()->getAsString() +
"'");
2573const Init *TGParser::ParseOperationForEachFilter(
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")
2599 TokError(
"expected ',' in !foreach/!filter");
2603 const Init *MHS = ParseValue(CurRec);
2608 TokError(
"expected ',' in !foreach/!filter");
2614 TokError(
"could not get type of !foreach/!filter list or dag");
2618 const RecTy *InEltType =
nullptr;
2619 const RecTy *ExprEltType =
nullptr;
2623 InEltType = InListTy->getElementType();
2627 ? OutListTy->getElementType()
2630 Error(OpLoc,
"expected value of type '" +
2632 "', but got list type");
2638 TokError(
"!filter must have a list argument");
2641 InEltType = InDagTy;
2644 "', but got dag type");
2650 TokError(
"!foreach must have a list or dag argument");
2652 TokError(
"!filter must have a list argument");
2658 std::unique_ptr<Record> ParseRecTmp;
2659 Record *ParseRec = CurRec;
2663 ParseRec = ParseRecTmp.get();
2665 TGVarScope *TempScope =
PushScope(ParseRec);
2667 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2668 ParseRec->removeValue(
LHS);
2674 TokError(
"expected ')' in !foreach/!filter");
2678 const RecTy *OutType = InEltType;
2682 TokError(
"could not get type of !foreach result expression");
2696const Init *TGParser::ParseOperationCond(
Record *CurRec,
2697 const RecTy *ItemType) {
2701 TokError(
"expected '(' after !cond operator");
2712 const Init *
V = ParseValue(CurRec);
2718 TokError(
"expected ':' following a condition in !cond operator");
2722 V = ParseValue(CurRec, ItemType);
2731 TokError(
"expected ',' or ')' following a value in !cond operator");
2736 if (Case.
size() < 1) {
2738 "there should be at least 1 'condition : value' in the !cond operator");
2743 const RecTy *
Type =
nullptr;
2744 for (
const Init *V : Val) {
2745 const RecTy *VTy =
nullptr;
2747 VTy = Vt->getType();
2753 if (
Type ==
nullptr) {
2760 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
2770 TokError(
"could not determine type for !cond from its arguments");
2803const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2805 const Init *
R =
nullptr;
2810 return ParseOperation(CurRec, ItemType);
2814 TokError(
"Unknown or reserved token when parsing a value");
2830 auto BinaryVal = Lex.getCurBinaryIntVal();
2832 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2839 std::string Val = Lex.getCurStrVal();
2844 Val += Lex.getCurStrVal();
2860 SMRange NameLoc = Lex.getLocRange();
2866 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
2874 "Expected a class name, got '" +
Name->getValue() +
"'");
2881 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
2884 if (CheckTemplateArgValues(Args, ArgLocs, Class))
2887 if (resolveArguments(Class, Args, NameLoc.
Start))
2890 if (TrackReferenceLocs)
2891 Class->appendReferenceLoc(NameLoc);
2895 SMLoc BraceLoc = Lex.getLoc();
2900 ParseValueList(Vals, CurRec);
2905 TokError(
"expected '}' at end of bit list value");
2914 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
2920 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2927 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2936 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
2937 ") is not convertable to a bit");
2942 std::reverse(NewBits.
begin(), NewBits.
end());
2949 const RecTy *DeducedEltTy =
nullptr;
2950 const ListRecTy *GivenListTy =
nullptr;
2955 TokError(Twine(
"Encountered a list when expecting a ") +
2959 GivenListTy = ListType;
2963 ParseValueList(Vals, CurRec,
2969 TokError(
"expected ']' at end of list value");
2973 const RecTy *GivenEltTy =
nullptr;
2976 GivenEltTy = ParseType();
2983 TokError(
"expected '>' at end of list element type");
2989 const RecTy *EltTy =
nullptr;
2990 for (
const Init *V : Vals) {
2996 TokError(
"Incompatible types in list elements");
3000 EltTy = TArg->getType();
3009 TokError(
"Incompatible types in list elements");
3026 TokError(Twine(
"Element type mismatch for list: element type '") +
3032 DeducedEltTy = EltTy;
3043 TokError(
"expected identifier or list of value types in dag init");
3047 const Init *
Operator = ParseValue(CurRec);
3052 const StringInit *OperatorName =
nullptr;
3055 TokError(
"expected variable name in dag operator");
3064 ParseDagArgList(DagArgs, CurRec);
3065 if (DagArgs.
empty())
3070 TokError(
"expected ')' in dag init");
3088const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3090 SMLoc LHSLoc = Lex.getLoc();
3091 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3097 switch (Lex.getCode()) {
3101 if (
Mode == ParseNameMode)
3105 SMLoc CurlyLoc = Lex.getLoc();
3107 SmallVector<unsigned, 16>
Ranges;
3108 ParseRangeList(Ranges);
3116 Error(CurlyLoc,
"Invalid bit range for value");
3122 TokError(
"expected '}' at end of bit range list");
3130 Error(LHSLoc,
"Invalid value, list expected");
3137 "' is invalid, list expected");
3142 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3151 LHSTy->getElementType())
3159 TokError(
"expected ']' at end of list slice");
3166 TokError(
"expected field identifier after '.'");
3169 SMRange FieldNameLoc = Lex.getLocRange();
3170 const StringInit *FieldName =
3172 if (!
Result->getFieldType(FieldName)) {
3173 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3174 Result->getAsString() +
"'");
3179 if (TrackReferenceLocs) {
3181 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3182 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3185 for (
const Record *R : RecTy->getClasses())
3186 if (
const auto *RV =
R->getValue(FieldName))
3187 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3198 SMLoc PasteLoc = Lex.getLoc();
3201 Error(PasteLoc,
"LHS of paste is not typed!");
3209 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3211 switch (Lex.getCode()) {
3218 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3235 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3241 const TypedInit *
RHS =
nullptr;
3244 switch (Lex.getCode()) {
3257 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3262 Error(PasteLoc,
"RHS of paste is not typed!");
3272 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3293void TGParser::ParseDagArgList(
3306 const Init *Val = ParseValue(CurRec);
3313 const StringInit *
VarName =
nullptr;
3316 TokError(
"expected variable name in dag literal");
3324 Result.emplace_back(Val, VarName);
3339 Result.push_back(ParseValue(CurRec, ItemType));
3349 Result.push_back(ParseValue(CurRec, ItemType));
3365bool TGParser::ParseTemplateArgValueList(
3368 assert(
Result.empty() &&
"Result vector is not empty");
3374 bool HasNamedArg =
false;
3375 unsigned ArgIndex = 0;
3377 if (ArgIndex >= TArgs.
size()) {
3378 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3385 const Init *
Value = ParseValue(
3387 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3394 return Error(ValueLoc,
3395 "The name of named argument should be a valid identifier");
3399 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3401 return Error(ValueLoc,
3402 "Argument " +
Name->getAsString() +
" doesn't exist");
3405 ValueLoc = Lex.getLoc();
3406 Value = ParseValue(CurRec, NamedArg->getType());
3409 return Error(ValueLoc,
3410 "The value of named argument should be initialized, "
3412 Value->getAsString() +
"'");
3419 return Error(ValueLoc,
3420 "Positional argument should be put before named argument");
3428 return TokError(
"Expected comma before next argument");
3443const Init *TGParser::ParseDeclaration(
Record *CurRec,
3444 bool ParsingTemplateArgs) {
3448 const RecTy *
Type = ParseType();
3453 TokError(
"Expected identifier in declaration");
3457 std::string Str = Lex.getCurStrVal();
3458 if (Str ==
"NAME") {
3459 TokError(
"'" + Str +
"' is a reserved variable name");
3463 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3464 TokError(
"local variable of this name already exists");
3468 SMLoc IdLoc = Lex.getLoc();
3473 if (!ParsingTemplateArgs) {
3474 BadField = AddValue(CurRec, IdLoc,
3475 RecordVal(DeclName, IdLoc,
Type,
3478 }
else if (CurRec) {
3481 AddValue(CurRec, IdLoc,
3484 assert(CurMultiClass &&
"invalid context for template argument");
3487 AddValue(CurRec, IdLoc,
3495 SMLoc ValLoc = Lex.getLoc();
3496 const Init *Val = ParseValue(CurRec,
Type);
3498 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3519TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3521 TokError(
"Expected identifier in foreach declaration");
3530 TokError(
"Expected '=' in foreach declaration");
3534 const RecTy *IterType =
nullptr;
3535 SmallVector<unsigned, 16>
Ranges;
3537 switch (Lex.getCode()) {
3540 ParseRangeList(Ranges);
3542 TokError(
"expected '}' at end of bit range list");
3549 SMLoc ValueLoc = Lex.getLoc();
3550 const Init *
I = ParseValue(
nullptr);
3556 ForeachListValue =
I;
3562 if (ParseRangePiece(Ranges, TI))
3567 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3568 if (CurMultiClass) {
3569 PrintNote({},
"references to multiclass template arguments cannot be "
3570 "resolved at this time");
3577 assert(!IterType &&
"Type already initialized?");
3579 std::vector<Init *> Values;
3580 for (
unsigned R : Ranges)
3598bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3602 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3605 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3613 SMLoc Loc = Lex.getLoc();
3614 TemplArg = ParseDeclaration(CurRec,
true );
3619 return Error(Loc,
"template argument with the same name has already been "
3626 return TokError(
"expected '>' at end of template argument list");
3643 SMLoc Loc = Lex.getLoc();
3645 std::string CurStr = Lex.getCurStrVal();
3647 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3657 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3675bool TGParser::ParseBodyItem(
Record *CurRec) {
3677 return ParseAssert(
nullptr, CurRec);
3680 return ParseDefvar(CurRec);
3683 return ParseDump(
nullptr, CurRec);
3686 if (!ParseDeclaration(CurRec,
false))
3690 return TokError(
"expected ';' after declaration");
3698 return TokError(
"expected field identifier after let");
3700 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3703 SmallVector<unsigned, 16> BitList;
3704 if (ParseOptionalRangeList(BitList))
3706 std::reverse(BitList.
begin(), BitList.
end());
3709 return TokError(
"expected '=' in let expression");
3713 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3722 const Init *Val = ParseValue(CurRec,
Type);
3727 return TokError(
"expected ';' after let expression");
3729 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3740bool TGParser::ParseBody(
Record *CurRec) {
3746 return TokError(
"Expected '{' to start body or ';' for declaration only");
3749 if (ParseBodyItem(CurRec))
3756 SMLoc SemiLoc = Lex.getLoc();
3758 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3759 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3767bool TGParser::ApplyLetStack(
Record *CurRec) {
3768 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3769 for (LetRecord &LR : LetInfo)
3770 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3780 return ApplyLetStack(
Entry.Rec.get());
3783 if (
Entry.Assertion)
3790 for (
auto &
E :
Entry.Loop->Entries) {
3791 if (ApplyLetStack(
E))
3807bool TGParser::ParseObjectBody(
Record *CurRec) {
3809 TGVarScope *ObjectScope =
PushScope(CurRec);
3814 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3821 if (AddSubClass(CurRec, SubClass))
3826 SubClass = ParseSubClassReference(CurRec,
false);
3830 if (ApplyLetStack(CurRec))
3833 bool Result = ParseBody(CurRec);
3843bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3844 SMLoc DefLoc = Lex.getLoc();
3851 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
3854 std::unique_ptr<Record> CurRec;
3855 const Init *
Name = ParseObjectName(CurMultiClass);
3860 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
3863 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
3866 if (ParseObjectBody(CurRec.get()))
3869 return addEntry(std::move(CurRec));
3876bool TGParser::ParseDefset() {
3881 Defset.Loc = Lex.getLoc();
3882 const RecTy *
Type = ParseType();
3890 return TokError(
"expected identifier");
3891 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
3892 if (Records.getGlobal(DeclName->
getValue()))
3893 return TokError(
"def or global variable of this name already exists");
3899 SMLoc BraceLoc = Lex.getLoc();
3902 Defsets.push_back(&Defset);
3903 bool Err = ParseObjectList(
nullptr);
3909 TokError(
"expected '}' at end of defset");
3910 return Error(BraceLoc,
"to match this '{'");
3913 Records.addExtraGlobal(DeclName->
getValue(),
3922bool TGParser::ParseDeftype() {
3927 return TokError(
"expected identifier");
3929 const std::string
TypeName = Lex.getCurStrVal();
3930 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
3931 return TokError(
"type of this name '" + TypeName +
"' already exists");
3937 SMLoc Loc = Lex.getLoc();
3938 const RecTy *
Type = ParseType();
3943 return Error(Loc,
"cannot define type alias for class type '" +
3944 Type->getAsString() +
"'");
3958bool TGParser::ParseDefvar(
Record *CurRec) {
3963 return TokError(
"expected identifier");
3964 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
3965 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
3966 return TokError(
"local variable of this name already exists");
3971 if (V && !
V->isTemplateArg())
3972 return TokError(
"field of this name already exists");
3977 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
3978 return TokError(
"def or global variable of this name already exists");
3984 const Init *
Value = ParseValue(CurRec);
3991 if (!CurScope->isOutermost())
4005bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4006 SMLoc Loc = Lex.getLoc();
4012 const Init *ListValue =
nullptr;
4013 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4015 return TokError(
"expected declaration in for");
4021 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4023 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4024 Loops.push_back(std::move(TheLoop));
4028 if (ParseObject(CurMultiClass))
4031 SMLoc BraceLoc = Lex.getLoc();
4036 if (ParseObjectList(CurMultiClass))
4040 TokError(
"expected '}' at end of foreach command");
4041 return Error(BraceLoc,
"to match this '{'");
4048 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4051 return addEntry(std::move(Loop));
4059bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4060 SMLoc Loc = Lex.getLoc();
4066 const Init *Condition = ParseValue(
nullptr);
4080 const ListInit *SingletonList =
4086 const Init *ThenClauseList =
4090 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4092 if (ParseIfBody(CurMultiClass,
"then"))
4095 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4098 if (addEntry(std::move(Loop)))
4108 const Init *ElseClauseList =
4113 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4115 if (ParseIfBody(CurMultiClass,
"else"))
4118 Loop = std::move(Loops.back());
4121 if (addEntry(std::move(Loop)))
4139 if (ParseObject(CurMultiClass))
4142 SMLoc BraceLoc = Lex.getLoc();
4147 if (ParseObjectList(CurMultiClass))
4151 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4152 return Error(BraceLoc,
"to match this '{'");
4167 SMLoc ConditionLoc = Lex.getLoc();
4168 const Init *Condition = ParseValue(CurRec);
4173 TokError(
"expected ',' in assert statement");
4177 const Init *Message = ParseValue(CurRec);
4185 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4187 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4196bool TGParser::ParseClass() {
4201 return TokError(
"expected class name after 'class' keyword");
4203 const std::string &
Name = Lex.getCurStrVal();
4204 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4211 "' already defined");
4216 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4218 CurRec = NewRec.get();
4219 Records.addClass(std::move(NewRec));
4222 if (TypeAliases.count(Name))
4223 return TokError(
"there is already a defined type alias '" + Name +
"'");
4228 TGVarScope *ClassScope =
PushScope(CurRec);
4231 if (ParseTemplateArgList(CurRec))
4234 if (ParseObjectBody(CurRec))
4237 if (!NoWarnOnUnusedTemplateArgs)
4253 TokError(
"expected identifier in let definition");
4258 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4262 SmallVector<unsigned, 16>
Bits;
4263 if (ParseOptionalRangeList(Bits)) {
4267 std::reverse(
Bits.begin(),
Bits.end());
4270 TokError(
"expected '=' in let expression");
4275 const Init *Val = ParseValue(
nullptr);
4282 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4292bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4298 ParseLetList(LetInfo);
4299 if (LetInfo.
empty())
4301 LetStack.push_back(std::move(LetInfo));
4304 return TokError(
"expected 'in' at end of top-level 'let'");
4309 if (ParseObject(CurMultiClass))
4312 SMLoc BraceLoc = Lex.getLoc();
4320 if (ParseObjectList(CurMultiClass))
4324 TokError(
"expected '}' at end of top level let command");
4325 return Error(BraceLoc,
"to match this '{'");
4332 LetStack.pop_back();
4349bool TGParser::ParseMultiClass() {
4354 return TokError(
"expected identifier after multiclass for name");
4355 std::string
Name = Lex.getCurStrVal();
4357 auto Result = MultiClasses.try_emplace(
4358 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4361 return TokError(
"multiclass '" + Name +
"' already defined");
4363 CurMultiClass =
Result.first->second.get();
4367 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4371 if (ParseTemplateArgList(
nullptr))
4374 bool inherits =
false;
4381 SubMultiClassReference SubMultiClass =
4382 ParseSubMultiClassReference(CurMultiClass);
4385 if (!SubMultiClass.
MC)
4389 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4394 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4400 return TokError(
"expected '{' in multiclass definition");
4402 return TokError(
"expected ';' in multiclass definition");
4405 return TokError(
"multiclass must contain at least one def");
4408 switch (Lex.getCode()) {
4410 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4411 "'foreach', 'if', or 'let' in multiclass body");
4421 if (ParseObject(CurMultiClass))
4429 SMLoc SemiLoc = Lex.getLoc();
4431 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4432 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4436 if (!NoWarnOnUnusedTemplateArgs)
4437 CurMultiClass->Rec.checkUnusedTemplateArgs();
4440 CurMultiClass =
nullptr;
4448bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4452 const Init *DefmName = ParseObjectName(CurMultiClass);
4456 DefmName = Records.getNewAnonymousName();
4465 return TokError(
"expected ':' after defm identifier");
4468 std::vector<RecordsEntry> NewEntries;
4471 bool InheritFromClass =
false;
4476 SMLoc SubClassLoc = Lex.getLoc();
4477 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4487 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4488 assert(MC &&
"Didn't lookup multiclass correctly?");
4491 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4495 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4496 &NewEntries, &SubClassLoc))
4503 return TokError(
"expected identifier");
4505 SubClassLoc = Lex.getLoc();
4509 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4511 if (InheritFromClass)
4514 Ref = ParseSubClassReference(
nullptr,
true);
4517 if (InheritFromClass) {
4520 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4528 for (
auto &
E : NewEntries) {
4530 if (AddSubClass(
E, SubClass))
4536 SubClass = ParseSubClassReference(
nullptr,
false);
4540 for (
auto &
E : NewEntries) {
4541 if (ApplyLetStack(
E))
4544 addEntry(std::move(
E));
4548 return TokError(
"expected ';' at end of defm");
4566 switch (Lex.getCode()) {
4569 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4571 return ParseAssert(MC);
4573 return ParseDef(MC);
4575 return ParseDefm(MC);
4577 return ParseDeftype();
4579 return ParseDefvar();
4581 return ParseDump(MC);
4583 return ParseForeach(MC);
4587 return ParseTopLevelLet(MC);
4590 return TokError(
"defset is not allowed inside multiclass");
4591 return ParseDefset();
4594 return TokError(
"class is not allowed inside multiclass");
4596 return TokError(
"class is not allowed inside foreach loop");
4597 return ParseClass();
4600 return TokError(
"multiclass is not allowed inside foreach loop");
4601 return ParseMultiClass();
4607bool TGParser::ParseObjectList(
MultiClass *MC) {
4609 if (ParseObject(MC))
4618 if (ParseObjectList())
4626 return TokError(
"Unexpected token at top level");
4633bool TGParser::CheckTemplateArgValues(
4637 "expected as many values as locations");
4641 bool HasError =
false;
4643 const Init *ArgName =
nullptr;
4644 if (
Value->isPositional())
4645 ArgName = TArgs[
Value->getIndex()];
4646 if (
Value->isNamed())
4653 auto *CastValue = ArgValue->
getCastTo(ArgType);
4657 "result of template arg value cast has wrong type");
4661 Loc,
"Value specified for template argument '" +
4663 ArgValue->getType()->
getAsString() +
"; expected type " +
4672#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4681 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4682 <<
ListValue->getAsString() <<
" in {\n";
4691 errs() <<
"Record:\n";
4694 errs() <<
"Defs:\n";
4706 const Init *Message = ParseValue(CurRec);
4726 addEntry(std::make_unique<Record::DumpInfo>(
Loc, ResolvedMessage));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
PowerPC Reduce CR logical Operation
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static bool checkBitsConcrete(Record &R, const RecordVal &RV)
static const Init * QualifyName(const Record &CurRec, const Init *Name)
Return an Init with a qualifier prefix referring to CurRec's name.
static const Init * QualifiedNameOfImplicitName(const Record &Rec)
Return the qualified version of the implicit 'NAME' template argument.
static void checkConcrete(Record &R)
static SymbolRef::Type getType(const Symbol *Sym)
static const ArgumentInit * get(const Init *Value, ArgAuxType Aux)
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)
LetMode
Specifies how a 'let' assignment interacts with the existing field value.
bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
void PrintNote(const Twine &Msg)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_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
Parsed let mode keyword and field name (e.g.
std::vector< RecordsEntry > Entries
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
std::unique_ptr< ForeachLoop > Loop
std::unique_ptr< Record::AssertionInfo > Assertion
std::unique_ptr< Record::DumpInfo > Dump
std::unique_ptr< Record > Rec
SubClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs
SubMultiClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs