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();
779 if (ParseTemplateArgValueList(
Result.TemplateArgs, CurRec,
Result.Rec)) {
784 if (CheckTemplateArgValues(
Result.TemplateArgs,
Result.RefRange.Start,
802ParseSubMultiClassReference(
MultiClass *CurMC) {
806 Result.MC = ParseMultiClassID();
815 if (ParseTemplateArgValueList(
Result.TemplateArgs, &CurMC->
Rec,
837 auto LHSLoc = Lex.
getLoc();
838 auto *CurVal = ParseValue(CurRec);
841 const auto *
LHS = cast<TypedInit>(CurVal);
848 auto RHSLoc = Lex.
getLoc();
849 CurVal = ParseValue(CurRec);
852 RHS = cast<TypedInit>(CurVal);
863 TokError(
"invalid range, cannot be negative");
900const TypedInit *TGParser::ParseSliceElements(
Record *CurRec,
bool Single) {
905 auto FlushElems = [&] {
906 if (!Elems.
empty()) {
913 auto LHSLoc = Lex.
getLoc();
914 CurVal = ParseSliceElement(CurRec);
917 auto *CurValTy = CurVal->
getType();
919 if (
const auto *ListValTy = dyn_cast<ListRecTy>(CurValTy)) {
920 if (!isa<IntRecTy>(ListValTy->getElementType())) {
922 "expected list<int>, got " +
Twine(ListValTy->getAsString()));
930 }
else if (!isa<IntRecTy>(CurValTy)) {
932 "unhandled type " +
Twine(CurValTy->getAsString()) +
" in range");
960 for (
auto *Slice : Slices) {
976 const Init *CurVal = FirstItem;
978 CurVal = ParseValue(
nullptr);
980 const auto *
II = dyn_cast_or_null<IntInit>(CurVal);
982 return TokError(
"expected integer or bitrange");
984 int64_t Start =
II->getValue();
988 return TokError(
"invalid range, cannot be negative");
999 const Init *I_End = ParseValue(
nullptr);
1000 const auto *II_End = dyn_cast_or_null<IntInit>(I_End);
1002 TokError(
"expected integer value as end of range");
1006 End = II_End->getValue();
1016 return TokError(
"invalid range, cannot be negative");
1020 for (; Start <=
End; ++Start)
1023 for (; Start >=
End; --Start)
1034 if (ParseRangePiece(Result)) {
1040 if (ParseRangePiece(Result)) {
1055 ParseRangeList(Ranges);
1056 if (
Ranges.empty())
return true;
1059 TokError(
"expected '>' at end of range list");
1060 return Error(StartLoc,
"to match this '<'");
1074 ParseRangeList(Ranges);
1075 if (
Ranges.empty())
return true;
1078 TokError(
"expected '}' at end of bit list");
1079 return Error(StartLoc,
"to match this '{'");
1095const RecTy *TGParser::ParseType() {
1097 default:
TokError(
"Unknown token when expecting a type");
return nullptr;
1113 if (
I != TypeAliases.end()) {
1117 if (
const Record *R = ParseClassID())
1124 TokError(
"expected '<' after bits type");
1128 TokError(
"expected integer in bits<n> type");
1133 TokError(
"expected '>' at end of bits<n> type");
1141 TokError(
"expected '<' after list type");
1145 const RecTy *SubType = ParseType();
1146 if (!SubType)
return nullptr;
1149 TokError(
"expected '>' at end of list<ty> type");
1159 SMRange NameLoc, IDParseMode Mode) {
1160 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass,
Name, NameLoc,
1161 TrackReferenceLocs))
1164 if (Mode == ParseNameMode)
1169 if (TrackReferenceLocs) {
1170 if (
const auto *Def = dyn_cast<DefInit>(
I))
1171 Def->getDef()->appendReferenceLoc(NameLoc);
1178 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1182 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1190const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1217 Type = ParseOperatorType();
1220 TokError(
"did not get type for unary operator");
1279 Type = ParseOperatorType();
1282 TokError(
"did not get type for unary operator");
1286 if (!isa<RecordRecTy>(
Type)) {
1287 TokError(
"type for !getdagop must be a record type");
1302 TokError(
"expected '(' after unary operator");
1306 const Init *
LHS = ParseValue(CurRec);
1307 if (!LHS)
return nullptr;
1310 const auto *LHSl = dyn_cast<ListInit>(LHS);
1311 const auto *LHSs = dyn_cast<StringInit>(LHS);
1312 const auto *LHSd = dyn_cast<DagInit>(LHS);
1313 const auto *LHSt = dyn_cast<TypedInit>(LHS);
1314 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1315 TokError(
"expected string, list, or dag type argument in unary operator");
1319 if (!isa<ListRecTy, StringRecTy, DagRecTy>(LHSt->getType())) {
1320 TokError(
"expected string, list, or dag type argument in unary operator");
1328 const auto *LHSl = dyn_cast<ListInit>(LHS);
1329 const auto *LHSt = dyn_cast<TypedInit>(LHS);
1330 if (!LHSl && !LHSt) {
1331 TokError(
"expected list type argument in unary operator");
1335 if (!isa<ListRecTy>(LHSt->getType())) {
1336 TokError(
"expected list type argument in unary operator");
1341 if (LHSl && LHSl->empty()) {
1342 TokError(
"empty list argument in unary operator");
1345 bool UseElementType =
1348 const Init *Item = LHSl->getElement(0);
1349 const auto *Itemt = dyn_cast<TypedInit>(Item);
1351 TokError(
"untyped list element in unary operator");
1354 Type = UseElementType ? Itemt->getType()
1357 assert(LHSt &&
"expected list type argument in unary operator");
1358 const auto *LType = dyn_cast<ListRecTy>(LHSt->getType());
1359 Type = UseElementType ? LType->getElementType() : LType;
1365 const auto *InnerListTy = dyn_cast<ListRecTy>(
Type);
1377 TokError(
"expected ')' in unary operator");
1387 const RecTy *
Type = ParseOperatorType();
1392 TokError(
"expected '(' after type of !isa");
1396 const Init *
LHS = ParseValue(CurRec);
1412 const RecTy *
Type = ParseOperatorType();
1417 TokError(
"expected '(' after type of !exists");
1422 const Init *Expr = ParseValue(CurRec);
1426 const auto *ExprType = dyn_cast<TypedInit>(Expr);
1428 Error(ExprLoc,
"expected string type argument in !exists operator");
1432 const auto *RecType = dyn_cast<RecordRecTy>(ExprType->getType());
1435 "expected string type argument in !exists operator, please "
1436 "use !isa instead");
1440 const auto *SType = dyn_cast<StringRecTy>(ExprType->getType());
1442 Error(ExprLoc,
"expected string type argument in !exists operator");
1447 TokError(
"expected ')' in !exists");
1520 const RecTy *ArgType =
nullptr;
1530 Type = ParseOperatorType();
1532 TokError(
"did not get type for !getdagarg operator");
1583 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1584 Error(OpLoc,
Twine(
"expected value of type '") +
1586 Type->getAsString() +
"'");
1591 TokError(
"expected '(' after binary operator");
1601 InitList.
push_back(ParseValue(CurRec, ArgType));
1602 if (!InitList.
back())
return nullptr;
1604 const auto *InitListBack = dyn_cast<TypedInit>(InitList.
back());
1605 if (!InitListBack) {
1606 Error(OpLoc,
Twine(
"expected value to be a typed value, got '" +
1607 InitList.
back()->getAsString() +
"'"));
1610 const RecTy *ListType = InitListBack->getType();
1618 if (!isa<ListRecTy>(ArgType)) {
1619 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1625 if (ItemType && InitList.
size() == 1) {
1626 if (!isa<ListRecTy>(ItemType)) {
1628 Twine(
"expected output type to be a list, got type '") +
1633 Error(OpLoc,
Twine(
"expected first arg type to be '") +
1635 "', got value of type '" +
1636 cast<ListRecTy>(ItemType)
1643 if (InitList.
size() == 2 && !isa<IntRecTy>(ArgType)) {
1644 Error(InitLoc,
Twine(
"expected second parameter to be an int, got "
1645 "value of type '") +
1652 if (!isa<ListRecTy>(ArgType)) {
1653 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1663 Error(InitLoc,
Twine(
"expected bit, bits, int, string, or record; "
1664 "got value of type '") +
1677 Error(InitLoc,
Twine(
"expected bit, bits, int, or string; "
1678 "got value of type '") +
1684 switch (InitList.
size()) {
1689 Error(InitLoc,
Twine(
"expected list of string, int, bits, or bit; "
1690 "got value of type '") +
1696 if (!isa<StringRecTy>(ArgType)) {
1697 Error(InitLoc,
Twine(
"expected second argument to be a string, "
1698 "got value of type '") +
1714 Error(InitLoc,
Twine(
"expected value of type '") +
1753 TokError(
"expected ')' in operator");
1762 Type = cast<TypedInit>(InitList.
front())->getType()->getListTy();
1773 while (InitList.
size() > 2) {
1780 if (InitList.
size() == 2)
1784 Error(OpLoc,
"expected two operands to operator");
1790 return ParseOperationForEachFilter(CurRec, ItemType);
1798 TokError(
"expected '(' after !range operator");
1803 bool FirstArgIsList =
false;
1805 if (
Args.size() >= 3) {
1806 TokError(
"expected at most three values of integer");
1811 Args.push_back(ParseValue(CurRec));
1815 const auto *ArgBack = dyn_cast<TypedInit>(
Args.back());
1817 Error(OpLoc,
Twine(
"expected value to be a typed value, got '" +
1818 Args.back()->getAsString() +
"'"));
1822 const RecTy *ArgBackType = ArgBack->getType();
1823 if (!FirstArgIsList ||
Args.size() == 1) {
1824 if (
Args.size() == 1 && isa<ListRecTy>(ArgBackType)) {
1825 FirstArgIsList =
true;
1826 }
else if (isa<IntRecTy>(ArgBackType)) {
1829 if (
Args.size() != 1)
1830 Error(InitLoc,
Twine(
"expected value of type 'int', got '" +
1833 Error(InitLoc,
Twine(
"expected list or int, got value of type '") +
1839 assert(isa<ListRecTy>(cast<TypedInit>(Args[0])->
getType()));
1840 Error(InitLoc,
Twine(
"expected one list, got extra value of type '") +
1849 TokError(
"expected ')' in operator");
1854 auto ArgCount =
Args.size();
1856 const auto *Arg0 = cast<TypedInit>(Args[0]);
1857 const auto *Arg0Ty = Arg0->getType();
1858 if (ArgCount == 1) {
1859 if (isa<ListRecTy>(Arg0Ty)) {
1866 assert(isa<IntRecTy>(Arg0Ty));
1873 assert(isa<IntRecTy>(Arg0Ty));
1874 const auto *Arg1 = cast<TypedInit>(Args[1]);
1875 assert(isa<IntRecTy>(Arg1->getType()));
1878 if (ArgCount == 3) {
1880 const auto *Arg2 = cast<TypedInit>(Args[2]);
1881 assert(isa<IntRecTy>(Arg2->getType()));
1927 TokError(
"expected '(' after ternary operator");
1931 const Init *
LHS = ParseValue(CurRec);
1932 if (!LHS)
return nullptr;
1935 TokError(
"expected ',' in ternary operator");
1940 const Init *MHS = ParseValue(CurRec, ItemType);
1945 TokError(
"expected ',' in ternary operator");
1950 const Init *
RHS = ParseValue(CurRec, ItemType);
1955 TokError(
"expected ')' in binary operator");
1962 const auto *MHSt = dyn_cast<TypedInit>(MHS);
1963 if (!MHSt && !isa<UnsetInit>(MHS)) {
1964 Error(MHSLoc,
"could not determine type of the child list in !dag");
1967 if (MHSt && !isa<ListRecTy>(MHSt->getType())) {
1968 Error(MHSLoc,
Twine(
"expected list of children, got type '") +
1969 MHSt->getType()->getAsString() +
"'");
1973 const auto *RHSt = dyn_cast<TypedInit>(RHS);
1974 if (!RHSt && !isa<UnsetInit>(RHS)) {
1975 Error(RHSLoc,
"could not determine type of the name list in !dag");
1979 Error(RHSLoc,
Twine(
"expected list<string>, got type '") +
1980 RHSt->getType()->getAsString() +
"'");
1984 if (!MHSt && !RHSt) {
1986 "cannot have both unset children and unset names in !dag");
1992 const RecTy *MHSTy =
nullptr;
1993 const RecTy *RHSTy =
nullptr;
1995 if (
const auto *MHSt = dyn_cast<TypedInit>(MHS))
1996 MHSTy = MHSt->getType();
1997 if (
const auto *MHSbits = dyn_cast<BitsInit>(MHS))
1999 if (isa<BitInit>(MHS))
2002 if (
const auto *RHSt = dyn_cast<TypedInit>(RHS))
2003 RHSTy = RHSt->getType();
2004 if (
const auto *RHSbits = dyn_cast<BitsInit>(RHS))
2006 if (isa<BitInit>(RHS))
2010 if (isa<UnsetInit>(MHS))
2012 if (isa<UnsetInit>(RHS))
2015 if (!MHSTy || !RHSTy) {
2016 TokError(
"could not get type for !if");
2029 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2031 TokError(
"could not get type for !subst");
2034 Type = RHSt->getType();
2038 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2039 if (!MHSt || !isa<IntRecTy, StringRecTy>(MHSt->getType())) {
2040 Error(MHSLoc,
Twine(
"expected integer index or string name, got ") +
2041 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2049 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2050 if (!MHSt || !isa<IntRecTy, StringRecTy>(MHSt->getType())) {
2051 Error(MHSLoc,
Twine(
"expected integer index or string name, got ") +
2052 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2057 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2059 if (RHSt && !isa<StringRecTy>(RHSt->getType())) {
2060 Error(RHSLoc,
Twine(
"expected string or unset name, got type '") +
2061 RHSt->getType()->getAsString() +
"'");
2071 return ParseOperationSubstr(CurRec, ItemType);
2074 return ParseOperationFind(CurRec, ItemType);
2077 return ParseOperationCond(CurRec, ItemType);
2083 TokError(
"expected '(' after !foldl");
2087 const Init *StartUntyped = ParseValue(CurRec);
2091 const auto *Start = dyn_cast<TypedInit>(StartUntyped);
2099 TokError(
"expected ',' in !foldl");
2103 const Init *ListUntyped = ParseValue(CurRec);
2107 const auto *
List = dyn_cast<TypedInit>(ListUntyped);
2114 const auto *ListType = dyn_cast<ListRecTy>(
List->getType());
2116 TokError(
Twine(
"!foldl list must be a list, but is of type '") +
2117 List->getType()->getAsString());
2122 TokError(
"expected ',' in !foldl");
2127 TokError(
"third argument of !foldl must be an identifier");
2134 "' already defined")
2140 TokError(
"expected ',' in !foldl");
2145 TokError(
"fourth argument of !foldl must be an identifier");
2152 "' already defined")
2158 TokError(
"expected ',' in !foldl");
2165 std::unique_ptr<Record> ParseRecTmp;
2166 Record *ParseRec = CurRec;
2168 ParseRecTmp = std::make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
2169 ParseRec = ParseRecTmp.get();
2176 const Init *ExprUntyped = ParseValue(ParseRec);
2177 ParseRec->removeValue(
A);
2178 ParseRec->removeValue(
B);
2183 const auto *Expr = dyn_cast<TypedInit>(ExprUntyped);
2185 TokError(
"could not get type of !foldl expression");
2189 if (Expr->getType() != Start->getType()) {
2190 TokError(
Twine(
"!foldl expression must be of same type as start (") +
2191 Start->getType()->getAsString() +
"), but is of type " +
2197 TokError(
"expected ')' in fold operator");
2212const RecTy *TGParser::ParseOperatorType() {
2216 TokError(
"expected type name for operator");
2221 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2226 TokError(
"expected type name for operator");
2231 TokError(
"expected type name for operator");
2241const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2242 const RecTy *ItemType) {
2249 TokError(
"expected '(' after !substr operator");
2253 const Init *
LHS = ParseValue(CurRec);
2258 TokError(
"expected ',' in !substr operator");
2263 const Init *MHS = ParseValue(CurRec);
2271 RHS = ParseValue(CurRec);
2279 TokError(
"expected ')' in !substr operator");
2283 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2284 Error(RHSLoc,
Twine(
"expected value of type '") +
2286 Type->getAsString() +
"'");
2289 const auto *LHSt = dyn_cast<TypedInit>(LHS);
2290 if (!LHSt && !isa<UnsetInit>(LHS)) {
2291 TokError(
"could not determine type of the string in !substr");
2294 if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
2296 LHSt->getType()->getAsString() +
"'");
2300 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2301 if (!MHSt && !isa<UnsetInit>(MHS)) {
2302 TokError(
"could not determine type of the start position in !substr");
2305 if (MHSt && !isa<IntRecTy>(MHSt->getType())) {
2306 Error(MHSLoc,
Twine(
"expected int, got type '") +
2307 MHSt->getType()->getAsString() +
"'");
2312 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2313 if (!RHSt && !isa<UnsetInit>(RHS)) {
2314 TokError(
"could not determine type of the length in !substr");
2317 if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
2319 RHSt->getType()->getAsString() +
"'");
2330const Init *TGParser::ParseOperationFind(
Record *CurRec,
2331 const RecTy *ItemType) {
2338 TokError(
"expected '(' after !find operator");
2342 const Init *
LHS = ParseValue(CurRec);
2347 TokError(
"expected ',' in !find operator");
2352 const Init *MHS = ParseValue(CurRec);
2360 RHS = ParseValue(CurRec);
2368 TokError(
"expected ')' in !find operator");
2372 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2373 Error(RHSLoc,
Twine(
"expected value of type '") +
2375 Type->getAsString() +
"'");
2378 const auto *LHSt = dyn_cast<TypedInit>(LHS);
2379 if (!LHSt && !isa<UnsetInit>(LHS)) {
2380 TokError(
"could not determine type of the source string in !find");
2383 if (LHSt && !isa<StringRecTy>(LHSt->getType())) {
2385 LHSt->getType()->getAsString() +
"'");
2389 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2390 if (!MHSt && !isa<UnsetInit>(MHS)) {
2391 TokError(
"could not determine type of the target string in !find");
2394 if (MHSt && !isa<StringRecTy>(MHSt->getType())) {
2395 Error(MHSLoc,
Twine(
"expected string, got type '") +
2396 MHSt->getType()->getAsString() +
"'");
2401 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2402 if (!RHSt && !isa<UnsetInit>(RHS)) {
2403 TokError(
"could not determine type of the start position in !find");
2406 if (RHSt && !isa<IntRecTy>(RHSt->getType())) {
2408 RHSt->getType()->getAsString() +
"'");
2420const Init *TGParser::ParseOperationForEachFilter(
Record *CurRec,
2421 const RecTy *ItemType) {
2426 TokError(
"expected '(' after !foreach/!filter");
2431 TokError(
"first argument of !foreach/!filter must be an identifier");
2438 if (CurRec && CurRec->
getValue(LHS)) {
2440 "' is already defined")
2446 TokError(
"expected ',' in !foreach/!filter");
2450 const Init *MHS = ParseValue(CurRec);
2455 TokError(
"expected ',' in !foreach/!filter");
2459 const auto *MHSt = dyn_cast<TypedInit>(MHS);
2461 TokError(
"could not get type of !foreach/!filter list or dag");
2465 const RecTy *InEltType =
nullptr;
2466 const RecTy *ExprEltType =
nullptr;
2469 if (
const auto *InListTy = dyn_cast<ListRecTy>(MHSt->getType())) {
2470 InEltType = InListTy->getElementType();
2472 if (
const auto *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
2474 ? OutListTy->getElementType()
2478 "expected value of type '" +
2480 "', but got list type");
2484 }
else if (
const auto *InDagTy = dyn_cast<DagRecTy>(MHSt->getType())) {
2486 TokError(
"!filter must have a list argument");
2489 InEltType = InDagTy;
2490 if (ItemType && !isa<DagRecTy>(ItemType)) {
2493 "', but got dag type");
2499 TokError(
"!foreach must have a list or dag argument");
2501 TokError(
"!filter must have a list argument");
2507 std::unique_ptr<Record> ParseRecTmp;
2508 Record *ParseRec = CurRec;
2512 ParseRec = ParseRecTmp.get();
2516 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2517 ParseRec->removeValue(LHS);
2523 TokError(
"expected ')' in !foreach/!filter");
2527 const RecTy *OutType = InEltType;
2529 const auto *RHSt = dyn_cast<TypedInit>(RHS);
2531 TokError(
"could not get type of !foreach result expression");
2541 LHS, MHS, RHS, OutType))
2545const Init *TGParser::ParseOperationCond(
Record *CurRec,
2546 const RecTy *ItemType) {
2550 TokError(
"expected '(' after !cond operator");
2561 const Init *
V = ParseValue(CurRec);
2567 TokError(
"expected ':' following a condition in !cond operator");
2571 V = ParseValue(CurRec, ItemType);
2580 TokError(
"expected ',' or ')' following a value in !cond operator");
2585 if (Case.
size() < 1) {
2586 TokError(
"there should be at least 1 'condition : value' in the !cond operator");
2592 for (
const Init *V : Val) {
2593 const RecTy *VTy =
nullptr;
2594 if (
const auto *Vt = dyn_cast<TypedInit>(V))
2595 VTy = Vt->getType();
2596 if (
const auto *Vbits = dyn_cast<BitsInit>(V))
2598 if (isa<BitInit>(V))
2601 if (
Type ==
nullptr) {
2602 if (!isa<UnsetInit>(V))
2605 if (!isa<UnsetInit>(V)) {
2618 TokError(
"could not determine type for !cond from its arguments");
2651const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2653 const Init *
R =
nullptr;
2658 return ParseOperation(CurRec, ItemType);
2661 default:
TokError(
"Unknown or reserved token when parsing a value");
break;
2678 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2712 return ParseIDValue(CurRec,
Name, NameLoc, Mode);
2720 "Expected a class name, got '" +
Name->getValue() +
"'");
2726 if (ParseTemplateArgValueList(Args, CurRec, Class))
2729 if (CheckTemplateArgValues(Args, NameLoc.
Start, Class))
2732 if (resolveArguments(Class, Args, NameLoc.
Start))
2735 if (TrackReferenceLocs)
2736 Class->appendReferenceLoc(NameLoc);
2745 ParseValueList(Vals, CurRec);
2746 if (Vals.
empty())
return nullptr;
2749 TokError(
"expected '}' at end of bit list value");
2758 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
2763 if (
const auto *BI = dyn_cast<BitsInit>(Vals[i])) {
2764 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2769 if (
const auto *VI = dyn_cast<VarInit>(Vals[i])) {
2770 if (
const auto *BitsRec = dyn_cast<BitsRecTy>(
VI->getType())) {
2771 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2780 Error(BraceLoc,
"Element #" +
Twine(i) +
" (" + Vals[i]->getAsString() +
2781 ") is not convertable to a bit");
2786 std::reverse(NewBits.
begin(), NewBits.
end());
2793 const RecTy *DeducedEltTy =
nullptr;
2797 const auto *ListType = dyn_cast<ListRecTy>(ItemType);
2803 GivenListTy = ListType;
2807 ParseValueList(Vals, CurRec,
2809 if (Vals.
empty())
return nullptr;
2812 TokError(
"expected ']' at end of list value");
2816 const RecTy *GivenEltTy =
nullptr;
2819 GivenEltTy = ParseType();
2826 TokError(
"expected '>' at end of list element type");
2832 const RecTy *EltTy =
nullptr;
2833 for (
const Init *V : Vals) {
2834 const auto *TArg = dyn_cast<TypedInit>(V);
2839 TokError(
"Incompatible types in list elements");
2843 EltTy = TArg->getType();
2852 TokError(
"Incompatible types in list elements");
2869 TokError(
Twine(
"Element type mismatch for list: element type '") +
2875 DeducedEltTy = EltTy;
2886 TokError(
"expected identifier or list of value types in dag init");
2897 TokError(
"expected variable name in dag operator");
2906 ParseDagArgList(DagArgs, CurRec);
2907 if (DagArgs.
empty())
return nullptr;
2911 TokError(
"expected ')' in dag init");
2929const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
2932 const Init *
Result = ParseSimpleValue(CurRec, ItemType, Mode);
2933 if (!Result)
return nullptr;
2940 if (Mode == ParseNameMode)
2947 ParseRangeList(Ranges);
2948 if (
Ranges.empty())
return nullptr;
2954 Error(CurlyLoc,
"Invalid bit range for value");
2960 TokError(
"expected '}' at end of bit range list");
2966 const auto *
LHS = dyn_cast<TypedInit>(Result);
2968 Error(LHSLoc,
"Invalid value, list expected");
2972 const auto *LHSTy = dyn_cast<ListRecTy>(
LHS->
getType());
2975 "' is invalid, list expected");
2980 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
2989 LHSTy->getElementType())
2997 TokError(
"expected ']' at end of list slice");
3004 TokError(
"expected field identifier after '.'");
3010 if (!
Result->getFieldType(FieldName)) {
3012 Result->getAsString() +
"'");
3017 if (TrackReferenceLocs) {
3018 if (
const auto *DI = dyn_cast<DefInit>(Result)) {
3019 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3020 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3021 }
else if (
const auto *TI = dyn_cast<TypedInit>(Result)) {
3022 if (
const auto *
RecTy = dyn_cast<RecordRecTy>(TI->getType())) {
3024 if (
const auto *RV =
R->getValue(FieldName))
3037 const auto *
LHS = dyn_cast<TypedInit>(Result);
3039 Error(PasteLoc,
"LHS of paste is not typed!");
3047 assert(Mode == ParseValueMode &&
"encountered paste of lists in name");
3056 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3068 auto CastLHS = dyn_cast<TypedInit>(
3073 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3095 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3098 RHS = dyn_cast<TypedInit>(RHSResult);
3100 Error(PasteLoc,
"RHS of paste is not typed!");
3105 auto CastRHS = dyn_cast<TypedInit>(
3110 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3131void TGParser::ParseDagArgList(
3144 const Init *Val = ParseValue(CurRec);
3154 TokError(
"expected variable name in dag literal");
3162 Result.push_back(std::make_pair(Val, VarName));
3177 Result.push_back(ParseValue(CurRec, ItemType));
3187 Result.push_back(ParseValue(CurRec, ItemType));
3203bool TGParser::ParseTemplateArgValueList(
3206 assert(
Result.empty() &&
"Result vector is not empty");
3212 bool HasNamedArg =
false;
3213 unsigned ArgIndex = 0;
3215 if (ArgIndex >= TArgs.
size()) {
3216 TokError(
"Too many template arguments: " + utostr(ArgIndex + 1));
3225 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3231 if (!isa<StringInit>(
Value))
3232 return Error(ValueLoc,
3233 "The name of named argument should be a valid identifier");
3239 return Error(ValueLoc,
3240 "Argument " +
Name->getAsString() +
" doesn't exist");
3244 Value = ParseValue(CurRec, NamedArg->getType());
3246 if (isa<UnsetInit>(
Value))
3247 return Error(ValueLoc,
3248 "The value of named argument should be initialized, "
3250 Value->getAsString() +
"'");
3257 return Error(ValueLoc,
3258 "Positional argument should be put before named argument");
3266 return TokError(
"Expected comma before next argument");
3281const Init *TGParser::ParseDeclaration(
Record *CurRec,
3282 bool ParsingTemplateArgs) {
3287 if (!
Type)
return nullptr;
3290 TokError(
"Expected identifier in declaration");
3295 if (Str ==
"NAME") {
3296 TokError(
"'" + Str +
"' is a reserved variable name");
3300 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3301 TokError(
"local variable of this name already exists");
3310 if (!ParsingTemplateArgs) {
3311 BadField = AddValue(CurRec, IdLoc,
3315 }
else if (CurRec) {
3318 AddValue(CurRec, IdLoc,
3321 assert(CurMultiClass &&
"invalid context for template argument");
3324 AddValue(CurRec, IdLoc,
3333 const Init *Val = ParseValue(CurRec,
Type);
3335 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3356TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3358 TokError(
"Expected identifier in foreach declaration");
3367 TokError(
"Expected '=' in foreach declaration");
3371 const RecTy *IterType =
nullptr;
3377 ParseRangeList(Ranges);
3379 TokError(
"expected '}' at end of bit range list");
3387 const Init *
I = ParseValue(
nullptr);
3391 const auto *TI = dyn_cast<TypedInit>(
I);
3392 if (TI && isa<ListRecTy>(TI->getType())) {
3393 ForeachListValue =
I;
3394 IterType = cast<ListRecTy>(TI->getType())->getElementType();
3399 if (ParseRangePiece(Ranges, TI))
3404 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3405 if (CurMultiClass) {
3406 PrintNote({},
"references to multiclass template arguments cannot be "
3407 "resolved at this time");
3415 assert(!IterType &&
"Type already initialized?");
3417 std::vector<Init *> Values;
3418 for (
unsigned R : Ranges)
3436bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3440 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->
Rec;
3443 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3452 TemplArg = ParseDeclaration(CurRec,
true);
3457 return Error(Loc,
"template argument with the same name has already been "
3464 return TokError(
"expected '>' at end of template argument list");
3476bool TGParser::ParseBodyItem(
Record *CurRec) {
3478 return ParseAssert(
nullptr, CurRec);
3481 return ParseDefvar(CurRec);
3484 return ParseDump(
nullptr, CurRec);
3487 if (!ParseDeclaration(CurRec,
false))
3491 return TokError(
"expected ';' after declaration");
3497 return TokError(
"expected field identifier after let");
3504 if (ParseOptionalBitList(BitList))
3506 std::reverse(BitList.
begin(), BitList.
end());
3509 return TokError(
"expected '=' in let expression");
3513 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3516 if (!BitList.
empty() && isa<BitsRecTy>(
Type)) {
3522 const Init *Val = ParseValue(CurRec,
Type);
3523 if (!Val)
return true;
3526 return TokError(
"expected ';' after let expression");
3528 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
3538bool TGParser::ParseBody(
Record *CurRec) {
3544 return TokError(
"Expected '{' to start body or ';' for declaration only");
3547 if (ParseBodyItem(CurRec))
3556 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3557 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3565bool TGParser::ApplyLetStack(
Record *CurRec) {
3568 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
3576 return ApplyLetStack(
Entry.Rec.get());
3579 if (
Entry.Assertion)
3586 for (
auto &E :
Entry.Loop->Entries) {
3587 if (ApplyLetStack(E))
3603bool TGParser::ParseObjectBody(
Record *CurRec) {
3613 if (!SubClass.
Rec)
return true;
3616 if (AddSubClass(CurRec, SubClass))
3621 SubClass = ParseSubClassReference(CurRec,
false);
3625 if (ApplyLetStack(CurRec))
3628 bool Result = ParseBody(CurRec);
3638bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3649 std::unique_ptr<Record> CurRec;
3650 const Init *
Name = ParseObjectName(CurMultiClass);
3654 if (isa<UnsetInit>(
Name)) {
3658 CurRec = std::make_unique<Record>(
Name, NameLoc, Records);
3661 if (ParseObjectBody(CurRec.get()))
3664 return addEntry(std::move(CurRec));
3671bool TGParser::ParseDefset() {
3680 if (!isa<ListRecTy>(
Type))
3682 Defset.EltTy = cast<ListRecTy>(
Type)->getElementType();
3685 return TokError(
"expected identifier");
3688 return TokError(
"def or global variable of this name already exists");
3697 Defsets.push_back(&Defset);
3698 bool Err = ParseObjectList(
nullptr);
3704 TokError(
"expected '}' at end of defset");
3705 return Error(BraceLoc,
"to match this '{'");
3717bool TGParser::ParseDeftype() {
3722 return TokError(
"expected identifier");
3725 if (TypeAliases.count(TypeName) || Records.
getClass(TypeName))
3726 return TokError(
"type of this name '" + TypeName +
"' already exists");
3738 return Error(Loc,
"cannot define type alias for class type '" +
3739 Type->getAsString() +
"'");
3753bool TGParser::ParseDefvar(
Record *CurRec) {
3758 return TokError(
"expected identifier");
3760 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
3761 return TokError(
"local variable of this name already exists");
3766 if (V && !
V->isTemplateArg())
3767 return TokError(
"field of this name already exists");
3773 return TokError(
"def or global variable of this name already exists");
3786 if (!CurScope->isOutermost())
3800bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
3807 const Init *ListValue =
nullptr;
3808 const VarInit *IterName = ParseForeachDeclaration(ListValue);
3810 return TokError(
"expected declaration in for");
3816 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
3819 Loops.push_back(std::move(TheLoop));
3823 if (ParseObject(CurMultiClass))
3831 if (ParseObjectList(CurMultiClass))
3835 TokError(
"expected '}' at end of foreach command");
3836 return Error(BraceLoc,
"to match this '{'");
3843 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
3846 return addEntry(std::move(
Loop));
3854bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
3861 const Init *Condition = ParseValue(
nullptr);
3881 const Init *ThenClauseList =
3885 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
3887 if (ParseIfBody(CurMultiClass,
"then"))
3890 std::unique_ptr<ForeachLoop>
Loop = std::move(Loops.back());
3893 if (addEntry(std::move(
Loop)))
3903 const Init *ElseClauseList =
3908 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
3910 if (ParseIfBody(CurMultiClass,
"else"))
3913 Loop = std::move(Loops.back());
3916 if (addEntry(std::move(
Loop)))
3934 if (ParseObject(CurMultiClass))
3942 if (ParseObjectList(CurMultiClass))
3946 TokError(
"expected '}' at end of '" + Kind +
"' clause");
3947 return Error(BraceLoc,
"to match this '{'");
3963 const Init *Condition = ParseValue(CurRec);
3968 TokError(
"expected ',' in assert statement");
3972 const Init *Message = ParseValue(CurRec);
3980 CurRec->
addAssertion(ConditionLoc, Condition, Message);
3982 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
3991bool TGParser::ParseClass() {
3996 return TokError(
"expected class name after 'class' keyword");
4006 "' already defined");
4013 CurRec = NewRec.get();
4014 Records.
addClass(std::move(NewRec));
4017 if (TypeAliases.count(
Name))
4018 return TokError(
"there is already a defined type alias '" +
Name +
"'");
4026 if (ParseTemplateArgList(CurRec))
4029 if (ParseObjectBody(CurRec))
4032 if (!NoWarnOnUnusedTemplateArgs)
4048 TokError(
"expected identifier in let definition");
4059 if (ParseOptionalRangeList(Bits)) {
4063 std::reverse(
Bits.begin(),
Bits.end());
4066 TokError(
"expected '=' in let expression");
4071 const Init *Val = ParseValue(
nullptr);
4078 Result.emplace_back(
Name, Bits, Val, NameLoc);
4088bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4094 ParseLetList(LetInfo);
4095 if (LetInfo.
empty())
return true;
4099 return TokError(
"expected 'in' at end of top-level 'let'");
4104 if (ParseObject(CurMultiClass))
4115 if (ParseObjectList(CurMultiClass))
4119 TokError(
"expected '}' at end of top level let command");
4120 return Error(BraceLoc,
"to match this '{'");
4144bool TGParser::ParseMultiClass() {
4149 return TokError(
"expected identifier after multiclass for name");
4153 MultiClasses.insert(std::make_pair(
Name,
4154 std::make_unique<MultiClass>(
Name, Lex.
getLoc(),Records)));
4157 return TokError(
"multiclass '" +
Name +
"' already defined");
4159 CurMultiClass =
Result.first->second.get();
4167 if (ParseTemplateArgList(
nullptr))
4170 bool inherits =
false;
4178 ParseSubMultiClassReference(CurMultiClass);
4181 if (!SubMultiClass.
MC)
return true;
4184 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4189 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4195 return TokError(
"expected '{' in multiclass definition");
4197 return TokError(
"expected ';' in multiclass definition");
4200 return TokError(
"multiclass must contain at least one def");
4205 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4206 "'foreach', 'if', or 'let' in multiclass body");
4216 if (ParseObject(CurMultiClass))
4226 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4227 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4231 if (!NoWarnOnUnusedTemplateArgs)
4235 CurMultiClass =
nullptr;
4243bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4247 const Init *DefmName = ParseObjectName(CurMultiClass);
4250 if (isa<UnsetInit>(DefmName)) {
4260 return TokError(
"expected ':' after defm identifier");
4263 std::vector<RecordsEntry> NewEntries;
4266 bool InheritFromClass =
false;
4275 if (!
Ref.Rec)
return true;
4281 MultiClass *MC = MultiClasses[std::string(
Ref.Rec->getName())].get();
4282 assert(MC &&
"Didn't lookup multiclass correctly?");
4285 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4289 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4290 &NewEntries, &SubClassLoc))
4297 return TokError(
"expected identifier");
4299 SubClassLoc = Lex.
getLoc();
4305 if (InheritFromClass)
4308 Ref = ParseSubClassReference(
nullptr,
true);
4311 if (InheritFromClass) {
4317 if (!SubClass.
Rec)
return true;
4321 for (
auto &E : NewEntries) {
4323 if (AddSubClass(E, SubClass))
4329 SubClass = ParseSubClassReference(
nullptr,
false);
4333 for (
auto &E : NewEntries) {
4334 if (ApplyLetStack(E))
4337 addEntry(std::move(E));
4341 return TokError(
"expected ';' at end of defm");
4362 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4367 return ParseDeftype();
4370 return ParseDump(MC);
4373 case tgtok::Let:
return ParseTopLevelLet(MC);
4376 return TokError(
"defset is not allowed inside multiclass");
4377 return ParseDefset();
4380 return TokError(
"class is not allowed inside multiclass");
4382 return TokError(
"class is not allowed inside foreach loop");
4383 return ParseClass();
4386 return TokError(
"multiclass is not allowed inside foreach loop");
4387 return ParseMultiClass();
4393bool TGParser::ParseObjectList(
MultiClass *MC) {
4395 if (ParseObject(MC))
4404 if (ParseObjectList())
4412 return TokError(
"Unexpected token at top level");
4419bool TGParser::CheckTemplateArgValues(
4425 const Init *ArgName =
nullptr;
4426 if (
Value->isPositional())
4427 ArgName = TArgs[
Value->getIndex()];
4428 if (
Value->isNamed())
4434 if (
const auto *ArgValue = dyn_cast<TypedInit>(
Value->getValue())) {
4435 auto *CastValue = ArgValue->
getCastTo(ArgType);
4437 assert((!isa<TypedInit>(CastValue) ||
4438 cast<TypedInit>(CastValue)->
getType()->typeIsA(ArgType)) &&
4439 "result of template arg value cast has wrong type");
4454#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4473 errs() <<
"Record:\n";
4476 errs() <<
"Defs:\n";
4488 const Init *Message = ParseValue(CurRec);
4494 if (isa<DefInit>(Message))
4502 CurRec->
addDump(Loc, Message);
4508 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...
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
'Opcode' - Represent a reference to an entire variable object.
static const VarInit * get(StringRef VN, const RecTy *T)
std::string getAsString() const override
Convert this value to a literal form.
#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.
@ SC
CHAIN = SC CHAIN, Imm128 - System call.
@ 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.
void PrintFatalError(const Twine &Msg)
void PrintError(const Twine &Msg)
bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
void PrintNote(const Twine &Msg)
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.
void dumpMessage(SMLoc Loc, const Init *Message)
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.
ForeachLoop - Record the iteration state associated with a for loop.
std::vector< RecordsEntry > Entries
std::vector< RecordsEntry > Entries
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
std::unique_ptr< ForeachLoop > Loop
std::unique_ptr< Record::AssertionInfo > Assertion
std::unique_ptr< Record::DumpInfo > Dump
std::unique_ptr< Record > Rec
SubClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs
SubMultiClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs