TGParser.cpp revision 72cba6cdf640411e2fb6207858a0abd87c4286fc
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" 157c788888872233748da10a8177a9a1eb176c1bc8Peter Collingbourne#include "llvm/TableGen/Record.h" 16f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner#include "llvm/ADT/StringExtras.h" 171a55180238dbcf11113f610aea010447e51f595bDaniel Dunbar#include <algorithm> 181a55180238dbcf11113f610aea010447e51f595bDaniel Dunbar#include <sstream> 198d978a75b477c0e4861d937096518eebba639e72Chris Lattner#include "llvm/ADT/SmallVector.h" 2046f55527d848bcc7cff1210137caff29bbf1b010Chris Lattner#include "llvm/Support/CommandLine.h" 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 { 291e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc RefLoc; 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 { 381e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc RefLoc; 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()) 9605bce0beee87512e52428d4b80f5a8e79a949576David Greene if (VarInit *VI = dynamic_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()) { 10505bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *CurVal = dynamic_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) { 11377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen V->convertInitializerTo(BitsRecTy::get(BitList.size())); 114f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Initializer is not compatible with bit range"); 115f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 11621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 117f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // We should have a BitsInit type now. 11805bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *BInit = dynamic_cast<BitsInit*>(BI); 119f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(BInit != 0); 120f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 12105bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init *, 16> NewBits(CurVal->getNumBits()); 122f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 123f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Loop over bits, assigning values as appropriate. 124f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = BitList.size(); i != e; ++i) { 125f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner unsigned Bit = BitList[i]; 126ca7fd3de360b266783438666054dba198ff77ba2David Greene if (NewBits[Bit]) 127f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(Loc, "Cannot set bit #" + utostr(Bit) + " of value '" + 128917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene ValName->getAsUnquotedString() + "' more than once"); 129ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[Bit] = BInit->getBit(i); 130f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 131f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 132f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = CurVal->getNumBits(); i != e; ++i) 133ca7fd3de360b266783438666054dba198ff77ba2David Greene if (NewBits[i] == 0) 134ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[i] = CurVal->getBit(i); 135f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 136dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene V = BitsInit::get(NewBits); 137f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 138f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 139f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (RV->setValue(V)) 140917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene return Error(Loc, "Value '" + ValName->getAsUnquotedString() + "' of type '" 141917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene + RV->getType()->getAsString() + 142917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene "' is incompatible with initializer '" + V->getAsString() 143917924d9912df76ba2e639c8c5b00cdcac91a16eDavid Greene + "'"); 144f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 146f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// AddSubClass - Add SubClass as a subclass to CurRec, resolving its template 148f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// args as SubClass's template arguments. 149aff9c270de8de7d1a0bc138d391bc67136bad58eCedric Venetbool TGParser::AddSubClass(Record *CurRec, SubClassReference &SubClass) { 150f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *SC = SubClass.Rec; 151f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add all of the values in the subclass into the current class. 152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<RecordVal> &Vals = SC->getValues(); 153f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = Vals.size(); i != e; ++i) 154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddValue(CurRec, SubClass.RefLoc, Vals[i])) 155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 156f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 157e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = SC->getTemplateArgs(); 158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 159f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Ensure that an appropriate number of template arguments are specified. 160f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (TArgs.size() < SubClass.TemplateArgs.size()) 161f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc, "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. 16821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (SetValue(CurRec, SubClass.RefLoc, TArgs[i], std::vector<unsigned>(), 169f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 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()) { 179f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc,"Value not specified for template argument #" 180e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + utostr(i) + " (" + TArgs[i]->getAsUnquotedString() 181e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + ") of subclass '" + SC->getNameInitAsString() + "'!"); 182f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 183f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 184f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 185f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Since everything went well, we can now set the "superclass" list for the 186f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // current record. 187f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const std::vector<Record*> &SCs = SC->getSuperClasses(); 188f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = SCs.size(); i != e; ++i) { 189f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isSubClassOf(SCs[i])) 190f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc, 191f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "Already subclass of '" + SCs[i]->getName() + "'!\n"); 192f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->addSuperClass(SCs[i]); 193f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 19421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 195f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isSubClassOf(SC)) 196f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(SubClass.RefLoc, 197f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "Already subclass of '" + SC->getName() + "'!\n"); 198f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->addSuperClass(SC); 199f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 200f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 201f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 202de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// AddSubMultiClass - Add SubMultiClass as a subclass to 203440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson/// CurMC, resolving its template args as SubMultiClass's 204de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// template arguments. 205440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilsonbool TGParser::AddSubMultiClass(MultiClass *CurMC, 2061d512df4a9e249ceb6d6b69e89527c75d872ecd1Bob Wilson SubMultiClassReference &SubMultiClass) { 207de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *SMC = SubMultiClass.MC; 208440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson Record *CurRec = &CurMC->Rec; 209de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 210440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson const std::vector<RecordVal> &MCVals = CurRec->getValues(); 211de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 212de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the values in the subclass into the current class. 213de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene const std::vector<RecordVal> &SMCVals = SMC->Rec.getValues(); 214de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = SMCVals.size(); i != e; ++i) 215de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddValue(CurRec, SubMultiClass.RefLoc, SMCVals[i])) 216de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 217de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 218440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson int newDefStart = CurMC->DefPrototypes.size(); 219d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 220de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the defs in the subclass into the current multiclass. 221de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (MultiClass::RecordVector::const_iterator i = SMC->DefPrototypes.begin(), 222de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene iend = SMC->DefPrototypes.end(); 223de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene i != iend; 224de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene ++i) { 225de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Clone the def and add it to the current multiclass 226de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Record *NewDef = new Record(**i); 227de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 228de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add all of the values in the superclass into the current def. 229de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = MCVals.size(); i != e; ++i) 230de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddValue(NewDef, SubMultiClass.RefLoc, MCVals[i])) 231de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 232de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 233440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson CurMC->DefPrototypes.push_back(NewDef); 234de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 23532558650ae09247b31aa89cdee64e8c849771024Bob Wilson 236e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &SMCTArgs = SMC->Rec.getTemplateArgs(); 237de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 238d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // Ensure that an appropriate number of template arguments are 239d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // specified. 240de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (SMCTArgs.size() < SubMultiClass.TemplateArgs.size()) 241d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return Error(SubMultiClass.RefLoc, 242d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene "More template args specified than expected"); 24332558650ae09247b31aa89cdee64e8c849771024Bob Wilson 244de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Loop over all of the template arguments, setting them to the specified 245de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // value or leaving them as the default if necessary. 246de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene for (unsigned i = 0, e = SMCTArgs.size(); i != e; ++i) { 247de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (i < SubMultiClass.TemplateArgs.size()) { 248d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // If a value is specified for this template arg, set it in the 249d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // superclass now. 250d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (SetValue(CurRec, SubMultiClass.RefLoc, SMCTArgs[i], 25132558650ae09247b31aa89cdee64e8c849771024Bob Wilson std::vector<unsigned>(), 252de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass.TemplateArgs[i])) 253de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 254de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 255de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Resolve it next. 256de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene CurRec->resolveReferencesTo(CurRec->getValue(SMCTArgs[i])); 25732558650ae09247b31aa89cdee64e8c849771024Bob Wilson 258de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Now remove it. 259de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene CurRec->removeValue(SMCTArgs[i]); 260de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 261d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // If a value is specified for this template arg, set it in the 262d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene // new defs now. 263d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene for (MultiClass::RecordVector::iterator j = 264440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson CurMC->DefPrototypes.begin() + newDefStart, 265440548dd1f78682e372a38cacccf7c5ac1781b2cBob Wilson jend = CurMC->DefPrototypes.end(); 266de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene j != jend; 267de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene ++j) { 268de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Record *Def = *j; 269de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 270d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (SetValue(Def, SubMultiClass.RefLoc, SMCTArgs[i], 27132558650ae09247b31aa89cdee64e8c849771024Bob Wilson std::vector<unsigned>(), 272de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass.TemplateArgs[i])) 273de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 274de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 275de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Resolve it next. 276de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Def->resolveReferencesTo(Def->getValue(SMCTArgs[i])); 277de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 278de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Now remove it 279de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Def->removeValue(SMCTArgs[i]); 280de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 281de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } else if (!CurRec->getValue(SMCTArgs[i])->getValue()->isComplete()) { 282d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return Error(SubMultiClass.RefLoc, 283d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene "Value not specified for template argument #" 284e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + utostr(i) + " (" + SMCTArgs[i]->getAsUnquotedString() 285e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + ") of subclass '" + SMC->Rec.getNameInitAsString() + "'!"); 286de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 287de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 288de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 289de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return false; 290de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 291de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 292cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ProcessForeachDefs - Given a record, apply all of the variable 293cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// values in all surrounding foreach loops, creating new records for 294cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// each combination of values. 2958e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesenbool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc) { 2968e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (Loops.empty()) 2978e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return false; 2988e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen 299cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // We want to instantiate a new copy of CurRec for each combination 300cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // of nested loop iterator values. We don't want top instantiate 301cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // any copies until we have values for each loop iterator. 302cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene IterSet IterVals; 3038e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return ProcessForeachDefs(CurRec, Loc, IterVals); 304cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 305cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 306cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ProcessForeachDefs - Given a record, a loop and a loop iterator, 307cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// apply each of the variable values in this loop and then process 308cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// subloops. 3098e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesenbool TGParser::ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals){ 3108e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Recursively build a tuple of iterator values. 3118e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (IterVals.size() != Loops.size()) { 3128e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen assert(IterVals.size() < Loops.size()); 3138e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen ForeachLoop &CurLoop = Loops[IterVals.size()]; 3148e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen ListInit *List = dynamic_cast<ListInit *>(CurLoop.ListValue); 3158e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (List == 0) { 3168e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "Loop list is not a list"); 3178e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3188e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 319cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3208e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Process each value. 3218e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen for (int64_t i = 0; i < List->getSize(); ++i) { 3228e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Init *ItemVal = List->resolveListElementReference(*CurRec, 0, i); 3238e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterVals.push_back(IterRecord(CurLoop.IterVar, ItemVal)); 3248e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (ProcessForeachDefs(CurRec, Loc, IterVals)) 3258e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3268e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterVals.pop_back(); 3278e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 3288e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return false; 3298e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 330cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3318e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // This is the bottom of the recursion. We have all of the iterator values 3328e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // for this point in the iteration space. Instantiate a new record to 3338e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // reflect this combination of values. 3348e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Record *IterRec = new Record(*CurRec); 335cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3368e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Set the iterator values now. 3378e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen for (unsigned i = 0, e = IterVals.size(); i != e; ++i) { 3388e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen VarInit *IterVar = IterVals[i].IterVar; 3398e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen TypedInit *IVal = dynamic_cast<TypedInit *>(IterVals[i].IterValue); 3408e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (IVal == 0) { 3418e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "foreach iterator value is untyped"); 3428e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3438e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 344cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3458e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->addValue(RecordVal(IterVar->getName(), IVal->getType(), false)); 346cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3478e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (SetValue(IterRec, Loc, IterVar->getName(), 3488e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen std::vector<unsigned>(), IVal)) { 3498e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "when instantiating this def"); 3508e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 351cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 352cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3538e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Resolve it next. 3548e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->resolveReferencesTo(IterRec->getValue(IterVar->getName())); 355cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 3568e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen // Remove it. 3578e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->removeValue(IterVar->getName()); 358cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 3598e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen 3608e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (Records.getDef(IterRec->getNameInitAsString())) { 3618e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Error(Loc, "def already exists: " + IterRec->getNameInitAsString()); 3628e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen return true; 3638e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen } 3648e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen 3658e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Records.addDef(IterRec); 3668e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen IterRec->resolveReferences(); 367cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return false; 368cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 369cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 370f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 371f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner// Parser Code 372f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner//===----------------------------------------------------------------------===// 373f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 374f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// isObjectStart - Return true if this is a valid first token for an Object. 375f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstatic bool isObjectStart(tgtok::TokKind K) { 376f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return K == tgtok::Class || K == tgtok::Def || 377cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene K == tgtok::Defm || K == tgtok::Let || 378cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene K == tgtok::MultiClass || K == tgtok::Foreach; 379f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 380f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 381df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattnerstatic std::string GetNewAnonymousName() { 382df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner static unsigned AnonCounter = 0; 383df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner return "anonymous."+utostr(AnonCounter++); 384df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner} 385df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner 386f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectName - If an object name is specified, return it. Otherwise, 387f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// return an anonymous name. 388a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene/// ObjectName ::= Value [ '#' Value ]* 389f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectName ::= /*empty*/ 390f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 391a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid GreeneInit *TGParser::ParseObjectName(MultiClass *CurMultiClass) { 392a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene switch (Lex.getCode()) { 393a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene case tgtok::colon: 394a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene case tgtok::semi: 395a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene case tgtok::l_brace: 396a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene // These are all of the tokens that can begin an object body. 397a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene // Some of these can also begin values but we disallow those cases 398a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene // because they are unlikely to be useful. 399a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene return StringInit::get(GetNewAnonymousName()); 400a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene default: 401a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene break; 402a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene } 403c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 404a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Record *CurRec = 0; 405a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene if (CurMultiClass) 406a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene CurRec = &CurMultiClass->Rec; 407f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 408a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene RecTy *Type = 0; 409a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene if (CurRec) { 410a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene const TypedInit *CurRecName = 411a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene dynamic_cast<const TypedInit *>(CurRec->getNameInit()); 412a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene if (!CurRecName) { 413a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene TokError("Record name is not typed!"); 414a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene return 0; 415a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene } 416a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Type = CurRecName->getType(); 417a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene } 418a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene 419a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene return ParseValue(CurRec, Type, ParseNameMode); 420a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene} 421f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 422f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseClassID - Parse and resolve a reference to a class name. This returns 423f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// null on error. 424f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 425f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ClassID ::= ID 426f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 427f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecord *TGParser::ParseClassID() { 428f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 429f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected name for ClassID"); 430f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 431f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 43221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 433f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Result = Records.getClass(Lex.getCurStrVal()); 434f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) 435f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 43621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 437f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 438f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 439f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 440f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 44132558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ParseMultiClassID - Parse and resolve a reference to a multiclass name. 44232558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// This returns null on error. 443de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 444de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// MultiClassID ::= ID 445de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 446de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneMultiClass *TGParser::ParseMultiClassID() { 447de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::Id) { 448de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("expected name for ClassID"); 449de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return 0; 450de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 45132558650ae09247b31aa89cdee64e8c849771024Bob Wilson 452de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene MultiClass *Result = MultiClasses[Lex.getCurStrVal()]; 453de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result == 0) 454de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("Couldn't find class '" + Lex.getCurStrVal() + "'"); 45532558650ae09247b31aa89cdee64e8c849771024Bob Wilson 456de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 457de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 458de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 459de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 460f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecord *TGParser::ParseDefmID() { 461f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 462f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected multiclass name"); 463f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 464f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 46521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 466f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner MultiClass *MC = MultiClasses[Lex.getCurStrVal()]; 467f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (MC == 0) { 468f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Couldn't find multiclass '" + Lex.getCurStrVal() + "'"); 469f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 470f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 47121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 472f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 473f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return &MC->Rec; 47421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson} 475f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 476f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 477f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseSubClassReference - Parse a reference to a subclass or to a templated 478f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// subclass. This returns a SubClassRefTy with a null Record* on error. 479f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 480f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SubClassRef ::= ClassID 481f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SubClassRef ::= ClassID '<' ValueList '>' 482f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 483f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerSubClassReference TGParser:: 484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerParseSubClassReference(Record *CurRec, bool isDefm) { 485f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference Result; 486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.RefLoc = Lex.getLoc(); 48721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (isDefm) 489f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = ParseDefmID(); 490f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner else 491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = ParseClassID(); 492f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result.Rec == 0) return Result; 49321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 494f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there is no template arg list, we're done. 495f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::less) 496f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 497f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the '<' 49821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 499f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::greater) { 500f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("subclass reference requires a non-empty list of template values"); 501f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; 502f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 503f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 50421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 505e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.TemplateArgs = ParseValueList(CurRec, Result.Rec); 506f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result.TemplateArgs.empty()) { 507f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; // Error parsing value list. 508f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 509f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 51021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 511f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 512f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' in template value list"); 513f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.Rec = 0; 514f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 515f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 516f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 51721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 518f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 519f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 520f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 52132558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ParseSubMultiClassReference - Parse a reference to a subclass or to a 52232558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// templated submulticlass. This returns a SubMultiClassRefTy with a null 52332558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// Record* on error. 524de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 525de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// SubMultiClassRef ::= MultiClassID 526de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// SubMultiClassRef ::= MultiClassID '<' ValueList '>' 527de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene/// 528de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneSubMultiClassReference TGParser:: 529de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid GreeneParseSubMultiClassReference(MultiClass *CurMC) { 530de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClassReference Result; 531de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.RefLoc = Lex.getLoc(); 53232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 533de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = ParseMultiClassID(); 534de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result.MC == 0) return Result; 53532558650ae09247b31aa89cdee64e8c849771024Bob Wilson 536de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // If there is no template arg list, we're done. 537de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::less) 538de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 539de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); // Eat the '<' 54032558650ae09247b31aa89cdee64e8c849771024Bob Wilson 541de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() == tgtok::greater) { 542de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("subclass reference requires a non-empty list of template values"); 543de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; 544de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 545de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 54632558650ae09247b31aa89cdee64e8c849771024Bob Wilson 547e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.TemplateArgs = ParseValueList(&CurMC->Rec, &Result.MC->Rec); 548de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Result.TemplateArgs.empty()) { 549de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; // Error parsing value list. 550de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 551de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 55232558650ae09247b31aa89cdee64e8c849771024Bob Wilson 553de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::greater) { 554de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene TokError("expected '>' in template value list"); 555de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Result.MC = 0; 556de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 557de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 558de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 559de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 560de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return Result; 561de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene} 562de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 563f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseRangePiece - Parse a bit/value range. 564f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL 565f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL '-' INTVAL 566f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangePiece ::= INTVAL INTVAL 567f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseRangePiece(std::vector<unsigned> &Ranges) { 568811281e788432d3136de13f72d05790eced52d92Chris Lattner if (Lex.getCode() != tgtok::IntVal) { 569811281e788432d3136de13f72d05790eced52d92Chris Lattner TokError("expected integer or bitrange"); 570811281e788432d3136de13f72d05790eced52d92Chris Lattner return true; 571811281e788432d3136de13f72d05790eced52d92Chris Lattner } 57263f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman int64_t Start = Lex.getCurIntVal(); 57363f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman int64_t End; 57421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 575f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Start < 0) 576f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("invalid range, cannot be negative"); 57721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 578f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.Lex()) { // eat first character. 57921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson default: 580f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 581f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 582f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::minus: 583f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::IntVal) { 584f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected integer value as end of range"); 585f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 586f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 587f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner End = Lex.getCurIntVal(); 588f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 589f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::IntVal: 590f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner End = -Lex.getCurIntVal(); 591f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 59321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (End < 0) 594f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("invalid range, cannot be negative"); 595f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 59621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 597f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add to the range. 598f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Start < End) { 599f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (; Start <= End; ++Start) 600f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 601f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 602f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (; Start >= End; --Start) 603f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges.push_back(Start); 604f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 605f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 606f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 607f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 608f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseRangeList - Parse a list of scalars and ranges into scalar values. 609f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 610f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// RangeList ::= RangePiece (',' RangePiece)* 611f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 612f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::vector<unsigned> TGParser::ParseRangeList() { 613f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Result; 61421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 615f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the first piece. 616f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseRangePiece(Result)) 617f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<unsigned>(); 618f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 619f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the comma. 620f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 621f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the next range piece. 622f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseRangePiece(Result)) 623f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<unsigned>(); 624f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 625f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 626f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 627f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 628f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. 629f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalRangeList ::= '<' RangeList '>' 630f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalRangeList ::= /*empty*/ 631f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseOptionalRangeList(std::vector<unsigned> &Ranges) { 632f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::less) 633f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 63421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 6351e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc StartLoc = Lex.getLoc(); 636f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '<' 63721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 638f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the range list. 639f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges = ParseRangeList(); 640f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return true; 64121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 642f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 643f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of range list"); 644f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(StartLoc, "to match this '<'"); 645f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 646f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '>'. 647f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 648f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 649f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 650f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseOptionalBitList - Parse either a bit list in {}'s or nothing. 651f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalBitList ::= '{' RangeList '}' 652f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// OptionalBitList ::= /*empty*/ 653f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseOptionalBitList(std::vector<unsigned> &Ranges) { 654f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) 655f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 65621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 6571e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc StartLoc = Lex.getLoc(); 658f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 65921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 660f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the range list. 661f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Ranges = ParseRangeList(); 662f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return true; 66321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 664f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 665f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit list"); 666f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(StartLoc, "to match this '{'"); 667f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 668f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '}'. 669f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 670f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 671f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 672f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 673f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseType - Parse and return a tblgen type. This returns null on error. 674f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 675f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= STRING // string type 6768dd6f0c8353f80de6526810899f271d539f6929cJakob Stoklund Olesen/// Type ::= CODE // code type 677f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= BIT // bit type 678f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= BITS '<' INTVAL '>' // bits<x> type 679f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= INT // int type 680f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= LIST '<' Type '>' // list<x> type 681f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= DAG // dag type 682f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Type ::= ClassID // Record Type 683f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 684f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerRecTy *TGParser::ParseType() { 685f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 686f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: TokError("Unknown token when expecting a type"); return 0; 68777f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::String: Lex.Lex(); return StringRecTy::get(); 6888dd6f0c8353f80de6526810899f271d539f6929cJakob Stoklund Olesen case tgtok::Code: Lex.Lex(); return StringRecTy::get(); 68977f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Bit: Lex.Lex(); return BitRecTy::get(); 69077f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Int: Lex.Lex(); return IntRecTy::get(); 69177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::Dag: Lex.Lex(); return DagRecTy::get(); 692f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Id: 69377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen if (Record *R = ParseClassID()) return RecordRecTy::get(R); 694f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 695f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Bits: { 696f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) { // Eat 'bits' 697f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '<' after bits type"); 698f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 699f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 700f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::IntVal) { // Eat '<' 701f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected integer in bits<n> type"); 702f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 703f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 70463f97201dc9dcebbe84d1b73113166c64212b4b8Dan Gohman uint64_t Val = Lex.getCurIntVal(); 705f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::greater) { // Eat count. 706f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of bits<n> type"); 707f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 708f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 709f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '>' 71077f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return BitsRecTy::get(Val); 711f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 712f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::List: { 713f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) { // Eat 'bits' 714f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '<' after list type"); 715f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 716f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 717f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '<' 718f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecTy *SubType = ParseType(); 719f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SubType == 0) return 0; 72021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 721f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) { 722f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '>' at end of list<ty> type"); 723f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 724f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 725f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat '>' 72677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return ListRecTy::get(SubType); 727f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 72821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 729f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 730f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 731f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseIDValue - Parse an ID as a value and decode what it means. 732f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 733f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def local value] 734f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def template arg] 735f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [multiclass local value] 736f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [multiclass template argument] 737f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// IDValue ::= ID [def name] 738f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 739f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David GreeneInit *TGParser::ParseIDValue(Record *CurRec, IDParseMode Mode) { 740f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Id && "Expected ID in ParseIDValue"); 741f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 7421e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc Loc = Lex.getLoc(); 743f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 744f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseIDValue(CurRec, Name, Loc); 745f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 746f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 747f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseIDValue - This is just like ParseIDValue above, but it assumes the ID 748f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// has already been read. 74905bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseIDValue(Record *CurRec, 750f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene const std::string &Name, SMLoc NameLoc, 751f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene IDParseMode Mode) { 752f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 753f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (const RecordVal *RV = CurRec->getValue(Name)) 754dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(Name, RV->getType()); 75521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 756e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *TemplateArgName = QualifyName(*CurRec, CurMultiClass, Name, ":"); 757e22b321d2276b634519165b101b02d92c2fcf5c7David Greene 758caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene if (CurMultiClass) 759e22b321d2276b634519165b101b02d92c2fcf5c7David Greene TemplateArgName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, 760e22b321d2276b634519165b101b02d92c2fcf5c7David Greene "::"); 761caa25c81cd12f8a25b6cb7a3cba865a0dbcd4eafDavid Greene 762f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec->isTemplateArg(TemplateArgName)) { 763f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const RecordVal *RV = CurRec->getValue(TemplateArgName); 764f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(RV && "Template arg doesn't exist??"); 765dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(TemplateArgName, RV->getType()); 766f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 767f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 76821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 769f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass) { 770e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *MCName = QualifyName(CurMultiClass->Rec, CurMultiClass, Name, 771e22b321d2276b634519165b101b02d92c2fcf5c7David Greene "::"); 772e22b321d2276b634519165b101b02d92c2fcf5c7David Greene 773f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass->Rec.isTemplateArg(MCName)) { 774f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner const RecordVal *RV = CurMultiClass->Rec.getValue(MCName); 775f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(RV && "Template arg doesn't exist??"); 776dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return VarInit::get(MCName, RV->getType()); 777f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 778f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 77921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 780cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // If this is in a foreach loop, make sure it's not a loop iterator 781cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene for (LoopVector::iterator i = Loops.begin(), iend = Loops.end(); 782cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene i != iend; 783cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene ++i) { 784cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene VarInit *IterVar = dynamic_cast<VarInit *>(i->IterVar); 785cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (IterVar && IterVar->getName() == Name) 786cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return IterVar; 787cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 788cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 789bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene if (Mode == ParseNameMode) 790bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene return StringInit::get(Name); 791bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene 792f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Record *D = Records.getDef(Name)) 79377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return DefInit::get(D); 794f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 795bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene if (Mode == ParseValueMode) { 796bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene Error(NameLoc, "Variable not defined: '" + Name + "'"); 797bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene return 0; 798bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene } 799bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene 800bbec279d8eb0d7e27c2bf6e4da4f44286451d142David Greene return StringInit::get(Name); 801f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 802f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 803d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// ParseOperation - Parse an operator. This returns null on error. 804d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 805d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// Operation ::= XOperator ['<' Type '>'] '(' Args ')' 806d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 80705bce0beee87512e52428d4b80f5a8e79a949576David GreeneInit *TGParser::ParseOperation(Record *CurRec) { 808d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene switch (Lex.getCode()) { 809d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene default: 810d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("unknown operation"); 811d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 8121434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 8131434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 8141434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 815e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: { // Value ::= !unop '(' Value ')' 816e6c27de069225e1122c78385ad22a2ff656db8e6David Greene UnOpInit::UnaryOp Code; 817e6c27de069225e1122c78385ad22a2ff656db8e6David Greene RecTy *Type = 0; 818d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 819e6c27de069225e1122c78385ad22a2ff656db8e6David Greene switch (Lex.getCode()) { 820858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 821e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: 822e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the operation 823e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Code = UnOpInit::CAST; 824d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 825e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Type = ParseOperatorType(); 826d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 827e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Type == 0) { 8285f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("did not get type for unary operator"); 829e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 830e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 831d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 832e6c27de069225e1122c78385ad22a2ff656db8e6David Greene break; 8331434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 8345f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 8351434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::HEAD; 8365f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 8371434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 8385f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 8391434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::TAIL; 8405f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 8411434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 8425f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Lex.Lex(); // eat the operation 8431434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene Code = UnOpInit::EMPTY; 84477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = IntRecTy::get(); 8455f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene break; 846e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 847e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Lex.getCode() != tgtok::l_paren) { 848e6c27de069225e1122c78385ad22a2ff656db8e6David Greene TokError("expected '(' after unary operator"); 849e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 850e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 851e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the '(' 852d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 85305bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *LHS = ParseValue(CurRec); 854e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (LHS == 0) return 0; 855d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 8561434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD 8571434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::TAIL 8581434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::EMPTY) { 85905bce0beee87512e52428d4b80f5a8e79a949576David Greene ListInit *LHSl = dynamic_cast<ListInit*>(LHS); 86005bce0beee87512e52428d4b80f5a8e79a949576David Greene StringInit *LHSs = dynamic_cast<StringInit*>(LHS); 86105bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *LHSt = dynamic_cast<TypedInit*>(LHS); 862e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LHSl == 0 && LHSs == 0 && LHSt == 0) { 863e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list or string type argument in unary operator"); 8645f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8655f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8665f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSt) { 8675f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); 868e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene StringRecTy *SType = dynamic_cast<StringRecTy*>(LHSt->getType()); 869e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LType == 0 && SType == 0) { 870e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list or string type argumnet in unary operator"); 8715f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8725f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8735f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8745f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene 8751434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD 8761434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene || Code == UnOpInit::TAIL) { 877e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (LHSl == 0 && LHSt == 0) { 878e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected list type argumnet in unary operator"); 879e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 880e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 88121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 8825f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSl && LHSl->getSize() == 0) { 8835f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("empty list argument in unary operator"); 8845f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8855f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8865f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LHSl) { 88705bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Item = LHSl->getElement(0); 88805bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *Itemt = dynamic_cast<TypedInit*>(Item); 8895f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (Itemt == 0) { 8905f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("untyped list element in unary operator"); 8915f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 8925f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 8931434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD) { 8945f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = Itemt->getType(); 89521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 89677f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = ListRecTy::get(Itemt->getType()); 8975f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 89821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 8995f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene assert(LHSt && "expected list type argument in unary operator"); 9005f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene ListRecTy *LType = dynamic_cast<ListRecTy*>(LHSt->getType()); 9015f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene if (LType == 0) { 9025f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene TokError("expected list type argumnet in unary operator"); 9035f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene return 0; 9045f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9051434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene if (Code == UnOpInit::HEAD) { 9065f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = LType->getElementType(); 90721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 9085f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene Type = LType; 9095f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9105f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9115f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9125f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene } 9135f9f9ba00b85eb19da2618f393e43a6a11b5d892David Greene 914e6c27de069225e1122c78385ad22a2ff656db8e6David Greene if (Lex.getCode() != tgtok::r_paren) { 915e6c27de069225e1122c78385ad22a2ff656db8e6David Greene TokError("expected ')' in unary operator"); 916e6c27de069225e1122c78385ad22a2ff656db8e6David Greene return 0; 917e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 918e6c27de069225e1122c78385ad22a2ff656db8e6David Greene Lex.Lex(); // eat the ')' 919dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec, CurMultiClass); 920e6c27de069225e1122c78385ad22a2ff656db8e6David Greene } 921d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 922d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XConcat: 92321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XSRA: 924d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XSRL: 925d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene case tgtok::XSHL: 9266786d5e18387465d6106eaef0b7923526ef0bc10David Greene case tgtok::XEq: 927c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner case tgtok::XStrConcat: { // Value ::= !binop '(' Value ',' Value ')' 9288d978a75b477c0e4861d937096518eebba639e72Chris Lattner tgtok::TokKind OpTok = Lex.getCode(); 9298d978a75b477c0e4861d937096518eebba639e72Chris Lattner SMLoc OpLoc = Lex.getLoc(); 9308d978a75b477c0e4861d937096518eebba639e72Chris Lattner Lex.Lex(); // eat the operation 9318d978a75b477c0e4861d937096518eebba639e72Chris Lattner 932d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene BinOpInit::BinaryOp Code; 933d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene RecTy *Type = 0; 934d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 9358d978a75b477c0e4861d937096518eebba639e72Chris Lattner switch (OpTok) { 936858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 93777f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XConcat: Code = BinOpInit::CONCAT;Type = DagRecTy::get(); break; 93877f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSRA: Code = BinOpInit::SRA; Type = IntRecTy::get(); break; 93977f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSRL: Code = BinOpInit::SRL; Type = IntRecTy::get(); break; 94077f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XSHL: Code = BinOpInit::SHL; Type = IntRecTy::get(); break; 94177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen case tgtok::XEq: Code = BinOpInit::EQ; Type = BitRecTy::get(); break; 94221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XStrConcat: 943d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Code = BinOpInit::STRCONCAT; 94477f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = StringRecTy::get(); 945d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene break; 946d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 947c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 948d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::l_paren) { 949d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected '(' after binary operator"); 950d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 951d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 952d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the '(' 953d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 95405bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init*, 2> InitList; 955c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 9568d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.push_back(ParseValue(CurRec)); 9578d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.back() == 0) return 0; 958d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 9598d978a75b477c0e4861d937096518eebba639e72Chris Lattner while (Lex.getCode() == tgtok::comma) { 9608d978a75b477c0e4861d937096518eebba639e72Chris Lattner Lex.Lex(); // eat the ',' 96121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 9628d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.push_back(ParseValue(CurRec)); 9638d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.back() == 0) return 0; 9648d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 965d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 966d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::r_paren) { 9678d978a75b477c0e4861d937096518eebba639e72Chris Lattner TokError("expected ')' in operator"); 968d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 969d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 970d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the ')' 9718d978a75b477c0e4861d937096518eebba639e72Chris Lattner 9728d978a75b477c0e4861d937096518eebba639e72Chris Lattner // We allow multiple operands to associative operators like !strconcat as 9738d978a75b477c0e4861d937096518eebba639e72Chris Lattner // shorthand for nesting them. 9748d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (Code == BinOpInit::STRCONCAT) { 9758d978a75b477c0e4861d937096518eebba639e72Chris Lattner while (InitList.size() > 2) { 97605bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *RHS = InitList.pop_back_val(); 977dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene RHS = (BinOpInit::get(Code, InitList.back(), RHS, Type)) 978dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene ->Fold(CurRec, CurMultiClass); 9798d978a75b477c0e4861d937096518eebba639e72Chris Lattner InitList.back() = RHS; 9808d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 9818d978a75b477c0e4861d937096518eebba639e72Chris Lattner } 982c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 9838d978a75b477c0e4861d937096518eebba639e72Chris Lattner if (InitList.size() == 2) 984dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (BinOpInit::get(Code, InitList[0], InitList[1], Type)) 9858d978a75b477c0e4861d937096518eebba639e72Chris Lattner ->Fold(CurRec, CurMultiClass); 986c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 9878d978a75b477c0e4861d937096518eebba639e72Chris Lattner Error(OpLoc, "expected two operands to operator"); 9888d978a75b477c0e4861d937096518eebba639e72Chris Lattner return 0; 989d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 990d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 9919bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 992beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 9934afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 9944afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TernOpInit::TernaryOp Code; 9954afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene RecTy *Type = 0; 9964afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 9974afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene tgtok::TokKind LexCode = Lex.getCode(); 9984afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the operation 9994afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene switch (LexCode) { 1000858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 10019bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 10029bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene Code = TernOpInit::IF; 10039bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene break; 1004beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 1005beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene Code = TernOpInit::FOREACH; 1006beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene break; 10074afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: 10084afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Code = TernOpInit::SUBST; 10094afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene break; 10104afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10114afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::l_paren) { 10124afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected '(' after ternary operator"); 10134afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10144afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10154afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the '(' 10164afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 101705bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *LHS = ParseValue(CurRec); 10184afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (LHS == 0) return 0; 10194afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10204afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::comma) { 10214afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ',' in ternary operator"); 10224afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10234afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10244afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ',' 102521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 102605bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *MHS = ParseValue(CurRec); 10274afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (MHS == 0) return 0; 10284afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10294afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::comma) { 10304afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ',' in ternary operator"); 10314afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10324afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10334afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ',' 103421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 103505bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *RHS = ParseValue(CurRec); 10364afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (RHS == 0) return 0; 10374afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10384afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (Lex.getCode() != tgtok::r_paren) { 10394afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("expected ')' in binary operator"); 10404afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 10414afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 10424afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Lex.Lex(); // eat the ')' 10434afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene 10444afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene switch (LexCode) { 1045858143816d43e58b17bfd11cb1b57afbd7f0f893Craig Topper default: llvm_unreachable("Unhandled code!"); 10469bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: { 1047548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling // FIXME: The `!if' operator doesn't handle non-TypedInit well at 1048548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling // all. This can be made much more robust. 104905bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *MHSt = dynamic_cast<TypedInit*>(MHS); 105005bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *RHSt = dynamic_cast<TypedInit*>(RHS); 1051548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1052548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RecTy *MHSTy = 0; 1053548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RecTy *RHSTy = 0; 1054548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1055548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSt == 0 && RHSt == 0) { 105605bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *MHSbits = dynamic_cast<BitsInit*>(MHS); 105705bce0beee87512e52428d4b80f5a8e79a949576David Greene BitsInit *RHSbits = dynamic_cast<BitsInit*>(RHS); 1058548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1059548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSbits && RHSbits && 1060548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling MHSbits->getNumBits() == RHSbits->getNumBits()) { 106177f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = BitRecTy::get(); 1062548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling break; 1063548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else { 106405bce0beee87512e52428d4b80f5a8e79a949576David Greene BitInit *MHSbit = dynamic_cast<BitInit*>(MHS); 106505bce0beee87512e52428d4b80f5a8e79a949576David Greene BitInit *RHSbit = dynamic_cast<BitInit*>(RHS); 1066548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1067548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSbit && RHSbit) { 106877f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen Type = BitRecTy::get(); 1069548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling break; 1070548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } 1071548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } 1072548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else if (MHSt != 0 && RHSt != 0) { 1073548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling MHSTy = MHSt->getType(); 1074548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling RHSTy = RHSt->getType(); 1075548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } 1076548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1077548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (!MHSTy || !RHSTy) { 10789bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene TokError("could not get type for !if"); 10799bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene return 0; 10809bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 1081548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling 1082548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling if (MHSTy->typeIsConvertibleTo(RHSTy)) { 1083548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling Type = RHSTy; 1084548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling } else if (RHSTy->typeIsConvertibleTo(MHSTy)) { 1085548f5a0b751aafba88473e4863c2baf7741b56a5Bill Wendling Type = MHSTy; 108621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 10879bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene TokError("inconsistent types for !if"); 10889bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene return 0; 10899bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 10909bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene break; 10919bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene } 1092beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: { 109305bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *MHSt = dynamic_cast<TypedInit *>(MHS); 1094beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene if (MHSt == 0) { 1095beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene TokError("could not get type for !foreach"); 1096beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene return 0; 1097beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene } 1098beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene Type = MHSt->getType(); 1099beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene break; 1100beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene } 11014afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { 110205bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *RHSt = dynamic_cast<TypedInit *>(RHS); 11034afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene if (RHSt == 0) { 11044afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene TokError("could not get type for !subst"); 11054afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene return 0; 11064afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 11074afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene Type = RHSt->getType(); 11084afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene break; 11094afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 11104afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 1111dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return (TernOpInit::get(Code, LHS, MHS, RHS, Type))->Fold(CurRec, 111221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson CurMultiClass); 11134afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene } 1114d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1115d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene} 1116d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1117d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// ParseOperatorType - Parse a type for an operator. This returns 1118d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// null on error. 1119d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 1120d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// OperatorType ::= '<' Type '>' 1121d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene/// 1122a9ad04191cb56c42944b17980b8b2bb2afe11ab2Dan GohmanRecTy *TGParser::ParseOperatorType() { 1123d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene RecTy *Type = 0; 1124d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1125d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::less) { 1126d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1127d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1128d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1129d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the < 1130d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1131d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Type = ParseType(); 1132d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1133d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Type == 0) { 1134d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1135d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1136d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1137d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1138d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene if (Lex.getCode() != tgtok::greater) { 1139d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene TokError("expected type name for operator"); 1140d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return 0; 1141d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene } 1142d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene Lex.Lex(); // eat the > 1143d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1144d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return Type; 1145d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene} 1146d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1147d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene 1148f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseSimpleValue - Parse a tblgen value. This returns null on error. 1149f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1150f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= IDValue 1151f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= INTVAL 1152d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner/// SimpleValue ::= STRVAL+ 1153f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= CODEFRAGMENT 1154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '?' 1155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '{' ValueList '}' 1156f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= ID '<' ValueListNE '>' 1157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '[' ValueList ']' 1158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= '(' IDValue DagArgList ')' 1159f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= CONCATTOK '(' Value ',' Value ')' 1160f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SHLTOK '(' Value ',' Value ')' 1161f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SRATOK '(' Value ',' Value ')' 1162f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= SRLTOK '(' Value ',' Value ')' 1163f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// SimpleValue ::= STRCONCATTOK '(' Value ',' Value ')' 1164f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1165f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David GreeneInit *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, 1166f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene IDParseMode Mode) { 116705bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *R = 0; 1168f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 1169f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: TokError("Unknown token when parsing a value"); break; 1170d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::paste: 1171d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // This is a leading paste operation. This is deprecated but 1172d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // still exists in some .td files. Ignore it. 1173d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Lex.Lex(); // Skip '#'. 1174d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene return ParseSimpleValue(CurRec, ItemType, Mode); 1175dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene case tgtok::IntVal: R = IntInit::get(Lex.getCurIntVal()); Lex.Lex(); break; 1176d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner case tgtok::StrVal: { 1177d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner std::string Val = Lex.getCurStrVal(); 1178d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Lex.Lex(); 117921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1180da4231f134989af7dc6bd3408821ba573def27b2Jim Grosbach // Handle multiple consecutive concatenated strings. 1181d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner while (Lex.getCode() == tgtok::StrVal) { 1182d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Val += Lex.getCurStrVal(); 1183d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner Lex.Lex(); 1184d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner } 118521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1186dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = StringInit::get(Val); 1187d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner break; 1188d7a50cf28c48d44cee0aa4c5abe2dce359a10727Chris Lattner } 1189f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::CodeFragment: 11908dd6f0c8353f80de6526810899f271d539f6929cJakob Stoklund Olesen R = StringInit::get(Lex.getCurStrVal()); 1191578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner Lex.Lex(); 1192578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner break; 1193578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner case tgtok::question: 1194dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene R = UnsetInit::get(); 1195578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner Lex.Lex(); 1196578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner break; 1197f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Id: { 11981e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc NameLoc = Lex.getLoc(); 1199f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 1200f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::less) // consume the Id. 1201f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene return ParseIDValue(CurRec, Name, NameLoc, Mode); // Value ::= IDValue 120221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1203f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Value ::= ID '<' ValueListNE '>' 1204f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() == tgtok::greater) { 1205f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected non-empty value list"); 1206f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1207f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1208e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1209f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // This is a CLASS<initvalslist> expression. This is supposed to synthesize 1210f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // a new anonymous definition, deriving from CLASS<initvalslist> with no 1211f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // body. 1212f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *Class = Records.getClass(Name); 1213f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!Class) { 1214f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(NameLoc, "Expected a class name, got '" + Name + "'"); 1215f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1216f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1217e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 121805bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> ValueList = ParseValueList(CurRec, Class); 1219e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ValueList.empty()) return 0; 122021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1221e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() != tgtok::greater) { 1222e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected '>' at end of value list"); 1223e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1224e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1225e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '>' 122621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1227f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Create the new record, set it as CurRec temporarily. 1228f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner static unsigned AnonCounter = 0; 12299c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner Record *NewRec = new Record("anonymous.val."+utostr(AnonCounter++), 12309c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner NameLoc, 12319c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner Records); 1232f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference SCRef; 1233f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.RefLoc = NameLoc; 1234f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.Rec = Class; 1235f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SCRef.TemplateArgs = ValueList; 1236f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add info about the subclass to NewRec. 1237f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddSubClass(NewRec, SCRef)) 1238f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1239f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner NewRec->resolveReferences(); 1240f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addDef(NewRec); 124121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1242f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // The result of the expression is a reference to the new record. 124377f8274c7d4bfb5e2a449eb49dc78dcae37e5457Jakob Stoklund Olesen return DefInit::get(NewRec); 124421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1245f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_brace: { // Value ::= '{' ValueList '}' 12461e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc BraceLoc = Lex.getLoc(); 1247f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 124805bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Vals; 124921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1250f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1251f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Vals = ParseValueList(CurRec); 1252f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Vals.empty()) return 0; 1253f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1254f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1255f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit list value"); 1256f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1257f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1258f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '}' 125921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 126005bce0beee87512e52428d4b80f5a8e79a949576David Greene SmallVector<Init *, 16> NewBits(Vals.size()); 1261ca7fd3de360b266783438666054dba198ff77ba2David Greene 1262f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = Vals.size(); i != e; ++i) { 126305bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Bit = Vals[i]->convertInitializerTo(BitRecTy::get()); 1264f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Bit == 0) { 12655d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner Error(BraceLoc, "Element #" + utostr(i) + " (" + Vals[i]->getAsString()+ 12665d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner ") is not convertable to a bit"); 1267f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1268f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1269ca7fd3de360b266783438666054dba198ff77ba2David Greene NewBits[Vals.size()-i-1] = Bit; 1270f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1271dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return BitsInit::get(NewBits); 1272f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1273f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_square: { // Value ::= '[' ValueList ']' 1274f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '[' 127505bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Vals; 127621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1277e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *DeducedEltTy = 0; 1278e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ListRecTy *GivenListTy = 0; 127921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1280e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ItemType != 0) { 1281e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ListRecTy *ListType = dynamic_cast<ListRecTy*>(ItemType); 1282e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ListType == 0) { 1283e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene std::stringstream s; 128421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson s << "Type mismatch for list, expected list type, got " 1285e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene << ItemType->getAsString(); 1286e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError(s.str()); 12876a44adade20bdea80c76b68833ec5eff5baaba98Jim Grosbach return 0; 1288e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1289e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene GivenListTy = ListType; 129021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1291e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1292f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 129321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Vals = ParseValueList(CurRec, 0, 129421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson GivenListTy ? GivenListTy->getElementType() : 0); 1295f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Vals.empty()) return 0; 1296f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1297f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 1298f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ']' at end of list value"); 1299f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1300f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1301f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ']' 1302e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1303e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *GivenEltTy = 0; 1304e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() == tgtok::less) { 1305e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Optional list element type 1306e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '<' 1307e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1308e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene GivenEltTy = ParseType(); 1309e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenEltTy == 0) { 1310e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Couldn't parse element type 1311e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1312e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1313e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1314e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Lex.getCode() != tgtok::greater) { 1315e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("expected '>' at end of list element type"); 1316e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1317e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1318e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Lex.Lex(); // eat the '>' 1319e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1320e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1321e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Check elements 1322e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *EltTy = 0; 132305bce0beee87512e52428d4b80f5a8e79a949576David Greene for (std::vector<Init *>::iterator i = Vals.begin(), ie = Vals.end(); 1324e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene i != ie; 1325e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++i) { 132605bce0beee87512e52428d4b80f5a8e79a949576David Greene TypedInit *TArg = dynamic_cast<TypedInit*>(*i); 1327e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (TArg == 0) { 1328e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Untyped list element"); 1329e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1330e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1331e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy != 0) { 1332e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = resolveTypes(EltTy, TArg->getType()); 1333e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy == 0) { 1334e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Incompatible types in list elements"); 1335e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1336e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 133721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1338e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = TArg->getType(); 1339e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1340e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1341e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1342e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenEltTy != 0) { 1343e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy != 0) { 1344e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Verify consistency 1345e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (!EltTy->typeIsConvertibleTo(GivenEltTy)) { 1346e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Incompatible types in list elements"); 1347e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1348e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1349e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1350e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene EltTy = GivenEltTy; 1351e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1352e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1353e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (EltTy == 0) { 1354e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ItemType == 0) { 1355e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("No type for list"); 1356e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1357e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1358e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene DeducedEltTy = GivenListTy->getElementType(); 135921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 1360e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene // Make sure the deduced type is compatible with the given type 1361e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (GivenListTy) { 1362e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (!EltTy->typeIsConvertibleTo(GivenListTy->getElementType())) { 1363e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene TokError("Element type mismatch for list"); 1364e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return 0; 1365e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1366e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1367e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene DeducedEltTy = EltTy; 1368e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 136921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1370dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return ListInit::get(Vals, DeducedEltTy); 1371f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1372f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_paren: { // Value ::= '(' IDValue DagArgList ')' 1373f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '(' 1374c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner if (Lex.getCode() != tgtok::Id && Lex.getCode() != tgtok::XCast) { 13753dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner TokError("expected identifier in dag init"); 13763dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner return 0; 13773dc2e96eec566f8c4e88a34296d6f10b840d7d1eChris Lattner } 137821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 137905bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Operator = ParseValue(CurRec); 1380578bcf0e48ab5858eeb8870839fa2946b6edd4b4Chris Lattner if (Operator == 0) return 0; 1381c7cafcd815519b06318629b424abe746437e1389David Greene 13827cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman // If the operator name is present, parse it. 13837cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman std::string OperatorName; 13847cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman if (Lex.getCode() == tgtok::colon) { 13857cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman if (Lex.Lex() != tgtok::VarName) { // eat the ':' 13867cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman TokError("expected variable name in dag operator"); 13877cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman return 0; 13887cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman } 13897cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman OperatorName = Lex.getCurStrVal(); 13907cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman Lex.Lex(); // eat the VarName. 13917cee81703de8333d457ec9e560b0537b71df5a48Nate Begeman } 139221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 139305bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<std::pair<llvm::Init*, std::string> > DagArgs; 1394f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_paren) { 1395f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner DagArgs = ParseDagArgList(CurRec); 1396f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (DagArgs.empty()) return 0; 1397f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 139821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1399f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_paren) { 1400f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ')' in dag init"); 1401f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1402f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1403f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ')' 140421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1405dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene return DagInit::get(Operator, OperatorName, DagArgs); 1406f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 140721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 14081434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XHead: 14091434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XTail: 14101434f66b2e132a707e2c8ccb3350ea13fb5aa051David Greene case tgtok::XEmpty: 1411e6c27de069225e1122c78385ad22a2ff656db8e6David Greene case tgtok::XCast: // Value ::= !unop '(' Value ')' 1412f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XConcat: 141321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson case tgtok::XSRA: 1414f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XSRL: 1415f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::XSHL: 14166786d5e18387465d6106eaef0b7923526ef0bc10David Greene case tgtok::XEq: 1417c7252ce74324e44efa7ec2506035a8e2d2e93d2fChris Lattner case tgtok::XStrConcat: // Value ::= !binop '(' Value ',' Value ')' 14189bea7c85d71bbb192f686a56e9b5bf32ae3389e6David Greene case tgtok::XIf: 1419beb31a51f67f651c5fa3c5094a78266d04a697a5David Greene case tgtok::XForEach: 14204afc509b7ffe2c4ea234dfd7af5105feb21685d9David Greene case tgtok::XSubst: { // Value ::= !ternop '(' Value ',' Value ',' Value ')' 1421d418c1b768b5ff26afe05a5ce84d920bbbc99583David Greene return ParseOperation(CurRec); 1422f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1423f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 142421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1425f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return R; 1426f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1427f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1428f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseValue - Parse a tblgen value. This returns null on error. 1429f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1430f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Value ::= SimpleValue ValueSuffix* 1431f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '{' BitList '}' 1432f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '[' BitList ']' 1433f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueSuffix ::= '.' ID 1434f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1435f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David GreeneInit *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) { 1436f3744a0cf9f622e0879a80c1fdcb0f6072e5a6c3David Greene Init *Result = ParseSimpleValue(CurRec, ItemType, Mode); 1437f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) return 0; 143821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1439f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the suffixes now if present. 1440f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1441f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 1442f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner default: return Result; 1443f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_brace: { 1444cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Mode == ParseNameMode || Mode == ParseForeachMode) 14458592b2b2a3dde4e5c8f00e855497f760ae94272fDavid Greene // This is the beginning of the object body. 14468592b2b2a3dde4e5c8f00e855497f760ae94272fDavid Greene return Result; 14478592b2b2a3dde4e5c8f00e855497f760ae94272fDavid Greene 14481e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc CurlyLoc = Lex.getLoc(); 1449f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{' 1450f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Ranges = ParseRangeList(); 1451f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return 0; 145221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1453f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Reverse the bitlist. 1454f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(Ranges.begin(), Ranges.end()); 1455f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result = Result->convertInitializerBitRange(Ranges); 1456f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) { 1457f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(CurlyLoc, "Invalid bit range for value"); 1458f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1459f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 146021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1461f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '}'. 1462f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 1463f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of bit range list"); 1464f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1465f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1466f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1467f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1468f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1469f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::l_square: { 14701e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc SquareLoc = Lex.getLoc(); 1471f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '[' 1472f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Ranges = ParseRangeList(); 1473f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Ranges.empty()) return 0; 147421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1475f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result = Result->convertInitListSlice(Ranges); 1476f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Result == 0) { 1477f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Error(SquareLoc, "Invalid range for list slice"); 1478f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1479f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 148021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1481f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the ']'. 1482f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_square) { 1483f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected ']' at end of list slice"); 1484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1485f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1487f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1489f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::period: 1490f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::Id) { // eat the . 1491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected field identifier after '.'"); 1492f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1493f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1494f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (!Result->getFieldType(Lex.getCurStrVal())) { 1495f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Cannot access field '" + Lex.getCurStrVal() + "' of value '" + 14965d814864133fd3be4414a043341508bcc2caa7b5Chris Lattner Result->getAsString() + "'"); 1497f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return 0; 1498f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1499dcd35c797d458d8b1dbc36cf7f1504166d5b2f16David Greene Result = FieldInit::get(Result, Lex.getCurStrVal()); 1500f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat field name 1501f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner break; 1502d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1503d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::paste: 1504d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene SMLoc PasteLoc = Lex.getLoc(); 1505d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1506d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // Create a !strconcat() operation, first casting each operand to 1507d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // a string if necessary. 1508d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1509d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene TypedInit *LHS = dynamic_cast<TypedInit *>(Result); 1510d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (!LHS) { 1511d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Error(PasteLoc, "LHS of paste is not typed!"); 1512d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene return 0; 1513d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1514d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1515d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (LHS->getType() != StringRecTy::get()) { 1516d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene LHS = UnOpInit::get(UnOpInit::CAST, LHS, StringRecTy::get()); 1517d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1518d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1519d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene TypedInit *RHS = 0; 1520d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1521d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Lex.Lex(); // Eat the '#'. 1522d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene switch (Lex.getCode()) { 1523d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::colon: 1524d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::semi: 1525d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene case tgtok::l_brace: 1526d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // These are all of the tokens that can begin an object body. 1527d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // Some of these can also begin values but we disallow those cases 1528d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // because they are unlikely to be useful. 1529d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1530d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // Trailing paste, concat with an empty string. 1531d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene RHS = StringInit::get(""); 1532d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene break; 1533d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1534d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene default: 1535d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Init *RHSResult = ParseValue(CurRec, ItemType, ParseNameMode); 1536d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene RHS = dynamic_cast<TypedInit *>(RHSResult); 1537d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (!RHS) { 1538d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Error(PasteLoc, "RHS of paste is not typed!"); 1539d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene return 0; 1540d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1541d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1542d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (RHS->getType() != StringRecTy::get()) { 1543d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene RHS = UnOpInit::get(UnOpInit::CAST, RHS, StringRecTy::get()); 1544d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1545d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1546d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene break; 1547d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene } 1548d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene 1549d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene Result = BinOpInit::get(BinOpInit::STRCONCAT, LHS, RHS, 1550d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene StringRecTy::get())->Fold(CurRec, CurMultiClass); 1551d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene break; 1552f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1553f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1554f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1555f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1556f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList - Parse the argument list for a dag literal expression. 1557f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1558f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList ::= Value (':' VARNAME)? 1559f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDagArgList ::= ParseDagArgList ',' Value (':' VARNAME)? 156005bce0beee87512e52428d4b80f5a8e79a949576David Greenestd::vector<std::pair<llvm::Init*, std::string> > 1561f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris LattnerTGParser::ParseDagArgList(Record *CurRec) { 156205bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<std::pair<llvm::Init*, std::string> > Result; 156321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1564f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 156505bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec); 156605bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Val == 0) return std::vector<std::pair<llvm::Init*, std::string> >(); 156721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1568f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If the variable name is present, add it. 1569f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string VarName; 1570f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::colon) { 1571f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::VarName) { // eat the ':' 1572f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected variable name in dag literal"); 157305bce0beee87512e52428d4b80f5a8e79a949576David Greene return std::vector<std::pair<llvm::Init*, std::string> >(); 1574f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1575f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner VarName = Lex.getCurStrVal(); 1576f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the VarName. 1577f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 157821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1579f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.push_back(std::make_pair(Val, VarName)); 158021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1581f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) break; 158221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the ',' 1583f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 158421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1585f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 1586f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1587f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1588f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1589f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseValueList - Parse a comma separated list of values, returning them as a 1590f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// vector. Note that this always expects to be able to parse at least one 1591f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// value. It returns an empty list if this is not possible. 1592f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1593f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ValueList ::= Value (',' Value) 1594f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 159505bce0beee87512e52428d4b80f5a8e79a949576David Greenestd::vector<Init*> TGParser::ParseValueList(Record *CurRec, Record *ArgsRec, 1596d568b3f55294917d1cc701da14a8a7daeb6563e6Eric Christopher RecTy *EltTy) { 159705bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> Result; 1598e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *ItemType = EltTy; 159967acdf2977595a1d9755bf919d678f80ee65364fDavid Greene unsigned int ArgN = 0; 1600e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ArgsRec != 0 && EltTy == 0) { 1601e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs(); 1602b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach if (!TArgs.size()) { 1603b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach TokError("template argument provided to non-template class"); 1604b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach return std::vector<Init*>(); 1605b1320cb38158aedd5b59e0e3649ce5d1e90c9776Jim Grosbach } 1606e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1607d9746fe58ace4a8625983da1ecf68c0564883616David Greene if (!RV) { 1608d9746fe58ace4a8625983da1ecf68c0564883616David Greene errs() << "Cannot find template arg " << ArgN << " (" << TArgs[ArgN] 1609d9746fe58ace4a8625983da1ecf68c0564883616David Greene << ")\n"; 1610d9746fe58ace4a8625983da1ecf68c0564883616David Greene } 1611e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene assert(RV && "Template argument record not found??"); 1612e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ItemType = RV->getType(); 1613e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++ArgN; 1614e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1615e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.push_back(ParseValue(CurRec, ItemType)); 161605bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Result.back() == 0) return std::vector<Init*>(); 161721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1618f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 1619f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the comma 162021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1621e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (ArgsRec != 0 && EltTy == 0) { 1622e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = ArgsRec->getTemplateArgs(); 162367acdf2977595a1d9755bf919d678f80ee65364fDavid Greene if (ArgN >= TArgs.size()) { 162467acdf2977595a1d9755bf919d678f80ee65364fDavid Greene TokError("too many template arguments"); 162505bce0beee87512e52428d4b80f5a8e79a949576David Greene return std::vector<Init*>(); 162621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } 1627e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene const RecordVal *RV = ArgsRec->getValue(TArgs[ArgN]); 1628e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene assert(RV && "Template argument record not found??"); 1629e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ItemType = RV->getType(); 1630e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene ++ArgN; 1631e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene } 1632e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene Result.push_back(ParseValue(CurRec, ItemType)); 163305bce0beee87512e52428d4b80f5a8e79a949576David Greene if (Result.back() == 0) return std::vector<Init*>(); 1634f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 163521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1636f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 1637f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1638f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1639f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1640f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDeclaration - Read a declaration, returning the name of field ID, or an 1641f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// empty string on error. This can happen in a number of different context's, 1642f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// including within a def or in the template args for a def (which which case 1643f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// CurRec will be non-null) and within the template args for a multiclass (in 1644f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// which case CurRec will be null, but CurMultiClass will be set). This can 1645f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// also happen within a def that is within a multiclass, which will set both 1646f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// CurRec and CurMultiClass. 1647f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1648f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Declaration ::= FIELD? Type ID ('=' Value)? 1649f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1650e22b321d2276b634519165b101b02d92c2fcf5c7David GreeneInit *TGParser::ParseDeclaration(Record *CurRec, 1651f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool ParsingTemplateArgs) { 1652f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the field prefix if present. 1653f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner bool HasField = Lex.getCode() == tgtok::Field; 1654f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (HasField) Lex.Lex(); 165521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1656f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner RecTy *Type = ParseType(); 1657e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (Type == 0) return 0; 165821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1659f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 1660f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("Expected identifier in declaration"); 1661e22b321d2276b634519165b101b02d92c2fcf5c7David Greene return 0; 1662f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 166321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 16641e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc IdLoc = Lex.getLoc(); 1665e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *DeclName = StringInit::get(Lex.getCurStrVal()); 1666f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 166721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1668f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParsingTemplateArgs) { 1669f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 1670e22b321d2276b634519165b101b02d92c2fcf5c7David Greene DeclName = QualifyName(*CurRec, CurMultiClass, DeclName, ":"); 1671f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 1672f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(CurMultiClass); 1673f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1674f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass) 1675e22b321d2276b634519165b101b02d92c2fcf5c7David Greene DeclName = QualifyName(CurMultiClass->Rec, CurMultiClass, DeclName, 1676e22b321d2276b634519165b101b02d92c2fcf5c7David Greene "::"); 1677f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 167821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1679f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add the value. 1680f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddValue(CurRec, IdLoc, RecordVal(DeclName, Type, HasField))) 1681e22b321d2276b634519165b101b02d92c2fcf5c7David Greene return 0; 168221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1683f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If a value is present, parse it. 1684f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::equal) { 1685f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 16861e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc ValLoc = Lex.getLoc(); 168705bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec, Type); 1688f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0 || 1689f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SetValue(CurRec, ValLoc, DeclName, std::vector<unsigned>(), Val)) 1690e22b321d2276b634519165b101b02d92c2fcf5c7David Greene return 0; 1691f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 169221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1693f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return DeclName; 1694f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1695f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1696cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ParseForeachDeclaration - Read a foreach declaration, returning 1697cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// the name of the declared object or a NULL Init on error. Return 1698cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// the name of the parsed initializer list through ForeachListName. 1699cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 1700cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ForeachDeclaration ::= ID '=' Value 1701cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 17028e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund OlesenVarInit *TGParser::ParseForeachDeclaration(ListInit *&ForeachListValue) { 1703cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::Id) { 1704cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("Expected identifier in foreach declaration"); 1705cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return 0; 1706cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1707cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1708cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Init *DeclName = StringInit::get(Lex.getCurStrVal()); 1709cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); 1710cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1711cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // If a value is present, parse it. 1712cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::equal) { 1713cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("Expected '=' in foreach declaration"); 1714cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return 0; 1715cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1716cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the '=' 1717cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1718cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Expect a list initializer. 17198e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen Init *List = ParseSimpleValue(0, 0, ParseForeachMode); 1720cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 17218e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen ForeachListValue = dynamic_cast<ListInit*>(List); 17228e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (ForeachListValue == 0) { 17238e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen TokError("Expected a Value list"); 1724cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return 0; 1725cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1726cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 17278e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen RecTy *ValueType = ForeachListValue->getType(); 1728cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene ListRecTy *ListType = dynamic_cast<ListRecTy *>(ValueType); 1729cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (ListType == 0) { 1730cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("Value list is not of list type"); 1731cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return 0; 1732cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1733cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1734cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene RecTy *IterType = ListType->getElementType(); 1735cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene VarInit *IterVar = VarInit::get(DeclName, IterType); 1736cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1737cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return IterVar; 1738cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 1739cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1740f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseTemplateArgList - Read a template argument list, which is a non-empty 1741f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// sequence of template-declarations in <>'s. If CurRec is non-null, these are 1742f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// template args for a def, which may or may not be in a multiclass. If null, 1743f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// these are the template args for a multiclass. 1744f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1745f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// TemplateArgList ::= '<' Declaration (',' Declaration)* '>' 174621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson/// 1747f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseTemplateArgList(Record *CurRec) { 1748f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::less && "Not a template arg list!"); 1749f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '<' 175021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1751f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *TheRecToAddTo = CurRec ? CurRec : &CurMultiClass->Rec; 175221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1753f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the first declaration. 1754e22b321d2276b634519165b101b02d92c2fcf5c7David Greene Init *TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1755e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (TemplArg == 0) 1756f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 175721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1758f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TheRecToAddTo->addTemplateArg(TemplArg); 175921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1760f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() == tgtok::comma) { 1761f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the ',' 176221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1763f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read the following declarations. 1764f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); 1765e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (TemplArg == 0) 1766f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1767f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TheRecToAddTo->addTemplateArg(TemplArg); 1768f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 176921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1770f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::greater) 1771f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected '>' at end of template argument list"); 1772f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '>'. 1773f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1774f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1775f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1776f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1777f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseBodyItem - Parse a single item at within the body of a def or class. 1778f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1779f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyItem ::= Declaration ';' 1780f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyItem ::= LET ID OptionalBitList '=' Value ';' 1781f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseBodyItem(Record *CurRec) { 1782f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Let) { 1783e22b321d2276b634519165b101b02d92c2fcf5c7David Greene if (ParseDeclaration(CurRec, false) == 0) 1784f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 178521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1786f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::semi) 1787f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ';' after declaration"); 1788f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1789f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1790f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1791f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1792f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // LET ID OptionalRangeList '=' Value ';' 1793f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.Lex() != tgtok::Id) 1794f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected field identifier after let"); 179521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 17961e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc IdLoc = Lex.getLoc(); 1797f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string FieldName = Lex.getCurStrVal(); 1798f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the field name. 179921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1800f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> BitList; 180121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseOptionalBitList(BitList)) 1802f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1803f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(BitList.begin(), BitList.end()); 180421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1805f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::equal) 1806f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected '=' in let expression"); 1807f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '='. 180821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1809e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecordVal *Field = CurRec->getValue(FieldName); 1810e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene if (Field == 0) 1811e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene return TokError("Value '" + FieldName + "' unknown!"); 1812e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene 1813e1b469170bb3ab259fd488fb95c29efe802cdd6aDavid Greene RecTy *Type = Field->getType(); 181421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 181505bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(CurRec, Type); 1816f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0) return true; 181721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1818f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::semi) 1819f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ';' after let expression"); 1820f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 182121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1822f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return SetValue(CurRec, IdLoc, FieldName, BitList, Val); 1823f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1824f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1825f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseBody - Read the body of a class or def. Return true on error, false on 1826f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// success. 1827f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1828f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Body ::= ';' 1829f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Body ::= '{' BodyList '}' 1830f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BodyList BodyItem* 1831f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1832f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseBody(Record *CurRec) { 1833f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is a null definition, just eat the semi and return. 1834f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::semi) { 1835f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1836f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1837f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 183821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1839f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) 1840f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Expected ';' or '{' to start body"); 1841f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '{'. 1842f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 184321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1844f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (Lex.getCode() != tgtok::r_brace) 1845f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseBodyItem(CurRec)) 1846f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 1847f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1848f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Eat the '}'. 1849f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 1850f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 1851f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1852f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1853f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectBody - Parse the body of a def or class. This consists of an 1854f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// optional ClassList followed by a Body. CurRec is the current def or class 1855f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// that is being parsed. 1856f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1857f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectBody ::= BaseClassList Body 1858f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassList ::= /*empty*/ 1859f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassList ::= ':' BaseClassListNE 1860f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// BaseClassListNE ::= SubClassRef (',' SubClassRef)* 1861f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1862f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseObjectBody(Record *CurRec) { 1863f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there is a baseclass list, read it. 1864f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::colon) { 1865f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 186621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1867f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Read all of the subclasses. 1868f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference SubClass = ParseSubClassReference(CurRec, false); 1869f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 1870f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Check for error. 1871f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SubClass.Rec == 0) return true; 187221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1873f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add it. 1874f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (AddSubClass(CurRec, SubClass)) 1875f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 187621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1877f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) break; 1878f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat ','. 1879f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClass = ParseSubClassReference(CurRec, false); 1880f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1881f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1882f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1883f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Process any variables on the let stack. 1884f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 1885f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 1886f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 1887f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack[i][j].Bits, LetStack[i][j].Value)) 1888f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 188921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1890f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseBody(CurRec); 1891f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 1892f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1893f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDef - Parse and return a top level or multiclass def, return the record 1894f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// corresponding to it. This returns null on error. 1895f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1896f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// DefInst ::= DEF ObjectName ObjectBody 1897f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 1898ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseDef(MultiClass *CurMultiClass) { 18991e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc DefLoc = Lex.getLoc(); 1900f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Def && "Unknown tok"); 190121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // Eat the 'def' token. 1902f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 1903f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse ObjectName and make a record for it. 1904a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Record *CurRec = new Record(ParseObjectName(CurMultiClass), DefLoc, Records); 190521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 190672cba6cdf640411e2fb6207858a0abd87c4286fcJakob Stoklund Olesen if (!CurMultiClass && Loops.empty()) { 1907f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Top-level def definition. 190821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1909f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Ensure redefinition doesn't happen. 19101d5013992f8ecf5cc670ab8a1a599db4722c2f5dDavid Greene if (Records.getDef(CurRec->getNameInitAsString())) { 19112c49fbb32c5f042ecf64ac415f1a628100951a44David Greene Error(DefLoc, "def '" + CurRec->getNameInitAsString() 19122c49fbb32c5f042ecf64ac415f1a628100951a44David Greene + "' already defined"); 1913ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1914f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1915f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addDef(CurRec); 191672cba6cdf640411e2fb6207858a0abd87c4286fcJakob Stoklund Olesen } else if (CurMultiClass) { 1917f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Otherwise, a def inside a multiclass, add it to the multiclass. 1918f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); i != e; ++i) 191991919cd8166bb60145efe54dd790b98521b4328aDavid Greene if (CurMultiClass->DefPrototypes[i]->getNameInit() 192091919cd8166bb60145efe54dd790b98521b4328aDavid Greene == CurRec->getNameInit()) { 192191919cd8166bb60145efe54dd790b98521b4328aDavid Greene Error(DefLoc, "def '" + CurRec->getNameInitAsString() + 1922f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner "' already defined in this multiclass!"); 1923ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 1924f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 1925f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurMultiClass->DefPrototypes.push_back(CurRec); 1926f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 192721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1928f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseObjectBody(CurRec)) 1929ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 193021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1931f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurMultiClass == 0) // Def's in multiclasses aren't really defs. 19320d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // See Record::setName(). This resolve step will see any new name 19330d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // for the def that might have been created when resolving 19340d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // inheritance, values and arguments above. 1935f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurRec->resolveReferences(); 193621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 1937f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If ObjectBody has template arguments, it's an error. 1938f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(CurRec->getTemplateArgs().empty() && "How'd this get template args?"); 1939ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1940ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (CurMultiClass) { 1941ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes // Copy the template arguments for the multiclass into the def. 1942e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = 1943ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes CurMultiClass->Rec.getTemplateArgs(); 1944ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 1945ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 1946ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes const RecordVal *RV = CurMultiClass->Rec.getValue(TArgs[i]); 1947ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes assert(RV && "Template arg doesn't exist?"); 1948ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes CurRec->addValue(*RV); 1949ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 1950ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 1951ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes 19528e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen if (ProcessForeachDefs(CurRec, DefLoc)) { 1953cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Error(DefLoc, 1954cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene "Could not process loops for def" + CurRec->getNameInitAsString()); 1955cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return true; 1956cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1957cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1958cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return false; 1959cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene} 1960cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1961cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// ParseForeach - Parse a for statement. Return the record corresponding 1962cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// to it. This returns true on error. 1963cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 1964cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// Foreach ::= FOREACH Declaration IN '{ ObjectList '}' 1965cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// Foreach ::= FOREACH Declaration IN Object 1966cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene/// 1967cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greenebool TGParser::ParseForeach(MultiClass *CurMultiClass) { 1968cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene assert(Lex.getCode() == tgtok::Foreach && "Unknown tok"); 1969cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the 'for' token. 1970cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1971cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Make a temporary object to record items associated with the for 1972cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // loop. 19738e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen ListInit *ListValue = 0; 19748e5286e18ffbe4716ef92cd1de8901942d685e1bJakob Stoklund Olesen VarInit *IterName = ParseForeachDeclaration(ListValue); 1975cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (IterName == 0) 1976cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return TokError("expected declaration in for"); 1977cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1978cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::In) 1979cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return TokError("Unknown tok"); 1980cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the in 1981cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1982cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Create a loop object and remember it. 1983cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Loops.push_back(ForeachLoop(IterName, ListValue)); 1984cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1985cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::l_brace) { 1986cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // FOREACH Declaration IN Object 1987cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (ParseObject(CurMultiClass)) 1988cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return true; 1989cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 1990cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene else { 1991cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene SMLoc BraceLoc = Lex.getLoc(); 1992cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Otherwise, this is a group foreach. 1993cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // eat the '{'. 1994cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1995cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // Parse the object list. 1996cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (ParseObjectList(CurMultiClass)) 1997cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return true; 1998cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 1999cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene if (Lex.getCode() != tgtok::r_brace) { 2000cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene TokError("expected '}' at end of foreach command"); 2001cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene return Error(BraceLoc, "to match this '{'"); 2002cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 2003cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Lex.Lex(); // Eat the } 2004cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene } 2005cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2006cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene // We've processed everything in this loop. 2007cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene Loops.pop_back(); 2008cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene 2009ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return false; 2010f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2011f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2012f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseClass - Parse a tblgen class definition. 2013f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2014f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ClassInst ::= CLASS ID TemplateArgList? ObjectBody 2015f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2016f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseClass() { 2017f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Class && "Unexpected token!"); 2018f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 201921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2020f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) 2021f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected class name after 'class' keyword"); 202221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2023f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Record *CurRec = Records.getClass(Lex.getCurStrVal()); 2024f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (CurRec) { 2025f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If the body was previously defined, this is an error. 2026e338565757bfcfe9d762751c976684f66954fb45David Greene if (CurRec->getValues().size() > 1 || // Account for NAME. 2027f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner !CurRec->getSuperClasses().empty() || 2028f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner !CurRec->getTemplateArgs().empty()) 202969a2394b2dd0136581f1485d69669246fc3b62c5David Greene return TokError("Class '" + CurRec->getNameInitAsString() 203069a2394b2dd0136581f1485d69669246fc3b62c5David Greene + "' already defined"); 2031f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { 2032f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is the first reference to this class, create and add it. 20339c6b60eb28d2717008f8d6ff52f7666ebc81113dChris Lattner CurRec = new Record(Lex.getCurStrVal(), Lex.getLoc(), Records); 2034f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Records.addClass(CurRec); 2035f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2036f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the name. 203721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2038f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there are template args, parse them. 2039f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::less) 2040f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseTemplateArgList(CurRec)) 2041f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2042f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2043f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Finally, parse the object body. 2044f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return ParseObjectBody(CurRec); 2045f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2046f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2047f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseLetList - Parse a non-empty list of assignment expressions into a list 2048f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// of LetRecords. 2049f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2050f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// LetList ::= LetItem (',' LetItem)* 2051f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// LetItem ::= ID OptionalRangeList '=' Value 2052f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2053f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerstd::vector<LetRecord> TGParser::ParseLetList() { 2054f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<LetRecord> Result; 205521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2056f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (1) { 2057f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) { 2058f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected identifier in let definition"); 2059f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 2060f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2061f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 20621e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc NameLoc = Lex.getLoc(); 206321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // Eat the identifier. 2064f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2065f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Check for an optional RangeList. 2066f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<unsigned> Bits; 206721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson if (ParseOptionalRangeList(Bits)) 2068f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 2069f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::reverse(Bits.begin(), Bits.end()); 207021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2071f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::equal) { 2072f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '=' in let expression"); 2073f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return std::vector<LetRecord>(); 2074f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2075f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '='. 207621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 207705bce0beee87512e52428d4b80f5a8e79a949576David Greene Init *Val = ParseValue(0); 2078f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Val == 0) return std::vector<LetRecord>(); 207921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2080f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Now that we have everything, add the record. 2081f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Result.push_back(LetRecord(Name, Bits, Val, NameLoc)); 208221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2083f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::comma) 2084f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Result; 208521870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the comma. 2086f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2087f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2088f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2089f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseTopLevelLet - Parse a 'let' at top level. This can be a couple of 2090ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes/// different related productions. This works inside multiclasses too. 2091f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2092f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LET LetList IN '{' ObjectList '}' 2093f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LET LetList IN Object 2094f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2095ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseTopLevelLet(MultiClass *CurMultiClass) { 2096f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Let && "Unexpected token"); 2097f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 209821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2099f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Add this entry to the let stack. 2100f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::vector<LetRecord> LetInfo = ParseLetList(); 2101f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (LetInfo.empty()) return true; 2102f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack.push_back(LetInfo); 2103f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2104f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::In) 2105f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected 'in' at end of top-level 'let'"); 2106f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 210721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2108f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If this is a scalar let, just handle it now 2109f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::l_brace) { 2110f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // LET LetList IN Object 2111ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(CurMultiClass)) 2112f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2113f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } else { // Object ::= LETCommand '{' ObjectList '}' 21141e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc BraceLoc = Lex.getLoc(); 2115f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Otherwise, this is a group let. 2116f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // eat the '{'. 211721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2118f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Parse the object list. 2119ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObjectList(CurMultiClass)) 2120f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 212121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2122f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::r_brace) { 2123f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner TokError("expected '}' at end of top level let command"); 2124f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return Error(BraceLoc, "to match this '{'"); 2125f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2126f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 2127f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 212821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2129f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // Outside this let scope, this let block is not active. 2130f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner LetStack.pop_back(); 2131f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2132f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2133f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2134f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseMultiClass - Parse a multiclass definition. 2135f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 213632558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// MultiClassInst ::= MULTICLASS ID TemplateArgList? 213732558650ae09247b31aa89cdee64e8c849771024Bob Wilson/// ':' BaseMultiClassList '{' MultiClassDef+ '}' 2138f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2139f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseMultiClass() { 2140f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::MultiClass && "Unexpected token"); 2141f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the multiclass token. 2142f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2143f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() != tgtok::Id) 2144f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected identifier after multiclass for name"); 2145f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner std::string Name = Lex.getCurStrVal(); 214621870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2147f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (MultiClasses.count(Name)) 2148f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("multiclass '" + Name + "' already defined"); 214921870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 215067db883487fca3472fdde51e931657e22d4d0495Chris Lattner CurMultiClass = MultiClasses[Name] = new MultiClass(Name, 215167db883487fca3472fdde51e931657e22d4d0495Chris Lattner Lex.getLoc(), Records); 2152f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Eat the identifier. 215321870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2154f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If there are template args, parse them. 2155f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::less) 2156f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseTemplateArgList(0)) 2157f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2158f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2159d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene bool inherits = false; 2160d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 2161de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // If there are submulticlasses, parse them. 2162de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() == tgtok::colon) { 2163d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene inherits = true; 2164d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene 2165de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); 216632558650ae09247b31aa89cdee64e8c849771024Bob Wilson 2167de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Read all of the submulticlasses. 216832558650ae09247b31aa89cdee64e8c849771024Bob Wilson SubMultiClassReference SubMultiClass = 216932558650ae09247b31aa89cdee64e8c849771024Bob Wilson ParseSubMultiClassReference(CurMultiClass); 2170de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene while (1) { 2171de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Check for error. 2172de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (SubMultiClass.MC == 0) return true; 217332558650ae09247b31aa89cdee64e8c849771024Bob Wilson 2174de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene // Add it. 2175de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (AddSubMultiClass(CurMultiClass, SubMultiClass)) 2176de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene return true; 217732558650ae09247b31aa89cdee64e8c849771024Bob Wilson 2178de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene if (Lex.getCode() != tgtok::comma) break; 2179de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene Lex.Lex(); // eat ','. 2180de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene SubMultiClass = ParseSubMultiClassReference(CurMultiClass); 2181de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 2182de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene } 2183de444af6bb9a8a7bb95e2a274d8fa8697e8f4e3fDavid Greene 2184d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (Lex.getCode() != tgtok::l_brace) { 2185d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (!inherits) 2186d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return TokError("expected '{' in multiclass definition"); 218721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson else if (Lex.getCode() != tgtok::semi) 218821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson return TokError("expected ';' in multiclass definition"); 2189d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene else 219021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson Lex.Lex(); // eat the ';'. 219121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson } else { 2192d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene if (Lex.Lex() == tgtok::r_brace) // eat the '{'. 2193d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene return TokError("multiclass must contain at least one def"); 219421870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2195270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes while (Lex.getCode() != tgtok::r_brace) { 2196ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes switch (Lex.getCode()) { 2197ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes default: 2198a1b1b79be15c4b79a4282f148085ebad1cf877caDavid Greene return TokError("expected 'let', 'def' or 'defm' in multiclass body"); 2199ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Let: 2200ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Def: 2201ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Defm: 2202cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene case tgtok::Foreach: 2203ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(CurMultiClass)) 2204ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes return true; 2205ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes break; 2206ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes } 2207270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes } 2208d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene Lex.Lex(); // eat the '}'. 2209d34a73b3b709001c56db68a4d9a84e5d2002b1f0David Greene } 221021870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2211f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner CurMultiClass = 0; 2212f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2213f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2214f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2215e499a2df4492dab21a50d74f3f687b989f910a2fDavid GreeneRecord *TGParser:: 2216e499a2df4492dab21a50d74f3f687b989f910a2fDavid GreeneInstantiateMulticlassDef(MultiClass &MC, 2217e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *DefProto, 22187be867e48300861b7b1bf614eb204463533d6724David Greene Init *DefmPrefix, 2219e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc) { 22207be867e48300861b7b1bf614eb204463533d6724David Greene // We need to preserve DefProto so it can be reused for later 22217be867e48300861b7b1bf614eb204463533d6724David Greene // instantiations, so create a new Record to inherit from it. 22227be867e48300861b7b1bf614eb204463533d6724David Greene 2223e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Add in the defm name. If the defm prefix is empty, give each 2224e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // instantiated def a unique name. Otherwise, if "#NAME#" exists in the 2225e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // name, substitute the prefix for #NAME#. Otherwise, use the defm name 2226e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // as a prefix. 2227e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 22287be867e48300861b7b1bf614eb204463533d6724David Greene if (DefmPrefix == 0) 22297be867e48300861b7b1bf614eb204463533d6724David Greene DefmPrefix = StringInit::get(GetNewAnonymousName()); 22307be867e48300861b7b1bf614eb204463533d6724David Greene 22317be867e48300861b7b1bf614eb204463533d6724David Greene Init *DefName = DefProto->getNameInit(); 22327be867e48300861b7b1bf614eb204463533d6724David Greene 2233d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene StringInit *DefNameString = dynamic_cast<StringInit *>(DefName); 22347be867e48300861b7b1bf614eb204463533d6724David Greene 2235d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene if (DefNameString != 0) { 2236d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // We have a fully expanded string so there are no operators to 2237d3d1cad535d1c88e13e8e082c136260ee624967fDavid Greene // resolve. We should concatenate the given prefix and name. 22387be867e48300861b7b1bf614eb204463533d6724David Greene DefName = 22397be867e48300861b7b1bf614eb204463533d6724David Greene BinOpInit::get(BinOpInit::STRCONCAT, 22407be867e48300861b7b1bf614eb204463533d6724David Greene UnOpInit::get(UnOpInit::CAST, DefmPrefix, 22417be867e48300861b7b1bf614eb204463533d6724David Greene StringRecTy::get())->Fold(DefProto, &MC), 22427be867e48300861b7b1bf614eb204463533d6724David Greene DefName, StringRecTy::get())->Fold(DefProto, &MC); 22437be867e48300861b7b1bf614eb204463533d6724David Greene } 22447be867e48300861b7b1bf614eb204463533d6724David Greene 2245e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec = new Record(DefName, DefmPrefixLoc, Records); 2246e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2247e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SubClassReference Ref; 2248e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Ref.RefLoc = DefmPrefixLoc; 2249e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Ref.Rec = DefProto; 2250e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene AddSubClass(CurRec, Ref); 2251e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2252e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene if (DefNameString == 0) { 2253e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // We must resolve references to NAME. 2254e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene if (SetValue(CurRec, Ref.RefLoc, "NAME", std::vector<unsigned>(), 2255e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene DefmPrefix)) { 2256e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene Error(DefmPrefixLoc, "Could not resolve " 2257e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene + CurRec->getNameInitAsString() + ":NAME to '" 2258e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene + DefmPrefix->getAsUnquotedString() + "'"); 2259e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene return 0; 2260e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2261e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2262e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene RecordVal *DefNameRV = CurRec->getValue("NAME"); 2263e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene CurRec->resolveReferencesTo(DefNameRV); 2264e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2265e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2266e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene if (!CurMultiClass) { 2267e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // We do this after resolving NAME because before resolution, many 2268e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // multiclass defs will have the same name expression. If we are 2269e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // currently in a multiclass, it means this defm appears inside a 2270e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // multiclass and its name won't be fully resolvable until we see 2271e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // the top-level defm. Therefore, we don't add this to the 2272e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // RecordKeeper at this point. If we did we could get duplicate 2273e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // defs as more than one probably refers to NAME or some other 2274e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // common internal placeholder. 2275e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2276e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene // Ensure redefinition doesn't happen. 2277e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene if (Records.getDef(CurRec->getNameInitAsString())) { 2278e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene Error(DefmPrefixLoc, "def '" + CurRec->getNameInitAsString() + 2279e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene "' already defined, instantiating defm with subdef '" + 2280e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene DefProto->getNameInitAsString() + "'"); 2281e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene return 0; 2282e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2283e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2284e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene Records.addDef(CurRec); 2285e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene } 2286e5b252f9fe7a3dfc85ae25ca1603cb406071851bDavid Greene 2287e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return CurRec; 2288e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2289e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2290e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greenebool TGParser::ResolveMulticlassDefArgs(MultiClass &MC, 2291e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec, 2292e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc, 2293e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc SubClassLoc, 2294e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs, 2295e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene std::vector<Init *> &TemplateVals, 2296e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene bool DeleteArgs) { 2297e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Loop over all of the template arguments, setting them to the specified 2298e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // value or leaving them as the default if necessary. 2299e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = TArgs.size(); i != e; ++i) { 2300e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Check if a value is specified for this temp-arg. 2301e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (i < TemplateVals.size()) { 2302e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Set it now. 2303e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (SetValue(CurRec, DefmPrefixLoc, TArgs[i], std::vector<unsigned>(), 2304e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene TemplateVals[i])) 2305e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return true; 2306e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2307e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Resolve it next. 2308e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->resolveReferencesTo(CurRec->getValue(TArgs[i])); 2309e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2310e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (DeleteArgs) 2311e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Now remove it. 2312e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->removeValue(TArgs[i]); 2313e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2314e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } else if (!CurRec->getValue(TArgs[i])->getValue()->isComplete()) { 2315e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "value not specified for template argument #"+ 2316e22b321d2276b634519165b101b02d92c2fcf5c7David Greene utostr(i) + " (" + TArgs[i]->getAsUnquotedString() 2317e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + ") of multiclassclass '" + MC.Rec.getNameInitAsString() 2318e22b321d2276b634519165b101b02d92c2fcf5c7David Greene + "'"); 2319e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2320e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2321e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return false; 2322e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2323e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2324e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greenebool TGParser::ResolveMulticlassDef(MultiClass &MC, 2325e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *CurRec, 2326e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene Record *DefProto, 2327e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene SMLoc DefmPrefixLoc) { 2328e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // If the mdef is inside a 'let' expression, add to each def. 2329e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 2330e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 2331e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 2332e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene LetStack[i][j].Bits, LetStack[i][j].Value)) 2333e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(DefmPrefixLoc, "when instantiating this defm"); 2334e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2335e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Don't create a top level definition for defm inside multiclasses, 2336e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // instead, only update the prototypes and bind the template args 2337e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // with the new created definition. 2338e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (CurMultiClass) { 2339e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = CurMultiClass->DefPrototypes.size(); 2340e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene i != e; ++i) 234122dde7e655b76f75bf11e86129410a7dcbfac3baDavid Greene if (CurMultiClass->DefPrototypes[i]->getNameInit() 234222dde7e655b76f75bf11e86129410a7dcbfac3baDavid Greene == CurRec->getNameInit()) 234322dde7e655b76f75bf11e86129410a7dcbfac3baDavid Greene return Error(DefmPrefixLoc, "defm '" + CurRec->getNameInitAsString() + 2344e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene "' already defined in this multiclass!"); 2345e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurMultiClass->DefPrototypes.push_back(CurRec); 2346e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2347e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene // Copy the template arguments for the multiclass into the new def. 2348e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TA = 2349e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurMultiClass->Rec.getTemplateArgs(); 2350e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2351e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene for (unsigned i = 0, e = TA.size(); i != e; ++i) { 2352e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene const RecordVal *RV = CurMultiClass->Rec.getValue(TA[i]); 2353e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene assert(RV && "Template arg doesn't exist?"); 2354e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene CurRec->addValue(*RV); 2355e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2356e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene } 2357e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2358e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return false; 2359e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene} 2360e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 2361f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseDefm - Parse the instantiation of a multiclass. 2362f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2363f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// DefMInst ::= DEFM ID ':' DefmSubClassRef ';' 2364f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// 2365270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopesbool TGParser::ParseDefm(MultiClass *CurMultiClass) { 2366f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner assert(Lex.getCode() == tgtok::Defm && "Unexpected token!"); 236721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2368a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene Init *DefmPrefix = 0; 2369a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene 2370df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner if (Lex.Lex() == tgtok::Id) { // eat the defm. 2371a9e07dd66dcd951900f9e360fafc1ea30edcc9cdDavid Greene DefmPrefix = ParseObjectName(CurMultiClass); 2372df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner } 2373c761f7dca2134d4d500ce7f7db4422356725ee8cMikhail Glushenkov 23741e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc DefmPrefixLoc = Lex.getLoc(); 2375df72eaef3d863be99fd45f59c59919a8c1261d05Chris Lattner if (Lex.getCode() != tgtok::colon) 2376f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("expected ':' after defm identifier"); 237721870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 23786e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Keep track of the new generated record definitions. 23796e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes std::vector<Record*> NewRecDefs; 23806e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 23816e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // This record also inherits from a regular class (non-multiclass)? 23826e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes bool InheritFromClass = false; 23836e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 2384f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // eat the colon. 2385f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); 2386f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 23871e3a8a492471f5dc3f50452af9eb9a2dfb1aeb39Chris Lattner SMLoc SubClassLoc = Lex.getLoc(); 2388f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner SubClassReference Ref = ParseSubClassReference(0, true); 23895654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 23905654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene while (1) { 23915654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Ref.Rec == 0) return true; 23925654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 23935654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // To instantiate a multiclass, we need to first get the multiclass, then 23945654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // instantiate each def contained in the multiclass with the SubClassRef 23955654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // template parameters. 23965654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene MultiClass *MC = MultiClasses[Ref.Rec->getName()]; 23975654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene assert(MC && "Didn't lookup multiclass correctly?"); 239805bce0beee87512e52428d4b80f5a8e79a949576David Greene std::vector<Init*> &TemplateVals = Ref.TemplateArgs; 23995654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24005654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // Verify that the correct number of template arguments were specified. 2401e22b321d2276b634519165b101b02d92c2fcf5c7David Greene const std::vector<Init *> &TArgs = MC->Rec.getTemplateArgs(); 24025654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (TArgs.size() < TemplateVals.size()) 24035654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene return Error(SubClassLoc, 24045654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene "more template args specified than multiclass expects"); 24055654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24065654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene // Loop over all the def's in the multiclass, instantiating each one. 24075654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene for (unsigned i = 0, e = MC->DefPrototypes.size(); i != e; ++i) { 24085654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Record *DefProto = MC->DefPrototypes[i]; 24095654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24107be867e48300861b7b1bf614eb204463533d6724David Greene Record *CurRec = InstantiateMulticlassDef(*MC, DefProto, DefmPrefix, DefmPrefixLoc); 241194f2dc90a54443ea405776d6ffa2a4e27800d3d6Jim Grosbach if (!CurRec) 241294f2dc90a54443ea405776d6ffa2a4e27800d3d6Jim Grosbach return true; 2413270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes 2414e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (ResolveMulticlassDefArgs(*MC, CurRec, DefmPrefixLoc, SubClassLoc, 2415e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene TArgs, TemplateVals, true/*Delete args*/)) 2416e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "could not instantiate def"); 2417270562b3d4c61ae1381cb1b0026bb703b46ff88fBruno Cardoso Lopes 2418e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene if (ResolveMulticlassDef(*MC, CurRec, DefProto, DefmPrefixLoc)) 2419e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene return Error(SubClassLoc, "could not instantiate def"); 24206e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24216e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes NewRecDefs.push_back(CurRec); 2422f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 24235654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 2424e499a2df4492dab21a50d74f3f687b989f910a2fDavid Greene 24255654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Lex.getCode() != tgtok::comma) break; 24265654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Lex.Lex(); // eat ','. 24275654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24285654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene SubClassLoc = Lex.getLoc(); 24296e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24306e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // A defm can inherit from regular classes (non-multiclass) as 24316e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // long as they come in the end of the inheritance list. 24326e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes InheritFromClass = (Records.getClass(Lex.getCurStrVal()) != 0); 24336e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24346e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (InheritFromClass) 24356e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes break; 24366e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24375654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Ref = ParseSubClassReference(0, true); 2438f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 24395654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene 24406e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (InheritFromClass) { 24416e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Process all the classes to inherit as if they were part of a 24426e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // regular 'def' and inherit all record values. 24436e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes SubClassReference SubClass = ParseSubClassReference(0, false); 24446e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes while (1) { 24456e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Check for error. 24466e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (SubClass.Rec == 0) return true; 24476e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24486e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Get the expanded definition prototypes and teach them about 24496e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // the record values the current class to inherit has 24506e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) { 24516e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes Record *CurRec = NewRecDefs[i]; 24526e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24536e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Add it. 24546e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (AddSubClass(CurRec, SubClass)) 24556e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes return true; 24566e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24576e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes // Process any variables on the let stack. 24586e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned i = 0, e = LetStack.size(); i != e; ++i) 24596e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes for (unsigned j = 0, e = LetStack[i].size(); j != e; ++j) 24606e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (SetValue(CurRec, LetStack[i][j].Loc, LetStack[i][j].Name, 24616e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes LetStack[i][j].Bits, LetStack[i][j].Value)) 24626e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes return true; 24636e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 24646e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 24656e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes if (Lex.getCode() != tgtok::comma) break; 24666e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes Lex.Lex(); // eat ','. 24676e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes SubClass = ParseSubClassReference(0, false); 24686e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 24696e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes } 24706e0a99a7ab6d6fd0099946c4859466f2a9cdd1e1Bruno Cardoso Lopes 2471e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes if (!CurMultiClass) 2472e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes for (unsigned i = 0, e = NewRecDefs.size(); i != e; ++i) 24730d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // See Record::setName(). This resolve step will see any new 24740d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // name for the def that might have been created when resolving 24750d886401b3ec09b0c2d267942b07702a2f0740f4David Greene // inheritance, values and arguments above. 2476e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes NewRecDefs[i]->resolveReferences(); 2477e5104ac99aae21fb8ee514f3b65eac9856399dc0Bruno Cardoso Lopes 24785654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene if (Lex.getCode() != tgtok::semi) 24795654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene return TokError("expected ';' at end of defm"); 24805654613a013b94a92f9ed65e1a2f34e0a8d9cfdfDavid Greene Lex.Lex(); 248121870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2482f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2483f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2484f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2485f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObject 2486f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= ClassInst 2487f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= DefInst 2488f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= MultiClassInst 2489f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= DefMInst 2490f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LETCommand '{' ObjectList '}' 2491f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// Object ::= LETCommand Object 2492ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseObject(MultiClass *MC) { 2493f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner switch (Lex.getCode()) { 2494d6d9dd9f5e950383b7040a864bd8e004f95bcc91Chris Lattner default: 2495d6d9dd9f5e950383b7040a864bd8e004f95bcc91Chris Lattner return TokError("Expected class, def, defm, multiclass or let definition"); 2496ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Let: return ParseTopLevelLet(MC); 2497ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Def: return ParseDef(MC); 2498cebb4ee93a0064e4a2cb1fd1da7455b01e5655cbDavid Greene case tgtok::Foreach: return ParseForeach(MC); 2499ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes case tgtok::Defm: return ParseDefm(MC); 2500f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::Class: return ParseClass(); 2501f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner case tgtok::MultiClass: return ParseMultiClass(); 2502f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2503f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2504f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2505f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ParseObjectList 2506f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner/// ObjectList :== Object* 2507ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopesbool TGParser::ParseObjectList(MultiClass *MC) { 2508f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner while (isObjectStart(Lex.getCode())) { 2509ee65db3add855bfbc4ddc7e45926d1b9bafca8a4Bruno Cardoso Lopes if (ParseObject(MC)) 2510f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return true; 2511f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner } 2512f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 2513f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2514f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2515f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattnerbool TGParser::ParseFile() { 2516f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner Lex.Lex(); // Prime the lexer. 2517f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (ParseObjectList()) return true; 251821870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2519f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner // If we have unread input at the end of the file, report it. 2520f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner if (Lex.getCode() == tgtok::Eof) 2521f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return false; 252221870411d9c1807526b613d04c2ebae5a43c263bBob Wilson 2523f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner return TokError("Unexpected input at top level"); 2524f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner} 2525f460165a4c1bf4bc762f9b3f12b9ed284b89cc99Chris Lattner 2526