19#include "llvm/Config/llvm-config.h"
66#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
68 errs() <<
"Multiclass:\n";
72 errs() <<
"Template args:\n";
80 for (
unsigned i = 0, e = BV->getNumBits(); i != e; ++i) {
81 const Init *Bit = BV->getBit(i);
82 bool IsReference =
false;
85 if (R.getValue(VI->getName()))
91 if (!(IsReference || Bit->isConcrete()))
98 for (
const RecordVal &RV : R.getValues()) {
103 if (RV.isNonconcreteOK())
106 if (
const Init *V = RV.getValue()) {
110 RV.getNameInitAsString() +
"' in '" +
111 R.getNameInitAsString() +
112 "' could not be fully resolved: " +
113 RV.getValue()->getAsString());
129 NewName = BinOp->Fold(&CurRec);
149 bool TrackReferenceLocs)
const {
151 auto It = Vars.find(Name->getValue());
152 if (It != Vars.end())
155 auto FindValueInArgs = [&](
Record *Rec,
162 assert(RV &&
"Template arg doesn't exist??");
164 if (TrackReferenceLocs)
168 return Name->getValue() ==
"NAME"
182 if (TrackReferenceLocs)
183 RV->addReferenceLoc(NameLoc);
188 if (CurRec->isClass())
189 if (
auto *V = FindValueInArgs(CurRec, Name))
196 if (CurLoop->IterVar) {
198 if (IterVar && IterVar->getNameInit() == Name)
206 if (
auto *V = FindValueInArgs(&CurMultiClass->Rec, Name))
214 return Parent->getVar(Records, ParsingMultiClass, Name, NameLoc,
222 CurRec = &CurMultiClass->
Rec;
227 return Error(
Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
229 "' is incompatible with " +
230 "previous definition of type '" +
231 ERV->getType()->getAsString() +
"'");
242 bool AllowSelfAssignment,
bool OverrideDefLoc,
248 CurRec = &CurMultiClass->Rec;
250 RecordVal *RV = CurRec->
getValue(ValName);
259 if (!BitList.
empty())
260 return Error(Loc,
"Cannot use append/prepend with bit range");
262 const Init *CurrentValue = RV->
getValue();
263 const RecTy *FieldType = RV->
getType();
269 const Init *
LHS = IsAppendMode ? CurrentValue :
V;
270 const Init *
RHS = IsAppendMode ?
V : CurrentValue;
280 return Error(Loc, Twine(
"Cannot ") +
281 (IsAppendMode ?
"append to" :
"prepend to") +
283 "' of type '" + FieldType->getAsString() +
284 "' (expected list, string, code, or dag)");
294 if (
VI->getNameInit() == ValName && !AllowSelfAssignment)
295 return Error(Loc,
"Recursion / self-assignment forbidden");
299 if (!BitList.
empty()) {
303 "' is not a bits type");
308 return Error(Loc,
"Initializer is not compatible with bit range");
313 for (
unsigned i = 0, e = BitList.
size(); i != e; ++i) {
314 unsigned Bit = BitList[i];
316 return Error(Loc,
"Cannot set bit #" + Twine(Bit) +
" of value '" +
322 for (
unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i)
324 NewBits[i] = CurVal->getBit(i);
330 std::string InitType;
332 InitType = (Twine(
"' of type bit initializer with length ") +
333 Twine(BI->getNumBits()))
337 (Twine(
"' of type '") + TI->getType()->getAsString() +
"'").str();
341 "' is incompatible with value '" +
V->getAsString() +
351 MapResolver
R(CurRec);
356 if (!
Field.isTemplateArg())
360 if (resolveArgumentsOfClass(R, SC, SubClass.
TemplateArgs,
384 "Already subclass of '" + SC->
getName() +
"'!\n");
391 return AddSubClass(
Entry.Rec.get(), SubClass);
396 for (
auto &
E :
Entry.Loop->Entries) {
397 if (AddSubClass(
E, SubClass))
407bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
412 if (resolveArgumentsOfMultiClass(
425 assert((!!
E.Rec + !!
E.Loop + !!
E.Assertion + !!
E.Dump) == 1 &&
426 "RecordsEntry has invalid number of items");
429 if (!Loops.empty()) {
430 Loops.back()->Entries.push_back(std::move(
E));
437 return resolve(*
E.Loop, Stack, CurMultiClass ==
nullptr,
438 CurMultiClass ? &CurMultiClass->Entries :
nullptr);
443 CurMultiClass->Entries.push_back(std::move(
E));
449 CheckAssert(
E.Assertion->Loc,
E.Assertion->Condition,
E.Assertion->Message);
459 return addDefOne(std::move(
E.Rec));
467bool TGParser::resolve(
const ForeachLoop &
Loop, SubstStack &Substs,
bool Final,
468 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
471 for (
const auto &S : Substs)
472 R.set(S.first, S.second);
483 const Init *OldLHS = TI->getLHS();
487 PrintError(Loop.
Loc, Twine(
"unable to resolve if condition '") +
489 "' at end of containing scope");
492 const Init *MHS = TI->getMHS();
493 const Init *
RHS = TI->getRHS();
503 return resolve(Loop.
Entries, Substs, Final, &Dest->back().Loop->Entries,
508 List->getAsString() +
"', expected a list");
513 for (
auto *Elt : *LI) {
530bool TGParser::resolve(
const std::vector<RecordsEntry> &Source,
531 SubstStack &Substs,
bool Final,
532 std::vector<RecordsEntry> *Dest,
SMLoc *
Loc) {
534 for (
auto &
E : Source) {
536 Error = resolve(*
E.Loop, Substs, Final, Dest);
538 }
else if (
E.Assertion) {
540 for (
const auto &S : Substs)
541 R.set(S.first, S.second);
542 const Init *Condition =
E.Assertion->Condition->resolveReferences(R);
543 const Init *Message =
E.Assertion->Message->resolveReferences(R);
546 Dest->push_back(std::make_unique<Record::AssertionInfo>(
547 E.Assertion->Loc, Condition, Message));
553 for (
const auto &S : Substs)
554 R.set(S.first, S.second);
559 std::make_unique<Record::DumpInfo>(
E.Dump->Loc, Message));
564 auto Rec = std::make_unique<Record>(*
E.Rec);
566 Rec->appendLoc(*Loc);
568 MapResolver
R(Rec.get());
569 for (
const auto &S : Substs)
570 R.set(S.first, S.second);
571 Rec->resolveReferences(R);
574 Dest->push_back(std::move(Rec));
576 Error = addDefOne(std::move(Rec));
585bool TGParser::addDefOne(std::unique_ptr<Record> Rec) {
586 const Init *NewName =
nullptr;
587 if (
const Record *Prev = Records.getDef(Rec->getNameInitAsString())) {
588 if (!Rec->isAnonymous()) {
590 "def already exists: " + Rec->getNameInitAsString());
591 PrintNote(Prev->getLoc(),
"location of previous definition");
594 NewName = Records.getNewAnonymousName();
597 Rec->resolveReferences(NewName);
601 PrintError(Rec->getLoc(), Twine(
"record name '") +
602 Rec->getNameInit()->getAsString() +
603 "' could not be fully resolved");
608 Rec->checkRecordAssertions();
611 Rec->emitRecordDumps();
614 assert(Rec->getTemplateArgs().empty() &&
"How'd this get template args?");
616 for (DefsetRecord *Defset : Defsets) {
617 DefInit *
I = Rec->getDefInit();
618 if (!
I->getType()->typeIsA(
Defset->EltTy)) {
619 PrintError(Rec->getLoc(), Twine(
"adding record of incompatible type '") +
620 I->getType()->getAsString() +
628 Records.addDef(std::move(Rec));
632bool TGParser::resolveArguments(
const Record *Rec,
634 SMLoc Loc, ArgValueHandler ArgValueHandler) {
637 "Too many template arguments allowed");
641 for (
auto *Arg : ArgValues) {
642 const Init *ArgName =
nullptr;
643 const Init *ArgValue = Arg->getValue();
644 if (Arg->isPositional())
645 ArgName = ArgNames[Arg->getIndex()];
647 ArgName = Arg->getName();
651 return Error(Loc,
"We can only specify the template argument '" +
654 ArgValueHandler(ArgName, ArgValue);
659 for (
auto *UnsolvedArgName : UnsolvedArgNames) {
662 std::string
Name = UnsolvedArgName->getAsUnquotedString();
663 Error(Loc,
"value not specified for template argument '" + Name +
"'");
668 ArgValueHandler(UnsolvedArgName,
Default);
679 return resolveArguments(
681 [&](
const Init *Name,
const Init *
Value) {
R.set(Name,
Value); });
686bool TGParser::resolveArgumentsOfMultiClass(
691 return resolveArguments(&MC->
Rec, ArgValues, Loc,
692 [&](
const Init *Name,
const Init *
Value) {
693 Substs.emplace_back(Name, Value);
702 if (Lex.getCode() == K) {
716 switch (Lex.getCode()) {
730 CurRec = &CurMultiClass->Rec;
739 HasReferenceResolver
R(NameStr);
740 Name->resolveReferences(R);
754const Record *TGParser::ParseClassID() {
756 TokError(
"expected name for ClassID");
760 const Record *
Result = Records.getClass(Lex.getCurStrVal());
762 std::string Msg(
"Couldn't find class '" + Lex.getCurStrVal() +
"'");
763 if (MultiClasses[Lex.getCurStrVal()].get())
764 TokError(Msg +
". Use 'defm' if you meant to use multiclass '" +
765 Lex.getCurStrVal() +
"'");
768 }
else if (TrackReferenceLocs) {
769 Result->appendReferenceLoc(Lex.getLocRange());
783 TokError(
"expected name for MultiClassID");
789 TokError(
"Couldn't find multiclass '" + Lex.getCurStrVal() +
"'");
804 Result.RefRange.Start = Lex.getLoc();
807 if (MultiClass *MC = ParseMultiClassID())
810 Result.Rec = ParseClassID();
817 Result.RefRange.End = Lex.getLoc();
822 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, CurRec,
828 if (CheckTemplateArgValues(
Result.TemplateArgs, ArgLocs,
Result.Rec)) {
833 Result.RefRange.End = Lex.getLoc();
845TGParser::ParseSubMultiClassReference(
MultiClass *CurMC) {
846 SubMultiClassReference
Result;
847 Result.RefRange.Start = Lex.getLoc();
849 Result.MC = ParseMultiClassID();
855 Result.RefRange.End = Lex.getLoc();
860 if (ParseTemplateArgValueList(
Result.TemplateArgs, ArgLocs, &CurMC->
Rec,
866 Result.RefRange.End = Lex.getLoc();
882 auto LHSLoc = Lex.getLoc();
883 auto *CurVal = ParseValue(CurRec);
888 const TypedInit *
RHS =
nullptr;
889 switch (Lex.getCode()) {
893 auto RHSLoc = Lex.getLoc();
894 CurVal = ParseValue(CurRec);
900 "expected int...int, got " + Twine(
RHS->
getType()->getAsString()));
906 auto i = -Lex.getCurIntVal();
908 TokError(
"invalid range, cannot be negative");
925 "expected int...int, got " + Twine(
LHS->
getType()->getAsString()));
945const TypedInit *TGParser::ParseSliceElements(
Record *CurRec,
bool Single) {
946 const TypedInit *CurVal;
950 auto FlushElems = [&] {
951 if (!Elems.
empty()) {
958 auto LHSLoc = Lex.getLoc();
959 CurVal = ParseSliceElement(CurRec);
962 auto *CurValTy = CurVal->
getType();
967 "expected list<int>, got " + Twine(ListValTy->getAsString()));
977 "unhandled type " + Twine(CurValTy->getAsString()) +
" in range");
1004 const TypedInit *
Result =
nullptr;
1005 for (
auto *Slice : Slices) {
1021 const Init *CurVal = FirstItem;
1023 CurVal = ParseValue(
nullptr);
1027 return TokError(
"expected integer or bitrange");
1029 int64_t
Start =
II->getValue();
1033 return TokError(
"invalid range, cannot be negative");
1035 switch (Lex.getCode()) {
1044 const Init *I_End = ParseValue(
nullptr);
1047 TokError(
"expected integer value as end of range");
1051 End = II_End->getValue();
1055 End = -Lex.getCurIntVal();
1061 return TokError(
"invalid range, cannot be negative");
1079 if (ParseRangePiece(Result)) {
1085 if (ParseRangePiece(Result)) {
1095 SMLoc StartLoc = Lex.getLoc();
1100 ParseRangeList(Ranges);
1105 TokError(
"expected '>' at end of range list");
1106 return Error(StartLoc,
"to match this '<'");
1115 SMLoc StartLoc = Lex.getLoc();
1120 ParseRangeList(Ranges);
1125 TokError(
"expected '}' at end of bit list");
1126 return Error(StartLoc,
"to match this '{'");
1142const RecTy *TGParser::ParseType() {
1143 switch (Lex.getCode()) {
1145 TokError(
"Unknown token when expecting a type");
1161 auto I = TypeAliases.find(Lex.getCurStrVal());
1162 if (
I != TypeAliases.end()) {
1166 if (
const Record *R = ParseClassID())
1173 TokError(
"expected '<' after bits type");
1177 TokError(
"expected integer in bits<n> type");
1180 uint64_t Val = Lex.getCurIntVal();
1182 TokError(
"expected '>' at end of bits<n> type");
1190 TokError(
"expected '<' after list type");
1194 const RecTy *SubType = ParseType();
1199 TokError(
"expected '>' at end of list<ty> type");
1210 if (
const Init *
I = CurScope->getVar(Records, CurMultiClass, Name, NameLoc,
1211 TrackReferenceLocs))
1214 if (
Mode == ParseNameMode)
1217 if (
const Init *
I = Records.getGlobal(
Name->getValue())) {
1219 if (TrackReferenceLocs) {
1221 Def->getDef()->appendReferenceLoc(NameLoc);
1228 if (CurRec && !CurRec->
isClass() && !CurMultiClass &&
1232 Error(NameLoc.
Start,
"Variable not defined: '" +
Name->getValue() +
"'");
1240const Init *TGParser::ParseOperation(
Record *CurRec,
const RecTy *ItemType) {
1241 switch (Lex.getCode()) {
1260 const RecTy *
Type =
nullptr;
1262 switch (Lex.getCode()) {
1269 Type = ParseOperatorType();
1272 TokError(
"did not get type for unary operator");
1331 Type = ParseOperatorType();
1334 TokError(
"did not get type for unary operator");
1339 TokError(
"type for !getdagop must be a record type");
1359 TokError(
"expected '(' after unary operator");
1363 const Init *
LHS = ParseValue(CurRec);
1372 if (!LHSl && !LHSs && !LHSd && !LHSt) {
1374 "expected string, list, or dag type argument in unary operator");
1380 "expected string, list, or dag type argument in unary operator");
1390 if (!LHSl && !LHSt) {
1391 TokError(
"expected list type argument in unary operator");
1396 TokError(
"expected list type argument in unary operator");
1401 if (LHSl && LHSl->empty()) {
1402 TokError(
"empty list argument in unary operator");
1405 bool UseElementType =
1408 const Init *Item = LHSl->getElement(0);
1411 TokError(
"untyped list element in unary operator");
1414 Type = UseElementType ? Itemt->getType()
1417 assert(LHSt &&
"expected list type argument in unary operator");
1419 Type = UseElementType ? LType->getElementType() : LType;
1437 TokError(
"expected ')' in unary operator");
1447 const RecTy *
Type = ParseOperatorType();
1452 TokError(
"expected '(' after type of !isa");
1456 const Init *
LHS = ParseValue(CurRec);
1472 const RecTy *
Type = ParseOperatorType();
1477 TokError(
"expected '(' after type of !exists");
1481 SMLoc ExprLoc = Lex.getLoc();
1482 const Init *Expr = ParseValue(CurRec);
1488 Error(ExprLoc,
"expected string type argument in !exists operator");
1495 "expected string type argument in !exists operator, please "
1496 "use !isa instead");
1502 Error(ExprLoc,
"expected string type argument in !exists operator");
1507 TokError(
"expected ')' in !exists");
1518 const RecTy *
Type = ParseOperatorType();
1523 TokError(
"expected '(' after type of !instances");
1530 SMLoc RegexLoc = Lex.getLoc();
1531 Regex = ParseValue(CurRec);
1535 Error(RegexLoc,
"expected string type argument in !instances operator");
1541 Error(RegexLoc,
"expected string type argument in !instances operator");
1550 TokError(
"expected ')' in !instances");
1585 SMLoc OpLoc = Lex.getLoc();
1675 const RecTy *
Type =
nullptr;
1676 const RecTy *ArgType =
nullptr;
1690 Type = ParseOperatorType();
1692 TokError(
"did not get type for !getdagarg operator");
1747 if (
Type && ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
1749 "', got '" +
Type->getAsString() +
"'");
1754 TokError(
"expected '(' after binary operator");
1763 SMLoc InitLoc = Lex.getLoc();
1764 InitList.
push_back(ParseValue(CurRec, ArgType));
1765 if (!InitList.
back())
1769 if (!InitListBack) {
1770 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1771 InitList.
back()->getAsString() +
"'"));
1774 const RecTy *ListType = InitListBack->getType();
1783 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1789 if (ItemType && InitList.
size() == 1) {
1792 Twine(
"expected output type to be a list, got type '") +
1797 Error(OpLoc, Twine(
"expected first arg type to be '") +
1799 "', got value of type '" +
1808 Error(InitLoc, Twine(
"expected second parameter to be an int, got "
1809 "value of type '") +
1817 Error(InitLoc, Twine(
"expected a list, got value of type '") +
1827 Error(InitLoc, Twine(
"expected bit, bits, int, string, or record; "
1828 "got value of type '") +
1841 Error(InitLoc, Twine(
"expected bit, bits, int, or string; "
1842 "got value of type '") +
1848 switch (InitList.
size()) {
1854 Twine(
"expected list of string, int, bits, or bit; "
1855 "got value of type '") +
1862 Error(InitLoc, Twine(
"expected second argument to be a string, "
1863 "got value of type '") +
1880 Error(InitLoc, Twine(
"expected value of type '") +
1923 TokError(
"expected ')' in operator");
1943 while (InitList.
size() > 2) {
1950 if (InitList.
size() == 2)
1954 Error(OpLoc,
"expected two operands to operator");
1960 return ParseOperationForEachFilter(CurRec, ItemType);
1964 SMLoc OpLoc = Lex.getLoc();
1968 TokError(
"expected '(' after !range operator");
1973 bool FirstArgIsList =
false;
1975 if (
Args.size() >= 3) {
1976 TokError(
"expected at most three values of integer");
1980 SMLoc InitLoc = Lex.getLoc();
1981 Args.push_back(ParseValue(CurRec));
1987 Error(OpLoc, Twine(
"expected value to be a typed value, got '" +
1988 Args.back()->getAsString() +
"'"));
1992 const RecTy *ArgBackType = ArgBack->getType();
1993 if (!FirstArgIsList ||
Args.size() == 1) {
1995 FirstArgIsList =
true;
1999 if (
Args.size() != 1)
2000 Error(InitLoc, Twine(
"expected value of type 'int', got '" +
2003 Error(InitLoc, Twine(
"expected list or int, got value of type '") +
2010 Error(InitLoc, Twine(
"expected one list, got extra value of type '") +
2019 TokError(
"expected ')' in operator");
2023 const Init *
LHS, *MHS, *
RHS;
2024 auto ArgCount =
Args.size();
2027 const auto *Arg0Ty = Arg0->getType();
2028 if (ArgCount == 1) {
2048 if (ArgCount == 3) {
2069 const RecTy *
Type =
nullptr;
2099 TokError(
"expected '(' after ternary operator");
2103 const Init *
LHS = ParseValue(CurRec);
2108 TokError(
"expected ',' in ternary operator");
2112 SMLoc MHSLoc = Lex.getLoc();
2113 const Init *MHS = ParseValue(CurRec, ItemType);
2118 TokError(
"expected ',' in ternary operator");
2122 SMLoc RHSLoc = Lex.getLoc();
2123 const Init *
RHS = ParseValue(CurRec, ItemType);
2128 TokError(
"expected ')' in binary operator");
2138 Error(MHSLoc,
"could not determine type of the child list in !dag");
2142 Error(MHSLoc, Twine(
"expected list of children, got type '") +
2143 MHSt->getType()->getAsString() +
"'");
2149 Error(RHSLoc,
"could not determine type of the name list in !dag");
2153 Error(RHSLoc, Twine(
"expected list<string>, got type '") +
2154 RHSt->getType()->getAsString() +
"'");
2158 if (!MHSt && !RHSt) {
2160 "cannot have both unset children and unset names in !dag");
2166 const RecTy *MHSTy =
nullptr;
2167 const RecTy *RHSTy =
nullptr;
2170 MHSTy = MHSt->getType();
2177 RHSTy = RHSt->getType();
2189 if (!MHSTy || !RHSTy) {
2190 TokError(
"could not get type for !if");
2205 TokError(
"could not get type for !subst");
2208 Type = RHSt->getType();
2214 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2215 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2225 Error(MHSLoc, Twine(
"expected integer index or string name, got ") +
2226 (MHSt ? (
"type '" + MHSt->getType()->getAsString())
2234 Error(RHSLoc, Twine(
"expected string or unset name, got type '") +
2235 RHSt->getType()->getAsString() +
"'");
2245 return ParseOperationSubstr(CurRec, ItemType);
2248 return ParseOperationFind(CurRec, ItemType);
2251 return ParseOperationCond(CurRec, ItemType);
2257 TokError(
"expected '(' after !foldl");
2261 const Init *StartUntyped = ParseValue(CurRec);
2267 TokError(Twine(
"could not get type of !foldl start: '") +
2273 TokError(
"expected ',' in !foldl");
2277 const Init *ListUntyped = ParseValue(CurRec);
2283 TokError(Twine(
"could not get type of !foldl list: '") +
2290 TokError(Twine(
"!foldl list must be a list, but is of type '") +
2291 List->getType()->getAsString());
2296 TokError(
"expected ',' in !foldl");
2301 TokError(
"third argument of !foldl must be an identifier");
2307 TokError((Twine(
"left !foldl variable '") +
A->getAsString() +
2308 "' already defined")
2314 TokError(
"expected ',' in !foldl");
2319 TokError(
"fourth argument of !foldl must be an identifier");
2325 TokError((Twine(
"right !foldl variable '") +
B->getAsString() +
2326 "' already defined")
2332 TokError(
"expected ',' in !foldl");
2339 std::unique_ptr<Record> ParseRecTmp;
2340 Record *ParseRec = CurRec;
2344 ParseRec = ParseRecTmp.get();
2347 TGVarScope *FoldScope =
PushScope(ParseRec);
2351 const Init *ExprUntyped = ParseValue(ParseRec);
2352 ParseRec->removeValue(
A);
2353 ParseRec->removeValue(
B);
2360 TokError(
"could not get type of !foldl expression");
2364 if (Expr->getType() !=
Start->getType()) {
2365 TokError(Twine(
"!foldl expression must be of same type as start (") +
2366 Start->getType()->getAsString() +
"), but is of type " +
2372 TokError(
"expected ')' in fold operator");
2387const RecTy *TGParser::ParseOperatorType() {
2388 const RecTy *
Type =
nullptr;
2391 TokError(
"expected type name for operator");
2396 TokError(
"the 'code' type is not allowed in bang operators; use 'string'");
2401 TokError(
"expected type name for operator");
2406 TokError(
"expected type name for operator");
2416const Init *TGParser::ParseOperationSubstr(
Record *CurRec,
2417 const RecTy *ItemType) {
2424 TokError(
"expected '(' after !substr operator");
2428 const Init *
LHS = ParseValue(CurRec);
2433 TokError(
"expected ',' in !substr operator");
2437 SMLoc MHSLoc = Lex.getLoc();
2438 const Init *MHS = ParseValue(CurRec);
2442 SMLoc RHSLoc = Lex.getLoc();
2445 RHSLoc = Lex.getLoc();
2446 RHS = ParseValue(CurRec);
2454 TokError(
"expected ')' in !substr operator");
2458 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2459 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2460 "', got '" +
Type->getAsString() +
"'");
2465 TokError(
"could not determine type of the string in !substr");
2469 TokError(Twine(
"expected string, got type '") +
2470 LHSt->getType()->getAsString() +
"'");
2476 TokError(
"could not determine type of the start position in !substr");
2480 Error(MHSLoc, Twine(
"expected int, got type '") +
2481 MHSt->getType()->getAsString() +
"'");
2488 TokError(
"could not determine type of the length in !substr");
2492 TokError(Twine(
"expected int, got type '") +
2493 RHSt->getType()->getAsString() +
"'");
2504const Init *TGParser::ParseOperationFind(
Record *CurRec,
2505 const RecTy *ItemType) {
2512 TokError(
"expected '(' after !find operator");
2516 const Init *
LHS = ParseValue(CurRec);
2521 TokError(
"expected ',' in !find operator");
2525 SMLoc MHSLoc = Lex.getLoc();
2526 const Init *MHS = ParseValue(CurRec);
2530 SMLoc RHSLoc = Lex.getLoc();
2533 RHSLoc = Lex.getLoc();
2534 RHS = ParseValue(CurRec);
2542 TokError(
"expected ')' in !find operator");
2546 if (ItemType && !
Type->typeIsConvertibleTo(ItemType)) {
2547 Error(RHSLoc, Twine(
"expected value of type '") + ItemType->
getAsString() +
2548 "', got '" +
Type->getAsString() +
"'");
2553 TokError(
"could not determine type of the source string in !find");
2557 TokError(Twine(
"expected string, got type '") +
2558 LHSt->getType()->getAsString() +
"'");
2564 TokError(
"could not determine type of the target string in !find");
2568 Error(MHSLoc, Twine(
"expected string, got type '") +
2569 MHSt->getType()->getAsString() +
"'");
2576 TokError(
"could not determine type of the start position in !find");
2580 TokError(Twine(
"expected int, got type '") +
2581 RHSt->getType()->getAsString() +
"'");
2593const Init *TGParser::ParseOperationForEachFilter(
Record *CurRec,
2594 const RecTy *ItemType) {
2595 SMLoc OpLoc = Lex.getLoc();
2599 TokError(
"expected '(' after !foreach/!filter");
2604 TokError(
"first argument of !foreach/!filter must be an identifier");
2612 TokError((Twine(
"iteration variable '") +
LHS->getAsString() +
2613 "' is already defined")
2619 TokError(
"expected ',' in !foreach/!filter");
2623 const Init *MHS = ParseValue(CurRec);
2628 TokError(
"expected ',' in !foreach/!filter");
2634 TokError(
"could not get type of !foreach/!filter list or dag");
2638 const RecTy *InEltType =
nullptr;
2639 const RecTy *ExprEltType =
nullptr;
2643 InEltType = InListTy->getElementType();
2647 ? OutListTy->getElementType()
2650 Error(OpLoc,
"expected value of type '" +
2652 "', but got list type");
2658 TokError(
"!filter must have a list argument");
2661 InEltType = InDagTy;
2664 "', but got dag type");
2670 TokError(
"!foreach must have a list or dag argument");
2672 TokError(
"!filter must have a list argument");
2678 std::unique_ptr<Record> ParseRecTmp;
2679 Record *ParseRec = CurRec;
2683 ParseRec = ParseRecTmp.get();
2685 TGVarScope *TempScope =
PushScope(ParseRec);
2687 const Init *
RHS = ParseValue(ParseRec, ExprEltType);
2688 ParseRec->removeValue(
LHS);
2694 TokError(
"expected ')' in !foreach/!filter");
2698 const RecTy *OutType = InEltType;
2702 TokError(
"could not get type of !foreach result expression");
2716const Init *TGParser::ParseOperationCond(
Record *CurRec,
2717 const RecTy *ItemType) {
2721 TokError(
"expected '(' after !cond operator");
2732 const Init *
V = ParseValue(CurRec);
2738 TokError(
"expected ':' following a condition in !cond operator");
2742 V = ParseValue(CurRec, ItemType);
2751 TokError(
"expected ',' or ')' following a value in !cond operator");
2756 if (Case.
size() < 1) {
2758 "there should be at least 1 'condition : value' in the !cond operator");
2763 const RecTy *
Type =
nullptr;
2764 for (
const Init *V : Val) {
2765 const RecTy *VTy =
nullptr;
2767 VTy = Vt->getType();
2773 if (
Type ==
nullptr) {
2780 TokError(Twine(
"inconsistent types '") +
Type->getAsString() +
2790 TokError(
"could not determine type for !cond from its arguments");
2823const Init *TGParser::ParseSimpleValue(
Record *CurRec,
const RecTy *ItemType,
2825 const Init *
R =
nullptr;
2830 return ParseOperation(CurRec, ItemType);
2834 TokError(
"Unknown or reserved token when parsing a value");
2850 auto BinaryVal = Lex.getCurBinaryIntVal();
2852 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
2859 std::string Val = Lex.getCurStrVal();
2864 Val += Lex.getCurStrVal();
2880 SMRange NameLoc = Lex.getLocRange();
2886 return ParseIDValue(CurRec, Name, NameLoc,
Mode);
2894 "Expected a class name, got '" +
Name->getValue() +
"'");
2901 if (ParseTemplateArgValueList(Args, ArgLocs, CurRec, Class))
2904 if (CheckTemplateArgValues(Args, ArgLocs, Class))
2907 if (resolveArguments(Class, Args, NameLoc.
Start))
2910 if (TrackReferenceLocs)
2911 Class->appendReferenceLoc(NameLoc);
2915 SMLoc BraceLoc = Lex.getLoc();
2920 ParseValueList(Vals, CurRec);
2925 TokError(
"expected '}' at end of bit list value");
2934 for (
unsigned i = 0, e = Vals.
size(); i != e; ++i) {
2940 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
2947 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
2956 Error(BraceLoc,
"Element #" + Twine(i) +
" (" + Vals[i]->getAsString() +
2957 ") is not convertable to a bit");
2962 std::reverse(NewBits.
begin(), NewBits.
end());
2969 const RecTy *DeducedEltTy =
nullptr;
2970 const ListRecTy *GivenListTy =
nullptr;
2975 TokError(Twine(
"Encountered a list when expecting a ") +
2979 GivenListTy = ListType;
2983 ParseValueList(Vals, CurRec,
2989 TokError(
"expected ']' at end of list value");
2993 const RecTy *GivenEltTy =
nullptr;
2996 GivenEltTy = ParseType();
3003 TokError(
"expected '>' at end of list element type");
3009 const RecTy *EltTy =
nullptr;
3010 for (
const Init *V : Vals) {
3016 TokError(
"Incompatible types in list elements");
3020 EltTy = TArg->getType();
3029 TokError(
"Incompatible types in list elements");
3046 TokError(Twine(
"Element type mismatch for list: element type '") +
3052 DeducedEltTy = EltTy;
3063 TokError(
"expected identifier or list of value types in dag init");
3067 const Init *
Operator = ParseValue(CurRec);
3072 const StringInit *OperatorName =
nullptr;
3075 TokError(
"expected variable name in dag operator");
3084 ParseDagArgList(DagArgs, CurRec);
3085 if (DagArgs.
empty())
3090 TokError(
"expected ')' in dag init");
3108const Init *TGParser::ParseValue(
Record *CurRec,
const RecTy *ItemType,
3110 SMLoc LHSLoc = Lex.getLoc();
3111 const Init *
Result = ParseSimpleValue(CurRec, ItemType,
Mode);
3117 switch (Lex.getCode()) {
3121 if (
Mode == ParseNameMode)
3125 SMLoc CurlyLoc = Lex.getLoc();
3127 SmallVector<unsigned, 16>
Ranges;
3128 ParseRangeList(Ranges);
3136 Error(CurlyLoc,
"Invalid bit range for value");
3142 TokError(
"expected '}' at end of bit range list");
3150 Error(LHSLoc,
"Invalid value, list expected");
3157 "' is invalid, list expected");
3162 const TypedInit *
RHS = ParseSliceElements(CurRec,
true);
3171 LHSTy->getElementType())
3179 TokError(
"expected ']' at end of list slice");
3186 TokError(
"expected field identifier after '.'");
3189 SMRange FieldNameLoc = Lex.getLocRange();
3190 const StringInit *FieldName =
3192 if (!
Result->getFieldType(FieldName)) {
3193 TokError(
"Cannot access field '" + Lex.getCurStrVal() +
"' of value '" +
3194 Result->getAsString() +
"'");
3199 if (TrackReferenceLocs) {
3201 const RecordVal *
V = DI->getDef()->getValue(FieldName);
3202 const_cast<RecordVal *
>(
V)->addReferenceLoc(FieldNameLoc);
3205 for (
const Record *R : RecTy->getClasses())
3206 if (
const auto *RV =
R->getValue(FieldName))
3207 const_cast<RecordVal *
>(RV)->addReferenceLoc(FieldNameLoc);
3218 SMLoc PasteLoc = Lex.getLoc();
3221 Error(PasteLoc,
"LHS of paste is not typed!");
3229 assert(
Mode == ParseValueMode &&
"encountered paste of lists in name");
3231 switch (Lex.getCode()) {
3238 const Init *RHSResult = ParseValue(CurRec, ItemType, ParseValueMode);
3255 Twine(
"can't cast '") +
LHS->getAsString() +
"' to string");
3261 const TypedInit *
RHS =
nullptr;
3264 switch (Lex.getCode()) {
3277 const Init *RHSResult = ParseValue(CurRec,
nullptr, ParseNameMode);
3282 Error(PasteLoc,
"RHS of paste is not typed!");
3292 Twine(
"can't cast '") +
RHS->getAsString() +
"' to string");
3313void TGParser::ParseDagArgList(
3326 const Init *Val = ParseValue(CurRec);
3333 const StringInit *
VarName =
nullptr;
3336 TokError(
"expected variable name in dag literal");
3344 Result.emplace_back(Val, VarName);
3359 Result.push_back(ParseValue(CurRec, ItemType));
3369 Result.push_back(ParseValue(CurRec, ItemType));
3385bool TGParser::ParseTemplateArgValueList(
3388 assert(
Result.empty() &&
"Result vector is not empty");
3394 bool HasNamedArg =
false;
3395 unsigned ArgIndex = 0;
3397 if (ArgIndex >= TArgs.
size()) {
3398 TokError(
"Too many template arguments: " +
utostr(ArgIndex + 1));
3405 const Init *
Value = ParseValue(
3407 HasNamedArg ?
nullptr : ArgsRec->
getValue(TArgs[ArgIndex])->
getType());
3414 return Error(ValueLoc,
3415 "The name of named argument should be a valid identifier");
3419 auto *NamedArg = ArgsRec->
getValue(QualifiedName);
3421 return Error(ValueLoc,
3422 "Argument " +
Name->getAsString() +
" doesn't exist");
3425 ValueLoc = Lex.getLoc();
3426 Value = ParseValue(CurRec, NamedArg->getType());
3429 return Error(ValueLoc,
3430 "The value of named argument should be initialized, "
3432 Value->getAsString() +
"'");
3439 return Error(ValueLoc,
3440 "Positional argument should be put before named argument");
3448 return TokError(
"Expected comma before next argument");
3463const Init *TGParser::ParseDeclaration(
Record *CurRec,
3464 bool ParsingTemplateArgs) {
3468 const RecTy *
Type = ParseType();
3473 TokError(
"Expected identifier in declaration");
3477 std::string Str = Lex.getCurStrVal();
3478 if (Str ==
"NAME") {
3479 TokError(
"'" + Str +
"' is a reserved variable name");
3483 if (!ParsingTemplateArgs && CurScope->varAlreadyDefined(Str)) {
3484 TokError(
"local variable of this name already exists");
3488 SMLoc IdLoc = Lex.getLoc();
3493 if (!ParsingTemplateArgs) {
3494 BadField = AddValue(CurRec, IdLoc,
3495 RecordVal(DeclName, IdLoc,
Type,
3498 }
else if (CurRec) {
3501 AddValue(CurRec, IdLoc,
3504 assert(CurMultiClass &&
"invalid context for template argument");
3507 AddValue(CurRec, IdLoc,
3515 SMLoc ValLoc = Lex.getLoc();
3516 const Init *Val = ParseValue(CurRec,
Type);
3518 SetValue(CurRec, ValLoc, DeclName, {}, Val,
3539TGParser::ParseForeachDeclaration(
const Init *&ForeachListValue) {
3541 TokError(
"Expected identifier in foreach declaration");
3550 TokError(
"Expected '=' in foreach declaration");
3554 const RecTy *IterType =
nullptr;
3555 SmallVector<unsigned, 16>
Ranges;
3557 switch (Lex.getCode()) {
3560 ParseRangeList(Ranges);
3562 TokError(
"expected '}' at end of bit range list");
3569 SMLoc ValueLoc = Lex.getLoc();
3570 const Init *
I = ParseValue(
nullptr);
3576 ForeachListValue =
I;
3582 if (ParseRangePiece(Ranges, TI))
3587 Error(ValueLoc,
"expected a list, got '" +
I->getAsString() +
"'");
3588 if (CurMultiClass) {
3589 PrintNote({},
"references to multiclass template arguments cannot be "
3590 "resolved at this time");
3597 assert(!IterType &&
"Type already initialized?");
3599 std::vector<Init *> Values;
3600 for (
unsigned R : Ranges)
3618bool TGParser::ParseTemplateArgList(
Record *CurRec) {
3622 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec;
3625 const Init *TemplArg = ParseDeclaration(CurRec,
true );
3633 SMLoc Loc = Lex.getLoc();
3634 TemplArg = ParseDeclaration(CurRec,
true );
3639 return Error(Loc,
"template argument with the same name has already been "
3646 return TokError(
"expected '>' at end of template argument list");
3663 SMLoc Loc = Lex.getLoc();
3665 std::string CurStr = Lex.getCurStrVal();
3667 LetMode Mode = llvm::StringSwitch<LetMode>(CurStr)
3677 LetModeAndName
Result = {
Mode, Lex.getLoc(), Lex.getCurStrVal()};
3695bool TGParser::ParseBodyItem(
Record *CurRec) {
3697 return ParseAssert(
nullptr, CurRec);
3700 return ParseDefvar(CurRec);
3703 return ParseDump(
nullptr, CurRec);
3706 if (!ParseDeclaration(CurRec,
false))
3710 return TokError(
"expected ';' after declaration");
3718 return TokError(
"expected field identifier after let");
3720 auto [
Mode, IdLoc, FieldNameStr] = ParseLetModeAndName();
3723 SmallVector<unsigned, 16> BitList;
3724 if (ParseOptionalBitList(BitList))
3726 std::reverse(BitList.
begin(), BitList.
end());
3729 return TokError(
"expected '=' in let expression");
3733 return Error(IdLoc,
"Value '" + FieldName->
getValue() +
"' unknown!");
3742 const Init *Val = ParseValue(CurRec,
Type);
3747 return TokError(
"expected ';' after let expression");
3749 return SetValue(CurRec, IdLoc, FieldName, BitList, Val,
3760bool TGParser::ParseBody(
Record *CurRec) {
3766 return TokError(
"Expected '{' to start body or ';' for declaration only");
3769 if (ParseBodyItem(CurRec))
3776 SMLoc SemiLoc = Lex.getLoc();
3778 PrintError(SemiLoc,
"A class or def body should not end with a semicolon");
3779 PrintNote(
"Semicolon ignored; remove to eliminate this error");
3787bool TGParser::ApplyLetStack(
Record *CurRec) {
3788 for (SmallVectorImpl<LetRecord> &LetInfo : LetStack)
3789 for (LetRecord &LR : LetInfo)
3790 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value,
3800 return ApplyLetStack(
Entry.Rec.get());
3803 if (
Entry.Assertion)
3810 for (
auto &
E :
Entry.Loop->Entries) {
3811 if (ApplyLetStack(
E))
3827bool TGParser::ParseObjectBody(
Record *CurRec) {
3829 TGVarScope *ObjectScope =
PushScope(CurRec);
3834 SubClassReference SubClass = ParseSubClassReference(CurRec,
false);
3841 if (AddSubClass(CurRec, SubClass))
3846 SubClass = ParseSubClassReference(CurRec,
false);
3850 if (ApplyLetStack(CurRec))
3853 bool Result = ParseBody(CurRec);
3863bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
3864 SMLoc DefLoc = Lex.getLoc();
3871 SMLoc NameLoc = Lex.getCode() ==
tgtok::Id ? Lex.getLoc() : DefLoc;
3874 std::unique_ptr<Record> CurRec;
3875 const Init *
Name = ParseObjectName(CurMultiClass);
3880 CurRec = std::make_unique<Record>(Records.getNewAnonymousName(), DefLoc,
3883 CurRec = std::make_unique<Record>(Name, NameLoc, Records);
3886 if (ParseObjectBody(CurRec.get()))
3889 return addEntry(std::move(CurRec));
3896bool TGParser::ParseDefset() {
3901 Defset.Loc = Lex.getLoc();
3902 const RecTy *
Type = ParseType();
3910 return TokError(
"expected identifier");
3911 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
3912 if (Records.getGlobal(DeclName->
getValue()))
3913 return TokError(
"def or global variable of this name already exists");
3919 SMLoc BraceLoc = Lex.getLoc();
3922 Defsets.push_back(&Defset);
3923 bool Err = ParseObjectList(
nullptr);
3929 TokError(
"expected '}' at end of defset");
3930 return Error(BraceLoc,
"to match this '{'");
3933 Records.addExtraGlobal(DeclName->
getValue(),
3942bool TGParser::ParseDeftype() {
3947 return TokError(
"expected identifier");
3949 const std::string
TypeName = Lex.getCurStrVal();
3950 if (TypeAliases.count(TypeName) || Records.getClass(TypeName))
3951 return TokError(
"type of this name '" + TypeName +
"' already exists");
3957 SMLoc Loc = Lex.getLoc();
3958 const RecTy *
Type = ParseType();
3963 return Error(Loc,
"cannot define type alias for class type '" +
3964 Type->getAsString() +
"'");
3978bool TGParser::ParseDefvar(
Record *CurRec) {
3983 return TokError(
"expected identifier");
3984 const StringInit *DeclName =
StringInit::get(Records, Lex.getCurStrVal());
3985 if (CurScope->varAlreadyDefined(DeclName->
getValue()))
3986 return TokError(
"local variable of this name already exists");
3991 if (V && !
V->isTemplateArg())
3992 return TokError(
"field of this name already exists");
3997 if (CurScope->isOutermost() && Records.getGlobal(DeclName->
getValue()))
3998 return TokError(
"def or global variable of this name already exists");
4004 const Init *
Value = ParseValue(CurRec);
4011 if (!CurScope->isOutermost())
4025bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
4026 SMLoc Loc = Lex.getLoc();
4032 const Init *ListValue =
nullptr;
4033 const VarInit *IterName = ParseForeachDeclaration(ListValue);
4035 return TokError(
"expected declaration in for");
4041 auto TheLoop = std::make_unique<ForeachLoop>(Loc, IterName, ListValue);
4043 TGVarScope *ForeachScope =
PushScope(TheLoop.get());
4044 Loops.push_back(std::move(TheLoop));
4048 if (ParseObject(CurMultiClass))
4051 SMLoc BraceLoc = Lex.getLoc();
4056 if (ParseObjectList(CurMultiClass))
4060 TokError(
"expected '}' at end of foreach command");
4061 return Error(BraceLoc,
"to match this '{'");
4068 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4071 return addEntry(std::move(Loop));
4079bool TGParser::ParseIf(
MultiClass *CurMultiClass) {
4080 SMLoc Loc = Lex.getLoc();
4086 const Init *Condition = ParseValue(
nullptr);
4100 const ListInit *SingletonList =
4106 const Init *ThenClauseList =
4110 Loops.push_back(std::make_unique<ForeachLoop>(Loc,
nullptr, ThenClauseList));
4112 if (ParseIfBody(CurMultiClass,
"then"))
4115 std::unique_ptr<ForeachLoop> Loop = std::move(Loops.back());
4118 if (addEntry(std::move(Loop)))
4128 const Init *ElseClauseList =
4133 std::make_unique<ForeachLoop>(Loc,
nullptr, ElseClauseList));
4135 if (ParseIfBody(CurMultiClass,
"else"))
4138 Loop = std::move(Loops.back());
4141 if (addEntry(std::move(Loop)))
4159 if (ParseObject(CurMultiClass))
4162 SMLoc BraceLoc = Lex.getLoc();
4167 if (ParseObjectList(CurMultiClass))
4171 TokError(
"expected '}' at end of '" + Kind +
"' clause");
4172 return Error(BraceLoc,
"to match this '{'");
4187 SMLoc ConditionLoc = Lex.getLoc();
4188 const Init *Condition = ParseValue(CurRec);
4193 TokError(
"expected ',' in assert statement");
4197 const Init *Message = ParseValue(CurRec);
4205 CurRec->
addAssertion(ConditionLoc, Condition, Message);
4207 addEntry(std::make_unique<Record::AssertionInfo>(ConditionLoc, Condition,
4216bool TGParser::ParseClass() {
4221 return TokError(
"expected class name after 'class' keyword");
4223 const std::string &
Name = Lex.getCurStrVal();
4224 Record *CurRec =
const_cast<Record *
>(Records.getClass(Name));
4231 "' already defined");
4236 auto NewRec = std::make_unique<Record>(Lex.getCurStrVal(), Lex.getLoc(),
4238 CurRec = NewRec.get();
4239 Records.addClass(std::move(NewRec));
4242 if (TypeAliases.count(Name))
4243 return TokError(
"there is already a defined type alias '" + Name +
"'");
4248 TGVarScope *ClassScope =
PushScope(CurRec);
4251 if (ParseTemplateArgList(CurRec))
4254 if (ParseObjectBody(CurRec))
4257 if (!NoWarnOnUnusedTemplateArgs)
4273 TokError(
"expected identifier in let definition");
4278 auto [
Mode, NameLoc, NameStr] = ParseLetModeAndName();
4282 SmallVector<unsigned, 16>
Bits;
4283 if (ParseOptionalRangeList(Bits)) {
4287 std::reverse(
Bits.begin(),
Bits.end());
4290 TokError(
"expected '=' in let expression");
4295 const Init *Val = ParseValue(
nullptr);
4302 Result.emplace_back(Name, Bits, Val, NameLoc,
Mode);
4312bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
4318 ParseLetList(LetInfo);
4319 if (LetInfo.
empty())
4321 LetStack.push_back(std::move(LetInfo));
4324 return TokError(
"expected 'in' at end of top-level 'let'");
4329 if (ParseObject(CurMultiClass))
4332 SMLoc BraceLoc = Lex.getLoc();
4340 if (ParseObjectList(CurMultiClass))
4344 TokError(
"expected '}' at end of top level let command");
4345 return Error(BraceLoc,
"to match this '{'");
4352 LetStack.pop_back();
4369bool TGParser::ParseMultiClass() {
4374 return TokError(
"expected identifier after multiclass for name");
4375 std::string
Name = Lex.getCurStrVal();
4377 auto Result = MultiClasses.try_emplace(
4378 Name, std::make_unique<MultiClass>(Name, Lex.getLoc(), Records));
4381 return TokError(
"multiclass '" + Name +
"' already defined");
4383 CurMultiClass =
Result.first->second.get();
4387 TGVarScope *MulticlassScope =
PushScope(CurMultiClass);
4391 if (ParseTemplateArgList(
nullptr))
4394 bool inherits =
false;
4401 SubMultiClassReference SubMultiClass =
4402 ParseSubMultiClassReference(CurMultiClass);
4405 if (!SubMultiClass.
MC)
4409 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
4414 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
4420 return TokError(
"expected '{' in multiclass definition");
4422 return TokError(
"expected ';' in multiclass definition");
4425 return TokError(
"multiclass must contain at least one def");
4428 switch (Lex.getCode()) {
4430 return TokError(
"expected 'assert', 'def', 'defm', 'defvar', 'dump', "
4431 "'foreach', 'if', or 'let' in multiclass body");
4441 if (ParseObject(CurMultiClass))
4449 SMLoc SemiLoc = Lex.getLoc();
4451 PrintError(SemiLoc,
"A multiclass body should not end with a semicolon");
4452 PrintNote(
"Semicolon ignored; remove to eliminate this error");
4456 if (!NoWarnOnUnusedTemplateArgs)
4457 CurMultiClass->Rec.checkUnusedTemplateArgs();
4460 CurMultiClass =
nullptr;
4468bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
4472 const Init *DefmName = ParseObjectName(CurMultiClass);
4476 DefmName = Records.getNewAnonymousName();
4485 return TokError(
"expected ':' after defm identifier");
4488 std::vector<RecordsEntry> NewEntries;
4491 bool InheritFromClass =
false;
4496 SMLoc SubClassLoc = Lex.getLoc();
4497 SubClassReference
Ref = ParseSubClassReference(
nullptr,
true);
4507 MultiClass *MC = MultiClasses[
Ref.Rec->getName().str()].get();
4508 assert(MC &&
"Didn't lookup multiclass correctly?");
4511 if (resolveArgumentsOfMultiClass(Substs, MC,
Ref.TemplateArgs, DefmName,
4515 if (resolve(MC->
Entries, Substs, !CurMultiClass && Loops.empty(),
4516 &NewEntries, &SubClassLoc))
4523 return TokError(
"expected identifier");
4525 SubClassLoc = Lex.getLoc();
4529 InheritFromClass = (Records.getClass(Lex.getCurStrVal()) !=
nullptr);
4531 if (InheritFromClass)
4534 Ref = ParseSubClassReference(
nullptr,
true);
4537 if (InheritFromClass) {
4540 SubClassReference SubClass = ParseSubClassReference(
nullptr,
false);
4548 for (
auto &
E : NewEntries) {
4550 if (AddSubClass(
E, SubClass))
4556 SubClass = ParseSubClassReference(
nullptr,
false);
4560 for (
auto &
E : NewEntries) {
4561 if (ApplyLetStack(
E))
4564 addEntry(std::move(
E));
4568 return TokError(
"expected ';' at end of defm");
4586 switch (Lex.getCode()) {
4589 "Expected assert, class, def, defm, defset, dump, foreach, if, or let");
4591 return ParseAssert(MC);
4593 return ParseDef(MC);
4595 return ParseDefm(MC);
4597 return ParseDeftype();
4599 return ParseDefvar();
4601 return ParseDump(MC);
4603 return ParseForeach(MC);
4607 return ParseTopLevelLet(MC);
4610 return TokError(
"defset is not allowed inside multiclass");
4611 return ParseDefset();
4614 return TokError(
"class is not allowed inside multiclass");
4616 return TokError(
"class is not allowed inside foreach loop");
4617 return ParseClass();
4620 return TokError(
"multiclass is not allowed inside foreach loop");
4621 return ParseMultiClass();
4627bool TGParser::ParseObjectList(
MultiClass *MC) {
4629 if (ParseObject(MC))
4638 if (ParseObjectList())
4646 return TokError(
"Unexpected token at top level");
4653bool TGParser::CheckTemplateArgValues(
4657 "expected as many values as locations");
4661 bool HasError =
false;
4663 const Init *ArgName =
nullptr;
4664 if (
Value->isPositional())
4665 ArgName = TArgs[
Value->getIndex()];
4666 if (
Value->isNamed())
4673 auto *CastValue = ArgValue->
getCastTo(ArgType);
4677 "result of template arg value cast has wrong type");
4681 Loc,
"Value specified for template argument '" +
4683 ArgValue->getType()->
getAsString() +
"; expected type " +
4692#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4701 errs() <<
"foreach " <<
IterVar->getAsString() <<
" = "
4702 <<
ListValue->getAsString() <<
" in {\n";
4711 errs() <<
"Record:\n";
4714 errs() <<
"Defs:\n";
4726 const Init *Message = ParseValue(CurRec);
4746 addEntry(std::make_unique<Record::DumpInfo>(
Loc, ResolvedMessage));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
uint64_t IntrinsicInst * II
OptimizedStructLayoutField Field
PowerPC Reduce CR logical Operation
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
This file defines the SmallVector class.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static bool checkBitsConcrete(Record &R, const RecordVal &RV)
static const Init * QualifyName(const Record &CurRec, const Init *Name)
Return an Init with a qualifier prefix referring to CurRec's name.
static const Init * QualifiedNameOfImplicitName(const Record &Rec)
Return the qualified version of the implicit 'NAME' template argument.
static void checkConcrete(Record &R)
static SymbolRef::Type getType(const Symbol *Sym)
static const ArgumentInit * get(const Init *Value, ArgAuxType Aux)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
bool empty() const
empty - Check if the array is empty.
static const BinOpInit * get(BinaryOp opc, const Init *lhs, const Init *rhs, const RecTy *Type)
static const Init * getStrConcat(const Init *lhs, const Init *rhs)
static const Init * getListConcat(const TypedInit *lhs, const Init *rhs)
const Init * Fold(const Record *CurRec) const
static BitInit * get(RecordKeeper &RK, bool V)
static const BitRecTy * get(RecordKeeper &RK)
static BitsInit * get(RecordKeeper &RK, ArrayRef< const Init * > Range)
static const BitsRecTy * get(RecordKeeper &RK, unsigned Sz)
const Init * Fold(const Record *CurRec) const
static const CondOpInit * get(ArrayRef< const Init * > Conds, ArrayRef< const Init * > Values, const RecTy *Type)
static const DagInit * get(const Init *V, const StringInit *VN, ArrayRef< const Init * > Args, ArrayRef< const StringInit * > ArgNames)
static const DagRecTy * get(RecordKeeper &RK)
static const ExistsOpInit * get(const RecTy *CheckType, const Init *Expr)
const Init * Fold(const Record *CurRec) const
static const FieldInit * get(const Init *R, const StringInit *FN)
const Init * Fold(const Record *CurRec) const
static const FoldOpInit * get(const Init *Start, const Init *List, const Init *A, const Init *B, const Init *Expr, const RecTy *Type)
Do not resolve anything, but keep track of whether a given variable was referenced.
virtual const Init * resolveReferences(Resolver &R) const
This function is used by classes that refer to other variables which may not be defined at the time t...
virtual std::string getAsUnquotedString() const
Convert this value to a literal form, without adding quotes around a string.
virtual std::string getAsString() const =0
Convert this value to a literal form.
virtual const Init * getBit(unsigned Bit) const =0
Get the Init value of the specified bit.
virtual const Init * getCastTo(const RecTy *Ty) const =0
If this value is convertible to type Ty, return a value whose type is Ty, generating a !...
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
static const InstancesOpInit * get(const RecTy *Type, const Init *Regex)
static IntInit * get(RecordKeeper &RK, int64_t V)
static const IntRecTy * get(RecordKeeper &RK)
static const IsAOpInit * get(const RecTy *CheckType, const Init *Expr)
const Init * Fold() const
static const ListInit * get(ArrayRef< const Init * > Range, const RecTy *EltTy)
const RecTy * getElementType() const
static const ListRecTy * get(const RecTy *T)
bool typeIsConvertibleTo(const RecTy *RHS) const override
Return true if all values of 'this' type can be converted to the specified type.
Represents a single loop in the control flow graph.
Resolve arbitrary mappings.
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
Return true if all values of 'this' type can be converted to the specified type.
virtual std::string getAsString() const =0
const ListRecTy * getListTy() const
Returns the type representing list<thistype>.
static const RecordRecTy * get(RecordKeeper &RK, ArrayRef< const Record * > Classes)
Get the record type with the given non-redundant list of superclasses.
This class represents a field in a record, including its name, type, value, and source location.
std::string getNameInitAsString() const
Get the name of the field as a std::string.
void setUsed(bool Used)
Whether this value is used.
bool setValue(const Init *V)
Set the value of the field from an Init.
const Init * getValue() const
Get the value of the field as an Init.
StringRef getName() const
Get the name of the field as a StringRef.
void addReferenceLoc(SMRange Loc)
Add a reference to this record value.
const Init * getNameInit() const
Get the name of the field as an Init.
const RecTy * getType() const
Get the type of the field value as a RecTy.
const RecordRecTy * getType() const
void addDump(SMLoc Loc, const Init *Message)
void checkUnusedTemplateArgs()
std::string getNameInitAsString() const
RecordKeeper & getRecords() const
const RecordVal * getValue(const Init *Name) const
void addTemplateArg(const Init *Name)
bool isMultiClass() const
void addValue(const RecordVal &RV)
void addAssertion(SMLoc Loc, const Init *Condition, const Init *Message)
ArrayRef< std::pair< const Record *, SMRange > > getDirectSuperClasses() const
Return the direct superclasses of this record.
StringRef getName() const
bool isTemplateArg(const Init *Name) const
void appendDumps(const Record *Rec)
bool isSubClassOf(const Record *R) const
ArrayRef< RecordVal > getValues() const
SMLoc getFieldLoc(StringRef FieldName) const
Return the source location for the named field.
void resolveReferences(const Init *NewName=nullptr)
If there are any field references that refer to fields that have been filled in, we can propagate the...
ArrayRef< const Init * > getTemplateArgs() const
void updateClassLoc(SMLoc Loc)
void addDirectSuperClass(const Record *R, SMRange Range)
void appendAssertions(const Record *Rec)
const Init * getNameInit() const
void setFinal(bool Final)
Represents a location in source code.
Represents a range in source code.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
"foo" - Represent an initialization by a string value.
static const StringInit * get(RecordKeeper &RK, StringRef, StringFormat Fmt=SF_String)
StringRef getValue() const
static const StringRecTy * get(RecordKeeper &RK)
StringRef - Represent a constant reference to a string, i.e.
tgtok::TokKind getCode() const
void PopScope(TGVarScope *ExpectedStackTop)
bool Error(SMLoc L, const Twine &Msg) const
bool TokError(const Twine &Msg) const
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
const Init * getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass, const StringInit *Name, SMRange NameLoc, bool TrackReferenceLocs) const
const Init * Fold(const Record *CurRec) const
static const TernOpInit * get(TernaryOp opc, const Init *lhs, const Init *mhs, const Init *rhs, const RecTy *Type)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
This is the common superclass of types that have a specific, explicit type, stored in ValueTy.
const RecTy * getType() const
Get the type of the Init as a RecTy.
static const UnOpInit * get(UnaryOp opc, const Init *lhs, const RecTy *Type)
const Init * Fold(const Record *CurRec, bool IsFinal=false) const
static UnsetInit * get(RecordKeeper &RK)
Get the singleton unset Init.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
static const VarDefInit * get(SMLoc Loc, const Record *Class, ArrayRef< const ArgumentInit * > Args)
const Init * Fold() const
'Opcode' - Represent a reference to an entire variable object.
static const VarInit * get(StringRef VN, const RecTy *T)
const Init * getNameInit() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ Resolved
Queried, materialization begun.
NodeAddr< DefNode * > Def
NodeAddr< CodeNode * > Code
static bool isBangOperator(tgtok::TokKind Kind)
isBangOperator - Return true if this is a bang operator.
static bool isObjectStart(tgtok::TokKind Kind)
isObjectStart - Return true if this is a valid first token for a statement.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
void PrintError(const Twine &Msg)
std::string utostr(uint64_t X, bool isNeg=false)
LetMode
Specifies how a 'let' assignment interacts with the existing field value.
bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
auto dyn_cast_or_null(const Y &Val)
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
void PrintNote(const Twine &Msg)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Ref
The access may reference the value stored in memory.
FunctionAddr VTableAddr Next
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
void dumpMessage(SMLoc Loc, const Init *Message)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
const RecTy * resolveTypes(const RecTy *T1, const RecTy *T2)
Find a common type that T1 and T2 convert to.
@ Default
The result values are uniform if and only if all operands are uniform.
Implement std::hash so that hash_code can be used in STL containers.
ForeachLoop - Record the iteration state associated with a for loop.
std::vector< RecordsEntry > Entries
Parsed let mode keyword and field name (e.g.
std::vector< RecordsEntry > Entries
RecordsEntry - Holds exactly one of a Record, ForeachLoop, or AssertionInfo.
std::unique_ptr< ForeachLoop > Loop
std::unique_ptr< Record::AssertionInfo > Assertion
std::unique_ptr< Record::DumpInfo > Dump
std::unique_ptr< Record > Rec
SubClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs
SubMultiClassReference()=default
SmallVector< const ArgumentInit *, 4 > TemplateArgs