49 errs() <<
"Multiclass:\n";
53 errs() <<
"Template args:\n";
62 CurRec = &CurMultiClass->
Rec;
67 return Error(Loc,
"New definition of '" + RV.
getName() +
"' of type '" +
69 "previous definition of type '" +
70 ERV->getType()->getAsString() +
"'");
80 const std::vector<unsigned> &BitList,
Init *V) {
83 if (!CurRec) CurRec = &CurMultiClass->
Rec;
94 if (
VI->getNameInit() == ValName)
101 if (!BitList.empty()) {
105 "' is not a bits type");
110 return Error(Loc,
"Initializer is not compatible with bit range");
113 BitsInit *BInit = cast<BitsInit>(BI);
118 for (
unsigned i = 0, e = BitList.size(); i != e; ++i) {
119 unsigned Bit = BitList[i];
121 return Error(Loc,
"Cannot set bit #" +
Twine(Bit) +
" of value '" +
126 for (
unsigned i = 0, e = CurVal->
getNumBits(); i != e; ++i)
128 NewBits[i] = CurVal->
getBit(i);
134 std::string InitType =
"";
135 if (
BitsInit *BI = dyn_cast<BitsInit>(V))
136 InitType = (
Twine(
"' of type bit initializer with length ") +
137 Twine(BI->getNumBits())).str();
140 "' is incompatible with initializer '" + V->
getAsString() +
160 "More template args specified than expected");
164 for (
unsigned i = 0, e = TArgs.size(); i != e; ++i) {
179 "Value not specified for template argument #" +
180 Twine(i) +
" (" + TArgs[i]->getAsUnquotedString() +
189 for (
unsigned i = 0, e = SCs.
size(); i != e; ++i) {
192 "Already subclass of '" + SCs[i]->getName() +
"'!\n");
198 "Already subclass of '" + SC->
getName() +
"'!\n");
206 bool TGParser::AddSubMultiClass(
MultiClass *CurMC,
219 for (
const std::unique_ptr<Record> &R : SMC->
DefPrototypes) {
221 auto NewDef = make_unique<Record>(*R);
224 for (
const auto &MCVal : CurRec->
getValues())
225 if (AddValue(NewDef.get(), SubMultiClass.
RefRange.
Start, MCVal))
235 if (SMCTArgs.size() < SubMultiClass.
TemplateArgs.size())
237 "More template args specified than expected");
241 for (
unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) {
245 if (SetValue(CurRec, SubMultiClass.
RefRange.
Start, SMCTArgs[i],
246 std::vector<unsigned>(),
258 for (
const auto &
Def :
261 std::vector<unsigned>(),
266 Def->resolveReferencesTo(
Def->getValue(SMCTArgs[i]));
269 Def->removeValue(SMCTArgs[i]);
273 "Value not specified for template argument #" +
274 Twine(i) +
" (" + SMCTArgs[i]->getAsUnquotedString() +
285 bool TGParser::ProcessForeachDefs(
Record *CurRec,
SMLoc Loc) {
293 return ProcessForeachDefs(CurRec, Loc, IterVals);
299 bool TGParser::ProcessForeachDefs(
Record *CurRec,
SMLoc Loc, IterSet &IterVals){
301 if (IterVals.size() != Loops.size()) {
302 assert(IterVals.size() < Loops.size());
306 Error(Loc,
"Loop list is not a list");
311 for (
unsigned i = 0; i < List->
size(); ++i) {
313 IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal));
314 if (ProcessForeachDefs(CurRec, Loc, IterVals))
324 auto IterRec = make_unique<Record>(*CurRec);
327 for (IterRecord &
IR : IterVals) {
331 return Error(Loc,
"foreach iterator value is untyped");
335 if (SetValue(IterRec.get(), Loc, IterVar->
getName(),
336 std::vector<unsigned>(), IVal))
337 return Error(Loc,
"when instantiating this def");
340 IterRec->resolveReferencesTo(IterRec->getValue(IterVar->
getName()));
343 IterRec->removeValue(IterVar->
getName());
346 if (Records.
getDef(IterRec->getNameInitAsString())) {
348 if (!IterRec->isAnonymous())
349 return Error(Loc,
"def already exists: " +IterRec->getNameInitAsString());
351 IterRec->setName(GetNewAnonymousName());
354 Record *IterRecSave = IterRec.get();
355 Records.
addDef(std::move(IterRec));
373 std::string TGParser::GetNewAnonymousName() {
374 return "anonymous_" +
utostr(AnonCounter++);
397 CurRec = &CurMultiClass->
Rec;
403 TokError(
"Record name is not typed!");
409 return ParseValue(CurRec, Type, ParseNameMode);
417 Record *TGParser::ParseClassID() {
419 TokError(
"expected name for ClassID");
438 TokError(
"expected name for MultiClassID");
457 ParseSubClassReference(
Record *CurRec,
bool isDefm) {
463 Result.
Rec = &MC->Rec;
465 Result.
Rec = ParseClassID();
467 if (!Result.
Rec)
return Result;
477 TokError(
"subclass reference requires a non-empty list of template values");
478 Result.
Rec =
nullptr;
484 Result.
Rec =
nullptr;
489 TokError(
"expected '>' in template value list");
490 Result.
Rec =
nullptr;
507 ParseSubMultiClassReference(
MultiClass *CurMC) {
511 Result.
MC = ParseMultiClassID();
512 if (!Result.
MC)
return Result;
522 TokError(
"subclass reference requires a non-empty list of template values");
534 TokError(
"expected '>' in template value list");
548 bool TGParser::ParseRangePiece(std::vector<unsigned> &
Ranges) {
550 TokError(
"expected integer or bitrange");
557 return TokError(
"invalid range, cannot be negative");
561 Ranges.push_back(Start);
565 TokError(
"expected integer value as end of range");
575 return TokError(
"invalid range, cannot be negative");
580 for (; Start <= End; ++Start)
581 Ranges.push_back(Start);
583 for (; Start >= End; --Start)
584 Ranges.push_back(Start);
592 std::vector<unsigned> TGParser::ParseRangeList() {
593 std::vector<unsigned> Result;
596 if (ParseRangePiece(Result))
597 return std::vector<unsigned>();
602 if (ParseRangePiece(Result))
603 return std::vector<unsigned>();
611 bool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) {
619 Ranges = ParseRangeList();
620 if (Ranges.empty())
return true;
623 TokError(
"expected '>' at end of range list");
624 return Error(StartLoc,
"to match this '<'");
633 bool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) {
641 Ranges = ParseRangeList();
642 if (Ranges.empty())
return true;
645 TokError(
"expected '}' at end of bit list");
646 return Error(StartLoc,
"to match this '{'");
664 RecTy *TGParser::ParseType() {
666 default:
TokError(
"Unknown token when expecting a type");
return nullptr;
677 TokError(
"expected '<' after bits type");
681 TokError(
"expected integer in bits<n> type");
686 TokError(
"expected '>' at end of bits<n> type");
694 TokError(
"expected '<' after list type");
698 RecTy *SubType = ParseType();
699 if (!SubType)
return nullptr;
702 TokError(
"expected '>' at end of list<ty> type");
720 Init *TemplateArgName =
QualifyName(*CurRec, CurMultiClass, Name,
":");
723 TemplateArgName =
QualifyName(CurMultiClass->
Rec, CurMultiClass, Name,
728 assert(RV &&
"Template arg doesn't exist??");
739 assert(RV &&
"Template arg doesn't exist??");
745 for (
const auto &L : Loops) {
751 if (Mode == ParseNameMode)
757 if (Mode == ParseValueMode) {
758 Error(NameLoc,
"Variable not defined: '" + Name +
"'");
779 RecTy *Type =
nullptr;
787 Type = ParseOperatorType();
790 TokError(
"did not get type for unary operator");
810 TokError(
"expected '(' after unary operator");
815 Init *LHS = ParseValue(CurRec);
816 if (!LHS)
return nullptr;
824 if (!LHSl && !LHSs && !LHSt) {
825 TokError(
"expected list or string type argument in unary operator");
831 if (!LType && !SType) {
832 TokError(
"expected list or string type argument in unary operator");
838 if (!LHSl && !LHSt) {
839 TokError(
"expected list type argument in unary operator");
843 if (LHSl && LHSl->
empty()) {
844 TokError(
"empty list argument in unary operator");
851 TokError(
"untyped list element in unary operator");
857 assert(LHSt &&
"expected list type argument in unary operator");
860 TokError(
"expected list type argument in unary operator");
869 TokError(
"expected ')' in unary operator");
873 return (
UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass);
890 RecTy *Type =
nullptr;
912 TokError(
"expected '(' after binary operator");
920 if (!InitList.
back())
return nullptr;
926 if (!InitList.
back())
return nullptr;
930 TokError(
"expected ')' in operator");
937 if (
VarInit *Arg0 = dyn_cast<VarInit>(InitList[0]))
938 Type = Arg0->getType();
939 else if (
ListInit *Arg0 = dyn_cast<ListInit>(InitList[0]))
940 Type = Arg0->getType();
943 Error(OpLoc,
"expected a list");
951 while (InitList.
size() > 2) {
954 ->Fold(CurRec, CurMultiClass);
955 InitList.
back() = RHS;
959 if (InitList.
size() == 2)
961 ->Fold(CurRec, CurMultiClass);
963 Error(OpLoc,
"expected two operands to operator");
971 RecTy *Type =
nullptr;
988 TokError(
"expected '(' after ternary operator");
993 Init *LHS = ParseValue(CurRec);
994 if (!LHS)
return nullptr;
997 TokError(
"expected ',' in ternary operator");
1002 Init *MHS = ParseValue(CurRec, ItemType);
1007 TokError(
"expected ',' in ternary operator");
1012 Init *RHS = ParseValue(CurRec, ItemType);
1017 TokError(
"expected ')' in binary operator");
1025 RecTy *MHSTy =
nullptr;
1026 RecTy *RHSTy =
nullptr;
1028 if (
TypedInit *MHSt = dyn_cast<TypedInit>(MHS))
1029 MHSTy = MHSt->getType();
1030 if (
BitsInit *MHSbits = dyn_cast<BitsInit>(MHS))
1032 if (isa<BitInit>(MHS))
1035 if (
TypedInit *RHSt = dyn_cast<TypedInit>(RHS))
1036 RHSTy = RHSt->getType();
1037 if (
BitsInit *RHSbits = dyn_cast<BitsInit>(RHS))
1039 if (isa<BitInit>(RHS))
1043 if (isa<UnsetInit>(MHS))
1045 if (isa<UnsetInit>(RHS))
1048 if (!MHSTy || !RHSTy) {
1049 TokError(
"could not get type for !if");
1058 TokError(
"inconsistent types for !if");
1066 TokError(
"could not get type for !foreach");
1075 TokError(
"could not get type for !subst");
1093 RecTy *TGParser::ParseOperatorType() {
1094 RecTy *Type =
nullptr;
1097 TokError(
"expected type name for operator");
1105 TokError(
"expected type name for operator");
1110 TokError(
"expected type name for operator");
1142 default:
TokError(
"Unknown token when parsing a value");
break;
1147 return ParseSimpleValue(CurRec, ItemType, Mode);
1152 for (
unsigned i = 0, e = BinaryVal.second; i != e; ++i)
1183 return ParseIDValue(CurRec, Name, NameLoc, Mode);
1187 TokError(
"expected non-empty value list");
1196 Error(NameLoc,
"Expected a class name, got '" + Name +
"'");
1200 std::vector<Init*> ValueList = ParseValueList(CurRec, Class);
1201 if (ValueList.empty())
return nullptr;
1204 TokError(
"expected '>' at end of value list");
1211 auto NewRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), NameLoc,
1213 Record *NewRec = NewRecOwner.get();
1219 if (AddSubClass(NewRec, SCRef))
1222 if (!CurMultiClass) {
1223 NewRec->resolveReferences();
1224 Records.addDef(std::move(NewRecOwner));
1228 NewRec->setResolveFirst(
true);
1230 CurMultiClass->
DefPrototypes.push_back(std::move(NewRecOwner));
1235 assert(RV &&
"Template arg doesn't exist?");
1236 NewRec->addValue(*RV);
1242 assert(MCNameRV &&
"multiclass record must have a NAME");
1259 std::vector<Init*> Vals;
1262 Vals = ParseValueList(CurRec);
1263 if (Vals.empty())
return nullptr;
1266 TokError(
"expected '}' at end of bit list value");
1276 for (
unsigned i = 0, e = Vals.size(); i != e; ++i) {
1281 if (
BitsInit *BI = dyn_cast<BitsInit>(Vals[i])) {
1282 for (
unsigned i = 0, e = BI->getNumBits(); i != e; ++i)
1283 NewBits.
push_back(BI->getBit((e - i) - 1));
1287 if (
VarInit *
VI = dyn_cast<VarInit>(Vals[i])) {
1288 if (
BitsRecTy *BitsRec = dyn_cast<BitsRecTy>(
VI->getType())) {
1289 for (
unsigned i = 0, e = BitsRec->getNumBits(); i != e; ++i)
1298 Error(BraceLoc,
"Element #" +
Twine(i) +
" (" + Vals[i]->getAsString() +
1299 ") is not convertable to a bit");
1304 std::reverse(NewBits.
begin(), NewBits.
end());
1309 std::vector<Init*> Vals;
1311 RecTy *DeducedEltTy =
nullptr;
1317 TokError(
Twine(
"Type mismatch for list, expected list type, got ") +
1321 GivenListTy = ListType;
1325 Vals = ParseValueList(CurRec,
nullptr,
1327 if (Vals.empty())
return nullptr;
1330 TokError(
"expected ']' at end of list value");
1335 RecTy *GivenEltTy =
nullptr;
1340 GivenEltTy = ParseType();
1347 TokError(
"expected '>' at end of list element type");
1354 RecTy *EltTy =
nullptr;
1355 for (
Init *V : Vals) {
1364 TokError(
"Incompatible types in list elements");
1376 TokError(
"Incompatible types in list elements");
1393 TokError(
"Element type mismatch for list");
1397 DeducedEltTy = EltTy;
1405 TokError(
"expected identifier in dag init");
1410 if (!Operator)
return nullptr;
1413 std::string OperatorName;
1416 TokError(
"expected variable name in dag operator");
1423 std::vector<std::pair<llvm::Init*, std::string> > DagArgs;
1425 DagArgs = ParseDagArgList(CurRec);
1426 if (DagArgs.empty())
return nullptr;
1430 TokError(
"expected ')' in dag init");
1454 return ParseOperation(CurRec, ItemType);
1468 Init *TGParser::ParseValue(
Record *CurRec,
RecTy *ItemType, IDParseMode Mode) {
1469 Init *Result = ParseSimpleValue(CurRec, ItemType, Mode);
1470 if (!Result)
return nullptr;
1475 default:
return Result;
1477 if (Mode == ParseNameMode || Mode == ParseForeachMode)
1483 std::vector<unsigned> Ranges = ParseRangeList();
1484 if (Ranges.empty())
return nullptr;
1487 std::reverse(Ranges.begin(), Ranges.end());
1490 Error(CurlyLoc,
"Invalid bit range for value");
1496 TokError(
"expected '}' at end of bit range list");
1505 std::vector<unsigned> Ranges = ParseRangeList();
1506 if (Ranges.empty())
return nullptr;
1508 Result = Result->convertInitListSlice(Ranges);
1510 Error(SquareLoc,
"Invalid range for list slice");
1516 TokError(
"expected ']' at end of list slice");
1524 TokError(
"expected field identifier after '.'");
1529 Result->getAsString() +
"'");
1544 Error(PasteLoc,
"LHS of paste is not typed!");
1568 Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode);
1571 Error(PasteLoc,
"RHS of paste is not typed!");
1595 std::vector<std::pair<llvm::Init*, std::string> >
1596 TGParser::ParseDagArgList(
Record *CurRec) {
1597 std::vector<std::pair<llvm::Init*, std::string> > Result;
1607 Init *Val = ParseValue(CurRec);
1609 return std::vector<std::pair<llvm::Init*, std::string> >();
1615 TokError(
"expected variable name in dag literal");
1616 return std::vector<std::pair<llvm::Init*, std::string> >();
1622 Result.push_back(std::make_pair(Val, VarName));
1638 std::vector<Init*> TGParser::ParseValueList(
Record *CurRec,
Record *ArgsRec,
1640 std::vector<Init*> Result;
1641 RecTy *ItemType = EltTy;
1642 unsigned int ArgN = 0;
1643 if (ArgsRec && !EltTy) {
1645 if (TArgs.empty()) {
1646 TokError(
"template argument provided to non-template class");
1647 return std::vector<Init*>();
1651 errs() <<
"Cannot find template arg " << ArgN <<
" (" << TArgs[ArgN]
1654 assert(RV &&
"Template argument record not found??");
1658 Result.push_back(ParseValue(CurRec, ItemType));
1659 if (!Result.back())
return std::vector<Init*>();
1664 if (ArgsRec && !EltTy) {
1666 if (ArgN >= TArgs.size()) {
1667 TokError(
"too many template arguments");
1668 return std::vector<Init*>();
1671 assert(RV &&
"Template argument record not found??");
1675 Result.push_back(ParseValue(CurRec, ItemType));
1676 if (!Result.back())
return std::vector<Init*>();
1693 Init *TGParser::ParseDeclaration(
Record *CurRec,
1694 bool ParsingTemplateArgs) {
1697 if (HasField) Lex.
Lex();
1699 RecTy *Type = ParseType();
1700 if (!Type)
return nullptr;
1703 TokError(
"Expected identifier in declaration");
1711 if (ParsingTemplateArgs) {
1713 DeclName =
QualifyName(*CurRec, CurMultiClass, DeclName,
":");
1715 assert(CurMultiClass);
1717 DeclName =
QualifyName(CurMultiClass->
Rec, CurMultiClass, DeclName,
1722 if (AddValue(CurRec, IdLoc,
RecordVal(DeclName, Type, HasField)))
1729 Init *Val = ParseValue(CurRec, Type);
1731 SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val))
1749 VarInit *TGParser::ParseForeachDeclaration(
ListInit *&ForeachListValue) {
1751 TokError(
"Expected identifier in foreach declaration");
1760 TokError(
"Expected '=' in foreach declaration");
1765 RecTy *IterType =
nullptr;
1766 std::vector<unsigned>
Ranges;
1769 default:
TokError(
"Unknown token when expecting a range list");
return nullptr;
1771 Init *List = ParseSimpleValue(
nullptr,
nullptr, ParseForeachMode);
1773 if (!ForeachListValue) {
1780 TokError(
"Value list is not of list type");
1788 if (ParseRangePiece(Ranges))
1795 Ranges = ParseRangeList();
1797 TokError(
"expected '}' at end of bit range list");
1805 if (!Ranges.empty()) {
1806 assert(!IterType &&
"Type already initialized?");
1808 std::vector<Init*> Values;
1809 for (
unsigned R : Ranges)
1827 bool TGParser::ParseTemplateArgList(
Record *CurRec) {
1831 Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->
Rec;
1834 Init *TemplArg = ParseDeclaration(CurRec,
true);
1844 TemplArg = ParseDeclaration(CurRec,
true);
1851 return TokError(
"expected '>' at end of template argument list");
1861 bool TGParser::ParseBodyItem(
Record *CurRec) {
1863 if (!ParseDeclaration(CurRec,
false))
1867 return TokError(
"expected ';' after declaration");
1874 return TokError(
"expected field identifier after let");
1880 std::vector<unsigned> BitList;
1881 if (ParseOptionalBitList(BitList))
1883 std::reverse(BitList.begin(), BitList.end());
1886 return TokError(
"expected '=' in let expression");
1891 return TokError(
"Value '" + FieldName +
"' unknown!");
1895 Init *Val = ParseValue(CurRec, Type);
1896 if (!Val)
return true;
1899 return TokError(
"expected ';' after let expression");
1902 return SetValue(CurRec, IdLoc, FieldName, BitList, Val);
1912 bool TGParser::ParseBody(
Record *CurRec) {
1920 return TokError(
"Expected ';' or '{' to start body");
1925 if (ParseBodyItem(CurRec))
1935 bool TGParser::ApplyLetStack(
Record *CurRec) {
1936 for (std::vector<LetRecord> &LetInfo : LetStack)
1938 if (SetValue(CurRec, LR.Loc, LR.Name, LR.Bits, LR.Value))
1952 bool TGParser::ParseObjectBody(
Record *CurRec) {
1961 if (!SubClass.
Rec)
return true;
1964 if (AddSubClass(CurRec, SubClass))
1969 SubClass = ParseSubClassReference(CurRec,
false);
1973 if (ApplyLetStack(CurRec))
1976 return ParseBody(CurRec);
1984 bool TGParser::ParseDef(
MultiClass *CurMultiClass) {
1990 std::unique_ptr<Record> CurRecOwner;
1991 Init *Name = ParseObjectName(CurMultiClass);
1993 CurRecOwner = make_unique<Record>(
Name, DefLoc, Records);
1995 CurRecOwner = llvm::make_unique<Record>(GetNewAnonymousName(), DefLoc,
1997 Record *CurRec = CurRecOwner.get();
1999 if (!CurMultiClass && Loops.empty()) {
2003 if (Records.getDef(CurRec->getNameInitAsString()))
2004 return Error(DefLoc,
"def '" + CurRec->getNameInitAsString()+
2005 "' already defined");
2006 Records.addDef(std::move(CurRecOwner));
2008 if (ParseObjectBody(CurRec))
2010 }
else if (CurMultiClass) {
2016 if (ParseObjectBody(CurRec))
2021 if (Proto->getNameInit() == CurRec->getNameInit())
2022 return Error(DefLoc,
"def '" + CurRec->getNameInitAsString() +
2023 "' already defined in this multiclass!");
2024 CurMultiClass->
DefPrototypes.push_back(std::move(CurRecOwner));
2025 }
else if (ParseObjectBody(CurRec)) {
2033 CurRec->resolveReferences();
2036 assert(CurRec->getTemplateArgs().empty() &&
"How'd this get template args?");
2038 if (CurMultiClass) {
2042 assert(RV &&
"Template arg doesn't exist?");
2043 CurRec->addValue(*RV);
2047 if (ProcessForeachDefs(CurRec, DefLoc))
2048 return Error(DefLoc,
"Could not process loops for def" +
2049 CurRec->getNameInitAsString());
2060 bool TGParser::ParseForeach(
MultiClass *CurMultiClass) {
2067 VarInit *IterName = ParseForeachDeclaration(ListValue);
2069 return TokError(
"expected declaration in for");
2076 Loops.push_back(
ForeachLoop(IterName, ListValue));
2080 if (ParseObject(CurMultiClass))
2088 if (ParseObjectList(CurMultiClass))
2092 TokError(
"expected '}' at end of foreach command");
2093 return Error(BraceLoc,
"to match this '{'");
2108 bool TGParser::ParseClass() {
2113 return TokError(
"expected class name after 'class' keyword");
2122 "' already defined");
2127 CurRec = NewRec.get();
2128 Records.addClass(std::move(NewRec));
2134 if (ParseTemplateArgList(CurRec))
2138 return ParseObjectBody(CurRec);
2147 std::vector<LetRecord> TGParser::ParseLetList() {
2148 std::vector<LetRecord> Result;
2152 TokError(
"expected identifier in let definition");
2153 return std::vector<LetRecord>();
2160 std::vector<unsigned>
Bits;
2161 if (ParseOptionalRangeList(Bits))
2162 return std::vector<LetRecord>();
2163 std::reverse(Bits.begin(), Bits.end());
2166 TokError(
"expected '=' in let expression");
2167 return std::vector<LetRecord>();
2171 Init *Val = ParseValue(
nullptr);
2172 if (!Val)
return std::vector<LetRecord>();
2175 Result.emplace_back(std::move(Name), std::move(Bits), Val, NameLoc);
2189 bool TGParser::ParseTopLevelLet(
MultiClass *CurMultiClass) {
2194 std::vector<LetRecord> LetInfo = ParseLetList();
2195 if (LetInfo.empty())
return true;
2196 LetStack.push_back(std::move(LetInfo));
2199 return TokError(
"expected 'in' at end of top-level 'let'");
2205 if (ParseObject(CurMultiClass))
2213 if (ParseObjectList(CurMultiClass))
2217 TokError(
"expected '}' at end of top level let command");
2218 return Error(BraceLoc,
"to match this '{'");
2224 LetStack.pop_back();
2238 bool TGParser::ParseMultiClass() {
2243 return TokError(
"expected identifier after multiclass for name");
2247 MultiClasses.insert(std::make_pair(Name,
2248 llvm::make_unique<MultiClass>(Name, Lex.
getLoc(),Records)));
2251 return TokError(
"multiclass '" + Name +
"' already defined");
2253 CurMultiClass = Result.first->second.get();
2258 if (ParseTemplateArgList(
nullptr))
2261 bool inherits =
false;
2271 ParseSubMultiClassReference(CurMultiClass);
2274 if (!SubMultiClass.
MC)
return true;
2277 if (AddSubMultiClass(CurMultiClass, SubMultiClass))
2282 SubMultiClass = ParseSubMultiClassReference(CurMultiClass);
2288 return TokError(
"expected '{' in multiclass definition");
2290 return TokError(
"expected ';' in multiclass definition");
2294 return TokError(
"multiclass must contain at least one def");
2299 return TokError(
"expected 'let', 'def' or 'defm' in multiclass body");
2304 if (ParseObject(CurMultiClass))
2312 CurMultiClass =
nullptr;
2321 const std::vector<Init *> &TArgs,
2322 std::vector<Init *> &TemplateVals) {
2331 bool IsAnonymous =
false;
2340 if (DefNameString) {
2352 Locs.append(DefProto->
getLoc().begin(), DefProto->
getLoc().end());
2353 auto CurRec = make_unique<Record>(DefName, Locs, Records, IsAnonymous);
2358 AddSubClass(CurRec.get(), Ref);
2363 if (SetValue(CurRec.get(), Ref.RefRange.Start,
"NAME",
2364 std::vector<unsigned>(), DefmPrefix)) {
2365 Error(DefmPrefixRange.
Start,
"Could not resolve " +
2366 CurRec->getNameInitAsString() +
":NAME to '" +
2374 if (!DefNameString) {
2376 CurRec->resolveReferencesTo(DefNameRV);
2379 if (!CurMultiClass) {
2383 CurRec->resolveReferencesTo(DefNameRV);
2387 DefName = CurRec->getNameInit();
2392 if (!DefNameString) {
2393 ResolveMulticlassDefArgs(MC, CurRec.get(), DefmPrefixRange.
Start,
2394 Lex.
getLoc(), TArgs, TemplateVals,
2396 DefName = CurRec->getNameInit();
2404 if (!DefNameString) {
2406 DefName->getAsUnquotedString() +
" is not a string.");
2410 CurRec->setName(DefName);
2423 if (Records.getDef(CurRec->getNameInitAsString())) {
2424 Error(DefmPrefixRange.
Start,
"def '" + CurRec->getNameInitAsString() +
2425 "' already defined, instantiating defm with subdef '" +
2430 Record *CurRecSave = CurRec.get();
2431 Records.addDef(std::move(CurRec));
2437 return CurRec.release();
2440 bool TGParser::ResolveMulticlassDefArgs(
MultiClass &MC,
2442 SMLoc DefmPrefixLoc,
2444 const std::vector<Init *> &TArgs,
2445 std::vector<Init *> &TemplateVals,
2449 for (
unsigned i = 0, e = TArgs.size(); i != e; ++i) {
2451 if (i < TemplateVals.size()) {
2453 if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(),
2465 return Error(SubClassLoc,
"value not specified for template argument #" +
2466 Twine(i) +
" (" + TArgs[i]->getAsUnquotedString() +
2474 bool TGParser::ResolveMulticlassDef(
MultiClass &MC,
2477 SMLoc DefmPrefixLoc) {
2479 if (ApplyLetStack(CurRec))
2480 return Error(DefmPrefixLoc,
"when instantiating this defm");
2488 if (Proto->getNameInit() == CurRec->
getNameInit())
2490 "' already defined in this multiclass!");
2491 CurMultiClass->
DefPrototypes.push_back(std::unique_ptr<Record>(CurRec));
2496 assert(RV &&
"Template arg doesn't exist?");
2507 bool TGParser::ParseDefm(
MultiClass *CurMultiClass) {
2510 Init *DefmPrefix =
nullptr;
2513 DefmPrefix = ParseObjectName(CurMultiClass);
2518 return TokError(
"expected ':' after defm identifier");
2521 std::vector<Record*> NewRecDefs;
2524 bool InheritFromClass =
false;
2533 if (!Ref.
Rec)
return true;
2539 assert(MC &&
"Didn't lookup multiclass correctly?");
2544 if (TArgs.size() < TemplateVals.size())
2545 return Error(SubClassLoc,
2546 "more template args specified than multiclass expects");
2549 for (
const std::unique_ptr<Record> &DefProto : MC->
DefPrototypes) {
2555 Record *CurRec = InstantiateMulticlassDef(*MC, DefProto.get(), DefmPrefix,
2558 TArgs, TemplateVals);
2563 if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc,
2564 TArgs, TemplateVals,
true))
2565 return Error(SubClassLoc,
"could not instantiate def");
2567 if (ResolveMulticlassDef(*MC, CurRec, DefProto.get(), DefmLoc))
2568 return Error(SubClassLoc,
"could not instantiate def");
2572 if (DefProto->isResolveFirst() && !CurMultiClass) {
2576 NewRecDefs.push_back(CurRec);
2584 return TokError(
"expected identifier");
2586 SubClassLoc = Lex.
getLoc();
2590 InheritFromClass = (Records.getClass(Lex.
getCurStrVal()) !=
nullptr);
2592 if (InheritFromClass)
2595 Ref = ParseSubClassReference(
nullptr,
true);
2598 if (InheritFromClass) {
2604 if (!SubClass.
Rec)
return true;
2608 for (
Record *CurRec : NewRecDefs) {
2610 if (AddSubClass(CurRec, SubClass))
2613 if (ApplyLetStack(CurRec))
2619 SubClass = ParseSubClassReference(
nullptr,
false);
2624 for (
Record *CurRec : NewRecDefs)
2631 return TokError(
"expected ';' at end of defm");
2647 return TokError(
"Expected class, def, defm, multiclass or let definition");
2648 case tgtok::Let:
return ParseTopLevelLet(MC);
2659 bool TGParser::ParseObjectList(
MultiClass *MC) {
2661 if (ParseObject(MC))
2669 if (ParseObjectList())
return true;
2675 return TokError(
"Unexpected input at top level");
static BinOpInit * get(BinaryOp opc, Init *lhs, Init *rhs, RecTy *Type)
Represents a range in source code.
void push_back(const T &Elt)
std::pair< int64_t, unsigned > getCurBinaryIntVal() const
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
virtual bool isComplete() const
isComplete - This virtual method should be overridden by values that may not be completely specified ...
unsigned getNumBits() const
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
tgtok::TokKind getCode() const
ListInit - [AL, AH, CL] - Represent a list of defs.
ListRecTy - 'list<Ty>' - Represent a list of values, all of which must be of the specified type...
virtual bool typeIsConvertibleTo(const RecTy *RHS) const
typeIsConvertibleTo - Return true if all values of 'this' type can be converted to the specified type...
static UnOpInit * get(UnaryOp opc, Init *lhs, RecTy *Type)
void addDef(std::unique_ptr< Record > R)
Init * resolveListElementReference(Record &R, const RecordVal *RV, unsigned Elt) const override
resolveListElementReference - This method is used to implement VarListElementInit::resolveReferences...
Init * getElement(unsigned i) const
void setResolveFirst(bool b)
BitsInit - { a, b, c } - Represents an initializer for a BitsRecTy value.
virtual std::string getAsUnquotedString() const
getAsUnquotedString - Convert this value to a string form, without adding quote markers.
ArrayRef< Init * > getTemplateArgs() const
const std::string & getName() const
static IntInit * get(int64_t V)
void addSuperClass(Record *R, SMRange Range)
static BitsRecTy * get(unsigned Sz)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
TypedInit - This is the common super-class of types that have a specific, explicit, type.
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
T LLVM_ATTRIBUTE_UNUSED_RESULT pop_back_val()
ArrayRef< SMRange > getSuperClassRanges() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static StringRecTy * get()
Init * convertInitializerTo(RecTy *Ty) const override
convertInitializerTo - This virtual function converts to the appropriate Init based on the passed in ...
Record * getClass(const std::string &Name) const
BitsRecTy - 'bits<n>' - Represent a fixed number of bits.
const std::string getNameInitAsString() const
void resolveReferences()
resolveReferences - If there are any field references that refer to fields that have been filled in...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
static std::string utostr(uint64_t X, bool isNeg=false)
const std::string & getName() const
size_t size() const
size - Get the array size.
static BitsInit * get(ArrayRef< Init * > Range)
void resolveReferencesTo(const RecordVal *RV)
resolveReferencesTo - If anything in this record refers to RV, replace the reference to RV with the R...
virtual Init * convertInitializerTo(RecTy *Ty) const =0
convertInitializerTo - This virtual function converts to the appropriate Init based on the passed in ...
Record * getDef(const std::string &Name) const
int64_t getCurIntVal() const
static VarInit * get(const std::string &VN, RecTy *T)
The instances of the Type class are immutable: once they are created, they are never changed...
const std::string & getCurStrVal() const
StringInit - "foo" - Represent an initialization by a string value.
RecordVector DefPrototypes
const RecordVal * getValue(const Init *Name) const
StringRecTy - 'string' - Represent an string value.
static BitInit * get(bool V)
static FieldInit * get(Init *R, const std::string &FN)
DefInit * getDefInit()
get the corresponding DefInit.
static bool isObjectStart(tgtok::TokKind K)
isObjectStart - Return true if this is a valid first token for an Object.
Init * QualifyName(Record &CurRec, MultiClass *CurMultiClass, Init *Name, const std::string &Scoper)
QualifyName - Return an Init with a qualifier prefix referring to CurRec's name.
const Init * getNameInit() const
ForeachLoop - Record the iteration state associated with a for loop.
void addTemplateArg(Init *Name)
bool Error(SMLoc L, const Twine &Msg) const
ArrayRef< SMLoc > getLoc() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
This is a utility class that provides an abstraction for the common functionality between Instruction...
static StringInit * get(StringRef)
static RecordRecTy * get(Record *R)
CHAIN = SC CHAIN, Imm128 - System call.
Init * getNameInit() const
static DefInit * get(Record *)
ArrayRef< Record * > getSuperClasses() const
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
bool isTemplateArg(Init *Name) const
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void removeValue(Init *Name)
Init * getBit(unsigned Bit) const override
getBit - This method is used to return the initializer for the specified bit.
ArrayRef< RecordVal > getValues() const
std::vector< Init * > TemplateArgs
RecTy * getElementType() const
static DagInit * get(Init *V, const std::string &VN, ArrayRef< Init * > ArgRange, ArrayRef< std::string > NameRange)
bool isSubClassOf(const Record *R) const
virtual Init * convertInitializerBitRange(const std::vector< unsigned > &Bits) const
convertInitializerBitRange - This method is used to implement the bitrange selection operator...
std::vector< Init * > TemplateArgs
void addValue(const RecordVal &RV)
static TernOpInit * get(TernaryOp opc, Init *lhs, Init *mhs, Init *rhs, RecTy *Type)
RecTy * resolveTypes(RecTy *T1, RecTy *T2)
resolveTypes - Find a common type that T1 and T2 convert to.
VarInit - 'Opcode' - Represent a reference to an entire variable object.
static ListInit * get(ArrayRef< Init * > Range, RecTy *EltTy)
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
static ListRecTy * get(RecTy *T)
Init * Fold(Record *CurRec, MultiClass *CurMultiClass) const override
const std::string & getName() const
Init * getNameInit() const
virtual std::string getAsString() const =0
getAsString - Convert this value to a string form.
virtual std::string getAsString() const =0
Represents a location in source code.
bool TokError(const Twine &Msg) const
Statically lint checks LLVM IR