19 #include "llvm/Config/llvm-config.h"
58 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
60 errs() <<
"Multiclass:\n";
64 errs() <<
"Template args:\n";
76 bool IsReference =
false;
77 if (
auto VBI = dyn_cast<VarBitInit>(
Bit)) {
78 if (
auto VI = dyn_cast<VarInit>(VBI->getBitVar())) {
79 if (
R.getValue(
VI->getName()))
82 }
else if (isa<VarInit>(
Bit)) {
85 if (!(IsReference ||
Bit->isConcrete()))
97 if (RV.isNonconcreteOK())
100 if (
Init *V = RV.getValue()) {
104 Twine(
"Initializer of '") + RV.getNameInitAsString() +
105 "' in '" +
R.getNameInitAsString() +
106 "' could not be fully resolved: " +
107 RV.getValue()->getAsString());
120 NewName = BinOpInit::getStrConcat(NewName,
Name);
121 if (CurMultiClass && Scoper !=
"::") {
124 NewName = BinOpInit::getStrConcat(
Prefix, NewName);
127 if (
BinOpInit *BinOp = dyn_cast<BinOpInit>(NewName))
128 NewName = BinOp->Fold(&CurRec);
145 CurRec = &CurMultiClass->
Rec;
150 return Error(Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
152 "previous definition of type '" +
153 ERV->getType()->getAsString() +
"'");
164 bool AllowSelfAssignment) {
165 if (!V)
return false;
167 if (!CurRec) CurRec = &CurMultiClass->
Rec;
178 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
179 return Error(Loc,
"Recursion / self-assignment forbidden");
185 if (!BitList.
empty()) {
189 "' is not a bits type");
194 return Error(Loc,
"Initializer is not compatible with bit range");
199 for (
unsigned i = 0,
e = BitList.
size();
i !=
e; ++
i) {
200 unsigned Bit = BitList[
i];
202 return Error(Loc,
"Cannot set bit #" +
Twine(
Bit) +
" of value '" +
215 std::string InitType;
216 if (
BitsInit *BI = dyn_cast<BitsInit>(V))
217 InitType = (
Twine(
"' of type bit initializer with length ") +
218 Twine(BI->getNumBits())).str();
219 else if (
TypedInit *TI = dyn_cast<TypedInit>(V))
220 InitType = (
Twine(
"' of type '") + TI->getType()->getAsString()).str();
223 "' is incompatible with value '" +
238 if (
Field.isTemplateArg()) {
248 "Too many template arguments allowed");
252 for (
unsigned I = 0,
E = TArgs.
size();
I !=
E; ++
I) {
255 else if (!
R.isComplete(TArgs[
I]))
257 "Value not specified for template argument '" +
258 TArgs[
I]->getAsUnquotedString() +
"' (#" +
Twine(
I) +
259 ") of parent class '" +
SC->getNameInitAsString() +
"'");
278 for (
const auto &SCPair : SCs) {
281 "Already subclass of '" + SCPair.first->getName() +
"'!\n");
287 "Already subclass of '" +
SC->getName() +
"'!\n");
294 return AddSubClass(Entry.Rec.get(), SubClass);
299 for (
auto &
E : Entry.Loop->Entries) {
300 if (AddSubClass(
E, SubClass))
310 bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
317 "More template args specified than expected");
322 for (
unsigned i = 0,
e = SMCTArgs.
size();
i !=
e; ++
i) {
329 "value not specified for template argument #" +
Twine(
i) +
330 " (" + SMCTArgs[
i]->getAsUnquotedString() +
348 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion) == 1 &&
349 "RecordsEntry has invalid number of items");
352 if (!Loops.empty()) {
353 Loops.back()->Entries.push_back(
std::move(
E));
360 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
361 CurMultiClass ? &CurMultiClass->
Entries :
nullptr);
372 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
386 bool Final, std::vector<RecordsEntry> *Dest,
389 for (
const auto &
S : Substs)
390 R.set(
S.first,
S.second);
392 auto LI = dyn_cast<ListInit>(List);
395 Dest->emplace_back(std::make_unique<ForeachLoop>(
Loop.Loc,
Loop.IterVar,
397 return resolve(
Loop.Entries, Substs, Final, &Dest->back().Loop->Entries,
402 List->getAsString() +
"', expected a list");
407 for (
auto Elt : *LI) {
409 Substs.emplace_back(
Loop.IterVar->getNameInit(), Elt);
410 Error = resolve(
Loop.Entries, Substs, Final, Dest);
424 bool TGParser::resolve(
const std::vector<RecordsEntry> &
Source,
425 SubstStack &Substs,
bool Final,
426 std::vector<RecordsEntry> *Dest,
SMLoc *Loc) {
430 Error = resolve(*
E.Loop, Substs, Final, Dest);
432 }
else if (
E.Assertion) {
434 for (
const auto &
S : Substs)
435 R.set(
S.first,
S.second);
436 Init *Condition =
E.Assertion->Condition->resolveReferences(R);
437 Init *Message =
E.Assertion->Message->resolveReferences(R);
440 Dest->push_back(std::make_unique<Record::AssertionInfo>(
441 E.Assertion->Loc, Condition, Message));
446 auto Rec = std::make_unique<Record>(*
E.Rec);
448 Rec->appendLoc(*Loc);
451 for (
const auto &
S : Substs)
452 R.set(
S.first,
S.second);
453 Rec->resolveReferences(R);
467 bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
468 Init *NewName =
nullptr;
469 if (
Record *Prev = Records.
getDef(Rec->getNameInitAsString())) {
470 if (!Rec->isAnonymous()) {
472 "def already exists: " + Rec->getNameInitAsString());
473 PrintNote(Prev->getLoc(),
"location of previous definition");
482 if (!isa<StringInit>(Rec->getNameInit())) {
484 Rec->getNameInit()->getAsString() +
485 "' could not be fully resolved");
490 Rec->checkRecordAssertions();
493 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
497 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
498 PrintError(Rec->getLoc(),
Twine(
"adding record of incompatible type '") +
499 I->getType()->getAsString() +
552 CurRec = &CurMultiClass->
Rec;
561 Name->resolveReferences(R);
575 Record *TGParser::ParseClassID() {
577 TokError(
"expected name for ClassID");
585 TokError(
Msg +
". Use 'defm' if you meant to use multiclass '" +
602 TokError(
"expected name for MultiClassID");
621 ParseSubClassReference(
Record *CurRec,
bool isDefm) {
629 Result.Rec = ParseClassID();
639 if (ParseTemplateArgValueList(
Result.TemplateArgs, CurRec,
Result.Rec)) {
644 if (CheckTemplateArgValues(
Result.TemplateArgs,
Result.RefRange.Start,
662 ParseSubMultiClassReference(
MultiClass *CurMC) {
666 Result.MC = ParseMultiClassID();
675 if (ParseTemplateArgValueList(
Result.TemplateArgs, &CurMC->
Rec,
694 Init *CurVal = FirstItem;
696 CurVal = ParseValue(
nullptr);
698 IntInit *II = dyn_cast_or_null<IntInit>(CurVal);
700 return TokError(
"expected integer or bitrange");
706 return TokError(
"invalid range, cannot be negative");
710 Ranges.push_back(Start);
717 Init *I_End = ParseValue(
nullptr);
718 IntInit *II_End = dyn_cast_or_null<IntInit>(I_End);
720 TokError(
"expected integer value as end of range");
734 return TokError(
"invalid range, cannot be negative");
738 for (; Start <= End; ++Start)
739 Ranges.push_back(Start);
741 for (; Start >= End; --Start)
742 Ranges.push_back(Start);
752 if (ParseRangePiece(Result)) {
758 if (ParseRangePiece(Result)) {
773 ParseRangeList(Ranges);
774 if (Ranges.empty())
return true;
777 TokError(
"expected '>' at end of range list");
778 return Error(StartLoc,
"to match this '<'");
792 ParseRangeList(Ranges);
793 if (Ranges.empty())
return true;
796 TokError(
"expected '}' at end of bit list");
797 return Error(StartLoc,
"to match this '{'");
813 RecTy *TGParser::ParseType() {
815 default:
TokError(
"Unknown token when expecting a type");
return nullptr;
830 if (
Record *R = ParseClassID())
836 TokError(
"expected '<' after bits type");
840 TokError(
"expected integer in bits<n> type");
845 TokError(
"expected '>' at end of bits<n> type");
853 TokError(
"expected '<' after list type");
857 RecTy *SubType = ParseType();
858 if (!SubType)
return nullptr;
861 TokError(
"expected '>' at end of list<ty> type");
877 if ((CurRec && CurRec->
isClass()) || CurMultiClass) {
878 Init *TemplateArgName;
885 Record *TemplateRec = CurMultiClass ? &CurMultiClass->
Rec : CurRec;
888 assert(RV &&
"Template arg doesn't exist??");
891 }
else if (
Name->getValue() ==
"NAME") {
897 if (
Init *
I = CurLocalScope->getVar(
Name->getValue()))
901 for (
const auto &L : Loops) {
903 VarInit *IterVar = dyn_cast<VarInit>(L->IterVar);
909 if (
Mode == ParseNameMode)
917 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
921 Error(NameLoc,
"Variable not defined: '" +
Name->getValue() +
"'");
950 Type = ParseOperatorType();
953 TokError(
"did not get type for unary operator");
987 Type = ParseOperatorType();
990 TokError(
"did not get type for unary operator");
994 if (!isa<RecordRecTy>(
Type)) {
995 TokError(
"type for !getdagop must be a record type");
1005 TokError(
"expected '(' after unary operator");
1009 Init *
LHS = ParseValue(CurRec);
1010 if (!
LHS)
return nullptr;
1017 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1018 TokError(
"expected string, list, or dag type argument in unary operator");
1025 if (!LType && !SType && !DType) {
1026 TokError(
"expected string, list, or dag type argument in unary operator");
1035 if (!LHSl && !LHSt) {
1036 TokError(
"expected list type argument in unary operator");
1042 TokError(
"expected list type argument in unary operator");
1047 if (LHSl && LHSl->
empty()) {
1048 TokError(
"empty list argument in unary operator");
1053 TypedInit *Itemt = dyn_cast<TypedInit>(Item);
1055 TokError(
"untyped list element in unary operator");
1061 assert(LHSt &&
"expected list type argument in unary operator");
1068 TokError(
"expected ')' in unary operator");
1083 TokError(
"expected '(' after type of !isa");
1087 Init *
LHS = ParseValue(CurRec);
1108 TokError(
"expected '(' after type of !exists");
1113 Init *Expr = ParseValue(CurRec);
1117 TypedInit *ExprType = dyn_cast<TypedInit>(Expr);
1119 Error(ExprLoc,
"expected string type argument in !exists operator");
1126 "expected string type argument in !exists operator, please "
1127 "use !isa instead");
1133 Error(ExprLoc,
"expected string type argument in !exists operator");
1138 TokError(
"expected ')' in !exists");
1197 RecTy *ArgType =
nullptr;
1243 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1244 Error(OpLoc,
Twine(
"expected value of type '") +
1246 Type->getAsString() +
"'");
1251 TokError(
"expected '(' after binary operator");
1261 InitList.push_back(ParseValue(CurRec, ArgType));
1262 if (!InitList.back())
return nullptr;
1264 TypedInit *InitListBack = dyn_cast<TypedInit>(InitList.back());
1265 if (!InitListBack) {
1266 Error(OpLoc,
Twine(
"expected value to be a typed value, got '" +
1267 InitList.back()->getAsString() +
"'"));
1278 if (!isa<ListRecTy>(ArgType)) {
1279 Error(InitLoc,
Twine(
"expected a list, got value of type '") +
1285 if (ItemType && InitList.size() == 1) {
1286 if (!isa<ListRecTy>(ItemType)) {
1288 Twine(
"expected output type to be a list, got type '") +
1293 Error(OpLoc,
Twine(
"expected first arg type to be '") +
1295 "', got value of type '" +
1296 cast<ListRecTy>(ItemType)
1303 if (InitList.size() == 2 && !isa<IntRecTy>(ArgType)) {
1304 Error(InitLoc,
Twine(
"expected second parameter to be an int, got "
1305 "value of type '") +
1316 Error(InitLoc,
Twine(
"expected bit, bits, int, string, or record; "
1328 Error(InitLoc,
Twine(
"expected bit, bits, int, or string; "
1335 switch (InitList.size()) {
1340 Error(InitLoc,
Twine(
"expected list of string, int, bits, or bit; "
1341 "got value of type '") +
1347 if (!isa<StringRecTy>(ArgType)) {
1348 Error(InitLoc,
Twine(
"expected second argument to be a string, "
1349 "got value of type '") +
1365 Error(InitLoc,
Twine(
"expected value of type '") +
1395 TokError(
"expected ')' in operator");
1404 Type = cast<TypedInit>(InitList.front())->getType()->getListTy();
1412 while (InitList.size() > 2) {
1415 InitList.back() =
RHS;
1419 if (InitList.size() == 2)
1423 Error(OpLoc,
"expected two operands to operator");
1429 return ParseOperationForEachFilter(CurRec, ItemType);
1455 TokError(
"expected '(' after ternary operator");
1459 Init *
LHS = ParseValue(CurRec);
1460 if (!
LHS)
return nullptr;
1463 TokError(
"expected ',' in ternary operator");
1468 Init *MHS = ParseValue(CurRec, ItemType);
1473 TokError(
"expected ',' in ternary operator");
1478 Init *
RHS = ParseValue(CurRec, ItemType);
1483 TokError(
"expected ')' in binary operator");
1490 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1491 if (!MHSt && !isa<UnsetInit>(MHS)) {
1492 Error(MHSLoc,
"could not determine type of the child list in !dag");
1495 if (MHSt && !isa<ListRecTy>(MHSt->
getType())) {
1496 Error(MHSLoc,
Twine(
"expected list of children, got type '") +
1502 if (!RHSt && !isa<UnsetInit>(
RHS)) {
1503 Error(RHSLoc,
"could not determine type of the name list in !dag");
1507 Error(RHSLoc,
Twine(
"expected list<string>, got type '") +
1512 if (!MHSt && !RHSt) {
1514 "cannot have both unset children and unset names in !dag");
1520 RecTy *MHSTy =
nullptr;
1521 RecTy *RHSTy =
nullptr;
1523 if (
TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1525 if (
BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1527 if (isa<BitInit>(MHS))
1534 if (isa<BitInit>(
RHS))
1538 if (isa<UnsetInit>(MHS))
1540 if (isa<UnsetInit>(
RHS))
1543 if (!MHSTy || !RHSTy) {
1544 TokError(
"could not get type for !if");
1559 TokError(
"could not get type for !subst");
1570 return ParseOperationSubstr(CurRec, ItemType);
1573 return ParseOperationFind(CurRec, ItemType);
1576 return ParseOperationCond(CurRec, ItemType);
1582 TokError(
"expected '(' after !foldl");
1586 Init *StartUntyped = ParseValue(CurRec);
1590 TypedInit *Start = dyn_cast<TypedInit>(StartUntyped);
1598 TokError(
"expected ',' in !foldl");
1602 Init *ListUntyped = ParseValue(CurRec);
1615 TokError(
Twine(
"!foldl list must be a list, but is of type '") +
1616 List->getType()->getAsString());
1621 TokError(
"expected ',' in !foldl");
1626 TokError(
"third argument of !foldl must be an identifier");
1631 if (CurRec && CurRec->
getValue(A)) {
1633 "' already defined")
1639 TokError(
"expected ',' in !foldl");
1644 TokError(
"fourth argument of !foldl must be an identifier");
1651 "' already defined")
1657 TokError(
"expected ',' in !foldl");
1664 std::unique_ptr<Record> ParseRecTmp;
1665 Record *ParseRec = CurRec;
1667 ParseRecTmp = std::make_unique<Record>(
".parse",
ArrayRef<SMLoc>{}, Records);
1668 ParseRec = ParseRecTmp.get();
1674 Init *ExprUntyped = ParseValue(ParseRec);
1675 ParseRec->removeValue(A);
1676 ParseRec->removeValue(
B);
1680 TypedInit *Expr = dyn_cast<TypedInit>(ExprUntyped);
1682 TokError(
"could not get type of !foldl expression");
1686 if (Expr->
getType() != Start->getType()) {
1687 TokError(
Twine(
"!foldl expression must be of same type as start (") +
1688 Start->getType()->getAsString() +
"), but is of type " +
1694 TokError(
"expected ')' in fold operator");
1709 RecTy *TGParser::ParseOperatorType() {
1713 TokError(
"expected type name for operator");
1718 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
1723 TokError(
"expected type name for operator");
1728 TokError(
"expected type name for operator");
1738 Init *TGParser::ParseOperationSubstr(
Record *CurRec,
RecTy *ItemType) {
1745 TokError(
"expected '(' after !substr operator");
1749 Init *
LHS = ParseValue(CurRec);
1754 TokError(
"expected ',' in !substr operator");
1759 Init *MHS = ParseValue(CurRec);
1767 RHS = ParseValue(CurRec);
1775 TokError(
"expected ')' in !substr operator");
1779 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1780 Error(RHSLoc,
Twine(
"expected value of type '") +
1782 Type->getAsString() +
"'");
1786 if (!LHSt && !isa<UnsetInit>(
LHS)) {
1787 TokError(
"could not determine type of the string in !substr");
1790 if (LHSt && !isa<StringRecTy>(LHSt->
getType())) {
1796 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1797 if (!MHSt && !isa<UnsetInit>(MHS)) {
1798 TokError(
"could not determine type of the start position in !substr");
1801 if (MHSt && !isa<IntRecTy>(MHSt->
getType())) {
1802 Error(MHSLoc,
Twine(
"expected int, got type '") +
1809 if (!RHSt && !isa<UnsetInit>(
RHS)) {
1810 TokError(
"could not determine type of the length in !substr");
1813 if (RHSt && !isa<IntRecTy>(RHSt->
getType())) {
1833 TokError(
"expected '(' after !find operator");
1837 Init *
LHS = ParseValue(CurRec);
1842 TokError(
"expected ',' in !find operator");
1847 Init *MHS = ParseValue(CurRec);
1855 RHS = ParseValue(CurRec);
1863 TokError(
"expected ')' in !find operator");
1867 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1868 Error(RHSLoc,
Twine(
"expected value of type '") +
1870 Type->getAsString() +
"'");
1874 if (!LHSt && !isa<UnsetInit>(
LHS)) {
1875 TokError(
"could not determine type of the source string in !find");
1878 if (LHSt && !isa<StringRecTy>(LHSt->
getType())) {
1884 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1885 if (!MHSt && !isa<UnsetInit>(MHS)) {
1886 TokError(
"could not determine type of the target string in !find");
1889 if (MHSt && !isa<StringRecTy>(MHSt->
getType())) {
1890 Error(MHSLoc,
Twine(
"expected string, got type '") +
1897 if (!RHSt && !isa<UnsetInit>(
RHS)) {
1898 TokError(
"could not determine type of the start position in !find");
1901 if (RHSt && !isa<IntRecTy>(RHSt->
getType())) {
1915 Init *TGParser::ParseOperationForEachFilter(
Record *CurRec,
RecTy *ItemType) {
1920 TokError(
"expected '(' after !foreach/!filter");
1925 TokError(
"first argument of !foreach/!filter must be an identifier");
1934 "' is already defined")
1940 TokError(
"expected ',' in !foreach/!filter");
1944 Init *MHS = ParseValue(CurRec);
1949 TokError(
"expected ',' in !foreach/!filter");
1953 TypedInit *MHSt = dyn_cast<TypedInit>(MHS);
1955 TokError(
"could not get type of !foreach/!filter list or dag");
1959 RecTy *InEltType =
nullptr;
1960 RecTy *ExprEltType =
nullptr;
1964 InEltType = InListTy->getElementType();
1966 if (
ListRecTy *OutListTy = dyn_cast<ListRecTy>(ItemType)) {
1968 ? OutListTy->getElementType()
1972 "expected value of type '" +
1974 "', but got list type");
1980 TokError(
"!filter must have a list argument");
1983 InEltType = InDagTy;
1984 if (ItemType && !isa<DagRecTy>(ItemType)) {
1987 "', but got dag type");
1993 TokError(
"!foreach must have a list or dag argument");
1995 TokError(
"!filter must have a list argument");
2001 std::unique_ptr<Record> ParseRecTmp;
2002 Record *ParseRec = CurRec;
2006 ParseRec = ParseRecTmp.get();
2010 Init *
RHS = ParseValue(ParseRec, ExprEltType);
2011 ParseRec->removeValue(
LHS);
2016 TokError(
"expected ')' in !foreach/!filter");
2020 RecTy *OutType = InEltType;
2024 TokError(
"could not get type of !foreach result expression");
2042 TokError(
"expected '(' after !cond operator");
2053 Init *V = ParseValue(CurRec);
2059 TokError(
"expected ':' following a condition in !cond operator");
2063 V = ParseValue(CurRec, ItemType);
2072 TokError(
"expected ',' or ')' following a value in !cond operator");
2077 if (Case.size() < 1) {
2078 TokError(
"there should be at least 1 'condition : value' in the !cond operator");
2084 for (
Init *V : Val) {
2085 RecTy *VTy =
nullptr;
2086 if (
TypedInit *Vt = dyn_cast<TypedInit>(V))
2087 VTy = Vt->getType();
2088 if (
BitsInit *Vbits = dyn_cast<BitsInit>(V))
2090 if (isa<BitInit>(V))
2093 if (
Type ==
nullptr) {
2094 if (!isa<UnsetInit>(V))
2097 if (!isa<UnsetInit>(V)) {
2110 TokError(
"could not determine type for !cond from its arguments");
2142 default:
TokError(
"Unknown or reserved token when parsing a value");
break;
2159 for (
unsigned i = 0,
e = BinaryVal.second;
i !=
e; ++
i)
2190 return ParseIDValue(CurRec,
Name, NameLoc,
Mode);
2197 Error(NameLoc,
"Expected a class name, got '" +
Name->getValue() +
"'");
2203 if (ParseTemplateArgValueList(
Args, CurRec, Class))
2206 if (CheckTemplateArgValues(
Args, NameLoc, Class))
2212 for (
unsigned I =
Args.size(),
E = TArgs.
size();
I <
E; ++
I) {
2214 if (!
Arg->getValue()->isComplete())
2215 Error(NameLoc,
"Value not specified for template argument '" +
2216 TArgs[
I]->getAsUnquotedString() +
"' (#" +
Twine(
I) +
2217 ") of parent class '" +
2218 Class->getNameInitAsString() +
"'");
2230 ParseValueList(Vals, CurRec);
2231 if (Vals.empty())
return nullptr;
2234 TokError(
"expected '}' at end of bit list value");
2243 for (
unsigned i = 0,
e = Vals.size();
i !=
e; ++
i) {
2248 if (
BitsInit *BI = dyn_cast<BitsInit>(Vals[
i])) {
2249 for (
unsigned i = 0,
e = BI->getNumBits();
i !=
e; ++
i)
2250 NewBits.push_back(BI->
getBit((
e -
i) - 1));
2254 if (
VarInit *
VI = dyn_cast<VarInit>(Vals[
i])) {
2255 if (
BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(
VI->getType())) {
2256 for (
unsigned i = 0,
e = BitsRec->getNumBits();
i !=
e; ++
i)
2257 NewBits.push_back(
VI->getBit((
e -
i) - 1));
2265 Error(BraceLoc,
"Element #" +
Twine(
i) +
" (" + Vals[
i]->getAsString() +
2266 ") is not convertable to a bit");
2269 NewBits.push_back(
Bit);
2278 RecTy *DeducedEltTy =
nullptr;
2282 ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType);
2288 GivenListTy = ListType;
2292 ParseValueList(Vals, CurRec,
2294 if (Vals.empty())
return nullptr;
2297 TokError(
"expected ']' at end of list value");
2301 RecTy *GivenEltTy =
nullptr;
2304 GivenEltTy = ParseType();
2311 TokError(
"expected '>' at end of list element type");
2317 RecTy *EltTy =
nullptr;
2318 for (
Init *V : Vals) {
2319 TypedInit *TArg = dyn_cast<TypedInit>(V);
2324 TokError(
"Incompatible types in list elements");
2337 TokError(
"Incompatible types in list elements");
2354 TokError(
Twine(
"Element type mismatch for list: element type '") +
2360 DeducedEltTy = EltTy;
2369 TokError(
"expected identifier in dag init");
2380 TokError(
"expected variable name in dag operator");
2389 ParseDagArgList(DagArgs, CurRec);
2390 if (DagArgs.empty())
return nullptr;
2394 TokError(
"expected ')' in dag init");
2440 return ParseOperation(CurRec, ItemType);
2456 if (!Result)
return nullptr;
2463 if (
Mode == ParseNameMode)
2470 ParseRangeList(Ranges);
2471 if (Ranges.empty())
return nullptr;
2477 Error(CurlyLoc,
"Invalid bit range for value");
2483 TokError(
"expected '}' at end of bit range list");
2492 ParseRangeList(Ranges);
2493 if (Ranges.empty())
return nullptr;
2497 Error(SquareLoc,
"Invalid range for list slice");
2503 TokError(
"expected ']' at end of list slice");
2510 TokError(
"expected field identifier after '.'");
2514 if (!
Result->getFieldType(FieldName)) {
2516 Result->getAsString() +
"'");
2528 Error(PasteLoc,
"LHS of paste is not typed!");
2536 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
2545 Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
2557 auto CastLHS = dyn_cast<TypedInit>(
2562 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
2584 Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
2587 RHS = dyn_cast<TypedInit>(RHSResult);
2589 Error(PasteLoc,
"RHS of paste is not typed!");
2594 auto CastRHS = dyn_cast<TypedInit>(
2599 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
2620 void TGParser::ParseDagArgList(
2633 Init *Val = ParseValue(CurRec);
2643 TokError(
"expected variable name in dag literal");
2667 Result.push_back(ParseValue(CurRec, ItemType));
2677 Result.push_back(ParseValue(CurRec, ItemType));
2694 assert(
Result.empty() &&
"Result vector is not empty");
2696 unsigned ArgIndex = 0;
2703 if (ArgIndex >= TArgs.
size()) {
2704 TokError(
"Too many template arguments: " + utostr(ArgIndex + 1));
2708 assert(
Arg &&
"Template argument record not found");
2710 ItemType =
Arg->getType();
2711 Init *
Value = ParseValue(CurRec, ItemType);
2719 return TokError(
"Expected comma before next argument");
2734 Init *TGParser::ParseDeclaration(
Record *CurRec,
2735 bool ParsingTemplateArgs) {
2740 if (!
Type)
return nullptr;
2743 TokError(
"Expected identifier in declaration");
2748 if (Str ==
"NAME") {
2749 TokError(
"'" + Str +
"' is a reserved variable name");
2758 if (!ParsingTemplateArgs) {
2759 BadField = AddValue(CurRec, IdLoc,
2764 }
else if (CurRec) {
2765 DeclName =
QualifyName(*CurRec, CurMultiClass, DeclName,
":");
2766 BadField = AddValue(CurRec, IdLoc,
RecordVal(DeclName, IdLoc,
Type,
2770 assert(CurMultiClass &&
"invalid context for template argument");
2771 DeclName =
QualifyName(CurMultiClass->
Rec, CurMultiClass, DeclName,
"::");
2772 BadField = AddValue(CurRec, IdLoc,
RecordVal(DeclName, IdLoc,
Type,
2781 Init *Val = ParseValue(CurRec,
Type);
2783 SetValue(CurRec, ValLoc, DeclName,
None, Val))
2801 VarInit *TGParser::ParseForeachDeclaration(
Init *&ForeachListValue) {
2803 TokError(
"Expected identifier in foreach declaration");
2812 TokError(
"Expected '=' in foreach declaration");
2816 RecTy *IterType =
nullptr;
2822 ParseRangeList(Ranges);
2824 TokError(
"expected '}' at end of bit range list");
2832 Init *
I = ParseValue(
nullptr);
2837 if (TI && isa<ListRecTy>(TI->
getType())) {
2838 ForeachListValue =
I;
2839 IterType = cast<ListRecTy>(TI->
getType())->getElementType();
2844 if (ParseRangePiece(Ranges, TI))
2849 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
2850 if (CurMultiClass) {
2851 PrintNote({},
"references to multiclass template arguments cannot be "
2852 "resolved at this time");
2859 if (!Ranges.empty()) {
2860 assert(!IterType &&
"Type already initialized?");
2862 std::vector<Init *> Values;
2863 for (
unsigned R : Ranges)
2881 bool TGParser::ParseTemplateArgList(
Record *CurRec) {
2885 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->
Rec;
2888 Init *TemplArg = ParseDeclaration(CurRec,
true);
2897 TemplArg = ParseDeclaration(CurRec,
true);
2902 return Error(Loc,
"template argument with the same name has already been "
2909 return TokError(
"expected '>' at end of template argument list");
2920 bool TGParser::ParseBodyItem(
Record *CurRec) {
2922 return ParseAssert(
nullptr, CurRec);
2925 return ParseDefvar();
2928 if (!ParseDeclaration(CurRec,
false))
2932 return TokError(
"expected ';' after declaration");
2938 return TokError(
"expected field identifier after let");
2945 if (ParseOptionalBitList(BitList))
2950 return TokError(
"expected '=' in let expression");
2957 if (!BitList.empty() && isa<BitsRecTy>(
Type)) {
2963 Init *Val = ParseValue(CurRec,
Type);
2964 if (!Val)
return true;
2967 return TokError(
"expected ';' after let expression");
2969 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
2979 bool TGParser::ParseBody(
Record *CurRec) {
2985 return TokError(
"Expected '{' to start body or ';' for declaration only");
2991 if (ParseBodyItem(CurRec))
3002 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3003 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3011 bool TGParser::ApplyLetStack(
Record *CurRec) {
3014 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
3022 return ApplyLetStack(Entry.Rec.get());
3025 if (Entry.Assertion)
3028 for (
auto &
E : Entry.Loop->Entries) {
3029 if (ApplyLetStack(
E))
3045 bool TGParser::ParseObjectBody(
Record *CurRec) {
3053 if (!SubClass.
Rec)
return true;
3056 if (AddSubClass(CurRec, SubClass))
3061 SubClass = ParseSubClassReference(CurRec,
false);
3065 if (ApplyLetStack(CurRec))
3068 return ParseBody(CurRec);
3076 bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3082 std::unique_ptr<Record> CurRec;
3083 Init *
Name = ParseObjectName(CurMultiClass);
3087 if (isa<UnsetInit>(
Name))
3091 CurRec = std::make_unique<Record>(
Name, DefLoc, Records);
3093 if (ParseObjectBody(CurRec.get()))
3103 bool TGParser::ParseDefset() {
3112 if (!isa<ListRecTy>(
Type))
3114 Defset.EltTy = cast<ListRecTy>(
Type)->getElementType();
3117 return TokError(
"expected identifier");
3120 return TokError(
"def or global variable of this name already exists");
3129 Defsets.push_back(&
Defset);
3130 bool Err = ParseObjectList(
nullptr);
3136 TokError(
"expected '}' at end of defset");
3137 return Error(BraceLoc,
"to match this '{'");
3149 bool TGParser::ParseDefvar() {
3154 return TokError(
"expected identifier");
3156 if (CurLocalScope) {
3157 if (CurLocalScope->varAlreadyDefined(DeclName->
getValue()))
3158 return TokError(
"local variable of this name already exists");
3161 return TokError(
"def or global variable of this name already exists");
3189 bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
3196 Init *ListValue =
nullptr;
3197 VarInit *IterName = ParseForeachDeclaration(ListValue);
3199 return TokError(
"expected declaration in for");
3205 Loops.push_back(std::make_unique<ForeachLoop>(Loc, IterName, ListValue));
3212 if (ParseObject(CurMultiClass))
3220 if (ParseObjectList(CurMultiClass))
3224 TokError(
"expected '}' at end of foreach command");
3225 return Error(BraceLoc,
"to match this '{'");
3243 bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
3250 Init *Condition = ParseValue(
nullptr);
3270 Init *ThenClauseList =
3274 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
3276 if (ParseIfBody(CurMultiClass,
"then"))
3292 Init *ElseClauseList =
3297 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
3299 if (ParseIfBody(CurMultiClass,
"else"))
3322 if (ParseObject(CurMultiClass))
3330 if (ParseObjectList(CurMultiClass))
3334 TokError(
"expected '}' at end of '" +
Kind +
"' clause");
3335 return Error(BraceLoc,
"to match this '{'");
3351 Init *Condition = ParseValue(CurRec);
3356 TokError(
"expected ',' in assert statement");
3360 Init *Message = ParseValue(CurRec);
3368 CurRec->
addAssertion(ConditionLoc, Condition, Message);
3370 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
3379 bool TGParser::ParseClass() {
3384 return TokError(
"expected class name after 'class' keyword");
3393 "' already defined");
3399 CurRec = NewRec.get();
3406 if (ParseTemplateArgList(CurRec))
3409 if (ParseObjectBody(CurRec))
3412 if (!NoWarnOnUnusedTemplateArgs)
3426 TokError(
"expected identifier in let definition");
3437 if (ParseOptionalRangeList(
Bits)) {
3444 TokError(
"expected '=' in let expression");
3449 Init *Val = ParseValue(
nullptr);
3466 bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
3472 ParseLetList(LetInfo);
3473 if (LetInfo.empty())
return true;
3477 return TokError(
"expected 'in' at end of top-level 'let'");
3484 if (ParseObject(CurMultiClass))
3492 if (ParseObjectList(CurMultiClass))
3496 TokError(
"expected '}' at end of top level let command");
3497 return Error(BraceLoc,
"to match this '{'");
3504 LetStack.pop_back();
3521 bool TGParser::ParseMultiClass() {
3526 return TokError(
"expected identifier after multiclass for name");
3530 MultiClasses.insert(std::make_pair(
Name,
3531 std::make_unique<MultiClass>(
Name, Lex.
getLoc(),Records)));
3534 return TokError(
"multiclass '" +
Name +
"' already defined");
3536 CurMultiClass =
Result.first->second.get();
3541 if (ParseTemplateArgList(
nullptr))
3544 bool inherits =
false;
3552 ParseSubMultiClassReference(CurMultiClass);
3555 if (!SubMultiClass.
MC)
return true;
3558 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
3563 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
3569 return TokError(
"expected '{' in multiclass definition");
3571 return TokError(
"expected ';' in multiclass definition");
3574 return TokError(
"multiclass must contain at least one def");
3582 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', "
3583 "'foreach', 'if', or 'let' in multiclass body");
3592 if (ParseObject(CurMultiClass))
3602 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
3603 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3609 if (!NoWarnOnUnusedTemplateArgs)
3612 CurMultiClass =
nullptr;
3620 bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
3624 Init *DefmName = ParseObjectName(CurMultiClass);
3627 if (isa<UnsetInit>(DefmName)) {
3637 return TokError(
"expected ':' after defm identifier");
3640 std::vector<RecordsEntry> NewEntries;
3643 bool InheritFromClass =
false;
3652 if (!
Ref.Rec)
return true;
3658 MultiClass *MC = MultiClasses[std::string(
Ref.Rec->getName())].get();
3659 assert(MC &&
"Didn't lookup multiclass correctly?");
3665 for (
unsigned i = 0,
e = TArgs.
size();
i !=
e; ++
i) {
3666 if (
i < TemplateVals.
size()) {
3667 Substs.emplace_back(TArgs[
i], TemplateVals[
i]);
3671 return Error(SubClassLoc,
3672 "value not specified for template argument '" +
3673 TArgs[
i]->getAsUnquotedString() +
"' (#" +
3674 Twine(
i) +
") of multiclass '" +
3676 Substs.emplace_back(TArgs[
i], Default);
3682 if (resolve(MC->
Entries, Substs, !CurMultiClass &&
Loops.empty(),
3683 &NewEntries, &SubClassLoc))
3690 return TokError(
"expected identifier");
3692 SubClassLoc = Lex.
getLoc();
3698 if (InheritFromClass)
3701 Ref = ParseSubClassReference(
nullptr,
true);
3704 if (InheritFromClass) {
3710 if (!SubClass.
Rec)
return true;
3714 for (
auto &
E : NewEntries) {
3716 if (AddSubClass(
E, SubClass))
3722 SubClass = ParseSubClassReference(
nullptr,
false);
3726 for (
auto &
E : NewEntries) {
3727 if (ApplyLetStack(
E))
3734 return TokError(
"expected ';' at end of defm");
3753 "Expected assert, class, def, defm, defset, foreach, if, or let");
3760 case tgtok::Let:
return ParseTopLevelLet(MC);
3763 return TokError(
"defset is not allowed inside multiclass");
3764 return ParseDefset();
3767 return TokError(
"class is not allowed inside multiclass");
3769 return TokError(
"class is not allowed inside foreach loop");
3770 return ParseClass();
3773 return TokError(
"multiclass is not allowed inside foreach loop");
3774 return ParseMultiClass();
3780 bool TGParser::ParseObjectList(
MultiClass *MC) {
3782 if (ParseObject(MC))
3790 if (ParseObjectList())
return true;
3796 return TokError(
"Unexpected token at top level");
3808 for (
unsigned I = 0,
E = Values.size();
I <
E; ++
I) {
3814 auto *CastValue = ArgValue->getCastTo(ArgType);
3816 assert((!isa<TypedInit>(CastValue) ||
3817 cast<TypedInit>(CastValue)->
getType()->typeIsA(ArgType)) &&
3818 "result of template arg value cast has wrong type");
3819 Values[
I] = CastValue;
3822 "Value specified for template argument '" +
3823 Arg->getNameInitAsString() +
"' (#" +
Twine(
I) +
3824 ") is of type " + ArgValue->getType()->getAsString() +
3825 "; expected type " + ArgType->
getAsString() +
": " +
3826 ArgValue->getAsString());
3834 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3853 errs() <<
"Record:\n";
3856 errs() <<
"Defs:\n";