17#include "llvm/Config/llvm-config.h"
56#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
58 errs() <<
"Multiclass:\n";
62 errs() <<
"Template args:\n";
71 const auto *BV = cast<BitsInit>(RV.
getValue());
72 for (
unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
73 const Init *Bit = BV->getBit(i);
74 bool IsReference =
false;
75 if (
const auto *VBI = dyn_cast<VarBitInit>(Bit)) {
76 if (
const auto *VI = dyn_cast<VarInit>(VBI->getBitVar())) {
77 if (R.getValue(VI->getName()))
80 }
else if (isa<VarInit>(Bit)) {
83 if (!(IsReference || Bit->isConcrete()))
90 for (
const RecordVal &RV : R.getValues()) {
95 if (RV.isNonconcreteOK())
98 if (
const Init *V = RV.getValue()) {
102 Twine(
"Initializer of '") + RV.getNameInitAsString() +
103 "' in '" + R.getNameInitAsString() +
104 "' could not be fully resolved: " +
105 RV.getValue()->getAsString());
120 if (
const auto *BinOp = dyn_cast<BinOpInit>(NewName))
121 NewName = BinOp->Fold(&CurRec);
141 bool TrackReferenceLocs)
const {
143 auto It = Vars.find(
Name->getValue());
144 if (It != Vars.end())
147 auto FindValueInArgs = [&](
Record *Rec,
154 assert(RV &&
"Template arg doesn't exist??");
156 if (TrackReferenceLocs)
160 return Name->getValue() ==
"NAME"
174 if (TrackReferenceLocs)
175 RV->addReferenceLoc(NameLoc);
181 if (
auto *V = FindValueInArgs(CurRec,
Name))
189 const auto *IterVar = dyn_cast<VarInit>(CurLoop->
IterVar);
190 if (IterVar && IterVar->getNameInit() ==
Name)
198 if (
auto *V = FindValueInArgs(&CurMultiClass->
Rec,
Name))
206 return Parent->getVar(Records, ParsingMultiClass,
Name, NameLoc,
214 CurRec = &CurMultiClass->
Rec;
219 return Error(Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
221 "previous definition of type '" +
222 ERV->getType()->getAsString() +
"'");
233 bool AllowSelfAssignment,
bool OverrideDefLoc) {
234 if (!V)
return false;
236 if (!CurRec) CurRec = &CurMultiClass->
Rec;
246 if (
const auto *VI = dyn_cast<VarInit>(V))
247 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
248 return Error(Loc,
"Recursion / self-assignment forbidden");
254 if (!BitList.
empty()) {
255 const auto *CurVal = dyn_cast<BitsInit>(RV->
getValue());
258 "' is not a bits type");
263 return Error(Loc,
"Initializer is not compatible with bit range");
268 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
269 unsigned Bit = BitList[i];
271 return Error(Loc,
"Cannot set bit #" +
Twine(Bit) +
" of value '" +
276 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
278 NewBits[i] = CurVal->
getBit(i);
284 std::string InitType;
285 if (
const auto *BI = dyn_cast<BitsInit>(V))
286 InitType = (
Twine(
"' of type bit initializer with length ") +
287 Twine(BI->getNumBits())).str();
288 else if (
const auto *TI = dyn_cast<TypedInit>(V))
289 InitType = (
Twine(
"' of type '") + TI->getType()->getAsString()).str();
292 "' is incompatible with value '" +
293 V->getAsString() + InitType +
"'");
307 if (!
Field.isTemplateArg())
311 if (resolveArgumentsOfClass(R, SC, SubClass.
TemplateArgs,
333 for (
const auto &[SC, Loc] :
SC->getSuperClasses()) {
336 "Already subclass of '" +
SC->getName() +
"'!\n");
342 "Already subclass of '" +
SC->getName() +
"'!\n");
349 return AddSubClass(
Entry.Rec.get(), SubClass);
354 for (
auto &E :
Entry.Loop->Entries) {
355 if (AddSubClass(E, SubClass))
365bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
370 if (resolveArgumentsOfMultiClass(
384 "RecordsEntry has invalid number of items");
387 if (!Loops.empty()) {
388 Loops.back()->Entries.push_back(std::move(E));
395 return resolve(*E.
Loop, Stack, CurMultiClass ==
nullptr,
396 CurMultiClass ? &CurMultiClass->
Entries :
nullptr);
401 CurMultiClass->
Entries.push_back(std::move(E));
417 return addDefOne(std::move(E.
Rec));
426 bool Final, std::vector<RecordsEntry> *Dest,
430 for (
const auto &S : Substs)
431 R.set(S.first, S.second);
432 const Init *
List =
Loop.ListValue->resolveReferences(R);
440 if (
const auto *TI = dyn_cast<TernOpInit>(
List);
442 const Init *OldLHS = TI->getLHS();
447 Twine(
"unable to resolve if condition '") +
448 LHS->getAsString() +
"' at end of containing scope");
451 const Init *MHS = TI->getMHS();
452 const Init *
RHS = TI->getRHS();
457 const auto *LI = dyn_cast<ListInit>(
List);
460 Dest->emplace_back(std::make_unique<ForeachLoop>(
Loop.Loc,
Loop.IterVar,
462 return resolve(
Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
467 List->getAsString() +
"', expected a list");
472 for (
auto *Elt : *LI) {
474 Substs.emplace_back(
Loop.IterVar->getNameInit(), Elt);
475 Error = resolve(
Loop.Entries, Substs, Final, Dest);
489bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
490 SubstStack &Substs,
bool Final,
491 std::vector<RecordsEntry> *Dest,
SMLoc *Loc) {
493 for (
auto &E : Source) {
495 Error = resolve(*E.
Loop, Substs, Final, Dest);
499 for (
const auto &S : Substs)
500 R.set(S.first, S.second);
501 const Init *Condition = E.
Assertion->Condition->resolveReferences(R);
502 const Init *Message = E.
Assertion->Message->resolveReferences(R);
505 Dest->push_back(std::make_unique<Record::AssertionInfo>(
512 for (
const auto &S : Substs)
513 R.set(S.first, S.second);
514 const Init *Message = E.
Dump->Message->resolveReferences(R);
518 std::make_unique<Record::DumpInfo>(E.
Dump->Loc, Message));
523 auto Rec = std::make_unique<Record>(*E.
Rec);
525 Rec->appendLoc(*Loc);
528 for (
const auto &S : Substs)
529 R.set(S.first, S.second);
530 Rec->resolveReferences(R);
533 Dest->push_back(std::move(Rec));
535 Error = addDefOne(std::move(Rec));
544bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
545 const Init *NewName =
nullptr;
546 if (
const Record *Prev = Records.
getDef(Rec->getNameInitAsString())) {
547 if (!Rec->isAnonymous()) {
549 "def already exists: " + Rec->getNameInitAsString());
550 PrintNote(Prev->getLoc(),
"location of previous definition");
559 if (!isa<StringInit>(Rec->getNameInit())) {
561 Rec->getNameInit()->getAsString() +
562 "' could not be fully resolved");
567 Rec->checkRecordAssertions();
570 Rec->emitRecordDumps();
573 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
577 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
578 PrintError(Rec->getLoc(),
Twine(
"adding record of incompatible type '") +
579 I->getType()->getAsString() +
587 Records.
addDef(std::move(Rec));
591bool TGParser::resolveArguments(
const Record *Rec,
593 SMLoc Loc, ArgValueHandler ArgValueHandler) {
596 "Too many template arguments allowed");
600 for (
auto *Arg : ArgValues) {
601 const Init *ArgName =
nullptr;
602 const Init *ArgValue = Arg->getValue();
603 if (Arg->isPositional())
604 ArgName = ArgNames[Arg->getIndex()];
606 ArgName = Arg->getName();
610 return Error(Loc,
"We can only specify the template argument '" +
613 ArgValueHandler(ArgName, ArgValue);
618 for (
auto *UnsolvedArgName : UnsolvedArgNames) {
621 std::string
Name = UnsolvedArgName->getAsUnquotedString();
622 Error(Loc,
"value not specified for template argument '" +
Name +
"'");
627 ArgValueHandler(UnsolvedArgName,
Default);
638 return resolveArguments(
645bool TGParser::resolveArgumentsOfMultiClass(
650 return resolveArguments(&MC->
Rec, ArgValues, Loc,
652 Substs.emplace_back(Name, Value);
689 CurRec = &CurMultiClass->
Rec;
699 Name->resolveReferences(R);
713const Record *TGParser::ParseClassID() {
715 TokError(
"expected name for ClassID");
721 std::string Msg(
"Couldn't find class '" + Lex.
getCurStrVal() +
"'");
723 TokError(Msg +
". Use 'defm' if you meant to use multiclass '" +
727 }
else if (TrackReferenceLocs) {
742 TokError(
"expected name for MultiClassID");
761ParseSubClassReference(
Record *CurRec,
bool isDefm) {
769 Result.Rec = ParseClassID();
780 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
786 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
803ParseSubMultiClassReference(
MultiClass *CurMC) {
807 Result.MC = ParseMultiClassID();
817 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, &CurMC->
Rec,
839 auto LHSLoc = Lex.
getLoc();
840 auto *CurVal = ParseValue(CurRec);
843 const auto *
LHS = cast<TypedInit>(CurVal);
850 auto RHSLoc = Lex.
getLoc();
851 CurVal = ParseValue(CurRec);
854 RHS = cast<TypedInit>(CurVal);
865 TokError(
"invalid range, cannot be negative");
902const TypedInit *TGParser::ParseSliceElements(
Record *CurRec,
bool Single) {
907 auto FlushElems = [&] {
908 if (!Elems.
empty()) {
915 auto LHSLoc = Lex.
getLoc();
916 CurVal = ParseSliceElement(CurRec);
919 auto *CurValTy = CurVal->
getType();
921 if (
const auto *ListValTy = dyn_cast<ListRecTy>(CurValTy)) {
922 if (!isa<IntRecTy>(ListValTy->getElementType())) {
924 "expected list<int>, got " +
Twine(ListValTy->getAsString()));
932 }
else if (!isa<IntRecTy>(CurValTy)) {
934 "unhandled type " +
Twine(CurValTy->getAsString()) +
" in range");
962 for (
auto *Slice : Slices) {
978 const Init *CurVal = FirstItem;
980 CurVal = ParseValue(
nullptr);
982 const auto *
II = dyn_cast_or_null<IntInit>(CurVal);
984 return TokError(
"expected integer or bitrange");
986 int64_t Start =
II->getValue();
990 return TokError(
"invalid range, cannot be negative");
1001 const Init *I_End = ParseValue(
nullptr);
1002 const auto *II_End = dyn_cast_or_null<IntInit>(I_End);
1004 TokError(
"expected integer value as end of range");
1008 End = II_End->getValue();
1018 return TokError(
"invalid range, cannot be negative");
1022 for (; Start <=
End; ++Start)
1025 for (; Start >=
End; --Start)
1036 if (ParseRangePiece(Result)) {
1042 if (ParseRangePiece(Result)) {
1057 ParseRangeList(Ranges);
1058 if (
Ranges.empty())
return true;
1061 TokError(
"expected '>' at end of range list");
1062 return Error(StartLoc,
"to match this '<'");
1076 ParseRangeList(Ranges);
1077 if (
Ranges.empty())
return true;
1080 TokError(
"expected '}' at end of bit list");
1081 return Error(StartLoc,
"to match this '{'");
1097const RecTy *TGParser::ParseType() {
1099 default:
TokError(
"Unknown token when expecting a type");
return nullptr;
1115 if (
I != TypeAliases.end()) {
1119 if (
const Record *R = ParseClassID())
1126 TokError(
"expected '<' after bits type");
1130 TokError(
"expected integer in bits<n> type");
1135 TokError(
"expected '>' at end of bits<n> type");
1143 TokError(
"expected '<' after list type");
1147 const RecTy *SubType = ParseType();
1148 if (!SubType)
return nullptr;
1151 TokError(
"expected '>' at end of list<ty> type");
1161 SMRange NameLoc, IDParseMode Mode) {
1162 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass,
Name, NameLoc,
1163 TrackReferenceLocs))
1166 if (Mode == ParseNameMode)
1171 if (TrackReferenceLocs) {
1172 if (
const auto *Def = dyn_cast<DefInit>(
I))
1173 Def->getDef()->appendReferenceLoc(NameLoc);
1180 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1184 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1192const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1219 Type = ParseOperatorType();
1222 TokError(
"did not get type for unary operator");
1281 Type = ParseOperatorType();
1284 TokError(
"did not get type for unary operator");
1288 if (!isa<RecordRecTy>(
Type)) {
1289 TokError(
"type for !getdagop must be a record type");
1304 TokError(
"expected '(' after unary operator");
1308 const Init *
LHS = ParseValue(CurRec);
1309 if (!LHS)
return nullptr;
1312 const auto *LHSl = dyn_cast<ListInit>(LHS);
1313 const auto *LHSs = dyn_cast<StringInit>(LHS);
1314 const auto *LHSd = dyn_cast<DagInit>(LHS);
1315 const auto *LHSt = dyn_cast<TypedInit>(LHS);
1316 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1317 TokError(
"expected string, list, or dag type argument in unary operator");
1321 if (!isa<ListRecTy, StringRecTy, DagRecTy>(LHSt->getType())) {
1322 TokError(
"expected string, list, or dag type argument in unary operator");
1330 const auto *LHSl = dyn_cast<ListInit>(LHS);
1331 const auto *LHSt = dyn_cast<TypedInit>(LHS);
1332 if (!LHSl && !LHSt) {
1333 TokError(
"expected list type argument in unary operator");
1337 if (!isa<ListRecTy>(LHSt->getType())) {
1338 TokError(
"expected list type argument in unary operator");
1343 if (LHSl && LHSl->empty()) {
1344 TokError(
"empty list argument in unary operator");
1347 bool UseElementType =
1350 const Init *Item = LHSl->getElement(0);
1351 const auto *Itemt = dyn_cast<TypedInit>(Item);
1353 TokError(
"untyped list element in unary operator");
1356 Type = UseElementType ? Itemt->getType()
1359 assert(LHSt &&
"expected list type argument in unary operator");
1360 const auto *LType = dyn_cast<ListRecTy>(LHSt->getType());
1361 Type = UseElementType ? LType->getElementType() : LType;
1367 const auto *InnerListTy = dyn_cast<ListRecTy>(
Type);
1379 TokError(
"expected ')' in unary operator");
1389 const RecTy *
Type = ParseOperatorType();
1394 TokError(
"expected '(' after type of !isa");
1398 const Init *
LHS = ParseValue(CurRec);
1414 const RecTy *
Type = ParseOperatorType();
1419 TokError(
"expected '(' after type of !exists");
1424 const Init *Expr = ParseValue(CurRec);
1428 const auto *ExprType = dyn_cast<TypedInit>(Expr);
1430 Error(ExprLoc,
"expected string type argument in !exists operator");
1434 const auto *RecType = dyn_cast<RecordRecTy>(ExprType->getType());
1437 "expected string type argument in !exists operator, please "
1438 "use !isa instead");
1442 const auto *SType = dyn_cast<StringRecTy>(ExprType->getType());
1444 Error(ExprLoc,
"expected string type argument in !exists operator");
1449 TokError(
"expected ')' in !exists");
1522 const RecTy *ArgType =
nullptr;
1532 Type = ParseOperatorType();
1534 TokError(
"did not get type for !getdagarg operator");
1585 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1586 Error(OpLoc,
Twine(
"expected value of type '") +
1588 Type->getAsString() +
"'");
1593 TokError(
"expected '(' after binary operator");
1603 InitList.
push_back(ParseValue(CurRec, ArgType));
1604 if (!InitList.
back())
return nullptr;
1606 const auto *InitListBack = dyn_cast<TypedInit>(InitList.
back());
1607 if (!InitListBack) {
1608 Error(OpLoc,
Twine(
"expected value to be a typed value, got '" +
1609 InitList.
back()->getAsString() +
"'"));
1612 const RecTy *ListType = InitListBack->getType();
1620 if (!isa<ListRecTy>(ArgType)) {
1621 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1627 if (ItemType && InitList.
size() == 1) {
1628 if (!isa<ListRecTy>(ItemType)) {
1630 Twine(
"expected output type to be a list, got type '") +
1635 Error(OpLoc,
Twine(
"expected first arg type to be '") +
1637 "', got value of type '" +
1638 cast<ListRecTy>(ItemType)
1645 if (InitList.
size() == 2 && !isa<IntRecTy>(ArgType)) {
1646 Error(InitLoc,
Twine(
"expected second parameter to be an int, got "
1647 "value of type '") +
1654 if (!isa<ListRecTy>(ArgType)) {
1655 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1665 Error(InitLoc,
Twine(
"expected bit, bits, int, string, or record; "
1666 "got value of type '") +
1679 Error(InitLoc,
Twine(
"expected bit, bits, int, or string; "
1680 "got value of type '") +
1686 switch (InitList.
size()) {
1691 Error(InitLoc,
Twine(
"expected list of string, int, bits, or bit; "
1692 "got value of type '") +
1698 if (!isa<StringRecTy>(ArgType)) {
1699 Error(InitLoc,
Twine(
"expected second argument to be a string, "
1700 "got value of type '") +
1716 Error(InitLoc,
Twine(
"expected value of type '") +
1755 TokError(
"expected ')' in operator");
1764 Type = cast<TypedInit>(InitList.
front())->getType()->getListTy();
1775 while (InitList.
size() > 2) {
1782 if (InitList.
size() == 2)
1786 Error(OpLoc,
"expected two operands to operator");
1792 return ParseOperationForEachFilter(CurRec, ItemType);
1800 TokError(
"expected '(' after !range operator");
1805 bool FirstArgIsList =
false;
1807 if (
Args.size() >= 3) {
1808 TokError(
"expected at most three values of integer");
1813 Args.push_back(ParseValue(CurRec));
1817 const auto *ArgBack = dyn_cast<TypedInit>(
Args.back());
1819 Error(OpLoc,
Twine(
"expected value to be a typed value, got '" +
1820 Args.back()->getAsString() +
"'"));
1824 const RecTy *ArgBackType = ArgBack->getType();
1825 if (!FirstArgIsList ||
Args.size() == 1) {
1826 if (
Args.size() == 1 && isa<ListRecTy>(ArgBackType)) {
1827 FirstArgIsList =
true;
1828 }
else if (isa<IntRecTy>(ArgBackType)) {
1831 if (
Args.size() != 1)
1832 Error(InitLoc,
Twine(
"expected value of type 'int', got '" +
1835 Error(InitLoc,
Twine(
"expected list or int, got value of type '") +
1841 assert(isa<ListRecTy>(cast<TypedInit>(Args[0])->
getType()));
1842 Error(InitLoc,
Twine(
"expected one list, got extra value of type '") +
1851 TokError(
"expected ')' in operator");
1856 auto ArgCount =
Args.size();
1858 const auto *Arg0 = cast<TypedInit>(Args[0]);
1859 const auto *Arg0Ty = Arg0->getType();
1860 if (ArgCount == 1) {
1861 if (isa<ListRecTy>(Arg0Ty)) {
1868 assert(isa<IntRecTy>(Arg0Ty));
1875 assert(isa<IntRecTy>(Arg0Ty));
1876 const auto *Arg1 = cast<TypedInit>(Args[1]);
1877 assert(isa<IntRecTy>(Arg1->getType()));
1880 if (ArgCount == 3) {
1882 const auto *Arg2 = cast<TypedInit>(Args[2]);
1883 assert(isa<IntRecTy>(Arg2->getType()));
1929 TokError(
"expected '(' after ternary operator");
1933 const Init *
LHS = ParseValue(CurRec);
1934 if (!LHS)
return nullptr;
1937 TokError(
"expected ',' in ternary operator");
1942 const Init *MHS = ParseValue(CurRec, ItemType);
1947 TokError(
"expected ',' in ternary operator");
1952 const Init *
RHS = ParseValue(CurRec, ItemType);
1957 TokError(
"expected ')' in binary operator");
1964 const auto *MHSt = dyn_cast<TypedInit>(MHS);
1965 if (!MHSt && !isa<UnsetInit>(MHS)) {
1966 Error(MHSLoc,
"could not determine type of the child list in !dag");
1969 if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1970 Error(MHSLoc,
Twine(
"expected list of children, got type '") +
1971 MHSt->getType()->getAsString() +
"'");
1975 const auto *RHSt = dyn_cast<TypedInit>(RHS);
1976 if (!RHSt && !isa<UnsetInit>(RHS)) {
1977 Error(RHSLoc,
"could not determine type of the name list in !dag");
1981 Error(RHSLoc,
Twine(
"expected list<string>, got type '") +
1982 RHSt->getType()->getAsString() +
"'");
1986 if (!MHSt && !RHSt) {
1988 "cannot have both unset children and unset names in !dag");
1994 const RecTy *MHSTy =
nullptr;
1995 const RecTy *RHSTy =
nullptr;
1997 if (
const auto *MHSt = dyn_cast<TypedInit>(MHS))
1998 MHSTy = MHSt->getType();
1999 if (
const auto *MHSbits = dyn_cast<BitsInit>(MHS))
2001 if (isa<BitInit>(MHS))
2004 if (
const auto *RHSt = dyn_cast<TypedInit>(RHS))
2005 RHSTy = RHSt->getType();
2006 if (
const auto *RHSbits = dyn_cast<BitsInit>(RHS))
2008 if (isa<BitInit>(RHS))
2012 if (isa<UnsetInit>(MHS))
2014 if (isa<UnsetInit>(RHS))
2017 if (!MHSTy || !RHSTy) {
2018 TokError(
"could not get type for !if");
2031 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2033 TokError(
"could not get type for !subst");
2036 Type = RHSt->getType();
2040 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2041 if (!MHSt || !isa<IntRecTy, StringRecTy>(MHSt->getType())) {
2042 Error(MHSLoc,
Twine(
"expected integer index or string name, got ") +
2043 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2051 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2052 if (!MHSt || !isa<IntRecTy, StringRecTy>(MHSt->getType())) {
2053 Error(MHSLoc,
Twine(
"expected integer index or string name, got ") +
2054 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2059 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2061 if (RHSt && !isa<StringRecTy>(RHSt->getType())) {
2062 Error(RHSLoc,
Twine(
"expected string or unset name, got type '") +
2063 RHSt->getType()->getAsString() +
"'");
2073 return ParseOperationSubstr(CurRec, ItemType);
2076 return ParseOperationFind(CurRec, ItemType);
2079 return ParseOperationCond(CurRec, ItemType);
2085 TokError(
"expected '(' after !foldl");
2089 const Init *StartUntyped = ParseValue(CurRec);
2093 const auto *Start = dyn_cast<TypedInit>(StartUntyped);
2101 TokError(
"expected ',' in !foldl");
2105 const Init *ListUntyped = ParseValue(CurRec);
2109 const auto *
List = dyn_cast<TypedInit>(ListUntyped);
2116 const auto *ListType = dyn_cast<ListRecTy>(
List->getType());
2118 TokError(
Twine(
"!foldl list must be a list, but is of type '") +
2119 List->getType()->getAsString());
2124 TokError(
"expected ',' in !foldl");
2129 TokError(
"third argument of !foldl must be an identifier");
2136 "' already defined")
2142 TokError(
"expected ',' in !foldl");
2147 TokError(
"fourth argument of !foldl must be an identifier");
2154 "' already defined")
2160 TokError(
"expected ',' in !foldl");
2167 std::unique_ptr<Record> ParseRecTmp;
2168 Record *ParseRec = CurRec;
2170 ParseRecTmp = std::make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
2171 ParseRec = ParseRecTmp.get();
2178 const Init *ExprUntyped = ParseValue(ParseRec);
2179 ParseRec->removeValue(
A);
2180 ParseRec->removeValue(
B);
2185 const auto *Expr = dyn_cast<TypedInit>(ExprUntyped);
2187 TokError(
"could not get type of !foldl expression");
2191 if (Expr->getType() != Start->getType()) {
2192 TokError(
Twine(
"!foldl expression must be of same type as start (") +
2193 Start->getType()->getAsString() +
"), but is of type " +
2199 TokError(
"expected ')' in fold operator");
2214const RecTy *TGParser::ParseOperatorType() {
2218 TokError(
"expected type name for operator");
2223 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2228 TokError(
"expected type name for operator");
2233 TokError(
"expected type name for operator");
2243const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2244 const RecTy *ItemType) {
2251 TokError(
"expected '(' after !substr operator");
2255 const Init *
LHS = ParseValue(CurRec);
2260 TokError(
"expected ',' in !substr operator");
2265 const Init *MHS = ParseValue(CurRec);
2273 RHS = ParseValue(CurRec);
2281 TokError(
"expected ')' in !substr operator");
2285 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2286 Error(RHSLoc,
Twine(
"expected value of type '") +
2288 Type->getAsString() +
"'");
2291 const auto *LHSt = dyn_cast<TypedInit>(LHS);
2292 if (!LHSt && !isa<UnsetInit>(LHS)) {
2293 TokError(
"could not determine type of the string in !substr");
2296 if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
2298 LHSt->getType()->getAsString() +
"'");
2302 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2303 if (!MHSt && !isa<UnsetInit>(MHS)) {
2304 TokError(
"could not determine type of the start position in !substr");
2307 if (MHSt && !isa<IntRecTy>(MHSt->getType())) {
2308 Error(MHSLoc,
Twine(
"expected int, got type '") +
2309 MHSt->getType()->getAsString() +
"'");
2314 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2315 if (!RHSt && !isa<UnsetInit>(RHS)) {
2316 TokError(
"could not determine type of the length in !substr");
2319 if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
2321 RHSt->getType()->getAsString() +
"'");
2332const Init *TGParser::ParseOperationFind(
Record *CurRec,
2333 const RecTy *ItemType) {
2340 TokError(
"expected '(' after !find operator");
2344 const Init *
LHS = ParseValue(CurRec);
2349 TokError(
"expected ',' in !find operator");
2354 const Init *MHS = ParseValue(CurRec);
2362 RHS = ParseValue(CurRec);
2370 TokError(
"expected ')' in !find operator");
2374 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2375 Error(RHSLoc,
Twine(
"expected value of type '") +
2377 Type->getAsString() +
"'");
2380 const auto *LHSt = dyn_cast<TypedInit>(LHS);
2381 if (!LHSt && !isa<UnsetInit>(LHS)) {
2382 TokError(
"could not determine type of the source string in !find");
2385 if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
2387 LHSt->getType()->getAsString() +
"'");
2391 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2392 if (!MHSt && !isa<UnsetInit>(MHS)) {
2393 TokError(
"could not determine type of the target string in !find");
2396 if (MHSt && !isa<StringRecTy>(MHSt->getType())) {
2397 Error(MHSLoc,
Twine(
"expected string, got type '") +
2398 MHSt->getType()->getAsString() +
"'");
2403 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2404 if (!RHSt && !isa<UnsetInit>(RHS)) {
2405 TokError(
"could not determine type of the start position in !find");
2408 if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
2410 RHSt->getType()->getAsString() +
"'");
2422const Init *TGParser::ParseOperationForEachFilter(
Record *CurRec,
2423 const RecTy *ItemType) {
2428 TokError(
"expected '(' after !foreach/!filter");
2433 TokError(
"first argument of !foreach/!filter must be an identifier");
2440 if (CurRec && CurRec->
getValue(LHS)) {
2442 "' is already defined")
2448 TokError(
"expected ',' in !foreach/!filter");
2452 const Init *MHS = ParseValue(CurRec);
2457 TokError(
"expected ',' in !foreach/!filter");
2461 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2463 TokError(
"could not get type of !foreach/!filter list or dag");
2467 const RecTy *InEltType =
nullptr;
2468 const RecTy *ExprEltType =
nullptr;
2471 if (
const auto *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
2472 InEltType = InListTy->getElementType();
2474 if (
const auto *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
2476 ? OutListTy->getElementType()
2480 "expected value of type '" +
2482 "', but got list type");
2486 }
else if (
const auto *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
2488 TokError(
"!filter must have a list argument");
2491 InEltType = InDagTy;
2492 if (ItemType && !isa<DagRecTy>(ItemType)) {
2495 "', but got dag type");
2501 TokError(
"!foreach must have a list or dag argument");
2503 TokError(
"!filter must have a list argument");
2509 std::unique_ptr<Record> ParseRecTmp;
2510 Record *ParseRec = CurRec;
2514 ParseRec = ParseRecTmp.get();
2518 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2519 ParseRec->removeValue(LHS);
2525 TokError(
"expected ')' in !foreach/!filter");
2529 const RecTy *OutType = InEltType;
2531 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2533 TokError(
"could not get type of !foreach result expression");
2543 LHS, MHS, RHS, OutType))
2547const Init *TGParser::ParseOperationCond(
Record *CurRec,
2548 const RecTy *ItemType) {
2552 TokError(
"expected '(' after !cond operator");
2563 const Init *
V = ParseValue(CurRec);
2569 TokError(
"expected ':' following a condition in !cond operator");
2573 V = ParseValue(CurRec, ItemType);
2582 TokError(
"expected ',' or ')' following a value in !cond operator");
2587 if (Case.
size() < 1) {
2588 TokError(
"there should be at least 1 'condition : value' in the !cond operator");
2594 for (
const Init *V : Val) {
2595 const RecTy *VTy =
nullptr;
2596 if (
const auto *Vt = dyn_cast<TypedInit>(V))
2597 VTy = Vt->getType();
2598 if (
const auto *Vbits = dyn_cast<BitsInit>(V))
2600 if (isa<BitInit>(V))
2603 if (
Type ==
nullptr) {
2604 if (!isa<UnsetInit>(V))
2607 if (!isa<UnsetInit>(V)) {
2620 TokError(
"could not determine type for !cond from its arguments");
2653const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2655 const Init *
R =
nullptr;
2660 return ParseOperation(CurRec, ItemType);
2663 default:
TokError(
"Unknown or reserved token when parsing a value");
break;
2680 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2714 return ParseIDValue(CurRec,
Name, NameLoc, Mode);
2722 "Expected a class name, got '" +
Name->getValue() +
"'");
2729 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
2732 if (CheckTemplateArgValues(Args, ArgLocs, Class))
2735 if (resolveArguments(Class, Args, NameLoc.
Start))
2738 if (TrackReferenceLocs)
2739 Class->appendReferenceLoc(NameLoc);
2748 ParseValueList(Vals, CurRec);
2749 if (Vals.
empty())
return nullptr;
2752 TokError(
"expected '}' at end of bit list value");
2761 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
2766 if (
const auto *BI = dyn_cast<BitsInit>(Vals[i])) {
2767 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2772 if (
const auto *VI = dyn_cast<VarInit>(Vals[i])) {
2773 if (
const auto *BitsRec = dyn_cast<BitsRecTy>(
VI->getType())) {
2774 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2783 Error(BraceLoc,
"Element #" +
Twine(i) +
" (" + Vals[i]->getAsString() +
2784 ") is not convertable to a bit");
2789 std::reverse(NewBits.
begin(), NewBits.
end());
2796 const RecTy *DeducedEltTy =
nullptr;
2800 const auto *ListType = dyn_cast<ListRecTy>(ItemType);
2806 GivenListTy = ListType;
2810 ParseValueList(Vals, CurRec,
2812 if (Vals.
empty())
return nullptr;
2815 TokError(
"expected ']' at end of list value");
2819 const RecTy *GivenEltTy =
nullptr;
2822 GivenEltTy = ParseType();
2829 TokError(
"expected '>' at end of list element type");
2835 const RecTy *EltTy =
nullptr;
2836 for (
const Init *V : Vals) {
2837 const auto *TArg = dyn_cast<TypedInit>(V);
2842 TokError(
"Incompatible types in list elements");
2846 EltTy = TArg->getType();
2855 TokError(
"Incompatible types in list elements");
2872 TokError(
Twine(
"Element type mismatch for list: element type '") +
2878 DeducedEltTy = EltTy;
2889 TokError(
"expected identifier or list of value types in dag init");
2900 TokError(
"expected variable name in dag operator");
2909 ParseDagArgList(DagArgs, CurRec);
2910 if (DagArgs.
empty())
return nullptr;
2914 TokError(
"expected ')' in dag init");
2932const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
2935 const Init *
Result = ParseSimpleValue(CurRec, ItemType, Mode);
2936 if (!Result)
return nullptr;
2943 if (Mode == ParseNameMode)
2950 ParseRangeList(Ranges);
2951 if (
Ranges.empty())
return nullptr;
2957 Error(CurlyLoc,
"Invalid bit range for value");
2963 TokError(
"expected '}' at end of bit range list");
2969 const auto *
LHS = dyn_cast<TypedInit>(Result);
2971 Error(LHSLoc,
"Invalid value, list expected");
2975 const auto *LHSTy = dyn_cast<ListRecTy>(
LHS->
getType());
2978 "' is invalid, list expected");
2983 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
2992 LHSTy->getElementType())
3000 TokError(
"expected ']' at end of list slice");
3007 TokError(
"expected field identifier after '.'");
3013 if (!
Result->getFieldType(FieldName)) {
3015 Result->getAsString() +
"'");
3020 if (TrackReferenceLocs) {
3021 if (
const auto *DI = dyn_cast<DefInit>(Result)) {
3022 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3023 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3024 }
else if (
const auto *TI = dyn_cast<TypedInit>(Result)) {
3025 if (
const auto *
RecTy = dyn_cast<RecordRecTy>(TI->getType())) {
3027 if (
const auto *RV =
R->getValue(FieldName))
3040 const auto *
LHS = dyn_cast<TypedInit>(Result);
3042 Error(PasteLoc,
"LHS of paste is not typed!");
3050 assert(Mode == ParseValueMode &&
"encountered paste of lists in name");
3059 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3071 auto CastLHS = dyn_cast<TypedInit>(
3076 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3098 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3101 RHS = dyn_cast<TypedInit>(RHSResult);
3103 Error(PasteLoc,
"RHS of paste is not typed!");
3108 auto CastRHS = dyn_cast<TypedInit>(
3113 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3134void TGParser::ParseDagArgList(
3147 const Init *Val = ParseValue(CurRec);
3157 TokError(
"expected variable name in dag literal");
3165 Result.push_back(std::make_pair(Val, VarName));
3180 Result.push_back(ParseValue(CurRec, ItemType));
3190 Result.push_back(ParseValue(CurRec, ItemType));
3206bool TGParser::ParseTemplateArgValueList(
3209 assert(
Result.empty() &&
"Result vector is not empty");
3215 bool HasNamedArg =
false;
3216 unsigned ArgIndex = 0;
3218 if (ArgIndex >= TArgs.
size()) {
3219 TokError(
"Too many template arguments: " + utostr(ArgIndex + 1));
3228 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3234 if (!isa<StringInit>(
Value))
3235 return Error(ValueLoc,
3236 "The name of named argument should be a valid identifier");
3242 return Error(ValueLoc,
3243 "Argument " +
Name->getAsString() +
" doesn't exist");
3247 Value = ParseValue(CurRec, NamedArg->getType());
3249 if (isa<UnsetInit>(
Value))
3250 return Error(ValueLoc,
3251 "The value of named argument should be initialized, "
3253 Value->getAsString() +
"'");
3260 return Error(ValueLoc,
3261 "Positional argument should be put before named argument");
3269 return TokError(
"Expected comma before next argument");
3284const Init *TGParser::ParseDeclaration(
Record *CurRec,
3285 bool ParsingTemplateArgs) {
3290 if (!
Type)
return nullptr;
3293 TokError(
"Expected identifier in declaration");
3298 if (Str ==
"NAME") {
3299 TokError(
"'" + Str +
"' is a reserved variable name");
3303 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3304 TokError(
"local variable of this name already exists");
3313 if (!ParsingTemplateArgs) {
3314 BadField = AddValue(CurRec, IdLoc,
3318 }
else if (CurRec) {
3321 AddValue(CurRec, IdLoc,
3324 assert(CurMultiClass &&
"invalid context for template argument");
3327 AddValue(CurRec, IdLoc,
3336 const Init *Val = ParseValue(CurRec,
Type);
3338 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3359TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3361 TokError(
"Expected identifier in foreach declaration");
3370 TokError(
"Expected '=' in foreach declaration");
3374 const RecTy *IterType =
nullptr;
3380 ParseRangeList(Ranges);
3382 TokError(
"expected '}' at end of bit range list");
3390 const Init *
I = ParseValue(
nullptr);
3394 const auto *TI = dyn_cast<TypedInit>(
I);
3395 if (TI && isa<ListRecTy>(TI->getType())) {
3396 ForeachListValue =
I;
3397 IterType = cast<ListRecTy>(TI->getType())->getElementType();
3402 if (ParseRangePiece(Ranges, TI))
3407 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3408 if (CurMultiClass) {
3409 PrintNote({},
"references to multiclass template arguments cannot be "
3410 "resolved at this time");
3418 assert(!IterType &&
"Type already initialized?");
3420 std::vector<Init *> Values;
3421 for (
unsigned R : Ranges)
3439bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3443 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->
Rec;
3446 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3455 TemplArg = ParseDeclaration(CurRec,
true);
3460 return Error(Loc,
"template argument with the same name has already been "
3467 return TokError(
"expected '>' at end of template argument list");
3479bool TGParser::ParseBodyItem(
Record *CurRec) {
3481 return ParseAssert(
nullptr, CurRec);
3484 return ParseDefvar(CurRec);
3487 return ParseDump(
nullptr, CurRec);
3490 if (!ParseDeclaration(CurRec,
false))
3494 return TokError(
"expected ';' after declaration");
3500 return TokError(
"expected field identifier after let");
3507 if (ParseOptionalBitList(BitList))
3509 std::reverse(BitList.
begin(), BitList.
end());
3512 return TokError(
"expected '=' in let expression");
3516 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3519 if (!BitList.
empty() && isa<BitsRecTy>(
Type)) {
3525 const Init *Val = ParseValue(CurRec,
Type);
3526 if (!Val)
return true;
3529 return TokError(
"expected ';' after let expression");
3531 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
3541bool TGParser::ParseBody(
Record *CurRec) {
3547 return TokError(
"Expected '{' to start body or ';' for declaration only");
3550 if (ParseBodyItem(CurRec))
3559 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3560 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3568bool TGParser::ApplyLetStack(
Record *CurRec) {
3571 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
3579 return ApplyLetStack(
Entry.Rec.get());
3582 if (
Entry.Assertion)
3589 for (
auto &E :
Entry.Loop->Entries) {
3590 if (ApplyLetStack(E))
3606bool TGParser::ParseObjectBody(
Record *CurRec) {
3616 if (!SubClass.
Rec)
return true;
3619 if (AddSubClass(CurRec, SubClass))
3624 SubClass = ParseSubClassReference(CurRec,
false);
3628 if (ApplyLetStack(CurRec))
3631 bool Result = ParseBody(CurRec);
3641bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3652 std::unique_ptr<Record> CurRec;
3653 const Init *
Name = ParseObjectName(CurMultiClass);
3657 if (isa<UnsetInit>(
Name)) {
3661 CurRec = std::make_unique<Record>(
Name, NameLoc, Records);
3664 if (ParseObjectBody(CurRec.get()))
3667 return addEntry(std::move(CurRec));
3674bool TGParser::ParseDefset() {
3683 if (!isa<ListRecTy>(
Type))
3685 Defset.EltTy = cast<ListRecTy>(
Type)->getElementType();
3688 return TokError(
"expected identifier");
3691 return TokError(
"def or global variable of this name already exists");
3700 Defsets.push_back(&Defset);
3701 bool Err = ParseObjectList(
nullptr);
3707 TokError(
"expected '}' at end of defset");
3708 return Error(BraceLoc,
"to match this '{'");
3720bool TGParser::ParseDeftype() {
3725 return TokError(
"expected identifier");
3728 if (TypeAliases.count(TypeName) || Records.
getClass(TypeName))
3729 return TokError(
"type of this name '" + TypeName +
"' already exists");
3741 return Error(Loc,
"cannot define type alias for class type '" +
3742 Type->getAsString() +
"'");
3756bool TGParser::ParseDefvar(
Record *CurRec) {
3761 return TokError(
"expected identifier");
3763 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
3764 return TokError(
"local variable of this name already exists");
3769 if (V && !
V->isTemplateArg())
3770 return TokError(
"field of this name already exists");
3776 return TokError(
"def or global variable of this name already exists");
3789 if (!CurScope->isOutermost())
3803bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
3810 const Init *ListValue =
nullptr;
3811 const VarInit *IterName = ParseForeachDeclaration(ListValue);
3813 return TokError(
"expected declaration in for");
3819 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
3822 Loops.push_back(std::move(TheLoop));
3826 if (ParseObject(CurMultiClass))
3834 if (ParseObjectList(CurMultiClass))
3838 TokError(
"expected '}' at end of foreach command");
3839 return Error(BraceLoc,
"to match this '{'");
3846 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
3849 return addEntry(std::move(
Loop));
3857bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
3864 const Init *Condition = ParseValue(
nullptr);
3884 const Init *ThenClauseList =
3888 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
3890 if (ParseIfBody(CurMultiClass,
"then"))
3893 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
3896 if (addEntry(std::move(
Loop)))
3906 const Init *ElseClauseList =
3911 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
3913 if (ParseIfBody(CurMultiClass,
"else"))
3916 Loop = std::move(Loops.back());
3919 if (addEntry(std::move(
Loop)))
3937 if (ParseObject(CurMultiClass))
3945 if (ParseObjectList(CurMultiClass))
3949 TokError(
"expected '}' at end of '" + Kind +
"' clause");
3950 return Error(BraceLoc,
"to match this '{'");
3966 const Init *Condition = ParseValue(CurRec);
3971 TokError(
"expected ',' in assert statement");
3975 const Init *Message = ParseValue(CurRec);
3983 CurRec->
addAssertion(ConditionLoc, Condition, Message);
3985 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
3994bool TGParser::ParseClass() {
3999 return TokError(
"expected class name after 'class' keyword");
4009 "' already defined");
4016 CurRec = NewRec.get();
4017 Records.
addClass(std::move(NewRec));
4020 if (TypeAliases.count(
Name))
4021 return TokError(
"there is already a defined type alias '" +
Name +
"'");
4029 if (ParseTemplateArgList(CurRec))
4032 if (ParseObjectBody(CurRec))
4035 if (!NoWarnOnUnusedTemplateArgs)
4051 TokError(
"expected identifier in let definition");
4062 if (ParseOptionalRangeList(Bits)) {
4066 std::reverse(
Bits.begin(),
Bits.end());
4069 TokError(
"expected '=' in let expression");
4074 const Init *Val = ParseValue(
nullptr);
4081 Result.emplace_back(
Name, Bits, Val, NameLoc);
4091bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4097 ParseLetList(LetInfo);
4098 if (LetInfo.
empty())
return true;
4102 return TokError(
"expected 'in' at end of top-level 'let'");
4107 if (ParseObject(CurMultiClass))
4118 if (ParseObjectList(CurMultiClass))
4122 TokError(
"expected '}' at end of top level let command");
4123 return Error(BraceLoc,
"to match this '{'");
4147bool TGParser::ParseMultiClass() {
4152 return TokError(
"expected identifier after multiclass for name");
4156 MultiClasses.insert(std::make_pair(
Name,
4157 std::make_unique<MultiClass>(
Name, Lex.
getLoc(),Records)));
4160 return TokError(
"multiclass '" +
Name +
"' already defined");
4162 CurMultiClass =
Result.first->second.get();
4170 if (ParseTemplateArgList(
nullptr))
4173 bool inherits =
false;
4181 ParseSubMultiClassReference(CurMultiClass);
4184 if (!SubMultiClass.
MC)
return true;
4187 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4192 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4198 return TokError(
"expected '{' in multiclass definition");
4200 return TokError(
"expected ';' in multiclass definition");
4203 return TokError(
"multiclass must contain at least one def");
4208 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4209 "'foreach', 'if', or 'let' in multiclass body");
4219 if (ParseObject(CurMultiClass))
4229 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4230 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4234 if (!NoWarnOnUnusedTemplateArgs)
4238 CurMultiClass =
nullptr;
4246bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4250 const Init *DefmName = ParseObjectName(CurMultiClass);
4253 if (isa<UnsetInit>(DefmName)) {
4263 return TokError(
"expected ':' after defm identifier");
4266 std::vector<RecordsEntry> NewEntries;
4269 bool InheritFromClass =
false;
4278 if (!
Ref.Rec)
return true;
4284 MultiClass *MC = MultiClasses[std::string(
Ref.Rec->getName())].get();
4285 assert(MC &&
"Didn't lookup multiclass correctly?");
4288 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4292 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4293 &NewEntries, &SubClassLoc))
4300 return TokError(
"expected identifier");
4302 SubClassLoc = Lex.
getLoc();
4308 if (InheritFromClass)
4311 Ref = ParseSubClassReference(
nullptr,
true);
4314 if (InheritFromClass) {
4320 if (!SubClass.
Rec)
return true;
4324 for (
auto &E : NewEntries) {
4326 if (AddSubClass(E, SubClass))
4332 SubClass = ParseSubClassReference(
nullptr,
false);
4336 for (
auto &E : NewEntries) {
4337 if (ApplyLetStack(E))
4340 addEntry(std::move(E));
4344 return TokError(
"expected ';' at end of defm");
4365 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4370 return ParseDeftype();
4373 return ParseDump(MC);
4376 case tgtok::Let:
return ParseTopLevelLet(MC);
4379 return TokError(
"defset is not allowed inside multiclass");
4380 return ParseDefset();
4383 return TokError(
"class is not allowed inside multiclass");
4385 return TokError(
"class is not allowed inside foreach loop");
4386 return ParseClass();
4389 return TokError(
"multiclass is not allowed inside foreach loop");
4390 return ParseMultiClass();
4396bool TGParser::ParseObjectList(
MultiClass *MC) {
4398 if (ParseObject(MC))
4407 if (ParseObjectList())
4415 return TokError(
"Unexpected token at top level");
4422bool TGParser::CheckTemplateArgValues(
4426 "expected as many values as locations");
4430 bool HasError =
false;
4432 const Init *ArgName =
nullptr;
4433 if (
Value->isPositional())
4434 ArgName = TArgs[
Value->getIndex()];
4435 if (
Value->isNamed())
4441 if (
const auto *ArgValue = dyn_cast<TypedInit>(
Value->getValue())) {
4442 auto *CastValue = ArgValue->
getCastTo(ArgType);
4444 assert((!isa<TypedInit>(CastValue) ||
4445 cast<TypedInit>(CastValue)->
getType()->typeIsA(ArgType)) &&
4446 "result of template arg value cast has wrong type");
4450 Loc,
"Value specified for template argument '" +
4452 ArgValue->getType()->
getAsString() +
"; expected type " +
4461#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4480 errs() <<
"Record:\n";
4483 errs() <<
"Defs:\n";
4495 const Init *Message = ParseValue(CurRec);
4501 if (isa<DefInit>(Message))
4509 CurRec->
addDump(Loc, Message);
4515 addEntry(std::make_unique<Record::DumpInfo>(Loc, ResolvedMessage));
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
uint64_t IntrinsicInst * II
PowerPC Reduce CR logical Operation
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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)
static const CondOpInit * get(ArrayRef< const Init * > C, ArrayRef< const Init * > V, const RecTy *Type)
const Init * Fold(const Record *CurRec) const
static const DagInit * get(const Init *V, const StringInit *VN, ArrayRef< const Init * > ArgRange, ArrayRef< const StringInit * > NameRange)
static const DagRecTy * get(RecordKeeper &RK)
AL - Represent a reference to a 'def' in the description.
Lightweight error class with error context and mandatory checking.
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 !...
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
[AL, AH, CL] - Represent a list of defs
static const ListInit * get(ArrayRef< const Init * > Range, const RecTy *EltTy)
'list<Ty>' - Represent a list of element values, all of which must be of the specified type.
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.
This is a utility class that provides an abstraction for the common functionality between Instruction...
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>.
void addDef(std::unique_ptr< Record > R)
void addClass(std::unique_ptr< Record > R)
const Record * getClass(StringRef Name) const
Get the class with the specified name.
const Init * getNewAnonymousName()
GetNewAnonymousName - Generate a unique anonymous name that can be used as an identifier.
const Init * getGlobal(StringRef Name) const
Get the Init value of the specified global variable.
void addExtraGlobal(StringRef Name, const Init *I)
const Record * getDef(StringRef Name) const
Get the concrete record with the specified name.
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
ArrayRef< std::pair< const Record *, SMRange > > getSuperClasses() 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)
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 appendAssertions(const Record *Rec)
const Init * getNameInit() const
void addSuperClass(const Record *R, SMRange Range)
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.
SMRange getLocRange() const
int64_t getCurIntVal() const
std::pair< int64_t, unsigned > getCurBinaryIntVal() const
const std::string & getCurStrVal() const
tgtok::TokKind getCode() const
void PopScope(TGVarScope *ExpectedStackTop)
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...
The instances of the Type class are immutable: once they are created, they are never changed.
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.
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
<