TGParser.cpp revision 47cfec02842f885b46ea0d3c812793e660691640
1f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===- TGParser.cpp - Parser for TableGen Files ---------------------------===// 2f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// 3f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// The LLVM Compiler Infrastructure 4f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// 53060910e290949a9ac5eda8726d030790c4d60ffChris Lattner// This file is distributed under the University of Illinois Open Source 63060910e290949a9ac5eda8726d030790c4d60ffChris Lattner// License. See LICENSE.TXT for details. 7f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// 8f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 9f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// 10f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// Implement the Parser for TableGen. 11f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// 12f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 13f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 14f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner#include "TGParser.h" 15d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/SmallVector.h" 16f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner#include "llvm/ADT/StringExtras.h" 17d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/Support/CommandLine.h" 18d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/TableGen/Record.h" 191a55180238dbcf11113f610aea010447e51f595bDaniel Dunbar#include <algorithm> 201a55180238dbcf11113f610aea010447e51f595bDaniel Dunbar#include <sstream> 21f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerusing namespace llvm; 22f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 23f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 24f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// Support Code for the Semantic Actions. 25f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 26f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 27f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnernamespace llvm { 28f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstruct SubClassReference { 29b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMRange RefRange; 30f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Rec; 3105bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> TemplateArgs; 321c8ae59dfdc85d917db0333ae0b93e2be4ca6c36Chris Lattner SubClassReference() : Rec(0) {} 33d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 34f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool isInvalid() const { return Rec == 0; } 35f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner}; 36de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 37de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greenestruct SubMultiClassReference { 38b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMRange RefRange; 39de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *MC; 4005bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> TemplateArgs; 41de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClassReference() : MC(0) {} 4232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 43de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene bool isInvalid() const { return MC == 0; } 44d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene void dump() const; 45de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene}; 46d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 47d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greenevoid SubMultiClassReference::dump() const { 481a55180238dbcf11113f610aea010447e51f595bDaniel Dunbar errs() << "Multiclass:\n"; 4921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 50d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene MC->dump(); 5121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 521a55180238dbcf11113f610aea010447e51f595bDaniel Dunbar errs() << "Template args:\n"; 5305bce0beee87512e52428d4b80f5a8e79a949576David Greene for (std::vector<Init *>::const_iterator i = TemplateArgs.begin(), 54d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene iend = TemplateArgs.end(); 55d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene i != iend; 56d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene ++i) { 57d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene (*i)->dump(); 58d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene } 59d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene} 60d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 61f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} // end namespace llvm 62f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 631e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattnerbool TGParser::AddValue(Record *CurRec, SMLoc Loc, const RecordVal &RV) { 64f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec == 0) 65f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec = &CurMultiClass->Rec; 6621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 67ebaf92c67dac4974f98a08f8096d3eb2f4edd09dJakob Stoklund Olesen if (RecordVal *ERV = CurRec->getValue(RV.getNameInit())) { 68f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // The value already exists in the class, treat this as a set. 69f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ERV->setValue(RV.getValue())) 70f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "New definition of '" + RV.getName() + "' of type '" + 71f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RV.getType()->getAsString() + "' is incompatible with " + 7221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson "previous definition of type '" + 73f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner ERV->getType()->getAsString() + "'"); 74f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 75f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->addValue(RV); 76f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 77f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 78f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 79f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 80f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SetValue - 81f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Return true on error, false on success. 82917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greenebool TGParser::SetValue(Record *CurRec, SMLoc Loc, Init *ValName, 8305bce0beee87512e52428d4b80f5a8e79a949576David Greene const std::vector<unsigned> &BitList, Init *V) { 84f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!V) return false; 85f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 86f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec == 0) CurRec = &CurMultiClass->Rec; 87f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 88f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecordVal *RV = CurRec->getValue(ValName); 89f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (RV == 0) 90917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene return Error(Loc, "Value '" + ValName->getAsUnquotedString() 91917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene + "' unknown!"); 92f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 93f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Do not allow assignments like 'X = X'. This will just cause infinite loops 94f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // in the resolution machinery. 95f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (BitList.empty()) 966cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva if (VarInit *VI = dyn_cast<VarInit>(V)) 97917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene if (VI->getNameInit() == ValName) 98f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 9921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 100f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If we are assigning to a subset of the bits in the value... then we must be 101f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // assigning to a field of BitsRecTy, which must have a BitsInit 102f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // initializer. 103f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // 104f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!BitList.empty()) { 1056cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva BitsInit *CurVal = dyn_cast<BitsInit>(RV->getValue()); 106f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurVal == 0) 107917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene return Error(Loc, "Value '" + ValName->getAsUnquotedString() 108917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene + "' is not a bits type"); 109f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 110f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Convert the incoming value to a bits type of the appropriate size... 11105bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *BI = V->convertInitializerTo(BitsRecTy::get(BitList.size())); 112f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (BI == 0) { 113f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Initializer is not compatible with bit range"); 114f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 11521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 116f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // We should have a BitsInit type now. 1176cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva BitsInit *BInit = dyn_cast<BitsInit>(BI); 118f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(BInit != 0); 119f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 12005bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init *, 16> NewBits(CurVal->getNumBits()); 121f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 122f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Loop over bits, assigning values as appropriate. 123f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = BitList.size(); i != e; ++i) { 124f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner unsigned Bit = BitList[i]; 125ca7fd3de360b266783438666054dba198ff77ba2David Greene if (NewBits[Bit]) 126f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" + 127917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene ValName->getAsUnquotedString() + "' more than once"); 128ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[Bit] = BInit->getBit(i); 129f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 130f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 131f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) 132ca7fd3de360b266783438666054dba198ff77ba2David Greene if (NewBits[i] == 0) 133ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[i] = CurVal->getBit(i); 134f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 135dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene V = BitsInit::get(NewBits); 136f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 137f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 138f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (RV->setValue(V)) 139917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '" 140917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene + RV->getType()->getAsString() + 141917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene "' is incompatible with initializer '" + V->getAsString() 142917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene + "'"); 143f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 144f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 146f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template 147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// args as SubClass's template arguments. 148aff9c270de8de7d1a0bc138d391bc67136bad58eCedric Venetbool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { 149f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *SC = SubClass.Rec; 150f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add all of the values in the subclass into the current class. 151f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<RecordVal> &Vals = SC->getValues(); 152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = Vals.size(); i != e; ++i) 153b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (AddValue(CurRec, SubClass.RefRange.Start, Vals[i])) 154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 156e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = SC->getTemplateArgs(); 157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Ensure that an appropriate number of template arguments are specified. 159f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (TArgs.size() < SubClass.TemplateArgs.size()) 160b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose return Error(SubClass.RefRange.Start, 161b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose "More template args specified than expected"); 16221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 163f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Loop over all of the template arguments, setting them to the specified 164f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // value or leaving them as the default if necessary. 165f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 166f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (i < SubClass.TemplateArgs.size()) { 167f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If a value is specified for this template arg, set it now. 168b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (SetValue(CurRec, SubClass.RefRange.Start, TArgs[i], 169b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose std::vector<unsigned>(), SubClass.TemplateArgs[i])) 170f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 17121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 172f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Resolve it next. 173f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 17421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 175f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Now remove it. 176f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->removeValue(TArgs[i]); 177f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 178f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 179b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose return Error(SubClass.RefRange.Start, 180b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose "Value not specified for template argument #" 181e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + utostr(i) + " (" + TArgs[i]->getAsUnquotedString() 182e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + ") of subclass '" + SC->getNameInitAsString() + "'!"); 183f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 184f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 185f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 186f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Since everything went well, we can now set the "superclass" list for the 187f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // current record. 188f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<Record*> &SCs = SC->getSuperClasses(); 189b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose ArrayRef<SMRange> SCRanges = SC->getSuperClassRanges(); 190f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = SCs.size(); i != e; ++i) { 191f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isSubClassOf(SCs[i])) 192b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose return Error(SubClass.RefRange.Start, 193f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "Already subclass of '" + SCs[i]->getName() + "'!\n"); 194b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose CurRec->addSuperClass(SCs[i], SCRanges[i]); 195f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 19621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 197f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isSubClassOf(SC)) 198b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose return Error(SubClass.RefRange.Start, 199f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "Already subclass of '" + SC->getName() + "'!\n"); 200b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose CurRec->addSuperClass(SC, SubClass.RefRange); 201f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 202f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 203f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 204de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// AddSubMultiClass - Add SubMultiClass as a subclass to 205440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson/// CurMC, resolving its template args as SubMultiClass's 206de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// template arguments. 207440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilsonbool TGParser::AddSubMultiClass(MultiClass *CurMC, 2081d512df4a9e249ceb6d6b69e89527c75d872ecd1Bob Wilson SubMultiClassReference &SubMultiClass) { 209de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *SMC = SubMultiClass.MC; 210440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson Record *CurRec = &CurMC->Rec; 211de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 212440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson const std::vector<RecordVal> &MCVals = CurRec->getValues(); 213de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 214de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the values in the subclass into the current class. 215de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues(); 216de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = SMCVals.size(); i != e; ++i) 217b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (AddValue(CurRec, SubMultiClass.RefRange.Start, SMCVals[i])) 218de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 219de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 220440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson int newDefStart = CurMC->DefPrototypes.size(); 221d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 222de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the defs in the subclass into the current multiclass. 223de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(), 224de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene iend = SMC->DefPrototypes.end(); 225de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene i != iend; 226de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene ++i) { 227de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Clone the def and add it to the current multiclass 228de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Record *NewDef = new Record(**i); 229de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 230de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the values in the superclass into the current def. 231de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = MCVals.size(); i != e; ++i) 232b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (AddValue(NewDef, SubMultiClass.RefRange.Start, MCVals[i])) 233de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 234de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 235440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson CurMC->DefPrototypes.push_back(NewDef); 236de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 23732558650ae09247b31aa89cdee64e8c849771024Bob Wilson 238e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs(); 239de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 240d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // Ensure that an appropriate number of template arguments are 241d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // specified. 242de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) 243b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose return Error(SubMultiClass.RefRange.Start, 244d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene "More template args specified than expected"); 24532558650ae09247b31aa89cdee64e8c849771024Bob Wilson 246de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Loop over all of the template arguments, setting them to the specified 247de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // value or leaving them as the default if necessary. 248de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { 249de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (i < SubMultiClass.TemplateArgs.size()) { 250d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // If a value is specified for this template arg, set it in the 251d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // superclass now. 252b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (SetValue(CurRec, SubMultiClass.RefRange.Start, SMCTArgs[i], 25332558650ae09247b31aa89cdee64e8c849771024Bob Wilson std::vector<unsigned>(), 254de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass.TemplateArgs[i])) 255de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 256de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 257de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Resolve it next. 258de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i])); 25932558650ae09247b31aa89cdee64e8c849771024Bob Wilson 260de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Now remove it. 261de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene CurRec->removeValue(SMCTArgs[i]); 262de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 263d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // If a value is specified for this template arg, set it in the 264d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // new defs now. 265d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene for (MultiClass::RecordVector::iterator j = 266440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson CurMC->DefPrototypes.begin() + newDefStart, 267440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson jend = CurMC->DefPrototypes.end(); 268de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene j != jend; 269de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene ++j) { 270de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Record *Def = *j; 271de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 272b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (SetValue(Def, SubMultiClass.RefRange.Start, SMCTArgs[i], 27332558650ae09247b31aa89cdee64e8c849771024Bob Wilson std::vector<unsigned>(), 274de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass.TemplateArgs[i])) 275de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 276de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 277de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Resolve it next. 278de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Def->resolveReferencesTo(Def->getValue(SMCTArgs[i])); 279de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 280de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Now remove it 281de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Def->removeValue(SMCTArgs[i]); 282de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 283de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) { 284b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose return Error(SubMultiClass.RefRange.Start, 285d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene "Value not specified for template argument #" 286e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString() 287e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!"); 288de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 289de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 290de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 291de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return false; 292de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 293de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 294cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ProcessForeachDefs - Given a record, apply all of the variable 295cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// values in all surrounding foreach loops, creating new records for 296cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// each combination of values. 2978e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesenbool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) { 2988e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (Loops.empty()) 2998e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return false; 3008e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen 301cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // We want to instantiate a new copy of CurRec for each combination 302cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // of nested loop iterator values. We don't want top instantiate 303cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // any copies until we have values for each loop iterator. 304cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene IterSet IterVals; 3058e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return ProcessForeachDefs(CurRec, Loc, IterVals); 306cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 307cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 308cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ProcessForeachDefs - Given a record, a loop and a loop iterator, 309cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// apply each of the variable values in this loop and then process 310cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// subloops. 3118e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesenbool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){ 3128e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Recursively build a tuple of iterator values. 3138e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (IterVals.size() != Loops.size()) { 3148e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen assert(IterVals.size() < Loops.size()); 3158e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen ForeachLoop &CurLoop = Loops[IterVals.size()]; 3166cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva ListInit *List = dyn_cast<ListInit>(CurLoop.ListValue); 3178e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (List == 0) { 3188e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "Loop list is not a list"); 3198e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3208e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 321cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3228e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Process each value. 3238e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen for (int64_t i = 0; i < List->getSize(); ++i) { 3248e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i); 3258e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal)); 3268e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (ProcessForeachDefs(CurRec, Loc, IterVals)) 3278e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3288e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterVals.pop_back(); 3298e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 3308e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return false; 3318e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 332cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3338e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // This is the bottom of the recursion. We have all of the iterator values 3348e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // for this point in the iteration space. Instantiate a new record to 3358e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // reflect this combination of values. 3368e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Record *IterRec = new Record(*CurRec); 337cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3388e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Set the iterator values now. 3398e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen for (unsigned i = 0, e = IterVals.size(); i != e; ++i) { 3408e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen VarInit *IterVar = IterVals[i].IterVar; 3416cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *IVal = dyn_cast<TypedInit>(IterVals[i].IterValue); 3428e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (IVal == 0) { 3438e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "foreach iterator value is untyped"); 3448e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3458e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 346cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3478e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false)); 348cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3498e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (SetValue(IterRec, Loc, IterVar->getName(), 3508e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen std::vector<unsigned>(), IVal)) { 3518e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "when instantiating this def"); 3528e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 353cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 354cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3558e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Resolve it next. 3568e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName())); 357cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3588e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Remove it. 3598e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->removeValue(IterVar->getName()); 360cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 3618e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen 3628e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (Records.getDef(IterRec->getNameInitAsString())) { 3638e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "def already exists: " + IterRec->getNameInitAsString()); 3648e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3658e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 3668e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen 3678e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Records.addDef(IterRec); 3688e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->resolveReferences(); 369cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return false; 370cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 371cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 372f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 373f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// Parser Code 374f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 375f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 376f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// isObjectStart - Return true if this is a valid first token for an Object. 377f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstatic bool isObjectStart(tgtok::TokKind K) { 378f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return K == tgtok::Class || K == tgtok::Def || 379cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene K == tgtok::Defm || K == tgtok::Let || 380cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene K == tgtok::MultiClass || K == tgtok::Foreach; 381f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 382f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 383df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattnerstatic std::string GetNewAnonymousName() { 384df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner static unsigned AnonCounter = 0; 385f15fe8195b0a42d0e950f3694c4d6ccd4034804aMichael J. Spencer unsigned Tmp = AnonCounter++; // MSVC2012 ICEs without this. 386f15fe8195b0a42d0e950f3694c4d6ccd4034804aMichael J. Spencer return "anonymous." + utostr(Tmp); 387df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner} 388df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner 389f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectName - If an object name is specified, return it. Otherwise, 390d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose/// return 0. 391a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene/// ObjectName ::= Value [ '#' Value ]* 392f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectName ::= /*empty*/ 393f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 394a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid GreeneInit *TGParser::ParseObjectName(MultiClass *CurMultiClass) { 395a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene switch (Lex.getCode()) { 396a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene case tgtok::colon: 397a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene case tgtok::semi: 398a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene case tgtok::l_brace: 399a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene // These are all of the tokens that can begin an object body. 400a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene // Some of these can also begin values but we disallow those cases 401a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene // because they are unlikely to be useful. 402d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose return 0; 403a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene default: 404a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene break; 405a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene } 406c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 407a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Record *CurRec = 0; 408a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene if (CurMultiClass) 409a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene CurRec = &CurMultiClass->Rec; 410f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 411a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene RecTy *Type = 0; 412a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene if (CurRec) { 4133f7b7f8ce0b050fc6a0100839d9c5a84198b2aedSean Silva const TypedInit *CurRecName = dyn_cast<TypedInit>(CurRec->getNameInit()); 414a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene if (!CurRecName) { 415a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene TokError("Record name is not typed!"); 416a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene return 0; 417a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene } 418a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Type = CurRecName->getType(); 419a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene } 420a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene 421a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene return ParseValue(CurRec, Type, ParseNameMode); 422a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene} 423f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 424f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseClassID - Parse and resolve a reference to a class name. This returns 425f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// null on error. 426f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 427f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ClassID ::= ID 428f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 429f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecord *TGParser::ParseClassID() { 430f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 431f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected name for ClassID"); 432f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 433f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 43421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 435f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Result = Records.getClass(Lex.getCurStrVal()); 436f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) 437f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 43821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 439f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 440f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 441f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 442f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 44332558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ParseMultiClassID - Parse and resolve a reference to a multiclass name. 44432558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// This returns null on error. 445de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 446de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// MultiClassID ::= ID 447de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 448de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneMultiClass *TGParser::ParseMultiClassID() { 449de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::Id) { 45036febfd70e1a28d6008d45a753da4c75fd994140Sean Silva TokError("expected name for MultiClassID"); 451de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return 0; 452de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 45332558650ae09247b31aa89cdee64e8c849771024Bob Wilson 454de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; 455de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result == 0) 45636febfd70e1a28d6008d45a753da4c75fd994140Sean Silva TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); 45732558650ae09247b31aa89cdee64e8c849771024Bob Wilson 458de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 459de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 460de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 461de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 462f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseSubClassReference - Parse a reference to a subclass or to a templated 463f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// subclass. This returns a SubClassRefTy with a null Record* on error. 464f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 465f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SubClassRef ::= ClassID 466f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SubClassRef ::= ClassID '<' ValueList '>' 467f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 468f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerSubClassReference TGParser:: 469f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerParseSubClassReference(Record *CurRec, bool isDefm) { 470f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference Result; 471b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Result.RefRange.Start = Lex.getLoc(); 47221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 4737be9021754559956f7a079d6063b58a36e5bf2fbSean Silva if (isDefm) { 4747be9021754559956f7a079d6063b58a36e5bf2fbSean Silva if (MultiClass *MC = ParseMultiClassID()) 4757be9021754559956f7a079d6063b58a36e5bf2fbSean Silva Result.Rec = &MC->Rec; 4767be9021754559956f7a079d6063b58a36e5bf2fbSean Silva } else { 477f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = ParseClassID(); 4787be9021754559956f7a079d6063b58a36e5bf2fbSean Silva } 479f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result.Rec == 0) return Result; 48021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 481f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there is no template arg list, we're done. 482b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (Lex.getCode() != tgtok::less) { 483b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Result.RefRange.End = Lex.getLoc(); 484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 485b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose } 486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the '<' 48721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::greater) { 489f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("subclass reference requires a non-empty list of template values"); 490f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; 491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 492f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 49321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 494e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.TemplateArgs = ParseValueList(CurRec, Result.Rec); 495f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result.TemplateArgs.empty()) { 496f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; // Error parsing value list. 497f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 498f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 49921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 500f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 501f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' in template value list"); 502f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; 503f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 504f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 505f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 506b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Result.RefRange.End = Lex.getLoc(); 50721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 508f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 509f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 510f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 51132558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ParseSubMultiClassReference - Parse a reference to a subclass or to a 51232558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// templated submulticlass. This returns a SubMultiClassRefTy with a null 51332558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// Record* on error. 514de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 515de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// SubMultiClassRef ::= MultiClassID 516de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// SubMultiClassRef ::= MultiClassID '<' ValueList '>' 517de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 518de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneSubMultiClassReference TGParser:: 519de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneParseSubMultiClassReference(MultiClass *CurMC) { 520de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClassReference Result; 521b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Result.RefRange.Start = Lex.getLoc(); 52232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 523de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = ParseMultiClassID(); 524de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result.MC == 0) return Result; 52532558650ae09247b31aa89cdee64e8c849771024Bob Wilson 526de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // If there is no template arg list, we're done. 527b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (Lex.getCode() != tgtok::less) { 528b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Result.RefRange.End = Lex.getLoc(); 529de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 530b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose } 531de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); // Eat the '<' 53232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 533de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() == tgtok::greater) { 534de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("subclass reference requires a non-empty list of template values"); 535de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; 536de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 537de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 53832558650ae09247b31aa89cdee64e8c849771024Bob Wilson 539e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec); 540de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result.TemplateArgs.empty()) { 541de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; // Error parsing value list. 542de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 543de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 54432558650ae09247b31aa89cdee64e8c849771024Bob Wilson 545de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::greater) { 546de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("expected '>' in template value list"); 547de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; 548de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 549de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 550de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 551b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Result.RefRange.End = Lex.getLoc(); 552de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 553de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 554de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 555de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 556f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseRangePiece - Parse a bit/value range. 557f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL 558f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL '-' INTVAL 559f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL INTVAL 560f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { 561811281e788432d3136de13f72d05790eced52d92Chris Lattner if (Lex.getCode() != tgtok::IntVal) { 562811281e788432d3136de13f72d05790eced52d92Chris Lattner TokError("expected integer or bitrange"); 563811281e788432d3136de13f72d05790eced52d92Chris Lattner return true; 564811281e788432d3136de13f72d05790eced52d92Chris Lattner } 56563f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman int64_t Start = Lex.getCurIntVal(); 56663f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman int64_t End; 56721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 568f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Start < 0) 569f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("invalid range, cannot be negative"); 57021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 571f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.Lex()) { // eat first character. 57221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson default: 573f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 574f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 575f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::minus: 576f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::IntVal) { 577f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected integer value as end of range"); 578f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 579f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 580f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner End = Lex.getCurIntVal(); 581f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 582f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::IntVal: 583f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner End = -Lex.getCurIntVal(); 584f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 585f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 58621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (End < 0) 587f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("invalid range, cannot be negative"); 588f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 58921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 590f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add to the range. 591f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Start < End) { 592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (; Start <= End; ++Start) 593f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 594f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 595f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (; Start >= End; --Start) 596f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 597f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 598f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 599f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 600f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 601f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseRangeList - Parse a list of scalars and ranges into scalar values. 602f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 603f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangeList ::= RangePiece (',' RangePiece)* 604f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 605f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::vector<unsigned> TGParser::ParseRangeList() { 606f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Result; 60721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 608f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the first piece. 609f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseRangePiece(Result)) 610f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<unsigned>(); 611f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 612f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the comma. 613f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 614f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the next range piece. 615f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseRangePiece(Result)) 616f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<unsigned>(); 617f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 618f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 619f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 620f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 621f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. 622f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalRangeList ::= '<' RangeList '>' 623f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalRangeList ::= /*empty*/ 624f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { 625f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::less) 626f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 62721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 6281e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc StartLoc = Lex.getLoc(); 629f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '<' 63021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 631f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the range list. 632f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges = ParseRangeList(); 633f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return true; 63421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 635f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 636f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of range list"); 637f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(StartLoc, "to match this '<'"); 638f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 639f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '>'. 640f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 641f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 642f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 643f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. 644f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalBitList ::= '{' RangeList '}' 645f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalBitList ::= /*empty*/ 646f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { 647f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) 648f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 64921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 6501e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc StartLoc = Lex.getLoc(); 651f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 65221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 653f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the range list. 654f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges = ParseRangeList(); 655f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return true; 65621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 657f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 658f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit list"); 659f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(StartLoc, "to match this '{'"); 660f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 661f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '}'. 662f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 663f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 664f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 665f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 666f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseType - Parse and return a tblgen type. This returns null on error. 667f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 668f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= STRING // string type 6698dd6f0c8353f80de6526810899f271d539f6929cJakob Stoklund Olesen/// Type ::= CODE // code type 670f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= BIT // bit type 671f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= BITS '<' INTVAL '>' // bits<x> type 672f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= INT // int type 673f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= LIST '<' Type '>' // list<x> type 674f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= DAG // dag type 675f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= ClassID // Record Type 676f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 677f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecTy *TGParser::ParseType() { 678f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 679f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: TokError("Unknown token when expecting a type"); return 0; 68077f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::String: Lex.Lex(); return StringRecTy::get(); 6818dd6f0c8353f80de6526810899f271d539f6929cJakob Stoklund Olesen case tgtok::Code: Lex.Lex(); return StringRecTy::get(); 68277f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Bit: Lex.Lex(); return BitRecTy::get(); 68377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Int: Lex.Lex(); return IntRecTy::get(); 68477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Dag: Lex.Lex(); return DagRecTy::get(); 685f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Id: 68677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen if (Record *R = ParseClassID()) return RecordRecTy::get(R); 687f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 688f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Bits: { 689f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) { // Eat 'bits' 690f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '<' after bits type"); 691f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 692f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 693f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::IntVal) { // Eat '<' 694f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected integer in bits<n> type"); 695f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 696f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 69763f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman uint64_t Val = Lex.getCurIntVal(); 698f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::greater) { // Eat count. 699f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of bits<n> type"); 700f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 701f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 702f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '>' 70377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return BitsRecTy::get(Val); 704f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 705f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::List: { 706f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) { // Eat 'bits' 707f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '<' after list type"); 708f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 709f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 710f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '<' 711f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecTy *SubType = ParseType(); 712f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SubType == 0) return 0; 71321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 714f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 715f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of list<ty> type"); 716f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 717f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 718f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '>' 71977f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return ListRecTy::get(SubType); 720f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 72121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 722f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 723f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 724f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseIDValue - Parse an ID as a value and decode what it means. 725f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 726f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def local value] 727f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def template arg] 728f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [multiclass local value] 729f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [multiclass template argument] 730f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def name] 731f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 732f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David GreeneInit *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) { 733f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); 734f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 7351e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc Loc = Lex.getLoc(); 736f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 737f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseIDValue(CurRec, Name, Loc); 738f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 739f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 740f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID 741f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// has already been read. 74205bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseIDValue(Record *CurRec, 743f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene const std::string &Name, SMLoc NameLoc, 744f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene IDParseMode Mode) { 745f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 746f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (const RecordVal *RV = CurRec->getValue(Name)) 747dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(Name, RV->getType()); 74821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 749e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":"); 750e22b321d2276b634519165b101b02d92c2fcf5c7David Greene 751caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene if (CurMultiClass) 752e22b321d2276b634519165b101b02d92c2fcf5c7David Greene TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, 753e22b321d2276b634519165b101b02d92c2fcf5c7David Greene "::"); 754caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene 755f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isTemplateArg(TemplateArgName)) { 756f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const RecordVal *RV = CurRec->getValue(TemplateArgName); 757f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(RV && "Template arg doesn't exist??"); 758dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(TemplateArgName, RV->getType()); 759f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 760f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 76121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 762f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass) { 763e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, 764e22b321d2276b634519165b101b02d92c2fcf5c7David Greene "::"); 765e22b321d2276b634519165b101b02d92c2fcf5c7David Greene 766f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass->Rec.isTemplateArg(MCName)) { 767f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); 768f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(RV && "Template arg doesn't exist??"); 769dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(MCName, RV->getType()); 770f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 771f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 77221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 773cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // If this is in a foreach loop, make sure it's not a loop iterator 774cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene for (LoopVector::iterator i = Loops.begin(), iend = Loops.end(); 775cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene i != iend; 776cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene ++i) { 7776cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva VarInit *IterVar = dyn_cast<VarInit>(i->IterVar); 778cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (IterVar && IterVar->getName() == Name) 779cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return IterVar; 780cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 781cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 782bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene if (Mode == ParseNameMode) 783bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene return StringInit::get(Name); 784bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene 785f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Record *D = Records.getDef(Name)) 78677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return DefInit::get(D); 787f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 788bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene if (Mode == ParseValueMode) { 789bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene Error(NameLoc, "Variable not defined: '" + Name + "'"); 790bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene return 0; 791bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene } 792bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene 793bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene return StringInit::get(Name); 794f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 795f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 796d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// ParseOperation - Parse an operator. This returns null on error. 797d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 798d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// Operation ::= XOperator ['<' Type '>'] '(' Args ')' 799d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 80005bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseOperation(Record *CurRec) { 801d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene switch (Lex.getCode()) { 802d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene default: 803d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("unknown operation"); 804d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 8051434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 8061434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 8071434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 808e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: { // Value ::= !unop '(' Value ')' 809e6c27de069225e1122c78385ad22a2ff656db8e6David Greene UnOpInit::UnaryOp Code; 810e6c27de069225e1122c78385ad22a2ff656db8e6David Greene RecTy *Type = 0; 811d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 812e6c27de069225e1122c78385ad22a2ff656db8e6David Greene switch (Lex.getCode()) { 813858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 814e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: 815e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the operation 816e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Code = UnOpInit::CAST; 817d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 818e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Type = ParseOperatorType(); 819d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 820e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Type == 0) { 8215f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("did not get type for unary operator"); 822e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 823e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 824d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 825e6c27de069225e1122c78385ad22a2ff656db8e6David Greene break; 8261434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 8275f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 8281434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::HEAD; 8295f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 8301434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 8315f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 8321434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::TAIL; 8335f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 8341434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 8355f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 8361434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::EMPTY; 83777f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = IntRecTy::get(); 8385f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 839e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 840e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Lex.getCode() != tgtok::l_paren) { 841e6c27de069225e1122c78385ad22a2ff656db8e6David Greene TokError("expected '(' after unary operator"); 842e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 843e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 844e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the '(' 845d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 84605bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *LHS = ParseValue(CurRec); 847e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (LHS == 0) return 0; 848d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 8491434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD 8501434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::TAIL 8511434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::EMPTY) { 8526cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva ListInit *LHSl = dyn_cast<ListInit>(LHS); 8536cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva StringInit *LHSs = dyn_cast<StringInit>(LHS); 8546cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *LHSt = dyn_cast<TypedInit>(LHS); 855e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LHSl == 0 && LHSs == 0 && LHSt == 0) { 856e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list or string type argument in unary operator"); 8575f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8585f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8595f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSt) { 860736ceace11249da645ec4ed91b8714832193ead4Sean Silva ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType()); 861736ceace11249da645ec4ed91b8714832193ead4Sean Silva StringRecTy *SType = dyn_cast<StringRecTy>(LHSt->getType()); 862e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LType == 0 && SType == 0) { 863e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list or string type argumnet in unary operator"); 8645f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8655f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8665f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8675f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene 8681434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD 8691434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::TAIL) { 870e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LHSl == 0 && LHSt == 0) { 871e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list type argumnet in unary operator"); 872e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 873e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 87421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 8755f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSl && LHSl->getSize() == 0) { 8765f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("empty list argument in unary operator"); 8775f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8785f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8795f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSl) { 88005bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Item = LHSl->getElement(0); 8816cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *Itemt = dyn_cast<TypedInit>(Item); 8825f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (Itemt == 0) { 8835f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("untyped list element in unary operator"); 8845f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8855f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8861434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD) { 8875f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = Itemt->getType(); 88821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 88977f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = ListRecTy::get(Itemt->getType()); 8905f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 89121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 8925f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene assert(LHSt && "expected list type argument in unary operator"); 893736ceace11249da645ec4ed91b8714832193ead4Sean Silva ListRecTy *LType = dyn_cast<ListRecTy>(LHSt->getType()); 8945f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LType == 0) { 8955f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("expected list type argumnet in unary operator"); 8965f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8975f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8981434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD) { 8995f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = LType->getElementType(); 90021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 9015f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = LType; 9025f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9035f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9045f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9055f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9065f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene 907e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Lex.getCode() != tgtok::r_paren) { 908e6c27de069225e1122c78385ad22a2ff656db8e6David Greene TokError("expected ')' in unary operator"); 909e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 910e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 911e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the ')' 912dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass); 913e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 914d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 915d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XConcat: 916d23a41c153712b929bd84f5e713bda5db5d6e66dHal Finkel case tgtok::XADD: 91721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XSRA: 918d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XSRL: 919d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XSHL: 9206786d5e18387465d6106eaef0b7923526ef0bc10David Greene case tgtok::XEq: 921c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')' 9228d978a75b477c0e4861d937096518eebba639e72Chris Lattner tgtok::TokKind OpTok = Lex.getCode(); 9238d978a75b477c0e4861d937096518eebba639e72Chris Lattner SMLoc OpLoc = Lex.getLoc(); 9248d978a75b477c0e4861d937096518eebba639e72Chris Lattner Lex.Lex(); // eat the operation 9258d978a75b477c0e4861d937096518eebba639e72Chris Lattner 926d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene BinOpInit::BinaryOp Code; 927d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene RecTy *Type = 0; 928d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 9298d978a75b477c0e4861d937096518eebba639e72Chris Lattner switch (OpTok) { 930858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 93177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break; 932d23a41c153712b929bd84f5e713bda5db5d6e66dHal Finkel case tgtok::XADD: Code = BinOpInit::ADD; Type = IntRecTy::get(); break; 93377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break; 93477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break; 93577f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break; 93677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break; 93721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XStrConcat: 938d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Code = BinOpInit::STRCONCAT; 93977f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = StringRecTy::get(); 940d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene break; 941d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 942c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 943d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::l_paren) { 944d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected '(' after binary operator"); 945d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 946d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 947d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the '(' 948d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 94905bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init*, 2> InitList; 950c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 9518d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.push_back(ParseValue(CurRec)); 9528d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.back() == 0) return 0; 953d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 9548d978a75b477c0e4861d937096518eebba639e72Chris Lattner while (Lex.getCode() == tgtok::comma) { 9558d978a75b477c0e4861d937096518eebba639e72Chris Lattner Lex.Lex(); // eat the ',' 95621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 9578d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.push_back(ParseValue(CurRec)); 9588d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.back() == 0) return 0; 9598d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 960d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 961d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::r_paren) { 9628d978a75b477c0e4861d937096518eebba639e72Chris Lattner TokError("expected ')' in operator"); 963d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 964d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 965d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the ')' 9668d978a75b477c0e4861d937096518eebba639e72Chris Lattner 9678d978a75b477c0e4861d937096518eebba639e72Chris Lattner // We allow multiple operands to associative operators like !strconcat as 9688d978a75b477c0e4861d937096518eebba639e72Chris Lattner // shorthand for nesting them. 9698d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (Code == BinOpInit::STRCONCAT) { 9708d978a75b477c0e4861d937096518eebba639e72Chris Lattner while (InitList.size() > 2) { 97105bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *RHS = InitList.pop_back_val(); 972dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type)) 973dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene ->Fold(CurRec, CurMultiClass); 9748d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.back() = RHS; 9758d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 9768d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 977c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 9788d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.size() == 2) 979dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) 9808d978a75b477c0e4861d937096518eebba639e72Chris Lattner ->Fold(CurRec, CurMultiClass); 981c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 9828d978a75b477c0e4861d937096518eebba639e72Chris Lattner Error(OpLoc, "expected two operands to operator"); 9838d978a75b477c0e4861d937096518eebba639e72Chris Lattner return 0; 984d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 985d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 9869bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 987beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 9884afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 9894afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TernOpInit::TernaryOp Code; 9904afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene RecTy *Type = 0; 9914afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 9924afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene tgtok::TokKind LexCode = Lex.getCode(); 9934afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the operation 9944afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene switch (LexCode) { 995858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 9969bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 9979bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene Code = TernOpInit::IF; 9989bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene break; 999beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 1000beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene Code = TernOpInit::FOREACH; 1001beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene break; 10024afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: 10034afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Code = TernOpInit::SUBST; 10044afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene break; 10054afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10064afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::l_paren) { 10074afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected '(' after ternary operator"); 10084afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10094afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10104afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the '(' 10114afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 101205bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *LHS = ParseValue(CurRec); 10134afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (LHS == 0) return 0; 10144afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10154afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::comma) { 10164afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ',' in ternary operator"); 10174afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10184afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10194afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ',' 102021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 102105bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *MHS = ParseValue(CurRec); 10224afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (MHS == 0) return 0; 10234afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10244afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::comma) { 10254afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ',' in ternary operator"); 10264afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10274afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10284afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ',' 102921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 103005bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *RHS = ParseValue(CurRec); 10314afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (RHS == 0) return 0; 10324afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10334afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::r_paren) { 10344afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ')' in binary operator"); 10354afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10364afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10374afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ')' 10384afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10394afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene switch (LexCode) { 1040858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 10419bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: { 1042548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RecTy *MHSTy = 0; 1043548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RecTy *RHSTy = 0; 1044548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 10456cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva if (TypedInit *MHSt = dyn_cast<TypedInit>(MHS)) 1046548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling MHSTy = MHSt->getType(); 10476cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva if (BitsInit *MHSbits = dyn_cast<BitsInit>(MHS)) 1048307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao MHSTy = BitsRecTy::get(MHSbits->getNumBits()); 10493f7b7f8ce0b050fc6a0100839d9c5a84198b2aedSean Silva if (isa<BitInit>(MHS)) 1050307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao MHSTy = BitRecTy::get(); 1051307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao 10526cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva if (TypedInit *RHSt = dyn_cast<TypedInit>(RHS)) 1053548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RHSTy = RHSt->getType(); 10546cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva if (BitsInit *RHSbits = dyn_cast<BitsInit>(RHS)) 1055307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao RHSTy = BitsRecTy::get(RHSbits->getNumBits()); 10563f7b7f8ce0b050fc6a0100839d9c5a84198b2aedSean Silva if (isa<BitInit>(RHS)) 1057307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao RHSTy = BitRecTy::get(); 1058307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao 1059307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao // For UnsetInit, it's typed from the other hand. 10603f7b7f8ce0b050fc6a0100839d9c5a84198b2aedSean Silva if (isa<UnsetInit>(MHS)) 1061307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao MHSTy = RHSTy; 10623f7b7f8ce0b050fc6a0100839d9c5a84198b2aedSean Silva if (isa<UnsetInit>(RHS)) 1063307525cd24c3b9c081ddb3c34a3418f2875cd556Michael Liao RHSTy = MHSTy; 1064548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1065548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (!MHSTy || !RHSTy) { 10669bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene TokError("could not get type for !if"); 10679bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene return 0; 10689bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 1069548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1070548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSTy->typeIsConvertibleTo(RHSTy)) { 1071548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling Type = RHSTy; 1072548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else if (RHSTy->typeIsConvertibleTo(MHSTy)) { 1073548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling Type = MHSTy; 107421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 10759bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene TokError("inconsistent types for !if"); 10769bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene return 0; 10779bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 10789bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene break; 10799bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 1080beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: { 10816cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *MHSt = dyn_cast<TypedInit>(MHS); 1082beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene if (MHSt == 0) { 1083beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene TokError("could not get type for !foreach"); 1084beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene return 0; 1085beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene } 1086beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene Type = MHSt->getType(); 1087beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene break; 1088beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene } 10894afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { 10906cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *RHSt = dyn_cast<TypedInit>(RHS); 10914afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (RHSt == 0) { 10924afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("could not get type for !subst"); 10934afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10944afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10954afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Type = RHSt->getType(); 10964afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene break; 10974afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10984afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 1099dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec, 110021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson CurMultiClass); 11014afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 1102d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1103d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene} 1104d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1105d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// ParseOperatorType - Parse a type for an operator. This returns 1106d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// null on error. 1107d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 1108d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// OperatorType ::= '<' Type '>' 1109d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 1110a9ad04191cb56c42944b17980b8b2bb2afe11ab2Dan GohmanRecTy *TGParser::ParseOperatorType() { 1111d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene RecTy *Type = 0; 1112d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1113d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::less) { 1114d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1115d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1116d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1117d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the < 1118d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1119d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Type = ParseType(); 1120d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1121d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Type == 0) { 1122d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1123d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1124d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1125d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1126d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::greater) { 1127d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1128d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1129d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1130d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the > 1131d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1132d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return Type; 1133d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene} 1134d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1135d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1136f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseSimpleValue - Parse a tblgen value. This returns null on error. 1137f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1138f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= IDValue 1139f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= INTVAL 1140d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner/// SimpleValue ::= STRVAL+ 1141f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= CODEFRAGMENT 1142f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '?' 1143f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '{' ValueList '}' 1144f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= ID '<' ValueListNE '>' 1145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '[' ValueList ']' 1146f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '(' IDValue DagArgList ')' 1147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' 1148d23a41c153712b929bd84f5e713bda5db5d6e66dHal Finkel/// SimpleValue ::= ADDTOK '(' Value ',' Value ')' 1149f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SHLTOK '(' Value ',' Value ')' 1150f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SRATOK '(' Value ',' Value ')' 1151f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SRLTOK '(' Value ',' Value ')' 1152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' 1153f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1154f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David GreeneInit *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, 1155f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene IDParseMode Mode) { 115605bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *R = 0; 1157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 1158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: TokError("Unknown token when parsing a value"); break; 1159d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::paste: 1160d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // This is a leading paste operation. This is deprecated but 1161d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // still exists in some .td files. Ignore it. 1162d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Lex.Lex(); // Skip '#'. 1163d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene return ParseSimpleValue(CurRec, ItemType, Mode); 1164dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; 1165d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner case tgtok::StrVal: { 1166d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner std::string Val = Lex.getCurStrVal(); 1167d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Lex.Lex(); 116821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1169da4231f134989af7dc6bd3408821ba573def27b2Jim Grosbach // Handle multiple consecutive concatenated strings. 1170d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner while (Lex.getCode() == tgtok::StrVal) { 1171d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Val += Lex.getCurStrVal(); 1172d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Lex.Lex(); 1173d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner } 117421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1175dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = StringInit::get(Val); 1176d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner break; 1177d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner } 1178f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::CodeFragment: 11798dd6f0c8353f80de6526810899f271d539f6929cJakob Stoklund Olesen R = StringInit::get(Lex.getCurStrVal()); 1180578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner Lex.Lex(); 1181578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner break; 1182578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner case tgtok::question: 1183dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = UnsetInit::get(); 1184578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner Lex.Lex(); 1185578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner break; 1186f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Id: { 11871e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc NameLoc = Lex.getLoc(); 1188f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 1189f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) // consume the Id. 1190f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue 119121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1192f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Value ::= ID '<' ValueListNE '>' 1193f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() == tgtok::greater) { 1194f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected non-empty value list"); 1195f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1196f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1197e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1198f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // This is a CLASS<initvalslist> expression. This is supposed to synthesize 1199f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // a new anonymous definition, deriving from CLASS<initvalslist> with no 1200f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // body. 1201f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Class = Records.getClass(Name); 1202f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!Class) { 1203f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(NameLoc, "Expected a class name, got '" + Name + "'"); 1204f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1205f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1206e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 120705bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> ValueList = ParseValueList(CurRec, Class); 1208e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ValueList.empty()) return 0; 120921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1210e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() != tgtok::greater) { 1211e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected '>' at end of value list"); 1212e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1213e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1214e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '>' 1215b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMLoc EndLoc = Lex.getLoc(); 121621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1217f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Create the new record, set it as CurRec temporarily. 1218f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner static unsigned AnonCounter = 0; 12199c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++), 12209c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner NameLoc, 1221d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose Records, 1222d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose /*IsAnonymous=*/true); 1223f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference SCRef; 1224b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SCRef.RefRange = SMRange(NameLoc, EndLoc); 1225f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.Rec = Class; 1226f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.TemplateArgs = ValueList; 1227f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add info about the subclass to NewRec. 1228f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddSubClass(NewRec, SCRef)) 1229f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1230f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner NewRec->resolveReferences(); 1231f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addDef(NewRec); 123221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1233f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // The result of the expression is a reference to the new record. 123477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return DefInit::get(NewRec); 123521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1236f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_brace: { // Value ::= '{' ValueList '}' 12371e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc BraceLoc = Lex.getLoc(); 1238f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 123905bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Vals; 124021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1241f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1242f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Vals = ParseValueList(CurRec); 1243f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Vals.empty()) return 0; 1244f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1245f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1246f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit list value"); 1247f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1248f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1249f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '}' 125021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 125105bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init *, 16> NewBits(Vals.size()); 1252ca7fd3de360b266783438666054dba198ff77ba2David Greene 1253f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 125405bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get()); 1255f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Bit == 0) { 12565d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ 12575d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner ") is not convertable to a bit"); 1258f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1259f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1260ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[Vals.size()-i-1] = Bit; 1261f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1262dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return BitsInit::get(NewBits); 1263f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1264f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_square: { // Value ::= '[' ValueList ']' 1265f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '[' 126605bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Vals; 126721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1268e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *DeducedEltTy = 0; 1269e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ListRecTy *GivenListTy = 0; 127021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1271e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ItemType != 0) { 1272736ceace11249da645ec4ed91b8714832193ead4Sean Silva ListRecTy *ListType = dyn_cast<ListRecTy>(ItemType); 1273e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ListType == 0) { 127447cfec02842f885b46ea0d3c812793e660691640Reid Kleckner std::string s; 127547cfec02842f885b46ea0d3c812793e660691640Reid Kleckner raw_string_ostream ss(s); 127647cfec02842f885b46ea0d3c812793e660691640Reid Kleckner ss << "Type mismatch for list, expected list type, got " 127747cfec02842f885b46ea0d3c812793e660691640Reid Kleckner << ItemType->getAsString(); 127847cfec02842f885b46ea0d3c812793e660691640Reid Kleckner TokError(ss.str()); 12796a44adade20bdea80c76b68833ec5eff5baaba98Jim Grosbach return 0; 1280e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1281e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene GivenListTy = ListType; 128221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1283e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1284f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 128521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Vals = ParseValueList(CurRec, 0, 128621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson GivenListTy ? GivenListTy->getElementType() : 0); 1287f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Vals.empty()) return 0; 1288f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1289f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 1290f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ']' at end of list value"); 1291f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1292f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1293f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ']' 1294e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1295e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *GivenEltTy = 0; 1296e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() == tgtok::less) { 1297e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Optional list element type 1298e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '<' 1299e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1300e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene GivenEltTy = ParseType(); 1301e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenEltTy == 0) { 1302e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Couldn't parse element type 1303e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1304e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1305e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1306e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() != tgtok::greater) { 1307e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected '>' at end of list element type"); 1308e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1309e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1310e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '>' 1311e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1312e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1313e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Check elements 1314e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *EltTy = 0; 131505bce0beee87512e52428d4b80f5a8e79a949576David Greene for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end(); 1316e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene i != ie; 1317e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++i) { 13186cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *TArg = dyn_cast<TypedInit>(*i); 1319e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (TArg == 0) { 1320e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Untyped list element"); 1321e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1322e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1323e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy != 0) { 1324e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = resolveTypes(EltTy, TArg->getType()); 1325e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy == 0) { 1326e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Incompatible types in list elements"); 1327e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1328e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 132921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1330e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = TArg->getType(); 1331e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1332e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1333e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1334e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenEltTy != 0) { 1335e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy != 0) { 1336e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Verify consistency 1337e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { 1338e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Incompatible types in list elements"); 1339e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1340e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1341e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1342e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = GivenEltTy; 1343e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1344e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1345e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy == 0) { 1346e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ItemType == 0) { 1347e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("No type for list"); 1348e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1349e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1350e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene DeducedEltTy = GivenListTy->getElementType(); 135121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1352e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Make sure the deduced type is compatible with the given type 1353e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenListTy) { 1354e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { 1355e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Element type mismatch for list"); 1356e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1357e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1358e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1359e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene DeducedEltTy = EltTy; 1360e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 136121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1362dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return ListInit::get(Vals, DeducedEltTy); 1363f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1364f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' 1365f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '(' 1366c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) { 13673dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner TokError("expected identifier in dag init"); 13683dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner return 0; 13693dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner } 137021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 137105bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Operator = ParseValue(CurRec); 1372578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner if (Operator == 0) return 0; 1373c7cafcd815519b06318629b424abe746437e1389David Greene 13747cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman // If the operator name is present, parse it. 13757cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman std::string OperatorName; 13767cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman if (Lex.getCode() == tgtok::colon) { 13777cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman if (Lex.Lex() != tgtok::VarName) { // eat the ':' 13787cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman TokError("expected variable name in dag operator"); 13797cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman return 0; 13807cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman } 13817cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman OperatorName = Lex.getCurStrVal(); 13827cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman Lex.Lex(); // eat the VarName. 13837cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman } 138421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 138505bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<std::pair<llvm::Init*, std::string> > DagArgs; 1386f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_paren) { 1387f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner DagArgs = ParseDagArgList(CurRec); 1388f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (DagArgs.empty()) return 0; 1389f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 139021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1391f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_paren) { 1392f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ')' in dag init"); 1393f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1394f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1395f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ')' 139621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1397dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return DagInit::get(Operator, OperatorName, DagArgs); 1398f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 139921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 14001434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 14011434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 14021434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 1403e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: // Value ::= !unop '(' Value ')' 1404f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XConcat: 1405d23a41c153712b929bd84f5e713bda5db5d6e66dHal Finkel case tgtok::XADD: 140621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XSRA: 1407f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XSRL: 1408f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XSHL: 14096786d5e18387465d6106eaef0b7923526ef0bc10David Greene case tgtok::XEq: 1410c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')' 14119bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 1412beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 14134afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 1414d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return ParseOperation(CurRec); 1415f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1416f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 141721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1418f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return R; 1419f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1420f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1421f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseValue - Parse a tblgen value. This returns null on error. 1422f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1423f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Value ::= SimpleValue ValueSuffix* 1424f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '{' BitList '}' 1425f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '[' BitList ']' 1426f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '.' ID 1427f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1428f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David GreeneInit *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) { 1429f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene Init *Result = ParseSimpleValue(CurRec, ItemType, Mode); 1430f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) return 0; 143121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1432f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the suffixes now if present. 1433f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1434f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 1435f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: return Result; 1436f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_brace: { 1437cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Mode == ParseNameMode || Mode == ParseForeachMode) 14388592b2b2a3dde4e5c8f00e855497f760ae94272fDavid Greene // This is the beginning of the object body. 14398592b2b2a3dde4e5c8f00e855497f760ae94272fDavid Greene return Result; 14408592b2b2a3dde4e5c8f00e855497f760ae94272fDavid Greene 14411e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc CurlyLoc = Lex.getLoc(); 1442f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 1443f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Ranges = ParseRangeList(); 1444f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return 0; 144521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1446f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Reverse the bitlist. 1447f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(Ranges.begin(), Ranges.end()); 1448f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result = Result->convertInitializerBitRange(Ranges); 1449f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) { 1450f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(CurlyLoc, "Invalid bit range for value"); 1451f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1452f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 145321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1454f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '}'. 1455f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1456f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit range list"); 1457f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1458f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1459f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1460f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1461f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1462f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_square: { 14631e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc SquareLoc = Lex.getLoc(); 1464f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '[' 1465f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Ranges = ParseRangeList(); 1466f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return 0; 146721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1468f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result = Result->convertInitListSlice(Ranges); 1469f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) { 1470f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(SquareLoc, "Invalid range for list slice"); 1471f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1472f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 147321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1474f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the ']'. 1475f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 1476f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ']' at end of list slice"); 1477f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1478f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1479f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1480f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1481f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1482f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::period: 1483f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::Id) { // eat the . 1484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected field identifier after '.'"); 1485f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1487f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!Result->getFieldType(Lex.getCurStrVal())) { 1488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + 14895d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner Result->getAsString() + "'"); 1490f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1492dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene Result = FieldInit::get(Result, Lex.getCurStrVal()); 1493f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat field name 1494f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1495d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1496d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::paste: 1497d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene SMLoc PasteLoc = Lex.getLoc(); 1498d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1499d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // Create a !strconcat() operation, first casting each operand to 1500d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // a string if necessary. 1501d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 15026cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva TypedInit *LHS = dyn_cast<TypedInit>(Result); 1503d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (!LHS) { 1504d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Error(PasteLoc, "LHS of paste is not typed!"); 1505d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene return 0; 1506d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1507d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1508d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (LHS->getType() != StringRecTy::get()) { 1509d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get()); 1510d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1511d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1512d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene TypedInit *RHS = 0; 1513d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1514d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Lex.Lex(); // Eat the '#'. 1515d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene switch (Lex.getCode()) { 1516d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::colon: 1517d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::semi: 1518d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::l_brace: 1519d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // These are all of the tokens that can begin an object body. 1520d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // Some of these can also begin values but we disallow those cases 1521d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // because they are unlikely to be useful. 1522d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1523d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // Trailing paste, concat with an empty string. 1524d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene RHS = StringInit::get(""); 1525d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene break; 1526d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1527d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene default: 1528d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode); 15296cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva RHS = dyn_cast<TypedInit>(RHSResult); 1530d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (!RHS) { 1531d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Error(PasteLoc, "RHS of paste is not typed!"); 1532d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene return 0; 1533d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1534d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1535d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (RHS->getType() != StringRecTy::get()) { 1536d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get()); 1537d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1538d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1539d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene break; 1540d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1541d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1542d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS, 1543d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene StringRecTy::get())->Fold(CurRec, CurMultiClass); 1544d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene break; 1545f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1546f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1547f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1548f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1549f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList - Parse the argument list for a dag literal expression. 1550f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 15514717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen/// DagArg ::= Value (':' VARNAME)? 15524717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen/// DagArg ::= VARNAME 15534717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen/// DagArgList ::= DagArg 15544717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen/// DagArgList ::= DagArgList ',' DagArg 155505bce0beee87512e52428d4b80f5a8e79a949576David Greenestd::vector<std::pair<llvm::Init*, std::string> > 1556f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerTGParser::ParseDagArgList(Record *CurRec) { 155705bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<std::pair<llvm::Init*, std::string> > Result; 155821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1559f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 15604717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen // DagArg ::= VARNAME 15614717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen if (Lex.getCode() == tgtok::VarName) { 15624717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen // A missing value is treated like '?'. 15634717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen Result.push_back(std::make_pair(UnsetInit::get(), Lex.getCurStrVal())); 15644717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen Lex.Lex(); 15654717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen } else { 15664717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen // DagArg ::= Value (':' VARNAME)? 15674717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen Init *Val = ParseValue(CurRec); 15684717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen if (Val == 0) 156905bce0beee87512e52428d4b80f5a8e79a949576David Greene return std::vector<std::pair<llvm::Init*, std::string> >(); 157021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 15714717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen // If the variable name is present, add it. 15724717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen std::string VarName; 15734717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen if (Lex.getCode() == tgtok::colon) { 15744717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen if (Lex.Lex() != tgtok::VarName) { // eat the ':' 15754717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen TokError("expected variable name in dag literal"); 15764717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen return std::vector<std::pair<llvm::Init*, std::string> >(); 15774717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen } 15784717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen VarName = Lex.getCurStrVal(); 15794717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen Lex.Lex(); // eat the VarName. 15804717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen } 158121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 15824717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen Result.push_back(std::make_pair(Val, VarName)); 15834717fd4c0faf454ff37f133709320d0cfec0da89Jakob Stoklund Olesen } 1584f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) break; 158521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the ',' 1586f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 158721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1588f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 1589f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1590f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1591f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseValueList - Parse a comma separated list of values, returning them as a 1593f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// vector. Note that this always expects to be able to parse at least one 1594f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// value. It returns an empty list if this is not possible. 1595f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1596f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueList ::= Value (',' Value) 1597f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 159805bce0beee87512e52428d4b80f5a8e79a949576David Greenestd::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, 1599d568b3f55294917d1cc701da14a8a7daeb6563e6Eric Christopher RecTy *EltTy) { 160005bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Result; 1601e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *ItemType = EltTy; 160267acdf2977595a1d9755bf919d678f80ee65364fDavid Greene unsigned int ArgN = 0; 1603e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ArgsRec != 0 && EltTy == 0) { 1604e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs(); 1605b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach if (!TArgs.size()) { 1606b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach TokError("template argument provided to non-template class"); 1607b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach return std::vector<Init*>(); 1608b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach } 1609e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1610d9746fe58ace4a8625983da1ecf68c0564883616David Greene if (!RV) { 1611d9746fe58ace4a8625983da1ecf68c0564883616David Greene errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN] 1612d9746fe58ace4a8625983da1ecf68c0564883616David Greene << ")\n"; 1613d9746fe58ace4a8625983da1ecf68c0564883616David Greene } 1614e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene assert(RV && "Template argument record not found??"); 1615e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ItemType = RV->getType(); 1616e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++ArgN; 1617e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1618e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.push_back(ParseValue(CurRec, ItemType)); 161905bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Result.back() == 0) return std::vector<Init*>(); 162021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1621f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 1622f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the comma 162321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1624e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ArgsRec != 0 && EltTy == 0) { 1625e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs(); 162667acdf2977595a1d9755bf919d678f80ee65364fDavid Greene if (ArgN >= TArgs.size()) { 162767acdf2977595a1d9755bf919d678f80ee65364fDavid Greene TokError("too many template arguments"); 162805bce0beee87512e52428d4b80f5a8e79a949576David Greene return std::vector<Init*>(); 162921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1630e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1631e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene assert(RV && "Template argument record not found??"); 1632e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ItemType = RV->getType(); 1633e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++ArgN; 1634e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1635e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.push_back(ParseValue(CurRec, ItemType)); 163605bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Result.back() == 0) return std::vector<Init*>(); 1637f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 163821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1639f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 1640f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1641f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1642f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1643f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDeclaration - Read a declaration, returning the name of field ID, or an 1644f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// empty string on error. This can happen in a number of different context's, 1645f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// including within a def or in the template args for a def (which which case 1646f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// CurRec will be non-null) and within the template args for a multiclass (in 1647f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// which case CurRec will be null, but CurMultiClass will be set). This can 1648f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// also happen within a def that is within a multiclass, which will set both 1649f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// CurRec and CurMultiClass. 1650f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1651f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Declaration ::= FIELD? Type ID ('=' Value)? 1652f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1653e22b321d2276b634519165b101b02d92c2fcf5c7David GreeneInit *TGParser::ParseDeclaration(Record *CurRec, 1654f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool ParsingTemplateArgs) { 1655f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the field prefix if present. 1656f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool HasField = Lex.getCode() == tgtok::Field; 1657f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (HasField) Lex.Lex(); 165821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1659f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecTy *Type = ParseType(); 1660e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (Type == 0) return 0; 166121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1662f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 1663f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Expected identifier in declaration"); 1664e22b321d2276b634519165b101b02d92c2fcf5c7David Greene return 0; 1665f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 166621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 16671e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc IdLoc = Lex.getLoc(); 1668e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *DeclName = StringInit::get(Lex.getCurStrVal()); 1669f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 167021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1671f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParsingTemplateArgs) { 1672f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 1673e22b321d2276b634519165b101b02d92c2fcf5c7David Greene DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":"); 1674f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 1675f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(CurMultiClass); 1676f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1677f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass) 1678e22b321d2276b634519165b101b02d92c2fcf5c7David Greene DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName, 1679e22b321d2276b634519165b101b02d92c2fcf5c7David Greene "::"); 1680f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 168121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1682f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add the value. 1683f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) 1684e22b321d2276b634519165b101b02d92c2fcf5c7David Greene return 0; 168521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1686f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If a value is present, parse it. 1687f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::equal) { 1688f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 16891e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc ValLoc = Lex.getLoc(); 169005bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec, Type); 1691f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0 || 1692f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) 1693e22b321d2276b634519165b101b02d92c2fcf5c7David Greene return 0; 1694f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 169521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1696f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return DeclName; 1697f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1698f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1699cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ParseForeachDeclaration - Read a foreach declaration, returning 1700cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// the name of the declared object or a NULL Init on error. Return 1701cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// the name of the parsed initializer list through ForeachListName. 1702cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 1703fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen/// ForeachDeclaration ::= ID '=' '[' ValueList ']' 1704fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen/// ForeachDeclaration ::= ID '=' '{' RangeList '}' 1705fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen/// ForeachDeclaration ::= ID '=' RangePiece 1706cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 17078e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund OlesenVarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) { 1708cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::Id) { 1709cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("Expected identifier in foreach declaration"); 1710cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return 0; 1711cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1712cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1713cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Init *DeclName = StringInit::get(Lex.getCurStrVal()); 1714cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); 1715cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1716cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // If a value is present, parse it. 1717cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::equal) { 1718cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("Expected '=' in foreach declaration"); 1719cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return 0; 1720cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1721cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the '=' 1722cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1723fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen RecTy *IterType = 0; 1724fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen std::vector<unsigned> Ranges; 1725cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1726fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen switch (Lex.getCode()) { 1727fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen default: TokError("Unknown token when expecting a range list"); return 0; 1728fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen case tgtok::l_square: { // '[' ValueList ']' 1729fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen Init *List = ParseSimpleValue(0, 0, ParseForeachMode); 17306cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva ForeachListValue = dyn_cast<ListInit>(List); 1731fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen if (ForeachListValue == 0) { 1732fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen TokError("Expected a Value list"); 1733fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen return 0; 1734fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen } 1735fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen RecTy *ValueType = ForeachListValue->getType(); 1736736ceace11249da645ec4ed91b8714832193ead4Sean Silva ListRecTy *ListType = dyn_cast<ListRecTy>(ValueType); 1737fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen if (ListType == 0) { 1738fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen TokError("Value list is not of list type"); 1739fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen return 0; 1740fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen } 1741fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen IterType = ListType->getElementType(); 1742fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen break; 1743cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1744cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1745fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen case tgtok::IntVal: { // RangePiece. 1746fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen if (ParseRangePiece(Ranges)) 1747fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen return 0; 1748fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen break; 1749fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen } 1750fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen 1751fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen case tgtok::l_brace: { // '{' RangeList '}' 1752fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen Lex.Lex(); // eat the '{' 1753fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen Ranges = ParseRangeList(); 1754fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen if (Lex.getCode() != tgtok::r_brace) { 1755fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen TokError("expected '}' at end of bit range list"); 1756fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen return 0; 1757fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen } 1758fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen Lex.Lex(); 1759fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen break; 1760fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen } 1761cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1762cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1763fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen if (!Ranges.empty()) { 1764fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen assert(!IterType && "Type already initialized?"); 1765fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen IterType = IntRecTy::get(); 1766fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen std::vector<Init*> Values; 1767fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen for (unsigned i = 0, e = Ranges.size(); i != e; ++i) 1768fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen Values.push_back(IntInit::get(Ranges[i])); 1769fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen ForeachListValue = ListInit::get(Values, IterType); 1770fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen } 1771fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen 1772fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen if (!IterType) 1773fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen return 0; 1774cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1775fae8b1de47c004fefaa6c2683ae193d465b9d93fJakob Stoklund Olesen return VarInit::get(DeclName, IterType); 1776cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 1777cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1778f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseTemplateArgList - Read a template argument list, which is a non-empty 1779f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// sequence of template-declarations in <>'s. If CurRec is non-null, these are 1780f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// template args for a def, which may or may not be in a multiclass. If null, 1781f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// these are the template args for a multiclass. 1782f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1783f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' 178421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson/// 1785f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseTemplateArgList(Record *CurRec) { 1786f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); 1787f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '<' 178821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1789f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; 179021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1791f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the first declaration. 1792e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1793e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (TemplArg == 0) 1794f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 179521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1796f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TheRecToAddTo->addTemplateArg(TemplArg); 179721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1798f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 1799f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ',' 180021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1801f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the following declarations. 1802f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1803e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (TemplArg == 0) 1804f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1805f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TheRecToAddTo->addTemplateArg(TemplArg); 1806f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 180721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1808f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) 1809f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected '>' at end of template argument list"); 1810f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '>'. 1811f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1812f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1813f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1814f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1815f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseBodyItem - Parse a single item at within the body of a def or class. 1816f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1817f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyItem ::= Declaration ';' 1818f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyItem ::= LET ID OptionalBitList '=' Value ';' 1819f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseBodyItem(Record *CurRec) { 1820f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Let) { 1821e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (ParseDeclaration(CurRec, false) == 0) 1822f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 182321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1824f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::semi) 1825f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ';' after declaration"); 1826f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1827f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1828f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1829f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1830f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // LET ID OptionalRangeList '=' Value ';' 1831f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::Id) 1832f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected field identifier after let"); 183321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 18341e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc IdLoc = Lex.getLoc(); 1835f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string FieldName = Lex.getCurStrVal(); 1836f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the field name. 183721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1838f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> BitList; 183921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseOptionalBitList(BitList)) 1840f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1841f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(BitList.begin(), BitList.end()); 184221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1843f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::equal) 1844f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected '=' in let expression"); 1845f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '='. 184621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1847e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecordVal *Field = CurRec->getValue(FieldName); 1848e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Field == 0) 1849e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return TokError("Value '" + FieldName + "' unknown!"); 1850e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1851e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *Type = Field->getType(); 185221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 185305bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec, Type); 1854f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0) return true; 185521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1856f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::semi) 1857f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ';' after let expression"); 1858f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 185921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1860f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return SetValue(CurRec, IdLoc, FieldName, BitList, Val); 1861f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1862f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1863f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseBody - Read the body of a class or def. Return true on error, false on 1864f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// success. 1865f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1866f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Body ::= ';' 1867f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Body ::= '{' BodyList '}' 1868f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyList BodyItem* 1869f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1870f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseBody(Record *CurRec) { 1871f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is a null definition, just eat the semi and return. 1872f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::semi) { 1873f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1874f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1875f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 187621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1877f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) 1878f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Expected ';' or '{' to start body"); 1879f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '{'. 1880f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 188121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1882f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() != tgtok::r_brace) 1883f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseBodyItem(CurRec)) 1884f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1885f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1886f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '}'. 1887f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1888f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1889f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1890f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 18919cceede447118852df76e340252387d1a2cce37dSean Silva/// \brief Apply the current let bindings to \a CurRec. 18929cceede447118852df76e340252387d1a2cce37dSean Silva/// \returns true on error, false otherwise. 18939cceede447118852df76e340252387d1a2cce37dSean Silvabool TGParser::ApplyLetStack(Record *CurRec) { 18949cceede447118852df76e340252387d1a2cce37dSean Silva for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 18959cceede447118852df76e340252387d1a2cce37dSean Silva for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 18969cceede447118852df76e340252387d1a2cce37dSean Silva if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 18979cceede447118852df76e340252387d1a2cce37dSean Silva LetStack[i][j].Bits, LetStack[i][j].Value)) 18989cceede447118852df76e340252387d1a2cce37dSean Silva return true; 18999cceede447118852df76e340252387d1a2cce37dSean Silva return false; 19009cceede447118852df76e340252387d1a2cce37dSean Silva} 19019cceede447118852df76e340252387d1a2cce37dSean Silva 1902f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectBody - Parse the body of a def or class. This consists of an 1903f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// optional ClassList followed by a Body. CurRec is the current def or class 1904f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// that is being parsed. 1905f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1906f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectBody ::= BaseClassList Body 1907f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassList ::= /*empty*/ 1908f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassList ::= ':' BaseClassListNE 1909f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassListNE ::= SubClassRef (',' SubClassRef)* 1910f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1911f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseObjectBody(Record *CurRec) { 1912f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there is a baseclass list, read it. 1913f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::colon) { 1914f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 191521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1916f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read all of the subclasses. 1917f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference SubClass = ParseSubClassReference(CurRec, false); 1918f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1919f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Check for error. 1920f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SubClass.Rec == 0) return true; 192121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1922f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add it. 1923f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddSubClass(CurRec, SubClass)) 1924f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 192521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1926f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) break; 1927f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat ','. 1928f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClass = ParseSubClassReference(CurRec, false); 1929f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1930f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1931f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 19329cceede447118852df76e340252387d1a2cce37dSean Silva if (ApplyLetStack(CurRec)) 19339cceede447118852df76e340252387d1a2cce37dSean Silva return true; 193421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1935f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseBody(CurRec); 1936f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1937f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1938f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDef - Parse and return a top level or multiclass def, return the record 1939f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// corresponding to it. This returns null on error. 1940f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1941f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// DefInst ::= DEF ObjectName ObjectBody 1942f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1943ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseDef(MultiClass *CurMultiClass) { 19441e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc DefLoc = Lex.getLoc(); 1945f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Def && "Unknown tok"); 194621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // Eat the 'def' token. 1947f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1948f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse ObjectName and make a record for it. 1949d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose Record *CurRec; 1950d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose Init *Name = ParseObjectName(CurMultiClass); 1951d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose if (Name) 1952d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose CurRec = new Record(Name, DefLoc, Records); 1953d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose else 1954d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose CurRec = new Record(GetNewAnonymousName(), DefLoc, Records, 1955d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose /*IsAnonymous=*/true); 195621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 195772cba6cdf640411e2fb6207858a0abd87c4286fcJakob Stoklund Olesen if (!CurMultiClass && Loops.empty()) { 1958f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Top-level def definition. 195921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1960f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Ensure redefinition doesn't happen. 19611d5013992f8ecf5cc670ab8a1a599db4722c2f5dDavid Greene if (Records.getDef(CurRec->getNameInitAsString())) { 19622c49fbb32c5f042ecf64ac415f1a628100951a44David Greene Error(DefLoc, "def '" + CurRec->getNameInitAsString() 19632c49fbb32c5f042ecf64ac415f1a628100951a44David Greene + "' already defined"); 1964ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1965f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1966f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addDef(CurRec); 196772cba6cdf640411e2fb6207858a0abd87c4286fcJakob Stoklund Olesen } else if (CurMultiClass) { 1968f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Otherwise, a def inside a multiclass, add it to the multiclass. 1969f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) 197091919cd8166bb60145efe54dd790b98521b4328aDavid Greene if (CurMultiClass->DefPrototypes[i]->getNameInit() 197191919cd8166bb60145efe54dd790b98521b4328aDavid Greene == CurRec->getNameInit()) { 197291919cd8166bb60145efe54dd790b98521b4328aDavid Greene Error(DefLoc, "def '" + CurRec->getNameInitAsString() + 1973f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "' already defined in this multiclass!"); 1974ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1975f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1976f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurMultiClass->DefPrototypes.push_back(CurRec); 1977f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 197821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1979f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseObjectBody(CurRec)) 1980ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 198121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1982f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. 19830d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // See Record::setName(). This resolve step will see any new name 19840d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // for the def that might have been created when resolving 19850d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // inheritance, values and arguments above. 1986f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->resolveReferences(); 198721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1988f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If ObjectBody has template arguments, it's an error. 1989f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); 1990ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1991ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (CurMultiClass) { 1992ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes // Copy the template arguments for the multiclass into the def. 1993e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = 1994ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes CurMultiClass->Rec.getTemplateArgs(); 1995ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1996ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1997ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); 1998ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes assert(RV && "Template arg doesn't exist?"); 1999ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes CurRec->addValue(*RV); 2000ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 2001ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 2002ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 20038e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (ProcessForeachDefs(CurRec, DefLoc)) { 2004cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Error(DefLoc, 2005cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene "Could not process loops for def" + CurRec->getNameInitAsString()); 2006cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return true; 2007cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 2008cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2009cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return false; 2010cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 2011cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2012cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ParseForeach - Parse a for statement. Return the record corresponding 2013cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// to it. This returns true on error. 2014cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 2015cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}' 2016cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// Foreach ::= FOREACH Declaration IN Object 2017cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 2018cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greenebool TGParser::ParseForeach(MultiClass *CurMultiClass) { 2019cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene assert(Lex.getCode() == tgtok::Foreach && "Unknown tok"); 2020cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the 'for' token. 2021cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2022cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Make a temporary object to record items associated with the for 2023cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // loop. 20248e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen ListInit *ListValue = 0; 20258e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen VarInit *IterName = ParseForeachDeclaration(ListValue); 2026cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (IterName == 0) 2027cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return TokError("expected declaration in for"); 2028cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2029cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::In) 2030cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return TokError("Unknown tok"); 2031cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the in 2032cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2033cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Create a loop object and remember it. 2034cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Loops.push_back(ForeachLoop(IterName, ListValue)); 2035cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2036cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::l_brace) { 2037cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // FOREACH Declaration IN Object 2038cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (ParseObject(CurMultiClass)) 2039cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return true; 2040cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 2041cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene else { 2042cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene SMLoc BraceLoc = Lex.getLoc(); 2043cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Otherwise, this is a group foreach. 2044cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // eat the '{'. 2045cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2046cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Parse the object list. 2047cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (ParseObjectList(CurMultiClass)) 2048cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return true; 2049cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2050cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::r_brace) { 2051cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("expected '}' at end of foreach command"); 2052cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return Error(BraceLoc, "to match this '{'"); 2053cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 2054cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the } 2055cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 2056cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2057cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // We've processed everything in this loop. 2058cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Loops.pop_back(); 2059cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2060ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return false; 2061f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2062f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2063f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseClass - Parse a tblgen class definition. 2064f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2065f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody 2066f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2067f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseClass() { 2068f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); 2069f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 207021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2071f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) 2072f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected class name after 'class' keyword"); 207321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2074f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *CurRec = Records.getClass(Lex.getCurStrVal()); 2075f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 2076f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If the body was previously defined, this is an error. 2077e338565757bfcfe9d762751c976684f66954fb45David Greene if (CurRec->getValues().size() > 1 || // Account for NAME. 2078f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner !CurRec->getSuperClasses().empty() || 2079f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner !CurRec->getTemplateArgs().empty()) 208069a2394b2dd0136581f1485d69669246fc3b62c5David Greene return TokError("Class '" + CurRec->getNameInitAsString() 208169a2394b2dd0136581f1485d69669246fc3b62c5David Greene + "' already defined"); 2082f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 2083f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is the first reference to this class, create and add it. 20849c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records); 2085f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addClass(CurRec); 2086f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2087f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the name. 208821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2089f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there are template args, parse them. 2090f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::less) 2091f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseTemplateArgList(CurRec)) 2092f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2093f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2094f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Finally, parse the object body. 2095f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseObjectBody(CurRec); 2096f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2097f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2098f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseLetList - Parse a non-empty list of assignment expressions into a list 2099f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// of LetRecords. 2100f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2101f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// LetList ::= LetItem (',' LetItem)* 2102f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// LetItem ::= ID OptionalRangeList '=' Value 2103f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2104f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::vector<LetRecord> TGParser::ParseLetList() { 2105f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<LetRecord> Result; 210621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2107f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 2108f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 2109f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected identifier in let definition"); 2110f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 2111f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2112f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 21131e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc NameLoc = Lex.getLoc(); 211421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // Eat the identifier. 2115f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2116f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Check for an optional RangeList. 2117f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Bits; 211821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseOptionalRangeList(Bits)) 2119f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 2120f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(Bits.begin(), Bits.end()); 212121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2122f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::equal) { 2123f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '=' in let expression"); 2124f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 2125f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2126f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '='. 212721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 212805bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(0); 2129f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0) return std::vector<LetRecord>(); 213021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2131f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Now that we have everything, add the record. 2132f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); 213321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2134f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) 2135f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 213621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the comma. 2137f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2138f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2139f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2140f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of 2141ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes/// different related productions. This works inside multiclasses too. 2142f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2143f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LET LetList IN '{' ObjectList '}' 2144f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LET LetList IN Object 2145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2146ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { 2147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Let && "Unexpected token"); 2148f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 214921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2150f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add this entry to the let stack. 2151f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<LetRecord> LetInfo = ParseLetList(); 2152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (LetInfo.empty()) return true; 2153f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack.push_back(LetInfo); 2154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::In) 2156f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected 'in' at end of top-level 'let'"); 2157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 215821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2159f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is a scalar let, just handle it now 2160f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) { 2161f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // LET LetList IN Object 2162ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(CurMultiClass)) 2163f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2164f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { // Object ::= LETCommand '{' ObjectList '}' 21651e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc BraceLoc = Lex.getLoc(); 2166f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Otherwise, this is a group let. 2167f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{'. 216821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2169f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the object list. 2170ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObjectList(CurMultiClass)) 2171f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 217221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2173f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 2174f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of top level let command"); 2175f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(BraceLoc, "to match this '{'"); 2176f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2177f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 2178f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 217921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2180f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Outside this let scope, this let block is not active. 2181f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack.pop_back(); 2182f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2183f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2184f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2185f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseMultiClass - Parse a multiclass definition. 2186f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 218732558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// MultiClassInst ::= MULTICLASS ID TemplateArgList? 21889302dcc91458f9d6c8005934f1180ead4427aabaSean Silva/// ':' BaseMultiClassList '{' MultiClassObject+ '}' 21899302dcc91458f9d6c8005934f1180ead4427aabaSean Silva/// MultiClassObject ::= DefInst 21909302dcc91458f9d6c8005934f1180ead4427aabaSean Silva/// MultiClassObject ::= MultiClassInst 21919302dcc91458f9d6c8005934f1180ead4427aabaSean Silva/// MultiClassObject ::= DefMInst 21929302dcc91458f9d6c8005934f1180ead4427aabaSean Silva/// MultiClassObject ::= LETCommand '{' ObjectList '}' 21939302dcc91458f9d6c8005934f1180ead4427aabaSean Silva/// MultiClassObject ::= LETCommand Object 2194f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2195f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseMultiClass() { 2196f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); 2197f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the multiclass token. 2198f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2199f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) 2200f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected identifier after multiclass for name"); 2201f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 220221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2203f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (MultiClasses.count(Name)) 2204f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("multiclass '" + Name + "' already defined"); 220521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 220667db883487fca3472fdde51e931657e22d4d0495Chris Lattner CurMultiClass = MultiClasses[Name] = new MultiClass(Name, 220767db883487fca3472fdde51e931657e22d4d0495Chris Lattner Lex.getLoc(), Records); 2208f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the identifier. 220921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2210f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there are template args, parse them. 2211f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::less) 2212f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseTemplateArgList(0)) 2213f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2214f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2215d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene bool inherits = false; 2216d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 2217de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // If there are submulticlasses, parse them. 2218de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() == tgtok::colon) { 2219d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene inherits = true; 2220d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 2221de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 222232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 2223de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Read all of the submulticlasses. 222432558650ae09247b31aa89cdee64e8c849771024Bob Wilson SubMultiClassReference SubMultiClass = 222532558650ae09247b31aa89cdee64e8c849771024Bob Wilson ParseSubMultiClassReference(CurMultiClass); 2226de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene while (1) { 2227de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Check for error. 2228de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (SubMultiClass.MC == 0) return true; 222932558650ae09247b31aa89cdee64e8c849771024Bob Wilson 2230de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add it. 2231de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddSubMultiClass(CurMultiClass, SubMultiClass)) 2232de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 223332558650ae09247b31aa89cdee64e8c849771024Bob Wilson 2234de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::comma) break; 2235de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); // eat ','. 2236de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass = ParseSubMultiClassReference(CurMultiClass); 2237de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 2238de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 2239de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 2240d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (Lex.getCode() != tgtok::l_brace) { 2241d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (!inherits) 2242d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return TokError("expected '{' in multiclass definition"); 224321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson else if (Lex.getCode() != tgtok::semi) 224421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson return TokError("expected ';' in multiclass definition"); 2245d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene else 224621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the ';'. 224721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 2248d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (Lex.Lex() == tgtok::r_brace) // eat the '{'. 2249d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return TokError("multiclass must contain at least one def"); 225021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2251270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes while (Lex.getCode() != tgtok::r_brace) { 2252ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes switch (Lex.getCode()) { 2253ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes default: 2254a1b1b79be15c4b79a4282f148085ebad1cf877caDavid Greene return TokError("expected 'let', 'def' or 'defm' in multiclass body"); 2255ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Let: 2256ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Def: 2257ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Defm: 2258cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene case tgtok::Foreach: 2259ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(CurMultiClass)) 2260ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 2261ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes break; 2262ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 2263270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes } 2264d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene Lex.Lex(); // eat the '}'. 2265d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene } 226621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2267f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurMultiClass = 0; 2268f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2269f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2270f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2271e499a2df4492dab21a50d74f3f687b989f910a2fDavid GreeneRecord *TGParser:: 2272e499a2df4492dab21a50d74f3f687b989f910a2fDavid GreeneInstantiateMulticlassDef(MultiClass &MC, 2273e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *DefProto, 22747be867e48300861b7b1bf614eb204463533d6724David Greene Init *DefmPrefix, 2275b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMRange DefmPrefixRange) { 22767be867e48300861b7b1bf614eb204463533d6724David Greene // We need to preserve DefProto so it can be reused for later 22777be867e48300861b7b1bf614eb204463533d6724David Greene // instantiations, so create a new Record to inherit from it. 22787be867e48300861b7b1bf614eb204463533d6724David Greene 2279e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Add in the defm name. If the defm prefix is empty, give each 2280e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // instantiated def a unique name. Otherwise, if "#NAME#" exists in the 2281e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // name, substitute the prefix for #NAME#. Otherwise, use the defm name 2282e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // as a prefix. 2283e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2284d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose bool IsAnonymous = false; 2285d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose if (DefmPrefix == 0) { 22867be867e48300861b7b1bf614eb204463533d6724David Greene DefmPrefix = StringInit::get(GetNewAnonymousName()); 2287d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose IsAnonymous = true; 2288d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose } 22897be867e48300861b7b1bf614eb204463533d6724David Greene 22907be867e48300861b7b1bf614eb204463533d6724David Greene Init *DefName = DefProto->getNameInit(); 22917be867e48300861b7b1bf614eb204463533d6724David Greene 22926cfc806a6b82b60a3e923b6b89f2b4da62cdb50bSean Silva StringInit *DefNameString = dyn_cast<StringInit>(DefName); 22937be867e48300861b7b1bf614eb204463533d6724David Greene 2294d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (DefNameString != 0) { 2295d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // We have a fully expanded string so there are no operators to 2296d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // resolve. We should concatenate the given prefix and name. 22977be867e48300861b7b1bf614eb204463533d6724David Greene DefName = 22987be867e48300861b7b1bf614eb204463533d6724David Greene BinOpInit::get(BinOpInit::STRCONCAT, 22997be867e48300861b7b1bf614eb204463533d6724David Greene UnOpInit::get(UnOpInit::CAST, DefmPrefix, 23007be867e48300861b7b1bf614eb204463533d6724David Greene StringRecTy::get())->Fold(DefProto, &MC), 23017be867e48300861b7b1bf614eb204463533d6724David Greene DefName, StringRecTy::get())->Fold(DefProto, &MC); 23027be867e48300861b7b1bf614eb204463533d6724David Greene } 23037be867e48300861b7b1bf614eb204463533d6724David Greene 2304376a8a773e38fdcd9102a40e08ab1e0661d645d9Jakob Stoklund Olesen // Make a trail of SMLocs from the multiclass instantiations. 2305b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SmallVector<SMLoc, 4> Locs(1, DefmPrefixRange.Start); 2306376a8a773e38fdcd9102a40e08ab1e0661d645d9Jakob Stoklund Olesen Locs.append(DefProto->getLoc().begin(), DefProto->getLoc().end()); 2307d122009e57217bd574703c46dd14b1a9235ed0b7Jordan Rose Record *CurRec = new Record(DefName, Locs, Records, IsAnonymous); 2308e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2309e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SubClassReference Ref; 2310b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Ref.RefRange = DefmPrefixRange; 2311e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Ref.Rec = DefProto; 2312e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene AddSubClass(CurRec, Ref); 2313e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2314cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // Set the value for NAME. We don't resolve references to it 'til later, 2315cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // though, so that uses in nested multiclass names don't get 2316cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // confused. 2317b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (SetValue(CurRec, Ref.RefRange.Start, "NAME", std::vector<unsigned>(), 2318cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach DefmPrefix)) { 2319b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Error(DefmPrefixRange.Start, "Could not resolve " 2320cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach + CurRec->getNameInitAsString() + ":NAME to '" 2321cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach + DefmPrefix->getAsUnquotedString() + "'"); 2322cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach return 0; 2323cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach } 2324e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2325cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // If the DefNameString didn't resolve, we probably have a reference to 2326cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // NAME and need to replace it. We need to do at least this much greedily, 2327cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // otherwise nested multiclasses will end up with incorrect NAME expansions. 2328cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach if (DefNameString == 0) { 2329e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene RecordVal *DefNameRV = CurRec->getValue("NAME"); 2330e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene CurRec->resolveReferencesTo(DefNameRV); 2331e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2332e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2333e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene if (!CurMultiClass) { 2334cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // Now that we're at the top level, resolve all NAME references 2335cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // in the resultant defs that weren't in the def names themselves. 2336cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach RecordVal *DefNameRV = CurRec->getValue("NAME"); 2337cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach CurRec->resolveReferencesTo(DefNameRV); 2338cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach 2339cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // Now that NAME references are resolved and we're at the top level of 2340cfbda4a04dacaf976505c54a5308f6954b3b9a58Jim Grosbach // any multiclass expansions, add the record to the RecordKeeper. If we are 2341e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // currently in a multiclass, it means this defm appears inside a 2342e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // multiclass and its name won't be fully resolvable until we see 2343e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // the top-level defm. Therefore, we don't add this to the 2344e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // RecordKeeper at this point. If we did we could get duplicate 2345e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // defs as more than one probably refers to NAME or some other 2346e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // common internal placeholder. 2347e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2348e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // Ensure redefinition doesn't happen. 2349e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene if (Records.getDef(CurRec->getNameInitAsString())) { 2350b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Error(DefmPrefixRange.Start, "def '" + CurRec->getNameInitAsString() + 2351e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene "' already defined, instantiating defm with subdef '" + 2352e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene DefProto->getNameInitAsString() + "'"); 2353e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene return 0; 2354e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2355e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2356e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene Records.addDef(CurRec); 2357e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2358e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2359e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return CurRec; 2360e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2361e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2362e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greenebool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, 2363e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec, 2364e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc, 2365e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc SubClassLoc, 2366e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs, 2367e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene std::vector<Init *> &TemplateVals, 2368e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene bool DeleteArgs) { 2369e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Loop over all of the template arguments, setting them to the specified 2370e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // value or leaving them as the default if necessary. 2371e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 2372e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Check if a value is specified for this temp-arg. 2373e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (i < TemplateVals.size()) { 2374e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Set it now. 2375e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), 2376e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene TemplateVals[i])) 2377e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return true; 2378e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2379e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Resolve it next. 2380e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 2381e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2382e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (DeleteArgs) 2383e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Now remove it. 2384e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->removeValue(TArgs[i]); 2385e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2386e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 2387e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "value not specified for template argument #"+ 2388e22b321d2276b634519165b101b02d92c2fcf5c7David Greene utostr(i) + " (" + TArgs[i]->getAsUnquotedString() 2389e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + ") of multiclassclass '" + MC.Rec.getNameInitAsString() 2390e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + "'"); 2391e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2392e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2393e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return false; 2394e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2395e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2396e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greenebool TGParser::ResolveMulticlassDef(MultiClass &MC, 2397e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec, 2398e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *DefProto, 2399e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc) { 2400e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // If the mdef is inside a 'let' expression, add to each def. 24019cceede447118852df76e340252387d1a2cce37dSean Silva if (ApplyLetStack(CurRec)) 24029cceede447118852df76e340252387d1a2cce37dSean Silva return Error(DefmPrefixLoc, "when instantiating this defm"); 2403e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2404e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Don't create a top level definition for defm inside multiclasses, 2405e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // instead, only update the prototypes and bind the template args 2406e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // with the new created definition. 2407699b8705563a62cbe2f56594144001a6c9639c2eSean Silva if (!CurMultiClass) 2408699b8705563a62cbe2f56594144001a6c9639c2eSean Silva return false; 2409699b8705563a62cbe2f56594144001a6c9639c2eSean Silva for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); 2410699b8705563a62cbe2f56594144001a6c9639c2eSean Silva i != e; ++i) 2411699b8705563a62cbe2f56594144001a6c9639c2eSean Silva if (CurMultiClass->DefPrototypes[i]->getNameInit() 2412699b8705563a62cbe2f56594144001a6c9639c2eSean Silva == CurRec->getNameInit()) 2413699b8705563a62cbe2f56594144001a6c9639c2eSean Silva return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() + 2414699b8705563a62cbe2f56594144001a6c9639c2eSean Silva "' already defined in this multiclass!"); 2415699b8705563a62cbe2f56594144001a6c9639c2eSean Silva CurMultiClass->DefPrototypes.push_back(CurRec); 2416699b8705563a62cbe2f56594144001a6c9639c2eSean Silva 2417699b8705563a62cbe2f56594144001a6c9639c2eSean Silva // Copy the template arguments for the multiclass into the new def. 2418699b8705563a62cbe2f56594144001a6c9639c2eSean Silva const std::vector<Init *> &TA = 2419699b8705563a62cbe2f56594144001a6c9639c2eSean Silva CurMultiClass->Rec.getTemplateArgs(); 2420699b8705563a62cbe2f56594144001a6c9639c2eSean Silva 2421699b8705563a62cbe2f56594144001a6c9639c2eSean Silva for (unsigned i = 0, e = TA.size(); i != e; ++i) { 2422699b8705563a62cbe2f56594144001a6c9639c2eSean Silva const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]); 2423699b8705563a62cbe2f56594144001a6c9639c2eSean Silva assert(RV && "Template arg doesn't exist?"); 2424699b8705563a62cbe2f56594144001a6c9639c2eSean Silva CurRec->addValue(*RV); 2425e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2426e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2427e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return false; 2428e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2429e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2430f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDefm - Parse the instantiation of a multiclass. 2431f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2432f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' 2433f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2434270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopesbool TGParser::ParseDefm(MultiClass *CurMultiClass) { 2435f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); 2436b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMLoc DefmLoc = Lex.getLoc(); 2437a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Init *DefmPrefix = 0; 2438a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene 24396a59f5ade8e13e323affe09d5a22df7d146ae259Craig Topper if (Lex.Lex() == tgtok::Id) { // eat the defm. 2440a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene DefmPrefix = ParseObjectName(CurMultiClass); 2441df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner } 2442c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 2443b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMLoc DefmPrefixEndLoc = Lex.getLoc(); 2444df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner if (Lex.getCode() != tgtok::colon) 2445f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ':' after defm identifier"); 244621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 24476e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Keep track of the new generated record definitions. 24486e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes std::vector<Record*> NewRecDefs; 24496e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24506e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // This record also inherits from a regular class (non-multiclass)? 24516e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes bool InheritFromClass = false; 24526e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 2453f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // eat the colon. 2454f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 2455f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 24561e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc SubClassLoc = Lex.getLoc(); 2457f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference Ref = ParseSubClassReference(0, true); 24585654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24595654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene while (1) { 24605654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Ref.Rec == 0) return true; 24615654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24625654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // To instantiate a multiclass, we need to first get the multiclass, then 24635654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // instantiate each def contained in the multiclass with the SubClassRef 24645654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // template parameters. 24655654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene MultiClass *MC = MultiClasses[Ref.Rec->getName()]; 24665654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene assert(MC && "Didn't lookup multiclass correctly?"); 246705bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> &TemplateVals = Ref.TemplateArgs; 24685654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24695654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // Verify that the correct number of template arguments were specified. 2470e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs(); 24715654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (TArgs.size() < TemplateVals.size()) 24725654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene return Error(SubClassLoc, 24735654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene "more template args specified than multiclass expects"); 24745654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24755654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // Loop over all the def's in the multiclass, instantiating each one. 24765654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { 24775654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Record *DefProto = MC->DefPrototypes[i]; 24785654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 2479b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, 2480b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose SMRange(DefmLoc, 2481b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose DefmPrefixEndLoc)); 248294f2dc90a54443ea405776d6ffa2a4e27800d3d6Jim Grosbach if (!CurRec) 248394f2dc90a54443ea405776d6ffa2a4e27800d3d6Jim Grosbach return true; 2484270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes 2485b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (ResolveMulticlassDefArgs(*MC, CurRec, DefmLoc, SubClassLoc, 2486e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene TArgs, TemplateVals, true/*Delete args*/)) 2487e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "could not instantiate def"); 2488270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes 2489b50df4a3df6db2ace3c011267934d3d10bdcc8dbJordan Rose if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmLoc)) 2490e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "could not instantiate def"); 24916e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24926e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes NewRecDefs.push_back(CurRec); 2493f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 24945654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 2495e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 24965654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Lex.getCode() != tgtok::comma) break; 24975654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Lex.Lex(); // eat ','. 24985654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24995654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene SubClassLoc = Lex.getLoc(); 25006e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25016e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // A defm can inherit from regular classes (non-multiclass) as 25026e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // long as they come in the end of the inheritance list. 25036e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0); 25046e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25056e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (InheritFromClass) 25066e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes break; 25076e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25085654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Ref = ParseSubClassReference(0, true); 2509f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 25105654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 25116e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (InheritFromClass) { 25126e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Process all the classes to inherit as if they were part of a 25136e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // regular 'def' and inherit all record values. 25146e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes SubClassReference SubClass = ParseSubClassReference(0, false); 25156e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes while (1) { 25166e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Check for error. 25176e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (SubClass.Rec == 0) return true; 25186e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25196e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Get the expanded definition prototypes and teach them about 25206e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // the record values the current class to inherit has 25216e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) { 25226e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes Record *CurRec = NewRecDefs[i]; 25236e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25246e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Add it. 25256e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (AddSubClass(CurRec, SubClass)) 25266e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes return true; 25276e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25289cceede447118852df76e340252387d1a2cce37dSean Silva if (ApplyLetStack(CurRec)) 25299cceede447118852df76e340252387d1a2cce37dSean Silva return true; 25306e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 25316e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 25326e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (Lex.getCode() != tgtok::comma) break; 25336e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes Lex.Lex(); // eat ','. 25346e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes SubClass = ParseSubClassReference(0, false); 25356e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 25366e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 25376e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 2538e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes if (!CurMultiClass) 2539e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) 25400d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // See Record::setName(). This resolve step will see any new 25410d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // name for the def that might have been created when resolving 25420d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // inheritance, values and arguments above. 2543e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes NewRecDefs[i]->resolveReferences(); 2544e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes 25455654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Lex.getCode() != tgtok::semi) 25465654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene return TokError("expected ';' at end of defm"); 25475654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Lex.Lex(); 254821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2549f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2550f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2551f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2552f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObject 2553f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= ClassInst 2554f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= DefInst 2555f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= MultiClassInst 2556f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= DefMInst 2557f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LETCommand '{' ObjectList '}' 2558f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LETCommand Object 2559ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseObject(MultiClass *MC) { 2560f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 2561d6d9dd9f5e950383b7040a864bd8e004f95bcc91Chris Lattner default: 2562d6d9dd9f5e950383b7040a864bd8e004f95bcc91Chris Lattner return TokError("Expected class, def, defm, multiclass or let definition"); 2563ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Let: return ParseTopLevelLet(MC); 2564ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Def: return ParseDef(MC); 2565cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene case tgtok::Foreach: return ParseForeach(MC); 2566ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Defm: return ParseDefm(MC); 2567f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Class: return ParseClass(); 2568f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::MultiClass: return ParseMultiClass(); 2569f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2570f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2571f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2572f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectList 2573f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectList :== Object* 2574ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseObjectList(MultiClass *MC) { 2575f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (isObjectStart(Lex.getCode())) { 2576ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(MC)) 2577f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2578f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2579f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2580f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2581f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2582f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseFile() { 2583f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Prime the lexer. 2584f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseObjectList()) return true; 258521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2586f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If we have unread input at the end of the file, report it. 2587f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::Eof) 2588f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 258921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2590f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Unexpected input at top level"); 2591f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2593